Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/UnitTestHexFixture.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> // for AssertHelper, EXPECT_EQ, etc
36 #include <map> // for map, map<>::mapped_type
37 #include <ostream> // for basic_ostream::operator<<
38 #include <vector> // for vector
39 #include "mpi.h" // for MPI_COMM_WORLD
40
41 #include "stk_mesh/base/BulkData.hpp" // for BulkData
42 #include "stk_mesh/base/Entity.hpp" // for Entity
43 #include "stk_mesh/base/MetaData.hpp" // for MetaData
44 #include "stk_mesh/base/Types.hpp" // for EntityId, EntityRank
45 #include "stk_topology/topology.hpp" // for topology, etc
46 #include "stk_unit_test_utils/stk_mesh_fixtures/HexFixture.hpp" // for HexFixture
47 #include "stk_util/parallel/Parallel.hpp" // for parallel_machine_rank, etc
48
49 namespace {
50
51 using stk::mesh::MetaData;
52 using stk::mesh::Entity;
53 using stk::mesh::EntityRank;
54 using stk::mesh::EntityId;
55
56 TEST( UnitTestHexFixture, elem_ids_1d_x )
57 {
58 // Test 3x1x1 HexFixture structure
59 const unsigned NX = 3;
60 const unsigned NY = 1;
61 const unsigned NZ = 1;
62 stk::mesh::fixtures::HexFixture hf(MPI_COMM_WORLD,NX,NY,NZ);
63 hf.m_meta.commit();
64 hf.generate_mesh();
65 EXPECT_EQ( hf.elem_id(0,0,0), 1u );
66 EXPECT_EQ( hf.elem_id(1,0,0), 2u );
67 EXPECT_EQ( hf.elem_id(2,0,0), 3u );
68 }
69
70 TEST( UnitTestHexFixture, elem_ids_3d_x )
71 {
72 // Test 3x3x3 HexFixture structure
73 const unsigned NX = 3;
74 const unsigned NY = 3;
75 const unsigned NZ = 3;
76 stk::mesh::fixtures::HexFixture hf(MPI_COMM_WORLD,NX,NY,NZ);
77 hf.m_meta.commit();
78 hf.generate_mesh();
79 EXPECT_EQ( hf.elem_id(0,0,0), 1u );
80 EXPECT_EQ( hf.elem_id(1,0,0), 2u );
81 EXPECT_EQ( hf.elem_id(2,0,0), 3u );
82 }
83
84 TEST( UnitTestHexFixture, elem_ids_1d_y )
85 {
86 // Test 1x3x1 HexFixture structure
87 const unsigned NX = 1;
88 const unsigned NY = 3;
89 const unsigned NZ = 1;
90 stk::mesh::fixtures::HexFixture hf(MPI_COMM_WORLD,NX,NY,NZ);
91 hf.m_meta.commit();
92 hf.generate_mesh();
93 EXPECT_EQ( hf.elem_id(0,0,0), 1u );
94 EXPECT_EQ( hf.elem_id(0,1,0), 2u );
95 EXPECT_EQ( hf.elem_id(0,2,0), 3u );
96 }
97
98 TEST( UnitTestHexFixture, elem_ids_3d_y )
99 {
100 // Test 3x3x3 HexFixture structure
101 const unsigned NX = 3;
102 const unsigned NY = 3;
103 const unsigned NZ = 3;
104 stk::mesh::fixtures::HexFixture hf(MPI_COMM_WORLD,NX,NY,NZ);
105 hf.m_meta.commit();
106 hf.generate_mesh();
107 EXPECT_EQ( hf.elem_id(0,0,0), 1u );
108 EXPECT_EQ( hf.elem_id(0,1,0), 4u );
109 EXPECT_EQ( hf.elem_id(0,2,0), 7u );
110 }
111
112 TEST( UnitTestHexFixture, elem_ids_1d_z )
113 {
114 // Test 1x1x3 HexFixture structure
115 const unsigned NX = 1;
116 const unsigned NY = 1;
117 const unsigned NZ = 3;
118 stk::mesh::fixtures::HexFixture hf(MPI_COMM_WORLD,NX,NY,NZ);
119 hf.m_meta.commit();
120 hf.generate_mesh();
121 EXPECT_EQ( hf.elem_id(0,0,0), 1u );
122 EXPECT_EQ( hf.elem_id(0,0,1), 2u );
123 EXPECT_EQ( hf.elem_id(0,0,2), 3u );
124 }
125
126 TEST( UnitTestHexFixture, elem_ids_3d_z )
127 {
128 // Test 3x3x3 HexFixture structure
129 const unsigned NX = 3;
130 const unsigned NY = 3;
131 const unsigned NZ = 3;
132 stk::mesh::fixtures::HexFixture hf(MPI_COMM_WORLD,NX,NY,NZ);
133 hf.m_meta.commit();
134 hf.generate_mesh();
135 EXPECT_EQ( hf.elem_id(0,0,0), 1u );
136 EXPECT_EQ( hf.elem_id(0,0,1), 10u );
137 EXPECT_EQ( hf.elem_id(0,0,2), 19u );
138 }
139
140 TEST( UnitTestHexFixture, elem_ids_3d_diag )
141 {
142 // Test 3x3x3 HexFixture structure
143 const unsigned NX = 3;
144 const unsigned NY = 3;
145 const unsigned NZ = 3;
146 stk::mesh::fixtures::HexFixture hf(MPI_COMM_WORLD,NX,NY,NZ);
147 hf.m_meta.commit();
148 hf.generate_mesh();
149 EXPECT_EQ( hf.elem_id(0,0,0), 1u );
150 EXPECT_EQ( hf.elem_id(1,1,1), 14u );
151 EXPECT_EQ( hf.elem_id(2,2,2), 27u );
152 }
153
154 TEST( UnitTestHexFixture, trivial_parallel_2 )
155 {
156 // Test a customized element distribution with one element on proc 0 and 1
157 // and none on the other procs
158
159 const int p_rank = stk::parallel_machine_rank(MPI_COMM_WORLD);
160 const int p_size = stk::parallel_machine_size(MPI_COMM_WORLD);
161
162 // Skip unless p_size is at least 2
163 if (p_size < 2)
164 return;
165
166 const unsigned NX = 2;
167 const unsigned NY = 1;
168 const unsigned NZ = 1;
169
170 // map< processor, vector of element ids >, this is our custom parallel
171 // distribution. One element will go on rank 0, the other on rank 1, any
172 // other ranks get nothing.
173 std::map<int,std::vector<EntityId> > parallel_distribution;
174 {
175 std::vector< EntityId> element_ids;
176 element_ids.push_back(1);
177 parallel_distribution[0] = element_ids;
178 element_ids[0] = 2;
179 parallel_distribution[1] = element_ids;
180 }
181
182 // Create the fixture
183 stk::mesh::fixtures::HexFixture hf(MPI_COMM_WORLD,NX,NY,NZ);
184 hf.m_meta.commit();
185 if (p_rank <= 1) {
186 hf.fill_node_map(parallel_distribution);
187 hf.generate_mesh(parallel_distribution[p_rank]);
188 }
189 else {
190 std::vector<EntityId> empty_vector;
191 hf.generate_mesh( empty_vector ) ;
192 }
193
194 stk::mesh::BulkData & mesh = hf.m_bulk_data;
195
196 // Verify element_id 1 is owned by proc 0
197 // Verify element_id 2 is owned by proc 1
198 const EntityRank element_rank = stk::topology::ELEMENT_RANK;
199 Entity entity_1 = mesh.get_entity(element_rank, 1);
200 Entity entity_2 = mesh.get_entity(element_rank ,2);
201 if (p_rank <= 1) {
202 ASSERT_TRUE( mesh.is_valid(entity_1) );
203 ASSERT_TRUE( mesh.is_valid(entity_2) );
204 EXPECT_EQ( 0, mesh.parallel_owner_rank(entity_1) );
205 EXPECT_EQ( 1, mesh.parallel_owner_rank(entity_2) );
206 }
207 else {
208 EXPECT_TRUE( !mesh.is_valid(entity_1) );
209 EXPECT_TRUE( !mesh.is_valid(entity_2) );
210 }
211 }
212
213 TEST( UnitTestHexFixture, disjoint_parallel_psizex1x1 )
214 {
215 // Test a customized element distribution with one element on each proc
216
217 const int p_rank = stk::parallel_machine_rank(MPI_COMM_WORLD);
218 const int p_size = stk::parallel_machine_size(MPI_COMM_WORLD);
219
220 const unsigned NX = p_size;
221 const unsigned NY = 1;
222 const unsigned NZ = 1;
223
224 // map< processor, vector of element ids >, this is our custom parallel
225 // distribution. Assign each processor an element such that rank+1 = elem_id
226 std::map<int,std::vector<EntityId> > parallel_distribution;
227 for (int p=0 ; p < p_size ; ++p) {
228 std::vector< EntityId> element_ids;
229 element_ids.push_back(p+1); // element id's start at 1
230 parallel_distribution[p] = element_ids;
231 }
232
233 // Create the fixture
234 stk::mesh::fixtures::HexFixture hf(MPI_COMM_WORLD,NX,NY,NZ);
235 hf.m_meta.commit();
236 hf.fill_node_map(parallel_distribution);
237 hf.generate_mesh(parallel_distribution[p_rank]);
238 stk::mesh::BulkData & mesh = hf.m_bulk_data;
239 const EntityRank element_rank = stk::topology::ELEMENT_RANK;
240
241 // We should always know about, and own, the element assigned to us
242 Entity my_entity = mesh.get_entity(element_rank, p_rank + 1);
243 ASSERT_TRUE( mesh.is_valid(my_entity) );
244 EXPECT_EQ( p_rank, mesh.parallel_owner_rank(my_entity) );
245
246 // If applicable, we know about the element on adjacent lower rank
247 if (p_rank > 0) {
248 Entity prior_entity = mesh.get_entity(element_rank, p_rank);
249 ASSERT_TRUE( mesh.is_valid(prior_entity) );
250 EXPECT_EQ( p_rank - 1, mesh.parallel_owner_rank(prior_entity) );
251 }
252
253 // If applicable, we know about the element on adjacent higher rank
254 if (p_rank < p_size - 1) {
255 Entity next_entity = mesh.get_entity(element_rank, p_rank + 2);
256 ASSERT_TRUE( mesh.is_valid(next_entity) );
257 EXPECT_EQ( p_rank + 1, mesh.parallel_owner_rank(next_entity) );
258 }
259 }
260
261 TEST( UnitTestHexFixture, disjoint_parallel_4x2x1 )
262 {
263 // layout: 4x2x1 hex mesh
264 // elements:
265 // [ e_1, e_2, e_3, e_4 ]
266 // [ e_5, e_6, e_7, e_8 ]
267 // processors:
268 // [ p_0, p_1, p_1, p_1 ]
269 // [ p_1, p_0, p_1, p_1 ]
270 //
271
272 const int p_rank = stk::parallel_machine_rank(MPI_COMM_WORLD);
273 const int p_size = stk::parallel_machine_size(MPI_COMM_WORLD);
274
275 // Skip unless p_size is at least 2
276 if (p_size < 2)
277 return;
278
279 const unsigned NX = 4;
280 const unsigned NY = 2;
281 const unsigned NZ = 1;
282
283 // map< processor, vector of element ids >, this is our custom parallel
284 // distribution. Assign 1,6 to proc 0, all the rest to proc 1. The other
285 // procs get nothing.
286 std::map<int,std::vector<EntityId> > parallel_distribution;
287 {
288 std::vector< EntityId> element_ids;
289 element_ids.push_back(1);
290 element_ids.push_back(6);
291 parallel_distribution[0] = element_ids; // proc 0
292 element_ids.clear();
293 element_ids.push_back(2);
294 element_ids.push_back(3);
295 element_ids.push_back(4);
296 element_ids.push_back(5);
297 element_ids.push_back(7);
298 element_ids.push_back(8);
299 parallel_distribution[1] = element_ids; // proc 1
300 }
301
302 // Create the fixture
303 stk::mesh::fixtures::HexFixture hf(MPI_COMM_WORLD,NX,NY,NZ);
304 hf.m_meta.commit();
305 if (p_rank <= 1) {
306 hf.fill_node_map(parallel_distribution);
307 hf.generate_mesh(parallel_distribution[p_rank]);
308 }
309 else {
310 std::vector<EntityId> empty_vector;
311 hf.generate_mesh( empty_vector ) ;
312 }
313
314 stk::mesh::BulkData & mesh = hf.m_bulk_data;
315
316 // Verify that the entities and known and owned by the appropriate procs
317 const EntityRank element_rank = stk::topology::ELEMENT_RANK;
318 Entity entity_1 = mesh.get_entity(element_rank, 1);
319 Entity entity_2 = mesh.get_entity(element_rank, 2);
320 Entity entity_3 = mesh.get_entity(element_rank, 3);
321 Entity entity_4 = mesh.get_entity(element_rank, 4);
322 Entity entity_5 = mesh.get_entity(element_rank, 5);
323 Entity entity_6 = mesh.get_entity(element_rank, 6);
324 Entity entity_7 = mesh.get_entity(element_rank, 7);
325 Entity entity_8 = mesh.get_entity(element_rank, 8);
326 if (p_rank == 0) {
327 ASSERT_TRUE( mesh.is_valid(entity_1) );
328 ASSERT_TRUE( mesh.is_valid(entity_2) );
329 ASSERT_TRUE( mesh.is_valid(entity_3) );
330 ASSERT_TRUE( !mesh.is_valid(entity_4) );
331 ASSERT_TRUE( mesh.is_valid(entity_5) );
332 ASSERT_TRUE( mesh.is_valid(entity_6) );
333 ASSERT_TRUE( mesh.is_valid(entity_7) );
334 ASSERT_TRUE( !mesh.is_valid(entity_8) );
335 EXPECT_EQ( 0, mesh.parallel_owner_rank(entity_1) );
336 EXPECT_EQ( 1, mesh.parallel_owner_rank(entity_2) );
337 EXPECT_EQ( 1, mesh.parallel_owner_rank(entity_3) );
338 EXPECT_EQ( 1, mesh.parallel_owner_rank(entity_5) );
339 EXPECT_EQ( 0, mesh.parallel_owner_rank(entity_6) );
340 EXPECT_EQ( 1, mesh.parallel_owner_rank(entity_7) );
341 }
342 else if (p_rank == 1) {
343 ASSERT_TRUE( mesh.is_valid(entity_1) );
344 ASSERT_TRUE( mesh.is_valid(entity_2) );
345 ASSERT_TRUE( mesh.is_valid(entity_3) );
346 ASSERT_TRUE( mesh.is_valid(entity_4) );
347 ASSERT_TRUE( mesh.is_valid(entity_5) );
348 ASSERT_TRUE( mesh.is_valid(entity_6) );
349 ASSERT_TRUE( mesh.is_valid(entity_7) );
350 ASSERT_TRUE( mesh.is_valid(entity_8) );
351 EXPECT_EQ( 0, mesh.parallel_owner_rank(entity_1) );
352 EXPECT_EQ( 1, mesh.parallel_owner_rank(entity_2) );
353 EXPECT_EQ( 1, mesh.parallel_owner_rank(entity_3) );
354 EXPECT_EQ( 1, mesh.parallel_owner_rank(entity_4) );
355 EXPECT_EQ( 1, mesh.parallel_owner_rank(entity_5) );
356 EXPECT_EQ( 0, mesh.parallel_owner_rank(entity_6) );
357 EXPECT_EQ( 1, mesh.parallel_owner_rank(entity_7) );
358 EXPECT_EQ( 1, mesh.parallel_owner_rank(entity_8) );
359 }
360 else {
361 EXPECT_TRUE( !mesh.is_valid(entity_1) );
362 EXPECT_TRUE( !mesh.is_valid(entity_2) );
363 EXPECT_TRUE( !mesh.is_valid(entity_3) );
364 EXPECT_TRUE( !mesh.is_valid(entity_4) );
365 EXPECT_TRUE( !mesh.is_valid(entity_5) );
366 EXPECT_TRUE( !mesh.is_valid(entity_6) );
367 EXPECT_TRUE( !mesh.is_valid(entity_7) );
368 EXPECT_TRUE( !mesh.is_valid(entity_8) );
369 }
370 }
371
372 TEST( UnitTestHexFixture, disjoint_parallel_5x1x1 )
373 {
374 // layout:
375 // [ e_1, e_2, e_3, e_4, e_5 ] elements
376 // [ p_0, p_1, p_1, p_1, p_0 ] processors
377 //
378 const int p_rank = stk::parallel_machine_rank(MPI_COMM_WORLD);
379 const int p_size = stk::parallel_machine_size(MPI_COMM_WORLD);
380
381 // Skip unless p_size is at least 2
382 if (p_size < 2)
383 return;
384
385 const unsigned NX = 5;
386 const unsigned NY = 1;
387 const unsigned NZ = 1;
388
389 // map< processor, vector of element ids >, this is our custom parallel
390 // distribution. Assign 1,5 to proc 0, all the rest to proc 1. The other
391 // procs get nothing.
392 std::map<int,std::vector<EntityId> > parallel_distribution;
393 {
394 std::vector< EntityId> element_ids;
395 element_ids.push_back(1);
396 element_ids.push_back(5);
397 parallel_distribution[0] = element_ids;
398 element_ids.clear();
399 element_ids.push_back(2);
400 element_ids.push_back(3);
401 element_ids.push_back(4);
402 parallel_distribution[1] = element_ids;
403 }
404
405 // Create the fixture
406 stk::mesh::fixtures::HexFixture hf(MPI_COMM_WORLD,NX,NY,NZ);
407 hf.m_meta.commit();
408 if (p_rank <= 1) {
409 hf.fill_node_map(parallel_distribution);
410 hf.generate_mesh(parallel_distribution[p_rank]);
411 }
412 else {
413 std::vector<EntityId> empty_vector;
414 hf.generate_mesh( empty_vector ) ;
415 }
416
417 stk::mesh::BulkData & mesh = hf.m_bulk_data;
418
419 // Verify that the entities and known and owned by the appropriate procs
420 const EntityRank element_rank = stk::topology::ELEMENT_RANK;
421 Entity entity_1 = mesh.get_entity(element_rank, 1);
422 Entity entity_2 = mesh.get_entity(element_rank, 2);
423 Entity entity_3 = mesh.get_entity(element_rank, 3);
424 Entity entity_4 = mesh.get_entity(element_rank, 4);
425 Entity entity_5 = mesh.get_entity(element_rank, 5);
426 if (p_rank == 0) {
427 ASSERT_TRUE( mesh.is_valid(entity_1) );
428 ASSERT_TRUE( mesh.is_valid(entity_2) );
429 ASSERT_TRUE( !mesh.is_valid(entity_3) );
430 ASSERT_TRUE( mesh.is_valid(entity_4) );
431 ASSERT_TRUE( mesh.is_valid(entity_5) );
432 EXPECT_EQ( 0, mesh.parallel_owner_rank(entity_1) );
433 EXPECT_EQ( 1, mesh.parallel_owner_rank(entity_2) );
434 EXPECT_EQ( 1, mesh.parallel_owner_rank(entity_4) );
435 EXPECT_EQ( 0, mesh.parallel_owner_rank(entity_5) );
436 }
437 else if (p_rank == 1) {
438 ASSERT_TRUE( mesh.is_valid(entity_1) );
439 ASSERT_TRUE( mesh.is_valid(entity_2) );
440 ASSERT_TRUE( mesh.is_valid(entity_3) );
441 ASSERT_TRUE( mesh.is_valid(entity_4) );
442 ASSERT_TRUE( mesh.is_valid(entity_5) );
443 EXPECT_EQ( 0, mesh.parallel_owner_rank(entity_1) );
444 EXPECT_EQ( 1, mesh.parallel_owner_rank(entity_2) );
445 EXPECT_EQ( 1, mesh.parallel_owner_rank(entity_3) );
446 EXPECT_EQ( 1, mesh.parallel_owner_rank(entity_4) );
447 EXPECT_EQ( 0, mesh.parallel_owner_rank(entity_5) );
448 }
449 else {
450 EXPECT_TRUE( !mesh.is_valid(entity_1) );
451 EXPECT_TRUE( !mesh.is_valid(entity_2) );
452 EXPECT_TRUE( !mesh.is_valid(entity_3) );
453 EXPECT_TRUE( !mesh.is_valid(entity_4) );
454 EXPECT_TRUE( !mesh.is_valid(entity_5) );
455 }
456 }
457
458 } // end namespace