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 template <typename T>
32 
42 {
43  const DenseVector<ADReal> & residuals;
44  const std::vector<dof_id_type> & dof_indices;
46 };
47 
49 {
50 public:
51  TaggingInterface(const MooseObject * moose_object);
52  virtual ~TaggingInterface();
53 
55 
61  {
62  friend class AttribVectorTags;
63  friend class NonlinearEigenSystem;
65  template <typename>
67 
70  };
71 
77  {
78  friend class AttribMatrixTags;
79  friend class NonlinearEigenSystem;
81  template <typename>
83 
86  };
87 
91  enum class ResidualTagType
92  {
94  Reference
95  };
96 
97  void useVectorTag(const TagName & tag_name, VectorTagsKey);
98 
99  void useMatrixTag(const TagName & tag_name, MatrixTagsKey);
100 
101  void useVectorTag(TagID tag_id, VectorTagsKey);
102 
103  void useMatrixTag(TagID tag_id, MatrixTagsKey);
104 
105  bool isVectorTagged() { return _vector_tags.size() > 0; }
106 
107  bool isMatrixTagged() { return _matrix_tags.size() > 0; }
108 
109  bool hasVectorTags() const { return !_vector_tags.empty(); }
110 
111  const std::set<TagID> & getVectorTags(VectorTagsKey) const { return _vector_tags; }
112 
113  const std::set<TagID> & getMatrixTags(MatrixTagsKey) const { return _matrix_tags; }
114 
115 protected:
122  void prepareVectorTag(Assembly & assembly, unsigned int ivar);
123 
131  void prepareVectorTag(Assembly & assembly, unsigned int ivar, ResidualTagType tag_type);
132 
140  void prepareVectorTagNeighbor(Assembly & assembly, unsigned int ivar);
141 
147  void prepareVectorTagLower(Assembly & assembly, unsigned int ivar);
148 
155  void prepareMatrixTag(Assembly & assembly, unsigned int ivar, unsigned int jvar);
156 
157  void prepareMatrixTag(Assembly & assembly,
158  unsigned int ivar,
159  unsigned int jvar,
160  DenseMatrix<Number> & k) const;
161 
168  void prepareMatrixTagNonlocal(Assembly & assembly, unsigned int ivar, unsigned int jvar);
169 
177  void prepareMatrixTagNeighbor(Assembly & assembly,
178  unsigned int ivar,
179  unsigned int jvar,
180  Moose::DGJacobianType type);
181 
182  void prepareMatrixTagNeighbor(Assembly & assembly,
183  unsigned int ivar,
184  unsigned int jvar,
186  DenseMatrix<Number> & k) const;
187 
193  void prepareMatrixTagLower(Assembly & assembly,
194  unsigned int ivar,
195  unsigned int jvar,
197 
203 
209 
215 
216  void accumulateTaggedLocalMatrix(Assembly & assembly,
217  unsigned int ivar,
218  unsigned int jvar,
219  const DenseMatrix<Number> & k);
220 
221  void accumulateTaggedLocalMatrix(Assembly & assembly,
222  unsigned int ivar,
223  unsigned int jvar,
225  const DenseMatrix<Number> & k);
226 
232 
238 
242  template <typename Residuals, typename Indices>
243  void addResiduals(Assembly & assembly,
244  const Residuals & residuals,
245  const Indices & dof_indices,
246  Real scaling_factor);
247 
251  template <typename T, typename Indices>
252  void addResiduals(Assembly & assembly,
253  const DenseVector<T> & residuals,
254  const Indices & dof_indices,
255  Real scaling_factor);
256 
261  template <typename Residuals, typename Indices>
262  void addResidualsAndJacobian(Assembly & assembly,
263  const Residuals & residuals,
264  const Indices & dof_indices,
265  Real scaling_factor);
266 
270  template <typename Residuals, typename Indices>
271  void addJacobian(Assembly & assembly,
272  const Residuals & residuals,
273  const Indices & dof_indices,
274  Real scaling_factor);
275 
279  void addResiduals(Assembly & assembly, const ADResidualsPacket & packet);
280 
285  void addResidualsAndJacobian(Assembly & assembly, const ADResidualsPacket & packet);
286 
290  void addJacobian(Assembly & assembly, const ADResidualsPacket & packet);
291 
297  template <typename Residuals, typename Indices>
299  const Residuals & residuals,
300  const Indices & dof_indices,
301  Real scaling_factor);
302 
308  template <typename Residuals, typename Indices>
310  const Residuals & residuals,
311  const Indices & dof_indices,
312  Real scaling_factor);
313 
319  template <typename Residuals, typename Indices>
320  void addJacobianWithoutConstraints(Assembly & assembly,
321  const Residuals & residuals,
322  const Indices & dof_indices,
323  Real scaling_factor);
324 
328  void addJacobianElement(Assembly & assembly,
329  Real value,
330  dof_id_type row_index,
331  dof_id_type column_index,
332  Real scaling_factor);
333 
337  void addJacobian(Assembly & assembly,
338  DenseMatrix<Real> & local_k,
339  const std::vector<dof_id_type> & row_indices,
340  const std::vector<dof_id_type> & column_indices,
341  Real scaling_factor);
342 
346  template <typename T>
347  void setResidual(SystemBase & sys, const T & residual, MooseVariableFE<T> & var);
348 
352  void setResidual(SystemBase & sys, Real residual, dof_id_type dof_index);
353 
357  template <typename SetResidualFunctor>
358  void setResidual(SystemBase & sys, SetResidualFunctor set_residual_functor);
359 
362 
365 
368 
371 
372 private:
379  void prepareVectorTagInternal(Assembly & assembly,
380  unsigned int ivar,
381  const std::set<TagID> & vector_tags,
382  const std::set<TagID> & absolute_value_vector_tags);
383 
385  std::set<TagID> _vector_tags;
386 
388  std::set<TagID> _abs_vector_tags;
389 
391  std::set<TagID> _matrix_tags;
392 
395  std::set<TagID> _non_ref_vector_tags;
396 
399  std::set<TagID> _non_ref_abs_vector_tags;
400 
404  std::set<TagID> _ref_vector_tags;
405 
409  std::set<TagID> _ref_abs_vector_tags;
410 
413 
416 
418  std::vector<DenseVector<Number> *> _re_blocks;
419 
421  std::vector<DenseVector<Number> *> _absre_blocks;
422 
424  std::vector<DenseMatrix<Number> *> _ke_blocks;
425 
428  std::vector<Real> _absolute_residuals;
429 
430  friend class NonlinearSystemBase;
431 };
432 
433 #define usingTaggingInterfaceMembers \
434  using TaggingInterface::_subproblem; \
435  using TaggingInterface::accumulateTaggedLocalResidual; \
436  using TaggingInterface::accumulateTaggedLocalMatrix; \
437  using TaggingInterface::prepareVectorTag; \
438  using TaggingInterface::prepareMatrixTag; \
439  using TaggingInterface::prepareVectorTagNeighbor; \
440  using TaggingInterface::_local_re; \
441  using TaggingInterface::prepareVectorTagLower; \
442  using TaggingInterface::prepareMatrixTagNeighbor; \
443  using TaggingInterface::prepareMatrixTagLower; \
444  using TaggingInterface::_local_ke
445 
446 template <typename Residuals, typename Indices>
447 void
449  const Residuals & residuals,
450  const Indices & dof_indices,
451  const Real scaling_factor)
452 {
453  assembly.cacheResiduals(
454  residuals, dof_indices, scaling_factor, Assembly::LocalDataKey{}, _vector_tags);
455  if (!_abs_vector_tags.empty())
456  {
457  _absolute_residuals.resize(residuals.size());
458  for (const auto i : index_range(residuals))
460 
462  dof_indices,
463  scaling_factor,
466  }
467 }
468 
469 template <typename T, typename Indices>
470 void
472  const DenseVector<T> & residuals,
473  const Indices & dof_indices,
474  const Real scaling_factor)
475 {
476  addResiduals(assembly, residuals.get_values(), dof_indices, scaling_factor);
477 }
478 
479 template <typename Residuals, typename Indices>
480 void
482  const Residuals & residuals,
483  const Indices & dof_indices,
484  const Real scaling_factor)
485 {
487  residuals, dof_indices, scaling_factor, Assembly::LocalDataKey{}, _vector_tags);
488  if (!_abs_vector_tags.empty())
489  {
490  _absolute_residuals.resize(residuals.size());
491  for (const auto i : index_range(residuals))
493 
495  dof_indices,
496  scaling_factor,
499  }
500 }
501 
502 template <typename Residuals, typename Indices>
503 void
505  const Residuals & residuals,
506  const Indices & dof_indices,
507  Real scaling_factor)
508 {
509  addResiduals(assembly, residuals, dof_indices, scaling_factor);
510  addJacobian(assembly, residuals, dof_indices, scaling_factor);
511 }
512 
513 template <typename Residuals, typename Indices>
514 void
516  const Residuals & residuals,
517  const Indices & dof_indices,
518  Real scaling_factor)
519 {
520  assembly.cacheJacobian(
521  residuals, dof_indices, scaling_factor, Assembly::LocalDataKey{}, _matrix_tags);
522 }
523 
524 template <typename Residuals, typename Indices>
525 void
527  const Residuals & residuals,
528  const Indices & dof_indices,
529  Real scaling_factor)
530 {
531  addResidualsWithoutConstraints(assembly, residuals, dof_indices, scaling_factor);
532  addJacobianWithoutConstraints(assembly, residuals, dof_indices, scaling_factor);
533 }
534 
535 template <typename Residuals, typename Indices>
536 void
538  const Residuals & residuals,
539  const Indices & dof_indices,
540  Real scaling_factor)
541 {
543  residuals, dof_indices, scaling_factor, Assembly::LocalDataKey{}, _matrix_tags);
544 }
545 
546 inline void
548  const Real value,
549  const dof_id_type row_index,
550  const dof_id_type column_index,
551  const Real scaling_factor)
552 {
553  assembly.cacheJacobian(
554  row_index, column_index, value * scaling_factor, Assembly::LocalDataKey{}, _matrix_tags);
555 }
556 
557 inline void
559  DenseMatrix<Real> & local_k,
560  const std::vector<dof_id_type> & row_indices,
561  const std::vector<dof_id_type> & column_indices,
562  const Real scaling_factor)
563 {
564  for (const auto matrix_tag : _matrix_tags)
565  assembly.cacheJacobianBlock(
566  local_k, row_indices, column_indices, scaling_factor, Assembly::LocalDataKey{}, matrix_tag);
567 }
568 
569 template <typename T>
570 void
572 {
573  for (const auto tag_id : _vector_tags)
574  if (sys.hasVector(tag_id))
575  var.insertNodalValue(sys.getVector(tag_id), residual);
576 }
577 
578 inline void
579 TaggingInterface::setResidual(SystemBase & sys, const Real residual, const dof_id_type dof_index)
580 {
581  for (const auto tag_id : _vector_tags)
582  if (sys.hasVector(tag_id))
583  sys.getVector(tag_id).set(dof_index, residual);
584 }
585 
586 template <typename SetResidualFunctor>
587 void
588 TaggingInterface::setResidual(SystemBase & sys, const SetResidualFunctor set_residual_functor)
589 {
590  for (const auto tag_id : _vector_tags)
591  if (sys.hasVector(tag_id))
592  set_residual_functor(sys.getVector(tag_id));
593 }
594 
595 inline void
597 {
598  addResiduals(assembly, packet.residuals, packet.dof_indices, packet.scaling_factor);
599 }
600 
601 inline void
603 {
604  addResidualsAndJacobian(assembly, packet.residuals, packet.dof_indices, packet.scaling_factor);
605 }
606 
607 inline void
609 {
610  addJacobian(assembly, packet.residuals, packet.dof_indices, packet.scaling_factor);
611 }
Nonlinear eigenvalue system to be solved.
MetaPhysicL::DualNumber< V, D, asd > abs(const MetaPhysicL::DualNumber< V, D, asd > &a)
Definition: EigenADReal.h:42
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:907
bool hasVectorTags() const
Keeps track of stuff related to assembling.
Definition: Assembly.h:101
unsigned int TagID
Definition: MooseTypes.h:210
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: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...
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:4065
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:3151
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:3031
void useMatrixTag(const TagName &tag_name, MatrixTagsKey)
void insertNodalValue(libMesh::NumericVector< libMesh::Number > &residual, const OutputData &v)
Write a nodal value to the passed-in solution vector.
DGJacobianType
Definition: MooseTypes.h:749
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
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:796
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...
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:916
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:833
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:3071
uint8_t dof_id_type
const std::set< TagID > & getVectorTags(VectorTagsKey) const