Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/handleMissingFieldOnRead.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>
36 #include <stddef.h> // for size_t
37 #include <unistd.h> // for unlink
38 #include <iomanip> // for operator<<
39 #include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker
40 #include <stk_mesh/base/Field.hpp> // for Field
41 #include <stk_mesh/base/FieldBLAS.hpp>
42 #include <stk_mesh/base/ForEachEntity.hpp>
43 #include <stk_mesh/base/MetaData.hpp> // for MetaData, put_field
44 #include <string> // for string
45 #include <vector> // for vector
46 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc
47 #include "stk_io/MeshField.hpp" // for MeshField
48 #include "stk_mesh/base/Entity.hpp" // for Entity
49 #include "stk_mesh/base/FieldBase.hpp" // for field_data
50 #include "stk_topology/topology.hpp" // for topology, etc
51 #include "stk_util/parallel/Parallel.hpp"
52 namespace {
53
54 TEST(StkMeshIoBrokerHowTo, handleMissingFieldOnRead)
55 {
56 std::string ic_name = "input_field_example.e";
57 MPI_Comm communicator = MPI_COMM_WORLD;
58 int numProcs = stk::parallel_machine_size(communicator);
59 if (numProcs != 1) {
60 return;
61 }
62
63 {
64 // ============================================================
65 //+ INITIALIZATION:
66 //+ Create a mesh with the nodal field "temp" for 3 timesteps.
67 //+ The value of the field at each node is 0.0 at time 0.0,
68 //+ 1.0 at time 1.0, and 2.0 at time 2.0
69 stk::io::StkMeshIoBroker stkIo(communicator);
70
71 const std::string generatedFileName = "generated:8x8x8|nodeset:xyz";
72 size_t index = stkIo.add_mesh_database(generatedFileName, stk::io::READ_MESH);
73 stkIo.set_active_mesh(index); // Optional if only a single input database
74 stkIo.create_input_mesh();
75
76 stk::mesh::Field<double> &temperature =
77 stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, "temperature", 1);
78 stk::mesh::put_field_on_mesh(temperature, stkIo.meta_data().universal_part(), nullptr);
79 stkIo.populate_bulk_data();
80
81 size_t fh = stkIo.create_output_mesh(ic_name, stk::io::WRITE_RESULTS);
82
83 //+ The name of the field on the database will be "temp"
84 stkIo.add_field(fh, temperature, "temp");
85
86 // Add three steps to the database
87 // For each step, the value of the field is the value 'time'
88 for (size_t i=0; i < 3; i++) {
89 double time = i;
90
91 stk::mesh::field_fill(time, temperature);
92
93 stkIo.begin_output_step(fh, time);
94 stkIo.write_defined_output_fields(fh);
95 stkIo.end_output_step(fh);
96 }
97 }
98
99 {
100 //-BEGIN
101 // ============================================================
102 //+ EXAMPLE:
103 //+ Demonstrate what happens when application requests the
104 //+ reading of a field that does not exist on the input
105 //+ mesh database. The nodal field "displacement" is
106 //+ requested for input from the database field "disp" which
107 //+ does not exist.
108 stk::io::StkMeshIoBroker stkIo(communicator);
109 size_t index = stkIo.add_mesh_database(ic_name, stk::io::READ_MESH);
110 stkIo.set_active_mesh(index);
111 stkIo.create_input_mesh();
112
113 stk::mesh::Field<double> &temperature =
114 stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, "temperature", 1);
115 stk::mesh::put_field_on_mesh(temperature, stkIo.meta_data().universal_part(), nullptr);
116
117 stk::mesh::Field<double> &displacement =
118 stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, "displacement", 3);
119 stk::mesh::put_field_on_mesh(displacement, stkIo.meta_data().universal_part(), nullptr);
120 stkIo.populate_bulk_data();
121
122 // The name of the field on the database is "temp"
123 // This field does exist and should be read correctly
124 stkIo.add_input_field(stk::io::MeshField(temperature, "temp"));
125
126 //+ The name of the field on the database is "disp"
127 //+ This field does not exist and will not be found.
128 stkIo.add_input_field(stk::io::MeshField(displacement, "disp"));
129
130
131 //+ Read the field values from the database at time 2.0
132 //+ The 'missing_fields' vector will contain the names of
133 //+ any fields that were not found.
134 std::vector<stk::io::MeshField> missing_fields;
135 stkIo.read_defined_input_fields(2.0, &missing_fields);
136
137 // ============================================================
138 //+ VERIFICATION
139 //+ The 'missing' vector should be of size 1 and contain
140 //+ 'disp'
141 EXPECT_EQ(2u, missing_fields.size());
142 EXPECT_EQ("disp", missing_fields[0].db_name());
143 EXPECT_EQ("displacement", missing_fields[0].field()->name());
144 EXPECT_EQ("disp", missing_fields[1].db_name());
145 EXPECT_EQ("displacement_STKFS_N", missing_fields[1].field()->name());
146
147 // The value of the "temperature" field at all nodes should be 2.0
148 stk::mesh::for_each_entity_run(stkIo.bulk_data(), stk::topology::NODE_RANK,
149 [&](const stk::mesh::BulkData& /*bulk*/, stk::mesh::Entity node) {
150 double *fieldDataForNode = stk::mesh::field_data(temperature, node);
151 EXPECT_DOUBLE_EQ(2.0, *fieldDataForNode);
152 });
153 //-END
154 }
155 // ============================================================
156 // Cleanup
157 unlink(ic_name.c_str());
158 }
159 }