www.mooseframework.org
EqualValueEmbeddedConstraint.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 // MOOSE includes
12 #include "FEProblem.h"
13 #include "DisplacedProblem.h"
14 #include "AuxiliarySystem.h"
15 #include "SystemBase.h"
16 #include "Assembly.h"
17 #include "MooseMesh.h"
18 #include "Executioner.h"
19 #include "AddVariableAction.h"
20 
21 #include "libmesh/string_to_enum.h"
22 #include "libmesh/sparse_matrix.h"
23 
25 
28 {
31  params.addClassDescription("This is a constraint enforcing overlapping portions of two blocks to "
32  "have the same variable value");
33  params.set<bool>("use_displaced_mesh") = false;
34  MooseEnum formulation("kinematic penalty", "kinematic");
35  params.addParam<MooseEnum>(
36  "formulation", formulation, "Formulation used to enforce the constraint");
37  params.addRequiredParam<Real>(
38  "penalty",
39  "Penalty parameter used in constraint enforcement for kinematic and penalty formulations.");
40 
41  return params;
42 }
43 
45  : NodeElemConstraint(parameters),
46  _displaced_problem(parameters.get<FEProblemBase *>("_fe_problem_base")->getDisplacedProblem()),
47  _fe_problem(*parameters.get<FEProblem *>("_fe_problem")),
48  _formulation(getParam<MooseEnum>("formulation").getEnum<Formulation>()),
49  _penalty(getParam<Real>("penalty")),
50  _residual_copy(_sys.residualGhosted())
51 {
54 }
55 
56 void
58 {
59  // get mesh pointLocator
60  std::unique_ptr<PointLocatorBase> pointLocator = _mesh.getPointLocator();
61  pointLocator->enable_out_of_mesh_mode();
62  const std::set<subdomain_id_type> allowed_subdomains{_primary};
63 
64  // secondary id and primary id
65  dof_id_type sid, mid;
66 
67  // prepare _secondary_to_primary_map
68  std::set<dof_id_type> unique_secondary_node_ids;
69  const MeshBase & meshhelper = _mesh.getMesh();
70  for (const auto & elem : as_range(meshhelper.active_subdomain_elements_begin(_secondary),
71  meshhelper.active_subdomain_elements_end(_secondary)))
72  {
73  for (auto & sn : elem->node_ref_range())
74  {
75  sid = sn.id();
77  {
78  // primary element
79  const Elem * me = pointLocator->operator()(sn, &allowed_subdomains);
80  if (me != NULL)
81  {
82  mid = me->id();
83  _secondary_to_primary_map.insert(std::pair<dof_id_type, dof_id_type>(sid, mid));
85  }
86  }
87  }
88  }
89 }
90 
91 bool
93 {
94  // primary element
95  auto it = _secondary_to_primary_map.find(_current_node->id());
96 
97  if (it != _secondary_to_primary_map.end())
98  {
99  const Elem * primary_elem = _mesh.elemPtr(it->second);
100  std::vector<Point> points = {*_current_node};
101 
102  // reinit variables on the primary element at the secondary point
103  _fe_problem.setNeighborSubdomainID(primary_elem, 0);
104  _fe_problem.reinitNeighborPhys(primary_elem, points, 0);
105 
107 
108  return true;
109  }
110  return false;
111 }
112 
113 void
115 {
116  const Node * node = _current_node;
117  unsigned int sys_num = _sys.number();
118  dof_id_type dof_number = node->dof_number(sys_num, _var.number(), 0);
119 
120  switch (_formulation)
121  {
123  _constraint_residual = -_residual_copy(dof_number);
124  break;
125 
128  break;
129 
130  default:
131  mooseError("Invalid formulation");
132  break;
133  }
134 }
135 
136 Real
138 {
139  return _u_secondary[_qp];
140 }
141 
142 Real
144 {
145  Real resid = _constraint_residual;
146 
147  switch (type)
148  {
149  case Moose::Secondary:
150  {
152  {
153  Real pen_force = _penalty * (_u_secondary[_qp] - _u_primary[_qp]);
154  resid += pen_force;
155  }
156  return _test_secondary[_i][_qp] * resid;
157  }
158 
159  case Moose::Primary:
160  return _test_primary[_i][_qp] * -resid;
161  }
162 
163  return 0.0;
164 }
165 
166 Real
168 {
169  unsigned int sys_num = _sys.number();
170  const Real penalty = _penalty;
171  Real curr_jac, secondary_jac;
172 
173  switch (type)
174  {
176  switch (_formulation)
177  {
179  curr_jac = (*_jacobian)(_current_node->dof_number(sys_num, _var.number(), 0),
181  return -curr_jac + _phi_secondary[_j][_qp] * penalty * _test_secondary[_i][_qp];
183  return _phi_secondary[_j][_qp] * penalty * _test_secondary[_i][_qp];
184  default:
185  mooseError("Invalid formulation");
186  }
187 
189  switch (_formulation)
190  {
192  return -_phi_primary[_j][_qp] * penalty * _test_secondary[_i][_qp];
194  return -_phi_primary[_j][_qp] * penalty * _test_secondary[_i][_qp];
195  default:
196  mooseError("Invalid formulation");
197  }
198 
200  switch (_formulation)
201  {
203  secondary_jac = (*_jacobian)(_current_node->dof_number(sys_num, _var.number(), 0),
205  return secondary_jac * _test_primary[_i][_qp];
207  return -_phi_secondary[_j][_qp] * penalty * _test_primary[_i][_qp];
208  default:
209  mooseError("Invalid formulation");
210  }
211 
213  switch (_formulation)
214  {
216  return 0.0;
218  return _test_primary[_i][_qp] * penalty * _phi_primary[_j][_qp];
219  default:
220  mooseError("Invalid formulation");
221  }
222 
223  default:
224  mooseError("Unsupported type");
225  break;
226  }
227  return 0.0;
228 }
229 
230 Real
232  unsigned int /*jvar*/)
233 {
234  Real curr_jac, secondary_jac;
235  unsigned int sys_num = _sys.number();
236 
237  switch (type)
238  {
240  curr_jac = (*_jacobian)(_current_node->dof_number(sys_num, _var.number(), 0),
242  return -curr_jac;
243 
245  return 0.0;
246 
248  switch (_formulation)
249  {
251  secondary_jac = (*_jacobian)(_current_node->dof_number(sys_num, _var.number(), 0),
253  return secondary_jac * _test_primary[_i][_qp];
255  return 0.0;
256  default:
257  mooseError("Invalid formulation");
258  }
259 
261  return 0.0;
262 
263  default:
264  mooseError("Unsupported type");
265  break;
266  }
267 
268  return 0.0;
269 }
MooseMesh & _mesh
Reference to this Kernel&#39;s mesh object.
unsigned short _secondary
secondary block id
static InputParameters validParams()
bool shouldApply() override
Whether or not this constraint should be applied.
ConstraintType
Definition: MooseTypes.h:670
const VariableTestValue & _test_primary
Side test function.
virtual Elem * elemPtr(const dof_id_type i)
Definition: MooseMesh.C:2863
bool _overwrite_secondary_residual
Whether or not the secondary&#39;s residual should be overwritten.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Definition: FEProblem.h:20
unsigned int number() const
Get variable number coming from libMesh.
unsigned short _primary
primary block id
virtual Real computeQpOffDiagJacobian(Moose::ConstraintJacobianType type, unsigned int jvar) override
This is the virtual that derived classes should override for computing the off-diag Jacobian...
T * get(const std::unique_ptr< T > &u)
The MooseUtils::get() specializations are used to support making forwards-compatible code changes fro...
Definition: MooseUtils.h:1147
Real _constraint_residual
constraint force needed to enforce the constraint
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
void reinitConstraint()
Prepare the residual contribution of the current constraint required to enforce it based on the speci...
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
NumericVector< Number > & _residual_copy
copy of the residual before the constraint is applied
unsigned int _i
Definition: Constraint.h:35
const VariableValue & _u_secondary
Value of the unknown variable on the secondary node.
Formulation
Formulations, currently only supports KINEMATIC and PENALTY.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
SystemBase & _sys
Reference to the EquationSystem object.
MooseVariable & _var
unsigned int _j
Definition: Constraint.h:35
static MooseEnum getNonlinearVariableOrders()
Get the possible variable orders.
registerMooseObject("MooseApp", EqualValueEmbeddedConstraint)
const Real _penalty
Penalty parameter used in constraint enforcement for kinematic and penalty formulations.
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:3198
const VariableValue & _u_primary
Holds the current solution at the current quadrature point.
virtual void reinitNeighborPhys(const Elem *neighbor, unsigned int neighbor_side, const std::vector< Point > &physical_points, const THREAD_ID tid) override
std::map< dof_id_type, dof_id_type > _secondary_to_primary_map
maps secondary node ids to primary element ids
SubProblem & _subproblem
Reference to this kernel&#39;s SubProblem.
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
enum EqualValueEmbeddedConstraint::Formulation _formulation
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:50
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
unsigned int number() const
Gets the number of this system.
Definition: SystemBase.C:1125
EqualValueEmbeddedConstraint(const InputParameters &parameters)
virtual Real computeQpResidual(Moose::ConstraintType type) override
This is the virtual that derived classes should override for computing the residual.
const VariablePhiValue & _phi_primary
Side shape function.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const Node *const & _current_node
current node being processed
virtual void addGhostedElem(dof_id_type elem_id)=0
Will make sure that all dofs connected to elem_id are ghosted to this processor.
ConstraintJacobianType
Definition: MooseTypes.h:709
std::vector< dof_id_type > _connected_dof_indices
dofs connected to the secondary node
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
virtual Real computeQpJacobian(Moose::ConstraintJacobianType type) override
This is the virtual that derived classes should override for computing the Jacobian.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
A EqualValueEmbeddedConstraint forces the value of a variable to be the same on overlapping portion o...
virtual void prepareSecondaryToPrimaryMap() override
prepare the _secondary_to_primary_map
A NodeElemConstraint is used when you need to create constraints between a secondary node and a prima...
VariableTestValue _test_secondary
Shape function on the secondary side. This will always only have one entry and that entry will always...
virtual std::unique_ptr< PointLocatorBase > getPointLocator() const
Proxy function to get a (sub)PointLocator from either the underlying libMesh mesh (default)...
Definition: MooseMesh.C:3479
VariablePhiValue _phi_secondary
Shape function on the secondary side.
unsigned int _qp
Definition: Constraint.h:36
virtual Real computeQpSecondaryValue() override
Compute the value the secondary node should have at the beginning of a timestep.
virtual void setNeighborSubdomainID(const Elem *elem, unsigned int side, const THREAD_ID tid) override
uint8_t dof_id_type