Panzer Version of the Day
Loading...
Searching...
No Matches
Panzer_FaceToElement_impl.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Panzer: A partial differential equation assembly
4// engine for strongly coupled complex multiphysics systems
5//
6// Copyright 2011 NTESS and the Panzer contributors.
7// SPDX-License-Identifier: BSD-3-Clause
8// *****************************************************************************
9// @HEADER
10
11/*
12 * FaceToElement.cpp
13 *
14 * Created on: Nov 15, 2016
15 * Author: mbetten
16 */
17
18#ifndef PANZER_FACE_TO_ELEMENT_IMPL_HPP
19#define PANZER_FACE_TO_ELEMENT_IMPL_HPP
20
26
27#include "Teuchos_TimeMonitor.hpp"
28
29#include <vector>
30#include <set>
31#include <string>
32
33namespace panzer
34{
35
36template <typename LocalOrdinal,typename GlobalOrdinal>
41
42template <typename LocalOrdinal,typename GlobalOrdinal>
45 const Teuchos::RCP<const Teuchos::Comm<int>> comm)
46{
47 initialize(conn, comm);
48}
49
50template <typename LocalOrdinal,typename GlobalOrdinal>
51void
54 const Teuchos::RCP<const Teuchos::Comm<int>> comm)
55{
56 // Create a map of elems
57 std::vector<std::string> block_ids;
58 conn.getElementBlockIds(block_ids);
59
60 const int shift = 1;
61
62 int dimension;
63 std::vector<shards::CellTopology> ebt;
65 dimension = ebt[0].getDimension();
66
67 int my_rank = comm->getRank();
68#ifndef NDEBUG
69 int nprocs = comm->getSize();
70#endif
71
72 if ( dimension == 1 ) {
73 panzer::EdgeFieldPattern edge_pattern(ebt[0]);
74 conn.buildConnectivity(edge_pattern);
75 } else if ( dimension == 2 ){
76 panzer::FaceFieldPattern face_pattern(ebt[0]);
77 conn.buildConnectivity(face_pattern);
78 } else {
79 panzer::ElemFieldPattern elem_pattern(ebt[0]);
80 conn.buildConnectivity(elem_pattern);
81 }
82 std::vector<GlobalOrdinal> element_GIDS;
83 for (size_t iblk = 0 ; iblk < block_ids.size(); ++iblk) {
84 const std::vector<LocalOrdinal> &block_elems = conn.getElementBlock(block_ids[iblk]);
85 for (size_t i=0; i<block_elems.size(); ++i) {
86 const auto * connectivity = conn.getConnectivity(block_elems[i]);
87 element_GIDS.push_back(*connectivity);
88 }
89 }
90 Teuchos::RCP<const Map> elem_map =
91 Teuchos::RCP<Map>( new Map(Teuchos::OrdinalTraits<GlobalOrdinal>::invalid(), &element_GIDS[0], element_GIDS.size(), 0, comm ));
92
93 // Now we need to create the face owned and owned/shared maps.
94 Teuchos::RCP<const Map> face_map,owned_face_map;
95 if ( dimension == 1 ) {
96 panzer::NodalFieldPattern edge_pattern(ebt[0]);
97 conn.buildConnectivity(edge_pattern);
98 } else if ( dimension == 2 ){
99 panzer::EdgeFieldPattern face_pattern(ebt[0]);
100 conn.buildConnectivity(face_pattern);
101 } else {
102 panzer::FaceFieldPattern elem_pattern(ebt[0]);
103 conn.buildConnectivity(elem_pattern);
104 }
105 {
106 std::vector<GlobalOrdinal> face_GIDS;
107 std::set<GlobalOrdinal> set_of_face_GIDS;
108 for (size_t iblk = 0 ; iblk < block_ids.size(); ++iblk) {
109 const std::vector<LocalOrdinal> &block_elems = conn.getElementBlock(block_ids[iblk]);
110 for (size_t i=0; i<block_elems.size(); ++i) {
111 int n_conn = conn.getConnectivitySize(block_elems[i]);
112 const panzer::GlobalOrdinal * connectivity = conn.getConnectivity(block_elems[i]);
113 for (int iface=0; iface<n_conn; ++iface)
114 set_of_face_GIDS.insert(connectivity[iface]);
115 }
116 }
117 face_GIDS.insert(face_GIDS.begin(), set_of_face_GIDS.begin(), set_of_face_GIDS.end());
118
119 face_map = Teuchos::RCP<Map>( new Map(Teuchos::OrdinalTraits<GlobalOrdinal>::invalid(), &face_GIDS[0], face_GIDS.size(), 0, comm ));
120 owned_face_map = Tpetra::createOneToOne(face_map);
121 }
122
123 // OK, now we have a map of faces, and owned faces
124 // We are going to do create a multi vector of size 8, block/elem/proc/lidx, block/elem/proc/lidx
125 // (note lidx is the local index associted with a face on each cell)
126 Teuchos::RCP<GOMultiVector> owned_face2elem_mv = Teuchos::RCP<GOMultiVector>(new GOMultiVector(owned_face_map, 8));
127 Teuchos::RCP<GOMultiVector> face2elem_mv = Teuchos::RCP<GOMultiVector>(new GOMultiVector(face_map, 8));
128 // set a flag of -1-shift to identify unmodified data
129 face2elem_mv->putScalar(-1-shift);
130 {
131 auto f2e = face2elem_mv->getLocalViewHost(Tpetra::Access::ReadWrite);
132 auto b1 = Kokkos::subview(f2e,Kokkos::ALL(),0);
133 auto e1 = Kokkos::subview(f2e,Kokkos::ALL(),1);
134 auto p1 = Kokkos::subview(f2e,Kokkos::ALL(),2);
135 auto l1 = Kokkos::subview(f2e,Kokkos::ALL(),3);
136 auto b2 = Kokkos::subview(f2e,Kokkos::ALL(),4);
137 auto e2 = Kokkos::subview(f2e,Kokkos::ALL(),5);
138 auto p2 = Kokkos::subview(f2e,Kokkos::ALL(),6);
139 auto l2 = Kokkos::subview(f2e,Kokkos::ALL(),7);
140 // Now loop once again over the blocks
141 GlobalOrdinal my_elem = 0;
142 for (size_t iblk = 0 ; iblk < block_ids.size(); ++iblk) {
143 const std::vector<LocalOrdinal> &block_elems = conn.getElementBlock(block_ids[iblk]);
144 for (size_t i=0; i<block_elems.size(); ++i) {
145 int n_conn = conn.getConnectivitySize(block_elems[i]);
146 const panzer::GlobalOrdinal * connectivity = conn.getConnectivity(block_elems[i]);
147 for (int iface=0; iface<n_conn; ++iface) {
148 LocalOrdinal f = face_map->getLocalElement(connectivity[iface]);
149
150 // Fun fact: this method breaks if we have cell 0 found in block 0 on process 0 for local index 0
151 // Fix = add 'shift' to everything
152 if (b1[f] < 0 ) {
153 b1[f] = iblk+shift;
154 e1[f] = elem_map->getGlobalElement(my_elem)+shift;
155 p1[f] = my_rank+shift;
156 l1[f] = iface+shift;
157 } else if (b2[f] < 0){
158 b2[f] = iblk+shift;
159 e2[f] = elem_map->getGlobalElement(my_elem)+shift;
160 p2[f] = my_rank+shift;
161 l2[f] = iface+shift;
162 } else
163 assert(false);
164 }
165 ++my_elem;
166 }
167 }
168 }
169
170 // So, now we can export our owned things to our owned one.
171 Import imp(owned_face_map, face_map);
172 Export exp(face_map, owned_face_map);
173 owned_face2elem_mv->doExport(*face2elem_mv, exp, Tpetra::ADD);
174
175 {
176 auto f2e = face2elem_mv->getLocalViewHost(Tpetra::Access::ReadWrite);
177 auto b1 = Kokkos::subview(f2e,Kokkos::ALL(),0);
178 auto e1 = Kokkos::subview(f2e,Kokkos::ALL(),1);
179 auto p1 = Kokkos::subview(f2e,Kokkos::ALL(),2);
180 auto l1 = Kokkos::subview(f2e,Kokkos::ALL(),3);
181 auto b2 = Kokkos::subview(f2e,Kokkos::ALL(),4);
182 auto e2 = Kokkos::subview(f2e,Kokkos::ALL(),5);
183 auto p2 = Kokkos::subview(f2e,Kokkos::ALL(),6);
184 auto l2 = Kokkos::subview(f2e,Kokkos::ALL(),7);
185
186 auto of2e = owned_face2elem_mv->getLocalViewHost(Tpetra::Access::ReadWrite);
187 auto ob1 = Kokkos::subview(of2e,Kokkos::ALL(),0);
188 auto oe1 = Kokkos::subview(of2e,Kokkos::ALL(),1);
189 auto op1 = Kokkos::subview(of2e,Kokkos::ALL(),2);
190 auto ol1 = Kokkos::subview(of2e,Kokkos::ALL(),3);
191 auto ob2 = Kokkos::subview(of2e,Kokkos::ALL(),4);
192 auto oe2 = Kokkos::subview(of2e,Kokkos::ALL(),5);
193 auto op2 = Kokkos::subview(of2e,Kokkos::ALL(),6);
194 auto ol2 = Kokkos::subview(of2e,Kokkos::ALL(),7);
195
196 // Since we added all of the arrays together, they're going to be broken
197 // We need to fix all of the broken faces
198 int num_boundary=0;
199 for (size_t i=0; i<ob1.size();++i){
200
201 // Make sure side 1 of face was set (either by this process or by multiple processes
202 assert(b1[i] >= shift);
203
204 LocalOrdinal shared_local_id = face_map->getLocalElement(owned_face_map->getGlobalElement(i));
205 // handle purely internal faces
206 if (ob1[i] == b1[shared_local_id] && ob2[i] == b2[shared_local_id] &&
207 oe1[i] == e1[shared_local_id] && oe2[i] == e2[shared_local_id]) {
208 if (ob2[i] < 0 )
209 num_boundary++;
210 continue;
211 }
212
213 // Handle shared nodes on a boundary, this shouldn't happen
214 if (ob1[i] < b1[shared_local_id] || oe1[i] < e1[shared_local_id]) {
215 assert(false);
216 }
217
218 if ( ob1[i] > b1[shared_local_id] || oe1[i] > e1[shared_local_id]) {
219 // This case both wrote to a face, we need to detangle
220 assert(ob2[i] < 0 && oe2[i] < 0);
221 ob2[i] = ob1[i] - b1[shared_local_id];
222 oe2[i] = oe1[i] - e1[shared_local_id];
223 op2[i] = op1[i] - p1[shared_local_id];
224 ol2[i] = ol1[i] - l1[shared_local_id];
225
226 ob1[i] = b1[shared_local_id];
227 oe1[i] = e1[shared_local_id];
228 op1[i] = p1[shared_local_id];
229 ol1[i] = l1[shared_local_id];
230
231 assert(op1[i] >=0 && op2[i] >= 0 && op1[i] < nprocs+shift && op2[i] < nprocs+shift);
232 }
233 }
234 }
235 face2elem_mv->doImport(*owned_face2elem_mv, imp, Tpetra::REPLACE);
236
237 // std::cout << "number ext boundaries "<<num_boundary << std::endl;
238
239 // Now cache the data
240 face_map_ = face_map;
241 LocalOrdinal nfaces = face_map_->getLocalNumElements();
242 elems_by_face_ = PHX::View<GlobalOrdinal *[2]>("FaceToElement::elems_by_face_", nfaces);
243 lidx_by_face_ = PHX::View<int *[2]>("FaceToElement::elems_by_face_", nfaces);
244 blocks_by_face_ = PHX::View<int *[2]> ("FaceToElement::blocks_by_face_", nfaces);
245 procs_by_face_ = PHX::View<int *[2]> ("FaceToElement::procs_by_face_", nfaces);
246 auto elems_by_face_h = Kokkos::create_mirror_view(elems_by_face_);
247 auto blocks_by_face_h = Kokkos::create_mirror_view(blocks_by_face_);
248 auto procs_by_face_h = Kokkos::create_mirror_view(procs_by_face_);
249 auto lidx_by_face_h = Kokkos::create_mirror_view(lidx_by_face_);
250
251 auto f2e = face2elem_mv->getLocalViewHost(Tpetra::Access::ReadWrite);
252 auto b1 = Kokkos::subview(f2e,Kokkos::ALL(),0);
253 auto e1 = Kokkos::subview(f2e,Kokkos::ALL(),1);
254 auto p1 = Kokkos::subview(f2e,Kokkos::ALL(),2);
255 auto l1 = Kokkos::subview(f2e,Kokkos::ALL(),3);
256 auto b2 = Kokkos::subview(f2e,Kokkos::ALL(),4);
257 auto e2 = Kokkos::subview(f2e,Kokkos::ALL(),5);
258 auto p2 = Kokkos::subview(f2e,Kokkos::ALL(),6);
259 auto l2 = Kokkos::subview(f2e,Kokkos::ALL(),7);
260
261 for (LocalOrdinal i=0; i< nfaces; ++i) {
262 elems_by_face_h (i,0) = e1[i]-shift;
263 elems_by_face_h (i,1) = e2[i]-shift;
264 blocks_by_face_h(i,0) = b1[i]-shift;
265 blocks_by_face_h(i,1) = b2[i]-shift;
266 procs_by_face_h (i,0) = p1[i]-shift;
267 procs_by_face_h (i,1) = p2[i]-shift;
268 lidx_by_face_h (i,0) = l1[i]-shift;
269 lidx_by_face_h (i,1) = l2[i]-shift;
270
271 if(elems_by_face_h (i,0) < 0){
272 elems_by_face_h (i,0) = -1;
273 }
274 if(elems_by_face_h (i,1) < 0){
275 elems_by_face_h (i,1) = -1;
276 }
277 }
278 Kokkos::deep_copy(elems_by_face_, elems_by_face_h);
279 Kokkos::deep_copy(blocks_by_face_, blocks_by_face_h);
280 Kokkos::deep_copy(procs_by_face_, procs_by_face_h);
281 Kokkos::deep_copy(lidx_by_face_, lidx_by_face_h);
282}
283
284}
285
286#endif /* __FaceToElementent_impl_hpp__ */
Pure virtual base class for supplying mesh connectivity information to the DOF Manager.
virtual const std::vector< LocalOrdinal > & getElementBlock(const std::string &blockID) const =0
virtual void buildConnectivity(const FieldPattern &fp)=0
virtual const GlobalOrdinal * getConnectivity(LocalOrdinal localElmtId) const =0
virtual void getElementBlockIds(std::vector< std::string > &elementBlockIds) const =0
virtual void getElementBlockTopologies(std::vector< shards::CellTopology > &elementBlockTopologies) const =0
virtual LocalOrdinal getConnectivitySize(LocalOrdinal localElmtId) const =0
Tpetra::MultiVector< GlobalOrdinal, LocalOrdinal, GlobalOrdinal, NodeType > GOMultiVector
Tpetra::Export< LocalOrdinal, GlobalOrdinal, NodeType > Export
void initialize(panzer::ConnManager &conn, const Teuchos::RCP< const Teuchos::Comm< int > > comm)
Tpetra::Map< LocalOrdinal, GlobalOrdinal, NodeType > Map
Tpetra::Import< LocalOrdinal, GlobalOrdinal, NodeType > Import