Line data Source code
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 : #include "AutomaticMortarGeneration.h"
11 : #include "MortarSegmentInfo.h"
12 : #include "NanoflannMeshAdaptor.h"
13 : #include "MooseError.h"
14 : #include "MooseTypes.h"
15 : #include "MooseLagrangeHelpers.h"
16 : #include "MortarSegmentHelper.h"
17 : #include "FormattedTable.h"
18 : #include "FEProblemBase.h"
19 : #include "DisplacedProblem.h"
20 : #include "Output.h"
21 :
22 : #include "libmesh/mesh_tools.h"
23 : #include "libmesh/explicit_system.h"
24 : #include "libmesh/numeric_vector.h"
25 : #include "libmesh/elem.h"
26 : #include "libmesh/node.h"
27 : #include "libmesh/dof_map.h"
28 : #include "libmesh/edge_edge2.h"
29 : #include "libmesh/edge_edge3.h"
30 : #include "libmesh/face_tri3.h"
31 : #include "libmesh/face_tri6.h"
32 : #include "libmesh/face_tri7.h"
33 : #include "libmesh/face_quad4.h"
34 : #include "libmesh/face_quad8.h"
35 : #include "libmesh/face_quad9.h"
36 : #include "libmesh/exodusII_io.h"
37 : #include "libmesh/quadrature_gauss.h"
38 : #include "libmesh/quadrature_nodal.h"
39 : #include "libmesh/distributed_mesh.h"
40 : #include "libmesh/replicated_mesh.h"
41 : #include "libmesh/enum_to_string.h"
42 : #include "libmesh/statistics.h"
43 : #include "libmesh/equation_systems.h"
44 :
45 : #include "metaphysicl/dualnumber.h"
46 :
47 : #include "timpi/communicator.h"
48 : #include "timpi/parallel_sync.h"
49 :
50 : #include <array>
51 : #include <algorithm>
52 :
53 : using namespace libMesh;
54 : using MetaPhysicL::DualNumber;
55 :
56 : // Make newer nanoflann API spelling compatible with older nanoflann
57 : // versions
58 : #if NANOFLANN_VERSION < 0x150
59 : namespace nanoflann
60 : {
61 : typedef SearchParams SearchParameters;
62 : }
63 : #endif
64 :
65 : class MortarNodalGeometryOutput : public Output
66 : {
67 : public:
68 2 : static InputParameters validParams()
69 : {
70 2 : auto params = Output::validParams();
71 4 : params.addPrivateParam<AutomaticMortarGeneration *>("_amg", nullptr);
72 2 : params.addPrivateParam<MooseApp *>(MooseBase::app_param, nullptr);
73 2 : params.set<std::string>(MooseBase::type_param) = "MortarNodalGeometryOutput";
74 2 : return params;
75 0 : };
76 :
77 2 : MortarNodalGeometryOutput(const InputParameters & params)
78 8 : : Output(params), _amg(*getCheckedPointerParam<AutomaticMortarGeneration *>("_amg"))
79 : {
80 2 : }
81 :
82 4 : void output() override
83 : {
84 : // Must call compute_nodal_geometry first!
85 8 : if (_amg._secondary_node_to_nodal_normal.empty() ||
86 4 : _amg._secondary_node_to_hh_nodal_tangents.empty())
87 0 : mooseError("No entries found in the secondary node -> nodal geometry map.");
88 :
89 4 : auto & problem = _app.feProblem();
90 4 : auto & subproblem = _amg._on_displaced
91 0 : ? static_cast<SubProblem &>(*problem.getDisplacedProblem())
92 4 : : static_cast<SubProblem &>(problem);
93 4 : auto & nodal_normals_es = subproblem.es();
94 :
95 4 : const std::string nodal_normals_sys_name = "nodal_normals";
96 :
97 4 : if (!_nodal_normals_system)
98 : {
99 6 : for (const auto s : make_range(nodal_normals_es.n_systems()))
100 4 : if (!nodal_normals_es.get_system(s).is_initialized())
101 : // This is really early on in the simulation and the systems have not been initialized. We
102 : // thus need to avoid calling reinit on systems that haven't even had their first init yet
103 0 : return;
104 :
105 2 : _nodal_normals_system =
106 2 : &nodal_normals_es.template add_system<ExplicitSystem>(nodal_normals_sys_name);
107 2 : _nnx_var_num = _nodal_normals_system->add_variable("nodal_normal_x", FEType(FIRST, LAGRANGE)),
108 2 : _nny_var_num = _nodal_normals_system->add_variable("nodal_normal_y", FEType(FIRST, LAGRANGE));
109 2 : _nnz_var_num = _nodal_normals_system->add_variable("nodal_normal_z", FEType(FIRST, LAGRANGE));
110 :
111 2 : _t1x_var_num =
112 2 : _nodal_normals_system->add_variable("nodal_tangent_1_x", FEType(FIRST, LAGRANGE)),
113 2 : _t1y_var_num =
114 2 : _nodal_normals_system->add_variable("nodal_tangent_1_y", FEType(FIRST, LAGRANGE));
115 2 : _t1z_var_num =
116 2 : _nodal_normals_system->add_variable("nodal_tangent_1_z", FEType(FIRST, LAGRANGE));
117 :
118 2 : _t2x_var_num =
119 2 : _nodal_normals_system->add_variable("nodal_tangent_2_x", FEType(FIRST, LAGRANGE)),
120 2 : _t2y_var_num =
121 2 : _nodal_normals_system->add_variable("nodal_tangent_2_y", FEType(FIRST, LAGRANGE));
122 2 : _t2z_var_num =
123 2 : _nodal_normals_system->add_variable("nodal_tangent_2_z", FEType(FIRST, LAGRANGE));
124 2 : nodal_normals_es.reinit();
125 : }
126 :
127 4 : const DofMap & dof_map = _nodal_normals_system->get_dof_map();
128 4 : std::vector<dof_id_type> dof_indices_nnx, dof_indices_nny, dof_indices_nnz;
129 4 : std::vector<dof_id_type> dof_indices_t1x, dof_indices_t1y, dof_indices_t1z;
130 4 : std::vector<dof_id_type> dof_indices_t2x, dof_indices_t2y, dof_indices_t2z;
131 :
132 4 : for (MeshBase::const_element_iterator el = _amg._mesh.elements_begin(),
133 4 : end_el = _amg._mesh.elements_end();
134 940 : el != end_el;
135 936 : ++el)
136 : {
137 936 : const Elem * elem = *el;
138 :
139 : // Get the nodal dofs for this Elem.
140 936 : dof_map.dof_indices(elem, dof_indices_nnx, _nnx_var_num);
141 936 : dof_map.dof_indices(elem, dof_indices_nny, _nny_var_num);
142 936 : dof_map.dof_indices(elem, dof_indices_nnz, _nnz_var_num);
143 :
144 936 : dof_map.dof_indices(elem, dof_indices_t1x, _t1x_var_num);
145 936 : dof_map.dof_indices(elem, dof_indices_t1y, _t1y_var_num);
146 936 : dof_map.dof_indices(elem, dof_indices_t1z, _t1z_var_num);
147 :
148 936 : dof_map.dof_indices(elem, dof_indices_t2x, _t2x_var_num);
149 936 : dof_map.dof_indices(elem, dof_indices_t2y, _t2y_var_num);
150 936 : dof_map.dof_indices(elem, dof_indices_t2z, _t2z_var_num);
151 :
152 : //
153 :
154 : // For each node of the Elem, if it is in the secondary_node_to_nodal_normal
155 : // container, set the corresponding nodal normal dof values.
156 7800 : for (MooseIndex(elem->n_vertices()) n = 0; n < elem->n_vertices(); ++n)
157 : {
158 6864 : auto it = _amg._secondary_node_to_nodal_normal.find(elem->node_ptr(n));
159 6864 : if (it != _amg._secondary_node_to_nodal_normal.end())
160 : {
161 384 : _nodal_normals_system->solution->set(dof_indices_nnx[n], it->second(0));
162 384 : _nodal_normals_system->solution->set(dof_indices_nny[n], it->second(1));
163 384 : _nodal_normals_system->solution->set(dof_indices_nnz[n], it->second(2));
164 : }
165 :
166 6864 : auto it_tangent = _amg._secondary_node_to_hh_nodal_tangents.find(elem->node_ptr(n));
167 6864 : if (it_tangent != _amg._secondary_node_to_hh_nodal_tangents.end())
168 : {
169 384 : _nodal_normals_system->solution->set(dof_indices_t1x[n], it_tangent->second[0](0));
170 384 : _nodal_normals_system->solution->set(dof_indices_t1y[n], it_tangent->second[0](1));
171 384 : _nodal_normals_system->solution->set(dof_indices_t1z[n], it_tangent->second[0](2));
172 :
173 384 : _nodal_normals_system->solution->set(dof_indices_t2x[n], it_tangent->second[1](0));
174 384 : _nodal_normals_system->solution->set(dof_indices_t2y[n], it_tangent->second[1](1));
175 384 : _nodal_normals_system->solution->set(dof_indices_t2z[n], it_tangent->second[1](2));
176 : }
177 :
178 : } // end loop over nodes
179 4 : } // end loop over elems
180 :
181 : // Finish assembly.
182 4 : _nodal_normals_system->solution->close();
183 :
184 12 : std::set<std::string> sys_names = {nodal_normals_sys_name};
185 :
186 : // Write the nodal normals to file
187 4 : ExodusII_IO nodal_normals_writer(_amg._mesh);
188 :
189 : // Default to non-HDF5 output for wider compatibility
190 4 : nodal_normals_writer.set_hdf5_writing(false);
191 :
192 4 : nodal_normals_writer.write_equation_systems(
193 : "nodal_geometry_only.e", nodal_normals_es, &sys_names);
194 8 : }
195 :
196 : private:
197 : /// The mortar generation object that we will query for nodal normal and tangent information
198 : AutomaticMortarGeneration & _amg;
199 :
200 : ///@{
201 : /** Member variables for geometry debug output */
202 : libMesh::System * _nodal_normals_system = nullptr;
203 : unsigned int _nnx_var_num;
204 : unsigned int _nny_var_num;
205 : unsigned int _nnz_var_num;
206 :
207 : unsigned int _t1x_var_num;
208 : unsigned int _t1y_var_num;
209 : unsigned int _t1z_var_num;
210 :
211 : unsigned int _t2x_var_num;
212 : unsigned int _t2y_var_num;
213 : unsigned int _t2z_var_num;
214 : ///@}
215 : };
216 :
217 1024 : AutomaticMortarGeneration::AutomaticMortarGeneration(
218 : MooseApp & app,
219 : MeshBase & mesh_in,
220 : const std::pair<BoundaryID, BoundaryID> & boundary_key,
221 : const std::pair<SubdomainID, SubdomainID> & subdomain_key,
222 : bool on_displaced,
223 : bool periodic,
224 : const bool debug,
225 : const bool correct_edge_dropping,
226 1024 : const Real minimum_projection_angle)
227 : : ConsoleStreamInterface(app),
228 1024 : _app(app),
229 1024 : _mesh(mesh_in),
230 1024 : _debug(debug),
231 1024 : _on_displaced(on_displaced),
232 1024 : _periodic(periodic),
233 : // We always ghost the entire mortar interface when it is allowed to displace
234 1024 : _distributed(_on_displaced ? false : !_mesh.is_replicated()),
235 1024 : _correct_edge_dropping(correct_edge_dropping),
236 2048 : _minimum_projection_angle(minimum_projection_angle)
237 : {
238 1024 : _primary_secondary_boundary_id_pairs.push_back(boundary_key);
239 1024 : _primary_requested_boundary_ids.insert(boundary_key.first);
240 1024 : _secondary_requested_boundary_ids.insert(boundary_key.second);
241 1024 : _primary_secondary_subdomain_id_pairs.push_back(subdomain_key);
242 1024 : _primary_boundary_subdomain_ids.insert(subdomain_key.first);
243 1024 : _secondary_boundary_subdomain_ids.insert(subdomain_key.second);
244 :
245 1024 : if (_distributed)
246 104 : _mortar_segment_mesh = std::make_unique<DistributedMesh>(_mesh.comm());
247 : else
248 920 : _mortar_segment_mesh = std::make_unique<ReplicatedMesh>(_mesh.comm());
249 1024 : }
250 :
251 : void
252 1024 : AutomaticMortarGeneration::initOutput()
253 : {
254 1024 : if (!_debug)
255 1022 : return;
256 :
257 2 : _output_params = std::make_unique<InputParameters>(MortarNodalGeometryOutput::validParams());
258 4 : _output_params->set<AutomaticMortarGeneration *>("_amg") = this;
259 4 : _output_params->set<FEProblemBase *>("_fe_problem_base") = &_app.feProblem();
260 2 : _output_params->set<MooseApp *>(MooseBase::app_param) = &_app;
261 2 : _output_params->set<std::string>(MooseBase::name_param) =
262 4 : "mortar_nodal_geometry_" +
263 8 : std::to_string(_primary_secondary_boundary_id_pairs.front().first) +
264 8 : std::to_string(_primary_secondary_boundary_id_pairs.front().second) + "_" +
265 4 : (_on_displaced ? "displaced" : "undisplaced");
266 4 : _output_params->finalize("MortarNodalGeometryOutput");
267 2 : _app.getOutputWarehouse().addOutput(std::make_shared<MortarNodalGeometryOutput>(*_output_params));
268 : }
269 :
270 : void
271 4937 : AutomaticMortarGeneration::clear()
272 : {
273 4937 : _mortar_segment_mesh->clear();
274 4937 : _nodes_to_secondary_elem_map.clear();
275 4937 : _nodes_to_primary_elem_map.clear();
276 4937 : _secondary_node_and_elem_to_xi2_primary_elem.clear();
277 4937 : _primary_node_and_elem_to_xi1_secondary_elem.clear();
278 4937 : _msm_elem_to_info.clear();
279 4937 : _lower_elem_to_side_id.clear();
280 4937 : _mortar_interface_coupling.clear();
281 4937 : _secondary_node_to_nodal_normal.clear();
282 4937 : _secondary_node_to_hh_nodal_tangents.clear();
283 4937 : _secondary_element_to_secondary_lowerd_element.clear();
284 4937 : _secondary_elems_to_mortar_segments.clear();
285 4937 : _secondary_ip_sub_ids.clear();
286 4937 : _primary_ip_sub_ids.clear();
287 4937 : _projected_secondary_nodes.clear();
288 4937 : _failed_secondary_node_projections.clear();
289 4937 : }
290 :
291 : void
292 4937 : AutomaticMortarGeneration::buildNodeToElemMaps()
293 : {
294 4937 : if (_secondary_requested_boundary_ids.empty() || _primary_requested_boundary_ids.empty())
295 0 : mooseError(
296 : "Must specify secondary and primary boundary ids before building node-to-elem maps.");
297 :
298 : // Construct nodes_to_secondary_elem_map
299 4937 : for (const auto & secondary_elem :
300 1000886 : as_range(_mesh.active_elements_begin(), _mesh.active_elements_end()))
301 : {
302 : // If this is not one of the lower-dimensional secondary side elements, go on to the next one.
303 495506 : if (!this->_secondary_boundary_subdomain_ids.count(secondary_elem->subdomain_id()))
304 468882 : continue;
305 :
306 94824 : for (const auto & nd : secondary_elem->node_ref_range())
307 : {
308 68200 : std::vector<const Elem *> & vec = _nodes_to_secondary_elem_map[nd.id()];
309 68200 : vec.push_back(secondary_elem);
310 : }
311 4937 : }
312 :
313 : // Construct nodes_to_primary_elem_map
314 4937 : for (const auto & primary_elem :
315 1000886 : as_range(_mesh.active_elements_begin(), _mesh.active_elements_end()))
316 : {
317 : // If this is not one of the lower-dimensional primary side elements, go on to the next one.
318 495506 : if (!this->_primary_boundary_subdomain_ids.count(primary_elem->subdomain_id()))
319 469964 : continue;
320 :
321 100125 : for (const auto & nd : primary_elem->node_ref_range())
322 : {
323 74583 : std::vector<const Elem *> & vec = _nodes_to_primary_elem_map[nd.id()];
324 74583 : vec.push_back(primary_elem);
325 : }
326 4937 : }
327 4937 : }
328 :
329 : std::vector<Point>
330 317852 : AutomaticMortarGeneration::getNodalNormals(const Elem & secondary_elem) const
331 : {
332 317852 : std::vector<Point> nodal_normals(secondary_elem.n_nodes());
333 1912764 : for (const auto n : make_range(secondary_elem.n_nodes()))
334 1594912 : nodal_normals[n] = _secondary_node_to_nodal_normal.at(secondary_elem.node_ptr(n));
335 :
336 317852 : return nodal_normals;
337 0 : }
338 :
339 : const Elem *
340 0 : AutomaticMortarGeneration::getSecondaryLowerdElemFromSecondaryElem(
341 : dof_id_type secondary_elem_id) const
342 : {
343 : mooseAssert(_secondary_element_to_secondary_lowerd_element.count(secondary_elem_id),
344 : "Map should locate secondary element");
345 :
346 0 : return _secondary_element_to_secondary_lowerd_element.at(secondary_elem_id);
347 : }
348 :
349 : std::map<unsigned int, unsigned int>
350 32880 : AutomaticMortarGeneration::getSecondaryIpToLowerElementMap(const Elem & lower_secondary_elem) const
351 : {
352 32880 : std::map<unsigned int, unsigned int> secondary_ip_i_to_lower_secondary_i;
353 32880 : const Elem * const secondary_ip = lower_secondary_elem.interior_parent();
354 : mooseAssert(secondary_ip, "This should be non-null");
355 :
356 98640 : for (const auto i : make_range(lower_secondary_elem.n_nodes()))
357 : {
358 65760 : const auto & nd = lower_secondary_elem.node_ref(i);
359 65760 : secondary_ip_i_to_lower_secondary_i[secondary_ip->get_node_index(&nd)] = i;
360 : }
361 :
362 32880 : return secondary_ip_i_to_lower_secondary_i;
363 0 : }
364 :
365 : std::map<unsigned int, unsigned int>
366 32880 : AutomaticMortarGeneration::getPrimaryIpToLowerElementMap(
367 : const Elem & lower_primary_elem,
368 : const Elem & primary_elem,
369 : const Elem & /*lower_secondary_elem*/) const
370 : {
371 32880 : std::map<unsigned int, unsigned int> primary_ip_i_to_lower_primary_i;
372 :
373 98640 : for (const auto i : make_range(lower_primary_elem.n_nodes()))
374 : {
375 65760 : const auto & nd = lower_primary_elem.node_ref(i);
376 65760 : primary_ip_i_to_lower_primary_i[primary_elem.get_node_index(&nd)] = i;
377 : }
378 :
379 32880 : return primary_ip_i_to_lower_primary_i;
380 0 : }
381 :
382 : std::array<MooseUtils::SemidynamicVector<Point, 9>, 2>
383 0 : AutomaticMortarGeneration::getNodalTangents(const Elem & secondary_elem) const
384 : {
385 : // MetaPhysicL will check if we ran out of allocated space.
386 0 : MooseUtils::SemidynamicVector<Point, 9> nodal_tangents_one(0);
387 0 : MooseUtils::SemidynamicVector<Point, 9> nodal_tangents_two(0);
388 :
389 0 : for (const auto n : make_range(secondary_elem.n_nodes()))
390 : {
391 : const auto & tangent_vectors =
392 0 : libmesh_map_find(_secondary_node_to_hh_nodal_tangents, secondary_elem.node_ptr(n));
393 0 : nodal_tangents_one.push_back(tangent_vectors[0]);
394 0 : nodal_tangents_two.push_back(tangent_vectors[1]);
395 : }
396 :
397 0 : return {{nodal_tangents_one, nodal_tangents_two}};
398 : }
399 :
400 : std::vector<Point>
401 13501 : AutomaticMortarGeneration::getNormals(const Elem & secondary_elem,
402 : const std::vector<Real> & oned_xi1_pts) const
403 : {
404 13501 : std::vector<Point> xi1_pts(oned_xi1_pts.size());
405 27002 : for (const auto qp : index_range(oned_xi1_pts))
406 13501 : xi1_pts[qp] = oned_xi1_pts[qp];
407 :
408 27002 : return getNormals(secondary_elem, xi1_pts);
409 13501 : }
410 :
411 : std::vector<Point>
412 313578 : AutomaticMortarGeneration::getNormals(const Elem & secondary_elem,
413 : const std::vector<Point> & xi1_pts) const
414 : {
415 313578 : const auto mortar_dim = _mesh.mesh_dimension() - 1;
416 313578 : const auto num_qps = xi1_pts.size();
417 313578 : const auto nodal_normals = getNodalNormals(secondary_elem);
418 313578 : std::vector<Point> normals(num_qps);
419 :
420 1890842 : for (const auto n : make_range(secondary_elem.n_nodes()))
421 10567070 : for (const auto qp : make_range(num_qps))
422 : {
423 : const auto phi =
424 : (mortar_dim == 1)
425 8989806 : ? Moose::fe_lagrange_1D_shape(secondary_elem.default_order(), n, xi1_pts[qp](0))
426 8588352 : : Moose::fe_lagrange_2D_shape(secondary_elem.type(),
427 8588352 : secondary_elem.default_order(),
428 : n,
429 8588352 : static_cast<const TypeVector<Real> &>(xi1_pts[qp]));
430 8989806 : normals[qp] += phi * nodal_normals[n];
431 : }
432 :
433 313578 : if (_periodic)
434 72758 : for (auto & normal : normals)
435 57277 : normal *= -1;
436 :
437 627156 : return normals;
438 313578 : }
439 :
440 : void
441 4719 : AutomaticMortarGeneration::buildMortarSegmentMesh()
442 : {
443 4719 : dof_id_type local_id_index = 0;
444 4719 : std::size_t node_unique_id_offset = 0;
445 :
446 : // Create an offset by the maximum number of mortar segment elements that can be created *plus*
447 : // the number of lower-dimensional secondary subdomain elements. Recall that the number of mortar
448 : // segments created is a function of node projection, *and* that if we split elems we will delete
449 : // that elem which has already taken a unique id
450 9438 : for (const auto & pr : _primary_secondary_boundary_id_pairs)
451 : {
452 4719 : const auto primary_bnd_id = pr.first;
453 4719 : const auto secondary_bnd_id = pr.second;
454 : const auto num_primary_nodes =
455 9438 : std::distance(_mesh.bid_nodes_begin(primary_bnd_id), _mesh.bid_nodes_end(primary_bnd_id));
456 9438 : const auto num_secondary_nodes = std::distance(_mesh.bid_nodes_begin(secondary_bnd_id),
457 9438 : _mesh.bid_nodes_end(secondary_bnd_id));
458 : mooseAssert(num_primary_nodes,
459 : "There are no primary nodes on boundary ID "
460 : << primary_bnd_id << ". Does that bondary ID even exist on the mesh?");
461 : mooseAssert(num_secondary_nodes,
462 : "There are no secondary nodes on boundary ID "
463 : << secondary_bnd_id << ". Does that bondary ID even exist on the mesh?");
464 :
465 4719 : node_unique_id_offset += num_primary_nodes + 2 * num_secondary_nodes;
466 : }
467 :
468 : // 1.) Add all lower-dimensional secondary side elements as the "initial" mortar segments.
469 4719 : for (MeshBase::const_element_iterator el = _mesh.active_elements_begin(),
470 4719 : end_el = _mesh.active_elements_end();
471 436164 : el != end_el;
472 431445 : ++el)
473 : {
474 431445 : const Elem * secondary_elem = *el;
475 :
476 : // If this is not one of the lower-dimensional secondary side elements, go on to the next one.
477 431445 : if (!this->_secondary_boundary_subdomain_ids.count(secondary_elem->subdomain_id()))
478 408926 : continue;
479 :
480 22519 : std::vector<Node *> new_nodes;
481 69785 : for (MooseIndex(secondary_elem->n_nodes()) n = 0; n < secondary_elem->n_nodes(); ++n)
482 : {
483 47266 : new_nodes.push_back(_mortar_segment_mesh->add_point(
484 : secondary_elem->point(n), secondary_elem->node_id(n), secondary_elem->processor_id()));
485 47266 : Node * const new_node = new_nodes.back();
486 47266 : new_node->set_unique_id(new_node->id() + node_unique_id_offset);
487 : }
488 :
489 22519 : std::unique_ptr<Elem> new_elem;
490 22519 : if (secondary_elem->default_order() == SECOND)
491 2228 : new_elem = std::make_unique<Edge3>();
492 : else
493 20291 : new_elem = std::make_unique<Edge2>();
494 :
495 22519 : new_elem->processor_id() = secondary_elem->processor_id();
496 22519 : new_elem->subdomain_id() = secondary_elem->subdomain_id();
497 22519 : new_elem->set_id(local_id_index++);
498 22519 : new_elem->set_unique_id(new_elem->id());
499 :
500 69785 : for (MooseIndex(new_elem->n_nodes()) n = 0; n < new_elem->n_nodes(); ++n)
501 47266 : new_elem->set_node(n, new_nodes[n]);
502 :
503 22519 : Elem * new_elem_ptr = _mortar_segment_mesh->add_elem(new_elem.release());
504 :
505 : // The xi^(1) values for this mortar segment are initially -1 and 1.
506 22519 : MortarSegmentInfo msinfo;
507 22519 : msinfo.xi1_a = -1;
508 22519 : msinfo.xi1_b = +1;
509 22519 : msinfo.secondary_elem = secondary_elem;
510 :
511 22519 : auto new_container_it0 = _secondary_node_and_elem_to_xi2_primary_elem.find(
512 22519 : std::make_pair(secondary_elem->node_ptr(0), secondary_elem)),
513 22519 : new_container_it1 = _secondary_node_and_elem_to_xi2_primary_elem.find(
514 22519 : std::make_pair(secondary_elem->node_ptr(1), secondary_elem));
515 :
516 : bool new_container_node0_found =
517 22519 : (new_container_it0 != _secondary_node_and_elem_to_xi2_primary_elem.end()),
518 : new_container_node1_found =
519 22519 : (new_container_it1 != _secondary_node_and_elem_to_xi2_primary_elem.end());
520 :
521 22519 : const Elem * node0_primary_candidate = nullptr;
522 22519 : const Elem * node1_primary_candidate = nullptr;
523 :
524 22519 : if (new_container_node0_found)
525 : {
526 18852 : const auto & xi2_primary_elem_pair = new_container_it0->second;
527 18852 : msinfo.xi2_a = xi2_primary_elem_pair.first;
528 18852 : node0_primary_candidate = xi2_primary_elem_pair.second;
529 : }
530 :
531 22519 : if (new_container_node1_found)
532 : {
533 22151 : const auto & xi2_primary_elem_pair = new_container_it1->second;
534 22151 : msinfo.xi2_b = xi2_primary_elem_pair.first;
535 22151 : node1_primary_candidate = xi2_primary_elem_pair.second;
536 : }
537 :
538 : // If both node0 and node1 agree on the primary element they are
539 : // projected into, then this mortar segment fits entirely within
540 : // a single primary element, and we can go ahead and set the
541 : // msinfo.primary_elem pointer now.
542 22519 : if (node0_primary_candidate == node1_primary_candidate)
543 9053 : msinfo.primary_elem = node0_primary_candidate;
544 :
545 : // Associate this MSM elem with the MortarSegmentInfo.
546 22519 : _msm_elem_to_info.emplace(new_elem_ptr, msinfo);
547 :
548 : // Maintain the mapping between secondary elems and mortar segment elems contained within them.
549 : // Initially, only the original secondary_elem is present.
550 22519 : _secondary_elems_to_mortar_segments[secondary_elem->id()].insert(new_elem_ptr);
551 27238 : }
552 :
553 : // 2.) Insert new nodes from primary side and split mortar segments as necessary.
554 28357 : for (const auto & pr : _primary_node_and_elem_to_xi1_secondary_elem)
555 : {
556 23638 : auto key = pr.first;
557 23638 : auto val = pr.second;
558 :
559 23638 : const Node * primary_node = std::get<1>(key);
560 23638 : Real xi1 = val.first;
561 23638 : const Elem * secondary_elem = val.second;
562 :
563 : // If this is an aligned node, we don't need to do anything.
564 23638 : if (std::abs(std::abs(xi1) - 1.) < _xi_tolerance)
565 10137 : continue;
566 :
567 13501 : auto && order = secondary_elem->default_order();
568 :
569 : // Determine physical location of new point to be inserted.
570 13501 : Point new_pt(0);
571 41097 : for (MooseIndex(secondary_elem->n_nodes()) n = 0; n < secondary_elem->n_nodes(); ++n)
572 27596 : new_pt += Moose::fe_lagrange_1D_shape(order, n, xi1) * secondary_elem->point(n);
573 :
574 : // Find the current mortar segment that will have to be split.
575 13501 : auto & mortar_segment_set = _secondary_elems_to_mortar_segments[secondary_elem->id()];
576 13501 : Elem * current_mortar_segment = nullptr;
577 13501 : MortarSegmentInfo * info = nullptr;
578 :
579 13501 : for (const auto & mortar_segment_candidate : mortar_segment_set)
580 : {
581 : try
582 : {
583 13501 : info = &_msm_elem_to_info.at(mortar_segment_candidate);
584 : }
585 0 : catch (std::out_of_range &)
586 : {
587 0 : mooseError("MortarSegmentInfo not found for the mortar segment candidate");
588 0 : }
589 13501 : if (info->xi1_a <= xi1 && xi1 <= info->xi1_b)
590 : {
591 13501 : current_mortar_segment = mortar_segment_candidate;
592 13501 : break;
593 : }
594 : }
595 :
596 : // Make sure we found one.
597 13501 : if (current_mortar_segment == nullptr)
598 0 : mooseError("Unable to find appropriate mortar segment during linear search!");
599 :
600 : // If node lands on endpoint of segment, don't split.
601 : // Jacob: This condition was getting missed by the < comparison a few lines above. To fix it I
602 : // just made it <= and put this condition in to handle equality different. It probably could be
603 : // done with a tolerance but the the toleranced equality is already handled later when we drop
604 : // segments with small volume.
605 13501 : if (info->xi1_a == xi1 || xi1 == info->xi1_b)
606 0 : continue;
607 :
608 13501 : const auto new_id = _mortar_segment_mesh->max_node_id() + 1;
609 : mooseAssert(_mortar_segment_mesh->comm().verify(new_id),
610 : "new_id must be the same on all processes");
611 : Node * const new_node =
612 13501 : _mortar_segment_mesh->add_point(new_pt, new_id, secondary_elem->processor_id());
613 13501 : new_node->set_unique_id(new_id + node_unique_id_offset);
614 :
615 : // Reconstruct the nodal normal at xi1. This will help us
616 : // determine the orientation of the primary elems relative to the
617 : // new mortar segments.
618 13501 : const Point normal = getNormals(*secondary_elem, std::vector<Real>({xi1}))[0];
619 :
620 : // Get the set of primary_node neighbors.
621 13501 : if (this->_nodes_to_primary_elem_map.find(primary_node->id()) ==
622 27002 : this->_nodes_to_primary_elem_map.end())
623 0 : mooseError("We should already have built this primary node to elem pair!");
624 : const std::vector<const Elem *> & primary_node_neighbors =
625 13501 : this->_nodes_to_primary_elem_map[primary_node->id()];
626 :
627 : // Sanity check
628 13501 : if (primary_node_neighbors.size() == 0 || primary_node_neighbors.size() > 2)
629 0 : mooseError("We must have either 1 or 2 primary side nodal neighbors, but we had ",
630 0 : primary_node_neighbors.size());
631 :
632 : // Primary Elem pointers which we will eventually assign to the
633 : // mortar segments being created. We start by assuming
634 : // primary_node_neighbor[0] is on the "left" and
635 : // primary_node_neighbor[1]/"nothing" is on the "right" and then
636 : // swap them if that's not the case.
637 13501 : const Elem * left_primary_elem = primary_node_neighbors[0];
638 : const Elem * right_primary_elem =
639 13501 : (primary_node_neighbors.size() == 2) ? primary_node_neighbors[1] : nullptr;
640 :
641 13501 : Real left_xi2 = MortarSegmentInfo::invalid_xi, right_xi2 = MortarSegmentInfo::invalid_xi;
642 :
643 : // Storage for z-component of cross products for determining
644 : // orientation.
645 : std::array<Real, 2> secondary_node_cps;
646 13501 : std::vector<Real> primary_node_cps(primary_node_neighbors.size());
647 :
648 : // Store z-component of left and right secondary node cross products with the nodal normal.
649 40503 : for (unsigned int nid = 0; nid < 2; ++nid)
650 27002 : secondary_node_cps[nid] = normal.cross(secondary_elem->point(nid) - new_pt)(2);
651 :
652 37204 : for (MooseIndex(primary_node_neighbors) mnn = 0; mnn < primary_node_neighbors.size(); ++mnn)
653 : {
654 23703 : const Elem * primary_neigh = primary_node_neighbors[mnn];
655 23703 : Point opposite = (primary_neigh->node_ptr(0) == primary_node) ? primary_neigh->point(1)
656 13501 : : primary_neigh->point(0);
657 23703 : Point cp = normal.cross(opposite - new_pt);
658 23703 : primary_node_cps[mnn] = cp(2);
659 : }
660 :
661 : // We will verify that only 1 orientation is actually valid.
662 13501 : bool orientation1_valid = false, orientation2_valid = false;
663 :
664 13501 : if (primary_node_neighbors.size() == 2)
665 : {
666 : // 2 primary neighbor case
667 10277 : orientation1_valid = (secondary_node_cps[0] * primary_node_cps[0] > 0.) &&
668 75 : (secondary_node_cps[1] * primary_node_cps[1] > 0.);
669 :
670 20329 : orientation2_valid = (secondary_node_cps[0] * primary_node_cps[1] > 0.) &&
671 10127 : (secondary_node_cps[1] * primary_node_cps[0] > 0.);
672 : }
673 3299 : else if (primary_node_neighbors.size() == 1)
674 : {
675 : // 1 primary neighbor case
676 3299 : orientation1_valid = (secondary_node_cps[0] * primary_node_cps[0] > 0.);
677 3299 : orientation2_valid = (secondary_node_cps[1] * primary_node_cps[0] > 0.);
678 : }
679 : else
680 0 : mooseError("Invalid primary node neighbors size ", primary_node_neighbors.size());
681 :
682 : // Verify that both orientations are not simultaneously valid/invalid. If they are not, then we
683 : // are going to throw an exception instead of erroring out since we can easily reach this point
684 : // if we have one bad linear solve. It's better in general to catch the error and then try a
685 : // smaller time-step
686 13501 : if (orientation1_valid && orientation2_valid)
687 : throw MooseException(
688 0 : "AutomaticMortarGeneration: Both orientations cannot simultaneously be valid.");
689 :
690 : // We are going to treat the case where both orientations are invalid as a case in which we
691 : // should not be splitting the mortar mesh to incorporate primary mesh elements.
692 : // In practice, this case has appeared for very oblique projections, so we assume these cases
693 : // will not be considered in mortar thermomechanical contact.
694 13501 : if (!orientation1_valid && !orientation2_valid)
695 : {
696 0 : mooseDoOnce(mooseWarning(
697 : "AutomaticMortarGeneration: Unable to determine valid secondary-primary orientation. "
698 : "Consequently we will consider projection of the primary node invalid and not split the "
699 : "mortar segment. "
700 : "This situation can indicate there are very oblique projections between primary (mortar) "
701 : "and secondary (non-mortar) surfaces for a good problem set up. It can also mean your "
702 : "time step is too large. This message is only printed once."));
703 0 : continue;
704 0 : }
705 :
706 : // Make an Elem on the left
707 13501 : std::unique_ptr<Elem> new_elem_left;
708 13501 : if (order == SECOND)
709 594 : new_elem_left = std::make_unique<Edge3>();
710 : else
711 12907 : new_elem_left = std::make_unique<Edge2>();
712 :
713 13501 : new_elem_left->processor_id() = current_mortar_segment->processor_id();
714 13501 : new_elem_left->subdomain_id() = current_mortar_segment->subdomain_id();
715 13501 : new_elem_left->set_id(local_id_index++);
716 13501 : new_elem_left->set_unique_id(new_elem_left->id());
717 13501 : new_elem_left->set_node(0, current_mortar_segment->node_ptr(0));
718 13501 : new_elem_left->set_node(1, new_node);
719 :
720 : // Make an Elem on the right
721 13501 : std::unique_ptr<Elem> new_elem_right;
722 13501 : if (order == SECOND)
723 594 : new_elem_right = std::make_unique<Edge3>();
724 : else
725 12907 : new_elem_right = std::make_unique<Edge2>();
726 :
727 13501 : new_elem_right->processor_id() = current_mortar_segment->processor_id();
728 13501 : new_elem_right->subdomain_id() = current_mortar_segment->subdomain_id();
729 13501 : new_elem_right->set_id(local_id_index++);
730 13501 : new_elem_right->set_unique_id(new_elem_right->id());
731 13501 : new_elem_right->set_node(0, new_node);
732 13501 : new_elem_right->set_node(1, current_mortar_segment->node_ptr(1));
733 :
734 13501 : if (order == SECOND)
735 : {
736 : // left
737 594 : Point left_interior_point(0);
738 594 : Real left_interior_xi = (xi1 + info->xi1_a) / 2;
739 :
740 : // This is eta for the current mortar segment that we're splitting
741 594 : Real current_left_interior_eta =
742 594 : (2. * left_interior_xi - info->xi1_a - info->xi1_b) / (info->xi1_b - info->xi1_a);
743 :
744 594 : for (MooseIndex(current_mortar_segment->n_nodes()) n = 0;
745 2376 : n < current_mortar_segment->n_nodes();
746 : ++n)
747 1782 : left_interior_point += Moose::fe_lagrange_1D_shape(order, n, current_left_interior_eta) *
748 1782 : current_mortar_segment->point(n);
749 :
750 594 : const auto new_interior_left_id = _mortar_segment_mesh->max_node_id() + 1;
751 : mooseAssert(_mortar_segment_mesh->comm().verify(new_interior_left_id),
752 : "new_id must be the same on all processes");
753 594 : Node * const new_interior_node_left = _mortar_segment_mesh->add_point(
754 594 : left_interior_point, new_interior_left_id, new_elem_left->processor_id());
755 594 : new_elem_left->set_node(2, new_interior_node_left);
756 594 : new_interior_node_left->set_unique_id(new_interior_left_id + node_unique_id_offset);
757 :
758 : // right
759 594 : Point right_interior_point(0);
760 594 : Real right_interior_xi = (xi1 + info->xi1_b) / 2;
761 : // This is eta for the current mortar segment that we're splitting
762 594 : Real current_right_interior_eta =
763 594 : (2. * right_interior_xi - info->xi1_a - info->xi1_b) / (info->xi1_b - info->xi1_a);
764 :
765 594 : for (MooseIndex(current_mortar_segment->n_nodes()) n = 0;
766 2376 : n < current_mortar_segment->n_nodes();
767 : ++n)
768 1782 : right_interior_point += Moose::fe_lagrange_1D_shape(order, n, current_right_interior_eta) *
769 1782 : current_mortar_segment->point(n);
770 :
771 594 : const auto new_interior_id_right = _mortar_segment_mesh->max_node_id() + 1;
772 : mooseAssert(_mortar_segment_mesh->comm().verify(new_interior_id_right),
773 : "new_id must be the same on all processes");
774 594 : Node * const new_interior_node_right = _mortar_segment_mesh->add_point(
775 594 : right_interior_point, new_interior_id_right, new_elem_right->processor_id());
776 594 : new_elem_right->set_node(2, new_interior_node_right);
777 594 : new_interior_node_right->set_unique_id(new_interior_id_right + node_unique_id_offset);
778 : }
779 :
780 : // If orientation 2 was valid, swap the left and right primaries.
781 13501 : if (orientation2_valid)
782 13426 : std::swap(left_primary_elem, right_primary_elem);
783 :
784 : // Now that we know left_primary_elem and right_primary_elem, we can determine left_xi2 and
785 : // right_xi2.
786 13501 : if (left_primary_elem)
787 10202 : left_xi2 = (primary_node == left_primary_elem->node_ptr(0)) ? -1 : +1;
788 13501 : if (right_primary_elem)
789 13501 : right_xi2 = (primary_node == right_primary_elem->node_ptr(0)) ? -1 : +1;
790 :
791 : // Grab the MortarSegmentInfo object associated with this
792 : // segment. We can use "at()" here since we want this to fail if
793 : // current_mortar_segment is not found... Since we're going to
794 : // erase this entry from the map momentarily, we make an actual
795 : // copy rather than grabbing a reference.
796 13501 : auto msm_it = _msm_elem_to_info.find(current_mortar_segment);
797 13501 : if (msm_it == _msm_elem_to_info.end())
798 0 : mooseError("MortarSegmentInfo not found for current_mortar_segment.");
799 13501 : MortarSegmentInfo current_msinfo = msm_it->second;
800 :
801 : // add_left
802 : {
803 13501 : Elem * msm_new_elem = _mortar_segment_mesh->add_elem(new_elem_left.release());
804 :
805 : // Create new MortarSegmentInfo objects for new_elem_left
806 13501 : MortarSegmentInfo new_msinfo_left;
807 :
808 : // The new MortarSegmentInfo info objects inherit their "outer"
809 : // information from current_msinfo and the rest is determined by
810 : // the Node being inserted.
811 13501 : new_msinfo_left.xi1_a = current_msinfo.xi1_a;
812 13501 : new_msinfo_left.xi2_a = current_msinfo.xi2_a;
813 13501 : new_msinfo_left.secondary_elem = secondary_elem;
814 13501 : new_msinfo_left.xi1_b = xi1;
815 13501 : new_msinfo_left.xi2_b = left_xi2;
816 13501 : new_msinfo_left.primary_elem = left_primary_elem;
817 :
818 : // Add new msinfo objects to the map.
819 13501 : _msm_elem_to_info.emplace(msm_new_elem, new_msinfo_left);
820 :
821 : // We need to insert new_elem_left in
822 : // the mortar_segment_set for this secondary_elem.
823 13501 : mortar_segment_set.insert(msm_new_elem);
824 : }
825 :
826 : // add_right
827 : {
828 13501 : Elem * msm_new_elem = _mortar_segment_mesh->add_elem(new_elem_right.release());
829 :
830 : // Create new MortarSegmentInfo objects for new_elem_right
831 13501 : MortarSegmentInfo new_msinfo_right;
832 :
833 13501 : new_msinfo_right.xi1_b = current_msinfo.xi1_b;
834 13501 : new_msinfo_right.xi2_b = current_msinfo.xi2_b;
835 13501 : new_msinfo_right.secondary_elem = secondary_elem;
836 13501 : new_msinfo_right.xi1_a = xi1;
837 13501 : new_msinfo_right.xi2_a = right_xi2;
838 13501 : new_msinfo_right.primary_elem = right_primary_elem;
839 :
840 13501 : _msm_elem_to_info.emplace(msm_new_elem, new_msinfo_right);
841 :
842 13501 : mortar_segment_set.insert(msm_new_elem);
843 : }
844 :
845 : // Erase the MortarSegmentInfo object for current_mortar_segment from the map.
846 13501 : _msm_elem_to_info.erase(msm_it);
847 :
848 : // current_mortar_segment must be erased from the
849 : // mortar_segment_set since it has now been split.
850 13501 : mortar_segment_set.erase(current_mortar_segment);
851 :
852 : // The original mortar segment has been split, so erase it from
853 : // the mortar segment mesh.
854 13501 : _mortar_segment_mesh->delete_elem(current_mortar_segment);
855 13501 : }
856 :
857 : // Remove all MSM elements without a primary contribution
858 : /**
859 : * This was a change to how inactive LM DoFs are handled. Now mortar segment elements
860 : * are not used in assembly if there is no corresponding primary element and inactive
861 : * LM DoFs (those with no contribution to an active primary element) are zeroed.
862 : */
863 40739 : for (auto msm_elem : _mortar_segment_mesh->active_element_ptr_range())
864 : {
865 36020 : MortarSegmentInfo & msinfo = libmesh_map_find(_msm_elem_to_info, msm_elem);
866 36020 : Elem * primary_elem = const_cast<Elem *>(msinfo.primary_elem);
867 68373 : if (primary_elem == nullptr || std::abs(msinfo.xi2_a) > 1.0 + TOLERANCE ||
868 32353 : std::abs(msinfo.xi2_b) > 1.0 + TOLERANCE)
869 : {
870 : // Erase from secondary to msms map
871 3667 : auto it = _secondary_elems_to_mortar_segments.find(msinfo.secondary_elem->id());
872 : mooseAssert(it != _secondary_elems_to_mortar_segments.end(),
873 : "We should have found the element");
874 3667 : auto & msm_set = it->second;
875 3667 : msm_set.erase(msm_elem);
876 : // We may be creating nodes with only one element neighbor where before this removal there
877 : // were two. But the nodal normal used in computations will reflect the two-neighbor geometry.
878 : // For a lower-d secondary mesh corner, that will imply the corner node will have a tilted
879 : // normal vector (same for tangents) despite the mortar segment mesh not including its
880 : // vertical neighboring element. It is the secondary element neighbors (not mortar segment
881 : // mesh neighbors) that determine the nodal normal field.
882 3667 : if (msm_set.empty())
883 368 : _secondary_elems_to_mortar_segments.erase(it);
884 :
885 : // Erase msinfo
886 3667 : _msm_elem_to_info.erase(msm_elem);
887 :
888 : // Remove element from mortar segment mesh
889 3667 : _mortar_segment_mesh->delete_elem(msm_elem);
890 : }
891 : else
892 : {
893 32353 : _secondary_ip_sub_ids.insert(msinfo.secondary_elem->interior_parent()->subdomain_id());
894 32353 : _primary_ip_sub_ids.insert(msinfo.primary_elem->interior_parent()->subdomain_id());
895 : }
896 4719 : }
897 :
898 4719 : std::unordered_set<Node *> msm_connected_nodes;
899 :
900 : // Deleting elements may produce isolated nodes.
901 : // Loops for identifying and removing such nodes from mortar segment mesh.
902 37072 : for (const auto & element : _mortar_segment_mesh->element_ptr_range())
903 99881 : for (auto & n : element->node_ref_range())
904 72247 : msm_connected_nodes.insert(&n);
905 :
906 48887 : for (const auto & node : _mortar_segment_mesh->node_ptr_range())
907 44168 : if (!msm_connected_nodes.count(node))
908 8980 : _mortar_segment_mesh->delete_node(node);
909 :
910 : #ifdef DEBUG
911 : // Verify that all segments without primary contribution have been deleted
912 : for (auto msm_elem : _mortar_segment_mesh->active_element_ptr_range())
913 : {
914 : const MortarSegmentInfo & msinfo = libmesh_map_find(_msm_elem_to_info, msm_elem);
915 : mooseAssert(msinfo.primary_elem != nullptr,
916 : "All mortar segment elements should have valid "
917 : "primary element.");
918 : }
919 : #endif
920 :
921 4719 : _mortar_segment_mesh->cache_elem_data();
922 :
923 : // (Optionally) Write the mortar segment mesh to file for inspection
924 4719 : if (_debug)
925 : {
926 0 : ExodusII_IO mortar_segment_mesh_writer(*_mortar_segment_mesh);
927 :
928 : // Default to non-HDF5 output for wider compatibility
929 0 : mortar_segment_mesh_writer.set_hdf5_writing(false);
930 :
931 0 : mortar_segment_mesh_writer.write("mortar_segment_mesh.e");
932 0 : }
933 :
934 4719 : buildCouplingInformation();
935 4719 : }
936 :
937 : void
938 218 : AutomaticMortarGeneration::buildMortarSegmentMesh3d()
939 : {
940 : // Add an integer flag to mortar segment mesh to keep track of which subelem
941 : // of second order primal elements mortar segments correspond to
942 436 : auto secondary_sub_elem = _mortar_segment_mesh->add_elem_integer("secondary_sub_elem");
943 436 : auto primary_sub_elem = _mortar_segment_mesh->add_elem_integer("primary_sub_elem");
944 :
945 218 : dof_id_type local_id_index = 0;
946 :
947 : // Loop through mortar secondary and primary pairs to create mortar segment mesh between each
948 436 : for (const auto & pr : _primary_secondary_subdomain_id_pairs)
949 : {
950 218 : const auto primary_subd_id = pr.first;
951 218 : const auto secondary_subd_id = pr.second;
952 :
953 : // Build k-d tree for use in Step 1.2 for primary interface coarse screening
954 218 : NanoflannMeshSubdomainAdaptor<3> mesh_adaptor(_mesh, primary_subd_id);
955 : subdomain_kd_tree_t kd_tree(
956 218 : 3, mesh_adaptor, nanoflann::KDTreeSingleIndexAdaptorParams(/*max leaf=*/10));
957 :
958 : // Construct the KD tree.
959 218 : kd_tree.buildIndex();
960 :
961 : // Define expression for getting sub-elements nodes (for sub-dividing secondary elements)
962 59293 : auto get_sub_elem_nodes = [](const ElemType type,
963 : const unsigned int sub_elem) -> std::vector<unsigned int>
964 : {
965 59293 : switch (type)
966 : {
967 2870 : case TRI3:
968 8610 : return {{0, 1, 2}};
969 18607 : case QUAD4:
970 55821 : return {{0, 1, 2, 3}};
971 10752 : case TRI6:
972 : case TRI7:
973 10752 : switch (sub_elem)
974 : {
975 2688 : case 0:
976 8064 : return {{0, 3, 5}};
977 2688 : case 1:
978 8064 : return {{3, 4, 5}};
979 2688 : case 2:
980 8064 : return {{3, 1, 4}};
981 2688 : case 3:
982 8064 : return {{5, 4, 2}};
983 0 : default:
984 0 : mooseError("get_sub_elem_nodes: Invalid sub_elem: ", sub_elem);
985 : }
986 22440 : case QUAD8:
987 22440 : switch (sub_elem)
988 : {
989 4488 : case 0:
990 13464 : return {{0, 4, 7}};
991 4488 : case 1:
992 13464 : return {{4, 1, 5}};
993 4488 : case 2:
994 13464 : return {{5, 2, 6}};
995 4488 : case 3:
996 13464 : return {{7, 6, 3}};
997 4488 : case 4:
998 13464 : return {{4, 5, 6, 7}};
999 0 : default:
1000 0 : mooseError("get_sub_elem_nodes: Invalid sub_elem: ", sub_elem);
1001 : }
1002 4624 : case QUAD9:
1003 4624 : switch (sub_elem)
1004 : {
1005 1156 : case 0:
1006 3468 : return {{0, 4, 8, 7}};
1007 1156 : case 1:
1008 3468 : return {{4, 1, 5, 8}};
1009 1156 : case 2:
1010 3468 : return {{8, 5, 2, 6}};
1011 1156 : case 3:
1012 3468 : return {{7, 8, 6, 3}};
1013 0 : default:
1014 0 : mooseError("get_sub_elem_nodes: Invalid sub_elem: ", sub_elem);
1015 : }
1016 0 : default:
1017 0 : mooseError("get_sub_elem_inds: Face element type: ",
1018 0 : libMesh::Utility::enum_to_string<ElemType>(type),
1019 : " invalid for 3D mortar");
1020 : }
1021 : };
1022 :
1023 : /**
1024 : * Step 1: Build mortar segments for all secondary elements
1025 : */
1026 218 : for (MeshBase::const_element_iterator el = _mesh.active_local_elements_begin(),
1027 218 : end_el = _mesh.active_local_elements_end();
1028 45312 : el != end_el;
1029 45094 : ++el)
1030 : {
1031 45094 : const Elem * secondary_side_elem = *el;
1032 :
1033 45094 : const Real secondary_volume = secondary_side_elem->volume();
1034 :
1035 : // If this Elem is not in the current secondary subdomain, go on to the next one.
1036 45094 : if (secondary_side_elem->subdomain_id() != secondary_subd_id)
1037 42176 : continue;
1038 :
1039 2918 : auto [secondary_elem_to_msm_map_it, insertion_happened] =
1040 2918 : _secondary_elems_to_mortar_segments.emplace(secondary_side_elem->id(),
1041 5836 : std::set<Elem *, CompareDofObjectsByID>{});
1042 2918 : libmesh_ignore(insertion_happened);
1043 2918 : auto & secondary_to_msm_element_set = secondary_elem_to_msm_map_it->second;
1044 :
1045 : std::vector<std::unique_ptr<MortarSegmentHelper>> mortar_segment_helper(
1046 2918 : secondary_side_elem->n_sub_elem());
1047 2918 : const auto nodal_normals = getNodalNormals(*secondary_side_elem);
1048 :
1049 : /**
1050 : * Step 1.1: Linearize secondary face elements
1051 : *
1052 : * For first order face elements (Tri3 and Quad4) elements are simply linearized around center
1053 : * For second order (Tri6 and Quad9) and third order (Tri7) face elements, elements are
1054 : * sub-divided into four first order elements then each of the sub-elements is linearized
1055 : * around their respective centers
1056 : * For Quad8 elements, they are sub-divided into one quad and four triangle elements and each
1057 : * sub-element is linearized around their respective centers
1058 : */
1059 8800 : for (auto sel : make_range(secondary_side_elem->n_sub_elem()))
1060 : {
1061 : // Get indices of sub-element nodes in element
1062 5882 : auto sub_elem_nodes = get_sub_elem_nodes(secondary_side_elem->type(), sel);
1063 :
1064 : // Secondary sub-element center, normal, and nodes
1065 5882 : Point center;
1066 5882 : Point normal;
1067 5882 : std::vector<Point> nodes(sub_elem_nodes.size());
1068 :
1069 : // Loop through sub_element nodes, collect points and compute center and normal
1070 26050 : for (auto iv : make_range(sub_elem_nodes.size()))
1071 : {
1072 20168 : const auto n = sub_elem_nodes[iv];
1073 20168 : nodes[iv] = secondary_side_elem->point(n);
1074 20168 : center += secondary_side_elem->point(n);
1075 20168 : normal += nodal_normals[n];
1076 : }
1077 5882 : center /= sub_elem_nodes.size();
1078 5882 : normal = normal.unit();
1079 :
1080 : // Build and store linearized sub-elements for later use
1081 5882 : mortar_segment_helper[sel] = std::make_unique<MortarSegmentHelper>(nodes, center, normal);
1082 5882 : }
1083 :
1084 : /**
1085 : * Step 1.2: Coarse screening using a k-d tree to find nodes on the primary interface that are
1086 : * 'close to' a center point of the secondary element.
1087 : */
1088 :
1089 : // Search point for performing Nanoflann (k-d tree) searches.
1090 : // In each case we use the center point of the original element (not sub-elements for second
1091 : // order elements). This is to do search for all sub-elements simultaneously
1092 : std::array<Real, 3> query_pt;
1093 2918 : Point center_point;
1094 2918 : switch (secondary_side_elem->type())
1095 : {
1096 2042 : case TRI3:
1097 : case QUAD4:
1098 2042 : center_point = mortar_segment_helper[0]->center();
1099 2042 : query_pt = {{center_point(0), center_point(1), center_point(2)}};
1100 2042 : break;
1101 384 : case TRI6:
1102 : case TRI7:
1103 384 : center_point = mortar_segment_helper[1]->center();
1104 384 : query_pt = {{center_point(0), center_point(1), center_point(2)}};
1105 384 : break;
1106 336 : case QUAD8:
1107 336 : center_point = mortar_segment_helper[4]->center();
1108 336 : query_pt = {{center_point(0), center_point(1), center_point(2)}};
1109 336 : break;
1110 156 : case QUAD9:
1111 156 : center_point = secondary_side_elem->point(8);
1112 156 : query_pt = {{center_point(0), center_point(1), center_point(2)}};
1113 156 : break;
1114 0 : default:
1115 0 : mooseError(
1116 0 : "Face element type: ", secondary_side_elem->type(), "not supported for 3D mortar");
1117 : }
1118 :
1119 : // The number of results we want to get. These results will only be used to find
1120 : // a single element with non-trivial overlap, after an element is identified a breadth
1121 : // first search is done on neighbors
1122 2918 : const std::size_t num_results = 3;
1123 :
1124 : // Initialize result_set and do the search.
1125 8754 : std::vector<size_t> ret_index(num_results);
1126 5836 : std::vector<Real> out_dist_sqr(num_results);
1127 2918 : nanoflann::KNNResultSet<Real> result_set(num_results);
1128 2918 : result_set.init(&ret_index[0], &out_dist_sqr[0]);
1129 2918 : kd_tree.findNeighbors(result_set, &query_pt[0], nanoflann::SearchParameters());
1130 :
1131 : // Initialize list of processed primary elements, we don't want to revisit processed elements
1132 5836 : std::set<const Elem *, CompareDofObjectsByID> processed_primary_elems;
1133 :
1134 : // Initialize candidate set and flag for switching between coarse screening and breadth-first
1135 : // search
1136 2918 : bool primary_elem_found = false;
1137 5836 : std::set<const Elem *, CompareDofObjectsByID> primary_elem_candidates;
1138 :
1139 : // Loop candidate nodes (returned by Nanoflann) and add all adjoining elems to candidate set
1140 11672 : for (auto r : make_range(result_set.size()))
1141 : {
1142 : // Verify that the squared distance we compute is the same as nanoflann's
1143 : mooseAssert(std::abs((_mesh.point(ret_index[r]) - center_point).norm_sq() -
1144 : out_dist_sqr[r]) <= TOLERANCE,
1145 : "Lower-dimensional element squared distance verification failed.");
1146 :
1147 : // Get list of elems connected to node
1148 : std::vector<const Elem *> & node_elems =
1149 8754 : this->_nodes_to_primary_elem_map.at(static_cast<dof_id_type>(ret_index[r]));
1150 :
1151 : // Uniquely add elems to candidate set
1152 40488 : for (auto elem : node_elems)
1153 31734 : primary_elem_candidates.insert(elem);
1154 : }
1155 :
1156 : /**
1157 : * Step 1.3: Loop through primary candidate nodes, create mortar segments
1158 : *
1159 : * Once an element with non-trivial projection onto secondary element identified, switch
1160 : * to breadth-first search (drop all current candidates and add only neighbors of elements
1161 : * with non-trivial overlap)
1162 : */
1163 29809 : while (!primary_elem_candidates.empty())
1164 : {
1165 26891 : const Elem * primary_elem_candidate = *primary_elem_candidates.begin();
1166 :
1167 : // If we've already processed this candidate, we don't need to check it again.
1168 26891 : if (processed_primary_elems.count(primary_elem_candidate))
1169 0 : continue;
1170 :
1171 : // Initialize set of nodes used to construct mortar segment elements
1172 26891 : std::vector<Point> nodal_points;
1173 :
1174 : // Initialize map from mortar segment elements to nodes
1175 26891 : std::vector<std::vector<unsigned int>> elem_to_node_map;
1176 :
1177 : // Initialize list of secondary and primary sub-elements that formed each mortar segment
1178 26891 : std::vector<std::pair<unsigned int, unsigned int>> sub_elem_map;
1179 :
1180 : /**
1181 : * Step 1.3.2: Sub-divide primary element candidate, then project onto secondary
1182 : * sub-elements, perform polygon clipping, and triangulate to form mortar segments
1183 : */
1184 80302 : for (auto p_el : make_range(primary_elem_candidate->n_sub_elem()))
1185 : {
1186 : // Get nodes of primary sub-elements
1187 53411 : auto sub_elem_nodes = get_sub_elem_nodes(primary_elem_candidate->type(), p_el);
1188 :
1189 : // Get list of primary sub-element vertex nodes
1190 53411 : std::vector<Point> primary_sub_elem(sub_elem_nodes.size());
1191 238841 : for (auto iv : make_range(sub_elem_nodes.size()))
1192 : {
1193 185430 : const auto n = sub_elem_nodes[iv];
1194 185430 : primary_sub_elem[iv] = primary_elem_candidate->point(n);
1195 : }
1196 :
1197 : // Loop through secondary sub-elements
1198 229510 : for (auto s_el : make_range(secondary_side_elem->n_sub_elem()))
1199 : {
1200 : // Mortar segment helpers were defined for each secondary sub-element, they will:
1201 : // 1. Project primary sub-element onto linearized secondary sub-element
1202 : // 2. Clip projected primary sub-element against secondary sub-element
1203 : // 3. Triangulate clipped polygon to form mortar segments
1204 : //
1205 : // Mortar segment helpers append a list of mortar segment nodes and connectivities that
1206 : // can be directly used to build mortar segments
1207 176099 : mortar_segment_helper[s_el]->getMortarSegments(
1208 : primary_sub_elem, nodal_points, elem_to_node_map);
1209 :
1210 : // Keep track of which secondary and primary sub-elements created segment
1211 239491 : for (auto i = sub_elem_map.size(); i < elem_to_node_map.size(); ++i)
1212 63392 : sub_elem_map.push_back(std::make_pair(s_el, p_el));
1213 : }
1214 53411 : }
1215 :
1216 : // Mark primary element as processed and remove from candidate list
1217 26891 : processed_primary_elems.insert(primary_elem_candidate);
1218 26891 : primary_elem_candidates.erase(primary_elem_candidate);
1219 :
1220 : // If overlap of polygons was non-trivial (created mortar segment elements)
1221 26891 : if (!elem_to_node_map.empty())
1222 : {
1223 : // If this is the first element with non-trivial overlap, set flag
1224 : // Candidates will now be neighbors of elements that had non-trivial overlap
1225 : // (i.e. we'll do a breadth first search now)
1226 9222 : if (!primary_elem_found)
1227 : {
1228 2918 : primary_elem_found = true;
1229 2918 : primary_elem_candidates.clear();
1230 : }
1231 :
1232 : // Add neighbors to candidate list
1233 45226 : for (auto neighbor : primary_elem_candidate->neighbor_ptr_range())
1234 : {
1235 : // If not valid or not on lower dimensional secondary subdomain, skip
1236 36004 : if (neighbor == nullptr || neighbor->subdomain_id() != primary_subd_id)
1237 3118 : continue;
1238 : // If already processed, skip
1239 32886 : if (processed_primary_elems.count(neighbor))
1240 11694 : continue;
1241 : // Otherwise, add to candidates
1242 21192 : primary_elem_candidates.insert(neighbor);
1243 : }
1244 :
1245 : /**
1246 : * Step 1.3.3: Create mortar segments and add to mortar segment mesh
1247 : */
1248 9222 : std::vector<Node *> new_nodes;
1249 101476 : for (auto pt : nodal_points)
1250 184508 : new_nodes.push_back(_mortar_segment_mesh->add_point(
1251 92254 : pt, _mortar_segment_mesh->max_node_id(), secondary_side_elem->processor_id()));
1252 :
1253 : // Loop through triangular elements in map
1254 72614 : for (auto el : index_range(elem_to_node_map))
1255 : {
1256 : // Create new triangular element
1257 63392 : std::unique_ptr<Elem> new_elem;
1258 63392 : if (elem_to_node_map[el].size() == 3)
1259 63392 : new_elem = std::make_unique<Tri3>();
1260 : else
1261 0 : mooseError("Active mortar segments only supports TRI elements, 3 nodes expected "
1262 : "but: ",
1263 0 : elem_to_node_map[el].size(),
1264 : " provided.");
1265 :
1266 63392 : new_elem->processor_id() = secondary_side_elem->processor_id();
1267 63392 : new_elem->subdomain_id() = secondary_side_elem->subdomain_id();
1268 63392 : new_elem->set_id(local_id_index++);
1269 :
1270 : // Attach newly created nodes
1271 253568 : for (auto i : index_range(elem_to_node_map[el]))
1272 190176 : new_elem->set_node(i, new_nodes[elem_to_node_map[el][i]]);
1273 :
1274 : // If element is smaller than tolerance, don't add to msm
1275 63392 : if (new_elem->volume() / secondary_volume < TOLERANCE)
1276 276 : continue;
1277 :
1278 : // Add elements to mortar segment mesh
1279 63116 : Elem * msm_new_elem = _mortar_segment_mesh->add_elem(new_elem.release());
1280 :
1281 63116 : msm_new_elem->set_extra_integer(secondary_sub_elem, sub_elem_map[el].first);
1282 63116 : msm_new_elem->set_extra_integer(primary_sub_elem, sub_elem_map[el].second);
1283 :
1284 : // Fill out mortar segment info
1285 63116 : MortarSegmentInfo msinfo;
1286 63116 : msinfo.secondary_elem = secondary_side_elem;
1287 63116 : msinfo.primary_elem = primary_elem_candidate;
1288 :
1289 : // Associate this MSM elem with the MortarSegmentInfo.
1290 63116 : _msm_elem_to_info.emplace(msm_new_elem, msinfo);
1291 :
1292 : // Add this mortar segment to the secondary elem to mortar segment map
1293 63116 : secondary_to_msm_element_set.insert(msm_new_elem);
1294 :
1295 63116 : _secondary_ip_sub_ids.insert(msinfo.secondary_elem->interior_parent()->subdomain_id());
1296 : // Unlike for 2D, we always have a primary when building the mortar mesh so we don't
1297 : // have to check for null
1298 63116 : _primary_ip_sub_ids.insert(msinfo.primary_elem->interior_parent()->subdomain_id());
1299 63392 : }
1300 9222 : }
1301 : // End loop through primary element candidates
1302 26891 : }
1303 :
1304 8800 : for (auto sel : make_range(secondary_side_elem->n_sub_elem()))
1305 : {
1306 : // Check if any segments failed to project
1307 5882 : if (mortar_segment_helper[sel]->remainder() == 1.0)
1308 0 : mooseDoOnce(
1309 : mooseWarning("Some secondary elements on mortar interface were unable to identify"
1310 : " a corresponding primary element; this may be expected depending on"
1311 : " problem geometry but may indicate a failure of the element search"
1312 : " or projection"));
1313 : }
1314 :
1315 2918 : if (secondary_to_msm_element_set.empty())
1316 0 : _secondary_elems_to_mortar_segments.erase(secondary_elem_to_msm_map_it);
1317 3136 : } // End loop through secondary elements
1318 218 : } // End loop through mortar constraint pairs
1319 :
1320 218 : _mortar_segment_mesh->cache_elem_data();
1321 :
1322 : // Output mortar segment mesh
1323 218 : if (_debug)
1324 : {
1325 : // If element is not triangular, increment subdomain id
1326 : // (ExodusII does not support mixed element types in a single subdomain)
1327 2138 : for (const auto msm_el : _mortar_segment_mesh->active_local_element_ptr_range())
1328 2136 : if (msm_el->type() != TRI3)
1329 2 : msm_el->subdomain_id()++;
1330 :
1331 2 : ExodusII_IO mortar_segment_mesh_writer(*_mortar_segment_mesh);
1332 :
1333 : // Default to non-HDF5 output for wider compatibility
1334 2 : mortar_segment_mesh_writer.set_hdf5_writing(false);
1335 :
1336 2 : mortar_segment_mesh_writer.write("mortar_segment_mesh.e");
1337 :
1338 : // Undo increment
1339 2138 : for (const auto msm_el : _mortar_segment_mesh->active_local_element_ptr_range())
1340 2136 : if (msm_el->type() != TRI3)
1341 2 : msm_el->subdomain_id()--;
1342 2 : }
1343 :
1344 218 : buildCouplingInformation();
1345 :
1346 : // Print mortar segment mesh statistics
1347 218 : if (_debug)
1348 : {
1349 2 : if (_mesh.n_processors() == 1)
1350 2 : msmStatistics();
1351 : else
1352 0 : mooseWarning("Mortar segment mesh statistics intended for debugging purposes in serial only, "
1353 : "parallel will only provide statistics for local mortar segment mesh.");
1354 : }
1355 218 : }
1356 :
1357 : void
1358 4937 : AutomaticMortarGeneration::buildCouplingInformation()
1359 : {
1360 : std::unordered_map<processor_id_type, std::vector<std::pair<dof_id_type, dof_id_type>>>
1361 4937 : coupling_info;
1362 :
1363 : // Loop over the msm_elem_to_info object and build a bi-directional
1364 : // multimap from secondary elements to the primary Elems which they are
1365 : // coupled to and vice-versa. This is used in the
1366 : // AugmentSparsityOnInterface functor to determine whether a given
1367 : // secondary Elem is coupled across the mortar interface to a primary
1368 : // element.
1369 100406 : for (const auto & pr : _msm_elem_to_info)
1370 : {
1371 95469 : const Elem * secondary_elem = pr.second.secondary_elem;
1372 95469 : const Elem * primary_elem = pr.second.primary_elem;
1373 :
1374 : // LowerSecondary
1375 95469 : coupling_info[secondary_elem->processor_id()].emplace_back(
1376 95469 : secondary_elem->id(), secondary_elem->interior_parent()->id());
1377 95469 : if (secondary_elem->processor_id() != _mesh.processor_id())
1378 : // We want to keep information for nonlocal lower-dimensional secondary element point
1379 : // neighbors for mortar nodal aux kernels
1380 7859 : _mortar_interface_coupling[secondary_elem->id()].insert(
1381 7859 : secondary_elem->interior_parent()->id());
1382 :
1383 : // LowerPrimary
1384 95469 : coupling_info[secondary_elem->processor_id()].emplace_back(
1385 95469 : secondary_elem->id(), primary_elem->interior_parent()->id());
1386 95469 : if (secondary_elem->processor_id() != _mesh.processor_id())
1387 : // We want to keep information for nonlocal lower-dimensional secondary element point
1388 : // neighbors for mortar nodal aux kernels
1389 7859 : _mortar_interface_coupling[secondary_elem->id()].insert(
1390 7859 : primary_elem->interior_parent()->id());
1391 :
1392 : // Lower-LowerDimensionalPrimary
1393 190938 : coupling_info[secondary_elem->processor_id()].emplace_back(secondary_elem->id(),
1394 95469 : primary_elem->id());
1395 95469 : if (secondary_elem->processor_id() != _mesh.processor_id())
1396 : // We want to keep information for nonlocal lower-dimensional secondary element point
1397 : // neighbors for mortar nodal aux kernels
1398 7859 : _mortar_interface_coupling[secondary_elem->id()].insert(primary_elem->id());
1399 :
1400 : // SecondaryLower
1401 95469 : coupling_info[secondary_elem->interior_parent()->processor_id()].emplace_back(
1402 95469 : secondary_elem->interior_parent()->id(), secondary_elem->id());
1403 :
1404 : // SecondaryPrimary
1405 95469 : coupling_info[secondary_elem->interior_parent()->processor_id()].emplace_back(
1406 95469 : secondary_elem->interior_parent()->id(), primary_elem->interior_parent()->id());
1407 :
1408 : // PrimaryLower
1409 95469 : coupling_info[primary_elem->interior_parent()->processor_id()].emplace_back(
1410 95469 : primary_elem->interior_parent()->id(), secondary_elem->id());
1411 :
1412 : // PrimarySecondary
1413 95469 : coupling_info[primary_elem->interior_parent()->processor_id()].emplace_back(
1414 95469 : primary_elem->interior_parent()->id(), secondary_elem->interior_parent()->id());
1415 : }
1416 :
1417 : // Push the coupling information
1418 : auto action_functor =
1419 7386 : [this](processor_id_type,
1420 : const std::vector<std::pair<dof_id_type, dof_id_type>> & coupling_info)
1421 : {
1422 675669 : for (auto [i, j] : coupling_info)
1423 668283 : _mortar_interface_coupling[i].insert(j);
1424 7386 : };
1425 4937 : TIMPI::push_parallel_vector_data(_mesh.comm(), coupling_info, action_functor);
1426 4937 : }
1427 :
1428 : void
1429 2 : AutomaticMortarGeneration::msmStatistics()
1430 : {
1431 : // Print boundary pairs
1432 2 : Moose::out << "Mortar Interface Statistics:" << std::endl;
1433 :
1434 : // Count number of elements on primary and secondary sides
1435 4 : for (const auto & pr : _primary_secondary_subdomain_id_pairs)
1436 : {
1437 2 : const auto primary_subd_id = pr.first;
1438 2 : const auto secondary_subd_id = pr.second;
1439 :
1440 : // Allocate statistics vectors for primary lower, secondary lower, and msm meshes
1441 2 : StatisticsVector<Real> primary; // primary.reserve(mesh.n_elem());
1442 2 : StatisticsVector<Real> secondary; // secondary.reserve(mesh.n_elem());
1443 2 : StatisticsVector<Real> msm; // msm.reserve(mortar_segment_mesh->n_elem());
1444 :
1445 470 : for (auto * el : _mesh.active_element_ptr_range())
1446 : {
1447 : // Add secondary and primary elem volumes to statistics vector
1448 468 : if (el->subdomain_id() == secondary_subd_id)
1449 24 : secondary.push_back(el->volume());
1450 444 : else if (el->subdomain_id() == primary_subd_id)
1451 54 : primary.push_back(el->volume());
1452 2 : }
1453 :
1454 : // Note: when we allow more than one primary secondary pair will need to make
1455 : // separate mortar segment mesh for each
1456 2138 : for (auto msm_elem : _mortar_segment_mesh->active_local_element_ptr_range())
1457 : {
1458 : // Add msm elem volume to statistic vector
1459 2136 : msm.push_back(msm_elem->volume());
1460 2 : }
1461 :
1462 : // Create table
1463 4 : std::vector<std::string> col_names = {"mesh", "n_elems", "max", "min", "median"};
1464 4 : std::vector<std::string> subds = {"secondary_lower", "primary_lower", "mortar_segment"};
1465 4 : std::vector<size_t> n_elems = {secondary.size(), primary.size(), msm.size()};
1466 4 : std::vector<Real> maxs = {secondary.maximum(), primary.maximum(), msm.maximum()};
1467 4 : std::vector<Real> mins = {secondary.minimum(), primary.minimum(), msm.minimum()};
1468 4 : std::vector<Real> medians = {secondary.median(), primary.median(), msm.median()};
1469 :
1470 2 : FormattedTable table;
1471 2 : table.clear();
1472 8 : for (auto i : index_range(subds))
1473 : {
1474 6 : table.addRow(i);
1475 6 : table.addData<std::string>(col_names[0], subds[i]);
1476 6 : table.addData<size_t>(col_names[1], n_elems[i]);
1477 6 : table.addData<Real>(col_names[2], maxs[i]);
1478 6 : table.addData<Real>(col_names[3], mins[i]);
1479 6 : table.addData<Real>(col_names[4], medians[i]);
1480 : }
1481 :
1482 2 : Moose::out << "secondary subdomain: " << secondary_subd_id
1483 2 : << " \tprimary subdomain: " << primary_subd_id << std::endl;
1484 2 : table.printTable(Moose::out, subds.size());
1485 2 : }
1486 2 : }
1487 :
1488 : // The blocks marked with **** are for regressing edge dropping treatment and should be removed
1489 : // eventually.
1490 : //****
1491 : // Compute inactve nodes when the old (incorrect) edge dropping treatemnt is enabled
1492 : void
1493 855 : AutomaticMortarGeneration::computeIncorrectEdgeDroppingInactiveLMNodes()
1494 : {
1495 : // Note that in 3D our trick to check whether an element has edge dropping needs loose tolerances
1496 : // since the mortar segments are on the linearized element and comparing the volume of the
1497 : // linearized element does not have the same volume as the warped element
1498 855 : const Real tol = (dim() == 3) ? 0.1 : TOLERANCE;
1499 :
1500 855 : std::unordered_map<processor_id_type, std::set<dof_id_type>> proc_to_inactive_nodes_set;
1501 855 : const auto my_pid = _mesh.processor_id();
1502 :
1503 : // List of inactive nodes on local secondary elements
1504 855 : std::unordered_set<dof_id_type> inactive_node_ids;
1505 :
1506 855 : std::unordered_map<const Elem *, Real> active_volume{};
1507 :
1508 1710 : for (const auto & pr : _primary_secondary_subdomain_id_pairs)
1509 7289 : for (const auto el : _mesh.active_subdomain_elements_ptr_range(pr.second))
1510 7289 : active_volume[el] = 0.;
1511 :
1512 : // Compute fraction of elements with corresponding primary elements
1513 9853 : for (const auto msm_elem : _mortar_segment_mesh->active_local_element_ptr_range())
1514 : {
1515 8998 : const MortarSegmentInfo & msinfo = _msm_elem_to_info.at(msm_elem);
1516 8998 : const Elem * secondary_elem = msinfo.secondary_elem;
1517 :
1518 8998 : active_volume[secondary_elem] += msm_elem->volume();
1519 855 : }
1520 :
1521 : // Mark all inactive local nodes
1522 1710 : for (const auto & pr : _primary_secondary_subdomain_id_pairs)
1523 : // Loop through all elements on my processor
1524 11553 : for (const auto el : _mesh.active_local_subdomain_elements_ptr_range(pr.second))
1525 : // If elem fully or partially dropped
1526 5349 : if (std::abs(active_volume[el] / el->volume() - 1.0) > tol)
1527 : {
1528 : // Add all nodes to list of inactive
1529 0 : for (auto n : make_range(el->n_nodes()))
1530 0 : inactive_node_ids.insert(el->node_id(n));
1531 855 : }
1532 :
1533 : // Assemble list of procs that nodes contribute to
1534 1710 : for (const auto & pr : _primary_secondary_subdomain_id_pairs)
1535 : {
1536 855 : const auto secondary_subd_id = pr.second;
1537 :
1538 : // Loop through all elements not on my processor
1539 13723 : for (const auto el : _mesh.active_subdomain_elements_ptr_range(secondary_subd_id))
1540 : {
1541 : // Get processor_id
1542 6434 : const auto pid = el->processor_id();
1543 :
1544 : // If element is in my subdomain, skip
1545 6434 : if (pid == my_pid)
1546 5349 : continue;
1547 :
1548 : // If element on proc pid shares any of my inactive nodes, mark to send
1549 4638 : for (const auto n : make_range(el->n_nodes()))
1550 : {
1551 3553 : const auto node_id = el->node_id(n);
1552 3553 : if (inactive_node_ids.find(node_id) != inactive_node_ids.end())
1553 0 : proc_to_inactive_nodes_set[pid].insert(node_id);
1554 : }
1555 855 : }
1556 : }
1557 :
1558 : // Send list of inactive nodes
1559 : {
1560 : // Pack set into vector for sending (push_parallel_vector_data doesn't like sets)
1561 855 : std::unordered_map<processor_id_type, std::vector<dof_id_type>> proc_to_inactive_nodes_vector;
1562 855 : for (const auto & proc_set : proc_to_inactive_nodes_set)
1563 0 : proc_to_inactive_nodes_vector[proc_set.first].insert(
1564 0 : proc_to_inactive_nodes_vector[proc_set.first].end(),
1565 : proc_set.second.begin(),
1566 : proc_set.second.end());
1567 :
1568 : // First push data
1569 0 : auto action_functor = [this, &inactive_node_ids](const processor_id_type pid,
1570 : const std::vector<dof_id_type> & sent_data)
1571 : {
1572 0 : if (pid == _mesh.processor_id())
1573 0 : mooseError("Should not be communicating with self.");
1574 0 : for (const auto pr : sent_data)
1575 0 : inactive_node_ids.insert(pr);
1576 0 : };
1577 855 : TIMPI::push_parallel_vector_data(_mesh.comm(), proc_to_inactive_nodes_vector, action_functor);
1578 855 : }
1579 855 : _inactive_local_lm_nodes.clear();
1580 855 : for (const auto node_id : inactive_node_ids)
1581 0 : _inactive_local_lm_nodes.insert(_mesh.node_ptr(node_id));
1582 855 : }
1583 :
1584 : void
1585 4937 : AutomaticMortarGeneration::computeInactiveLMNodes()
1586 : {
1587 4937 : if (!_correct_edge_dropping)
1588 : {
1589 855 : computeIncorrectEdgeDroppingInactiveLMNodes();
1590 855 : return;
1591 : }
1592 :
1593 4082 : std::unordered_map<processor_id_type, std::set<dof_id_type>> proc_to_active_nodes_set;
1594 4082 : const auto my_pid = _mesh.processor_id();
1595 :
1596 : // List of active nodes on local secondary elements
1597 4082 : std::unordered_set<dof_id_type> active_local_nodes;
1598 :
1599 : // Mark all active local nodes
1600 161306 : for (const auto msm_elem : _mortar_segment_mesh->active_local_element_ptr_range())
1601 : {
1602 78612 : const MortarSegmentInfo & msinfo = _msm_elem_to_info.at(msm_elem);
1603 78612 : const Elem * secondary_elem = msinfo.secondary_elem;
1604 :
1605 509716 : for (auto n : make_range(secondary_elem->n_nodes()))
1606 431104 : active_local_nodes.insert(secondary_elem->node_id(n));
1607 4082 : }
1608 :
1609 : // Assemble list of procs that nodes contribute to
1610 8164 : for (const auto & pr : _primary_secondary_subdomain_id_pairs)
1611 : {
1612 4082 : const auto secondary_subd_id = pr.second;
1613 :
1614 : // Loop through all elements not on my processor
1615 44462 : for (const auto el : _mesh.active_subdomain_elements_ptr_range(secondary_subd_id))
1616 : {
1617 : // Get processor_id
1618 20190 : const auto pid = el->processor_id();
1619 :
1620 : // If element is in my subdomain, skip
1621 20190 : if (pid == my_pid)
1622 14723 : continue;
1623 :
1624 : // If element on proc pid shares any of my active nodes, mark to send
1625 19813 : for (const auto n : make_range(el->n_nodes()))
1626 : {
1627 14346 : const auto node_id = el->node_id(n);
1628 14346 : if (active_local_nodes.find(node_id) != active_local_nodes.end())
1629 354 : proc_to_active_nodes_set[pid].insert(node_id);
1630 : }
1631 4082 : }
1632 : }
1633 :
1634 : // Send list of active nodes
1635 : {
1636 : // Pack set into vector for sending (push_parallel_vector_data doesn't like sets)
1637 4082 : std::unordered_map<processor_id_type, std::vector<dof_id_type>> proc_to_active_nodes_vector;
1638 4256 : for (const auto & proc_set : proc_to_active_nodes_set)
1639 : {
1640 174 : proc_to_active_nodes_vector[proc_set.first].reserve(proc_to_active_nodes_set.size());
1641 470 : for (const auto node_id : proc_set.second)
1642 296 : proc_to_active_nodes_vector[proc_set.first].push_back(node_id);
1643 : }
1644 :
1645 : // First push data
1646 174 : auto action_functor = [this, &active_local_nodes](const processor_id_type pid,
1647 : const std::vector<dof_id_type> & sent_data)
1648 : {
1649 174 : if (pid == _mesh.processor_id())
1650 0 : mooseError("Should not be communicating with self.");
1651 174 : active_local_nodes.insert(sent_data.begin(), sent_data.end());
1652 4256 : };
1653 4082 : TIMPI::push_parallel_vector_data(_mesh.comm(), proc_to_active_nodes_vector, action_functor);
1654 4082 : }
1655 :
1656 : // Every proc has correct list of active local nodes, now take complement (list of inactive nodes)
1657 : // and store to use later to zero LM DoFs on inactive nodes
1658 4082 : _inactive_local_lm_nodes.clear();
1659 8164 : for (const auto & pr : _primary_secondary_subdomain_id_pairs)
1660 4082 : for (const auto el : _mesh.active_local_subdomain_elements_ptr_range(
1661 37610 : /*secondary_subd_id*/ pr.second))
1662 49873 : for (const auto n : make_range(el->n_nodes()))
1663 35150 : if (active_local_nodes.find(el->node_id(n)) == active_local_nodes.end())
1664 4545 : _inactive_local_lm_nodes.insert(el->node_ptr(n));
1665 4082 : }
1666 :
1667 : // Note: could be combined with previous routine, keeping separate for clarity (for now)
1668 : void
1669 4937 : AutomaticMortarGeneration::computeInactiveLMElems()
1670 : {
1671 : // Mark all active secondary elements
1672 4937 : std::unordered_set<const Elem *> active_local_elems;
1673 :
1674 : //****
1675 : // Note that in 3D our trick to check whether an element has edge dropping needs loose tolerances
1676 : // since the mortar segments are on the linearized element and comparing the volume of the
1677 : // linearized element does not have the same volume as the warped element
1678 4937 : const Real tol = (dim() == 3) ? 0.1 : TOLERANCE;
1679 :
1680 4937 : std::unordered_map<const Elem *, Real> active_volume;
1681 :
1682 : // Compute fraction of elements with corresponding primary elements
1683 4937 : if (!_correct_edge_dropping)
1684 9853 : for (const auto msm_elem : _mortar_segment_mesh->active_local_element_ptr_range())
1685 : {
1686 8998 : const MortarSegmentInfo & msinfo = _msm_elem_to_info.at(msm_elem);
1687 8998 : const Elem * secondary_elem = msinfo.secondary_elem;
1688 :
1689 8998 : active_volume[secondary_elem] += msm_elem->volume();
1690 855 : }
1691 : //****
1692 :
1693 180157 : for (const auto msm_elem : _mortar_segment_mesh->active_local_element_ptr_range())
1694 : {
1695 87610 : const MortarSegmentInfo & msinfo = _msm_elem_to_info.at(msm_elem);
1696 87610 : const Elem * secondary_elem = msinfo.secondary_elem;
1697 :
1698 : //****
1699 87610 : if (!_correct_edge_dropping)
1700 8998 : if (std::abs(active_volume[secondary_elem] / secondary_elem->volume() - 1.0) > tol)
1701 0 : continue;
1702 : //****
1703 :
1704 87610 : active_local_elems.insert(secondary_elem);
1705 4937 : }
1706 :
1707 : // Take complement of active elements in active local subdomain to get inactive local elements
1708 4937 : _inactive_local_lm_elems.clear();
1709 9874 : for (const auto & pr : _primary_secondary_subdomain_id_pairs)
1710 4937 : for (const auto el : _mesh.active_local_subdomain_elements_ptr_range(
1711 50018 : /*secondary_subd_id*/ pr.second))
1712 20072 : if (active_local_elems.find(el) == active_local_elems.end())
1713 5215 : _inactive_local_lm_elems.insert(el);
1714 4937 : }
1715 :
1716 : void
1717 4937 : AutomaticMortarGeneration::computeNodalGeometry()
1718 : {
1719 : // The dimension according to Mesh::mesh_dimension().
1720 4937 : const auto dim = _mesh.mesh_dimension();
1721 :
1722 : // A nodal lower-dimensional nodal quadrature rule to be used on faces.
1723 4937 : QNodal qface(dim - 1);
1724 :
1725 : // A map from the node id to the attached elemental normals/weights evaluated at the node. Th
1726 : // length of the vector will correspond to the number of elements attached to the node. If it is a
1727 : // vertex node, for a 1D mortar mesh, the vector length will be two. If it is an interior node,
1728 : // the vector will be length 1. The first member of the pair is that element's normal at the node.
1729 : // The second member is that element's JxW at the node
1730 4937 : std::map<dof_id_type, std::vector<std::pair<Point, Real>>> node_to_normals_map;
1731 :
1732 : /// The _periodic flag tells us whether we want to inward vs outward facing normals
1733 4937 : Real sign = _periodic ? -1 : 1;
1734 :
1735 : // First loop over lower-dimensional secondary side elements and compute/save the outward normal
1736 : // for each one. We loop over all active elements currently, but this procedure could be
1737 : // parallelized as well.
1738 4937 : for (MeshBase::const_element_iterator el = _mesh.active_elements_begin(),
1739 4937 : end_el = _mesh.active_elements_end();
1740 500443 : el != end_el;
1741 495506 : ++el)
1742 : {
1743 495506 : const Elem * secondary_elem = *el;
1744 :
1745 : // If this is not one of the lower-dimensional secondary side elements, go on to the next one.
1746 495506 : if (!_secondary_boundary_subdomain_ids.count(secondary_elem->subdomain_id()))
1747 468882 : continue;
1748 :
1749 : // We will create an FE object and attach the nodal quadrature rule such that we can get out the
1750 : // normals at the element nodes
1751 26624 : FEType nnx_fe_type(secondary_elem->default_order(), LAGRANGE);
1752 26624 : std::unique_ptr<FEBase> nnx_fe_face(FEBase::build(dim, nnx_fe_type));
1753 26624 : nnx_fe_face->attach_quadrature_rule(&qface);
1754 26624 : const std::vector<Point> & face_normals = nnx_fe_face->get_normals();
1755 :
1756 26624 : const auto & JxW = nnx_fe_face->get_JxW();
1757 :
1758 : // Which side of the parent are we? We need to know this to know
1759 : // which side to reinit.
1760 26624 : const Elem * interior_parent = secondary_elem->interior_parent();
1761 : mooseAssert(interior_parent,
1762 : "No interior parent exists for element "
1763 : << secondary_elem->id()
1764 : << ". There may be a problem with your sideset set-up.");
1765 :
1766 : // Map to get lower dimensional element from interior parent on secondary surface
1767 : // This map can be used to provide a handle to methods in this class that need to
1768 : // operate on lower dimensional elements.
1769 26624 : _secondary_element_to_secondary_lowerd_element.emplace(interior_parent->id(), secondary_elem);
1770 :
1771 : // Look up which side of the interior parent secondary_elem is.
1772 26624 : auto s = interior_parent->which_side_am_i(secondary_elem);
1773 :
1774 : // Reinit the face FE object on side s.
1775 26624 : nnx_fe_face->reinit(interior_parent, s);
1776 :
1777 94824 : for (MooseIndex(secondary_elem->n_nodes()) n = 0; n < secondary_elem->n_nodes(); ++n)
1778 : {
1779 68200 : auto & normals_and_weights_vec = node_to_normals_map[secondary_elem->node_id(n)];
1780 68200 : normals_and_weights_vec.push_back(std::make_pair(sign * face_normals[n], JxW[n]));
1781 : }
1782 31561 : }
1783 :
1784 : // Note that contrary to the Bin Yang dissertation, we are not weighting by the face element
1785 : // lengths/volumes. It's not clear to me that this type of weighting is a good algorithm for cases
1786 : // where the face can be curved
1787 43173 : for (const auto & pr : node_to_normals_map)
1788 : {
1789 : // Compute normal vector
1790 38236 : const auto & node_id = pr.first;
1791 38236 : const auto & normals_and_weights_vec = pr.second;
1792 :
1793 38236 : Point nodal_normal;
1794 106436 : for (const auto & norm_and_weight : normals_and_weights_vec)
1795 68200 : nodal_normal += norm_and_weight.first * norm_and_weight.second;
1796 38236 : nodal_normal = nodal_normal.unit();
1797 :
1798 38236 : _secondary_node_to_nodal_normal[_mesh.node_ptr(node_id)] = nodal_normal;
1799 :
1800 38236 : Point nodal_tangent_one;
1801 38236 : Point nodal_tangent_two;
1802 38236 : householderOrthogolization(nodal_normal, nodal_tangent_one, nodal_tangent_two);
1803 :
1804 38236 : _secondary_node_to_hh_nodal_tangents[_mesh.node_ptr(node_id)][0] = nodal_tangent_one;
1805 38236 : _secondary_node_to_hh_nodal_tangents[_mesh.node_ptr(node_id)][1] = nodal_tangent_two;
1806 : }
1807 4937 : }
1808 :
1809 : void
1810 38236 : AutomaticMortarGeneration::householderOrthogolization(const Point & nodal_normal,
1811 : Point & nodal_tangent_one,
1812 : Point & nodal_tangent_two) const
1813 : {
1814 : mooseAssert(MooseUtils::absoluteFuzzyEqual(nodal_normal.norm(), 1),
1815 : "The input nodal normal should have unity norm");
1816 :
1817 38236 : const Real nx = nodal_normal(0);
1818 38236 : const Real ny = nodal_normal(1);
1819 38236 : const Real nz = nodal_normal(2);
1820 :
1821 : // See Lopes DS, Silva MT, Ambrosio JA. Tangent vectors to a 3-D surface normal: A geometric tool
1822 : // to find orthogonal vectors based on the Householder transformation. Computer-Aided Design. 2013
1823 : // Mar 1;45(3):683-94. We choose one definition of h_vector and deal with special case.
1824 38236 : const Point h_vector(nx + 1.0, ny, nz);
1825 :
1826 : // Avoid singularity of the equations at the end of routine by providing the solution to
1827 : // (nx,ny,nz)=(-1,0,0) Normal/tangent fields can be visualized by outputting nodal geometry mesh
1828 : // on a spherical problem.
1829 38236 : if (std::abs(h_vector(0)) < TOLERANCE)
1830 : {
1831 1995 : nodal_tangent_one(0) = 0;
1832 1995 : nodal_tangent_one(1) = 1;
1833 1995 : nodal_tangent_one(2) = 0;
1834 :
1835 1995 : nodal_tangent_two(0) = 0;
1836 1995 : nodal_tangent_two(1) = 0;
1837 1995 : nodal_tangent_two(2) = -1;
1838 :
1839 1995 : return;
1840 : }
1841 :
1842 36241 : const Real h = h_vector.norm();
1843 :
1844 36241 : nodal_tangent_one(0) = -2.0 * h_vector(0) * h_vector(1) / (h * h);
1845 36241 : nodal_tangent_one(1) = 1.0 - 2.0 * h_vector(1) * h_vector(1) / (h * h);
1846 36241 : nodal_tangent_one(2) = -2.0 * h_vector(1) * h_vector(2) / (h * h);
1847 :
1848 36241 : nodal_tangent_two(0) = -2.0 * h_vector(0) * h_vector(2) / (h * h);
1849 36241 : nodal_tangent_two(1) = -2.0 * h_vector(1) * h_vector(2) / (h * h);
1850 36241 : nodal_tangent_two(2) = 1.0 - 2.0 * h_vector(2) * h_vector(2) / (h * h);
1851 : }
1852 :
1853 : // Project secondary nodes onto their corresponding primary elements for each primary/secondary
1854 : // pair.
1855 : void
1856 4719 : AutomaticMortarGeneration::projectSecondaryNodes()
1857 : {
1858 : // For each primary/secondary boundary id pair, call the
1859 : // project_secondary_nodes_single_pair() helper function.
1860 9438 : for (const auto & pr : _primary_secondary_subdomain_id_pairs)
1861 4719 : projectSecondaryNodesSinglePair(pr.first, pr.second);
1862 4719 : }
1863 :
1864 : bool
1865 6576 : AutomaticMortarGeneration::processAlignedNodes(
1866 : const Node & secondary_node,
1867 : const Node & primary_node,
1868 : const std::vector<const Elem *> * secondary_node_neighbors,
1869 : const std::vector<const Elem *> * primary_node_neighbors,
1870 : const VectorValue<Real> & nodal_normal,
1871 : const Elem & candidate_element,
1872 : std::set<const Elem *> & rejected_elem_candidates)
1873 : {
1874 6576 : if (!secondary_node_neighbors)
1875 0 : secondary_node_neighbors = &libmesh_map_find(_nodes_to_secondary_elem_map, secondary_node.id());
1876 6576 : if (!primary_node_neighbors)
1877 6576 : primary_node_neighbors = &libmesh_map_find(_nodes_to_primary_elem_map, primary_node.id());
1878 :
1879 6576 : std::vector<bool> primary_elems_mapped(primary_node_neighbors->size(), false);
1880 :
1881 : // Add entries to secondary_node_and_elem_to_xi2_primary_elem container.
1882 : //
1883 : // First, determine "on left" vs. "on right" orientation of the nodal neighbors.
1884 : // There can be a max of 2 nodal neighbors, and we want to make sure that the
1885 : // secondary nodal neighbor on the "left" is associated with the primary nodal
1886 : // neighbor on the "left" and similarly for the "right". We use cross products to determine
1887 : // alignment. In the below diagram, 'x' denotes a node, and connected '|' are lower dimensional
1888 : // elements.
1889 : // x
1890 : // x |
1891 : // | |
1892 : // secondary x ----> x primary
1893 : // | |
1894 : // | x
1895 : // x
1896 : //
1897 : // Looking at the aligned nodes, the secondary node first, if we pick the top secondary lower
1898 : // dimensional element, then the cross product as written a few lines below points out of the
1899 : // screen towards you. (Point in the direction of the secondary nodal normal, and then curl your
1900 : // hand towards the secondary element's opposite node, then the thumb points in the direction of
1901 : // the cross product). Doing the same with the aligned primary node, if we pick the top primary
1902 : // element, then the cross product also points out of the screen. Because the cross products
1903 : // point in the same direction (positive dot product), then we know to associate the
1904 : // secondary-primary element pair. If we had picked the bottom primary element whose cross
1905 : // product points into the screen, then clearly the cross products point in the opposite
1906 : // direction and we don't have a match
1907 : std::array<Real, 2> secondary_node_neighbor_cps, primary_node_neighbor_cps;
1908 :
1909 17095 : for (const auto nn : index_range(*secondary_node_neighbors))
1910 : {
1911 10519 : const Elem * const secondary_neigh = (*secondary_node_neighbors)[nn];
1912 10519 : const Point opposite = (secondary_neigh->node_ptr(0) == &secondary_node)
1913 10519 : ? secondary_neigh->point(1)
1914 5264 : : secondary_neigh->point(0);
1915 10519 : const Point cp = nodal_normal.cross(opposite - secondary_node);
1916 10519 : secondary_node_neighbor_cps[nn] = cp(2);
1917 : }
1918 :
1919 16849 : for (const auto nn : index_range(*primary_node_neighbors))
1920 : {
1921 10273 : const Elem * const primary_neigh = (*primary_node_neighbors)[nn];
1922 10273 : const Point opposite = (primary_neigh->node_ptr(0) == &primary_node) ? primary_neigh->point(1)
1923 5264 : : primary_neigh->point(0);
1924 10273 : const Point cp = nodal_normal.cross(opposite - primary_node);
1925 10273 : primary_node_neighbor_cps[nn] = cp(2);
1926 : }
1927 :
1928 : // Associate secondary/primary elems on matching sides.
1929 6576 : bool found_match = false;
1930 17095 : for (const auto snn : index_range(*secondary_node_neighbors))
1931 28432 : for (const auto mnn : index_range(*primary_node_neighbors))
1932 17913 : if (secondary_node_neighbor_cps[snn] * primary_node_neighbor_cps[mnn] > 0)
1933 : {
1934 10260 : found_match = true;
1935 10260 : if (primary_elems_mapped[mnn])
1936 0 : continue;
1937 10260 : primary_elems_mapped[mnn] = true;
1938 :
1939 : // Figure out xi^(2) value by looking at which node primary_node is
1940 : // of the current primary node neighbor.
1941 10260 : const Real xi2 = (&primary_node == (*primary_node_neighbors)[mnn]->node_ptr(0)) ? -1 : +1;
1942 : const auto secondary_key =
1943 10260 : std::make_pair(&secondary_node, (*secondary_node_neighbors)[snn]);
1944 10260 : const auto primary_val = std::make_pair(xi2, (*primary_node_neighbors)[mnn]);
1945 10260 : _secondary_node_and_elem_to_xi2_primary_elem.emplace(secondary_key, primary_val);
1946 :
1947 : // Also map in the other direction.
1948 : const Real xi1 =
1949 10260 : (&secondary_node == (*secondary_node_neighbors)[snn]->node_ptr(0)) ? -1 : +1;
1950 :
1951 : const auto primary_key =
1952 10260 : std::make_tuple(primary_node.id(), &primary_node, (*primary_node_neighbors)[mnn]);
1953 10260 : const auto secondary_val = std::make_pair(xi1, (*secondary_node_neighbors)[snn]);
1954 10260 : _primary_node_and_elem_to_xi1_secondary_elem.emplace(primary_key, secondary_val);
1955 : }
1956 :
1957 6576 : if (!found_match)
1958 : {
1959 : // There could be coincident nodes and this might be a bad primary candidate (see
1960 : // issue #21680). Instead of giving up, let's try continuing
1961 13 : rejected_elem_candidates.insert(&candidate_element);
1962 13 : return false;
1963 : }
1964 :
1965 : // We need to handle the case where we've exactly projected a secondary node onto a
1966 : // primary node, but our secondary node is at one of the secondary boundary face endpoints and
1967 : // our primary node is not.
1968 6563 : if (secondary_node_neighbors->size() == 1 && primary_node_neighbors->size() == 2)
1969 0 : for (const auto i : index_range(primary_elems_mapped))
1970 0 : if (!primary_elems_mapped[i])
1971 : {
1972 0 : _primary_node_and_elem_to_xi1_secondary_elem.emplace(
1973 0 : std::make_tuple(primary_node.id(), &primary_node, (*primary_node_neighbors)[i]),
1974 0 : std::make_pair(1, nullptr));
1975 : }
1976 :
1977 6563 : return found_match;
1978 6576 : }
1979 :
1980 : void
1981 4719 : AutomaticMortarGeneration::projectSecondaryNodesSinglePair(
1982 : SubdomainID lower_dimensional_primary_subdomain_id,
1983 : SubdomainID lower_dimensional_secondary_subdomain_id)
1984 : {
1985 : // Build the "subdomain" adaptor based KD Tree.
1986 4719 : NanoflannMeshSubdomainAdaptor<3> mesh_adaptor(_mesh, lower_dimensional_primary_subdomain_id);
1987 : subdomain_kd_tree_t kd_tree(
1988 4719 : 3, mesh_adaptor, nanoflann::KDTreeSingleIndexAdaptorParams(/*max leaf=*/10));
1989 :
1990 : // Construct the KD tree.
1991 4719 : kd_tree.buildIndex();
1992 :
1993 4719 : for (MeshBase::const_element_iterator el = _mesh.active_elements_begin(),
1994 4719 : end_el = _mesh.active_elements_end();
1995 436164 : el != end_el;
1996 431445 : ++el)
1997 : {
1998 431445 : const Elem * secondary_side_elem = *el;
1999 :
2000 : // If this Elem is not in the current secondary subdomain, go on to the next one.
2001 431445 : if (secondary_side_elem->subdomain_id() != lower_dimensional_secondary_subdomain_id)
2002 408926 : continue;
2003 :
2004 : // For each node on the lower-dimensional element, find the nearest
2005 : // node on the primary side using the KDTree, then
2006 : // search in nearby elements for where it projects
2007 : // along the nodal normal direction.
2008 67557 : for (MooseIndex(secondary_side_elem->n_vertices()) n = 0; n < secondary_side_elem->n_vertices();
2009 : ++n)
2010 : {
2011 45038 : const Node * secondary_node = secondary_side_elem->node_ptr(n);
2012 :
2013 : // Get the nodal neighbors for secondary_node, so we can check whether we've
2014 : // already successfully projected it.
2015 : const std::vector<const Elem *> & secondary_node_neighbors =
2016 45038 : this->_nodes_to_secondary_elem_map.at(secondary_node->id());
2017 :
2018 : // Check whether we've already mapped this secondary node
2019 : // successfully for all of its nodal neighbors.
2020 45038 : bool is_mapped = true;
2021 79950 : for (MooseIndex(secondary_node_neighbors) snn = 0; snn < secondary_node_neighbors.size();
2022 : ++snn)
2023 : {
2024 62531 : auto secondary_key = std::make_pair(secondary_node, secondary_node_neighbors[snn]);
2025 62531 : if (!_secondary_node_and_elem_to_xi2_primary_elem.count(secondary_key))
2026 : {
2027 27619 : is_mapped = false;
2028 27619 : break;
2029 : }
2030 : }
2031 :
2032 : // Go to the next node if this one has already been mapped.
2033 45038 : if (is_mapped)
2034 17419 : continue;
2035 :
2036 : // Look up the new nodal normal value in the local storage, error if not found.
2037 27619 : Point nodal_normal = _secondary_node_to_nodal_normal.at(secondary_node);
2038 :
2039 : // Data structure for performing Nanoflann searches.
2040 : std::array<Real, 3> query_pt = {
2041 27619 : {(*secondary_node)(0), (*secondary_node)(1), (*secondary_node)(2)}};
2042 :
2043 : // The number of results we want to get. We'll look for a
2044 : // "few" nearest nodes, hopefully that is enough to let us
2045 : // figure out which lower-dimensional Elem on the primary
2046 : // side we are across from.
2047 27619 : const std::size_t num_results = 3;
2048 :
2049 : // Initialize result_set and do the search.
2050 55238 : std::vector<size_t> ret_index(num_results);
2051 27619 : std::vector<Real> out_dist_sqr(num_results);
2052 27619 : nanoflann::KNNResultSet<Real> result_set(num_results);
2053 27619 : result_set.init(&ret_index[0], &out_dist_sqr[0]);
2054 27619 : kd_tree.findNeighbors(result_set, &query_pt[0], nanoflann::SearchParameters());
2055 :
2056 : // If this flag gets set in the loop below, we can break out of the outer r-loop as well.
2057 27619 : bool projection_succeeded = false;
2058 :
2059 : // Once we've rejected a candidate for a given secondary_node,
2060 : // there's no reason to check it again.
2061 27619 : std::set<const Elem *> rejected_primary_elem_candidates;
2062 :
2063 : // Loop over the closest nodes, check whether
2064 : // the secondary node successfully projects into
2065 : // either of the closest neighbors, stop when
2066 : // the projection succeeds.
2067 39368 : for (MooseIndex(result_set) r = 0; r < result_set.size(); ++r)
2068 : {
2069 : // Verify that the squared distance we compute is the same as nanoflann'sFss
2070 : mooseAssert(std::abs((_mesh.point(ret_index[r]) - *secondary_node).norm_sq() -
2071 : out_dist_sqr[r]) <= TOLERANCE,
2072 : "Lower-dimensional element squared distance verification failed.");
2073 :
2074 : // Get a reference to the vector of lower dimensional elements from the
2075 : // nodes_to_primary_elem_map.
2076 : std::vector<const Elem *> & primary_elem_candidates =
2077 35456 : this->_nodes_to_primary_elem_map.at(static_cast<dof_id_type>(ret_index[r]));
2078 :
2079 : // Search the Elems connected to this node on the primary mesh side.
2080 57101 : for (MooseIndex(primary_elem_candidates) e = 0; e < primary_elem_candidates.size(); ++e)
2081 : {
2082 45352 : const Elem * primary_elem_candidate = primary_elem_candidates[e];
2083 :
2084 : // If we've already rejected this candidate, we don't need to check it again.
2085 45352 : if (rejected_primary_elem_candidates.count(primary_elem_candidate))
2086 7837 : continue;
2087 :
2088 : // Now generically solve for xi2
2089 37528 : const auto order = primary_elem_candidate->default_order();
2090 37528 : DualNumber<Real> xi2_dn{0, 1};
2091 37528 : unsigned int current_iterate = 0, max_iterates = 10;
2092 :
2093 : // Newton loop
2094 : do
2095 : {
2096 74086 : VectorValue<DualNumber<Real>> x2(0);
2097 74086 : for (MooseIndex(primary_elem_candidate->n_nodes()) n = 0;
2098 228543 : n < primary_elem_candidate->n_nodes();
2099 : ++n)
2100 : x2 +=
2101 154457 : Moose::fe_lagrange_1D_shape(order, n, xi2_dn) * primary_elem_candidate->point(n);
2102 74086 : const auto u = x2 - (*secondary_node);
2103 74086 : const auto F = u(0) * nodal_normal(1) - u(1) * nodal_normal(0);
2104 :
2105 74086 : if (std::abs(F) < _newton_tolerance)
2106 37528 : break;
2107 :
2108 36558 : if (F.derivatives())
2109 : {
2110 36558 : Real dxi2 = -F.value() / F.derivatives();
2111 :
2112 36558 : xi2_dn += dxi2;
2113 : }
2114 : else
2115 : // It's possible that the secondary surface nodal normal is completely orthogonal to
2116 : // the primary surface normal, in which case the derivative is 0. We know in this case
2117 : // that the projection should be a failure
2118 0 : current_iterate = max_iterates;
2119 185700 : } while (++current_iterate < max_iterates);
2120 :
2121 37528 : Real xi2 = xi2_dn.value();
2122 :
2123 : // Check whether the projection worked. The last condition checks for obliqueness of the
2124 : // projection
2125 : //
2126 : // We are projecting on one side first and the other side second. If we make the
2127 : // tolerance bigger and remove the (5) factor we are going to continue to miss the
2128 : // second projection and fall into the exception message in
2129 : // projectPrimaryNodesSinglePair. What makes this modification to not fall in the
2130 : // exception is that we are projecting on one side more xi than in the other. There
2131 : // should be a better way of doing this by using actual distances and not parametric
2132 : // coordinates. But I believe making the tolerance uniformly larger or smaller won't do
2133 : // the trick here.
2134 61248 : if ((current_iterate < max_iterates) && (std::abs(xi2) <= 1. + 5 * _xi_tolerance) &&
2135 23720 : (std::abs(
2136 61248 : (primary_elem_candidate->point(0) - primary_elem_candidate->point(1)).unit() *
2137 23720 : nodal_normal) < std::cos(_minimum_projection_angle * libMesh::pi / 180)))
2138 : {
2139 : // If xi2 == +1 or -1 then this secondary node mapped directly to a node on the primary
2140 : // surface. This isn't as unlikely as you might think, it will happen if the meshes
2141 : // on the interface start off being perfectly aligned. In this situation, we need to
2142 : // associate the secondary node with two different elements (and two corresponding
2143 : // xi^(2) values.
2144 23720 : if (std::abs(std::abs(xi2) - 1.) <= _xi_tolerance * 5.0)
2145 : {
2146 6576 : const Node * primary_node = (xi2 < 0) ? primary_elem_candidate->node_ptr(0)
2147 4077 : : primary_elem_candidate->node_ptr(1);
2148 : const bool created_mortar_segment =
2149 6576 : processAlignedNodes(*secondary_node,
2150 : *primary_node,
2151 : &secondary_node_neighbors,
2152 : nullptr,
2153 : nodal_normal,
2154 : *primary_elem_candidate,
2155 : rejected_primary_elem_candidates);
2156 :
2157 6576 : if (!created_mortar_segment)
2158 13 : continue;
2159 : }
2160 : else // Point falls somewhere in the middle of the Elem.
2161 : {
2162 : // Add two entries to secondary_node_and_elem_to_xi2_primary_elem.
2163 48010 : for (MooseIndex(secondary_node_neighbors) nn = 0;
2164 48010 : nn < secondary_node_neighbors.size();
2165 : ++nn)
2166 : {
2167 30866 : const Elem * neigh = secondary_node_neighbors[nn];
2168 92598 : for (MooseIndex(neigh->n_vertices()) nid = 0; nid < neigh->n_vertices(); ++nid)
2169 : {
2170 61732 : const Node * neigh_node = neigh->node_ptr(nid);
2171 61732 : if (secondary_node == neigh_node)
2172 : {
2173 30866 : auto key = std::make_pair(neigh_node, neigh);
2174 30866 : auto val = std::make_pair(xi2, primary_elem_candidate);
2175 30866 : _secondary_node_and_elem_to_xi2_primary_elem.emplace(key, val);
2176 : }
2177 : }
2178 : }
2179 : }
2180 :
2181 23707 : projection_succeeded = true;
2182 23707 : break; // out of e-loop
2183 : }
2184 : else
2185 : // The current secondary_node is not in this Elem, so keep track of the rejects.
2186 13808 : rejected_primary_elem_candidates.insert(primary_elem_candidate);
2187 37528 : }
2188 :
2189 35456 : if (projection_succeeded)
2190 23707 : break; // out of r-loop
2191 : } // r-loop
2192 :
2193 27619 : if (!projection_succeeded)
2194 : {
2195 3912 : _failed_secondary_node_projections.insert(secondary_node->id());
2196 3912 : if (_debug)
2197 0 : _console << "Failed to find primary Elem into which secondary node "
2198 0 : << static_cast<const Point &>(*secondary_node) << ", id '"
2199 0 : << secondary_node->id() << "', projects onto\n"
2200 0 : << std::endl;
2201 : }
2202 23707 : else if (_debug)
2203 0 : _projected_secondary_nodes.insert(secondary_node->id());
2204 27619 : } // loop over side nodes
2205 4719 : } // end loop over lower-dimensional elements
2206 :
2207 4719 : if (_distributed)
2208 : {
2209 94 : if (_debug)
2210 0 : _mesh.comm().set_union(_projected_secondary_nodes);
2211 94 : _mesh.comm().set_union(_failed_secondary_node_projections);
2212 : }
2213 :
2214 4719 : if (_debug)
2215 0 : _console << "\n"
2216 0 : << _projected_secondary_nodes.size() << " out of "
2217 0 : << _projected_secondary_nodes.size() + _failed_secondary_node_projections.size()
2218 0 : << " secondary nodes were successfully projected\n"
2219 0 : << std::endl;
2220 4719 : }
2221 :
2222 : // Inverse map primary nodes onto their corresponding secondary elements for each primary/secondary
2223 : // pair.
2224 : void
2225 4719 : AutomaticMortarGeneration::projectPrimaryNodes()
2226 : {
2227 : // For each primary/secondary boundary id pair, call the
2228 : // project_primary_nodes_single_pair() helper function.
2229 9438 : for (const auto & pr : _primary_secondary_subdomain_id_pairs)
2230 4719 : projectPrimaryNodesSinglePair(pr.first, pr.second);
2231 4719 : }
2232 :
2233 : void
2234 4719 : AutomaticMortarGeneration::projectPrimaryNodesSinglePair(
2235 : SubdomainID lower_dimensional_primary_subdomain_id,
2236 : SubdomainID lower_dimensional_secondary_subdomain_id)
2237 : {
2238 : // Build a Nanoflann object on the lower-dimensional secondary elements of the Mesh.
2239 4719 : NanoflannMeshSubdomainAdaptor<3> mesh_adaptor(_mesh, lower_dimensional_secondary_subdomain_id);
2240 : subdomain_kd_tree_t kd_tree(
2241 4719 : 3, mesh_adaptor, nanoflann::KDTreeSingleIndexAdaptorParams(/*max leaf=*/10));
2242 :
2243 : // Construct the KD tree for lower-dimensional elements in the volume mesh.
2244 4719 : kd_tree.buildIndex();
2245 :
2246 4719 : std::unordered_set<dof_id_type> primary_nodes_visited;
2247 :
2248 436164 : for (const auto & primary_side_elem : _mesh.active_element_ptr_range())
2249 : {
2250 : // If this is not one of the lower-dimensional primary side elements, go on to the next one.
2251 431445 : if (primary_side_elem->subdomain_id() != lower_dimensional_primary_subdomain_id)
2252 412446 : continue;
2253 :
2254 : // For each node on this side, find the nearest node on the secondary side using the KDTree,
2255 : // then search in nearby elements for where it projects along the nodal normal direction.
2256 56997 : for (MooseIndex(primary_side_elem->n_vertices()) n = 0; n < primary_side_elem->n_vertices();
2257 : ++n)
2258 : {
2259 : // Get a pointer to this node.
2260 37998 : const Node * primary_node = primary_side_elem->node_ptr(n);
2261 :
2262 : // Get the nodal neighbors connected to this primary node.
2263 : const std::vector<const Elem *> & primary_node_neighbors =
2264 37998 : _nodes_to_primary_elem_map.at(primary_node->id());
2265 :
2266 : // Check whether we have already successfully inverse mapped this primary node (whether during
2267 : // secondary node projection or now during primary node projection) or we have already failed
2268 : // to inverse map this primary node (now during primary node projection), and then skip if
2269 : // either of those things is true
2270 : auto primary_key =
2271 37998 : std::make_tuple(primary_node->id(), primary_node, primary_node_neighbors[0]);
2272 61729 : if (!primary_nodes_visited.insert(primary_node->id()).second ||
2273 23731 : _primary_node_and_elem_to_xi1_secondary_elem.count(primary_key))
2274 20707 : continue;
2275 :
2276 : // Data structure for performing Nanoflann searches.
2277 17291 : Real query_pt[3] = {(*primary_node)(0), (*primary_node)(1), (*primary_node)(2)};
2278 :
2279 : // The number of results we want to get. We'll look for a
2280 : // "few" nearest nodes, hopefully that is enough to let us
2281 : // figure out which lower-dimensional Elem on the secondary side
2282 : // we are across from.
2283 17291 : const size_t num_results = 3;
2284 :
2285 : // Initialize result_set and do the search.
2286 34582 : std::vector<size_t> ret_index(num_results);
2287 17291 : std::vector<Real> out_dist_sqr(num_results);
2288 17291 : nanoflann::KNNResultSet<Real> result_set(num_results);
2289 17291 : result_set.init(&ret_index[0], &out_dist_sqr[0]);
2290 17291 : kd_tree.findNeighbors(result_set, &query_pt[0], nanoflann::SearchParameters());
2291 :
2292 : // If this flag gets set in the loop below, we can break out of the outer r-loop as well.
2293 17291 : bool projection_succeeded = false;
2294 :
2295 : // Once we've rejected a candidate for a given
2296 : // primary_node, there's no reason to check it
2297 : // again.
2298 17291 : std::set<const Elem *> rejected_secondary_elem_candidates;
2299 :
2300 : // Loop over the closest nodes, check whether the secondary node successfully projects into
2301 : // either of the closest neighbors, stop when the projection succeeds.
2302 28661 : for (MooseIndex(result_set) r = 0; r < result_set.size(); ++r)
2303 : {
2304 : // Verify that the squared distance we compute is the same as nanoflann's
2305 : mooseAssert(std::abs((_mesh.point(ret_index[r]) - *primary_node).norm_sq() -
2306 : out_dist_sqr[r]) <= TOLERANCE,
2307 : "Lower-dimensional element squared distance verification failed.");
2308 :
2309 : // Get a reference to the vector of lower dimensional elements from the
2310 : // nodes_to_secondary_elem_map.
2311 : const std::vector<const Elem *> & secondary_elem_candidates =
2312 24871 : _nodes_to_secondary_elem_map.at(static_cast<dof_id_type>(ret_index[r]));
2313 :
2314 : // Print the Elems connected to this node on the secondary mesh side.
2315 48627 : for (MooseIndex(secondary_elem_candidates) e = 0; e < secondary_elem_candidates.size(); ++e)
2316 : {
2317 37257 : const Elem * secondary_elem_candidate = secondary_elem_candidates[e];
2318 :
2319 : // If we've already rejected this candidate, we don't need to check it again.
2320 37257 : if (rejected_secondary_elem_candidates.count(secondary_elem_candidate))
2321 7580 : continue;
2322 :
2323 29677 : std::vector<Point> nodal_normals(secondary_elem_candidate->n_nodes());
2324 90014 : for (const auto n : make_range(secondary_elem_candidate->n_nodes()))
2325 120674 : nodal_normals[n] =
2326 60337 : _secondary_node_to_nodal_normal.at(secondary_elem_candidate->node_ptr(n));
2327 :
2328 : // Use equation 2.4.6 from Bin Yang's dissertation to try and solve for
2329 : // the position on the secondary element where this primary came from. This
2330 : // requires a Newton iteration in general.
2331 29677 : DualNumber<Real> xi1_dn{0, 1}; // initial guess
2332 29677 : auto && order = secondary_elem_candidate->default_order();
2333 29677 : unsigned int current_iterate = 0, max_iterates = 10;
2334 :
2335 29677 : VectorValue<DualNumber<Real>> normals(0);
2336 :
2337 : // Newton iteration loop - this to converge in 1 iteration when it
2338 : // succeeds, and possibly two iterations when it converges to a
2339 : // xi outside the reference element. I don't know any reason why it should
2340 : // only take 1 iteration -- the Jacobian is not constant in general...
2341 : do
2342 : {
2343 58790 : VectorValue<DualNumber<Real>> x1(0);
2344 178129 : for (MooseIndex(secondary_elem_candidate->n_nodes()) n = 0;
2345 178129 : n < secondary_elem_candidate->n_nodes();
2346 : ++n)
2347 : {
2348 119339 : const auto phi = Moose::fe_lagrange_1D_shape(order, n, xi1_dn);
2349 119339 : x1 += phi * secondary_elem_candidate->point(n);
2350 119339 : normals += phi * nodal_normals[n];
2351 119339 : }
2352 :
2353 58790 : const auto u = x1 - (*primary_node);
2354 :
2355 58790 : const auto F = u(0) * normals(1) - u(1) * normals(0);
2356 :
2357 58790 : if (std::abs(F) < _newton_tolerance)
2358 29677 : break;
2359 :
2360 : // Unlike for projection of nodal normals onto primary surfaces, we should never have a
2361 : // case where the nodal normal is completely orthogonal to the secondary surface, so we
2362 : // do not have to guard against F.derivatives() == 0 here
2363 29113 : Real dxi1 = -F.value() / F.derivatives();
2364 :
2365 29113 : xi1_dn += dxi1;
2366 :
2367 29113 : normals = 0;
2368 147257 : } while (++current_iterate < max_iterates);
2369 :
2370 29677 : Real xi1 = xi1_dn.value();
2371 :
2372 : // Check for convergence to a valid solution... The last condition checks for obliqueness
2373 : // of the projection
2374 43178 : if ((current_iterate < max_iterates) && (std::abs(xi1) <= 1. + _xi_tolerance) &&
2375 13501 : (std::abs((primary_side_elem->point(0) - primary_side_elem->point(1)).unit() *
2376 43178 : MetaPhysicL::raw_value(normals).unit()) <
2377 13501 : std::cos(_minimum_projection_angle * libMesh::pi / 180.0)))
2378 : {
2379 13501 : if (std::abs(std::abs(xi1) - 1.) < _xi_tolerance)
2380 : {
2381 : // Special case: xi1=+/-1.
2382 : // It is unlikely that we get here, because this primary node should already
2383 : // have been mapped during the project_secondary_nodes() routine, but
2384 : // there is still a chance since the tolerances are applied to
2385 : // the xi coordinate and that value may be different on a primary element and a
2386 : // secondary element since they may have different sizes. It's also possible that we
2387 : // may reach this point if the solve has yielded a non-physical configuration such as
2388 : // one block being pushed way out into space
2389 0 : const Node & secondary_node = (xi1 < 0) ? secondary_elem_candidate->node_ref(0)
2390 0 : : secondary_elem_candidate->node_ref(1);
2391 0 : bool created_mortar_segment = false;
2392 :
2393 : // If we have failed to project this secondary node, let's try again now
2394 0 : if (_failed_secondary_node_projections.count(secondary_node.id()))
2395 0 : created_mortar_segment = processAlignedNodes(secondary_node,
2396 : *primary_node,
2397 : nullptr,
2398 : &primary_node_neighbors,
2399 0 : MetaPhysicL::raw_value(normals),
2400 : *secondary_elem_candidate,
2401 : rejected_secondary_elem_candidates);
2402 : else
2403 0 : rejected_secondary_elem_candidates.insert(secondary_elem_candidate);
2404 :
2405 0 : if (!created_mortar_segment)
2406 : // We used to throw an exception in this scope but now that we support processing
2407 : // aligned nodes within this primary node projection method, I don't see any harm in
2408 : // simply rejecting the secondary element candidate in the case of failure and
2409 : // continuing just as we do when projecting secondary nodes
2410 0 : continue;
2411 : }
2412 : else // somewhere in the middle of the Elem
2413 : {
2414 : // Add entry to primary_node_and_elem_to_xi1_secondary_elem
2415 : //
2416 : // Note: we originally duplicated the map values for the keys (node, left_neighbor)
2417 : // and (node, right_neighbor) but I don't think that should be necessary. Instead we
2418 : // just do it for neighbor 0, but really maybe we don't even need to do that since
2419 : // we can always look up the neighbors later given the Node... keeping it like this
2420 : // helps to maintain the "symmetry" of the two containers.
2421 13501 : const Elem * neigh = primary_node_neighbors[0];
2422 40503 : for (MooseIndex(neigh->n_vertices()) nid = 0; nid < neigh->n_vertices(); ++nid)
2423 : {
2424 27002 : const Node * neigh_node = neigh->node_ptr(nid);
2425 27002 : if (primary_node == neigh_node)
2426 : {
2427 13501 : auto key = std::make_tuple(neigh_node->id(), neigh_node, neigh);
2428 13501 : auto val = std::make_pair(xi1, secondary_elem_candidate);
2429 13501 : _primary_node_and_elem_to_xi1_secondary_elem.emplace(key, val);
2430 : }
2431 : }
2432 : }
2433 :
2434 13501 : projection_succeeded = true;
2435 13501 : break; // out of e-loop
2436 : }
2437 : else
2438 : {
2439 : // The current primary_point is not in this Elem, so keep track of the rejects.
2440 16176 : rejected_secondary_elem_candidates.insert(secondary_elem_candidate);
2441 : }
2442 56679 : } // end e-loop over candidate elems
2443 :
2444 24871 : if (projection_succeeded)
2445 13501 : break; // out of r-loop
2446 : } // r-loop
2447 :
2448 17291 : if (!projection_succeeded && _debug)
2449 : {
2450 0 : _console << "\nFailed to find point from which primary node "
2451 0 : << static_cast<const Point &>(*primary_node) << " was projected." << std::endl
2452 0 : << std::endl;
2453 : }
2454 17291 : } // loop over side nodes
2455 4719 : } // end loop over elements for finding where primary points would have projected from.
2456 4719 : }
2457 :
2458 : std::vector<AutomaticMortarGeneration::MortarFilterIter>
2459 675 : AutomaticMortarGeneration::secondariesToMortarSegments(const Node & node) const
2460 : {
2461 675 : auto secondary_it = _nodes_to_secondary_elem_map.find(node.id());
2462 675 : if (secondary_it == _nodes_to_secondary_elem_map.end())
2463 0 : return {};
2464 :
2465 675 : const auto & secondary_elems = secondary_it->second;
2466 675 : std::vector<MortarFilterIter> ret;
2467 675 : ret.reserve(secondary_elems.size());
2468 :
2469 1638 : for (const auto i : index_range(secondary_elems))
2470 : {
2471 963 : auto * const secondary_elem = secondary_elems[i];
2472 963 : auto msm_it = _secondary_elems_to_mortar_segments.find(secondary_elem->id());
2473 963 : if (msm_it == _secondary_elems_to_mortar_segments.end())
2474 : // We may have removed this element key from this map
2475 0 : continue;
2476 :
2477 : mooseAssert(secondary_elem->active(),
2478 : "We loop over active elements when building the mortar segment mesh, so we golly "
2479 : "well hope this is active.");
2480 : mooseAssert(!msm_it->second.empty(),
2481 : "We should have removed all secondaries from this map if they do not have any "
2482 : "mortar segments associated with them.");
2483 963 : ret.push_back(msm_it);
2484 : }
2485 :
2486 675 : return ret;
2487 675 : }
|