GenericTemperatureModel_impl.hpp
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
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 2 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 Consult the COPYING file in the top-level source directory of this
20 module for the precise wording of the license and the list of
21 copyright holders.
22*/
28#ifndef OPM_GENERIC_TEMPERATURE_MODEL_IMPL_HPP
29#define OPM_GENERIC_TEMPERATURE_MODEL_IMPL_HPP
30
31// Improve IDE experience
32#ifndef OPM_GENERIC_TEMPERATURE_MODEL_HPP
33#include <config.h>
35#endif
36
37#include <dune/istl/operators.hh>
38#include <dune/istl/solvers.hh>
39#include <dune/istl/schwarz.hh>
40#include <dune/istl/preconditioners.hh>
41#include <dune/istl/schwarz.hh>
42
43#include <opm/common/OpmLog/OpmLog.hpp>
44
45#include <opm/grid/CpGrid.hpp>
46
47#include <opm/input/eclipse/EclipseState/EclipseState.hpp>
48#include <opm/input/eclipse/Schedule/Well/Well.hpp>
49
51
55
56#include <memory>
57#include <set>
58#include <stdexcept>
59#include <string>
60
61#include <fmt/format.h>
62
63namespace Opm {
64
65#if HAVE_MPI
66template<class M, class V>
68{
69 using Comm = Dune::OwnerOverlapCopyCommunication<int, int>;
70 using EnergyOperator = Dune::OverlappingSchwarzOperator<M, V, V, Comm>;
72};
73
74template<class Vector, class Grid, class Matrix>
75std::tuple<std::unique_ptr<Dune::OverlappingSchwarzOperator<Matrix,Vector,Vector,
76 Dune::OwnerOverlapCopyCommunication<int,int>>>,
77 std::unique_ptr<typename EnergySolverSelector<Matrix,Vector>::type>>
78createParallelFlexibleSolver(const Grid&, const Matrix&, const PropertyTree&)
79{
80 OPM_THROW(std::logic_error, "Grid not supported for parallel Temperatures.");
81 return {nullptr, nullptr};
82}
83
84template<class Vector, class Matrix>
85std::tuple<std::unique_ptr<Dune::OverlappingSchwarzOperator<Matrix,Vector,Vector,
86 Dune::OwnerOverlapCopyCommunication<int,int>>>,
87 std::unique_ptr<typename EnergySolverSelector<Matrix,Vector>::type>>
88createParallelFlexibleSolver(const Dune::CpGrid& grid, const Matrix& M, const PropertyTree& prm)
89{
90 using EnergyOperator = Dune::OverlappingSchwarzOperator<Matrix,Vector,Vector,
91 Dune::OwnerOverlapCopyCommunication<int,int>>;
92 using EnergySolver = Dune::FlexibleSolver<EnergyOperator>;
93 const auto& cellComm = grid.cellCommunication();
94 auto op = std::make_unique<EnergyOperator>(M, cellComm);
95 auto dummyWeights = [](){ return Vector();};
96 return {std::move(op), std::make_unique<EnergySolver>(*op, cellComm, prm, dummyWeights, 0)};
97}
98#endif
99
100template<class Grid, class GridView, class DofMapper, class Stencil, class FluidSystem, class Scalar>
102GenericTemperatureModel(const GridView& gridView,
103 const EclipseState& eclState,
104 const CartesianIndexMapper& cartMapper,
105 const DofMapper& dofMapper)
106 : gridView_(gridView)
107 , eclState_(eclState)
108 , cartMapper_(cartMapper)
109 , dofMapper_(dofMapper)
110{
111}
112
113template<class Grid, class GridView, class DofMapper, class Stencil, class FluidSystem, class Scalar>
115doInit(std::size_t numGridDof)
116{
117 doTemp_ = eclState_.getSimulationConfig().isTemp();
118
119 temperature_.resize(numGridDof);
120 energyVector_.resize(numGridDof);
121 // allocate matrix for storing the Jacobian of the temperature residual
122 energyMatrix_ = std::make_unique<EnergyMatrix>(numGridDof, numGridDof, EnergyMatrix::random);
123
124 // find the sparsity pattern of the temperature matrix
125 using NeighborSet = std::set<unsigned>;
126 std::vector<NeighborSet> neighbors(numGridDof);
127
128 Stencil stencil(gridView_, dofMapper_);
129 for (const auto& elem : elements(gridView_)) {
130 stencil.update(elem);
131
132 for (unsigned primaryDofIdx = 0; primaryDofIdx < stencil.numPrimaryDof(); ++primaryDofIdx) {
133 unsigned myIdx = stencil.globalSpaceIndex(primaryDofIdx);
134
135 for (unsigned dofIdx = 0; dofIdx < stencil.numDof(); ++dofIdx) {
136 unsigned neighborIdx = stencil.globalSpaceIndex(dofIdx);
137 neighbors[myIdx].insert(neighborIdx);
138 }
139 }
140 }
141
142 // allocate space for the rows of the matrix
143 for (unsigned dofIdx = 0; dofIdx < numGridDof; ++ dofIdx) {
144 energyMatrix_->setrowsize(dofIdx, neighbors[dofIdx].size());
145 }
146 energyMatrix_->endrowsizes();
147
148 // fill the rows with indices. each degree of freedom talks to
149 // all of its neighbors. (it also talks to itself since
150 // degrees of freedom are sometimes quite egocentric.)
151 for (unsigned dofIdx = 0; dofIdx < numGridDof; ++ dofIdx) {
152 typename NeighborSet::iterator nIt = neighbors[dofIdx].begin();
153 typename NeighborSet::iterator nEndIt = neighbors[dofIdx].end();
154 for (; nIt != nEndIt; ++nIt) {
155 energyMatrix_->addindex(dofIdx, *nIt);
156 }
157 }
158 energyMatrix_->endindices();
159
160 maxTempChange_ = Parameters::Get<Parameters::MaxTemperatureChange<Scalar>>();
161}
162
163template<class Grid, class GridView, class DofMapper, class Stencil, class FluidSystem, class Scalar>
166{
167 x = 0.0;
168 Scalar tolerance = 1e-2;
169 int maxIter = 100;
170
171 int verbosity = 0;
172 PropertyTree prm;
173 prm.put("maxiter", maxIter);
174 prm.put("tol", tolerance);
175 prm.put("verbosity", verbosity);
176 prm.put("solver", std::string("bicgstab"));
177 prm.put("preconditioner.type", std::string("ParOverILU0"));
178
179#if HAVE_MPI
180 if(gridView_.grid().comm().size() > 1)
181 {
182 auto [energyOperator, solver] =
183 createParallelFlexibleSolver<EnergyVector>(gridView_.grid(), M, prm);
184 (void) energyOperator;
185
187 solver->apply(x, b, result);
188
189 // return the result of the solver
190 return result.converged;
191 }
192 else
193#endif
194 {
195 using EnergySolver = Dune::BiCGSTABSolver<EnergyVector>;
196 using EnergyOperator = Dune::MatrixAdapter<EnergyMatrix,EnergyVector,EnergyVector>;
197 using EnergyScalarProduct = Dune::SeqScalarProduct<EnergyVector>;
198 using EnergyPreconditioner = Dune::SeqILU< EnergyMatrix,EnergyVector,EnergyVector>;
199
200 EnergyOperator energyOperator(M);
201 EnergyScalarProduct energyScalarProduct;
202 EnergyPreconditioner energyPreconditioner(M, 0, 1); // results in ILU0
203
204 EnergySolver solver (energyOperator, energyScalarProduct,
205 energyPreconditioner, tolerance, maxIter,
206 verbosity);
207
209 solver.apply(x, b, result);
210
211 // return the result of the solver
212 return result.converged;
213 }
214}
215
216} // namespace Opm
217
218#endif // OPM_GENERIC_TEMPERATURE_MODEL_IMPL_HPP
Definition: CollectDataOnIORank.hpp:49
Definition: FlexibleSolver.hpp:45
GenericTemperatureModel(const GridView &gridView, const EclipseState &eclState, const CartesianIndexMapper &cartMapper, const DofMapper &dofMapper)
Definition: GenericTemperatureModel_impl.hpp:102
Dune::BCRSMatrix< Opm::MatrixBlock< Scalar, 1, 1 > > EnergyMatrix
Definition: GenericTemperatureModel.hpp:54
bool linearSolve_(const EnergyMatrix &M, EnergyVector &x, EnergyVector &b)
Definition: GenericTemperatureModel_impl.hpp:165
Dune::BlockVector< Dune::FieldVector< Scalar, 1 > > EnergyVector
Definition: GenericTemperatureModel.hpp:55
void doInit(std::size_t numGridDof)
Initialize all internal data structures needed by the temperature module.
Definition: GenericTemperatureModel_impl.hpp:115
Hierarchical collection of key/value pairs.
Definition: PropertyTree.hpp:39
void put(const std::string &key, const T &data)
Definition: blackoilbioeffectsmodules.hh:43
Dune::InverseOperatorResult InverseOperatorResult
Definition: GpuBridge.hpp:32
std::tuple< std::unique_ptr< Dune::OverlappingSchwarzOperator< Matrix, Vector, Vector, Dune::OwnerOverlapCopyCommunication< int, int > > >, std::unique_ptr< typename EnergySolverSelector< Matrix, Vector >::type > > createParallelFlexibleSolver(const Grid &, const Matrix &, const PropertyTree &)
Definition: GenericTemperatureModel_impl.hpp:78
Definition: GenericTemperatureModel_impl.hpp:68
Dune::OverlappingSchwarzOperator< M, V, V, Comm > EnergyOperator
Definition: GenericTemperatureModel_impl.hpp:70
Dune::OwnerOverlapCopyCommunication< int, int > Comm
Definition: GenericTemperatureModel_impl.hpp:69