GenericTracerModel_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_TRACER_MODEL_IMPL_HPP
29#define OPM_GENERIC_TRACER_MODEL_IMPL_HPP
30
31#include <dune/istl/operators.hh>
32#include <dune/istl/solvers.hh>
33#include <dune/istl/schwarz.hh>
34#include <dune/istl/preconditioners.hh>
35#include <dune/istl/schwarz.hh>
36
37#include <opm/common/OpmLog/OpmLog.hpp>
38
39#include <opm/grid/CpGrid.hpp>
40
41#include <opm/input/eclipse/EclipseState/EclipseState.hpp>
42#include <opm/input/eclipse/EclipseState/Phase.hpp>
43#include <opm/input/eclipse/EclipseState/Tables/TracerVdTable.hpp>
44#include <opm/input/eclipse/Schedule/Well/Well.hpp>
45#include <opm/input/eclipse/Schedule/Well/WellTracerProperties.hpp>
46
47#include <opm/models/discretization/ecfv/ecfvstencil.hh>
48
50#include <opm/simulators/linalg/ilufirstelement.hh>
53
54#include <fmt/format.h>
55
56#include <array>
57#include <functional>
58#include <memory>
59#include <set>
60#include <stdexcept>
61#include <string>
63namespace Opm {
64
65#if HAVE_MPI
66template<class M, class V>
69 using Comm = Dune::OwnerOverlapCopyCommunication<int, int>;
70 using TracerOperator = 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 TracerSolverSelector<Matrix,Vector>::type>>
78createParallelFlexibleSolver(const Grid&, const Matrix&, const PropertyTree&)
79{
80 OPM_THROW(std::logic_error, "Grid not supported for parallel Tracers.");
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 TracerSolverSelector<Matrix,Vector>::type>>
88createParallelFlexibleSolver(const Dune::CpGrid& grid, const Matrix& M, const PropertyTree& prm)
89{
90 using TracerOperator = Dune::OverlappingSchwarzOperator<Matrix,Vector,Vector,
91 Dune::OwnerOverlapCopyCommunication<int,int>>;
92 using TracerSolver = Dune::FlexibleSolver<TracerOperator>;
93 const auto& cellComm = grid.cellCommunication();
94 auto op = std::make_unique<TracerOperator>(M, cellComm);
95 auto dummyWeights = [](){ return Vector();};
96 return {std::move(op), std::make_unique<TracerSolver>(*op, cellComm, prm, dummyWeights, 0)};
97}
98#endif
99
100template<class Grid, class GridView, class DofMapper, class Stencil, class Scalar>
102GenericTracerModel(const GridView& gridView,
103 const EclipseState& eclState,
104 const CartesianIndexMapper& cartMapper,
105 const DofMapper& dofMapper,
106 const std::function<std::array<double,dimWorld>(int)> centroids)
107 : gridView_(gridView)
108 , eclState_(eclState)
109 , cartMapper_(cartMapper)
110 , dofMapper_(dofMapper)
111 , centroids_(centroids)
112{
113}
114
115template<class Grid,class GridView, class DofMapper, class Stencil, class Scalar>
117tracerConcentration(int tracerIdx, int globalDofIdx) const
118{
119 if (tracerConcentration_.empty())
120 return 0.0;
121
122 return tracerConcentration_[tracerIdx][globalDofIdx];
123}
124
125template<class Grid,class GridView, class DofMapper, class Stencil, class Scalar>
127setTracerConcentration(int tracerIdx, int globalDofIdx, Scalar value)
128{
129 this->tracerConcentration_[tracerIdx][globalDofIdx] = value;
130}
131
132template<class Grid,class GridView, class DofMapper, class Stencil, class Scalar>
134numTracers() const
135{
136 return this->eclState_.tracer().size();
137}
138
139template<class Grid,class GridView, class DofMapper, class Stencil, class Scalar>
141fname(int tracerIdx) const
142{
143 return this->eclState_.tracer()[tracerIdx].fname();
144}
145
146template<class Grid,class GridView, class DofMapper, class Stencil, class Scalar>
148currentConcentration_(const Well& eclWell, const std::string& name) const
149{
150 return eclWell.getTracerProperties().getConcentration(name);
151}
152
153template<class Grid,class GridView, class DofMapper, class Stencil, class Scalar>
155name(int tracerIdx) const
156{
157 return this->eclState_.tracer()[tracerIdx].name;
158}
159
160template<class Grid,class GridView, class DofMapper, class Stencil, class Scalar>
162doInit(bool rst, std::size_t numGridDof,
163 std::size_t gasPhaseIdx, std::size_t oilPhaseIdx, std::size_t waterPhaseIdx)
164{
165 const auto& tracers = eclState_.tracer();
166
167 if (tracers.size() == 0)
168 return; // tracer treatment is supposed to be disabled
169
170 // retrieve the number of tracers from the deck
171 const std::size_t numTracers = tracers.size();
172 tracerConcentration_.resize(numTracers);
173 storageOfTimeIndex1_.resize(numTracers);
174
175 // the phase where the tracer is
176 tracerPhaseIdx_.resize(numTracers);
177 for (std::size_t tracerIdx = 0; tracerIdx < numTracers; tracerIdx++) {
178 const auto& tracer = tracers[tracerIdx];
179
180 if (tracer.phase == Phase::WATER)
181 tracerPhaseIdx_[tracerIdx] = waterPhaseIdx;
182 else if (tracer.phase == Phase::OIL)
183 tracerPhaseIdx_[tracerIdx] = oilPhaseIdx;
184 else if (tracer.phase == Phase::GAS)
185 tracerPhaseIdx_[tracerIdx] = gasPhaseIdx;
186
187 tracerConcentration_[tracerIdx].resize(numGridDof);
188 storageOfTimeIndex1_[tracerIdx].resize(numGridDof);
189
190 if (rst)
191 continue;
192
193
194 //TBLK keyword
195 if (tracer.free_concentration.has_value()){
196 const auto& free_concentration = tracer.free_concentration.value();
197 int tblkDatasize = free_concentration.size();
198 if (tblkDatasize < cartMapper_.cartesianSize()){
199 throw std::runtime_error("Wrong size of TBLK for" + tracer.name);
200 }
201 for (std::size_t globalDofIdx = 0; globalDofIdx < numGridDof; ++globalDofIdx) {
202 int cartDofIdx = cartMapper_.cartesianIndex(globalDofIdx);
203 tracerConcentration_[tracerIdx][globalDofIdx] = free_concentration[cartDofIdx];
204 }
205 }
206 //TVDPF keyword
207 else if (tracer.free_tvdp.has_value()) {
208 const auto& free_tvdp = tracer.free_tvdp.value();
209 for (std::size_t globalDofIdx = 0; globalDofIdx < numGridDof; ++globalDofIdx) {
210 tracerConcentration_[tracerIdx][globalDofIdx] =
211 free_tvdp.evaluate("TRACER_CONCENTRATION",
212 centroids_(globalDofIdx)[2]);
213 }
214 } else
215 throw std::logic_error(fmt::format("Can not initialize tracer: {}", tracer.name));
216 }
217
218 // allocate matrix for storing the Jacobian of the tracer residual
219 tracerMatrix_ = std::make_unique<TracerMatrix>(numGridDof, numGridDof, TracerMatrix::random);
220
221 // find the sparsity pattern of the tracer matrix
222 using NeighborSet = std::set<unsigned>;
223 std::vector<NeighborSet> neighbors(numGridDof);
224
225 Stencil stencil(gridView_, dofMapper_);
226 for (const auto& elem : elements(gridView_)) {
227 stencil.update(elem);
228
229 for (unsigned primaryDofIdx = 0; primaryDofIdx < stencil.numPrimaryDof(); ++primaryDofIdx) {
230 unsigned myIdx = stencil.globalSpaceIndex(primaryDofIdx);
231
232 for (unsigned dofIdx = 0; dofIdx < stencil.numDof(); ++dofIdx) {
233 unsigned neighborIdx = stencil.globalSpaceIndex(dofIdx);
234 neighbors[myIdx].insert(neighborIdx);
235 }
236 }
237 }
238
239 // allocate space for the rows of the matrix
240 for (unsigned dofIdx = 0; dofIdx < numGridDof; ++ dofIdx)
241 tracerMatrix_->setrowsize(dofIdx, neighbors[dofIdx].size());
242 tracerMatrix_->endrowsizes();
243
244 // fill the rows with indices. each degree of freedom talks to
245 // all of its neighbors. (it also talks to itself since
246 // degrees of freedom are sometimes quite egocentric.)
247 for (unsigned dofIdx = 0; dofIdx < numGridDof; ++ dofIdx) {
248 typename NeighborSet::iterator nIt = neighbors[dofIdx].begin();
249 typename NeighborSet::iterator nEndIt = neighbors[dofIdx].end();
250 for (; nIt != nEndIt; ++nIt)
251 tracerMatrix_->addindex(dofIdx, *nIt);
252 }
253 tracerMatrix_->endindices();
254}
255
256template<class Grid,class GridView, class DofMapper, class Stencil, class Scalar>
259{
260 x = 0.0;
261 Scalar tolerance = 1e-2;
262 int maxIter = 100;
263
264 int verbosity = 0;
265 PropertyTree prm;
266 prm.put("maxiter", maxIter);
267 prm.put("tol", tolerance);
268 prm.put("verbosity", verbosity);
269 prm.put("solver", std::string("bicgstab"));
270 prm.put("preconditioner.type", std::string("ParOverILU0"));
271
272#if HAVE_MPI
273 if(gridView_.grid().comm().size() > 1)
274 {
275 auto [tracerOperator, solver] =
276 createParallelFlexibleSolver<TracerVector>(gridView_.grid(), M, prm);
277 (void) tracerOperator;
278
280 solver->apply(x, b, result);
281
282 // return the result of the solver
283 return result.converged;
284 }
285 else
286 {
287#endif
288 using TracerSolver = Dune::BiCGSTABSolver<TracerVector>;
289 using TracerOperator = Dune::MatrixAdapter<TracerMatrix,TracerVector,TracerVector>;
290 using TracerScalarProduct = Dune::SeqScalarProduct<TracerVector>;
291 using TracerPreconditioner = Dune::SeqILU< TracerMatrix,TracerVector,TracerVector>;
292
293 TracerOperator tracerOperator(M);
294 TracerScalarProduct tracerScalarProduct;
295 TracerPreconditioner tracerPreconditioner(M, 0, 1); // results in ILU0
296
297 TracerSolver solver (tracerOperator, tracerScalarProduct,
298 tracerPreconditioner, tolerance, maxIter,
299 verbosity);
300
302 solver.apply(x, b, result);
303
304 // return the result of the solver
305 return result.converged;
306#if HAVE_MPI
307 }
308#endif
309}
310
311template<class Grid,class GridView, class DofMapper, class Stencil, class Scalar>
313linearSolveBatchwise_(const TracerMatrix& M, std::vector<TracerVector>& x, std::vector<TracerVector>& b)
314{
315 Scalar tolerance = 1e-2;
316 int maxIter = 100;
317
318 int verbosity = 0;
319 PropertyTree prm;
320 prm.put("maxiter", maxIter);
321 prm.put("tol", tolerance);
322 prm.put("verbosity", verbosity);
323 prm.put("solver", std::string("bicgstab"));
324 prm.put("preconditioner.type", std::string("ParOverILU0"));
325
326#if HAVE_MPI
327 if(gridView_.grid().comm().size() > 1)
328 {
329 auto [tracerOperator, solver] =
330 createParallelFlexibleSolver<TracerVector>(gridView_.grid(), M, prm);
331 (void) tracerOperator;
332 bool converged = true;
333 for (std::size_t nrhs = 0; nrhs < b.size(); ++nrhs) {
334 x[nrhs] = 0.0;
336 solver->apply(x[nrhs], b[nrhs], result);
337 converged = (converged && result.converged);
338 }
339 return converged;
340 }
341 else
342 {
343#endif
344 using TracerSolver = Dune::BiCGSTABSolver<TracerVector>;
345 using TracerOperator = Dune::MatrixAdapter<TracerMatrix,TracerVector,TracerVector>;
346 using TracerScalarProduct = Dune::SeqScalarProduct<TracerVector>;
347 using TracerPreconditioner = Dune::SeqILU< TracerMatrix,TracerVector,TracerVector>;
348
349 TracerOperator tracerOperator(M);
350 TracerScalarProduct tracerScalarProduct;
351 TracerPreconditioner tracerPreconditioner(M, 0, 1); // results in ILU0
352
353 TracerSolver solver (tracerOperator, tracerScalarProduct,
354 tracerPreconditioner, tolerance, maxIter,
355 verbosity);
356
357 bool converged = true;
358 for (std::size_t nrhs = 0; nrhs < b.size(); ++nrhs) {
359 x[nrhs] = 0.0;
361 solver.apply(x[nrhs], b[nrhs], result);
362 converged = (converged && result.converged);
363 }
364
365 // return the result of the solver
366 return converged;
367#if HAVE_MPI
368 }
369#endif
370}
371
372} // namespace Opm
373
374#endif // OPM_GENERIC_TRACER_MODEL_IMPL_HPP
Definition: CollectDataOnIORank.hpp:49
Definition: FlexibleSolver.hpp:45
Scalar tracerConcentration(int tracerIdx, int globalDofIdx) const
Return the tracer concentration for tracer index and global DofIdx.
Definition: GenericTracerModel_impl.hpp:117
bool linearSolve_(const TracerMatrix &M, TracerVector &x, TracerVector &b)
Definition: GenericTracerModel_impl.hpp:258
std::string fname(int tracerIdx) const
Definition: GenericTracerModel_impl.hpp:141
void doInit(bool rst, std::size_t numGridDof, std::size_t gasPhaseIdx, std::size_t oilPhaseIdx, std::size_t waterPhaseIdx)
Initialize all internal data structures needed by the tracer module.
Definition: GenericTracerModel_impl.hpp:162
const std::string & name(int tracerIdx) const
Return the tracer name.
Definition: GenericTracerModel_impl.hpp:155
Dune::BlockVector< Dune::FieldVector< Scalar, 1 > > TracerVector
Definition: GenericTracerModel.hpp:56
void setTracerConcentration(int tracerIdx, int globalDofIdx, Scalar value)
Definition: GenericTracerModel_impl.hpp:127
bool linearSolveBatchwise_(const TracerMatrix &M, std::vector< TracerVector > &x, std::vector< TracerVector > &b)
Definition: GenericTracerModel_impl.hpp:313
GenericTracerModel(const GridView &gridView, const EclipseState &eclState, const CartesianIndexMapper &cartMapper, const DofMapper &dofMapper, const std::function< std::array< double, dimWorld >(int)> centroids)
Definition: GenericTracerModel_impl.hpp:102
int numTracers() const
Return the number of tracers considered by the tracerModel.
Definition: GenericTracerModel_impl.hpp:134
Dune::BCRSMatrix< Opm::MatrixBlock< Scalar, 1, 1 > > TracerMatrix
Definition: GenericTracerModel.hpp:55
double currentConcentration_(const Well &eclWell, const std::string &name) const
Definition: GenericTracerModel_impl.hpp:148
Definition: PropertyTree.hpp:37
void put(const std::string &key, const T &data)
Definition: BlackoilPhases.hpp:27
Dune::InverseOperatorResult InverseOperatorResult
Definition: BdaBridge.hpp:32
std::tuple< std::unique_ptr< Dune::OverlappingSchwarzOperator< Matrix, Vector, Vector, Dune::OwnerOverlapCopyCommunication< int, int > > >, std::unique_ptr< typename TracerSolverSelector< Matrix, Vector >::type > > createParallelFlexibleSolver(const Grid &, const Matrix &, const PropertyTree &)
Definition: GenericTracerModel_impl.hpp:78
Definition: GenericTracerModel_impl.hpp:68
Dune::OverlappingSchwarzOperator< M, V, V, Comm > TracerOperator
Definition: GenericTracerModel_impl.hpp:70
Dune::OwnerOverlapCopyCommunication< int, int > Comm
Definition: GenericTracerModel_impl.hpp:69