Debian Codegraph @

packages/stk/stk_doc_tests/stk_mesh/UnitTestGhostParts.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 <stddef.h> // for size_t
36 #include <stdlib.h> // for exit
37 #include <exception> // for exception
38 #include <iostream> // for ostringstream, etc
39 #include <iterator> // for distance
40 #include <map> // for _Rb_tree_const_iterator, etc
41 #include <stdexcept> // for logic_error, runtime_error
42 #include <stk_mesh/base/BulkData.hpp> // for BulkData, etc
43 #include <stk_mesh/base/FieldParallel.hpp> // for communicate_field_data, etc
44 #include <stk_mesh/base/GetEntities.hpp> // for count_entities, etc
45 #include <stk_util/parallel/Parallel.hpp> // for ParallelMachine, etc
46 #include <gtest/gtest.h>
47 #include <string> // for string, basic_string, etc
48 #include <utility> // for pair
49 #include <vector> // for vector, etc
50 #include "stk_mesh/base/Bucket.hpp" // for Bucket, has_superset
51 #include "stk_mesh/base/Entity.hpp" // for Entity
52 #include "stk_mesh/base/EntityKey.hpp" // for EntityKey
53 #include "stk_mesh/base/Field.hpp" // for Field
54 #include "stk_mesh/base/FieldBase.hpp" // for field_data, etc
55 #include "stk_mesh/base/Ghosting.hpp" // for Ghosting
56 #include "stk_mesh/base/MetaData.hpp" // for MetaData, entity_rank_names, etc
57 #include "stk_mesh/base/Part.hpp" // for Part
58 #include "stk_mesh/base/Relation.hpp"
59 #include "stk_mesh/base/Selector.hpp" // for Selector, operator|
60 #include "stk_mesh/base/Types.hpp" // for EntityProc, EntityVector, etc
61 #include "stk_topology/topology.hpp" // for topology, etc
62 #include "stk_util/util/PairIter.hpp" // for PairIter
63 #include "stk_io/StkMeshIoBroker.hpp"
64
65 //-BEGIN
66 TEST(UnitTestGhostParts, Aura)
67 {
68 stk::ParallelMachine communicator = MPI_COMM_WORLD;
69
70 int numProcs = stk::parallel_machine_size(communicator);
71 if (numProcs != 2) {
72 return;
73 }
74
75 stk::io::StkMeshIoBroker stkMeshIoBroker(communicator);
76 const std::string generatedMeshSpecification = "generated:1x1x3";
77 stkMeshIoBroker.add_mesh_database(generatedMeshSpecification, stk::io::READ_MESH);
78 stkMeshIoBroker.create_input_mesh();
79 stkMeshIoBroker.populate_bulk_data();
80
81 stk::mesh::MetaData &stkMeshMetaData = stkMeshIoBroker.meta_data();
82 stk::mesh::BulkData &stkMeshBulkData = stkMeshIoBroker.bulk_data();
83
84 std::cerr<<"about to get aura_part..."<<std::endl;
85 stk::mesh::Part& aura_part = stkMeshMetaData.aura_part();
86 std::cerr<<"...got aura part with name="<<aura_part.name()<<std::endl;
87 stk::mesh::Selector aura_selector = aura_part;
88
89 stk::mesh::Ghosting& aura_ghosting = stkMeshBulkData.aura_ghosting();
90 EXPECT_EQ(aura_part.mesh_meta_data_ordinal(), stkMeshBulkData.ghosting_part(aura_ghosting).mesh_meta_data_ordinal());
91
92 stk::mesh::Selector not_owned_nor_shared = (!stkMeshMetaData.locally_owned_part()) & (!stkMeshMetaData.globally_shared_part());
93
94 const stk::mesh::BucketVector& not_owned_nor_shared_node_buckets = stkMeshBulkData.get_buckets(stk::topology::NODE_RANK, not_owned_nor_shared);
95 size_t expected_num_not_owned_nor_shared_node_buckets = 1;
96 EXPECT_EQ(expected_num_not_owned_nor_shared_node_buckets, not_owned_nor_shared_node_buckets.size());
97
98 const stk::mesh::BucketVector& aura_node_buckets = stkMeshBulkData.get_buckets(stk::topology::NODE_RANK, aura_selector);
99
100 EXPECT_EQ(not_owned_nor_shared_node_buckets.size(), aura_node_buckets.size());
101
102 const size_t expected_num_ghost_nodes = 4;
103 size_t counted_nodes = 0;
104 size_t counted_aura_nodes = 0;
105 for(size_t i=0; i<not_owned_nor_shared_node_buckets.size(); ++i)
106 {
107 counted_nodes += not_owned_nor_shared_node_buckets[i]->size();
108 counted_aura_nodes += aura_node_buckets[i]->size();
109 }
110 EXPECT_EQ(expected_num_ghost_nodes, counted_nodes);
111 EXPECT_EQ(expected_num_ghost_nodes, counted_aura_nodes);
112 }
113
114 TEST(UnitTestGhostParts, Custom1)
115 {
116 stk::ParallelMachine communicator = MPI_COMM_WORLD;
117
118 int numProcs = stk::parallel_machine_size(communicator);
119 if (numProcs != 2) {
120 return;
121 }
122
123 stk::io::StkMeshIoBroker stkMeshIoBroker(communicator);
124 const std::string generatedMeshSpecification = "generated:1x1x4";
125 stkMeshIoBroker.add_mesh_database(generatedMeshSpecification, stk::io::READ_MESH);
126 stkMeshIoBroker.create_input_mesh();
127 stkMeshIoBroker.populate_bulk_data();
128
129 stk::mesh::BulkData &stkMeshBulkData = stkMeshIoBroker.bulk_data();
130
131 int myProc = stkMeshBulkData.parallel_rank();
132 int otherProc = (myProc == 0) ? 1 : 0;
133
134 stkMeshBulkData.modification_begin();
135
136 stk::mesh::Ghosting& custom_ghosting = stkMeshBulkData.create_ghosting("CustomGhosting1");
137
138 std::vector<stk::mesh::EntityProc> elems_to_ghost;
139
140 const stk::mesh::BucketVector& elem_buckets = stkMeshBulkData.buckets(stk::topology::ELEM_RANK);
141 for(size_t i=0; i<elem_buckets.size(); ++i) {
142 const stk::mesh::Bucket& bucket = *elem_buckets[i];
143 for(size_t j=0; j<bucket.size(); ++j) {
144 if (stkMeshBulkData.parallel_owner_rank(bucket[j]) == myProc) {
145 elems_to_ghost.push_back(std::make_pair(bucket[j], otherProc));
146 }
147 }
148 }
149
150 stkMeshBulkData.change_ghosting(custom_ghosting, elems_to_ghost);
151
152 stkMeshBulkData.modification_end();
153
154 //now each processor should have 2 elements that were received as ghosts of elements from the other proc.
155 const size_t expected_num_elems_for_custom_ghosting = 2;
156
157 stk::mesh::Part& custom_ghost_part = stkMeshBulkData.ghosting_part(custom_ghosting);
158 stk::mesh::Selector custom_ghost_selector = custom_ghost_part;
159
160 const stk::mesh::BucketVector& custom_ghost_elem_buckets = stkMeshBulkData.get_buckets(stk::topology::ELEM_RANK, custom_ghost_selector);
161 size_t counted_elements = 0;
162 for(size_t i=0; i<custom_ghost_elem_buckets.size(); ++i) {
163 counted_elements += custom_ghost_elem_buckets[i]->size();
164 }
165
166 EXPECT_EQ(expected_num_elems_for_custom_ghosting, counted_elements);
167 }
168 //-END
169
170 TEST(UnitTestAura, test_num_communicated_entities)
171 {
172 stk::ParallelMachine communicator = MPI_COMM_WORLD;
173
174 int numProcs = stk::parallel_machine_size(communicator);
175 if (numProcs == 2) {
176 stk::io::StkMeshIoBroker stkMeshIoBroker(communicator);
177 const std::string generatedMeshSpecification = "generated:1x1x4";
178 stkMeshIoBroker.add_mesh_database(generatedMeshSpecification, stk::io::READ_MESH);
179 stkMeshIoBroker.create_input_mesh();
180 stkMeshIoBroker.populate_bulk_data();
181
182 stk::mesh::BulkData &stkMeshBulkData = stkMeshIoBroker.bulk_data();
183
184 size_t num_comm_entities = stkMeshBulkData.get_num_communicated_entities();
185
186 const size_t expected_num_comm_entities = 14;
187 EXPECT_EQ(expected_num_comm_entities, num_comm_entities);
188 }
189 }