SystemPreconditioner.hpp
Go to the documentation of this file.
1/*
2 Copyright Equinor ASA 2026
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef OPM_SYSTEMPRECONDITIONER_HEADER_INCLUDED
20#define OPM_SYSTEMPRECONDITIONER_HEADER_INCLUDED
21
27
28#include <dune/istl/paamg/pinfo.hh>
29
30#include <functional>
31#include <memory>
32
33namespace Opm {
34
35// Preconditioner for the coupled reservoir-well system.
36//
37// Templated on scalar type, reservoir operator and communication types to
38// unify sequential and parallel implementations. The 3-stage algorithm:
39// 1. Reservoir CPR solve
40// 2. Well solve + reservoir smoothing
41// 3. Final well solve
42//
43// For parallel runs, copyOwnerToAll synchronises overlap DOFs before
44// each reservoir sub-solve.
45template <class Scalar, class ResOp, class ResComm = Dune::Amg::SequentialInformation>
46class SystemPreconditioner : public Dune::PreconditionerWithUpdate<SystemVector<Scalar>, SystemVector<Scalar>>
47{
48public:
49 static constexpr bool isParallel = !std::is_same_v<ResComm, Dune::Amg::SequentialInformation>;
50
52 using WellOperator = Dune::MatrixAdapter<WWMatrix<Scalar>, WellVector<Scalar>, WellVector<Scalar>>;
54
55 static constexpr auto _0 = Dune::Indices::_0;
56 static constexpr auto _1 = Dune::Indices::_1;
57
58 // Sequential constructor (enabled only for non-parallel specializations).
60 const std::function<SystemVector<Scalar>()>& weightsCalculator,
61 int pressureIndex,
62 const Opm::PropertyTree& prm) requires (!isParallel);
63
64 // Parallel constructor (enabled only for parallel specializations).
66 const std::function<SystemVector<Scalar>()>& weightsCalculator,
67 int pressureIndex,
68 const Opm::PropertyTree& prm,
69 const ResComm& resComm) requires (isParallel);
70
72 {
73 }
74
75 void post(SystemVector<Scalar>&) override
76 {
77 }
78
79 Dune::SolverCategory::Category category() const override
80 {
81 if constexpr (isParallel)
82 return Dune::SolverCategory::overlapping;
83 else
84 return Dune::SolverCategory::sequential;
85 }
86
87 void update() override;
88
90
91 bool hasPerfectUpdate() const override
92 {
93 return true;
94 }
95
96// System matrix block structure:
97//
98// [ A C ] [ x_res ] [ resRes ]
99// S = [ B D ] [ x_well ] = [ wRes ]
100//
101// A = reservoir-reservoir (top-left)
102// C = reservoir-well coupling (top-right)
103// B = well-reservoir coupling (bottom-left)
104// D = well-well (bottom-right)
105 void apply(SystemVector<Scalar>& v, const SystemVector<Scalar>& d) override;
106
107private:
108 const SystemMatrix<Scalar>& S_;
109 const ResComm* resComm_ = nullptr;
110 int pressureIndex_ = 0;
111 static constexpr int dummyWellPressureIndex = std::numeric_limits<int>::min();
112 Opm::PropertyTree wellprm_;
113
114 std::unique_ptr<ResOp> rop_;
115 std::unique_ptr<WellOperator> wop_;
116 std::unique_ptr<ResFlexibleSolverType> resSolver_;
117 std::unique_ptr<ResFlexibleSolverType> resSmoother_;
118 std::unique_ptr<WellFlexibleSolverType> wellSolver_;
119
120 // Non-null when the pressure stage includes the well unknowns (CPRW).
121 // Then resSolver_ is not built and stage 1 goes through cprwStage_.
123 std::unique_ptr<CprwStage> cprwStage_;
124 std::function<SystemVector<Scalar>()> weightsCalculator_;
125 SystemVector<Scalar> weights_;
126
127 WellVector<Scalar> wSol_;
128 ResVector<Scalar> resSol_;
129 ResVector<Scalar> dresSol_;
130 WellVector<Scalar> dwSol_;
131 ResVector<Scalar> tmp_resRes_;
132 WellVector<Scalar> tmp_wRes_;
133 ResVector<Scalar> resRes_;
134 WellVector<Scalar> wRes_;
135
136 void syncResVector(ResVector<Scalar>& v);
137
138 void initWellSolver();
139
140 void initSubSolvers(const Opm::PropertyTree& prm,
141 const std::function<SystemVector<Scalar>()>& weightsCalculator);
142
143 void initWorkVectors();
144
145 void resizeReservoirWorkVectors();
146
147 void resizeWellWorkVectors();
148};
149
150} // namespace Opm
151
152#endif // OPM_SYSTEMPRECONDITIONER_HEADER_INCLUDED
Definition: FlexibleSolver.hpp:45
Interface class adding the update() method to the preconditioner interface.
Definition: PreconditionerWithUpdate.hpp:34
Hierarchical collection of key/value pairs.
Definition: PropertyTree.hpp:39
Definition: SystemCprwPressureStage.hpp:131
Definition: SystemTypes.hpp:176
Definition: SystemPreconditioner.hpp:47
bool hasPerfectUpdate() const override
Definition: SystemPreconditioner.hpp:91
void post(SystemVector< Scalar > &) override
Definition: SystemPreconditioner.hpp:75
void pre(SystemVector< Scalar > &, SystemVector< Scalar > &) override
Definition: SystemPreconditioner.hpp:71
Dune::SolverCategory::Category category() const override
Definition: SystemPreconditioner.hpp:79
void updateForChangedWellStructure()
Definition: SystemPreconditioner_impl.hpp:78
static constexpr auto _0
Definition: SystemPreconditioner.hpp:55
static constexpr auto _1
Definition: SystemPreconditioner.hpp:56
Dune::MatrixAdapter< WWMatrix< Scalar >, WellVector< Scalar >, WellVector< Scalar > > WellOperator
Definition: SystemPreconditioner.hpp:52
static constexpr bool isParallel
Definition: SystemPreconditioner.hpp:49
SystemPreconditioner(const SystemMatrix< Scalar > &S, const std::function< SystemVector< Scalar >()> &weightsCalculator, int pressureIndex, const Opm::PropertyTree &prm)
Definition: SystemPreconditioner_impl.hpp:34
void update() override
Definition: SystemPreconditioner_impl.hpp:63
void apply(SystemVector< Scalar > &v, const SystemVector< Scalar > &d) override
Definition: SystemPreconditioner_impl.hpp:96
Definition: blackoilbioeffectsmodules.hh:45
Dune::MultiTypeBlockVector< ResVector< Scalar >, WellVector< Scalar > > SystemVector
Definition: SystemTypes.hpp:65
Dune::BlockVector< Dune::FieldVector< Scalar, numResDofs > > ResVector
Definition: SystemTypes.hpp:61
Dune::BlockVector< Dune::FieldVector< Scalar, numWellDofs > > WellVector
Definition: SystemTypes.hpp:63