LCOV - code coverage report
Current view: top level - include/convergence - ReferenceResidualConvergence.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #33416 (b10b36) with base 9fbd27 Lines: 2 2 100.0 %
Date: 2026-07-23 16:15:30 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        4503 :     ReferenceVectorTagIDKey() {}
      41             :     ReferenceVectorTagIDKey(const ReferenceVectorTagIDKey &) {}
      42             :   };
      43             : 
      44             :   /// Returns the tag ID associated with the reference vector tag ID key
      45        4503 :   TagID referenceVectorTagID(ReferenceVectorTagIDKey) const { return _reference_vector_tag_id; }
      46             : 
      47             : protected:
      48             :   virtual NonlinearSystemBase & nonlinearSystem() override;
      49             : 
      50             :   virtual void nonlinearConvergenceSetup() override;
      51             : 
      52             :   virtual bool checkResidualConvergence(const unsigned int n_iter,
      53             :                                         const Real fnorm,
      54             :                                         const Real ref_norm,
      55             :                                         const Real rel_tol,
      56             :                                         const Real abs_tol,
      57             :                                         std::ostringstream & oss) override;
      58             : 
      59             :   /**
      60             :    * Check the convergence by comparing the norm of each variable's residual separately against
      61             :    * its reference variable's norm. Only consider the solution converged if all
      62             :    * variables are converged individually using either a relative or absolute
      63             :    * criterion.
      64             :    * @param fnorm Function norm (norm of full residual vector)
      65             :    * @param abs_tol Absolute convergence tolerance
      66             :    * @param rel_tol Relative convergence tolerance
      67             :    * @param initial_residual_before_preset_bcs Initial norm of full residual vector
      68             :    *                                           before applying preset bcs
      69             :    * @return true if all variables are converged
      70             :    */
      71             :   bool checkConvergenceIndividVars(const Real fnorm,
      72             :                                    const Real abs_tol,
      73             :                                    const Real rel_tol,
      74             :                                    const Real initial_residual_before_preset_bcs);
      75             : 
      76             :   /// Enum holding the normalization type
      77             :   const MooseEnum _norm_type_enum;
      78             : 
      79             :   ///@{
      80             :   /// List of solution variable names whose reference residuals will be stored,
      81             :   /// and the residual variable names that will store them.
      82             :   std::vector<NonlinearVariableName> _soln_var_names;
      83             :   std::vector<AuxVariableName> _ref_resid_var_names;
      84             :   ///@}
      85             : 
      86             :   ///@{
      87             :   /// List of grouped solution variable names whose reference residuals will be stored
      88             :   std::vector<NonlinearVariableName> _group_names;
      89             :   ///@}
      90             : 
      91             :   ///@{
      92             :   /// Variable numbers associated with the names in _soln_var_names and _ref_resid_var_names.
      93             :   std::vector<unsigned int> _soln_vars;
      94             :   std::vector<unsigned int> _ref_resid_vars;
      95             :   ///@}
      96             : 
      97             :   ///@{
      98             :   /// "Acceptable" absolute and relative tolerance multiplier and
      99             :   /// acceptable number of iterations.  Used when checking the
     100             :   /// convergence of individual variables.
     101             :   const Real _accept_mult;
     102             :   const unsigned int _accept_iters;
     103             :   ///@}
     104             : 
     105             :   /// Nonlinear system to which this convergence object applies.
     106             :   const unsigned int _nl_sys_num;
     107             : 
     108             :   ///@{
     109             :   /// Local storage for *discrete L2 residual norms* of the grouped variables.
     110             :   std::vector<Real> _group_ref_resid;
     111             :   std::vector<Real> _group_resid;
     112             :   ///@}
     113             : 
     114             :   /// Vector of bools to signify if variable is in a group.
     115             :   std::vector<bool> _is_var_grouped;
     116             : 
     117             :   /// Group number index for each variable
     118             :   std::vector<unsigned int> _group_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 optional vector storing the reference residual values
     124             :   const NumericVector<Number> * _residual_vector;
     125             : 
     126             :   /// The vector storing the reference residual values
     127             :   const NumericVector<Number> * _reference_vector;
     128             : 
     129             :   /// Flag for each solution variable or group being in 'converge_on'
     130             :   std::vector<bool> _converge_on_var;
     131             :   std::vector<bool> _converge_on_group;
     132             : 
     133             :   /// Container for convergence treatment when the reference residual is zero
     134             :   const enum class ZeroReferenceType { ZERO_TOLERANCE, RELATIVE_TOLERANCE } _zero_ref_type;
     135             : 
     136             :   /// Bool to unscale the residual before convergence checks and screen output
     137             :   const bool _unscale_the_residual;
     138             : 
     139             :   /// Flag to optionally perform normalization of residual by reference residual before or after L2 norm is computed
     140             :   bool _local_norm;
     141             : 
     142             :   /// Container for normalization type
     143             :   libMesh::FEMNormType _norm_type;
     144             : 
     145             :   /// The reference vector tag id
     146             :   TagID _reference_vector_tag_id;
     147             : };

Generated by: LCOV version 1.14