Debian Codegraph @

packages/stk/stk_performance_tests/stk_mesh/GatherGears.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 <stk_util/environment/CPUTime.hpp>
36 #include <gtest/gtest.h>
37 #include <stk_util/environment/perf_util.hpp>
38
39 #include <stk_mesh/base/Relation.hpp>
40 #include <stk_mesh/base/Entity.hpp>
41 #include <stk_mesh/base/BulkData.hpp>
42
43 #include <stk_performance_tests/stk_mesh/calculate_centroid.hpp>
44
45 #include "stk_unit_test_utils/stk_mesh_fixtures/Gear.hpp"
46 #include "stk_unit_test_utils/stk_mesh_fixtures/GearsFixture.hpp"
47 #include <sstream>
48
49 namespace stk {
50 namespace performance_tests {
51
52 namespace {
53
54 //This very simple test will visit all local elements, gather coordinates,
55 //compute element-centroid (simple average of nodal coords) for each, and
56 //store the sum of the centroids in sum_centroid.
57 void do_stk_gather_gears_test(stk::mesh::BulkData& bulk, std::vector<double>& sum_centroid)
58 {
59 using namespace stk::mesh;
60 typedef Field<double> VectorField;
61
62 const MetaData& meta = bulk.mesh_meta_data();
63 const unsigned spatial_dim = meta.spatial_dimension();
64 for(unsigned d=0; d<spatial_dim; ++d) sum_centroid[d] = 0;
65
66 std::vector<double> elem_centroid(spatial_dim, 0);
67
68 const VectorField * coord_field = meta.get_field<double>(stk::topology::NODE_RANK, "coordinates");
69 STK_ThrowAssert(coord_field != nullptr);
70
71 Selector local = meta.locally_owned_part();
72
73 BucketVector const& buckets = bulk.get_buckets(stk::topology::ELEMENT_RANK, local);
74
75 std::vector<double> elem_node_coords;
76
77 for(size_t ib=0; ib<buckets.size(); ++ib) {
78 const Bucket& b = *buckets[ib];
79
80 for(size_t i=0; i<b.size(); ++i) {
81
82 Entity const *node_rels = b.begin_nodes(i);
83 size_t num_nodes = b.num_nodes(i);
84 size_t len = num_nodes*spatial_dim;
85 if (elem_node_coords.size() != len) elem_node_coords.resize(len);
86
87 //here's the gather:
88
89 unsigned offset = 0;
90 for(size_t n=0; n<num_nodes; ++n) {
91 Entity node = node_rels[n];
92 double* node_coords = stk::mesh::field_data(*coord_field, node);
93 elem_node_coords[offset++] = node_coords[0];
94 elem_node_coords[offset++] = node_coords[1];
95 elem_node_coords[offset++] = node_coords[2];
96 }
97
98 stk::performance_tests::calculate_centroid_3d(num_nodes, &elem_node_coords[0], &elem_centroid[0]);
99
100 //add this element-centroid to the sum_centroid vector, and
101 //re-zero the element-centroid vector:
102 sum_centroid[0] += elem_centroid[0]; elem_centroid[0] = 0;
103 sum_centroid[1] += elem_centroid[1]; elem_centroid[1] = 0;
104 sum_centroid[2] += elem_centroid[2]; elem_centroid[2] = 0;
105 }
106 }
107 }
108
109 } // empty namespace
110
111 TEST(gather_gears, gather_gears)
112 {
113 stk::mesh::fixtures::GearsFixture fixture(MPI_COMM_WORLD, 1,
114 stk::mesh::fixtures::GearParams(0.01, 0.4, 1.5, -0.4, 0.4));
115 fixture.meta_data.commit();
116
117 double start_time = stk::cpu_time();
118
119 fixture.generate_mesh();
120
121 double mesh_create_time = stk::cpu_time() - start_time;
122
123 const size_t spatial_dim = fixture.meta_data.spatial_dimension();
124
125 std::vector<double> avg_centroid(spatial_dim, 0);
126
127 start_time = stk::cpu_time();
128
129 const int num_iters = 150;
130 for(int t=0; t<num_iters; ++t) {
131
132 do_stk_gather_gears_test(fixture.bulk_data, avg_centroid);
133
134 double tolerance = 1.e-6;
135
136 for(size_t d=0; d<spatial_dim; ++d) {
137 double expected = 0;
138 EXPECT_LT(std::abs(avg_centroid[d] - expected), tolerance);
139 }
140 }
141
142 double gather_time = stk::cpu_time() - start_time;
143
144 std::cout << "Gear: ";
145 std::cout << "\tNum Nodes: " << fixture.get_gear(0).num_nodes;
146 std::cout << "\tNum Elements: " << fixture.get_gear(0).num_elements << std::endl;
147
148 double total_time = mesh_create_time + gather_time;
149
150 static const int NUM_TIMERS = 3;
151 const double timers[NUM_TIMERS] = {mesh_create_time, gather_time, total_time};
152 const char* timer_names[NUM_TIMERS] = {"Create mesh", "Gather", "Total time"};
153
154 stk::print_timers_and_memory(timer_names, timers, NUM_TIMERS);
155
156 stk::parallel_print_time_without_output_and_hwm(MPI_COMM_WORLD, total_time);
157 }
158
159 } // namespace performance_tests
160 } // namespace stk