opm-simulators
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 
52 #include <opm/simulators/linalg/ilufirstelement.hh>
53 #include <opm/simulators/linalg/PropertyTree.hpp>
54 #include <opm/simulators/linalg/FlexibleSolver.hpp>
55 
56 #include <memory>
57 #include <set>
58 #include <stdexcept>
59 #include <string>
60 
61 #include <fmt/format.h>
62 
63 namespace Opm {
64 
65 #if HAVE_MPI
66 template<class M, class V>
67 struct EnergySolverSelector
68 {
69  using Comm = Dune::OwnerOverlapCopyCommunication<int, int>;
70  using EnergyOperator = Dune::OverlappingSchwarzOperator<M, V, V, Comm>;
72 };
73 
74 template<class Vector, class Grid, class Matrix>
75 std::tuple<std::unique_ptr<Dune::OverlappingSchwarzOperator<Matrix,Vector,Vector,
76  Dune::OwnerOverlapCopyCommunication<int,int>>>,
77  std::unique_ptr<typename EnergySolverSelector<Matrix,Vector>::type>>
78 createParallelFlexibleSolver(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 
84 template<class Vector, class Matrix>
85 std::tuple<std::unique_ptr<Dune::OverlappingSchwarzOperator<Matrix,Vector,Vector,
86  Dune::OwnerOverlapCopyCommunication<int,int>>>,
87  std::unique_ptr<typename EnergySolverSelector<Matrix,Vector>::type>>
88 createParallelFlexibleSolver(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 
100 template<class Grid, class GridView, class DofMapper, class Stencil, class FluidSystem, class Scalar>
101 GenericTemperatureModel<Grid,GridView,DofMapper,Stencil,FluidSystem,Scalar>::
102 GenericTemperatureModel(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 
113 template<class Grid, class GridView, class DofMapper, class Stencil, class FluidSystem, class Scalar>
115 doInit(std::size_t numGridDof)
116 {
117  doTemp_ = eclState_.getSimulationConfig().isTemp();
118  temperature_.resize(numGridDof);
119 
120  if (!doTemp_)
121  return;
122 
123  energyVector_.resize(numGridDof);
124  maxTempChange_ = Parameters::Get<Parameters::MaxTemperatureChange<Scalar>>();
125 }
126 
127 template<class Grid, class GridView, class DofMapper, class Stencil, class FluidSystem, class Scalar>
129 setupLinearSolver(const EnergyMatrix& matrix)
130 {
131  Scalar tolerance = 1e-2;
132  int maxIter = 100;
133  int verbosity = 0;
134  PropertyTree prm;
135  prm.put("maxiter", maxIter);
136  prm.put("tol", tolerance);
137  prm.put("verbosity", verbosity);
138  prm.put("solver", std::string("bicgstab"));
139  prm.put("preconditioner.type", std::string("ParOverILU0"));
140 
141 #if HAVE_MPI
142  if (gridView_.grid().comm().size() > 1)
143  {
144  auto [energyOperator, solver] =
145  createParallelFlexibleSolver<EnergyVector>(gridView_.grid(), matrix, prm);
146  op_ = std::move(energyOperator);
147  pre_ = &solver->preconditioner();
148  linear_solver_ = std::move(solver);
149  }
150  else
151 #endif
152  {
153  using SeqOperator = Dune::MatrixAdapter<EnergyMatrix, EnergyVector, EnergyVector>;
154  using SeqSolver = Dune::FlexibleSolver<SeqOperator>;
155  auto seqOp = std::make_unique<SeqOperator>(matrix);
156  auto dummyWeights = [](){ return EnergyVector(); };
157  auto seqSolver = std::make_unique<SeqSolver>(*seqOp, prm, dummyWeights, 0);
158  op_ = std::move(seqOp);
159  pre_ = &seqSolver->preconditioner();
160  linear_solver_ = std::move(seqSolver);
161  }
162 }
163 
164 template<class Grid, class GridView, class DofMapper, class Stencil, class FluidSystem, class Scalar>
165 bool GenericTemperatureModel<Grid,GridView,DofMapper,Stencil,FluidSystem,Scalar>::
166 linearSolve_(const EnergyMatrix& M, EnergyVector& x, EnergyVector& b)
167 {
168  if (!linear_solver_) {
169  setupLinearSolver(M);
170  } else {
171  pre_->update();
172  }
173 
174  Dune::InverseOperatorResult result;
175  linear_solver_->apply(x, b, result);
176  return result.converged;
177 }
178 
179 } // namespace Opm
180 
181 #endif // OPM_GENERIC_TEMPERATURE_MODEL_IMPL_HPP
Represents the stencil (finite volume geometry) of a single element in the ECFV discretization.
void put(const std::string &key, const T &data)
Insert key/value pair into property tree.
Definition: PropertyTree.cpp:71
Definition: GenericTemperatureModel.hpp:52
A solver class that encapsulates all needed objects for a linear solver (operator, scalar product, iterative solver and preconditioner) and sets them up based on runtime parameters, using the PreconditionerFactory for setting up preconditioners.
Definition: FlexibleSolver.hpp:43
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
A class which handles sequential implicit solution of the energy equation as specified in by TEMP...
Hierarchical collection of key/value pairs.
Definition: PropertyTree.hpp:38
void doInit(std::size_t numGridDof)
Initialize all internal data structures needed by the temperature module.
Definition: GenericTemperatureModel_impl.hpp:115