Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/readInitialConditionMultiSubset.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 <stk_mesh/base/MeshBuilder.hpp>
43 #include <string> // for allocator, operator+, etc
44 #include <vector> // for vector
45 #include "Ioss_DBUsage.h" // for DatabaseUsage::READ_MODEL
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/Part.hpp" // for Part
53 #include "stk_mesh/base/Selector.hpp" // for Selector
54 #include "stk_mesh/base/Types.hpp" // for PartVector
55 #include "stk_topology/topology.hpp" // for topology, etc
56 #include "stk_util/parallel/Parallel.hpp"
57
58 namespace Ioss { class DatabaseIO; }
59
60 namespace {
61
62 TEST(StkMeshIoBrokerHowTo, readInitialConditionMultiSubset)
63 {
64 std::string dbFieldNameShell = "ElementBlock_1";
65 std::string appFieldName = "pressure";
66
67 MPI_Comm communicator = MPI_COMM_WORLD;
68 int numProcs = stk::parallel_machine_size(communicator);
69 if (numProcs != 1) {
70 return;
71 }
72
73 {
74 //-BEGIN
75 // ============================================================
76 // INITIALIZATION
77 //+ Create a generated mesh containg hexes and shells with a
78 //+ single element variable -- pressure
79 std::string input_filename = "9x9x9|shell:xyzXYZ|variables:element,1|times:1";
80
81 stk::io::StkMeshIoBroker stkIo(communicator);
82 stkIo.add_mesh_database(input_filename, "generated", stk::io::READ_MESH);
83 stkIo.create_input_mesh();
84
85 stk::mesh::MetaData &meta_data = stkIo.meta_data();
86
87 // Declare the element "pressure" field...
88 stk::mesh::Field<double> &pressure =
89 stkIo.meta_data().declare_field<double>(stk::topology::ELEMENT_RANK, appFieldName, 1);
90
91 const stk::mesh::PartVector &all_parts = meta_data.get_mesh_parts();
92 for (size_t i=0; i < all_parts.size(); i++) {
93 //+ Put the field on all element block parts...
94 stk::mesh::put_field_on_mesh(pressure, *all_parts[i], nullptr);
95 }
96
97 // This commits BulkData and populates the coordinates, connectivity, mesh...
98 stkIo.populate_bulk_data();
99
100 double time = stkIo.get_input_ioss_region()->get_state_time(1);
101
102 //+ Initialize the "pressure" field from mesh data on the shell parts on demand...
103 for (size_t i=0; i < all_parts.size(); i++) {
104 stk::topology topo = all_parts[i]->topology();
105 if (topo == stk::topology::SHELL_QUAD_4) {
106
107 stk::io::MeshField mf(pressure, dbFieldNameShell);
108 mf.set_read_time(time);
109 mf.add_subset(*all_parts[i]);
110 stkIo.add_input_field(mf);
111 }
112 }
113
114 //+ Populate any other fields with data from the input mesh.
115 //+ This would *not* know about the MeshFields above since
116 //+ "add_input_field()" was not called...
117 stkIo.read_defined_input_fields(time);
118
119
120 //-END
121 // ============================================================
122 //+ VERIFICATION
123 //+ The value of the field on the first 729 elements should be 0.0;
124 //+ The value of the field on the remaining elements should be sqrt(i+1)
125 std::vector<stk::mesh::Entity> elements;
126 stk::mesh::get_entities(stkIo.bulk_data(), stk::topology::ELEMENT_RANK,
127 elements);
128 EXPECT_TRUE(elements.size() >= 729);
129
130 for(size_t i=0; i<729; i++) {
131 double *fieldDataForElement = stk::mesh::field_data(pressure, elements[i]);
132 EXPECT_DOUBLE_EQ(0.0, *fieldDataForElement);
133 }
134
135 for(size_t i=729; i<elements.size(); i++) {
136 double *fieldDataForElement = stk::mesh::field_data(pressure, elements[i]);
137 EXPECT_DOUBLE_EQ(sqrt(i+1), *fieldDataForElement);
138 }
139 }
140 }
141 }