Debian Codegraph @

packages/stk/stk_doc_tests/stk_mesh/customGhosting.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 <sstream> // for ostringstream, etc
38 #include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker
39 #include <stk_mesh/base/BulkData.hpp> // for BulkData
40 #include <stk_mesh/base/MeshBuilder.hpp>
41 #include <stk_mesh/base/MetaData.hpp> // for MetaData
42 #include <stk_topology/topology.hpp> // for topology, etc
43 #include <string> // for string
44 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH
45 #include "stk_mesh/base/Types.hpp" // for BucketVector, PartVector
46 #include "stk_unit_test_utils/ioUtils.hpp"
47
48 namespace stk { namespace mesh { class Part; } }
49
50 namespace
51 {
52
53 int get_other_proc(int myProc)
54 {
55 return (myProc == 1) ? 0 : 1;
56 }
57
58 void verify_that_elem1_and_node1_are_only_valid_on_p0(const stk::mesh::BulkData &bulkData, stk::mesh::Entity elem1, stk::mesh::Entity node1)
59 {
60 if (bulkData.parallel_rank() == 0)
61 {
62 EXPECT_TRUE(bulkData.is_valid(elem1));
63 EXPECT_TRUE(bulkData.is_valid(node1));
64 }
65 else {
66 EXPECT_FALSE(bulkData.is_valid(elem1)); //elem1 only valid on proc 0, initially
67 EXPECT_FALSE(bulkData.is_valid(node1)); //node1 only valid on proc 0, initially
68 }
69 }
70
71 void verify_that_elem1_and_downward_connected_entities_are_ghosted_from_p0_to_p1(const stk::mesh::BulkData &bulkData, stk::mesh::EntityId id)
72 {
73 //now we have ghosted elem1 from proc 0 to proc 1, so it should be valid on both procs
74 //when an entity is ghosted, any downward-connected entities for that entity will also
75 //be ghosted. So node1 should now also be valid on both procs
76 stk::mesh::Entity elem1 = bulkData.get_entity(stk::topology::ELEM_RANK, id);
77 stk::mesh::Entity node1 = bulkData.get_entity(stk::topology::NODE_RANK, id);
78 EXPECT_TRUE(bulkData.is_valid(elem1));
79 EXPECT_TRUE(bulkData.is_valid(node1));
80 }
81
82 void verify_elem1_is_valid_only_on_p0(const stk::mesh::BulkData &bulk, stk::mesh::Entity elem1)
83 {
84 if(bulk.parallel_rank() == 0)
85 EXPECT_TRUE(bulk.is_valid(elem1));
86 else
87 EXPECT_TRUE(!bulk.is_valid(elem1));
88 }
89
90 void verify_elem1_is_valid_on_both_procs(const stk::mesh::BulkData &bulk, stk::mesh::EntityId id)
91 {
92 stk::mesh::Entity elem1 = bulk.get_entity(stk::topology::ELEM_RANK, id);
93 EXPECT_TRUE(bulk.is_valid(elem1));
94 }
95
96 //BEGIN_GHOST_ELEM
97 TEST(StkMeshHowTo, customGhostElem)
98 {
99 MPI_Comm communicator = MPI_COMM_WORLD;
100 if (stk::parallel_machine_size(communicator) == 2)
101 {
102 std::shared_ptr<stk::mesh::BulkData> bulkPtr = stk::mesh::MeshBuilder(communicator).create();
103 stk::mesh::BulkData& bulkData = *bulkPtr;
104 stk::io::fill_mesh("generated:1x1x4", bulkData);
105
106 stk::mesh::EntityId id = 1;
107 stk::mesh::Entity elem1 = bulkData.get_entity(stk::topology::ELEM_RANK, id);
108 stk::mesh::Entity node1 = bulkData.get_entity(stk::topology::NODE_RANK, id);
109 verify_that_elem1_and_node1_are_only_valid_on_p0(bulkData, elem1, node1);
110
111 bulkData.modification_begin();
112 stk::mesh::Ghosting& ghosting = bulkData.create_ghosting("custom ghost for elem 1");
113 std::vector<std::pair<stk::mesh::Entity, int> > elemProcPairs;
114 if (bulkData.parallel_rank() == 0)
115 elemProcPairs.push_back(std::make_pair(elem1, get_other_proc(bulkData.parallel_rank())));
116 bulkData.change_ghosting(ghosting, elemProcPairs);
117 bulkData.modification_end();
118
119 verify_that_elem1_and_downward_connected_entities_are_ghosted_from_p0_to_p1(bulkData, id);
120 }
121 }
122
123 TEST(StkMeshHowTo, addElementToGhostingUsingSpecializedModificationForPerformance)
124 {
125 MPI_Comm communicator = MPI_COMM_WORLD;
126 if(stk::parallel_machine_size(communicator) == 2)
127 {
128 std::shared_ptr<stk::mesh::BulkData> bulkPtr = stk::mesh::MeshBuilder(communicator).create();
129 stk::mesh::BulkData& bulk = *bulkPtr;
130 stk::io::fill_mesh("generated:1x1x4", bulk);
131
132 stk::mesh::EntityId elementId = 1;
133 stk::mesh::Entity elem1 = bulk.get_entity(stk::topology::ELEM_RANK, elementId);
134 verify_elem1_is_valid_only_on_p0(bulk, elem1);
135
136 bulk.modification_begin();
137 stk::mesh::Ghosting& ghosting = bulk.create_ghosting("my custom ghosting");
138 bulk.modification_end();
139
140 stk::mesh::EntityProcVec entityProcPairs;
141 if(bulk.parallel_rank() == 0)
142 entityProcPairs.push_back(stk::mesh::EntityProc(elem1, get_other_proc(bulk.parallel_rank())));
143
144 bulk.batch_add_to_ghosting(ghosting, entityProcPairs);
145
146 verify_elem1_is_valid_on_both_procs(bulk, elementId);
147 }
148 }
149 //END_GHOST_ELEM
150 }