Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/readInitialConditionNodalSubset.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, ASSERT_EQ, 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/Field.hpp> // for Field
40 #include <stk_mesh/base/GetEntities.hpp> // for get_entities
41 #include <stk_mesh/base/MetaData.hpp> // for MetaData, put_field
42 #include <string> // for allocator, operator+, etc
43 #include <vector> // for vector
44 #include "Ioss_DBUsage.h" // for DatabaseUsage::READ_MODEL
45 #include "Ioss_NodeSet.h" // for NodeSet
46 #include "Ioss_Region.h" // for NodeSetContainer, Region
47 #include "Ioss_Utils.h" // for Utils
48 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc
49 #include "stk_mesh/base/Entity.hpp" // for Entity
50 #include "stk_mesh/base/FieldBase.hpp" // for field_data
51 #include "stk_mesh/base/Part.hpp" // for Part
52 #include "stk_mesh/base/Selector.hpp" // for Selector
53 #include "stk_mesh/base/Types.hpp" // for PartVector
54 #include "stk_topology/topology.hpp" // for topology, etc
55 #include "stk_util/parallel/Parallel.hpp"
56 namespace Ioss { class DatabaseIO; }
57
58 namespace {
59
60 TEST(StkMeshIoBrokerHowTo, readInitialConditionNodalSubset)
61 {
62 //-BEGIN
63 std::string dbFieldNameShell = "NodeBlock_1";
64 std::string appFieldName = "temperature";
65 size_t num_elems_per_edge = 9;
66
67 MPI_Comm communicator = MPI_COMM_WORLD;
68 int numProcs = stk::parallel_machine_size(communicator);
69 if (numProcs != 1) {
70 return;
71 }
72
73 {
74 // ============================================================
75 // INITIALIZATION
76 //+ Create a generated mesh containg hexes and shells with a
77 //+ single nodal variable -- NodeBlock_1
78 std::string s_elems_per_edge = std::to_string(num_elems_per_edge);
79
80 //+ Create a generated mesh containg hexes and shells.
81 std::string input_filename = s_elems_per_edge + "x" +
82 s_elems_per_edge + "x" +
83 s_elems_per_edge;
84 input_filename += "|shell:xyzXYZ|variables:nodal,1|times:1";
85
86 stk::io::StkMeshIoBroker stkIo(communicator);
87 stkIo.add_mesh_database(input_filename, "generated", stk::io::READ_MESH);
88 stkIo.create_input_mesh();
89
90 stk::mesh::MetaData &meta_data = stkIo.meta_data();
91
92 // Declare the nodal "temperature" field. Exists on all nodes.
93 constexpr int numStates = 1;
94 stk::mesh::Field<double> &temperature =
95 stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, appFieldName,numStates);
96
97 // "NodeBlock_1" is the name of the node field on the input mesh.
98 stk::io::MeshField mf(temperature, dbFieldNameShell);
99 double time = stkIo.get_input_ioss_region()->get_state_time(1);
100 mf.set_read_time(time);
101
102 const stk::mesh::PartVector &all_parts = meta_data.get_mesh_parts();
103 for (size_t i=0; i < all_parts.size(); i++) {
104 const stk::mesh::Part *part = all_parts[i];
105
106 //+ It exists on all nodes in the mesh...
107 stk::mesh::put_field_on_mesh(temperature, *part, nullptr);
108
109 stk::topology topo = part->topology();
110 if (topo == stk::topology::SHELL_QUAD_4) {
111 //+ Temperature field exists on all nodes in the mesh,
112 //+ but only initialize it on the shell nodes.
113 mf.add_subset(*part);
114 }
115 }
116
117 stkIo.populate_bulk_data();
118
119
120 //+ Populate the fields with data from the input mesh.
121 stkIo.read_input_field(mf);
122
123 //-END
124 // ============================================================
125 //+ VERIFICATION
126 //+ The value of the field on the first 729 elements should be 0.0;
127 //+ The value of the field on the remaining elements should be sqrt(i+1)
128 size_t num_nodes_per_edge = num_elems_per_edge+1;
129 std::vector<stk::mesh::Entity> nodes;
130 stk::mesh::get_entities(stkIo.bulk_data(), stk::topology::NODE_RANK,
131 nodes);
132 size_t all_nodes = num_nodes_per_edge * num_nodes_per_edge * num_nodes_per_edge;
133 EXPECT_EQ(all_nodes, nodes.size());
134
135 // Create a selector for the nodes that are attached to the shells.
136 stk::mesh::Selector shell_subset;
137 for (size_t i=0; i < all_parts.size(); i++) {
138 const stk::mesh::Part *part = all_parts[i];
139 stk::topology topo = part->topology();
140 if (topo == stk::topology::SHELL_QUAD_4) {
141 shell_subset |= *part;
142 }
143 }
144
145 //+ Get all nodes attached and not attached to shells.
146 std::vector<stk::mesh::Entity> shell_nodes;
147 std::vector<stk::mesh::Entity> other_nodes;
148 stk::mesh::get_entities(stkIo.bulk_data(), stk::topology::NODE_RANK, shell_subset, shell_nodes);
149 stk::mesh::get_entities(stkIo.bulk_data(), stk::topology::NODE_RANK, !shell_subset, other_nodes);
150
151 // Calculate number of nodes on surface of the mesh...
152 size_t expected_nodes = 6 * num_nodes_per_edge*num_nodes_per_edge;
153 expected_nodes -= 12 * num_nodes_per_edge; // Nodes on each edge were double-counted in previous calculation.
154 expected_nodes += 8; // Nodes on each corner were removed in previous calculation; add them back.
155 EXPECT_EQ(expected_nodes, shell_nodes.size());
156 EXPECT_EQ(all_nodes-expected_nodes, other_nodes.size());
157
158 for(size_t i=0; i<other_nodes.size(); i++) {
159 double *fieldDataForNode = stk::mesh::field_data(temperature, other_nodes[i]);
160 EXPECT_DOUBLE_EQ(0.0, *fieldDataForNode);
161 }
162
163 for(size_t i=0; i<shell_nodes.size(); i++) {
164 double *fieldDataForNode = stk::mesh::field_data(temperature, shell_nodes[i]);
165 size_t id = stkIo.bulk_data().identifier(shell_nodes[i]);
166 EXPECT_DOUBLE_EQ(sqrt(id), *fieldDataForNode);
167 }
168 }
169 }
170 }