LCOV - code coverage report
Current view: top level - include/variables - MooseVariableDataBase.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: fef103 Lines: 15 16 93.8 %
Date: 2025-09-03 20:01:23 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      518111 :   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    28226025 :   void prepareAux() { _has_dof_values = false; }
     141             : 
     142             :   /**
     143             :    * size matrix tag data
     144             :    */
     145             :   void sizeMatrixTagData();
     146             : 
     147             : protected:
     148             :   /**
     149             :    * @returns The variable to which the data in this class belongs to
     150             :    */
     151           0 :   virtual const MooseVariableField<OutputType> & var() const { return _var; }
     152             : 
     153             :   /**
     154             :    * insert a solution tag into our tag containers
     155             :    */
     156             :   void insertSolutionTag(TagID tag_id);
     157             : 
     158             :   /**
     159             :    * Request that we have at least \p state number of older solution states/vectors
     160             :    */
     161             :   void needSolutionState(unsigned int state);
     162             : 
     163             :   /**
     164             :    * Helper methods for assigning dof values from their corresponding solution values
     165             :    */
     166             :   void fetchDoFValues();
     167             :   void zeroSizeDofValues();
     168             :   void getArrayDoFValues(const libMesh::NumericVector<libMesh::Number> & sol,
     169             :                          unsigned int n,
     170             :                          MooseArray<RealEigenVector> & dof_values) const;
     171             :   void assignNodalValue();
     172             : 
     173             :   /**
     174             :    * Helper method that converts a \p SolutionState argument into a corresponding tag ID,
     175             :    * potentially requesting necessary additional solution states and assigning tag id data members,
     176             :    * and then calls the provided \p functor with the tag ID
     177             :    */
     178             :   template <typename ReturnType, typename Functor>
     179             :   const ReturnType & stateToTagHelper(Moose::SolutionState state, Functor functor);
     180             : 
     181             :   /**
     182             :    * resize the vector tag need flags and data containers to accomodate this tag index
     183             :    */
     184             :   void resizeVectorTagData(TagID tag);
     185             : 
     186             :   /// The MOOSE system which ultimately holds the vectors and matrices relevant to this variable
     187             :   /// data
     188             :   SystemBase & _sys;
     189             : 
     190             :   /// The subproblem which we can query for information related to tagged vectors and matrices
     191             :   const SubProblem & _subproblem;
     192             : 
     193             :   /// The thread ID that this object is on
     194             :   const THREAD_ID _tid;
     195             : 
     196             :   /// The degree of freedom map from libMesh
     197             :   const libMesh::DofMap & _dof_map;
     198             : 
     199             :   /// Number of components of the associated variable
     200             :   unsigned int _count;
     201             : 
     202             :   /// Whether we currently have degree of freedom values stored in our local containers
     203             :   /// (corresponding to the current element)
     204             :   bool _has_dof_values;
     205             : 
     206             :   /// The maximum number of older solution states our variable needs
     207             :   unsigned int _max_state;
     208             : 
     209             :   /// The vector tag ID corresponding to the solution vector
     210             :   TagID _solution_tag;
     211             : 
     212             :   /// The vector tag ID corresponding to the old solution vector
     213             :   TagID _old_solution_tag;
     214             : 
     215             :   /// The vector tag ID corresponding to the older solution vector
     216             :   TagID _older_solution_tag;
     217             : 
     218             :   /// The vector tag ID corresponding to the previous nonlinear iteration's solution vector
     219             :   TagID _previous_nl_solution_tag;
     220             : 
     221             :   /// The dof indices for the current element
     222             :   std::vector<dof_id_type> _dof_indices;
     223             : 
     224             :   mutable std::vector<bool> _need_vector_tag_dof_u;
     225             :   mutable std::vector<bool> _need_matrix_tag_dof_u;
     226             : 
     227             :   // Dof values of tagged vectors
     228             :   std::vector<DoFValue> _vector_tags_dof_u;
     229             :   // Dof values of the diagonal of tagged matrices
     230             :   std::vector<DoFValue> _matrix_tags_dof_u;
     231             : 
     232             :   std::vector<FieldVariableValue> _vector_tag_u;
     233             :   mutable std::vector<bool> _need_vector_tag_u;
     234             :   std::vector<FieldVariableGradient> _vector_tag_grad;
     235             :   mutable std::vector<bool> _need_vector_tag_grad;
     236             :   std::vector<FieldVariableValue> _matrix_tag_u;
     237             :   mutable std::vector<bool> _need_matrix_tag_u;
     238             : 
     239             :   /// Nodal values
     240             :   OutputType _nodal_value;
     241             :   OutputType _nodal_value_old;
     242             :   OutputType _nodal_value_older;
     243             :   OutputType _nodal_value_previous_nl;
     244             : 
     245             :   /// Nodal values as MooseArrays for use with AuxKernels
     246             :   MooseArray<OutputType> _nodal_value_array;
     247             :   MooseArray<OutputType> _nodal_value_old_array;
     248             :   MooseArray<OutputType> _nodal_value_older_array;
     249             : 
     250             :   /// u dot flags
     251             :   mutable bool _need_u_dot;
     252             :   mutable bool _need_u_dotdot;
     253             :   mutable bool _need_u_dot_old;
     254             :   mutable bool _need_u_dotdot_old;
     255             :   mutable bool _need_du_dot_du;
     256             :   mutable bool _need_du_dotdot_du;
     257             : 
     258             :   /// gradient dot flags
     259             :   mutable bool _need_grad_dot;
     260             :   mutable bool _need_grad_dotdot;
     261             : 
     262             :   /// local solution flags
     263             :   mutable bool _need_dof_values_dot;
     264             :   mutable bool _need_dof_values_dotdot;
     265             :   mutable bool _need_dof_values_dot_old;
     266             :   mutable bool _need_dof_values_dotdot_old;
     267             :   mutable bool _need_dof_du_dot_du;
     268             :   mutable bool _need_dof_du_dotdot_du;
     269             : 
     270             :   /// time derivative of the solution values
     271             :   DoFValue _dof_values_dot;
     272             :   /// second time derivative of the solution values
     273             :   DoFValue _dof_values_dotdot;
     274             :   /// the previous time step's solution value time derivative
     275             :   DoFValue _dof_values_dot_old;
     276             :   /// the previous time step's solution value second time derivative
     277             :   DoFValue _dof_values_dotdot_old;
     278             :   /// derivatives of the solution value time derivative with respect to the degrees of freedom
     279             :   MooseArray<libMesh::Number> _dof_du_dot_du;
     280             :   /// derivatives of the solution value second time derivative with respect to the degrees of
     281             :   /// freedom
     282             :   MooseArray<libMesh::Number> _dof_du_dotdot_du;
     283             : 
     284             :   /// nodal values of u_dot
     285             :   OutputType _nodal_value_dot;
     286             :   /// nodal values of u_dotdot
     287             :   OutputType _nodal_value_dotdot;
     288             :   /// nodal values of u_dot_old
     289             :   OutputType _nodal_value_dot_old;
     290             :   /// nodal values of u_dotdot_old
     291             :   OutputType _nodal_value_dotdot_old;
     292             : 
     293             :   /// The set of vector tags (residual + solution) we need to evaluate
     294             :   std::set<TagID> _required_vector_tags;
     295             : 
     296             :   /// The set of solution tags we need to evaluate
     297             :   std::set<TagID> _solution_tags;
     298             : 
     299             : private:
     300             :   /// A const reference to the owning MooseVariableField object
     301             :   const MooseVariableField<OutputType> & _var;
     302             : };
     303             : 
     304             : template <>
     305             : void MooseVariableDataBase<RealEigenVector>::fetchDoFValues();
     306             : 
     307             : template <>
     308             : void MooseVariableDataBase<RealVectorValue>::assignNodalValue();
     309             : 
     310             : template <typename OutputType>
     311             : void
     312    60085163 : MooseVariableDataBase<OutputType>::setActiveTags(const std::set<TagID> & vtags)
     313             : {
     314    60085163 :   _required_vector_tags = _solution_tags;
     315    60645565 :   for (const auto tag : vtags)
     316      560402 :     _required_vector_tags.insert(tag);
     317             : 
     318    60085163 :   if (!_required_vector_tags.empty())
     319             :   {
     320    60085163 :     const auto largest_tag_id = *_required_vector_tags.rbegin();
     321    60085163 :     if (largest_tag_id >= _need_vector_tag_dof_u.size())
     322         238 :       resizeVectorTagData(largest_tag_id);
     323             :   }
     324    60085163 : }
     325             : 
     326             : template <typename OutputType>
     327             : void
     328      590372 : MooseVariableDataBase<OutputType>::insertSolutionTag(const TagID tag_id)
     329             : {
     330      590372 :   _solution_tags.insert(tag_id);
     331      590372 :   _required_vector_tags.insert(tag_id);
     332      590372 : }

Generated by: LCOV version 1.14