Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/UnitTestCommunicateFieldData.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 "SetupKeyholeMesh.hpp"
36 #include "mpi.h" // for MPI_COMM_WORLD, etc
37 #include "stk_mesh/base/Bucket.hpp" // for Bucket
38 #include "stk_mesh/base/Entity.hpp" // for Entity
39 #include "stk_mesh/base/Field.hpp" // for Field
40 #include "stk_mesh/base/FieldBase.hpp" // for field_data, etc
41 #include "stk_mesh/base/MetaData.hpp" // for MetaData, put_field
42 #include "stk_mesh/base/Part.hpp" // for Part
43 #include "stk_mesh/base/Selector.hpp" // for operator!, Selector, etc
44 #include "stk_mesh/base/Types.hpp" // for EntityProc, BucketVector, etc
45 #include "stk_topology/topology.hpp" // for topology, etc
46 #include <gtest/gtest.h> // for AssertHelper, ASSERT_EQ, etc
47 #include <iostream> // for basic_ostream::operator<<
48 #include <stddef.h> // for size_t
49 #include <stk_mesh/base/BulkData.hpp> // for BulkData, etc
50 #include <stk_mesh/base/FieldParallel.hpp> // for communicate_field_data
51 #include <stk_util/parallel/Parallel.hpp> // for parallel_machine_rank, etc
52 #include <stk_unit_test_utils/BuildMesh.hpp>
53 #include <vector> // for vector
54 namespace stk { namespace mesh { class Ghosting; } }
55 using stk::unit_test_util::build_mesh;
56
57 TEST(CommunicateFieldData, communicate)
58 {
59 stk::ParallelMachine communicator = MPI_COMM_WORLD;
60
61 int numProcs = stk::parallel_machine_size(communicator);
62 if (numProcs != 2) {
63 return;
64 }
65 int myProc = stk::parallel_machine_rank(communicator);
66
67 const unsigned spatialDim = 2;
68 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, communicator);
69 stk::mesh::MetaData& meta = bulkPtr->mesh_meta_data();
70 stk::mesh::BulkData& bulk = *bulkPtr;
71
72 stk::mesh::Field<double>& field = meta.declare_field<double>(stk::topology::NODE_RANK, "field1");
73 stk::mesh::put_field_on_mesh(field, meta.universal_part(), nullptr);
74
75 stk::mesh::unit_test::setupKeyholeMesh2D_case1(bulk);
76
77 stk::mesh::Part& owned_part = meta.locally_owned_part();
78 const stk::mesh::BucketVector& owned_node_buckets = bulk.get_buckets(stk::topology::NODE_RANK,owned_part);
79
80 for(size_t i=0; i<owned_node_buckets.size(); ++i) {
81 stk::mesh::Bucket& bucket = *owned_node_buckets[i];
82 for(size_t j=0; j<bucket.size(); ++j) {
83 stk::mesh::Entity node = bucket[j];
84 double value = myProc*100 + bulk.identifier(node);
85 double* data = stk::mesh::field_data(field, node);
86 *data = value;
87 }
88 }
89
90 const stk::mesh::Ghosting& aura_ghosting = *bulk.ghostings()[stk::mesh::BulkData::AURA];
91
92 std::vector<const stk::mesh::FieldBase*> fields(1, &field);
93 stk::mesh::communicate_field_data(aura_ghosting, fields);
94
95 stk::mesh::Part& aura_part = meta.aura_part();
96 const stk::mesh::BucketVector& aura_node_buckets = bulk.get_buckets(stk::topology::NODE_RANK, aura_part);
97 for(size_t i=0; i<aura_node_buckets.size(); ++i) {
98 stk::mesh::Bucket& bucket = *aura_node_buckets[i];
99 for(size_t j=0; j<bucket.size(); ++j) {
100 stk::mesh::Entity node = bucket[j];
101 int owner = bulk.parallel_owner_rank(node);
102 double value = owner*100 + bulk.identifier(node);
103 double* data = stk::mesh::field_data(field, node);
104 EXPECT_EQ(*data, value);
105 }
106 }
107 }
108
109
110
111 TEST(CommunicateFieldData, communicateMultipleGhostings)
112 {
113 stk::ParallelMachine communicator = MPI_COMM_WORLD;
114
115 int numProcs = stk::parallel_machine_size(communicator);
116 if (numProcs != 2) {
117 return;
118 }
119 int myProc = stk::parallel_machine_rank(communicator);
120
121 const unsigned spatialDim = 2;
122 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, communicator);
123 stk::mesh::MetaData& meta = bulkPtr->mesh_meta_data();
124 stk::mesh::BulkData& bulk = *bulkPtr;
125
126 stk::mesh::Field<double>& field = meta.declare_field<double>(stk::topology::NODE_RANK, "field1");
127 stk::mesh::put_field_on_mesh(field, meta.universal_part(), nullptr);
128
129 stk::mesh::unit_test::setupKeyholeMesh2D_case2(bulk);
130
131 bulk.modification_begin();
132 // Create ghosting
133 stk::mesh::Ghosting & ghosting_1 = bulk.create_ghosting( "CUSTOM_1" );
134 stk::mesh::Ghosting & ghosting_2 = bulk.create_ghosting( "CUSTOM_2" );
135 stk::mesh::Ghosting & ghosting_3 = bulk.create_ghosting( "CUSTOM_3" );
136
137 std::vector<stk::mesh::EntityProc> ghosting_1_send;
138 std::vector<stk::mesh::EntityProc> ghosting_2_send;
139 std::vector<stk::mesh::EntityProc> ghosting_3_send;
140 if (myProc == 1)
141 {
142 const int proc_0 = 0;
143 ghosting_1_send.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, stk::mesh::EntityId(7)),proc_0));
144 ghosting_2_send.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, stk::mesh::EntityId(8)),proc_0));
145 ghosting_3_send.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, stk::mesh::EntityId(9)),proc_0));
146 ghosting_3_send.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, stk::mesh::EntityId(7)),proc_0)); // duplicate entity
147 }
148 bulk.change_ghosting( ghosting_1, ghosting_1_send );
149 bulk.change_ghosting( ghosting_2, ghosting_2_send );
150 bulk.change_ghosting( ghosting_3, ghosting_3_send );
151 bulk.modification_end();
152
153 stk::mesh::Part& owned_part = meta.locally_owned_part();
154 const stk::mesh::BucketVector& owned_node_buckets = bulk.get_buckets(stk::topology::NODE_RANK,owned_part);
155
156 for(size_t i=0; i<owned_node_buckets.size(); ++i) {
157 stk::mesh::Bucket& bucket = *owned_node_buckets[i];
158 for(size_t j=0; j<bucket.size(); ++j) {
159 stk::mesh::Entity node = bucket[j];
160 double value = myProc*100 + bulk.identifier(node);
161 double* data = stk::mesh::field_data(field, node);
162 *data = value;
163 }
164 }
165
166 stk::mesh::Selector select_ghosted = (!meta.locally_owned_part()) & (!meta.globally_shared_part());
167 const stk::mesh::BucketVector& ghosted_buckets = bulk.get_buckets(stk::topology::NODE_RANK,select_ghosted);
168
169 int num_ghosted_nodes = 0;
170 for(size_t i=0; i<ghosted_buckets.size(); ++i) {
171 stk::mesh::Bucket& bucket = *ghosted_buckets[i];
172 for(size_t j=0; j<bucket.size(); ++j) {
173 ++num_ghosted_nodes;
174 stk::mesh::Entity node = bucket[j];
175 double garbage = -1.2345;
176 double* data = stk::mesh::field_data(field, node);
177 *data = garbage;
178 }
179 }
180 if (myProc == 0) {
181 ASSERT_EQ(5, num_ghosted_nodes);
182 }
183 else { // myProc == 1
184 ASSERT_EQ(2, num_ghosted_nodes);
185 }
186
187 std::vector<const stk::mesh::FieldBase*> fields(1, &field);
188 stk::mesh::communicate_field_data(bulk, fields);
189
190 for(size_t i=0; i<ghosted_buckets.size(); ++i) {
191 stk::mesh::Bucket& bucket = *ghosted_buckets[i];
192 for(size_t j=0; j<bucket.size(); ++j) {
193 stk::mesh::Entity node = bucket[j];
194 int owner = bulk.parallel_owner_rank(node);
195 double value = owner*100 + bulk.identifier(node);
196 double* data = stk::mesh::field_data(field, node);
197 EXPECT_EQ(value, *data);
198 }
199 }
200 }