17 #ifndef OPM_ISTLSOLVERRUNTIMEOPTIONPROXY_HEADER_INCLUDED 18 #define OPM_ISTLSOLVERRUNTIMEOPTIONPROXY_HEADER_INCLUDED 20 #include "opm/simulators/linalg/FlowLinearSolverParameters.hpp" 21 #include <opm/simulators/linalg/setupPropertyTree.hpp> 22 #include <opm/simulators/linalg/AbstractISTLSolver.hpp> 23 #include <opm/simulators/linalg/ISTLSolver.hpp> 25 #if COMPILE_GPU_BRIDGE 26 #include <opm/simulators/linalg/ISTLSolverGpuBridge.hpp> 30 #include <opm/simulators/linalg/gpuistl_hip/ISTLSolverGPUISTL.hpp> 32 #include <opm/simulators/linalg/gpuistl/ISTLSolverGPUISTL.hpp> 35 #include <opm/simulators/linalg/system/ISTLSolverSystem.hpp> 38 #include <fmt/format.h> 46 template <
class TypeTag>
47 class ISTLSolverRuntimeOptionProxy :
public AbstractISTLSolver<GetPropType<TypeTag, Properties::SparseMatrixAdapter>,
48 GetPropType<TypeTag, Properties::GlobalEqVector>>
51 using SparseMatrixAdapter = GetPropType<TypeTag, Properties::SparseMatrixAdapter>;
52 using Vector = GetPropType<TypeTag, Properties::GlobalEqVector>;
53 using Simulator = GetPropType<TypeTag, Properties::Simulator>;
54 using Matrix =
typename SparseMatrixAdapter::IstlMatrix;
57 using CommunicationType = Dune::OwnerOverlapCopyCommunication<int, int>;
59 using CommunicationType = Dune::Communication<int>;
63 static void registerParameters()
65 FlowLinearSolverParameters::registerParameters();
77 bool forceSerial =
false)
79 createSolver(simulator, parameters, forceSerial);
86 createSolver(simulator);
92 istlSolver_->eraseMatrix();
97 istlSolver_->setActiveSolver(num);
102 return istlSolver_->numAvailableSolvers();
105 void prepare(
const SparseMatrixAdapter& M, Vector& b)
override 107 istlSolver_->prepare(M, b);
110 void prepare(
const Matrix& M, Vector& b)
override 112 istlSolver_->prepare(M, b);
115 void setResidual(Vector& b)
override 117 istlSolver_->setResidual(b);
120 void getResidual(Vector& b)
const override 122 istlSolver_->getResidual(b);
125 void setMatrix(
const SparseMatrixAdapter& M)
override 127 istlSolver_->setMatrix(M);
130 bool solve(Vector& x)
override 132 return istlSolver_->solve(x);
137 return istlSolver_->iterations();
140 const CommunicationType*
comm()
const override 142 return istlSolver_->comm();
147 return istlSolver_->getSolveCount();
151 std::unique_ptr<AbstractISTLSolver<SparseMatrixAdapter, Vector>> istlSolver_;
154 template <
class... Args>
155 void createSolver(
const Simulator& simulator, Args&&... args)
157 auto linSolverConf = Parameters::Get<Parameters::LinearSolver>();
158 bool useSystemCpr = (linSolverConf ==
"system_cpr");
159 if (!useSystemCpr && linSolverConf.size() > 5
160 && linSolverConf.ends_with(
".json")
161 && std::filesystem::exists(linSolverConf)) {
164 useSystemCpr = (prm.get<std::string>(
"preconditioner.type",
"") ==
"system_cpr");
168 using Indices = GetPropType<TypeTag, Properties::Indices>;
169 const auto backend = Parameters::linearSolverAcceleratorTypeFromCLI();
170 if (backend != Parameters::LinearSolverAcceleratorType::CPU) {
171 OPM_THROW(std::invalid_argument,
172 "The system_cpr preconditioner currently only " 173 "supports --linear-solver-accelerator=cpu");
176 if constexpr (Indices::numEq == 3) {
177 istlSolver_ = std::make_unique<ISTLSolverSystem<TypeTag>>(
178 simulator, std::forward<Args>(args)...);
180 OPM_THROW(std::invalid_argument,
181 "The system_cpr preconditioner is only supported for " 182 "standard 3-phase blackoil (3 equations). This model has " +
183 std::to_string(Indices::numEq) +
" equations.");
185 checkSystemCPRMatrixAddWell(Parameters::Get<Parameters::MatrixAddWellContributions>());
189 const auto backend = Parameters::linearSolverAcceleratorTypeFromCLI();
190 if (backend == Parameters::LinearSolverAcceleratorType::CPU) {
192 #if COMPILE_GPU_BRIDGE 193 istlSolver_ = std::make_unique<ISTLSolverGpuBridge<TypeTag>>(simulator, std::forward<Args>(args)...);
195 istlSolver_ = std::make_unique<ISTLSolver<TypeTag>>(simulator, std::forward<Args>(args)...);
199 else if (backend == Parameters::LinearSolverAcceleratorType::GPU) {
200 istlSolver_ = std::make_unique<gpuistl::ISTLSolverGPUISTL<TypeTag>>(simulator, std::forward<Args>(args)...);
206 OPM_THROW(std::invalid_argument, fmt::format(fmt::runtime(
"Unknown backend: {}"), Parameters::toString(backend)));
ISTLSolverRuntimeOptionProxy(const Simulator &simulator, const FlowLinearSolverParameters ¶meters, bool forceSerial=false)
Construct a system solver.
Definition: ISTLSolverRuntimeOptionProxy.hpp:75
Structs needed for tpfalinearizer and its gpuparams struct extracted to be defined in one place that ...
Definition: blackoilbioeffectsmodules.hh:45
ISTLSolverRuntimeOptionProxy(const Simulator &simulator)
Construct a system solver.
Definition: ISTLSolverRuntimeOptionProxy.hpp:84
const CommunicationType * comm() const override
Get the communication object used by the solver.
Definition: ISTLSolverRuntimeOptionProxy.hpp:140
The Opm property system, traits with inheritance.
void eraseMatrix() override
Signals that the memory for the matrix internally in the solver could be erased.
Definition: ISTLSolverRuntimeOptionProxy.hpp:90
This class carries all parameters for the NewtonIterationBlackoilInterleaved class.
Definition: FlowLinearSolverParameters.hpp:97
int numAvailableSolvers() const override
Get the number of available solvers.
Definition: ISTLSolverRuntimeOptionProxy.hpp:100
Manages the initializing and running of time dependent problems.
Definition: simulator.hh:83
Hierarchical collection of key/value pairs.
Definition: PropertyTree.hpp:38
int getSolveCount() const override
Get the count of how many times the solver has been called.
Definition: ISTLSolverRuntimeOptionProxy.hpp:145
int iterations() const override
Get the number of iterations used in the last solve.
Definition: ISTLSolverRuntimeOptionProxy.hpp:135
void setActiveSolver(int num) override
Set the active solver by its index.
Definition: ISTLSolverRuntimeOptionProxy.hpp:95