v0.16.3
Loading...
Searching...
No Matches
FieldCore.cpp
Go to the documentation of this file.
1/** \file FieldCore.cpp
2 * \brief Core interface methods for managing fields.
3 */
4
5
6#include <MoFEM.hpp>
7
8#define FieldCoreFunctionBegin \
9 MoFEMFunctionBegin; \
10 MOFEM_LOG_CHANNEL("WORLD"); \
11 MOFEM_LOG_CHANNEL("SYNC"); \
12 MOFEM_LOG_FUNCTION(); \
13 MOFEM_LOG_TAG("WORLD", "FieldCore"); \
14 MOFEM_LOG_TAG("SYNC", "FieldCore");
15
16namespace MoFEM {
17
18BitFieldId Core::get_field_id(const std::string &name) const {
19 auto &set = fIelds.get<FieldName_mi_tag>();
20 auto miit = set.find(name);
21 if (miit == set.end()) {
22 THROW_MESSAGE("field < " + name +
23 " > not in database (top tip: check spelling)");
24 }
25 return (*miit)->getId();
26}
27
29 auto &set = fIelds.get<FieldName_mi_tag>();
30 auto miit = set.find(name);
31 if (miit == set.end())
32 THROW_MESSAGE("field < " + name +
33 " > not in database (top tip: check spelling)");
34 return (*miit)->getBitNumber();
35}
36
37std::string Core::get_field_name(const BitFieldId id) const {
38 auto &set = fIelds.get<BitFieldId_mi_tag>();
39 auto miit = set.find(id);
40 if (miit == set.end())
41 THROW_MESSAGE("field not in database (top tip: check spelling)");
42 return (*miit)->getName();
43}
44
45EntityHandle Core::get_field_meshset(const BitFieldId id) const {
46 auto &set = fIelds.get<BitFieldId_mi_tag>();
47 auto miit = set.find(id);
48 if (miit == set.end())
49 THROW_MESSAGE("field not in database (top tip: check spelling)");
50 return (*miit)->meshSet;
51}
52
53EntityHandle Core::get_field_meshset(const std::string name) const {
54 return get_field_meshset(get_field_id(name));
55}
56
57bool Core::check_field(const std::string &name) const {
58 auto miit = fIelds.get<FieldName_mi_tag>().find(name);
59 if (miit == fIelds.get<FieldName_mi_tag>().end())
60 return false;
61 else
62 return true;
63}
64
65const Field *Core::get_field_structure(const std::string &name,
66 enum MoFEMTypes bh) const {
67 auto miit = fIelds.get<FieldName_mi_tag>().find(name);
68 if (miit == fIelds.get<FieldName_mi_tag>().end()) {
69 if (bh == MF_EXIST)
70 throw MoFEMException(
72 std::string("field < " + name +
73 " > not in database (top tip: check spelling)")
74 .c_str());
75 else
76 return nullptr;
77 }
78 return miit->get();
79}
80
82 int dim,
83 Range &ents) const {
84
86 EntityHandle meshset = get_field_meshset(name);
87 CHKERR get_moab().get_entities_by_dimension(meshset, dim, ents, true);
89}
90
92 EntityType type,
93 Range &ents) const {
95 EntityHandle meshset = get_field_meshset(name);
96 CHKERR get_moab().get_entities_by_type(meshset, type, ents, true);
98}
99
101 Range &ents) const {
103 EntityHandle meshset = get_field_meshset(name);
104 CHKERR get_moab().get_entities_by_handle(meshset, ents, true);
106}
107
108MoFEMErrorCode Core::addField(const std::string &name, const FieldSpace space,
109 const FieldContinuity continuity,
111 const FieldCoefficientsNumber nb_of_coefficients,
112 const TagType tag_type, const enum MoFEMTypes bh,
113 int verb) {
114 MOFEM_LOG_CHANNEL("WORLD");
115 MOFEM_LOG_TAG("WORLD", "FieldCore");
116 MOFEM_LOG_CHANNEL("SYNC");
117 MOFEM_LOG_TAG("SYNC", "FieldCore");
120
121 // Add field mesh set to partion meshset. In case of no elements
122 // on processor part, when mesh file is read, finite element meshset is
123 // prevented from deletion by moab reader.
124 auto add_meshset_to_partition = [&](auto meshset) {
126 const void *tag_vals[] = {&rAnk};
127 ParallelComm *pcomm = ParallelComm::get_pcomm(
128 &get_moab(), get_basic_entity_data_ptr()->pcommID);
129 Tag part_tag = pcomm->part_tag();
130 Range tagged_sets;
131 CHKERR get_moab().get_entities_by_type_and_tag(0, MBENTITYSET, &part_tag,
132 tag_vals, 1, tagged_sets,
133 moab::Interface::UNION);
134 for (auto s : tagged_sets)
135 CHKERR get_moab().add_entities(s, &meshset, 1);
137 };
138
139 auto create_tags = [&](auto meshset, auto id) {
141 CHKERR get_moab().tag_set_data(th_FieldId, &meshset, 1, &id);
142 // space
143 CHKERR get_moab().tag_set_data(th_FieldSpace, &meshset, 1, &space);
144 // continuity
145 CHKERR get_moab().tag_set_data(th_FieldContinuity, &meshset, 1,
146 &continuity);
147 // base
148 CHKERR get_moab().tag_set_data(th_FieldBase, &meshset, 1, &base);
149
150 // name
151 void const *tag_data[] = {name.c_str()};
152 int tag_sizes[1];
153 tag_sizes[0] = name.size();
154 CHKERR get_moab().tag_set_by_ptr(th_FieldName, &meshset, 1, tag_data,
155 tag_sizes);
156 // rank
157 Tag th_rank;
158 int def_rank = 1;
159 const std::string tag_rank_name = "_Field_Rank_" + name;
160 CHKERR get_moab().tag_get_handle(tag_rank_name.c_str(), 1, MB_TYPE_INTEGER,
161 th_rank, MB_TAG_CREAT | MB_TAG_SPARSE,
162 &def_rank);
163 CHKERR get_moab().tag_set_data(th_rank, &meshset, 1, &nb_of_coefficients);
164
165 Version file_ver;
166 ierr = UnknownInterface::getFileVersion(moab, file_ver);
167 CHK_THROW_MESSAGE(ierr, "Not known file version");
168 if (file_ver.majorVersion >= 0 && file_ver.minorVersion >= 12 &&
169 file_ver.buildVersion >= 1) {
170
171 // Change tag names comparing to older versions
172
173 const std::string name_data_prefix("_App_Data_");
174 void const *tag_prefix_data[] = {name_data_prefix.c_str()};
175 int tag_prefix_sizes[1];
176 tag_prefix_sizes[0] = name_data_prefix.size();
177 CHKERR get_moab().tag_set_by_ptr(th_FieldName_DataNamePrefix, &meshset, 1,
178 tag_prefix_data, tag_prefix_sizes);
179 Tag th_app_order, th_field_data, th_field_data_vert;
180
181 // order
182 ApproximationOrder def_approx_order = -1;
183 const std::string tag_approx_order_name = "_App_Order_" + name;
184 CHKERR get_moab().tag_get_handle(
185 tag_approx_order_name.c_str(), 1, MB_TYPE_INTEGER, th_app_order,
186 MB_TAG_CREAT | MB_TAG_SPARSE, &def_approx_order);
187
188 // data
189 std::string tag_data_name = name_data_prefix + name;
190 const int def_len = 0;
191 CHKERR get_moab().tag_get_handle(
192 tag_data_name.c_str(), def_len, MB_TYPE_DOUBLE, th_field_data,
193 MB_TAG_CREAT | MB_TAG_VARLEN | MB_TAG_SPARSE, NULL);
194 std::string tag_data_name_verts = name_data_prefix + name + "_V";
195 VectorDouble def_vert_data(nb_of_coefficients);
196 def_vert_data.clear();
197 CHKERR get_moab().tag_get_handle(
198 tag_data_name_verts.c_str(), nb_of_coefficients, MB_TYPE_DOUBLE,
199 th_field_data_vert, MB_TAG_CREAT | tag_type, &*def_vert_data.begin());
200 } else {
201 // Deprecated names
202 const std::string name_data_prefix("_App_Data");
203 void const *tag_prefix_data[] = {name_data_prefix.c_str()};
204 int tag_prefix_sizes[1];
205 tag_prefix_sizes[0] = name_data_prefix.size();
206 CHKERR get_moab().tag_set_by_ptr(th_FieldName_DataNamePrefix, &meshset, 1,
207 tag_prefix_data, tag_prefix_sizes);
208 Tag th_app_order, th_field_data, th_field_data_vert;
209
210 // order
211 ApproximationOrder def_approx_order = -1;
212 const std::string tag_approx_order_name = "_App_Order_" + name;
213 CHKERR get_moab().tag_get_handle(
214 tag_approx_order_name.c_str(), 1, MB_TYPE_INTEGER, th_app_order,
215 MB_TAG_CREAT | MB_TAG_SPARSE, &def_approx_order);
216
217 // data
218 std::string tag_data_name = name_data_prefix + name;
219 const int def_len = 0;
220 CHKERR get_moab().tag_get_handle(
221 tag_data_name.c_str(), def_len, MB_TYPE_DOUBLE, th_field_data,
222 MB_TAG_CREAT | MB_TAG_VARLEN | MB_TAG_SPARSE, NULL);
223 std::string tag_data_name_verts = name_data_prefix + name + "V";
224 VectorDouble def_vert_data(nb_of_coefficients);
225 def_vert_data.clear();
226 CHKERR get_moab().tag_get_handle(
227 tag_data_name_verts.c_str(), nb_of_coefficients, MB_TYPE_DOUBLE,
228 th_field_data_vert, MB_TAG_CREAT | tag_type, &*def_vert_data.begin());
229 }
230
232 };
233
234 if (verb == -1)
235 verb = verbose;
236 *buildMoFEM = 0;
237 auto fit = fIelds.get<FieldName_mi_tag>().find(name);
238 if (fit != fIelds.get<FieldName_mi_tag>().end()) {
239 if (bh == MF_EXCL)
240 SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL,
241 "field is <%s> in database", name.c_str());
242
243 } else {
244
245 EntityHandle meshset;
246 CHKERR get_moab().create_meshset(MESHSET_SET, meshset);
247 CHKERR add_meshset_to_partition(meshset);
248
249 // id
250 int field_shift = 0;
251 for (;
252 fIelds.get<BitFieldId_mi_tag>().find(BitFieldId().set(field_shift)) !=
253 fIelds.get<BitFieldId_mi_tag>().end();
254 ++field_shift) {
255 if (field_shift == BITFEID_SIZE)
256 SETERRQ(PETSC_COMM_SELF, MOFEM_IMPOSSIBLE_CASE,
257 "Maximal number of fields exceeded");
258 }
259
260 auto id = BitFieldId().set(field_shift);
261 CHKERR create_tags(meshset, id);
262
263 auto p = fIelds.insert(boost::make_shared<Field>(moab, meshset));
264 if (verb > QUIET) {
265 MOFEM_LOG("WORLD", Sev::inform) << "Add field " << **p.first;
266 MOFEM_LOG("WORLD", Sev::noisy)
267 << "Field " << (*p.first)->getName() << " core value < "
268 << this->getValue() << " > field value ( "
269 << static_cast<int>((*p.first)->getBitNumber()) << " )";
270 }
271
272 if (!p.second)
273 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_FOUND,
274 "field not inserted %s (top tip, it could be already "
275 "there)",
276 Field(moab, meshset).getName().c_str());
277 }
278
279 MOFEM_LOG_CHANNEL("WORLD");
280 MOFEM_LOG_CHANNEL("SYNC");
282}
283
285 const std::string name, const FieldSpace space,
287 const FieldCoefficientsNumber nb_of_coefficients,
288
289 std::vector<
290
291 std::pair<EntityType,
293
294 >>
295 list_dof_side_map,
296
297 const TagType tag_type, const enum MoFEMTypes bh, int verb) {
299 CHKERR this->addField(name, space, DISCONTINUOUS, base, nb_of_coefficients,
300 tag_type, bh, verb);
301
302 auto fit = fIelds.get<FieldName_mi_tag>().find(name);
303 if (fit == fIelds.get<FieldName_mi_tag>().end())
304 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
305 "field <%s> not in database", name.c_str());
306
307 for(auto &p : list_dof_side_map) {
308 auto &data_side_map = (*fit)->getDofSideMap()[p.first];
309 CHKERR p.second(data_side_map);
310 }
311
313}
314
315MoFEMErrorCode Core::add_field(const std::string name, const FieldSpace space,
317 const FieldCoefficientsNumber nb_of_coefficients,
318 const TagType tag_type, const enum MoFEMTypes bh,
319 int verb) {
320 return this->addField(name, space, CONTINUOUS, base, nb_of_coefficients,
321 tag_type, bh, verb);
322}
323
325 const std::string &name, int verb) {
326
327 *buildMoFEM = 0;
328 EntityHandle idm = no_handle;
329 if (verb == -1)
330 verb = verbose;
331
332 MOFEM_LOG_CHANNEL("SYNC");
333 MOFEM_LOG_TAG("SYNC", "FieldCore");
335
337 idm = get_field_meshset(name);
338 FieldSpace space;
339 CHKERR get_moab().tag_get_data(th_FieldSpace, &idm, 1, &space);
340 FieldContinuity continuity;
341 CHKERR get_moab().tag_get_data(th_FieldContinuity, &idm, 1, &continuity);
342
343 switch (continuity) {
344 case CONTINUOUS:
345 case DISCONTINUOUS:
346 break;
347 default:
348 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
349 "sorry, unknown continuity added to entity");
350 }
351
352 std::vector<int> nb_ents_on_dim(3, 0);
353 switch (space) {
354 case L2:
355 CHKERR get_moab().add_entities(idm, ents);
356 break;
357 case H1:
358 CHKERR get_moab().add_entities(idm, ents);
359 if (continuity == DISCONTINUOUS)
360 break;
361 for (int dd = 0; dd != dim; ++dd) {
362 Range adj_ents;
363 CHKERR get_moab().get_adjacencies(ents, dd, false, adj_ents,
364 moab::Interface::UNION);
365 if (dd == 0) {
366 Range topo_nodes;
367 CHKERR get_moab().get_connectivity(ents, topo_nodes, true);
368 Range mid_nodes;
369 CHKERR get_moab().get_connectivity(ents, mid_nodes, false);
370 mid_nodes = subtract(mid_nodes, topo_nodes);
371 adj_ents = subtract(adj_ents, mid_nodes);
372 }
373 CHKERR get_moab().add_entities(idm, adj_ents);
374 nb_ents_on_dim[dd] = adj_ents.size();
375 }
376 break;
377 case HCURL:
378 CHKERR get_moab().add_entities(idm, ents);
379 if (continuity == DISCONTINUOUS)
380 break;
381 for (int dd = 1; dd != dim; ++dd) {
382 Range adj_ents;
383 CHKERR get_moab().get_adjacencies(ents, dd, false, adj_ents,
384 moab::Interface::UNION);
385 CHKERR get_moab().add_entities(idm, adj_ents);
386 nb_ents_on_dim[dd] = adj_ents.size();
387 }
388 break;
389 case HDIV:
390 CHKERR get_moab().add_entities(idm, ents);
391 if (continuity == DISCONTINUOUS)
392 break;
393 if (dim > 2) {
394 Range adj_ents;
395 CHKERR get_moab().get_adjacencies(ents, 2, false, adj_ents,
396 moab::Interface::UNION);
397 CHKERR get_moab().add_entities(idm, adj_ents);
398 nb_ents_on_dim[2] = adj_ents.size();
399 }
400 break;
401 default:
402 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
403 "sorry, unknown space added to entity");
404 }
405
406 if (verb >= VERBOSE) {
407 MOFEM_LOG("SYNC", Sev::noisy) << "add entities to field " << name;
408 MOFEM_LOG("SYNC", Sev::noisy) << "\tnb. add ents " << ents.size();
409 MOFEM_LOG("SYNC", Sev::noisy) << "\tnb. add faces " << nb_ents_on_dim[2];
410 MOFEM_LOG("SYNC", Sev::noisy) << "\tnb. add edges " << nb_ents_on_dim[1];
411 MOFEM_LOG("SYNC", Sev::noisy) << "\tnb. add nodes " << nb_ents_on_dim[0];
412 }
414}
415
417 const std::string &name,
418 int verb) {
420 if (verb == -1)
421 verb = verbose;
422 Range ents_dim = ents.subset_by_dimension(dim);
423 CHKERR addEntsToFieldByDim(ents_dim, dim, name, verb);
424 // A quiet call may be made independently on one rank.
425 if (verb > QUIET)
426 MOFEM_LOG_SYNCHRONISE(mofemComm);
428}
429
431 const EntityType type,
432 const std::string &name,
433 int verb) {
435 Range ents_type = ents.subset_by_type(type);
436 if (!ents_type.empty()) {
437 const int dim = get_moab().dimension_from_handle(ents_type[0]);
438 CHKERR addEntsToFieldByDim(ents_type, dim, name, verb);
439 }
440 MOFEM_LOG_SYNCHRONISE(mofemComm);
442}
443
445 const int dim,
446 const std::string &name,
447 const bool recursive, int verb) {
449 Range ents;
450 CHKERR get_moab().get_entities_by_dimension(meshset, dim, ents, recursive);
451 CHKERR addEntsToFieldByDim(ents, dim, name, verb);
452 MOFEM_LOG_SYNCHRONISE(mofemComm);
454}
455
457 const EntityType type,
458 const std::string &name,
459 const bool recursive, int verb) {
461 Range ents;
462 CHKERR get_moab().get_entities_by_type(meshset, type, ents, recursive);
463 if (!ents.empty()) {
464 const int dim = get_moab().dimension_from_handle(ents[0]);
465 CHKERR addEntsToFieldByDim(ents, dim, name, verb);
466 }
467 MOFEM_LOG_SYNCHRONISE(mofemComm);
469}
470
472 const double coords[],
473 int size, int verb) {
475
476 if (verb == DEFAULT_VERBOSITY)
477 verb = verbose;
478
479 Range verts;
480
481 auto create_vertices = [&]() {
483
484 vector<double *> arrays_coord;
485 EntityHandle startv = 0;
486 ReadUtilIface *iface;
487 CHKERR get_moab().query_interface(iface);
488 CHKERR iface->get_node_coords(3, size, 0, startv, arrays_coord);
489 verts.insert(startv, startv + size - 1);
490 for (int n = 0; n != size; ++n)
491 for (auto d : {0, 1, 2})
492 arrays_coord[d][n] = coords[3 * n + d];
493
495 };
496
497 auto add_verts_to_field = [&]() {
499 EntityHandle field_meshset = get_field_meshset(name);
500 CHKERR get_moab().add_entities(field_meshset, verts);
502 };
503
504 CHKERR create_vertices();
505 CHKERR add_verts_to_field();
506
508}
509
510MoFEMErrorCode Core::setFieldOrderImpl(boost::shared_ptr<Field> field_ptr,
511 const Range &ents,
513 int verb) {
515
516 if (verb == DEFAULT_VERBOSITY)
517 verb = verbose;
518 *buildMoFEM = 0;
519
520 const auto field_meshset = field_ptr->getMeshset();
521 const auto bit_number = field_ptr->getBitNumber();
522
523 // intersection with field meshset
524 Range ents_of_id_meshset;
525 CHKERR get_moab().get_entities_by_handle(field_meshset, ents_of_id_meshset,
526 false);
527 Range field_ents = intersect(ents, ents_of_id_meshset);
528 if (verb > QUIET) {
529 MOFEM_LOG_C("SYNC", Sev::noisy,
530 "change nb. of ents for order in the field <%s> %zu -> %zu",
531 field_ptr->getName().c_str(), ents.size(), field_ents.size());
532 }
533
534 // ent view by field id (in set all MoabEnts has the same FieldId)
535 auto eiit = entsFields.get<Unique_mi_tag>().lower_bound(
536 FieldEntity::getLoBitNumberUId(field_ptr->getBitNumber()));
538 if (eiit != entsFields.get<Unique_mi_tag>().end()) {
539 auto hi_eiit = entsFields.get<Unique_mi_tag>().upper_bound(
540 FieldEntity::getHiBitNumberUId(field_ptr->getBitNumber()));
541 std::copy(eiit, hi_eiit, std::back_inserter(ents_id_view));
542 }
543
544 if (verb > QUIET)
545 MOFEM_LOG_C("SYNC", Sev::noisy,
546 "current nb. of ents in the multi index field <%s> %zu",
547 field_ptr->getName().c_str(), ents_id_view.size());
548
549 // loop over ents
550 int nb_ents_set_order_up = 0;
551 int nb_ents_set_order_down = 0;
552 int nb_ents_set_order_new = 0;
553
554 FieldEntity_change_order modify_order_no_size_change(order, false);
555 FieldEntity_change_order modify_order_size_change(order, true);
556
557 for (auto pit = field_ents.const_pair_begin();
558 pit != field_ents.const_pair_end(); pit++) {
559 EntityHandle first = pit->first;
560 EntityHandle second = pit->second;
561
562 const auto type = static_cast<EntityType>(first >> MB_ID_WIDTH);
563
564 // Sanity check
565 switch (field_ptr->getSpace()) {
566 case H1:
567 break;
568 case HCURL:
569 if (type == MBVERTEX)
570 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
571 "Hcurl space on vertices makes no sense");
572
573 break;
574 case HDIV:
575 if (type == MBVERTEX)
576 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
577 "Hdiv space on vertices makes no sense");
578
579 if (type == MBEDGE)
580 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
581 "Hdiv space on edges makes no sense");
582
583 break;
584 default:
585 break;
586 }
587
588 // Entity is in database, change order only if needed
589 Range ents_in_database;
590 auto vit = ents_id_view.get<1>().lower_bound(first);
591 auto hi_vit = ents_id_view.get<1>().upper_bound(second);
592 if (order >= 0) {
593 for (; vit != hi_vit; ++vit) {
594 ents_in_database.insert(vit->get()->getEnt());
595 // entity is in database and order is changed or reset
596 const ApproximationOrder old_approximation_order =
597 (*vit)->getMaxOrder();
598
599 if (old_approximation_order != order) {
600
601 FieldEntity_multiIndex::iterator miit =
602 entsFields.get<Unique_mi_tag>().find((*vit)->getLocalUniqueId());
603
604 if ((*miit)->getMaxOrder() < order)
605 nb_ents_set_order_up++;
606 if ((*miit)->getMaxOrder() > order)
607 nb_ents_set_order_down++;
608
609 // set dofs inactive if order is reduced, and set new order to entity
610 // if order is increased (note that dofs are not build if order is
611 // increased)
612
613 bool can_change_size = true;
614 auto dit = dofsField.get<Unique_mi_tag>().lower_bound(
616 (*miit)->getEnt()));
617 if (dit != dofsField.get<Unique_mi_tag>().end()) {
618 auto hi_dit = dofsField.get<Unique_mi_tag>().upper_bound(
620 (*miit)->getEnt()));
621
622 if (dit != hi_dit)
623 can_change_size = false;
624 for (; dit != hi_dit; dit++) {
625 if ((*dit)->getDofOrder() > order) {
626 bool success = dofsField.modify(dofsField.project<0>(dit),
628 if (!success)
629 SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL,
630 "modification unsuccessful");
631 }
632 }
633 }
634
635 bool success =
636 entsFields.modify(entsFields.project<0>(miit),
637 can_change_size ? modify_order_size_change
638 : modify_order_no_size_change);
639 if (!success)
640 SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL,
641 "modification unsuccessful");
642 }
643 }
644 }
645
646 Range new_ents = subtract(Range(first, second), ents_in_database);
647 for (Range::const_pair_iterator pit = new_ents.const_pair_begin();
648 pit != new_ents.const_pair_end(); ++pit) {
649 const auto first = pit->first;
650 const auto second = pit->second;
651 const auto ent_type = get_moab().type_from_handle(first);
652
653 if (!field_ptr->getFieldOrderTable()[ent_type])
654 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
655 "Number of degrees of freedom for entity %s for %s space on "
656 "base %s can "
657 "not be deduced",
658 moab::CN::EntityTypeName(ent_type),
659 field_ptr->getSpaceName().c_str(),
660 field_ptr->getApproxBaseName().c_str());
661
662 auto get_nb_dofs_on_order = [&](const int order) {
663 return order >= 0 ? (field_ptr->getFieldOrderTable()[ent_type])(order)
664 : 0;
665 };
666 const int nb_dofs_on_order = get_nb_dofs_on_order(order);
667 if (nb_dofs_on_order || order == -1) {
668
669 const int field_rank = field_ptr->getNbOfCoeffs();
670 const int nb_dofs = nb_dofs_on_order * field_rank;
671
672 // Entity is not in database and order is changed or reset
673 auto miit_ref_ent =
674 refinedEntities.get<Ent_mi_tag>().lower_bound(first);
675
676 auto create_tags_for_max_order = [&](const Range &ents) {
678 if (order >= 0) {
679 std::vector<ApproximationOrder> o_vec(ents.size(), order);
680 CHKERR get_moab().tag_set_data(field_ptr->th_AppOrder, ents,
681 &*o_vec.begin());
682 }
684 };
685
686 auto create_tags_for_data = [&](const Range &ents) {
688 if (order >= 0) {
689
690 if (nb_dofs > 0) {
691 if (ent_type == MBVERTEX) {
692 std::vector<FieldData> d_vec(nb_dofs * ents.size(), 0);
693 CHKERR get_moab().tag_set_data(field_ptr->th_FieldDataVerts,
694 ents, &*d_vec.begin());
695 } else {
696 std::vector<int> tag_size(ents.size(), nb_dofs);
697 std::vector<FieldData> d_vec(nb_dofs, 0);
698 std::vector<void const *> d_vec_ptr(ents.size(),
699 &*d_vec.begin());
700 CHKERR get_moab().tag_set_by_ptr(field_ptr->th_FieldData, ents,
701 &*d_vec_ptr.begin(),
702 &*tag_size.begin());
703 }
704 }
705 }
706
708 };
709
710 auto get_ents_in_ref_ent = [&](auto miit_ref_ent) {
711 auto hi = refinedEntities.get<Ent_mi_tag>().upper_bound(second);
712 Range in;
713 for (; miit_ref_ent != hi; ++miit_ref_ent)
714 in.insert(miit_ref_ent->get()->getEnt());
715 return in;
716 };
717
718 auto get_ents_max_order = [&](const Range &ents) {
719 boost::shared_ptr<std::vector<const void *>> vec(
720 new std::vector<const void *>());
721 vec->resize(ents.size());
722 CHKERR get_moab().tag_get_by_ptr(field_ptr->th_AppOrder, ents,
723 &*vec->begin());
724 return vec;
725 };
726
727 auto get_ents_field_data_vector_adaptor =
728 [&](const Range &ents,
729 boost::shared_ptr<std::vector<const void *>> &ents_max_orders) {
730 // create shared pointer and reserve memory
731 boost::shared_ptr<std::vector<double *>> vec(
732 new std::vector<double *>());
733 vec->reserve(ents.size());
734
735 if (order >= 0 && nb_dofs == 0) {
736 // set empty vector adaptor
737 for (int i = 0; i != ents.size(); ++i)
738 vec->emplace_back(nullptr);
739 } else {
740 moab::ErrorCode rval;
741 std::vector<int> tag_size(ents.size());
742 std::vector<const void *> d_vec_ptr(ents.size());
743
744 // get tags data
745 if (ent_type == MBVERTEX)
746 rval = get_moab().tag_get_by_ptr(field_ptr->th_FieldDataVerts,
747 ents, &*d_vec_ptr.begin(),
748 &*tag_size.begin());
749 else
750 rval = get_moab().tag_get_by_ptr(field_ptr->th_FieldData,
751 ents, &*d_vec_ptr.begin(),
752 &*tag_size.begin());
753
754 auto cast = [](auto p) {
755 return const_cast<FieldData *const>(
756 static_cast<const FieldData *>(p));
757 };
758
759 // some of entities has tag not set or zero dofs on entity
760 if (rval == MB_SUCCESS) {
761 // all is ok, all entities has tag set
762 auto tit = d_vec_ptr.begin();
763 auto oit = ents_max_orders->begin();
764 for (auto sit = tag_size.begin(); sit != tag_size.end();
765 ++sit, ++tit, ++oit)
766 vec->emplace_back(cast(*tit));
767
768 } else {
769 // set empty vector adaptor
770 for (int i = 0; i != ents.size(); ++i)
771 vec->emplace_back(nullptr);
772
773 // check order on all entities, and if for that order non zero
774 // dofs are expected get pointer to tag data and reset vector
775 // adaptor
776 auto oit = ents_max_orders->begin();
777 auto dit = vec->begin();
778 for (auto eit = ents.begin(); eit != ents.end();
779 ++eit, ++oit, ++dit) {
780
781 const int ent_order =
782 *static_cast<const ApproximationOrder *>(*oit);
783 const int ent_nb_dofs = get_nb_dofs_on_order(ent_order);
784
785 if (ent_nb_dofs) {
786 int tag_size;
787 const void *ret_val;
788 if (ent_type == MBVERTEX) {
789 rval = get_moab().tag_get_by_ptr(
790 field_ptr->th_FieldDataVerts, &*eit, 1, &ret_val,
791 &tag_size);
792 } else {
793 rval = get_moab().tag_get_by_ptr(
794 field_ptr->th_FieldData, &*eit, 1, &ret_val,
795 &tag_size);
796 if (rval != MB_SUCCESS) {
797
798 const int set_tag_size[] = {ent_nb_dofs * field_rank};
799 std::array<FieldData, MAX_DOFS_ON_ENTITY> set_d_vec;
800 std::fill(set_d_vec.begin(),
801 &set_d_vec[set_tag_size[0]], 0);
802 const void *set_d_vec_ptr[] = {set_d_vec.data()};
803 CHKERR get_moab().tag_set_by_ptr(
804 field_ptr->th_FieldData, &*eit, 1, set_d_vec_ptr,
805 set_tag_size);
806 rval = get_moab().tag_get_by_ptr(
807 field_ptr->th_FieldData, &*eit, 1, &ret_val,
808 &tag_size);
809
810 if (rval != MB_SUCCESS) {
814 MOFEM_LOG("SELF", Sev::error)
815 << "Error is triggered in MOAB, field tag data "
816 "for same reason can not be for accessed.";
817 MOFEM_LOG("SELF", Sev::error)
818 << "Set order: " << order;
819 MOFEM_LOG("SELF", Sev::error)
820 << "Nb. dofs on entity for given order: "
821 << set_tag_size[0];
822 MOFEM_LOG("SELF", Sev::error)
823 << "Entity type: "
824 << moab::CN::EntityTypeName(ent_type);
825 MOFEM_LOG("SELF", Sev::error)
826 << "Field: " << *field_ptr;
827 CHKERR rval;
828 }
829 }
830 }
831 const_cast<FieldData *&>(*dit) = cast(ret_val);
832 }
833 }
834 }
835 }
836 return vec;
837 };
838
839 auto ents_in_ref_ent = get_ents_in_ref_ent(miit_ref_ent);
840
841 CHKERR create_tags_for_max_order(ents_in_ref_ent);
842 CHKERR create_tags_for_data(ents_in_ref_ent);
843 auto ents_max_order = get_ents_max_order(ents_in_ref_ent);
844 auto ent_field_data =
845 get_ents_field_data_vector_adaptor(ents_in_ref_ent, ents_max_order);
846
847 // reserve memory for field dofs
848 auto ents_array = boost::make_shared<std::vector<FieldEntity>>();
849 // Add sequence to field data structure. Note that entities are
850 // allocated once into vector. This vector is passed into sequence as a
851 // weak_ptr. Vector is destroyed at the point last entity inside that
852 // vector is destroyed.
853 ents_array->reserve(second - first + 1);
854 auto vit_max_order = ents_max_order->begin();
855 auto vit_field_data = ent_field_data->begin();
856 for (int i = 0; i != ents_in_ref_ent.size(); ++i) {
857
858 ents_array->emplace_back(
859 field_ptr, *miit_ref_ent,
860 boost::shared_ptr<double *const>(ent_field_data,
861 &*vit_field_data),
862 boost::shared_ptr<const int>(
863 ents_max_order, static_cast<const int *>(*vit_max_order)));
864 ++vit_max_order;
865 ++vit_field_data;
866 ++miit_ref_ent;
867 }
868 if (!ents_array->empty())
869 if ((*ents_array)[0].getFieldRawPtr() != field_ptr.get())
870 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
871 "Get field ent poiter and field pointer do not match for "
872 "field %s",
873 field_ptr->getName().c_str());
874 nb_ents_set_order_new += ents_array->size();
875
876 // Check if any of entities in the range has bit level but is not added
877 // to database. That generate data inconsistency and error.
878 if (ents_in_ref_ent.size() < (second - first + 1)) {
879 Range ents_not_in_database =
880 subtract(Range(first, second), ents_in_ref_ent);
881 std::vector<const void *> vec_bits(ents_not_in_database.size());
882 CHKERR get_moab().tag_get_by_ptr(
883 get_basic_entity_data_ptr()->th_RefBitLevel, ents_not_in_database,
884 &*vec_bits.begin());
885 auto cast = [](auto p) {
886 return static_cast<const BitRefLevel *>(p);
887 };
888 for (auto v : vec_bits)
889 if (cast(v)->any())
890 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
891 "Try to add entities which are not seeded or added to "
892 "database");
893 }
894
895 // Add entities to database
896 // Keep the successor of this ascending batch, including gap rebuilds.
897 const auto hint = ents_array->empty()
898 ? entsFields.end()
899 : entsFields.upper_bound(
900 ents_array->back().getLocalUniqueId());
901 for (auto &v : *ents_array)
902 entsFields.emplace_hint(hint, ents_array, &v);
903 }
904 }
905 }
906
907 if (verb > QUIET) {
908 MOFEM_LOG_C("SYNC", Sev::noisy,
909 "nb. of entities in field <%s> for which order was "
910 "increased %d (order %d)",
911 field_ptr->getName().c_str(), nb_ents_set_order_up, order);
912 MOFEM_LOG_C("SYNC", Sev::noisy,
913 "nb. of entities in field <%s> for which order was "
914 "reduced %d (order %d)",
915 field_ptr->getName().c_str(), nb_ents_set_order_down, order);
917 "SYNC", Sev::noisy,
918 "nb. of entities in field <%s> for which order set %d (order %d)",
919 field_ptr->getName().c_str(), nb_ents_set_order_new, order);
920 }
921
922 if (verb > QUIET) {
923 auto eiit = entsFields.get<Unique_mi_tag>().lower_bound(
924 FieldEntity::getLoBitNumberUId(field_ptr->getBitNumber()));
925 auto hi_eiit = entsFields.get<Unique_mi_tag>().upper_bound(
926 FieldEntity::getHiBitNumberUId(field_ptr->getBitNumber()));
927 MOFEM_LOG_C("SYNC", Sev::noisy,
928 "nb. of ents in the multi index field <%s> %d",
929 field_ptr->getName().c_str(), std::distance(eiit, hi_eiit));
930 }
931
932 if (verb > QUIET)
933 MOFEM_LOG_SEVERITY_SYNC(mofemComm, Sev::noisy);
934
936}
937
939 const ApproximationOrder order, int verb) {
940 MOFEM_LOG_CHANNEL("WORLD");
941 MOFEM_LOG_TAG("WORLD", "FieldCore");
942 MOFEM_LOG_CHANNEL("SYNC");
943 MOFEM_LOG_TAG("SYNC", "FieldCore");
946
947 // check field & meshset
948 auto field_it = fIelds.get<BitFieldId_mi_tag>().find(id);
949 if (field_it == fIelds.get<BitFieldId_mi_tag>().end())
950 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_FOUND, "no field found");
951
952 MOFEM_LOG("WORLD", Sev::noisy)
953 << "Field " << (*field_it)->getName() << " core value < "
954 << this->getValue() << " > field value ( "
955 << static_cast<int>((*field_it)->getBitNumber()) << " )"
956 << " nb. of ents " << ents.size();
957
958 CHKERR this->setFieldOrderImpl(*field_it, ents, order, verb);
959
961}
962
963MoFEMErrorCode Core::set_field_order(const EntityHandle meshset,
964 const EntityType type, const BitFieldId id,
965 const ApproximationOrder order, int verb) {
967 if (verb == -1)
968 verb = verbose;
969 *buildMoFEM = 0;
970 Range ents;
971 CHKERR get_moab().get_entities_by_type(meshset, type, ents);
972 CHKERR this->set_field_order(ents, id, order, verb);
974}
975MoFEMErrorCode Core::set_field_order(const EntityHandle meshset,
976 const EntityType type,
977 const std::string &name,
978 const ApproximationOrder order, int verb) {
980 if (verb == -1)
981 verb = verbose;
982 *buildMoFEM = 0;
983 CHKERR this->set_field_order(meshset, type, get_field_id(name), order, verb);
985}
986MoFEMErrorCode Core::set_field_order(const Range &ents, const std::string &name,
987 const ApproximationOrder order, int verb) {
989 if (verb == -1)
990 verb = verbose;
991 *buildMoFEM = 0;
992 CHKERR this->set_field_order(ents, get_field_id(name), order, verb);
994}
996 const BitRefLevel &bit, const BitRefLevel &mask, const EntityType type,
997 const BitFieldId id, const ApproximationOrder order, int verb) {
999 if (verb == -1)
1000 verb = verbose;
1001 *buildMoFEM = 0;
1002 Range ents;
1004 ents, verb);
1005 CHKERR this->set_field_order(ents, id, order, verb);
1007}
1008
1010 const BitRefLevel &bit, const BitRefLevel &mask, const EntityType type,
1011 const std::string &name, const ApproximationOrder order, int verb) {
1013 if (verb == -1)
1014 verb = verbose;
1015 *buildMoFEM = 0;
1016 Range ents;
1018 ents, verb);
1019 CHKERR this->set_field_order(ents, get_field_id(name), order, verb);
1021}
1022
1024Core::buildFieldForNoFieldImpl(boost::shared_ptr<Field> field_ptr,
1025 std::map<EntityType, int> &dof_counter,
1026 int verb) {
1028
1029 const auto bit_number = field_ptr->getBitNumber();
1030
1031 // ents in the field meshset
1032 Range ents;
1033 CHKERR get_moab().get_entities_by_handle(field_ptr->getMeshset(), ents,
1034 false);
1035 if (verb > VERBOSE)
1036 MOFEM_LOG_C("SYNC", Sev::noisy, "Ents in field %s meshset %zu\n",
1037 field_ptr->getName().c_str(), ents.size());
1038
1039 // ent view by field id (in set all MoabEnts has the same FieldId)
1040 auto eiit = entsFields.get<Unique_mi_tag>().lower_bound(
1041 FieldEntity::getLoBitNumberUId(field_ptr->getBitNumber()));
1043 if (eiit != entsFields.get<Unique_mi_tag>().end()) {
1044 auto hi_eiit = entsFields.get<Unique_mi_tag>().upper_bound(
1045 FieldEntity::getHiBitNumberUId(field_ptr->getBitNumber()));
1046 std::copy(eiit, hi_eiit, std::back_inserter(ents_id_view));
1047 }
1048
1049 boost::shared_ptr<const int> zero_order(new const int(0));
1050
1051 for (auto ent : ents) {
1052 // search if field meshset is in database
1053 auto ref_ent_it = refinedEntities.get<Ent_mi_tag>().find(ent);
1054 if (ref_ent_it == refinedEntities.get<Ent_mi_tag>().end())
1055 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
1056 "Entity is not in MoFEM database, entities in field meshset need "
1057 "to be seeded (i.e. bit ref level add to them)");
1058
1059 auto add_dofs = [&](auto field_eit) {
1061 // create dofs on this entity (nb. of dofs is equal to rank)
1062 for (FieldCoefficientsNumber rank = 0; rank < field_ptr->getNbOfCoeffs();
1063 rank++) {
1064 // insert dof
1065 auto p = dofsField.insert(
1066 boost::make_shared<DofEntity>(field_eit, 0, rank, rank));
1067 if (p.second) {
1068 dof_counter[MBENTITYSET]++; // Count entities in the meshset
1069 } else
1070 SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL,
1071 "Dof expected to be created");
1072 }
1074 };
1075
1076 // create database entity
1077 auto field_ent_it = ents_id_view.get<1>().find(ent);
1078 if (field_ent_it == ents_id_view.get<1>().end()) {
1079
1080 auto p = entsFields.insert(
1081
1082 boost::make_shared<FieldEntity>(
1083 field_ptr, *ref_ent_it,
1085 *ref_ent_it),
1086 boost::shared_ptr<const int>(zero_order, zero_order.get()))
1087
1088 );
1089
1090 if ((*p.first)->getFieldRawPtr() != field_ptr.get())
1091 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
1092 "Get field ent poiter and field pointer do not match for "
1093 "field %s",
1094 field_ptr->getName().c_str());
1095
1096 if (!p.second)
1097 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
1098 "Entity should be created");
1099
1100 CHKERR add_dofs(*(p.first));
1101
1102 } else {
1103
1104 // If there are DOFs in that range is more pragmatic to remove them
1105 // rather than to find sub-ranges or make them inactive
1106 auto dit = dofsField.get<Unique_mi_tag>().lower_bound(
1108 auto hi_dit = dofsField.get<Unique_mi_tag>().upper_bound(
1110 dofsField.get<Unique_mi_tag>().erase(dit, hi_dit);
1111 CHKERR add_dofs(*field_ent_it);
1112 }
1113 }
1114
1115 if (verb > VERBOSE) {
1116 auto lo_dof = dofsField.get<Unique_mi_tag>().lower_bound(
1117 FieldEntity::getLoBitNumberUId(field_ptr->getBitNumber()));
1118 auto hi_dof = dofsField.get<Unique_mi_tag>().upper_bound(
1119 FieldEntity::getHiBitNumberUId(field_ptr->getBitNumber()));
1120 for (; lo_dof != hi_dof; lo_dof++)
1121 MOFEM_LOG("SYNC", Sev::noisy) << **lo_dof;
1122 MOFEM_LOG_SEVERITY_SYNC(mofemComm, Sev::noisy);
1123 }
1124
1126}
1127
1130 std::map<EntityType, int> &dof_counter, int verb) {
1132
1133 if (verb == -1)
1134 verb = verbose;
1135
1136 // find fields
1137 auto field_it = fIelds.get<BitFieldId_mi_tag>().find(id);
1138 if (field_it == fIelds.get<BitFieldId_mi_tag>().end())
1139 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_FOUND, "Field not found");
1140
1141 if (verb > QUIET)
1142 MOFEM_LOG("WORLD", Sev::noisy)
1143 << "Field " << (*field_it)->getName() << " core value < "
1144 << this->getValue() << " > field value () "
1145 << (*field_it)->getBitNumber() << " )";
1146
1147 CHKERR this->buildFieldForNoFieldImpl(*field_it, dof_counter, verb);
1148
1150}
1151
1153 const BitFieldId id, std::map<EntityType, int> &dof_counter,
1154 std::map<EntityType, int> &inactive_dof_counter, const Range *ents_ptr,
1155 int verb) {
1157 if (verb == -1)
1158 verb = verbose;
1159
1160 // Find field
1161 auto &set_id = fIelds.get<BitFieldId_mi_tag>();
1162 auto field_it = set_id.find(id);
1163 if (field_it == set_id.end()) {
1164 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_FOUND, "Field not found");
1165 }
1166 const int bit_number = field_it->get()->getBitNumber();
1167 const int rank = field_it->get()->getNbOfCoeffs();
1168
1169 // Ents in the field meshset
1170 Range ents_of_id_meshset;
1171 CHKERR get_moab().get_entities_by_handle((*field_it)->meshSet,
1172 ents_of_id_meshset, false);
1173 // Every entity visited below has its DOFs erased and recreated, so an
1174 // incremental build must restrict the range to the entities that changed.
1175 if (ents_ptr)
1176 ents_of_id_meshset = intersect(ents_of_id_meshset, *ents_ptr);
1177 if (verb > VERY_NOISY) {
1178 MOFEM_LOG_C("SYNC", Sev::noisy, "Ents in field %s meshset %zu",
1179 (*field_it)->getName().c_str(), ents_of_id_meshset.size());
1180 }
1181
1182 for (auto p_eit = ents_of_id_meshset.pair_begin();
1183 p_eit != ents_of_id_meshset.pair_end(); ++p_eit) {
1184
1185 const EntityHandle first = p_eit->first;
1186 const EntityHandle second = p_eit->second;
1187 const auto lo_uid =
1188 FieldEntity::getLoLocalEntityBitNumber(bit_number, first);
1189 const auto hi_uid =
1190 FieldEntity::getHiLocalEntityBitNumber(bit_number, second);
1191
1192 auto feit = entsFields.get<Unique_mi_tag>().lower_bound(lo_uid);
1193 if (feit == entsFields.get<Unique_mi_tag>().end())
1194 continue;
1195 auto hi_feit = entsFields.get<Unique_mi_tag>().upper_bound(hi_uid);
1196
1197 // If there are DOFs in that range is more pragmatic to remove them
1198 // rather than to find sub-ranges or make them inactive
1199 auto dit = dofsField.get<Unique_mi_tag>().lower_bound(lo_uid);
1200 auto hi_dit = dofsField.get<Unique_mi_tag>().upper_bound(hi_uid);
1201 dofsField.get<Unique_mi_tag>().erase(dit, hi_dit);
1202
1203 // Add vertices DOFs by bulk
1204 boost::shared_ptr<std::vector<DofEntity>> dofs_array =
1205 boost::make_shared<std::vector<DofEntity>>(std::vector<DofEntity>());
1206 // Add Sequence of DOFs to sequence container as weak_ptr
1207 int nb_dofs_on_ents = 0;
1208 for (auto tmp_feit = feit; tmp_feit != hi_feit; ++tmp_feit) {
1209 nb_dofs_on_ents += rank * tmp_feit->get()->getOrderNbDofs(
1210 tmp_feit->get()->getMaxOrder());
1211 }
1212
1213 // Reserve memory
1214 dofs_array->reserve(nb_dofs_on_ents);
1215
1216 // Create DOFs
1217 for (; feit != hi_feit; ++feit) {
1218 // Create dofs instances and shared pointers
1219 int DD = 0;
1220 // Loop orders (loop until max entity order is set)
1221 for (int oo = 0; oo <= feit->get()->getMaxOrder(); ++oo) {
1222 // Loop nb. dofs at order oo
1223 for (int dd = 0; dd < feit->get()->getOrderNbDofsDiff(oo); ++dd) {
1224 // Loop rank
1225 for (int rr = 0; rr < rank; ++rr, ++DD) {
1226 dofs_array->emplace_back(*feit, oo, rr, DD);
1227 ++dof_counter[feit->get()->getEntType()];
1228 }
1229 }
1230 }
1231 if (DD > feit->get()->getNbDofsOnEnt()) {
1232 std::ostringstream ss;
1233 ss << "rank " << rAnk << " ";
1234 ss << **feit << std::endl;
1235 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
1236 "Expected number of DOFs on entity not equal to number added "
1237 "to database (DD = %d != %d = "
1238 "feit->get()->getNbDofsOnEnt())\n"
1239 "%s",
1240 DD, feit->get()->getNbDofsOnEnt(), ss.str().c_str());
1241 }
1242 }
1243
1244 // Insert into Multi-Index container
1245 int dofs_field_size0 = dofsField.size();
1246 // The old interval was erased above; hi_dit is its surviving successor.
1247 const auto hint = hi_dit;
1248 for (auto &v : *dofs_array)
1249 dofsField.emplace_hint(hint, dofs_array, &v);
1250
1251 // Add Sequence of DOFs to sequence container as weak_ptr
1252 field_it->get()->getDofSequenceContainer().push_back(dofs_array);
1253
1254 // Check data consistency
1255 if (PetscUnlikely(static_cast<int>(dofs_array.use_count()) !=
1256 static_cast<int>(dofs_array->size() + 1))) {
1257 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
1258 "Wrong use count %ld != %zu", dofs_array.use_count(),
1259 dofs_array->size() + 1);
1260 }
1261 if (dofs_field_size0 + dofs_array->size() != dofsField.size()) {
1262 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
1263 "Wrong number of inserted DOFs %zu != %zu", dofs_array->size(),
1264 dofsField.size() - dofs_field_size0);
1265 }
1266 }
1268}
1269
1270MoFEMErrorCode Core::buildField(const boost::shared_ptr<Field> &field,
1271 const Range *ents_ptr, int verb) {
1273 if (verb == -1)
1274 verb = verbose;
1275 if (verb > QUIET)
1276 MOFEM_LOG("SYNC", Sev::verbose) << "Build field " << field->getName();
1277
1278 std::map<EntityType, int> dof_counter;
1279 std::map<EntityType, int> inactive_dof_counter;
1280
1281 // Need to rebuild order table since number of dofs on each order when
1282 // field was created.
1283 if (field->getApproxBase() == USER_BASE)
1284 CHKERR field->rebuildDofsOrderMap();
1285
1286 switch (field->getSpace()) {
1287 case NOFIELD:
1288 CHKERR this->buildFieldForNoField(field->getId(), dof_counter, verb);
1289 break;
1290 case L2:
1291 case H1:
1292 case HCURL:
1293 case HDIV:
1294 CHKERR this->buildFieldForL2H1HcurlHdiv(field->getId(), dof_counter,
1295 inactive_dof_counter, ents_ptr,
1296 verb);
1297 break;
1298 default:
1299 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_IMPLEMENTED, "not implemented");
1300 }
1301
1302 if (verb > QUIET) {
1303 int nb_added_dofs = 0;
1304 int nb_inactive_added_dofs = 0;
1305 for (auto const &it : dof_counter) {
1306 MOFEM_LOG("SYNC", Sev::verbose)
1307 << "Nb. of dofs (" << moab::CN::EntityTypeName(it.first) << ") "
1308 << it.second << " (inactive " << inactive_dof_counter[it.first]
1309 << ")";
1310 nb_added_dofs += it.second;
1311 nb_inactive_added_dofs += inactive_dof_counter[it.first];
1312 }
1313 MOFEM_LOG("SYNC", Sev::verbose)
1314 << "Nb. added dofs " << nb_added_dofs << " (number of inactive dofs "
1315 << nb_inactive_added_dofs << " )";
1316 }
1318}
1319
1320MoFEMErrorCode Core::build_field(const std::string field_name, int verb) {
1322 auto field_it = fIelds.get<FieldName_mi_tag>().find(field_name);
1323 if (field_it == fIelds.get<FieldName_mi_tag>().end())
1324 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_FOUND, "Field < %s > not found",
1325 field_name.c_str());
1326
1327 CHKERR this->buildField(*field_it, nullptr, verb);
1328 if (verb > QUIET)
1329 MOFEM_LOG_SYNCHRONISE(mofemComm);
1331}
1332
1334 const Range &ents, int verb) {
1336 auto field_it = fIelds.get<FieldName_mi_tag>().find(field_name);
1337 if (field_it == fIelds.get<FieldName_mi_tag>().end())
1338 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_FOUND, "Field < %s > not found",
1339 field_name.c_str());
1340
1341 CHKERR this->buildField(*field_it, &ents, verb);
1342 if (verb > QUIET)
1343 MOFEM_LOG_SYNCHRONISE(mofemComm);
1345}
1346
1349 if (verb == -1)
1350 verb = verbose;
1351
1352 for (auto field : fIelds.get<BitFieldId_mi_tag>())
1353 CHKERR this->buildField(field, &ents, verb);
1354
1355 *buildMoFEM = 1 << 0;
1356 if (verb > QUIET) {
1357 MOFEM_LOG("SYNC", Sev::verbose) << "Number of dofs " << dofsField.size();
1358 MOFEM_LOG_SYNCHRONISE(mofemComm);
1359 }
1361}
1362
1365 if (verb == -1)
1366 verb = verbose;
1367
1368 for (auto field : fIelds.get<BitFieldId_mi_tag>())
1369 CHKERR this->buildField(field, nullptr, verb);
1370
1371 *buildMoFEM = 1 << 0;
1372 if (verb > QUIET) {
1373 MOFEM_LOG("SYNC", Sev::verbose) << "Number of dofs " << dofsField.size();
1374 MOFEM_LOG_SYNCHRONISE(mofemComm);
1375 }
1376
1378}
1379
1383 auto dit = dofsField.get<Unique_mi_tag>().lower_bound(
1384 FieldEntity::getLoBitNumberUId(get_field_bit_number((field_name))));
1385 auto hi_dit = dofsField.get<Unique_mi_tag>().upper_bound(
1386 FieldEntity::getHiBitNumberUId(get_field_bit_number(field_name)));
1387 MOFEM_LOG("SYNC", Sev::inform) << "List DOFs:";
1388 for (; dit != hi_dit; dit++)
1389 MOFEM_LOG("SYNC", Sev::inform) << *dit;
1390
1391 MOFEM_LOG_SYNCHRONISE(mofemComm);
1393}
1394
1397 MOFEM_LOG("SYNC", Sev::inform) << "List Fields:";
1398 for (auto &miit : fIelds.get<BitFieldId_mi_tag>())
1399 MOFEM_LOG("SYNC", Sev::inform) << *miit;
1400
1401 MOFEM_LOG_SYNCHRONISE(mofemComm);
1403}
1404
1405FieldEntityByUId::iterator
1407 return entsFields.get<Unique_mi_tag>().lower_bound(
1408 FieldEntity::getLoBitNumberUId(get_field_bit_number(field_name)));
1409}
1410FieldEntityByUId::iterator
1412 return entsFields.get<Unique_mi_tag>().upper_bound(
1413 FieldEntity::getHiBitNumberUId(get_field_bit_number(field_name)));
1414}
1415DofEntityByUId::iterator
1416Core::get_dofs_by_name_begin(const std::string &field_name) const {
1417 return dofsField.get<Unique_mi_tag>().lower_bound(
1418 FieldEntity::getLoBitNumberUId(get_field_bit_number(field_name)));
1419}
1420DofEntityByUId::iterator
1421Core::get_dofs_by_name_end(const std::string &field_name) const {
1422 return dofsField.get<Unique_mi_tag>().upper_bound(
1423 FieldEntity::getHiBitNumberUId(get_field_bit_number(field_name)));
1424}
1425DofEntityByUId::iterator
1427 const EntityHandle ent) const {
1428 return dofsField.get<Unique_mi_tag>().lower_bound(
1430 ent));
1431}
1432DofEntityByUId::iterator
1434 const EntityHandle ent) const {
1435 return dofsField.get<Unique_mi_tag>().upper_bound(
1437 ent));
1438}
1439DofEntityByUId::iterator
1441 const EntityType type) const {
1442 return dofsField.get<Unique_mi_tag>().lower_bound(
1445}
1446DofEntityByUId::iterator
1448 const EntityType type) const {
1449 return dofsField.get<Unique_mi_tag>().upper_bound(
1452}
1456 auto it = fIelds.get<FieldName_mi_tag>().find(name);
1457 if (it == fIelds.get<FieldName_mi_tag>().end()) {
1458 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
1459 "field not found < %s >", name.c_str());
1460 }
1461 EntityHandle meshset = (*it)->getMeshset();
1462 int num_entities;
1463 CHKERR get_moab().get_number_entities_by_handle(meshset, num_entities);
1464
1465 auto count_field_ents = [&]() {
1466 auto bit_number = (*it)->getBitNumber();
1467 auto low_eit = entsFields.get<Unique_mi_tag>().lower_bound(
1468 FieldEntity::getLoBitNumberUId(bit_number));
1469 auto hi_eit = entsFields.get<Unique_mi_tag>().upper_bound(
1470 FieldEntity::getHiBitNumberUId(bit_number));
1471 return std::distance(low_eit, hi_eit);
1472 };
1473
1474 if (count_field_ents() > (unsigned int)num_entities) {
1475 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
1476 "not equal number of entities in meshset and field multiindex "
1477 "< %s >",
1478 name.c_str());
1479 }
1481}
1484 for (auto &it : fIelds.get<FieldName_mi_tag>()) {
1485 if (it->getSpace() == NOFIELD)
1486 continue; // FIXME: should be treated properly, not test is just
1487 // skipped for this NOFIELD space
1488 EntityHandle meshset = it->getMeshset();
1489 int num_entities;
1490 CHKERR get_moab().get_number_entities_by_handle(meshset, num_entities);
1491
1492 auto count_field_ents = [&]() {
1493 auto bit_number = it->getBitNumber();
1494 auto low_eit = entsFields.get<Unique_mi_tag>().lower_bound(
1495 FieldEntity::getLoBitNumberUId(bit_number));
1496 auto hi_eit = entsFields.get<Unique_mi_tag>().upper_bound(
1497 FieldEntity::getHiBitNumberUId(bit_number));
1498 return std::distance(low_eit, hi_eit);
1499 };
1500
1501 if (count_field_ents() > (unsigned int)num_entities) {
1502 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
1503 "not equal number of entities in meshset and field "
1504 "multiindex < %s >",
1505 it->getName().c_str());
1506 }
1507 }
1509}
1510
1511} // namespace MoFEM
std::vector< int > size
WorkMesh base
ProjectedField & field
#define FieldCoreFunctionBegin
Definition FieldCore.cpp:8
std::string type
#define MOFEM_LOG_SEVERITY_SYNC(comm, severity)
Synchronise "SYNC" on curtain severity level.
#define MOFEM_LOG_SYNCHRONISE(comm)
Synchronise "SYNC" channel.
#define MOFEM_LOG_C(channel, severity, format,...)
It it
Definition Schur.cpp:2568
It end
Definition Schur.cpp:2568
std::string name
@ QUIET
@ DEFAULT_VERBOSITY
@ VERBOSE
@ VERY_NOISY
MoFEMTypes
Those types control how functions respond on arguments, f.e. error handling.
@ MF_EXIST
@ MF_EXCL
FieldApproximationBase
approximation base
Definition definitions.h:58
@ USER_BASE
user implemented approximation base
Definition definitions.h:68
#define CHK_THROW_MESSAGE(err, msg)
Check and throw MoFEM exception.
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
FieldSpace
approximation spaces
Definition definitions.h:82
@ L2
field with C-1 continuity
Definition definitions.h:88
@ NOFIELD
scalar or vector of scalars describe (no true field)
Definition definitions.h:84
@ H1
continuous field
Definition definitions.h:85
@ HCURL
field with continuous tangents
Definition definitions.h:86
@ HDIV
field with continuous normal traction
Definition definitions.h:87
#define MB_ID_WIDTH
FieldContinuity
Field continuity.
Definition definitions.h:99
@ CONTINUOUS
Regular field.
@ DISCONTINUOUS
Broken continuity (No effect on L2 space)
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_NOT_FOUND
Definition definitions.h:33
@ MOFEM_IMPOSSIBLE_CASE
Definition definitions.h:35
@ MOFEM_OPERATION_UNSUCCESSFUL
Definition definitions.h:34
@ 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 MoFEMFunctionBeginHot
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define BITFEID_SIZE
max number of finite elements
#define THROW_MESSAGE(msg)
Throw MoFEM exception.
constexpr int order
MoFEMErrorCode getEntitiesByTypeAndRefLevel(const BitRefLevel bit, const BitRefLevel mask, const EntityType type, const EntityHandle meshset, int verb=0) const
add all ents from ref level given by bit to meshset
#define MOFEM_LOG(channel, severity)
Log.
#define MOFEM_LOG_TAG(channel, tag)
Tag channel.
#define MOFEM_LOG_CHANNEL(channel)
Set and reset channel.
#define MOFEM_LOG_ATTRIBUTES(channel, bit)
Add attributes to channel.
#define MOFEM_LOG_FUNCTION()
Set scope.
auto bit
set bit
FTensor::Index< 'i', SPACE_DIM > i
const double v
phase velocity of light in medium (cm/ns)
static MoFEMErrorCodeGeneric< PetscErrorCode > ierr
static MoFEMErrorCodeGeneric< moab::ErrorCode > rval
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
int ApproximationOrder
Approximation on the entity.
Definition Types.hpp:26
std::bitset< BITFIELDID_SIZE > BitFieldId
Field Id.
Definition Types.hpp:42
int FieldCoefficientsNumber
Number of field coefficients.
Definition Types.hpp:27
std::bitset< BITREFLEVEL_SIZE > BitRefLevel
Bit structure attached to each entity identifying to what mesh entity is attached.
Definition Types.hpp:40
char FieldBitNumber
Field bit number.
Definition Types.hpp:28
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
multi_index_container< boost::shared_ptr< FieldEntity >, indexed_by< sequenced<>, ordered_non_unique< tag< Ent_mi_tag >, const_mem_fun< FieldEntity::interface_type_RefEntity, EntityHandle, &FieldEntity::getEnt > > > > FieldEntity_multiIndex_ent_view
EntityHandle get_id_for_max_type()
EntityHandle get_id_for_min_type()
Field_multiIndex::index< FieldName_mi_tag >::type::iterator field_it
const EntityHandle no_handle
No entity handle is indicated by zero handle, i.e. root meshset.
Definition Common.hpp:12
constexpr auto field_name
multi_index_container< DofsSideMapData, indexed_by< ordered_non_unique< tag< TypeSide_mi_tag >, composite_key< DofsSideMapData, member< DofsSideMapData, EntityType, &DofsSideMapData::type >, member< DofsSideMapData, int, &DofsSideMapData::side > > >, ordered_unique< tag< EntDofIdx_mi_tag >, member< DofsSideMapData, int, &DofsSideMapData::dof > > > > DofsSideMap
Map entity stype and side to element/entity dof index.
Managing BitRefLevels.
std::string get_field_name(const BitFieldId id) const
get field name from id
Definition FieldCore.cpp:37
MoFEMErrorCode set_field_order_by_entity_type_and_bit_ref(const BitRefLevel &bit, const BitRefLevel &mask, const EntityType type, const BitFieldId id, const ApproximationOrder order, int verb=DEFAULT_VERBOSITY)
const Field * get_field_structure(const std::string &name, enum MoFEMTypes bh=MF_EXIST) const
get field structure
Definition FieldCore.cpp:65
MoFEMErrorCode add_ents_to_field_by_dim(const Range &ents, const int dim, const std::string &name, int verb=DEFAULT_VERBOSITY)
Add entities to field meshset.
virtual MoFEMErrorCode add_broken_field(const std::string name, const FieldSpace space, const FieldApproximationBase base, const FieldCoefficientsNumber nb_coefficients, std::vector< std::pair< EntityType, std::function< MoFEMErrorCode(BaseFunction::DofsSideMap &)> > > list_dof_side_map, const TagType tag_type=MB_TAG_SPARSE, const enum MoFEMTypes bh=MF_EXCL, int verb=DEFAULT_VERBOSITY)
Add field.
bool check_field(const std::string &name) const
check if field is in database
Definition FieldCore.cpp:57
DofEntityByUId::iterator get_dofs_by_name_and_type_end(const std::string &field_name, const EntityType ent) const
get end iterator of field dofs of given name and ent type(instead you can use IT_GET_DOFS_FIELD_BY_NA...
MoFEMErrorCode get_field_entities_by_dimension(const std::string name, int dim, Range &ents) const
get entities in the field by dimension
Definition FieldCore.cpp:81
MoFEMErrorCode buildField(const boost::shared_ptr< Field > &field, const Range *ents_ptr, int verb=DEFAULT_VERBOSITY)
MoFEMErrorCode add_ents_to_field_by_type(const Range &ents, const EntityType type, const std::string &name, int verb=DEFAULT_VERBOSITY)
Add entities to field meshset.
MoFEMErrorCode buildFieldForNoFieldImpl(boost::shared_ptr< Field > field_ptr, std::map< EntityType, int > &dof_counter, int verb)
EntityHandle get_field_meshset(const BitFieldId id) const
Definition FieldCore.cpp:45
MoFEMErrorCode create_vertices_and_add_to_field(const std::string name, const double coords[], int size, int verb=DEFAULT_VERBOSITY)
Create vertices and add to the field object.
MoFEMErrorCode set_field_order(const Range &ents, const BitFieldId id, const ApproximationOrder order, int verb=DEFAULT_VERBOSITY)
DofEntityByUId::iterator get_dofs_by_name_and_type_begin(const std::string &field_name, const EntityType type) const
get begin iterator of field dofs of given name and ent type (instead you can use IT_GET_DOFS_FIELD_BY...
FieldEntityByUId::iterator get_ent_field_by_name_begin(const std::string &field_name) const
get begin iterator of field ents of given name (instead you can use IT_GET_ENT_FIELD_BY_NAME_FOR_LOOP...
MoFEMErrorCode addEntsToFieldByDim(const Range &ents, const int dim, const std::string &name, int verb=DEFAULT_VERBOSITY)
FieldBitNumber get_field_bit_number(const std::string name) const
get field bit number
Definition FieldCore.cpp:28
BitFieldId get_field_id(const std::string &name) const
Get field Id.
Definition FieldCore.cpp:18
DofEntityByUId::iterator get_dofs_by_name_and_ent_begin(const std::string &field_name, const EntityHandle ent) const
get begin iterator of field dofs of given name and ent(instead you can use IT_GET_DOFS_FIELD_BY_NAME_...
DofEntityByUId::iterator get_dofs_by_name_and_ent_end(const std::string &field_name, const EntityHandle ent) const
get end iterator of field dofs of given name and ent (instead you can use IT_GET_DOFS_FIELD_BY_NAME_F...
MoFEMErrorCode check_number_of_ents_in_ents_field() const
check data consistency in entitiesPtr
MoFEMErrorCode build_field(const std::string field_name, int verb=DEFAULT_VERBOSITY)
build field by name
DofEntityByUId::iterator get_dofs_by_name_begin(const std::string &field_name) const
get begin iterator of field dofs of given name (instead you can use IT_GET_DOFS_FIELD_BY_NAME_FOR_LOO...
MoFEMErrorCode addField(const std::string &name, const FieldSpace space, const FieldContinuity continuity, const FieldApproximationBase base, const FieldCoefficientsNumber nb_coefficients, const TagType tag_type, const enum MoFEMTypes bh, int verb)
Template for add_field.
MoFEMErrorCode setFieldOrderImpl(boost::shared_ptr< Field > field_ptr, const Range &ents, const ApproximationOrder order, int verb)
MoFEMErrorCode list_dofs_by_field_name(const std::string &name) const
MoFEMErrorCode buildFieldForNoField(const BitFieldId id, std::map< EntityType, int > &dof_counter, int verb=DEFAULT_VERBOSITY)
MoFEMErrorCode buildFieldForL2H1HcurlHdiv(const BitFieldId id, std::map< EntityType, int > &dof_counter, std::map< EntityType, int > &inactive_dof_counter, const Range *ents_ptr, int verb=DEFAULT_VERBOSITY)
DofEntityByUId::iterator get_dofs_by_name_end(const std::string &field_name) const
get begin iterator of field dofs of given name (instead you can use IT_GET_DOFS_FIELD_BY_NAME_FOR_LOO...
FieldEntityByUId::iterator get_ent_field_by_name_end(const std::string &field_name) const
get end iterator of field ents of given name (instead you can use IT_GET_ENT_FIELD_BY_NAME_FOR_LOOP(M...
MoFEMErrorCode build_fields(int verb=DEFAULT_VERBOSITY)
MoFEMErrorCode get_field_entities_by_handle(const std::string name, Range &ents) const
get entities in the field by handle
virtual MoFEMErrorCode add_field(const std::string name, const FieldSpace space, const FieldApproximationBase base, const FieldCoefficientsNumber nb_coefficients, const TagType tag_type=MB_TAG_SPARSE, const enum MoFEMTypes bh=MF_EXCL, int verb=DEFAULT_VERBOSITY)
Add field.
MoFEMErrorCode list_fields() const
list entities in the field
MoFEMErrorCode get_field_entities_by_type(const std::string name, EntityType type, Range &ents) const
get entities in the field by type
Definition FieldCore.cpp:91
int getValue() const
Definition Core.hpp:40
structure to change FieldEntity order
static UId getHiBitNumberUId(const FieldBitNumber bit_number)
static boost::shared_ptr< FieldData *const > makeSharedFieldDataAdaptorPtr(const boost::shared_ptr< Field > &field_ptr, const boost::shared_ptr< RefEntity > &ref_ents_ptr)
Return shared pointer to entity field data vector adaptor.
static UId getLoBitNumberUId(const FieldBitNumber bit_number)
static UId getLoLocalEntityBitNumber(const char bit_number, const EntityHandle ent)
static UId getHiLocalEntityBitNumber(const char bit_number, const EntityHandle ent)
MultiIndex Tag for field name.
Field data structure for finite element approximation.
static MoFEMErrorCode getFileVersion(moab::Interface &moab, Version &version)
Get database major version.