Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/subsettingOutputDB.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 ASSERT_EQ, AssertHelper, etc
36 #include <stddef.h> // for size_t
37 #include <unistd.h> // for unlink
38 #include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker
39 #include <stk_mesh/base/MetaData.hpp> // for MetaData
40 #include <string> // for allocator, operator+, etc
41 #include "Ioss_DBUsage.h" // for DatabaseUsage::READ_MODEL
42 #include "Ioss_IOFactory.h" // for IOFactory
43 #include "Ioss_Property.h" // for Property
44 #include "Ioss_Region.h" // for Region
45 #include "Ioss_Utils.h" // for Utils
46 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc
47 #include "stk_mesh/base/Part.hpp" // for Part
48 #include "stk_mesh/base/Selector.hpp" // for Selector
49 #include "stk_mesh/base/Types.hpp" // for PartVector
50 #include "stk_topology/topology.hpp" // for topology, etc
51 #include "stk_util/parallel/Parallel.hpp"
52 namespace Ioss { class DatabaseIO; }
53
54 namespace {
55
56 TEST(StkMeshIoBrokerHowTo, subsetOutputDatabase)
57 {
58 std::string resultsFilename = "subsetted.results";
59 MPI_Comm communicator = MPI_COMM_WORLD;
60 int numProcs = stk::parallel_machine_size(communicator);
61 if (numProcs != 1) { return; }
62
63 size_t num_elems_per_edge = 9;
64 {
65 //-BEGIN
66 // ============================================================
67 // INITIALIZATION
68 std::string s_elems_per_edge = std::to_string(num_elems_per_edge);
69
70 //+ Create a generated mesh containg hexes and shells.
71 std::string input_filename = s_elems_per_edge + "x" +
72 s_elems_per_edge + "x" +
73 s_elems_per_edge + "|shell:xyzXYZ";
74
75 stk::io::StkMeshIoBroker stkIo(communicator);
76 size_t index = stkIo.add_mesh_database(input_filename, "generated", stk::io::READ_MESH);
77 stkIo.set_active_mesh(index);
78 stkIo.create_input_mesh();
79 stkIo.populate_bulk_data();
80
81 stk::mesh::MetaData &meta_data = stkIo.meta_data();
82 const stk::mesh::PartVector &all_parts = meta_data.get_mesh_parts();
83
84 // ============================================================
85 //+ EXAMPLE
86 //+ Create a selector containing just the shell parts.
87 stk::mesh::Selector shell_subset;
88 for (size_t i=0; i < all_parts.size(); i++) {
89 const stk::mesh::Part *part = all_parts[i];
90 stk::topology topo = part->topology();
91 if (topo == stk::topology::SHELL_QUAD_4) {
92 shell_subset |= *part;
93 }
94 }
95
96 // Create the output...
97 size_t fh = stkIo.create_output_mesh(resultsFilename,
98 stk::io::WRITE_RESULTS);
99
100 //+ Specify that only the subset of parts selected by the
101 //+ "shell_subset" selector will be on the output database.
102 stkIo.set_subset_selector(fh, shell_subset);
103 stkIo.write_output_mesh(fh);
104 // Verification omitted...
105 //-END
106 }
107
108 {
109 // ==================================================
110 // VERIFICATION
111 // Verify output mesh has correct number of nodes and elements.
112 // Note that the output mesh will contain all element blocks;
113 // however, the non-shell element block will have zero elements.
114 // This is due to the subset_selector subsetting the entities and
115 // not the parts...
116 Ioss::DatabaseIO *iossDb = Ioss::IOFactory::create("exodus", resultsFilename,
117 Ioss::READ_MODEL, communicator);
118 Ioss::Region ioRegion(iossDb);
119
120
121 // The output model should consist of the elements and nodes in the 6 shell blocks.
122 size_t num_elements = ioRegion.get_property("element_count").get_int();
123 size_t num_nodes = ioRegion.get_property("node_count").get_int();
124
125 // Calculate the expected number of nodes and elements.
126 size_t expected_elements = 6 * num_elems_per_edge * num_elems_per_edge ;
127
128 size_t num_nodes_per_edge = num_elems_per_edge+1;
129 size_t expected_nodes = 6 * num_nodes_per_edge*num_nodes_per_edge;
130 expected_nodes -= 12 * num_nodes_per_edge; // Nodes on each edge were double-counted in previous calculation.
131 expected_nodes += 8; // Nodes on each corner were removed in previous calculation; add them back.
132
133 ASSERT_EQ(num_elements, expected_elements);
134 ASSERT_EQ(num_nodes, expected_nodes);
135 }
136 unlink(resultsFilename.c_str());
137 }
138
139 }