Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/UnitTestChangeEntityId.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, EXPECT_EQ, etc
36 #include <stddef.h> // for size_t
37 #include <vector> // for vector, operator==
38 #include "mpi.h" // for MPI_COMM_WORLD, etc
39
40 #include "stk_mesh/base/Bucket.hpp" // for Bucket
41 #include "stk_mesh/base/BulkData.hpp" // for BulkData
42 #include "stk_mesh/base/Entity.hpp" // for Entity
43 #include "stk_mesh/base/Field.hpp" // for Field
44 #include "stk_mesh/base/FieldBase.hpp" // for field_data
45 #include "stk_mesh/base/MetaData.hpp" // for MetaData, put_field
46 #include "stk_mesh/base/Types.hpp" // for EntityId, PartVector, etc
47 #include "stk_topology/topology.hpp" // for topology, etc
48 #include "stk_unit_test_utils/stk_mesh_fixtures/HexFixture.hpp" // for HexFixture
49 #include "stk_unit_test_utils/BuildMesh.hpp"
50 #include "stk_util/parallel/Parallel.hpp" // for ParallelMachine
51 namespace stk { namespace mesh { class Part; } }
52
53 using stk::unit_test_util::build_mesh;
54
55 TEST( UnitTestChangeEntityId, change_id_small )
56 {
57 // change_entity_id is a broken concept in parallel:
58 // Only entity owners should have to call change-id. As it stands now,
59 // every process that might know about the entity, even as a ghost,
60 // must make the call. If any sharer/ghoster of the changing entity
61 // forgets to make the call, or changes to wrong id, awful errors ensue
62 // in the next modification_end. The only way to make this safe is to have
63 // change_entity_id be a parallel call similar to change_entity_owner; this
64 // would be very expensive. We need to implement the tracking of exodus-ids
65 // in a different fashion in the long run.
66
67 // Demonstrate how change_entity_id should work
68
69 using namespace stk::mesh;
70
71 stk::ParallelMachine pm = MPI_COMM_WORLD;
72
73 const unsigned spatial_dim = 2;
74 std::shared_ptr<BulkData> bulkPtr = build_mesh(spatial_dim, pm);
75 MetaData& meta_data = bulkPtr->mesh_meta_data();
76 Part &quad4_part = meta_data.declare_part_with_topology("quad4_Part", stk::topology::QUAD_4_2D);
77 meta_data.commit();
78
79 BulkData& mesh = *bulkPtr;
80 int p_rank = mesh.parallel_rank();
81 int p_numProcs = mesh.parallel_size();
82 if (p_numProcs > 1) {
83 //change-entity-id test only supported in serial
84 return;
85 }
86
87 stk::mesh::PartVector elem_parts;
88 elem_parts.push_back(&quad4_part);
89
90 mesh.modification_begin();
91
92 Entity elem = mesh.declare_element(p_rank + 1 /*id*/, elem_parts);
93
94 Entity node0 = mesh.declare_node(p_rank*3 + 5000 /*id*/);
95 Entity node1_local_chg_id = mesh.declare_node(p_rank*3 + 1 /*id*/);
96 Entity node2_shared_chg_id = mesh.declare_node( 2 /*id*/);
97 Entity node3 = mesh.declare_node(p_rank*3 + 3 /*id*/);
98
99 mesh.declare_relation(elem, node0 , 0 /*relation ordinal*/);
100 mesh.declare_relation(elem, node1_local_chg_id , 1 /*relation ordinal*/);
101 mesh.declare_relation(elem, node2_shared_chg_id, 2 /*relation ordinal*/);
102 mesh.declare_relation(elem, node3 , 3 /*relation ordinal*/);
103
104 mesh.modification_end();
105
106 mesh.modification_begin();
107
108 const EntityId new_id_for_local_node = 42 + p_rank + 1;
109 const EntityId new_id_for_shared_node = 666;
110
111 mesh.change_entity_id(new_id_for_local_node, node1_local_chg_id);
112 if (mesh.parallel_owner_rank(node2_shared_chg_id) == p_rank) {
113 mesh.change_entity_id(new_id_for_shared_node, node2_shared_chg_id);
114 }
115
116 EXPECT_EQ(new_id_for_local_node, mesh.identifier(node1_local_chg_id));
117
118 mesh.modification_end();
119
120 EXPECT_EQ(new_id_for_local_node, mesh.identifier(node1_local_chg_id));
121 EXPECT_EQ(new_id_for_shared_node, mesh.identifier(node2_shared_chg_id));
122 }
123
124 TEST( UnitTestChangeEntityId, change_id_large )
125 {
126 using namespace stk::mesh;
127
128 const unsigned NX = 20;
129 const unsigned NY = 20;
130 const unsigned NZ = 20;
131 const unsigned num_elems = NX * NY * NZ;
132
133 fixtures::HexFixture hf(MPI_COMM_WORLD,NX,NY,NZ);
134
135 Field<int> & simple_nodal_field = hf.m_meta.declare_field<int>(stk::topology::NODE_RANK, "simple_nodal_field");
136
137 put_field_on_mesh(simple_nodal_field, *hf.m_elem_parts[0], nullptr);
138
139 //create nodal field on hex topo
140
141 hf.m_meta.commit();
142
143 hf.generate_mesh();
144
145 stk::mesh::BulkData & mesh = hf.m_bulk_data;
146 if (mesh.parallel_size() > 1) {
147 return;
148 }
149
150 mesh.modification_begin();
151
152 const BucketVector & node_buckets = mesh.buckets(stk::topology::NODE_RANK);
153
154 for(Bucket * b : node_buckets) {
155 int* nodal_field = stk::mesh::field_data( simple_nodal_field, *b );
156 for (size_t i =0; i<b->size(); ++i) {
157 nodal_field[i] = 1;
158 }
159 }
160
161 const BucketVector & elem_buckets = mesh.buckets(stk::topology::ELEMENT_RANK);
162
163 std::vector<EntityId> old_ids;
164 old_ids.reserve(num_elems);
165 for(Bucket * b : elem_buckets) {
166 for (size_t i =0; i<b->size(); ++i) {
167 Entity e = (*b)[i];
168 old_ids.push_back(mesh.identifier(e));
169 mesh.change_entity_id( mesh.identifier(e)+num_elems, e);
170 }
171 }
172
173 mesh.modification_end();
174
175 mesh.modification_begin();
176 mesh.modification_end();
177
178 std::vector<EntityId> new_ids_minus_num_elems;
179 new_ids_minus_num_elems.reserve(num_elems);
180 for(Bucket * b : elem_buckets) {
181 for (size_t i =0; i<b->size(); ++i) {
182 Entity e = (*b)[i];
183 new_ids_minus_num_elems.push_back(mesh.identifier(e)-num_elems);
184 }
185 }
186
187 EXPECT_TRUE(old_ids == new_ids_minus_num_elems);
188
189 for(Bucket * b : node_buckets) {
190 int* nodal_field = stk::mesh::field_data( simple_nodal_field, *b );
191 for (size_t i =0; i<b->size(); ++i) {
192 EXPECT_TRUE( nodal_field[i] == 1);
193 }
194 }
195 }