Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/UnitTestRingFixture.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 "mpi.h" // for MPI_Barrier, MPI_COMM_WORLD, etc
36 #include "stk_mesh/base/Entity.hpp" // for Entity
37 #include "stk_mesh/base/EntityCommListInfo.hpp"
38 #include "stk_mesh/base/EntityKey.hpp" // for EntityKey
39 #include "stk_mesh/base/Part.hpp" // for Part
40 #include "stk_mesh/base/Selector.hpp" // for Selector, operator|
41 #include "stk_mesh/base/Types.hpp" // for EntityProc, EntityRank
42 #include "stk_topology/topology.hpp" // for topology, etc
43 #include "stk_unit_test_utils/stk_mesh_fixtures/RingFixture.hpp" // for RingFixture
44 #include "stk_util/parallel/Parallel.hpp" // for ParallelMachine
45 #include <gtest/gtest.h> // for AssertHelper, ASSERT_EQ, etc
46 #include <stk_mesh/base/BulkData.hpp> // for BulkData
47 #include <stk_mesh/base/GetEntities.hpp> // for count_entities, etc
48 #include <stk_mesh/base/MetaData.hpp> // for MetaData
49 #include <stk_unit_test_utils/BulkDataTester.hpp>
50 #include <vector> // for vector, etc
51
52 using stk::mesh::MetaData;
53 using stk::mesh::BulkData;
54 using stk::mesh::Selector;
55 using stk::mesh::Entity;
56 using stk::mesh::EntityKey;
57 using stk::mesh::EntityProc;
58 using stk::mesh::fixtures::RingFixture;
59
60 namespace {
61
62 const stk::mesh::EntityRank NODE_RANK = stk::topology::NODE_RANK;
63
64 TEST( UnitTestBoxFixture, verifyRingFixture )
65 {
66 // A unit test to verify the correctness of the RingFixture fixture.
67
68 stk::ParallelMachine pm = MPI_COMM_WORLD;
69 MPI_Barrier( pm );
70 // Create the ring fixture we'll be testing
71
72 RingFixture fixture(pm);
73 MetaData& meta = fixture.m_meta_data;
74 BulkData& bulk = fixture.m_bulk_data;
75
76 const stk::mesh::EntityRank element_rank = stk::topology::ELEMENT_RANK;
77
78 meta.commit();
79
80 const unsigned p_rank = bulk.parallel_rank();
81 const unsigned p_size = bulk.parallel_size();
82 const unsigned nPerProc = fixture.m_num_element_per_proc ;
83 const unsigned id_total = nPerProc * p_size ;
84 const unsigned id_begin = nPerProc * p_rank ;
85 const unsigned id_end = nPerProc * ( p_rank + 1 );
86 const unsigned nLocalNode = nPerProc + ( 1 < p_size ? 1 : 0 );
87 const unsigned nLocalElement = nPerProc ;
88 const unsigned n_extra = 1 < p_size ? 2 : 0 ;
89
90 bulk.modification_begin();
91 fixture.generate_mesh();
92
93 Selector select_owned( meta.locally_owned_part() );
94 Selector select_used = meta.locally_owned_part() |
95 meta.globally_shared_part();
96 Selector select_all( meta.universal_part() );
97
98 std::vector<size_t> local_count;
99 stk::mesh::count_entities( select_used , bulk , local_count );
100 ASSERT_EQ( local_count[NODE_RANK] , nLocalNode );
101 ASSERT_EQ( local_count[element_rank] , nLocalElement );
102
103 std::vector<Entity> all_nodes;
104 get_entities( bulk, NODE_RANK, all_nodes);
105
106 unsigned num_selected_nodes =
107 count_selected_entities( select_used, bulk.buckets(NODE_RANK) );
108 ASSERT_EQ( num_selected_nodes , local_count[NODE_RANK] );
109
110 std::vector<Entity> universal_nodes;
111 get_selected_entities(select_all, bulk.buckets(NODE_RANK),
112 universal_nodes );
113 ASSERT_EQ( universal_nodes.size() , all_nodes.size() );
114
115 ASSERT_TRUE(bulk.modification_end());
116
117 // Verify declarations and sharing two end nodes:
118
119 stk::mesh::count_entities( select_used , bulk , local_count );
120 ASSERT_EQ( local_count[stk::topology::NODE_RANK] , nLocalNode );
121 ASSERT_EQ( local_count[stk::topology::ELEMENT_RANK] , nLocalElement );
122
123 if ( 1 < p_size ) {
124 const unsigned n0 = id_end < id_total ? id_begin : 0 ;
125 const unsigned n1 = id_end < id_total ? id_end : id_begin ;
126
127 Entity const node0 = bulk.get_entity( NODE_RANK , fixture.m_node_ids[n0] );
128 Entity const node1 = bulk.get_entity( NODE_RANK , fixture.m_node_ids[n1] );
129
130 ASSERT_TRUE( bulk.is_valid(node0) );
131 ASSERT_TRUE( bulk.is_valid(node1) );
132
133 std::vector<int> node0_shared_procs;
134 bulk.comm_shared_procs(bulk.entity_key(node0), node0_shared_procs);
135 std::vector<int> node1_shared_procs;
136 bulk.comm_shared_procs(bulk.entity_key(node0), node1_shared_procs);
137 ASSERT_EQ( node0_shared_procs.size(), 1u );
138 ASSERT_EQ( node1_shared_procs.size(), 1u );
139 }
140
141 // Test no-op first:
142
143 std::vector<EntityProc> change ;
144 bulk.change_entity_owner( change );
145
146 stk::mesh::count_entities( select_used , bulk , local_count );
147 ASSERT_EQ( local_count[stk::topology::NODE_RANK] , nLocalNode );
148 ASSERT_EQ( local_count[stk::topology::ELEMENT_RANK] , nLocalElement );
149
150 stk::mesh::count_entities( select_all , bulk , local_count );
151 ASSERT_EQ( local_count[stk::topology::NODE_RANK] , nLocalNode + n_extra );
152 ASSERT_EQ( local_count[stk::topology::ELEMENT_RANK] , nLocalElement + n_extra );
153
154 fixture.fixup_node_ownership();
155
156 // Make sure that element->owner_rank() == element->node[1]->owner_rank()
157 if ( 1 < p_size ) {
158 stk::mesh::count_entities( select_all , bulk , local_count );
159 ASSERT_EQ( local_count[stk::topology::NODE_RANK] , nLocalNode + n_extra );
160 ASSERT_EQ( local_count[stk::topology::ELEMENT_RANK] , nLocalElement + n_extra );
161
162 stk::mesh::count_entities( select_used , bulk , local_count );
163 ASSERT_EQ( local_count[stk::topology::NODE_RANK] , nLocalNode );
164 ASSERT_EQ( local_count[stk::topology::ELEMENT_RANK] , nLocalElement );
165
166 stk::mesh::count_entities( select_owned , bulk , local_count );
167 ASSERT_EQ( local_count[stk::topology::NODE_RANK] , nPerProc );
168 ASSERT_EQ( local_count[stk::topology::ELEMENT_RANK] , nPerProc );
169 }
170 }
171
172 }
173
174 namespace stk {
175 namespace unit_test {
176
177 void test_shift_ring( RingFixture& ring )
178 {
179 MetaData& meta = ring.m_meta_data;
180 stk::unit_test_util::BulkDataTester& bulk = ring.m_bulk_data;
181
182 const int p_rank = bulk.parallel_rank();
183 const int p_size = bulk.parallel_size();
184 const unsigned nPerProc = ring.m_num_element_per_proc ;
185 const unsigned id_total = nPerProc * p_size ;
186 const unsigned id_begin = nPerProc * p_rank ;
187 const unsigned id_end = nPerProc * ( p_rank + 1 );
188 const unsigned nLocalNode = nPerProc + ( 1 < p_size ? 1 : 0 );
189 const unsigned nLocalElement = nPerProc ;
190
191 const unsigned p_send = ( p_rank + 1 ) % p_size ;
192 const unsigned id_send = id_end - 2 ;
193 const unsigned id_recv = ( id_begin + id_total - 2 ) % id_total ;
194
195 Selector select_used = meta.locally_owned_part() |
196 meta.globally_shared_part();
197
198 std::vector<size_t> local_count ;
199 std::vector<EntityProc> change ;
200
201 Entity send_element_1 = bulk.get_entity( stk::topology::ELEMENT_RANK , ring.m_element_ids[ id_send ] );
202 Entity send_element_2 = bulk.get_entity( stk::topology::ELEMENT_RANK , ring.m_element_ids[ id_send + 1 ] );
203
204 Entity send_node_1 = *(bulk.begin_nodes(send_element_1) + 1);
205 Entity send_node_2 = *(bulk.begin_nodes(send_element_2) + 1);
206
207 Entity recv_element_1 = bulk.get_entity( stk::topology::ELEMENT_RANK , ring.m_element_ids[ id_recv ] );
208 Entity recv_element_2 = bulk.get_entity( stk::topology::ELEMENT_RANK , ring.m_element_ids[ id_recv + 1 ] );
209
210 ASSERT_TRUE( bulk.is_valid(send_element_1) && p_rank == bulk.parallel_owner_rank(send_element_1) );
211 ASSERT_TRUE( bulk.is_valid(send_element_2) && p_rank == bulk.parallel_owner_rank(send_element_2) );
212 ASSERT_TRUE( !bulk.is_valid(recv_element_1) || p_rank != bulk.parallel_owner_rank(recv_element_1) );
213 ASSERT_TRUE( !bulk.is_valid(recv_element_2) || p_rank != bulk.parallel_owner_rank(recv_element_2) );
214
215 if ( p_rank == bulk.parallel_owner_rank(send_node_1) ) {
216 EntityProc entry( send_node_1 , p_send );
217 change.push_back( entry );
218 }
219 if ( p_rank == bulk.parallel_owner_rank(send_node_2) ) {
220 EntityProc entry( send_node_2 , p_send );
221 change.push_back( entry );
222 }
223 {
224 EntityProc entry( send_element_1 , p_send );
225 change.push_back( entry );
226 }
227 {
228 EntityProc entry( send_element_2 , p_send );
229 change.push_back( entry );
230 }
231
232 send_element_1 = Entity();
233 send_element_2 = Entity();
234 send_node_1 = Entity();
235 send_node_2 = Entity();
236 recv_element_1 = Entity();
237 recv_element_2 = Entity();
238
239 bulk.change_entity_owner( change );
240
241 send_element_1 = bulk.get_entity( stk::topology::ELEMENT_RANK , ring.m_element_ids[ id_send ] );
242 send_element_2 = bulk.get_entity( stk::topology::ELEMENT_RANK , ring.m_element_ids[ id_send + 1 ] );
243 recv_element_1 = bulk.get_entity( stk::topology::ELEMENT_RANK , ring.m_element_ids[ id_recv ] );
244 recv_element_2 = bulk.get_entity( stk::topology::ELEMENT_RANK , ring.m_element_ids[ id_recv + 1 ] );
245
246 ASSERT_TRUE( !bulk.is_valid(send_element_1) || p_rank != bulk.parallel_owner_rank(send_element_1) );
247 ASSERT_TRUE( !bulk.is_valid(send_element_2) || p_rank != bulk.parallel_owner_rank(send_element_2) );
248 ASSERT_TRUE( bulk.is_valid(recv_element_1) && p_rank == bulk.parallel_owner_rank(recv_element_1) );
249 ASSERT_TRUE( bulk.is_valid(recv_element_2) && p_rank == bulk.parallel_owner_rank(recv_element_2) );
250
251 stk::mesh::count_entities( select_used , bulk , local_count );
252 ASSERT_EQ( local_count[stk::topology::NODE_RANK] , nLocalNode );
253 ASSERT_EQ( local_count[stk::topology::ELEMENT_RANK] , nLocalElement );
254
255 unsigned count_shared = 0 ;
256 for ( stk::mesh::EntityCommListInfoVector::const_iterator
257 i = bulk.my_internal_comm_list().begin() ;
258 i != bulk.my_internal_comm_list().end() ; ++i )
259 {
260 if ( bulk.in_shared( i->key ) ) { ++count_shared ; }
261 }
262 ASSERT_EQ( count_shared , 2u );
263
264 {
265 const EntityKey node_recv = EntityKey(NODE_RANK , ring.m_node_ids[id_recv]);
266 const EntityKey node_send = EntityKey(NODE_RANK , ring.m_node_ids[id_send]);
267
268 std::vector<int> node_recv_shared_procs;
269 bulk.comm_shared_procs(node_recv,node_recv_shared_procs);
270 ASSERT_EQ( node_recv_shared_procs.size(), 1u );
271 std::vector<int> node_send_shared_procs;
272 bulk.comm_shared_procs(node_send,node_send_shared_procs);
273 ASSERT_EQ( node_send_shared_procs.size(), 1u );
274 }
275 }
276
277 }
278 }