|
3 | 3 | #include "pcms/utility/inclusive_scan.h" |
4 | 4 | #include "pcms/utility/profile.h" |
5 | 5 | #include <Omega_h_for.hpp> |
| 6 | +#include <MeshField.hpp> |
6 | 7 | #include <memory> |
7 | 8 |
|
8 | 9 | namespace pcms |
@@ -36,59 +37,133 @@ Omega_h::Write<Omega_h::GO> GetGidsHelper(LO total_ents, |
36 | 37 | return owned_gids; |
37 | 38 | } |
38 | 39 |
|
39 | | -// this is a workaround to specify the parametric coordinates for MeshFields to |
40 | | -// be replaced when https://github.com/SCOREC/meshFields/issues/70 is resolved |
41 | | -struct ComputeVertexCoordsFunctor |
| 40 | +namespace |
42 | 41 | { |
43 | | - Kokkos::View<Real**> dof_holder_coords_; |
44 | | - Omega_h::Reals coords_; |
45 | | - size_t offset_; |
46 | | - |
47 | | - ComputeVertexCoordsFunctor(Kokkos::View<Real**> dof_holder_coords, |
48 | | - Omega_h::Reals coords, size_t offset) |
49 | | - : dof_holder_coords_(dof_holder_coords), coords_(coords), offset_(offset) |
50 | | - { |
| 42 | +// Maps a meshFields topology to the dimension. |
| 43 | +KOKKOS_INLINE_FUNCTION int TopologyToDim(MeshField::Mesh_Topology topo) |
| 44 | +{ |
| 45 | + switch (topo) { |
| 46 | + case MeshField::Vertex: return 0; |
| 47 | + case MeshField::Edge: return 1; |
| 48 | + case MeshField::Triangle: return 2; |
| 49 | + case MeshField::Tetrahedron: return 3; |
| 50 | + default: return -1; |
51 | 51 | } |
| 52 | +} |
52 | 53 |
|
53 | | - KOKKOS_INLINE_FUNCTION |
54 | | - void operator()(LO i) const |
55 | | - { |
56 | | - dof_holder_coords_(offset_ + i, 0) = coords_[2 * i + 0]; |
57 | | - dof_holder_coords_(offset_ + i, 1) = coords_[2 * i + 1]; |
| 54 | +// Gets the appropriate MeshField element |
| 55 | +template <int Dim, int Order> |
| 56 | +auto GetMeshFieldElement(Omega_h::Mesh& mesh) |
| 57 | +{ |
| 58 | + if constexpr (Dim == 2) { |
| 59 | + return MeshField::Omegah::getTriangleElement<Order>(mesh); |
| 60 | + } else { |
| 61 | + return MeshField::Omegah::getTetrahedronElement<Order>(mesh); |
58 | 62 | } |
59 | | -}; |
| 63 | +} |
60 | 64 |
|
61 | | -// this is a workaround to specify the parametric coordinates for MeshFields to |
62 | | -// be replaced when https://github.com/SCOREC/meshFields/issues/70 is resolved |
63 | | -struct ComputeEdgeCoordsFunctor |
| 65 | +template <int Dim, int Order> |
| 66 | +void BuildDofHolderCoordsFromMeshFieldImpl( |
| 67 | + Omega_h::Mesh& mesh, Kokkos::View<Real**> holder_coords, |
| 68 | + const std::array<int, 4>& nodes_per_dim) |
64 | 69 | { |
65 | | - Kokkos::View<Real**> dof_holder_coords_; |
66 | | - Omega_h::Reals coords_; |
67 | | - Omega_h::LOs edge_verts_; |
68 | | - size_t offset_; |
| 70 | + const auto elem = GetMeshFieldElement<Dim, Order>(mesh); |
69 | 71 |
|
70 | | - ComputeEdgeCoordsFunctor(Kokkos::View<Real**> dof_holder_coords, |
71 | | - Omega_h::Reals coords, Omega_h::LOs edge_verts, |
72 | | - size_t offset) |
73 | | - : dof_holder_coords_(dof_holder_coords), |
74 | | - coords_(coords), |
75 | | - edge_verts_(edge_verts), |
76 | | - offset_(offset) |
| 72 | + using ShapeT = std::decay_t<decltype(elem.shp)>; |
| 73 | + constexpr size_t numNodes = ShapeT::numNodes; |
| 74 | + constexpr size_t meshDim = ShapeT::meshEntDim; |
| 75 | + constexpr MeshField::Mesh_Topology elemTopo = |
| 76 | + (Dim == 2) ? MeshField::Triangle : MeshField::Tetrahedron; |
| 77 | + const auto map = elem.map; |
| 78 | + |
| 79 | + // Compute the starting offset of DOF holders for each entity dimension. |
| 80 | + Kokkos::Array<LO, 4> dof_holder_offset; |
77 | 81 | { |
| 82 | + LO cur_offset = 0; |
| 83 | + for (int d = 0; d < 4; ++d) { |
| 84 | + dof_holder_offset[d] = cur_offset; |
| 85 | + if (d <= mesh.dim() && nodes_per_dim[d] != 0) |
| 86 | + cur_offset += mesh.nents(d); |
| 87 | + } |
78 | 88 | } |
79 | 89 |
|
80 | | - KOKKOS_INLINE_FUNCTION |
81 | | - void operator()(LO i) const |
82 | | - { |
83 | | - auto verts = Omega_h::gather_verts<2>(edge_verts_, i); |
84 | | - Real x0 = coords_[2 * verts[0] + 0]; |
85 | | - Real y0 = coords_[2 * verts[0] + 1]; |
86 | | - Real x1 = coords_[2 * verts[1] + 0]; |
87 | | - Real y1 = coords_[2 * verts[1] + 1]; |
88 | | - dof_holder_coords_(offset_ + i, 0) = (x0 + x1) / 2; |
89 | | - dof_holder_coords_(offset_ + i, 1) = (y0 + y1) / 2; |
| 90 | + const LO nelems = mesh.nelems(); |
| 91 | + const auto vtx_coords = mesh.coords(); |
| 92 | + const auto param_coords = elem.shp.getNodeParametricCoords(); |
| 93 | + |
| 94 | + Kokkos::parallel_for( |
| 95 | + "MeshFieldsDofHolderCoords", |
| 96 | + Kokkos::RangePolicy<DeviceMemorySpace::execution_space>(0, nelems), |
| 97 | + KOKKOS_LAMBDA(const LO e) { |
| 98 | + constexpr size_t NVerts = Dim + 1; |
| 99 | + |
| 100 | + Real Xv[NVerts * Dim]; |
| 101 | + for (size_t b = 0; b < NVerts; ++b) { |
| 102 | + const auto hv = map(static_cast<MeshField::LO>(b), 0, e, elemTopo); |
| 103 | + const LO v = hv.entity; |
| 104 | + for (size_t d = 0; d < Dim; ++d) |
| 105 | + Xv[b * Dim + d] = vtx_coords[static_cast<size_t>(v) * Dim + d]; |
| 106 | + } |
| 107 | + |
| 108 | + for (size_t n = 0; n < numNodes; ++n) { |
| 109 | + const auto h = map(static_cast<MeshField::LO>(n), 0, e, elemTopo); |
| 110 | + |
| 111 | + // Compute the barycentric coordinates L[b] at the node's parametric |
| 112 | + // coordinates. |
| 113 | + Real L[NVerts]; |
| 114 | + L[0] = 1; |
| 115 | + for (size_t d = 0; d < Dim; ++d) { |
| 116 | + const Real xi_d = param_coords[n * Dim + d]; |
| 117 | + L[d + 1] = xi_d; |
| 118 | + L[0] -= xi_d; |
| 119 | + } |
| 120 | + |
| 121 | + Real X[Dim] = {0}; |
| 122 | + for (size_t b = 0; b < NVerts; ++b) |
| 123 | + for (size_t d = 0; d < Dim; ++d) |
| 124 | + X[d] += L[b] * Xv[b * Dim + d]; |
| 125 | + |
| 126 | + const int dim_of_holder = TopologyToDim(h.topo); |
| 127 | + const LO row = dof_holder_offset[dim_of_holder] + h.entity; |
| 128 | + for (size_t d = 0; d < Dim; ++d) |
| 129 | + holder_coords(row, d) = X[d]; |
| 130 | + } |
| 131 | + }); |
| 132 | +} |
| 133 | + |
| 134 | +void BuildDofHolderCoordsFromMeshField(Omega_h::Mesh& mesh, |
| 135 | + Kokkos::View<Real**> holder_coords, |
| 136 | + const std::array<int, 4>& nodes_per_dim) |
| 137 | +{ |
| 138 | + int dim = mesh.dim(); |
| 139 | + int order = 0; |
| 140 | + for (int i = 0; i <= dim; ++i) { |
| 141 | + if (nodes_per_dim[i] == 1) |
| 142 | + ++order; |
| 143 | + else if (nodes_per_dim[i] != 0) { |
| 144 | + std::cerr << "Unsupported" << std::endl; |
| 145 | + std::abort(); |
| 146 | + } |
90 | 147 | } |
91 | | -}; |
| 148 | + |
| 149 | + if (dim == 2 && order == 1) |
| 150 | + BuildDofHolderCoordsFromMeshFieldImpl<2, 1>(mesh, holder_coords, |
| 151 | + nodes_per_dim); |
| 152 | + else if (dim == 2 && order == 2) |
| 153 | + BuildDofHolderCoordsFromMeshFieldImpl<2, 2>(mesh, holder_coords, |
| 154 | + nodes_per_dim); |
| 155 | + else if (dim == 3 && order == 1) |
| 156 | + BuildDofHolderCoordsFromMeshFieldImpl<3, 1>(mesh, holder_coords, |
| 157 | + nodes_per_dim); |
| 158 | + else if (dim == 3 && order == 2) |
| 159 | + BuildDofHolderCoordsFromMeshFieldImpl<3, 2>(mesh, holder_coords, |
| 160 | + nodes_per_dim); |
| 161 | + else { |
| 162 | + std::cerr << "Unsupported element/order combination" << std::endl; |
| 163 | + std::abort(); |
| 164 | + } |
| 165 | +} |
| 166 | +} // namespace |
92 | 167 |
|
93 | 168 | struct CopyClassInfoFunctor |
94 | 169 | { |
@@ -152,32 +227,9 @@ MeshFieldsAdapterLayout::MeshFieldsAdapterLayout( |
152 | 227 | std::abort(); |
153 | 228 | } |
154 | 229 |
|
155 | | - auto coords = mesh_.coords(); |
| 230 | + BuildDofHolderCoordsFromMeshField(mesh_, dof_holder_coords_, nodes_per_dim); |
156 | 231 |
|
157 | 232 | size_t offset = 0; |
158 | | - for (int i = 0; i <= mesh_.dim(); ++i) { |
159 | | - if (nodes_per_dim[i] == 1) { |
160 | | - if (i == 0) { |
161 | | - ComputeVertexCoordsFunctor functor(dof_holder_coords_, coords, offset); |
162 | | - Kokkos::parallel_for(mesh_.nents(0), functor); |
163 | | - } else if (i == 1) { |
164 | | - auto edge_verts = mesh_.ask_verts_of(1); |
165 | | - ComputeEdgeCoordsFunctor functor(dof_holder_coords_, coords, edge_verts, |
166 | | - offset); |
167 | | - Kokkos::parallel_for(mesh_.nents(1), functor); |
168 | | - } else { |
169 | | - std::cerr << "Unsupported" << std::endl; |
170 | | - std::abort(); |
171 | | - } |
172 | | - } else if (nodes_per_dim[i] != 0) { |
173 | | - std::cerr << "Unsupported" << std::endl; |
174 | | - std::abort(); |
175 | | - } |
176 | | - |
177 | | - offset += mesh.nents(i); |
178 | | - } |
179 | | - |
180 | | - offset = 0; |
181 | 233 | for (int i = 0; i <= mesh_.dim(); ++i) { |
182 | 234 | if (nodes_per_dim_[i]) { |
183 | 235 | auto ids = mesh_.get_array<Omega_h::ClassId>(i, "class_id"); |
|
0 commit comments