Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/edge_creation/UnitTestEdgeConnection.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 #include <gtest/gtest.h>
35 #include <stk_topology/topology.hpp>
36 #include <stk_mesh/base/BulkData.hpp>
37 #include <stk_mesh/base/MetaData.hpp>
38 #include <stk_mesh/base/GetEntities.hpp>
39 #include <stk_mesh/baseImpl/MeshImplUtils.hpp>
40 #include <stk_mesh/baseImpl/ConnectEdgesImpl.hpp>
41 #include <stk_mesh/base/Types.hpp>
42 #include <stk_mesh/base/Comm.hpp>
43 #include <stk_mesh/base/FEMHelpers.hpp>
44 #include <stk_mesh/base/SkinBoundary.hpp>
45 #include <stk_unit_test_utils/GetMeshSpec.hpp>
46 #include <stk_unit_test_utils/TextMesh.hpp>
47 #include <stk_unit_test_utils/BuildMesh.hpp>
48 #include <stk_io/FillMesh.hpp>
49 #include <stk_io/IossBridge.hpp>
50 #include <stk_io/StkMeshIoBroker.hpp>
51 #include <stk_util/parallel/Parallel.hpp>
52
53 using stk::unit_test_util::build_mesh;
54
55 bool is_valid_element_edge(const stk::mesh::BulkData& bulk, stk::mesh::Entity elem, stk::mesh::Entity edge)
56 {
57 EXPECT_EQ(stk::topology::ELEM_RANK, bulk.entity_rank(elem));
58 EXPECT_EQ(stk::topology::EDGE_RANK, bulk.entity_rank(edge));
59
60 stk::mesh::EntityVector edgeNodes(bulk.begin_nodes(edge), bulk.end_nodes(edge));
61
62 stk::mesh::OrdinalAndPermutation ordinalAndPerm = stk::mesh::get_ordinal_and_permutation(bulk, elem,
63 stk::topology::EDGE_RANK, edgeNodes);
64
65 return (ordinalAndPerm.first != stk::mesh::INVALID_CONNECTIVITY_ORDINAL);
66 }
67
68 TEST(EdgeConnectorTest, CheckValidEdge)
69 {
70 if(stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { return; }
71
72 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(3, MPI_COMM_WORLD);
73 stk::mesh::BulkData& bulk = *bulkPtr;
74 stk::io::fill_mesh("generated:1x1x1", bulk);;
75
76 stk::mesh::Entity elem = bulk.get_entity(stk::topology::ELEM_RANK, 1u);
77 bulk.modification_begin();
78 stk::mesh::Entity edge = stk::mesh::declare_element_edge(bulk, 1u, elem, 0u);
79 bulk.modification_end();
80 const stk::mesh::Entity* nodes = bulk.begin_nodes(edge);
81 unsigned numNodes = bulk.num_nodes(edge);
82
83 EXPECT_EQ(2u, numNodes);
84 EXPECT_EQ(1u, bulk.identifier(nodes[0]));
85 EXPECT_EQ(2u, bulk.identifier(nodes[1]));
86
87 EXPECT_TRUE(is_valid_element_edge(bulk, elem, edge));
88 }
89
90 TEST(EdgeConnectorTest, CheckInvalidEdge)
91 {
92 if(stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { return; }
93
94 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(3, MPI_COMM_WORLD);
95 stk::mesh::BulkData& bulk = *bulkPtr;
96 stk::io::fill_mesh("generated:1x1x1", bulk);;
97
98 stk::mesh::Entity elem = bulk.get_entity(stk::topology::ELEM_RANK, 1u);
99 stk::mesh::Entity node1 = bulk.get_entity(stk::topology::NODE_RANK, 1u);
100 stk::mesh::Entity node7 = bulk.get_entity(stk::topology::NODE_RANK, 7u);
101
102 stk::mesh::Part& edgePart = bulk.mesh_meta_data().get_topology_root_part(stk::topology::LINE_2);
103 stk::mesh::PartVector edgeParts = { &edgePart };
104
105 bulk.modification_begin();
106 stk::mesh::Entity edge = bulk.declare_edge(1u, edgeParts);
107 bulk.declare_relation(edge, node1, 0u);
108 bulk.declare_relation(edge, node7, 1u);
109 bulk.modification_end();
110
111 const stk::mesh::Entity* nodes = bulk.begin_nodes(edge);
112 unsigned numNodes = bulk.num_nodes(edge);
113
114 EXPECT_EQ(2u, numNodes);
115 EXPECT_EQ(1u, bulk.identifier(nodes[0]));
116 EXPECT_EQ(7u, bulk.identifier(nodes[1]));
117
118 EXPECT_FALSE(is_valid_element_edge(bulk, elem, edge));
119 }
120
121 TEST(EdgeConnectorTest, CheckConnectElemToValidEdge)
122 {
123 if(stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { return; }
124
125 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(3, MPI_COMM_WORLD);
126 stk::mesh::BulkData& bulk = *bulkPtr;
127 stk::io::fill_mesh("generated:1x1x1", bulk);;
128
129 stk::mesh::Entity elem = bulk.get_entity(stk::topology::ELEM_RANK, 1u);
130 stk::mesh::Entity node1 = bulk.get_entity(stk::topology::NODE_RANK, 1u);
131 stk::mesh::Entity node2 = bulk.get_entity(stk::topology::NODE_RANK, 2u);
132 stk::mesh::Part& edgePart = bulk.mesh_meta_data().get_topology_root_part(stk::topology::LINE_2);
133 stk::mesh::PartVector edgeParts = { &edgePart };
134
135 bulk.modification_begin();
136 stk::mesh::Entity edge = bulk.declare_edge(1u, edgeParts);
137 bulk.declare_relation(edge, node1, 0u);
138 bulk.declare_relation(edge, node2, 1u);
139 stk::mesh::impl::connect_edge_to_elements(bulk, edge);
140 bulk.modification_end();
141
142 EXPECT_EQ(1u, bulk.num_elements(edge));
143 const stk::mesh::Entity* edgeElement = bulk.begin_elements(edge);
144 EXPECT_EQ(edgeElement[0], elem);
145
146 EXPECT_EQ(1u, bulk.num_edges(elem));
147 const stk::mesh::Entity* elementEdge = bulk.begin_edges(elem);
148 EXPECT_EQ(elementEdge[0], edge);
149 }
150
151 TEST(EdgeConnectorTest, CheckConnectElemToInvalidEdge)
152 {
153 if(stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { return; }
154
155 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(3, MPI_COMM_WORLD);
156 stk::mesh::BulkData& bulk = *bulkPtr;
157 stk::io::fill_mesh("generated:1x1x1", bulk);;
158
159 stk::mesh::Entity elem = bulk.get_entity(stk::topology::ELEM_RANK, 1u);
160 stk::mesh::Entity node1 = bulk.get_entity(stk::topology::NODE_RANK, 1u);
161 stk::mesh::Entity node7 = bulk.get_entity(stk::topology::NODE_RANK, 7u);
162 stk::mesh::Part& edgePart = bulk.mesh_meta_data().get_topology_root_part(stk::topology::LINE_2);
163 stk::mesh::PartVector edgeParts = { &edgePart };
164
165 bulk.modification_begin();
166 stk::mesh::Entity edge = bulk.declare_edge(1u, edgeParts);
167 bulk.declare_relation(edge, node1, 0u);
168 bulk.declare_relation(edge, node7, 1u);
169 EXPECT_FALSE(stk::mesh::impl::connect_edge_to_elements(bulk, edge));
170 bulk.modification_end();
171
172 EXPECT_EQ(0u, bulk.num_elements(edge));
173 EXPECT_EQ(0u, bulk.num_edges(elem));
174 }
175
176 TEST(StkEdgeIo, ParallelWriteMesh)
177 {
178 if(stk::parallel_machine_size(MPI_COMM_WORLD) != 2) { return; }
179
180 std::string filename = "output.exo";
181 {
182 std::string meshDesc = stk::unit_test_util::get_many_block_mesh_desc(2, 2);
183 std::vector<double> coords = stk::unit_test_util::get_many_block_coordinates(2);
184 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(3, MPI_COMM_WORLD);
185 stk::mesh::BulkData& bulk = *bulkPtr;
186 stk::mesh::MetaData& meta = bulkPtr->mesh_meta_data();
187 stk::mesh::Part* part = &meta.declare_part_with_topology("edgeBlock", stk::topology::LINE_2);
188 stk::io::put_edge_block_io_part_attribute(*part);
189 stk::unit_test_util::setup_text_mesh(bulk, stk::unit_test_util::get_full_text_mesh_desc(meshDesc, coords));
190 stk::mesh::create_edges(bulk, meta.universal_part(), part);
191 const stk::mesh::BucketVector& buckets = bulk.buckets(stk::topology::EDGE_RANK);
192
193 unsigned numLocalEdges = stk::mesh::count_selected_entities(meta.locally_owned_part(), buckets);
194 unsigned expectedVal = (bulk.parallel_rank() == 0) ? 12u : 8u;
195 EXPECT_EQ(expectedVal, numLocalEdges);
196
197 stk::io::StkMeshIoBroker stkIo;
198 stkIo.set_bulk_data(bulk);
199 size_t outputFileIndex = stkIo.create_output_mesh(filename, stk::io::WRITE_RESULTS);
200 stkIo.write_output_mesh(outputFileIndex);
201 }
202
203 {
204 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(MPI_COMM_WORLD);
205 stk::mesh::MetaData& meta = bulkPtr->mesh_meta_data();
206 stk::mesh::BulkData& bulk = *bulkPtr;
207 stk::io::fill_mesh(filename, bulk);
208 const stk::mesh::BucketVector& buckets = bulk.buckets(stk::topology::EDGE_RANK);
209
210 std::vector<size_t> entityCounts;
211 stk::mesh::comm_mesh_counts(bulk, entityCounts);
212 EXPECT_EQ(20u, entityCounts[stk::topology::EDGE_RANK]);
213 EXPECT_EQ(2u, entityCounts[stk::topology::ELEM_RANK]);
214
215 unsigned numLocalEdges = stk::mesh::count_selected_entities(meta.locally_owned_part(), buckets);
216 unsigned expectedVal = (bulk.parallel_rank() == 0) ? 12u : 8u;
217 EXPECT_EQ(expectedVal, numLocalEdges);
218 }
219
220 std::string pllFileName = filename+"."+std::to_string(stk::parallel_machine_size(MPI_COMM_WORLD))+"."+std::to_string(stk::parallel_machine_rank(MPI_COMM_WORLD));
221 unlink(pllFileName.c_str());
222 }
223
224 TEST(StkEdgeIo, ParallelWriteMeshWithFace)
225 {
226 if(stk::parallel_machine_size(MPI_COMM_WORLD) != 2) { return; }
227
228 std::string filename = "output.exo";
229 {
230 std::string meshDesc = stk::unit_test_util::get_many_block_mesh_desc(2, 2);
231 std::vector<double> coords = stk::unit_test_util::get_many_block_coordinates(2);
232 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(3, MPI_COMM_WORLD);
233 stk::mesh::MetaData& meta = bulkPtr->mesh_meta_data();
234 stk::mesh::BulkData& bulk = *bulkPtr;
235 stk::mesh::Part* edgePart = &meta.declare_part_with_topology("edgeBlock", stk::topology::LINE_2);
236 stk::mesh::Part* facePart = &meta.declare_part_with_topology("faceBlock", stk::topology::QUAD_4);
237 stk::io::put_edge_block_io_part_attribute(*edgePart);
238 stk::io::put_io_part_attribute(*facePart);
239 stk::unit_test_util::setup_text_mesh(bulk, stk::unit_test_util::get_full_text_mesh_desc(meshDesc, coords));
240
241 stk::mesh::PartVector faceParts = {facePart};
242 stk::mesh::create_interior_block_boundary_sides(bulk, meta.universal_part(), faceParts);
243 stk::mesh::create_edges(bulk, meta.universal_part(), edgePart);
244 const stk::mesh::BucketVector& buckets = bulk.buckets(stk::topology::EDGE_RANK);
245
246 unsigned numLocalEdges = stk::mesh::count_selected_entities(meta.locally_owned_part(), buckets);
247 unsigned expectedVal = (bulk.parallel_rank() == 0) ? 12u : 8u;
248 EXPECT_EQ(expectedVal, numLocalEdges);
249
250 stk::mesh::EntityVector faces;
251 stk::mesh::get_selected_entities(*facePart, bulk.buckets(stk::topology::FACE_RANK), faces);
252 EXPECT_EQ(1u, faces.size());
253 EXPECT_EQ(4u, bulk.num_edges(faces[0]));
254
255 stk::io::StkMeshIoBroker stkIo;
256 stkIo.set_bulk_data(bulk);
257 size_t outputFileIndex = stkIo.create_output_mesh(filename, stk::io::WRITE_RESULTS);
258 stkIo.write_output_mesh(outputFileIndex);
259 }
260
261 {
262 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(3, MPI_COMM_WORLD);
263 stk::mesh::MetaData& meta = bulkPtr->mesh_meta_data();
264 stk::mesh::BulkData& bulk = *bulkPtr;
265 stk::io::fill_mesh(filename, bulk);
266 const stk::mesh::BucketVector& buckets = bulk.buckets(stk::topology::EDGE_RANK);
267
268 std::vector<size_t> entityCounts;
269 stk::mesh::comm_mesh_counts(bulk, entityCounts);
270 EXPECT_EQ(20u, entityCounts[stk::topology::EDGE_RANK]);
271 EXPECT_EQ(2u, entityCounts[stk::topology::ELEM_RANK]);
272
273 unsigned numLocalEdges = stk::mesh::count_selected_entities(meta.locally_owned_part(), buckets);
274 unsigned expectedVal = (bulk.parallel_rank() == 0) ? 12u : 8u;
275 EXPECT_EQ(expectedVal, numLocalEdges);
276
277 stk::mesh::EntityVector faces;
278 stk::mesh::get_selected_entities(meta.universal_part(), bulk.buckets(stk::topology::FACE_RANK), faces);
279 EXPECT_EQ(1u, faces.size());
280 EXPECT_EQ(4u, bulk.num_edges(faces[0]));
281 }
282
283 std::string pllFileName = filename+"."+std::to_string(stk::parallel_machine_size(MPI_COMM_WORLD))+"."+std::to_string(stk::parallel_machine_rank(MPI_COMM_WORLD));
284 unlink(pllFileName.c_str());
285 }