https://mooseframework.inl.gov
Loading...
Searching...
No Matches
ComputeWeightedGapCartesianLMMechanicalContact.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
11#include "DisplacedProblem.h"
12#include "MortarContactUtils.h"
13#include "MortarNodalFrames.h"
14#include "Assembly.h"
16#include "metaphysicl/metaphysicl_version.h"
17#include "metaphysicl/dualsemidynamicsparsenumberarray.h"
18#include "metaphysicl/parallel_dualnumber.h"
19#if METAPHYSICL_MAJOR_VERSION < 2
20#include "metaphysicl/parallel_dynamic_std_array_wrapper.h"
21#else
22#include "metaphysicl/parallel_dynamic_array_wrapper.h"
23#endif
24#include "metaphysicl/parallel_semidynamicsparsenumberarray.h"
25#include "timpi/parallel_sync.h"
26
28
29namespace
30{
31const InputParameters &
32assignVarsInParamsCartesianWeightedGap(const InputParameters & params_in)
33{
34 InputParameters & ret = const_cast<InputParameters &>(params_in);
35 const auto & disp_x_name = ret.get<std::vector<VariableName>>("disp_x");
36 if (disp_x_name.size() != 1)
37 mooseError("We require that the disp_x parameter have exactly one coupled name");
38
39 // We do this so we don't get any variable errors during MortarConstraint(Base) construction
40 ret.set<VariableName>("secondary_variable") = disp_x_name[0];
41 ret.set<VariableName>("primary_variable") = disp_x_name[0];
42
43 return ret;
44}
45}
46
49{
51 // Mechanical contact residuals use only primal values and normals
52 params.set<bool>("_compute_primal_gradients") = false;
53 params.addClassDescription("Computes the weighted gap that will later be used to enforce the "
54 "zero-penetration mechanical contact conditions");
55 params.suppressParameter<VariableName>("secondary_variable");
56 params.suppressParameter<VariableName>("primary_variable");
57 params.addRequiredCoupledVar("disp_x", "The x displacement variable");
58 params.addRequiredCoupledVar("disp_y", "The y displacement variable");
59 params.addCoupledVar("disp_z", "The z displacement variable");
60 params.addParam<Real>(
61 "c", 1e6, "Parameter for balancing the size of the gap and contact pressure");
62 params.addRequiredCoupledVar("lm_x",
63 "Mechanical contact Lagrange multiplier along the x Cartesian axis");
65 "lm_y", "Mechanical contact Lagrange multiplier along the y Cartesian axis.");
66 params.addCoupledVar("lm_z",
67 "Mechanical contact Lagrange multiplier along the z Cartesian axis.");
68 // The nodal normal at a secondary node depends on every face incident to it, so the Jacobian
69 // needs the secondary face point-neighbor ghosted and coupled.
70 params.set<bool>("ghost_point_neighbors") = true;
71 params.set<bool>("interpolate_normals") = false;
72 params.addParam<bool>(
73 "normalize_c",
74 false,
75 "Whether to normalize c by weighting function norm. When unnormalized "
76 "the value of c effectively depends on element size since in the constraint we compare nodal "
77 "Lagrange Multiplier values to integrated gap values (LM nodal value is independent of "
78 "element size, where integrated values are dependent on element size).");
79 params.set<bool>("use_displaced_mesh") = true;
80 return params;
81}
82
84 const InputParameters & parameters)
85 : ADMortarConstraint(assignVarsInParamsCartesianWeightedGap(parameters)),
86 _secondary_disp_x(adCoupledValue("disp_x")),
87 _primary_disp_x(adCoupledNeighborValue("disp_x")),
88 _secondary_disp_y(adCoupledValue("disp_y")),
89 _primary_disp_y(adCoupledNeighborValue("disp_y")),
90 _has_disp_z(isCoupled("disp_z")),
91 _secondary_disp_z(_has_disp_z ? &adCoupledValue("disp_z") : nullptr),
92 _primary_disp_z(_has_disp_z ? &adCoupledNeighborValue("disp_z") : nullptr),
93 _c(getParam<Real>("c")),
94 _normalize_c(getParam<bool>("normalize_c")),
95 _nodal(getVar("disp_x", 0)->feType().family == LAGRANGE),
96 _disp_x_var(getVar("disp_x", 0)),
97 _disp_y_var(getVar("disp_y", 0)),
98 _disp_z_var(_has_disp_z ? getVar("disp_z", 0) : nullptr),
99 _nodal_frames(std::array<const MooseVariable *, 3>{{_disp_x_var, _disp_y_var, _disp_z_var}})
100{
101 _lm_vars.push_back(getVar("lm_x", 0));
102 _lm_vars.push_back(getVar("lm_y", 0));
103
104 if (isParamValid("lm_z") ^ _has_disp_z)
105 paramError("lm_z",
106 "In three-dimensions, both the Z Lagrange multiplier and the Z displacement need to "
107 "be provided");
108
109 if (_has_disp_z)
110 _lm_vars.push_back(getVar("lm_z", 0));
111
112 if (!getParam<bool>("use_displaced_mesh"))
113 paramError(
114 "use_displaced_mesh",
115 "'use_displaced_mesh' must be true for the ComputeWeightedGapLMMechanicalContact object");
116
117 if (getParam<bool>("interpolate_normals"))
118 paramError("interpolate_normals",
119 "Mechanical mortar contact uses normalized secondary nodal normals and cannot be "
120 "combined with quadrature-point normal interpolation.");
121
122 for (const auto i : index_range(_lm_vars))
123 if (!_lm_vars[i]->isNodal())
124 if (_lm_vars[i]->feType().order != static_cast<Order>(0))
125 mooseError("Normal contact constraints only support elemental variables of CONSTANT order");
126}
127
128void
136
137ADReal
139{
140 mooseError("We should never call computeQpResidual for ComputeWeightedGapLMMechanicalContact");
141}
142
143void
145{
146 // Trim interior node variable derivatives
147 const auto & primary_ip_lowerd_map = amg().getPrimaryIpToLowerElementMap(
151 const auto & secondary_ip_lowerd_map =
153
154 std::array<const MooseVariable *, 3> var_array{{_disp_x_var, _disp_y_var, _disp_z_var}};
155 std::array<ADReal, 3> primary_disp{
156 {_primary_disp_x[_qp], _primary_disp_y[_qp], _has_disp_z ? (*_primary_disp_z)[_qp] : 0}};
157 std::array<ADReal, 3> secondary_disp{{_secondary_disp_x[_qp],
159 _has_disp_z ? (*_secondary_disp_z)[_qp] : 0}};
160
161 trimInteriorNodeDerivatives(primary_ip_lowerd_map, var_array, primary_disp, false);
162 trimInteriorNodeDerivatives(secondary_ip_lowerd_map, var_array, secondary_disp, true);
163
164 const ADReal & prim_x = primary_disp[0];
165 const ADReal & prim_y = primary_disp[1];
166 const ADReal * prim_z = nullptr;
167 if (_has_disp_z)
168 prim_z = &primary_disp[2];
169
170 const ADReal & sec_x = secondary_disp[0];
171 const ADReal & sec_y = secondary_disp[1];
172 const ADReal * sec_z = nullptr;
173 if (_has_disp_z)
174 sec_z = &secondary_disp[2];
175
176 // Compute gap vector
178
179 gap_vec(0).derivatives() = prim_x.derivatives() - sec_x.derivatives();
180 gap_vec(1).derivatives() = prim_y.derivatives() - sec_y.derivatives();
181 if (_has_disp_z)
182 gap_vec(2).derivatives() = prim_z->derivatives() - sec_z->derivatives();
183
184 // Compute integration point quantities
185 _qp_gap_nodal = gap_vec * (_JxW_msm[_qp] * _coord[_qp]);
186
187 // To do normalization of constraint coefficient (c_n)
189}
190
191void
193{
194 mooseAssert(_normals.size() == _lower_secondary_elem->n_nodes(),
195 "Making sure that _normals is the expected size");
196
197 // Get the _dof_to_weighted_gap map
198 const DofObject * dof = _lm_vars[0]->isNodal()
199 ? cast_ptr<const DofObject *>(_lower_secondary_elem->node_ptr(_i))
200 : cast_ptr<const DofObject *>(_lower_secondary_elem);
201
202 _dof_to_weighted_gap[dof].first += _test[_i][_qp] * _qp_gap_nodal *
204 _lower_secondary_elem->node_ref(_i),
205 _normals[_i],
207
208 if (_normalize_c)
209 _dof_to_weighted_gap[dof].second += _test[_i][_qp] * _qp_factor;
210}
211
212void
214{
216
217 for (const auto & [dof, frame_node] : _dof_to_frame_node)
218 _dof_to_old_normal_vector.emplace(dof, frame_node.second);
219}
220
221void
228
229void
234
235void
237{
238 if (mortar_type != Moose::MortarType::Lower)
239 return;
240
241 for (const auto i : index_range(_lm_vars))
242 {
243 mooseAssert(_lm_vars[i], "LM variable is null");
244 libmesh_ignore(i);
245 }
246
247 for (_qp = 0; _qp < _qrule_msm->n_points(); _qp++)
248 {
250 for (_i = 0; _i < _test.size(); ++_i)
251 {
253
254 // Get the _dof_to_weighted_gap map
255 const DofObject * dof = _lm_vars[0]->isNodal()
256 ? cast_ptr<const DofObject *>(_lower_secondary_elem->node_ptr(_i))
257 : cast_ptr<const DofObject *>(_lower_secondary_elem);
258 // We do not interpolate geometry, so just match the local node _i with the corresponding _i
260 }
261 }
262}
263
264const ADRealVectorValue &
266{
267 const auto & [node, stored_normal] = libmesh_map_find(_dof_to_frame_node, dof);
268 return _nodal_frames.normal(amg(), *node, stored_normal, Moose::doDerivatives(_subproblem, _sys));
269}
270
271const std::array<ADRealVectorValue, 2> &
273{
274 const auto & [node, stored_normal] = libmesh_map_find(_dof_to_frame_node, dof);
275 return _nodal_frames.tangents(
276 amg(), *node, stored_normal, Moose::doDerivatives(_subproblem, _sys));
277}
278
279void
281{
282 // During "computeResidual" and "computeJacobian" we are actually just computing properties on the
283 // mortar segment element mesh. We are *not* actually assembling into the residual/Jacobian. For
284 // the zero-penetration constraint, the property of interest is the map from node to weighted gap.
285 // Computation of the properties proceeds identically for residual and Jacobian evaluation hence
286 // why we simply call computeResidual here. We will assemble into the residual/Jacobian later from
287 // the post() method
288 computeResidual(mortar_type);
289}
290
291void
293{
296
297 for (const auto & pr : _dof_to_weighted_gap)
298 {
299 if (pr.first->processor_id() != this->processor_id())
300 continue;
301
302 _weighted_gap_ptr = &pr.second.first;
303 _normalization_ptr = &pr.second.second;
304
305 enforceConstraintOnDof(pr.first);
306 }
307}
308
309void
311 const std::unordered_set<const Node *> & inactive_lm_nodes)
312{
315
316 for (const auto & pr : _dof_to_weighted_gap)
317 {
318 if ((inactive_lm_nodes.find(static_cast<const Node *>(pr.first)) != inactive_lm_nodes.end()) ||
319 (pr.first->processor_id() != this->processor_id()))
320 continue;
321
322 _weighted_gap_ptr = &pr.second.first;
323 _normalization_ptr = &pr.second.second;
324
325 enforceConstraintOnDof(pr.first);
326 }
327}
328
329void
331{
332 const auto & weighted_gap = *_weighted_gap_ptr;
333 const Real c = _normalize_c ? _c / *_normalization_ptr : _c;
334
335 const auto dof_index_x = dof->dof_number(_sys.number(), _lm_vars[0]->number(), 0);
336 const auto dof_index_y = dof->dof_number(_sys.number(), _lm_vars[1]->number(), 0);
337 const Real scaling_factor_x = _lm_vars[0]->scalingFactor();
338 const Real scaling_factor_y = _lm_vars[1]->scalingFactor();
339 Real scaling_factor_z = 1;
340
341 ADReal lm_x = (*_sys.currentSolution())(dof_index_x);
342 ADReal lm_y = (*_sys.currentSolution())(dof_index_y);
343
344 Moose::derivInsert(lm_x.derivatives(), dof_index_x, 1.);
345 Moose::derivInsert(lm_y.derivatives(), dof_index_y, 1.);
346
347 dof_id_type dof_index_z(-1);
348 ADReal lm_z;
349 if (_has_disp_z)
350 {
351 dof_index_z = dof->dof_number(_sys.number(), _lm_vars[2]->number(), 0);
352 lm_z = (*_sys.currentSolution())(dof_index_z);
353 Moose::derivInsert(lm_z.derivatives(), dof_index_z, 1.);
354 scaling_factor_z = _lm_vars[2]->scalingFactor();
355 }
356
357 const auto & normal = dofNormal(dof);
358 const auto & tangents = dofTangents(dof);
359
360 ADReal normal_pressure_value = lm_x * normal(0) + lm_y * normal(1);
361 ADReal tangential_pressure_value = lm_x * tangents[0](0) + lm_y * tangents[0](1);
362
363 ADReal tangential_pressure_value_dir = 0;
364
365 if (_has_disp_z)
366 {
367 normal_pressure_value += lm_z * normal(2);
368 tangential_pressure_value += lm_z * tangents[0](2);
369 tangential_pressure_value_dir =
370 lm_x * tangents[1](0) + lm_y * tangents[1](1) + lm_z * tangents[1](2);
371 }
372
373 ADReal normal_dof_residual = std::min(normal_pressure_value, weighted_gap * c);
374 ADReal tangential_dof_residual = tangential_pressure_value;
375
376 // Get index for normal constraint.
377 // We do this to get a decent Jacobian structure, which is key for the use of iterative solvers.
378 // Using old normal vector to avoid changes in the Jacobian structure within one time step
379
380 Real ny, nz;
381 // Intially, use the current normal vector
382 if (_dof_to_old_normal_vector[dof].norm() < TOLERANCE)
383 {
384 ny = MetaPhysicL::raw_value(normal(1));
385 nz = MetaPhysicL::raw_value(normal(2));
386 }
387 else
388 {
389 ny = _dof_to_old_normal_vector[dof](1);
390 nz = _dof_to_old_normal_vector[dof](2);
391 }
392 unsigned int component_normal = 0;
393 if (std::abs(ny) > 0.57735)
394 component_normal = 1;
395 else if (std::abs(nz) > 0.57735)
396 component_normal = 2;
397
398 libmesh_ignore(component_normal);
399
401 _assembly,
402 std::array<ADReal, 1>{{normal_dof_residual}},
403 std::array<dof_id_type, 1>{{component_normal == 0
404 ? dof_index_x
405 : (component_normal == 1 ? dof_index_y : dof_index_z)}},
406 component_normal == 0 ? scaling_factor_x
407 : (component_normal == 1 ? scaling_factor_y : scaling_factor_z));
408
410 _assembly,
411 std::array<ADReal, 1>{{tangential_dof_residual}},
412 std::array<dof_id_type, 1>{
413 {(component_normal == 0 || component_normal == 2) ? dof_index_y : dof_index_x}},
414 (component_normal == 0 || component_normal == 2) ? scaling_factor_y : scaling_factor_x);
415
416 if (_has_disp_z)
418 _assembly,
419 std::array<ADReal, 1>{{tangential_pressure_value_dir}},
420 std::array<dof_id_type, 1>{
421 {(component_normal == 0 || component_normal == 1) ? dof_index_z : dof_index_x}},
422 (component_normal == 0 || component_normal == 1) ? scaling_factor_z : scaling_factor_x);
423}
DualNumber< Real, DNDerivativeType, true > ADReal
registerMooseObject("ContactApp", ComputeWeightedGapCartesianLMMechanicalContact)
void mooseError(Args &&... args)
static InputParameters validParams()
std::map< unsigned int, unsigned int > getSecondaryIpToLowerElementMap(const Elem &lower_secondary_elem) const
std::map< unsigned int, unsigned int > getPrimaryIpToLowerElementMap(const Elem &primary_elem, const Elem &primary_elem_ip, const Elem &lower_secondary_elem) const
static const Elem & primaryInteriorParentOrSelf(const Elem &lower_primary_elem)
Computes the weighted gap that will later be used to enforce the zero-penetration mechanical contact ...
virtual void enforceConstraintOnDof(const DofObject *const dof)
Method called from post().
virtual void computeQpIProperties()
Computes properties that are functions both of _qp and _i, for example the weighted gap.
const ADVariableValue & _secondary_disp_x
x-displacement on the secondary face
Real _qp_factor
The value of the LM at the current quadrature point.
Moose::Mortar::ADNodalFrames _nodal_frames
Secondary nodal normals and tangents with their displacement derivatives.
const MooseVariable *const _disp_y_var
The y displacement variable.
std::vector< MooseVariable * > _lm_vars
Cartesian Lagrange multipliers for mechanical contact.
const ADVariableValue & _primary_disp_x
x-displacement on the primary face
const std::array< ADRealVectorValue, 2 > & dofTangents(const DofObject *dof)
const ADReal * _weighted_gap_ptr
A pointer members that can be used to help avoid copying ADReals.
std::unordered_map< const DofObject *, RealVectorValue > _dof_to_old_normal_vector
A map from node to normal vector (2D) - old.
std::unordered_map< const DofObject *, std::pair< ADReal, Real > > _dof_to_weighted_gap
A map from node to weighted gap and normalization (if requested)
const ADVariableValue & _primary_disp_y
y-displacement on the primary face
ADRealVectorValue _qp_gap_nodal
Vector for computation of weighted gap with nodal normals.
bool _normalize_c
Whether to normalize weighted gap by weighting function norm.
const MooseVariable *const _disp_x_var
The x displacement variable.
void incorrectEdgeDroppingPost(const std::unordered_set< const Node * > &inactive_lm_nodes) override
Copy of the post routine but that skips assembling inactive nodes.
virtual void computeQpProperties()
Computes properties that are functions only of the current quadrature point (_qp),...
const ADVariableValue & _secondary_disp_y
y-displacement on the secondary face
const bool _nodal
Whether the dof objects are nodal; if they're not, then they're elemental.
std::unordered_map< const DofObject *, std::pair< const Node *, Point > > _dof_to_frame_node
A map from LM degree of freedom to the secondary node whose frame projects its multipliers,...
const MooseVariable *const _disp_z_var
The z displacement variable.
const bool _has_disp_z
For 2D mortar contact no displacement will be specified, so const pointers used.
unsigned int _qp
unsigned int _i
void suppressParameter(const std::string &name)
void addRequiredCoupledVar(const std::string &name, const std::string &doc_string)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
void addClassDescription(const std::string &doc_string)
T & set(const std::string &name, bool quiet_mode=false)
void addCoupledVar(const std::string &name, const std::string &doc_string)
void mooseError(Args &&... args) const
const std::array< ADRealVectorValue, 2 > & tangents(const AutomaticMortarGeneration &amg, const Node &node, const Point &stored_normal, bool do_derivatives)
const ADRealVectorValue & normal(const AutomaticMortarGeneration &amg, const Node &node, const Point &stored_normal, bool do_derivatives)
const MooseArray< Real > & _coord
const VariableTestValue & _test
Elem const *const & _lower_secondary_elem
const libMesh::QBase *const & _qrule_msm
const AutomaticMortarGeneration & amg() const
Elem const *const & _lower_primary_elem
const MooseArray< Point > & _phys_points_secondary
const MooseArray< Point > & _phys_points_primary
std::vector< Point > _normals
static void trimInteriorNodeDerivatives(const std::map< unsigned int, unsigned int > &primary_ip_lowerd_map, const Variables &moose_var, DualNumbers &ad_vars, const bool is_secondary)
const std::vector< Real > & _JxW_msm
MooseMesh & _mesh
Assembly & _assembly
SubProblem & _subproblem
SystemBase & _sys
virtual void initialSetup()
virtual const NumericVector< Number > *const & currentSolution() const=0
unsigned int number() const
void addResidualsAndJacobian(Assembly &assembly, const Residuals &residuals, const Indices &dof_indices, Real scaling_factor)
const Parallel::Communicator & _communicator
processor_id_type processor_id() const
unsigned int n_points() const
auto raw_value(const Eigen::Map< T > &in)
void communicateGaps(std::unordered_map< const DofObject *, std::pair< ADReal, Real > > &dof_to_weighted_gap, const MooseMesh &mesh, bool nodal, bool normalize_c, const Parallel::Communicator &communicator, bool send_data_back)
This function is used to communicate gaps across processes.
void checkNodalNormalDisplacements(const MooseBase &object, const std::array< const MooseVariable *, 3 > &disp_vars, const std::array< std::string, 3 > &param_names, const SystemBase &sys)
bool doDerivatives(const SubProblem &subproblem, const SystemBase &sys)
void derivInsert(SemiDynamicSparseNumberArray< Real, libMesh::dof_id_type, NWrapper< N > > &derivs, libMesh::dof_id_type index, Real value)