v0.10.0
loop_entities.cpp
Go to the documentation of this file.
1 /** \file loop_entities.cpp
2  \example loop_entities.cpp
3  \brief Make a loop over entities
4 
5 */
6 
7 /* This file is part of MoFEM.
8  * MoFEM is free software: you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * MoFEM is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  * License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with MoFEM. If not, see <http://www.gnu.org/licenses/>. */
20 
21 #include <MoFEM.hpp>
22 
23 using namespace MoFEM;
24 
25 static char help[] = "...\n\n";
26 
28  TestEntityMethod(const Range &ents)
29  : EntityMethod(), allEnts(ents) {}
30 
33  entsIt = allEnts.begin();
35  }
36 
39  if (entPtr->getEnt() != *entsIt)
40  SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
41  "Entity and entity iterator should be the same");
42  ++entsIt;
44  }
45 
48  if(entsIt != allEnts.end())
49  SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
50  "All entities should be iterated");
52  }
53 
54 private:
55  const Range allEnts;
56  Range::const_iterator entsIt;
57 };
58 
59 int main(int argc, char *argv[]) {
60  // initialize petsc
61  MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
62 
63  try {
64 
65  // read mesh and create moab and mofem data structures
66  moab::Core mb_instance;
67  moab::Interface &moab = mb_instance;
68 
69  MoFEM::Core core(moab, PETSC_COMM_WORLD);
70  MoFEM::Interface &m_field = core;
71 
72  // Register DM Manager
73  DMType dm_name = "DMMOFEM";
74  CHKERR DMRegister_MoFEM(dm_name);
75 
76  EntityHandle root_set = moab.get_root_set();
77 
78  // Simple interface
79  Simple *simple_interface;
80  CHKERR m_field.getInterface(simple_interface);
81  {
82  // get options from command line
83  CHKERR simple_interface->getOptions();
84  // load mesh file
85  CHKERR simple_interface->loadFile();
86  // add fields
87  CHKERR simple_interface->addDomainField("FIELD1", H1,
89  // set fields order
90  CHKERR simple_interface->setFieldOrder("FIELD1", 4);
91  // setup problem
92  CHKERR simple_interface->setUp();
93 
94  Range all_ents;
95  CHKERR m_field.get_moab().get_entities_by_handle(root_set, all_ents);
96  all_ents = subtract(all_ents, all_ents.subset_by_type(MBENTITYSET));
97 
98  auto testingEntitiesInDatabase = [&]() {
100  TestEntityMethod method(all_ents);
101  CHKERR m_field.loop_entities("FIELD1", method);
103  };
104 
105  CHKERR testingEntitiesInDatabase();
106 
107  auto testingEntitiesInDatabaseSubRange = [&]() {
109  Range edges = all_ents.subset_by_type(MBEDGE);
110  TestEntityMethod method(edges);
111  CHKERR m_field.loop_entities("FIELD1", method, &edges);
113  };
114 
115  CHKERR testingEntitiesInDatabaseSubRange();
116 
117  auto dm = simple_interface->getDM();
118 
119  auto testingEntitiesInDM = [&]() {
121  TestEntityMethod method(all_ents);
122  const MoFEM::Problem *prb_ptr;
123  CHKERR DMMoFEMGetProblemPtr(dm, &prb_ptr);
124  CHKERR m_field.loop_entities(prb_ptr, "FIELD1", ROW, method, 0,
125  m_field.get_comm_size());
127  };
128 
129  CHKERR testingEntitiesInDM();
130 
131  }
132  }
133  CATCH_ERRORS;
134 
135  // finish work cleaning memory, getting statistics, ect.
137 
138  return 0;
139 }
@ ROW
Definition: definitions.h:192
#define CATCH_ERRORS
Catch errors.
Definition: definitions.h:441
@ AINSWORTH_LEGENDRE_BASE
Ainsworth Cole (Legendre) approx. base .
Definition: definitions.h:152
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:516
@ H1
continuous field
Definition: definitions.h:177
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:415
@ MOFEM_ATOM_TEST_INVALID
Definition: definitions.h:132
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:485
#define CHKERR
Inline error check.
Definition: definitions.h:604
#define MoFEMFunctionBeginHot
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:509
PetscErrorCode DMMoFEMGetProblemPtr(DM dm, const MoFEM::Problem **problem_ptr)
Get pointer to problem data structure.
Definition: DMMMoFEM.cpp:348
PetscErrorCode DMRegister_MoFEM(const char sname[])
Register MoFEM problem.
Definition: DMMMoFEM.cpp:48
virtual MoFEMErrorCode loop_entities(const std::string field_name, EntityMethod &method, Range const *const ents=nullptr, int verb=DEFAULT_VERBOSITY)=0
Loop over field entities.
int main(int argc, char *argv[])
static char help[]
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:67
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:21
CoreTmp< 0 > Core
Definition: Core.hpp:1129
DeprecatedCoreInterface Interface
Definition: Interface.hpp:1943
virtual int get_comm_size() const =0
virtual moab::Interface & get_moab()=0
Core (interface) class.
Definition: Core.hpp:77
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
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition: Core.cpp:100
Deprecated interface functions.
Data structure to exchange data between mofem and User Loop Methods on entities.
keeps basic data about problem
Simple interface for fast problem set-up.
Definition: Simple.hpp:36
MoFEMErrorCode addDomainField(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_ZERO, int verb=-1)
Add field on domain.
Definition: Simple.cpp:269
MoFEMErrorCode getOptions()
get options
Definition: Simple.cpp:198
MoFEMErrorCode getDM(DM *dm)
Get DM.
Definition: Simple.cpp:707
MoFEMErrorCode loadFile(const std::string options, const std::string mesh_file_name)
Load mesh file.
Definition: Simple.cpp:212
MoFEMErrorCode setFieldOrder(const std::string field_name, const int order, const Range *ents=NULL)
Set field order.
Definition: Simple.cpp:482
MoFEMErrorCode setUp(const PetscBool is_partitioned=PETSC_TRUE)
Setup problem.
Definition: Simple.cpp:673
MoFEMErrorCode getInterface(const MOFEMuuid &uuid, IFACE *&iface) const
Get interface by uuid and return reference to pointer of interface.
MoFEMErrorCode operator()()
function is run for every finite element
TestEntityMethod(const Range &ents)
MoFEMErrorCode preProcess()
function is run at the beginning of loop
MoFEMErrorCode postProcess()
function is run at the end of loop
Range::const_iterator entsIt
const Range allEnts