Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/replaceBulkData.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 <unistd.h> // for unlink
38 #include <ostream> // for basic_ostream::operator<<
39 #include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker
40 #include <stk_mesh/base/Field.hpp> // for Field
41 #include <stk_mesh/base/GetEntities.hpp> // for get_entities
42 #include <stk_mesh/base/MetaData.hpp> // for MetaData, put_field
43 #include <string> // for string
44 #include <vector> // for vector
45 #include "Ioss_DBUsage.h" // for DatabaseUsage::READ_MODEL
46 #include "Ioss_Field.h" // for Field, etc
47 #include "Ioss_IOFactory.h" // for IOFactory
48 #include "Ioss_NodeBlock.h" // for NodeBlock
49 #include "Ioss_Property.h" // for Property
50 #include "Ioss_Region.h" // for Region, NodeBlockContainer
51 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc
52 #include "stk_mesh/base/Entity.hpp" // for Entity
53 #include "stk_mesh/base/FieldBase.hpp" // for field_data
54 #include "stk_topology/topology.hpp" // for topology, etc
55 #include "stk_mesh/base/MeshBuilder.hpp"
56
57 namespace Ioss { class DatabaseIO; }
58
59 namespace {
60
61 TEST(StkMeshIoBrokerHowTo, replaceBulkData)
62 {
63 std::string mesh_name = "input_mesh_example.e";
64 std::string results_name = "output.results";
65 MPI_Comm communicator = MPI_COMM_WORLD;
66
67 {
68 // ============================================================
69 //+ INITIALIZATION:
70 //+ Create a basic mesh with a hex block, 3 shell blocks, 3 nodesets, and 3 sidesets.
71 stk::io::StkMeshIoBroker stkIo(communicator);
72
73 const std::string generatedFileName = "generated:8x8x8|shell:xyz|nodeset:xyz|sideset:XYZ";
74 size_t index = stkIo.add_mesh_database(generatedFileName, stk::io::READ_MESH);
75 stkIo.set_active_mesh(index);
76 stkIo.create_input_mesh();
77 stkIo.populate_bulk_data();
78
79 size_t fh = stkIo.create_output_mesh(mesh_name, stk::io::WRITE_RESULTS);
80 stkIo.write_output_mesh(fh);
81 }
82
83 {
84 //-BEGIN
85 // ============================================================
86 //+ EXAMPLE:
87 //+ Read mesh data from the specified file.
88 stk::io::StkMeshIoBroker stkIo(communicator);
89 stkIo.add_mesh_database(mesh_name, stk::io::READ_MESH);
90
91 //+ Creates meta data; creates parts
92 stkIo.create_input_mesh();
93
94 //+ Declare a field
95 //+ NOTE: Fields must be declared before "populate_bulk_data()" is called
96 //+ since it commits the meta data.
97 const std::string fieldName = "disp";
98 stk::mesh::Field<double> &field = stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, fieldName, 1);
99 stk::mesh::put_field_on_mesh(field, stkIo.meta_data().universal_part(), nullptr);
100
101 //+ commit the meta data and create the bulk data.
102 //+ populate the bulk data with data from the mesh file.
103 stkIo.populate_bulk_data();
104
105 // ============================================================
106 //+ Create a new bulk_data from the existing meta_data...
107 //+ and replace the current bulk data with the new bulk data.
108 //+ This is not a common operation and should be used only if you
109 //+ know what you are doing.
110 //+
111 //+ Note that since we are not adding nodes and elements to the new
112 //+ bulk data, there will be no nodes or elements on the output
113 //+ mesh. It should have the same meta_data (element blocks, nodesets, ...)
114 //+
115 //+ A real use of this would require the client to populate the
116 //+ bulk data with mesh entities.
117 stk::ParallelMachine parallel = stkIo.bulk_data().parallel();
118
119 stk::mesh::MetaData &md = stkIo.meta_data();
120 md.set_mesh_bulk_data(nullptr);
121
122 auto bd = stk::mesh::MeshBuilder(parallel).create(std::shared_ptr<stk::mesh::MetaData>(&md, [](auto /*pointerWeWontDelete*/){}));
123 stkIo.replace_bulk_data(*bd);
124
125 // ============================================================
126 //+ Create results file. By default, all parts created from the input
127 //+ mesh will be written to the results output file.
128 size_t fh = stkIo.create_output_mesh(results_name, stk::io::WRITE_RESULTS);
129
130 //+ The field will be output to the results file with the default field name.
131 stkIo.add_field(fh, field); /*@\label{io:results:add_field}*/
132
133 std::vector<stk::mesh::Entity> nodes;
134 stk::mesh::get_entities(stkIo.bulk_data(), stk::topology::NODE_RANK, nodes);
135
136 // Iterate the application's execute loop five times and output
137 // field data each iteration.
138 for (int step=0; step < 5; step++) {
139 double time = step;
140
141 //+ Output the field data calculated by the application.
142 stkIo.begin_output_step(fh, time);
143 stkIo.write_defined_output_fields(fh);
144 stkIo.end_output_step(fh);
145 }
146 //-END
147 }
148 // ============================================================
149 //+ VERIFICATION
150 {
151 Ioss::DatabaseIO *resultsDb = Ioss::IOFactory::create("exodus", results_name,
152 Ioss::READ_MODEL, communicator);
153 Ioss::Region results(resultsDb);
154 // Should be 5 steps on database...
155 EXPECT_EQ(results.get_property("state_count").get_int(), 5);
156 // Should be 1 nodal field on database named "disp";
157 Ioss::NodeBlock *nb = results.get_node_blocks()[0];
158 EXPECT_EQ(1u, nb->field_count(Ioss::Field::TRANSIENT));
159 EXPECT_TRUE(nb->field_exists("disp"));
160 // Empty element blocks are not written, so even though
161 // input mesh had 4 blocks, the output mesh will have 0.
162 // Allowing the output of empty element blocks causes more
163 // problems than it solves
164 EXPECT_EQ(0u, results.get_element_blocks().size());
165 EXPECT_EQ(3u, results.get_nodesets().size());
166 }
167
168 // ============================================================
169 // Cleanup
170 unlink(mesh_name.c_str());
171 unlink(results_name.c_str());
172
173 }
174 }