int RectangularMesh::find_element(const Point2 &point,
                                  bool throw_exception) const
{
  const double px = point.x(); // coordinates of the point of interest
  const double pz = point.z();

  const double x0 = _min_coord.x(); // limits of the rect mesh
  const double x1 = _max_coord.x();
  const double z0 = _min_coord.z();
  const double z1 = _max_coord.z();

  // check that the point is within the mesh
  const double tol = FIND_CELL_TOLERANCE;
  if (px < x0 - tol || px > x1 + tol ||
      pz < z0 - tol || pz > z1 + tol)
  {
    if (throw_exception)
      require(false, "The given point " + d2s(point) + " doesn't belong "
              "to the rectangular mesh");

    return -1; // to show that the point in not here
  }

  // since the elements of the rectangular mesh are numerated in the following
  // way:
  // -----------
  // | 0  | 1  |
  // -----------
  // | 2  | 3  |
  // -----------
  // we can simplify the search of the element containing the given point:

  const double hx = (x1 - x0) / _n_elements_x;
  const double hz = (z1 - z0) / _n_elements_z;

  const int nx = std::min(static_cast<int>((px-x0)/hx), _n_elements_x-1);
  const int nz = std::min(static_cast<int>((pz-z0)/hz), _n_elements_z-1);

  if (nx < 0 || nx >= _n_elements_x ||
      nz < 0 || nz >= _n_elements_z)
  {
    if (throw_exception)
      require(false, "The rectangular element for the point " + d2s(point) +
              " wasn't found");

    return -1; // to show that the point in not here
  }

  return (nz*_n_elements_x + nx);
}