v0.15.0
Loading...
Searching...
No Matches
mesh_cut.cpp
Go to the documentation of this file.
1/** \file mesh_cut.cpp
2 * \brief test for mesh cut interface
3 * \example mesh_cut.cpp
4 *
5 * \ingroup mesh_cut
6 */
7
8
9
10#include <MoFEM.hpp>
11
12using namespace MoFEM;
13
14static char help[] = "mesh cutting\n\n";
15
16int main(int argc, char *argv[]) {
17
18 MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
19
20 try {
21
22 PetscBool flg_myfile = PETSC_TRUE;
23 char mesh_file_name[255];
24 int surface_side_set = 200;
25 PetscBool flg_vol_block_set;
26 int vol_block_set = 1;
27 int edges_block_set = 1;
28 int vertex_block_set = 2;
29 PetscBool flg_shift;
30 double shift[] = {0, 0, 0};
31 int nmax = 3;
32 int fraction_level = 2;
33 PetscBool squash_bits = PETSC_TRUE;
34 PetscBool set_coords = PETSC_TRUE;
35 PetscBool output_vtk = PETSC_TRUE;
36 int create_surface_side_set = 201;
37 PetscBool flg_create_surface_side_set;
38 int nb_ref_cut = 0;
39 int nb_ref_trim = 0;
40 PetscBool flg_tol;
41 double tol[] = {1e-2, 2e-1, 2e-1};
42 int nmax_tol = 3;
43 PetscBool debug = PETSC_FALSE;
44
45 PetscOptionsBegin(PETSC_COMM_WORLD, "", "Mesh cut options", "none");
46 CHKERR PetscOptionsString("-my_file", "mesh file name", "", "mesh.h5m",
48 CHKERR PetscOptionsInt("-surface_side_set", "surface side set", "",
49 surface_side_set, &surface_side_set, PETSC_NULLPTR);
50 CHKERR PetscOptionsInt("-vol_block_set", "volume side set", "",
51 vol_block_set, &vol_block_set, &flg_vol_block_set);
52 CHKERR PetscOptionsInt("-edges_block_set", "edges block set", "",
53 edges_block_set, &edges_block_set, PETSC_NULLPTR);
54 CHKERR PetscOptionsInt("-vertex_block_set", "vertex block set", "",
55 vertex_block_set, &vertex_block_set, PETSC_NULLPTR);
56 CHKERR PetscOptionsRealArray("-shift", "shift surface by vector", "", shift,
57 &nmax, &flg_shift);
58 CHKERR PetscOptionsInt("-fraction_level", "fraction of merges merged", "",
59 fraction_level, &fraction_level, PETSC_NULLPTR);
60 CHKERR PetscOptionsBool("-squash_bits", "true to squash bits at the end",
61 "", squash_bits, &squash_bits, PETSC_NULLPTR);
62 CHKERR PetscOptionsBool("-set_coords", "true to set coords at the end", "",
63 set_coords, &set_coords, PETSC_NULLPTR);
64 CHKERR PetscOptionsBool("-output_vtk", "if true outout vtk file", "",
65 output_vtk, &output_vtk, PETSC_NULLPTR);
66 CHKERR PetscOptionsInt("-create_side_set", "crete side set", "",
67 create_surface_side_set, &create_surface_side_set,
68 &flg_create_surface_side_set);
69 CHKERR PetscOptionsInt("-nb_ref_cut", "nb refinements befor cut", "",
70 nb_ref_cut, &nb_ref_cut, PETSC_NULLPTR);
71 CHKERR PetscOptionsInt("-nb_ref_trim", "nb refinements befor trim", "",
72 nb_ref_trim, &nb_ref_trim, PETSC_NULLPTR);
73 CHKERR PetscOptionsRealArray(
74 "-tol", "tolerances tolCut, tolCutClose, tolTrim, tolTrimClose", "",
75 tol, &nmax_tol, &flg_tol);
76 CHKERR PetscOptionsBool("-debug", "debug (produces many VTK files)", "",
77 debug, &debug, PETSC_NULLPTR);
78 PetscOptionsEnd();
79
80 if (flg_myfile != PETSC_TRUE) {
81 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
82 "*** ERROR -my_file (MESH FILE NEEDED)");
83 }
84 if (flg_shift && nmax != 3) {
85 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA, "three values expected");
86 }
87
88 if (flg_tol && nmax_tol != 3)
89 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA, "four values expected");
90
91 moab::Core mb_instance;
92 moab::Interface &moab = mb_instance;
93 ParallelComm *pcomm = ParallelComm::get_pcomm(&moab, MYPCOMM_INDEX);
94 if (pcomm == NULL)
95 pcomm = new ParallelComm(&moab, PETSC_COMM_WORLD);
96
97 const char *option;
98 option = "";
99 CHKERR moab.load_file(mesh_file_name, 0, option);
100
101 MoFEM::Core core(moab);
102 MoFEM::CoreInterface &m_field =
104
105 // get cut mesh interface
106 CutMeshInterface *cut_mesh;
107 CHKERR m_field.getInterface(cut_mesh);
108 // get meshset manager interface
109 MeshsetsManager *meshset_manager;
110 CHKERR m_field.getInterface(meshset_manager);
111 // get bit ref manager interface
112 BitRefManager *bit_ref_manager;
113 CHKERR m_field.getInterface(bit_ref_manager);
114
116 (core.getInterface<MeshsetsManager &, 0>()), SIDESET, it)) {
117 cout << *it << endl;
118 }
119
120 CHKERR bit_ref_manager->setBitRefLevelByType(0, MBTET,
121 BitRefLevel().set(0));
122
123 // get surface entities form side set
125 if (meshset_manager->checkMeshset(surface_side_set, SIDESET))
126 CHKERR meshset_manager->getEntitiesByDimension(surface_side_set, SIDESET,
127 2, surface, true);
128
129 if (surface.empty())
130 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID, "No surface to cut");
131
132 // Set surface entities. If surface entities are from existing side set,
133 // copy those entities and do other geometrical transformations, like shift
134 // scale or Stretch, rotate.
135 if (meshset_manager->checkMeshset(surface_side_set, SIDESET))
136 CHKERR cut_mesh->copySurface(surface, NULL, shift);
137 else
138 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
139 "Side set not found %d", surface_side_set);
140
141 // Get geometric corner nodes and corner edges
142 Range fixed_edges, corner_nodes;
143 if (meshset_manager->checkMeshset(edges_block_set, BLOCKSET))
145 1, fixed_edges, true);
146 if (meshset_manager->checkMeshset(edges_block_set, SIDESET))
148 1, fixed_edges, true);
149 if (meshset_manager->checkMeshset(vertex_block_set, BLOCKSET))
150 CHKERR meshset_manager->getEntitiesByDimension(
151 vertex_block_set, BLOCKSET, 0, corner_nodes, true);
152
153
154 Range tets;
155 if (flg_vol_block_set) {
156 if (meshset_manager->checkMeshset(vol_block_set, BLOCKSET))
157 CHKERR meshset_manager->getEntitiesByDimension(vol_block_set, BLOCKSET,
158 3, tets, true);
159 else
160 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
161 "Block set %d not found", vol_block_set);
162 } else
163 CHKERR moab.get_entities_by_dimension(0, 3, tets, false);
164
165 CHKERR cut_mesh->setVolume(tets);
166 CHKERR cut_mesh->makeFront(true);
167
168 // Build tree, it is used to ask geometrical queries, i.e. to find edges to
169 // cut or trim.
170 CHKERR cut_mesh->buildTree();
171
172 // Refine mesh
173 CHKERR cut_mesh->refineMesh(0, nb_ref_cut, nb_ref_trim, &fixed_edges,
174 VERBOSE, false);
175 auto shift_after_ref = [&]() {
177 BitRefLevel mask;
178 mask.set(0);
179 for (int ll = 1; ll != nb_ref_cut + nb_ref_trim + 1; ++ll)
180 mask.set(ll);
181 CHKERR core.getInterface<BitRefManager>()->shiftRightBitRef(
182 nb_ref_cut + nb_ref_trim, mask, VERBOSE);
184 };
185 CHKERR shift_after_ref();
186
187 // Create tag storing nodal positions
188 double def_position[] = {0, 0, 0};
189 Tag th;
190 CHKERR moab.tag_get_handle("POSITION", 3, MB_TYPE_DOUBLE, th,
191 MB_TAG_CREAT | MB_TAG_SPARSE, def_position);
192 // Set tag values with coordinates of nodes
193 CHKERR cut_mesh->setTagData(th);
194
195 // Cut mesh, trim surface and merge bad edges
196 int first_bit = 1;
197 CHKERR cut_mesh->cutTrimAndMerge(first_bit, fraction_level, th, tol[0],
198 tol[1], tol[2], fixed_edges, corner_nodes,
199 true, false);
200
201 // Set coordinates for tag data
202 if (set_coords)
203 CHKERR cut_mesh->setCoords(th);
204
205 // Add tets from last level to block
206 if (flg_vol_block_set) {
207 EntityHandle meshset;
208 CHKERR meshset_manager->getMeshset(vol_block_set, BLOCKSET, meshset);
209 CHKERR bit_ref_manager->getEntitiesByTypeAndRefLevel(
210 BitRefLevel().set(first_bit), BitRefLevel().set(), MBTET, meshset);
211 }
212
213 // Shift bits
214 if (squash_bits) {
215 BitRefLevel shift_mask;
216 for (int ll = 0; ll != first_bit; ++ll)
217 shift_mask.set(ll);
218 CHKERR core.getInterface<BitRefManager>()->shiftRightBitRef(
219 first_bit - 1, shift_mask, VERBOSE);
220 }
221
222 Range surface_verts;
223 CHKERR moab.get_connectivity(cut_mesh->getSurface(), surface_verts);
224 Range surface_edges;
225 CHKERR moab.get_adjacencies(cut_mesh->getSurface(), 1, false, surface_edges,
226 moab::Interface::UNION);
227 CHKERR moab.delete_entities(cut_mesh->getSurface());
228 CHKERR moab.delete_entities(surface_edges);
229 CHKERR moab.delete_entities(surface_verts);
230
231 if (flg_create_surface_side_set) {
232 // Check is meshset is there
234 create_surface_side_set, SIDESET))
235 CHKERR meshset_manager->addMeshset(SIDESET, create_surface_side_set);
236 else
237 MOFEM_LOG_C("WORLD", Sev::warning,
238 "Warring >>> sideset %d is on the mesh",
239 create_surface_side_set);
240
241 CHKERR meshset_manager->addEntitiesToMeshset(
242 SIDESET, create_surface_side_set, cut_mesh->getMergedSurfaces());
243 }
244
245 CHKERR moab.write_file("out.h5m");
246
247 if (output_vtk) {
248 EntityHandle meshset;
249 CHKERR moab.create_meshset(MESHSET_SET, meshset);
250 if (flg_vol_block_set) {
251 Range ents;
252 meshset_manager->getEntitiesByDimension(vol_block_set, BLOCKSET, 3,
253 ents, true);
254 CHKERR moab.add_entities(meshset, ents);
255 } else {
256 BitRefLevel bit = BitRefLevel().set(0);
257 CHKERR bit_ref_manager->getEntitiesByTypeAndRefLevel(
258 bit, BitRefLevel().set(), MBTET, meshset);
259 }
260 CHKERR moab.add_entities(meshset, cut_mesh->getMergedSurfaces());
261 CHKERR moab.write_file("out.vtk", "VTK", "", &meshset, 1);
262 CHKERR moab.delete_entities(&meshset, 1);
263 }
264 }
266
268
269 return 0;
270}
#define MOFEM_LOG_C(channel, severity, format,...)
int main()
@ VERBOSE
#define CATCH_ERRORS
Catch errors.
#define MYPCOMM_INDEX
default communicator number PCOMM
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ SIDESET
@ BLOCKSET
@ MOFEM_ATOM_TEST_INVALID
Definition definitions.h:40
@ MOFEM_DATA_INCONSISTENCY
Definition definitions.h:31
@ MOFEM_INVALID_DATA
Definition definitions.h:36
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
MoFEMErrorCode setBitRefLevelByType(const EntityHandle meshset, const EntityType type, const BitRefLevel bit, int verb=QUIET) const
Set the Bit Ref Level By Type object.
MoFEMErrorCode getEntitiesByTypeAndRefLevel(const BitRefLevel bit, const BitRefLevel mask, const EntityType type, const EntityHandle meshset, int verb=0) const
add all ents from ref level given by bit to meshset
MoFEMErrorCode getMeshset(const int ms_id, const unsigned int cubit_bc_type, EntityHandle &meshset) const
get meshset from CUBIT Id and CUBIT type
bool checkMeshset(const int ms_id, const CubitBCType cubit_bc_type) const
check for CUBIT Id and CUBIT type
MoFEMErrorCode addMeshset(const CubitBCType cubit_bc_type, const int ms_id, const std::string name="")
add cubit meshset
MoFEMErrorCode getEntitiesByDimension(const int ms_id, const unsigned int cubit_bc_type, const int dimension, Range &entities, const bool recursive=true) const
get entities from CUBIT/meshset of a particular entity dimension
#define _IT_CUBITMESHSETS_BY_SET_TYPE_FOR_LOOP_(MESHSET_MANAGER, CUBITBCTYPE, IT)
Iterator that loops over a specific Cubit MeshSet having a particular BC meshset in a moFEM field.
auto bit
set bit
static char help[]
Definition mesh_cut.cpp:14
double tol
PetscBool output_vtk
PetscBool flg_myfile
int vertex_block_set
int edges_block_set
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
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
static const bool debug
Managing BitRefLevels.
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:118
Interface to cut meshes.
MoFEMErrorCode copySurface(const Range surface, Tag th=NULL, double *shift=NULL, double *origin=NULL, double *transform=NULL, const std::string save_mesh="")
copy surface entities
MoFEMErrorCode setCoords(Tag th, const BitRefLevel bit=BitRefLevel(), const BitRefLevel mask=BitRefLevel().set())
set coords from tag
const Range & getMergedSurfaces() const
MoFEMErrorCode buildTree()
build tree
const Range & getSurface() const
MoFEMErrorCode setVolume(const Range volume)
set volume entities
MoFEMErrorCode setTagData(Tag th, const BitRefLevel bit=BitRefLevel())
set coords to tag
MoFEMErrorCode cutTrimAndMerge(int &first_bit, const int fraction_level, Tag th, const double tol_geometry, const double tol_cut_close, const double tol_trim_close, Range &fixed_edges, Range &corner_nodes, const bool update_meshsets=false, const bool debug=false)
Cut, trim and merge.
MoFEMErrorCode makeFront(const bool debug=false)
Create front from the surface.
MoFEMErrorCode refineMesh(const int init_bit_level, const int surf_levels, const int front_levels, Range *fixed_edges=nullptr, int verb=QUIET, const bool debug=false)
Refine and set level sets.
Interface for managing meshsets containing materials and boundary conditions.
MoFEMErrorCode addEntitiesToMeshset(const CubitBCType cubit_bc_type, const int ms_id, const Range &ents)
add entities to cubit meshset
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.