SystemPreconditioner_impl.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_IMPL_HEADER_INCLUDED
20#define OPM_SYSTEMPRECONDITIONER_IMPL_HEADER_INCLUDED
21
23
24#include <dune/istl/operators.hh>
25
26#include <opm/common/ErrorMacros.hpp>
27
29
30namespace Opm {
31
32template <class Scalar, class ResOp, class ResComm>
35 const std::function<SystemVector<Scalar>()>& weightsCalculator,
36 int pressureIndex,
37 const Opm::PropertyTree& prm) requires (!isParallel)
38 : S_(S)
39 , pressureIndex_(pressureIndex)
40{
41 initSubSolvers(prm, weightsCalculator);
42 initWorkVectors();
43}
44
45template <class Scalar, class ResOp, class ResComm>
48 const std::function<SystemVector<Scalar>()>& weightsCalculator,
49 int pressureIndex,
50 const Opm::PropertyTree& prm,
51 const ResComm& resComm) requires (isParallel)
52 : S_(S)
53 , resComm_(&resComm)
54 , pressureIndex_(pressureIndex)
55{
56 initSubSolvers(prm, weightsCalculator);
57 initWorkVectors();
58}
59
60template <class Scalar, class ResOp, class ResComm>
61void
63update()
64{
65 if (cprwStage_) {
66 weights_ = weightsCalculator_();
67 cprwStage_->update(weights_);
68 } else {
69 resSolver_->preconditioner().update();
70 }
71 resSmoother_->preconditioner().update();
72 wellSolver_->preconditioner().update();
73}
74
75template <class Scalar, class ResOp, class ResComm>
76void
79{
80 if (cprwStage_) {
81 // The coarse system carries one unknown per well, so a changed
82 // well structure changes its dimension and pattern.
83 weights_ = weightsCalculator_();
84 cprwStage_->buildStructure(weights_);
85 } else {
86 resSolver_->preconditioner().update();
87 }
88 resSmoother_->preconditioner().update();
89 initWellSolver();
90 resizeWellWorkVectors();
91}
92
93template <class Scalar, class ResOp, class ResComm>
94void
97{
98 // Extract blocks using the agreed convention
99 const auto& A = S_[_0][_0];
100 const auto& C = S_[_0][_1];
101 const auto& B = S_[_1][_0];
102 const auto& D = S_[_1][_1];
103
104 resRes_ = d[_0];
105 wRes_ = d[_1];
106 resSol_ = 0.0;
107 wSol_ = 0.0;
108
109 // Stage 1: pressure solve. Either reservoir-only CPR, or -- with
110 // add_wells -- CPRW on the full system, in which case the stage also
111 // produces a correction for the well unknowns.
112 if (cprwStage_) {
113 dresSol_ = 0.0;
114 dwSol_ = 0.0;
115 tmp_resRes_ = resRes_;
116 syncResVector(tmp_resRes_);
117 cprwStage_->apply(tmp_resRes_, wRes_, weights_, dresSol_, dwSol_);
118 resSol_ += dresSol_;
119 // resRes_ -= A * dresSol_
120 A.mmv(dresSol_, resRes_);
121 // wRes_ -= B * dresSol_
122 B.mmv(dresSol_, wRes_);
123 if (cprwStage_->prolongatesWellPressure()) {
124 wSol_ += dwSol_;
125 // resRes_ -= C * dwSol_ ; wRes_ -= D * dwSol_
126 C.mmv(dwSol_, resRes_);
127 D.mmv(dwSol_, wRes_);
128 }
129 } else {
131 dresSol_ = 0.0;
132 tmp_resRes_ = resRes_;
133 syncResVector(tmp_resRes_);
134 resSolver_->apply(dresSol_, tmp_resRes_, res_result);
135 resSol_ += dresSol_;
136 // resRes_ -= A * dresSol_
137 A.mmv(dresSol_, resRes_);
138 // wRes_ -= B * dresSol_
139 B.mmv(dresSol_, wRes_);
140 }
141
142 // Stage 2: Well solve + reservoir system smoothing
143 {
144 Dune::InverseOperatorResult well_result;
145 dwSol_ = 0.0;
146 tmp_wRes_ = wRes_;
147 wellSolver_->apply(dwSol_, tmp_wRes_, well_result);
148 wSol_ += dwSol_;
149 // resRes_ -= C * dwSol_
150 C.mmv(dwSol_, resRes_);
151 // resRes_ -= D * dwSol_
152 D.mmv(dwSol_, wRes_);
153
155 dresSol_ = 0.0;
156 tmp_resRes_ = resRes_;
157 syncResVector(tmp_resRes_);
158 resSmoother_->apply(dresSol_, tmp_resRes_, res_result);
159 resSol_ += dresSol_;
160 // wRes_ -= B * dresSol_
161 B.mmv(dresSol_, wRes_);
162 }
163
164 // Stage 3: Final well solve
165 {
166 Dune::InverseOperatorResult well_result;
167 dwSol_ = 0.0;
168 tmp_wRes_ = wRes_;
169 wellSolver_->apply(dwSol_, tmp_wRes_, well_result);
170 wSol_ += dwSol_;
171 }
172
173 syncResVector(resSol_);
174 v[_0] = resSol_;
175 v[_1] = wSol_;
176}
177
178template <class Scalar, class ResOp, class ResComm>
179void
182{
183 if constexpr (isParallel) {
184 resComm_->copyOwnerToAll(v, v);
185 }
186}
187
188template <class Scalar, class ResOp, class ResComm>
189void
190SystemPreconditioner<Scalar,ResOp,ResComm>::
191initWellSolver()
192{
193 wop_ = std::make_unique<WellOperator>(S_[_1][_1]);
194 std::function<WellVector<Scalar>()> weightsCalculatorWell;
195 wellSolver_ = std::make_unique<WellFlexibleSolverType>(
196 *wop_, wellprm_, weightsCalculatorWell, dummyWellPressureIndex);
197}
198
199template <class Scalar, class ResOp, class ResComm>
200void
201SystemPreconditioner<Scalar,ResOp,ResComm>::
202initSubSolvers(const Opm::PropertyTree& prm,
203 const std::function<SystemVector<Scalar>()>& weightsCalculator)
204{
205 auto resprm = prm.get_child("reservoir_solver");
206 auto resprmsmoother = prm.get_child("reservoir_smoother");
207 wellprm_ = prm.get_child("well_solver");
208
209 // add_wells is the same switch the classic CPR/CPRW pair uses: it
210 // promotes the pressure stage from reservoir-only CPR to CPRW over the
211 // full (reservoir, well) system.
212 const bool addWells = resprm.get("preconditioner.add_wells", false);
213
214 // The weights arrive from the outer layer for the whole system; the
215 // reservoir-only sub-solvers want just their own part of them.
216 std::function<ResVector<Scalar>()> resWeightCalc;
217 if (weightsCalculator) {
218 resWeightCalc = [weightsCalculator]() {
219 return weightsCalculator()[_0];
220 };
221 }
222
223 if constexpr (isParallel) {
224 rop_ = std::make_unique<ResOp>(S_[_0][_0], *resComm_);
225 resSmoother_ = std::make_unique<ResFlexibleSolverType>(
226 *rop_, *resComm_, resprmsmoother, resWeightCalc, pressureIndex_);
227 } else {
228 rop_ = std::make_unique<ResOp>(S_[_0][_0]);
229 resSmoother_ = std::make_unique<ResFlexibleSolverType>(
230 *rop_, resprmsmoother, resWeightCalc, pressureIndex_);
231 }
232
233 if (addWells) {
234 if (!weightsCalculator) {
235 OPM_THROW(std::invalid_argument,
236 "The CPRW pressure stage (add_wells) needs a weights calculator, but "
237 "none was configured. Set reservoir_solver.preconditioner.weight_type.");
238 }
239 weightsCalculator_ = weightsCalculator;
240 weights_ = weightsCalculator_();
241
242 auto coarseprm = resprm.get_child_optional("preconditioner.coarsesolver")
243 ? resprm.get_child("preconditioner.coarsesolver")
244 : PropertyTree();
245 const auto wellTransfer = wellTransferFromString(
246 // Same default as setupPropertyTree ships, so a JSON that omits
247 // the key gets the same preconditioner as the built-in setup.
248 prm.get("well_transfer", std::string{"classic"}));
249 const auto diagonal = wellCoarseDiagonalFromString(
250 prm.get("well_coarse_diagonal", std::string{"contract_d"}));
251 cprwStage_ = std::make_unique<CprwStage>(S_, coarseprm, pressureIndex_,
252 wellTransfer, resComm_, diagonal,
253 prm.get("verbosity", 0));
254 cprwStage_->buildStructure(weights_);
255 } else {
256 if constexpr (isParallel) {
257 resSolver_ = std::make_unique<ResFlexibleSolverType>(
258 *rop_, *resComm_, resprm, resWeightCalc, pressureIndex_);
259 } else {
260 resSolver_ = std::make_unique<ResFlexibleSolverType>(
261 *rop_, resprm, resWeightCalc, pressureIndex_);
262 }
263 }
264
265 initWellSolver();
266}
267
268template <class Scalar, class ResOp, class ResComm>
269void
270SystemPreconditioner<Scalar,ResOp,ResComm>::
271initWorkVectors()
272{
273 resizeReservoirWorkVectors();
274 resizeWellWorkVectors();
275}
276
277template <class Scalar, class ResOp, class ResComm>
278void
279SystemPreconditioner<Scalar,ResOp,ResComm>::
280resizeReservoirWorkVectors()
281{
282 const auto numRes = S_[_0][_0].N();
283 resSol_.resize(numRes);
284 dresSol_.resize(numRes);
285 tmp_resRes_.resize(numRes);
286 resRes_.resize(numRes);
287}
288
289template <class Scalar, class ResOp, class ResComm>
290void
291SystemPreconditioner<Scalar,ResOp,ResComm>::
292resizeWellWorkVectors()
293{
294 const auto numWell = S_[_1][_1].N();
295 wSol_.resize(numWell);
296 dwSol_.resize(numWell);
297 tmp_wRes_.resize(numWell);
298 wRes_.resize(numWell);
299}
300
301} // namespace Opm
302
303#endif // OPM_SYSTEMPRECONDITIONER_IMPL_HEADER_INCLUDED
Hierarchical collection of key/value pairs.
Definition: PropertyTree.hpp:39
T get(const std::string &key) const
PropertyTree get_child(const std::string &key) const
Definition: SystemTypes.hpp:176
Definition: SystemPreconditioner.hpp:47
void updateForChangedWellStructure()
Definition: SystemPreconditioner_impl.hpp:78
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::InverseOperatorResult InverseOperatorResult
Definition: GpuBridge.hpp:32
WellCoarseDiagonal wellCoarseDiagonalFromString(const std::string &name)
Definition: SystemCprwPressureStage.hpp:102
Dune::BlockVector< Dune::FieldVector< Scalar, numResDofs > > ResVector
Definition: SystemTypes.hpp:61
WellTransfer wellTransferFromString(const std::string &name)
Definition: SystemCprwPressureStage.hpp:112