https://mooseframework.inl.gov
Loading...
Searching...
No Matches
AugmentSparsityOnInterface.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10// App includes
12#include "Executioner.h"
13#include "FEProblemBase.h"
14#include "MooseApp.h"
15
16// libMesh includes
17#include "libmesh/elem.h"
18#include "libmesh/mesh_base.h"
19#include "libmesh/boundary_info.h"
20
22
24AugmentSparsityOnInterface::validParams()
25{
27 params.addRequiredParam<BoundaryName>("primary_boundary",
28 "The name of the primary boundary sideset.");
29 params.addRequiredParam<BoundaryName>("secondary_boundary",
30 "The name of the secondary boundary sideset.");
31 params.addRequiredParam<SubdomainName>("primary_subdomain",
32 "The name of the primary lower dimensional subdomain.");
33 params.addRequiredParam<SubdomainName>("secondary_subdomain",
34 "The name of the secondary lower dimensional subdomain.");
35 params.addParam<bool>(
36 "ghost_point_neighbors",
37 false,
38 "Whether we should ghost point neighbors of secondary lower-dimensional elements and "
39 "also their mortar interface couples for applications such as mortar nodal auxiliary "
40 "kernels.");
41 params.addParam<bool>(
42 "ghost_higher_d_neighbors",
43 false,
44 "Whether we should ghost higher-dimensional neighbors. This is necessary when we are doing "
45 "second order mortar with finite volume primal variables, because in order for the method to "
46 "be second order we must use cell gradients, which couples in the neighbor cells.");
47
48 // We want to wait until our mortar mesh has been built before trying to delete remote elements.
49 // And our mortar mesh cannot be built until the entire mesh has been generated. By setting this
50 // parameter to false we will make sure that any prepare_for_use calls during the mesh generation
51 // phase will not delete remote elements *and* we will set a flag on the moose mesh saying that we
52 // need to delete remote elements after the addition of late geometric ghosting functors
53 // (including this ghosting functor)
54 params.set<bool>("attach_geometric_early") = false;
55 return params;
56}
57
59 : RelationshipManager(params),
60 _primary_boundary_name(getParam<BoundaryName>("primary_boundary")),
61 _secondary_boundary_name(getParam<BoundaryName>("secondary_boundary")),
62 _primary_subdomain_name(getParam<SubdomainName>("primary_subdomain")),
63 _secondary_subdomain_name(getParam<SubdomainName>("secondary_subdomain")),
64 _is_coupling_functor(isType(Moose::RelationshipManagerType::COUPLING)),
65 _ghost_point_neighbors(getParam<bool>("ghost_point_neighbors")),
66 _ghost_higher_d_neighbors(getParam<bool>("ghost_higher_d_neighbors"))
67{
68}
69
71 : RelationshipManager(other),
72 _primary_boundary_name(other._primary_boundary_name),
73 _secondary_boundary_name(other._secondary_boundary_name),
74 _primary_subdomain_name(other._primary_subdomain_name),
75 _secondary_subdomain_name(other._secondary_subdomain_name),
76 _is_coupling_functor(other._is_coupling_functor),
77 _ghost_point_neighbors(other._ghost_point_neighbors),
78 _ghost_higher_d_neighbors(other._ghost_higher_d_neighbors)
79{
80}
81
82void
83AugmentSparsityOnInterface::internalInitWithMesh(const MeshBase &)
84{
85}
86
87std::string
88AugmentSparsityOnInterface::getInfo() const
89{
90 std::ostringstream oss;
91 oss << "AugmentSparsityOnInterface";
92 return oss.str();
93}
94
95void
96AugmentSparsityOnInterface::ghostMortarInterfaceCouplings(
97 const processor_id_type p,
98 const Elem * const elem,
99 map_type & coupled_elements,
100 const AutomaticMortarGeneration & amg) const
101{
102 // Look up elem in the mortar_interface_coupling data structure.
103 const auto & mic = amg.mortarInterfaceCoupling();
104 auto find_it = mic.find(elem->id());
105 if (find_it == mic.end())
106 return;
107
108 const auto & coupled_set = find_it->second;
109
110 for (const auto coupled_elem_id : coupled_set)
111 {
112 const Elem * coupled_elem = _mesh->elem_ptr(coupled_elem_id);
113 mooseAssert(coupled_elem,
114 "The coupled element with id " << coupled_elem_id << " doesn't exist!");
115
116 if (coupled_elem->processor_id() != p)
117 coupled_elements.emplace(coupled_elem, _null_mat);
118 }
119}
120
121void
122AugmentSparsityOnInterface::ghostLowerDSecondaryElemPointNeighbors(
123 const processor_id_type p,
124 const Elem * const query_elem,
125 map_type & coupled_elements,
126 const BoundaryID secondary_boundary_id,
127 const SubdomainID secondary_subdomain_id,
128 const AutomaticMortarGeneration & amg) const
129{
130 // I hypothesize that node processor ids are tied to higher dimensional element processor
131 // ids over lower dimensional element processor ids based on debugging experience.
132 // Consequently we need to be checking for higher dimensional elements and whether they are
133 // along our secondary boundary. From there we will query the AMG object's
134 // mortar-interface-coupling container to get the secondary lower-dimensional element, and
135 // then we will ghost it's point neighbors and their mortar interface couples
136
137 // It's possible that one higher-dimensional element could have multiple lower-dimensional
138 // elements from multiple sides. Morever, even if there is only one lower-d element per
139 // higher-d element, the unordered_multimap that holds the coupling information can have
140 // duplicate key-value pairs if there are multiple mortar segments per secondary face. So to
141 // prevent attempting to insert into the coupled elements map multiple times with the same
142 // element, we'll keep track of the elements we've handled. We're going to use a tree-based
143 // set here since the number of lower-d elements handled should never exceed the number of
144 // element sides (which is small)
145 std::set<dof_id_type> secondary_lower_elems_handled;
146 const BoundaryInfo & binfo = _mesh->get_boundary_info();
147 for (auto side : query_elem->side_index_range())
148 {
149 if (!binfo.has_boundary_id(query_elem, side, secondary_boundary_id))
150 // We're not a higher-dimensional element along the secondary face, or at least this
151 // side isn't
152 continue;
153
154 const auto & mic = amg.mortarInterfaceCoupling();
155 auto find_it = mic.find(query_elem->id());
156 if (find_it == mic.end())
157 continue;
158
159 const auto & coupled_set = find_it->second;
160 for (const auto coupled_elem_id : coupled_set)
161 {
162 auto * const coupled_elem = _mesh->elem_ptr(coupled_elem_id);
163
164 if (coupled_elem->subdomain_id() != secondary_subdomain_id)
165 {
166 // We support higher-d-secondary to higher-d-primary coupling now, e.g.
167 // if we get here, coupled_elem is not actually a secondary lower elem; it's a
168 // primary higher-d elem
169 mooseAssert(coupled_elem->dim() == query_elem->dim(), "These should be matching dim");
170 continue;
171 }
172
173 auto insert_pr = secondary_lower_elems_handled.insert(coupled_elem_id);
174
175 // If insertion didn't happen, then we've already handled this element
176 if (!insert_pr.second)
177 continue;
178
179 // We've already ghosted the secondary lower-d element itself if it needed to be
180 // outside of the _ghost_point_neighbors logic. But now we must make sure to ghost the
181 // point neighbors of the secondary lower-d element and their mortar interface
182 // couplings
183 std::set<const Elem *> secondary_lower_elem_point_neighbors;
184 coupled_elem->find_point_neighbors(secondary_lower_elem_point_neighbors);
185
186 for (const Elem * const neigh : secondary_lower_elem_point_neighbors)
187 {
188 if (neigh->processor_id() != p)
189 coupled_elements.emplace(neigh, _null_mat);
190
191 ghostMortarInterfaceCouplings(p, neigh, coupled_elements, amg);
192 }
193 } // end iteration over mortar interface couplings
194
195 // We actually should have added all the lower-dimensional elements associated with the
196 // higher-dimensional element, so we can stop iterating over sides
197 return;
198
199 } // end for side_index_range
200}
201
202void
203AugmentSparsityOnInterface::ghostHigherDNeighbors(const processor_id_type p,
204 const Elem * const query_elem,
205 map_type & coupled_elements,
206 const BoundaryID secondary_boundary_id,
207 const SubdomainID secondary_subdomain_id,
208 const AutomaticMortarGeneration & amg) const
209{
210 // The coupling is this for second order FV: secondary lower dimensional elem dofs will depend on
211 // higher-d secondary elem neighbor primal dofs and higher-d primary elem neighbor primal dofs.
212 // Higher dimensional primal dofs only depend on the secondary lower dimensional LM dofs for the
213 // current FV gap heat transfer use cases
214
215 if (query_elem->subdomain_id() != secondary_subdomain_id)
216 return;
217
218 const BoundaryInfo & binfo = _mesh->get_boundary_info();
219 const auto which_side = query_elem->interior_parent()->which_side_am_i(query_elem);
220 if (!binfo.has_boundary_id(query_elem->interior_parent(), which_side, secondary_boundary_id))
221 return;
222
223 const auto & mic = amg.mortarInterfaceCoupling();
224 auto find_it = mic.find(query_elem->id());
225 if (find_it == mic.end())
226 // Perhaps no projection onto primary
227 return;
228
229 const auto & lower_d_coupled_set = find_it->second;
230 for (const auto coupled_elem_id : lower_d_coupled_set)
231 {
232 auto * const coupled_elem = _mesh->elem_ptr(coupled_elem_id);
233 if (coupled_elem->dim() == query_elem->dim())
234 // lower-d-elem
235 continue;
236
237 std::vector<const Elem *> active_neighbors;
238
239 for (auto s : coupled_elem->side_index_range())
240 {
241 const Elem * const neigh = coupled_elem->neighbor_ptr(s);
242
243 if (!neigh || neigh == remote_elem)
244 continue;
245
246 // With any kind of neighbor, we need to couple to all the
247 // active descendants on our side.
248 neigh->active_family_tree_by_neighbor(active_neighbors, coupled_elem);
249
250 for (const auto & neighbor : active_neighbors)
251 if (neighbor->processor_id() != p)
252 {
253 mooseAssert(
254 neighbor->subdomain_id() != secondary_subdomain_id,
255 "Ensure that we aren't missing potential erasures from the secondary-to-msms map");
256 coupled_elements.emplace(neighbor, _null_mat);
257 }
258 }
259 }
260}
261
262void
263AugmentSparsityOnInterface::operator()(const MeshBase::const_element_iterator & range_begin,
264 const MeshBase::const_element_iterator & range_end,
265 const processor_id_type p,
266 map_type & coupled_elements)
267{
268 // We ask the user to pass boundary names instead of ids to our constraint object. However, We
269 // are unable to get the boundary ids from boundary names until we've attached the MeshBase object
270 // to the MooseMesh
271 const bool generating_mesh = !_moose_mesh->getMeshPtr();
272 const auto primary_boundary_id = generating_mesh
274 : _moose_mesh->getBoundaryID(_primary_boundary_name);
275 const auto secondary_boundary_id = generating_mesh
277 : _moose_mesh->getBoundaryID(_secondary_boundary_name);
278 const auto primary_subdomain_id = generating_mesh
280 : _moose_mesh->getSubdomainID(_primary_subdomain_name);
281 const auto secondary_subdomain_id = generating_mesh
283 : _moose_mesh->getSubdomainID(_secondary_subdomain_name);
284
285 const AutomaticMortarGeneration * const amg =
287 std::make_pair(primary_boundary_id, secondary_boundary_id),
288 std::make_pair(primary_subdomain_id, secondary_subdomain_id),
290 : nullptr;
291
292 // If we're on a dynamic mesh or we have not yet constructed the mortar mesh, we need to ghost the
293 // entire interface because we don't know a priori what elements will project onto what. We *do
294 // not* add the whole interface if we are a coupling functor because it is very expensive. This is
295 // because when building the sparsity pattern, we call through to the ghosting functors with one
296 // element at a time (and then below we do a loop over all the mesh's active elements). It's
297 // perhaps faster in this case to deal with mallocs coming out of MatSetValues, especially if the
298 // mesh displacements are relatively small
299 if ((!amg || _use_displaced_mesh) && !_is_coupling_functor)
300 {
301 for (const Elem * const elem : _mesh->active_element_ptr_range())
302 {
303 if (generating_mesh)
304 {
305 // We are still generating the mesh, so it's possible we don't even have the right boundary
306 // ids created yet! So we actually ghost all boundary elements and all lower dimensional
307 // elements who have parents on a boundary
308 if (elem->on_boundary())
309 coupled_elements.insert(std::make_pair(elem, _null_mat));
310 else if (const Elem * const ip = elem->interior_parent())
311 {
312 if (ip->on_boundary())
313 coupled_elements.insert(std::make_pair(elem, _null_mat));
314 }
315 }
316 else
317 {
318 // We've finished generating our mesh so we can be selective and only ghost elements lying
319 // in our lower-dimensional subdomains and their interior parents
320
321 mooseAssert(primary_boundary_id != Moose::INVALID_BOUNDARY_ID,
322 "Primary boundary id should exist by now.");
323 mooseAssert(secondary_boundary_id != Moose::INVALID_BOUNDARY_ID,
324 "Secondary boundary id should exist by now.");
325 mooseAssert(primary_subdomain_id != Moose::INVALID_BLOCK_ID,
326 "Primary subdomain id should exist by now.");
327 mooseAssert(secondary_subdomain_id != Moose::INVALID_BLOCK_ID,
328 "Secondary subdomain id should exist by now.");
329
330 // Higher-dimensional boundary elements
331 const BoundaryInfo & binfo = _mesh->get_boundary_info();
332
333 for (auto side : elem->side_index_range())
334 if ((elem->processor_id() != p) &&
335 (binfo.has_boundary_id(elem, side, primary_boundary_id) ||
336 binfo.has_boundary_id(elem, side, secondary_boundary_id)))
337 coupled_elements.insert(std::make_pair(elem, _null_mat));
338
339 // Lower dimensional subdomain elements
340 if ((elem->processor_id() != p) && (elem->subdomain_id() == primary_subdomain_id ||
341 elem->subdomain_id() == secondary_subdomain_id))
342 {
343 coupled_elements.insert(std::make_pair(elem, _null_mat));
344
345#ifndef NDEBUG
346 // let's do some safety checks
347 const Elem * const ip = elem->interior_parent();
348 mooseAssert(ip,
349 "We should have set interior parents for all of our lower-dimensional mortar "
350 "subdomains");
351 auto side = ip->which_side_am_i(elem);
352 auto bnd_id = elem->subdomain_id() == primary_subdomain_id ? primary_boundary_id
353 : secondary_boundary_id;
354 mooseAssert(_mesh->get_boundary_info().has_boundary_id(ip, side, bnd_id),
355 "The interior parent for the lower-dimensional element does not lie on the "
356 "boundary");
357#endif
358 }
359 }
360 }
361 }
362 // For a static mesh (or for determining a sparsity pattern approximation on a displaced mesh) we
363 // can just ghost the coupled elements determined during mortar mesh generation
364 else if (amg)
365 {
366 for (const Elem * const elem : as_range(range_begin, range_end))
367 {
368 ghostMortarInterfaceCouplings(p, elem, coupled_elements, *amg);
369
370 if (_ghost_point_neighbors)
371 ghostLowerDSecondaryElemPointNeighbors(
372 p, elem, coupled_elements, secondary_boundary_id, secondary_subdomain_id, *amg);
373 if (_ghost_higher_d_neighbors)
374 ghostHigherDNeighbors(
375 p, elem, coupled_elements, secondary_boundary_id, secondary_subdomain_id, *amg);
376 } // end for loop over input range
377 } // end if amg
378}
379
380bool
381AugmentSparsityOnInterface::operator>=(const RelationshipManager & other) const
382{
383 if (auto asoi = dynamic_cast<const AugmentSparsityOnInterface *>(&other))
384 {
385 if (_primary_boundary_name == asoi->_primary_boundary_name &&
386 _secondary_boundary_name == asoi->_secondary_boundary_name &&
387 _primary_subdomain_name == asoi->_primary_subdomain_name &&
388 _secondary_subdomain_name == asoi->_secondary_subdomain_name &&
389 _ghost_point_neighbors >= asoi->_ghost_point_neighbors &&
390 _ghost_higher_d_neighbors >= asoi->_ghost_higher_d_neighbors && baseGreaterEqual(*asoi))
391 return true;
392 }
393 return false;
394}
395
396std::unique_ptr<GhostingFunctor>
398{
399 return _app.getFactory().copyConstruct(*this);
400}
registerMooseObject("MooseApp", AugmentSparsityOnInterface)
boundary_id_type BoundaryID
subdomain_id_type SubdomainID
if(!dmm->_nl) SETERRQ(PETSC_COMM_WORLD
AugmentSparsityOnInterface(MeshBase &mesh, boundary_id_type crack_boundary_lower, boundary_id_type crack_boundary_upper)
virtual std::unique_ptr< GhostingFunctor > clone() const override
virtual void operator()(const MeshBase::const_element_iterator &range_begin, const MeshBase::const_element_iterator &range_end, processor_id_type p, map_type &coupled_elements) override
This class is a container/interface for the objects involved in automatic generation of mortar spaces...
const std::unordered_map< dof_id_type, std::unordered_set< dof_id_type > > & mortarInterfaceCoupling() const
FEProblemBase & feProblem()
Return a reference to this Executioner's FEProblemBase instance.
const AutomaticMortarGeneration & getMortarInterface(const std::pair< BoundaryID, BoundaryID > &primary_secondary_boundary_pair, const std::pair< SubdomainID, SubdomainID > &primary_secondary_subdomain_pair, bool on_displaced) const
Return the undisplaced or displaced mortar generation object associated with the provided boundaries ...
std::unique_ptr< T > copyConstruct(const T &object)
Copy constructs the object object.
Definition Factory.h:358
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object.
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
Executioner * getExecutioner() const
Retrieve the Executioner for this App.
Definition MooseApp.C:2021
Factory & getFactory()
Retrieve a writable reference to the Factory associated with this App.
Definition MooseApp.h:407
const MeshBase * getMeshPtr() const
Definition MooseMesh.C:3551
SubdomainID getSubdomainID(const SubdomainName &subdomain_name) const
Get the associated subdomain ID for the subdomain name.
Definition MooseMesh.C:1723
BoundaryID getBoundaryID(const BoundaryName &boundary_name) const
Get the associated BoundaryID for the boundary name.
Definition MooseMesh.C:1684
MooseApp & _app
The MOOSE application this is associated with.
Definition MooseBase.h:375
RelationshipManagers are used for describing what kinds of non-local resources are needed for an obje...
const bool _use_displaced_mesh
Which system this should go to (undisplaced or displaced)
virtual bool baseGreaterEqual(const RelationshipManager &rhs) const
Whether the base class provides more or the same amount and type of ghosting as the rhs.
static InputParameters validParams()
MooseMesh * _moose_mesh
Pointer to the MooseMesh object.
processor_id_type processor_id() const
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
RelationshipManagerType
Main types of Relationship Managers.
const BoundaryID INVALID_BOUNDARY_ID
Definition MooseTypes.C:22
const SubdomainID INVALID_BLOCK_ID
Definition MooseTypes.C:20
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)