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

Generated by: LCOV version 1.14