v0.13.2
Loading...
Searching...
No Matches
nonlinear_elastic.cpp
Go to the documentation of this file.
1/**
2 * \file nonlinear_elastic.cpp
3 * \example nonlinear_elastic.cpp
4 *
5 * Plane stress elastic dynamic problem
6 *
7 */
8
9#include <MoFEM.hpp>
10#include <MatrixFunction.hpp>
11
12using namespace MoFEM;
13
14template <int DIM> struct ElementsAndOps {};
15
16template <> struct ElementsAndOps<2> {
19};
20
21template <> struct ElementsAndOps<3> {
24};
25
26constexpr int SPACE_DIM = 3; //< Space dimension of problem, mesh
27
33
35
37 GAUSS>::OpGradTensorGrad<1, SPACE_DIM, SPACE_DIM, 1>;
40
45
49
50constexpr double young_modulus = 100;
51constexpr double poisson_ratio = 0.3;
52constexpr double bulk_modulus_K = young_modulus / (3 * (1 - 2 * poisson_ratio));
53constexpr double shear_modulus_G = young_modulus / (2 * (1 + poisson_ratio));
54
55#include <HenckyOps.hpp>
56using namespace HenckyOps;
57
58struct Example {
59
60 Example(MoFEM::Interface &m_field) : mField(m_field) {}
61
63
64private:
66
75
76 boost::shared_ptr<MatrixDouble> matGradPtr;
77 boost::shared_ptr<MatrixDouble> matStrainPtr;
78 boost::shared_ptr<MatrixDouble> matStressPtr;
79 boost::shared_ptr<MatrixDouble> matDPtr;
80 boost::shared_ptr<HenckyOps::CommonData> commonHenckyDataPtr;
81};
82
83//! [Create common data]
86
87 auto set_material_stiffness = [&]() {
94 constexpr double A =
95 (SPACE_DIM == 2) ? 2 * shear_modulus_G /
96 (bulk_modulus_K + (4. / 3.) * shear_modulus_G)
97 : 1;
98 auto t_D = getFTensor4DdgFromMat<SPACE_DIM, SPACE_DIM, 0>(*matDPtr);
99 t_D(i, j, k, l) = 2 * shear_modulus_G * ((t_kd(i, k) ^ t_kd(j, l)) / 4.) +
100 A * (bulk_modulus_K - (2. / 3.) * shear_modulus_G) *
101 t_kd(i, j) * t_kd(k, l);
103 };
104
105 matGradPtr = boost::make_shared<MatrixDouble>();
106 matStrainPtr = boost::make_shared<MatrixDouble>();
107 matStressPtr = boost::make_shared<MatrixDouble>();
108 matDPtr = boost::make_shared<MatrixDouble>();
109
110 commonHenckyDataPtr = boost::make_shared<HenckyOps::CommonData>();
111 commonHenckyDataPtr->matGradPtr = matGradPtr;
112 commonHenckyDataPtr->matDPtr = matDPtr;
113
114 constexpr auto size_symm = (SPACE_DIM * (SPACE_DIM + 1)) / 2;
115 matDPtr->resize(size_symm * size_symm, 1);
116
117 CHKERR set_material_stiffness();
118
120}
121//! [Create common data]
122
123//! [Run problem]
135}
136//! [Run problem]
137
138//! [Read mesh]
142 CHKERR simple->getOptions();
143 CHKERR simple->loadFile();
145}
146//! [Read mesh]
147
148//! [Set up problem]
152 // Add field
153 CHKERR simple->addDomainField("U", H1, AINSWORTH_LEGENDRE_BASE, SPACE_DIM);
154 CHKERR simple->addBoundaryField("U", H1, AINSWORTH_LEGENDRE_BASE, SPACE_DIM);
155 int order = 2;
156 CHKERR PetscOptionsGetInt(PETSC_NULL, "", "-order", &order, PETSC_NULL);
157 CHKERR simple->setFieldOrder("U", order);
158 CHKERR simple->setUp();
160}
161//! [Set up problem]
162
163//! [Boundary condition]
166 auto *pipeline_mng = mField.getInterface<PipelineManager>();
168 auto bc_mng = mField.getInterface<BcManager>();
169 auto time_scale = boost::make_shared<TimeScale>();
170
171 auto integration_rule = [](int, int, int approx_order) {
172 return 2 * (approx_order - 1);
173 };
174
175 CHKERR pipeline_mng->setDomainRhsIntegrationRule(integration_rule);
176 CHKERR pipeline_mng->setDomainLhsIntegrationRule(integration_rule);
177 CHKERR pipeline_mng->setBoundaryRhsIntegrationRule(integration_rule);
178
180 pipeline_mng->getOpBoundaryRhsPipeline(), mField, "U", {time_scale},
181 "FORCE", Sev::inform);
182
183 //! [Define gravity vector]
185 pipeline_mng->getOpDomainRhsPipeline(), mField, "U", {time_scale},
186 "BODY_FORCE", Sev::inform);
187
188 // Essential BC
189 CHKERR bc_mng->removeBlockDOFsOnEntities<DisplacementCubitBcData>(
190 simple->getProblemName(), "U");
191
192 auto get_bc_hook_rhs = [&]() {
194 mField, pipeline_mng->getDomainRhsFE(), {time_scale}, false);
195 return hook;
196 };
197
198 auto get_bc_hook_lhs = [&]() {
200 mField, pipeline_mng->getDomainLhsFE(), {time_scale}, false);
201 return hook;
202 };
203
204 pipeline_mng->getDomainRhsFE()->preProcessHook = get_bc_hook_rhs();
205 pipeline_mng->getDomainLhsFE()->preProcessHook = get_bc_hook_lhs();
206
208}
209//! [Boundary condition]
210
211//! [Push operators to pipeline]
214 auto *simple = mField.getInterface<Simple>();
215 auto *pipeline_mng = mField.getInterface<PipelineManager>();
216
217 auto add_domain_base_ops = [&](auto &pipeline) {
219
222 "U", matGradPtr));
223 pipeline.push_back(
225 pipeline.push_back(
227 pipeline.push_back(
229 pipeline.push_back(
231 pipeline.push_back(
233
235 };
236
237 auto add_domain_ops_lhs = [&](auto &pipeline) {
239
240 pipeline.push_back(
242 pipeline.push_back(new OpK("U", "U", commonHenckyDataPtr->getMatTangent()));
243
245 };
246
247 auto add_domain_ops_rhs = [&](auto &pipeline) {
249 // Calculate internal force
250 pipeline.push_back(new OpInternalForce(
251 "U", commonHenckyDataPtr->getMatFirstPiolaStress()));
252
254 };
255
256 CHKERR add_domain_base_ops(pipeline_mng->getOpDomainLhsPipeline());
257 CHKERR add_domain_ops_lhs(pipeline_mng->getOpDomainLhsPipeline());
258 CHKERR add_domain_base_ops(pipeline_mng->getOpDomainRhsPipeline());
259 CHKERR add_domain_ops_rhs(pipeline_mng->getOpDomainRhsPipeline());
260
262}
263//! [Push operators to pipeline]
264
265/**
266 * @brief Monitor solution
267 *
268 * This functions is called by TS solver at the end of each step. It is used
269 * to output results to the hard drive.
270 */
271struct Monitor : public FEMethod {
272 Monitor(SmartPetscObj<DM> dm, boost::shared_ptr<PostProcEle> post_proc)
273 : dM(dm), postProc(post_proc){};
276 constexpr int save_every_nth_step = 1;
277 if (ts_step % save_every_nth_step == 0) {
279 CHKERR postProc->writeFile(
280 "out_step_" + boost::lexical_cast<std::string>(ts_step) + ".h5m");
281 }
283 }
284
285private:
287 boost::shared_ptr<PostProcEle> postProc;
288};
289
290//! [Solve]
293 auto *simple = mField.getInterface<Simple>();
294 auto *pipeline_mng = mField.getInterface<PipelineManager>();
295
296 auto dm = simple->getDM();
297 auto ts = pipeline_mng->createTSIM();
298
299 // Setup postprocessing
300 auto post_proc_fe = boost::make_shared<PostProcEle>(mField);
301
303 post_proc_fe->getOpPtrVector(), {H1});
304
305 post_proc_fe->getOpPtrVector().push_back(
307 "U", commonHenckyDataPtr->matGradPtr));
308 post_proc_fe->getOpPtrVector().push_back(
310 post_proc_fe->getOpPtrVector().push_back(
312 post_proc_fe->getOpPtrVector().push_back(
314 post_proc_fe->getOpPtrVector().push_back(
316 post_proc_fe->getOpPtrVector().push_back(
318
319 auto u_ptr = boost::make_shared<MatrixDouble>();
320 post_proc_fe->getOpPtrVector().push_back(
322
324
325 post_proc_fe->getOpPtrVector().push_back(
326
327 new OpPPMap(
328
329 post_proc_fe->getPostProcMesh(), post_proc_fe->getMapGaussPts(),
330
331 {},
332
333 {{"U", u_ptr}},
334
335 {{"GRAD", commonHenckyDataPtr->matGradPtr},
336 {"FIRST_PIOLA", commonHenckyDataPtr->getMatFirstPiolaStress()}},
337
338 {}
339
340 )
341
342 );
343
344 // Add monitor to time solver
345 boost::shared_ptr<FEMethod> null_fe;
346 auto monitor_ptr = boost::make_shared<Monitor>(dm, post_proc_fe);
347 CHKERR DMMoFEMTSSetMonitor(dm, ts, simple->getDomainFEName(), null_fe,
348 null_fe, monitor_ptr);
349
350 double ftime = 1;
351 CHKERR TSSetDuration(ts, PETSC_DEFAULT, ftime);
352 CHKERR TSSetExactFinalTime(ts, TS_EXACTFINALTIME_MATCHSTEP);
353
354 auto D = smartCreateDMVector(simple->getDM());
355
356 CHKERR TSSetSolution(ts, D);
357 CHKERR TSSetFromOptions(ts);
358
359 CHKERR TSSolve(ts, NULL);
360 CHKERR TSGetTime(ts, &ftime);
361
362 PetscInt steps, snesfails, rejects, nonlinits, linits;
363 CHKERR TSGetTimeStepNumber(ts, &steps);
364 CHKERR TSGetSNESFailures(ts, &snesfails);
365 CHKERR TSGetStepRejections(ts, &rejects);
366 CHKERR TSGetSNESIterations(ts, &nonlinits);
367 CHKERR TSGetKSPIterations(ts, &linits);
368 MOFEM_LOG_C("EXAMPLE", Sev::inform,
369 "steps %D (%D rejected, %D SNES fails), ftime %g, nonlinits "
370 "%D, linits %D\n",
371 steps, rejects, snesfails, ftime, nonlinits, linits);
372
374}
375//! [Solve]
376
377//! [Postprocessing results]
380 PetscBool test_flg = PETSC_FALSE;
381 CHKERR PetscOptionsGetBool(PETSC_NULL, "", "-test", &test_flg, PETSC_NULL);
382 if (test_flg) {
383 auto *simple = mField.getInterface<Simple>();
384 auto T = smartCreateDMVector(simple->getDM());
385 CHKERR DMoFEMMeshToLocalVector(simple->getDM(), T, INSERT_VALUES,
386 SCATTER_FORWARD);
387 double nrm2;
388 CHKERR VecNorm(T, NORM_2, &nrm2);
389 MOFEM_LOG("EXAMPLE", Sev::inform) << "Regression norm " << nrm2;
390 constexpr double regression_value = 1.09572;
391 if (fabs(nrm2 - regression_value) > 1e-2)
392 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
393 "Regression test field; wrong norm value.");
394 }
396}
397//! [Postprocessing results]
398
399//! [Check]
403}
404//! [Check]
405
406static char help[] = "...\n\n";
407
408int main(int argc, char *argv[]) {
409
410 // Initialisation of MoFEM/PETSc and MOAB data structures
411 const char param_file[] = "param_file.petsc";
413
414 // Add logging channel for example
415 auto core_log = logging::core::get();
416 core_log->add_sink(
418 LogManager::setLog("EXAMPLE");
419 MOFEM_LOG_TAG("EXAMPLE", "example");
420
421 try {
422
423 //! [Register MoFEM discrete manager in PETSc]
424 DMType dm_name = "DMMOFEM";
425 CHKERR DMRegister_MoFEM(dm_name);
426 //! [Register MoFEM discrete manager in PETSc
427
428 //! [Create MoAB]
429 moab::Core mb_instance; ///< mesh database
430 moab::Interface &moab = mb_instance; ///< mesh database interface
431 //! [Create MoAB]
432
433 //! [Create MoFEM]
434 MoFEM::Core core(moab); ///< finite element database
435 MoFEM::Interface &m_field = core; ///< finite element database interface
436 //! [Create MoFEM]
437
438 //! [Example]
439 Example ex(m_field);
440 CHKERR ex.runProblem();
441 //! [Example]
442 }
444
446}
std::string param_file
#define MOFEM_LOG_C(channel, severity, format,...)
Definition: LogManager.hpp:311
void simple(double P1[], double P2[], double P3[], double c[], const int N)
Definition: acoustic.cpp:69
int main()
Definition: adol-c_atom.cpp:46
constexpr int SPACE_DIM
ElementsAndOps< SPACE_DIM >::DomainEle DomainEle
ElementsAndOps< SPACE_DIM >::BoundaryEle BoundaryEle
Kronecker Delta class symmetric.
#define CATCH_ERRORS
Catch errors.
Definition: definitions.h:372
@ AINSWORTH_LEGENDRE_BASE
Ainsworth Cole (Legendre) approx. base .
Definition: definitions.h:60
@ H1
continuous field
Definition: definitions.h:85
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:346
@ MOFEM_ATOM_TEST_INVALID
Definition: definitions.h:40
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:416
#define CHKERR
Inline error check.
Definition: definitions.h:535
FormsIntegrators< DomainEleOp >::Assembly< A >::LinearForm< I >::OpGradTimesSymTensor< 1, SPACE_DIM, SPACE_DIM > OpInternalForce
Definition: elastic.cpp:36
constexpr double shear_modulus_G
Definition: elastic.cpp:60
constexpr double bulk_modulus_K
Definition: elastic.cpp:59
auto integration_rule
constexpr auto t_kd
PetscErrorCode DMoFEMMeshToLocalVector(DM dm, Vec l, InsertMode mode, ScatterMode scatter_mode)
set local (or ghosted) vector values on mesh for partition only
Definition: DMMoFEM.cpp:511
PetscErrorCode DMRegister_MoFEM(const char sname[])
Register MoFEM problem.
Definition: DMMoFEM.cpp:47
PetscErrorCode DMoFEMLoopFiniteElements(DM dm, const char fe_name[], MoFEM::FEMethod *method, CacheTupleWeakPtr cache_ptr=CacheTupleSharedPtr())
Executes FEMethod for finite elements in DM.
Definition: DMMoFEM.cpp:574
auto smartCreateDMVector(DM dm)
Get smart vector from DM.
Definition: DMMoFEM.hpp:995
static LoggerType & setLog(const std::string channel)
Set ans resset chanel logger.
Definition: LogManager.cpp:391
#define MOFEM_LOG(channel, severity)
Log.
Definition: LogManager.hpp:308
#define MOFEM_LOG_TAG(channel, tag)
Tag channel.
Definition: LogManager.hpp:339
double D
FTensor::Index< 'k', 3 > k
const double T
FormsIntegrators< DomainEleOp >::Assembly< USER_ASSEMBLE >::LinearForm< GAUSS >::OpSource< 1, 3 > OpBodyForce
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:56
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:10
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:1044
PetscErrorCode PetscOptionsGetInt(PetscOptions *, const char pre[], const char name[], PetscInt *ivalue, PetscBool *set)
PetscErrorCode PetscOptionsGetBool(PetscOptions *, const char pre[], const char name[], PetscBool *bval, PetscBool *set)
constexpr AssemblyType A
int save_every_nth_step
OpPostProcMapInMoab< SPACE_DIM, SPACE_DIM > OpPPMap
constexpr auto size_symm
Definition: plastic.cpp:33
static constexpr int approx_order
FTensor::Index< 'l', 3 > l
FTensor::Index< 'j', 3 > j
FTensor::Index< 'i', 3 > i
[Example]
Definition: plastic.cpp:141
MoFEMErrorCode boundaryCondition()
MoFEMErrorCode assembleSystem()
MoFEMErrorCode readMesh()
boost::shared_ptr< MatrixDouble > matGradPtr
boost::shared_ptr< MatrixDouble > matStressPtr
boost::shared_ptr< MatrixDouble > matStrainPtr
MoFEMErrorCode checkResults()
MoFEMErrorCode solveSystem()
MoFEMErrorCode createCommonData()
Example(MoFEM::Interface &m_field)
MoFEMErrorCode runProblem()
MoFEM::Interface & mField
Definition: plastic.cpp:148
MoFEMErrorCode setupProblem()
MoFEMErrorCode outputResults()
boost::shared_ptr< MatrixDouble > matDPtr
boost::shared_ptr< HenckyOps::CommonData > commonHenckyDataPtr
Definition: plastic.cpp:157
Add operators pushing bases from local to physical configuration.
Simple interface for fast problem set-up.
Definition: BcManager.hpp:24
Core (interface) class.
Definition: Core.hpp:82
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
Definition: Core.cpp:72
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition: Core.cpp:112
Deprecated interface functions.
Definition of the displacement bc data structure.
Definition: BCData.hpp:72
Data on single entity (This is passed as argument to DataOperator::doWork)
Class (Function) to enforce essential constrains.
Definition: Essential.hpp:39
structure for User Loop Methods on finite elements
static boost::shared_ptr< SinkType > createSink(boost::shared_ptr< std::ostream > stream_ptr, std::string comm_filter)
Create a sink object.
Definition: LogManager.cpp:300
static boost::shared_ptr< std::ostream > getStrmWorld()
Get the strm world object.
Definition: LogManager.cpp:346
Assembly methods.
Definition: Natural.hpp:57
Get field gradients at integration pts for scalar filed rank 0, i.e. vector field.
Get values at integration pts for tensor filed rank 1, i.e. vector field.
Post post-proc data at points from hash maps.
PipelineManager interface.
MoFEM::FaceElementForcesAndSourcesCore FaceEle
MoFEM::EdgeElementForcesAndSourcesCore EdgeEle
Simple interface for fast problem set-up.
Definition: Simple.hpp:27
intrusive_ptr for managing petsc objects
PetscInt ts_step
time step number
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface refernce to pointer of interface.
Monitor solution.
Monitor(SmartPetscObj< DM > dm, boost::shared_ptr< PostProcEle > post_proc)
SmartPetscObj< DM > dM
MoFEMErrorCode postProcess()
function is run at the end of loop
boost::shared_ptr< PostProcEle > postProc
static char help[]
[Check]
constexpr int SPACE_DIM
constexpr double poisson_ratio
constexpr double shear_modulus_G
constexpr double bulk_modulus_K
constexpr double young_modulus