dune-mmesh (1.4)

distance.hh
1#ifndef DUNE_PYTHON_MMESH_DISTANCE
2#define DUNE_PYTHON_MMESH_DISTANCE
3
4#if HAVE_DUNE_FEM
5
6#include <dune/common/exceptions.hh>
7#include <dune/geometry/multilineargeometry.hh>
8#include <dune/python/common/typeregistry.hh>
9#include <dune/fem/common/intersectionside.hh>
10#include <dune/fem/function/localfunction/const.hh>
11#include <dune/fem/function/localfunction/bindable.hh>
12#include <dune/fempy/py/grid/gridpart.hh>
13#include <dune/fempy/py/grid/function.hh>
14
15namespace Dune
16{
17
18 namespace Fem
19 {
20
22 // Distance //
24
25 template <class GV>
26 struct Distance
27 : public BindableGridFunctionWithSpace<FemPy::GridPart<GV>, Dune::FieldVector<double, 1>>
28 {
29 using GridView = GV;
30 static constexpr int dim = GridView::dimensionworld;
31 using GridPartType = FemPy::GridPart<GridView>;
32 using Base = BindableGridFunctionWithSpace<GridPartType, Dune::FieldVector<double, 1>>;
33 using RangeType = typename Base::RangeType;
34 static constexpr bool scalar = true;
35
36 Distance(const GridView &gridView)
37 : Base(FemPy::gridPart<GridView>(gridView), "distance", 0),
38 interpolation_(GeometryTypes::simplex(dim), std::vector<Dune::FieldVector<double, 1>>())
39 {}
40
41 void bind(const typename Base::EntityType &entity)
42 {
43 Base::bind(entity);
44
45 const auto& distance = this->gridPart().grid().indicator().distance();
46
47 std::vector<Dune::FieldVector<double, 1>> distances;
48 distances.resize(dim+1);
49 for ( std::size_t i = 0; i < entity.subEntities( dim ); ++i )
50 {
51 const auto& vertex = entity.template subEntity<dim>( i );
52 distances[i] = distance(vertex);
53 }
54
55 interpolation_ = Dune::MultiLinearGeometry<double, dim, 1> (entity.type(), distances);
56 }
57
58 public:
59 template <class Point>
60 void evaluate(const Point &x, RangeType &ret) const
61 {
62 auto xLocal = Dune::Fem::coordinate(x);
63 ret = interpolation_.global(xLocal);
64 }
65
66 template <class Point>
67 void jacobian(const Point &x, typename Base::JacobianRangeType &ret) const
68 {
69 const auto geo = this->entity().geometry();
70
71 Dune::FieldVector<double, dim> p;
72 RangeType dt0;
73 auto t0 = geo.local(p);
74 evaluate(t0, dt0);
75
76 RangeType dt;
77 for (std::size_t i = 0; i < dim; ++i)
78 {
79 p[i] = 1.;
80 auto t = geo.local(p);
81 p[i] = 0.;
82 evaluate(t, dt);
83 ret[0][i] = dt - dt0;
84 }
85 }
86
87 template <class Point>
88 void hessian(const Point &x, typename Base::HessianRangeType &ret) const
89 {
90 ret = typename Base::HessianRangeType(0);
91 }
92
93 private:
94 Dune::MultiLinearGeometry<double, dim, 1> interpolation_;
95 };
96
97 template< class GridView >
98 inline static void registerDistance(pybind11::module module, pybind11::class_< Distance<GridView> > cls)
99 {
100 using pybind11::operator""_a;
101
102 cls.def( pybind11::init( [] ( const GridView &gridView ) {
103 return Distance<GridView> ( gridView );
104 } ), "gridView"_a, pybind11::keep_alive< 1, 2 >() );
105
106 cls.def_property_readonly( "scalar", [] ( Distance<GridView> &self ) { return true; } );
107
108 Dune::FemPy::registerGridFunction( module, cls );
109 }
110
111 } // namespace Fem
112
113} // namespace Dune
114
115#endif // HAVE_DUNE_FEM
116
117#endif // DUNE_PYTHON_MMESH_DISTANCE
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Apr 6, 22:49, 2025)