v0.10.0
dm_create_subdm.cpp
Go to the documentation of this file.
1 /** \file dm_create_subdm.cpp
2  \example dm_create_subdm.cpp
3  \brief Atom test for Data Manager Interface and create sub-problem
4 */
5 
6 /* This file is part of MoFEM.
7  * MoFEM is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * MoFEM is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15  * License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with MoFEM. If not, see <http://www.gnu.org/licenses/>. */
19 
20 #include <MoFEM.hpp>
21 
22 using namespace MoFEM;
23 
24 static char help[] = "...\n\n";
25 
26 static const bool debug = false;
27 
28 int main(int argc, char *argv[]) {
29 
30  // initialize petsc
31  MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
32 
33  try {
34 
35  PetscBool flg = PETSC_TRUE;
36  char mesh_file_name[255];
37 #if PETSC_VERSION_GE(3, 6, 4)
38  CHKERR PetscOptionsGetString(PETSC_NULL, "", "-my_file", mesh_file_name,
39  255, &flg);
40 #else
41  CHKERR PetscOptionsGetString(PETSC_NULL, PETSC_NULL, "-my_file",
42  mesh_file_name, 255, &flg);
43 #endif
44  if (flg != PETSC_TRUE) {
45  SETERRQ(PETSC_COMM_SELF, 1, "*** ERROR -my_file (MESH FILE NEEDED)");
46  }
47 
48  // register new dm type, i.e. mofem
49  DMType dm_name = "DMMOFEM";
50  CHKERR DMRegister_MoFEM(dm_name);
51  DMType dm_name_sub = "DMSUB";
52  CHKERR DMRegister_MoFEM(dm_name_sub);
53 
54  // craete dm instance
55  DM dm;
56  CHKERR DMCreate(PETSC_COMM_WORLD, &dm);
57  CHKERR DMSetType(dm, dm_name);
58 
59  DM subdm0, subdm1;
60  CHKERR DMCreate(PETSC_COMM_WORLD, &subdm0);
61  CHKERR DMSetType(subdm0, dm_name_sub);
62  CHKERR DMCreate(PETSC_COMM_WORLD, &subdm1);
63  CHKERR DMSetType(subdm1, dm_name_sub);
64 
65  // read mesh and create moab and mofem data structures
66  moab::Core mb_instance;
67  moab::Interface &moab = mb_instance;
68  ParallelComm *pcomm = ParallelComm::get_pcomm(&moab, MYPCOMM_INDEX);
69  if (pcomm == NULL)
70  pcomm = new ParallelComm(&moab, PETSC_COMM_WORLD);
71  const char *option;
72  option = "PARALLEL=BCAST_DELETE;PARALLEL_RESOLVE_SHARED_ENTS;PARTITION="
73  "PARALLEL_PARTITION;";
74  CHKERR moab.load_file(mesh_file_name, 0, option);
75 
76  MoFEM::Core core(moab);
77  MoFEM::Interface &m_field = core;
78 
79  EntityHandle root_set = moab.get_root_set();
80  // add all entities to database, all of them will be used
81  BitRefLevel bit_level0;
82  bit_level0.set(0);
83  CHKERR m_field.getInterface<BitRefManager>()->setBitRefLevelByDim(
84  root_set, 3, bit_level0);
85  // define & build field
86  const int field_rank = 1;
87  CHKERR m_field.add_field("FIELD0", H1, AINSWORTH_LEGENDRE_BASE, field_rank);
88  // add entities to field
89  CHKERR m_field.add_ents_to_field_by_type(root_set, MBTET, "FIELD0");
90  // set app. order
91  CHKERR m_field.set_field_order(root_set, MBVERTEX, "FIELD0", 1);
92  CHKERR m_field.add_field("FIELD1", H1, AINSWORTH_LEGENDRE_BASE, field_rank);
93  // add entities to field
94  CHKERR m_field.add_ents_to_field_by_type(root_set, MBTET, "FIELD1");
95  // set app. order
96  CHKERR m_field.set_field_order(root_set, MBVERTEX, "FIELD1", 1);
97  CHKERR m_field.set_field_order(root_set, MBEDGE, "FIELD1", 2);
98 
99  // build data structures for fields
100  CHKERR m_field.build_fields();
101 
102  // define & build finite elements
103  CHKERR m_field.add_finite_element("FE00");
104  // Define rows/cols and element data
105  CHKERR m_field.modify_finite_element_add_field_row("FE00", "FIELD0");
106  CHKERR m_field.modify_finite_element_add_field_col("FE00", "FIELD0");
107  CHKERR m_field.modify_finite_element_add_field_data("FE00", "FIELD0");
108  // add entities to finite element
109  CHKERR m_field.add_ents_to_finite_element_by_type(root_set, MBTET, "FE00");
110 
111  // define & build finite elements
112  CHKERR m_field.add_finite_element("FE11");
113  // Define rows/cols and element data
114  CHKERR m_field.modify_finite_element_add_field_row("FE11", "FIELD1");
115  CHKERR m_field.modify_finite_element_add_field_col("FE11", "FIELD1");
116  CHKERR m_field.modify_finite_element_add_field_data("FE11", "FIELD1");
117  // add entities to finite element
118  CHKERR m_field.add_ents_to_finite_element_by_type(root_set, MBTET, "FE11");
119 
120  // define & build finite elements
121  CHKERR m_field.add_finite_element("FE01");
122  // Define rows/cols and element data
123  CHKERR m_field.modify_finite_element_add_field_row("FE01", "FIELD0");
124  CHKERR m_field.modify_finite_element_add_field_col("FE01", "FIELD1");
125  CHKERR m_field.modify_finite_element_add_field_data("FE01", "FIELD0");
126  CHKERR m_field.modify_finite_element_add_field_data("FE01", "FIELD1");
127  // add entities to finite element
128  CHKERR m_field.add_ents_to_finite_element_by_type(root_set, MBTET, "FE01");
129 
130  // build finite elemnts
131  CHKERR m_field.build_finite_elements();
132  // build adjacencies
133  CHKERR m_field.build_adjacencies(bit_level0);
134 
135  // set dm data structure which created mofem data structures
136  CHKERR DMMoFEMCreateMoFEM(dm, &m_field, "MAIN_PROBLEM", bit_level0);
137  CHKERR DMSetFromOptions(dm);
138  CHKERR DMMoFEMAddElement(dm, "FE00");
139  CHKERR DMMoFEMAddElement(dm, "FE11");
140  CHKERR DMMoFEMAddElement(dm, "FE01");
141  CHKERR DMSetUp(dm);
143  ->checkMPIAIJWithArraysMatrixFillIn<PetscGlobalIdx_mi_tag>(
144  "MAIN_PROBLEM", -1, -1, 1);
145 
146  int nf;
147  char **field_names;
148  IS *fields;
149  CHKERR DMCreateFieldIS(dm, &nf, &field_names, &fields);
150  for (int f = 0; f != nf; f++) {
151  PetscPrintf(PETSC_COMM_WORLD, "%d field %s\n", f, field_names[f]);
152  CHKERR PetscFree(field_names[f]);
153  CHKERR ISDestroy(&(fields[f]));
154  }
155  CHKERR PetscFree(field_names);
156  CHKERR PetscFree(fields);
157 
158  CHKERR DMMoFEMCreateSubDM(subdm0, dm, "SUB0");
159  CHKERR DMMoFEMSetSquareProblem(subdm0, PETSC_TRUE);
160  CHKERR DMMoFEMAddElement(subdm0, "FE11");
161  CHKERR DMMoFEMAddSubFieldRow(subdm0, "FIELD1", MBVERTEX, MBVERTEX);
162  CHKERR DMMoFEMAddSubFieldCol(subdm0, "FIELD1", MBVERTEX, MBVERTEX);
163  CHKERR DMSetUp(subdm0);
165  ->checkMPIAIJWithArraysMatrixFillIn<PetscGlobalIdx_mi_tag>("SUB0", -1,
166  -1, 1);
167  if (debug) {
168  Mat A;
169  CHKERR DMCreateMatrix(subdm0, &A);
170  MatView(A, PETSC_VIEWER_DRAW_WORLD);
171  std::string wait;
172  std::cin >> wait;
173  CHKERR MatDestroy(&A);
174  }
175 
176  CHKERR DMMoFEMCreateSubDM(subdm1, dm, "SUB1");
177  CHKERR DMMoFEMSetSquareProblem(subdm1, PETSC_FALSE);
178  CHKERR DMMoFEMAddElement(subdm1, "FE01");
179  CHKERR DMMoFEMAddSubFieldRow(subdm1, "FIELD0");
180  CHKERR DMMoFEMAddSubFieldCol(subdm1, "FIELD1");
181  CHKERR DMSetUp(subdm1);
183  ->checkMPIAIJWithArraysMatrixFillIn<PetscGlobalIdx_mi_tag>("SUB1", -1,
184  -1, 1);
185 
186  if (debug) {
187  Mat B;
188  CHKERR DMCreateMatrix(subdm1, &B);
189  MatView(B, PETSC_VIEWER_DRAW_WORLD);
190  std::string wait;
191  std::cin >> wait;
192  CHKERR MatDestroy(&B);
193  }
194 
195  // destry dm
196  CHKERR DMDestroy(&dm);
197  CHKERR DMDestroy(&subdm0);
198  CHKERR DMDestroy(&subdm1);
199  }
200  CATCH_ERRORS;
201 
202  // finish work cleaning memory, getting statistics, ect.
204 
205  return 0;
206 }
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
Deprecated interface functions.
virtual MoFEMErrorCode build_adjacencies(const Range &ents, int verb=DEFAULT_VERBOSITY)=0
build adjacencies
CoreTmp< 0 > Core
Definition: Core.hpp:1129
PetscErrorCode DMMoFEMSetSquareProblem(DM dm, PetscBool square_problem)
set squared problemIt if true is assumed that matrix has the same indexing on rows and columns....
Definition: DMMMoFEM.cpp:378
PetscErrorCode DMMoFEMAddSubFieldCol(DM dm, const char field_name[], EntityType lo_type=MBVERTEX, EntityType hi_type=MBMAXTYPE)
Definition: DMMMoFEM.cpp:221
PetscErrorCode DMMoFEMAddElement(DM dm, const char fe_name[])
add element to dm
Definition: DMMMoFEM.cpp:425
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
Definition: Core.cpp:60
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.
int main(int argc, char *argv[])
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.
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:21
char mesh_file_name[255]
PetscErrorCode DMMoFEMCreateSubDM(DM subdm, DM dm, const char problem_name[])
Must be called by user to set Sub DM MoFEM data structures.
Definition: DMMMoFEM.cpp:177
PetscErrorCode DMMoFEMAddSubFieldRow(DM dm, const char field_name[], EntityType lo_type=MBVERTEX, EntityType hi_type=MBMAXTYPE)
Definition: DMMMoFEM.cpp:198
PetscErrorCode PetscOptionsGetString(PetscOptions *, const char pre[], const char name[], char str[], size_t size, PetscBool *set)
MoFEMErrorCode getInterface(const MOFEMuuid &uuid, IFACE *&iface) const
Get interface by uuid and return reference to pointer of interface.
static char help[]
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.
static const bool debug
Managing BitRefLevels.
PetscErrorCode DMRegister_MoFEM(const char sname[])
Register MoFEM problem.
Definition: DMMMoFEM.cpp:48
Ainsworth Cole (Legendre) approx. base .
Definition: definitions.h:152
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: DMMMoFEM.cpp:105
virtual MoFEMErrorCode add_finite_element(const std::string &fe_name, enum MoFEMTypes bh=MF_EXCL, int verb=DEFAULT_VERBOSITY)=0
add finite element
#define CHKERR
Inline error check.
Definition: definitions.h:604
virtual MoFEMErrorCode modify_finite_element_add_field_data(const std::string &fe_name, const std::string &name_filed)=0
set finite element field data
#define MYPCOMM_INDEX
default communicator number PCOMM
Definition: definitions.h:292
std::bitset< BITREFLEVEL_SIZE > BitRefLevel
Bit structure attached to each entity identifying to what mesh entity is attached.
Definition: Types.hpp:51
virtual MoFEMErrorCode build_fields(int verb=DEFAULT_VERBOSITY)=0
DeprecatedCoreInterface Interface
Definition: Interface.hpp:1943
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
Core (interface) class.
Definition: Core.hpp:77
#define CATCH_ERRORS
Catch errors.
Definition: definitions.h:441
continuous field
Definition: definitions.h:177
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition: Core.cpp:100
virtual MoFEMErrorCode build_finite_elements(int verb=DEFAULT_VERBOSITY)=0
Build finite elementsBuild finite element data structures. Have to be run before problem and adjacenc...
Matrix manager is used to build and partition problems.