v0.14.0
MoFEMManager.hpp
Go to the documentation of this file.
1 /* This file is part of MoFEM.
2  * MoFEM is free software: you can redistribute it and/or modify it under
3  * the terms of the GNU Lesser General Public License as published by the
4  * Free Software Foundation, either version 3 of the License, or (at your
5  * option) any later version.
6  *
7  * MoFEM is distributed in the hope that it will be useful, but WITHOUT
8  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  * License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with MoFEM. If not, see <http://www.gnu.org/licenses/>. */
14 
15 namespace MoFEMManager {
16 
17 struct MFManager {
18 
19  int order;
21  int atomTest;
22  PetscBool isQuasiStatic;
23  PetscBool isExplicit;
24  PetscBool isPartitioned;
25  PetscBool doPrintMaxMin;
26 
28 
30  SmartPetscObj<TS> tSolver;
31  SmartPetscObj<DM> dM;
32 
33  boost::shared_ptr<std::vector<unsigned char>> boundaryMarker;
34 
35  // FIXME: should we have a global (static) post-processing on volume and skin,
36  // probably yes
37  // boost::shared_ptr<PostProcEle> postProc;
38  // boost::shared_ptr<PostProcSkinEle> postProcSkin;
39 
40  std::tuple<SmartPetscObj<Vec>, SmartPetscObj<VecScatter>> vScatter[3];
41  boost::shared_ptr<FEMethod> monitorPtr;
42 
43  MFManager(MoFEM::Interface &m_field) : mField(m_field) {
44  monitorPtr = boost::make_shared<FEMethod>();
45  saveEveryNthStep = 1;
46  order = 1;
47  isPartitioned = PETSC_FALSE;
48  atomTest = -1;
49  doPrintMaxMin = PETSC_TRUE;
50  }
51 
57 
59  printMaxMin(std::tuple<SmartPetscObj<Vec>, SmartPetscObj<VecScatter>> &tuple,
60  const std::string msg, Vec &ts_u, double time);
61 
62  auto getTSType();
63 };
64 
66  auto t_type = GenericElementInterface::IM2;
67  if (isQuasiStatic)
69  if (isExplicit)
71  return t_type;
72 }
73 
76  isQuasiStatic = PETSC_TRUE;
77  isExplicit = PETSC_FALSE;
78  CHKERR PetscOptionsGetBool(PETSC_NULL, "-is_quasi_static", &isQuasiStatic,
79  PETSC_NULL);
80  CHKERR PetscOptionsGetBool(PETSC_NULL, "-is_explicit", &isExplicit,
81  PETSC_NULL);
82  CHKERR PetscOptionsGetInt(PETSC_NULL, "-order", &order, PETSC_NULL);
83  CHKERR PetscOptionsGetInt(PETSC_NULL, "-output_every", &saveEveryNthStep,
84  PETSC_NULL);
85  CHKERR PetscOptionsGetInt(PETSC_NULL, "-atom_test", &atomTest, PETSC_NULL);
86 
87  CHKERR PetscOptionsGetBool(PETSC_NULL, "-print_max_min", &doPrintMaxMin,
88  PETSC_NULL);
89  CHKERR PetscOptionsGetBool(PETSC_NULL, "-is_partitioned", &isPartitioned,
90  PETSC_NULL);
91  MOFEM_LOG("WORLD", Sev::inform)
92  << "Mesh Partition Flag Status: " << isPartitioned;
93 
95 }
96 
97 
100  // MoFEMFunctionReturnHot(0);
101 
102  // Select base
103  enum bases { AINSWORTH, DEMKOWICZ, BERNSTEIN, LASBASETOPT };
104  const char *list_bases[] = {"ainsworth", "demkowicz", "bernstein"};
105  PetscInt choice_base_value = AINSWORTH;
106  CHKERR PetscOptionsGetEList(PETSC_NULL, NULL, "-base", list_bases,
107  LASBASETOPT, &choice_base_value, PETSC_NULL);
108 
110  switch (choice_base_value) {
111  case AINSWORTH:
113  MOFEM_LOG("WORLD", Sev::inform)
114  << "Set AINSWORTH_LEGENDRE_BASE for displacements";
115  break;
116  case DEMKOWICZ:
117  base = DEMKOWICZ_JACOBI_BASE;
118  MOFEM_LOG("WORLD", Sev::inform)
119  << "Set DEMKOWICZ_JACOBI_BASE for displacements";
120  break;
121  case BERNSTEIN:
123  MOFEM_LOG("WORLD", Sev::inform)
124  << "Set AINSWORTH_BERNSTEIN_BEZIER_BASE for displacements";
125  break;
126  default:
127  base = LASTBASE;
128  break;
129  }
130 
131  // Add displacement field
132  CHKERR mField.add_field("U", H1, base, 3);
133 
134  // Add field representing ho-geometry
135  CHKERR mField.add_field("MESH_NODE_POSITIONS", H1, AINSWORTH_LEGENDRE_BASE,
136  3);
137 
138  // Add entities to field
139  CHKERR mField.add_ents_to_field_by_type(0, MBTET, "U");
140  CHKERR mField.add_ents_to_field_by_type(0, MBTET, "MESH_NODE_POSITIONS");
141 
142  // Set approximation order to entities
144  CHKERR mField.set_field_order(0, MBVERTEX, "U", order);
145  else
146  CHKERR mField.set_field_order(0, MBVERTEX, "U", 1);
147  CHKERR mField.set_field_order(0, MBEDGE, "U", order);
148  CHKERR mField.set_field_order(0, MBTRI, "U", order);
149  CHKERR mField.set_field_order(0, MBTET, "U", order);
150 
151  CHKERR mField.set_field_order(0, MBVERTEX, "MESH_NODE_POSITIONS", 1);
152  // CHKERR mField.set_field_order(0, MBEDGE, "MESH_NODE_POSITIONS", 2);
153  CHKERR mField.set_field_order(0, MBEDGE, "MESH_NODE_POSITIONS", 1);
154  CHKERR mField.set_field_order(0, MBTRI, "MESH_NODE_POSITIONS", 1);
155  CHKERR mField.set_field_order(0, MBTET, "MESH_NODE_POSITIONS", 1);
156 
158 }
159 
162 
163  auto simple = mField.getInterface<Simple>();
164  DMType dm_name = "DMMOFEM";
165  // Register DM problem
166  CHKERR DMRegister_MoFEM(dm_name);
167  dM = createSmartDM(mField.get_comm(), dm_name);
168  CHKERR DMSetType(dM, dm_name);
170  // Create DM instance
171  CHKERR DMMoFEMCreateMoFEM(dM, &mField, simple->getProblemName().c_str(),
172  simple->getBitRefLevel());
173  CHKERR DMSetFromOptions(dM);
174 
175  // mField.getInterface<ProblemsManager>()->buildProblemFromFields = PETSC_TRUE;
176 
178 }
179 
180 
183 
184  Simple *simple = mField.getInterface<Simple>();
185  PipelineManager *pipeline_mng = mField.getInterface<PipelineManager>();
186  ISManager *is_manager = mField.getInterface<ISManager>();
187 
188  auto set_time_monitor = [&](auto solver) {
190  boost::shared_ptr<ForcesAndSourcesCore> null;
191  CHKERR DMMoFEMTSSetMonitor(dM, solver, simple->getDomainFEName(),
192  monitorPtr, null, null);
194  };
195 
196  auto set_section_monitor = [&](auto solver) {
198  SNES snes;
199  CHKERR TSGetSNES(solver, &snes);
200  PetscViewerAndFormat *vf;
201  CHKERR PetscViewerAndFormatCreate(PETSC_VIEWER_STDOUT_WORLD,
202  PETSC_VIEWER_DEFAULT, &vf);
203  CHKERR SNESMonitorSet(
204  snes,
205  (MoFEMErrorCode(*)(SNES, PetscInt, PetscReal, void *))SNESMonitorFields,
206  vf, (MoFEMErrorCode(*)(void **))PetscViewerAndFormatDestroy);
208  };
209 
210 
211  auto set_dm_section = [&](auto dm) {
213  auto section = mField.getInterface<ISManager>()->sectionCreate(
214  simple->getProblemName());
215  CHKERR DMSetSection(dm, section);
217  };
218 
219  auto scatter_create = [&](auto coeff, auto D) {
220  SmartPetscObj<IS> is;
221  CHKERR is_manager->isCreateProblemFieldAndRank(simple->getProblemName(),
222  ROW, "U", coeff, coeff, is);
223  int loc_size;
224  CHKERR ISGetLocalSize(is, &loc_size);
225  Vec v;
226  CHKERR VecCreateMPI(mField.get_comm(), loc_size, PETSC_DETERMINE, &v);
227  VecScatter scatter;
228  CHKERR VecScatterCreate(D, is, v, PETSC_NULL, &scatter);
229  return std::make_tuple(SmartPetscObj<Vec>(v),
230  SmartPetscObj<VecScatter>(scatter));
231  };
232 
233  CHKERR set_dm_section(dM);
234 
235  auto D = smartCreateDMVector(dM);
237 
238  //FIXME: for explicit you need to implement proper mass matrix for rhs operators
239  if (isQuasiStatic || isExplicit) {
240  CHKERR TSSetSolution(tSolver, D);
241  } else {
242  CHKERR TSSetType(tSolver, TSALPHA2);
243  auto DD = smartVectorDuplicate(D);
244  CHKERR TS2SetSolution(tSolver, D, DD);
245  }
246  CHKERR set_section_monitor(tSolver);
247  CHKERR TSSetDM(tSolver, dM);
248 
249  //default max time is 1
250  CHKERR TSSetMaxTime(tSolver, 1.0);
251  CHKERR TSSetExactFinalTime(tSolver, TS_EXACTFINALTIME_MATCHSTEP);
252  CHKERR TSSetFromOptions(tSolver);
253 
254  for (int i = 0; i != 3; ++i)
255  vScatter[i] = scatter_create(i, D);
256  CHKERR set_time_monitor(tSolver);
257 
259 }
260 
263 
264  CHKERR TSSetUp(tSolver);
265  CHKERR TSSolve(tSolver, NULL);
266 
267  // CHKERR VecGhostUpdateBegin(D, INSERT_VALUES, SCATTER_FORWARD);
268  // CHKERR VecGhostUpdateEnd(D, INSERT_VALUES, SCATTER_FORWARD);
269  // CHKERR DMoFEMMeshToLocalVector(dm, D, INSERT_VALUES, SCATTER_REVERSE);
270 
272 }
273 
275  std::tuple<SmartPetscObj<Vec>, SmartPetscObj<VecScatter>> &tuple,
276  const std::string msg, Vec &ts_u, double time) {
278  double min_disp, max_disp;
279 
280  CHKERR VecScatterBegin(std::get<1>(tuple), ts_u, std::get<0>(tuple),
281  INSERT_VALUES, SCATTER_FORWARD);
282  CHKERR VecScatterEnd(std::get<1>(tuple), ts_u, std::get<0>(tuple),
283  INSERT_VALUES, SCATTER_FORWARD);
284  CHKERR VecMax(std::get<0>(tuple), PETSC_NULL, &max_disp);
285  CHKERR VecMin(std::get<0>(tuple), PETSC_NULL, &min_disp);
286  MOFEM_LOG_C("WORLD", Sev::inform, "%s time %3.4e min %3.4e max %3.4e\n",
287  msg.c_str(), time, min_disp, max_disp);
288 
289  if (atomTest == 3 && time == 1.00)
290  if (msg == "Ux") {
291  MOFEM_LOG("WORLD", Sev::inform)
292  << "Atom test min displacement X:" << min_disp;
293  if (abs(min_disp + 0.2220) > 1e-4)
294  SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
295  "atom test diverged!");
296  }
297 
299 
300 }
301 } // namespace MoFEMManager
MoFEMFunctionReturnHot
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:460
MoFEM::UnknownInterface::getInterface
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.
Definition: UnknownInterface.hpp:93
H1
@ H1
continuous field
Definition: definitions.h:85
MoFEMManager::MFManager::MFManager
MFManager(MoFEM::Interface &m_field)
Definition: MoFEMManager.hpp:43
LASTBASE
@ LASTBASE
Definition: definitions.h:69
MoFEMManager::MFManager::isQuasiStatic
PetscBool isQuasiStatic
Definition: MoFEMManager.hpp:22
MoFEM::createTS
auto createTS(MPI_Comm comm)
Definition: PetscSmartObj.hpp:249
MoFEM::CoreInterface::get_comm
virtual MPI_Comm & get_comm() const =0
MoFEMManager::MFManager::printMaxMin
MoFEMErrorCode printMaxMin(std::tuple< SmartPetscObj< Vec >, SmartPetscObj< VecScatter >> &tuple, const std::string msg, Vec &ts_u, double time)
Definition: MoFEMManager.hpp:274
MoFEM::createSmartDM
DEPRECATED auto createSmartDM(MPI_Comm comm, const std::string dm_type_name)
Definition: PetscSmartObj.hpp:149
MoFEMManager::MFManager::saveEveryNthStep
int saveEveryNthStep
Definition: MoFEMManager.hpp:20
MoFEM::Exceptions::MoFEMErrorCode
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:56
MoFEMManager::MFManager::tsSetup
MoFEMErrorCode tsSetup()
Definition: MoFEMManager.hpp:181
MoFEMManager
Definition: MoFEMManager.hpp:15
GenericElementInterface::IM
@ IM
Definition: GenericElementInterface.hpp:16
MoFEMManager::MFManager::mField
MoFEM::Interface & mField
Definition: MoFEMManager.hpp:29
GenericElementInterface::EX
@ EX
Definition: GenericElementInterface.hpp:16
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::DeprecatedCoreInterface
Deprecated interface functions.
Definition: DeprecatedCoreInterface.hpp:16
MoFEMManager::MFManager::defineDM
MoFEMErrorCode defineDM()
Definition: MoFEMManager.hpp:160
ROW
@ ROW
Definition: definitions.h:136
MoFEMManager::MFManager::tsSolve
MoFEMErrorCode tsSolve()
Definition: MoFEMManager.hpp:261
MoFEMManager::MFManager::setMainField
MoFEMErrorCode setMainField()
Definition: MoFEMManager.hpp:98
CHKERR
#define CHKERR
Inline error check.
Definition: definitions.h:548
MoFEMManager::MFManager
Definition: MoFEMManager.hpp:17
MoFEMManager::MFManager::monitorPtr
boost::shared_ptr< FEMethod > monitorPtr
Definition: MoFEMManager.hpp:41
MoFEMManager::MFManager::dM
SmartPetscObj< DM > dM
Definition: MoFEMManager.hpp:31
MOFEM_LOG_C
#define MOFEM_LOG_C(channel, severity, format,...)
Definition: LogManager.hpp:311
simple
void simple(double P1[], double P2[], double P3[], double c[], const int N)
Definition: acoustic.cpp:69
MoFEMManager::MFManager::isExplicit
PetscBool isExplicit
Definition: MoFEMManager.hpp:23
MoFEMManager::MFManager::doPrintMaxMin
PetscBool doPrintMaxMin
Definition: MoFEMManager.hpp:25
MoFEM::DMRegister_MoFEM
PetscErrorCode DMRegister_MoFEM(const char sname[])
Register MoFEM problem.
Definition: DMMoFEM.cpp:43
MoFEM::smartVectorDuplicate
DEPRECATED SmartPetscObj< Vec > smartVectorDuplicate(Vec vec)
Definition: PetscSmartObj.hpp:230
MoFEMManager::MFManager::atomTest
int atomTest
Definition: MoFEMManager.hpp:21
MoFEM::smartCreateDMVector
DEPRECATED auto smartCreateDMVector(DM dm)
Definition: DMMoFEM.hpp:1107
MoFEMManager::MFManager::isPartitioned
PetscBool isPartitioned
Definition: MoFEMManager.hpp:24
GenericElementInterface::IM2
@ IM2
Definition: GenericElementInterface.hpp:16
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
i
FTensor::Index< 'i', SPACE_DIM > i
Definition: hcurl_divergence_operator_2d.cpp:27
AINSWORTH_BERNSTEIN_BEZIER_BASE
@ AINSWORTH_BERNSTEIN_BEZIER_BASE
Definition: definitions.h:64
MoFEMManager::MFManager::vScatter
std::tuple< SmartPetscObj< Vec >, SmartPetscObj< VecScatter > > vScatter[3]
Definition: MoFEMManager.hpp:40
v
const double v
phase velocity of light in medium (cm/ns)
Definition: initial_diffusion.cpp:40
MoFEMManager::MFManager::tSolver
SmartPetscObj< TS > tSolver
Definition: MoFEMManager.hpp:30
MOFEM_LOG
#define MOFEM_LOG(channel, severity)
Log.
Definition: LogManager.hpp:308
DEMKOWICZ_JACOBI_BASE
@ DEMKOWICZ_JACOBI_BASE
Definition: definitions.h:66
MoFEMManager::MFManager::getTSType
auto getTSType()
Definition: MoFEMManager.hpp:65
MoFEM::DMMoFEMTSSetMonitor
PetscErrorCode DMMoFEMTSSetMonitor(DM dm, TS ts, const std::string fe_name, boost::shared_ptr< MoFEM::FEMethod > method, boost::shared_ptr< MoFEM::BasicMethod > pre_only, boost::shared_ptr< MoFEM::BasicMethod > post_only)
Set Monitor To TS solver.
Definition: DMMoFEM.cpp:1056
AINSWORTH_LEGENDRE_BASE
@ AINSWORTH_LEGENDRE_BASE
Ainsworth Cole (Legendre) approx. base .
Definition: definitions.h:60
EigenMatrix::Vec
const FTensor::Tensor2< T, Dim, Dim > Vec
Definition: MatrixFunction.hpp:66
MoFEM::PetscOptionsGetEList
PetscErrorCode PetscOptionsGetEList(PetscOptions *, const char pre[], const char name[], const char *const *list, PetscInt next, PetscInt *value, PetscBool *set)
Definition: DeprecatedPetsc.hpp:203
FieldApproximationBase
FieldApproximationBase
approximation base
Definition: definitions.h:58
MoFEMManager::MFManager::order
int order
Definition: MoFEMManager.hpp:19
ReactionDiffusionEquation::D
const double D
diffusivity
Definition: reaction_diffusion.cpp:20
MoFEMManager::MFManager::getParams
MoFEMErrorCode getParams()
Definition: MoFEMManager.hpp:74
MOFEM_ATOM_TEST_INVALID
@ MOFEM_ATOM_TEST_INVALID
Definition: definitions.h:40
MoFEMFunctionBeginHot
#define MoFEMFunctionBeginHot
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:453
MoFEMManager::MFManager::lagJacFromNthSTep
int lagJacFromNthSTep
Definition: MoFEMManager.hpp:27
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::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:429
MoFEMManager::MFManager::boundaryMarker
boost::shared_ptr< std::vector< unsigned char > > boundaryMarker
Definition: MoFEMManager.hpp:33
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.
MoFEMFunctionBegin
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:359
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