Debian Codegraph @

packages/stk/stk_doc_tests/stk_mesh/howToIterateConnectivity.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_NE, etc
36 #include <stddef.h> // for size_t
37 #include <stk_io/FillMesh.hpp>
38 #include <stk_mesh/base/MetaData.hpp> // for MetaData
39 #include <stk_mesh/base/BulkData.hpp> // for BulkData
40 #include <stk_mesh/base/MeshBuilder.hpp>
41 #include <stk_mesh/base/ForEachEntity.hpp>
42 #include <stk_topology/topology.hpp> // for topology, etc
43 #include "stk_mesh/base/Bucket.hpp" // for Bucket
44 #include "stk_mesh/base/Entity.hpp" // for Entity
45 #include "stk_mesh/base/Field.hpp" // for Field
46 #include "stk_mesh/base/FieldBase.hpp" // for field_data, FieldBase
47 #include "stk_mesh/base/Types.hpp" // for BucketVector
48 #include <cmath>
49 #include <string>
50 namespace stk { namespace mesh { class Part; } }
51
52 namespace
53 {
54 //-BEGIN
55 TEST(StkMeshHowTo, iterateElemNodeConnectivity_ForEachEntityWithNodes)
56 {
57 MPI_Comm comm = MPI_COMM_WORLD;
58 if (stk::parallel_machine_size(comm) != 1) { GTEST_SKIP(); }
59 std::unique_ptr<stk::mesh::BulkData> stkMesh = stk::mesh::MeshBuilder(comm).create();
60 // Generate a mesh of unit-cube hexes with a sideset
61 const std::string generatedMeshSpecification = "generated:2x2x2|sideset:X";
62 stk::io::fill_mesh(generatedMeshSpecification, *stkMesh);
63
64 typedef stk::mesh::Field<double> CoordinatesField_t;
65 CoordinatesField_t const & coord_field =
66 *dynamic_cast<CoordinatesField_t const *>(stkMesh->mesh_meta_data().coordinate_field());
67
68 constexpr unsigned nodesPerHex = 8;
69 constexpr unsigned spatialDim = 3;
70 unsigned count = 0;
71 double elementNodeCoords[nodesPerHex][spatialDim] = {
72 {NAN,NAN,NAN}, {NAN,NAN,NAN}, {NAN,NAN,NAN}, {NAN,NAN,NAN},
73 {NAN,NAN,NAN}, {NAN,NAN,NAN}, {NAN,NAN,NAN}, {NAN,NAN,NAN} };
74
75 stk::mesh::Selector all = stkMesh->mesh_meta_data().universal_part();
76
77 stk::mesh::for_each_entity_run_with_nodes(*stkMesh, stk::topology::ELEM_RANK, all,
78 [&](stk::mesh::Entity /*elem*/, const stk::mesh::Entity* nodes, size_t numNodesPerEntity) {
79 EXPECT_EQ(numNodesPerEntity, nodesPerHex);
80 for (unsigned inode = 0; inode < numNodesPerEntity; ++inode) {
81 const double *coords = stk::mesh::field_data(coord_field, nodes[inode]);
82 elementNodeCoords[inode][0] = coords[0];
83 elementNodeCoords[inode][1] = coords[1];
84 elementNodeCoords[inode][2] = coords[2];
85 ++count;
86 }
87 });
88
89 const unsigned numElems = 2*2*2;
90 const unsigned totalNodesVisited = numElems * nodesPerHex;
91 EXPECT_EQ(count, totalNodesVisited);
92 EXPECT_FALSE(std::isnan(elementNodeCoords[0][0]));
93 }
94
95 TEST(StkMeshHowTo, iterateConnectivity_General_BulkData)
96 {
97 MPI_Comm comm = MPI_COMM_WORLD;
98 if (stk::parallel_machine_size(comm) != 1) { GTEST_SKIP(); }
99 std::unique_ptr<stk::mesh::BulkData> stkMesh = stk::mesh::MeshBuilder(comm).create();
100 // Generate a mesh of unit-cube hexes with a sideset
101 const std::string generatedMeshSpecification = "generated:2x2x2|sideset:X";
102 stk::io::fill_mesh(generatedMeshSpecification, *stkMesh);
103
104 typedef stk::mesh::Field<double> CoordinatesField_t;
105 CoordinatesField_t const & coord_field =
106 *dynamic_cast<CoordinatesField_t const *>(stkMesh->mesh_meta_data().coordinate_field());
107
108 constexpr unsigned nodesPerHex = 8;
109 constexpr unsigned spatialDim = 3;
110 unsigned count = 0;
111 double elementNodeCoords[nodesPerHex][spatialDim] = {
112 {NAN,NAN,NAN}, {NAN,NAN,NAN}, {NAN,NAN,NAN}, {NAN,NAN,NAN},
113 {NAN,NAN,NAN}, {NAN,NAN,NAN}, {NAN,NAN,NAN}, {NAN,NAN,NAN} };
114
115 stk::mesh::for_each_entity_run(*stkMesh, stk::topology::ELEM_RANK,
116 [&](const stk::mesh::BulkData& /*bulk*/, stk::mesh::Entity elem) {
117 const stk::mesh::ConnectedEntities nodes = stkMesh->get_connected_entities(elem, stk::topology::NODE_RANK);
118 EXPECT_EQ(nodes.size(), nodesPerHex);
119
120 for (unsigned inode = 0; inode < nodes.size(); ++inode) {
121 const double *coords = stk::mesh::field_data(coord_field, nodes[inode]);
122 elementNodeCoords[inode][0] = coords[0];
123 elementNodeCoords[inode][1] = coords[1];
124 elementNodeCoords[inode][2] = coords[2];
125 ++count;
126 }
127 });
128
129 const unsigned numElems = 2*2*2;
130 const unsigned totalNodesVisited = numElems * nodesPerHex;
131 EXPECT_EQ(count, totalNodesVisited);
132 EXPECT_FALSE(std::isnan(elementNodeCoords[0][0]));
133 }
134
135 TEST(StkMeshHowTo, iterateConnectivity_Buckets)
136 {
137 MPI_Comm comm = MPI_COMM_WORLD;
138 if (stk::parallel_machine_size(comm) != 1) { return; }
139 std::unique_ptr<stk::mesh::BulkData> stkMesh = stk::mesh::MeshBuilder(comm).create();
140 // Generate a mesh of unit-cube hexes with a sideset
141 const std::string generatedMeshSpecification = "generated:2x2x2|sideset:X";
142 stk::io::fill_mesh(generatedMeshSpecification, *stkMesh);
143
144 typedef stk::mesh::Field<double> CoordinatesField_t;
145 CoordinatesField_t const & coord_field =
146 *dynamic_cast<CoordinatesField_t const *>(stkMesh->mesh_meta_data().coordinate_field());
147
148 const stk::mesh::BucketVector &elementBuckets = stkMesh->buckets(stk::topology::ELEMENT_RANK);
149
150 constexpr unsigned nodesPerHex = 8;
151 constexpr unsigned spatialDim = 3;
152 unsigned count = 0;
153 double elementNodeCoords[nodesPerHex][spatialDim] = {
154 {NAN,NAN,NAN}, {NAN,NAN,NAN}, {NAN,NAN,NAN}, {NAN,NAN,NAN},
155 {NAN,NAN,NAN}, {NAN,NAN,NAN}, {NAN,NAN,NAN}, {NAN,NAN,NAN} };
156
157 for (size_t bucketIndex = 0; bucketIndex < elementBuckets.size(); ++bucketIndex) {
158 const stk::mesh::Bucket &elemBucket = *elementBuckets[bucketIndex];
159
160 for (size_t elemIndex = 0; elemIndex < elemBucket.size(); ++elemIndex) {
161 unsigned numNodes = elemBucket.num_nodes(elemIndex);
162 EXPECT_EQ(numNodes, nodesPerHex);
163 const stk::mesh::Entity* nodes = elemBucket.begin_nodes(elemIndex);
164
165 for (unsigned inode = 0; inode < numNodes; ++inode) {
166 const double *coords = stk::mesh::field_data(coord_field, nodes[inode]);
167 elementNodeCoords[inode][0] = coords[0];
168 elementNodeCoords[inode][1] = coords[1];
169 elementNodeCoords[inode][2] = coords[2];
170 ++count;
171 }
172 }
173 }
174 const unsigned numElems = 2*2*2;
175 const unsigned totalNodesVisited = numElems * nodesPerHex;
176 EXPECT_EQ(count, totalNodesVisited);
177 EXPECT_FALSE(std::isnan(elementNodeCoords[0][0]));
178 }
179 //-END
180 }