Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/readAttributes.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/StkMeshIoBroker.hpp> // for StkMeshIoBroker
40 #include <stk_mesh/base/Field.hpp> // for Field
41 #include <stk_mesh/base/GetEntities.hpp> // for get_entities
42 #include <stk_mesh/base/MetaData.hpp> // for MetaData, put_field
43 #include <string> // for string
44 #include <vector> // for vector
45 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc
46 #include "stk_mesh/base/Entity.hpp" // for Entity
47 #include "stk_mesh/base/FieldBase.hpp" // for field_data
48 #include "stk_topology/topology.hpp" // for topology, etc
49 #include "stk_util/parallel/Parallel.hpp"
50 #include <stk_unit_test_utils/MeshFixture.hpp>
51 #include "stk_io/AttributeFields.hpp"
52
53 namespace {
54
55 class ExodusFileWithAttributes : public stk::unit_test_util::MeshFixture { };
56
57 stk::mesh::FieldVector get_attribute_fields_for_part(const stk::mesh::MetaData &meta, const stk::mesh::Part *ioPart)
58 {
59 stk::mesh::FieldVector attributes;
60
61 for(stk::mesh::FieldBase *field : meta.get_fields())
62 {
63 const Ioss::Field::RoleType *fieldRole = stk::io::get_field_role(*field);
64 if(fieldRole != nullptr && *fieldRole == Ioss::Field::ATTRIBUTE)
65 {
66 for(const stk::mesh::FieldBase::Restriction &restriction : field->restrictions())
67 {
68 const stk::mesh::Selector &selector = restriction.selector();
69 if(selector(ioPart))
70 attributes.push_back(field);
71 }
72 }
73 }
74 return attributes;
75 }
76
77 //-BEGIN
78 std::vector<double> get_attributes_of_first_element(const stk::mesh::BulkData &bulk, const stk::mesh::Part *ioPart)
79 {
80 stk::mesh::FieldVector attributeFields = get_attribute_fields_for_part(bulk.mesh_meta_data(), ioPart);
81
82 stk::mesh::EntityVector elements;
83 stk::mesh::get_entities(bulk, stk::topology::ELEM_RANK, *ioPart, elements);
84
85 std::vector<double> attributes;
86 if(!elements.empty()) {
87 for(const stk::mesh::FieldBase *field : attributeFields) {
88 unsigned numAttribute = stk::mesh::field_scalars_per_entity(*field, elements[0]);
89 double *dataForElement = static_cast<double*> (stk::mesh::field_data(*field, elements[0]));
90 for(unsigned i=0; i<numAttribute; ++i)
91 attributes.push_back(dataForElement[i]);
92 }
93 }
94 return attributes;
95 }
96
97 TEST_F(ExodusFileWithAttributes, readAttributes_haveFieldsWithAttributes_externalFile)
98 {
99 setup_mesh("hex_spider.exo", stk::mesh::BulkData::AUTO_AURA);
100
101 const stk::mesh::Part *partBlock2 = get_meta().get_part("block_2");
102 const stk::mesh::Part *partBlock10 = get_meta().get_part("block_10");
103
104 EXPECT_EQ(1u, get_attributes_of_first_element(get_bulk(), partBlock2).size());
105 EXPECT_EQ(7u, get_attributes_of_first_element(get_bulk(), partBlock10).size());
106 }
107
108 void mark_field_as_attribute(stk::mesh::FieldBase &field)
109 {
110 stk::io::set_field_role(field, Ioss::Field::ATTRIBUTE);
111 }
112
113 TEST_F(ExodusFileWithAttributes, addAttribute_haveFieldsWithAttribute_externalFile)
114 {
115 allocate_bulk(stk::mesh::BulkData::AUTO_AURA);
116
117 stk::io::StkMeshIoBroker stkIo;
118 stkIo.set_bulk_data(get_bulk());
119 stkIo.add_mesh_database("hex_spider.exo", stk::io::READ_MESH);
120 stkIo.create_input_mesh();
121
122 double initialValue = 0.0;
123 auto &newAttrField = get_meta().declare_field<double>(stk::topology::ELEM_RANK, "newAttr");
124 mark_field_as_attribute(newAttrField);
125
126 const stk::mesh::Part *partBlock10 = get_meta().get_part("block_10");
127 stk::mesh::put_field_on_mesh(newAttrField, *partBlock10, &initialValue);
128
129 stkIo.populate_bulk_data();
130
131 EXPECT_EQ(8u, get_attributes_of_first_element(get_bulk(), partBlock10).size());
132 }
133 //-END
134 }