LCOV - code coverage report
Current view: top level - include/convergence - ReferenceResidualConvergence.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #33187 (5aa0b2) with base d7c4bd Lines: 2 2 100.0 %
Date: 2026-06-30 12:18:20 Functions: 2 2 100.0 %
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 "DefaultNonlinearConvergence.h"
      13             : #include "ReferenceResidualInterface.h"
      14             : 
      15             : // PETSc includes
      16             : #include <petsc.h>
      17             : #include <petscmat.h>
      18             : 
      19             : #include "libmesh/enum_norm_type.h"
      20             : 
      21             : /**
      22             :  * Uses a reference residual to define relative convergence criteria.
      23             :  */
      24             : class ReferenceResidualConvergence : public DefaultNonlinearConvergence,
      25             :                                      public ReferenceResidualInterface
      26             : {
      27             : public:
      28             :   static InputParameters validParams();
      29             : 
      30             :   ReferenceResidualConvergence(const InputParameters & parameters);
      31             : 
      32             :   /// Computes the reference residuals for each group
      33             :   void updateReferenceResidual();
      34             : 
      35             :   virtual void initialSetup() override;
      36             : 
      37             :   class ReferenceVectorTagIDKey
      38             :   {
      39             :     friend class TaggingInterface;
      40        3285 :     ReferenceVectorTagIDKey() {}
      41             :     ReferenceVectorTagIDKey(const ReferenceVectorTagIDKey &) {}
      42             :   };
      43             : 
      44             :   /// Returns the tag ID associated with the reference vector tag ID key
      45        3285 :   TagID referenceVectorTagID(ReferenceVectorTagIDKey) const { return _reference_vector_tag_id; }
      46             : 
      47             : protected:
      48             :   virtual void nonlinearConvergenceSetup() override;
      49             : 
      50             :   virtual bool checkResidualConvergence(const unsigned int it,
      51             :                                         const Real fnorm,
      52             :                                         const Real ref_norm,
      53             :                                         const Real rel_tol,
      54             :                                         const Real abs_tol,
      55             :                                         std::ostringstream & oss) override;
      56             : 
      57             :   /**
      58             :    * Check the convergence by comparing the norm of each variable's residual separately against
      59             :    * its reference variable's norm. Only consider the solution converged if all
      60             :    * variables are converged individually using either a relative or absolute
      61             :    * criterion.
      62             :    * @param fnorm Function norm (norm of full residual vector)
      63             :    * @param abs_tol Absolute convergence tolerance
      64             :    * @param rel_tol Relative convergence tolerance
      65             :    * @param initial_residual_before_preset_bcs Initial norm of full residual vector
      66             :    *                                           before applying preset bcs
      67             :    * @return true if all variables are converged
      68             :    */
      69             :   bool checkConvergenceIndividVars(const Real fnorm,
      70             :                                    const Real abs_tol,
      71             :                                    const Real rel_tol,
      72             :                                    const Real initial_residual_before_preset_bcs);
      73             : 
      74             :   /// Enum holding the normalization type
      75             :   const MooseEnum _norm_type_enum;
      76             : 
      77             :   ///@{
      78             :   /// List of solution variable names whose reference residuals will be stored,
      79             :   /// and the residual variable names that will store them.
      80             :   std::vector<NonlinearVariableName> _soln_var_names;
      81             :   std::vector<AuxVariableName> _ref_resid_var_names;
      82             :   ///@}
      83             : 
      84             :   ///@{
      85             :   /// List of grouped solution variable names whose reference residuals will be stored
      86             :   std::vector<NonlinearVariableName> _group_names;
      87             :   ///@}
      88             : 
      89             :   ///@{
      90             :   /// Variable numbers associated with the names in _soln_var_names and _ref_resid_var_names.
      91             :   std::vector<unsigned int> _soln_vars;
      92             :   std::vector<unsigned int> _ref_resid_vars;
      93             :   ///@}
      94             : 
      95             :   ///@{
      96             :   /// "Acceptable" absolute and relative tolerance multiplier and
      97             :   /// acceptable number of iterations.  Used when checking the
      98             :   /// convergence of individual variables.
      99             :   const Real _accept_mult;
     100             :   const unsigned int _accept_iters;
     101             :   ///@}
     102             : 
     103             :   ///@{
     104             :   /// Local storage for *discrete L2 residual norms* of the grouped variables.
     105             :   std::vector<Real> _group_ref_resid;
     106             :   std::vector<Real> _group_resid;
     107             :   ///@}
     108             : 
     109             :   /// Vector of bools to signify if variable is in a group.
     110             :   std::vector<bool> _is_var_grouped;
     111             : 
     112             :   /// Group number index for each variable
     113             :   std::vector<unsigned int> _group_index;
     114             : 
     115             :   /// Local storage for the scaling factors applied to each of the variables to apply to _ref_resid_vars.
     116             :   std::vector<Real> _scaling_factors;
     117             : 
     118             :   /// The optional vector storing the reference residual values
     119             :   const NumericVector<Number> * _residual_vector;
     120             : 
     121             :   /// The vector storing the reference residual values
     122             :   const NumericVector<Number> * _reference_vector;
     123             : 
     124             :   /// Flag for each solution variable or group being in 'converge_on'
     125             :   std::vector<bool> _converge_on_var;
     126             :   std::vector<bool> _converge_on_group;
     127             : 
     128             :   /// Container for convergence treatment when the reference residual is zero
     129             :   const enum class ZeroReferenceType { ZERO_TOLERANCE, RELATIVE_TOLERANCE } _zero_ref_type;
     130             : 
     131             :   /// Bool to unscale the residual before convergence checks and screen output
     132             :   const bool _unscale_the_residual;
     133             : 
     134             :   /// Flag to optionally perform normalization of residual by reference residual before or after L2 norm is computed
     135             :   bool _local_norm;
     136             : 
     137             :   /// Container for normalization type
     138             :   libMesh::FEMNormType _norm_type;
     139             : 
     140             :   /// The reference vector tag id
     141             :   TagID _reference_vector_tag_id;
     142             : };

Generated by: LCOV version 1.14