Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/readInitialConditionTwoFieldSubset.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, readInitialConditionTwoFieldSubset)
61 {
62 //-BEGIN
63 std::string dbFieldNameShell = "ElementBlock_1";
64 std::string dbFieldNameOther = "ElementBlock_2";
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 // ============================================================
75 // INITIALIZATION
76 //+ Create a generated mesh containg hexes and shells with two
77 //+ element variables -- ElementBlock_1 and ElementBlock_2
78 std::string input_filename = "9x9x9|shell:xyzXYZ|variables:element,2|times:1";
79
80 stk::io::StkMeshIoBroker stkIo(communicator);
81 stkIo.add_mesh_database(input_filename, "generated", stk::io::READ_MESH);
82 stkIo.create_input_mesh();
83
84 stk::mesh::MetaData &meta_data = stkIo.meta_data();
85
86 // Declare the element "pressure" field...
87 stk::mesh::Field<double> &pressure =
88 stkIo.meta_data().declare_field<double>(stk::topology::ELEMENT_RANK, appFieldName,1);
89
90 stk::io::MeshField mf_shell(pressure, dbFieldNameShell);
91 stk::io::MeshField mf_other(pressure, dbFieldNameOther);
92
93 const stk::mesh::PartVector &all_parts = meta_data.get_mesh_parts();
94 for (size_t i=0; i < all_parts.size(); i++) {
95 const stk::mesh::Part *part = all_parts[i];
96
97 //+ Put the field on all element block parts...
98 stk::mesh::put_field_on_mesh(pressure, *part, nullptr);
99
100 stk::topology topo = part->topology();
101 if (topo == stk::topology::SHELL_QUAD_4) {
102 //+ The shell blocks will have the pressure field initialized
103 //+ from the dbFieldNameShell database variable.
104 mf_shell.add_subset(*part);
105 }
106 else {
107 //+ The non-shell blocks will have the pressure field initialized
108 //+ from the dbFieldNameOther database variable.
109 mf_other.add_subset(*part);
110 }
111 }
112
113 stkIo.add_input_field(mf_shell);
114 stkIo.add_input_field(mf_other);
115 stkIo.populate_bulk_data();
116
117 double time = stkIo.get_input_ioss_region()->get_state_time(1);
118
119 //+ Populate the fields with data from the input mesh.
120 stkIo.read_defined_input_fields(time);
121
122 //-END
123 // ============================================================
124 //+ VERIFICATION
125 //+ The value of the field on all elements should be sqrt(i+1)
126 std::vector<stk::mesh::Entity> elements;
127 stk::mesh::get_entities(stkIo.bulk_data(), stk::topology::ELEMENT_RANK,
128 elements);
129 EXPECT_TRUE(elements.size() >= 729);
130
131 for(size_t i=0; i<elements.size(); i++) {
132 double *fieldDataForElement = stk::mesh::field_data(pressure, elements[i]);
133 EXPECT_DOUBLE_EQ(sqrt(i+1), *fieldDataForElement);
134 }
135 }
136 }
137 }