dune-mmesh 1.4.1-git
Loading...
Searching...
No Matches
utility.hh
Go to the documentation of this file.
1#ifndef DUNE_PYTHON_MMESH_UTILITY
2#define DUNE_PYTHON_MMESH_UTILITY
3
4#if HAVE_DUNE_FEM
5
10#include <dune/fempy/py/grid/function.hh>
11#include <dune/fempy/py/grid/gridpart.hh>
12#include <dune/python/common/typeregistry.hh>
13
14namespace Dune {
15
16namespace Fem {
17
19// Interface indicator //
21
22template <class BulkGV>
23struct InterfaceIndicator
24 : public BindableGridFunctionWithSpace<FemPy::GridPart<BulkGV>,
25 Dune::FieldVector<double, 1>> {
26 using GridView = BulkGV;
27 using GridPartType = FemPy::GridPart<BulkGV>;
28 using Base =
29 BindableGridFunctionWithSpace<GridPartType, Dune::FieldVector<double, 1>>;
30 using SideGeometry = typename GridPartType::GridType::Intersection::
31 LocalGeometry::Implementation;
32 using RangeType = typename Base::RangeType;
33 static constexpr bool scalar = true;
34
35 InterfaceIndicator(const BulkGV &bulkGV)
36 : Base(FemPy::gridPart<BulkGV>(bulkGV), "interfaceindicator", 0),
37 onInterface_(false) {}
38
39 void bind(const typename Base::EntityType &entity) { Base::bind(entity); }
40
41 void bind(const typename BulkGV::Intersection &intersection,
42 IntersectionSide side) {
43 if (Base::gridPart().grid().isInterface(intersection))
44 onInterface_ = true;
45 else
46 onInterface_ = false;
47 }
48
49 template <class Point>
50 void evaluate(const Point &x, typename Base::RangeType &ret) const {
51 if (onInterface_)
52 ret = RangeType(1);
53 else
54 ret = RangeType(0);
55 }
56
57 template <class Point>
58 void jacobian(const Point &x, typename Base::JacobianRangeType &ret) const {
59 ret = typename Base::JacobianRangeType(0);
60 }
61
62 template <class Point>
63 void hessian(const Point &x, typename Base::HessianRangeType &ret) const {
64 ret = typename Base::HessianRangeType(0);
65 }
66
67 private:
68 bool onInterface_;
69};
70
71template <class BulkGV>
72inline static void registerInterfaceIndicator(
73 pybind11::module module, pybind11::class_<InterfaceIndicator<BulkGV>> cls) {
74 using pybind11::operator""_a;
75
76 cls.def(pybind11::init([](const BulkGV &bulkGV) {
77 return InterfaceIndicator<BulkGV>(bulkGV);
78 }),
79 "bulkGV"_a, pybind11::keep_alive<1, 2>());
80
81 cls.def_property_readonly(
82 "scalar", [](InterfaceIndicator<BulkGV> &self) { return true; });
83
84 Dune::FemPy::registerGridFunction(module, cls);
85}
86
88// Normals //
90
91template <class InterfaceGV>
92struct Normals : public BindableGridFunctionWithSpace<
93 FemPy::GridPart<InterfaceGV>,
94 Dune::FieldVector<double, InterfaceGV::dimensionworld>> {
95 static constexpr int dimw = InterfaceGV::dimensionworld;
96 using GridView = InterfaceGV;
97 using GridPartType = FemPy::GridPart<InterfaceGV>;
98 using Base = BindableGridFunctionWithSpace<GridPartType,
100 using RangeType = typename Base::RangeType;
101 static constexpr bool scalar = false;
102
103 Normals(const InterfaceGV &interfaceGV)
104 : Base(FemPy::gridPart<InterfaceGV>(interfaceGV), "normals", 0) {}
105
106 void bind(const typename Base::EntityType &entity) {
107 Base::bind(entity);
108 normal_ = Base::gridPart()
109 .grid()
110 .getMMesh()
111 .asIntersection(entity)
112 .centerUnitOuterNormal();
113 }
114
115 public:
116 template <class Point>
117 void evaluate(const Point &x, RangeType &ret) const {
118 ret = normal_;
119 }
120
121 template <class Point>
122 void jacobian(const Point &x, typename Base::JacobianRangeType &ret) const {
123 ret = typename Base::JacobianRangeType(0);
124 }
125
126 template <class Point>
127 void hessian(const Point &x, typename Base::HessianRangeType &ret) const {
128 ret = typename Base::HessianRangeType(0);
129 }
130
131 private:
133};
134
135template <class InterfaceGV>
136inline static void registerNormals(pybind11::module module,
137 pybind11::class_<Normals<InterfaceGV>> cls) {
138 using pybind11::operator""_a;
139
140 cls.def(pybind11::init([](const InterfaceGV &interfaceGV) {
141 return Normals<InterfaceGV>(interfaceGV);
142 }),
143 "interfaceGV"_a, pybind11::keep_alive<1, 2>());
144
145 cls.def_property_readonly("scalar",
146 [](Normals<InterfaceGV> &self) { return false; });
147
148 Dune::FemPy::registerGridFunction(module, cls);
149}
150
152// DistanceFunction //
154
155template <class GV>
156struct DistanceFunction
157 : public BindableGridFunctionWithSpace<FemPy::GridPart<GV>,
158 Dune::FieldVector<double, 1>> {
159 using GridView = GV;
160 static constexpr int dim = GridView::dimensionworld;
161 using GridPartType = FemPy::GridPart<GridView>;
162 using Base =
163 BindableGridFunctionWithSpace<GridPartType, Dune::FieldVector<double, 1>>;
164 using RangeType = typename Base::RangeType;
165 static constexpr bool scalar = true;
166
167 DistanceFunction(const GridView &gridView)
168 : Base(FemPy::gridPart<GridView>(gridView), "distance", 0),
169 interpolation_(GeometryTypes::simplex(dim),
170 std::vector<Dune::FieldVector<double, 1>>()) {}
171
172 void bind(const typename Base::EntityType &entity) {
173 Base::bind(entity);
174
175 const auto &distance = this->gridPart().grid().indicator().distance();
176
178 distances.resize(dim + 1);
179 for (std::size_t i = 0; i < entity.subEntities(dim); ++i) {
180 const auto &vertex = entity.template subEntity<dim>(i);
181 distances[i] = distance(vertex);
182 }
183
184 interpolation_ =
186 }
187
188 public:
189 template <class Point>
190 void evaluate(const Point &x, RangeType &ret) const {
191 auto xLocal = Dune::Fem::coordinate(x);
192 ret = interpolation_.global(xLocal);
193 }
194
195 template <class Point>
196 void jacobian(const Point &x, typename Base::JacobianRangeType &ret) const {
197 auto xLocal = Dune::Fem::coordinate(x);
198 auto jacT = interpolation_.jacobianTransposed(xLocal);
199 for (std::size_t i = 0; i < dim; ++i) ret[0][i] = jacT[i][0];
200 }
201
202 template <class Point>
203 void hessian(const Point &x, typename Base::HessianRangeType &ret) const {
204 ret = typename Base::HessianRangeType(0);
205 }
206
207 private:
209};
210
211template <class GridView>
212inline static void registerDistanceFunction(
213 pybind11::module module, pybind11::class_<DistanceFunction<GridView>> cls) {
214 using pybind11::operator""_a;
215
216 cls.def(pybind11::init([](const GridView &gridView) {
217 return DistanceFunction<GridView>(gridView);
218 }),
219 "gridView"_a, pybind11::keep_alive<1, 2>());
220
221 cls.def_property_readonly(
222 "scalar", [](DistanceFunction<GridView> &self) { return true; });
223
224 Dune::FemPy::registerGridFunction(module, cls);
225}
226
227} // namespace Fem
228
229} // namespace Dune
230
231#endif // HAVE_DUNE_FEM
232
233#endif // DUNE_PYTHON_MMESH_UTILITY
STL namespace.
size_type dim() const
const Grid & grid() const
GridView(const Implementation &imp)
const GridPartType & gridPart() const
void evaluate(const DomainType &global, RangeType &result) const
AnalyticalJacobianType & jacobian()
const EntityType & entity() const
AnalyticalHessianType & hessian()
static const Point & coordinate(const Point &x)
T bind(T... args)
T distance(T... args)
T resize(T... args)