Make a loop over entities

/** \file loop_entities.cpp
\example loop_entities.cpp
\brief Make a loop over entities
/* This file is part of MoFEM.
* MoFEM is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
* MoFEM is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with MoFEM. If not, see <http://www.gnu.org/licenses/>. */
#include <MoFEM.hpp>
using namespace MoFEM;
static char help[] = "...\n\n";
TestEntityMethod(const Range &ents)
: EntityMethod(), allEnts(ents) {}
MoFEMErrorCode preProcess() {
entsIt = allEnts.begin();
MoFEMErrorCode operator()() {
if (entPtr->getEnt() != *entsIt)
"Entity and entity iterator should be the same");
MoFEMErrorCode postProcess() {
if(entsIt != allEnts.end())
"All entities should be iterated");
const Range allEnts;
Range::const_iterator entsIt;
int main(int argc, char *argv[]) {
// initialize petsc
MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
try {
// read mesh and create moab and mofem data structures
moab::Core mb_instance;
moab::Interface &moab = mb_instance;
MoFEM::Core core(moab, PETSC_COMM_WORLD);
MoFEM::Interface &m_field = core;
// Register DM Manager
DMType dm_name = "DMMOFEM";
EntityHandle root_set = moab.get_root_set();
// Simple interface
Simple *simple_interface;
CHKERR m_field.getInterface(simple_interface);
// get options from command line
CHKERR simple_interface->getOptions();
// load mesh file
CHKERR simple_interface->loadFile();
// add fields
CHKERR simple_interface->addDomainField("FIELD1", H1,
// set fields order
CHKERR simple_interface->setFieldOrder("FIELD1", 4);
// setup problem
CHKERR simple_interface->setUp();
Range all_ents;
CHKERR m_field.get_moab().get_entities_by_handle(root_set, all_ents);
all_ents = subtract(all_ents, all_ents.subset_by_type(MBENTITYSET));
auto testingEntitiesInDatabase = [&]() {
TestEntityMethod method(all_ents);
CHKERR m_field.loop_entities("FIELD1", method);
CHKERR testingEntitiesInDatabase();
auto testingEntitiesInDatabaseSubRange = [&]() {
Range edges = all_ents.subset_by_type(MBEDGE);
TestEntityMethod method(edges);
CHKERR m_field.loop_entities("FIELD1", method, &edges);
CHKERR testingEntitiesInDatabaseSubRange();
auto dm = simple_interface->getDM();
auto testingEntitiesInDM = [&]() {
TestEntityMethod method(all_ents);
const MoFEM::Problem *prb_ptr;
CHKERR m_field.loop_entities(prb_ptr, "FIELD1", ROW, method, 0,
CHKERR testingEntitiesInDM();
// finish work cleaning memory, getting statistics, ect.
return 0;
Definition: definitions.h:192
Catch errors.
Definition: definitions.h:441
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
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.
keeps basic data about problem
MoFEMErrorCode getInterface(const MOFEMuuid &uuid, IFACE *&iface) const
Get interface by uuid and return reference to pointer of interface.