Debian Codegraph @

packages/stk/stk_performance_tests/stk_mesh/GearsSkinning.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>
36 #include <random> // for random_device, mt19937, etc.
37 #include <stk_util/parallel/Parallel.hpp>
38 #include <stk_util/parallel/ParallelReduce.hpp>
39 #include <stk_util/environment/WallTime.hpp>
40 #include <stk_util/util/ReportHandler.hpp>
41 #include <stk_util/environment/perf_util.hpp>
42
43 #include <stk_mesh/base/Comm.hpp>
44 #include <stk_mesh/base/Selector.hpp>
45 #include <stk_mesh/base/GetEntities.hpp>
46 #include <stk_mesh/base/Entity.hpp>
47 #include <stk_mesh/base/SkinMesh.hpp>
48
49 #include <stk_io/IossBridge.hpp>
50 #include <stk_io/StkMeshIoBroker.hpp>
51
52 #include <Ionit_Initializer.h>
53 #include <Ioss_SubSystem.h>
54
55 #include "stk_unit_test_utils/stk_mesh_fixtures/Gear.hpp"
56 #include "stk_unit_test_utils/stk_mesh_fixtures/GearsFixture.hpp"
57 #include <algorithm>
58 #include <cmath>
59 #include <iomanip>
60 #include <numeric>
61 #include <sstream>
62 #include <stdexcept>
63
64 namespace {
65
66 /** To create a movie from this performance test, change the
67 output_exodus_file flag to true and run. Then do the following:
68 > conjoin -output gears.e mesh_*
69 > module load viz
70 > paraview
71 [ open conjoin file, filter until happy ]
72 [ change viewport to largest possible (to increase resolution of image)]
73 [ press play till happy ]
74 [ save as animation with 60fps ]
75 **/
76
77 static const stk::mesh::EntityRank NODE_RANK = stk::topology::NODE_RANK;
78
79 typedef stk::mesh::fixtures::GearsFixture::CartesianField CartesianField;
80 typedef stk::mesh::Field<int> IntField;
81
82 //
83 //-----------------------------------------------------------------------------
84 //
85
86 /**
87 * Make a single wedge seperate itself from the cylinder and fly off on its
88 * own.
89 */
90 // if do_separate_wedge == true then wedge must be nonnull pointer
91 void separate_wedge(
92 bool do_separate_wedge,
93 stk::mesh::fixtures::GearsFixture & fixture,
94 stk::mesh::Entity wedge,
95 CartesianField & velocity_field,
96 stk::mesh::Part & skin_part
97 )
98 {
99 // Parallel collective call:
100 fixture.bulk_data.modification_begin();
101
102 // Request new nodes
103 const size_t num_nodes_per_wedge = 6;
104 const size_t spatial_dim = fixture.meta_data.spatial_dimension();
105 stk::mesh::EntityVector new_nodes;
106
107 std::vector<size_t> requests(fixture.meta_data.entity_rank_count(), 0);
108 if (do_separate_wedge) {
109 requests[NODE_RANK] = num_nodes_per_wedge;
110 } else {
111 requests[NODE_RANK] = 0;
112 }
113
114 // Parallel collective call:
115 fixture.bulk_data.generate_new_entities(requests, new_nodes);
116
117 if (do_separate_wedge) {
118
119 // Remove wedge from cylidrical_coord_part
120 stk::mesh::PartVector empty_parts, remove_parts;
121 remove_parts.push_back(& fixture.cylindrical_coord_part);
122 fixture.bulk_data.change_entity_parts(wedge, empty_parts, remove_parts);
123
124 // Replace wedge's nodes with new nodes because any nodes shared with other
125 // entities cannot be taken along with the separated wedge. As a
126 // simplification, we simply leave all the old nodes behind.
127 STK_ThrowAssert(static_cast<size_t>(fixture.bulk_data.num_nodes(wedge)) == num_nodes_per_wedge);
128 stk::mesh::Entity const *rel_nodes = fixture.bulk_data.begin_nodes(wedge);
129
130 for (size_t i = 0; i < num_nodes_per_wedge; ++i) {
131 stk::mesh::Entity old_node = rel_nodes[i];
132 stk::mesh::Entity new_node = new_nodes[i];
133
134 fixture.bulk_data.destroy_relation(wedge, old_node, i);
135 fixture.bulk_data.declare_relation(wedge, new_node, i);
136
137 fixture.bulk_data.copy_entity_fields( old_node, new_node);
138 }
139
140 // Compute the velocities of the nodes by taking the average of the
141 // differences in the displacements in the last time step. Note that we need
142 // all the nodes to have the same velocity; otherwise, the wedge will stretch
143 std::vector<double> avg_velocity_data(spatial_dim, 0);
144 for (size_t i = 0; i < num_nodes_per_wedge; ++i) {
145 stk::mesh::Entity new_node = new_nodes[i];
146 const double * const new_displacement_data =
147 stk::mesh::field_data( fixture.displacement_field->field_of_state(stk::mesh::StateNew), new_node);
148
149 const double * const old_displacement_data =
150 stk::mesh::field_data( fixture.displacement_field->field_of_state(stk::mesh::StateOld), new_node);
151
152 for (size_t k=0 ; k < spatial_dim ; ++k) {
153 avg_velocity_data[k] += new_displacement_data[k] - old_displacement_data[k];
154 }
155
156 }
157
158 for (size_t k=0 ; k < spatial_dim ; ++k) {
159 avg_velocity_data[k] /= 1.0*num_nodes_per_wedge;
160 }
161
162 const double detached_wedge_speedup_multiplier = 1.1;
163 for (size_t i = 0; i < num_nodes_per_wedge; ++i) {
164 stk::mesh::Entity new_node = new_nodes[i];
165 double * const velocity_data =
166 stk::mesh::field_data( velocity_field , new_node );
167
168 for (size_t k=0 ; k < spatial_dim ; ++k) {
169 velocity_data[k] = detached_wedge_speedup_multiplier*avg_velocity_data[k];
170 }
171
172 }
173 }
174
175 // Parallel collective call:
176 fixture.bulk_data.modification_end();
177
178 // Parallel collective call:
179 stk::mesh::PartVector add_parts(1,&skin_part);
180 stk::mesh::skin_mesh(fixture.bulk_data, add_parts);
181
182 // Parallel collective call:
183 fixture.communicate_model_fields();
184 }
185
186 //
187 //-----------------------------------------------------------------------------
188 //
189
190 /**
191 * Stores all the wedges still attached to the cylinder, in random order,
192 * in the wedges argument.
193 */
194 void find_and_shuffle_wedges_to_separate(
195 stk::mesh::fixtures::GearsFixture & fixture,
196 stk::mesh::EntityVector & wedges
197 )
198 {
199 // Get all wedges still attached to the cylinder and shuffle them.
200
201 stk::mesh::Selector select_wedge =
202 fixture.cylindrical_coord_part &
203 fixture.wedge_part &
204 fixture.meta_data.locally_owned_part();
205
206 const stk::mesh::BucketVector wedge_buckets = fixture.bulk_data.buckets(stk::topology::ELEMENT_RANK);
207
208 stk::mesh::get_selected_entities(
209 select_wedge,
210 wedge_buckets,
211 wedges
212 );
213
214 #if __cplusplus >= 201402L
215 std::random_device random_number_generator;
216 std::mt19937 uniform_random_number_generator(random_number_generator());
217 std::shuffle(wedges.begin(), wedges.end(), uniform_random_number_generator);
218 #else
219 std::random_shuffle(wedges.begin(),wedges.end());
220 #endif
221 }
222
223 //
224 //-----------------------------------------------------------------------------
225 //
226
227 /**
228 * Make all the wedges that have already been detached from the cylinder
229 * continue flying through the air.
230 */
231 void move_detached_wedges(
232 stk::mesh::fixtures::GearsFixture & fixture,
233 CartesianField & velocity_field
234 )
235 {
236
237 // Select all detached nodes by creating a selector for things not in the
238 // cylinder part.
239 stk::mesh::Selector select_detached_wedges = (! fixture.cylindrical_coord_part ) &
240 (fixture.meta_data.locally_owned_part() | fixture.meta_data.globally_shared_part());
241
242 stk::mesh::BucketVector const& node_buckets = fixture.bulk_data.get_buckets(NODE_RANK, select_detached_wedges);
243
244 // Iterate over selected node_buckets, then iterate over each node in the
245 // bucket, adjusting the node's displacement according to its velocity.
246 for (stk::mesh::BucketVector::const_iterator b_itr = node_buckets.begin();
247 b_itr != node_buckets.end();
248 ++b_itr)
249 {
250 stk::mesh::Bucket & b = **b_itr;
251
252 const CartesianField::value_type* velocity_data = stk::mesh::field_data(velocity_field, b);
253 CartesianField::value_type* old_displacement_data = stk::mesh::field_data(fixture.displacement_field->field_of_state(stk::mesh::StateOld), b);
254 CartesianField::value_type* new_displacement_data = stk::mesh::field_data(fixture.displacement_field->field_of_state(stk::mesh::StateNew), b);
255 int ndim = fixture.meta_data.spatial_dimension();
256
257 for (size_t i = 0; i < b.size(); ++i) {
258 for (size_t j = 0; j < fixture.meta_data.spatial_dimension(); ++j) {
259 new_displacement_data[j+i*ndim] = old_displacement_data[j+i*ndim] + velocity_data[j+i*ndim];
260 }
261 }
262 }
263 }
264
265 //
266 //-----------------------------------------------------------------------------
267 //
268
269 void populate_processor_id_field_data( stk::mesh::fixtures::GearsFixture & fixture,
270 IntField & processor_field
271 )
272 {
273 const unsigned p_rank = fixture.bulk_data.parallel_rank();
274
275 stk::mesh::Selector locally_owned_selector = fixture.meta_data.locally_owned_part();
276
277 stk::mesh::BucketVector const& element_buckets = fixture.bulk_data.get_buckets(stk::topology::ELEMENT_RANK, locally_owned_selector);
278
279 for (stk::mesh::BucketVector::const_iterator b_itr = element_buckets.begin();
280 b_itr != element_buckets.end();
281 ++b_itr)
282 {
283 stk::mesh::Bucket & b = **b_itr;
284 int* processor_data = stk::mesh::field_data(processor_field, b);
285 for (size_t index = 0; index < b.size(); ++index) {
286 processor_data[index] = p_rank;
287 }
288 }
289 }
290
291 } // namespace
292
293 TEST( gears_skinning, gears_skinning )
294 {
295 // Initialize IO system. Registers all element types and storage
296 // types and the exodusII default database type.
297 Ioss::Init::Initializer init_db;
298
299 const size_t NUM_GEARS = 1;
300 double start_time = stk::wall_time();
301
302 stk::mesh::fixtures::GearsFixture fixture(MPI_COMM_WORLD, NUM_GEARS,
303 stk::mesh::fixtures::GearParams(0.025, 0.6, 1.05, -0.4, 0.4));
304 const unsigned p_rank = fixture.bulk_data.parallel_rank();
305 std::srand(p_rank); // Seed pseudo-random generator based on processor rank.
306
307 stk::mesh::Part & skin_part = fixture.meta_data.declare_part("Skin_part",fixture.meta_data.side_rank());
308
309 const unsigned ONE_STATE = 1;
310 CartesianField & velocity_field = fixture.meta_data.declare_field<double>(stk::topology::NODE_RANK, "velocity",ONE_STATE);
311 stk::topology::rank_t face_rank = static_cast<stk::topology::rank_t>(fixture.meta_data.side_rank());
312 CartesianField & displacement = fixture.meta_data.declare_field<double>(face_rank, "face_displacement",ONE_STATE);
313 IntField & processor_field = fixture.meta_data.declare_field<int>(stk::topology::ELEMENT_RANK, "processor_id",ONE_STATE);
314
315 stk::mesh::put_field_on_mesh(
316 velocity_field,
317 fixture.meta_data.universal_part(),
318 fixture.meta_data.spatial_dimension(),
319 nullptr
320 );
321
322 stk::mesh::put_field_on_mesh(
323 processor_field,
324 fixture.meta_data.universal_part(),
325 nullptr
326 );
327
328 // add io parts
329 stk::io::put_io_part_attribute( fixture.hex_part);
330 stk::io::put_io_part_attribute( fixture.wedge_part);
331 stk::io::put_io_part_attribute( skin_part);
332
333 const stk::mesh::PartVector & parts = fixture.meta_data.get_parts();
334 for ( stk::mesh::PartVector::const_iterator ip = parts.begin(); ip != parts.end(); ++ip ) {
335 stk::mesh::Part & topo_part = **ip;
336 if(topo_part.primary_entity_rank() == stk::topology::ELEMENT_RANK-1 && std::string::npos!=topo_part.name().find("FEM_ROOT_CELL_TOPOLOGY_PART")) {
337 std::string t;
338 if (t.npos != topo_part.name().find("Triangle_3")) t = "skin_wedge6_tri3_1";
339 else if (t.npos != topo_part.name().find("Triangle_6")) t = "skin_wedge15_tri6_2";
340 else if (t.npos != topo_part.name().find("Triangle_4")) t = "skin_wedge6_tri4_3";
341 else if (t.npos != topo_part.name().find("Quadrilateral_4")) t = "skin_hex8_quad4_4";
342 else if (t.npos != topo_part.name().find("Quadrilateral_8")) t = "skin_hex20_quad8_5";
343 else if (t.npos != topo_part.name().find("Quadrilateral_9")) t = "skin_hex27_quad9_6";
344 else {
345 t = topo_part.name()+std::string("_Skin_part");
346 t.erase(t.find("FEM_ROOT_CELL_TOPOLOGY_PART"), sizeof("FEM_ROOT_CELL_TOPOLOGY_PART"));
347 }
348 stk::mesh::Part & topo_skin_part = fixture.meta_data.declare_part(t, fixture.meta_data.side_rank());
349 stk::io::put_io_part_attribute(topo_skin_part);
350 fixture.meta_data.declare_part_subset(topo_part, topo_skin_part);
351 fixture.meta_data.declare_part_subset(skin_part, topo_skin_part);
352 if ( t == "skin_hex8_quad4_4" || t == "skin_hex20_quad8_5") {
353 if (t == "skin_hex8_quad4_4") t = "skin_wedge6_quad4_4";
354 else t = "skin_wedge15_quad4_8";
355 stk::mesh::Part & topo_skin_part2 = fixture.meta_data.declare_part(t, fixture.meta_data.side_rank());
356 stk::io::put_io_part_attribute(topo_skin_part2);
357 fixture.meta_data.declare_part_subset(topo_part, topo_skin_part2);
358 fixture.meta_data.declare_part_subset(skin_part, topo_skin_part2);
359 }
360 }
361 }
362 stk::mesh::Selector surface_select = fixture.meta_data.locally_owned_part();
363 {
364 stk::mesh::put_field_on_mesh( displacement, skin_part, 3, nullptr);
365 const stk::mesh::PartVector &surf_parts = skin_part.subsets();
366 for ( stk::mesh::PartVector::const_iterator ip = surf_parts.begin(); ip != surf_parts.end(); ++ip ) {
367 stk::mesh::Part & surf_part = **ip;
368 if (surf_part.primary_entity_rank() == stk::topology::ELEMENT_RANK-1) {
369 surface_select |= surf_part;
370 stk::mesh::put_field_on_mesh( displacement, surf_part, 3, nullptr);
371 }
372 }
373 }
374
375 fixture.meta_data.commit();
376
377 fixture.generate_mesh();
378
379 populate_processor_id_field_data(fixture,processor_field);
380
381 stk::mesh::PartVector add_parts(1,&skin_part);
382 stk::mesh::skin_mesh(fixture.bulk_data, add_parts);
383
384 stk::mesh::EntityVector wedges_to_separate;
385
386 find_and_shuffle_wedges_to_separate( fixture, wedges_to_separate );
387
388 const size_t NUM_TIME_STEPS = 150;
389 const size_t separation_interval = 30;
390
391 const double rotation = TWO_PI*4.0/NUM_TIME_STEPS;
392 const double x = 0;
393 const double y = 0;
394 const double z = 0;
395 const stk::mesh::fixtures::GearMovement gear_movement_data(rotation,x,y,z);
396
397 stk::mesh::fixtures::Gear & gear = fixture.get_gear(0);
398
399 // Iterate over the time steps, updating the locations of the entities and
400 // writing the current mesh state to output files.
401 const bool output_exodus_file = true;
402
403 stk::io::StkMeshIoBroker stkMeshIoBroker;
404 stkMeshIoBroker.set_bulk_data(fixture.bulk_data);
405
406 size_t vol_mesh_index = std::numeric_limits<size_t>::max();
407 size_t surf_mesh_index = std::numeric_limits<size_t>::max();
408
409 for (size_t time_step = 0; time_step < NUM_TIME_STEPS; ++time_step) {
410
411 // Determine if it's time to separate a wedge
412 const bool do_separate_wedge = !wedges_to_separate.empty() && (time_step%separation_interval == 0);
413
414 if (time_step > 0) {
415
416 fixture.bulk_data.update_field_data_states();
417
418 // Move the gear; this will only rotate the gear since x,y,z are all 0
419 gear.move(gear_movement_data);
420 stk::mesh::Entity wedge = stk::mesh::Entity();
421
422 // Separate the wedge if it's time
423 if (do_separate_wedge) {
424 wedge = wedges_to_separate.back();
425 wedges_to_separate.pop_back();
426 }
427
428 separate_wedge(
429 do_separate_wedge,
430 fixture,
431 wedge,
432 velocity_field,
433 skin_part);
434
435 move_detached_wedges(
436 fixture,
437 velocity_field
438 );
439
440 // Parallel collective call:
441 fixture.communicate_model_fields();
442 }
443
444 // update a face field
445 stk::mesh::BucketVector const& face_buckets = fixture.bulk_data.get_buckets(stk::topology::FACE_RANK, surface_select);
446 for (stk::mesh::BucketVector::const_iterator b_itr = face_buckets.begin(); b_itr != face_buckets.end(); ++b_itr) {
447 stk::mesh::Bucket & b = **b_itr;
448 for (size_t i = 0; i < b.size(); ++i) {
449 stk::mesh::Entity face = b[i];
450 double *elem_node_disp = stk::mesh::field_data(displacement, face);
451 if (elem_node_disp) {
452 const size_t num_nodes = b.num_nodes(i);
453
454 stk::mesh::Entity const *node_rels_itr = b.begin_nodes(i);
455 stk::mesh::Entity const *node_rels_end = b.end_nodes(i);
456 for ( ; node_rels_itr != node_rels_end; ++node_rels_itr)
457 {
458 const stk::mesh::Entity node = *node_rels_itr;
459 double* node_disp = stk::mesh::field_data(*fixture.displacement_field, node);
460 elem_node_disp[0] = node_disp[0];
461 elem_node_disp[1] = node_disp[1];
462 elem_node_disp[2] = node_disp[2];
463 }
464 elem_node_disp[0] /= num_nodes;
465 elem_node_disp[1] /= num_nodes;
466 elem_node_disp[2] /= num_nodes;
467 }
468 }
469 }
470
471 //This section writes mesh data out to an exodus file:
472 if (output_exodus_file) {
473 // Write the output file at the first time step and every time the mesh is modified.
474 const bool create_output_file = do_separate_wedge || !time_step;
475 // Write the model to the mesh file (topology, coordinates, attributes, etc)
476 if (create_output_file) {
477 std::ostringstream volume_out_filename;
478 volume_out_filename << "volume_mesh_" << std::setw(7) << std::setfill('0') << time_step << ".e";
479
480 vol_mesh_index = stkMeshIoBroker.create_output_mesh(volume_out_filename.str(), stk::io::WRITE_RESULTS);
481
482 std::ostringstream surface_out_filename;
483 surface_out_filename << "surface_mesh_" << std::setw(7) << std::setfill('0') << time_step << ".e";
484 surf_mesh_index = stkMeshIoBroker.create_output_mesh(volume_out_filename.str(), stk::io::WRITE_RESULTS);
485
486 stk::io::set_field_role(fixture.displacement_field->field_of_state(stk::mesh::StateNew), Ioss::Field::TRANSIENT);
487 stk::io::set_field_role(displacement, Ioss::Field::TRANSIENT);
488 stk::io::set_field_role(processor_field, Ioss::Field::TRANSIENT);
489
490 stkMeshIoBroker.add_field(vol_mesh_index, fixture.displacement_field->field_of_state(stk::mesh::StateNew));
491 stkMeshIoBroker.add_field(vol_mesh_index, displacement);
492 stkMeshIoBroker.add_field(vol_mesh_index, processor_field);
493 }
494
495 stkMeshIoBroker.process_output_request(vol_mesh_index, time_step/60.0);
496 stkMeshIoBroker.process_output_request(surf_mesh_index, time_step/60.0);
497 }
498 }
499
500 double total_time = stk::wall_time() - start_time;
501 const char* timer_label = "Total Time";
502 if (p_rank == 0) {
503 stk::print_timers_and_memory(&timer_label, &total_time, 1);
504 }
505 stk::parallel_print_time_without_output_and_hwm(MPI_COMM_WORLD, total_time);
506 }