v0.16.0
Loading...
Searching...
No Matches
Functions | Variables
map_disp_prism.cpp File Reference
#include <MoFEM.hpp>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <boost/tokenizer.hpp>
#include <DispMap.hpp>

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Variables

static char help [] = "\n"
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 23 of file map_disp_prism.cpp.

23 {
24 MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
25
26 try {
27
28 moab::Core mb_instance;
29 moab::Interface &moab = mb_instance;
30
31 // global variables
32 char mesh_file_name[255];
33 char data_file_name1[255];
34 char data_file_name2[255];
35 PetscBool flg_file = PETSC_FALSE;
36 PetscInt order = 1;
37 PetscBool flg_n_part = PETSC_FALSE;
38 PetscInt n_partas = 1;
39 PetscBool flg_thickness = PETSC_FALSE;
40 PetscBool flg_legacy_thickness = PETSC_FALSE;
41 double thickness = 0;
42 double legacy_thickness = 0;
43
44 PetscOptionsBegin(PETSC_COMM_WORLD, "", "none", "none");
45 CHKERR PetscOptionsString("-my_file", "mesh file name", "", "mesh.h5m",
46 mesh_file_name, 255, &flg_file);
47
48 CHKERR PetscOptionsString("-my_data_x", "data file name", "", "data_x.h5m",
49 data_file_name1, 255, PETSC_NULLPTR);
50
51 CHKERR PetscOptionsString("-my_data_y", "data file name", "", "data_y.h5m",
52 data_file_name2, 255, PETSC_NULLPTR);
53
54 CHKERR PetscOptionsInt("-my_order", "default approximation order", "", 1,
55 &order, PETSC_NULLPTR);
56
57 CHKERR PetscOptionsInt("-my_nparts", "number of parts", "", 1, &n_partas,
58 &flg_n_part);
59
60 CHKERR PetscOptionsScalar("-my_thickness", "top layer thickness", "",
61 thickness, &thickness, &flg_thickness);
62 CHKERR PetscOptionsScalar(
63 "-my_thinckness", "deprecated alias for -my_thickness", "",
64 legacy_thickness, &legacy_thickness, &flg_legacy_thickness);
65
66 PetscOptionsEnd();
67
68 if (flg_thickness != PETSC_TRUE &&
69 flg_legacy_thickness == PETSC_TRUE) {
70 thickness = legacy_thickness;
71 }
72
73 if (flg_file != PETSC_TRUE) {
74 SETERRQ(PETSC_COMM_SELF, 1, "*** ERROR -my_file (MESH FILE NEEDED)");
75 }
76
77 if (flg_n_part != PETSC_TRUE) {
78 SETERRQ(PETSC_COMM_SELF, 1, "*** ERROR partitioning number not given");
79 }
80
81 if (flg_thickness != PETSC_TRUE &&
82 flg_legacy_thickness != PETSC_TRUE) {
83 SETERRQ(PETSC_COMM_SELF, 1, "*** ERROR thickness of top layer not given");
84 }
85
86 const char *option;
87 option = "";
88 CHKERR moab.load_file(mesh_file_name, 0, option);
89 ParallelComm *pcomm = ParallelComm::get_pcomm(&moab, MYPCOMM_INDEX);
90 if (pcomm == nullptr)
91 pcomm = new ParallelComm(&moab, PETSC_COMM_WORLD);
92
93 MoFEM::Core core(moab);
94 MoFEM::Interface &m_field = core;
95 // meshset consisting all entities in mesh
96 EntityHandle root_set = moab.get_root_set();
97
98 // Seed all mesh entities to MoFEM database, those entities can be
99 // potentially used as finite elements or as entities which carry some
100 // approximation field.
101 BitRefLevel bit_level0;
102 bit_level0.set(0);
103 CHKERR m_field.getInterface<BitRefManager>()->setBitRefLevelByDim(
104 root_set, 3, bit_level0);
105 // Add field
106 CHKERR m_field.add_field("DISP_X", H1, AINSWORTH_LEGENDRE_BASE, 1);
107 CHKERR m_field.add_field("DISP_Y", H1, AINSWORTH_LEGENDRE_BASE, 1);
108
109 auto *meshsets_manager_ptr = m_field.getInterface<MeshsetsManager>();
110
112 CHKERR meshsets_manager_ptr->getEntitiesByDimension(101, SIDESET, 2,
113 surface, true);
114 CHKERR m_field.add_ents_to_field_by_type(surface, MBTRI, "DISP_X");
115 CHKERR m_field.add_ents_to_field_by_type(surface, MBTRI, "DISP_Y");
116
117 CHKERR m_field.set_field_order(0, MBVERTEX, "DISP_X", 1);
118 CHKERR m_field.set_field_order(0, MBEDGE, "DISP_X", order);
119 CHKERR m_field.set_field_order(0, MBTRI, "DISP_X", order);
120
121 CHKERR m_field.set_field_order(0, MBVERTEX, "DISP_Y", 1);
122 CHKERR m_field.set_field_order(0, MBEDGE, "DISP_Y", order);
123 CHKERR m_field.set_field_order(0, MBTRI, "DISP_Y", order);
124
125 CHKERR m_field.build_fields();
126
127 CHKERR m_field.add_finite_element("DISP_MAP_ELEMENT");
128 CHKERR m_field.modify_finite_element_add_field_row("DISP_MAP_ELEMENT",
129 "DISP_X");
130 CHKERR m_field.modify_finite_element_add_field_col("DISP_MAP_ELEMENT",
131 "DISP_X");
132 CHKERR m_field.modify_finite_element_add_field_data("DISP_MAP_ELEMENT",
133 "DISP_X");
134 CHKERR m_field.modify_finite_element_add_field_data("DISP_MAP_ELEMENT",
135 "DISP_Y");
136
137 // Add entities to that element
139 "DISP_MAP_ELEMENT");
140
141 // build finite elements
143 // build adjacencies between elements and degrees of freedom
144 CHKERR m_field.build_adjacencies(bit_level0);
145
146 // set discrete manager
147 DM dm_disp;
148 DMType dm_name = "DM_DISP";
149 // Register DM problem
150 CHKERR DMRegister_MoFEM(dm_name);
151 CHKERR DMCreate(PETSC_COMM_WORLD, &dm_disp);
152 CHKERR DMSetType(dm_disp, dm_name);
153 // Create DM instance
154 CHKERR DMMoFEMCreateMoFEM(dm_disp, &m_field, dm_name, bit_level0);
155 // Configure DM form line command options (DM itself, solvers,
156 // pre-conditioners, ... )
157 CHKERR DMSetFromOptions(dm_disp);
158 // Add elements to dm (only one here)
159 CHKERR DMMoFEMAddElement(dm_disp, "DISP_MAP_ELEMENT");
160 // Set up problem (number dofs, partition mesh, etc.)
161 CHKERR DMSetUp(dm_disp);
162
163 DispMapFe fe_face(m_field);
164
165 // string file(argv[1]); cout << "argument 1:" << argv[1] <<endl;
166 // string file2(argv[2]); cout << "argument 2:" << argv[2] <<endl;
167
168 string file1(data_file_name1);
169 DataFromFiles data_from_files(file1);
170 CHKERR data_from_files.fromOptions();
171 CHKERR data_from_files.loadFileData();
172
173 string file2(data_file_name2);
174 DataFromFiles data_from_files2(file2);
175 CHKERR data_from_files2.fromOptions();
176 CHKERR data_from_files2.loadFileData();
177
178 auto &list_of_operators = fe_face.getOpPtrVector();
179
180 Mat A;
181 Vec Fx, Dx;
182 CHKERR DMCreateMatrix(dm_disp, &A);
183 CHKERR m_field.getInterface<VecManager>()->vecCreateGhost("DM_DISP", ROW,
184 &Fx);
185 CHKERR m_field.getInterface<VecManager>()->vecCreateGhost("DM_DISP", COL,
186 &Dx);
187 Vec Fy, Dy;
188 CHKERR VecDuplicate(Fx, &Fy);
189 CHKERR VecDuplicate(Dx, &Dy);
190
191 CHKERR MatZeroEntries(A);
192 CHKERR VecZeroEntries(Fx);
193 CHKERR VecZeroEntries(Fy);
194
195 CommonData common_data;
196 common_data.globA = A;
197
198 common_data.globF = Fx;
199
200 auto det_ptr = boost::make_shared<VectorDouble>();
201 auto jac_ptr = boost::make_shared<MatrixDouble>();
202 auto inv_jac_ptr = boost::make_shared<MatrixDouble>();
203 list_of_operators.push_back(new OpCalculateHOJacForFace(jac_ptr));
204 list_of_operators.push_back(
205 new OpInvertMatrix<2>(jac_ptr, det_ptr, inv_jac_ptr));
206 list_of_operators.push_back(new OpSetInvJacH1ForFace(inv_jac_ptr));
207 list_of_operators.push_back(
208 new OpCalculateLhs("DISP_X", "DISP_X", common_data, data_from_files));
209 list_of_operators.push_back(
210 new OpCalulatefRhoAtGaussPts("DISP_X", common_data, data_from_files));
211 list_of_operators.push_back(new OpAssembleRhs("DISP_X", common_data));
212
213 CHKERR DMoFEMLoopFiniteElements(dm_disp, "DISP_MAP_ELEMENT", &fe_face);
214
215 // cout << "error.no !" <<error++ << endl;
216 CHKERR MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
217 CHKERR MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);
218 CHKERR VecGhostUpdateBegin(Fx, ADD_VALUES, SCATTER_REVERSE);
219 CHKERR VecGhostUpdateEnd(Fx, ADD_VALUES, SCATTER_REVERSE);
220 CHKERR VecAssemblyBegin(Fx);
221 CHKERR VecAssemblyEnd(Fx);
222
223 // int error=1;
224 common_data.globF = Fy;
225 list_of_operators.clear();
226 list_of_operators.push_back(new OpCalculateHOJacForFace(jac_ptr));
227 list_of_operators.push_back(
228 new OpInvertMatrix<2>(jac_ptr, det_ptr, inv_jac_ptr));
229 list_of_operators.push_back(new OpSetInvJacH1ForFace(inv_jac_ptr));
230 list_of_operators.push_back(
231 new OpCalulatefRhoAtGaussPts("DISP_X", common_data, data_from_files2));
232 list_of_operators.push_back(new OpAssembleRhs("DISP_X", common_data));
233 // cout << "error.no !"<< error++<< endl;
234
235 CHKERR DMoFEMLoopFiniteElements(dm_disp, "DISP_MAP_ELEMENT", &fe_face);
236 // cout << "error.no !" <<error++ << endl;
237 CHKERR VecGhostUpdateBegin(Fy, ADD_VALUES, SCATTER_REVERSE);
238 CHKERR VecGhostUpdateEnd(Fy, ADD_VALUES, SCATTER_REVERSE);
239 CHKERR VecAssemblyBegin(Fy);
240 CHKERR VecAssemblyEnd(Fy);
241
242 // VecView(F,PETSC_VIEWER_DRAW_WORLD);
243 // MatView(A,PETSC_VIEWER_DRAW_WORLD);
244 // std::string wait;
245 // std::cin >> wait;
246
247 // solve
248 {
249 KSP ksp;
250 CHKERR KSPCreate(PETSC_COMM_WORLD, &ksp);
251 CHKERR KSPSetOperators(ksp, A, A);
252 CHKERR KSPSetFromOptions(ksp);
253 CHKERR KSPSolve(ksp, Fx, Dx);
254 CHKERR KSPSolve(ksp, Fy, Dy);
255 }
256
257 CHKERR VecGhostUpdateBegin(Dx, INSERT_VALUES, SCATTER_FORWARD);
258 CHKERR VecGhostUpdateEnd(Dx, INSERT_VALUES, SCATTER_FORWARD);
259 CHKERR m_field.getInterface<VecManager>()->setGlobalGhostVector(
260 "DM_DISP", COL, Dx, INSERT_VALUES, SCATTER_REVERSE);
261 CHKERR VecGhostUpdateBegin(Dy, INSERT_VALUES, SCATTER_FORWARD);
262 CHKERR VecGhostUpdateEnd(Dy, INSERT_VALUES, SCATTER_FORWARD);
263 CHKERR m_field.getInterface<VecManager>()->setOtherGlobalGhostVector(
264 "DM_DISP", "DISP_X", "DISP_Y", COL, Dy, INSERT_VALUES, SCATTER_REVERSE);
265
266 // Remove rigid translation
267 double sum_dx = 0;
268 int nb_dx = 0;
270 MBVERTEX, dof)) {
271 sum_dx += (*dof)->getFieldData();
272 nb_dx++;
273 }
274 sum_dx /= nb_dx;
276 MBVERTEX, dof)) {
277 (*dof)->getFieldData() -= sum_dx;
278 }
279 double sum_dy = 0;
280 int nb_dy = 0;
282 MBVERTEX, dof)) {
283 sum_dy += (*dof)->getFieldData();
284 nb_dy++;
285 }
286 sum_dy /= nb_dy;
288 MBVERTEX, dof)) {
289 (*dof)->getFieldData() -= sum_dy;
290 }
291
292 using PostProcFace =
295
296 PostProcFace post_proc(m_field);
297 CHKERR AddHOOps<2, 2, 3>::add(post_proc.getOpPtrVector(), {H1});
298
299 auto disp_x_ptr = boost::make_shared<VectorDouble>();
300 auto disp_y_ptr = boost::make_shared<VectorDouble>();
301 post_proc.getOpPtrVector().push_back(
302 new OpCalculateScalarFieldValues("DISP_X", disp_x_ptr));
303 post_proc.getOpPtrVector().push_back(
304 new OpCalculateScalarFieldValues("DISP_Y", disp_y_ptr));
305 post_proc.getOpPtrVector().push_back(new OpPPMap(
306 post_proc.getPostProcMesh(), post_proc.getMapGaussPts(),
307 OpPPMap::DataMapVec{{"DISP_X", disp_x_ptr},
308 {"DISP_Y", disp_y_ptr}},
311
312 CHKERR DMoFEMLoopFiniteElements(dm_disp, "DISP_MAP_ELEMENT", &post_proc);
313 CHKERR post_proc.writeFile("out_disp.h5m");
314
315 CHKERR VecDestroy(&Fx);
316 CHKERR VecDestroy(&Fy);
317 CHKERR VecDestroy(&Dx);
318 CHKERR VecDestroy(&Dy);
319 CHKERR MatDestroy(&A);
320 CHKERR DMDestroy(&dm_disp);
321
322 // Delete element which will be no longer used
323 CHKERR m_field.delete_finite_element("DISP_MAP_ELEMENT");
324
325 // Add top layer
326 {
327 auto mmanager_ptr = m_field.getInterface<MeshsetsManager>();
328 Range ents_2nd_layer;
329 CHKERR mmanager_ptr->getEntitiesByDimension(101, SIDESET, 2,
330 ents_2nd_layer, true);
331 Range tris_nodes;
332 CHKERR moab.get_connectivity(ents_2nd_layer, tris_nodes, true);
333 std::map<EntityHandle, EntityHandle> map_nodes;
334 for (Range::iterator nit = tris_nodes.begin(); nit != tris_nodes.end();
335 nit++) {
336 double coords[3];
337 CHKERR moab.get_coords(&*nit, 1, coords);
338 coords[2] += thickness;
339 CHKERR moab.create_vertex(coords, map_nodes[*nit]);
340 }
341
342 Range prisms;
343 Range top_tris;
344 for (Range::iterator tit = ents_2nd_layer.begin();
345 tit != ents_2nd_layer.end(); tit++) {
346 int num_nodes;
347 const EntityHandle *conn;
348 CHKERR moab.get_connectivity(*tit, conn, num_nodes);
349 EntityHandle conn_prism[6];
350 for (int n = 0; n != 3; n++) {
351 conn_prism[n] = conn[n];
352 conn_prism[n + 3] = map_nodes[conn[n]];
353 }
354 EntityHandle prism;
355 CHKERR moab.create_element(MBPRISM, conn_prism, 6, prism);
356 Range adj;
357 CHKERR moab.get_adjacencies(&prism, 1, 2, true, adj);
358 CHKERR moab.get_adjacencies(&prism, 1, 1, true, adj);
359 prisms.insert(prism);
360 EntityHandle tri;
361 CHKERR moab.side_element(prism, 2, 4, tri);
362 top_tris.insert(tri);
363 }
364 CHKERR mmanager_ptr->addMeshset(SIDESET, 202);
365 CHKERR mmanager_ptr->addEntitiesToMeshset(SIDESET, 202, top_tris);
366 CHKERR mmanager_ptr->addMeshset(BLOCKSET, 2, "MAT_ELASTIC_2");
367 CHKERR mmanager_ptr->addEntitiesToMeshset(BLOCKSET, 2, prisms);
368 Mat_Elastic mat_elastic;
369 mat_elastic.data.Young = 1;
370 mat_elastic.data.Poisson = 0;
371 CHKERR mmanager_ptr->setAttributesByDataStructure(BLOCKSET, 2,
372 mat_elastic);
373 }
374
375 {
376 // Add one node to fix
377 auto mmanager_ptr = m_field.getInterface<MeshsetsManager>();
378 Range ents_1st_layer;
379 CHKERR mmanager_ptr->getEntitiesByDimension(202, SIDESET, 2,
380 ents_1st_layer, true);
381 int num_nodes;
382 const EntityHandle *conn;
383 CHKERR moab.get_connectivity(ents_1st_layer[0], conn, num_nodes);
384 Skinner skin(&moab);
385 Range skin_edges;
386 CHKERR skin.find_skin(0, ents_1st_layer, false, skin_edges);
387 Range edges;
388 CHKERR moab.get_adjacencies(&*ents_1st_layer.begin(), 1, 1, false, edges);
389 EntityHandle meshset;
390 CHKERR mmanager_ptr->getMeshset(202, SIDESET, meshset);
391 CHKERR moab.add_entities(meshset, conn, 1);
392 CHKERR moab.add_entities(meshset, skin_edges);
393 }
394
395 {
396 Range ents3d;
397 CHKERR moab.get_entities_by_dimension(0, 3, ents3d, false);
398 CHKERR m_field.getInterface<CommInterface>()->partitionMesh(
399 ents3d, 3, 2, n_partas);
400 }
401
402 CHKERR moab.write_file("analysis_mesh.h5m");
403 }
405
407
408 PetscFunctionReturn(0);
409}
@ COL
@ ROW
#define CATCH_ERRORS
Catch errors.
@ AINSWORTH_LEGENDRE_BASE
Ainsworth Cole (Legendre) approx. base .
Definition definitions.h:60
@ H1
continuous field
Definition definitions.h:85
#define MYPCOMM_INDEX
default communicator number PCOMM
@ SIDESET
@ BLOCKSET
#define CHKERR
Inline error check.
constexpr int order
PetscErrorCode DMMoFEMAddElement(DM dm, std::string fe_name)
add element to dm
Definition DMMoFEM.cpp:488
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
PetscErrorCode DMRegister_MoFEM(const char sname[])
Register MoFEM problem.
Definition DMMoFEM.cpp:43
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:576
#define _IT_GET_DOFS_FIELD_BY_NAME_AND_TYPE_FOR_LOOP_(MFIELD, NAME, TYPE, IT)
loop over all dofs from a moFEM field and particular field
virtual MoFEMErrorCode add_finite_element(const std::string &fe_name, enum MoFEMTypes bh=MF_EXCL, int verb=DEFAULT_VERBOSITY)=0
add finite element
virtual MoFEMErrorCode build_finite_elements(int verb=DEFAULT_VERBOSITY)=0
Build finite elements.
virtual MoFEMErrorCode modify_finite_element_add_field_col(const std::string &fe_name, const std::string name_row)=0
set field col which finite element use
virtual MoFEMErrorCode add_ents_to_finite_element_by_type(const EntityHandle entities, const EntityType type, const std::string name, const bool recursive=true)=0
add entities to finite element
virtual MoFEMErrorCode modify_finite_element_add_field_row(const std::string &fe_name, const std::string name_row)=0
set field row which finite element use
virtual MoFEMErrorCode modify_finite_element_add_field_data(const std::string &fe_name, const std::string name_field)=0
set finite element field data
virtual MoFEMErrorCode build_fields(int verb=DEFAULT_VERBOSITY)=0
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.
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.
const double n
refractive index of diffusive medium
static char help[]
const FTensor::Tensor2< T, Dim, Dim > Vec
std::bitset< BITREFLEVEL_SIZE > BitRefLevel
Bit structure attached to each entity identifying to what mesh entity is attached.
Definition Types.hpp:40
OpCalculateHOJacForFaceImpl< 2 > OpCalculateHOJacForFace
constexpr AssemblyType A
OpPostProcMapInMoab< SPACE_DIM, SPACE_DIM > OpPPMap
Add operators pushing bases from local to physical configuration.
Managing BitRefLevels.
Managing BitRefLevels.
virtual MoFEMErrorCode build_adjacencies(const Range &ents, int verb=DEFAULT_VERBOSITY)=0
build adjacencies
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.
virtual MoFEMErrorCode delete_finite_element(const std::string name, int verb=DEFAULT_VERBOSITY)=0
Delete finite element from MoFEM database.
Core (interface) class.
Definition Core.hpp:83
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
Definition Core.cpp:68
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition Core.cpp:123
Deprecated interface functions.
Elastic material data structure.
Interface for managing meshsets containing materials and boundary conditions.
Specialization for double precision scalar field values calculation.
Operator for inverting matrices at integration points.
Post post-proc data at points from hash maps.
std::map< std::string, ScalarDataPtr > DataMapVec
std::map< std::string, boost::shared_ptr< MatrixDouble > > DataMapMat
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.
Vector manager is used to create vectors \mofem_vectors.

Variable Documentation

◆ help

char help[] = "\n"
static

Definition at line 21 of file map_disp_prism.cpp.