37 <<
"Completed " <<
stretchSteps <<
" stretch/reconstruction steps";
61 std::array<double, 3>
scale{2, 1, .75};
68 CHKERR MPI_Allreduce(&local_error, &error, 1, MPI_INT, MPI_MAX,
72 "Mesh stage failed on an MPI rank");
79 char file[PETSC_MAX_PATH_LEN] =
"sphere.py";
81 sizeof(file),
nullptr);
87 "hybrid_order must be positive");
92 "twist_degrees must be finite");
97 "stretch_steps must be positive");
106 for (
int d = 0;
d != 3; ++
d) {
107 const std::string option = std::string(
"-scale_") +
"xyz"[
d];
112 "Scaling must be positive");
150 const int initial_owner = 0;
157 nullptr,
nullptr,
QUIET,
false,
true);
172 const double before =
static_cast<double>(step - 1) /
stretchSteps;
173 const double after =
static_cast<double>(step) /
stretchSteps;
174 std::array<double, 3> old_scale, new_scale;
175 for (
int d = 0;
d != 3; ++
d) {
176 old_scale[
d] = 1. + before * (
scale[
d] - 1.);
177 new_scale[
d] = 1. + after * (
scale[
d] - 1.);
180 const double angle_increment =
182 for (
auto vertex :
currentPtr->entities.subset_by_type(MBVERTEX)) {
183 std::array<double, 3> x;
184 CHKERR mb.get_coords(&vertex, 1, x.data());
185 for (
int d = 0;
d != 3; ++
d)
186 x[
d] /= old_scale[
d];
187 const double angle = angle_increment * x[0];
189 const std::array<double, 3> rotated{x[0],
c * x[1] -
s * x[2],
190 s * x[1] +
c * x[2]};
191 for (
int d = 0;
d != 3; ++
d)
192 x[
d] = new_scale[
d] * rotated[
d];
193 CHKERR mb.set_coords(&vertex, 1, x.data());
199 std::set<std::array<int, 4>> keys;
200 for (
const auto &cell :
mesh.cells) {
201 std::array<int, 4> ids;
202 for (
int d = 0;
d != 4; ++
d)
203 ids[
d] =
mesh.vertexIds[cell[
d]];
204 std::sort(ids.begin(), ids.end());
211 std::map<std::array<int, 3>,
int> incidence;
212 for (
const auto &cell :
mesh.cells)
213 for (
int opposite = 0; opposite != 4; ++opposite) {
214 std::array<int, 3> face;
216 for (
int d = 0;
d != 4; ++
d)
218 face[
n++] =
mesh.vertexIds[cell[
d]];
219 std::sort(face.begin(), face.end());
222 std::set<std::array<int, 3>>
skin;
223 for (
const auto &[face, count] : incidence)
235 auto changes = [](
const auto &before,
const auto &after) {
236 const auto retained =
237 std::count_if(before.begin(), before.end(),
238 [&](
const auto &
key) { return after.count(key); });
239 return std::make_pair(before.size() - retained,
240 after.size() - retained);
245 <<
"Step " << step <<
"/" <<
stretchSteps <<
": tetrahedra removed "
247 <<
"; skin triangles removed " <<
skin.first <<
", added "
293 CHKERR mb.get_entities_by_handle(0, geometry);
294 geometry = subtract(geometry, geometry.subset_by_type(MBENTITYSET));
300 CHKERR mb.tag_clear_data(
field.get_basic_entity_data_ptr()->th_RefBitLevel,
302 const EntityHandle no_parent = 0;
307 const unsigned char unshared = 0;
308 CHKERR mb.tag_clear_data(pc->pstatus_tag(), geometry, &unshared);
309 for (
auto tag : {pc->sharedp_tag(), pc->sharedps_tag(), pc->sharedh_tag(),
310 pc->sharedhs_tag()}) {
311 const auto error =
mb.tag_delete_data(
tag, geometry);
312 if (error != MB_SUCCESS && error != MB_TAG_NOT_FOUND)
315 Range sets = pc->interface_sets();
316 sets.merge(pc->partition_sets());
322 pc =
new ParallelComm(&
mb,
field.get_comm(), &
id);
325 "Unexpected ParallelComm index after resetting sharing");
339 CHKERR mb.get_entities_by_type(0, MBENTITYSET, sets);
340 for (
auto set : sets)
341 CHKERR mb.remove_entities(set, remove);
342 for (
int dim = 3; dim >= 0; --dim) {
343 const auto batch = remove.subset_by_dimension(dim);
353 auto trim_lineage = [&]() {
360 const auto parent_tag =
368 for (
int dim = 2; dim >= 0; --dim) {
370 CHKERR mb.get_adjacencies(needed.subset_by_dimension(dim + 1), dim,
371 false,
closure, moab::Interface::UNION);
390 if (!
mesh.sharingReady) {
392 auto retain = [&]() {
394 if (!
field.get_comm_rank())
396 const auto cells =
mesh.entities.subset_by_type(MBTET);
398 for (
auto cell :
cells) {
406 moab::Interface::UNION);
410 Range owned_faces, face_cells;
411 for (
auto face :
mesh.entities.subset_by_type(MBTRI)) {
415 owned_faces.insert(face);
417 CHKERR mb.get_adjacencies(owned_faces, 3,
false, face_cells,
418 moab::Interface::UNION);
419 halo.merge(face_cells);
421 halo = intersect(halo,
cells);
422 for (
int dim : {0, 1, 2}) {
424 CHKERR mb.get_adjacencies(halo.subset_by_type(MBTET), dim,
false,
425 closure, moab::Interface::UNION);
428 Range protected_entities = preserve;
429 protected_entities.merge(halo);
431 mesh.entities,
mesh.activeBit, protected_entities);
432 mesh.entities = halo;
437 mesh.entities,
mesh.activeBit,
false);
440 mesh.sharingReady =
true;
442 int owned = 0, invalid = 0;
443 for (
auto cell :
mesh.entities.subset_by_type(MBTET)) {
451 int global_owned, root_cells =
mesh.entities.num_of_type(MBTET);
452 CHKERR MPI_Allreduce(&owned, &global_owned, 1, MPI_INT, MPI_SUM,
454 CHKERR MPI_Bcast(&root_cells, 1, MPI_INT, 0,
field.get_comm());
457 CHKERR testing_ptr->checkValue<TestType::SCALAR>(
458 "root retains every owned cell", root_cells, global_owned, zero);
459 CHKERR testing_ptr->checkValue<TestType::SCALAR>(
460 "off-partition residents are ghost cells", invalid, 0, zero);
461 CHKERR pc->check_all_shared_handles();
469 std::vector<std::array<int, 3>> keys;
470 auto find_interior = [&]() {
472 if (
field.get_comm_rank())
474 const auto tets =
mesh.entities.subset_by_type(MBTET);
476 CHKERR mb.get_adjacencies(tets, 2,
true,
faces, moab::Interface::UNION);
477 Skinner skinner(&
mb);
478 CHKERR skinner.find_skin(0, tets,
false,
skin);
479 for (
auto face : subtract(
faces,
skin)) {
480 const EntityHandle *conn;
482 std::array<int, 3> ids;
483 CHKERR mb.get_connectivity(face, conn,
n,
true);
484 CHKERR mb.tag_get_data(
mb.globalId_tag(), conn, 3, ids.data());
485 std::sort(ids.begin(), ids.end());
488 if (keys.size() >
static_cast<size_t>(INT_MAX) / 3)
490 "Skeleton exceeds MPI count range");
494 int count = keys.size();
495 CHKERR MPI_Bcast(&count, 1, MPI_INT, 0,
field.get_comm());
497 CHKERR MPI_Bcast(keys.data(), 3 * count, MPI_INT, 0,
field.get_comm());
498 const std::set<std::array<int, 3>> interior(keys.begin(), keys.end());
500 auto select_skeleton = [&]() {
502 for (
auto face :
mesh.entities.subset_by_type(MBTRI)) {
503 const EntityHandle *conn;
505 std::array<int, 3> ids;
506 CHKERR mb.get_connectivity(face, conn,
n,
true);
507 CHKERR mb.tag_get_data(
mb.globalId_tag(), conn, 3, ids.data());
508 std::sort(ids.begin(), ids.end());
509 if (interior.count(ids))
510 skeleton.insert(face);
518 mesh.registrationChanged =
false;
525 auto &m_field =
mesh.field;
526 const auto &entities =
mesh.entities;
527 std::ostringstream
name;
530 name <<
"_" << std::setw(4) << std::setfill(
'0') << step;
532 const auto file =
name.str();
536 if (!distributed && m_field.get_comm_rank())
538 auto &
mb = m_field.get_moab();
539 EntityHandle selection;
540 CHKERR mb.create_meshset(MESHSET_SET, selection);
541 CHKERR mb.add_entities(selection, entities);
543 CHKERR mb.tag_get_handle(
"FUN3_HYBRID_COUNTS", 5, MB_TYPE_DOUBLE,
544 summary_tag, MB_TAG_SPARSE | MB_TAG_CREAT);
545 CHKERR mb.tag_set_data(summary_tag, &selection, 1,
546 mesh.hybrid.summary.data());
548 auto tag = pc->part_tag();
550 CHKERR mb.get_entities_by_type_and_tag(0, MBENTITYSET, &
tag,
nullptr, 1,
552 CHKERR mb.add_entities(selection, parts);
553 CHKERR mb.write_file(file.c_str(),
"MOAB",
554 distributed ?
"PARALLEL=WRITE_PART" :
"", &selection,
556 CHKERR mb.delete_entities(&selection, 1);
560 MOFEM_LOG(
"WORLD", Sev::inform) <<
"Saved " << file;
565int main(
int argc,
char *argv[]) {
567 &argc, &argv,
nullptr,
568 "Sphere, partition, stretch, twist and reconstruct a mesh\n");
CGALInterfaceImpl & owner
std::vector< std::array< EntityHandle, 3 > > faces
Sorted keys.
bool angle
Radians between candidate face and target plane.
std::vector< int > vertices
std::vector< int > parent
bool closure
The face (f0, f1, w) closes a corner at f0 or f1.
std::vector< Cell > cells
Reusable hybrid algebra and graph validation for FUN-3.
#define CATCH_ERRORS
Catch errors.
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#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 ...
@ MOFEM_DATA_INCONSISTENCY
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
PetscErrorCode DMRegister_MoFEM(const char sname[])
Register MoFEM problem.
#define MOFEM_LOG(channel, severity)
Log.
const double c
speed of light (cm/ns)
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
std::bitset< BITREFLEVEL_SIZE > BitRefLevel
Bit structure attached to each entity identifying to what mesh entity is attached.
implementation of Data Operators for Forces and Sources
PetscErrorCode PetscOptionsGetInt(PetscOptions *, const char pre[], const char name[], PetscInt *ivalue, PetscBool *set)
PetscErrorCode PetscOptionsGetReal(PetscOptions *, const char pre[], const char name[], PetscReal *dval, PetscBool *set)
PetscErrorCode PetscOptionsGetString(PetscOptions *, const char pre[], const char name[], char str[], size_t size, PetscBool *set)
Fun3::HybridProblem hybrid
std::unique_ptr< Mesh > currentPtr
TetMeshData reconstructed
MoFEMErrorCode rebuildHybrid(Mesh &mesh, const std::string &tag)
MoFEMErrorCode deleteGeometry(const Range &remove)
MoFEMErrorCode generateSphere()
CGALTools::ImplicitMeshSettings settings
MoFEMErrorCode distributeSphere()
MoFEMErrorCode reconstructOnRoot(PetscInt step)
static auto skinKeys(const TetMeshData &mesh)
MoFEMErrorCode reconstructMesh(PetscInt step)
MoFEMErrorCode saveStage(Mesh &mesh, const char *stage, bool distributed, PetscInt step=0)
static auto cellKeys(const TetMeshData &mesh)
MoFEMErrorCode readOptions()
MoFEMErrorCode synchroniseMesh()
MoFEMErrorCode resetMeshForImport()
MoFEMErrorCode deformMesh(PetscInt step)
MoFEMErrorCode partitionSphere()
MoFEMErrorCode agree(MoFEMErrorCode local_error)
MoFEMErrorCode prepareDistributed(Mesh &mesh, const Range &preserve={})
std::array< double, 3 > scale
Retain the parent/sub-DMs, field DOFs and block storage across mesh edits.
Tag get_th_RefParentHandle() const
virtual CGALTools & getTools()=0
virtual MoFEMErrorCode importMesh(const TetMeshData &data, BitRefLevel bit, Range &entities, EntityHandle &volume, EntityHandle &surface)=0
virtual MoFEMErrorCode reconstructDelaunay(const Range &source_entities, BitRefLevel output_bit, Range &entities, CGALTools::ReconstructionMethod method=CGALTools::ReconstructionMethod::PointDelaunay)=0
MoFEMErrorCode inheritPartitions(const Range &source_entities, const Range &child_entities)
MoFEMErrorCode gatherMesh(const Range &cells, TetMeshData &data, int root=0)
MoFEMErrorCode broadcastMesh(TetMeshData &data, int root=0)
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Deprecated interface functions.
Mesh refinement interface.
MoFEMErrorCode setTopologicalParents(const Range &source_entities, const Range &child_entities, const BitRefLevel &output_bit, int verb=QUIET, const std::map< EntityHandle, EntityHandle > &moved_vertices={})
static MoFEMErrorCode initialise()
static MoFEMErrorCode finalise()
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.