Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/requestedResultsFieldName.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_FALSE, 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 string
43 #include <vector> // for vector
44 #include "Ioss_NodeBlock.h" // for NodeBlock
45 #include "Ioss_Region.h" // for NodeBlockContainer, Region
46 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc
47 #include "stk_mesh/base/Entity.hpp" // for Entity
48 #include "stk_mesh/base/FieldBase.hpp" // for field_data
49 #include "stk_topology/topology.hpp" // for topology, etc
50
51 namespace {
52
53 TEST(StkMeshIoBrokerHowTo, writeResults)
54 {
55 std::string mesh_name = "input_mesh_example.e";
56 std::string results_name = "output.results";
57 MPI_Comm communicator = MPI_COMM_WORLD;
58
59 {
60 // ============================================================
61 //+ INITIALIZATION:
62 //+ Create a basic mesh with a hex block, 3 shell blocks, 3 nodesets, and 3 sidesets.
63 stk::io::StkMeshIoBroker stkIo(communicator);
64
65 const std::string generatedFileName = "generated:8x8x8|shell:xyz|nodeset:xyz|sideset:XYZ";
66 size_t index = stkIo.add_mesh_database(generatedFileName, stk::io::READ_MESH);
67 stkIo.set_active_mesh(index);
68 stkIo.create_input_mesh();
69 stkIo.populate_bulk_data();
70
71 size_t fh = stkIo.create_output_mesh(mesh_name, stk::io::WRITE_RESULTS);
72 stkIo.write_output_mesh(fh);
73 }
74
75 {
76 // ============================================================
77 //+ EXAMPLE:
78 //+ Read mesh data from the specified file.
79 stk::io::StkMeshIoBroker stkIo(communicator);
80 stkIo.add_mesh_database(mesh_name, stk::io::READ_MESH);
81
82 //+ Creates meta data; creates parts
83 stkIo.create_input_mesh();
84
85 //+ Declare a field
86 //+ NOTE: Fields must be declared before "populate_bulk_data()" is called
87 //+ since it commits the meta data.
88 const std::string fieldName = "disp";
89 stk::mesh::Field<double> &field = stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, fieldName, 1);
90 stk::mesh::put_field_on_mesh(field, stkIo.meta_data().universal_part(), nullptr);
91
92 //+ commit the meta data and create the bulk data.
93 //+ populate the bulk data with data from the mesh file.
94 stkIo.populate_bulk_data();
95
96 // ============================================================
97 //+ Create results file. By default, all parts created from the input
98 //+ mesh will be written to the results output file.
99 size_t fh = stkIo.create_output_mesh(results_name, stk::io::WRITE_RESULTS);
100
101 //-BEGIN
102 //+ The field 'fieldName' will be output to the results file with the name 'alternateFieldName'
103 std::string alternateFieldName("displacement");
104 stkIo.add_field(fh, field, alternateFieldName);
105 //-END
106
107 std::vector<stk::mesh::Entity> nodes;
108 stk::mesh::get_entities(stkIo.bulk_data(), stk::topology::NODE_RANK, nodes);
109
110 // Iterate the application's execute loop five times and output
111 // field data each iteration.
112 for (int step=0; step < 5; step++) {
113 double time = step;
114
115 // Application execution...
116 double value = 10.0 * time;
117 for(size_t i=0; i<nodes.size(); i++) {
118 double *node_data = stk::mesh::field_data(field, nodes[i]);
119 *node_data = value;
120 }
121
122 //+ Output the field data calculated by the application.
123 stkIo.begin_output_step(fh, time);
124 stkIo.write_defined_output_fields(fh);
125 stkIo.end_output_step(fh);
126 }
127
128 // ============================================================
129 //+ VERIFICATION
130 Ioss::Region *io_region = stkIo.get_output_ioss_region(fh).get();
131 Ioss::NodeBlock *nb = io_region->get_node_blocks()[0];
132 ASSERT_TRUE(nb->field_exists(alternateFieldName));
133 ASSERT_FALSE(nb->field_exists(fieldName));
134 }
135
136 // ============================================================
137 // Cleanup
138 unlink(results_name.c_str());
139 }
140 }
141