v0.16.3
Loading...
Searching...
No Matches
build_problems.cpp
Go to the documentation of this file.
1/** \file build_problem.cpp
2 \example mofem/atom_tests/build_problems.cpp
3 \brief Atom test for building problems
4
5 \bug Not verifying what if partitioned mesh is loaded.
6
7*/
8
9#include <MoFEM.hpp>
10using namespace MoFEM;
11
12static char help[] = "...\n\n";
13
14namespace {
15
16MoFEMErrorCode checkUidEvaluation(MoFEM::Interface &m_field) {
18 int mismatches = 0;
19 for (const auto &field_ptr : *m_field.get_field_ents()) {
20 const auto &ref_ptr = field_ptr->getRefEntityPtr();
21 const auto owner = ref_ptr->getOwnerProcAndEnt();
22 mismatches += owner.first != ref_ptr->getOwnerProc();
23 mismatches += owner.second != ref_ptr->getOwnerEnt();
25 ref_ptr->getOwnerProc(), field_ptr->getBitNumber(),
26 ref_ptr->getOwnerEnt());
27 mismatches += field_ptr->getGlobalUniqueId() != expected;
28 }
29 for (const auto &dof_ptr : *m_field.get_dofs()) {
30 const UId local = dof_ptr->getFieldEntityPtr()->getLocalUniqueId() |
31 dof_ptr->getEntDofIdx();
32 const UId global = dof_ptr->getFieldEntityPtr()->getGlobalUniqueId() |
33 dof_ptr->getEntDofIdx();
34 mismatches += dof_ptr->getLocalUniqueId() != local;
35 mismatches += dof_ptr->getGlobalUniqueId() != global;
36 NumeredDofEntity numbered(dof_ptr);
37 const auto &entity_uid = numbered.getEntGlobalUniqueId();
38 mismatches +=
39 entity_uid != dof_ptr->getFieldEntityPtr()->getGlobalUniqueId();
40 }
41 for (char field : {char(1), char(BITFIELDID_SIZE)})
42 for (EntityHandle handle : {EntityHandle(0), EntityHandle(1),
43 std::numeric_limits<EntityHandle>::max()})
44 for (int owner : {0, MAX_PROCESSORS_NUMBER - 1})
45 for (int dof : {0, MAX_DOFS_ON_ENTITY - 1, -1}) {
46 const UId entity_uid =
48 const UId expected = entity_uid | dof;
49 UId uid = entity_uid;
50 uid = DofEntity::getUniqueIdCalculate(dof, uid);
51 mismatches += uid != expected;
52 }
54 "UID evaluation preserves ownership, encoding and value lifetime",
55 mismatches, 0, TestingInterface::Tolerance{0, 0});
57}
58
59/// Compare traversal and bounds with a stable sort independent of the trees.
60template <int I = 0>
61MoFEMErrorCode checkPublishedIndex(TestingInterface &testing,
62 const std::string &label,
63 const NumeredDofEntity_multiIndex &dofs) {
65 const auto &index = dofs.get<I>();
66 std::vector<boost::shared_ptr<NumeredDofEntity>> expected(dofs.begin(),
67 dofs.end());
68 const auto compare = index.value_comp();
69 std::stable_sort(expected.begin(), expected.end(), compare);
70 int mismatches = 0;
71 auto actual = index.begin();
72 for (auto first = expected.begin(); first != expected.end();) {
73 auto last = std::next(first);
74 while (last != expected.end() && !compare(*first, *last))
75 ++last;
76 const auto key = index.key_extractor()(*first);
77 mismatches += index.lower_bound(key) != actual;
78 for (auto it = first; it != last; ++it, ++actual)
79 mismatches += (*it)->getLocalUniqueId() != (*actual)->getLocalUniqueId();
80 mismatches += index.upper_bound(key) != actual;
81 first = last;
82 }
83 CHKERR testing.checkValue<TestType::SCALAR>(
84 label + " index " + std::to_string(I) + " traversal and bounds",
85 mismatches, 0, TestingInterface::Tolerance{0, 0});
86 // Five ordered indices: UID, part, PETSc global, PETSc local, entity.
87 if constexpr (I < 4) {
88 CHKERR checkPublishedIndex<I + 1>(testing, label, dofs);
89 }
91}
92
93MoFEMErrorCode checkPublishedDofs(MoFEM::Interface &m_field,
94 const std::string &name) {
96 const Problem *problem_ptr;
97 CHKERR m_field.get_problem(name, &problem_ptr);
98 auto testing_ptr = m_field.getInterface<TestingInterface>();
99 CHKERR checkPublishedIndex(*testing_ptr, name + " rows",
100 *problem_ptr->getNumeredRowDofsPtr());
101 if (problem_ptr->getNumeredRowDofsPtr() !=
102 problem_ptr->getNumeredColDofsPtr()) {
103 CHKERR checkPublishedIndex(*testing_ptr, name + " columns",
104 *problem_ptr->getNumeredColDofsPtr());
105 }
107}
108
109} // namespace
110
111int main(int argc, char *argv[]) {
112
113 MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
114
115 try {
116
117 moab::Core mb_instance;
118 moab::Interface &moab = mb_instance;
119 int rank;
120 MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
121
122 PetscBool flg = PETSC_TRUE;
123 char mesh_file_name[255];
124#if PETSC_VERSION_GE(3, 6, 4)
125 CHKERR PetscOptionsGetString(PETSC_NULLPTR, "", "-my_file", mesh_file_name,
126 255, &flg);
127#else
128 CHKERR PetscOptionsGetString(PETSC_NULLPTR, PETSC_NULLPTR, "-my_file",
129 mesh_file_name, 255, &flg);
130#endif
131 if (flg != PETSC_TRUE) {
132 SETERRQ(PETSC_COMM_SELF, 1, "*** ERROR -my_file (MESH FILE NEEDED)");
133 }
134
135 CHKERR moab.load_file(mesh_file_name, 0, "");
136
137 // Create MoFEM database
138 MoFEM::Core core(moab);
139 MoFEM::Interface &m_field = core;
140
141 // set entities bit level
142 BitRefLevel bit_level0;
143 bit_level0.set(0);
144 CHKERR m_field.getInterface<BitRefManager>()->setBitRefLevelByDim(
145 0, 3, bit_level0);
146
147 // Fields
148 CHKERR m_field.add_field("F1", L2, AINSWORTH_LEGENDRE_BASE, 1);
149 CHKERR m_field.add_field("F2", HDIV, DEMKOWICZ_JACOBI_BASE, 1);
150
151 // meshset consisting all entities in mesh
152 EntityHandle root_set = moab.get_root_set();
153 // add entities to field
154 CHKERR m_field.add_ents_to_field_by_dim(root_set, 3, "F1", VERBOSE);
155 CHKERR m_field.add_ents_to_field_by_dim(root_set, 3, "F2", VERBOSE);
156
157 // set app. order
158 // see Hierarchic Finite Element Bases on Unstructured Tetrahedral Meshes
159 // (Mark Ainsworth & Joe Coyle)
160 int order = 2;
161 CHKERR m_field.set_field_order(root_set, MBTET, "F1", order - 1);
162 CHKERR m_field.set_field_order(root_set, MBHEX, "F1", order - 1);
163 CHKERR m_field.set_field_order(root_set, MBTET, "F2", order);
164 CHKERR m_field.set_field_order(root_set, MBTRI, "F2", order);
165 CHKERR m_field.set_field_order(root_set, MBHEX, "F2", order);
166 CHKERR m_field.set_field_order(root_set, MBQUAD, "F2", order);
167
168 CHKERR m_field.build_fields();
169 CHKERR checkUidEvaluation(m_field);
170
171 // add elements
172 CHKERR m_field.add_finite_element("E1");
173 CHKERR m_field.add_finite_element("E2");
174 CHKERR m_field.add_finite_element("E3");
175
180 // To build composite problem
183
184 CHKERR m_field.add_ents_to_finite_element_by_type(root_set, MBTET, "E1");
185 CHKERR m_field.add_ents_to_finite_element_by_type(root_set, MBTET, "E2");
186 CHKERR m_field.add_ents_to_finite_element_by_type(root_set, MBTET, "E3");
187 CHKERR m_field.add_ents_to_finite_element_by_type(root_set, MBHEX, "E1");
188 CHKERR m_field.add_ents_to_finite_element_by_type(root_set, MBHEX, "E2");
189 CHKERR m_field.add_ents_to_finite_element_by_type(root_set, MBHEX, "E3");
190
192 CHKERR m_field.build_adjacencies(bit_level0);
193
194 // Problems
195 CHKERR m_field.add_problem("P1");
196 CHKERR m_field.add_problem("P2");
197
198 // set refinement level for problem
199 CHKERR m_field.modify_problem_ref_level_add_bit("P1", bit_level0);
200 CHKERR m_field.modify_problem_ref_level_add_bit("P2", bit_level0);
201
202 CHKERR m_field.modify_problem_add_finite_element("P1", "E1");
203 CHKERR m_field.modify_problem_add_finite_element("P2", "E2");
204
205 // build problems
206 ProblemsManager *prb_mng_ptr;
207 CHKERR m_field.getInterface(prb_mng_ptr);
208 CHKERR prb_mng_ptr->buildProblem("P1", true);
209 CHKERR prb_mng_ptr->buildProblem("P2", true);
210 CHKERR prb_mng_ptr->partitionProblem("P1");
211 CHKERR prb_mng_ptr->partitionProblem("P2");
212
213 CHKERR prb_mng_ptr->partitionFiniteElements("P1");
214 CHKERR prb_mng_ptr->partitionGhostDofs("P1");
216 ->checkMPIAIJWithArraysMatrixFillIn<PetscGlobalIdx_mi_tag>("P1", -1, -1,
217 0);
218 CHKERR prb_mng_ptr->partitionFiniteElements("P2");
219 CHKERR prb_mng_ptr->partitionGhostDofs("P2");
221 ->checkMPIAIJWithArraysMatrixFillIn<PetscGlobalIdx_mi_tag>("P2", -1, -1,
222 0);
223 // compose problem
224 CHKERR m_field.add_problem("P3");
225 CHKERR m_field.modify_problem_ref_level_add_bit("P3", bit_level0);
226 CHKERR m_field.modify_problem_add_finite_element("P3", "E3");
227
228 CHKERR prb_mng_ptr->buildProblem("P3", false);
229 CHKERR checkPublishedDofs(m_field, "P3");
230 CHKERR prb_mng_ptr->inheritPartition("P3", "P1", false, "P2", true);
231 CHKERR prb_mng_ptr->partitionFiniteElements("P3");
232 CHKERR prb_mng_ptr->partitionGhostDofs("P3");
234 ->checkMPIAIJWithArraysMatrixFillIn<PetscGlobalIdx_mi_tag>("P3", -1, -1,
235 0);
236
237 // Both fields include volume entities: UID order resets the entity key
238 // between fields. Also exercise sub-problem and composed publication.
239 CHKERR m_field.add_problem("P4");
240 CHKERR m_field.modify_problem_ref_level_add_bit("P4", bit_level0);
241 CHKERR m_field.modify_problem_add_finite_element("P4", "E1");
242 CHKERR m_field.modify_problem_add_finite_element("P4", "E2");
243 CHKERR prb_mng_ptr->buildProblem("P4", true);
244 CHKERR checkPublishedDofs(m_field, "P4");
245 CHKERR prb_mng_ptr->partitionProblem("P4");
246 CHKERR prb_mng_ptr->partitionFiniteElements("P4");
247 CHKERR prb_mng_ptr->partitionGhostDofs("P4");
248 CHKERR m_field.add_problem("P4_sub");
249 CHKERR m_field.modify_problem_ref_level_add_bit("P4_sub", bit_level0);
250 CHKERR prb_mng_ptr->buildSubProblem("P4_sub", {"F2", "F1"}, {"F2", "F1"},
251 "P4");
252 CHKERR checkPublishedDofs(m_field, "P4_sub");
253 CHKERR m_field.add_problem("P4_empty_field");
254 CHKERR m_field.modify_problem_ref_level_add_bit("P4_empty_field",
255 bit_level0);
256 const std::map<std::string, boost::shared_ptr<Range>> entity_map = {
257 {"F2", boost::make_shared<Range>()}};
258 CHKERR prb_mng_ptr->buildSubProblem("P4_empty_field", {"F1", "F2"},
259 {"F1", "F2"}, "P4", true, &entity_map,
260 &entity_map);
261 CHKERR checkPublishedDofs(m_field, "P4_empty_field");
262 CHKERR m_field.add_problem("P4_composed");
263 CHKERR m_field.modify_problem_ref_level_add_bit("P4_composed", bit_level0);
264 CHKERR prb_mng_ptr->buildComposedProblem("P4_composed", {"P1", "P2"},
265 {"P1", "P2"});
266 CHKERR checkPublishedDofs(m_field, "P4_composed");
267
268 // A fully excluded rectangular block leaves valid rows with empty column
269 // lists. Exercise both CSR publication and count-only preallocation.
270 const Problem *problem_ptr;
271 CHKERR m_field.get_problem("P3", &problem_ptr);
272 problem_ptr->addFieldToEmptyFieldBlocks(
273 {m_field.get_field_id("F1"), m_field.get_field_id("F2")});
274 auto matrix_ptr = m_field.getInterface<MatrixManager>();
275 auto testing_ptr = m_field.getInterface<TestingInterface>();
276 const auto zero = TestingInterface::Tolerance{0, 0};
277 SmartPetscObj<Mat> empty_matrix;
278 CHKERR matrix_ptr->createMPIAIJWithArrays<PetscGlobalIdx_mi_tag>(
279 "P3", empty_matrix);
280 MatInfo info;
281 CHKERR MatGetInfo(empty_matrix, MAT_GLOBAL_SUM, &info);
282 CHKERR testing_ptr->checkValue<TestType::SCALAR>(
283 "excluded field block has an empty CSR graph", info.nz_used, 0, zero);
284 empty_matrix.reset();
285 CHKERR matrix_ptr->createMPIAIJ<PetscGlobalIdx_mi_tag>("P3", empty_matrix);
286 CHKERR MatGetInfo(empty_matrix, MAT_GLOBAL_SUM, &info);
287 CHKERR testing_ptr->checkValue<TestType::SCALAR>(
288 "excluded field block has zero preallocation", info.nz_allocated, 0,
289 zero);
290 }
292
293 // finish work cleaning memory, getting statistics, etc.
295
296 return 0;
297}
CGALInterfaceImpl & owner
std::map< EntityHandle, int > index
std::vector< int > label
EntityHandle handle
ParentChildKey key
ProjectedField & field
It it
Definition Schur.cpp:2568
std::string name
int main()
static char help[]
@ VERBOSE
#define CATCH_ERRORS
Catch errors.
#define MAX_DOFS_ON_ENTITY
Maximal number of DOFs on entity.
@ AINSWORTH_LEGENDRE_BASE
Ainsworth Cole (Legendre) approx. base .
Definition definitions.h:60
@ DEMKOWICZ_JACOBI_BASE
Definition definitions.h:66
#define MAX_PROCESSORS_NUMBER
Maximal number of processors.
@ L2
field with C-1 continuity
Definition definitions.h:88
@ HDIV
field with continuous normal traction
Definition definitions.h:87
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define BITFIELDID_SIZE
max number of fields
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
constexpr int order
multi_index_container< boost::shared_ptr< NumeredDofEntity >, indexed_by< ordered_unique< tag< Unique_mi_tag >, const_mem_fun< NumeredDofEntity::interface_type_DofEntity, UId, &NumeredDofEntity::getLocalUniqueId > >, ordered_non_unique< tag< Part_mi_tag >, member< NumeredDofEntity, unsigned int, &NumeredDofEntity::pArt > >, ordered_non_unique< tag< PetscGlobalIdx_mi_tag >, member< NumeredDofEntity, DofIdx, &NumeredDofEntity::petscGloablDofIdx > >, ordered_non_unique< tag< PetscLocalIdx_mi_tag >, member< NumeredDofEntity, DofIdx, &NumeredDofEntity::petscLocalDofIdx > >, ordered_non_unique< tag< Ent_mi_tag >, const_mem_fun< NumeredDofEntity::interface_type_DofEntity, EntityHandle, &NumeredDofEntity::getEnt > > > > NumeredDofEntity_multiIndex
MultiIndex container keeps NumeredDofEntity.
virtual const Problem * get_problem(const std::string problem_name) const =0
Get the problem object.
virtual const FieldEntity_multiIndex * get_field_ents() const =0
Get the field ents object.
virtual const DofEntity_multiIndex * get_dofs() const =0
Get the dofs object.
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 add_ents_to_finite_element_by_type(const EntityHandle entities, const EntityType type, const std::string name, const bool recursive=true)=0
add entities to finite element
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 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.
MoFEMErrorCode partitionGhostDofs(const std::string name, int verb=VERBOSE)
determine ghost nodes
MoFEMErrorCode buildComposedProblem(const std::string out_name, const std::vector< std::string > add_row_problems, const std::vector< std::string > add_col_problems, const bool square_matrix=true, int verb=1)
build composite problem
MoFEMErrorCode buildProblem(const std::string name, const bool square_matrix, int verb=VERBOSE)
build problem data structures
MoFEMErrorCode buildSubProblem(const std::string out_name, const std::vector< std::string > &fields_row, const std::vector< std::string > &fields_col, const std::string main_problem, const bool square_matrix=true, const map< std::string, boost::shared_ptr< Range > > *entityMapRow=nullptr, const map< std::string, boost::shared_ptr< Range > > *entityMapCol=nullptr, int verb=VERBOSE)
build sub problem
MoFEMErrorCode partitionProblem(const std::string name, int verb=VERBOSE)
partition problem dofs (collective)
MoFEMErrorCode inheritPartition(const std::string name, const std::string problem_for_rows, bool copy_rows, const std::string problem_for_cols, bool copy_cols, int verb=VERBOSE)
build indexing and partition problem inheriting indexing and partitioning from two other problems
MoFEMErrorCode partitionFiniteElements(const std::string name, bool part_from_moab=false, int low_proc=-1, int hi_proc=-1, int verb=VERBOSE)
partition finite elements
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
PetscBool testing
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
uint128_t UId
Unique Id.
Definition Types.hpp:31
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
MoFEMErrorCode TestingInterface::checkValue< TestType::SCALAR >(const std::string &check_name, PetscReal actual, PetscReal expected, Tolerance tolerance, Sev error_severity, Sev success_severity) const
PetscErrorCode PetscOptionsGetString(PetscOptions *, const char pre[], const char name[], char str[], size_t size, PetscBool *set)
constexpr IntegrationType I
Managing BitRefLevels.
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.
virtual BitFieldId get_field_id(const std::string &name) const =0
Get field Id.
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
Deprecated interface functions.
static UId getUniqueIdCalculate(const DofIdx dof, const UId &ent_uid)
UId getGlobalUniqueIdCalculate() const
Calculate global UId.
Matrix manager is used to build and partition problems.
keeps information about indexed dofs for the problem
Index tags of the numbered DOF containers that also read the index.
keeps basic data about problem
auto & getNumeredColDofsPtr() const
get access to numeredColDofsPtr storing DOFs on cols
BlockFieldPair & addFieldToEmptyFieldBlocks(const BlockFieldPair add_fields) const
Add fields to the empty field blocks.
auto & getNumeredRowDofsPtr() const
get access to numeredRowDofsPtr storing DOFs on rows
Problem manager is used to build and partition problems.
intrusive_ptr for managing petsc objects
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.