Debian Codegraph @

packages/stk/stk_doc_tests/stk_mesh/createFacesEdgesHex.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 <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker
37 #include <stk_mesh/base/CreateFaces.hpp> // for create_faces
38 #include <stk_mesh/base/CreateEdges.hpp> // for create_edges
39 #include <stk_mesh/base/GetEntities.hpp> // for count_entities
40 #include <stk_mesh/base/MetaData.hpp> // for MetaData
41 #include <stk_mesh/base/Selector.hpp> // for Selector
42 #include <stk_topology/topology.hpp> // for topology, etc
43 #include <string> // for string
44 #include <vector> // for vector
45 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH
46 #include <stddef.h>
47
48 #include <generated/Iogn_DashSurfaceMesh.h>
49 #include <generated/Iogn_GeneratedMesh.h>
50 #include <generated/Iogn_DatabaseIO.h>
51 #include <Ionit_Initializer.h>
52 #include <Ioss_IOFactory.h>
53 #include <Ioss_Utils.h>
54 #include <Ioss_Region.h>
55
56 namespace stk { namespace mesh { class BulkData; } }
57
58 namespace
59 {
60 //Example1 (for documentation)
61 TEST(StkMeshHowTo, CreateFacesEdgesHex)
62 {
63 // ============================================================
64 // INITIALIZATION
65 MPI_Comm communicator = MPI_COMM_WORLD;
66 if (stk::parallel_machine_size(communicator) != 1) { return; }
67 stk::io::StkMeshIoBroker stkIo(communicator);
68
69 const std::string generatedFileName = "generated:8x8x8";
70 stkIo.add_mesh_database(generatedFileName, stk::io::READ_MESH);
71 stkIo.create_input_mesh();
72 stkIo.populate_bulk_data();
73
74 // ============================================================
75 //+ EXAMPLE
76 //+ Create the faces..
77 stk::mesh::create_faces(stkIo.bulk_data());
78
79 //+ Create the edges..
80 stk::mesh::create_edges(stkIo.bulk_data());
81
82 // ==================================================
83 // VERIFICATION
84 stk::mesh::Selector allEntities = stkIo.meta_data().universal_part();
85 std::vector<size_t> entityCounts;
86 stk::mesh::count_entities(allEntities, stkIo.bulk_data(), entityCounts);
87 EXPECT_EQ( 512u, entityCounts[stk::topology::ELEMENT_RANK]);
88 EXPECT_EQ(1728u, entityCounts[stk::topology::FACE_RANK]);
89 EXPECT_EQ(1944u, entityCounts[stk::topology::EDGE_RANK]);
90 // MAKE SURE FACES ARE HOOKED TO EDGES
91 // this should happen if create_faces is called before create_edges
92 stk::mesh::BucketVector const & face_buckets = stkIo.bulk_data().buckets(stk::topology::FACE_RANK);
93 for (size_t bucket_count=0, bucket_end=face_buckets.size(); bucket_count < bucket_end; ++bucket_count) {
94 stk::mesh::Bucket & bucket = *face_buckets[bucket_count];
95 const unsigned num_expected_edges = bucket.topology().num_edges();
96 EXPECT_EQ(4u, num_expected_edges);
97 for (size_t face_count=0, face_end=bucket.size(); face_count < face_end; ++face_count) {
98 stk::mesh::Entity face = bucket[face_count];
99 EXPECT_EQ(num_expected_edges, stkIo.bulk_data().num_edges(face));
100 }
101 }
102 }
103 //End Example1 (for documentation)
104
105 //for now, this is just a copy of above with a different order of create_edges and create_faces
106 TEST(StkMeshHowTo, CreateEdgesFacesHex)
107 {
108 // ============================================================
109 // INITIALIZATION
110 MPI_Comm communicator = MPI_COMM_WORLD;
111 if (stk::parallel_machine_size(communicator) != 1) { return; }
112 stk::io::StkMeshIoBroker stkIo(communicator);
113
114 const std::string generatedFileName = "generated:8x8x8";
115 stkIo.add_mesh_database(generatedFileName, stk::io::READ_MESH);
116 stkIo.create_input_mesh();
117 stkIo.populate_bulk_data();
118
119 // ============================================================
120 //+ EXAMPLE
121 //+ Create the edges..
122 stk::mesh::create_edges(stkIo.bulk_data());
123
124 //+ Create the faces..
125 stk::mesh::create_faces(stkIo.bulk_data(), true);
126 // ==================================================
127 // VERIFICATION
128 stk::mesh::Selector allEntities = stkIo.meta_data().universal_part();
129 std::vector<size_t> entityCounts;
130 stk::mesh::count_entities(allEntities, stkIo.bulk_data(), entityCounts);
131 EXPECT_EQ( 512u, entityCounts[stk::topology::ELEMENT_RANK]);
132 EXPECT_EQ(1728u, entityCounts[stk::topology::FACE_RANK]);
133 EXPECT_EQ(1944u, entityCounts[stk::topology::EDGE_RANK]);
134 // MAKE SURE FACES ARE HOOKED TO EDGES
135 // this should happen if create_faces is called before create_edges
136 stk::mesh::BucketVector const & face_buckets = stkIo.bulk_data().buckets(stk::topology::FACE_RANK);
137 for (size_t bucket_count=0, bucket_end=face_buckets.size(); bucket_count < bucket_end; ++bucket_count) {
138 stk::mesh::Bucket & bucket = *face_buckets[bucket_count];
139 const unsigned num_expected_edges = bucket.topology().num_edges();
140 EXPECT_EQ(4u, num_expected_edges);
141 for (size_t face_count=0, face_end=bucket.size(); face_count < face_end; ++face_count) {
142 stk::mesh::Entity face = bucket[face_count];
143 EXPECT_EQ(num_expected_edges, stkIo.bulk_data().num_edges(face));
144 }
145 }
146 }
147
148 //for now, this is just a copy of above with a different order of create_edges and create_faces and false
149 //passed in to create_faces for connect_faces_to_edges so we don't connect them together
150 TEST(StkMeshHowTo, CreateEdgesFacesHexNoConnect)
151 {
152 // ============================================================
153 // INITIALIZATION
154 MPI_Comm communicator = MPI_COMM_WORLD;
155 if (stk::parallel_machine_size(communicator) != 1) { return; }
156 stk::io::StkMeshIoBroker stkIo(communicator);
157
158 const std::string generatedFileName = "generated:8x8x8";
159 stkIo.add_mesh_database(generatedFileName, stk::io::READ_MESH);
160 stkIo.create_input_mesh();
161 stkIo.populate_bulk_data();
162
163 // ============================================================
164 //+ EXAMPLE
165 //+ Create the edges..
166 stk::mesh::create_edges(stkIo.bulk_data());
167
168 //+ Create the faces..
169 stk::mesh::create_faces(stkIo.bulk_data(), false); //false means don't connect faces to edges
170 // ==================================================
171 // VERIFICATION
172 stk::mesh::Selector allEntities = stkIo.meta_data().universal_part();
173 std::vector<size_t> entityCounts;
174 stk::mesh::count_entities(allEntities, stkIo.bulk_data(), entityCounts);
175 EXPECT_EQ( 512u, entityCounts[stk::topology::ELEMENT_RANK]);
176 EXPECT_EQ(1728u, entityCounts[stk::topology::FACE_RANK]);
177 EXPECT_EQ(1944u, entityCounts[stk::topology::EDGE_RANK]);
178 // MAKE SURE FACES ARE NOT HOOKED TO EDGES
179 stk::mesh::BucketVector const & face_buckets = stkIo.bulk_data().buckets(stk::topology::FACE_RANK);
180 for (size_t bucket_count=0, bucket_end=face_buckets.size(); bucket_count < bucket_end; ++bucket_count) {
181 stk::mesh::Bucket & bucket = *face_buckets[bucket_count];
182 const unsigned num_expected_edges = bucket.topology().num_edges();
183 EXPECT_EQ(4u, num_expected_edges);
184 for (size_t face_count=0, face_end=bucket.size(); face_count < face_end; ++face_count) {
185 stk::mesh::Entity face = bucket[face_count];
186 EXPECT_EQ(0u, stkIo.bulk_data().num_edges(face)); //expect no edges now
187 }
188 }
189 }
190
191 Iogn::ExodusData createExodusData(int numberOfHexes, stk::mesh::EntityId* globalIds);
192 void writeExodusFile(Iogn::GeneratedMesh *generatedMesh, const std::string &exodusFileName);
193
194 TEST(StkMeshHowTo, UnderstandEdgeAndFaceOrdering)
195 {
196 int oneHex=1;
197 stk::mesh::EntityId globalIdsHex[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
198 Iogn::ExodusData exoData = createExodusData(oneHex, globalIdsHex);
199 Iogn::ExodusMesh* exoMesh = new Iogn::ExodusMesh(exoData);
200 std::string exodusFileName = "oneElem.exo";
201 // No Need To Delete ExoMesh When Using 'writeExodusFile'
202 writeExodusFile(exoMesh, exodusFileName);
203
204 // ============================================================
205 // INITIALIZATION
206 MPI_Comm communicator = MPI_COMM_WORLD;
207 stk::io::StkMeshIoBroker stkIo(communicator);
208 const std::string generatedFileName = exodusFileName;
209 stkIo.add_mesh_database(generatedFileName, stk::io::READ_MESH);
210 stkIo.create_input_mesh();
211 stkIo.populate_bulk_data();
212
213 stk::mesh::BulkData &bulkData = stkIo.bulk_data();
214
215 // BEGIN EXAMPLE 2
216 // ============================================================
217 //+ EXAMPLE
218 //+ Create the faces..
219 stk::mesh::create_faces(bulkData);
220
221 unsigned goldValuesForHexFaceNodesFromStkTopology[6][4] = {
222 {1, 2, 6, 5}, {2, 3, 7, 6}, {3, 4, 8, 7}, {1, 5, 8, 4}, {1, 4, 3, 2}, {5, 6, 7, 8} };
223
224 // Lexicographical smallest permutation per face leads from topology ordering (above) for face to ordering below
225
226 unsigned goldValuesForHexFaceNodesFromCreateFaces[6][4] = {
227 {1, 2, 6, 5}, {2, 3, 7, 6}, {3, 4, 8, 7}, {1, 4, 8, 5}, {1, 2, 3, 4}, {5, 6, 7, 8} };
228
229 //+ Create the edges..
230 stk::mesh::create_edges(bulkData);
231
232 unsigned goldValuesHexEdgeNodesFromStkTopology[12][2] = {
233 {1, 2}, {2, 3}, {3, 4}, {4, 1}, {5, 6}, {6, 7}, {7, 8}, {8, 5}, {1, 5}, {2, 6}, {3, 7}, {4, 8} };
234
235 // Lexicographical smallest permutation per edge leads from topology ordering (above) for edge to ordering below
236
237 unsigned goldValuesHexEdgeNodesFromCreateEdges[12][2] = {
238 {1, 2}, {2, 3}, {3, 4}, {1, 4}, {5, 6}, {6, 7}, {7, 8}, {5, 8}, {1, 5}, {2, 6}, {3, 7}, {4, 8} };
239
240 // END EXAMPLE 2
241
242 // ==================================================
243 // VERIFICATION
244
245 unsigned elementId=1;
246 stk::mesh::Entity element=bulkData.get_entity(stk::topology::ELEMENT_RANK, elementId);
247 const stk::mesh::Entity *edges = bulkData.begin_edges(element);
248 stk::topology hex = stk::topology::HEXAHEDRON_8;
249 unsigned edgeNodeIds[2];
250
251 for (unsigned i=0;i<bulkData.num_edges(element);i++)
252 {
253 const stk::mesh::Entity *edgeNodes = bulkData.begin_nodes(edges[i]);
254 EXPECT_EQ(goldValuesHexEdgeNodesFromCreateEdges[i][0], bulkData.identifier(edgeNodes[0]));
255 EXPECT_EQ(goldValuesHexEdgeNodesFromCreateEdges[i][1], bulkData.identifier(edgeNodes[1]));
256
257 hex.edge_nodes((stk::mesh::EntityId*)globalIdsHex, i, (unsigned*)edgeNodeIds);
258 EXPECT_EQ(goldValuesHexEdgeNodesFromStkTopology[i][0], edgeNodeIds[0]);
259 EXPECT_EQ(goldValuesHexEdgeNodesFromStkTopology[i][1], edgeNodeIds[1]);
260 }
261
262 const stk::mesh::Entity *faces = bulkData.begin_faces(element);
263 unsigned faceNodeIds[4];
264 for (unsigned i=0;i<bulkData.num_faces(element);i++)
265 {
266 const stk::mesh::Entity *faceNodes = bulkData.begin_nodes(faces[i]);
267 EXPECT_EQ(goldValuesForHexFaceNodesFromCreateFaces[i][0], bulkData.identifier(faceNodes[0])) << "failed for face " << i << std::endl;
268 EXPECT_EQ(goldValuesForHexFaceNodesFromCreateFaces[i][1], bulkData.identifier(faceNodes[1])) << "failed for face " << i << std::endl;
269 EXPECT_EQ(goldValuesForHexFaceNodesFromCreateFaces[i][2], bulkData.identifier(faceNodes[2])) << "failed for face " << i << std::endl;
270 EXPECT_EQ(goldValuesForHexFaceNodesFromCreateFaces[i][3], bulkData.identifier(faceNodes[3])) << "failed for face " << i << std::endl;
271
272 hex.face_nodes((stk::mesh::EntityId*)globalIdsHex, i, (unsigned*)faceNodeIds);
273 EXPECT_EQ(goldValuesForHexFaceNodesFromStkTopology[i][0], faceNodeIds[0]);
274 EXPECT_EQ(goldValuesForHexFaceNodesFromStkTopology[i][1], faceNodeIds[1]);
275 EXPECT_EQ(goldValuesForHexFaceNodesFromStkTopology[i][2], faceNodeIds[2]);
276 EXPECT_EQ(goldValuesForHexFaceNodesFromStkTopology[i][3], faceNodeIds[3]);
277 }
278
279 stk::mesh::Selector allEntities = stkIo.meta_data().universal_part();
280 std::vector<size_t> entityCounts;
281 stk::mesh::count_entities(allEntities, stkIo.bulk_data(), entityCounts);
282 EXPECT_EQ(1u, entityCounts[stk::topology::ELEMENT_RANK]);
283 EXPECT_EQ(6u, entityCounts[stk::topology::FACE_RANK]);
284 EXPECT_EQ(12u, entityCounts[stk::topology::EDGE_RANK]);
285
286 unlink(exodusFileName.c_str());
287 }
288
289 Iogn::ExodusData createExodusData(int numberOfHexes, stk::mesh::EntityId* globalIds)
290 {
291 int numberOfElementBlocks = numberOfHexes;
292 int numberOfNodesInEachElementBlock = 8;
293 int globalNumberOfNodes = numberOfElementBlocks * numberOfNodesInEachElementBlock;
294 std::vector<double> coordinates(globalNumberOfNodes*3);
295 std::vector< std::vector<int> > elementBlockConnectivity(numberOfElementBlocks);
296 for(int blockIndex=0; blockIndex < numberOfElementBlocks; blockIndex++)
297 {
298 elementBlockConnectivity[blockIndex].resize(numberOfNodesInEachElementBlock);
299 }
300 int numberOfElementsInEachBlock = 1;
301 std::vector<int> globalNumberOfElementsInBlock(numberOfElementBlocks, numberOfElementsInEachBlock);
302 std::vector<int> localNumberOfElementsInBlock(numberOfElementBlocks, numberOfElementsInEachBlock);
303 std::vector<Iogn::Topology> blockTopologicalData(numberOfElementBlocks, Iogn::Hex8);
304 int numberOfTotalElements = numberOfElementsInEachBlock * numberOfElementBlocks;
305 std::vector<int> globalIdsOfLocalElements(numberOfTotalElements);
306 std::vector<int> globalIdsOfLocalNodes(globalNumberOfNodes);
307 for(int i=0; i < numberOfTotalElements; i++)
308 {
309 globalIdsOfLocalElements[i] = i+1;
310 }
311 for(int i=0; i < globalNumberOfNodes; i++)
312 {
313 // globalIdsOfLocalNodes[i] = i+1;
314 globalIdsOfLocalNodes[i] = globalIds[i];
315 }
316 for(int blockIndex=0; blockIndex < numberOfElementBlocks; blockIndex++)
317 {
318 int elementBlockOffset = numberOfNodesInEachElementBlock * blockIndex;
319 for(int i=0; i < numberOfNodesInEachElementBlock; i++)
320 {
321 elementBlockConnectivity[blockIndex][i] = elementBlockOffset+i+1;
322 }
323 }
324
325 double coords[] = {
326 0, 0, 0,
327 1, 0, 0,
328 1, 1, 0,
329 0, 1, 0,
330 0, 0, 1,
331 1, 0, 1,
332 1, 1, 1,
333 0, 1, 1,
334 };
335
336 coordinates.clear();
337 coordinates.resize(globalNumberOfNodes*3);
338 for (int i=0;i<numberOfElementBlocks;i++)
339 {
340 int offset = 3*numberOfNodesInEachElementBlock*i;
341 for (int j=0;j<numberOfNodesInEachElementBlock;j++)
342 {
343 coordinates[offset+3*j + 0] = coords[3*j+0]+i;
344 coordinates[offset+3*j + 1] = coords[3*j+1];
345 coordinates[offset+3*j + 2] = coords[3*j+2];
346 }
347 }
348
349 return Iogn::ExodusData(coordinates, elementBlockConnectivity, globalNumberOfElementsInBlock, localNumberOfElementsInBlock,
350 blockTopologicalData, globalNumberOfNodes, globalIdsOfLocalElements, globalIdsOfLocalNodes);
351 }
352
353 void writeExodusFile(Iogn::GeneratedMesh *generatedMesh, const std::string &exodusFileName)
354 {
355 Ioss::Init::Initializer io;
356
357 Ioss::DatabaseIO *database = Ioss::IOFactory::create("generated", "2x3x3", Ioss::READ_MODEL, MPI_COMM_WORLD);
358 Iogn::DatabaseIO *iognDatabase = dynamic_cast<Iogn::DatabaseIO *>(database);
359 iognDatabase->setGeneratedMesh(generatedMesh);
360
361 Ioss::Region* io_region = new Ioss::Region(database);
362 stk::io::StkMeshIoBroker meshData;
363 std::shared_ptr<Ioss::Region> junk(io_region, [](auto /*pointerWeWontDelete*/){});
364 meshData.add_mesh_database(junk);
365 meshData.create_input_mesh();
366 meshData.populate_bulk_data();
367 size_t result_file_index = meshData.create_output_mesh(exodusFileName, stk::io::WRITE_RESULTS);
368 meshData.write_output_mesh(result_file_index);
369 delete io_region;
370 }
371
372 }
373