v0.16.3
Loading...
Searching...
No Matches
dm_partitioned_no_field.cpp
Go to the documentation of this file.
1/** \file dm_build_partitioned_mesh.cpp
2 \example mofem/atom_tests/dm_partitioned_no_field.cpp
3 \brief Testing problem for partitioned mesh with NOFIELD field
4
5*/
6
7#include <MoFEM.hpp>
8
9using namespace MoFEM;
10
11static char help[] = "...\n\n";
12constexpr bool debug = false;
13
14int main(int argc, char *argv[]) {
15 // initialize petsc
16 MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
17
18 try {
19
20 PetscBool flg = PETSC_TRUE;
21 char mesh_file_name[255];
22 CHKERR PetscOptionsGetString(PETSC_NULLPTR, "", "-my_file", mesh_file_name,
23 255, &flg);
24 if (flg != PETSC_TRUE)
25 SETERRQ(PETSC_COMM_SELF, 1, "*** ERROR -my_file (MESH FILE NEEDED)");
26
27 // register new dm type, i.e. mofem
28 DMType dm_name = "DMMOFEM";
29 CHKERR DMRegister_MoFEM(dm_name);
30
31 // create dm instance
32 DM dm;
33 CHKERR DMCreate(PETSC_COMM_WORLD, &dm);
34 CHKERR DMSetType(dm, dm_name);
35
36 // read mesh and create moab and mofem data structures
37 moab::Core mb_instance;
38 moab::Interface &moab = mb_instance;
39
40 ParallelComm *pcomm = ParallelComm::get_pcomm(&moab, MYPCOMM_INDEX);
41 auto moab_comm_wrap =
42 boost::make_shared<WrapMPIComm>(PETSC_COMM_WORLD, false);
43 if (pcomm == NULL)
44 pcomm = new ParallelComm(&moab, moab_comm_wrap->get_comm());
45
46 const std::string options = "PARALLEL=READ_PART;"
47 "PARALLEL_RESOLVE_SHARED_ENTS;"
48 "PARTITION=PARALLEL_PARTITION;";
49 CHKERR moab.load_file(mesh_file_name, 0, options.c_str());
50
51 MoFEM::Core core(moab, PETSC_COMM_WORLD);
52 MoFEM::Interface &m_field = core;
53
54 auto *bit_ref_ptr = m_field.getInterface<BitRefManager>();
55 auto *comm_interface_ptr = m_field.getInterface<CommInterface>();
56
57 EntityHandle root_set = moab.get_root_set();
58 // add all entities to database, all of them will be used
59 CHKERR bit_ref_ptr->setBitRefLevelByDim(root_set, 3, BitRefLevel().set(0));
60
61 // add field
62 CHKERR m_field.add_field("FIELD", H1, AINSWORTH_LEGENDRE_BASE, 1);
63 CHKERR m_field.add_field("LAMBDA", NOFIELD, NOBASE, 3);
64
65 // add entities to field
66 CHKERR m_field.add_ents_to_field_by_type(root_set, MBTET, "FIELD");
67
68 // set app. order
69 CHKERR m_field.set_field_order(root_set, MBVERTEX, "FIELD", 1);
70
71 // Create vertices for NOFILE
72 std::array<double, 6> coords = {0, 0, 0, 0, 0, 0};
73 CHKERR m_field.create_vertices_and_add_to_field("LAMBDA", coords.data(), 2);
74 CHKERR comm_interface_ptr->makeFieldEntitiesMultishared("LAMBDA", 0,
75 NOISY);
76 CHKERR bit_ref_ptr->setFieldEntitiesBitRefLevel("LAMBDA",
77 BitRefLevel().set(0));
78
79 // build data structures for fields
80 CHKERR m_field.build_fields();
81
82 // Check owned, shared and multishared UID metadata against the separate
83 // ownership queries, including the NOFIELD entities shared on every rank.
84 for (const auto &field_ptr : *m_field.get_field_ents()) {
85 const auto &ref_ptr = field_ptr->getRefEntityPtr();
86 const auto owner = ref_ptr->getOwnerProcAndEnt();
88 ref_ptr->getOwnerProc(), field_ptr->getBitNumber(),
89 ref_ptr->getOwnerEnt());
90 if (owner.first != ref_ptr->getOwnerProc() ||
91 owner.second != ref_ptr->getOwnerEnt() ||
92 field_ptr->getGlobalUniqueId() != expected)
93 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
94 "Global UID ownership evaluation differs");
95 }
96
97 auto print_field_ents = [&](const std::string field_name) {
98 auto *field_ents = m_field.get_field_ents();
99 auto field_bit_number = m_field.get_field_bit_number(field_name);
100 auto lo = field_ents->get<Unique_mi_tag>().lower_bound(
101 FieldEntity::getLoBitNumberUId(field_bit_number));
102 auto hi = field_ents->get<Unique_mi_tag>().upper_bound(
103 FieldEntity::getHiBitNumberUId(field_bit_number));
104 for (auto it = lo; it != hi; ++it) {
105 std::ostringstream ss;
106 ss << "Rank " << m_field.get_comm_rank() << " -> " << **it << endl;
107 PetscSynchronizedPrintf(m_field.get_comm(), "%s", ss.str().c_str());
108 PetscSynchronizedFlush(m_field.get_comm(), PETSC_STDOUT);
109 }
110 };
111
112 print_field_ents("LAMBDA");
113
114 if (m_field.get_comm_rank() == 0) {
115 CHKERR m_field.getInterface<FieldBlas>()->setField(1, "LAMBDA");
116 CHKERR m_field.getInterface<FieldBlas>()->setField(1, "FIELD");
117 }
118
119 CHKERR comm_interface_ptr->exchangeFieldData("LAMBDA");
120 CHKERR comm_interface_ptr->exchangeFieldData("FIELD");
121
122 auto check_exchanged_values = [&](const std::string field_name) {
124 if (m_field.get_comm_rank() != 0) {
125 auto *field_ents = m_field.get_field_ents();
126 auto field_bit_number = m_field.get_field_bit_number(field_name);
127 auto lo = field_ents->get<Unique_mi_tag>().lower_bound(
128 FieldEntity::getLoBitNumberUId(field_bit_number));
129 auto hi = field_ents->get<Unique_mi_tag>().upper_bound(
130 FieldEntity::getHiBitNumberUId(field_bit_number));
131 for (auto it = lo; it != hi; ++it) {
132 VectorDouble field_data = (*it)->getEntFieldData();
133 for (auto v : field_data)
134 if (v != 1)
135 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
136 "Wrong value on field %4.3f", v);
137 }
138 }
140 };
141
142 CHKERR check_exchanged_values("LAMBDA");
143
144 // define & build finite elements
145 CHKERR m_field.add_finite_element("FE");
146 // Define rows/cols and element data
147 CHKERR m_field.modify_finite_element_add_field_row("FE", "FIELD");
148 CHKERR m_field.modify_finite_element_add_field_col("FE", "FIELD");
149 CHKERR m_field.modify_finite_element_add_field_data("FE", "FIELD");
150 CHKERR m_field.modify_finite_element_add_field_row("FE", "LAMBDA");
151 CHKERR m_field.modify_finite_element_add_field_col("FE", "LAMBDA");
152 CHKERR m_field.modify_finite_element_add_field_data("FE", "LAMBDA");
153 // add entities to finite element
154 CHKERR m_field.add_ents_to_finite_element_by_type(root_set, MBTET, "FE");
155
156 // add meshset finite element
157 CHKERR m_field.add_finite_element("MESHSET_FE");
158 auto create_meshset = [&]() {
159 EntityHandle fe_meshset;
160 auto create = [&]() {
162 CHKERR m_field.get_moab().create_meshset(MESHSET_SET, fe_meshset);
163 {
164 ParallelComm *pcomm =
165 ParallelComm::get_pcomm(&m_field.get_moab(), MYPCOMM_INDEX);
166 Tag part_tag = pcomm->part_tag();
167 int rank = pcomm->rank();
168 CHKERR m_field.get_moab().tag_set_data(part_tag, &fe_meshset, 1, &rank);
169 CHKERR m_field.getInterface<BitRefManager>()->setBitLevelToMeshset(
170 fe_meshset, BitRefLevel().set());
171
172 }
174 };
175 CHK_THROW_MESSAGE(create(), "Failed to create meshset");
176 return fe_meshset;
177 };
178
179 auto add_verts_to_meshset = [&](std::string field_name,
180 EntityHandle meshset) {
182 auto field_meshest = m_field.get_field_meshset(field_name);
183 Range verts;
184 CHKERR m_field.get_moab().get_entities_by_dimension(field_meshest, 0,
185 verts, true);
186 CHKERR m_field.get_moab().add_entities(meshset, verts);
188 };
189
190 auto fe_meshset = create_meshset();
191 CHKERR add_verts_to_meshset("FIELD", fe_meshset);
192
194 "MESHSET_FE", false);
195
196 CHKERR m_field.modify_finite_element_add_field_row("MESHSET_FE", "FIELD");
197 CHKERR m_field.modify_finite_element_add_field_col("MESHSET_FE", "FIELD");
198 CHKERR m_field.modify_finite_element_add_field_data("MESHSET_FE", "FIELD");
199 CHKERR m_field.modify_finite_element_add_field_row("MESHSET_FE", "LAMBDA");
200 CHKERR m_field.modify_finite_element_add_field_col("MESHSET_FE", "LAMBDA");
201 CHKERR m_field.modify_finite_element_add_field_data("MESHSET_FE", "LAMBDA");
202
203 // build finite elements
204 CHKERR m_field.build_finite_elements("FE");
205 CHKERR m_field.build_finite_elements("MESHSET_FE");
206 // build adjacencies
207 CHKERR m_field.build_adjacencies(BitRefLevel().set());
208
209 // set dm data structure which created mofem data structures
210 CHKERR DMMoFEMCreateMoFEM(dm, &m_field, "TEST_PROBLEM",
211 BitRefLevel().set(0));
213 dm, PETSC_FALSE); // this is for testing (this problem has the same rows
214 // and cols)
215 CHKERR DMSetFromOptions(dm);
216 CHKERR DMMoFEMAddElement(dm, "FE");
217 CHKERR DMMoFEMAddElement(dm, "MESHSET_FE");
218 CHKERR DMSetUp(dm);
219
220 Mat m;
221 CHKERR DMCreateMatrix(dm, &m);
222
224 ->checkMPIAIJWithArraysMatrixFillIn<PetscGlobalIdx_mi_tag>(
225 "TEST_PROBLEM", -1, -1, 1);
226
227 CHKERR MatDestroy(&m);
228
229 struct PrintField : public FEMethod {
230
231 PrintField(int bit_number) : bitNumber(bit_number) {}
232
233 MoFEMErrorCode preProcess() { return 0; }
236 MOFEM_LOG("SYNC", Sev::inform) << "Element: getDataDofsPtr";
237 auto data_dofs = getDataDofsPtr();
238 auto dit = data_dofs->get<Unique_mi_tag>().lower_bound(
240 auto hi_dit = data_dofs->get<Unique_mi_tag>().upper_bound(
242 for (; dit != hi_dit; dit++) {
243 MOFEM_LOG("SYNC", Sev::inform)
244 << "Ent " << (*dit)->getEnt() << " dof idx "
245 << (*dit)->getEntDofIdx();
246 }
247 MOFEM_LOG("SYNC", Sev::inform) << "Element: getRowDofsPtr";
248 auto row_dofs = getRowDofsPtr();
249 auto r_dit = row_dofs->get<Unique_mi_tag>().lower_bound(
251 auto hi_r_dit = row_dofs->get<Unique_mi_tag>().upper_bound(
253 for (; r_dit != hi_r_dit; r_dit++) {
254 MOFEM_LOG("SYNC", Sev::inform)
255 << "Dof idx " << (*r_dit)->getEntDofIdx() << " global idx "
256 << (*r_dit)->getPetscGlobalDofIdx() << " local idx "
257 << (*r_dit)->getPetscLocalDofIdx();
258 }
259 MOFEM_LOG("SYNC", Sev::inform) << "Element: getColDofsPtr";
260 auto col_dofs = getColDofsPtr();
261 auto c_dit = col_dofs->get<Unique_mi_tag>().lower_bound(
263 auto hi_c_dit = col_dofs->get<Unique_mi_tag>().upper_bound(
265 for (; c_dit != hi_c_dit; c_dit++) {
266 MOFEM_LOG("SYNC", Sev::inform)
267 << "Dof idx " << (*c_dit)->getEntDofIdx() << " global idx "
268 << (*c_dit)->getPetscGlobalDofIdx() << " local idx "
269 << (*c_dit)->getPetscLocalDofIdx();
270 }
272 }
273 MoFEMErrorCode postProcess() { return 0; }
274
275 private:
276 int bitNumber;
277 };
278
279 PrintField print_no_field(m_field.get_field_bit_number("LAMBDA"));
280
281 MOFEM_LOG("SYNC", Sev::inform) << "Loop elements MESHSET_FE (LAMBDA)";
282 CHKERR m_field.loop_finite_elements("TEST_PROBLEM", "MESHSET_FE",
283 print_no_field);
284
285 PrintField print_field(m_field.get_field_bit_number("FIELD"));
286 MOFEM_LOG("SYNC", Sev::inform) << "Loop elements MESHSET_FE (FIELD)";
287 CHKERR m_field.loop_finite_elements("TEST_PROBLEM", "MESHSET_FE",
288 print_field);
289
290
292
293 // check if file can be saved
294 if (debug)
295 CHKERR moab.write_file("test_out.h5m", "MOAB", "PARALLEL=WRITE_PART");
296
297 // destry dm
298 CHKERR DMDestroy(&dm);
299 }
301
302 // finish work cleaning memory, getting statistics, ect.
304
305 return 0;
306}
CGALInterfaceImpl & owner
#define MOFEM_LOG_SYNCHRONISE(comm)
Synchronise "SYNC" channel.
It it
Definition Schur.cpp:2568
int main()
@ NOISY
#define CATCH_ERRORS
Catch errors.
@ AINSWORTH_LEGENDRE_BASE
Ainsworth Cole (Legendre) approx. base .
Definition definitions.h:60
@ NOBASE
Definition definitions.h:59
#define CHK_THROW_MESSAGE(err, msg)
Check and throw MoFEM exception.
@ NOFIELD
scalar or vector of scalars describe (no true field)
Definition definitions.h:84
@ H1
continuous field
Definition definitions.h:85
#define MYPCOMM_INDEX
default communicator number PCOMM
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_ATOM_TEST_INVALID
Definition definitions.h:40
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
static char help[]
constexpr bool debug
PetscErrorCode DMMoFEMAddElement(DM dm, std::string fe_name)
add element to dm
Definition DMMoFEM.cpp:574
PetscErrorCode DMMoFEMSetSquareProblem(DM dm, PetscBool square_problem)
set squared problem
Definition DMMoFEM.cpp:536
PetscErrorCode DMMoFEMCreateMoFEM(DM dm, MoFEM::Interface *m_field_ptr, const char problem_name[], const MoFEM::BitRefLevel bit_level, const MoFEM::BitRefLevel bit_mask=MoFEM::BitRefLevel().set())
Must be called by user to set MoFEM data structures.
Definition DMMoFEM.cpp:143
PetscErrorCode DMRegister_MoFEM(const char sname[])
Register MoFEM problem.
Definition DMMoFEM.cpp:43
virtual const FieldEntity_multiIndex * get_field_ents() const =0
Get the field ents object.
MoFEMErrorCode setFieldEntitiesBitRefLevel(const std::string field_name, const BitRefLevel bit=BitRefLevel(), int verb=QUIET) const
Set the bit ref level to entities in the field meshset.
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_MESHSET(const EntityHandle meshset, const std::string &name, const bool recursive=false)=0
add MESHSET element to finite element database given by name
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 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 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.
virtual MoFEMErrorCode add_ents_to_field_by_type(const Range &ents, const EntityType type, const std::string &name, int verb=DEFAULT_VERBOSITY)=0
Add entities to field meshset.
#define MOFEM_LOG(channel, severity)
Log.
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.
const double v
phase velocity of light in medium (cm/ns)
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
PetscErrorCode PetscOptionsGetString(PetscOptions *, const char pre[], const char name[], char str[], size_t size, PetscBool *set)
constexpr auto field_name
FTensor::Index< 'm', 3 > m
virtual MoFEMErrorCode postProcess()
Post-processing function executed at loop completion.
virtual MoFEMErrorCode operator()()
Main operator function executed for each loop iteration.
virtual MoFEMErrorCode preProcess()
Pre-processing function executed at loop initialization.
Managing BitRefLevels.
Managing BitRefLevels.
virtual FieldBitNumber get_field_bit_number(const std::string name) const =0
get field bit number
virtual moab::Interface & get_moab()=0
virtual EntityHandle get_field_meshset(const std::string name) const =0
get field meshset
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 MPI_Comm & get_comm() const =0
virtual int get_comm_rank() const =0
virtual MoFEMErrorCode create_vertices_and_add_to_field(const std::string name, const double coords[], int size, int verb=DEFAULT_VERBOSITY)=0
Create vertices and add to the field object.
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.
Structure for user loop methods on finite elements.
auto getDataDofsPtr() const
Get pointer to DOF data for the current finite element.
auto getColDofsPtr() const
Get pointer to column DOFs for the current finite element.
auto getRowDofsPtr() const
Get pointer to row DOFs for the current finite element.
Basic algebra on fields.
Definition FieldBlas.hpp:21
static UId getHiBitNumberUId(const FieldBitNumber bit_number)
static UId getLoBitNumberUId(const FieldBitNumber bit_number)
UId getGlobalUniqueIdCalculate() const
Calculate global UId.
Matrix manager is used to build and partition problems.
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.