https://mooseframework.inl.gov
TaggingInterface.h
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 #pragma once
11 
12 #include "DenseMatrix.h"
13 #include "MooseTypes.h"
14 #include "MultiMooseEnum.h"
15 #include "Assembly.h"
16 #include "MooseVariableFE.h"
17 #include "SystemBase.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;
29 
30 #ifdef MOOSE_KOKKOS_ENABLED
31 namespace Moose::Kokkos
32 {
33 class ResidualObject;
34 }
35 #endif
36 
37 template <typename T>
39 
49 {
50  const DenseVector<ADReal> & residuals;
51  const std::vector<dof_id_type> & dof_indices;
53 };
54 
56 {
57 public:
58  TaggingInterface(const MooseObject * moose_object);
59 
60 #ifdef MOOSE_KOKKOS_ENABLED
61 
65 #endif
66 
67  virtual ~TaggingInterface();
68 
70 
76  {
77  friend class AttribVectorTags;
78  friend class NonlinearEigenSystem;
80  template <typename>
82 #ifdef MOOSE_KOKKOS_ENABLED
84 #endif
85 
88  };
89 
95  {
96  friend class AttribMatrixTags;
97  friend class NonlinearEigenSystem;
99  template <typename>
101 #ifdef MOOSE_KOKKOS_ENABLED
103 #endif
104 
107  };
108 
112  enum class ResidualTagType
113  {
114  NonReference,
115  Reference
116  };
117 
118  void useVectorTag(const TagName & tag_name, VectorTagsKey);
119 
120  void useMatrixTag(const TagName & tag_name, MatrixTagsKey);
121 
122  void useVectorTag(TagID tag_id, VectorTagsKey);
123 
124  void useMatrixTag(TagID tag_id, MatrixTagsKey);
125 
126  bool isVectorTagged() { return _vector_tags.size() > 0; }
127 
128  bool isMatrixTagged() { return _matrix_tags.size() > 0; }
129 
130  bool hasVectorTags() const { return !_vector_tags.empty(); }
131 
132  const std::set<TagID> & getVectorTags(VectorTagsKey) const { return _vector_tags; }
133 
134  const std::set<TagID> & getMatrixTags(MatrixTagsKey) const { return _matrix_tags; }
135 
136 protected:
143  void prepareVectorTag(Assembly & assembly, unsigned int ivar);
144 
152  void prepareVectorTag(Assembly & assembly, unsigned int ivar, ResidualTagType tag_type);
153 
161  void prepareVectorTagNeighbor(Assembly & assembly, unsigned int ivar);
162 
168  void prepareVectorTagLower(Assembly & assembly, unsigned int ivar);
169 
176  void prepareMatrixTag(Assembly & assembly, unsigned int ivar, unsigned int jvar);
177 
178  void prepareMatrixTag(Assembly & assembly,
179  unsigned int ivar,
180  unsigned int jvar,
181  DenseMatrix<Number> & k) const;
182 
189  void prepareMatrixTagNonlocal(Assembly & assembly, unsigned int ivar, unsigned int jvar);
190 
198  void prepareMatrixTagNeighbor(Assembly & assembly,
199  unsigned int ivar,
200  unsigned int jvar,
201  Moose::DGJacobianType type);
202 
203  void prepareMatrixTagNeighbor(Assembly & assembly,
204  unsigned int ivar,
205  unsigned int jvar,
207  DenseMatrix<Number> & k) const;
208 
214  void prepareMatrixTagLower(Assembly & assembly,
215  unsigned int ivar,
216  unsigned int jvar,
218 
224 
230 
236 
237  void accumulateTaggedLocalMatrix(Assembly & assembly,
238  unsigned int ivar,
239  unsigned int jvar,
240  const DenseMatrix<Number> & k);
241 
242  void accumulateTaggedLocalMatrix(Assembly & assembly,
243  unsigned int ivar,
244  unsigned int jvar,
246  const DenseMatrix<Number> & k);
247 
253 
259 
263  template <typename Residuals, typename Indices>
264  void addResiduals(Assembly & assembly,
265  const Residuals & residuals,
266  const Indices & dof_indices,
267  Real scaling_factor);
268 
269  template <typename Residuals, typename Indices>
270  void addResiduals(Assembly & assembly,
271  const Residuals & residuals,
272  const Indices & dof_indices,
273  const std::vector<Real> & scaling_factors);
274 
278  template <typename T, typename Indices>
279  void addResiduals(Assembly & assembly,
280  const DenseVector<T> & residuals,
281  const Indices & dof_indices,
282  Real scaling_factor);
283 
288  template <typename Residuals, typename Indices>
289  void addResidualsAndJacobian(Assembly & assembly,
290  const Residuals & residuals,
291  const Indices & dof_indices,
292  Real scaling_factor);
293 
297  template <typename Residuals, typename Indices>
298  void addJacobian(Assembly & assembly,
299  const Residuals & residuals,
300  const Indices & dof_indices,
301  Real scaling_factor);
302 
307  template <typename Residuals, typename Indices>
308  void addJacobian(Assembly & assembly,
309  const Residuals & residuals,
310  const Indices & dof_indices,
311  const std::vector<Real> & scaling_factors);
312 
316  void addResiduals(Assembly & assembly, const ADResidualsPacket & packet);
317 
322  void addResidualsAndJacobian(Assembly & assembly, const ADResidualsPacket & packet);
323 
327  void addJacobian(Assembly & assembly, const ADResidualsPacket & packet);
328 
334  template <typename Residuals, typename Indices>
336  const Residuals & residuals,
337  const Indices & dof_indices,
338  Real scaling_factor);
339 
345  template <typename Residuals, typename Indices>
347  const Residuals & residuals,
348  const Indices & dof_indices,
349  Real scaling_factor);
350 
356  template <typename Residuals, typename Indices>
357  void addJacobianWithoutConstraints(Assembly & assembly,
358  const Residuals & residuals,
359  const Indices & dof_indices,
360  Real scaling_factor);
361 
365  void addJacobianElement(Assembly & assembly,
366  Real value,
367  dof_id_type row_index,
368  dof_id_type column_index,
369  Real scaling_factor);
370 
374  void addJacobian(Assembly & assembly,
375  DenseMatrix<Real> & local_k,
376  const std::vector<dof_id_type> & row_indices,
377  const std::vector<dof_id_type> & column_indices,
378  Real scaling_factor);
379 
383  template <typename T>
384  void setResidual(SystemBase & sys, const T & residual, MooseVariableFE<T> & var);
385 
389  void setResidual(SystemBase & sys, Real residual, dof_id_type dof_index);
390 
394  template <typename SetResidualFunctor>
395  void setResidual(SystemBase & sys, SetResidualFunctor set_residual_functor);
396 
399 
402 
405 
408 
409 private:
416  void prepareVectorTagInternal(Assembly & assembly,
417  unsigned int ivar,
418  const std::set<TagID> & vector_tags,
419  const std::set<TagID> & absolute_value_vector_tags);
420 
422  std::set<TagID> _vector_tags;
423 
425  std::set<TagID> _abs_vector_tags;
426 
428  std::set<TagID> _matrix_tags;
429 
432  std::set<TagID> _non_ref_vector_tags;
433 
436  std::set<TagID> _non_ref_abs_vector_tags;
437 
441  std::set<TagID> _ref_vector_tags;
442 
446  std::set<TagID> _ref_abs_vector_tags;
447 
450 
453 
455  std::vector<DenseVector<Number> *> _re_blocks;
456 
458  std::vector<DenseVector<Number> *> _absre_blocks;
459 
461  std::vector<DenseMatrix<Number> *> _ke_blocks;
462 
465  std::vector<Real> _absolute_residuals;
466 
467  friend class NonlinearSystemBase;
468 };
469 
470 #define usingTaggingInterfaceMembers \
471  using TaggingInterface::_subproblem; \
472  using TaggingInterface::accumulateTaggedLocalResidual; \
473  using TaggingInterface::accumulateTaggedLocalMatrix; \
474  using TaggingInterface::prepareVectorTag; \
475  using TaggingInterface::prepareMatrixTag; \
476  using TaggingInterface::prepareVectorTagNeighbor; \
477  using TaggingInterface::_local_re; \
478  using TaggingInterface::prepareVectorTagLower; \
479  using TaggingInterface::prepareMatrixTagNeighbor; \
480  using TaggingInterface::prepareMatrixTagLower; \
481  using TaggingInterface::_local_ke
482 
483 template <typename Residuals, typename Indices>
484 void
486  const Residuals & residuals,
487  const Indices & dof_indices,
488  const Real scaling_factor)
489 {
490  assembly.cacheResiduals(
491  residuals, dof_indices, scaling_factor, Assembly::LocalDataKey{}, _vector_tags);
492  if (!_abs_vector_tags.empty())
493  {
494  _absolute_residuals.resize(residuals.size());
495  for (const auto i : index_range(residuals))
497 
499  dof_indices,
500  scaling_factor,
503  }
504 }
505 
506 template <typename Residuals, typename Indices>
507 void
509  const Residuals & residuals,
510  const Indices & dof_indices,
511  const std::vector<Real> & scaling_factors)
512 {
513  const auto count = scaling_factors.size();
514  mooseAssert(dof_indices.size() % count == 0,
515  "The number of dof indices should be divided cleanly by the variable count");
516  const auto nshapes = dof_indices.size() / count;
517 
518  for (const auto j : make_range(count))
519  // The Residuals type may not offer operator[] (e.g. eigen vectors) but more commonly it
520  // should offer data()
521  addResiduals(assembly,
522  Moose::makeSpan(residuals, j * nshapes, nshapes),
523  Moose::makeSpan(dof_indices, j * nshapes, nshapes),
524  scaling_factors[j]);
525 }
526 
527 template <typename T, typename Indices>
528 void
530  const DenseVector<T> & residuals,
531  const Indices & dof_indices,
532  const Real scaling_factor)
533 {
534  addResiduals(assembly, residuals.get_values(), dof_indices, scaling_factor);
535 }
536 
537 template <typename Residuals, typename Indices>
538 void
540  const Residuals & residuals,
541  const Indices & dof_indices,
542  const Real scaling_factor)
543 {
545  residuals, dof_indices, scaling_factor, Assembly::LocalDataKey{}, _vector_tags);
546  if (!_abs_vector_tags.empty())
547  {
548  _absolute_residuals.resize(residuals.size());
549  for (const auto i : index_range(residuals))
551 
553  dof_indices,
554  scaling_factor,
557  }
558 }
559 
560 template <typename Residuals, typename Indices>
561 void
563  const Residuals & residuals,
564  const Indices & dof_indices,
565  Real scaling_factor)
566 {
567  addResiduals(assembly, residuals, dof_indices, scaling_factor);
568  addJacobian(assembly, residuals, dof_indices, scaling_factor);
569 }
570 
571 template <typename Residuals, typename Indices>
572 void
574  const Residuals & residuals,
575  const Indices & dof_indices,
576  Real scaling_factor)
577 {
578  assembly.cacheJacobian(
579  residuals, dof_indices, scaling_factor, Assembly::LocalDataKey{}, _matrix_tags);
580 }
581 
582 template <typename Residuals, typename Indices>
583 void
585  const Residuals & residuals,
586  const Indices & dof_indices,
587  const std::vector<Real> & scaling_factors)
588 {
589  const auto count = scaling_factors.size();
590  mooseAssert(dof_indices.size() % count == 0,
591  "The number of dof indices should be divided cleanly by the variable count");
592  const auto nshapes = dof_indices.size() / count;
593 
594  for (const auto j : make_range(count))
595  // The Residuals type may not offer operator[] (e.g. eigen vectors) but more commonly it
596  // should offer data()
597  addJacobian(assembly,
598  Moose::makeSpan(residuals, j * nshapes, nshapes),
599  Moose::makeSpan(dof_indices, j * nshapes, nshapes),
600  scaling_factors[j]);
601 }
602 
603 template <typename Residuals, typename Indices>
604 void
606  const Residuals & residuals,
607  const Indices & dof_indices,
608  Real scaling_factor)
609 {
610  addResidualsWithoutConstraints(assembly, residuals, dof_indices, scaling_factor);
611  addJacobianWithoutConstraints(assembly, residuals, dof_indices, scaling_factor);
612 }
613 
614 template <typename Residuals, typename Indices>
615 void
617  const Residuals & residuals,
618  const Indices & dof_indices,
619  Real scaling_factor)
620 {
622  residuals, dof_indices, scaling_factor, Assembly::LocalDataKey{}, _matrix_tags);
623 }
624 
625 inline void
627  const Real value,
628  const dof_id_type row_index,
629  const dof_id_type column_index,
630  const Real scaling_factor)
631 {
632  assembly.cacheJacobian(
633  row_index, column_index, value * scaling_factor, Assembly::LocalDataKey{}, _matrix_tags);
634 }
635 
636 inline void
638  DenseMatrix<Real> & local_k,
639  const std::vector<dof_id_type> & row_indices,
640  const std::vector<dof_id_type> & column_indices,
641  const Real scaling_factor)
642 {
643  for (const auto matrix_tag : _matrix_tags)
644  assembly.cacheJacobianBlock(
645  local_k, row_indices, column_indices, scaling_factor, Assembly::LocalDataKey{}, matrix_tag);
646 }
647 
648 template <typename T>
649 void
651 {
652  for (const auto tag_id : _vector_tags)
653  if (sys.hasVector(tag_id))
654  var.insertNodalValue(sys.getVector(tag_id), residual);
655 }
656 
657 inline void
658 TaggingInterface::setResidual(SystemBase & sys, const Real residual, const dof_id_type dof_index)
659 {
660  for (const auto tag_id : _vector_tags)
661  if (sys.hasVector(tag_id))
662  sys.getVector(tag_id).set(dof_index, residual);
663 }
664 
665 template <typename SetResidualFunctor>
666 void
667 TaggingInterface::setResidual(SystemBase & sys, const SetResidualFunctor set_residual_functor)
668 {
669  for (const auto tag_id : _vector_tags)
670  if (sys.hasVector(tag_id))
671  set_residual_functor(sys.getVector(tag_id));
672 }
673 
674 inline void
676 {
677  addResiduals(assembly, packet.residuals, packet.dof_indices, packet.scaling_factor);
678 }
679 
680 inline void
682 {
683  addResidualsAndJacobian(assembly, packet.residuals, packet.dof_indices, packet.scaling_factor);
684 }
685 
686 inline void
688 {
689  addJacobian(assembly, packet.residuals, packet.dof_indices, packet.scaling_factor);
690 }
Nonlinear eigenvalue system to be solved.
MetaPhysicL::DualNumber< V, D, asd > abs(const MetaPhysicL::DualNumber< V, D, asd > &a)
Definition: EigenADReal.h:50
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:924
bool hasVectorTags() const
Keeps track of stuff related to assembling.
Definition: Assembly.h:109
unsigned int TagID
Definition: MooseTypes.h:238
const std::vector< dof_id_type > & dof_indices
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: EigenADReal.h:100
std::vector< DenseVector< Number > * > _absre_blocks
Residual blocks for absolute value residual tags.
void insertNodalValue(libMesh::NumericVector< libMesh::Number > &residual, const DofValue &v)
Write a nodal value to the passed-in solution vector.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
The base class for Kokkos residual objects.
const Real scaling_factor
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.
const DenseVector< ADReal > & residuals
std::set< TagID > _ref_vector_tags
A set of either size 1 or 0.
Base class for a system (of equations)
Definition: SystemBase.h:84
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:4036
Nonlinear system to be solved.
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:28
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:3182
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 vector tag ids this Kernel will contribute to.
void accumulateTaggedLocalMatrix()
Local Jacobian blocks will be appended by adding the current local kernel Jacobian.
Utility structure for packaging up all of the residual object&#39;s information needed to add into the sy...
A storage container for MooseObjects that inherit from SetupInterface.
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:3062
void useMatrixTag(const TagName &tag_name, MatrixTagsKey)
DGJacobianType
Definition: MooseTypes.h:798
void prepareVectorTagNeighbor(Assembly &assembly, unsigned int ivar)
Prepare data for computing element residual the according to active tags for DG and interface kernels...
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
This is the common base class for objects that give residual contributions.
Generic class for solving transient nonlinear problems.
Definition: SubProblem.h:78
This is the common base class for objects that give contributions to a linear system.
ConstraintJacobianType
Definition: MooseTypes.h:845
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
IntRange< T > make_range(T beg, T end)
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...
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.
auto makeSpan(C &container, std::size_t offset, std::size_t n)
Helper function for creating a span from a given container.
Definition: MooseTypes.h:1090
virtual NumericVector< Number > & getVector(const std::string &name)
Get a raw NumericVector by name.
Definition: SystemBase.C:933
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:862
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:3102
uint8_t dof_id_type
const std::set< TagID > & getVectorTags(VectorTagsKey) const