MeshFields
GPU accelerated mesh-based fields
MeshField_Element.hpp
1 #ifndef MESHFIELD_ELEMENT_H
2 #define MESHFIELD_ELEMENT_H
3 
4 #include <Kokkos_Core.hpp>
5 #include <MeshField_Defines.hpp>
6 #include <MeshField_Fail.hpp>
7 #include <MeshField_For.hpp>
8 #include <MeshField_Shape.hpp>
9 #include <MeshField_Utility.hpp> // getLastValue
10 #include <iostream>
11 #include <type_traits> // has_static_size helper
12 
13 namespace {
14 Kokkos::View<MeshField::LO *> getOffsets(MeshField::LO numItems,
15  MeshField::LO numEntriesPerItem) {
16  Kokkos::View<MeshField::LO *> offsets("offsets", numItems + 1);
17  auto first = Kokkos::subview(offsets, 0);
18  Kokkos::deep_copy(first, 0); // write 0 to the first item
19  Kokkos::parallel_for(
20  numItems, KOKKOS_LAMBDA(const int i) {
21  offsets(i + 1) = (i + 1) * numEntriesPerItem;
22  });
23  return offsets;
24 }
25 
26 // chatgpt prompt 2/20/2025:
27 // c++ static assert that checks that a type
28 // provides a function named size
29 template <typename T> class has_size_method {
30 private:
31  template <typename U>
32  static auto test(int) -> decltype(std::declval<U>().size(), std::true_type());
33  template <typename> static std::false_type test(...);
34 
35 public:
36  static constexpr bool value = decltype(test<T>(0))::value;
37 };
38 
39 template <typename T> class has_static_rank {
40 private:
41  template <typename U>
42  static auto test(int) -> decltype(U::rank(), std::true_type());
43  template <typename> static std::false_type test(...);
44 
45 public:
46  static constexpr bool value = decltype(test<T>(0))::value;
47 };
48 
49 template <typename T> class has_extent_method {
50 private:
51  template <typename U>
52  static auto
53  test(int) -> decltype(std::declval<U>().extent(std::declval<std::size_t>()),
54  std::true_type());
55  template <typename> static std::false_type test(...);
56 
57 public:
58  static constexpr bool value = decltype(test<T>(0))::value;
59 };
60 
61 // FIXME - addTensorProduct(...) likely needs performance improvements
63 template <typename VecA, typename VecB, typename Matrix>
64 KOKKOS_INLINE_FUNCTION auto addTensorProduct(VecA const &a, VecB const &b,
65  Matrix &A) {
66  static_assert(has_size_method<VecA>::value,
67  "VecA must have a size() method.");
68  static_assert(has_size_method<VecB>::value,
69  "VecB must have a size() method.");
70  static_assert(has_static_rank<VecA>::value,
71  "VecA must have a static rank() method.");
72  static_assert(has_static_rank<VecB>::value,
73  "VecB must have a static rank() method.");
74  static_assert(
75  std::is_same_v<typename VecA::value_type, typename VecB::value_type>);
76  static_assert(VecA::rank() == 1);
77  static_assert(VecB::rank() == 1);
78  static_assert(has_extent_method<Matrix>::value,
79  "Matrix must have an extent(size_t) method.");
80  const auto M = a.size();
81  const auto N = b.size();
82  for (std::size_t i = 0; i < M; ++i) {
83  for (std::size_t j = 0; j < N; ++j) {
84  A(i, j) += b[j] * a[i];
85  }
86  }
87 }
88 } // namespace
89 
90 namespace MeshField {
91 
92 
93 //
94 
105  LO node;
106 
112 
116  LO entity;
117 
121  Mesh_Topology topo;
122 };
123 
147 template <typename FieldAccessor, typename ShapeType,
148  typename ElementDofHolderAccessor>
149 struct FieldElement {
150  const size_t numMeshEnts;
151  const FieldAccessor field;
152  ShapeType shapeFn;
153  ElementDofHolderAccessor elm2dof;
154 
155  static const size_t MeshEntDim = ShapeType::meshEntDim;
156  FieldElement(size_t numMeshEntsIn, const FieldAccessor &fieldIn,
157  const ShapeType shapeFnIn,
158  const ElementDofHolderAccessor elm2dofIn)
159  : numMeshEnts(numMeshEntsIn), field(fieldIn), shapeFn(shapeFnIn),
160  elm2dof(elm2dofIn) {}
161 
163  static constexpr KOKKOS_FUNCTION auto getTopology() {
164  return ElementDofHolderAccessor::getTopology();
165  }
166 
167  /* general template for baseType which simply sets type
168  */
169  template <typename T> struct baseType {
170  using type = T;
171  };
172  /* template specialization to recursively strip type to get base type
173  * Example: int[5][6] => int[6] => int
174  */
175  template <typename T, size_t N> struct baseType<T[N]> {
176  using type = typename baseType<T>::type;
177  };
178  using ValArray =
179  Kokkos::Array<typename baseType<typename FieldAccessor::BaseType>::type,
180  FieldAccessor::numComp>;
181  static const size_t NumComponents = FieldAccessor::numComp;
182 
184  using DofArray =
185  Kokkos::Array<typename baseType<typename FieldAccessor::BaseType>::type,
186  ShapeType::numNodes * NumComponents>;
187 
191  Kokkos::Array<typename baseType<typename FieldAccessor::BaseType>::type,
192  MeshEntDim * NumComponents>;
193 
202  KOKKOS_INLINE_FUNCTION DofArray gatherDofs(int ent) const {
203  assert(ent >= 0);
204  assert(static_cast<size_t>(ent) < numMeshEnts);
205  DofArray dofs;
206  for (auto topo : elm2dof.getTopology()) { // element topology
207  for (size_t ni = 0; ni < ShapeType::numNodes; ++ni) {
208  for (size_t ci = 0; ci < NumComponents; ++ci) {
209  const auto map = elm2dof(ni, ci, ent, topo);
210  dofs[ni * NumComponents + ci] =
211  field(map.entity, map.node, map.component, map.topo);
212  }
213  }
214  }
215  return dofs;
216  }
217 
232  KOKKOS_INLINE_FUNCTION DofArray coefficients(int ent) const {
233  return gatherDofs(ent);
234  }
235 
248  template <typename Coefficients>
249  KOKKOS_INLINE_FUNCTION ValArray valueFromCoefficients(
250  Coefficients const &c, Kokkos::Array<Real, MeshEntDim> localCoord) const {
251  const auto shapeValues = shapeFn.getValues(localCoord);
252  ValArray v;
253  for (size_t ci = 0; ci < NumComponents; ++ci)
254  v[ci] = 0;
255  for (size_t ni = 0; ni < ShapeType::numNodes; ++ni)
256  for (size_t ci = 0; ci < NumComponents; ++ci)
257  v[ci] += c[ni * NumComponents + ci] * shapeValues[ni];
258  return v;
259  }
260 
275  template <typename Coefficients>
277  Coefficients const &c, Kokkos::Array<Real, MeshEntDim> localCoord) const {
279  c, shapeFn.getLocalGradients(localCoord));
280  }
281 
291  template <typename Coefficients, typename ShapeGradients>
293  Coefficients const &c, ShapeGradients const &shapeGrads) const {
294  LocalGradArray g;
295  for (size_t i = 0; i < MeshEntDim * NumComponents; ++i)
296  g[i] = 0;
297  for (size_t ni = 0; ni < ShapeType::numNodes; ++ni)
298  for (size_t d = 0; d < MeshEntDim; ++d)
299  for (size_t ci = 0; ci < NumComponents; ++ci)
300  g[d * NumComponents + ci] +=
301  c[ni * NumComponents + ci] * shapeGrads[ni * MeshEntDim + d];
302  return g;
303  }
304 
318  KOKKOS_INLINE_FUNCTION ValArray
319  getValue(int ent, Kokkos::Array<Real, MeshEntDim> localCoord) const {
320  return valueFromCoefficients(coefficients(ent), localCoord);
321  }
322 
323  using NodeArray =
324  Kokkos::Array<typename baseType<typename FieldAccessor::BaseType>::type,
325  ShapeType::meshEntDim * ShapeType::numNodes>;
326  KOKKOS_INLINE_FUNCTION NodeArray getNodeValues(int ent) const {
327  NodeArray c;
328  for (auto topo : elm2dof.getTopology()) { // element topology
329  for (size_t ni = 0; ni < ShapeType::numNodes; ++ni) {
330  for (size_t d = 0; d < ShapeType::meshEntDim; ++d) {
331  auto map = elm2dof(ni, d, ent, topo);
332  const auto fval =
333  field(map.entity, map.node, map.component, map.topo);
334  c[ni * ShapeType::meshEntDim + d] = fval;
335  }
336  }
337  }
338  return c;
339  }
340 
343  using JacobianArray = Kokkos::Array<Real, MeshEntDim * MeshEntDim>;
344 
358  KOKKOS_INLINE_FUNCTION JacobianArray jacobianFromNodeValues(
359  NodeArray const &x, Kokkos::Array<Real, MeshEntDim> localCoord) const {
360  return jacobianFromShapeGradients(x, shapeFn.getLocalGradients(localCoord));
361  }
362 
372  template <typename ShapeGradients>
374  NodeArray const &x, ShapeGradients const &grads) const {
375  JacobianArray J;
376  for (size_t i = 0; i < MeshEntDim * MeshEntDim; ++i)
377  J[i] = 0;
378  for (size_t n = 0; n < ShapeType::numNodes; ++n)
379  for (size_t i = 0; i < MeshEntDim; ++i)
380  for (size_t j = 0; j < MeshEntDim; ++j)
381  J[i * MeshEntDim + j] +=
382  grads[n * MeshEntDim + i] * x[n * MeshEntDim + j];
383  return J;
384  }
385 
398  KOKKOS_INLINE_FUNCTION Real getJacobian1d(int ent) const {
399  assert(ent >= 0);
400  assert(static_cast<size_t>(ent) < numMeshEnts);
401  Vector1 ignored;
402  const auto nodalGradients = shapeFn.getLocalGradients(ignored);
403  const auto nodeValues = getNodeValues(ent);
404  auto g = nodalGradients[0] * nodeValues[0];
405  for (size_t i = 1; i < shapeFn.numNodes; ++i) {
406  g = g + nodalGradients[i] * nodeValues[i];
407  }
408  return g;
409  }
410 
415  template <typename Matrices>
416  Kokkos::View<Real *> getJacobianDeterminants(Matrices const &J) const {
417  static_assert(has_static_rank<Matrices>::value,
418  "Matrices must have a static rank() method.");
419  static_assert(has_extent_method<Matrices>::value,
420  "Matrices must have an extent(size_t) method.");
421  static_assert(Matrices::rank() == 3); // array of rank two matrices
422  if (J.extent(1) != J.extent(2)) {
423  fail("getJacobianDeterminant only supports square matrices. "
424  "The given matrices have dimension %lu x %lu \n",
425  J.extent(1), J.extent(2));
426  }
427  const auto dimension = J.extent(1);
428  if (dimension > 3 || dimension < 1) {
429  fail("getJacobianDeterminant: invalid dimension of input matrix. "
430  "The given matrices have dimension %lu x %lu \n",
431  J.extent(0), J.extent(1));
432  }
433  if (dimension == 3) {
434  /* det(J) is also the triple product of the
435  "tangent vectors" in 3D, the volume of their
436  parallelpiped, which is the differential volume
437  of the coordinate field */
438  Kokkos::View<Real *> determinants("3dJacobianDeterminants", J.extent(0));
439  Kokkos::parallel_for(
440  J.extent(0), KOKKOS_LAMBDA(const int i) {
441  const auto Ji = Kokkos::subview(J, i, Kokkos::ALL(), Kokkos::ALL());
442  const auto cofactorSum =
443  Ji(0, 0) * (Ji(1, 1) * Ji(2, 2) - Ji(1, 2) * Ji(2, 1)) -
444  Ji(0, 1) * (Ji(1, 0) * Ji(2, 2) - Ji(1, 2) * Ji(2, 0)) +
445  Ji(0, 2) * (Ji(1, 0) * Ji(2, 1) - Ji(1, 1) * Ji(2, 0));
446  determinants(i) = cofactorSum;
447  });
448  return determinants;
449  }
450  if (dimension == 2) {
451  if (J.extent(1) != 2) {
452  fail("getJacobianDeterminant only supports 2x2 matrices in 2d. "
453  "The given matrices have dimension %lu x %lu \n",
454  J.extent(1), J.extent(2));
455  }
456  /* |\frac{\partial x}{\partial s}\times
457  \frac{\partial x}{\partial t}|,
458  the area spanned by the tangent vectors
459  at this point, surface integral. */
460  Kokkos::View<Real *> determinants("2dJacobianDeterminants", J.extent(0));
461  // compute the cross product of the 2x2 jacobian matrix
462  Kokkos::parallel_for(
463  J.extent(0), KOKKOS_LAMBDA(const int i) {
464  // TODO use nested parallel for?
465  auto Ji = Kokkos::subview(J, i, Kokkos::ALL(), Kokkos::ALL());
466  const auto cross = Ji(0, 0) * Ji(1, 1) - Ji(1, 0) * Ji(0, 1);
467  determinants(i) = cross;
468  });
469  return determinants;
470  }
471  if (dimension == 1) {
472  fail("getJacobianDeterminant doesn't yet support 1d. "
473  "The given matrices have dimension %lu x %lu \n",
474  J.extent(0), J.extent(1));
475  }
476  // assuming at this point dimension=1
477  /* \|\vec{x}_{,\xi}\| the length
478  of the tangent vector at this point.
479  line integral:
480  ds = sqrt(dx^2 + dy^2 + dz^2) */
481  return Kokkos::View<Real *>("foo", J.extent(0));
482  }
483 
484  Kokkos::View<Real ***>
485  getJacobiansStaticLength(Kokkos::View<Real **> localCoords) const {
486  if (Debug) {
487  LO numErrors = 0;
488  Kokkos::parallel_reduce(
489  "checkCoords", localCoords.extent(0),
490  KOKKOS_LAMBDA(const int &ent, LO &lerrors) {
491  Real sum = 0;
492  LO isError = 0;
493  for (size_t i = 0; i < localCoords.extent(1); i++) {
494  if (localCoords(ent, i) < 0)
495  isError++;
496  sum += localCoords(ent, i);
497  }
498  if (sum > 1.0)
499  isError++;
500  lerrors += isError;
501  },
502  numErrors);
503  if (numErrors) {
504  fail("One or more of the parametric coordinates passed "
505  "to evaluate(...) were invalid\n");
506  }
507  }
508  if (MeshEntDim != 1 && MeshEntDim != 2 && MeshEntDim != 3) {
509  fail("getJacobians only currently supports 1d, 2d, and 3d meshes. Input "
510  "mesh "
511  "has %zu dimensions.\n",
512  numMeshEnts);
513  }
514 
515  if constexpr (MeshEntDim == 1) {
516  const auto numPts = localCoords.extent(0);
517  Kokkos::View<Real ***> res("result", numPts * numMeshEnts, 1, 1);
518  auto jacobianFunc = KOKKOS_CLASS_LAMBDA(const int ent, const int pt) {
519  const auto val = getJacobian1d(ent);
520  res(pt, 0, 0) = val;
521  };
522  MeshField::parallel_for<typename FieldAccessor::Ctrlr, true>({0, 0},
523  {numMeshEnts, numPts}, jacobianFunc, "1dJacobian");
524  return res;
525  } else if constexpr (MeshEntDim == 2 || MeshEntDim == 3) {
526  const auto numPts = localCoords.extent(0);
527  Kokkos::View<Real ***> res("result", numPts * numMeshEnts, MeshEntDim,
528  MeshEntDim);
529  Kokkos::deep_copy(res, 0.0);
530  auto jacobianFunc = KOKKOS_CLASS_LAMBDA(const int ent, const int pt) {
531  const auto vals = getNodeValues(ent);
532  Kokkos::Array<Real, MeshEntDim> xi;
533  for (size_t d = 0; d < MeshEntDim; d++)
534  xi[d] = localCoords(pt, d);
535  const auto grad = shapeFn.getLocalGradients(xi);
536  auto A = Kokkos::subview(res, ent * numPts + pt, Kokkos::ALL(),
537  Kokkos::ALL());
538  Real localA[MeshEntDim][MeshEntDim] = {};
539  for (size_t node = 0; node < ShapeType::numNodes; node++) {
540  for (size_t i = 0; i < MeshEntDim; ++i) {
541  for (size_t j = 0; j < MeshEntDim; ++j) {
542  localA[j][i] += vals[node * MeshEntDim + i] *
543  grad[node * MeshEntDim + j];
544  }
545  }
546  }
547  for (size_t i = 0; i < MeshEntDim; ++i) {
548  for (size_t j = 0; j < MeshEntDim; ++j) {
549  A(i, j) = localA[i][j];
550  }
551  }
552  };
553  MeshField::parallel_for<typename FieldAccessor::Ctrlr, true>({0, 0},
554  {numMeshEnts, numPts}, jacobianFunc, "2d3dJacobian");
555  return res;
556  }
557  }
558 
574  Kokkos::View<Real ***> getJacobians(Kokkos::View<Real **> localCoords,
575  Kokkos::View<LO *> offsets) const {
576  if (Debug) {
577  // check input parametric coords are positive and sum to one
578  // TODO move this to helper function
579  LO numErrors = 0;
580  Kokkos::parallel_reduce(
581  "checkCoords", numMeshEnts,
582  KOKKOS_LAMBDA(const int &ent, LO &lerrors) {
583  Real sum = 0;
584  LO isError = 0;
585  for (size_t i = 0; i < localCoords.extent(1); i++) {
586  if (localCoords(ent, i) < 0)
587  isError++;
588  sum += localCoords(ent, i);
589  }
590  if (sum > 1.0)
591  isError++;
592  lerrors += isError;
593  },
594  numErrors);
595  if (numErrors) {
596  fail("One or more of the parametric coordinates passed "
597  "to evaluate(...) were invalid\n");
598  }
599  }
600  if (localCoords.extent(0) < numMeshEnts) {
601  fail("The size of dimension 0 of the local coordinates input array "
602  "must be at least %zu.\n",
603  numMeshEnts);
604  }
605  if (localCoords.extent(1) != MeshEntDim) {
606  fail("Dimension 1 of the input array of local coordinates "
607  "must have size = %zu.\n",
608  MeshEntDim);
609  }
610  if (offsets.size() != numMeshEnts + 1) {
611  fail("The input array of offsets must have size = %zu\n",
612  numMeshEnts + 1);
613  }
614  if (MeshEntDim != 1 && MeshEntDim != 2 && MeshEntDim != 3) {
615  fail("getJacobians only currently supports 1d, 2d, and 3d meshes. Input "
616  "mesh "
617  "has %zu dimensions.\n",
618  numMeshEnts);
619  }
620  if constexpr (MeshEntDim == 1) {
621  const auto numPts = MeshFieldUtil::getLastValue(offsets);
622  Kokkos::View<Real ***> res("result", numPts, 1, 1);
623  Kokkos::parallel_for(
624  numMeshEnts, KOKKOS_CLASS_LAMBDA(const int ent) {
625  // TODO use nested parallel for?
626  for (auto pt = offsets(ent); pt < offsets(ent + 1); pt++) {
627  const auto val = getJacobian1d(ent);
628  res(pt, 0, 0) = val;
629  }
630  });
631  return res;
632  } else if constexpr (MeshEntDim == 2 || MeshEntDim == 3) {
633  const auto numPts = MeshFieldUtil::getLastValue(offsets);
634  // one matrix per point
635  Kokkos::View<Real ***> res("result", numPts, MeshEntDim, MeshEntDim);
636  Kokkos::deep_copy(res, 0.0); // initialize all entries to zero
637  Kokkos::parallel_for(
638  numMeshEnts, KOKKOS_CLASS_LAMBDA(const int ent) {
639  const auto vals = getNodeValues(ent);
640  assert(vals.size() == MeshEntDim * ShapeType::numNodes);
641  for (auto pt = offsets(ent); pt < offsets(ent + 1); pt++) {
642  auto A = Kokkos::subview(res, pt, Kokkos::ALL(), Kokkos::ALL());
643  Real localA[MeshEntDim][MeshEntDim] = {};
644  Kokkos::Array<Real, MeshEntDim> xi;
645  for (size_t d = 0; d < MeshEntDim; d++)
646  xi[d] = localCoords(pt, d);
647  const auto grad = shapeFn.getLocalGradients(xi);
648  for (size_t node = 0; node < ShapeType::numNodes; node++) {
649  for (size_t i = 0; i < MeshEntDim; ++i) {
650  for (size_t j = 0; j < MeshEntDim; ++j) {
651  localA[j][i] += vals[node * MeshEntDim + i] *
652  grad[node * MeshEntDim + j];
653  }
654  }
655  }
656  for (size_t i = 0; i < MeshEntDim; ++i) {
657  for (size_t j = 0; j < MeshEntDim; ++j) {
658  A(i, j) = localA[i][j];
659  }
660  }
661  }
662  });
663 
664  return res;
665  }
666  }
667 };
668 
669 template <typename FieldElement>
670 Kokkos::View<Real *[FieldElement::NumComponents]>
671 evaluateStaticLength(FieldElement &fes, Kokkos::View<Real **> localCoords) {
672  if (Debug) {
673  LO numErrors = 0;
674  Kokkos::parallel_reduce(
675  "checkCoords", localCoords.extent(0),
676  KOKKOS_LAMBDA(const int &ent, LO &lerrors) {
677  Real sum = 0;
678  LO isError = 0;
679  for (size_t i = 0; i < localCoords.extent(1); i++) {
680  if (localCoords(ent, i) < 0)
681  isError++;
682  sum += localCoords(ent, i);
683  }
684  if (sum > 1.0)
685  isError++;
686  lerrors += isError;
687  },
688  numErrors);
689  if (numErrors) {
690  fail("One or more of the parametric coordinates passed "
691  "to evaluate(...) were invalid\n");
692  }
693  }
694  constexpr const auto numComponents = FieldElement::ValArray::size();
695  const auto numPts = localCoords.extent(0);
696  Kokkos::View<Real *[numComponents]> res("result", numPts * fes.numMeshEnts);
697  auto evaluateFunc = KOKKOS_LAMBDA(const int ent, const int pt) {
698  Kokkos::Array<Real, FieldElement::MeshEntDim> lc;
699  for (size_t i = 0; i < localCoords.extent(1); ++i)
700  lc[i] = localCoords(pt, i);
701  const auto val = fes.getValue(ent, lc);
702  for (size_t i = 0; i < numComponents; ++i) {
703  res(ent * numPts + pt, i) = val[i];
704  }
705  };
706  MeshField::parallel_for<typename decltype(fes.field)::Ctrlr, true>({0, 0}, {fes.numMeshEnts, numPts}, evaluateFunc,
707  "evaluate");
708  return res;
709 }
710 
729 template <typename FieldElement>
730 Kokkos::View<Real *[FieldElement::NumComponents]> evaluate(
731  FieldElement &fes, Kokkos::View<Real **> localCoords,
732  Kokkos::View<LO *> offsets) {
733  if (Debug) {
734  // check input parametric coords are positive and sum to one
735  LO numErrors = 0;
736  Kokkos::parallel_reduce(
737  "checkCoords", fes.numMeshEnts,
738  KOKKOS_LAMBDA(const int &ent, LO &lerrors) {
739  Real sum = 0;
740  LO isError = 0;
741  for (size_t i = 0; i < localCoords.extent(1); i++) {
742  if (localCoords(ent, i) < 0)
743  isError++;
744  sum += localCoords(ent, i);
745  }
746  if (sum > 1.0)
747  isError++;
748  lerrors += isError;
749  },
750  numErrors);
751  if (numErrors) {
752  fail("One or more of the parametric coordinates passed "
753  "to evaluate(...) were invalid\n");
754  }
755  }
756 
757  if (localCoords.extent(1) != fes.MeshEntDim) {
758  fail("Dimension 1 of the input array of local coordinates "
759  "must have size = %zu.\n",
760  fes.MeshEntDim);
761  }
762  if (offsets.size() != fes.numMeshEnts + 1) {
763  fail("The input array of offsets must have size = %zu\n",
764  fes.numMeshEnts + 1);
765  }
766  LO numLocalCoords;
767  Kokkos::deep_copy(numLocalCoords,
768  Kokkos::subview(offsets, offsets.size() - 1));
769  if (localCoords.extent(0) != static_cast<size_t>(numLocalCoords)) {
770  fail("The size of dimension 0 of the local coordinates input array (%zu) "
771  "does not match the last entry of the offsets array (%d).\n",
772  localCoords.extent(0), numLocalCoords);
773  }
774 
775  const auto numPts = MeshFieldUtil::getLastValue(offsets);
776  Kokkos::View<Real *[FieldElement::NumComponents]> res("result", numPts);
777  Kokkos::parallel_for(
778  fes.numMeshEnts, KOKKOS_LAMBDA(const int ent) {
779  Kokkos::Array<Real, FieldElement::MeshEntDim> lc;
780  const auto c = fes.coefficients(ent);
781  // TODO use nested parallel for?
782  for (auto pt = offsets(ent); pt < offsets(ent + 1); pt++) {
783  for (size_t i = 0; i < localCoords.extent(1); i++) // better way?
784  lc[i] = localCoords(pt, i);
785  const auto val = fes.valueFromCoefficients(c, lc);
786  for (size_t i = 0; i < FieldElement::NumComponents; i++)
787  res(pt, i) = val[i];
788  }
789  });
790  return res;
791 }
792 
801 template <typename FieldElement>
802 Kokkos::View<Real *[FieldElement::NumComponents]> evaluate(
803  FieldElement &fes, Kokkos::View<Real **> localCoords) {
804  const auto numPtsPerElement = 1;
805  return evaluate(fes, localCoords, numPtsPerElement);
806 }
807 
819 template <typename FieldElement>
820 Kokkos::View<Real *[FieldElement::NumComponents]> evaluate(
821  FieldElement &fes, Kokkos::View<Real **> localCoords,
822  size_t numPtsPerElement) {
823  const auto offsets = getOffsets(fes.numMeshEnts, numPtsPerElement);
824  return evaluate(fes, localCoords, offsets);
825 }
826 
841 template <typename FieldElement>
842 Kokkos::View<Real ***> getJacobians(FieldElement &fes,
843  Kokkos::View<Real **> localCoords,
844  size_t numPtsPerElement) {
845  const auto offsets = getOffsets(fes.numMeshEnts, numPtsPerElement);
846  return fes.getJacobians(localCoords, offsets);
847 }
848 
859 template <typename FieldElement, typename Matrices>
860 Kokkos::View<Real *> getJacobianDeterminants(FieldElement &fes, Matrices J) {
861  return fes.getJacobianDeterminants(J);
862 }
863 
870  static constexpr size_t NumTopologies = Pyramid + 1;
871  Kokkos::Array<LO, NumTopologies + 1> offset;
872  Kokkos::View<LO *> owner;
873 
875  KOKKOS_INLINE_FUNCTION LO operator()(Mesh_Topology topo, LO entity) const {
876  return owner(offset[topo] + entity);
877  }
878 };
879 
885 template <typename FieldElement>
886 DofHolderOwningEntities getDofHolderOwningEntities(FieldElement const &fes) {
887  constexpr size_t numNodes = decltype(fes.shapeFn)::numNodes;
888  constexpr size_t numTopologies = DofHolderOwningEntities::NumTopologies;
889  const auto &meshInfo = fes.field.meshInfo;
890  const LO numElements = fes.numMeshEnts;
891  DofHolderOwningEntities owners;
892  owners.offset[0] = 0;
893  for (size_t t = 0; t < numTopologies; ++t)
894  owners.offset[t + 1] =
895  owners.offset[t] + meshInfo.numEntities(static_cast<Mesh_Topology>(t));
896  owners.owner = Kokkos::View<LO *>(
897  Kokkos::view_alloc(Kokkos::WithoutInitializing, "dofHolderOwner"),
898  owners.offset[numTopologies]);
899  Kokkos::deep_copy(owners.owner, numElements);
900  const auto offset = owners.offset;
901  const auto owner = owners.owner;
902  Kokkos::parallel_for(
903  "getDofHolderOwningEntities", numElements, KOKKOS_LAMBDA(const int ent) {
904  for (auto topo : fes.elm2dof.getTopology()) {
905  for (size_t n = 0; n < numNodes; ++n) {
906  const auto map = fes.elm2dof(n, 0, ent, topo);
907  Kokkos::atomic_min(&owner(offset[map.topo] + map.entity), ent);
908  }
909  }
910  });
911  return owners;
912 }
913 
922 template <typename FieldElement, typename Source>
923 void setDofByInterpolation(FieldElement &target, Source const &source) {
924  constexpr size_t numNodes = decltype(target.shapeFn)::numNodes;
925  constexpr size_t meshEntDim = FieldElement::MeshEntDim;
926  constexpr size_t numComp = FieldElement::NumComponents;
927  // we get a list of owners so that we only perform evaluation once
928  // per shared dof
929  const auto owners = getDofHolderOwningEntities(target);
930  Kokkos::parallel_for(
931  "interpolate", target.numMeshEnts, KOKKOS_LAMBDA(const int ent) {
932  const auto nodeCoords = target.shapeFn.getNodeParametricCoords();
933  for (auto topo : target.elm2dof.getTopology()) {
934  for (size_t n = 0; n < numNodes; ++n) {
935  const auto holder = target.elm2dof(n, 0, ent, topo);
936  if (owners(holder.topo, holder.entity) != ent)
937  continue;
938  Kokkos::Array<Real, meshEntDim> xi;
939  for (size_t d = 0; d < meshEntDim; ++d)
940  xi[d] = nodeCoords[n * meshEntDim + d];
941  const auto val = source(ent, xi);
942  for (size_t c = 0; c < numComp; ++c) {
943  const auto map = target.elm2dof(n, c, ent, topo);
944  target.field(map.entity, map.node, map.component, map.topo) =
945  val[c];
946  }
947  }
948  }
949  });
950 }
951 
952 } // namespace MeshField
953 #endif
Shape function definitions for finite element analysis.
Kokkos::Array< Real, 1 > Vector1
1D parametric coordinate vector
For each dof holder of a field, the element that owns it; see getDofHolderOwningEntities.
KOKKOS_INLINE_FUNCTION LO operator()(Mesh_Topology topo, LO entity) const
the index of the element that owns the given dof holder
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...
KOKKOS_INLINE_FUNCTION LocalGradArray localGradientFromShapeGradients(Coefficients const &c, ShapeGradients const &shapeGrads) const
localGradientFromCoefficients with the shape function gradients already evaluated,...
KOKKOS_INLINE_FUNCTION ValArray valueFromCoefficients(Coefficients const &c, Kokkos::Array< Real, MeshEntDim > localCoord) const
compute dot product between the basis functions and the coefficients I.e., $$\sum_{A=1}^{N} c_A N_A$$
KOKKOS_INLINE_FUNCTION LocalGradArray localGradientFromCoefficients(Coefficients const &c, Kokkos::Array< Real, MeshEntDim > localCoord) const
compute dot product between the basis function gradients and the coefficients I.e....
KOKKOS_INLINE_FUNCTION JacobianArray jacobianFromNodeValues(NodeArray const &x, Kokkos::Array< Real, MeshEntDim > localCoord) const
compute the Jacobian at a parametric coordinate from the node coordinates
Kokkos::View< Real * > getJacobianDeterminants(Matrices const &J) const
Kokkos::Array< typename baseType< typename FieldAccessor::BaseType >::type, ShapeType::numNodes *NumComponents > DofArray
every dof of one element, indexed [node * NumComponents + comp]
Kokkos::View< Real *** > getJacobians(Kokkos::View< Real ** > localCoords, Kokkos::View< LO * > offsets) const
Given an array of parametric coordinates 'localCoords', one per mesh element, compute the jacobian wi...
KOKKOS_INLINE_FUNCTION DofArray coefficients(int ent) const
the coefficients of one element on its reference basis
KOKKOS_INLINE_FUNCTION DofArray gatherDofs(int ent) const
gather element's dofs into an array
static constexpr KOKKOS_FUNCTION auto getTopology()
the topology of the element's reference element
KOKKOS_INLINE_FUNCTION Real getJacobian1d(int ent) const
compute the Jacobian of an edge
KOKKOS_INLINE_FUNCTION ValArray getValue(int ent, Kokkos::Array< Real, MeshEntDim > localCoord) const
evaluate the field in the specified element at the specified parametric/local/area coordinate
Kokkos::Array< typename baseType< typename FieldAccessor::BaseType >::type, MeshEntDim *NumComponents > LocalGradArray
parametric gradients of every component at one point, indexed [direction * NumComponents + comp]
KOKKOS_INLINE_FUNCTION JacobianArray jacobianFromShapeGradients(NodeArray const &x, ShapeGradients const &grads) const
jacobianFromNodeValues with the shape function gradients already evaluated, so they can be shared wit...
Kokkos::Array< Real, MeshEntDim *MeshEntDim > JacobianArray
the Jacobian at one point, row-major with J[i][j] = dx_j/dxi_i