20#include "libmesh/compare_elems_by_level.h"
21#include "libmesh/distributed_mesh.h"
22#include "libmesh/null_output_iterator.h"
23#include "libmesh/parallel_elem.h"
24#include "libmesh/parallel_node.h"
25#include "libmesh/sparse_matrix.h"
33 MooseEnum formulationtype(
"penalty kinematic",
"penalty");
36 "Formulation used to calculate constraint - penalty or kinematic.");
37 params.
addParam<NonlinearVariableName>(
"variable_secondary",
38 "The name of the variable for the secondary nodes, if it "
39 "is different from the primary nodes' variable");
47 this, true,
Moose::VarKindType::VAR_SOLVER,
Moose::VarFieldType::VAR_FIELD_STANDARD),
48 _var(_sys.getFieldVariable<Real>(_tid, parameters.get<NonlinearVariableName>(
"variable"))),
49 _var_secondary(_sys.getFieldVariable<Real>(
51 isParamValid(
"variable_secondary")
52 ? parameters.get<NonlinearVariableName>(
"variable_secondary")
53 : parameters.get<NonlinearVariableName>(
"variable"))),
54 _u_secondary(_var_secondary.dofValuesNeighbor()),
55 _u_primary(_var.dofValues())
60 MooseEnum temp_formulation = getParam<MooseEnum>(
"formulation");
61 if (temp_formulation ==
"penalty")
63 else if (temp_formulation ==
"kinematic")
66 mooseError(
"Formulation must be either Penalty or Kinematic");
69std::vector<dof_id_type>
71 const std::vector<dof_id_type> & node_ids)
73 const auto & node_to_elem_map =
mesh.nodeToElemMap();
88 retained_elems.clear();
90 std::set<Elem *, libMesh::CompareElemIdsByLevel> elems_to_ghost;
91 std::set<Node *> nodes_to_ghost;
94 for (
const auto node_id : node_ids)
96 const auto node_to_elem_pair = node_to_elem_map.find(node_id);
99 bool someone_found_elem =
false;
102 if (node_to_elem_pair != node_to_elem_map.end())
103 for (
const auto elem_id : node_to_elem_pair->second)
104 if (
auto *
const elem =
mesh.queryElemPtr(elem_id))
106 elems_to_ghost.insert(elem);
107 for (
const auto n : make_range(elem->n_nodes()))
108 nodes_to_ghost.insert(elem->node_ptr(n));
110 someone_found_elem =
true;
117 mesh.getMesh().comm().max(someone_found_elem);
118 mooseAssert(someone_found_elem || node_ids.empty(),
"Missing entry in node to elem map");
123 mesh.getMesh().comm().allgather_packed_range(&
mesh.getMesh(),
124 nodes_to_ghost.begin(),
125 nodes_to_ghost.end(),
127 mesh.getMesh().comm().allgather_packed_range(&
mesh.getMesh(),
128 elems_to_ghost.begin(),
129 elems_to_ghost.end(),
137 std::vector<dof_id_type> elem_ids;
138 for (
const auto node_id : node_ids)
141 const auto node_to_elem_pair = node_to_elem_map.find(node_id);
142 if (node_to_elem_pair == node_to_elem_map.end() || node_to_elem_pair->second.empty())
143 mooseError(
"Couldn't find any elements connected to primary node");
146 node_to_elem_pair->second.front();
148 elem_ids.push_back(elem_id);
151 if (distributed_mesh)
153 auto *
const elem =
mesh.elemPtr(elem_id);
154 distributed_mesh->add_extra_ghost_elem(elem);
155 retained_elems.insert(elem);
160 mooseAssert(node_ids.size() == elem_ids.size(),
161 "Mismatch between number of primary nodes and connected elements");
166std::vector<dof_id_type>
171 std::vector<dof_id_type> local_nodes;
172 for (
const auto node_id : node_ids)
175 const Node *
const node = lm_mesh.query_node_ptr(node_id);
176 if (node && node->processor_id() == lm_mesh.processor_id())
177 local_nodes.push_back(node_id);
205 for (
const auto elem_id : elem_ids)
210 const auto displaced_elem_ids =
212 if (displaced_elem_ids != elem_ids)
213 mooseError(
"Reference and displaced meshes selected different primary elements");
236 " primary degrees of freedom for ",
238 " primary nodes. Nodal constraints index per-primary data by position in the "
239 "primary node list, so variable '",
241 "' must be defined on every primary node.");
253 DenseVector<Number> re(primarydof.size());
254 DenseVector<Number> neighbor_re(secondarydof.size());
259 for (
_i = 0;
_i < secondarydof.size(); ++
_i)
261 for (
_j = 0;
_j < primarydof.size(); ++
_j)
271 Real res = residual(secondarydof[
_i]);
280 if (!primarydof.empty())
282 if (!secondarydof.empty())
296 DenseMatrix<Number> Ken(primarydof.size(), secondarydof.size());
297 DenseMatrix<Number> Kne(secondarydof.size(), primarydof.size());
306 std::vector<Number> Kee_diag(primarydof.size(), 0.);
307 std::vector<Number> Knn_diag(secondarydof.size(), 0.);
309 for (
_i = 0;
_i < secondarydof.size(); ++
_i)
311 for (
_j = 0;
_j < primarydof.size(); ++
_j)
326 Kne(
_i,
_j) += -jacobian(secondarydof[
_i], primarydof[
_j]) / primarydof.size() +
328 Knn_diag[
_i] += -jacobian(secondarydof[
_i], secondarydof[
_i]) / primarydof.size() +
337 for (
const auto j : index_range(primarydof))
340 for (
const auto i : index_range(secondarydof))
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
std::shared_ptr< DisplacedProblem > displaced_problem
Base class for all Constraint types.
static InputParameters validParams()
virtual std::shared_ptr< const DisplacedProblem > getDisplacedProblem() const
virtual MooseMesh & mesh() override
const std::string & name() const
Get the name of the class.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
void scalingFactor(const std::vector< Real > &factor)
Set the scaling factor for this variable.
void addMooseVariableDependency(MooseVariableFieldBase *var)
Call this function to add the passed in MooseVariableFieldBase as a variable that this object depends...
const std::vector< dof_id_type > & dofIndicesNeighbor() const final
Get neighbor DOF indices for currently selected element.
const std::vector< dof_id_type > & dofIndices() const final
Get local DoF indices.
Enhances MooseVariableInterface interface provide values from neighbor elements.
virtual void computeResidual() override final
Computes the nodal residual.
Moose::ConstraintFormulationType _formulation
Specifies formulation type used to apply constraints.
std::vector< dof_id_type > localSecondaryNodes(const std::vector< dof_id_type > &node_ids) const
Select the nodes of 'node_ids' that this process owns, which are the secondary nodes it is responsibl...
unsigned int _i
Counter for primary and secondary nodes.
virtual Real computeQpResidual(Moose::ConstraintType type)=0
This is the virtual that derived classes should override for computing the residual on neighboring el...
std::vector< dof_id_type > _primary_node_vector
node IDs of the primary node
NodalConstraint(const InputParameters ¶meters)
std::vector< dof_id_type > _connected_nodes
node IDs connected to the primary node (secondary nodes)
void updateConstrainedNodes()
Rebuild the constrained node lists and the ghosting they require.
std::vector< Real > _weights
When the secondary node is constrained to move as a linear combination of the primary nodes,...
std::vector< dof_id_type > gatherAndRetainConnectedElems(MooseMesh &mesh, const std::vector< dof_id_type > &node_ids)
Gather and retain elements connected to the provided nodes on the provided mesh.
std::map< MooseMesh *, std::set< Elem * > > _retained_elems
Elements this constraint retained on each distributed mesh during the previous mesh update.
static InputParameters validParams()
virtual void updateConnectivity()
Fill _primary_node_vector and _connected_nodes for this constraint.
virtual Real computeQpJacobian(Moose::ConstraintJacobianType type)=0
This is the virtual that derived classes should override for computing the Jacobian on neighboring el...
virtual void computeJacobian() override final
Computes the jacobian for the current element.
MooseVariable & _var_secondary
void reinitConstraintNodes()
Reinitialize the primary and secondary nodes on the SubProblem that owns this constraint's variables.
void ghostConnectedElems(const std::vector< dof_id_type > &node_ids)
Gather and retain one element per node on the reference mesh, and on the displaced mesh when one exis...
THREAD_ID _tid
The thread ID for this kernel.
MooseMesh & _mesh
Reference to this Kernel's mesh object.
Assembly & _assembly
Reference to this Kernel's assembly object.
SubProblem & _subproblem
Reference to this kernel's SubProblem.
FEProblemBase & _fe_problem
Reference to this kernel's FEProblemBase.
void reinitNodesNeighbor(const std::vector< dof_id_type > &nodes, const THREAD_ID tid)
void reinitNodes(const std::vector< dof_id_type > &nodes, const THREAD_ID tid)
virtual void addGhostedElem(dof_id_type elem_id)=0
Will make sure that all dofs connected to elem_id are ghosted to this processor.
void addJacobianElement(Assembly &assembly, Real value, dof_id_type row_index, dof_id_type column_index, Real scaling_factor)
Add into a single Jacobian element.
void addJacobian(Assembly &assembly, const Residuals &residuals, const Indices &dof_indices, Real scaling_factor)
Add the provided residual derivatives into the Jacobian for the provided dof indices.
void addResiduals(Assembly &assembly, const Residuals &residuals, const Indices &dof_indices, Real scaling_factor)
Add the provided incoming residuals corresponding to the provided dof indices.
virtual void clear_extra_ghost_elems()
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...