v0.16.0
Loading...
Searching...
No Matches
ElasticAdaptiveExample.hpp
Go to the documentation of this file.
1/**
2 * @file ElasticAdaptiveExample.hpp
3 * @brief elastic example with adaptive order refinement
4 *
5 * @copyright Copyright (c) 2026
6 *
7 */
8
9#include <ElasticExample.hpp>
10
12
14
15 MoFEMErrorCode runProblem();
16
17private:
18 MoFEMErrorCode readMesh();
19 MoFEMErrorCode setupAdaptivity();
20 MoFEMErrorCode computeErrorNorms();
21 MoFEMErrorCode outputResults(int ref_iter = -1);
22 MoFEMErrorCode refineOrder(int ref_level);
23 MoFEMErrorCode computeErrorIndicators();
24 MoFEMErrorCode refineGeometry();
25 MoFEMErrorCode calculateGeometryError();
26 MoFEMErrorCode kspSetUpAndSolve(SmartPetscObj<KSP> solver) override;
27
28 int oRder = 2;
29 PetscBool refGeom = PETSC_FALSE;
30 int refAdaptNum = 0;
32 double refGeomThreshold = 1;
33 std::array<double, 2> meanError = {0.0, 0.0};
34 double errorIndicMean = 0.0;
35
36 int atomTest = 0;
37};
38
40
41#include <AdaptiveOrderRef.hpp>
43
44//! [Run problem]
75//! [Run problem]
76
77//! [Read mesh]
80 auto simple = mField.getInterface<Simple>();
81 CHKERR simple->getOptions();
82
83 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-ref_geom", &refGeom,
84 PETSC_NULLPTR);
85
86 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-ref_adapt_num", &refAdaptNum,
87 PETSC_NULLPTR);
88
89 if ((refAdaptNum > 0) && refGeom) {
90 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
91 "Adaptive order and geometry refinement cannot be used together");
92 }
93
94 simple->getAddSkeletonFE() = true;
95 CHKERR simple->loadFile();
96
97 // Add meshsets if config file provided
98 CHKERR mField.getInterface<MeshsetsManager>()->setMeshsetFromFile();
99
100 auto update_ghost_ents = [&]() {
102
103 for (auto m : mField.getInterface<MeshsetsManager>()->getCubitMeshsetPtr(
104 std::regex((boost::format("%s(.*)") % "MAT_ELASTIC").str()))
105
106 ) {
107 Range ents;
108 CHKERR mField.get_moab().get_entities_by_handle(m->getMeshset(), ents,
109 true);
110 CHKERR mField.getInterface<CommInterface>()->synchroniseEntities(ents);
111 CHKERR mField.get_moab().add_entities(m->getMeshset(), ents);
112 }
113
115 };
116
117 CHKERR update_ghost_ents();
118
120}
121//! [Read mesh]
122
123//! [Set up problem]
126
127 Range domainEntities;
128 CHKERR mField.get_moab().get_entities_by_dimension(0, SPACE_DIM,
129 domainEntities);
130 Tag th_order;
131 CHKERR getTagHandle(mField, "ORDER", MB_TYPE_INTEGER, th_order);
132
133 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-order", &oRder, PETSC_NULLPTR);
134 for (auto ent : domainEntities) {
135 CHKERR mField.get_moab().tag_set_data(th_order, &ent, 1, &oRder);
136 }
137
138 CHKERR copyTagOnSkin(mField, "ORDER", MB_TYPE_INTEGER);
139
140 if (refGeom) {
142 CHKERR refineGeometry(); // optional ho refinement
143 }
144
145 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-test", &atomTest,
146 PETSC_NULLPTR);
147
149}
150//! [Set up problem]
151
152// ! [KSP set up]
153MoFEMErrorCode
156
157 MOFEM_LOG_CHANNEL("TIMER");
158 MOFEM_LOG_TAG("TIMER", "timer");
159
160 DM dm;
161 CHKERR KSPGetDM(solver, &dm);
162 auto D = createDMVector(dm);
163 auto F = vectorDuplicate(D);
164
165 CHKERR VecZeroEntries(D);
166 CHKERR VecGhostUpdateBegin(D, INSERT_VALUES, SCATTER_FORWARD);
167 CHKERR VecGhostUpdateEnd(D, INSERT_VALUES, SCATTER_FORWARD);
168 CHKERR DMoFEMMeshToLocalVector(dm, D, INSERT_VALUES, SCATTER_REVERSE);
169
170 BOOST_LOG_SCOPED_THREAD_ATTR("Timeline", attrs::timer());
171 MOFEM_LOG("TIMER", Sev::inform) << "KSPSetUp";
172 CHKERR KSPSetUp(solver);
173 MOFEM_LOG("TIMER", Sev::inform) << "KSPSetUp <= Done";
174 MOFEM_LOG("TIMER", Sev::inform) << "KSPSolve";
175 CHKERR KSPSolve(solver, F, D);
176 MOFEM_LOG("TIMER", Sev::inform) << "KSPSolve <= Done";
177
178 CHKERR VecGhostUpdateBegin(D, INSERT_VALUES, SCATTER_FORWARD);
179 CHKERR VecGhostUpdateEnd(D, INSERT_VALUES, SCATTER_FORWARD);
180 CHKERR DMoFEMMeshToLocalVector(dm, D, INSERT_VALUES, SCATTER_REVERSE);
181
183}
184// ! [KSP set up]
185
186inline auto sqr = [](double v) { return v * v; };
187
188inline MoFEM::VectorFunc analyticalDisplacement = [](double x, double y,
189 double z) {
190 double E = 1e3;
191 double nu = 0.3;
192
193 double r = std::sqrt(x * x + y * y);
194
195 double a = 0.5;
196 double b = 1.0;
197
198 double pa = 2;
199 double pb = 1;
200
201 double A = (pa * sqr(a) - pb * sqr(b)) / (sqr(b) - sqr(a));
202 double B = sqr(a) * sqr(b) * (pa - pb) / (sqr(b) - sqr(a));
203
204 VectorDouble u;
205 u.resize(SPACE_DIM);
206 std::vector<double> v_u;
207
208 double u_r = (1. + nu) / E * ((1. - 2. * nu) * A * r + B / r);
209
210 if (SPACE_DIM == 2)
211 v_u = {u_r / r * x, u_r / r * y};
212 else
213 v_u = {u_r / r * x, u_r / r * y, 0.0};
214
215 for (int i = 0; i < SPACE_DIM; ++i)
216 u(i) = v_u[i];
217
218 return u;
219};
220
221//! [Postprocess results]
222MoFEMErrorCode ElasticAdaptiveExample::outputResults(int ref_iter) {
224 auto simple = mField.getInterface<Simple>();
225
226 std::ostringstream strm;
227 strm << "out_elastic";
228 if (ref_iter >= 0) {
229 strm << "_" << ref_iter;
230 }
231 strm << ".h5m";
232
235 mField, simple->getDM(), simple->getDomainFEName(), strm.str(), {},
236 {"GEOMETRY_ERROR", "SOLUTION_ERROR", "ORDER"}, Sev::verbose);
238}
239//! [Postprocess results]
240
241//! [Error Norms]
243 MOFEM_LOG_CHANNEL("WORLD");
244 auto simple = mField.getInterface<Simple>();
246
247 auto integration_rule = [](int, int, int p_data) { return p_data + 1; };
248 auto evaluation_fe = boost::make_shared<DomainEle>(mField);
249 evaluation_fe->getRuleHook = integration_rule;
250
251 auto &evaluation_pipeline = evaluation_fe->getOpPtrVector();
253 {H1}, "GEOMETRY");
254
255 auto u_ptr = boost::make_shared<MatrixDouble>();
256 evaluation_pipeline.push_back(
257 new OpCalculateVectorFieldValues<SPACE_DIM>("U", u_ptr));
258
259 auto analytical_disp_ptr = boost::make_shared<MatrixDouble>();
260
261 evaluation_pipeline.push_back(new OpGetTensor1fromFunc<SPACE_DIM, SPACE_DIM>(
262 analytical_disp_ptr, analyticalDisplacement));
263
264 auto normsVec = createVectorMPI(mField.get_comm(),
265 (mField.get_comm_rank() == 0) ? 1 : 0, 1);
266
267 evaluation_pipeline.push_back(new OpCalcNormL2Tensor1<SPACE_DIM>(
268 u_ptr, normsVec, 0, analytical_disp_ptr));
269
270 CHKERR VecZeroEntries(normsVec);
271 CHKERR DMoFEMLoopFiniteElements(simple->getDM(), simple->getDomainFEName(),
272 evaluation_fe);
273 CHKERR VecAssemblyBegin(normsVec);
274 CHKERR VecAssemblyEnd(normsVec);
275
276 if (mField.get_comm_rank() == 0) {
277 const double *norms;
278 CHKERR VecGetArrayRead(normsVec, &norms);
279 MOFEM_TAG_AND_LOG("SELF", Sev::inform, "Errors")
280 << "Displacement error L2 norm: " << std::scientific
281 << std::sqrt(norms[0]);
282
283 if (atomTest == 3 && std::sqrt(norms[0]) > 2.195e-06) {
284 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
285 "atom test failed: displacement error L2 norm is too high: "
286 "%3.6e",
287 std::sqrt(norms[0]));
288 }
289
290 if (atomTest == 4 && std::sqrt(norms[0]) > 1.995e-07) {
291 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
292 "atom test failed: displacement error L2 norm is too high: "
293 "%3.6e",
294 std::sqrt(norms[0]));
295 }
296
297 CHKERR VecRestoreArrayRead(normsVec, &norms);
298 }
299
301}
302//! [Error Norms]
Calculate error indicators.
static MoFEMErrorCode getTagHandle(MoFEM::Interface &m_field, const char *name, DataType type, Tag &tag_handle)
static MoFEMErrorCode copyTagOnSkin(MoFEM::Interface &m_field, const char *name, DataType type)
MoFEM::VectorFunc analyticalDisplacement
Implementation of elastic example class.
Higher Order Geometry refinement.
#define MOFEM_TAG_AND_LOG(channel, severity, tag)
Tag and log in channel.
void simple(double P1[], double P2[], double P3[], double c[], const int N)
Definition acoustic.cpp:69
constexpr double a
constexpr int SPACE_DIM
ElementsAndOps< SPACE_DIM >::DomainEle DomainEle
ElementsAndOps< SPACE_DIM >::BoundaryEle BoundaryEle
@ 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 ...
@ MOFEM_ATOM_TEST_INVALID
Definition definitions.h:40
@ MOFEM_DATA_INCONSISTENCY
Definition definitions.h:31
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
@ F
auto integration_rule
#define MOFEM_LOG(channel, severity)
Log.
#define MOFEM_LOG_TAG(channel, tag)
Tag channel.
#define MOFEM_LOG_CHANNEL(channel)
Set and reset channel.
FTensor::Index< 'i', SPACE_DIM > i
double D
const double v
phase velocity of light in medium (cm/ns)
MoFEMErrorCode postProcessElasticResults(MoFEM::Interface &mField, SmartPetscObj< DM > dm, const std::string &domain_fe_name, const std::string &out_file_name, std::vector< std::pair< std::string, SmartPetscObj< Vec > > > extra_vectors={}, const std::vector< std::string > &tags_to_transfer={}, const Sev hooke_ops_sev=Sev::verbose)
boost::function< VectorDouble(const double, const double, const double)> VectorFunc
constexpr AssemblyType A
FTensor::Index< 'm', 3 > m
MoFEMErrorCode calculateGeometryError()
Calculate geometry error and save on a tag.
MoFEMErrorCode computeErrorNorms()
[Postprocess results]
ElasticAdaptiveExample(MoFEM::Interface &m_field)
std::array< double, 2 > meanError
MoFEMErrorCode kspSetUpAndSolve(SmartPetscObj< KSP > solver) override
[Set up problem]
MoFEMErrorCode refineOrder(int ref_level)
MoFEMErrorCode computeErrorIndicators()
MoFEMErrorCode runProblem()
[Run problem]
MoFEMErrorCode readMesh()
[Run problem]
MoFEMErrorCode setupAdaptivity()
[Read mesh]
virtual MoFEMErrorCode solveSystem()
[Solve]
virtual MoFEMErrorCode outputResults()
[Solve]
virtual MoFEMErrorCode assembleSystem()
[Boundary condition]
virtual MoFEMErrorCode setupProblem()
[Read mesh]
MoFEM::Interface & mField
virtual MoFEMErrorCode boundaryCondition()
[Set up problem]
virtual MoFEMErrorCode checkResults()
[Postprocess results]
virtual moab::Interface & get_moab()=0
virtual MPI_Comm & get_comm() const =0
virtual int get_comm_rank() const =0
Deprecated interface functions.
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.
ElementsAndOps< SPACE_DIM >::SideEle SideEle
Definition plastic.cpp:62