LCOV - code coverage report
Current view: top level - include/variables - MooseVariableDataBase.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #32971 (54bef8) with base c6cf66 Lines: 15 16 93.8 %
Date: 2026-05-29 20:35:17 Functions: 11 18 61.1 %
Legend: Lines: hit not hit

          Line data    Source code
       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 "MooseArray.h"
      13             : #include "MooseTypes.h"
      14             : 
      15             : #include "libmesh/tensor_tools.h"
      16             : #include "libmesh/vector_value.h"
      17             : #include "libmesh/tensor_value.h"
      18             : #include "libmesh/type_n_tensor.h"
      19             : #include "libmesh/enum_fe_family.h"
      20             : 
      21             : #include <vector>
      22             : 
      23             : template <typename>
      24             : class MooseVariableField;
      25             : class SubProblem;
      26             : class SystemBase;
      27             : namespace libMesh
      28             : {
      29             : class DofMap;
      30             : }
      31             : 
      32             : template <typename OutputType>
      33             : class MooseVariableDataBase
      34             : {
      35             : public:
      36             :   // type for gradient, second and divergence of template class OutputType
      37             :   typedef typename libMesh::TensorTools::IncrementRank<OutputType>::type OutputGradient;
      38             :   typedef typename libMesh::TensorTools::IncrementRank<OutputGradient>::type OutputSecond;
      39             :   typedef typename libMesh::TensorTools::DecrementRank<OutputType>::type OutputDivergence;
      40             : 
      41             :   // shortcut for types storing values on quadrature points
      42             :   typedef MooseArray<OutputType> FieldVariableValue;
      43             :   typedef MooseArray<OutputGradient> FieldVariableGradient;
      44             : 
      45             :   // DoF value type for the template class OutputType
      46             :   typedef typename Moose::DOFType<OutputType>::type DofValue;
      47             :   typedef typename Moose::ADType<DofValue>::type ADDofValue;
      48             :   typedef MooseArray<DofValue> DofValues;
      49             :   typedef MooseArray<ADDofValue> ADDofValues;
      50             : 
      51             :   MooseVariableDataBase(const MooseVariableField<OutputType> & var,
      52             :                         SystemBase & sys,
      53             :                         THREAD_ID tid);
      54             : 
      55      508601 :   virtual ~MooseVariableDataBase() = default;
      56             : 
      57             :   /**
      58             :    * @return Whether this data is associated with a nodal variable
      59             :    */
      60             :   virtual bool isNodal() const = 0;
      61             : 
      62             :   /**
      63             :    * Whether this data is associated with a variable that has DoFs on nodes
      64             :    */
      65             :   virtual bool hasDoFsOnNodes() const = 0;
      66             : 
      67             :   /**
      68             :    * Return the variable continuity
      69             :    */
      70             :   virtual libMesh::FEContinuity getContinuity() const = 0;
      71             : 
      72             :   /**
      73             :    * Local solution getter
      74             :    * @param state The state of the simulation: current, old, older, previous nl
      75             :    */
      76             :   const FieldVariableValue & sln(Moose::SolutionState state) const;
      77             : 
      78             :   /**
      79             :    * Local solution gradient getter
      80             :    * @param state The state of the simulation: current, old, older, previous nl
      81             :    */
      82             :   const FieldVariableGradient & gradSln(Moose::SolutionState state) const;
      83             : 
      84             :   /**
      85             :    * The oldest solution state that is requested for this variable
      86             :    * (0 = current, 1 = old, 2 = older, etc).
      87             :    */
      88             :   unsigned int oldestSolutionStateRequested() const;
      89             : 
      90             :   /**
      91             :    * Set nodal value
      92             :    */
      93             :   void setNodalValue(const OutputType & value, unsigned int idx = 0);
      94             : 
      95             :   /**
      96             :    * Set the current local DOF values to the input vector
      97             :    */
      98             :   void insert(libMesh::NumericVector<libMesh::Number> & residual);
      99             : 
     100             :   /**
     101             :    * Add the current local DOF values to the input vector
     102             :    */
     103             :   void add(libMesh::NumericVector<libMesh::Number> & residual);
     104             : 
     105             :   /**
     106             :    * prepare the initial condition
     107             :    */
     108             :   void prepareIC();
     109             : 
     110             :   /////////////////////////// DoF value getters /////////////////////////////////////
     111             : 
     112             :   const DofValues & dofValues() const;
     113             :   const DofValues & dofValuesOld() const;
     114             :   const DofValues & dofValuesOlder() const;
     115             :   const DofValues & dofValuesPreviousNL() const;
     116             : 
     117             :   ///////////////////////// Nodal value getters ///////////////////////////////////////////
     118             : 
     119             :   const OutputType & nodalValue(Moose::SolutionState state) const;
     120             :   const MooseArray<OutputType> & nodalValueArray(Moose::SolutionState state) const;
     121             : 
     122             :   /////////////////////////////// Tags ///////////////////////////////////////////////////
     123             : 
     124             :   const FieldVariableValue & vectorTagValue(TagID tag) const;
     125             :   const FieldVariableGradient & vectorTagGradient(TagID tag) const;
     126             :   const FieldVariableValue & matrixTagValue(TagID tag) const;
     127             :   const DofValues & nodalVectorTagValue(TagID tag) const;
     128             :   const DofValues & nodalMatrixTagValue(TagID tag) const;
     129             :   const DofValues & vectorTagDofValue(TagID tag) const;
     130             :   const DofValues & vectorTagDofValue(Moose::SolutionState state) const;
     131             : 
     132             :   /**
     133             :    * Set the active vector tags
     134             :    * @param vtags Additional vector tags that this variable will need to query at dof indices for,
     135             :    * in addition to our own required solution tags
     136             :    */
     137             :   void setActiveTags(const std::set<TagID> & vtags);
     138             : 
     139             :   /**
     140             :    * Clear aux state
     141             :    */
     142    24954873 :   void prepareAux() { _has_dof_values = false; }
     143             : 
     144             :   /**
     145             :    * size matrix tag data
     146             :    */
     147             :   void sizeMatrixTagData();
     148             : 
     149             : protected:
     150             :   /**
     151             :    * @returns The variable to which the data in this class belongs to
     152             :    */
     153           0 :   virtual const MooseVariableField<OutputType> & var() const { return _var; }
     154             : 
     155             :   /**
     156             :    * insert a solution tag into our tag containers
     157             :    */
     158             :   void insertSolutionTag(TagID tag_id);
     159             : 
     160             :   /**
     161             :    * Request that we have at least \p state number of older solution states/vectors
     162             :    */
     163             :   void needSolutionState(unsigned int state);
     164             : 
     165             :   /**
     166             :    * Helper methods for assigning dof values from their corresponding solution values
     167             :    */
     168             :   void fetchDofValues();
     169             :   void zeroSizeDofValues();
     170             :   void getArrayDofValues(const libMesh::NumericVector<libMesh::Number> & sol,
     171             :                          unsigned int n,
     172             :                          MooseArray<RealEigenVector> & dof_values) const;
     173             :   void assignNodalValue();
     174             : 
     175             :   /**
     176             :    * Helper method that converts a \p SolutionState argument into a corresponding tag ID,
     177             :    * potentially requesting necessary additional solution states and assigning tag id data members,
     178             :    * and then calls the provided \p functor with the tag ID
     179             :    */
     180             :   template <typename ReturnType, typename Functor>
     181             :   const ReturnType & stateToTagHelper(Moose::SolutionState state, Functor functor);
     182             : 
     183             :   /**
     184             :    * resize the vector tag need flags and data containers to accomodate this tag index
     185             :    */
     186             :   void resizeVectorTagData(TagID tag);
     187             : 
     188             :   /// The MOOSE system which ultimately holds the vectors and matrices relevant to this variable
     189             :   /// data
     190             :   SystemBase & _sys;
     191             : 
     192             :   /// The subproblem which we can query for information related to tagged vectors and matrices
     193             :   const SubProblem & _subproblem;
     194             : 
     195             :   /// The thread ID that this object is on
     196             :   const THREAD_ID _tid;
     197             : 
     198             :   /// The degree of freedom map from libMesh
     199             :   const libMesh::DofMap & _dof_map;
     200             : 
     201             :   /// Number of components of the associated variable
     202             :   unsigned int _count;
     203             : 
     204             :   /// Whether we currently have degree of freedom values stored in our local containers
     205             :   /// (corresponding to the current element)
     206             :   bool _has_dof_values;
     207             : 
     208             :   /// The maximum number of older solution states our variable needs
     209             :   unsigned int _max_state;
     210             : 
     211             :   /// The vector tag ID corresponding to the solution vector
     212             :   TagID _solution_tag;
     213             : 
     214             :   /// The vector tag ID corresponding to the old solution vector
     215             :   TagID _old_solution_tag;
     216             : 
     217             :   /// The vector tag ID corresponding to the older solution vector
     218             :   TagID _older_solution_tag;
     219             : 
     220             :   /// The vector tag ID corresponding to the previous nonlinear iteration's solution vector
     221             :   TagID _previous_nl_solution_tag;
     222             : 
     223             :   /// The dof indices for the current element
     224             :   std::vector<dof_id_type> _dof_indices;
     225             : 
     226             :   mutable std::vector<bool> _need_vector_tag_dof_u;
     227             :   mutable std::vector<bool> _need_matrix_tag_dof_u;
     228             : 
     229             :   // Dof values of tagged vectors
     230             :   std::vector<DofValues> _vector_tags_dof_u;
     231             :   // Dof values of the diagonal of tagged matrices
     232             :   std::vector<DofValues> _matrix_tags_dof_u;
     233             : 
     234             :   std::vector<FieldVariableValue> _vector_tag_u;
     235             :   mutable std::vector<bool> _need_vector_tag_u;
     236             :   std::vector<FieldVariableGradient> _vector_tag_grad;
     237             :   mutable std::vector<bool> _need_vector_tag_grad;
     238             :   std::vector<FieldVariableValue> _matrix_tag_u;
     239             :   mutable std::vector<bool> _need_matrix_tag_u;
     240             : 
     241             :   /// Nodal values
     242             :   OutputType _nodal_value;
     243             :   OutputType _nodal_value_old;
     244             :   OutputType _nodal_value_older;
     245             :   OutputType _nodal_value_previous_nl;
     246             : 
     247             :   /// Nodal values as MooseArrays for use with AuxKernels
     248             :   MooseArray<OutputType> _nodal_value_array;
     249             :   MooseArray<OutputType> _nodal_value_old_array;
     250             :   MooseArray<OutputType> _nodal_value_older_array;
     251             : 
     252             :   /// u dot flags
     253             :   mutable bool _need_u_dot;
     254             :   mutable bool _need_u_dotdot;
     255             :   mutable bool _need_u_dot_old;
     256             :   mutable bool _need_u_dotdot_old;
     257             :   mutable bool _need_du_dot_du;
     258             :   mutable bool _need_du_dotdot_du;
     259             : 
     260             :   /// gradient dot flags
     261             :   mutable bool _need_grad_dot;
     262             :   mutable bool _need_grad_dotdot;
     263             : 
     264             :   /// local solution flags
     265             :   mutable bool _need_dof_values_dot;
     266             :   mutable bool _need_dof_values_dotdot;
     267             :   mutable bool _need_dof_values_dot_old;
     268             :   mutable bool _need_dof_values_dotdot_old;
     269             :   mutable bool _need_dof_du_dot_du;
     270             :   mutable bool _need_dof_du_dotdot_du;
     271             : 
     272             :   /// time derivative of the solution values
     273             :   DofValues _dof_values_dot;
     274             :   /// second time derivative of the solution values
     275             :   DofValues _dof_values_dotdot;
     276             :   /// the previous time step's solution value time derivative
     277             :   DofValues _dof_values_dot_old;
     278             :   /// the previous time step's solution value second time derivative
     279             :   DofValues _dof_values_dotdot_old;
     280             :   /// derivatives of the solution value time derivative with respect to the degrees of freedom
     281             :   MooseArray<libMesh::Number> _dof_du_dot_du;
     282             :   /// derivatives of the solution value second time derivative with respect to the degrees of
     283             :   /// freedom
     284             :   MooseArray<libMesh::Number> _dof_du_dotdot_du;
     285             : 
     286             :   /// nodal values of u_dot
     287             :   OutputType _nodal_value_dot;
     288             :   /// nodal values of u_dotdot
     289             :   OutputType _nodal_value_dotdot;
     290             :   /// nodal values of u_dot_old
     291             :   OutputType _nodal_value_dot_old;
     292             :   /// nodal values of u_dotdot_old
     293             :   OutputType _nodal_value_dotdot_old;
     294             : 
     295             :   /// The set of vector tags (residual + solution) we need to evaluate
     296             :   std::set<TagID> _required_vector_tags;
     297             : 
     298             :   /// The set of solution tags we need to evaluate
     299             :   std::set<TagID> _solution_tags;
     300             : 
     301             : private:
     302             :   /// A const reference to the owning MooseVariableField object
     303             :   const MooseVariableField<OutputType> & _var;
     304             : };
     305             : 
     306             : template <>
     307             : void MooseVariableDataBase<RealEigenVector>::fetchDofValues();
     308             : 
     309             : template <>
     310             : void MooseVariableDataBase<RealVectorValue>::assignNodalValue();
     311             : 
     312             : template <typename OutputType>
     313             : void
     314    54632612 : MooseVariableDataBase<OutputType>::setActiveTags(const std::set<TagID> & vtags)
     315             : {
     316    54632612 :   _required_vector_tags = _solution_tags;
     317    55156803 :   for (const auto tag : vtags)
     318      524191 :     _required_vector_tags.insert(tag);
     319             : 
     320    54632612 :   if (!_required_vector_tags.empty())
     321             :   {
     322    54632612 :     const auto largest_tag_id = *_required_vector_tags.rbegin();
     323    54632612 :     if (largest_tag_id >= _need_vector_tag_dof_u.size())
     324         221 :       resizeVectorTagData(largest_tag_id);
     325             :   }
     326    54632612 : }
     327             : 
     328             : template <typename OutputType>
     329             : void
     330      571531 : MooseVariableDataBase<OutputType>::insertSolutionTag(const TagID tag_id)
     331             : {
     332      571531 :   _solution_tags.insert(tag_id);
     333      571531 :   _required_vector_tags.insert(tag_id);
     334      571531 : }

Generated by: LCOV version 1.14