https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
25class InputParameters;
26class MooseObject;
27class SubProblem;
28class Assembly;
29
30#ifdef MOOSE_KOKKOS_ENABLED
31namespace Moose::Kokkos
32{
33class ResidualObject;
35class FESystem;
36}
37#endif
38
39template <typename T>
41
51{
52 const DenseVector<ADReal> & residuals;
53 const std::vector<dof_id_type> & dof_indices;
54 const Real scaling_factor;
55};
56
58{
59public:
60 TaggingInterface(const MooseObject * moose_object);
61
62#ifdef MOOSE_KOKKOS_ENABLED
67#endif
68
69 virtual ~TaggingInterface();
70
72
78 {
79 friend class AttribVectorTags;
82 template <typename>
84#ifdef MOOSE_KOKKOS_ENABLED
88#endif
89
92 };
93
99 {
100 friend class AttribMatrixTags;
103 template <typename>
105#ifdef MOOSE_KOKKOS_ENABLED
109#endif
110
113 };
114
119 {
122 };
123
124 void useVectorTag(const TagName & tag_name, VectorTagsKey);
125
126 void useMatrixTag(const TagName & tag_name, MatrixTagsKey);
127
128 void useVectorTag(TagID tag_id, VectorTagsKey);
129
130 void useMatrixTag(TagID tag_id, MatrixTagsKey);
131
132 bool isVectorTagged() { return _vector_tags.size() > 0; }
133
134 bool isMatrixTagged() { return _matrix_tags.size() > 0; }
135
136 bool hasVectorTags() const { return !_vector_tags.empty(); }
137
138 const std::set<TagID> & getVectorTags(VectorTagsKey) const { return _vector_tags; }
139
140 const std::set<TagID> & getMatrixTags(MatrixTagsKey) const { return _matrix_tags; }
141
142protected:
149 void prepareVectorTag(Assembly & assembly, unsigned int ivar);
150
158 void prepareVectorTag(Assembly & assembly, unsigned int ivar, ResidualTagType tag_type);
159
167 void prepareVectorTagNeighbor(Assembly & assembly, unsigned int ivar);
168
174 void prepareVectorTagLower(Assembly & assembly, unsigned int ivar);
175
182 void prepareMatrixTag(Assembly & assembly, unsigned int ivar, unsigned int jvar);
183
184 void prepareMatrixTag(Assembly & assembly,
185 unsigned int ivar,
186 unsigned int jvar,
187 DenseMatrix<Number> & k) const;
188
195 void prepareMatrixTagNonlocal(Assembly & assembly, unsigned int ivar, unsigned int jvar);
196
204 void prepareMatrixTagNeighbor(Assembly & assembly,
205 unsigned int ivar,
206 unsigned int jvar,
208
209 void prepareMatrixTagNeighbor(Assembly & assembly,
210 unsigned int ivar,
211 unsigned int jvar,
213 DenseMatrix<Number> & k) const;
214
220 void prepareMatrixTagLower(Assembly & assembly,
221 unsigned int ivar,
222 unsigned int jvar,
224
230
236
242
244 unsigned int ivar,
245 unsigned int jvar,
246 const DenseMatrix<Number> & k);
247
249 unsigned int ivar,
250 unsigned int jvar,
252 const DenseMatrix<Number> & k);
253
259
265
269 template <typename Residuals, typename Indices>
270 void addResiduals(Assembly & assembly,
271 const Residuals & residuals,
272 const Indices & dof_indices,
273 Real scaling_factor);
274
275 template <typename Residuals, typename Indices>
276 void addResiduals(Assembly & assembly,
277 const Residuals & residuals,
278 const Indices & dof_indices,
279 const std::vector<Real> & scaling_factors);
280
284 template <typename T, typename Indices>
285 void addResiduals(Assembly & assembly,
286 const DenseVector<T> & residuals,
287 const Indices & dof_indices,
288 Real scaling_factor);
289
294 template <typename Residuals, typename Indices>
295 void addResidualsAndJacobian(Assembly & assembly,
296 const Residuals & residuals,
297 const Indices & dof_indices,
298 Real scaling_factor);
299
303 template <typename Residuals, typename Indices>
304 void addJacobian(Assembly & assembly,
305 const Residuals & residuals,
306 const Indices & dof_indices,
307 Real scaling_factor);
308
313 template <typename Residuals, typename Indices>
314 void addJacobian(Assembly & assembly,
315 const Residuals & residuals,
316 const Indices & dof_indices,
317 const std::vector<Real> & scaling_factors);
318
322 void addResiduals(Assembly & assembly, const ADResidualsPacket & packet);
323
328 void addResidualsAndJacobian(Assembly & assembly, const ADResidualsPacket & packet);
329
333 void addJacobian(Assembly & assembly, const ADResidualsPacket & packet);
334
340 template <typename Residuals, typename Indices>
342 const Residuals & residuals,
343 const Indices & dof_indices,
344 Real scaling_factor);
345
351 template <typename Residuals, typename Indices>
353 const Residuals & residuals,
354 const Indices & dof_indices,
355 Real scaling_factor);
356
362 template <typename Residuals, typename Indices>
364 const Residuals & residuals,
365 const Indices & dof_indices,
366 Real scaling_factor);
367
371 void addJacobianElement(Assembly & assembly,
372 Real value,
373 dof_id_type row_index,
374 dof_id_type column_index,
375 Real scaling_factor);
376
380 void addJacobian(Assembly & assembly,
381 const DenseMatrix<Real> & local_k,
382 const std::vector<dof_id_type> & row_indices,
383 const std::vector<dof_id_type> & column_indices,
384 Real scaling_factor);
385
389 template <typename T>
390 void setResidual(SystemBase & sys, const T & residual, MooseVariableFE<T> & var);
391
395 void setResidual(SystemBase & sys, Real residual, dof_id_type dof_index);
396
400 template <typename SetResidualFunctor>
401 void setResidual(SystemBase & sys, SetResidualFunctor set_residual_functor);
402
405
407 DenseVector<Number> _local_re;
408
410 DenseMatrix<Number> _local_ke;
411
413 DenseMatrix<Number> _nonlocal_ke;
414
415private:
422 void prepareVectorTagInternal(Assembly & assembly,
423 unsigned int ivar,
424 const std::set<TagID> & vector_tags,
425 const std::set<TagID> & absolute_value_vector_tags);
426
427#ifndef NDEBUG
431 void checkForNans() const;
432#endif
433
435 std::set<TagID> _vector_tags;
436
438 std::set<TagID> _abs_vector_tags;
439
441 std::set<TagID> _matrix_tags;
442
445 std::set<TagID> _non_ref_vector_tags;
446
450
454 std::set<TagID> _ref_vector_tags;
455
459 std::set<TagID> _ref_abs_vector_tags;
460
463
466
468 std::vector<DenseVector<Number> *> _re_blocks;
469
471 std::vector<DenseVector<Number> *> _absre_blocks;
472
474 std::vector<DenseMatrix<Number> *> _ke_blocks;
475
478 std::vector<Real> _absolute_residuals;
479
481};
482
483#define usingTaggingInterfaceMembers \
484 using TaggingInterface::_subproblem; \
485 using TaggingInterface::accumulateTaggedLocalResidual; \
486 using TaggingInterface::accumulateTaggedLocalMatrix; \
487 using TaggingInterface::prepareVectorTag; \
488 using TaggingInterface::prepareMatrixTag; \
489 using TaggingInterface::prepareVectorTagNeighbor; \
490 using TaggingInterface::_local_re; \
491 using TaggingInterface::prepareVectorTagLower; \
492 using TaggingInterface::prepareMatrixTagNeighbor; \
493 using TaggingInterface::prepareMatrixTagLower; \
494 using TaggingInterface::_local_ke
495
496template <typename Residuals, typename Indices>
497void
499 const Residuals & residuals,
500 const Indices & dof_indices,
501 const Real scaling_factor)
502{
503 assembly.cacheResiduals(
504 residuals, dof_indices, scaling_factor, Assembly::LocalDataKey{}, _vector_tags);
505 if (!_abs_vector_tags.empty())
506 {
507 _absolute_residuals.resize(residuals.size());
508 for (const auto i : index_range(residuals))
509 _absolute_residuals[i] = std::abs(MetaPhysicL::raw_value(residuals[i]));
510
512 dof_indices,
513 scaling_factor,
516 }
517}
518
519template <typename Residuals, typename Indices>
520void
522 const Residuals & residuals,
523 const Indices & dof_indices,
524 const std::vector<Real> & scaling_factors)
525{
526 const auto count = scaling_factors.size();
527 mooseAssert(dof_indices.size() % count == 0,
528 "The number of dof indices should be divided cleanly by the variable count");
529 const auto nshapes = dof_indices.size() / count;
530
531 for (const auto j : make_range(count))
532 // The Residuals type may not offer operator[] (e.g. eigen vectors) but more commonly it
533 // should offer data()
534 addResiduals(assembly,
535 Moose::makeSpan(residuals, j * nshapes, nshapes),
536 Moose::makeSpan(dof_indices, j * nshapes, nshapes),
537 scaling_factors[j]);
538}
539
540template <typename T, typename Indices>
541void
543 const DenseVector<T> & residuals,
544 const Indices & dof_indices,
545 const Real scaling_factor)
546{
547 addResiduals(assembly, residuals.get_values(), dof_indices, scaling_factor);
548}
549
550template <typename Residuals, typename Indices>
551void
553 const Residuals & residuals,
554 const Indices & dof_indices,
555 const Real scaling_factor)
556{
558 residuals, dof_indices, scaling_factor, Assembly::LocalDataKey{}, _vector_tags);
559 if (!_abs_vector_tags.empty())
560 {
561 _absolute_residuals.resize(residuals.size());
562 for (const auto i : index_range(residuals))
563 _absolute_residuals[i] = std::abs(MetaPhysicL::raw_value(residuals[i]));
564
566 dof_indices,
567 scaling_factor,
570 }
571}
572
573template <typename Residuals, typename Indices>
574void
576 const Residuals & residuals,
577 const Indices & dof_indices,
578 Real scaling_factor)
579{
580 addResiduals(assembly, residuals, dof_indices, scaling_factor);
581 addJacobian(assembly, residuals, dof_indices, scaling_factor);
582}
583
584template <typename Residuals, typename Indices>
585void
587 const Residuals & residuals,
588 const Indices & dof_indices,
589 Real scaling_factor)
590{
591 assembly.cacheJacobian(
592 residuals, dof_indices, scaling_factor, Assembly::LocalDataKey{}, _matrix_tags);
593}
594
595template <typename Residuals, typename Indices>
596void
598 const Residuals & residuals,
599 const Indices & dof_indices,
600 const std::vector<Real> & scaling_factors)
601{
602 const auto count = scaling_factors.size();
603 mooseAssert(dof_indices.size() % count == 0,
604 "The number of dof indices should be divided cleanly by the variable count");
605 const auto nshapes = dof_indices.size() / count;
606
607 for (const auto j : make_range(count))
608 // The Residuals type may not offer operator[] (e.g. eigen vectors) but more commonly it
609 // should offer data()
610 addJacobian(assembly,
611 Moose::makeSpan(residuals, j * nshapes, nshapes),
612 Moose::makeSpan(dof_indices, j * nshapes, nshapes),
613 scaling_factors[j]);
614}
615
616template <typename Residuals, typename Indices>
617void
619 const Residuals & residuals,
620 const Indices & dof_indices,
621 Real scaling_factor)
622{
623 addResidualsWithoutConstraints(assembly, residuals, dof_indices, scaling_factor);
624 addJacobianWithoutConstraints(assembly, residuals, dof_indices, scaling_factor);
625}
626
627template <typename Residuals, typename Indices>
628void
630 const Residuals & residuals,
631 const Indices & dof_indices,
632 Real scaling_factor)
633{
635 residuals, dof_indices, scaling_factor, Assembly::LocalDataKey{}, _matrix_tags);
636}
637
638inline void
640 const Real value,
641 const dof_id_type row_index,
642 const dof_id_type column_index,
643 const Real scaling_factor)
644{
645 assembly.cacheJacobian(
646 row_index, column_index, value * scaling_factor, Assembly::LocalDataKey{}, _matrix_tags);
647}
648
649inline void
651 const DenseMatrix<Real> & local_k,
652 const std::vector<dof_id_type> & row_indices,
653 const std::vector<dof_id_type> & column_indices,
654 const Real scaling_factor)
655{
656 assembly.cacheJacobianBlock(
657 local_k, row_indices, column_indices, scaling_factor, Assembly::LocalDataKey{}, _matrix_tags);
658}
659
660template <typename T>
661void
663{
664 for (const auto tag_id : _vector_tags)
665 if (sys.hasVector(tag_id))
666 var.insertNodalValue(sys.getVector(tag_id), residual);
667}
668
669inline void
670TaggingInterface::setResidual(SystemBase & sys, const Real residual, const dof_id_type dof_index)
671{
672 for (const auto tag_id : _vector_tags)
673 if (sys.hasVector(tag_id))
674 sys.getVector(tag_id).set(dof_index, residual);
675}
676
677template <typename SetResidualFunctor>
678void
679TaggingInterface::setResidual(SystemBase & sys, const SetResidualFunctor set_residual_functor)
680{
681 for (const auto tag_id : _vector_tags)
682 if (sys.hasVector(tag_id))
683 set_residual_functor(sys.getVector(tag_id));
684}
unsigned int TagID
Definition MooseTypes.h:238
unsigned int count
Definition MortarUtils.C:53
InputParameters validParams()
Key structure for APIs adding/caching local element residuals/Jacobians.
Definition Assembly.h:863
Keeps track of stuff related to assembling.
Definition Assembly.h:110
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:3099
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:3059
void cacheJacobianBlock(const DenseMatrix< Number > &jac_block, const std::vector< dof_id_type > &idof_indices, const std::vector< dof_id_type > &jdof_indices, Real scaling_factor, LocalDataKey, const std::set< TagID > &tags)
Cache a local Jacobian block with the provided rows (idof_indices) and columns (jdof_indices) for eve...
Definition Assembly.C:3721
void cacheJacobian(GlobalDataKey)
Takes the values that are currently in _sub_Kee and appends them to the cached values.
Definition Assembly.C:4045
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:3179
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
This is the common base class for objects that give contributions to a linear system.
A storage container for MooseObjects that inherit from SetupInterface.
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
Class for stuff related to variables.
void insertNodalValue(libMesh::NumericVector< libMesh::Number > &residual, const DofValue &v)
Write a nodal value to the passed-in solution vector.
The Kokkos FE system class.
Base class for Kokkos objects that contribute to a linear system, i.e.
The base class for Kokkos residual objects.
Nonlinear eigenvalue system to be solved.
Nonlinear system to be solved.
This is the common base class for objects that give residual contributions.
Generic class for solving transient nonlinear problems.
Definition SubProblem.h:79
Base class for a system (of equations)
Definition SystemBase.h:87
bool hasVector(const std::string &tag_name) const
Check if the named vector exists in the system.
Definition SystemBase.C:923
virtual NumericVector< Number > & getVector(const std::string &name)
Get a raw NumericVector by name.
Definition SystemBase.C:932
Class that is used as a parameter to some of our matrix tag APIs that allows only blessed framework c...
MatrixTagsKey(const MatrixTagsKey &)
Class that is used as a parameter to some of our vector tag APIs that allows only blessed framework c...
VectorTagsKey(const VectorTagsKey &)
DenseMatrix< Number > _local_ke
Holds local Jacobian entries as they are accumulated by this Kernel.
SubProblem & _subproblem
SubProblem that contains tag info.
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.
void accumulateTaggedLocalMatrix()
Local Jacobian blocks will be appended by adding the current local kernel Jacobian.
std::vector< DenseMatrix< Number > * > _ke_blocks
Kernel blocks Vectors For each Tag.
virtual ~TaggingInterface()
const MooseObject & _moose_object
Moose objct this tag works on.
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.
std::set< TagID > _abs_vector_tags
The absolute value residual tag ids.
void prepareVectorTagLower(Assembly &assembly, unsigned int ivar)
Prepare data for computing the residual according to active tags for mortar constraints.
std::vector< DenseVector< Number > * > _re_blocks
Residual blocks Vectors For each Tag.
bool hasVectorTags() const
void checkForNans() const
Checks _local_re for NaNs/Infs and returns an error if found.
void setResidual(SystemBase &sys, const T &residual, MooseVariableFE< T > &var)
Set residual using the variables' insertion API.
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.
void prepareMatrixTag(Assembly &assembly, unsigned int ivar, unsigned int jvar)
Prepare data for computing element jacobian according to the active tags.
static InputParameters validParams()
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.
void useMatrixTag(const TagName &tag_name, MatrixTagsKey)
std::set< TagID > _vector_tags
The vector tag ids this Kernel will contribute to.
void accumulateTaggedNonlocalMatrix()
Nonlocal Jacobian blocks will be appended by adding the current nonlocal kernel Jacobian.
void accumulateTaggedLocalResidual()
Local residual blocks will be appended by adding the current local kernel residual.
std::set< TagID > _non_ref_vector_tags
A set to hold vector tags excluding the reference residual tag.
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.
void prepareMatrixTagNonlocal(Assembly &assembly, unsigned int ivar, unsigned int jvar)
Prepare data for computing nonlocal element jacobian according to the active tags.
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 std::set< TagID > & getMatrixTags(MatrixTagsKey) const
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...
void prepareVectorTagNeighbor(Assembly &assembly, unsigned int ivar)
Prepare data for computing element residual the according to active tags for DG and interface kernels...
std::vector< Real > _absolute_residuals
A container to hold absolute values of residuals passed into addResiduals.
void useVectorTag(const TagName &tag_name, VectorTagsKey)
const InputParameters & _tag_params
Parameters from moose object.
std::set< TagID > _ref_vector_tags
A set of either size 1 or 0.
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.
std::set< TagID > _matrix_tags
The matrices this Kernel will contribute to.
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...
const std::set< TagID > & getVectorTags(VectorTagsKey) const
void prepareVectorTag(Assembly &assembly, unsigned int ivar)
Prepare data for computing element residual according to active tags.
void assignTaggedLocalResidual()
Local residual blocks will assigned as the current local kernel residual.
DenseVector< Number > _local_re
Holds local residual entries as they are accumulated by this Kernel.
std::vector< DenseVector< Number > * > _absre_blocks
Residual blocks for absolute value residual tags.
ResidualTagType
Enumerate whether a (residual) vector tag is to be of a non-reference or reference tag type.
void assignTaggedLocalMatrix()
Local Jacobian blocks will assigned as the current local kernel Jacobian.
DenseMatrix< Number > _nonlocal_ke
Holds nonlocal Jacobian entries as they are accumulated by this Kernel.
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...
std::set< TagID > _non_ref_abs_vector_tags
A set to hold absolute value vector tags excluding the reference residual tag.
std::set< TagID > _ref_abs_vector_tags
A set of either size 1 or 0.
virtual void set(const numeric_index_type i, const T value)=0
auto raw_value(const Eigen::Map< T > &in)
ConstraintJacobianType
Definition MooseTypes.h:851
auto makeSpan(C &container, std::size_t offset, std::size_t n)
Helper function for creating a span from a given container.
DGJacobianType
Definition MooseTypes.h:804
Utility structure for packaging up all of the residual object's information needed to add into the sy...
const DenseVector< ADReal > & residuals
const std::vector< dof_id_type > & dof_indices
const Real scaling_factor