Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/UnitTestRelation.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 <stdexcept> // for runtime_error
36 #include <stk_mesh/base/BulkData.hpp> // for BulkData
37 #include <stk_mesh/base/FindPermutation.hpp>
38 #include <stk_mesh/base/MetaData.hpp> // for MetaData, entity_rank_names
39 #include <stk_util/parallel/Parallel.hpp> // for ParallelMachine, etc
40 #include <gtest/gtest.h>
41 #include <string> // for string
42 #include <vector> // for vector, vector<>::iterator
43
44 #include "stk_mesh/base/Bucket.hpp" // for Bucket
45 #include "stk_mesh/base/Entity.hpp" // for Entity, operator<<
46 #include "stk_mesh/base/Types.hpp" // for ConnectivityOrdinal, etc
47 #include "stk_topology/topology.hpp" // for topology, etc
48 #include "stk_unit_test_utils/stk_mesh_fixtures/BoxFixture.hpp"
49 #include "stk_unit_test_utils/stk_mesh_fixtures/HexFixture.hpp" // for HexFixture
50 #include "stk_unit_test_utils/stk_mesh_fixtures/RingFixture.hpp" // for RingFixture
51 #include "stk_unit_test_utils/BuildMesh.hpp"
52 namespace stk { namespace mesh { class Ghosting; } }
53 namespace stk { namespace mesh { class Part; } }
54 namespace stk { namespace mesh { class Relation; } }
55 namespace stk { namespace mesh { class Selector; } }
56 namespace stk { namespace mesh { namespace fixtures { class BoxFixture; } } }
57
58 using stk::mesh::Entity;
59 using stk::mesh::EntityRank;
60 using stk::mesh::EntityVector;
61 using stk::mesh::Part;
62 using stk::mesh::Relation;
63 using stk::mesh::Selector;
64 using stk::mesh::EntityId;
65 using stk::mesh::MetaData;
66 using stk::mesh::BulkData;
67 using stk::mesh::Ghosting;
68 using stk::mesh::fixtures::BoxFixture;
69 using stk::mesh::fixtures::HexFixture;
70 using stk::mesh::fixtures::RingFixture;
71 using stk::unit_test_util::build_mesh;
72
73 namespace {
74
75 TEST(UnitTestingOfRelation, testRelationCoverage)
76 {
77 // Test some relation error cases
78
79 stk::ParallelMachine pm = MPI_COMM_WORLD;
80 MPI_Barrier ( MPI_COMM_WORLD );
81
82 // Just use any random fixture for convenience
83 HexFixture fixture(pm, 3 /*x*/, 3 /*y*/, 3 /*z*/);
84 MetaData& meta = fixture.m_meta;
85 BulkData& bulk = fixture.m_bulk_data;
86
87 meta.commit();
88
89 fixture.generate_mesh();
90
91 Entity node0 = (*bulk.buckets(stk::topology::NODE_RANK)[0])[0];
92
93 bulk.modification_begin();
94 std::vector<Part*> empty_parts;
95 stk::mesh::EntityId new_id = bulk.parallel_rank() + 1;
96 Entity edge = bulk.declare_edge(new_id, empty_parts);
97
98 // Cannot declare a back relation
99 ASSERT_THROW ( bulk.declare_relation ( node0 , edge , 0 /*rel ord*/) , std::runtime_error );
100 }
101
102 TEST(UnitTestingOfRelation, testRelationNoGhosting)
103 {
104 stk::ParallelMachine pm = MPI_COMM_WORLD;
105
106 const unsigned p_rank = stk::parallel_machine_rank( pm );
107 const unsigned p_size = stk::parallel_machine_size( pm );
108
109 const unsigned nPerProc = 10;
110
111 RingFixture mesh( pm , nPerProc , false /* No element parts */, stk::mesh::BulkData::NO_AUTO_AURA );
112 mesh.m_meta_data.commit();
113
114 BulkData& ring_bulk = mesh.m_bulk_data;
115
116 ring_bulk.modification_begin();
117 mesh.generate_mesh( );
118 ASSERT_TRUE(ring_bulk.modification_end());
119
120 mesh.fixup_node_ownership();
121
122 // This process' first element in the loop
123 // if a parallel mesh has a shared node
124
125 Entity elementnew = ring_bulk.get_entity( stk::topology::ELEMENT_RANK , mesh.m_element_ids[ nPerProc * p_rank ] );
126
127 ring_bulk.modification_begin();
128 for ( unsigned p = 0 ; p < p_size ; ++p ) {
129 if ( p != p_rank ) {
130 ASSERT_EQ( ring_bulk.in_shared( ring_bulk.entity_key(elementnew) , p ), false );
131 ASSERT_EQ( ring_bulk.in_send_ghost( ring_bulk.entity_key(elementnew) , p ), false );
132 }
133 }
134
135 elementnew = ring_bulk.get_entity( stk::topology::ELEMENT_RANK , mesh.m_element_ids[ nPerProc * p_rank ] );
136 ASSERT_EQ( ring_bulk.in_send_ghost( ring_bulk.entity_key(elementnew) , p_rank+100 ), false );
137
138 Entity node = ring_bulk.get_entity( stk::topology::NODE_RANK , mesh.m_node_ids[ nPerProc * p_rank ] );
139 ASSERT_EQ( ring_bulk.in_shared( ring_bulk.entity_key(node) , p_rank+100 ), false );
140 }
141
142 TEST(UnitTestingOfRelation, testRelationWithGhosting)
143 {
144 stk::ParallelMachine pm = MPI_COMM_WORLD;
145
146 const unsigned p_rank = stk::parallel_machine_rank( pm );
147 const unsigned p_size = stk::parallel_machine_size( pm );
148
149 const unsigned nPerProc = 10;
150
151 if ( 1 < p_size ) { // With ghosting
152 RingFixture mesh( pm , nPerProc , false /* No element parts */ );
153 mesh.m_meta_data.commit();
154 BulkData& bulk = mesh.m_bulk_data;
155
156 bulk.modification_begin();
157 mesh.generate_mesh();
158 ASSERT_TRUE(bulk.modification_end());
159
160 mesh.fixup_node_ownership();
161
162 const unsigned nNotOwned = nPerProc * p_rank ;
163
164 // The not-owned shared entity:
165 Entity node = bulk.get_entity( stk::topology::NODE_RANK , mesh.m_node_ids[ nNotOwned ] );
166
167 bulk.modification_begin();
168
169 for ( unsigned p = 0 ; p < p_size ; ++p ) {
170 if ( p != p_rank ) {
171 ASSERT_EQ( bulk.in_send_ghost( bulk.entity_key(node) , p ), false );
172 }
173 }
174
175 //not owned and not shared
176 Entity node2 = bulk.get_entity( stk::topology::NODE_RANK , mesh.m_node_ids[ nPerProc * p_rank ] );
177
178 ASSERT_EQ( bulk.in_shared( bulk.entity_key(node2) , p_rank+100 ), false );
179 ASSERT_EQ( bulk.in_send_ghost( bulk.entity_key(node) , p_rank+100 ), false );
180 }
181 }
182
183 TEST(UnitTestingOfRelation, testDegenerateRelation)
184 {
185 // Test that, if you set up degenerate relations, only of the relations
186 // is deleted when you destroy one of the degenerate relations.
187 // BulkData::destroy_relation has been changed to take a relation-id so
188 // that it can work this way.
189 //
190 // To test this, we set up an element that has several relations
191 // to the same node and then delete them one by one.
192
193 stk::ParallelMachine pm = MPI_COMM_WORLD;
194 MPI_Barrier( MPI_COMM_WORLD );
195
196 // Set up meta and bulk data
197 const unsigned spatial_dim = 2;
198 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatial_dim, pm);
199 stk::mesh::MetaData& meta_data = bulkPtr->mesh_meta_data();
200 stk::mesh::BulkData& mesh = *bulkPtr;
201 meta_data.commit();
202 unsigned p_rank = mesh.parallel_rank();
203
204 // Begin modification cycle so we can create the entities and relations
205 mesh.modification_begin();
206
207 // We're just going to add everything to the universal part
208 stk::mesh::PartVector empty_parts;
209
210 // Create element
211 Entity elem = mesh.declare_element(p_rank+1 /*elem_id*/, empty_parts);
212
213 // Create node
214 Entity node = mesh.declare_node(p_rank+1 /*node_id*/, empty_parts);
215
216 // Add degenerate relations
217 const unsigned nodes_per_elem = 4;
218 for (unsigned i = 0; i < nodes_per_elem; ++i) {
219 mesh.declare_relation( elem, node, i );
220 }
221
222 // Elem should have nodes-per-elem relations
223 ASSERT_EQ( nodes_per_elem, static_cast<unsigned>(mesh.num_nodes(elem)) );
224
225 // Destroy relation one-by-one, always checking that appropriate number
226 // of relations remain.
227 for (unsigned i = 0; i < nodes_per_elem; ++i) {
228 mesh.destroy_relation( elem, node, i );
229 ASSERT_EQ( nodes_per_elem - (i+1), static_cast<unsigned>(mesh.num_nodes(elem)) );
230 }
231 }
232
233 TEST(UnitTestingOfRelation, testRelationExtraRank)
234 {
235 stk::ParallelMachine pm = MPI_COMM_WORLD;
236
237 const unsigned spatial_dim = 3;
238 const unsigned num_ext_ranks = 1;
239 const char * ext_rank_names[num_ext_ranks] = { "EXT_RANK_0" };
240 std::vector<std::string> entity_rank_names = stk::mesh::entity_rank_names();
241 for (unsigned i = 0; i < num_ext_ranks; ++i) {
242 entity_rank_names.push_back(ext_rank_names[i]);
243 }
244
245 stk::mesh::MeshBuilder builder(pm);
246 builder.set_spatial_dimension(spatial_dim);
247 builder.set_entity_rank_names(entity_rank_names);
248 std::shared_ptr<BulkData> bulkPtr = builder.create();
249
250 MetaData& meta_data = bulkPtr->mesh_meta_data();
251 Part &hex8_part = meta_data.declare_part_with_topology("Hex8_Part", stk::topology::HEX_8);
252 meta_data.commit();
253 BulkData& mesh = *bulkPtr;
254
255 std::vector<EntityRank> ext_ranks;
256 for (unsigned i = 0; i < num_ext_ranks; ++i) {
257 ext_ranks.push_back(meta_data.entity_rank(ext_rank_names[i]));
258 }
259
260 const unsigned p_rank = mesh.parallel_rank();
261 stk::mesh::PartVector empty_parts;
262 unsigned new_ent_id = (p_rank << 16) + 1;
263
264 // baseline
265 stk::mesh::PartVector elem_parts;
266 elem_parts.push_back(&hex8_part);
267 mesh.modification_begin();
268 Entity elem = mesh.declare_element(new_ent_id++, elem_parts);
269 Entity node;
270 for (unsigned i = 0; i < 8; ++i)
271 {
272 node = mesh.declare_node(new_ent_id++, empty_parts);
273 mesh.declare_relation(elem, node, i);
274 }
275 mesh.modification_end();
276
277 ASSERT_FALSE(mesh.num_connectivity(elem, ext_ranks[0]));
278
279 stk::mesh::ConnectivityOrdinal ord_count = static_cast<stk::mesh::ConnectivityOrdinal>(0);
280
281 // simplest test for get_others_offset_range(..) and other_entities_have_single_rank(..)
282 mesh.modification_begin();
283 Entity ere_0_0 = mesh.declare_entity(ext_ranks[0], new_ent_id++, empty_parts);
284 Entity ere_0_1 = mesh.declare_entity(ext_ranks[0], new_ent_id++, empty_parts);
285 mesh.declare_relation(ere_0_0, node, ++ord_count);
286 mesh.declare_relation(ere_0_1, node, ++ord_count);
287 mesh.declare_relation(ere_0_0, elem, ++ord_count);
288 mesh.declare_relation(ere_0_1, elem, ++ord_count);
289 mesh.modification_end();
290 {
291 EXPECT_EQ(mesh.num_connectivity(node, ext_ranks[0]), 2u);
292 EXPECT_EQ(mesh.num_connectivity(elem, ext_ranks[0]), 2u);
293 }
294 }
295
296 TEST(UnitTestingOfRelation, testDoubleDeclareOfRelation)
297 {
298 // It should be legal to declare the same relation between shared
299 // entities on two procs.
300 //
301 // 1---3---5
302 // | 1 | 2 |
303 // 2---4---6
304 //
305 // To test this, we use the mesh above, with elem 1 going on rank 0 and
306 // elem 2 going on rank 1. Nodes 3,4 are shared along with the edge between
307 // nodes 3 and 4. On both procs we declare relations from the shared edge
308 // to the shared nodes on both procs.
309 //
310 // TODO: If we change how declare_relation works, not requiring all
311 // sharers to declare the same relations, but instead allowing just
312 // the owner to declare relations, that should be tested here.
313
314 stk::ParallelMachine pm = MPI_COMM_WORLD;
315 MPI_Barrier( MPI_COMM_WORLD );
316
317 // Set up meta and bulk data
318 const unsigned spatial_dim = 2;
319 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatial_dim, pm);
320 stk::mesh::MetaData& meta_data = bulkPtr->mesh_meta_data();
321 stk::mesh::BulkData& mesh = *bulkPtr;
322 Part &quad4_part = meta_data.declare_part_with_topology("quad4_part", stk::topology::QUAD_4_2D);
323 Part &line2_part = meta_data.declare_part_with_topology("LINE2_part", stk::topology::LINE_2);
324 meta_data.commit();
325 unsigned p_rank = mesh.parallel_rank();
326 unsigned p_size = mesh.parallel_size();
327
328 if (p_size != 2) {
329 return;
330 }
331
332 Entity edge = Entity();
333 EntityVector nodes;
334 const unsigned nodes_per_elem = 4, nodes_per_side = 2;
335
336 // Begin modification cycle so we can create the entities and relations
337 mesh.modification_begin();
338
339 // entities with EDGE_RANK, FACE_RANK, or ELEMENT_RANK need topology before
340 // modification_end() is called.
341 stk::mesh::PartVector elems_parts, sides_parts, empty_parts;
342 elems_parts.push_back(&quad4_part);
343 sides_parts.push_back(&line2_part);
344
345 // Create element
346 Entity elem = mesh.declare_element(p_rank+1 /*elem_id*/, elems_parts);
347
348 // Create nodes
349 const unsigned starting_node_id = p_rank * nodes_per_side + 1;
350 for (unsigned id = starting_node_id; id < starting_node_id + nodes_per_elem; ++id) {
351 nodes.push_back(mesh.declare_node(id, empty_parts));
352 }
353
354 // Add relations to nodes
355 unsigned rel_id = 0;
356 for (EntityVector::iterator itr = nodes.begin(); itr != nodes.end(); ++itr, ++rel_id) {
357 mesh.declare_relation( elem, *itr, rel_id );
358 }
359
360 stk::mesh::EntityVector side_nodes(2);
361 side_nodes[0] = mesh.get_entity(stk::topology::NODE_RANK, 3);
362 side_nodes[1] = mesh.get_entity(stk::topology::NODE_RANK, 4);
363 int otherProc = (1 - p_rank);
364 mesh.add_node_sharing(side_nodes[0], otherProc);
365 mesh.add_node_sharing(side_nodes[1], otherProc);
366
367 // Create side (edge)
368 unsigned local_side_id = 2;
369 if (p_rank == 1)
370 {
371 local_side_id = 0;
372 }
373 edge = mesh.declare_element_side(elem, local_side_id, sides_parts);
374
375 stk::topology elem_top = mesh.bucket(elem).topology();
376 stk::mesh::Permutation perm1 = stk::mesh::find_permutation(mesh,
377 elem_top, nodes.data(), elem_top.side_topology(local_side_id), side_nodes.data(), local_side_id);
378 ASSERT_TRUE(perm1 != stk::mesh::Permutation::INVALID_PERMUTATION);
379
380 // Set up duplicate relations from edge to nodes
381 rel_id = 0;
382 const unsigned starting_node_idx = (1 - p_rank) * nodes_per_side;
383 for (unsigned node_idx = starting_node_idx;
384 node_idx < starting_node_idx + nodes_per_side;
385 ++node_idx, ++rel_id) {
386 mesh.declare_relation( edge, nodes[node_idx], rel_id );
387 }
388 mesh.modification_end();
389
390 }
391
392 }