From 79f5a1a9589a72aa7fee243ec9e0eb64fc071b81 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 14 Jan 2025 07:49:15 +0100 Subject: [PATCH] NonlinearSolver::Parameters: move to separate struct and move some more code to the TU. small tidy up enabled by typetag-less parameters system --- opm/simulators/flow/NonlinearSolver.cpp | 78 ++++++++++++++++++++----- opm/simulators/flow/NonlinearSolver.hpp | 71 ++++++---------------- 2 files changed, 81 insertions(+), 68 deletions(-) diff --git a/opm/simulators/flow/NonlinearSolver.cpp b/opm/simulators/flow/NonlinearSolver.cpp index f592eb584..03915ade2 100644 --- a/opm/simulators/flow/NonlinearSolver.cpp +++ b/opm/simulators/flow/NonlinearSolver.cpp @@ -105,21 +105,6 @@ void stabilizeNonlinearUpdate(BVector& dx, BVector& dxOld, return; } -template -void registerNonlinearParameters() -{ - Parameters::Register> - ("The maximum relaxation factor of a Newton iteration"); - Parameters::Register - ("The maximum number of Newton iterations per time step"); - Parameters::Register - ("The minimum number of Newton iterations per time step"); - Parameters::Register - ("The type of relaxation used by Newton method"); - - Parameters::SetDefault(20); -} - template using BV = Dune::BlockVector>; @@ -131,7 +116,6 @@ using BV = Dune::BlockVector>; template void detectOscillations(const std::vector>&, \ const int, const int, const T, \ const int, bool&, bool&); \ - template void registerNonlinearParameters(); \ INSTANTIATE(T,1) \ INSTANTIATE(T,2) \ INSTANTIATE(T,3) \ @@ -146,3 +130,65 @@ INSTANTIATE_TYPE(float) #endif } // namespace Opm::detail + +namespace Opm { + +template +NonlinearSolverParameters:: +NonlinearSolverParameters() +{ + // set default values + reset(); + + // overload with given parameters + relaxMax_ = Parameters::Get>(); + maxIter_ = Parameters::Get(); + minIter_ = Parameters::Get(); + + const auto& relaxationTypeString = Parameters::Get(); + if (relaxationTypeString == "dampen") { + relaxType_ = NonlinearRelaxType::Dampen; + } else if (relaxationTypeString == "sor") { + relaxType_ = NonlinearRelaxType::SOR; + } else { + OPM_THROW(std::runtime_error, + "Unknown Relaxtion Type " + relaxationTypeString); + } +} + +template +void NonlinearSolverParameters:: +reset() +{ + // default values for the solver parameters + relaxType_ = NonlinearRelaxType::Dampen; + relaxMax_ = 0.5; + relaxIncrement_ = 0.1; + relaxRelTol_ = 0.2; + maxIter_ = 10; + minIter_ = 1; +} + +template +void NonlinearSolverParameters:: +registerParameters() +{ + Parameters::Register> + ("The maximum relaxation factor of a Newton iteration"); + Parameters::Register + ("The maximum number of Newton iterations per time step"); + Parameters::Register + ("The minimum number of Newton iterations per time step"); + Parameters::Register + ("The type of relaxation used by Newton method"); + + Parameters::SetDefault(20); +} + +template struct NonlinearSolverParameters; + +#if FLOW_INSTANTIATE_FLOAT +template struct NonlinearSolverParameters; +#endif + +} diff --git a/opm/simulators/flow/NonlinearSolver.hpp b/opm/simulators/flow/NonlinearSolver.hpp index b05ea0a98..dae4aa1a8 100644 --- a/opm/simulators/flow/NonlinearSolver.hpp +++ b/opm/simulators/flow/NonlinearSolver.hpp @@ -30,7 +30,6 @@ #include #include -#include #include #include @@ -72,11 +71,26 @@ template void stabilizeNonlinearUpdate(BVector& dx, BVector& dxOld, const Scalar omega, NonlinearRelaxType relaxType); -template -void registerNonlinearParameters(); - } +// Solver parameters controlling nonlinear process. +template +struct NonlinearSolverParameters +{ + NonlinearRelaxType relaxType_; + Scalar relaxMax_; + Scalar relaxIncrement_; + Scalar relaxRelTol_; + int maxIter_; // max nonlinear iterations + int minIter_; // min nonlinear iterations + + NonlinearSolverParameters(); + + static void registerParameters(); + + void reset(); +}; + /// A nonlinear solver class suitable for general fully-implicit models, /// as well as pressure, transport and sequential models. template @@ -85,54 +99,7 @@ void registerNonlinearParameters(); using Scalar = GetPropType; public: - // Solver parameters controlling nonlinear process. - struct SolverParameters - { - NonlinearRelaxType relaxType_; - Scalar relaxMax_; - Scalar relaxIncrement_; - Scalar relaxRelTol_; - int maxIter_; // max nonlinear iterations - int minIter_; // min nonlinear iterations - - SolverParameters() - { - // set default values - reset(); - - // overload with given parameters - relaxMax_ = Parameters::Get>(); - maxIter_ = Parameters::Get(); - minIter_ = Parameters::Get(); - - const auto& relaxationTypeString = Parameters::Get(); - if (relaxationTypeString == "dampen") { - relaxType_ = NonlinearRelaxType::Dampen; - } else if (relaxationTypeString == "sor") { - relaxType_ = NonlinearRelaxType::SOR; - } else { - OPM_THROW(std::runtime_error, - "Unknown Relaxtion Type " + relaxationTypeString); - } - } - - static void registerParameters() - { - detail::registerNonlinearParameters(); - } - - void reset() - { - // default values for the solver parameters - relaxType_ = NonlinearRelaxType::Dampen; - relaxMax_ = 0.5; - relaxIncrement_ = 0.1; - relaxRelTol_ = 0.2; - maxIter_ = 10; - minIter_ = 1; - } - - }; + using SolverParameters = NonlinearSolverParameters; // --------- Public methods ---------