Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/UnitTestRootTopologyCost.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 <algorithm>
37
38 #include <stk_mesh/base/FEMHelpers.hpp> // for declare_element
39 #include <stk_unit_test_utils/BuildMesh.hpp>
40
41 using stk::unit_test_util::build_mesh;
42
43 void make_small_hybrid_mesh(stk::mesh::MetaData &meta, stk::mesh::BulkData &mesh,
44 bool user_attempt_no_induce = false, bool user_parts_force_no_induce = true)
45 {
46 stk::ParallelMachine pm = MPI_COMM_WORLD;
47 int p_size = stk::parallel_machine_size(pm);
48
49 if(p_size > 2)
50 {
51 return;
52 }
53
54 const unsigned p_rank = mesh.parallel_rank();
55
56 stk::mesh::Part * hexPart = &meta.get_topology_root_part(stk::topology::HEX_8);
57 stk::mesh::Part * pyrPart = &meta.get_topology_root_part(stk::topology::PYRAMID_5);
58 stk::mesh::Part * tetPart = &meta.get_topology_root_part(stk::topology::TET_4);
59
60 if (user_attempt_no_induce)
61 {
62 hexPart = &meta.declare_part_with_topology("my_hex_part",stk::topology::HEX_8, user_parts_force_no_induce);
63 pyrPart = &meta.declare_part_with_topology("my_pyr_part",stk::topology::PYRAMID_5, user_parts_force_no_induce);
64 tetPart = &meta.declare_part_with_topology("my_tet_part",stk::topology::TET_4, user_parts_force_no_induce);
65
66 EXPECT_EQ(user_parts_force_no_induce, hexPart->force_no_induce());
67 EXPECT_EQ(user_parts_force_no_induce, pyrPart->force_no_induce());
68 EXPECT_EQ(user_parts_force_no_induce, tetPart->force_no_induce());
69 }
70
71 meta.commit();
72
73 const size_t numHex = 1;
74 stk::mesh::EntityIdVector hexNodeIDs[] {
75 { 1, 2, 3, 4, 5, 6, 7, 8 }
76 };
77 stk::mesh::EntityId hexElemIDs[] = { 1 };
78
79 const size_t numPyr = 1;
80 stk::mesh::EntityIdVector pyrNodeIDs[] {
81 { 5, 6, 7, 8, 9 }
82 };
83 stk::mesh::EntityId pyrElemIDs[] = { 2 };
84
85 const size_t numTet = 4;
86 stk::mesh::EntityIdVector tetNodeIDs[] {
87 { 7, 8, 9, 12 },
88 { 6, 9, 10, 7 },
89 { 7, 9, 10, 12 },
90 { 7, 12, 10, 11 }
91 };
92 stk::mesh::EntityId tetElemIDs[] = { 3, 4, 5, 6 };
93
94 // list of triplets: (owner-proc, shared-nodeID, sharing-proc)
95 std::vector< std::vector<unsigned> > shared_nodeIDs_and_procs
96 {
97 { 0, 5, 1 }, // proc 0
98 { 0, 6, 1 },
99 { 0, 7, 1 },
100 { 0, 8, 1 },
101 { 1, 5, 0 }, // proc 1
102 { 1, 6, 0 },
103 { 1, 7, 0 },
104 { 1, 8, 0 }
105 };
106
107 mesh.modification_begin();
108
109 if (0 == p_rank) {
110 for (size_t i = 0; i < numHex; ++i) {
111 stk::mesh::declare_element(mesh, *hexPart, hexElemIDs[i], hexNodeIDs[i]);
112 }
113 }
114 if ( (1 == p_rank) || (1 == p_size) ) { // setup the pyramids/tets for either np 2 or serial
115 for (size_t i = 0; i < numPyr; ++i) {
116 stk::mesh::declare_element(mesh, *pyrPart, pyrElemIDs[i], pyrNodeIDs[i]);
117 }
118 for (size_t i = 0; i < numTet; ++i) {
119 stk::mesh::declare_element(mesh, *tetPart, tetElemIDs[i], tetNodeIDs[i]);
120 }
121 }
122
123 if (p_size > 1)
124 {
125 for (size_t nodeIdx = 0, end = shared_nodeIDs_and_procs.size(); nodeIdx < end; ++nodeIdx) {
126 if (p_rank == shared_nodeIDs_and_procs[nodeIdx][0]) {
127 stk::mesh::EntityId nodeID = shared_nodeIDs_and_procs[nodeIdx][1];
128 int sharingProc = shared_nodeIDs_and_procs[nodeIdx][2];
129 stk::mesh::Entity node = mesh.get_entity(stk::topology::NODE_RANK, nodeID);
130 mesh.add_node_sharing(node, sharingProc);
131 }
132 }
133 }
134
135 mesh.modification_end();
136 }
137
138
139 TEST(UnitTestRootTopology, autoInduceFragmentation)
140 {
141 stk::ParallelMachine pm = MPI_COMM_WORLD;
142 int p_size = stk::parallel_machine_size(pm);
143
144 if (p_size != 1)
145 {
146 return;
147 }
148
149 const unsigned spatialDim = 3;
150 {
151 // Root topology parts do not induce. No user parts. No bucket fragmentation.
152 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, pm);
153 stk::mesh::MetaData& meta_no_induce = bulkPtr->mesh_meta_data();
154 stk::mesh::BulkData& mesh = *bulkPtr;
155 stk::mesh::PartVector parts = meta_no_induce.get_parts();
156 for (auto part : parts)
157 {
158 meta_no_induce.force_no_induce(*part);
159 }
160 make_small_hybrid_mesh(meta_no_induce, mesh);
161
162 const stk::mesh::BucketVector &node_buckets = mesh.buckets(stk::topology::NODE_RANK);
163 unsigned num_buckets = node_buckets.size();
164 EXPECT_EQ(num_buckets, 1u);
165 }
166
167 {
168 // Root topology parts do induce. No user parts. Bucket fragmentation.
169 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, pm);
170 stk::mesh::MetaData& meta_induce = bulkPtr->mesh_meta_data();
171 stk::mesh::BulkData& mesh = *bulkPtr;
172 make_small_hybrid_mesh(meta_induce, mesh);
173
174 const stk::mesh::BucketVector &node_buckets = mesh.buckets(stk::topology::NODE_RANK);
175 unsigned num_buckets = node_buckets.size();
176 EXPECT_EQ(num_buckets, 5u);
177 }
178
179 {
180 // Root topology parts do not induce. User parts don't induce. No bucket fragmentation.
181 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, pm);
182 stk::mesh::MetaData& meta_no_induce = bulkPtr->mesh_meta_data();
183 stk::mesh::BulkData& mesh = *bulkPtr;
184 stk::mesh::PartVector parts = meta_no_induce.get_parts();
185 for (auto part : parts)
186 {
187 meta_no_induce.force_no_induce(*part);
188 }
189 make_small_hybrid_mesh(meta_no_induce, mesh, true);
190
191 const stk::mesh::BucketVector &node_buckets = mesh.buckets(stk::topology::NODE_RANK);
192 unsigned num_buckets = node_buckets.size();
193 EXPECT_EQ(num_buckets, 1u);
194 }
195
196 {
197 // Root topology parts do induce. User parts induce. Bucket fragmentation.
198 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, pm);
199 stk::mesh::MetaData& meta_induce = bulkPtr->mesh_meta_data();
200 stk::mesh::BulkData& mesh = *bulkPtr;
201 make_small_hybrid_mesh(meta_induce, mesh, true);
202
203 const stk::mesh::BucketVector &node_buckets = mesh.buckets(stk::topology::NODE_RANK);
204 unsigned num_buckets = node_buckets.size();
205 EXPECT_EQ(num_buckets, 5u);
206 }
207
208 {
209 // Root topology parts do not induce. User parts induce. No bucket fragmentation.
210 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, pm);
211 stk::mesh::MetaData& meta_no_induce = bulkPtr->mesh_meta_data();
212 stk::mesh::BulkData& mesh = *bulkPtr;
213 stk::mesh::PartVector parts = meta_no_induce.get_parts();
214 for (auto part : parts)
215 {
216 meta_no_induce.force_no_induce(*part);
217 }
218 make_small_hybrid_mesh(meta_no_induce, mesh, true, false);
219
220 const stk::mesh::BucketVector &node_buckets = mesh.buckets(stk::topology::NODE_RANK);
221 unsigned num_buckets = node_buckets.size();
222 EXPECT_EQ(num_buckets, 5u);
223 }
224
225 {
226 // Root topology parts do induce. User parts induce. Bucket fragmentation.
227 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, pm);
228 stk::mesh::MetaData& meta_induce = bulkPtr->mesh_meta_data();
229 stk::mesh::BulkData& mesh = *bulkPtr;
230 make_small_hybrid_mesh(meta_induce, mesh, true, false);
231
232 const stk::mesh::BucketVector &node_buckets = mesh.buckets(stk::topology::NODE_RANK);
233 unsigned num_buckets = node_buckets.size();
234 EXPECT_EQ(num_buckets, 5u);
235 }
236 }
237
238 void check_parts_allowed(const stk::mesh::BucketVector &buckets, const stk::mesh::PartVector &parts_allowed)
239 {
240 for (unsigned i = 0; i < buckets.size(); ++i)
241 {
242 const stk::mesh::Bucket &bucket = *buckets[i];
243 const stk::mesh::PartVector &parts_found = bucket.supersets();
244
245 for (unsigned j = 0; j < parts_found.size(); ++j)
246 {
247 const stk::mesh::Part *part = parts_found[j];
248 bool found = (std::find(parts_allowed.begin(), parts_allowed.end(), part) != parts_allowed.end());
249 EXPECT_TRUE(found);
250 }
251 }
252 }
253
254
255 TEST(UnitTestRootTopology, unusedPartsNotInBuckets)
256 {
257 stk::ParallelMachine pm = MPI_COMM_WORLD;
258 int p_size = stk::parallel_machine_size(pm);
259
260 if (p_size != 1)
261 {
262 return;
263 }
264
265 const unsigned spatialDim = 3;
266
267 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, pm);
268 stk::mesh::MetaData& meta = bulkPtr->mesh_meta_data();
269 stk::mesh::BulkData& mesh = *bulkPtr;
270 make_small_hybrid_mesh(meta, mesh);
271
272 stk::mesh::PartVector parts_allowed;
273 parts_allowed.push_back(&meta.universal_part());
274 parts_allowed.push_back(&meta.locally_owned_part());
275 parts_allowed.push_back(&meta.get_topology_root_part(stk::topology::NODE));
276 parts_allowed.push_back(&meta.get_topology_root_part(stk::topology::HEXAHEDRON_8));
277 parts_allowed.push_back(&meta.get_topology_root_part(stk::topology::TETRAHEDRON_4));
278 parts_allowed.push_back(&meta.get_topology_root_part(stk::topology::PYRAMID_5));
279
280 size_t num_entities = 0;
281 for (stk::mesh::EntityRank e_rank = stk::topology::NODE_RANK; e_rank <= stk::topology::ELEM_RANK; ++e_rank)
282 {
283 const stk::mesh::BucketVector &buckets = mesh.buckets(e_rank);
284 check_parts_allowed(buckets, parts_allowed);
285
286 for (unsigned bkt_j = 0; bkt_j < buckets.size(); ++bkt_j)
287 {
288 num_entities += buckets[bkt_j]->size();
289 }
290 }
291 EXPECT_EQ(num_entities, 18u);
292 }
293