https://mooseframework.inl.gov
Loading...
Searching...
No Matches
ADMortarLagrangeConstraint.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
12// MOOSE includes
13#include "MooseVariable.h"
14#include "MoosePreconditioner.h"
16#include "NonlinearSystemBase.h"
17#include "Assembly.h"
18#include "SystemBase.h"
19#include "ADUtils.h"
20#include "FEProblemBase.h"
22
23#include <algorithm>
24#include <vector>
25
28{
30 params.addParam<Real>(
31 "derivative_threshold",
32 1.0e-17,
33 "Threshold to discard automatic differentiation derivatives. This number is chosen on the "
34 "order of the machine epsilon based on current experience.");
35
36 return params;
37}
38
40 : ADMortarConstraint(parameters),
41 _ad_derivative_threshold(getParam<Real>("derivative_threshold")),
42 _apply_derivative_threshold(true)
43{
44}
45
46void
48{
50
51 // Detect if preconditioner is VCP. If so, disable automatic derivative trimming.
53
54 if (dynamic_cast<const VariableCondensationPreconditioner *>(mpc))
56}
57
58void
60{
65
67
68 unsigned int test_space_size = 0;
69 switch (mortar_type)
70 {
71 case Moose::MortarType::Secondary:
73 test_space_size = _test_secondary.size();
74 break;
75
76 case Moose::MortarType::Primary:
78 test_space_size = _test_primary.size();
79 break;
80
81 case Moose::MortarType::Lower:
82 mooseAssert(_var, "LM variable is null");
84 test_space_size = _test.size();
85 break;
86 }
87
88 std::vector<unsigned int> is_index_on_lower_dimension;
89 // Find out if nodes are on the surface
90 for (_i = 0; _i < test_space_size; _i++)
91 {
92 if (mortar_type == Moose::MortarType::Primary && !_primary_ip_lowerd_map.count(_i))
93 continue;
94
95 if (mortar_type == Moose::MortarType::Secondary && !_secondary_ip_lowerd_map.count(_i))
96 continue;
97
98 is_index_on_lower_dimension.push_back(_i);
99 }
100
101 for (_qp = 0; _qp < _qrule_msm->n_points(); _qp++)
102 {
104 for (const auto index : is_index_on_lower_dimension)
105 {
106 _i = index;
107 _local_re(_i) += raw_value(_JxW_msm[_qp] * _coord[_qp] * computeQpResidual(mortar_type));
108 }
109 }
110
112}
113
114void
116{
117 std::vector<ADReal> residuals;
118 size_t test_space_size = 0;
119 typedef Moose::ConstraintJacobianType JType;
120 typedef Moose::MortarType MType;
121 std::vector<JType> jacobian_types;
122 std::vector<dof_id_type> dof_indices;
123 Real scaling_factor = 1;
124
129
131
132 switch (mortar_type)
133 {
134 case MType::Secondary:
135 dof_indices = _secondary_var.dofIndices();
136 jacobian_types = {JType::SecondarySecondary, JType::SecondaryPrimary, JType::SecondaryLower};
137 scaling_factor = _secondary_var.scalingFactor();
138 break;
139
140 case MType::Primary:
141 dof_indices = _primary_var.dofIndicesNeighbor();
142 jacobian_types = {JType::PrimarySecondary, JType::PrimaryPrimary, JType::PrimaryLower};
143 scaling_factor = _primary_var.scalingFactor();
144 break;
145
146 case MType::Lower:
147 mooseAssert(_var, "This should be non-null");
148 dof_indices = _var->dofIndicesLower();
149 jacobian_types = {JType::LowerSecondary, JType::LowerPrimary, JType::LowerLower};
150 scaling_factor = _var->scalingFactor();
151 break;
152 }
153 test_space_size = dof_indices.size();
154
155 residuals.resize(test_space_size, 0);
156
157 std::vector<unsigned int> is_index_on_lower_dimension;
158 std::vector<dof_id_type> dof_indices_lower;
159
160 // Find out if nodes are on the surface
161 unsigned int number_indices_on_lowerd = 0;
162
163 for (_i = 0; _i < test_space_size; _i++)
164 {
165 if (mortar_type == Moose::MortarType::Primary && !_primary_ip_lowerd_map.count(_i))
166 continue;
167
168 if (mortar_type == Moose::MortarType::Secondary && !_secondary_ip_lowerd_map.count(_i))
169 continue;
170
171 is_index_on_lower_dimension.push_back(_i);
172 dof_indices_lower.push_back(dof_indices[_i]);
173 number_indices_on_lowerd++;
174 }
175
176 std::vector<ADReal> residuals_lower;
177 residuals_lower.resize(number_indices_on_lowerd, 0);
178
179 // Only populate nodal residuals on the primary/secondary surfaces
180 // We do this regardless of whether we are interpolating normals. Use of this class
181 // implies we have Lagrange elements, so internal (high-dimensional) normals have no meaning
182 // and should be zero. As such, we decide to omit them and avoid possible spurious population of
183 // automatic differentiation-generated derivatives.
184 for (_qp = 0; _qp < _qrule_msm->n_points(); _qp++)
185 {
187 unsigned int index_lower = 0;
188 for (const auto index : is_index_on_lower_dimension)
189 {
190 _i = index;
191 residuals_lower[index_lower] += _JxW_msm[_qp] * _coord[_qp] * computeQpResidual(mortar_type);
192
193 // Get rid of derivatives that we assume won't count (tolerance prescribed by user)
194 // This can cause zero diagonal terms with the variable condensation preconditioner when the
195 // no adaptivity option is used (dofs are not checked).
196 // Uncomment when https://github.com/libMesh/MetaPhysicL/pull/18 makes it to MOOSE
197
198 index_lower++;
199 }
200 }
201
202 // cacheJacobian() uses the first row's column layout. Every row here has that same derivative
203 // support because the interpolated quadrature-point traction is independent of the test-function
204 // index and multiplication by a Real preserves the AD sparsity pattern.
205 addResidualsAndJacobian(_assembly, residuals_lower, dof_indices_lower, scaling_factor);
206}
static InputParameters validParams()
virtual ADReal computeQpResidual(Moose::MortarType mortar_type)=0
ADMortarLagrangeConstraint(const InputParameters &parameters)
bool _apply_derivative_threshold
Whether to apply derivative trimming.
virtual void precomputeQpQuantities()
Compute quantities that depend on the quadrature point but not on the test function index.
std::map< unsigned int, unsigned int > _primary_ip_lowerd_map
Nodal map from primary interior parent to lower dimensional domain.
std::map< unsigned int, unsigned int > _secondary_ip_lowerd_map
Nodal map from secondary interior parent to lower dimensional domain.
virtual void computeJacobian() override
static InputParameters validParams()
virtual void computeResidual() override
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)
unsigned int _qp
unsigned int _i
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
virtual const std::vector< dof_id_type > & dofIndices() const
void scalingFactor(const std::vector< Real > &factor)
unsigned int number() const
const std::vector< dof_id_type > & dofIndicesLower() const final
virtual const std::vector< dof_id_type > & dofIndicesNeighbor() const=0
MooseVariableField< Real > & _primary_var
MooseVariable *const _var
const VariableTestValue & _test_secondary
const MooseArray< Real > & _coord
MooseVariableField< Real > & _secondary_var
const FEProblemBase & feProblem() const
const VariableTestValue & _test
const VariableTestValue & _test_primary
Elem const *const & _lower_secondary_elem
const libMesh::QBase *const & _qrule_msm
const AutomaticMortarGeneration & amg() const
Elem const *const & _lower_primary_elem
const std::vector< Real > & _JxW_msm
MoosePreconditioner const * getPreconditioner() const
Assembly & _assembly
SystemBase & _sys
virtual void initialSetup()
unsigned int number() const
void prepareVectorTagLower(Assembly &assembly, unsigned int ivar)
void accumulateTaggedLocalResidual()
void addResidualsAndJacobian(Assembly &assembly, const Residuals &residuals, const Indices &dof_indices, Real scaling_factor)
void prepareVectorTagNeighbor(Assembly &assembly, unsigned int ivar)
void prepareVectorTag(Assembly &assembly, unsigned int ivar)
DenseVector< Number > _local_re
unsigned int n_points() const
ConstraintJacobianType