LCOV - code coverage report
Current view: top level - include/systems - LinearSystem.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #32971 (54bef8) with base c6cf66 Lines: 9 13 69.2 %
Date: 2026-05-29 20:35:17 Functions: 9 13 69.2 %
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 "SolverSystem.h"
      13             : #include "LinearFVGradientInterface.h"
      14             : #include "PerfGraphInterface.h"
      15             : #include "GradientLimiterType.h"
      16             : 
      17             : #include <set>
      18             : 
      19             : #include "libmesh/transient_system.h"
      20             : #include "libmesh/linear_implicit_system.h"
      21             : #include "libmesh/linear_solver.h"
      22             : 
      23             : class LinearFVKernel;
      24             : 
      25             : // libMesh forward declarations
      26             : namespace libMesh
      27             : {
      28             : template <typename T>
      29             : class NumericVector;
      30             : template <typename T>
      31             : class SparseMatrix;
      32             : template <typename T>
      33             : class DiagonalMatrix;
      34             : } // namespace libMesh
      35             : 
      36             : /**
      37             :  * Linear system to be solved
      38             :  */
      39             : class LinearSystem : public SolverSystem,
      40             :                      public PerfGraphInterface,
      41             :                      public LinearFVGradientInterface
      42             : {
      43             : public:
      44             :   LinearSystem(FEProblemBase & problem, const std::string & name);
      45             :   virtual ~LinearSystem();
      46             : 
      47             :   virtual void solve() override;
      48             : 
      49             :   /**
      50             :    * At the moment, this is only used for the multi-system fixed point
      51             :    * iteration. We return true here since ther is no way to specify
      52             :    * separate linear residuals in FEProblemSolve yet.
      53             :    */
      54       26076 :   virtual bool converged() override { return _converged; }
      55             : 
      56             :   virtual void initialSetup() override;
      57             :   virtual void reinit() override;
      58             : 
      59             :   // Overriding these to make sure the linear systems don't do anything during
      60             :   // residual/jacobian setup
      61           0 :   virtual void residualSetup() override {}
      62           0 :   virtual void jacobianSetup() override {}
      63             : 
      64             :   /**
      65             :    * Quit the current solve as soon as possible.
      66             :    */
      67             :   virtual void stopSolve(const ExecFlagType & exec_flag,
      68             :                          const std::set<TagID> & vector_tags_to_close) override;
      69             : 
      70             :   /**
      71             :    * If the system has a kernel that corresponds to a time derivative.
      72             :    * Considering that we don't have transient capabilities for linear
      73             :    * systems at the moment, this is false.
      74             :    */
      75             :   virtual bool containsTimeKernel() override;
      76           0 :   virtual std::vector<std::string> timeKernelVariableNames() override { return {}; }
      77             : 
      78             :   /**
      79             :    * Compute the right hand side and the system matrix of the system for given tags.
      80             :    * @param vector_tags The IDs of the vector tags whose right hand side contribution should be
      81             :    * included
      82             :    * @param matrix_tags The IDs of the matrix tags whose matrix contribution should be included
      83             :    * @param compute_gradients A flag to disable the computation of new gradients during the
      84             :    * assembly, can be used to lag gradients
      85             :    */
      86             :   void computeLinearSystemTags(const std::set<TagID> & vector_tags,
      87             :                                const std::set<TagID> & matrix_tags,
      88             :                                const bool compute_gradients = true);
      89             : 
      90             :   /**
      91             :    * Return a reference to the stored linear implicit system
      92             :    */
      93      105100 :   libMesh::LinearImplicitSystem & linearImplicitSystem() { return _linear_implicit_system; }
      94             : 
      95             :   /**
      96             :    *  Return a numeric vector that is associated with the time tag.
      97             :    */
      98             :   NumericVector<Number> & getRightHandSideTimeVector();
      99             : 
     100             :   /**
     101             :    * Return a numeric vector that is associated with the nontime tag.
     102             :    */
     103             :   NumericVector<Number> & getRightHandSideNonTimeVector();
     104             : 
     105             :   virtual void augmentSparsity(SparsityPattern::Graph & sparsity,
     106             :                                std::vector<dof_id_type> & n_nz,
     107             :                                std::vector<dof_id_type> & n_oz) override;
     108             : 
     109             :   /**
     110             :    * Return the number of linear iterations
     111             :    */
     112         160 :   unsigned int nLinearIterations() const { return _n_linear_iters; }
     113             : 
     114      794345 :   virtual System & system() override { return _sys; }
     115     3779030 :   virtual const System & system() const override { return _sys; }
     116             : 
     117             :   ///@{
     118             :   /// Accessors of important tag IDs
     119           0 :   TagID rightHandSideTimeVectorTag() const { return _rhs_time_tag; }
     120             :   TagID rightHandSideNonTimeVectorTag() const { return _rhs_non_time_tag; }
     121       78480 :   TagID rightHandSideVectorTag() const { return _rhs_tag; }
     122       78480 :   virtual TagID systemMatrixTag() const override { return _system_matrix_tag; }
     123             :   ///@}
     124             : 
     125             :   /// Fetching the right hand side vector from the libmesh system.
     126       26160 :   NumericVector<Number> & getRightHandSideVector() { return *_linear_implicit_system.rhs; }
     127             :   const NumericVector<Number> & getRightHandSideVector() const
     128             :   {
     129             :     return *_linear_implicit_system.rhs;
     130             :   }
     131             : 
     132             :   /// Fetching the system matrix from the libmesh system.
     133       26160 :   SparseMatrix<Number> & getSystemMatrix() { return *_linear_implicit_system.matrix; }
     134             :   const SparseMatrix<Number> & getSystemMatrix() const { return *_linear_implicit_system.matrix; }
     135             : 
     136             :   using LinearFVGradientInterface::computeGradients;
     137             :   using LinearFVGradientInterface::linearFVLimitedGradientContainer;
     138             :   using LinearFVGradientInterface::requestLinearFVLimitedGradients;
     139             : 
     140             :   virtual void compute(ExecFlagType type) override;
     141             : 
     142             : protected:
     143             :   /**
     144             :    * Compute the right hand side and system matrix for given tags
     145             :    * @param vector_tags The tags of kernels for which the right hand side is to be computed.
     146             :    * @param matrix_tags The tags of kernels for which the system matrix is to be computed.
     147             :    * @param compute_gradients A flag to disable the computation of new gradients during the
     148             :    * assembly, can be used to lag gradients
     149             :    */
     150             :   void computeLinearSystemInternal(const std::set<TagID> & vector_tags,
     151             :                                    const std::set<TagID> & matrix_tags,
     152             :                                    const bool compute_gradients = true);
     153             : 
     154             :   /// Base class reference to the libmesh system
     155             :   System & _sys;
     156             : 
     157             :   /// The linear iterations needed for convergence
     158             :   unsigned int _current_l_its;
     159             : 
     160             :   /// Vector tags to temporarily store all tags associated with the current system.
     161             :   std::set<TagID> _vector_tags;
     162             : 
     163             :   /// Matrix tags to temporarily store all tags associated with the current system.
     164             :   std::set<TagID> _matrix_tags;
     165             : 
     166             :   /// Tag for time contribution rhs
     167             :   TagID _rhs_time_tag;
     168             : 
     169             :   /// right hand side vector for time contributions
     170             :   NumericVector<Number> * _rhs_time;
     171             : 
     172             :   /// Tag for non-time contribution rhs
     173             :   TagID _rhs_non_time_tag;
     174             : 
     175             :   /// right hand side vector for non-time contributions
     176             :   NumericVector<Number> * _rhs_non_time;
     177             : 
     178             :   /// Used for the right hand side vector from PETSc
     179             :   TagID _rhs_tag;
     180             : 
     181             :   /// Tag for non-time contribution to the system matrix
     182             :   TagID _system_matrix_non_time_tag;
     183             : 
     184             :   /// Tag for every contribution to system matrix
     185             :   TagID _system_matrix_tag;
     186             : 
     187             :   /// Number of linear iterations
     188             :   unsigned int _n_linear_iters;
     189             : 
     190             :   /// The initial linear residual
     191             :   Real _initial_linear_residual;
     192             : 
     193             :   /// The final linear residual
     194             :   Real _final_linear_residual;
     195             : 
     196             :   /// If the solve on the linear system converged
     197             :   bool _converged;
     198             : 
     199             :   /// Base class reference to the linear implicit system in libmesh
     200             :   libMesh::LinearImplicitSystem & _linear_implicit_system;
     201             : 
     202             : private:
     203             :   /// The current states of the solution (0 = current, 1 = old, etc)
     204             :   std::vector<NumericVector<Number> *> _solution_state;
     205             : };

Generated by: LCOV version 1.14