Debian Codegraph @

packages/stk/stk_doc_tests/stk_mesh/communicateFieldData.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 <stk_unit_test_utils/MeshFixture.hpp>
38 #include <stk_mesh/base/BulkData.hpp> // for BulkData
39 #include <stk_mesh/base/FieldParallel.hpp> // for communicate_field_data
40 #include <stk_mesh/base/ForEachEntity.hpp>
41 #include <stk_util/parallel/Parallel.hpp> // for ParallelMachine
42 #include <string> // for string
43 #include <vector> // for vector
44 #include "stk_io/FillMesh.hpp"
45 #include "stk_mesh/base/Bucket.hpp" // for Bucket
46 #include "stk_mesh/base/Entity.hpp" // for Entity
47 #include "stk_mesh/base/Field.hpp" // for Field
48 #include "stk_mesh/base/MetaData.hpp" // for MetaData, etc
49 #include "stk_mesh/base/Part.hpp" // for Part
50 #include "stk_mesh/base/Selector.hpp" // for operator!, Selector
51 #include "stk_mesh/base/Types.hpp" // for BucketVector
52 #include "stk_topology/topology.hpp" // for topology, etc
53
54 class ParallelHowTo : public stk::unit_test_util::MeshFixture {};
55
56 //BEGINCommuniateFieldData
57 TEST_F(ParallelHowTo, communicateFieldDataForSharedAndAura)
58 {
59 setup_empty_mesh(stk::mesh::BulkData::AUTO_AURA);
60 auto& field = get_meta().declare_field<double>(stk::topology::NODE_RANK, "temperature");
61
62 double initialValue = 25.0;
63 stk::mesh::put_field_on_entire_mesh_with_initial_value(field, &initialValue);
64
65 stk::io::fill_mesh("generated:8x8x8", get_bulk());
66
67 stk::mesh::Selector notOwned = !get_meta().locally_owned_part();
68 stk::mesh::for_each_entity_run(get_bulk(), stk::topology::NODE_RANK, notOwned,
69 [&](const stk::mesh::BulkData& /*bulk*/, stk::mesh::Entity node) {
70 *stk::mesh::field_data(field, node) = -1.2345;
71 });
72
73 stk::mesh::communicate_field_data(get_bulk(), {&field});
74
75 stk::mesh::for_each_entity_run(get_bulk(), stk::topology::NODE_RANK, notOwned,
76 [&](const stk::mesh::BulkData& /*bulk*/, stk::mesh::Entity node) {
77 EXPECT_EQ(initialValue, *stk::mesh::field_data(field, node));
78 });
79 }
80 //ENDCommuniateFieldData
81
82 //BEGINSum
83 void expect_nodal_field_has_value(const stk::mesh::Selector& selector,
84 const stk::mesh::Field<double> &field,
85 const double value)
86 {
87 stk::mesh::for_each_entity_run(field.get_mesh(), stk::topology::NODE_RANK, selector,
88 [&](const stk::mesh::BulkData& /*bulk*/, stk::mesh::Entity node) {
89 EXPECT_EQ(value, *stk::mesh::field_data(field, node));
90 });
91 }
92
93 TEST_F(ParallelHowTo, computeParallelSum)
94 {
95 setup_empty_mesh(stk::mesh::BulkData::AUTO_AURA);
96 auto& field = get_meta().declare_field<double>(stk::topology::NODE_RANK, "temperature");
97
98 double initialValue = 25.0;
99 stk::mesh::put_field_on_entire_mesh_with_initial_value(field, &initialValue);
100
101 stk::io::fill_mesh("generated:8x8x8", get_bulk());
102
103 expect_nodal_field_has_value(get_meta().globally_shared_part(), field, initialValue);
104 expect_nodal_field_has_value(!get_meta().globally_shared_part(), field, initialValue);
105
106 stk::mesh::parallel_sum(get_bulk(), {&field});
107
108 expect_nodal_field_has_value(get_meta().globally_shared_part(), field, 2*initialValue);
109 expect_nodal_field_has_value(!get_meta().globally_shared_part(), field, initialValue);
110 }
111 //ENDSum