From 40af5abf756ece0f33565cc7c7a2004c84171058 Mon Sep 17 00:00:00 2001 From: cgilet Date: Thu, 13 Jul 2023 21:00:31 -0400 Subject: [PATCH 1/4] Add the option to throw an exception on MLMG failure rather than aborting. --- .../source/LinearSolvers.rst | 32 ++++++++++++++ Src/LinearSolvers/MLMG/AMReX_MLMG.H | 42 ++++++++++++++----- 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/Docs/sphinx_documentation/source/LinearSolvers.rst b/Docs/sphinx_documentation/source/LinearSolvers.rst index 63694474e99..87048bc8195 100644 --- a/Docs/sphinx_documentation/source/LinearSolvers.rst +++ b/Docs/sphinx_documentation/source/LinearSolvers.rst @@ -372,6 +372,38 @@ Available choices are :cpp:`LPInfo::setConsolidationStrategy(int)`, to give control over how this process works. + +:cpp:`MLMG::setThrowException(bool)` controls whether multigrid failure results +in aborting (default) or throwing an exception, whereby control will return to the calling +application. The application code must catch the exception: + +.. highlight:: c++ + +:: + + try { + mlmg.solve(...); + } catch (const MLMG::error& e) { + Print()< friend class MLCGSolverT; using FAB = typename MF::fab_type; @@ -104,6 +111,7 @@ public: */ void apply (const Vector& out, const Vector& in); + void setThrowException (bool t) noexcept { throw_exception = t; } void setVerbose (int v) noexcept { verbose = v; } void setMaxIter (int n) noexcept { max_iters = n; } void setMaxFmgIter (int n) noexcept { max_fmg_iters = n; } @@ -211,7 +219,9 @@ public: private: + bool throw_exception = false; int verbose = 1; + int max_iters = 200; int do_fixed_number_of_iters = 0; @@ -453,16 +463,21 @@ MLMGT::solve (const Vector& a_sol, const Vector& a_rhs, } break; } else { - if (composite_norminf > RT(1.e20)*max_norm) - { - if (verbose > 0) { - amrex::Print() << "MLMG: Failing to converge after " << iter+1 << " iterations." - << " resid, resid/" << norm_name << " = " - << composite_norminf << ", " - << composite_norminf/max_norm << "\n"; - } - amrex::Abort("MLMG failing so lets stop here"); - } + if (composite_norminf > RT(1.e20)*max_norm) + { + if (verbose > 0) { + amrex::Print() << "MLMG: Failing to converge after " << iter+1 << " iterations." + << " resid, resid/" << norm_name << " = " + << composite_norminf << ", " + << composite_norminf/max_norm << "\n"; + } + + if ( throw_exception ) { + throw error(std::string("MLMG blew up.")); + } else { + amrex::Abort("MLMG failing so lets stop here"); + } + } } } @@ -473,7 +488,12 @@ MLMGT::solve (const Vector& a_sol, const Vector& a_rhs, << composite_norminf << ", " << composite_norminf/max_norm << "\n"; } - amrex::Abort("MLMG failed"); + + if ( throw_exception ) { + throw error(std::string("MLMG failed to converge.")); + } else { + amrex::Abort("MLMG failed."); + } } timer[iter_time] = amrex::second() - iter_start_time; } From 8fe1b607859acc861c31163edcb0157db53821c0 Mon Sep 17 00:00:00 2001 From: Candace Gilet Date: Fri, 14 Jul 2023 12:49:12 -0400 Subject: [PATCH 2/4] Update Src/LinearSolvers/MLMG/AMReX_MLMG.H Co-authored-by: Weiqun Zhang --- Src/LinearSolvers/MLMG/AMReX_MLMG.H | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/LinearSolvers/MLMG/AMReX_MLMG.H b/Src/LinearSolvers/MLMG/AMReX_MLMG.H index 99bef196953..e046dc09a0f 100644 --- a/Src/LinearSolvers/MLMG/AMReX_MLMG.H +++ b/Src/LinearSolvers/MLMG/AMReX_MLMG.H @@ -16,7 +16,7 @@ public: : public std::runtime_error { public : - error(const std::string& what) : std::runtime_error(what) {} + using std::runtime_error::runtime_error; }; template friend class MLCGSolverT; From caf6aa66a96da61dc3e4157e003a644acbeab2df Mon Sep 17 00:00:00 2001 From: Candace Gilet Date: Fri, 14 Jul 2023 12:49:23 -0400 Subject: [PATCH 3/4] Update Src/LinearSolvers/MLMG/AMReX_MLMG.H Co-authored-by: Weiqun Zhang --- Src/LinearSolvers/MLMG/AMReX_MLMG.H | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/LinearSolvers/MLMG/AMReX_MLMG.H b/Src/LinearSolvers/MLMG/AMReX_MLMG.H index e046dc09a0f..1ccba436cf1 100644 --- a/Src/LinearSolvers/MLMG/AMReX_MLMG.H +++ b/Src/LinearSolvers/MLMG/AMReX_MLMG.H @@ -473,7 +473,7 @@ MLMGT::solve (const Vector& a_sol, const Vector& a_rhs, } if ( throw_exception ) { - throw error(std::string("MLMG blew up.")); + throw error("MLMG blew up."); } else { amrex::Abort("MLMG failing so lets stop here"); } From 448a8d6c263a1051ea26c40bdd654a7808e0ad88 Mon Sep 17 00:00:00 2001 From: Candace Gilet Date: Fri, 14 Jul 2023 12:49:34 -0400 Subject: [PATCH 4/4] Update Src/LinearSolvers/MLMG/AMReX_MLMG.H Co-authored-by: Weiqun Zhang --- Src/LinearSolvers/MLMG/AMReX_MLMG.H | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/LinearSolvers/MLMG/AMReX_MLMG.H b/Src/LinearSolvers/MLMG/AMReX_MLMG.H index 1ccba436cf1..ccc54128f7c 100644 --- a/Src/LinearSolvers/MLMG/AMReX_MLMG.H +++ b/Src/LinearSolvers/MLMG/AMReX_MLMG.H @@ -490,7 +490,7 @@ MLMGT::solve (const Vector& a_sol, const Vector& a_rhs, } if ( throw_exception ) { - throw error(std::string("MLMG failed to converge.")); + throw error("MLMG failed to converge."); } else { amrex::Abort("MLMG failed."); }