Debian Codegraph @

packages/stk/stk_unit_tests/stk_tools/mesh_tools/UnitTestCustomAura.cpp

raw
1 // Copyright 2002 - 2008, 2010, 2011 National Technology Engineering
2 // Solutions of Sandia, LLC (NTESS). Under the terms of Contract
3 // DE-NA0003525 with NTESS, the U.S. Government retains certain rights
4 // in this software.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials provided
16 // with the distribution.
17 //
18 // * Neither the name of NTESS nor the names of its contributors
19 // may be used to endorse or promote products derived from this
20 // software without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 //
34
35 #include <gtest/gtest.h>
36 #include "UnitTestCustomAura.hpp"
37 #include "stk_tools/mesh_tools/CustomAura.hpp"
38 #include "stk_util/parallel/CommSparse.hpp"
39
40 namespace aura_unit_tests {
41
42 std::map<int,std::vector<int>> local_node_comm( stk::mesh::BulkData& meshBulk ) {
43 std::map<int, std::vector<int>> localComm;
44 stk::mesh::EntityVector nodes;
45 meshBulk.get_entities(stk::topology::NODE_RANK,meshBulk.mesh_meta_data().locally_owned_part(),nodes);
46 for( auto& node : nodes ) {
47 int nodeID = meshBulk.identifier(node);
48 std::set<int> pIDs;
49 pIDs.emplace(meshBulk.parallel_rank());
50 std::vector<int> procList;
51 meshBulk.comm_procs(node, procList);
52 for( auto p : procList ) {
53 pIDs.emplace(p);
54 }
55 std::vector<int> v(pIDs.begin(),pIDs.end());
56 localComm[nodeID] = v;
57 }
58 return localComm;
59 }
60
61 void FourQuadShellsInSequenceFixture::print_local_node_comm(const int rank) {
62 if ( rank == this->get_parallel_rank() ) {
63 std::map<int, std::vector<int>> nodeComm = local_node_comm(this->get_bulk());
64 std::stringstream pstream;
65 pstream << "On Proc " << this->get_parallel_rank() << ";" << std::endl;
66 for (auto& a : nodeComm) {
67 pstream << "goldMap[" << a.first << "] = {";
68 for (int p : a.second) {
69 pstream << " " << p << ",";
70 }
71 pstream << "};" << std::endl;
72 }
73 std::cout << pstream.str();
74 }
75 }
76
77 std::map<int,std::vector<int>> pre_aura_node_comm(int numRanks) {
78 std::map<int, std::vector<int>> goldMap;
79 if ( numRanks == 1 ) {
80 goldMap[1] = { 0 };
81 goldMap[2] = { 0 };
82 goldMap[3] = { 0 };
83 goldMap[4] = { 0 };
84 goldMap[5] = { 0 };
85 goldMap[6] = { 0 };
86 goldMap[7] = { 0 };
87 goldMap[8] = { 0 };
88 goldMap[9] = { 0 };
89 goldMap[10] = { 0 };
90 } else if ( numRanks == 2 ) {
91 goldMap[1] = { 0 };
92 goldMap[2] = { 0 };
93 goldMap[3] = { 0 };
94 goldMap[4] = { 0, 1 };
95 goldMap[5] = { 1 };
96 goldMap[6] = { 0 };
97 goldMap[7] = { 0 };
98 goldMap[8] = { 0 };
99 goldMap[9] = { 0, 1 };
100 goldMap[10] = { 1 };
101 } else if ( numRanks == 3 ) {
102 goldMap[1] = { 0 };
103 goldMap[2] = { 0 };
104 goldMap[3] = { 0, 1 };
105 goldMap[4] = { 1, 2 };
106 goldMap[5] = { 2 };
107 goldMap[6] = { 0 };
108 goldMap[7] = { 0 };
109 goldMap[8] = { 0, 1 };
110 goldMap[9] = { 1, 2 };
111 goldMap[10] = { 2 };
112 } else if ( numRanks == 4) {
113 goldMap[1] = { 0 };
114 goldMap[2] = { 0, 1 };
115 goldMap[3] = { 1, 2 };
116 goldMap[4] = { 2, 3 };
117 goldMap[5] = { 3 };
118 goldMap[6] = { 0 };
119 goldMap[7] = { 0, 1 };
120 goldMap[8] = { 1, 2 };
121 goldMap[9] = { 2, 3 };
122 goldMap[10] = { 3 };
123 }
124 return goldMap;
125 }
126
127 std::map<int,std::vector<int>> post_aura_node_comm(int numRanks, int numRings) {
128 std::map<int, std::vector<int>> goldMap;
129 if ( numRings == 1 ) {
130 if ( numRanks == 1 ) {
131 goldMap[1] = { 0 };
132 goldMap[2] = { 0 };
133 goldMap[3] = { 0 };
134 goldMap[4] = { 0 };
135 goldMap[5] = { 0 };
136 goldMap[6] = { 0 };
137 goldMap[7] = { 0 };
138 goldMap[8] = { 0 };
139 goldMap[9] = { 0 };
140 goldMap[10] = { 0 };
141 } else if ( numRanks == 2 ) {
142 goldMap[1] = { 0 };
143 goldMap[2] = { 0, 1 };
144 goldMap[3] = { 0, 1 };
145 goldMap[4] = { 0, 1 };
146 goldMap[5] = { 0, 1 };
147 goldMap[6] = { 0 };
148 goldMap[7] = { 0, 1 };
149 goldMap[8] = { 0, 1 };
150 goldMap[9] = { 0, 1 };
151 goldMap[10] = { 0, 1 };
152 } else if ( numRanks == 3 ) {
153 goldMap[1] = { 0, 1 };
154 goldMap[2] = { 0, 1 };
155 goldMap[3] = { 0, 1, 2 };
156 goldMap[4] = { 0, 1, 2 };
157 goldMap[5] = { 1, 2 };
158 goldMap[6] = { 0, 1 };
159 goldMap[7] = { 0, 1 };
160 goldMap[8] = { 0, 1, 2 };
161 goldMap[9] = { 0, 1, 2 };
162 goldMap[10] = { 1, 2 };
163 } else if ( numRanks == 4) {
164 goldMap[1] = { 0, 1 };
165 goldMap[2] = { 0, 1, 2 };
166 goldMap[3] = { 0, 1, 2, 3 };
167 goldMap[4] = { 1, 2, 3 };
168 goldMap[5] = { 2, 3 };
169 goldMap[6] = { 0, 1 };
170 goldMap[7] = { 0, 1, 2 };
171 goldMap[8] = { 0, 1, 2, 3 };
172 goldMap[9] = { 1, 2, 3 };
173 goldMap[10] = { 2, 3 };
174 }
175 } else {
176 for ( int nID(1); nID <= 10; ++nID ) {
177 std::vector<int> pIDs;
178 for ( int p(0); p < numRanks; ++p ) {
179 pIDs.push_back(p);
180 }
181 goldMap[nID] = pIDs;
182 }
183 }
184 return goldMap;
185 }
186
187 void test_node_comm_pre_aura( stk::mesh::BulkData& bulk ) {
188 std::map<int,std::vector<int>> nodeComm = local_node_comm(bulk);
189 std::map<int,std::vector<int>> goldComm = pre_aura_node_comm(bulk.parallel_size());
190 for( auto& a : nodeComm ) {
191 EXPECT_EQ(a.second,goldComm[a.first]) << " pre aura for node " << a.first;
192 }
193 }
194
195 void test_node_comm_post_aura( stk::mesh::BulkData& bulk, int numRings ) {
196 std::map<int,std::vector<int>> nodeComm = local_node_comm(bulk);
197 std::map<int,std::vector<int>> goldComm = post_aura_node_comm(bulk.parallel_size(),numRings);
198 for( auto& a : nodeComm ) {
199 EXPECT_EQ(a.second,goldComm[a.first]) << " post aura for node " << a.first;
200 }
201 }
202
203 void sym_comm_pre_ghost_check(stk::mesh::BulkData& bulk) {
204 stk::mesh::Entity n1 = bulk.get_entity(stk::topology::NODE_RANK, 1);
205 if ( bulk.parallel_rank() == 0 ) {
206 EXPECT_TRUE(n1 != stk::mesh::Entity::InvalidEntity);
207 } else {
208 EXPECT_TRUE(n1 == stk::mesh::Entity::InvalidEntity);
209 }
210 }
211
212 void sym_comm_post_ghost_check(stk::mesh::BulkData& bulk) {
213 stk::mesh::Entity n1 = bulk.get_entity(stk::topology::NODE_RANK, 1);
214 EXPECT_TRUE(n1 != stk::mesh::Entity::InvalidEntity);
215
216 if ( bulk.parallel_size() == 1 ) {
217 return;
218 }
219
220 std::vector<int> goldOwnedCommProcs;
221 for ( int p(1); p < bulk.parallel_size(); ++p ) {
222 goldOwnedCommProcs.emplace_back(p);
223 }
224 std::vector<int> goldGhostCommProcs = {0};
225
226
227 std::vector<int> commProcs;
228 bulk.comm_procs(n1,commProcs);
229 std::sort(commProcs.begin(),commProcs.end());
230
231 // check owned comm_procs
232 if (bulk.parallel_rank() == 0) {
233 EXPECT_EQ(commProcs,goldOwnedCommProcs);
234 // check ghost comm_procs
235 } else {
236 EXPECT_EQ(commProcs,goldGhostCommProcs);
237 }
238
239 std::map<stk::mesh::EntityKey, std::set<int>> symmMap;
240 stk::tools::populate_symm_comm_map(bulk,symmMap,stk::topology::NODE_RANK);
241
242 // Desired comm_procs
243 std::set<int> goldDesiredCommProcs;
244 for ( int p(0); p < bulk.parallel_size(); ++p ) {
245 goldDesiredCommProcs.insert(p);
246 }
247 std::set<int>& n1CommProcs = symmMap[bulk.entity_key(n1)];
248 EXPECT_EQ(n1CommProcs,goldDesiredCommProcs);
249
250 }
251
252 TEST_F(FourQuadShellsInSequenceFixture, GenSymmComm) {
253
254 stk::mesh::BulkData& bulk = this->get_bulk();
255 sym_comm_pre_ghost_check(bulk);
256
257 // Ghost Node1 to all Procs
258 stk::mesh::Ghosting * customGhost = nullptr;
259 if (bulk.parallel_size() > 1) {
260 bulk.modification_begin();
261 customGhost = &bulk.create_ghosting("UnitGenSymmComm");
262 stk::mesh::EntityProcVec entitiesToGhost;
263 if (bulk.parallel_rank() == 0) {
264 stk::mesh::Entity n1 = bulk.get_entity(stk::topology::NODE_RANK, 1);
265 for ( int p(1); p < bulk.parallel_size(); ++p ) {
266 entitiesToGhost.push_back(stk::mesh::EntityProc(n1,p));
267 }
268 }
269 bulk.change_ghosting(*customGhost, entitiesToGhost);
270 bulk.modification_end();
271 }
272
273 sym_comm_post_ghost_check(bulk);
274
275 stk::tools::destroy_custom_aura(bulk, customGhost);
276 }
277
278 TEST_F(FourQuadShellsInSequenceFixture, OneRingTest) {
279
280 test_node_comm_pre_aura(this->get_bulk());
281 // std::cout << "Pre Aura:" << std::endl;
282 // for ( int i(0); i < this->get_parallel_size(); ++i ) {
283 // print_local_node_comm(i);
284 // }
285
286 const int num_rings = 1;
287 stk::mesh::Ghosting* g = stk::tools::create_ringed_aura(this->get_bulk(), this->get_meta().universal_part(), "n_ring_aura", num_rings);
288 if ( this->get_parallel_size() == 1 ) {
289 EXPECT_EQ(g,nullptr);
290 }
291
292 test_node_comm_post_aura(this->get_bulk(),num_rings);
293 // std::cout << "Post Aura:" << std::endl;
294 // for ( int i(0); i < this->get_parallel_size(); ++i ) {
295 // print_local_node_comm(i);
296 // }
297
298 EXPECT_EQ(1, num_rings);
299 stk::tools::destroy_custom_aura(this->get_bulk(), g);
300 }
301
302 TEST_F(FourQuadShellsInSequenceFixture, TwoRingTest) {
303 test_node_comm_pre_aura(this->get_bulk());
304 const int num_rings = 2;
305 stk::mesh::Ghosting* g = stk::tools::create_ringed_aura(this->get_bulk(), this->get_meta().universal_part(), "n_ring_aura", num_rings);
306 if ( this->get_parallel_size() == 1 ) {
307 EXPECT_EQ(g,nullptr);
308 }
309 test_node_comm_post_aura(this->get_bulk(),num_rings);
310 EXPECT_EQ(2, num_rings);
311 stk::tools::destroy_custom_aura(this->get_bulk(), g);
312 }
313
314 TEST_F(FourQuadShellsInSequenceFixture, ThreeRingTest) {
315 test_node_comm_pre_aura(this->get_bulk());
316 const int num_rings = 3;
317 stk::mesh::Ghosting* g = stk::tools::create_ringed_aura(this->get_bulk(), this->get_meta().universal_part(), "n_ring_aura", num_rings);
318 if ( this->get_parallel_size() == 1 ) {
319 EXPECT_EQ(g,nullptr);
320 }
321 test_node_comm_post_aura(this->get_bulk(),num_rings);
322 EXPECT_EQ(3, num_rings);
323 stk::tools::destroy_custom_aura(this->get_bulk(), g);
324 }
325
326 }
327