LCOV - code coverage report
Current view: top level - include/executioners - FixedPointSolve.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 7 10 70.0 %
Date: 2025-07-17 01:28:37 Functions: 5 7 71.4 %
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 "SolveObject.h"
      13             : 
      14             : // System includes
      15             : #include <string>
      16             : 
      17             : class FixedPointSolve : public SolveObject
      18             : {
      19             : public:
      20             :   FixedPointSolve(Executioner & ex);
      21             : 
      22       53523 :   virtual ~FixedPointSolve() = default;
      23             : 
      24             :   static InputParameters fixedPointDefaultConvergenceParams();
      25             :   static InputParameters validParams();
      26             : 
      27             :   /**
      28             :    * Iteratively solves the FEProblem.
      29             :    * @return True if solver is converged.
      30             :    */
      31             :   virtual bool solve() override;
      32             : 
      33             :   /// Enumeration for fixed point convergence reasons
      34             :   enum class MooseFixedPointConvergenceReason
      35             :   {
      36             :     UNSOLVED = 0,                  /// Not solved yet
      37             :     CONVERGED_NONLINEAR = 1,       /// Main app nonlinear solve converged, FP unassessed
      38             :     CONVERGED_ABS = 2,             /// FP converged by absolute residual tolerance
      39             :     CONVERGED_RELATIVE = 3,        /// FP converged by relative residual tolerance
      40             :     CONVERGED_PP = 4,              /// FP converged by absolute or relative PP tolerance
      41             :     REACH_MAX_ITS = 5,             /// FP converged by hitting max iterations and accepting
      42             :     CONVERGED_OBJECT = 6,          /// FP converged according to Convergence object
      43             :     DIVERGED_MAX_ITS = -1,         /// FP diverged by hitting max iterations
      44             :     DIVERGED_NONLINEAR = -2,       /// Main app nonlinear solve diverged
      45             :     DIVERGED_FAILED_MULTIAPP = -3, /// Multiapp solve diverged
      46             :     DIVERGED_OBJECT = -4           /// FP diverged according to Convergence object
      47             :   };
      48             : 
      49             :   /**
      50             :    * Get the number of fixed point iterations performed
      51             :    * Because this returns the number of fixed point iterations, rather than the current
      52             :    * iteration count (which starts at 0), increment by 1.
      53             :    *
      54             :    * @return Number of fixed point iterations performed
      55             :    */
      56       86852 :   unsigned int numFixedPointIts() const { return _fixed_point_it + 1; }
      57             : 
      58             :   /// Deprecated getter for the number of fixed point iterations
      59             :   unsigned int numPicardIts() const
      60             :   {
      61             :     mooseDeprecated("numPicards() is deprecated. Please use numFixedPointIts() instead.");
      62             : 
      63             :     return _fixed_point_it + 1;
      64             :   }
      65             : 
      66             :   /// Check the solver status
      67             :   MooseFixedPointConvergenceReason checkConvergence() const { return _fixed_point_status; }
      68             : 
      69             :   /// This function checks the _xfem_repeat_step flag set by solve.
      70      232468 :   bool XFEMRepeatStep() const { return _xfem_repeat_step; }
      71             : 
      72             :   /// Set fixed point status
      73       11250 :   void setFixedPointStatus(MooseFixedPointConvergenceReason status)
      74             :   {
      75       11250 :     _fixed_point_status = status;
      76       11250 :   }
      77             : 
      78             :   /// Clear fixed point status
      79             :   void clearFixedPointStatus() { _fixed_point_status = MooseFixedPointConvergenceReason::UNSOLVED; }
      80             : 
      81             :   /// Whether or not this has fixed point iterations
      82             :   bool hasFixedPointIteration() { return _has_fixed_point_its; }
      83             : 
      84             :   /// Set relaxation factor for the current solve as a SubApp
      85             :   void setMultiAppRelaxationFactor(Real factor) { _secondary_relaxation_factor = factor; }
      86             : 
      87             :   /// Set relaxation variables for the current solve as a SubApp
      88             :   void setMultiAppTransformedVariables(const std::vector<std::string> & vars)
      89             :   {
      90             :     _secondary_transformed_variables = vars;
      91             :   }
      92             : 
      93             :   /// Set relaxation postprocessors for the current solve as a SubApp
      94           0 :   virtual void setMultiAppTransformedPostprocessors(const std::vector<PostprocessorName> & pps)
      95             :   {
      96           0 :     _secondary_transformed_pps = pps;
      97           0 :   }
      98             : 
      99             :   /**
     100             :    * Allocate storage for the fixed point algorithm.
     101             :    * This creates the system vector of old (older, pre/post solve) variable values and the
     102             :    * array of old (older, pre/post solve) postprocessor values.
     103             :    *
     104             :    * @param primary Whether this routine is to allocate storage for the primary transformed
     105             :    *                quantities (as main app) or the secondary ones (as a subapp)
     106             :    */
     107             :   virtual void allocateStorage(const bool primary) = 0;
     108             : 
     109             :   /// Whether sub-applications are automatically advanced no matter what happens during their solves
     110             :   bool autoAdvance() const;
     111             : 
     112             :   /// Mark the current solve as failed due to external conditions
     113          96 :   void failStep() { _fail_step = true; }
     114             : 
     115             :   /// Print the convergence history of the coupling, at every fixed point iteration
     116             :   virtual void
     117             :   printFixedPointConvergenceHistory(Real initial_norm,
     118             :                                     const std::vector<Real> & timestep_begin_norms,
     119             :                                     const std::vector<Real> & timestep_end_norms) const = 0;
     120             : 
     121             : protected:
     122             :   /**
     123             :    * Saves the current values of the variables, and update the old(er) vectors.
     124             :    *
     125             :    * @param primary Whether this routine is to save the variables for the primary transformed
     126             :    *                quantities (as main app) or the secondary ones (as a subapp)
     127             :    */
     128             :   virtual void saveVariableValues(const bool primary) = 0;
     129             : 
     130             :   /**
     131             :    * Saves the current values of the postprocessors, and update the old(er) vectors.
     132             :    *
     133             :    * @param primary Whether this routine is to save the variables for the primary transformed
     134             :    *                quantities (as main app) or the secondary ones (as a subapp)
     135             :    */
     136             :   virtual void savePostprocessorValues(const bool primary) = 0;
     137             : 
     138             :   /**
     139             :    * Use the fixed point algorithm transform instead of simply using the Picard update
     140             :    * This routine can be used to alternate Picard iterations and fixed point algorithm
     141             :    * updates based on the values of the variables before and after a solve / a Picard iteration.
     142             :    *
     143             :    * @param primary Whether this routine is used for the primary transformed
     144             :    *                quantities (as main app) or the secondary ones (as a subapp)
     145             :    */
     146             :   virtual bool useFixedPointAlgorithmUpdateInsteadOfPicard(const bool primary) = 0;
     147             : 
     148             :   /**
     149             :    * Perform one fixed point iteration or a full solve.
     150             :    *
     151             :    * @param transformed_dofs DoFs targetted by the fixed point algorithm
     152             :    *
     153             :    * @return True if both nonlinear solve and the execution of multiapps are successful.
     154             :    *
     155             :    * Note: this function also set _xfem_repeat_step flag for XFEM. It tracks _xfem_update_count
     156             :    * state.
     157             :    * FIXME: The proper design will be to let XFEM use Picard iteration to control the execution.
     158             :    */
     159             :   virtual bool solveStep(const std::set<dof_id_type> & transformed_dofs);
     160             : 
     161             :   /// Save both the variable and postprocessor values
     162             :   virtual void saveAllValues(const bool primary);
     163             : 
     164             :   /**
     165             :    * Use the fixed point algorithm to transform the postprocessors.
     166             :    * If this routine is not called, the next value of the postprocessors will just be from
     167             :    * the unrelaxed Picard fixed point algorithm.
     168             :    *
     169             :    * @param primary Whether this routine is to save the variables for the primary transformed
     170             :    *                quantities (as main app) or the secondary ones (as a subapp)
     171             :    */
     172             :   virtual void transformPostprocessors(const bool primary) = 0;
     173             : 
     174             :   /**
     175             :    * Use the fixed point algorithm to transform the variables.
     176             :    * If this routine is not called, the next value of the variables will just be from
     177             :    * the unrelaxed Picard fixed point algorithm.
     178             :    *
     179             :    * @param transformed_dofs The dofs that will be affected by the algorithm
     180             :    * @param primary Whether this routine is to save the variables for the primary transformed
     181             :    *                quantities (as main app) or the secondary ones (as a subapp)
     182             :    */
     183             :   virtual void transformVariables(const std::set<dof_id_type> & transformed_dofs,
     184             :                                   const bool primary) = 0;
     185             : 
     186             :   /// Examine the various convergence metrics
     187             :   bool examineFixedPointConvergence(bool & converged);
     188             : 
     189             :   /// Print information about the fixed point convergence
     190             :   void printFixedPointConvergenceReason();
     191             : 
     192             :   /// Whether or not we activate fixed point iteration
     193             :   const bool _has_fixed_point_its;
     194             : 
     195             :   /// Relaxation factor for fixed point Iteration
     196             :   const Real _relax_factor;
     197             :   /// The variables (transferred or not) that are going to be relaxed
     198             :   std::vector<std::string> _transformed_vars; // TODO: make const once relaxed_variables is removed
     199             :   /// The postprocessors (transferred or not) that are going to be relaxed
     200             :   const std::vector<PostprocessorName> _transformed_pps;
     201             :   /// Previous values of the relaxed postprocessors
     202             :   std::vector<std::vector<PostprocessorValue>> _transformed_pps_values;
     203             : 
     204             :   /// Relaxation factor outside of fixed point iteration (used as a subapp)
     205             :   Real _secondary_relaxation_factor;
     206             :   /// Variables to be relaxed outside of fixed point iteration (used as a subapp)
     207             :   std::vector<std::string> _secondary_transformed_variables;
     208             :   /// Postprocessors to be relaxed outside of fixed point iteration (used as a subapp)
     209             :   std::vector<PostprocessorName> _secondary_transformed_pps;
     210             :   /// Previous values of the postprocessors relaxed outside of the fixed point iteration (used as a subapp)
     211             :   std::vector<std::vector<PostprocessorValue>> _secondary_transformed_pps_values;
     212             : 
     213             :   ///@{ Variables used by the fixed point iteration
     214             :   /// fixed point iteration counter
     215             :   unsigned int _fixed_point_it;
     216             :   /// fixed point iteration counter for the main app
     217             :   unsigned int _main_fixed_point_it;
     218             :   /// Status of fixed point solve
     219             :   MooseFixedPointConvergenceReason _fixed_point_status;
     220             :   ///@}
     221             : private:
     222             :   /// Maximum number of xfem updates per step
     223             :   const unsigned int _max_xfem_update;
     224             :   /// Controls whether xfem should update the mesh at the beginning of the time step
     225             :   const bool _update_xfem_at_timestep_begin;
     226             : 
     227             :   /// Counter for number of xfem updates that have been performed in the current step
     228             :   unsigned int _xfem_update_count;
     229             :   /// Whether step should be repeated due to xfem modifying the mesh
     230             :   bool _xfem_repeat_step;
     231             : 
     232             :   /// Time of previous fixed point solve as a subapp
     233             :   Real _old_entering_time;
     234             : 
     235             :   /// force the current step to fail, triggering are repeat with a cut dt
     236             :   bool _fail_step;
     237             : 
     238             :   /// Whether the user has set the auto_advance parameter for handling advancement of
     239             :   /// sub-applications in multi-app contexts
     240             :   const bool _auto_advance_set_by_user;
     241             : 
     242             :   /// The value of auto_advance set by the user for handling advancement of sub-applications in
     243             :   /// multi-app contexts
     244             :   const bool _auto_advance_user_value;
     245             : };

Generated by: LCOV version 1.14