v0.16.3
Loading...
Searching...
No Matches
field_values_from_tags.cpp
Go to the documentation of this file.
1/**
2 * \file field_values_from_tags.cpp
3 * \example mofem/atom_tests/field_values_from_tags.cpp
4 *
5 * \brief Check operators evaluating MOAB tag data at integration points
6 *
7 * The mesh is built in the test: two edges, two triangles, two distorted
8 * quads, two tetrahedra and two distorted hexahedra, each part disconnected
9 * from the others. Vertex tags hold affine functions of the vertex
10 * coordinates, which vertex interpolation reproduces exactly on elements with
11 * linear or multilinear geometry. Entity tags hold one value per element.
12 *
13 * Tests (select with -test_nb or -test_name):
14 * 1. entity - TagSpace::ENTITY, scalar, vector and a padded Voigt tensor
15 * 2. vertex - TagSpace::VERTEX, scalar and vector against the exact values
16 * 3. h1 - VERTEX against an order-1 H1 field with the same vertex values
17 * 4. notation - symmetric and full tensors in FTENSOR and VOIGT order
18 * 5. side - VERTEX in a volume side pipeline driven by skin faces
19 * 6. errors - invalid or missing tag data makes the element loop fail
20 * 7. factory - invalid factory arguments throw
21 */
22
23#include <MoFEM.hpp>
24
25using namespace MoFEM;
26
27static char help[] = "Tag values at integration points\n\n";
28
29namespace {
30
32using CheckFun = boost::function<MoFEMErrorCode(UDO *op)>;
33
34constexpr TestingInterface::Tolerance TOL{1e-11, 0.};
35constexpr const char *PROBLEM = "FROM_TAGS";
36const BitRefLevel BIT = BitRefLevel().set(0);
37
38struct Part {
39 EntityType type;
40 std::string name;
41 int dim;
42 Range elements;
43 std::string feName() const { return "FE_" + name; }
44 std::string skinFeName() const { return "FE_SKIN_" + name; }
45 std::string scalarField() const { return "U_" + name; }
46 std::string vectorField() const { return "V_" + name; }
47};
48
49// Affine data on vertices.
50double scalarAt(const double *x) {
51 return 0.3 + 1.1 * x[0] - 0.7 * x[1] + 0.4 * x[2];
52}
53
54double vectorAt(const int k, const double *x) {
55 return 1. + k + (0.5 + k) * x[0] - (1. - 0.2 * k) * x[1] + 0.3 * k * x[2];
56}
57
58double fullAt(const int i, const int j, const double *x) {
59 return 1. + i - 2. * j + (i == j ? 3. : 0.) + 0.1 * (i + 1) * x[0] -
60 0.3 * (j + 1) * x[1] + 0.05 * (i + 2 * j) * x[2];
61}
62
63double symmetricAt(const int i, const int j, const double *x) {
64 return 0.5 * (fullAt(i, j, x) + fullAt(j, i, x));
65}
66
67// One value per element and component.
68double entityAt(const EntityHandle ent, const int k) {
69 return 1. + 0.5 * k + 0.25 * id_from_handle(ent) +
70 0.125 * static_cast<int>(type_from_handle(ent));
71}
72
73// Tensor tags, with the component orders written out independently of the
74// implementation.
75struct TensorTag {
76 std::string name;
77 int length;
78 std::function<std::vector<double>(const double *)> components;
79};
80
81std::vector<TensorTag> tensorTags() {
82 auto S = symmetricAt;
83 auto T = fullAt;
84 return {{"SYM2_FTENSOR", 3,
85 [=](auto x) {
86 return std::vector<double>{S(0, 0, x), S(0, 1, x), S(1, 1, x)};
87 }},
88 {"SYM2_VOIGT", 3,
89 [=](auto x) {
90 return std::vector<double>{S(0, 0, x), S(1, 1, x), S(0, 1, x)};
91 }},
92 {"SYM3_FTENSOR", 6,
93 [=](auto x) {
94 return std::vector<double>{S(0, 0, x), S(0, 1, x), S(0, 2, x),
95 S(1, 1, x), S(1, 2, x), S(2, 2, x)};
96 }},
97 {"SYM3_VOIGT", 6,
98 [=](auto x) {
99 return std::vector<double>{S(0, 0, x), S(1, 1, x), S(2, 2, x),
100 S(0, 1, x), S(0, 2, x), S(1, 2, x)};
101 }},
102 {"FULL2_FTENSOR", 4,
103 [=](auto x) {
104 return std::vector<double>{T(0, 0, x), T(0, 1, x), T(1, 0, x),
105 T(1, 1, x)};
106 }},
107 {"FULL2_VOIGT", 4,
108 [=](auto x) {
109 return std::vector<double>{T(0, 0, x), T(1, 1, x), T(0, 1, x),
110 T(1, 0, x)};
111 }},
112 {"FULL3_FTENSOR", 9,
113 [=](auto x) {
114 return std::vector<double>{T(0, 0, x), T(0, 1, x), T(0, 2, x),
115 T(1, 0, x), T(1, 1, x), T(1, 2, x),
116 T(2, 0, x), T(2, 1, x), T(2, 2, x)};
117 }},
118 {"FULL3_VOIGT", 9,
119 [=](auto x) {
120 return std::vector<double>{T(0, 0, x), T(1, 1, x), T(2, 2, x),
121 T(0, 1, x), T(1, 0, x), T(0, 2, x),
122 T(2, 0, x), T(1, 2, x), T(2, 1, x)};
123 }},
124 {"FULL32_FTENSOR", 6, [=](auto x) {
125 return std::vector<double>{T(0, 0, x), T(0, 1, x), T(1, 0, x),
126 T(1, 1, x), T(2, 0, x), T(2, 1, x)};
127 }}};
128}
129
130// Padded Voigt entity tag: six components, then values that must be ignored.
131std::vector<double> paddedVoigtAt(const double *x) {
132 auto S = symmetricAt;
133 return {S(0, 0, x), S(1, 1, x), S(2, 2, x), S(0, 1, x), S(0, 2, x),
134 S(1, 2, x), 1e6, 1e6, 1e6};
135}
136
137MoFEMErrorCode createParts(moab::Interface &moab, std::vector<Part> &parts) {
139
140 auto add_part = [&](EntityType type, const std::string &name,
141 const std::vector<double> &coords,
142 const std::vector<std::vector<int>> &conn) {
144 std::vector<EntityHandle> verts(coords.size() / 3);
145 for (size_t v = 0; v != verts.size(); ++v)
146 CHKERR moab.create_vertex(&coords[3 * v], verts[v]);
147 Part part{type, name, moab::CN::Dimension(type), Range()};
148 for (auto &c : conn) {
149 std::vector<EntityHandle> nodes;
150 for (auto v : c)
151 nodes.push_back(verts[v]);
152 EntityHandle ent;
153 CHKERR moab.create_element(type, nodes.data(), nodes.size(), ent);
154 part.elements.insert(ent);
155 }
156 // MoFEM element data needs the edges and faces of each element.
157 for (int d = 1; d < part.dim; ++d) {
158 Range adj;
159 CHKERR moab.get_adjacencies(part.elements, d, true, adj,
160 moab::Interface::UNION);
161 }
162 parts.push_back(part);
164 };
165
166 CHKERR add_part(MBEDGE, "edge", {0, 0, 0, 1, 0.2, 0.1, 2.1, 0.5, 0.4},
167 {{0, 1}, {1, 2}});
168 CHKERR add_part(MBTRI, "tri", {0, 0, 5, 1, 0, 5.1, 1.1, 1, 5.2, 0, 1.2, 5},
169 {{0, 1, 2}, {0, 2, 3}});
170 CHKERR add_part(MBQUAD, "quad",
171 {0, 0, 10, 1.2, 0.1, 10, 1.0, 1.1, 10, 0.1, 0.9, 10, 2.3, 0.3,
172 10, 2.1, 1.3, 10},
173 {{0, 1, 2, 3}, {1, 4, 5, 2}});
174 CHKERR add_part(MBTET, "tet",
175 {0, 0, 15, 1, 0, 15, 0, 1, 15, 0, 0, 16, 1, 1, 16},
176 {{0, 1, 2, 3}, {1, 2, 3, 4}});
177 CHKERR add_part(MBHEX, "hex",
178 {0, 0, 20, 1, 0, 20.05, 1.1, 1, 20,
179 0, 0.9, 20.1, 0.05, 0, 21, 1, 0.1, 21.1,
180 1.2, 1.1, 21, -0.1, 1, 20.9, 2, 0, 20,
181 2.1, 1.2, 20.1, 2.2, -0.1, 21, 2, 1, 21.2},
182 {{0, 1, 2, 3, 4, 5, 6, 7}, {1, 8, 9, 2, 5, 10, 11, 6}});
183
185}
186
187MoFEMErrorCode createTags(moab::Interface &moab,
188 const std::vector<Part> &parts) {
190
191 Range verts;
192 CHKERR moab.get_entities_by_type(0, MBVERTEX, verts);
193 std::vector<double> coords(3 * verts.size());
194 CHKERR moab.get_coords(verts, coords.data());
195
196 auto create = [&](const std::string &name, const int length,
197 const unsigned flags = MB_TAG_DENSE) {
198 Tag th;
199 CHK_MOAB_THROW(moab.tag_get_handle(name.c_str(), length, MB_TYPE_DOUBLE, th,
200 MB_TAG_CREAT | flags),
201 "create tag");
202 return th;
203 };
204
205 auto set_vertex_tag = [&](const std::string &name, const int length,
206 auto fun) {
208 auto th = create(name, length);
209 std::vector<double> data(length * verts.size());
210 for (size_t v = 0; v != verts.size(); ++v) {
211 const auto values = fun(&coords[3 * v]);
212 std::copy(values.begin(), values.end(), &data[length * v]);
213 }
214 CHKERR moab.tag_set_data(th, verts, data.data());
216 };
217
218 CHKERR set_vertex_tag(
219 "S_VERTEX", 1, [](auto x) { return std::vector<double>{scalarAt(x)}; });
220 CHKERR set_vertex_tag("V_VERTEX", 3, [](auto x) {
221 return std::vector<double>{vectorAt(0, x), vectorAt(1, x), vectorAt(2, x)};
222 });
223 for (auto &t : tensorTags())
224 CHKERR set_vertex_tag(t.name, t.length, t.components);
225
226 auto th_s = create("S_ENTITY", 1);
227 auto th_v = create("V_ENTITY", 3);
228 auto th_p = create("SYM3_VOIGT_PADDED", 9);
229 for (auto &part : parts) {
230 for (auto ent : part.elements) {
231 const double s = entityAt(ent, 0);
232 CHKERR moab.tag_set_data(th_s, &ent, 1, &s);
233 const double v[] = {entityAt(ent, 0), entityAt(ent, 1), entityAt(ent, 2)};
234 CHKERR moab.tag_set_data(th_v, &ent, 1, v);
235 const EntityHandle *conn;
236 int nb_conn;
237 CHKERR moab.get_connectivity(ent, conn, nb_conn, true);
238 double x[3];
239 CHKERR moab.get_coords(conn, 1, x);
240 const auto p = paddedVoigtAt(x);
241 CHKERR moab.tag_set_data(th_p, &ent, 1, p.data());
242 }
243 }
244
245 // Tags that the error test must reject.
246 CHKERR moab.tag_set_data(create("BAD_LENGTH", 2), verts,
247 std::vector<double>(2 * verts.size(), 1.).data());
248 Tag th_int;
249 CHKERR moab.tag_get_handle("BAD_TYPE", 1, MB_TYPE_INTEGER, th_int,
250 MB_TAG_CREAT | MB_TAG_DENSE);
251 CHKERR moab.tag_set_data(th_int, verts,
252 std::vector<int>(verts.size(), 1).data());
253 Tag th_varlen;
254 CHKERR moab.tag_get_handle("BAD_VARLEN", 0, MB_TYPE_DOUBLE, th_varlen,
255 MB_TAG_CREAT | MB_TAG_VARLEN | MB_TAG_SPARSE);
256 auto th_sparse = create("SPARSE_PARTIAL", 1, MB_TAG_SPARSE);
257 const double one = 1;
258 const EntityHandle first_vertex = verts.front();
259 CHKERR moab.tag_set_data(th_sparse, &first_vertex, 1, &one);
260
262}
263
264MoFEMErrorCode setUpProblem(MoFEM::Interface &m_field,
265 std::vector<Part> &parts) {
267 auto &moab = m_field.get_moab();
268
269 Range elements;
270 for (auto &part : parts)
271 elements.merge(part.elements);
272 CHKERR m_field.getInterface<BitRefManager>()->setBitRefLevel(elements, BIT,
273 false);
274
275 CHKERR m_field.add_problem(PROBLEM);
276 CHKERR m_field.modify_problem_ref_level_add_bit(PROBLEM, BIT);
277
278 auto add_fe = [&](const std::string &fe_name, const Part &part,
279 const Range &ents, const int dim) {
281 CHKERR m_field.add_finite_element(fe_name);
282 for (auto field : {part.scalarField(), part.vectorField()}) {
286 }
287 CHKERR m_field.add_ents_to_finite_element_by_dim(ents, dim, fe_name);
288 CHKERR m_field.modify_problem_add_finite_element(PROBLEM, fe_name);
290 };
291
292 for (auto &part : parts) {
293 // Hexes have H1 bases only in the Demkowicz family; simplices only in
294 // the Ainsworth family. Quads take either.
295 const auto base =
296 part.type == MBHEX ? DEMKOWICZ_JACOBI_BASE : AINSWORTH_LEGENDRE_BASE;
297 Range ents = part.elements;
298 for (int d = 0; d != part.dim; ++d)
299 CHKERR moab.get_adjacencies(part.elements, d, false, ents,
300 moab::Interface::UNION);
301 for (auto [field, nb] : {std::make_pair(part.scalarField(), 1),
302 std::make_pair(part.vectorField(), 3)}) {
303 CHKERR m_field.add_field(field, H1, base, nb);
304 CHKERR m_field.add_ents_to_field_by_dim(part.elements, part.dim, field);
305 CHKERR m_field.set_field_order(ents, field, 1);
306 }
307 CHKERR add_fe(part.feName(), part, part.elements, part.dim);
308 if (part.dim == 3) {
309 Skinner skin(&moab);
310 Range skin_faces;
311 CHKERR skin.find_skin(0, part.elements, false, skin_faces);
312 CHKERR add_fe(part.skinFeName(), part, skin_faces, 2);
313 }
314 }
315
316 CHKERR m_field.build_fields();
318 CHKERR m_field.build_adjacencies(BIT);
319
320 auto *problems = m_field.getInterface<ProblemsManager>();
321 CHKERR problems->buildProblem(PROBLEM, true);
322 CHKERR problems->partitionSimpleProblem(PROBLEM);
323 CHKERR problems->partitionFiniteElements(PROBLEM);
324 CHKERR problems->partitionGhostDofs(PROBLEM);
325
326 // Field DOFs equal to the vertex tag values.
327 auto *blas = m_field.getInterface<FieldBlas>();
328 for (auto &part : parts) {
329 CHKERR blas->setVertexDofs(
330 [](VectorAdaptor &&data, double *x, double *y, double *z) {
331 const double c[] = {*x, *y, *z};
332 data[0] = scalarAt(c);
333 return 0;
334 },
335 part.scalarField());
336 CHKERR blas->setVertexDofs(
337 [](VectorAdaptor &&data, double *x, double *y, double *z) {
338 const double c[] = {*x, *y, *z};
339 for (int k = 0; k != 3; ++k)
340 data[k] = vectorAt(k, c);
341 return 0;
342 },
343 part.vectorField());
344 }
345
347}
348
349boost::shared_ptr<ForcesAndSourcesCore> makeElement(MoFEM::Interface &m_field,
350 const int dim) {
351 boost::shared_ptr<ForcesAndSourcesCore> fe;
352 switch (dim) {
353 case 1:
354 fe = boost::make_shared<EdgeElementForcesAndSourcesCore>(m_field);
355 break;
356 case 2:
357 fe = boost::make_shared<FaceElementForcesAndSourcesCore>(m_field);
358 break;
359 default:
360 fe = boost::make_shared<VolumeElementForcesAndSourcesCore>(m_field);
361 }
362 fe->getRuleHook = [](int, int, int) { return 3; };
363 return fe;
364}
365
366UDO *makeCheck(CheckFun fun) {
367 auto op = new UDO(NOSPACE, UDO::OPSPACE);
368 op->doWorkRhsHook = [fun](DataOperator *op_ptr, int, EntityType,
370 return fun(static_cast<UDO *>(op_ptr));
371 };
372 return op;
373}
374
375/// Maximum absolute error per check, in a fixed order on all ranks
376struct Errors {
377 std::vector<std::string> names;
378 std::map<std::string, double> max;
379 void add(const std::string &name, const double error) {
380 if (!max.count(name)) {
381 names.push_back(name);
382 max[name] = 0;
383 }
384 max[name] = std::isfinite(error) ? std::max(max[name], std::abs(error))
385 : std::numeric_limits<double>::infinity();
386 }
389 for (auto &name : names)
390 CHKERR testing.checkError<TestType::POINTWISE>(name, max.at(name), 1.,
391 TOL);
393 }
394};
395
396struct FromTagsTest {
397
398 FromTagsTest(MoFEM::Interface &m_field, TestingInterface &testing,
399 std::vector<Part> &parts)
400 : mField(m_field), testing(testing), parts(parts) {}
401
402 MoFEMErrorCode testEntity();
403 MoFEMErrorCode testVertex();
404 MoFEMErrorCode testH1();
405 MoFEMErrorCode testNotation();
406 MoFEMErrorCode testSide();
407 MoFEMErrorCode testErrors();
408 MoFEMErrorCode testFactory();
409
410private:
413 std::vector<Part> &parts;
414
415 using PushOps =
416 boost::function<MoFEMErrorCode(Part &, boost::ptr_deque<UDO> &)>;
417
418 /// Run one pipeline per part and check that every element was visited
419 MoFEMErrorCode loopParts(const std::string &test, PushOps push_ops) {
421 for (auto &part : parts) {
422 auto fe = makeElement(mField, part.dim);
423 int nb_elements = 0;
424 CHKERR push_ops(part, fe->getOpPtrVector());
425 fe->getOpPtrVector().push_back(makeCheck([&](UDO *) {
426 ++nb_elements;
427 return 0;
428 }));
429 CHKERR mField.loop_finite_elements(PROBLEM, part.feName(), *fe);
430 CHKERR testing.checkValue<TestType::SCALAR>(
431 test + " " + part.name + " elements", nb_elements,
432 part.elements.size(), {0., 0.});
433 }
435 }
436};
437
438MoFEMErrorCode FromTagsTest::testEntity() {
440 Errors errors;
441 CHKERR loopParts("entity", [&](Part &part, auto &pip) {
443 auto s_ptr = boost::make_shared<VectorDouble>();
444 auto v_ptr = boost::make_shared<MatrixDouble>();
445 auto p_ptr = boost::make_shared<MatrixDouble>();
446 pip.push_back(createOpCalculateScalarFieldValuesFromTags("S_ENTITY", s_ptr,
447 TagSpace::ENTITY));
449 "V_ENTITY", v_ptr, TagSpace::ENTITY));
451 "SYM3_VOIGT_PADDED", p_ptr, TagSpace::ENTITY, TagNotation::VOIGT,
452 true));
453 pip.push_back(makeCheck([&, s_ptr, v_ptr, p_ptr](UDO *op) {
455 const auto ent = op->getFEEntityHandle();
456 const int nb_gauss = op->getGaussPts().size2();
457 const EntityHandle *conn;
458 int nb_conn;
459 CHKERR mField.get_moab().get_connectivity(ent, conn, nb_conn, true);
460 double x[3];
461 CHKERR mField.get_moab().get_coords(conn, 1, x);
462 if (static_cast<int>(s_ptr->size()) != nb_gauss || nb_gauss == 0)
463 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
464 "Wrong number of values");
465 auto t_v = getFTensor1FromMat<3>(*v_ptr);
466 auto t_p = getFTensor2SymmetricFromMat<3>(*p_ptr);
467 for (int gg = 0; gg != nb_gauss; ++gg) {
468 errors.add(part.name + " scalar entity",
469 (*s_ptr)[gg] - entityAt(ent, 0));
470 for (int k = 0; k != 3; ++k)
471 errors.add(part.name + " vector entity", t_v(k) - entityAt(ent, k));
472 for (int i = 0; i != 3; ++i)
473 for (int j = 0; j != 3; ++j)
474 errors.add(part.name + " padded voigt entity",
475 t_p(i, j) - symmetricAt(i, j, x));
476 ++t_v;
477 ++t_p;
478 }
480 }));
482 });
483 CHKERR errors.check(testing);
485}
486
487MoFEMErrorCode FromTagsTest::testVertex() {
489 Errors errors;
490 CHKERR loopParts("vertex", [&](Part &part, auto &pip) {
492 auto s_ptr = boost::make_shared<VectorDouble>();
493 auto v_ptr = boost::make_shared<MatrixDouble>();
494 pip.push_back(createOpCalculateScalarFieldValuesFromTags("S_VERTEX", s_ptr,
495 TagSpace::VERTEX));
497 "V_VERTEX", v_ptr, TagSpace::VERTEX));
498 pip.push_back(makeCheck([&, s_ptr, v_ptr](UDO *op) {
500 auto &coords = op->getCoordsAtGaussPts();
501 const int nb_gauss = op->getGaussPts().size2();
502 if (static_cast<int>(s_ptr->size()) != nb_gauss || nb_gauss == 0)
503 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
504 "Wrong number of values");
505 auto t_v = getFTensor1FromMat<3>(*v_ptr);
506 for (int gg = 0; gg != nb_gauss; ++gg) {
507 const double *x = &coords(gg, 0);
508 errors.add(part.name + " scalar vertex", (*s_ptr)[gg] - scalarAt(x));
509 for (int k = 0; k != 3; ++k)
510 errors.add(part.name + " vector vertex", t_v(k) - vectorAt(k, x));
511 ++t_v;
512 }
514 }));
516 });
517 CHKERR errors.check(testing);
519}
520
521MoFEMErrorCode FromTagsTest::testH1() {
523 Errors errors;
524 CHKERR loopParts("h1", [&](Part &part, auto &pip) {
526 auto s_ptr = boost::make_shared<VectorDouble>();
527 auto v_ptr = boost::make_shared<MatrixDouble>();
528 auto u_ptr = boost::make_shared<VectorDouble>();
529 auto w_ptr = boost::make_shared<MatrixDouble>();
530 pip.push_back(createOpCalculateScalarFieldValuesFromTags("S_VERTEX", s_ptr,
531 TagSpace::VERTEX));
533 "V_VERTEX", v_ptr, TagSpace::VERTEX));
534 pip.push_back(new OpCalculateScalarFieldValues(part.scalarField(), u_ptr));
535 pip.push_back(
536 new OpCalculateVectorFieldValues<3>(part.vectorField(), w_ptr));
537 pip.push_back(makeCheck([&, s_ptr, v_ptr, u_ptr, w_ptr](UDO *op) {
539 const int nb_gauss = op->getGaussPts().size2();
540 if (static_cast<int>(u_ptr->size()) != nb_gauss || nb_gauss == 0)
541 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
542 "Wrong number of values");
543 auto t_v = getFTensor1FromMat<3>(*v_ptr);
544 auto t_w = getFTensor1FromMat<3>(*w_ptr);
545 for (int gg = 0; gg != nb_gauss; ++gg) {
546 errors.add(part.name + " scalar h1", (*s_ptr)[gg] - (*u_ptr)[gg]);
547 for (int k = 0; k != 3; ++k)
548 errors.add(part.name + " vector h1", t_v(k) - t_w(k));
549 ++t_v;
550 ++t_w;
551 }
553 }));
555 });
556 CHKERR errors.check(testing);
558}
559
560MoFEMErrorCode FromTagsTest::testNotation() {
562 Errors errors;
563 CHKERR loopParts("notation", [&](Part &part, auto &pip) {
565 std::map<std::string, boost::shared_ptr<MatrixDouble>> ptr;
566 for (auto &t : tensorTags())
567 ptr[t.name] = boost::make_shared<MatrixDouble>();
568 for (auto [name, notation] :
569 {std::make_pair("FTENSOR", TagNotation::FTENSOR),
570 std::make_pair("VOIGT", TagNotation::VOIGT)}) {
571 const std::string n = name;
573 "SYM2_" + n, ptr["SYM2_" + n], TagSpace::VERTEX, notation));
575 "SYM3_" + n, ptr["SYM3_" + n], TagSpace::VERTEX, notation));
577 "FULL2_" + n, ptr["FULL2_" + n], TagSpace::VERTEX, notation));
579 "FULL3_" + n, ptr["FULL3_" + n], TagSpace::VERTEX, notation));
580 }
582 "FULL32_FTENSOR", ptr["FULL32_FTENSOR"], TagSpace::VERTEX));
583
584 pip.push_back(makeCheck([&, ptr](UDO *op) {
586 auto &coords = op->getCoordsAtGaussPts();
587 const int nb_gauss = op->getGaussPts().size2();
588 if (nb_gauss == 0)
589 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
590 "No integration points");
591 for (auto n : {"FTENSOR", "VOIGT"}) {
592 const std::string name = n;
593 auto t_s2 = getFTensor2SymmetricFromMat<2>(*ptr.at("SYM2_" + name));
594 auto t_s3 = getFTensor2SymmetricFromMat<3>(*ptr.at("SYM3_" + name));
595 auto t_f2 = getFTensor2FromMat<2, 2>(*ptr.at("FULL2_" + name));
596 auto t_f3 = getFTensor2FromMat<3, 3>(*ptr.at("FULL3_" + name));
597 for (int gg = 0; gg != nb_gauss; ++gg) {
598 const double *x = &coords(gg, 0);
599 for (int i = 0; i != 3; ++i) {
600 for (int j = 0; j != 3; ++j) {
601 if (i < 2 && j < 2) {
602 errors.add(part.name + " sym2 " + name,
603 t_s2(i, j) - symmetricAt(i, j, x));
604 errors.add(part.name + " full2 " + name,
605 t_f2(i, j) - fullAt(i, j, x));
606 }
607 errors.add(part.name + " sym3 " + name,
608 t_s3(i, j) - symmetricAt(i, j, x));
609 errors.add(part.name + " full3 " + name,
610 t_f3(i, j) - fullAt(i, j, x));
611 }
612 }
613 ++t_s2;
614 ++t_s3;
615 ++t_f2;
616 ++t_f3;
617 }
618 }
619 auto t_f32 = getFTensor2FromMat<3, 2>(*ptr.at("FULL32_FTENSOR"));
620 for (int gg = 0; gg != nb_gauss; ++gg) {
621 const double *x = &coords(gg, 0);
622 for (int i = 0; i != 3; ++i)
623 for (int j = 0; j != 2; ++j)
624 errors.add(part.name + " full32 FTENSOR",
625 t_f32(i, j) - fullAt(i, j, x));
626 ++t_f32;
627 }
629 }));
631 });
632 CHKERR errors.check(testing);
634}
635
636MoFEMErrorCode FromTagsTest::testSide() {
638 Errors errors;
639 for (auto &part : parts) {
640 if (part.dim != 3)
641 continue;
642 auto fe = makeElement(mField, 2);
643 auto face_coords = boost::make_shared<MatrixDouble>();
644 int nb_faces = 0, nb_sides = 0;
645 fe->getOpPtrVector().push_back(makeCheck([&, face_coords](UDO *op) {
646 *face_coords = op->getCoordsAtGaussPts();
647 ++nb_faces;
648 return 0;
649 }));
651 mField, part.feName(), 3, Sev::noisy);
652 auto s_ptr = boost::make_shared<VectorDouble>();
653 op_side->getOpPtrVector().push_back(
655 TagSpace::VERTEX));
656 op_side->getOpPtrVector().push_back(
657 makeCheck([&, face_coords, s_ptr](UDO *op) {
659 const int nb_gauss = face_coords->size1();
660 if (static_cast<int>(s_ptr->size()) != nb_gauss || nb_gauss == 0)
661 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
662 "Side and face integration points differ");
663 for (int gg = 0; gg != nb_gauss; ++gg)
664 errors.add(part.name + " side scalar vertex",
665 (*s_ptr)[gg] - scalarAt(&(*face_coords)(gg, 0)));
666 ++nb_sides;
668 }));
669 fe->getOpPtrVector().push_back(op_side);
670 CHKERR mField.loop_finite_elements(PROBLEM, part.skinFeName(), *fe);
671 // Every skin face has exactly one adjacent element.
672 CHKERR testing.checkValue<TestType::SCALAR>("side " + part.name + " visits",
673 nb_sides, nb_faces, {0., 0.});
674 if (nb_faces == 0)
675 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID, "No skin faces");
676 }
677 CHKERR errors.check(testing);
679}
680
681MoFEMErrorCode FromTagsTest::testErrors() {
683 auto &part = *std::find_if(parts.begin(), parts.end(),
684 [](auto &p) { return p.type == MBTET; });
685
686 auto run = [&](UDO *op) {
687 auto fe = makeElement(mField, part.dim);
688 fe->getOpPtrVector().push_back(op);
689 return mField.loop_finite_elements(PROBLEM, part.feName(), *fe);
690 };
691
692 auto rejects = [&](const std::string &name, UDO *op) {
694 CHKERR PetscPushErrorHandler(PetscReturnErrorHandler, nullptr);
695 const auto error = run(op);
696 CHKERR PetscPopErrorHandler();
697 CHKERR testing.checkValue<TestType::SCALAR>(
698 "rejects " + name, error, MOFEM_DATA_INCONSISTENCY, {0., 0.});
700 };
701
702 auto s_ptr = boost::make_shared<VectorDouble>();
704 "NO_SUCH_TAG", s_ptr, TagSpace::VERTEX));
706 "BAD_LENGTH", s_ptr, TagSpace::VERTEX));
708 "BAD_TYPE", s_ptr, TagSpace::VERTEX));
709 CHKERR rejects("variable length", createOpCalculateScalarFieldValuesFromTags(
710 "BAD_VARLEN", s_ptr, TagSpace::VERTEX));
711 CHKERR rejects("missing value",
713 "SPARSE_PARTIAL", s_ptr, TagSpace::VERTEX));
714 CHKERR rejects("missing entity value",
716 "SPARSE_PARTIAL", s_ptr, TagSpace::ENTITY));
717
718 // The same longer tag is accepted when asked for.
720 "BAD_LENGTH", s_ptr, TagSpace::VERTEX, true));
722}
723
724MoFEMErrorCode FromTagsTest::testFactory() {
726 auto throws = [&](const std::string &name, auto create) {
728 bool thrown = false;
729 try {
730 delete create();
731 } catch (MoFEMException const &) {
732 thrown = true;
733 }
734 CHKERR testing.checkValue<TestType::SCALAR>("throws " + name, thrown, 1,
735 {0., 0.});
737 };
738 auto mat_ptr = boost::make_shared<MatrixDouble>();
739 CHKERR throws("voigt non-square", [&]() {
741 "FULL32_FTENSOR", mat_ptr, TagSpace::VERTEX, TagNotation::VOIGT);
742 });
743 CHKERR throws("null pointer", [&]() {
744 return createOpCalculateScalarFieldValuesFromTags("S_VERTEX", nullptr,
745 TagSpace::VERTEX);
746 });
747 CHKERR throws("unknown space", [&]() {
749 "V_VERTEX", mat_ptr, static_cast<TagSpace>(7));
750 });
751 CHKERR throws("unknown notation", [&]() {
753 "SYM3_VOIGT", mat_ptr, TagSpace::VERTEX, static_cast<TagNotation>(7));
754 });
756}
757
758} // namespace
759
760int main(int argc, char *argv[]) {
761 MoFEM::Core::Initialize(&argc, &argv, nullptr, help);
762 try {
763 moab::Core mb_instance;
764 moab::Interface &moab = mb_instance;
765
766 std::vector<Part> parts;
767 CHKERR createParts(moab, parts);
768 CHKERR createTags(moab, parts);
769
770 MoFEM::Core core(moab);
771 MoFEM::Interface &m_field = core;
772 CHKERR setUpProblem(m_field, parts);
773
774 std::unique_ptr<TestingInterface> testing(
776 FromTagsTest test(m_field, *testing, parts);
777 CHKERR testing->addTestCommandLine("", 1, "entity",
778 [&]() { return test.testEntity(); });
779 CHKERR testing->addTestCommandLine("", 2, "vertex",
780 [&]() { return test.testVertex(); });
781 CHKERR testing->addTestCommandLine("", 3, "h1",
782 [&]() { return test.testH1(); });
783 CHKERR testing->addTestCommandLine("", 4, "notation",
784 [&]() { return test.testNotation(); });
785 CHKERR testing->addTestCommandLine("", 5, "side",
786 [&]() { return test.testSide(); });
787 CHKERR testing->addTestCommandLine("", 6, "errors",
788 [&]() { return test.testErrors(); });
789 CHKERR testing->addTestCommandLine("", 7, "factory",
790 [&]() { return test.testFactory(); });
791 CHKERR testing->runTests();
792 }
795 return 0;
796}
MoFEM::Interface & mField
FaceSet skin
double length
WorkMesh base
Flags flags
Vec t
Removal: the ring edge.
int components
Inferred from the source tag length.
ProjectedField & field
std::string type
int nb
number of DOFs
Definition Schur.cpp:2505
SmartPetscObj< Vec > values
std::string name
int main()
#define CATCH_ERRORS
Catch errors.
@ AINSWORTH_LEGENDRE_BASE
Ainsworth Cole (Legendre) approx. base .
Definition definitions.h:60
@ DEMKOWICZ_JACOBI_BASE
Definition definitions.h:66
@ H1
continuous field
Definition definitions.h:85
@ NOSPACE
Definition definitions.h:83
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define CHK_MOAB_THROW(err, msg)
Check error code of MoAB function and throw MoFEM exception.
@ MOFEM_ATOM_TEST_INVALID
Definition definitions.h:40
@ MOFEM_DATA_INCONSISTENCY
Definition definitions.h:31
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
auto fun
static char help[]
static const double face_coords[4][9]
virtual MoFEMErrorCode add_ents_to_finite_element_by_dim(const EntityHandle entities, const int dim, const std::string name, const bool recursive=true)=0
add entities to finite element
virtual MoFEMErrorCode add_finite_element(const std::string &fe_name, enum MoFEMTypes bh=MF_EXCL, int verb=DEFAULT_VERBOSITY)=0
add finite element
virtual MoFEMErrorCode build_finite_elements(int verb=DEFAULT_VERBOSITY)=0
Build finite elements.
virtual MoFEMErrorCode modify_finite_element_add_field_col(const std::string &fe_name, const std::string name_row)=0
set field col which finite element use
virtual MoFEMErrorCode modify_finite_element_add_field_row(const std::string &fe_name, const std::string name_row)=0
set field row which finite element use
virtual MoFEMErrorCode modify_finite_element_add_field_data(const std::string &fe_name, const std::string name_field)=0
set finite element field data
virtual MoFEMErrorCode build_fields(int verb=DEFAULT_VERBOSITY)=0
virtual MoFEMErrorCode add_ents_to_field_by_dim(const Range &ents, const int dim, const std::string &name, int verb=DEFAULT_VERBOSITY)=0
Add entities to field meshset.
virtual MoFEMErrorCode set_field_order(const EntityHandle meshset, const EntityType type, const std::string &name, const ApproximationOrder order, int verb=DEFAULT_VERBOSITY)=0
Set order approximation of the entities in the field.
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.
virtual MoFEMErrorCode loop_finite_elements(const std::string problem_name, const std::string &fe_name, FEMethod &method, boost::shared_ptr< NumeredEntFiniteElement_multiIndex > fe_ptr=nullptr, MoFEMTypes bh=MF_EXIST, CacheTupleWeakPtr cache_ptr=CacheTupleSharedPtr(), int verb=DEFAULT_VERBOSITY)=0
Make a loop over finite elements.
MoFEMErrorCode buildProblem(const std::string name, const bool square_matrix, int verb=VERBOSE)
build problem data structures
virtual MoFEMErrorCode add_problem(const std::string &name, enum MoFEMTypes bh=MF_EXCL, int verb=DEFAULT_VERBOSITY)=0
Add problem.
virtual MoFEMErrorCode modify_problem_ref_level_add_bit(const std::string &name_problem, const BitRefLevel &bit)=0
add ref level to problem
virtual MoFEMErrorCode modify_problem_add_finite_element(const std::string name_problem, const std::string &fe_name)=0
add finite element to problem, this add entities assigned to finite element to a particular problem
FTensor::Index< 'i', SPACE_DIM > i
PetscBool testing
const double c
speed of light (cm/ns)
const double v
phase velocity of light in medium (cm/ns)
FTensor::Index< 'j', 3 > j
FTensor::Index< 'k', 3 > k
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
VectorShallowArrayAdaptor< double > VectorAdaptor
Definition Types.hpp:115
std::bitset< BITREFLEVEL_SIZE > BitRefLevel
Bit structure attached to each entity identifying to what mesh entity is attached.
Definition Types.hpp:40
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)
TestingInterface * createInterface< TestingInterface >(const Core &core)
template ForcesAndSourcesCore::UserDataOperator * createOpCalculateTensor2FieldValuesFromTags< 2, 2 >(const std::string, boost::shared_ptr< MatrixDouble >, const TagSpace, const TagNotation, const bool)
template ForcesAndSourcesCore::UserDataOperator * createOpCalculateTensor2SymmetricFieldValuesFromTags< 3 >(const std::string, boost::shared_ptr< MatrixDouble >, const TagSpace, const TagNotation, const bool)
template ForcesAndSourcesCore::UserDataOperator * createOpCalculateTensor2SymmetricFieldValuesFromTags< 2 >(const std::string, boost::shared_ptr< MatrixDouble >, const TagSpace, const TagNotation, const bool)
template ForcesAndSourcesCore::UserDataOperator * createOpCalculateTensor2FieldValuesFromTags< 3, 2 >(const std::string, boost::shared_ptr< MatrixDouble >, const TagSpace, const TagNotation, const bool)
template ForcesAndSourcesCore::UserDataOperator * createOpCalculateVectorFieldValuesFromTags< 3 >(const std::string, boost::shared_ptr< MatrixDouble >, const TagSpace, const bool)
template ForcesAndSourcesCore::UserDataOperator * createOpCalculateTensor2FieldValuesFromTags< 3, 3 >(const std::string, boost::shared_ptr< MatrixDouble >, const TagSpace, const TagNotation, const bool)
MoFEMErrorCode run(MoFEM::Interface &m_field)
Definition sdf_python.cpp:7
Managing BitRefLevels.
virtual moab::Interface & get_moab()=0
virtual MoFEMErrorCode build_adjacencies(const Range &ents, int verb=DEFAULT_VERBOSITY)=0
build adjacencies
virtual MoFEMErrorCode add_field(const std::string name, const FieldSpace space, const FieldApproximationBase base, const FieldCoefficientsNumber nb_of_coefficients, const TagType tag_type=MB_TAG_SPARSE, const enum MoFEMTypes bh=MF_EXCL, int verb=DEFAULT_VERBOSITY)=0
Add field.
Core (interface) class.
Definition Core.hpp:83
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
Definition Core.cpp:68
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition Core.cpp:123
base operator to do operations at Gauss Pt. level
Deprecated interface functions.
Data on single entity (This is passed as argument to DataOperator::doWork)
Basic algebra on fields.
Definition FieldBlas.hpp:21
MoFEMErrorCode setVertexDofs(VertexCoordsFunction lambda, const std::string field_name, Range *verts=nullptr)
Set DOFs on vertices using user function.
Specialization for double precision scalar field values calculation.
Specialization for MatrixDouble vector field values calculation.
Element used to execute operators on side of the element.
Problem manager is used to build and partition problems.
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.