diff --git a/nufi/cells.h b/nufi/cells.h new file mode 100644 index 0000000..76abf15 --- /dev/null +++ b/nufi/cells.h @@ -0,0 +1,82 @@ +#ifndef CELLS_H +#define CELLS_H + +#include +#include +#include +#include +#include +#include + +using namespace dealii; + +template struct CellInfo { + typename DoFHandler::active_cell_iterator cell; + + Point lower; + Point upper; + double h; +}; + +template class CellLocator { +public: + using CellIterator = typename DoFHandler::active_cell_iterator; + + void rebuild(const DoFHandler &dof_handler, + const Triangulation &triangulation); + + CellIterator locate(const Point &p) const; + +private: + std::vector> cells; +}; + +template +void CellLocator::rebuild(const DoFHandler &dof_handler, + const Triangulation &triangulation) { + + cells.clear(); + cells.reserve(triangulation.n_active_cells()); + + for (const auto &cell : dof_handler.active_cell_iterators()) { + CellInfo info; + + info.cell = cell; + info.lower = cell->vertex(0); + info.upper = cell->vertex(GeometryInfo::vertices_per_cell - 1); + + info.h = info.upper[0] - info.lower[0]; + + cells.push_back(info); + } +} + +template +typename DoFHandler::active_cell_iterator +CellLocator::locate(const Point &p) const { + static_assert(dim == 1, + "Current CellLocator implementation only supports 1D."); + + AssertThrow(!cells.empty(), + ExcMessage("CellLocator::rebuild() has not been called.")); + + const double x = p[0]; + + // const double x_min = cells.front().lower[0]; + // const double x_max = cells.back().uppper[0]; + // const double L = x_max - x_min; + + // binary search loop + auto it = std::upper_bound(cells.begin(), cells.end(), x, + [](double value, const CellInfo &cell) { + return value < cell.lower[0]; + }); + if (it == cells.begin()) + it = cells.begin(); + else + --it; + + return it->cell; +} + +#endif // !CELLS_H diff --git a/nufi/poisson_problem.h b/nufi/poisson_problem.h index fb26183..3903126 100644 --- a/nufi/poisson_problem.h +++ b/nufi/poisson_problem.h @@ -50,6 +50,7 @@ #include #include +#include "nufi/cells.h" #include "nufi/parameters.h" using namespace dealii; @@ -227,6 +228,16 @@ std::vector PoissonProblem::eval_vector_grad( return values; } +// template +// std::vector eval_point_grad(const Mapping &mapping, +// const DoFHandler &dof_handler, +// const Vector &solution, +// const Point &point) { +// Tensor Ex = VectorTools::point_gradient(mapping, dof_handler, solution, +// points); +// return Ex[0]; +// } +// // template // std::vector eval_vector_grad(const Mapping &mapping, // const DoFHandler &dof_handler,