v0.14.0
Loading...
Searching...
No Matches
field_approximation.cpp File Reference

Go to the source code of this file.

Classes

struct  MyFunApprox
 Example approx. function. More...
 

Macros

#define HOON
 

Functions

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

Variables

static char help [] = "...\n\n"
 

Macro Definition Documentation

◆ HOON

#define HOON

Definition at line 9 of file field_approximation.cpp.

Function Documentation

◆ main()

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

Definition at line 28 of file field_approximation.cpp.

28 {
29
30 MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
31
32 try {
33
34 moab::Core mb_instance;
35 moab::Interface &moab = mb_instance;
36 int rank;
37 MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
38
39 PetscBool flg = PETSC_TRUE;
40 char mesh_file_name[255];
41 CHKERR PetscOptionsGetString(PETSC_NULL, PETSC_NULL, "-my_file",
42 mesh_file_name, 255, &flg);
43 if (flg != PETSC_TRUE) {
44 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
45 "*** ERROR -my_file (MESH FILE NEEDED)");
46 }
47
48 const char *option;
49 option = "";
50 CHKERR moab.load_file(mesh_file_name, 0, option);
51
52 // Create MoFEM (Joseph) database
53 MoFEM::Core core(moab);
54 MoFEM::Interface &m_field = core;
55
56 // set entities bit level
57 BitRefLevel bit_level0;
58 bit_level0.set(0);
59 EntityHandle meshset_level0;
60 CHKERR moab.create_meshset(MESHSET_SET, meshset_level0);
61 CHKERR m_field.getInterface<BitRefManager>()->setBitRefLevelByDim(
62 0, 3, bit_level0);
63
64 // Fields
65 CHKERR m_field.add_field("FIELD1", H1, AINSWORTH_LEGENDRE_BASE, 3);
66#ifdef HOON
67 CHKERR m_field.add_field("MESH_NODE_POSITIONS", H1, AINSWORTH_LEGENDRE_BASE,
68 3, MB_TAG_SPARSE, MF_ZERO);
69#endif
70
71 // FE
72 CHKERR m_field.add_finite_element("TEST_FE");
73
74 // Define rows/cols and element data
75 CHKERR m_field.modify_finite_element_add_field_row("TEST_FE", "FIELD1");
76 CHKERR m_field.modify_finite_element_add_field_col("TEST_FE", "FIELD1");
77 CHKERR m_field.modify_finite_element_add_field_data("TEST_FE", "FIELD1");
78#ifdef HOON
80 "MESH_NODE_POSITIONS");
81#endif
82
83 // Problem
84 CHKERR m_field.add_problem("TEST_PROBLEM");
85
86 // set finite elements for problem
87 CHKERR m_field.modify_problem_add_finite_element("TEST_PROBLEM", "TEST_FE");
88 // set refinement level for problem
89 CHKERR m_field.modify_problem_ref_level_add_bit("TEST_PROBLEM", bit_level0);
90
91 // meshset consisting all entities in mesh
92 EntityHandle root_set = moab.get_root_set();
93 // add entities to field
94 CHKERR m_field.add_ents_to_field_by_type(root_set, MBTET, "FIELD1");
95#ifdef HOON
96 CHKERR m_field.add_ents_to_field_by_type(root_set, MBTET,
97 "MESH_NODE_POSITIONS");
98#endif
99 // add entities to finite element
100 CHKERR
101 m_field.add_ents_to_finite_element_by_type(root_set, MBTET, "TEST_FE");
102
103 // set app. order
104 // see Hierarchic Finite Element Bases on Unstructured Tetrahedral Meshes
105 // (Mark Ainsworth & Joe Coyle)
106 int order = 3;
107 CHKERR
108 PetscOptionsGetInt(PETSC_NULL, PETSC_NULL, "-my_order", &order, &flg);
109 if (flg != PETSC_TRUE) {
110 order = 3;
111 }
112 CHKERR m_field.set_field_order(root_set, MBTET, "FIELD1", order);
113 CHKERR m_field.set_field_order(root_set, MBTRI, "FIELD1", order);
114 CHKERR m_field.set_field_order(root_set, MBEDGE, "FIELD1", order);
115 CHKERR m_field.set_field_order(root_set, MBVERTEX, "FIELD1", 1);
116#ifdef HOON
117 CHKERR m_field.set_field_order(root_set, MBTET, "MESH_NODE_POSITIONS", 2);
118 CHKERR m_field.set_field_order(root_set, MBTRI, "MESH_NODE_POSITIONS", 2);
119 CHKERR m_field.set_field_order(root_set, MBEDGE, "MESH_NODE_POSITIONS", 2);
120 CHKERR
121 m_field.set_field_order(root_set, MBVERTEX, "MESH_NODE_POSITIONS", 1);
122#endif
123
124 /****/
125 // build database
126 // build field
127 CHKERR m_field.build_fields();
128#ifdef HOON
129 Projection10NodeCoordsOnField ent_method_material(m_field,
130 "MESH_NODE_POSITIONS");
131 CHKERR m_field.loop_dofs("MESH_NODE_POSITIONS", ent_method_material);
132#endif
133 // build finite elemnts
135 // build adjacencies
136 CHKERR m_field.build_adjacencies(bit_level0);
137 // build problem
138 ProblemsManager *prb_mng_ptr;
139 CHKERR m_field.getInterface(prb_mng_ptr);
140 CHKERR prb_mng_ptr->buildProblem("TEST_PROBLEM", true);
141
142 /****/
143 // mesh partitioning
144 // partition
145 CHKERR prb_mng_ptr->partitionSimpleProblem("TEST_PROBLEM");
146 CHKERR prb_mng_ptr->partitionFiniteElements("TEST_PROBLEM");
147 // what are ghost nodes, see Petsc Manual
148 CHKERR prb_mng_ptr->partitionGhostDofs("TEST_PROBLEM");
149
150 Mat A;
151 CHKERR
152 m_field.getInterface<MatrixManager>()
153 ->createMPIAIJWithArrays<PetscGlobalIdx_mi_tag>("TEST_PROBLEM", &A);
154 Vec D, F;
155 CHKERR m_field.getInterface<VecManager>()->vecCreateGhost("TEST_PROBLEM",
156 ROW, &F);
157 CHKERR m_field.getInterface<VecManager>()->vecCreateGhost("TEST_PROBLEM",
158 COL, &D);
159
160 std::vector<Vec> vec_F;
161 vec_F.push_back(F);
162
163 {
164 MyFunApprox function_evaluator;
165 FieldApproximationH1 field_approximation(m_field);
166 field_approximation.loopMatrixAndVectorVolume(
167 "TEST_PROBLEM", "TEST_FE", "FIELD1", A, vec_F, function_evaluator);
168 }
169
170 CHKERR MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
171 CHKERR MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);
172 CHKERR VecGhostUpdateBegin(F, ADD_VALUES, SCATTER_REVERSE);
173 CHKERR VecGhostUpdateEnd(F, ADD_VALUES, SCATTER_REVERSE);
174 CHKERR VecGhostUpdateBegin(F, INSERT_VALUES, SCATTER_FORWARD);
175 CHKERR VecGhostUpdateEnd(F, INSERT_VALUES, SCATTER_FORWARD);
176
177 KSP solver;
178 CHKERR KSPCreate(PETSC_COMM_WORLD, &solver);
179 CHKERR KSPSetOperators(solver, A, A);
180
181 CHKERR PetscOptionsInsertString(NULL,
182 "-ksp_monitor -ksp_type fgmres -pc_type "
183 "bjacobi -ksp_atol 0 -ksp_rtol 1e-12");
184
185 CHKERR KSPSetFromOptions(solver);
186 CHKERR PetscOptionsView(NULL, PETSC_VIEWER_STDOUT_WORLD);
187 CHKERR KSPSetUp(solver);
188 CHKERR KSPSolve(solver, F, D);
189 CHKERR VecGhostUpdateBegin(D, INSERT_VALUES, SCATTER_FORWARD);
190 CHKERR VecGhostUpdateEnd(D, INSERT_VALUES, SCATTER_FORWARD);
191
192 CHKERR m_field.getInterface<VecManager>()->setGlobalGhostVector(
193 "TEST_PROBLEM", COL, D, INSERT_VALUES, SCATTER_REVERSE);
194
195 CHKERR KSPDestroy(&solver);
196 CHKERR VecDestroy(&D);
197 CHKERR VecDestroy(&F);
198 CHKERR MatDestroy(&A);
199
200 EntityHandle fe_meshset = m_field.get_finite_element_meshset("TEST_FE");
201 Range tets;
202 CHKERR moab.get_entities_by_type(fe_meshset, MBTET, tets, true);
203 Range tets_edges;
204 CHKERR moab.get_adjacencies(tets, 1, false, tets_edges,
205 moab::Interface::UNION);
206 EntityHandle edges_meshset;
207 CHKERR moab.create_meshset(MESHSET_SET, edges_meshset);
208 CHKERR moab.add_entities(edges_meshset, tets);
209 CHKERR moab.add_entities(edges_meshset, tets_edges);
210 CHKERR moab.convert_entities(edges_meshset, true, false, false);
211
212 ProjectionFieldOn10NodeTet ent_method_field1_on_10nodeTet(
213 m_field, "FIELD1", true, false, "FIELD1");
214 CHKERR m_field.loop_dofs("FIELD1", ent_method_field1_on_10nodeTet);
215 ent_method_field1_on_10nodeTet.setNodes = false;
216 CHKERR m_field.loop_dofs("FIELD1", ent_method_field1_on_10nodeTet);
217
218 // if (pcomm->rank() == 0) {
219 // EntityHandle out_meshset;
220 // CHKERR moab.create_meshset(MESHSET_SET, out_meshset);
221 // CHKERR m_field.get_problem_finite_elements_entities(
222 // "TEST_PROBLEM", "TEST_FE", out_meshset);
223 // CHKERR moab.write_file("out.vtk", "VTK", "", &out_meshset, 1);
224 // CHKERR moab.delete_entities(&out_meshset, 1);
225 // }
226
227 typedef tee_device<std::ostream, std::ofstream> TeeDevice;
228 typedef stream<TeeDevice> TeeStream;
229
230 std::ofstream ofs("field_approximation.txt");
231 TeeDevice tee(cout, ofs);
232 TeeStream my_split(tee);
233
234 Range nodes;
235 CHKERR moab.get_entities_by_type(0, MBVERTEX, nodes, true);
236 MatrixDouble nodes_vals;
237 nodes_vals.resize(nodes.size(), 3);
238 CHKERR moab.tag_get_data(ent_method_field1_on_10nodeTet.th, nodes,
239 &*nodes_vals.data().begin());
240
241 const double eps = 1e-4;
242
243 my_split.precision(3);
244 my_split.setf(std::ios::fixed);
245 for (DoubleAllocator::iterator it = nodes_vals.data().begin();
246 it != nodes_vals.data().end(); it++) {
247 *it = fabs(*it) < eps ? 0.0 : *it;
248 }
249 my_split << nodes_vals << std::endl;
250
251 const Problem *problemPtr;
252 CHKERR m_field.get_problem("TEST_PROBLEM", &problemPtr);
253 std::map<EntityHandle, double> m0, m1, m2;
254 for (_IT_NUMEREDDOF_ROW_FOR_LOOP_(problemPtr, dit)) {
255
256 my_split.precision(3);
257 my_split.setf(std::ios::fixed);
258 double val = fabs(dit->get()->getFieldData()) < eps
259 ? 0.0
260 : dit->get()->getFieldData();
261 my_split << dit->get()->getPetscGlobalDofIdx() << " " << val << std::endl;
262 }
263 }
265
267
268 return 0;
269}
static const double eps
@ COL
@ ROW
#define CATCH_ERRORS
Catch errors.
@ MF_ZERO
@ AINSWORTH_LEGENDRE_BASE
Ainsworth Cole (Legendre) approx. base nme:nme847.
Definition definitions.h:60
@ H1
continuous field
Definition definitions.h:85
@ MOFEM_INVALID_DATA
Definition definitions.h:36
#define CHKERR
Inline error check.
constexpr int order
static char help[]
tee_device< std::ostream, std::ofstream > TeeDevice
@ F
virtual const Problem * get_problem(const std::string problem_name) const =0
Get the problem object.
virtual MoFEMErrorCode add_finite_element(const std::string &fe_name, enum MoFEMTypes bh=MF_EXCL, int verb=DEFAULT_VERBOSITY)=0
add finite element
virtual EntityHandle get_finite_element_meshset(const std::string name) const =0
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.
virtual MoFEMErrorCode loop_dofs(const Problem *problem_ptr, const std::string &field_name, RowColData rc, DofMethod &method, int lower_rank, int upper_rank, int verb=DEFAULT_VERBOSITY)=0
Make a loop over dofs.
MoFEMErrorCode partitionGhostDofs(const std::string name, int verb=VERBOSE)
determine ghost nodes
MoFEMErrorCode partitionSimpleProblem(const std::string name, int verb=VERBOSE)
partition problem dofs
MoFEMErrorCode buildProblem(const std::string name, const bool square_matrix, int verb=VERBOSE)
build problem data structures
MoFEMErrorCode partitionFiniteElements(const std::string name, bool part_from_moab=false, int low_proc=-1, int hi_proc=-1, int verb=VERBOSE)
partition finite elements
virtual MoFEMErrorCode add_problem(const std::string &name, enum MoFEMTypes bh=MF_EXCL, int verb=DEFAULT_VERBOSITY)=0
Add problem.
virtual MoFEMErrorCode modify_problem_ref_level_add_bit(const std::string &name_problem, const BitRefLevel &bit)=0
add ref level to problem
virtual MoFEMErrorCode modify_problem_add_finite_element(const std::string name_problem, const std::string &fe_name)=0
add finite element to problem, this add entities assigned to finite element to a particular problem
#define _IT_NUMEREDDOF_ROW_FOR_LOOP_(PROBLEMPTR, IT)
use with loops to iterate row DOFs
double D
char mesh_file_name[255]
std::bitset< BITREFLEVEL_SIZE > BitRefLevel
Bit structure attached to each entity identifying to what mesh entity is attached.
Definition Types.hpp:40
PetscErrorCode PetscOptionsGetInt(PetscOptions *, const char pre[], const char name[], PetscInt *ivalue, PetscBool *set)
PetscErrorCode PetscOptionsGetString(PetscOptions *, const char pre[], const char name[], char str[], size_t size, PetscBool *set)
constexpr AssemblyType A
Finite element for approximating analytical filed on the mesh.
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.
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.
Matrix manager is used to build and partition problems.
keeps basic data about problem
Problem manager is used to build and partition problems.
Projection of edge entities with one mid-node on hierarchical basis.
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.
Vector manager is used to create vectors \mofem_vectors.
Example approx. function.

Variable Documentation

◆ help

char help[] = "...\n\n"
static

Definition at line 11 of file field_approximation.cpp.