MeshFields
GPU accelerated mesh-based fields
MeshField.hpp
1 #ifndef MESHFIELD_MESHFIELD_HPP
2 #define MESHFIELD_MESHFIELD_HPP
3 
4 #include "KokkosController.hpp"
5 #include "MeshField_Element.hpp"
6 #include "MeshField_Fail.hpp"
7 #include "MeshField_For.hpp"
8 #include "MeshField_MappedElement.hpp"
9 #include "MeshField_ShapeField.hpp"
10 #include "Omega_h_file.hpp" //move
11 #include "Omega_h_mesh.hpp" //move
12 #include "Omega_h_simplex.hpp" //move
13 
14 namespace {
15 
16 MeshField::MeshInfo getMeshInfo(Omega_h::Mesh &mesh) {
17  MeshField::MeshInfo meshInfo;
18  meshInfo.dim = mesh.dim();
19  meshInfo.numVtx = mesh.nverts();
20  if (mesh.dim() > 1)
21  meshInfo.numEdge = mesh.nedges();
22  if (mesh.family() == OMEGA_H_SIMPLEX) {
23  if (mesh.dim() > 1)
24  meshInfo.numTri = mesh.nfaces();
25  if (mesh.dim() == 3)
26  meshInfo.numTet = mesh.nregions();
27  } else { // hypercube
28  if (mesh.dim() > 1)
29  meshInfo.numQuad = mesh.nfaces();
30  if (mesh.dim() == 3)
31  meshInfo.numHex = mesh.nregions();
32  }
33  return meshInfo;
34 }
35 // we use functors because cannot use auto return type with KOKKOS_LAMBDAS
36 template <int dim, typename Field, typename Coords, typename CoordinateMap>
37 struct SetCoordFieldFunctor
38 {
39  SetCoordFieldFunctor(Field& field, Coords& coords, int meshDim,
40  const CoordinateMap& map) :
41  field_(field), coords_(coords), meshDim_(meshDim), map_(map) {}
42  KOKKOS_INLINE_FUNCTION
43  void operator()(const size_t vtx) const {
44  Kokkos::Array<MeshField::Real, dim> x{};
45  for (int d = 0; d < meshDim_; ++d)
46  x[d] = coords_[vtx * meshDim_ + d];
47  const auto mapped = map_(x);
48  for (int d = 0; d < meshDim_; ++d)
49  field_(vtx, 0, d, MeshField::Vertex) = mapped[d];
50  }
51  Field field_;
52  Coords coords_;
53  int meshDim_;
54  const CoordinateMap map_;
55 };
56 
57 } // anonymous namespace
58 
59 namespace MeshField {
60 
61 namespace Omegah {
62 struct LinearTriangleToVertexField {
63  Omega_h::LOs triVerts;
64  LinearTriangleToVertexField(Omega_h::Mesh &mesh)
65  : triVerts(mesh.ask_elem_verts()) {
66  if (mesh.dim() != 2 && mesh.family() != OMEGA_H_SIMPLEX) {
67  MeshField::fail(
68  "The mesh passed to %s must be 2D and simplex (triangles)\n",
69  __func__);
70  }
71  }
72 
73  static constexpr KOKKOS_FUNCTION Kokkos::Array<MeshField::Mesh_Topology, 1>
74  getTopology() {
75  return {MeshField::Triangle};
76  }
77 
78  KOKKOS_FUNCTION MeshField::ElementToDofHolderMap
79  operator()(MeshField::LO triNodeIdx, MeshField::LO triCompIdx,
80  MeshField::LO tri, MeshField::Mesh_Topology topo) const {
81  assert(topo == MeshField::Triangle);
82  const auto triDim = 2;
83  const auto vtxDim = 0;
84  const auto ignored = -1;
85  const auto localVtxIdx =
86  Omega_h::simplex_down_template(triDim, vtxDim, triNodeIdx, ignored);
87  const auto triToVtxDegree = Omega_h::simplex_degree(triDim, vtxDim);
88  const MeshField::LO vtx = triVerts[(tri * triToVtxDegree) + localVtxIdx];
89  return {0, triCompIdx, vtx, MeshField::Vertex}; // node, comp, ent, topo
90  }
91 };
92 struct LinearTetrahedronToVertexField {
93  Omega_h::LOs tetVerts;
94  LinearTetrahedronToVertexField(Omega_h::Mesh &mesh)
95  : tetVerts(mesh.ask_elem_verts()) {
96  if (mesh.dim() != 3 && mesh.family() != OMEGA_H_SIMPLEX) {
97  MeshField::fail(
98  "The mesh passed to %s must be 3D and simplex (tetrahedron)\n",
99  __func__);
100  }
101  }
102  static constexpr KOKKOS_FUNCTION Kokkos::Array<MeshField::Mesh_Topology, 1>
103  getTopology() {
104  return {MeshField::Tetrahedron};
105  }
106 
107  KOKKOS_FUNCTION MeshField::ElementToDofHolderMap
108  operator()(MeshField::LO tetNodeIdx, MeshField::LO tetCompIdx,
109  MeshField::LO tet, MeshField::Mesh_Topology topo) const {
110  assert(topo == MeshField::Tetrahedron);
111  const auto tetDim = 3;
112  const auto vtxDim = 0;
113  const auto ignored = -1;
114  const auto localVtxIdx = Omega_h::simplex_down_template(tetDim, vtxDim, tetNodeIdx, ignored);
115  const auto tetToVtxDegree = Omega_h::simplex_degree(tetDim, vtxDim);
116  const MeshField::LO vtx = tetVerts[(tet * tetToVtxDegree) + localVtxIdx];
117  return {0, tetCompIdx, vtx, MeshField::Vertex}; // node, comp, ent, topo
118  }
119 };
122  Omega_h::LOs triVerts;
123  Omega_h::LOs triEdges;
124  QuadraticTriangleToField(Omega_h::Mesh &mesh)
125  : triVerts(mesh.ask_elem_verts()),
126  triEdges(mesh.ask_down(mesh.dim(), 1).ab2b) {
127  if (mesh.dim() != 2 && mesh.family() != OMEGA_H_SIMPLEX) {
128  MeshField::fail(
129  "The mesh passed to %s must be 2D and simplex (triangles)\n",
130  __func__);
131  }
132  }
133 
134  static constexpr KOKKOS_FUNCTION Kokkos::Array<MeshField::Mesh_Topology, 1>
135  getTopology() {
136  return {MeshField::Triangle};
137  }
138 
139  KOKKOS_FUNCTION MeshField::ElementToDofHolderMap
140  operator()(MeshField::LO triNodeIdx, MeshField::LO triCompIdx,
141  MeshField::LO tri, MeshField::Mesh_Topology topo) const {
142  assert(topo == MeshField::Triangle);
143  // Omega_h has no concept of nodes so we can define the map from
144  // triNodeIdx to the dof holder index
145  const MeshField::LO triNode2DofHolder[6] = {
146  /*vertices*/ 0, 1, 2,
147  /*edges*/ 0, 1, 2};
148  const MeshField::Mesh_Topology triNode2DofHolderTopo[6] = {
149  /*vertices*/
150  MeshField::Vertex, MeshField::Vertex, MeshField::Vertex,
151  /*edges*/
152  MeshField::Edge, MeshField::Edge, MeshField::Edge};
153  const auto dofHolderIdx = triNode2DofHolder[triNodeIdx];
154  const auto dofHolderTopo = triNode2DofHolderTopo[triNodeIdx];
155  // Given the topo index and type find the Omega_h vertex or edge index that
156  // bounds the triangle
157  Omega_h::LO osh_ent;
158  if (dofHolderTopo == MeshField::Vertex) {
159  const auto triDim = 2;
160  const auto vtxDim = 0;
161  const auto ignored = -1;
162  const auto localVtxIdx = Omega_h::simplex_down_template(
163  triDim, vtxDim, dofHolderIdx, ignored);
164  const auto triToVtxDegree = Omega_h::simplex_degree(triDim, vtxDim);
165  osh_ent = triVerts[(tri * triToVtxDegree) + localVtxIdx];
166  } else if (dofHolderTopo == MeshField::Edge) {
167  const auto triDim = 2;
168  const auto edgeDim = 1;
169  const auto triToEdgeDegree = Omega_h::simplex_degree(triDim, edgeDim);
170  // passing dofHolderIdx as Omega_h_simplex.hpp does not provide
171  // a function that maps a triangle and edge index to a 'canonical' edge
172  // index. This may need to be revisited...
173  osh_ent = triEdges[(tri * triToEdgeDegree) + dofHolderIdx];
174  } else {
175  assert(false);
176  }
177  return {0, triCompIdx, osh_ent, dofHolderTopo};
178  }
179 };
181 
183  Omega_h::LOs tetVerts;
184  Omega_h::LOs tetEdges;
185  QuadraticTetrahedronToField(Omega_h::Mesh &mesh)
186  : tetVerts(mesh.ask_elem_verts()),
187  tetEdges(mesh.ask_down(mesh.dim(), 1).ab2b) {
188  if (mesh.dim() != 3 && mesh.family() != OMEGA_H_SIMPLEX) {
189  MeshField::fail(
190  "The mesh passed to %s must be 3D and simplex (tetrahedron)",
191  __func__);
192  }
193  }
194 
195  static constexpr KOKKOS_FUNCTION Kokkos::Array<MeshField::Mesh_Topology, 1>
196  getTopology() {
197  return {MeshField::Tetrahedron};
198  }
199 
200  KOKKOS_FUNCTION MeshField::ElementToDofHolderMap
201  operator()(MeshField::LO tetNodeIdx, MeshField::LO tetCompIdx,
202  MeshField::LO tet, MeshField::Mesh_Topology topo) const {
203  assert(topo == MeshField::Tetrahedron);
204  const MeshField::LO tetNode2DofHolder[10] = {0, 1, 2, 3, 0, 2, 3, 1, 5, 4};
205  const MeshField::Mesh_Topology tetNode2DofHolderTopo[10] = {
206  MeshField::Vertex, MeshField::Vertex, MeshField::Vertex,
207  MeshField::Vertex, MeshField::Edge, MeshField::Edge,
208  MeshField::Edge, MeshField::Edge, MeshField::Edge,
209  MeshField::Edge};
210  const auto dofHolderIdx = tetNode2DofHolder[tetNodeIdx];
211  const auto dofHolderTopo = tetNode2DofHolderTopo[tetNodeIdx];
212  Omega_h::LO osh_ent;
213  if (dofHolderTopo == MeshField::Vertex) {
214  const auto tetDim = 3;
215  const auto vtxDim = 0;
216  const auto ignored = -1;
217  const auto localVtxIdx = Omega_h::simplex_down_template(
218  tetDim, vtxDim, dofHolderIdx, ignored);
219  const auto tetToVtxDegree = Omega_h::simplex_degree(tetDim, vtxDim);
220  osh_ent = tetVerts[(tet * tetToVtxDegree) + localVtxIdx];
221  } else if (dofHolderTopo == MeshField::Edge) {
222  const auto tetDim = 3;
223  const auto edgeDim = 1;
224  const auto tetToEdgeDegree = Omega_h::simplex_degree(tetDim, edgeDim);
225  osh_ent = tetEdges[(tet * tetToEdgeDegree) + dofHolderIdx];
226  } else {
227  assert(false);
228  }
229  return {0, tetCompIdx, osh_ent, dofHolderTopo};
230  }
231 };
232 
234 template <int ShapeOrder> auto getTriangleElement(Omega_h::Mesh &mesh) {
235  static_assert(ShapeOrder == 1 || ShapeOrder == 2);
236  if constexpr (ShapeOrder == 1) {
237  struct result {
239  LinearTriangleToVertexField map;
240  };
241  return result{MeshField::LinearTriangleShape(),
242  LinearTriangleToVertexField(mesh)};
243  } else if constexpr (ShapeOrder == 2) {
244  struct result {
246  QuadraticTriangleToField map;
247  };
248  return result{MeshField::QuadraticTriangleShape(),
249  QuadraticTriangleToField(mesh)};
250  }
251 }
253 template <int ShapeOrder> auto getTetrahedronElement(Omega_h::Mesh &mesh) {
254  static_assert(ShapeOrder == 1 || ShapeOrder == 2);
255  if constexpr (ShapeOrder == 1) {
256  struct result {
258  LinearTetrahedronToVertexField map;
259  };
260  return result{MeshField::LinearTetrahedronShape(),
261  LinearTetrahedronToVertexField(mesh)};
262  } else if constexpr (ShapeOrder == 2) {
263  struct result {
265  QuadraticTetrahedronToField map;
266  };
267  return result{MeshField::QuadraticTetrahedronShape(),
268  QuadraticTetrahedronToField(mesh)};
269  }
270 }
271 
272 } // namespace Omegah
273 
278  template <typename Point>
279  KOKKOS_INLINE_FUNCTION Point operator()(Point const &x) const {
280  return x;
281  }
282 };
283 
284 template <typename ExecutionSpace, size_t dim,
285  template <typename...> typename Controller =
286  MeshField::KokkosController>
287 class OmegahMeshField {
288 private:
289  Omega_h::Mesh &mesh;
290  const MeshField::MeshInfo meshInfo;
291  using CoordField =
292  decltype(MeshField::CreateCoordinateField<ExecutionSpace, Controller,
293  dim>(MeshField::MeshInfo()));
294  CoordField coordField;
295 
296 public:
297  OmegahMeshField(Omega_h::Mesh &mesh_in)
298  : mesh(mesh_in), meshInfo(getMeshInfo(mesh)),
299  coordField(CreateLagrangeCoordinateField<1>()) {
300  static_assert(dim == 1 || dim == 2 || dim == 3);
301  }
302 
303  template <typename DataType, size_t order, size_t numComp>
304  // Ordering of field indexing changed to 'entity, node, component'
305  auto CreateLagrangeField() const {
306  return MeshField::CreateLagrangeField<ExecutionSpace, Controller, DataType,
307  order, dim, numComp>(meshInfo);
308  }
309 
325  template <size_t order, typename CoordinateMap = IdentityCoordinateMap>
326  auto CreateLagrangeCoordinateField(
327  const CoordinateMap &map = CoordinateMap()) const {
328  auto fieldWithCtrlr = CreateLagrangeField<Real, order, dim>();
329  auto field = fieldWithCtrlr.field;
330  // note order 1 is called in the constructor to initialize the coordinate field
331  // it must be called before higher order coordinate fields are constructed
332  if constexpr (order == 1) {
333  const auto meshDim = meshInfo.dim;
334  const auto coords = mesh.coords();
335  MeshField::parallel_for<decltype(fieldWithCtrlr.ctrlr)>(
336  {0}, {meshInfo.numVtx},
337  SetCoordFieldFunctor<dim, decltype(field), decltype(coords),
338  CoordinateMap>(field, coords, meshDim, map),
339  "setCoordField");
340  } else {
341  const auto linear = CreateLagrangeElement(coordField.field);
342  auto target = CreateLagrangeElement(field, linear);
343  MeshField::setDofByInterpolation(target, map);
344  }
345  return fieldWithCtrlr;
346  }
347 
348  auto getCoordField() { return coordField; }
349 
358  template <typename Field>
359  auto CreateLagrangeElement(const Field &field) const {
360  static_assert(dim == 2 || dim == 3,
361  "CreateLagrangeElement supports triangles and tetrahedra");
362  constexpr int order = Field::Order;
363  if constexpr (dim == 2) {
364  const auto [shp, map] = Omegah::getTriangleElement<order>(mesh);
365  return MeshField::FieldElement(meshInfo.numTri, field, shp, map);
366  } else {
367  const auto [shp, map] = Omegah::getTetrahedronElement<order>(mesh);
368  return MeshField::FieldElement(meshInfo.numTet, field, shp, map);
369  }
370  }
371 
379  template <typename ShapeField, typename GeomElem>
380  auto CreateLagrangeElement(const ShapeField &field,
381  const GeomElem &geomElem) const {
382  return MeshField::MappedElement(CreateLagrangeElement(field), geomElem);
383  }
384 
385  // FIXME support 2d and 3d and fields with order>1
386  template <typename Field> void writeVtk(Field &field) const {
387  using FieldDataType = typename decltype(field.vtxField)::BaseType;
388  // HACK assumes there is a vertex field.. in the Field Mixin object
389  auto field_view = field.vtxField.serialize();
390  Omega_h::Write<FieldDataType> field_write(field_view);
391  mesh.add_tag(0, "field", 1, Omega_h::read(field_write), false,
392  Omega_h::ArrayType::VectorND);
393  Omega_h::vtk::write_parallel("foo.vtk", &mesh, mesh.dim());
394  }
395 
396  template <typename ViewType = Kokkos::View<MeshField::LO *>>
397  ViewType createOffsets(size_t numTri, size_t numPtsPerElem) const {
398  ViewType offsets("offsets", numTri + 1);
399  Kokkos::parallel_for(
400  "setOffsets", numTri,
401  KOKKOS_LAMBDA(int i) { offsets(i) = i * numPtsPerElem; });
402  Kokkos::deep_copy(Kokkos::subview(offsets, offsets.size() - 1),
403  numTri * numPtsPerElem);
404  return offsets;
405  }
406 
407  // evaluate a field at the specified local coordinate for each triangle
408  template <typename ViewType, typename ShapeField>
409  auto triangleLocalPointEval(const ViewType &localCoords, size_t NumPtsPerElem,
410  const ShapeField &field) const {
411  auto offsets = createOffsets(meshInfo.numTri, NumPtsPerElem);
412  auto eval = triangleLocalPointEval<ViewType, ShapeField>(localCoords,
413  offsets, field);
414  return eval;
415  }
416 
417  // evaluate a field at the specified local coordinates for each triangle
418  template <typename ViewType, typename ShapeField>
419  auto triangleLocalPointEval(const ViewType &localCoords,
420  Kokkos::View<LO *> offsets,
421  const ShapeField &field) const {
422  const auto MeshDim = 2;
423  if (mesh.dim() != MeshDim) {
424  MeshField::fail("input mesh must be 2d\n");
425  }
426  const auto ShapeOrder = ShapeField::Order;
427  if (ShapeOrder != 1 && ShapeOrder != 2) {
428  MeshField::fail("input field order must be 1 or 2\n");
429  }
430 
431  const auto [shp, map] = Omegah::getTriangleElement<ShapeOrder>(mesh);
432 
433  MeshField::FieldElement<ShapeField, decltype(shp), decltype(map)> f(
434  meshInfo.numTri, field, shp, map);
435  auto eval = MeshField::evaluate(f, localCoords, offsets);
436  return eval;
437  }
438 
439  template <typename ViewType, typename ShapeField>
440  auto tetrahedronLocalPointEval(const ViewType &localCoords,
441  size_t NumPtsPerElem,
442  const ShapeField &field) const {
443  auto offsets = createOffsets(meshInfo.numTet, NumPtsPerElem);
444  auto eval = tetrahedronLocalPointEval(localCoords, offsets, field);
445  return eval;
446  }
447 
448  template <typename ViewType, typename ShapeField>
449  auto tetrahedronLocalPointEval(const ViewType &localCoords,
450  Kokkos::View<LO *> offsets,
451  const ShapeField &field) const {
452  const auto MeshDim = 3;
453  if (mesh.dim() != MeshDim) {
454  MeshField::fail("input mesh must be 3d\n");
455  }
456  const auto ShapeOrder = ShapeField::Order;
457  if (ShapeOrder != 1 && ShapeOrder != 2) {
458  MeshField::fail("input field order must be 1 or 2\n");
459  }
460  const auto [shp, map] = Omegah::getTetrahedronElement<ShapeOrder>(mesh);
461  MeshField::FieldElement<ShapeField, decltype(shp), decltype(map)> f(
462  meshInfo.numTet, field, shp, map);
463  auto eval = MeshField::evaluate(f, localCoords, offsets);
464  return eval;
465  }
466 };
467 
468 } // namespace MeshField
469 
470 #endif
Supports mapping between mesh (i.e., Omega_h) ordering and MeshFields ordering.
Supports the evaluation of a field, and other per-element operations, given the definition of the ele...
the CoordinateMap that leaves the mesh geometry unchanged
Definition: MeshField.hpp:277
Linear (P1) shape functions for 2D triangular elements.
On-process mesh metadata.
Quadratic (P2) shape functions for 3D tetrahedral elements.
Quadratic (P2) shape functions for 2D triangular elements.