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