Debian Codegraph @

packages/stk/stk_doc_tests/stk_mesh/howToUseSelectors.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 <stddef.h> // for size_t
37 #include <sstream> // for ostringstream, etc
38 #include <stk_io/FillMesh.hpp>
39 #include <stk_mesh/base/MeshBuilder.hpp>
40 #include <stk_mesh/base/BulkData.hpp> // for BulkData
41 #include <stk_mesh/base/MetaData.hpp> // for MetaData
42 #include <stk_mesh/base/Selector.hpp> // for Selector, operator<<, etc
43 #include <stk_mesh/base/GetEntities.hpp>
44 #include <stk_topology/topology.hpp> // for topology, etc
45 #include <stk_mesh/base/Types.hpp> // for BucketVector, PartVector
46 #include <stk_unit_test_utils/TextMesh.hpp>
47 #include <string> // for string
48 namespace stk { namespace mesh { class Part; } }
49
50 namespace
51 {
52 //-BEGIN
53 TEST(StkMeshHowTo, basicSelectorUsage)
54 {
55 MPI_Comm communicator = MPI_COMM_WORLD;
56 if (stk::parallel_machine_size(communicator) != 1) { GTEST_SKIP(); }
57 std::unique_ptr<stk::mesh::BulkData> bulkPtr = stk::mesh::MeshBuilder(communicator)
58 .set_spatial_dimension(3).create();
59 stk::mesh::MetaData& meta = bulkPtr->mesh_meta_data();
60
61 //create a simple shell-quad-4 mesh:
62 // 6
63 // 3*----*----*9
64 // | E2 | E4 |
65 // | | |
66 // 2*---5*----*8
67 // | E1 | E3 |
68 // | | |
69 // 1*----*----*7
70 // 4
71 //
72
73 std::string meshDesc = "0,1,SHELL_QUAD_4, 1,4,2,5, block_1\n"
74 "0,2,SHELL_QUAD_4, 2,5,6,3, block_2\n"
75 "0,3,SHELL_QUAD_4, 4,7,8,5, block_3\n"
76 "0,4,SHELL_QUAD_4, 5,8,9,6, block_4\n";
77 stk::unit_test_util::setup_text_mesh(*bulkPtr, meshDesc);
78
79 stk::mesh::Part& block_1 = *meta.get_part("block_1");
80 stk::mesh::Part& block_2 = *meta.get_part("block_2");
81 stk::mesh::Part& block_3 = *meta.get_part("block_3");
82 stk::mesh::Part& block_4 = *meta.get_part("block_4");
83 stk::mesh::PartVector allBlocks = {&block_1, &block_2, &block_3, &block_4};
84
85 stk::mesh::Selector allNodes = stk::mesh::selectUnion(allBlocks);
86 stk::mesh::Selector onlyCenterNode = stk::mesh::selectIntersection(allBlocks);
87 stk::mesh::Selector nodes456 = (block_1 | block_2) & (block_3 | block_4);
88 stk::mesh::Selector nodes123 = (block_1 | block_2) - nodes456;
89
90 EXPECT_EQ(9u, stk::mesh::count_entities(*bulkPtr, stk::topology::NODE_RANK, allNodes));
91 EXPECT_EQ(1u, stk::mesh::count_entities(*bulkPtr, stk::topology::NODE_RANK, onlyCenterNode));
92 EXPECT_EQ(3u, stk::mesh::count_entities(*bulkPtr, stk::topology::NODE_RANK, nodes456));
93 EXPECT_EQ(3u, stk::mesh::count_entities(*bulkPtr, stk::topology::NODE_RANK, nodes123));
94 }
95
96 TEST(StkMeshHowTo, betterUnderstandSelectorConstruction)
97 {
98 MPI_Comm communicator = MPI_COMM_WORLD;
99 if (stk::parallel_machine_size(communicator) != 1) { GTEST_SKIP(); }
100 std::unique_ptr<stk::mesh::BulkData> bulkPtr = stk::mesh::MeshBuilder(communicator).create();
101 const std::string generatedCubeMeshSpecification = "generated:1x1x1";
102 stk::io::fill_mesh(generatedCubeMeshSpecification, *bulkPtr);
103
104 stk::mesh::Selector nothingSelector_byDefaultConstruction;
105 size_t expectingZeroBuckets = 0;
106 EXPECT_EQ(expectingZeroBuckets, bulkPtr->get_buckets(stk::topology::NODE_RANK, nothingSelector_byDefaultConstruction).size());
107
108 std::ostringstream readableSelectorDescription;
109 readableSelectorDescription << nothingSelector_byDefaultConstruction;
110 EXPECT_STREQ("NOTHING", readableSelectorDescription.str().c_str());
111
112 stk::mesh::Selector allSelector(!nothingSelector_byDefaultConstruction);
113 size_t numberOfAllNodeBuckets = bulkPtr->buckets(stk::topology::NODE_RANK).size();
114 EXPECT_EQ(numberOfAllNodeBuckets, bulkPtr->get_buckets(stk::topology::NODE_RANK, allSelector).size());
115 }
116
117 TEST(StkMeshHowTo, makeSureYouAreNotIntersectingNothingSelector)
118 {
119 MPI_Comm communicator = MPI_COMM_WORLD;
120 if (stk::parallel_machine_size(communicator) != 1) { return; }
121 std::unique_ptr<stk::mesh::BulkData> bulkPtr = stk::mesh::MeshBuilder(communicator).create();
122 // syntax creates faces for surface on the positive: 'x-side', 'y-side', and 'z-side'
123 // of a 1x1x1 cube, these parts are given the names: 'surface_1', 'surface_2', and 'surface_3'
124 const std::string generatedCubeMeshSpecification = "generated:1x1x1|sideset:XYZ";
125 stk::io::fill_mesh(generatedCubeMeshSpecification, *bulkPtr);
126
127 stk::mesh::MetaData &stkMeshMetaData = bulkPtr->mesh_meta_data();
128 stk::mesh::Part *surface1Part = stkMeshMetaData.get_part("surface_1");
129 stk::mesh::Part *surface2Part = stkMeshMetaData.get_part("surface_2");
130 stk::mesh::Part *surface3Part = stkMeshMetaData.get_part("surface_3");
131 stk::mesh::PartVector allSurfaces;
132 allSurfaces.push_back(surface1Part);
133 allSurfaces.push_back(surface2Part);
134 allSurfaces.push_back(surface3Part);
135
136 stk::mesh::Selector selectorIntersectingNothing;
137 for (size_t surfaceIndex = 0; surfaceIndex < allSurfaces.size(); ++surfaceIndex) {
138 stk::mesh::Part &surfacePart = *(allSurfaces[surfaceIndex]);
139 stk::mesh::Selector surfaceSelector(surfacePart);
140 selectorIntersectingNothing &= surfacePart;
141 }
142
143 size_t expectedNumberOfBucketsWhenIntersectingNothing = 0;
144 stk::mesh::BucketVector selectedBuckets = bulkPtr->get_buckets(stk::topology::NODE_RANK, selectorIntersectingNothing);
145 EXPECT_EQ(expectedNumberOfBucketsWhenIntersectingNothing, selectedBuckets.size());
146
147 stk::mesh::Selector preferredBoundaryNodesSelector = stk::mesh::selectIntersection(allSurfaces);
148 size_t expectedNumberOfNodeBucketsWhenIntersectingAllSurfaces = 1;
149 selectedBuckets = bulkPtr->get_buckets(stk::topology::NODE_RANK, preferredBoundaryNodesSelector);
150 EXPECT_EQ(expectedNumberOfNodeBucketsWhenIntersectingAllSurfaces, selectedBuckets.size());
151 }
152 //-END
153 }