v0.16.3
Loading...
Searching...
No Matches
comm_reconstruction.cpp
Go to the documentation of this file.
1/** \file comm_reconstruction.cpp
2 * \brief Two-to-three cell flip, representative lineage and rebuilt sharing.
3 */
4#include <MoFEM.hpp>
5using namespace MoFEM;
6
7namespace {
11 auto &mb = m_field.get_moab();
12 auto pc = ParallelComm::get_pcomm(&mb, MYPCOMM_INDEX);
13 auto bridge = m_field.getInterface<CGALInterface>();
14 auto comm = m_field.getInterface<CommInterface>();
15 const int last = m_field.get_comm_size() - 1;
17 source.points = {{-1, 0, 0}, {1, 0, 0}, {0, 1, 0},
18 {.1, .25, 1}, {.1, .25, -1}, {0, -1, -.5}};
19 source.vertexIds = {1, 2, 3, 4, 5, 6};
20 source.cells = {{0, 1, 2, 3}, {0, 1, 2, 4}, {0, 1, 4, 5}};
21 source.partitions = {0, last, last};
22 if (m_field.get_comm_rank() % 2) {
23 std::reverse(source.points.begin(), source.points.end());
24 std::reverse(source.vertexIds.begin(), source.vertexIds.end());
25 for (auto &c : source.cells)
26 for (auto &i : c)
27 i = 5 - i;
28 std::reverse(source.cells.begin(), source.cells.end());
29 std::reverse(source.partitions.begin(), source.partitions.end());
30 }
31 Range old_entities, children;
32 EntityHandle volume, surface;
33 CHKERR bridge->importMesh(source, BitRefLevel().set(0), old_entities, volume,
34 surface);
35 TetMeshData output;
36 output.points = {{-1, 0, 0}, {1, 0, 0}, {0, 1, 0},
37 {.1, .25, 1}, {.1, .25, -1}, {0, -1, -.5}};
38 output.vertexIds = {1, 2, 3, 4, 5, 6};
39 output.cells = {{0, 1, 3, 4}, {1, 2, 3, 4}, {2, 0, 3, 4}, {0, 1, 4, 5}};
40 CHKERR bridge->importMesh(output, BitRefLevel().set(1), children, volume,
41 surface);
42 CHKERR m_field.getInterface<MeshRefinement>()->setTopologicalParents(
43 old_entities, children, BitRefLevel().set(1));
44 auto parent_tag =
45 m_field.getInterface<BitRefManager>()->get_th_RefParentHandle();
46 for (auto child : subtract(children, old_entities).subset_by_type(MBTET)) {
47 EntityHandle parent;
48 CHKERR mb.tag_get_data(parent_tag, &child, 1, &parent);
49 const EntityHandle *conn;
50 int count;
51 std::array<int, 4> ids;
52 CHKERR mb.get_connectivity(parent, conn, count, true);
53 CHKERR mb.tag_get_data(mb.globalId_tag(), conn, 4, ids.data());
54 std::sort(ids.begin(), ids.end());
55 if (ids != std::array<int, 4>{1, 2, 3, 4})
56 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
57 "Flip parent depends on local creation order");
58 auto &index = m_field.get_ref_ents()->get<Ent_Ent_mi_tag>();
59 auto range = index.equal_range(parent);
60 if (std::none_of(range.first, range.second,
61 [&](const auto &p) { return p->getEnt() == child; }))
62 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
63 "Parent index is stale");
64 }
65 CHKERR comm->inheritPartitions(old_entities, children);
66 CHKERR comm->retainPartition(children, BitRefLevel().set(1), old_entities);
68 CHKERR m_field.getInterface<BitRefManager>()->getEntitiesByRefLevel(
69 BitRefLevel().set(1), BitRefLevel().set(), active);
70 if (active != children)
71 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
72 "Inactive source copies retain output bit");
73 // Copies intentionally carry different geometry. Topological matching must
74 // leave it alone; collection must later select the owning rank's values.
75 std::map<EntityHandle, std::array<double, 3>> coordinates;
76 for (auto vertex : children.subset_by_type(MBVERTEX)) {
77 auto &x = coordinates[vertex];
78 CHKERR mb.get_coords(&vertex, 1, x.data());
79 x[0] += .01 * m_field.get_comm_rank();
80 CHKERR mb.set_coords(&vertex, 1, x.data());
81 }
82 TetMeshData unregistered;
83 CHKERR comm->gatherMesh(children.subset_by_type(MBTET), unregistered, last);
84 if (m_field.get_comm_rank() == last)
85 for (size_t i = 0; i != unregistered.vertexIds.size(); ++i) {
86 const auto id = unregistered.vertexIds[i];
87 const int producer = id == 6 ? last : 0;
88 auto expected = output.points[id - 1];
89 expected[0] += .01 * producer;
90 if (unregistered.points[i] != expected)
91 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
92 "Unregistered collection did not select the lowest producer");
93 }
94 CHKERR comm->synchroniseByConnectivity(children);
95 for (const auto &[vertex, expected] : coordinates) {
96 std::array<double, 3> x;
97 CHKERR mb.get_coords(&vertex, 1, x.data());
98 if (x != expected)
99 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
100 "Topological synchronisation changed vertex coordinates");
101 }
102 const int local_cells = children.subset_by_type(MBTET).size();
103 int global_cells;
104 CHKERR MPI_Allreduce(&local_cells, &global_cells, 1, MPI_INT, MPI_SUM,
105 m_field.get_comm());
106 if (global_cells != 4 ||
107 (m_field.get_comm_rank() == 0 && local_cells != (last ? 3 : 4)) ||
108 (last && m_field.get_comm_rank() == last && local_cells != 1))
109 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
110 "Cell partitions were not inherited");
111 Tag tag;
112 const int zero = 0;
113 CHKERR mb.tag_get_handle("RECONSTRUCTION_EXCHANGE", 1, MB_TYPE_INTEGER, tag,
114 MB_TAG_DENSE | MB_TAG_CREAT, &zero);
115 for (auto e : children) {
116 int owner;
117 CHKERR pc->get_owner(e, owner);
118 const int value = owner == m_field.get_comm_rank() ? owner + 10 : -1;
119 CHKERR mb.tag_set_data(tag, &e, 1, &value);
120 }
121 CHKERR pc->exchange_tags(tag, children);
122 for (auto e : children) {
123 int owner, value;
124 CHKERR pc->get_owner(e, owner);
125 CHKERR mb.tag_get_data(tag, &e, 1, &value);
126 if (value != owner + 10)
127 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
128 "New sharing cannot exchange tags");
129 }
130 CHKERR pc->check_all_shared_handles();
131 std::array<int, 6> local_owners, owners;
132 local_owners.fill(-1);
133 for (auto vertex : children.subset_by_type(MBVERTEX)) {
134 int id, owner;
135 CHKERR mb.tag_get_data(mb.globalId_tag(), &vertex, 1, &id);
136 CHKERR pc->get_owner(vertex, owner);
137 if (owner == m_field.get_comm_rank())
138 local_owners[id - 1] = owner;
139 }
140 CHKERR MPI_Allreduce(local_owners.data(), owners.data(), owners.size(),
141 MPI_INT, MPI_MAX, m_field.get_comm());
142 TetMeshData gathered;
143 CHKERR comm->gatherMesh(children.subset_by_type(MBTET), gathered, last);
144 CHKERR comm->broadcastMesh(gathered, last);
145 if (gathered.points.size() != 6 || gathered.cells.size() != 4)
146 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
147 "Nonzero-producer collection lost topology or vertex identities");
148 for (size_t i = 0; i != gathered.vertexIds.size(); ++i) {
149 const auto id = gathered.vertexIds[i];
150 auto expected = output.points[id - 1];
151 expected[0] += .01 * owners[id - 1];
152 if (owners[id - 1] < 0 || gathered.points[i] != expected)
153 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
154 "Collection did not select the owning rank's coordinates");
155 }
157}
158} // namespace
159
160int main(int argc, char *argv[]) {
161 MoFEM::Core::Initialize(&argc, &argv, nullptr,
162 "Reconnection communication checks\n");
163 try {
164 moab::Core mb;
165 MoFEM::Core core(mb);
166 const auto error = run(core);
167 if (error)
168 MPI_Abort(PETSC_COMM_WORLD, error);
169 }
172 return 0;
173}
CGALInterfaceImpl & owner
std::map< EntityHandle, int > index
std::vector< int > source
std::vector< int > parent
std::vector< Cell > cells
bool active
MPI_Comm comm
moab::Interface & mb
Tag tag
std::vector< std::array< double, 3 > > coordinates
int main()
#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 ...
@ MOFEM_ATOM_TEST_INVALID
Definition definitions.h:40
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
virtual const RefEntity_multiIndex * get_ref_ents() const =0
Get the ref ents object.
FTensor::Index< 'i', SPACE_DIM > i
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.
Definition Types.hpp:40
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
MoFEMErrorCode run(MoFEM::Interface &m_field)
Definition sdf_python.cpp:7
Managing BitRefLevels.
Managing BitRefLevels.
virtual int get_comm_size() const =0
virtual moab::Interface & get_moab()=0
virtual MPI_Comm & get_comm() const =0
virtual int get_comm_rank() const =0
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.
Mesh refinement interface.
std::vector< std::array< int, 4 > > cells
Zero-based point indices.
Definition CGALTools.hpp:19
std::vector< std::array< double, 3 > > points
Definition CGALTools.hpp:17
std::vector< int > vertexIds
Definition CGALTools.hpp:18
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.