www.mooseframework.org
TaggingInterface.h
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 #pragma once
11 
12 #include "DenseMatrix.h"
13 #include "MooseTypes.h"
14 #include "MultiMooseEnum.h"
15 #include "Assembly.h"
16 #include "NonlinearSystemBase.h"
17 #include "MooseVariableFE.h"
18 
19 #include "libmesh/dense_vector.h"
20 #include "metaphysicl/raw_type.h"
21 
22 #include <vector>
23 
24 // Forward declarations
25 class InputParameters;
26 class MooseObject;
27 class SubProblem;
28 class Assembly;
30 
31 template <typename T>
33 
35 {
36 public:
37  TaggingInterface(const MooseObject * moose_object);
38  virtual ~TaggingInterface();
39 
41 
47  {
48  friend class AttribVectorTags;
49  friend class NonlinearEigenSystem;
50  template <typename>
52 
55  };
56 
62  {
63  friend class AttribMatrixTags;
64  friend class NonlinearEigenSystem;
65  template <typename>
67 
70  };
71 
75  enum class ResidualTagType
76  {
78  Reference
79  };
80 
81  void useVectorTag(const TagName & tag_name, VectorTagsKey);
82 
83  void useMatrixTag(const TagName & tag_name, MatrixTagsKey);
84 
85  void useVectorTag(TagID tag_id, VectorTagsKey);
86 
87  void useMatrixTag(TagID tag_id, MatrixTagsKey);
88 
89  bool isVectorTagged() { return _vector_tags.size() > 0; }
90 
91  bool isMatrixTagged() { return _matrix_tags.size() > 0; }
92 
93  bool hasVectorTags() const { return !_vector_tags.empty(); }
94 
95  const std::set<TagID> & getVectorTags(VectorTagsKey) const { return _vector_tags; }
96 
97  const std::set<TagID> & getMatrixTags(MatrixTagsKey) const { return _matrix_tags; }
98 
99 protected:
106  void prepareVectorTag(Assembly & assembly, unsigned int ivar);
107 
115  void prepareVectorTag(Assembly & assembly, unsigned int ivar, ResidualTagType tag_type);
116 
124  void prepareVectorTagNeighbor(Assembly & assembly, unsigned int ivar);
125 
131  void prepareVectorTagLower(Assembly & assembly, unsigned int ivar);
132 
139  void prepareMatrixTag(Assembly & assembly, unsigned int ivar, unsigned int jvar);
140 
141  void prepareMatrixTag(Assembly & assembly,
142  unsigned int ivar,
143  unsigned int jvar,
144  DenseMatrix<Number> & k) const;
145 
152  void prepareMatrixTagNonlocal(Assembly & assembly, unsigned int ivar, unsigned int jvar);
153 
161  void prepareMatrixTagNeighbor(Assembly & assembly,
162  unsigned int ivar,
163  unsigned int jvar,
164  Moose::DGJacobianType type);
165 
166  void prepareMatrixTagNeighbor(Assembly & assembly,
167  unsigned int ivar,
168  unsigned int jvar,
170  DenseMatrix<Number> & k) const;
171 
177  void prepareMatrixTagLower(Assembly & assembly,
178  unsigned int ivar,
179  unsigned int jvar,
181 
187 
193 
199 
200  void accumulateTaggedLocalMatrix(Assembly & assembly,
201  unsigned int ivar,
202  unsigned int jvar,
203  const DenseMatrix<Number> & k);
204 
205  void accumulateTaggedLocalMatrix(Assembly & assembly,
206  unsigned int ivar,
207  unsigned int jvar,
209  const DenseMatrix<Number> & k);
210 
216 
222 
226  template <typename Residuals, typename Indices>
227  void addResiduals(Assembly & assembly,
228  const Residuals & residuals,
229  const Indices & dof_indices,
230  Real scaling_factor);
231 
235  template <typename T, typename Indices>
236  void addResiduals(Assembly & assembly,
237  const DenseVector<T> & residuals,
238  const Indices & dof_indices,
239  Real scaling_factor);
240 
245  template <typename Residuals, typename Indices>
246  void addResidualsAndJacobian(Assembly & assembly,
247  const Residuals & residuals,
248  const Indices & dof_indices,
249  Real scaling_factor);
250 
254  template <typename Residuals, typename Indices>
255  void addJacobian(Assembly & assembly,
256  const Residuals & residuals,
257  const Indices & dof_indices,
258  Real scaling_factor);
259 
265  template <typename Residuals, typename Indices>
267  const Residuals & residuals,
268  const Indices & dof_indices,
269  Real scaling_factor);
270 
276  template <typename Residuals, typename Indices>
278  const Residuals & residuals,
279  const Indices & dof_indices,
280  Real scaling_factor);
281 
287  template <typename Residuals, typename Indices>
288  void addJacobianWithoutConstraints(Assembly & assembly,
289  const Residuals & residuals,
290  const Indices & dof_indices,
291  Real scaling_factor);
292 
296  void addJacobianElement(Assembly & assembly,
297  Real value,
298  dof_id_type row_index,
299  dof_id_type column_index,
300  Real scaling_factor);
301 
305  void addJacobian(Assembly & assembly,
306  DenseMatrix<Real> & local_k,
307  const std::vector<dof_id_type> & row_indices,
308  const std::vector<dof_id_type> & column_indices,
309  Real scaling_factor);
310 
314  template <typename T>
315  void setResidual(SystemBase & sys, const T & residual, MooseVariableFE<T> & var);
316 
320  void setResidual(SystemBase & sys, Real residual, dof_id_type dof_index);
321 
325  template <typename SetResidualFunctor>
326  void setResidual(SystemBase & sys, SetResidualFunctor set_residual_functor);
327 
330 
333 
336 
339 
340 private:
347  void prepareVectorTagInternal(Assembly & assembly,
348  unsigned int ivar,
349  const std::set<TagID> & vector_tags,
350  const std::set<TagID> & absolute_value_vector_tags);
351 
353  std::set<TagID> _vector_tags;
354 
356  std::set<TagID> _abs_vector_tags;
357 
359  std::set<TagID> _matrix_tags;
360 
363  std::set<TagID> _non_ref_vector_tags;
364 
367  std::set<TagID> _non_ref_abs_vector_tags;
368 
372  std::set<TagID> _ref_vector_tags;
373 
377  std::set<TagID> _ref_abs_vector_tags;
378 
381 
384 
386  std::vector<DenseVector<Number> *> _re_blocks;
387 
389  std::vector<DenseVector<Number> *> _absre_blocks;
390 
392  std::vector<DenseMatrix<Number> *> _ke_blocks;
393 
396  std::vector<Real> _absolute_residuals;
397 
399 };
400 
401 #define usingTaggingInterfaceMembers \
402  using TaggingInterface::_subproblem; \
403  using TaggingInterface::accumulateTaggedLocalResidual; \
404  using TaggingInterface::accumulateTaggedLocalMatrix; \
405  using TaggingInterface::prepareVectorTag; \
406  using TaggingInterface::prepareMatrixTag; \
407  using TaggingInterface::prepareVectorTagNeighbor; \
408  using TaggingInterface::_local_re; \
409  using TaggingInterface::prepareVectorTagLower; \
410  using TaggingInterface::prepareMatrixTagNeighbor; \
411  using TaggingInterface::prepareMatrixTagLower; \
412  using TaggingInterface::_local_ke
413 
414 template <typename Residuals, typename Indices>
415 void
417  const Residuals & residuals,
418  const Indices & dof_indices,
419  const Real scaling_factor)
420 {
421  assembly.cacheResiduals(
422  residuals, dof_indices, scaling_factor, Assembly::LocalDataKey{}, _vector_tags);
423  if (!_abs_vector_tags.empty())
424  {
425  _absolute_residuals.resize(residuals.size());
426  for (const auto i : index_range(residuals))
428 
430  dof_indices,
431  scaling_factor,
434  }
435 }
436 
437 template <typename T, typename Indices>
438 void
440  const DenseVector<T> & residuals,
441  const Indices & dof_indices,
442  const Real scaling_factor)
443 {
444  addResiduals(assembly, residuals.get_values(), dof_indices, scaling_factor);
445 }
446 
447 template <typename Residuals, typename Indices>
448 void
450  const Residuals & residuals,
451  const Indices & dof_indices,
452  const Real scaling_factor)
453 {
455  residuals, dof_indices, scaling_factor, Assembly::LocalDataKey{}, _vector_tags);
456  if (!_abs_vector_tags.empty())
457  {
458  _absolute_residuals.resize(residuals.size());
459  for (const auto i : index_range(residuals))
461 
463  dof_indices,
464  scaling_factor,
467  }
468 }
469 
470 template <typename Residuals, typename Indices>
471 void
473  const Residuals & residuals,
474  const Indices & dof_indices,
475  Real scaling_factor)
476 {
477  addResiduals(assembly, residuals, dof_indices, scaling_factor);
478  addJacobian(assembly, residuals, dof_indices, scaling_factor);
479 }
480 
481 template <typename Residuals, typename Indices>
482 void
484  const Residuals & residuals,
485  const Indices & dof_indices,
486  Real scaling_factor)
487 {
488  assembly.cacheJacobian(
489  residuals, dof_indices, scaling_factor, Assembly::LocalDataKey{}, _matrix_tags);
490 }
491 
492 template <typename Residuals, typename Indices>
493 void
495  const Residuals & residuals,
496  const Indices & dof_indices,
497  Real scaling_factor)
498 {
499  addResidualsWithoutConstraints(assembly, residuals, dof_indices, scaling_factor);
500  addJacobianWithoutConstraints(assembly, residuals, dof_indices, scaling_factor);
501 }
502 
503 template <typename Residuals, typename Indices>
504 void
506  const Residuals & residuals,
507  const Indices & dof_indices,
508  Real scaling_factor)
509 {
511  residuals, dof_indices, scaling_factor, Assembly::LocalDataKey{}, _matrix_tags);
512 }
513 
514 inline void
516  const Real value,
517  const dof_id_type row_index,
518  const dof_id_type column_index,
519  const Real scaling_factor)
520 {
521  assembly.cacheJacobian(
522  row_index, column_index, value * scaling_factor, Assembly::LocalDataKey{}, _matrix_tags);
523 }
524 
525 inline void
527  DenseMatrix<Real> & local_k,
528  const std::vector<dof_id_type> & row_indices,
529  const std::vector<dof_id_type> & column_indices,
530  const Real scaling_factor)
531 {
532  for (const auto matrix_tag : _matrix_tags)
533  assembly.cacheJacobianBlock(
534  local_k, row_indices, column_indices, scaling_factor, Assembly::LocalDataKey{}, matrix_tag);
535 }
536 
537 template <typename T>
538 void
540 {
541  for (const auto tag_id : _vector_tags)
542  if (sys.hasVector(tag_id))
543  var.insertNodalValue(sys.getVector(tag_id), residual);
544 }
545 
546 inline void
547 TaggingInterface::setResidual(SystemBase & sys, const Real residual, const dof_id_type dof_index)
548 {
549  for (const auto tag_id : _vector_tags)
550  if (sys.hasVector(tag_id))
551  sys.getVector(tag_id).set(dof_index, residual);
552 }
553 
554 template <typename SetResidualFunctor>
555 void
556 TaggingInterface::setResidual(SystemBase & sys, const SetResidualFunctor set_residual_functor)
557 {
558  for (const auto tag_id : _vector_tags)
559  if (sys.hasVector(tag_id))
560  set_residual_functor(sys.getVector(tag_id));
561 }
Nonlinear eigenvalue system to be solved.
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.
void addResidualsAndJacobian(Assembly &assembly, const Residuals &residuals, const Indices &dof_indices, Real scaling_factor)
Add the provided incoming residuals and derivatives for the Jacobian, corresponding to the provided d...
const InputParameters & _tag_params
Parameters from moose object.
void accumulateTaggedLocalResidual()
Local residual blocks will be appended by adding the current local kernel residual.
bool hasVector(const std::string &tag_name) const
Check if the named vector exists in the system.
Definition: SystemBase.C:880
bool hasVectorTags() const
Keeps track of stuff related to assembling.
Definition: Assembly.h:93
unsigned int TagID
Definition: MooseTypes.h:199
void insertNodalValue(NumericVector< Number > &residual, const OutputData &v)
Write a nodal value to the passed-in solution vector.
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...
MatrixTagsKey(const MatrixTagsKey &)
VectorTagsKey(const VectorTagsKey &)
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.
void assignTaggedLocalResidual()
Local residual blocks will assigned as the current local kernel residual.
auto raw_value(const Eigen::Map< T > &in)
Definition: ADReal.h:73
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...
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.
Base class for a system (of equations)
Definition: SystemBase.h:85
DenseMatrix< Number > _local_ke
Holds local Jacobian entries as they are accumulated by this Kernel.
std::set< TagID > _abs_vector_tags
The absolute value residual tag ids.
void cacheJacobian(GlobalDataKey)
Takes the values that are currently in _sub_Kee and appends them to the cached values.
Definition: Assembly.C:4054
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)
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.
InputParameters validParams()
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.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:33
std::set< TagID > _matrix_tags
The matrices this Kernel will contribute to.
void cacheJacobianWithoutConstraints(const Residuals &residuals, const Indices &row_indices, Real scaling_factor, LocalDataKey, const std::set< TagID > &matrix_tags)
Process the derivatives() data of a vector of ADReals.
Definition: Assembly.h:3127
void addResidualsAndJacobianWithoutConstraints(Assembly &assembly, const Residuals &residuals, const Indices &dof_indices, Real scaling_factor)
Add the provided incoming residuals and derivatives for the Jacobian, corresponding to the provided d...
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.
void accumulateTaggedLocalMatrix()
Local Jacobian blocks will be appended by adding the current local kernel Jacobian.
A storage container for MooseObjects that inherit from SetupInterface.
FEProblemBase derived class to enable convergence checking relative to a user-specified postprocessor...
void cacheResiduals(const Residuals &residuals, const Indices &row_indices, Real scaling_factor, LocalDataKey, const std::set< TagID > &vector_tags)
Process the supplied residual values.
Definition: Assembly.h:3008
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...
void constraintJacobians(bool displaced)
Add jacobian contributions from Constraints.
Generic class for solving transient nonlinear problems.
Definition: SubProblem.h:75
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.
const std::set< TagID > & getMatrixTags(MatrixTagsKey) const
void addResidualsWithoutConstraints(Assembly &assembly, const Residuals &residuals, const Indices &dof_indices, Real scaling_factor)
Add the provided incoming residuals corresponding to the provided dof indices.
virtual ~TaggingInterface()
virtual void set(const numeric_index_type i, const Number value)=0
void cacheJacobianBlock(DenseMatrix< Number > &jac_block, const std::vector< dof_id_type > &idof_indices, const std::vector< dof_id_type > &jdof_indices, Real scaling_factor, LocalDataKey, TagID tag)
Cache a local Jacobian block with the provided rows (idof_indices) and columns (jdof_indices) for eve...
Definition: Assembly.C:3743
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...
void prepareVectorTag(Assembly &assembly, unsigned int ivar)
Prepare data for computing element residual according to active tags.
void addJacobianWithoutConstraints(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 prepareMatrixTag(Assembly &assembly, unsigned int ivar, unsigned int jvar)
Prepare data for computing element jacobian according to the active tags.
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.
DenseMatrix< Number > _nonlocal_ke
Holds nonlocal Jacobian entries as they are accumulated by this Kernel.
static InputParameters validParams()
void setResidual(SystemBase &sys, const T &residual, MooseVariableFE< T > &var)
Set residual using the variables&#39; insertion API.
virtual NumericVector< Number > & getVector(const std::string &name)
Get a raw NumericVector by name.
Definition: SystemBase.C:889
auto index_range(const T &sizable)
std::vector< Real > _absolute_residuals
A container to hold absolute values of residuals passed into addResiduals.
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...
void cacheResidualsWithoutConstraints(const Residuals &residuals, const Indices &row_indices, Real scaling_factor, LocalDataKey, const std::set< TagID > &vector_tags)
Process the supplied residual values.
Definition: Assembly.h:3048
uint8_t dof_id_type
const std::set< TagID > & getVectorTags(VectorTagsKey) const