https://mooseframework.inl.gov
Loading...
Searching...
No Matches
EqualValueBoundaryConstraint.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
10// MOOSE includes
12#include "MooseMesh.h"
13
15
18{
21 "Constraint for enforcing that variables on each side of a boundary are equivalent.");
22 params.addParam<unsigned int>(
23 "primary",
24 "The ID of the primary node. If no ID is provided, first node of secondary set is chosen.");
25 params.addParam<Point>("primary_node_coord", "Coordinates of the primary node to locate.");
26 params.addParam<std::vector<unsigned int>>("secondary_node_ids", "The IDs of the secondary node");
27 params.addParam<BoundaryName>("secondary", "The boundary ID associated with the secondary side");
28 params.addRequiredParam<Real>("penalty", "The penalty used for the boundary term");
29 return params;
30}
31
33 : NodalConstraint(parameters), _penalty(getParam<Real>("penalty"))
34{
36}
37
38void
44
45void
47{
48 // user provided nothing
49 if (!isParamValid("secondary_node_ids") && !isParamValid("secondary"))
50 paramError("secondary", "Either secondary or secondary_node_ids must be provided.");
51
52 // user provided conflicting params
53 if (isParamValid("secondary_node_ids") && isParamValid("secondary"))
54 paramError("secondary",
55 "Both 'secondary' and 'secondary_node_ids' parameters are set. They are mutually "
56 "exclusive.");
57
58 // Fill in _connected_nodes, which defines local secondary nodes in the base class
59 //
60 // Note that later on when we pick the primary node from the secondary node set, we
61 // will make sure to remove it from _connected_nodes
62
63 // user provided boundary name
64 if (isParamValid("secondary"))
65 {
66 const auto & secondary_bnd = getParam<BoundaryName>("secondary");
68 }
69 // user provided node ids
70 else if (isParamValid("secondary_node_ids"))
71 {
72 const auto & secondary_node_ids = getParam<std::vector<unsigned int>>("secondary_node_ids");
73 _connected_nodes = localSecondaryNodes({secondary_node_ids.begin(), secondary_node_ids.end()});
74 }
75}
76
77void
79{
80 // user provided nothing
81 if (isParamValid("primary") && isParamValid("primary_node_coord"))
83 "Both 'primary' and 'primary_node_coord' parameters are set. They are mutually exclusive.");
84
85 dof_id_type primary_node_id = Node::invalid_id;
86
87 // user provided primary node coordinates
88 if (isParamValid("primary_node_coord"))
89 primary_node_id = getPrimaryNodeIDByCoord();
90 // user provided primary node id
91 else if (isParamValid("primary"))
92 primary_node_id = getParam<unsigned int>("primary");
93 // no primary node provided, so we pick one from secondary nodes
94 else
95 {
96 // If the user provided secondary node ids, set primary node to first one
97 if (isParamValid("secondary_node_ids"))
98 {
99 if (_connected_nodes.size())
100 primary_node_id = _connected_nodes[0];
101 }
102 // otherwise, we pick the minimum node id from the secondary node set
103 else
104 {
105 if (_connected_nodes.size())
106 primary_node_id = (*std::min_element(_connected_nodes.begin(), _connected_nodes.end()));
107 _mesh.comm().min(primary_node_id);
108 }
109 }
110
111 mooseAssert(primary_node_id != Node::invalid_id, "We should have found a primary node");
112
113 // Remove primary node from secondary nodes
114 auto it = std::find(_connected_nodes.begin(), _connected_nodes.end(), primary_node_id);
115 if (it != _connected_nodes.end())
116 _connected_nodes.erase(it);
117
118 // Store primary node id
119 _primary_node_vector.resize(1);
120 _primary_node_vector[0] = primary_node_id;
121}
122
123dof_id_type
125{
126 const Real eps = libMesh::TOLERANCE;
127 std::unordered_set<dof_id_type> local_primary_node_ids;
128 const auto & primary_node_coord = getParam<Point>("primary_node_coord");
129
130 // Gather local candidates
131 for (const auto & bnd_node : *_mesh.getBoundaryNodeRange())
132 {
133 if ((*(bnd_node->_node) - primary_node_coord).norm() < eps)
134 {
135 // The primary node we found should belong to the secondary node set
136 //
137 // Note that we do not immediately break once a match is found because in theory, although it
138 // is extremely unlikely, there could be multiple coinciding nodes on the secondary boundary
139 // that match the provided coordinates. In that case, we would like to emit a meaning error
140 // message.
141 if (std::find(_connected_nodes.begin(), _connected_nodes.end(), bnd_node->_node->id()) !=
142 _connected_nodes.end())
143 local_primary_node_ids.insert(bnd_node->_node->id());
144 }
145 }
146
147 // Gather all candidates from all ranks
148 const std::vector<dof_id_type> local_node_vec(local_primary_node_ids.begin(),
149 local_primary_node_ids.end());
150 std::vector<std::vector<dof_id_type>> gathered_node_vecs;
151 _mesh.comm().allgather(local_node_vec, gathered_node_vecs);
152
153 // Deduplicate
154 std::unordered_set<dof_id_type> global_primary_node_ids;
155 for (const auto & vec : gathered_node_vecs)
156 global_primary_node_ids.insert(vec.begin(), vec.end());
157
158 // Make sure we found one and exactly one
159 if (global_primary_node_ids.size() == 0)
160 mooseError("Couldn't find a node ID for the specified primary_node_coord.");
161 else if (global_primary_node_ids.size() > 1)
162 mooseError("Multiple nodes found for the specified primary_node_coord.");
163
164 return *global_primary_node_ids.begin();
165}
166
167Real
169{
170 switch (type)
171 {
172 case Moose::Secondary:
173 return (_u_secondary[_i] - _u_primary[_j]) * _penalty;
174 case Moose::Primary:
175 return (_u_primary[_j] - _u_secondary[_i]) * _penalty;
176 }
177 return 0.;
178}
179
180Real
182{
183 switch (type)
184 {
186 return _penalty;
188 return -_penalty;
190 return _penalty;
192 return -_penalty;
193 default:
194 mooseError("Unsupported type");
195 break;
196 }
197 return 0.;
198}
registerMooseObject("MooseApp", EqualValueBoundaryConstraint)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
dof_id_type getPrimaryNodeIDByCoord() const
Get the primary node ID by searching for the node with coordinates matching _primary_node_coord on th...
virtual Real computeQpJacobian(Moose::ConstraintJacobianType type) override
Computes the jacobian for the constraint.
void pickPrimaryNode()
Pick the primary node from user input or from the secondary node set.
void populateSecondaryNodes()
Populate the set of secondary nodes from user input.
EqualValueBoundaryConstraint(const InputParameters &parameters)
Real _penalty
Penalty if constraint is not satisfied.
virtual void updateConnectivity() override
Update the sets of nodes with constrained DOFs.
virtual Real computeQpResidual(Moose::ConstraintType type) override
Computes the residual for the current secondary node.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object.
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...
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.
const std::string & type() const
Get the type of this class.
Definition MooseBase.h:93
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition MooseBase.h:466
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition MooseBase.h:199
BoundaryID getBoundaryID(const BoundaryName &boundary_name) const
Get the associated BoundaryID for the boundary name.
Definition MooseMesh.C:1683
const std::vector< dof_id_type > & getNodeList(boundary_id_type nodeset_id) const
Return a writable reference to a vector of node IDs that belong to nodeset_id.
Definition MooseMesh.C:3603
libMesh::StoredRange< MooseMesh::const_bnd_node_iterator, const BndNode * > * getBoundaryNodeRange()
Definition MooseMesh.C:1300
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.
std::vector< dof_id_type > _primary_node_vector
node IDs of the primary node
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.
static InputParameters validParams()
const VariableValue & _u_secondary
Value of the unknown variable this BC is action on.
const VariableValue & _u_primary
Holds the current solution at the current quadrature point.
MooseMesh & _mesh
Reference to this Kernel's mesh object.
void min(const T &r, T &o, Request &req) const
void allgather(const T &send_data, std::vector< T, A > &recv_data) const
const Parallel::Communicator & comm() const
ConstraintType
Definition MooseTypes.h:817
@ Primary
Definition MooseTypes.h:819
@ Secondary
Definition MooseTypes.h:818
ConstraintJacobianType
Definition MooseTypes.h:856
@ SecondarySecondary
Definition MooseTypes.h:857
@ SecondaryPrimary
Definition MooseTypes.h:858
@ PrimarySecondary
Definition MooseTypes.h:859
@ PrimaryPrimary
Definition MooseTypes.h:860
static constexpr Real TOLERANCE