Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/ngp/TestNgpMeshUpdate.cpp

raw
1 #include <gtest/gtest.h>
2 #include <stk_mesh/base/Ngp.hpp>
3 #include <stk_mesh/base/NgpAtomics.hpp>
4 #include <stk_mesh/base/NgpReductions.hpp>
5 #include <stk_mesh/base/NgpMesh.hpp>
6 #include <stk_mesh/base/NgpField.hpp>
7 #include <stk_unit_test_utils/MeshFixture.hpp>
8 #include <stk_mesh/base/MetaData.hpp>
9 #include <stk_mesh/base/BulkData.hpp>
10 #include <stk_mesh/base/GetNgpMesh.hpp>
11 #include "NgpUnitTestUtils.hpp"
12
13 namespace {
14
15 using ngp_unit_test_utils::check_bucket_layout;
16 using NgpMeshDefaultMemSpace = stk::mesh::NgpMeshDefaultMemSpace;
17
18 class UpdateNgpMesh : public stk::unit_test_util::MeshFixture
19 {
20 public:
21 void setup_test_mesh()
22 {
23 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
24 extraPart = &get_meta().declare_part("extraPart");
25 std::string meshDesc = "0,1,HEX_8,1,2,3,4,5,6,7,8\n";
26 stk::unit_test_util::setup_text_mesh(get_bulk(), meshDesc);
27 }
28 const stk::mesh::Part* extraPart = nullptr;
29 };
30
31 TEST_F(UpdateNgpMesh, explicitUpdate)
32 {
33 if (stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { GTEST_SKIP(); }
34 setup_test_mesh();
35
36 stk::mesh::NgpMesh * ngpMesh = &stk::mesh::get_updated_ngp_mesh(get_bulk());
37
38 get_bulk().modification_begin();
39 stk::mesh::Entity node1 = get_bulk().get_entity(stk::topology::NODE_RANK, 1);
40 get_bulk().change_entity_parts(node1, stk::mesh::ConstPartVector{extraPart});
41 get_bulk().modification_end();
42
43 EXPECT_FALSE(ngpMesh->is_up_to_date());
44 ngpMesh = &stk::mesh::get_updated_ngp_mesh(get_bulk());
45 EXPECT_TRUE(ngpMesh->is_up_to_date());
46 }
47
48 TEST_F(UpdateNgpMesh, explicitUpdate_custom_NgpMemSpace)
49 {
50 if (stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { GTEST_SKIP(); }
51 setup_test_mesh();
52
53 stk::mesh::NgpMeshT<NgpMeshDefaultMemSpace> * ngpMesh = &stk::mesh::get_updated_ngp_mesh<NgpMeshDefaultMemSpace>(get_bulk());
54
55 get_bulk().modification_begin();
56 stk::mesh::Entity node1 = get_bulk().get_entity(stk::topology::NODE_RANK, 1);
57 get_bulk().change_entity_parts(node1, stk::mesh::ConstPartVector{extraPart});
58 get_bulk().modification_end();
59
60 EXPECT_FALSE(ngpMesh->is_up_to_date());
61 ngpMesh = &stk::mesh::get_updated_ngp_mesh<NgpMeshDefaultMemSpace>(get_bulk());
62 EXPECT_TRUE(ngpMesh->is_up_to_date());
63 }
64
65 TEST_F(UpdateNgpMesh, referenceGetsUpdated)
66 {
67 if (stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { GTEST_SKIP(); }
68 setup_test_mesh();
69
70 // If storing a persistent reference, call the get_updated_ngp_mesh() function
71 // to ensure that it is synchronized with BulkData
72 stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
73
74 get_bulk().modification_begin();
75 stk::mesh::Entity node1 = get_bulk().get_entity(stk::topology::NODE_RANK, 1);
76 get_bulk().change_entity_parts(node1, stk::mesh::ConstPartVector{extraPart});
77 get_bulk().modification_end();
78
79 EXPECT_FALSE(ngpMesh.is_up_to_date());
80 stk::mesh::get_updated_ngp_mesh(get_bulk()); // Trigger update
81 EXPECT_TRUE(ngpMesh.is_up_to_date());
82 }
83
84 TEST_F(UpdateNgpMesh, referenceGetsUpdated_custom_NgpMemSpace)
85 {
86 if (stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { GTEST_SKIP(); }
87 setup_test_mesh();
88
89 // If storing a persistent reference, call the get_updated_ngp_mesh() function
90 // to ensure that it is synchronized with BulkData
91 stk::mesh::NgpMeshT<NgpMeshDefaultMemSpace> & ngpMesh = stk::mesh::get_updated_ngp_mesh<NgpMeshDefaultMemSpace>(get_bulk());
92
93 get_bulk().modification_begin();
94 stk::mesh::Entity node1 = get_bulk().get_entity(stk::topology::NODE_RANK, 1);
95 get_bulk().change_entity_parts(node1, stk::mesh::ConstPartVector{extraPart});
96 get_bulk().modification_end();
97
98 EXPECT_FALSE(ngpMesh.is_up_to_date());
99 stk::mesh::get_updated_ngp_mesh<NgpMeshDefaultMemSpace>(get_bulk()); // Trigger update
100 EXPECT_TRUE(ngpMesh.is_up_to_date());
101 }
102
103 TEST_F(UpdateNgpMesh, OnlyOneDeviceMesh_InternalAndExternal)
104 {
105 setup_test_mesh();
106
107 // Create first NgpMesh inside BulkData
108 stk::mesh::get_updated_ngp_mesh(get_bulk());
109
110 #ifdef STK_USE_DEVICE_MESH
111 EXPECT_THROW(stk::mesh::NgpMesh secondNgpMesh(get_bulk()), std::logic_error);
112 #else
113 EXPECT_NO_THROW(stk::mesh::NgpMesh secondNgpMesh(get_bulk()));
114 #endif
115 }
116
117 TEST_F(UpdateNgpMesh, OnlyOneDeviceMesh_InternalAndExternal_custom_NgpMemSpace)
118 {
119 setup_test_mesh();
120
121 // Create first NgpMesh inside BulkData
122 stk::mesh::get_updated_ngp_mesh<NgpMeshDefaultMemSpace>(get_bulk());
123
124 #ifdef STK_USE_DEVICE_MESH
125 EXPECT_THROW(stk::mesh::NgpMeshT<NgpMeshDefaultMemSpace> secondNgpMesh(get_bulk()), std::logic_error);
126 #else
127 EXPECT_NO_THROW(stk::mesh::NgpMeshT<NgpMeshDefaultMemSpace> secondNgpMesh(get_bulk()));
128 #endif
129 }
130
131 TEST_F(UpdateNgpMesh, OnlyOneDeviceMesh_TwoExternal)
132 {
133 setup_test_mesh();
134
135 stk::mesh::NgpMesh firstNgpMesh(get_bulk());
136
137 #ifdef STK_USE_DEVICE_MESH
138 EXPECT_THROW(stk::mesh::NgpMesh secondNgpMesh(get_bulk()), std::logic_error);
139 #else
140 EXPECT_NO_THROW(stk::mesh::NgpMesh secondNgpMesh(get_bulk()));
141 #endif
142 }
143
144 TEST_F(UpdateNgpMesh, OnlyOneDeviceMesh_TwoExternal_custom_NgpMemSpace)
145 {
146 setup_test_mesh();
147
148 stk::mesh::NgpMeshT<NgpMeshDefaultMemSpace> firstNgpMesh(get_bulk());
149
150 #ifdef STK_USE_DEVICE_MESH
151 EXPECT_THROW(stk::mesh::NgpMeshT<NgpMeshDefaultMemSpace> secondNgpMesh(get_bulk()), std::logic_error);
152 #else
153 EXPECT_NO_THROW(stk::mesh::NgpMeshT<NgpMeshDefaultMemSpace> secondNgpMesh(get_bulk()));
154 #endif
155 }
156
157 class BucketLayoutModification : public stk::unit_test_util::MeshFixture
158 {
159 public:
160 void setup_mesh_3hex_3block(unsigned bucketCapacity)
161 {
162 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
163 ngp_unit_test_utils::setup_mesh_3hex_3block(get_bulk(), bucketCapacity);
164 }
165
166 void setup_mesh_3hex_2block(unsigned bucketCapacity)
167 {
168 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
169 ngp_unit_test_utils::setup_mesh_3hex_2block(get_bulk(), bucketCapacity);
170 }
171 };
172
173
174 // ------------------------- -------------------------
175 // | | | | | | | |
176 // | 1 | 2 | 3 | ===> | 1 | 2 | 3 |
177 // |block_1|block_2|block_3| |block_1|block_1|block_3|
178 // ------------------------- -------------------------
179 //
180 TEST_F(BucketLayoutModification, DeleteBucketInMiddle)
181 {
182 if (get_parallel_size() != 1) return;
183
184 const unsigned bucketCapacity = 2;
185 setup_mesh_3hex_3block(bucketCapacity);
186
187 stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
188
189 check_bucket_layout(get_bulk(), {{{"block_1"}, {1}}, {{"block_2"}, {2}}, {{"block_3"}, {3}}});
190
191 get_bulk().modification_begin();
192 stk::mesh::PartVector addParts{get_meta().get_part("block_1")};
193 stk::mesh::PartVector removeParts{get_meta().get_part("block_2")};
194 get_bulk().change_entity_parts(get_bulk().get_entity(stk::topology::ELEM_RANK, 2), addParts, removeParts);
195 get_bulk().modification_end();
196
197 ngpMesh.update_mesh();
198
199 check_bucket_layout(get_bulk(), {{{"block_1"}, {1,2}}, {{"block_3"}, {3}}});
200 }
201
202 // ------------------------- -------------------------
203 // | | | | | | | |
204 // | 1 | 2 | 3 | ===> | 1 | 2 | 3 |
205 // |block_1|block_2|block_3| |block_1|block_2|block_1|
206 // ------------------------- -------------------------
207 //
208 TEST_F(BucketLayoutModification, AddBucketInMiddle)
209 {
210 if (get_parallel_size() != 1) return;
211
212 const unsigned bucketCapacity = 1;
213 setup_mesh_3hex_3block(bucketCapacity);
214
215 stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
216
217 check_bucket_layout(get_bulk(), {{{"block_1"}, {1}}, {{"block_2"}, {2}}, {{"block_3"}, {3}}});
218
219 get_bulk().modification_begin();
220 stk::mesh::PartVector addParts{get_meta().get_part("block_1")};
221 stk::mesh::PartVector removeParts{get_meta().get_part("block_3")};
222 get_bulk().change_entity_parts(get_bulk().get_entity(stk::topology::ELEM_RANK, 3), addParts, removeParts);
223 get_bulk().modification_end();
224
225 ngpMesh.update_mesh();
226
227 check_bucket_layout(get_bulk(), {{{"block_1"}, {1}}, {{"block_1"}, {3}}, {{"block_2"}, {2}}});
228 }
229
230 // ------------------------- -------------------------
231 // | | | | | | | |
232 // | 1 | 2 | 3 | ===> | 1 | 2 | 3 |
233 // |block_1|block_1|block_3| |block_1|block_3|block_3|
234 // ------------------------- -------------------------
235 //
236 TEST_F(BucketLayoutModification, ChangeBucketContents)
237 {
238 if (get_parallel_size() != 1) return;
239
240 const unsigned bucketCapacity = 2;
241 setup_mesh_3hex_2block(bucketCapacity);
242
243 stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
244
245 check_bucket_layout(get_bulk(), {{{"block_1"}, {1,2}}, {{"block_3"}, {3}}});
246
247 get_bulk().modification_begin();
248 stk::mesh::PartVector addParts{get_meta().get_part("block_3")};
249 stk::mesh::PartVector removeParts{get_meta().get_part("block_1")};
250 get_bulk().change_entity_parts(get_bulk().get_entity(stk::topology::ELEM_RANK, 2), addParts, removeParts);
251 get_bulk().modification_end();
252
253 ngpMesh.update_mesh();
254
255 check_bucket_layout(get_bulk(), {{{"block_1"}, {1}}, {{"block_3"}, {2,3}}});
256 }
257
258 // ------------------------- -------------------------
259 // | | | | | | | |
260 // | 1 | 2 | 3 | ===> | 1 | 2 | 3 |
261 // |block_1|block_2|block_3| |block_1|block_1|block_3|
262 // ------------------------- -------------------------
263 //
264 TEST_F(BucketLayoutModification, DeleteBucketInMiddle_WithCopy)
265 {
266 if (get_parallel_size() != 1) return;
267
268 const unsigned bucketCapacity = 2;
269 setup_mesh_3hex_3block(bucketCapacity);
270
271 stk::mesh::NgpMesh ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
272
273 check_bucket_layout(get_bulk(), {{{"block_1"}, {1}}, {{"block_2"}, {2}}, {{"block_3"}, {3}}});
274
275 get_bulk().modification_begin();
276 stk::mesh::PartVector addParts{get_meta().get_part("block_1")};
277 stk::mesh::PartVector removeParts{get_meta().get_part("block_2")};
278 get_bulk().change_entity_parts(get_bulk().get_entity(stk::topology::ELEM_RANK, 2), addParts, removeParts);
279 get_bulk().modification_end();
280
281 stk::mesh::get_updated_ngp_mesh(get_bulk()); // Trigger an update
282
283 check_bucket_layout(get_bulk(), {{{"block_1"}, {1,2}}, {{"block_3"}, {3}}});
284 }
285
286 // ------------------------- -------------------------
287 // | | | | | | | |
288 // | 1 | 2 | 3 | ===> | 1 | 2 | 3 |
289 // |block_1|block_2|block_3| |block_1|block_2|block_1|
290 // ------------------------- -------------------------
291 //
292 TEST_F(BucketLayoutModification, AddBucketInMiddle_WithCopy)
293 {
294 if (get_parallel_size() != 1) return;
295
296 const unsigned bucketCapacity = 1;
297 setup_mesh_3hex_3block(bucketCapacity);
298
299 stk::mesh::NgpMesh ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
300
301 check_bucket_layout(get_bulk(), {{{"block_1"}, {1}}, {{"block_2"}, {2}}, {{"block_3"}, {3}}});
302
303 get_bulk().modification_begin();
304 stk::mesh::PartVector addParts{get_meta().get_part("block_1")};
305 stk::mesh::PartVector removeParts{get_meta().get_part("block_3")};
306 get_bulk().change_entity_parts(get_bulk().get_entity(stk::topology::ELEM_RANK, 3), addParts, removeParts);
307 get_bulk().modification_end();
308
309 stk::mesh::get_updated_ngp_mesh(get_bulk()); // Trigger an update
310
311 check_bucket_layout(get_bulk(), {{{"block_1"}, {1}}, {{"block_1"}, {3}}, {{"block_2"}, {2}}});
312 }
313
314 // ------------------------- -------------------------
315 // | | | | | | | |
316 // | 1 | 2 | 3 | ===> | 1 | 2 | 3 |
317 // |block_1|block_1|block_3| |block_1|block_3|block_3|
318 // ------------------------- -------------------------
319 //
320 TEST_F(BucketLayoutModification, ChangeBucketContents_WithCopy)
321 {
322 if (get_parallel_size() != 1) return;
323
324 const unsigned bucketCapacity = 2;
325 setup_mesh_3hex_2block(bucketCapacity);
326
327 stk::mesh::NgpMesh ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
328
329 check_bucket_layout(get_bulk(), {{{"block_1"}, {1,2}}, {{"block_3"}, {3}}});
330
331 get_bulk().modification_begin();
332 stk::mesh::PartVector addParts{get_meta().get_part("block_3")};
333 stk::mesh::PartVector removeParts{get_meta().get_part("block_1")};
334 get_bulk().change_entity_parts(get_bulk().get_entity(stk::topology::ELEM_RANK, 2), addParts, removeParts);
335 get_bulk().modification_end();
336
337 stk::mesh::get_updated_ngp_mesh(get_bulk()); // Trigger an update
338
339 check_bucket_layout(get_bulk(), {{{"block_1"}, {1}}, {{"block_3"}, {2,3}}});
340 }
341
342 } // namespace