v0.14.0
LoopMethods.cpp
Go to the documentation of this file.
1 /** \file LoopMethods.cpp
2 \brief methods for managing loops
3 */
4 
5 
6 #include <MoFEM.hpp>
7 
8 namespace MoFEM {
9 
18 
19 // PetscData
21 PetscData::query_interface(boost::typeindex::type_index type_index,
22  UnknownInterface **iface) const {
23  *iface = const_cast<PetscData *>(this);
24  return 0;
25 }
26 
28  : data_ctx(PetscData::CtxSetNone), f(PETSC_NULL), A(PETSC_NULL),
29  B(PETSC_NULL), x(PETSC_NULL), x_t(PETSC_NULL), x_tt(PETSC_NULL) {}
30 
32  this->data_ctx = petsc_data.data_ctx;
33  this->f = petsc_data.f;
34  this->A = petsc_data.A;
35  this->B = petsc_data.B;
36  this->x = petsc_data.x;
37  this->x = petsc_data.x;
38  this->x_t = petsc_data.x_t;
39  this->x_tt = petsc_data.x_tt;
40  return 0;
41 }
42 
43 // KSP
45 KspMethod::query_interface(boost::typeindex::type_index type_index,
46  UnknownInterface **iface) const {
47  *iface = const_cast<KspMethod *>(this);
48  return 0;
49 };
50 
52  : ksp_ctx(CTX_KSPNONE), ksp(PETSC_NULL), ksp_f(PetscData::f),
53  ksp_A(PetscData::A), ksp_B(PetscData::B) {}
54 
58  this->ksp_ctx = ksp.ksp_ctx;
59  this->ksp = ksp.ksp;
61 }
62 
63 // SNES
65 SnesMethod::query_interface(boost::typeindex::type_index type_index,
66  UnknownInterface **iface) const {
67  *iface = const_cast<SnesMethod *>(this);
68  return 0;
69 }
70 
72  : snes_ctx(CTX_SNESNONE), snes_x(PetscData::x), snes_f(PetscData::f),
73  snes_A(PetscData::A), snes_B(PetscData::B) {}
74 
78  this->snes_ctx = snes.snes_ctx;
79  this->snes = snes.snes;
81 }
82 
83 // TS
85 TSMethod::query_interface(boost::typeindex::type_index type_index,
86  UnknownInterface **iface) const {
87  *iface = const_cast<TSMethod *>(this);
88  return 0;
89 }
90 
92  : ts_ctx(CTX_TSNONE), ts_step(-1), ts_a(0), ts_t(0), ts_u(PetscData::x),
93  ts_u_t(PetscData::x_t), ts_u_tt(PetscData::x_tt), ts_F(PetscData::f),
94  ts_A(PetscData::A), ts_B(PetscData::B) {}
95 
99  this->ts_ctx = ts.ts_ctx;
100  this->ts = ts.ts;
101  this->ts_step = ts.ts_step;
102  this->ts_a = ts.ts_a;
103  this->ts_t = ts.ts_t;
104  this->ts_dt = ts.ts_dt;
106 }
107 
108 // BasicMethod
110  : PetscData(), KspMethod(), SnesMethod(), TSMethod(), nInTheLoop(0),
111  loopSize(0), rAnk(-1), sIze(-1), refinedEntitiesPtr(nullptr),
112  refinedFiniteElementsPtr(nullptr), problemPtr(nullptr),
113  fieldsPtr(nullptr), entitiesPtr(nullptr), dofsPtr(nullptr),
114  finiteElementsPtr(nullptr), finiteElementsEntitiesPtr(nullptr),
115  adjacenciesPtr(nullptr) {}
116 
119 
120  this->nInTheLoop = basic.nInTheLoop;
121  this->loopSize = basic.loopSize;
122  this->rAnk = basic.rAnk;
123  this->sIze = basic.sIze;
126  this->problemPtr = basic.problemPtr;
127  this->fieldsPtr = basic.fieldsPtr;
128  this->entitiesPtr = basic.entitiesPtr;
129  this->dofsPtr = basic.dofsPtr;
130  this->finiteElementsPtr = basic.finiteElementsPtr;
132  this->adjacenciesPtr = basic.adjacenciesPtr;
133  this->cacheWeakPtr = basic.cacheWeakPtr;
134 
136 }
137 
140  if (preProcessHook) {
141  ierr = preProcessHook();
142  CHKERRG(ierr);
143  } else {
144  SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL,
145  "should be implemented by user in derived class (preProcess)");
146  }
148 }
151  if (postProcessHook) {
152  ierr = postProcessHook();
153  CHKERRG(ierr);
154  } else {
155  SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL,
156  "should be implemented by user in derived class (postProcess)");
157  }
159 }
162  if (operatorHook) {
163  ierr = operatorHook();
164  CHKERRG(ierr);
165  } else {
166  SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL,
167  "should be implemented by user in derived class (operator)");
168  }
170 }
171 
173  VectorDouble &data,
174  const bool reset_dofs) {
176 
177  // TODO: [CORE-60] Fix implementation not to use DOFs
178 
179  auto get_nodes_field_data =
180  [&](boost::shared_ptr<FEDofEntity_multiIndex> &&dofs,
181  VectorDouble &nodes_data) {
183 
184  auto bit_number = getFieldBitNumber(field_name);
185  auto &dofs_by_uid = dofs->get<Unique_mi_tag>();
186  auto dit =
187  dofs_by_uid.lower_bound(FieldEntity::getLocalUniqueIdCalculate(
188  bit_number, get_id_for_min_type<MBVERTEX>()));
189  auto hi_dit =
190  dofs_by_uid.upper_bound(FieldEntity::getLocalUniqueIdCalculate(
191  bit_number, get_id_for_max_type<MBVERTEX>()));
192 
193  if (dit != hi_dit) {
194  auto &first_dof = **dit;
195  const int num_nodes = getNumberOfNodes();
196  const int nb_dof_idx = first_dof.getNbOfCoeffs();
197  const int max_nb_dofs = nb_dof_idx * num_nodes;
198  nodes_data.resize(max_nb_dofs, false);
199  nodes_data.clear();
200 
201  for (; dit != hi_dit;) {
202  const auto &dof_ptr = *dit;
203  const auto &dof = *dof_ptr;
204  const auto &sn = *dof.getSideNumberPtr();
205  const int side_number = sn.side_number;
206 
207  int pos = side_number * nb_dof_idx;
208  auto ent_filed_data_vec = dof.getEntFieldData();
209  for (int ii = 0; ii != nb_dof_idx; ++ii) {
210  nodes_data[pos] = ent_filed_data_vec[ii];
211  ++pos;
212  ++dit;
213  }
214  }
215 
216  } else if (reset_dofs) {
217  nodes_data.resize(0, false);
218  }
219 
221  };
222 
223  return get_nodes_field_data(getDataDofsPtr(), data);
224 
226 }
227 
228 } // namespace MoFEM
MoFEMFunctionReturnHot
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:460
MoFEM::SnesMethod::query_interface
MoFEMErrorCode query_interface(boost::typeindex::type_index type_index, UnknownInterface **iface) const
Definition: LoopMethods.cpp:65
MoFEM::PetscData::Switches
std::bitset< 8 > Switches
Definition: LoopMethods.hpp:33
MoFEM::TSMethod::TSMethod
TSMethod()
Definition: LoopMethods.cpp:91
MoFEM::BasicMethod::refinedEntitiesPtr
const RefEntity_multiIndex * refinedEntitiesPtr
container of mofem dof entities
Definition: LoopMethods.hpp:245
MoFEM::TSMethod::ts_a
PetscReal ts_a
shift for U_t (see PETSc Time Solver)
Definition: LoopMethods.hpp:160
MoFEM::PetscData::x
Vec x
Definition: LoopMethods.hpp:51
MoFEM::BasicMethod::nInTheLoop
int nInTheLoop
number currently of processed method
Definition: LoopMethods.hpp:198
MoFEM::BasicMethod::finiteElementsEntitiesPtr
const EntFiniteElement_multiIndex * finiteElementsEntitiesPtr
Definition: LoopMethods.hpp:263
MoFEM::PetscData::CtxSetB
static constexpr Switches CtxSetB
Definition: LoopMethods.hpp:38
MoFEM::FEMethod::getDataDofsPtr
auto getDataDofsPtr() const
Definition: LoopMethods.hpp:404
MoFEM::PetscData::CtxSetA
static constexpr Switches CtxSetA
Definition: LoopMethods.hpp:37
MoFEM::PetscData::copyPetscData
MoFEMErrorCode copyPetscData(const PetscData &petsc_data)
Definition: LoopMethods.cpp:31
MoFEM::SnesMethod::snes_ctx
SNESContext snes_ctx
Definition: LoopMethods.hpp:118
MoFEM::Exceptions::MoFEMErrorCode
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:56
MoFEM::SnesMethod
data structure for snes (nonlinear solver) context
Definition: LoopMethods.hpp:102
MoFEM::PetscData::x_tt
Vec x_tt
Definition: LoopMethods.hpp:53
MoFEM::BasicMethod::BasicMethod
BasicMethod()
Definition: LoopMethods.cpp:109
MoFEM::PetscData::B
Mat B
Definition: LoopMethods.hpp:50
MoFEM.hpp
A
constexpr AssemblyType A
Definition: operators_tests.cpp:30
MoFEM::BasicMethod::preProcessHook
boost::function< MoFEMErrorCode()> preProcessHook
Hook function for pre-processing.
Definition: LoopMethods.hpp:294
MoFEM::PetscData
Definition: LoopMethods.hpp:13
ts_ctx
MoFEM::TsCtx * ts_ctx
Definition: level_set.cpp:1932
MoFEM::FEMethod::getNodeData
MoFEMErrorCode getNodeData(const std::string field_name, VectorDouble &data, const bool reset_dofs=true)
Definition: LoopMethods.cpp:172
MoFEM::BasicMethod::fieldsPtr
const Field_multiIndex * fieldsPtr
raw pointer to fields container
Definition: LoopMethods.hpp:252
MoFEM::TSMethod::query_interface
MoFEMErrorCode query_interface(boost::typeindex::type_index type_index, UnknownInterface **iface) const
Definition: LoopMethods.cpp:85
MoFEM::BasicMethod::adjacenciesPtr
const FieldEntityEntFiniteElementAdjacencyMap_multiIndex * adjacenciesPtr
Definition: LoopMethods.hpp:267
MoFEM::SnesMethod::snes
SNES snes
snes solver
Definition: LoopMethods.hpp:120
MoFEM::BasicMethod::sIze
int sIze
number of processors in communicator
Definition: LoopMethods.hpp:242
CHKERR
#define CHKERR
Inline error check.
Definition: definitions.h:548
MoFEM::BasicMethod::entitiesPtr
const FieldEntity_multiIndex * entitiesPtr
raw pointer to container of field entities
Definition: LoopMethods.hpp:255
MoFEM
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:10
MoFEM::KspMethod
data structure for ksp (linear solver) context
Definition: LoopMethods.hpp:64
MoFEM::PetscData::PetscData
PetscData()
Definition: LoopMethods.cpp:27
MoFEM::PetscData::data_ctx
Switches data_ctx
Definition: LoopMethods.hpp:44
MoFEM::FEMethod::getNumberOfNodes
auto getNumberOfNodes() const
Definition: LoopMethods.hpp:456
MoFEM::TSMethod::ts
TS ts
time solver
Definition: LoopMethods.hpp:155
MoFEM::FieldEntity::getLocalUniqueIdCalculate
UId getLocalUniqueIdCalculate()
Get the Local Unique Id Calculate object.
Definition: FieldEntsMultiIndices.hpp:136
MoFEM::BasicMethod::rAnk
int rAnk
processor rank
Definition: LoopMethods.hpp:240
MoFEM::SnesMethod::copySnes
MoFEMErrorCode copySnes(const SnesMethod &snes)
Copy snes data.
Definition: LoopMethods.cpp:75
MoFEM::PetscData::A
Mat A
Definition: LoopMethods.hpp:49
MoFEM::PetscData::CtxSetX_T
static constexpr Switches CtxSetX_T
Definition: LoopMethods.hpp:40
MoFEM::BasicMethod::preProcess
virtual MoFEMErrorCode preProcess()
function is run at the beginning of loop
Definition: LoopMethods.cpp:138
MoFEM::BasicMethod::postProcessHook
boost::function< MoFEMErrorCode()> postProcessHook
Hook function for post-processing.
Definition: LoopMethods.hpp:299
MoFEM::KspMethod::KspMethod
KspMethod()
Definition: LoopMethods.cpp:51
MoFEM::BasicMethod::refinedFiniteElementsPtr
const RefElement_multiIndex * refinedFiniteElementsPtr
container of mofem finite element entities
Definition: LoopMethods.hpp:248
MoFEM::TSMethod::ts_ctx
TSContext ts_ctx
Definition: LoopMethods.hpp:157
MoFEM::PetscData::CtxSetTime
static constexpr Switches CtxSetTime
Definition: LoopMethods.hpp:42
MOFEM_OPERATION_UNSUCCESSFUL
@ MOFEM_OPERATION_UNSUCCESSFUL
Definition: definitions.h:34
MoFEM::PetscData::CtxSetNone
static constexpr Switches CtxSetNone
Definition: LoopMethods.hpp:35
MoFEM::BasicMethod
Data structure to exchange data between mofem and User Loop Methods.
Definition: LoopMethods.hpp:183
MoFEM::BasicMethod::loopSize
int loopSize
local number oe methods to process
Definition: LoopMethods.hpp:203
MoFEM::KspMethod::ksp
KSP ksp
KSP solver.
Definition: LoopMethods.hpp:87
field_name
constexpr auto field_name
Definition: poisson_2d_homogeneous.cpp:13
MoFEM::PetscData::f
Vec f
Definition: LoopMethods.hpp:48
MoFEM::PetscData::CtxSetX_TT
static constexpr Switches CtxSetX_TT
Definition: LoopMethods.hpp:41
MoFEM::BasicMethod::copyBasicMethod
MoFEMErrorCode copyBasicMethod(const BasicMethod &basic)
Copy data from other base method to this base method.
Definition: LoopMethods.cpp:117
MoFEM::PetscData::CtxSetX
static constexpr Switches CtxSetX
Definition: LoopMethods.hpp:39
MoFEM::TSMethod::ts_step
PetscInt ts_step
time step number
Definition: LoopMethods.hpp:159
MoFEM::UnknownInterface
base class for all interface classes
Definition: UnknownInterface.hpp:34
MoFEM::BasicMethod::finiteElementsPtr
const FiniteElement_multiIndex * finiteElementsPtr
raw pointer to container finite elements
Definition: LoopMethods.hpp:260
MoFEM::BasicMethod::operatorHook
boost::function< MoFEMErrorCode()> operatorHook
Hook function for operator.
Definition: LoopMethods.hpp:304
MoFEM::TSMethod::ts_t
PetscReal ts_t
time
Definition: LoopMethods.hpp:162
MoFEM::BasicMethod::getFieldBitNumber
unsigned int getFieldBitNumber(std::string field_name) const
Definition: LoopMethods.hpp:270
MoFEM::BasicMethod::dofsPtr
const DofEntity_multiIndex * dofsPtr
raw pointer container of dofs
Definition: LoopMethods.hpp:257
HenckyOps::f
auto f
Definition: HenckyOps.hpp:15
MoFEM::SnesMethod::SnesMethod
SnesMethod()
Definition: LoopMethods.cpp:71
MoFEM::TSMethod::copyTs
MoFEMErrorCode copyTs(const TSMethod &ts)
Copy TS solver data.
Definition: LoopMethods.cpp:96
MoFEM::TSMethod
data structure for TS (time stepping) context
Definition: LoopMethods.hpp:134
UBlasVector< double >
MoFEM::Exceptions::ierr
static MoFEMErrorCodeGeneric< PetscErrorCode > ierr
Definition: Exceptions.hpp:76
MoFEM::BasicMethod::cacheWeakPtr
boost::weak_ptr< CacheTuple > cacheWeakPtr
Definition: LoopMethods.hpp:351
MoFEM::KspMethod::copyKsp
MoFEMErrorCode copyKsp(const KspMethod &ksp)
copy data form another method
Definition: LoopMethods.cpp:55
MoFEM::BasicMethod::problemPtr
const Problem * problemPtr
raw pointer to problem
Definition: LoopMethods.hpp:250
MoFEM::PetscData::x_t
Vec x_t
Definition: LoopMethods.hpp:52
MoFEM::PetscData::query_interface
MoFEMErrorCode query_interface(boost::typeindex::type_index type_index, UnknownInterface **iface) const
Definition: LoopMethods.cpp:21
MoFEM::BasicMethod::postProcess
virtual MoFEMErrorCode postProcess()
function is run at the end of loop
Definition: LoopMethods.cpp:149
MoFEM::PetscData::CtxSetF
static constexpr Switches CtxSetF
Definition: LoopMethods.hpp:36
MoFEMFunctionBeginHot
#define MoFEMFunctionBeginHot
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:453
MoFEM::Unique_mi_tag
Definition: TagMultiIndices.hpp:18
MoFEM::TSMethod::ts_dt
PetscReal ts_dt
time step size
Definition: LoopMethods.hpp:163
MoFEM::KspMethod::query_interface
MoFEMErrorCode query_interface(boost::typeindex::type_index type_index, UnknownInterface **iface) const
Definition: LoopMethods.cpp:45
MoFEMFunctionReturn
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:429
CHKERRG
#define CHKERRG(n)
Check error code of MoFEM/MOAB/PETSc function.
Definition: definitions.h:496
MoFEMFunctionBegin
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:359
MoFEM::BasicMethod::operator()
virtual MoFEMErrorCode operator()()
function is run for every finite element
Definition: LoopMethods.cpp:160
MoFEM::KspMethod::ksp_ctx
KSPContext ksp_ctx
Context.
Definition: LoopMethods.hpp:86