Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/interpolateFieldCyclic.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, etc
36 #include <stddef.h> // for size_t
37 #include <unistd.h> // for unlink
38 #include <iomanip> // for operator<<
39 #include <stk_io/InputFile.hpp> // for InputFile, etc
40 #include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker
41 #include <stk_mesh/base/Field.hpp> // for Field
42 #include <stk_mesh/base/FieldBLAS.hpp>
43 #include <stk_mesh/base/ForEachEntity.hpp>
44 #include <stk_mesh/base/MetaData.hpp> // for MetaData, put_field
45 #include <string> // for string
46 #include <vector> // for vector
47 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc
48 #include "stk_io/MeshField.hpp" // for MeshField, etc
49 #include "stk_mesh/base/Entity.hpp" // for Entity
50 #include "stk_mesh/base/FieldBase.hpp" // for field_data
51 #include "stk_topology/topology.hpp" // for topology, etc
52 #include "stk_util/parallel/Parallel.hpp"
53
54 namespace {
55
56 TEST(StkMeshIoBrokerHowTo, interpolateFieldCyclic)
57 {
58 std::string ic_name = "interpolate_field_example.e";
59 MPI_Comm communicator = MPI_COMM_WORLD;
60 int numProcs = stk::parallel_machine_size(communicator);
61 if (numProcs != 1) {
62 return;
63 }
64
65 {
66 // ============================================================
67 //+ INITIALIZATION:
68 //+ Create a mesh with the nodal field "temp" for 3 timesteps.
69 //+ The value of the field at each node is 0.0 at time 0.0,
70 //+ 10.0 at time 10.0, and 20.0 at time 20.0
71 stk::io::StkMeshIoBroker stkIo(communicator);
72
73 const std::string generatedFileName = "generated:1x1x1|nodeset:xyz";
74 stkIo.add_mesh_database(generatedFileName, stk::io::READ_MESH);
75 stkIo.create_input_mesh();
76
77 stk::mesh::Field<double> &temperature =
78 stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, "temperature", 1);
79 stk::mesh::put_field_on_mesh(temperature, stkIo.meta_data().universal_part(), nullptr);
80 stkIo.populate_bulk_data();
81
82 size_t fh = stkIo.create_output_mesh(ic_name, stk::io::WRITE_RESULTS);
83
84 //+ The name of the field on the database will be "temp"
85 stkIo.add_field(fh, temperature, "temp");
86
87 // Add three steps to the database
88 // For each step, the value of the field is the value 'time'
89 for (size_t i=0; i < 3; i++) {
90 double time = i * 10.0;
91
92 stk::mesh::field_fill(time, temperature);
93
94 stkIo.begin_output_step(fh, time);
95 stkIo.write_defined_output_fields(fh);
96 stkIo.end_output_step(fh);
97 }
98 }
99
100 {
101 //-BEGIN
102 // ============================================================
103 //+ EXAMPLE:
104 //+ The input mesh database has 3 timesteps with times 0.0, 10.0, 20.0,
105 //+ The value of the field "temp" is equal to the time
106 //+ Read the "temp" value at times 0.0 to 10.0 with an interval
107 //+ of 0.25 (0.0, 0.25, 0.50, 0.75, ..., 10.0)
108 //+ The mapping from analysis time (0.0 to 10.0) to database
109 //+ time will be reverse cyclic and scaled.
110 //+
111 //+ The parameters are:
112 //+ * period = 2.0
113 //+ * scale = 10.0
114 //+ * offset = 0.0
115 //+ * cycle type = REVERSING
116 //+
117 //+ Analysis Time and DB_Time:
118 //+ 0 1 2 3 4 5 6 7 8 9 10
119 //+ 0 10 20 10 0 10 20 10 0 10 20
120 //+
121
122 stk::io::StkMeshIoBroker stkIo(communicator);
123 size_t idx = stkIo.add_mesh_database(ic_name, stk::io::READ_MESH);
124 stkIo.create_input_mesh();
125
126 stk::mesh::Field<double> &temperature =
127 stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, "temperature", 1);
128 stk::mesh::put_field_on_mesh(temperature, stkIo.meta_data().universal_part(), nullptr);
129
130 stkIo.populate_bulk_data();
131
132 // The name of the field on the database is "temp"
133 stkIo.add_input_field(stk::io::MeshField(temperature, "temp",
134 stk::io::MeshField::LINEAR_INTERPOLATION));
135
136 //+ Set the periodic parameters on the input mesh...
137 double period_length = 2.0;
138 double startup = 0.0;
139 double scale = 10.0;
140 stkIo.get_mesh_database(idx)
141 .set_periodic_time(period_length, startup, stk::io::InputFile::REVERSING)
142 .set_scale_time(scale)
143 .set_start_time(0.0).set_offset_time(0.0).set_stop_time(999.0); // These are optional
144 double delta_time = 0.25;
145 double time = 0.0;
146 double expected = 0.0;
147 double exp_inc = 10.0 * delta_time;
148
149 while (time <= 10.0) {
150
151 //+ Read the field values from the database and verify that they
152 //+ are interpolated correctly.
153 stkIo.read_defined_input_fields(time);
154
155 // ============================================================
156 //+ VERIFICATION
157 // The value of the "temperature" field at all nodes should be 'expected'
158 stk::mesh::for_each_entity_run(stkIo.bulk_data(), stk::topology::NODE_RANK,
159 [&](const stk::mesh::BulkData& /*bulk*/, stk::mesh::Entity node) {
160 double *fieldData = stk::mesh::field_data(temperature, node);
161 EXPECT_DOUBLE_EQ(expected, *fieldData);
162 });
163
164 time += delta_time;
165 expected += exp_inc;
166 if (expected >= 20.0 || expected <= 0.0) {
167 exp_inc = -exp_inc;
168 }
169 }
170 //-END
171 }
172 // ============================================================
173 // Cleanup
174 unlink(ic_name.c_str());
175 }
176 }