v0.14.0
dm_build_partitioned_mesh.cpp
Go to the documentation of this file.
1 /** \file dm_build_partitioned_mesh.cpp
2  \example dm_build_partitioned_mesh.cpp
3  \brief Testing problem for partitioned mesh
4 
5 */
6 
7 #include <MoFEM.hpp>
8 
9 using namespace MoFEM;
10 
11 static char help[] = "...\n\n";
12 
13 struct CountUp : FEMethod {
14 
15  CountUp(int &counter) : FEMethod(), cOunter(counter) {}
16  MoFEMErrorCode preProcess() { return 0; }
18  ++cOunter;
19  return 0;
20  }
21  MoFEMErrorCode postProcess() { return 0; }
22 
23 private:
24  int &cOunter;
25 };
26 
27 struct CountDown : FEMethod {
28 
29  CountDown(int &counter) : FEMethod(), cOunter(counter) {}
30  MoFEMErrorCode preProcess() { return 0; }
32  --cOunter;
33  return 0;
34  }
35  MoFEMErrorCode postProcess() { return 0; }
36 
37 private:
38  int &cOunter;
39 };
40 
41 int main(int argc, char *argv[]) {
42  // initialize petsc
43  MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
44 
45  try {
46 
47  PetscBool flg = PETSC_TRUE;
48  char mesh_file_name[255];
49 #if PETSC_VERSION_GE(3, 6, 4)
50  CHKERR PetscOptionsGetString(PETSC_NULL, "", "-my_file", mesh_file_name,
51  255, &flg);
52 #else
53  CHKERR PetscOptionsGetString(PETSC_NULL, PETSC_NULL, "-my_file",
54  mesh_file_name, 255, &flg);
55 #endif
56  if (flg != PETSC_TRUE) {
57  SETERRQ(PETSC_COMM_SELF, 1, "*** ERROR -my_file (MESH FILE NEEDED)");
58  }
59 
60  // register new dm type, i.e. mofem
61  DMType dm_name = "DMMOFEM";
62  CHKERR DMRegister_MoFEM(dm_name);
63 
64  // create dm instance
65  DM dm;
66  CHKERR DMCreate(PETSC_COMM_WORLD, &dm);
67  CHKERR DMSetType(dm, dm_name);
68 
69  // read mesh and create moab and mofem data structures
70  moab::Core mb_instance;
71  moab::Interface &moab = mb_instance;
72 
73  ParallelComm *pcomm = ParallelComm::get_pcomm(&moab, MYPCOMM_INDEX);
74  auto moab_comm_wrap =
75  boost::make_shared<WrapMPIComm>(PETSC_COMM_WORLD, false);
76  if (pcomm == NULL)
77  pcomm = new ParallelComm(&moab, moab_comm_wrap->get_comm());
78 
79  const char *option;
80  option = "PARALLEL=BCAST_DELETE;PARALLEL_RESOLVE_SHARED_ENTS;PARTITION="
81  "PARALLEL_PARTITION;";
82  CHKERR moab.load_file(mesh_file_name, 0, option);
83 
84  MoFEM::Core core(moab, PETSC_COMM_WORLD);
85  MoFEM::Interface &m_field = core;
86 
87  EntityHandle root_set = moab.get_root_set();
88  // add all entities to database, all of them will be used
89  BitRefLevel bit_level0;
90  bit_level0.set(0);
91  CHKERR m_field.getInterface<BitRefManager>()->setBitRefLevelByDim(
92  root_set, 3, bit_level0);
93  // define & build field
94  int field_rank = 3;
95 #if PETSC_VERSION_GE(3, 6, 4)
96  CHKERR PetscOptionsGetInt(PETSC_NULL, "", "-my_field_rank", &field_rank,
97  &flg);
98 #else
99  CHKERR PetscOptionsGetInt(PETSC_NULL, PETSC_NULL, "-my_field_rank",
100  &field_rank, &flg);
101 #endif
102  CHKERR m_field.add_field("FIELD1", H1, AINSWORTH_LEGENDRE_BASE, field_rank);
103  CHKERR m_field.add_field("FIELD2", L2, AINSWORTH_LEGENDRE_BASE, 1);
104 
105  // add entities to field
106  CHKERR m_field.add_ents_to_field_by_type(root_set, MBTET, "FIELD1");
107  CHKERR m_field.add_ents_to_field_by_type(root_set, MBTET, "FIELD2");
108 
109  // set app. order
110  int order = 4;
111  CHKERR m_field.set_field_order(root_set, MBTET, "FIELD1", order);
112  CHKERR m_field.set_field_order(root_set, MBTRI, "FIELD1", order);
113  CHKERR m_field.set_field_order(root_set, MBEDGE, "FIELD1", order);
114  CHKERR m_field.set_field_order(root_set, MBVERTEX, "FIELD1", 1);
115  CHKERR m_field.set_field_order(root_set, MBTET, "FIELD2", order);
116 
117  // build data structures for fields
118  CHKERR m_field.build_fields();
119 
120  // define & build finite elements
121  CHKERR m_field.add_finite_element("FE");
122  // Define rows/cols and element data
123  CHKERR m_field.modify_finite_element_add_field_row("FE", "FIELD1");
124  CHKERR m_field.modify_finite_element_add_field_col("FE", "FIELD1");
125  CHKERR m_field.modify_finite_element_add_field_data("FE", "FIELD1");
126  CHKERR m_field.modify_finite_element_add_field_row("FE", "FIELD2");
127  CHKERR m_field.modify_finite_element_add_field_col("FE", "FIELD2");
128  CHKERR m_field.modify_finite_element_add_field_data("FE", "FIELD2");
129  // Only data
130  CHKERR m_field.add_finite_element("FE_ONLY_DATA");
131  CHKERR m_field.modify_finite_element_add_field_data("FE_ONLY_DATA",
132  "FIELD1");
133  CHKERR m_field.modify_finite_element_add_field_data("FE_ONLY_DATA",
134  "FIELD2");
135  // Add entities to finite element
136  CHKERR m_field.add_ents_to_finite_element_by_type(root_set, MBTET, "FE");
137  CHKERR m_field.add_ents_to_finite_element_by_type(root_set, MBTET,
138  "FE_ONLY_DATA");
139  // build finite elemnts
140  CHKERR m_field.build_finite_elements();
141  // build adjacencies
142  CHKERR m_field.build_adjacencies(bit_level0);
143 
144  // set dm data structure which created mofem data structures
145  CHKERR DMMoFEMCreateMoFEM(dm, &m_field, "TEST_PROBLEM", bit_level0);
146  CHKERR DMMoFEMSetIsPartitioned(dm, PETSC_TRUE);
148  dm, PETSC_FALSE); // this is for testing (this problem has the same rows
149  // and cols)
150  CHKERR DMSetFromOptions(dm);
151  CHKERR DMMoFEMAddElement(dm, "FE");
152  CHKERR DMMoFEMAddElement(dm, "FE_ONLY_DATA");
153  CHKERR DMSetUp(dm);
154 
155  // dump data to file, just to check if something was changed
158 
160  ->checkMPIAIJWithArraysMatrixFillIn<PetscGlobalIdx_mi_tag>(
161  "TEST_PROBLEM", -1, -1, 1);
162 
164  ->checkMPIAIJMatrixFillIn<PetscGlobalIdx_mi_tag>("TEST_PROBLEM", -1, -1,
165  1);
166 
167  std::vector<std::string> fields_list;
168  fields_list.push_back("FIELD1");
169 
170  // PetscSection section;
171  PetscSection section;
172  CHKERR m_field.getInterface<ISManager>()->sectionCreate("TEST_PROBLEM",
173  &section);
174  CHKERR PetscSectionView(section, PETSC_VIEWER_STDOUT_WORLD);
175  CHKERR DMSetSection(dm, section);
176  CHKERR PetscSectionDestroy(&section);
177 
178  PetscBool save_file = PETSC_TRUE;
179 #if PETSC_VERSION_GE(3, 6, 4)
180  CHKERR PetscOptionsGetBool(PETSC_NULL, "", "-my_save_file", &save_file,
181  &flg);
182 #else
183  CHKERR PetscOptionsGetBool(PETSC_NULL, PETSC_NULL, "-my_save_file",
184  &save_file, &flg);
185 #endif
186  if (save_file) {
187  PetscViewer viewer;
188  CHKERR PetscViewerASCIIOpen(PETSC_COMM_WORLD,
189  "dm_build_partitioned_mesh.txt", &viewer);
190  CHKERR PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_INFO);
191  MatView(m, viewer);
192  CHKERR PetscViewerDestroy(&viewer);
193  }
194 
195  int count = 0;
197  boost::make_shared<CountUp>(count));
198  CHKERR DMoFEMLoopFiniteElements(dm, "FE_ONLY_DATA",
199  boost::make_shared<CountDown>(count));
200  if (count)
201  SETERRQ1(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID, "Should be zero %d",
202  count);
203 
204  auto check_consistency_of_uids = [&]() {
206  auto field_ents_ptr = m_field.get_field_ents();
207  for (auto &fe : *field_ents_ptr) {
208  if (fe->getOwnerProc() !=
209  (FieldEntity::getOwnerFromUniqueId(fe->getGlobalUniqueId()))) {
210  MOFEM_LOG("SELF", Sev::error) << *fe;
211  MOFEM_LOG("SELF", Sev::error)
212  << "UId owner proc "
213  << FieldEntity::getOwnerFromUniqueId(fe->getGlobalUniqueId());
214  MOFEM_LOG("SELF", Sev::error)
215  << "Entity owner proc " << fe->getOwnerProc();
216  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
217  "UId and entity handle inconsistency");
218  }
219  if (fe->getEnt() !=
220  FieldEntity::getHandleFromUniqueId(fe->getLocalUniqueId())) {
221  MOFEM_LOG("SELF", Sev::error) << *fe;
222  MOFEM_LOG("SELF", Sev::error)
223  << "UId handle "
224  << FieldEntity::getHandleFromUniqueId(fe->getLocalUniqueId());
225  MOFEM_LOG("SELF", Sev::error) << "Entity handle " << fe->getEnt();
226  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
227  "UId and entity handle inconsistency");
228  }
229  const auto field_bit_number_from_uid =
230  FieldEntity::getFieldBitNumberFromUniqueId(fe->getLocalUniqueId());
231  if (fe->getBitNumber() != field_bit_number_from_uid) {
232  MOFEM_LOG("SELF", Sev::error) << *fe;
233  MOFEM_LOG("SELF", Sev::error)
234  << "UId field bit " << field_bit_number_from_uid;
235  MOFEM_LOG("SELF", Sev::error)
236  << "Entity owner proc " << fe->getBitNumber();
237  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
238  "UId and entity handle inconsistency");
239  }
240  if (fe->getName() != m_field.get_field_name(BitFieldId().set(
241  field_bit_number_from_uid - 1))) {
242  MOFEM_LOG("SELF", Sev::error) << *fe;
243  MOFEM_LOG("SELF", Sev::error)
244  << "UId field bit "
245  << m_field.get_field_name(
246  BitFieldId().set(field_bit_number_from_uid - 1));
247  MOFEM_LOG("SELF", Sev::error)
248  << "Entity owner proc " << fe->getName();
249  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
250  "UId and entity handle inconsistency");
251  }
252  }
253 
255  };
256 
257  CHKERR check_consistency_of_uids();
258 
259  // destry dm
260  CHKERR DMDestroy(&dm);
261  }
262  CATCH_ERRORS;
263 
264  // finish work cleaning memory, getting statistics, ect.
266 
267  return 0;
268 }
MoFEM::UnknownInterface::getInterface
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.
Definition: UnknownInterface.hpp:93
CountDown::operator()
MoFEMErrorCode operator()()
function is run for every finite element
Definition: dm_build_partitioned_mesh.cpp:31
MoFEM::CoreInterface::get_field_name
virtual std::string get_field_name(const BitFieldId id) const =0
get field name from id
MYPCOMM_INDEX
#define MYPCOMM_INDEX
default communicator number PCOMM
Definition: definitions.h:215
MoFEM::CoreTmp< 0 >
Core (interface) class.
Definition: Core.hpp:82
H1
@ H1
continuous field
Definition: definitions.h:85
EntityHandle
CountUp::operator()
MoFEMErrorCode operator()()
function is run for every finite element
Definition: dm_build_partitioned_mesh.cpp:17
MoFEM::FEMethod
structure for User Loop Methods on finite elements
Definition: LoopMethods.hpp:369
MoFEM::CoreInterface::modify_finite_element_add_field_row
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
MoFEM::DMMoFEMSetSquareProblem
PetscErrorCode DMMoFEMSetSquareProblem(DM dm, PetscBool square_problem)
set squared problem
Definition: DMMoFEM.cpp:456
L2
@ L2
field with C-1 continuity
Definition: definitions.h:88
MoFEM::Exceptions::MoFEMErrorCode
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:56
MoFEM::Types::BitFieldId
std::bitset< BITFIELDID_SIZE > BitFieldId
Field Id.
Definition: Types.hpp:42
MoFEM.hpp
MoFEM::CoreTmp< 0 >::Finalize
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition: Core.cpp:112
MoFEM::CoreInterface::add_ents_to_field_by_type
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.
MoFEM::DMMoFEMAddElement
PetscErrorCode DMMoFEMAddElement(DM dm, std::string fe_name)
add element to dm
Definition: DMMoFEM.cpp:497
order
constexpr int order
Definition: dg_projection.cpp:18
MoFEM::DeprecatedCoreInterface
Deprecated interface functions.
Definition: DeprecatedCoreInterface.hpp:16
MoFEM::Interface
DeprecatedCoreInterface Interface
Definition: Interface.hpp:1975
CountUp::postProcess
MoFEMErrorCode postProcess()
function is run at the end of loop
Definition: dm_build_partitioned_mesh.cpp:21
MoFEM::CoreInterface::add_ents_to_finite_element_by_type
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
MoFEM::DMCreateMatrix_MoFEM
PetscErrorCode DMCreateMatrix_MoFEM(DM dm, Mat *M)
Definition: DMMoFEM.cpp:1197
CHKERR
#define CHKERR
Inline error check.
Definition: definitions.h:535
MoFEM::CoreInterface::add_finite_element
virtual MoFEMErrorCode add_finite_element(const std::string &fe_name, enum MoFEMTypes bh=MF_EXCL, int verb=DEFAULT_VERBOSITY)=0
add finite element
MoFEM
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:10
MoFEM::CoreInterface::modify_finite_element_add_field_col
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
help
static char help[]
Definition: dm_build_partitioned_mesh.cpp:11
MoFEM::ISManager
Section manager is used to create indexes and sections.
Definition: ISManager.hpp:23
MoFEM::CoreInterface::build_finite_elements
virtual MoFEMErrorCode build_finite_elements(int verb=DEFAULT_VERBOSITY)=0
Build finite elements.
CountUp::preProcess
MoFEMErrorCode preProcess()
function is run at the beginning of loop
Definition: dm_build_partitioned_mesh.cpp:16
CountDown::cOunter
int & cOunter
Definition: dm_build_partitioned_mesh.cpp:38
MoFEM::FieldEntity::getHandleFromUniqueId
static auto getHandleFromUniqueId(const UId uid)
Get the Handle From Unique Id.
Definition: FieldEntsMultiIndices.hpp:192
MoFEM::CoreInterface::add_field
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.
main
int main(int argc, char *argv[])
Definition: dm_build_partitioned_mesh.cpp:41
CountDown::preProcess
MoFEMErrorCode preProcess()
function is run at the beginning of loop
Definition: dm_build_partitioned_mesh.cpp:30
MoFEM::DMRegister_MoFEM
PetscErrorCode DMRegister_MoFEM(const char sname[])
Register MoFEM problem.
Definition: DMMoFEM.cpp:43
MoFEM::CoreInterface::get_field_ents
virtual const FieldEntity_multiIndex * get_field_ents() const =0
Get the field ents object.
CountDown::CountDown
CountDown(int &counter)
Definition: dm_build_partitioned_mesh.cpp:29
CountUp
Definition: dm_build_partitioned_mesh.cpp:13
MoFEM::DMMoFEMCreateMoFEM
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:114
mesh_file_name
char mesh_file_name[255]
Definition: mesh_smoothing.cpp:23
MoFEM::MatrixManager
Matrix manager is used to build and partition problems.
Definition: MatrixManager.hpp:21
MoFEM::CoreTmp< 0 >::Initialize
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
Definition: Core.cpp:72
MOFEM_LOG
#define MOFEM_LOG(channel, severity)
Log.
Definition: LogManager.hpp:308
CATCH_ERRORS
#define CATCH_ERRORS
Catch errors.
Definition: definitions.h:372
CountUp::CountUp
CountUp(int &counter)
Definition: dm_build_partitioned_mesh.cpp:15
MoFEM::Core
CoreTmp< 0 > Core
Definition: Core.hpp:1102
AINSWORTH_LEGENDRE_BASE
@ AINSWORTH_LEGENDRE_BASE
Ainsworth Cole (Legendre) approx. base .
Definition: definitions.h:60
MOFEM_DATA_INCONSISTENCY
@ MOFEM_DATA_INCONSISTENCY
Definition: definitions.h:31
MoFEM::PetscOptionsGetString
PetscErrorCode PetscOptionsGetString(PetscOptions *, const char pre[], const char name[], char str[], size_t size, PetscBool *set)
Definition: DeprecatedPetsc.hpp:172
MoFEM::CoreInterface::build_fields
virtual MoFEMErrorCode build_fields(int verb=DEFAULT_VERBOSITY)=0
CountUp::cOunter
int & cOunter
Definition: dm_build_partitioned_mesh.cpp:24
MoFEM::CoreInterface::modify_finite_element_add_field_data
virtual MoFEMErrorCode modify_finite_element_add_field_data(const std::string &fe_name, const std::string name_filed)=0
set finite element field data
m
FTensor::Index< 'm', 3 > m
Definition: shallow_wave.cpp:80
MoFEM::BitRefManager
Managing BitRefLevels.
Definition: BitRefManager.hpp:21
MoFEM::Types::BitRefLevel
std::bitset< BITREFLEVEL_SIZE > BitRefLevel
Bit structure attached to each entity identifying to what mesh entity is attached.
Definition: Types.hpp:40
MOFEM_ATOM_TEST_INVALID
@ MOFEM_ATOM_TEST_INVALID
Definition: definitions.h:40
MoFEM::CoreInterface::build_adjacencies
virtual MoFEMErrorCode build_adjacencies(const Range &ents, int verb=DEFAULT_VERBOSITY)=0
build adjacencies
MoFEM::CoreInterface::set_field_order
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.
MoFEM::SmartPetscObj< Mat >
MoFEM::DMoFEMLoopFiniteElements
PetscErrorCode DMoFEMLoopFiniteElements(DM dm, const char fe_name[], MoFEM::FEMethod *method, CacheTupleWeakPtr cache_ptr=CacheTupleSharedPtr())
Executes FEMethod for finite elements in DM.
Definition: DMMoFEM.cpp:586
CountDown
Definition: dm_build_partitioned_mesh.cpp:27
MoFEM::FieldEntity::getFieldBitNumberFromUniqueId
static auto getFieldBitNumberFromUniqueId(const UId uid)
Get the Field Bit Number From Unique Id.
Definition: FieldEntsMultiIndices.hpp:205
MoFEM::PetscOptionsGetInt
PetscErrorCode PetscOptionsGetInt(PetscOptions *, const char pre[], const char name[], PetscInt *ivalue, PetscBool *set)
Definition: DeprecatedPetsc.hpp:142
MoFEMFunctionReturn
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:416
CountDown::postProcess
MoFEMErrorCode postProcess()
function is run at the end of loop
Definition: dm_build_partitioned_mesh.cpp:35
MoFEMFunctionBegin
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:346
MoFEM::DMMoFEMSetIsPartitioned
PetscErrorCode DMMoFEMSetIsPartitioned(DM dm, PetscBool is_partitioned)
Definition: DMMoFEM.cpp:1123
MoFEM::PetscOptionsGetBool
PetscErrorCode PetscOptionsGetBool(PetscOptions *, const char pre[], const char name[], PetscBool *bval, PetscBool *set)
Definition: DeprecatedPetsc.hpp:182
MoFEM::FieldEntity::getOwnerFromUniqueId
static auto getOwnerFromUniqueId(const UId uid)
Definition: FieldEntsMultiIndices.hpp:177