Debian Codegraph @

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