www.mooseframework.org
TaggingInterface.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 #include "TaggingInterface.h"
11 #include "Conversion.h"
12 #include "FEProblem.h"
13 #include "Assembly.h"
15 
16 #include "libmesh/dense_vector.h"
17 
20 {
21 
23 
24  // These are the default names for tags, but users will be able to add their own
25  MultiMooseEnum vtags("nontime time", "nontime", true);
26  MultiMooseEnum mtags("nontime system", "system", true);
27 
28  params.addPrivateParam<bool>("matrix_only", false);
29 
30  params.addParam<MultiMooseEnum>(
31  "vector_tags", vtags, "The tag for the vectors this Kernel should fill");
32 
33  params.addParam<MultiMooseEnum>(
34  "matrix_tags", mtags, "The tag for the matrices this Kernel should fill");
35 
36  params.addParam<std::vector<TagName>>("extra_vector_tags",
37  "The extra tags for the vectors this Kernel should fill");
38 
39  params.addParam<std::vector<TagName>>(
40  "absolute_value_vector_tags",
41  "The tags for the vectors this residual object should fill with the "
42  "absolute value of the residual contribution");
43 
44  params.addParam<std::vector<TagName>>("extra_matrix_tags",
45  "The extra tags for the matrices this Kernel should fill");
46 
47  params.addParamNamesToGroup(
48  "vector_tags matrix_tags extra_vector_tags extra_matrix_tags absolute_value_vector_tags",
49  "Tagging");
50 
51  return params;
52 }
53 
55  : _subproblem(*moose_object->parameters().getCheckedPointerParam<SubProblem *>("_subproblem")),
56  _moose_object(*moose_object),
57  _tag_params(_moose_object.parameters())
58 {
59  auto & vector_tag_names = _tag_params.get<MultiMooseEnum>("vector_tags");
60 
61  if (!vector_tag_names.isValid())
62  {
63  if (!_tag_params.get<bool>("matrix_only"))
64  mooseError("MUST provide at least one vector_tag for Kernel: ", _moose_object.name());
65  }
66  else
67  {
68  for (auto & vector_tag_name : vector_tag_names)
69  {
70  const TagID vector_tag_id = _subproblem.getVectorTagID(vector_tag_name.name());
72  mooseError("Vector tag '",
73  vector_tag_name.name(),
74  "' for Kernel '",
76  "' is not a residual vector tag");
77  _vector_tags.insert(vector_tag_id);
78  }
79  }
80 
81  // Add extra vector tags. These tags should be created in the System already, otherwise
82  // we can not add the extra tags
83  auto & extra_vector_tags = _tag_params.get<std::vector<TagName>>("extra_vector_tags");
84 
85  for (auto & vector_tag_name : extra_vector_tags)
86  {
87  const TagID vector_tag_id = _subproblem.getVectorTagID(vector_tag_name);
89  mooseError("Extra vector tag '",
90  vector_tag_name,
91  "' for Kernel '",
93  "' is not a residual vector tag");
94  _vector_tags.insert(vector_tag_id);
95  }
96 
97  // Add absolue value vector tags. These tags should be created in the System already, otherwise
98  // we can not add the extra tags
99  auto & abs_vector_tags = _tag_params.get<std::vector<TagName>>("absolute_value_vector_tags");
100 
101  for (auto & vector_tag_name : abs_vector_tags)
102  {
103  const TagID vector_tag_id = _subproblem.getVectorTagID(vector_tag_name);
105  mooseError("Absolute value vector tag '",
106  vector_tag_name,
107  "' for Kernel '",
109  "' is not a residual vector tag");
110  _abs_vector_tags.insert(vector_tag_id);
111  }
112 
113  auto & matrix_tag_names = _tag_params.get<MultiMooseEnum>("matrix_tags");
114 
115  if (matrix_tag_names.isValid())
116  for (auto & matrix_tag_name : matrix_tag_names)
117  _matrix_tags.insert(_subproblem.getMatrixTagID(matrix_tag_name.name()));
118 
119  auto & extra_matrix_tags = _tag_params.get<std::vector<TagName>>("extra_matrix_tags");
120 
121  for (auto & matrix_tag_name : extra_matrix_tags)
122  _matrix_tags.insert(_subproblem.getMatrixTagID(matrix_tag_name));
123 
124  _re_blocks.resize(_vector_tags.size());
125  _absre_blocks.resize(_abs_vector_tags.size());
126  _ke_blocks.resize(_matrix_tags.size());
127 
128  const auto * const fe_problem =
129  moose_object->parameters().getCheckedPointerParam<FEProblemBase *>("_fe_problem_base");
130  if (const auto * const ref_problem = dynamic_cast<const ReferenceResidualProblem *>(fe_problem))
131  {
132  const auto reference_tag = ref_problem->referenceVectorTagID({});
133  auto create_tags_split =
134  [reference_tag](const auto & tags, auto & non_ref_tags, auto & ref_tags)
135  {
136  for (const auto tag : tags)
137  if (tag == reference_tag)
138  ref_tags.insert(tag);
139  else
140  non_ref_tags.insert(tag);
141  };
144  }
145  else
146  {
149  }
150 }
151 
152 void
154 {
155  if (!_subproblem.vectorTagExists(tag_name))
156  mooseError("Vector tag ", tag_name, " does not exist in system");
157 
158  _vector_tags.insert(_subproblem.getVectorTagID(tag_name));
159 }
160 
161 void
163 {
164  if (!_subproblem.matrixTagExists(tag_name))
165  mooseError("Matrix tag ", tag_name, " does not exist in system");
166 
167  _matrix_tags.insert(_subproblem.getMatrixTagID(tag_name));
168 }
169 
170 void
172 {
173  if (!_subproblem.vectorTagExists(tag_id))
174  mooseError("Vector tag ", tag_id, " does not exist in system");
175 
176  _vector_tags.insert(tag_id);
177 }
178 
179 void
181 {
182  if (!_subproblem.matrixTagExists(tag_id))
183  mooseError("Matrix tag ", tag_id, " does not exist in system");
184 
185  _matrix_tags.insert(tag_id);
186 }
187 
188 void
189 TaggingInterface::prepareVectorTag(Assembly & assembly, const unsigned int ivar)
190 {
192 }
193 
194 void
196  const unsigned int ivar,
197  const ResidualTagType tag_type)
198 {
199  if (tag_type == ResidualTagType::NonReference)
201  else
203 }
204 
205 void
207  const unsigned int ivar,
208  const std::set<TagID> & vector_tags,
209  const std::set<TagID> & absolute_value_vector_tags)
210 {
211  auto prepare = [this, ivar, &assembly](auto & re_blocks, const auto & tags)
212  {
213  re_blocks.clear();
214  re_blocks.reserve(tags.size());
215  for (const auto tag_id : tags)
216  {
217  const auto & tag = _subproblem.getVectorTag(tag_id);
218  re_blocks.push_back(&assembly.residualBlock(ivar, Assembly::LocalDataKey{}, tag._type_id));
219  }
220  };
221 
222  prepare(_re_blocks, vector_tags);
223  prepare(_absre_blocks, absolute_value_vector_tags);
224 
225  _local_re.resize(_re_blocks.empty()
226  ? (_absre_blocks.empty() ? std::size_t(0) : _absre_blocks[0]->size())
227  : _re_blocks[0]->size());
228 }
229 
230 void
232 {
233  _re_blocks.resize(_vector_tags.size());
234  mooseAssert(_vector_tags.size() >= 1, "we need at least one active tag");
235  auto vector_tag = _vector_tags.begin();
236  for (MooseIndex(_vector_tags) i = 0; i < _vector_tags.size(); i++, ++vector_tag)
237  {
238  const VectorTag & tag = _subproblem.getVectorTag(*vector_tag);
240  }
241  _local_re.resize(_re_blocks[0]->size());
242 
243  _absre_blocks.resize(_abs_vector_tags.size());
244  vector_tag = _abs_vector_tags.begin();
245  for (MooseIndex(_abs_vector_tags) i = 0; i < _abs_vector_tags.size(); i++, ++vector_tag)
246  {
247  const VectorTag & tag = _subproblem.getVectorTag(*vector_tag);
248  _absre_blocks[i] =
249  &assembly.residualBlockNeighbor(ivar, Assembly::LocalDataKey{}, tag._type_id);
250  }
251 }
252 
253 void
254 TaggingInterface::prepareVectorTagLower(Assembly & assembly, unsigned int ivar)
255 {
256  _re_blocks.resize(_vector_tags.size());
257  mooseAssert(_vector_tags.size() >= 1, "we need at least one active tag");
258  auto vector_tag = _vector_tags.begin();
259  for (MooseIndex(_vector_tags) i = 0; i < _vector_tags.size(); i++, ++vector_tag)
260  {
261  const VectorTag & tag = _subproblem.getVectorTag(*vector_tag);
262  _re_blocks[i] = &assembly.residualBlockLower(ivar, Assembly::LocalDataKey{}, tag._type_id);
263  }
264  _local_re.resize(_re_blocks[0]->size());
265 
266  _absre_blocks.resize(_abs_vector_tags.size());
267  vector_tag = _abs_vector_tags.begin();
268  for (MooseIndex(_abs_vector_tags) i = 0; i < _abs_vector_tags.size(); i++, ++vector_tag)
269  {
270  const VectorTag & tag = _subproblem.getVectorTag(*vector_tag);
272  }
273 }
274 
275 void
276 TaggingInterface::prepareMatrixTag(Assembly & assembly, unsigned int ivar, unsigned int jvar)
277 {
278  _ke_blocks.resize(_matrix_tags.size());
279  mooseAssert(_matrix_tags.size() >= 1, "we need at least one active tag");
280  auto mat_vector = _matrix_tags.begin();
281  for (MooseIndex(_matrix_tags) i = 0; i < _matrix_tags.size(); i++, ++mat_vector)
282  _ke_blocks[i] = &assembly.jacobianBlock(ivar, jvar, Assembly::LocalDataKey{}, *mat_vector);
283 
284  _local_ke.resize(_ke_blocks[0]->m(), _ke_blocks[0]->n());
285 }
286 
287 void
289  unsigned int ivar,
290  unsigned int jvar,
291  DenseMatrix<Number> & k) const
292 {
293  mooseAssert(!_matrix_tags.empty(), "No matrix tags exist");
294  const auto & ij_mat =
295  assembly.jacobianBlock(ivar, jvar, Assembly::LocalDataKey{}, *_matrix_tags.begin());
296  k.resize(ij_mat.m(), ij_mat.n());
297 }
298 
299 void
301  unsigned int ivar,
302  unsigned int jvar)
303 {
304  _ke_blocks.resize(_matrix_tags.size());
305  mooseAssert(_matrix_tags.size() >= 1, "we need at least one active tag");
306  auto mat_vector = _matrix_tags.begin();
307  for (MooseIndex(_matrix_tags) i = 0; i < _matrix_tags.size(); i++, ++mat_vector)
308  _ke_blocks[i] =
309  &assembly.jacobianBlockNonlocal(ivar, jvar, Assembly::LocalDataKey{}, *mat_vector);
310 
311  _nonlocal_ke.resize(_ke_blocks[0]->m(), _ke_blocks[0]->n());
312 }
313 
314 void
316  unsigned int ivar,
317  unsigned int jvar,
319 {
320  _ke_blocks.resize(_matrix_tags.size());
321  mooseAssert(_matrix_tags.size() >= 1, "we need at least one active tag");
322  auto mat_vector = _matrix_tags.begin();
323  for (MooseIndex(_matrix_tags) i = 0; i < _matrix_tags.size(); i++, ++mat_vector)
324  _ke_blocks[i] =
325  &assembly.jacobianBlockNeighbor(type, ivar, jvar, Assembly::LocalDataKey{}, *mat_vector);
326 
327  _local_ke.resize(_ke_blocks[0]->m(), _ke_blocks[0]->n());
328 }
329 
330 void
332  unsigned int ivar,
333  unsigned int jvar,
335  DenseMatrix<Number> & k) const
336 {
337  mooseAssert(!_matrix_tags.empty(), "No matrix tags exist");
338  const auto & ij_mat = assembly.jacobianBlockNeighbor(
339  type, ivar, jvar, Assembly::LocalDataKey{}, *_matrix_tags.begin());
340  k.resize(ij_mat.m(), ij_mat.n());
341 }
342 
343 void
345  unsigned int ivar,
346  unsigned int jvar,
348 {
349  _ke_blocks.resize(_matrix_tags.size());
350  mooseAssert(_matrix_tags.size() >= 1, "we need at least one active tag");
351  auto mat_vector = _matrix_tags.begin();
352  for (MooseIndex(_matrix_tags) i = 0; i < _matrix_tags.size(); i++, ++mat_vector)
353  _ke_blocks[i] =
354  &assembly.jacobianBlockMortar(type, ivar, jvar, Assembly::LocalDataKey{}, *mat_vector);
355 
356  _local_ke.resize(_ke_blocks[0]->m(), _ke_blocks[0]->n());
357 }
358 
359 void
361 {
362  for (auto & re : _re_blocks)
363  *re += _local_re;
364  for (auto & absre : _absre_blocks)
365  for (const auto i : index_range(_local_re))
366  (*absre)(i) += std::abs(_local_re(i));
367 }
368 
369 void
371 {
372  for (auto & re : _re_blocks)
373  *re = _local_re;
374  for (auto & absre : _absre_blocks)
375  for (const auto i : index_range(_local_re))
376  (*absre)(i) = std::abs(_local_re(i));
377 }
378 
379 void
381 {
382  for (auto & ke : _ke_blocks)
383  *ke += _local_ke;
384 }
385 
386 void
388  const unsigned int ivar,
389  const unsigned int jvar,
390  const DenseMatrix<Number> & k)
391 {
392  _ke_blocks.resize(_matrix_tags.size());
393  mooseAssert(_matrix_tags.size() >= 1, "we need at least one active tag");
394  auto mat_vector = _matrix_tags.begin();
395  for (MooseIndex(_matrix_tags) i = 0; i < _matrix_tags.size(); i++, ++mat_vector)
396  _ke_blocks[i] = &assembly.jacobianBlock(ivar, jvar, Assembly::LocalDataKey{}, *mat_vector);
397  mooseAssert(_ke_blocks[0]->m() == k.m() && _ke_blocks[0]->n() == k.n(),
398  "Passed-in k must match the blocks we are about to sum into");
399  for (auto & ke : _ke_blocks)
400  *ke += k;
401 }
402 
403 void
405  const unsigned int ivar,
406  const unsigned int jvar,
407  const Moose::DGJacobianType type,
408  const DenseMatrix<Number> & k)
409 {
410  _ke_blocks.resize(_matrix_tags.size());
411  mooseAssert(_matrix_tags.size() >= 1, "we need at least one active tag");
412  auto mat_vector = _matrix_tags.begin();
413  for (MooseIndex(_matrix_tags) i = 0; i < _matrix_tags.size(); i++, ++mat_vector)
414  _ke_blocks[i] =
415  &assembly.jacobianBlockNeighbor(type, ivar, jvar, Assembly::LocalDataKey{}, *mat_vector);
416  mooseAssert(_ke_blocks[0]->m() == k.m() && _ke_blocks[0]->n() == k.n(),
417  "Passed-in k must match the blocks we are about to sum into");
418  for (auto & ke : _ke_blocks)
419  *ke += k;
420 }
421 
422 void
424 {
425  for (auto & ke : _ke_blocks)
426  *ke += _nonlocal_ke;
427 }
428 
429 void
431 {
432  for (auto & ke : _ke_blocks)
433  *ke = _local_ke;
434 }
435 
virtual TagID getVectorTagID(const TagName &tag_name) const
Get a TagID from a TagName.
Definition: SubProblem.C:180
void accumulateTaggedNonlocalMatrix()
Nonlocal Jacobian blocks will be appended by adding the current nonlocal kernel Jacobian.
std::set< TagID > _ref_abs_vector_tags
A set of either size 1 or 0.
SubProblem & _subproblem
SubProblem that contains tag info.
const InputParameters & _tag_params
Parameters from moose object.
void accumulateTaggedLocalResidual()
Local residual blocks will be appended by adding the current local kernel residual.
Keeps track of stuff related to assembling.
Definition: Assembly.h:93
unsigned int TagID
Definition: MooseTypes.h:199
void addPrivateParam(const std::string &name, const T &value)
These method add a parameter to the InputParameters object which can be retrieved like any other para...
DenseMatrix< Number > & jacobianBlockNonlocal(unsigned int ivar, unsigned int jvar, LocalDataKey, TagID tag)
Get local Jacobian block from non-local contribution for a pair of variables and a tag...
Definition: Assembly.h:1103
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
DenseMatrix< Number > & jacobianBlockNeighbor(Moose::DGJacobianType type, unsigned int ivar, unsigned int jvar, LocalDataKey, TagID tag)
Get local Jacobian block of a DG Jacobian type for a pair of variables and a tag. ...
Definition: Assembly.C:3108
void assignTaggedLocalMatrix()
Local Jacobian blocks will assigned as the current local kernel Jacobian.
Class that is used as a parameter to some of our matrix tag APIs that allows only blessed framework c...
void resize(const unsigned int n)
T getCheckedPointerParam(const std::string &name, const std::string &error_string="") const
Verifies that the requested parameter exists and is not NULL and returns it to the caller...
TagTypeID _type_id
The index for this tag into a vector that contains tags of only its type ordered by ID...
Definition: VectorTag.h:45
void assignTaggedLocalResidual()
Local residual blocks will assigned as the current local kernel residual.
std::vector< DenseVector< Number > * > _absre_blocks
Residual blocks for absolute value residual tags.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
unsigned int m() const
ResidualTagType
Enumerate whether a (residual) vector tag is to be of a non-reference or reference tag type...
std::vector< DenseVector< Number > * > _re_blocks
Residual blocks Vectors For each Tag.
std::set< TagID > _ref_vector_tags
A set of either size 1 or 0.
DenseMatrix< Number > & jacobianBlock(unsigned int ivar, unsigned int jvar, LocalDataKey, TagID tag)
Get local Jacobian block for a pair of variables and a tag.
Definition: Assembly.h:1092
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
DenseMatrix< Number > _local_ke
Holds local Jacobian entries as they are accumulated by this Kernel.
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:56
std::set< TagID > _abs_vector_tags
The absolute value residual tag ids.
InputParameters emptyInputParameters()
DenseMatrix< Number > & jacobianBlockMortar(Moose::ConstraintJacobianType type, unsigned int ivar, unsigned int jvar, LocalDataKey, TagID tag)
Returns the jacobian block for the given mortar Jacobian type.
Definition: Assembly.C:3149
ADRealEigenVector< T, D, asd > abs(const ADRealEigenVector< T, D, asd > &)
void prepareMatrixTagNeighbor(Assembly &assembly, unsigned int ivar, unsigned int jvar, Moose::DGJacobianType type)
Prepare data for computing element jacobian according to the active tags for DG and interface kernels...
TaggingInterface(const MooseObject *moose_object)
std::set< TagID > _non_ref_abs_vector_tags
A set to hold absolute value vector tags excluding the reference residual tag.
void prepareMatrixTagNonlocal(Assembly &assembly, unsigned int ivar, unsigned int jvar)
Prepare data for computing nonlocal element jacobian according to the active tags.
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:33
virtual TagID getMatrixTagID(const TagName &tag_name) const
Get a TagID from a TagName.
Definition: SubProblem.C:308
std::set< TagID > _matrix_tags
The matrices this Kernel will contribute to.
virtual Moose::VectorTagType vectorTagType(const TagID tag_id) const
Definition: SubProblem.C:208
DenseVector< Number > & residualBlockLower(unsigned int var_num, LocalDataKey, TagID tag_id)
Get residual block for lower.
Definition: Assembly.h:1083
std::set< TagID > _non_ref_vector_tags
A set to hold vector tags excluding the reference residual tag.
std::set< TagID > _vector_tags
The residual tag ids this Kernel will contribute to.
virtual bool vectorTagExists(const TagID tag_id) const
Check to see if a particular Tag exists.
Definition: SubProblem.h:163
void accumulateTaggedLocalMatrix()
Local Jacobian blocks will be appended by adding the current local kernel Jacobian.
void useMatrixTag(const TagName &tag_name, MatrixTagsKey)
DGJacobianType
Definition: MooseTypes.h:662
void prepareVectorTagNeighbor(Assembly &assembly, unsigned int ivar)
Prepare data for computing element residual the according to active tags for DG and interface kernels...
Generic class for solving transient nonlinear problems.
Definition: SubProblem.h:75
DenseVector< Number > & residualBlockNeighbor(unsigned int var_num, LocalDataKey, TagID tag_id)
Get local neighbor residual block for a variable and a tag.
Definition: Assembly.h:1074
DenseVector< Number > & residualBlock(unsigned int var_num, LocalDataKey, TagID tag_id)
Get local residual block for a variable and a tag.
Definition: Assembly.h:1065
ConstraintJacobianType
Definition: MooseTypes.h:709
void prepareMatrixTagLower(Assembly &assembly, unsigned int ivar, unsigned int jvar, Moose::ConstraintJacobianType type)
Prepare data for computing the jacobian according to the active tags for mortar.
DenseVector< Number > _local_re
Holds local residual entries as they are accumulated by this Kernel.
const MooseObject & _moose_object
Moose objct this tag works on.
void resize(const unsigned int new_m, const unsigned int new_n)
virtual ~TaggingInterface()
const InputParameters & parameters() const
Get the parameters of the object.
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...
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
std::vector< DenseMatrix< Number > * > _ke_blocks
Kernel blocks Vectors For each Tag.
void prepareVectorTagLower(Assembly &assembly, unsigned int ivar)
Prepare data for computing the residual according to active tags for mortar constraints.
void prepareVectorTagInternal(Assembly &assembly, unsigned int ivar, const std::set< TagID > &vector_tags, const std::set< TagID > &absolute_value_vector_tags)
Prepare data for computing element residual according to the specified tags Residual blocks for diffe...
unsigned int n() const
void prepareVectorTag(Assembly &assembly, unsigned int ivar)
Prepare data for computing element residual according to active tags.
void prepareMatrixTag(Assembly &assembly, unsigned int ivar, unsigned int jvar)
Prepare data for computing element jacobian according to the active tags.
Storage for all of the information pretaining to a vector tag.
Definition: VectorTag.h:15
DenseMatrix< Number > _nonlocal_ke
Holds nonlocal Jacobian entries as they are accumulated by this Kernel.
static InputParameters validParams()
virtual const VectorTag & getVectorTag(const TagID tag_id) const
Get a VectorTag from a TagID.
Definition: SubProblem.C:138
auto index_range(const T &sizable)
Key structure for APIs adding/caching local element residuals/Jacobians.
Definition: Assembly.h:812
void useVectorTag(const TagName &tag_name, VectorTagsKey)
Class that is used as a parameter to some of our vector tag APIs that allows only blessed framework c...
virtual bool matrixTagExists(const TagName &tag_name) const
Check to see if a particular Tag exists.
Definition: SubProblem.C:294
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...