Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/useNodesetDbVarForNodalField.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_IOFactory.h" // for IOFactory
46 #include "Ioss_NodeSet.h" // for NodeSet
47 #include "Ioss_Region.h" // for NodeSetContainer, Region
48 #include "Ioss_Utils.h" // for Utils
49 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc
50 #include "stk_mesh/base/Entity.hpp" // for Entity
51 #include "stk_mesh/base/FieldBase.hpp" // for field_data
52 #include "stk_mesh/base/FieldBLAS.hpp"
53 #include "stk_mesh/base/Part.hpp" // for Part
54 #include "stk_mesh/base/Selector.hpp" // for Selector
55 #include "stk_mesh/base/Types.hpp" // for PartVector
56 #include "stk_topology/topology.hpp" // for topology, etc
57 #include "stk_util/parallel/Parallel.hpp"
58 namespace Ioss { class DatabaseIO; }
59
60 namespace {
61
62 TEST(StkMeshIoBrokerHowTo, useNodesetDbVarForNodalFields)
63 {
64 std::string resultsFilename = "nodeset_fields.results";
65 std::string dbFieldName = "temp";
66 std::string appFieldName = "temperature";
67
68 MPI_Comm communicator = MPI_COMM_WORLD;
69 int numProcs = stk::parallel_machine_size(communicator);
70 if (numProcs != 1) { return; }
71
72 size_t num_elems_per_edge = 9;
73 {
74 //-BEGIN
75 // ============================================================
76 // INITIALIZATION
77 std::string s_elems_per_edge = std::to_string(num_elems_per_edge);
78
79 //+ Create a generated mesh containg hexes and shells.
80 std::string input_filename = s_elems_per_edge + "x" +
81 s_elems_per_edge + "x" +
82 s_elems_per_edge + "|shell:xyzXYZ";
83
84 stk::io::StkMeshIoBroker stkIo(communicator);
85 stkIo.add_mesh_database(input_filename, "generated",
86 stk::io::READ_MESH);
87 stkIo.create_input_mesh();
88
89 stk::mesh::MetaData &meta_data = stkIo.meta_data();
90 stk::mesh::Field<double> &temperature =
91 meta_data.declare_field<double>(stk::topology::NODE_RANK, appFieldName, 1);
92
93 // ============================================================
94 //+ Put the temperature field on the nodes of the shell parts.
95 const stk::mesh::PartVector &all_parts = meta_data.get_mesh_parts();
96 stk::mesh::Selector shell_subset;
97 for (size_t i=0; i < all_parts.size(); i++) {
98 const stk::mesh::Part *part = all_parts[i];
99 stk::topology topo = part->topology();
100 if (topo == stk::topology::SHELL_QUAD_4) {
101 stk::mesh::put_field_on_mesh(temperature, *part, nullptr);
102 }
103 }
104
105 stkIo.populate_bulk_data();
106
107 // Create the output...
108 size_t fh = stkIo.create_output_mesh(resultsFilename, stk::io::WRITE_RESULTS);
109
110 //+ The "temperature" field will be output on nodesets consisting
111 //+ of the nodes of each part the field is defined on.
112 stkIo.use_nodeset_for_sideset_nodes_fields(fh, true);
113 stkIo.use_nodeset_for_block_nodes_fields(fh, true);
114 stkIo.add_field(fh, temperature, dbFieldName);
115
116 // Add three steps to the database
117 // For each step, the value of the field is the value 'time'
118 for (size_t i=0; i < 3; i++) {
119 double time = i;
120
121 stk::mesh::field_fill(time, temperature);
122
123 stkIo.begin_output_step(fh, time);
124 stkIo.write_defined_output_fields(fh);
125 stkIo.end_output_step(fh);
126 }
127 // Verification omitted...
128 //-END
129 }
130
131 {
132 // ==================================================
133 // VERIFICATION
134 // Verify that the output mesh has 6 nodesets (input has 0)
135 // corresponding to the nodes of the 6 shell blocks.
136 // Each nodeset should have a single variable named dbFieldName
137
138 Ioss::DatabaseIO *iossDb = Ioss::IOFactory::create("exodus", resultsFilename,
139 Ioss::READ_MODEL, communicator);
140 Ioss::Region ioRegion(iossDb);
141
142 // Six nodesets should have been created -- 1 for each shell block in the mesh.
143 const Ioss::NodeSetContainer &nsets = ioRegion.get_nodesets();
144 size_t nset_count = 6;
145 ASSERT_EQ(nset_count, nsets.size());
146
147 for (size_t i=0; i < nsets.size(); i++) {
148 const Ioss::NodeSet *nset = nsets[i];
149 // Each nodeset should have a field named 'temp'
150 ASSERT_TRUE(nset->field_exists(dbFieldName));
151 }
152 }
153 unlink(resultsFilename.c_str());
154 }
155
156 }