19#ifndef OPM_ISTLSOLVERSYSTEM_HEADER_INCLUDED
20#define OPM_ISTLSOLVERSYSTEM_HEADER_INCLUDED
33template <
class TypeTag>
40 using Matrix =
typename SparseMatrixAdapter::IstlMatrix;
47 static_assert(Indices::numEq == 3,
48 "ISTLSolverSystem (with system_cpr preconditioner) only supports "
49 "3-equation blackoil models. This model has different equation count.");
52 = Indices::pressureSwitchIdx;
64 static constexpr auto _0 = Dune::Indices::_0;
65 static constexpr auto _1 = Dune::Indices::_1;
70 bool forceSerial =
false)
71 :
Parent(simulator, parameters, forceSerial)
82 OPM_TIMEBLOCK(istlSolverPrepare);
84 prepareSystemSolver();
89 OPM_TIMEBLOCK(istlSolverPrepare);
91 prepareSystemSolver();
96 OPM_TIMEBLOCK(istlSolverSolve);
102 sysX_[
_0].resize(numRes);
104 sysX_[
_1].resize(numWell);
107 sysRhs_[
_0].resize(numRes);
109 sysRhs_[
_1].resize(numWell);
113 sysSolver_->apply(sysX_, sysRhs_, result);
122 bool sysInitialized_ =
false;
126 std::vector<WRMatrix<Scalar>> wellBMatrices_;
127 std::vector<RWMatrix<Scalar>> wellCMatrices_;
128 std::vector<WWMatrix<Scalar>> wellDMatrices_;
141 std::unique_ptr<SystemSeqOp<Scalar>> sysOp_;
142 std::unique_ptr<Dune::FlexibleSolver<SystemSeqOp<Scalar>>> sysFlexSolverSeq_;
147 std::unique_ptr<WellComm> wellComm_;
148 std::unique_ptr<SystemComm> systemComm_;
149 std::unique_ptr<SystemParOp<Scalar>> sysOpPar_;
150 std::unique_ptr<Dune::FlexibleSolver<SystemParOp<Scalar>>> sysFlexSolverPar_;
159 SysSolverType* sysSolver_ =
nullptr;
160 SysPrecondType* sysPrecond_ =
nullptr;
162 void prepareSystemSolver()
164 OPM_TIMEBLOCK(flexibleSolverPrepare);
166 wellBMatrices_.clear();
167 wellCMatrices_.clear();
168 wellDMatrices_.clear();
171 this->
simulator_.problem().wellModel().addBCDMatrix(
172 wellBMatrices_, wellCMatrices_, wellDMatrices_, wellCells_);
175 Parent::matrix_->N(), wellBMatrices_, wellCMatrices_, wellDMatrices_, wellCells_);
177 const bool localStructureChanged = !sysInitialized_
178 || !merger.hasSameStructure(cachedWellStructure_);
184 const bool globalStructureChanged = this->
comm_->communicator().max(
185 static_cast<int>(localStructureChanged)) > 0;
187 const bool globalStructureChanged = localStructureChanged;
189 const bool needStructureRefresh = !sysInitialized_ || globalStructureChanged;
193 if (needStructureRefresh) {
194 OPM_TIMEBLOCK(flexibleSolverCreate);
195 merger.buildMatrices(mergedB_, mergedC_, mergedD_);
197 sysMatrix_.
B = &mergedB_;
198 sysMatrix_.
C = &mergedC_;
199 sysMatrix_.
D = &mergedD_;
200 cachedWellStructure_ = merger.buildStructure();
202 refreshSystemSolverForChangedWellStructure(prm);
203 sysInitialized_ =
true;
205 OPM_TIMEBLOCK(flexibleSolverUpdate);
208 merger.updateValues(mergedB_, mergedC_, mergedD_);
212 sysMatrix_.
B = &mergedB_;
213 sysMatrix_.
C = &mergedC_;
214 sysMatrix_.
D = &mergedD_;
215 sysPrecond_->update();
221 if (!sysInitialized_ || !sysPrecond_) {
222 createSystemSolver(prm);
227 if (this->
comm_->communicator().size() > 1) {
228 if (
auto* precond =
dynamic_cast<ParSysPrecondType*
>(sysPrecond_)) {
229 precond->updateForChangedWellStructure();
232 createSystemSolver(prm);
238 if (
auto* precond =
dynamic_cast<SeqSysPrecondType*
>(sysPrecond_)) {
239 precond->updateForChangedWellStructure();
242 createSystemSolver(prm);
249 auto resSolverPrm = prm.
get_child(
"preconditioner.reservoir_solver");
250 std::function<ResVector<Scalar>()> resWeightCalc
253 std::function<SystemVector<Scalar>()> sysWeightCalc;
255 sysWeightCalc = [resWeightCalc]() {
256 SystemVector<Scalar> w;
257 w[
_0] = resWeightCalc();
263 const bool is_parallel = this->
comm_->communicator().size() > 1;
265 wellComm_ = std::make_unique<WellComm>();
266 systemComm_ = std::make_unique<SystemComm>(*(this->
comm_), *wellComm_);
268 sysOpPar_ = std::make_unique<SystemParOp<Scalar>>(sysMatrix_, *systemComm_);
270 sysFlexSolverPar_ = std::make_unique<Dune::FlexibleSolver<SystemParOp<Scalar>>>(
271 *sysOpPar_, *systemComm_, prm, sysWeightCalc,
pressureIndex);
273 sysSolver_ = sysFlexSolverPar_.get();
274 sysPrecond_ = &sysFlexSolverPar_->preconditioner();
279 sysOp_ = std::make_unique<SystemSeqOp<Scalar>>(sysMatrix_);
281 sysFlexSolverSeq_ = std::make_unique<Dune::FlexibleSolver<SystemSeqOp<Scalar>>>(
284 sysSolver_ = sysFlexSolverSeq_.get();
285 sysPrecond_ = &sysFlexSolverSeq_->preconditioner();
Definition: MultiComm.hpp:75
Interface class adding the update() method to the preconditioner interface.
Definition: PreconditionerWithUpdate.hpp:34
Definition: ISTLSolver.hpp:152
std::shared_ptr< CommunicationType > comm_
Definition: ISTLSolver.hpp:686
typename SparseMatrixAdapter::IstlMatrix Matrix
Definition: ISTLSolver.hpp:161
int solveCount_
Definition: ISTLSolver.hpp:666
Matrix & getMatrix()
Definition: ISTLSolver.hpp:654
GetPropType< TypeTag, Properties::SparseMatrixAdapter > SparseMatrixAdapter
Definition: ISTLSolver.hpp:156
Dune::OwnerOverlapCopyCommunication< int, int > CommunicationType
Definition: ISTLSolver.hpp:177
Matrix * matrix_
Definition: ISTLSolver.hpp:670
bool checkConvergence(const Dune::InverseOperatorResult &result) const
Definition: ISTLSolver.hpp:484
GetPropType< TypeTag, Properties::GlobalEqVector > Vector
Definition: ISTLSolver.hpp:157
void initPrepare(const Matrix &M, Vector &b)
Definition: ISTLSolver.hpp:348
std::function< Vector()> getWeightsCalculator(const PropertyTree &prm, const Matrix &matrix, std::size_t pressIndex) const
Definition: ISTLSolver.hpp:587
int iterations_
Definition: ISTLSolver.hpp:665
GetPropType< TypeTag, Properties::Simulator > Simulator
Definition: ISTLSolver.hpp:160
Vector * rhs_
Definition: ISTLSolver.hpp:671
int activeSolverNum_
Definition: ISTLSolver.hpp:673
GetPropType< TypeTag, Properties::Indices > Indices
Definition: ISTLSolver.hpp:158
const Simulator & simulator_
Definition: ISTLSolver.hpp:664
std::vector< PropertyTree > prm_
Definition: ISTLSolver.hpp:684
Definition: ISTLSolverSystem.hpp:35
void prepare(const SparseMatrixAdapter &M, Vector &b) override
Definition: ISTLSolverSystem.hpp:80
@ enablePolymerMolarWeight
Definition: ISTLSolverSystem.hpp:54
static constexpr auto _1
Definition: ISTLSolverSystem.hpp:65
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: ISTLSolverSystem.hpp:37
static constexpr std::size_t pressureIndex
Definition: ISTLSolverSystem.hpp:52
ISTLSolverSystem(const Simulator &simulator, const FlowLinearSolverParameters ¶meters, bool forceSerial=false)
Definition: ISTLSolverSystem.hpp:68
static constexpr bool isIncompatibleWithCprw
Definition: ISTLSolverSystem.hpp:55
static constexpr auto _0
Definition: ISTLSolverSystem.hpp:64
ISTLSolverSystem(const Simulator &simulator)
Definition: ISTLSolverSystem.hpp:75
void prepare(const Matrix &M, Vector &b) override
Definition: ISTLSolverSystem.hpp:87
bool solve(Vector &x) override
Definition: ISTLSolverSystem.hpp:94
Hierarchical collection of key/value pairs.
Definition: PropertyTree.hpp:39
PropertyTree get_child(const std::string &key) const
Definition: SystemTypes.hpp:77
const WRMatrix< Scalar > * B
Definition: SystemTypes.hpp:88
const RWMatrix< Scalar > * C
Definition: SystemTypes.hpp:87
const RRMatrix< Scalar > * A
Definition: SystemTypes.hpp:86
const WWMatrix< Scalar > * D
Definition: SystemTypes.hpp:89
Definition: SystemPreconditioner.hpp:55
Definition: WellMatrixMerger.hpp:124
Definition: blackoilbioeffectsmodules.hh:45
Dune::MultiTypeBlockVector< ResVector< Scalar >, WellVector< Scalar > > SystemVector
Definition: SystemTypes.hpp:59
Dune::BCRSMatrix< Dune::FieldMatrix< Scalar, numWellDofs, numWellDofs > > WWMatrix
Definition: SystemTypes.hpp:52
Dune::InverseOperatorResult InverseOperatorResult
Definition: GpuBridge.hpp:32
Dune::BCRSMatrix< Dune::FieldMatrix< Scalar, numResDofs, numWellDofs > > RWMatrix
Definition: SystemTypes.hpp:48
Dune::OwnerOverlapCopyCommunication< int, int > ParResComm
Definition: SystemPreconditioner.hpp:38
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(....
Definition: propertysystem.hh:233
Dune::BCRSMatrix< Dune::FieldMatrix< Scalar, numWellDofs, numResDofs > > WRMatrix
Definition: SystemTypes.hpp:50
This class carries all parameters for the NewtonIterationBlackoilInterleaved class.
Definition: FlowLinearSolverParameters.hpp:98
Definition: WellMatrixMerger.hpp:63
std::size_t totalWellBlocks
Definition: WellMatrixMerger.hpp:65