LCOV - code coverage report
Current view: top level - include/convergence - ReferenceResidualConvergence.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 2 2 100.0 %
Date: 2025-07-17 01:28:37 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             :   enum class NormalizationType
      38             :   {
      39             :     GLOBAL_L2 = 0,
      40             :     LOCAL_L2 = 1,
      41             :     GLOBAL_LINF = 2,
      42             :     LOCAL_LINF = 3
      43             :   };
      44             : 
      45             :   class ReferenceVectorTagIDKey
      46             :   {
      47             :     friend class TaggingInterface;
      48        2372 :     ReferenceVectorTagIDKey() {}
      49             :     ReferenceVectorTagIDKey(const ReferenceVectorTagIDKey &) {}
      50             :   };
      51             : 
      52             :   /// Returns the tag ID associated with the reference vector tag ID key
      53        2372 :   TagID referenceVectorTagID(ReferenceVectorTagIDKey) const { return _reference_vector_tag_id; }
      54             : 
      55             : protected:
      56             :   virtual void nonlinearConvergenceSetup() override;
      57             : 
      58             :   virtual bool checkRelativeConvergence(const unsigned int it,
      59             :                                         const Real fnorm,
      60             :                                         const Real the_residual,
      61             :                                         const Real rtol,
      62             :                                         const Real abstol,
      63             :                                         std::ostringstream & oss) override;
      64             : 
      65             :   /**
      66             :    * Check the convergence by comparing the norm of each variable's residual separately against
      67             :    * its reference variable's norm. Only consider the solution converged if all
      68             :    * variables are converged individually using either a relative or absolute
      69             :    * criterion.
      70             :    * @param fnorm Function norm (norm of full residual vector)
      71             :    * @param abstol Absolute convergence tolerance
      72             :    * @param rtol Relative convergence tolerance
      73             :    * @param initial_residual_before_preset_bcs Initial norm of full residual vector
      74             :    *                                           before applying preset bcs
      75             :    * @return true if all variables are converged
      76             :    */
      77             :   bool checkConvergenceIndividVars(const Real fnorm,
      78             :                                    const Real abstol,
      79             :                                    const Real rtol,
      80             :                                    const Real initial_residual_before_preset_bcs);
      81             : 
      82             :   ///@{
      83             :   /// List of solution variable names whose reference residuals will be stored,
      84             :   /// and the residual variable names that will store them.
      85             :   std::vector<NonlinearVariableName> _soln_var_names;
      86             :   std::vector<AuxVariableName> _ref_resid_var_names;
      87             :   ///@}
      88             : 
      89             :   ///@{
      90             :   /// List of grouped solution variable names whose reference residuals will be stored,
      91             :   /// and the residual variable names that will store them.
      92             :   std::vector<NonlinearVariableName> _group_soln_var_names;
      93             :   std::vector<AuxVariableName> _group_ref_resid_var_names;
      94             :   ///@}
      95             : 
      96             :   ///@{
      97             :   /// Variable numbers associated with the names in _soln_var_names and _ref_resid_var_names.
      98             :   std::vector<unsigned int> _soln_vars;
      99             :   std::vector<unsigned int> _ref_resid_vars;
     100             :   ///@}
     101             : 
     102             :   ///@{
     103             :   /// "Acceptable" absolute and relative tolerance multiplier and
     104             :   /// acceptable number of iterations.  Used when checking the
     105             :   /// convergence of individual variables.
     106             :   Real _accept_mult;
     107             :   unsigned int _accept_iters;
     108             :   ///@}
     109             : 
     110             :   ///@{
     111             :   /// Local storage for *discrete L2 residual norms* of the grouped variables listed in _group_ref_resid_var_names.
     112             :   std::vector<Real> _group_ref_resid;
     113             :   std::vector<Real> _group_resid;
     114             :   std::vector<Real> _group_output_resid;
     115             :   ///@}
     116             : 
     117             :   /// Group number index for each variable
     118             :   std::vector<unsigned int> _variable_group_num_index;
     119             : 
     120             :   /// Local storage for the scaling factors applied to each of the variables to apply to _ref_resid_vars.
     121             :   std::vector<Real> _scaling_factors;
     122             : 
     123             :   /// The vector storing the reference residual values
     124             :   const NumericVector<Number> * _reference_vector;
     125             : 
     126             :   /// Variables to use for individual variable convergence checks
     127             :   std::vector<NonlinearVariableName> _converge_on;
     128             :   /// Flag for each solution variable being in 'converge_on'
     129             :   std::vector<bool> _converge_on_var;
     130             : 
     131             :   /// Container for convergence treatment when the reference residual is zero
     132             :   const enum class ZeroReferenceType { ZERO_TOLERANCE, RELATIVE_TOLERANCE } _zero_ref_type;
     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             : 
     143             :   bool _initialized;
     144             : };

Generated by: LCOV version 1.14