v0.16.3
Loading...
Searching...
No Matches
UserDataOperatorsFromTags.cpp
Go to the documentation of this file.
1/** \file UserDataOperatorsFromTags.cpp
2 * \brief Operators evaluating MOAB tag data at integration points
3 *
4 * See UserDataOperatorsFromTags.hpp for the interface.
5 */
6
7namespace MoFEM {
8
9// The finite-element sources are compiled as one translation unit (All.cpp),
10// so implementation details live in a named namespace.
11namespace FromTagsImpl {
12
15
16/// For each output component, in FTensor storage order, the index of the tag
17/// component that holds it
18template <int NB> using ComponentMap = std::array<int, NB>;
19
20template <int NB> ComponentMap<NB> identityMap() {
22 for (int k = 0; k != NB; ++k)
23 map[k] = k;
24 return map;
25}
26
27template <int DIM>
28ComponentMap<(DIM * (DIM + 1)) / 2> symmetricMap(const TagNotation notation) {
29 static_assert(DIM == 2 || DIM == 3,
30 "Symmetric tensor tags are implemented for DIM = 2, 3");
31 switch (notation) {
33 return identityMap<(DIM * (DIM + 1)) / 2>();
35 // Voigt (00, 11, 01) and (00, 11, 22, 01, 02, 12)
36 if constexpr (DIM == 2)
37 return {0, 2, 1};
38 else
39 return {0, 3, 4, 1, 5, 2};
40 }
41 THROW_MESSAGE("Unknown tag notation");
42 return {};
43}
44
45template <int DIM0, int DIM1>
47 switch (notation) {
49 return identityMap<DIM0 * DIM1>();
51 // Voigt (00, 11, 01, 10) and (00, 11, 22, 01, 10, 02, 20, 12, 21)
52 if constexpr (DIM0 == 2 && DIM1 == 2) {
53 return {0, 2, 3, 1};
54 } else if constexpr (DIM0 == 3 && DIM1 == 3) {
55 return {0, 3, 5, 4, 1, 7, 6, 8, 2};
56 } else {
57 THROW_MESSAGE("Voigt notation requires a square tensor");
58 return {};
59 }
60 }
61 THROW_MESSAGE("Unknown tag notation");
62 return {};
63}
64
65/// Resolve the tag by name and check its type and length
66MoFEMErrorCode getTag(moab::Interface &moab, const std::string &tag_name,
67 const int nb_components, const bool allow_longer_tag,
68 Tag &th, int &tag_length) {
70 if (moab.tag_get_handle(tag_name.c_str(), th) != MB_SUCCESS)
71 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY, "Tag %s does not exist",
72 tag_name.c_str());
73 DataType data_type;
74 CHKERR moab.tag_get_data_type(th, data_type);
75 if (data_type != MB_TYPE_DOUBLE)
76 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
77 "Tag %s must hold doubles", tag_name.c_str());
78 if (moab.tag_get_length(th, tag_length) != MB_SUCCESS)
79 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
80 "Tag %s must have a fixed length", tag_name.c_str());
81 if (allow_longer_tag ? tag_length < nb_components
82 : tag_length != nb_components)
83 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
84 "Tag %s has %d components, expected %s%d", tag_name.c_str(),
85 tag_length, allow_longer_tag ? "at least " : "", nb_components);
87}
88
89/// Read tag values, naming the entity when a value is missing
90MoFEMErrorCode getTagData(moab::Interface &moab, const Tag th,
91 const std::string &tag_name, const EntityHandle *ents,
92 const int nb_ents, double *data) {
94 const auto rval = moab.tag_get_data(th, ents, nb_ents, data);
95 if (rval == MB_TAG_NOT_FOUND) {
96 for (int i = 0; i != nb_ents; ++i) {
97 if (moab.tag_get_data(th, &ents[i], 1, data) == MB_TAG_NOT_FOUND)
98 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
99 "Tag %s has no value on %s %lu and no default value",
100 tag_name.c_str(),
101 moab::CN::EntityTypeName(type_from_handle(ents[i])),
102 static_cast<unsigned long>(id_from_handle(ents[i])));
103 }
104 }
105 CHKERR rval;
107}
108
109/// Vertex shape functions at integration points, nb_gauss x nb_nodes
111 MatrixDouble &gauss_pts,
112 std::vector<double> &shape,
113 int &nb_nodes) {
115 const int nb_gauss = gauss_pts.size2();
116 const int dim = moab::CN::Dimension(type);
117 if (static_cast<int>(gauss_pts.size1()) < dim)
118 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
119 "Integration points have %zu rows, expected at least %d for %s",
120 gauss_pts.size1(), dim, moab::CN::EntityTypeName(type));
121 // No point rows to address on an element without integration points
122 auto ksi = [&](const int d) { return nb_gauss ? &gauss_pts(d, 0) : nullptr; };
123 switch (type) {
124 case MBVERTEX:
125 nb_nodes = 1;
126 shape.assign(nb_gauss, 1.);
127 break;
128 case MBEDGE:
129 nb_nodes = 2;
130 shape.resize(nb_nodes * nb_gauss);
131 CHKERR Tools::shapeFunMBEDGE<1>(shape.data(), ksi(0), nb_gauss);
132 break;
133 case MBTRI:
134 nb_nodes = 3;
135 shape.resize(nb_nodes * nb_gauss);
136 CHKERR Tools::shapeFunMBTRI<1>(shape.data(), ksi(0), ksi(1), nb_gauss);
137 break;
138 case MBQUAD:
139 nb_nodes = 4;
140 shape.resize(nb_nodes * nb_gauss);
141 CHKERR Tools::shapeFunMBQUAD<1>(shape.data(), ksi(0), ksi(1), nb_gauss);
142 break;
143 case MBTET:
144 nb_nodes = 4;
145 shape.resize(nb_nodes * nb_gauss);
146 CHKERR Tools::shapeFunMBTET<1>(shape.data(), ksi(0), ksi(1), ksi(2),
147 nb_gauss);
148 break;
149 case MBHEX:
150 nb_nodes = 8;
151 shape.resize(nb_nodes * nb_gauss);
152 CHKERR Tools::shapeFunMBHEX<1>(shape.data(), ksi(0), ksi(1), ksi(2),
153 nb_gauss);
154 break;
155 default:
156 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_IMPLEMENTED,
157 "Vertex tag interpolation is not implemented for %s",
158 moab::CN::EntityTypeName(type));
159 }
161}
162
163/**
164 * @brief Evaluate NB tag components at integration points
165 *
166 * @tparam NB number of output components per point
167 * @tparam SPACE where the tag lives
168 * @tparam OUT VectorDouble for one component, MatrixDouble otherwise
169 */
170template <int NB, TagSpace SPACE, typename OUT>
172
173 OpFromTags(const std::string &tag_name, boost::shared_ptr<OUT> data_ptr,
174 const ComponentMap<NB> &component_map, const bool allow_longer_tag)
176 dataPtr(data_ptr), componentMap(component_map),
177 allowLongerTag(allow_longer_tag) {}
178
179 MoFEMErrorCode doWork(int, EntityType,
180 EntitiesFieldData::EntData &) override {
182
183 auto &moab = getPtrFE()->mField.get_moab();
184 Tag th;
185 int tag_length;
186 CHKERR getTag(moab, tagName, NB, allowLongerTag, th, tag_length);
187
188 const auto fe_ent = getFEEntityHandle();
189 auto &gauss_pts = getGaussPts();
190 const int nb_gauss = gauss_pts.size2();
191
192 int nb_nodes = 1;
193 const EntityHandle *nodes = &fe_ent;
194 if constexpr (SPACE == TagSpace::VERTEX) {
195 const auto type = type_from_handle(fe_ent);
196 CHKERR getVertexShapeFunctions(type, gauss_pts, shapeFunctions, nb_nodes);
197 if (type != MBVERTEX) {
198 int nb_corners;
199 CHKERR moab.get_connectivity(fe_ent, nodes, nb_corners, true);
200 if (nb_corners != nb_nodes)
201 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
202 "%s has %d corners, expected %d",
203 moab::CN::EntityTypeName(type), nb_corners, nb_nodes);
204 }
205 }
206
207 tagValues.resize(nb_nodes * tag_length);
208 CHKERR getTagData(moab, th, tagName, nodes, nb_nodes, tagValues.data());
209 // Reorder to FTensor storage before interpolation; both are linear, so
210 // they commute, and this touches nodes rather than integration points.
211 for (int n = 0; n != nb_nodes; ++n)
212 for (int k = 0; k != NB; ++k)
213 nodalValues[NB * n + k] = tagValues[tag_length * n + componentMap[k]];
214
215 auto value_at = [&](const int gg, const int k) {
216 if constexpr (SPACE == TagSpace::ENTITY) {
217 return nodalValues[k];
218 } else {
219 const double *shape = &shapeFunctions[nb_nodes * gg];
220 double value = 0;
221 for (int n = 0; n != nb_nodes; ++n)
222 value += shape[n] * nodalValues[NB * n + k];
223 return value;
224 }
225 };
226
227 if constexpr (std::is_same_v<OUT, VectorDouble>) {
228 static_assert(NB == 1, "Scalar output takes one component");
229 auto &vec = *dataPtr;
230 vec.resize(nb_gauss, false);
231 for (int gg = 0; gg != nb_gauss; ++gg)
232 vec[gg] = value_at(gg, 0);
233 } else {
234 auto &mat = *dataPtr;
235 auto get_values =
237 mat, nb_gauss);
238 if (nb_gauss == 0)
240 auto t_values = get_values();
241 for (int gg = 0; gg != nb_gauss; ++gg) {
242 for (int k = 0; k != NB; ++k)
243 t_values(k) = value_at(gg, k);
244 ++t_values;
245 }
246 }
247
249 }
250
251private:
252 const std::string tagName;
253 boost::shared_ptr<OUT> dataPtr;
255 const bool allowLongerTag;
256
257 std::vector<double> tagValues;
258 std::vector<double> shapeFunctions;
259 std::array<double, 8 * NB> nodalValues;
260};
261
262template <int NB, typename OUT>
264createOp(const std::string &tag_name, boost::shared_ptr<OUT> data_ptr,
265 const TagSpace space, const ComponentMap<NB> &component_map,
266 const bool allow_longer_tag) {
267 if (!data_ptr)
268 THROW_MESSAGE("Data pointer is not set");
269 switch (space) {
270 case TagSpace::ENTITY:
272 tag_name, data_ptr, component_map, allow_longer_tag);
273 case TagSpace::VERTEX:
275 tag_name, data_ptr, component_map, allow_longer_tag);
276 }
277 THROW_MESSAGE("Unknown tag space");
278 return nullptr;
279}
280
281} // namespace FromTagsImpl
282
283ForcesAndSourcesCore::UserDataOperator *
285 const std::string tag_name, boost::shared_ptr<VectorDouble> data_ptr,
286 const TagSpace space, const bool allow_longer_tag) {
287 return FromTagsImpl::createOp<1>(tag_name, data_ptr, space,
288 FromTagsImpl::identityMap<1>(),
289 allow_longer_tag);
290}
291
292template <int DIM>
293ForcesAndSourcesCore::UserDataOperator *
295 const std::string tag_name, boost::shared_ptr<MatrixDouble> data_ptr,
296 const TagSpace space, const bool allow_longer_tag) {
297 return FromTagsImpl::createOp<DIM>(tag_name, data_ptr, space,
298 FromTagsImpl::identityMap<DIM>(),
299 allow_longer_tag);
300}
301
302template <int DIM>
303ForcesAndSourcesCore::UserDataOperator *
305 const std::string tag_name, boost::shared_ptr<MatrixDouble> data_ptr,
306 const TagSpace space, const TagNotation notation,
307 const bool allow_longer_tag) {
308 return FromTagsImpl::createOp<(DIM * (DIM + 1)) / 2>(
309 tag_name, data_ptr, space, FromTagsImpl::symmetricMap<DIM>(notation),
310 allow_longer_tag);
311}
312
313template <int DIM0, int DIM1>
314ForcesAndSourcesCore::UserDataOperator *
316 const std::string tag_name, boost::shared_ptr<MatrixDouble> data_ptr,
317 const TagSpace space, const TagNotation notation,
318 const bool allow_longer_tag) {
319 return FromTagsImpl::createOp<DIM0 * DIM1>(
320 tag_name, data_ptr, space, FromTagsImpl::fullMap<DIM0, DIM1>(notation),
321 allow_longer_tag);
322}
323
324#define INSTANTIATE_VECTOR_FROM_TAGS(DIM) \
325 template ForcesAndSourcesCore::UserDataOperator * \
326 createOpCalculateVectorFieldValuesFromTags<DIM>( \
327 const std::string, boost::shared_ptr<MatrixDouble>, const TagSpace, \
328 const bool);
329
330#define INSTANTIATE_SYMMETRIC_FROM_TAGS(DIM) \
331 template ForcesAndSourcesCore::UserDataOperator * \
332 createOpCalculateTensor2SymmetricFieldValuesFromTags<DIM>( \
333 const std::string, boost::shared_ptr<MatrixDouble>, const TagSpace, \
334 const TagNotation, const bool);
335
336#define INSTANTIATE_TENSOR2_FROM_TAGS(DIM0, DIM1) \
337 template ForcesAndSourcesCore::UserDataOperator * \
338 createOpCalculateTensor2FieldValuesFromTags<DIM0, DIM1>( \
339 const std::string, boost::shared_ptr<MatrixDouble>, const TagSpace, \
340 const TagNotation, const bool);
341
351
352#undef INSTANTIATE_VECTOR_FROM_TAGS
353#undef INSTANTIATE_SYMMETRIC_FROM_TAGS
354#undef INSTANTIATE_TENSOR2_FROM_TAGS
355
356} // namespace MoFEM
std::vector< int > size
std::string type
#define INSTANTIATE_VECTOR_FROM_TAGS(DIM)
#define INSTANTIATE_TENSOR2_FROM_TAGS(DIM0, DIM1)
#define INSTANTIATE_SYMMETRIC_FROM_TAGS(DIM)
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
@ NOSPACE
Definition definitions.h:83
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_DATA_INCONSISTENCY
Definition definitions.h:31
@ MOFEM_NOT_IMPLEMENTED
Definition definitions.h:32
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
#define THROW_MESSAGE(msg)
Throw MoFEM exception.
TagSpace
Where tag values live and how they are evaluated.
ForcesAndSourcesCore::UserDataOperator * createOpCalculateScalarFieldValuesFromTags(const std::string tag_name, boost::shared_ptr< VectorDouble > data_ptr, const TagSpace space, const bool allow_longer_tag)
Create operator evaluating a scalar tag at integration points.
TagNotation
Component order of a tensor stored on a tag.
ForcesAndSourcesCore::UserDataOperator * createOpCalculateTensor2SymmetricFieldValuesFromTags(const std::string tag_name, boost::shared_ptr< MatrixDouble > data_ptr, const TagSpace space, const TagNotation notation, const bool allow_longer_tag)
Create operator evaluating a symmetric tensor tag at integration points.
ForcesAndSourcesCore::UserDataOperator * createOpCalculateVectorFieldValuesFromTags(const std::string tag_name, boost::shared_ptr< MatrixDouble > data_ptr, const TagSpace space, const bool allow_longer_tag)
Create operator evaluating a vector tag at integration points.
ForcesAndSourcesCore::UserDataOperator * createOpCalculateTensor2FieldValuesFromTags(const std::string tag_name, boost::shared_ptr< MatrixDouble > data_ptr, const TagSpace space, const TagNotation notation, const bool allow_longer_tag)
Create operator evaluating a second-order tensor tag at integration points.
FTensor::Index< 'i', SPACE_DIM > i
constexpr int DIM1
Definition level_set.cpp:21
FTensor::Index< 'k', 3 > k
static MoFEMErrorCodeGeneric< moab::ErrorCode > rval
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
ComponentMap<(DIM *(DIM+1))/2 > symmetricMap(const TagNotation notation)
UserDataOperator * createOp(const std::string &tag_name, boost::shared_ptr< OUT > data_ptr, const TagSpace space, const ComponentMap< NB > &component_map, const bool allow_longer_tag)
MoFEMErrorCode getVertexShapeFunctions(const EntityType type, MatrixDouble &gauss_pts, std::vector< double > &shape, int &nb_nodes)
Vertex shape functions at integration points, nb_gauss x nb_nodes.
MoFEMErrorCode getTag(moab::Interface &moab, const std::string &tag_name, const int nb_components, const bool allow_longer_tag, Tag &th, int &tag_length)
Resolve the tag by name and check its type and length.
ComponentMap< NB > identityMap()
ComponentMap< DIM0 *DIM1 > fullMap(const TagNotation notation)
MoFEMErrorCode getTagData(moab::Interface &moab, const Tag th, const std::string &tag_name, const EntityHandle *ents, const int nb_ents, double *data)
Read tag values, naming the entity when a value is missing.
std::array< int, NB > ComponentMap
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
auto type_from_handle(const EntityHandle h)
get type from entity handle
auto id_from_handle(const EntityHandle h)
decltype(GetFTensor1FromMatImpl< Tensor_Dim, S, DL, M >::get(std::declval< M & >(), 0, 0)) GetFTensor1FromMatType
virtual moab::Interface & get_moab()=0
Data on single entity (This is passed as argument to DataOperator::doWork)
EntityHandle getFEEntityHandle() const
Return finite element entity handle.
@ OPSPACE
operator do Work is execute on space data
MatrixDouble & getGaussPts()
matrix of integration (Gauss) points for Volume Element
Evaluate NB tag components at integration points.
MoFEMErrorCode doWork(int, EntityType, EntitiesFieldData::EntData &) override
Operator for linear form, usually to calculate values on right hand side.
OpFromTags(const std::string &tag_name, boost::shared_ptr< OUT > data_ptr, const ComponentMap< NB > &component_map, const bool allow_longer_tag)