Go to the documentation of this file.
28#ifndef OPM_GENERIC_TRACER_MODEL_IMPL_HPP
29#define OPM_GENERIC_TRACER_MODEL_IMPL_HPP
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>
37#include <opm/common/OpmLog/OpmLog.hpp>
38#include <opm/common/TimingMacros.hpp>
40#include <opm/grid/CpGrid.hpp>
42#include <opm/input/eclipse/EclipseState/EclipseState.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>
56#include <fmt/format.h>
68template< class M, class V>
71 using Comm = Dune::OwnerOverlapCopyCommunication<int, int>;
76template< class Vector, class Gr id, class Matrix>
77std::tuple<std::unique_ptr<Dune::OverlappingSchwarzOperator<Matrix,Vector,Vector,
78 Dune::OwnerOverlapCopyCommunication<int,int>>>,
79 std::unique_ptr<typename TracerSolverSelector<Matrix,Vector>::type>>
82 OPM_THROW(std::logic_error, "Grid not supported for parallel Tracers.");
83 return { nullptr, nullptr};
86template< class Vector, class Matrix>
87std::tuple<std::unique_ptr<Dune::OverlappingSchwarzOperator<Matrix,Vector,Vector,
88 Dune::OwnerOverlapCopyCommunication<int,int>>>,
89 std::unique_ptr<typename TracerSolverSelector<Matrix,Vector>::type>>
92 using TracerOperator = Dune::OverlappingSchwarzOperator<Matrix,Vector,Vector,
93 Dune::OwnerOverlapCopyCommunication<int,int>>;
95 const auto& cellComm = grid.cellCommunication();
96 auto op = std::make_unique<TracerOperator>(M, cellComm);
97 auto dummyWeights = [](){ return Vector();};
98 return {std::move(op), std::make_unique<TracerSolver>(*op, cellComm, prm, dummyWeights, 0)};
102template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
105 const EclipseState& eclState,
107 const DofMapper& dofMapper,
108 const std::function<std::array<double,dimWorld>( int)> centroids)
109 : gridView_(gridView)
110 , eclState_(eclState)
111 , cartMapper_(cartMapper)
112 , dofMapper_(dofMapper)
113 , centroids_(centroids)
117template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
121 if (freeTracerConcentration_.empty()) {
125 return freeTracerConcentration_[tracerIdx][globalDofIdx][0];
128template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
132 if (solTracerConcentration_.empty()) {
136 return solTracerConcentration_[tracerIdx][globalDofIdx][0];
139template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
143 this->freeTracerConcentration_[tracerIdx][globalDofIdx][0] = value;
144 this->tracerConcentration_[tracerIdx][globalDofIdx][0] = value;
147template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
151 this->solTracerConcentration_[tracerIdx][globalDofIdx][0] = value;
152 this->tracerConcentration_[tracerIdx][globalDofIdx][1] = value;
155template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
159 this->enableSolTracers_[tracerIdx] = enableSolTracer;
162template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
166 return this->eclState_.tracer().size();
169template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
171fname( int tracerIdx) const
173 return this->eclState_.tracer()[tracerIdx].fname();
176template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
178sname( int tracerIdx) const
180 return this->eclState_.tracer()[tracerIdx].sname();
183template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
187 return this->eclState_.tracer()[tracerIdx].wellfname();
190template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
194 return this->eclState_.tracer()[tracerIdx].wellsname();
197template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
199phase( int tracerIdx) const
201 return this->eclState_.tracer()[tracerIdx].phase;
204template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
208 return this->enableSolTracers_;
211template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
213currentConcentration_( const Well& eclWell, const std::string& trName, const SummaryState& summaryState) const
215 return eclWell.getTracerProperties().getConcentration(WellTracerProperties::Well { eclWell.name() },
216 WellTracerProperties::Tracer { trName },
220template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
222name( int tracerIdx) const
224 return this->eclState_.tracer()[tracerIdx].name;
227template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
229doInit( bool rst, std::size_t numGridDof,
230 std::size_t gasPhaseIdx, std::size_t oilPhaseIdx, std::size_t waterPhaseIdx)
232 const auto& tracers = eclState_.tracer();
234 if (tracers.size() == 0) {
239 const std::size_t numTracers = tracers.size();
240 enableSolTracers_.resize(numTracers);
241 tracerConcentration_.resize(numTracers);
242 freeTracerConcentration_.resize(numTracers);
243 solTracerConcentration_.resize(numTracers);
246 tracerPhaseIdx_.resize(numTracers);
247 for (std::size_t tracerIdx = 0; tracerIdx < numTracers; tracerIdx++) {
248 const auto& tracer = tracers[tracerIdx];
250 if (tracer.phase == Phase::WATER)
251 tracerPhaseIdx_[tracerIdx] = waterPhaseIdx;
252 else if (tracer.phase == Phase::OIL)
253 tracerPhaseIdx_[tracerIdx] = oilPhaseIdx;
254 else if (tracer.phase == Phase::GAS)
255 tracerPhaseIdx_[tracerIdx] = gasPhaseIdx;
257 tracerConcentration_[tracerIdx].resize(numGridDof);
258 freeTracerConcentration_[tracerIdx].resize(numGridDof);
259 solTracerConcentration_[tracerIdx].resize(numGridDof);
266 if (tracer.free_concentration.has_value()){
267 const auto& free_concentration = tracer.free_concentration.value();
268 int tblkDatasize = free_concentration.size();
269 if (tblkDatasize < cartMapper_.cartesianSize()){
270 throw std::runtime_error( "Wrong size of TBLKF for" + tracer.name);
272 for (std::size_t globalDofIdx = 0; globalDofIdx < numGridDof; ++globalDofIdx) {
273 int cartDofIdx = cartMapper_.cartesianIndex(globalDofIdx);
274 tracerConcentration_[tracerIdx][globalDofIdx][0] = free_concentration[cartDofIdx];
275 freeTracerConcentration_[tracerIdx][globalDofIdx] = free_concentration[cartDofIdx];
279 else if (tracer.free_tvdp.has_value()) {
280 const auto& free_tvdp = tracer.free_tvdp.value();
281 for (std::size_t globalDofIdx = 0; globalDofIdx < numGridDof; ++globalDofIdx) {
282 tracerConcentration_[tracerIdx][globalDofIdx][0] =
283 free_tvdp.evaluate( "TRACER_CONCENTRATION",
284 centroids_(globalDofIdx)[2]);
285 freeTracerConcentration_[tracerIdx][globalDofIdx] =
286 free_tvdp.evaluate( "TRACER_CONCENTRATION",
287 centroids_(globalDofIdx)[2]);
291 OpmLog::warning(fmt::format( "No TBLKF or TVDPF given for free tracer {}. "
292 "Initial values set to zero. ", tracer.name));
293 for (std::size_t globalDofIdx = 0; globalDofIdx < numGridDof; ++globalDofIdx) {
294 tracerConcentration_[tracerIdx][globalDofIdx][0] = 0.0;
295 freeTracerConcentration_[tracerIdx][globalDofIdx] = 0.0;
300 if (tracer.phase != Phase::WATER &&
301 ((tracer.phase == Phase::GAS && FluidSystem::enableDissolvedGas()) ||
302 (tracer.phase == Phase::OIL && FluidSystem::enableVaporizedOil()))) {
304 if (tracer.solution_concentration.has_value()){
305 enableSolTracers_[tracerIdx] = true;
306 const auto& solution_concentration = tracer.solution_concentration.value();
307 int tblkDatasize = solution_concentration.size();
308 if (tblkDatasize < cartMapper_.cartesianSize()){
309 throw std::runtime_error( "Wrong size of TBLKS for" + tracer.name);
311 for (std::size_t globalDofIdx = 0; globalDofIdx < numGridDof; ++globalDofIdx) {
312 int cartDofIdx = cartMapper_.cartesianIndex(globalDofIdx);
313 tracerConcentration_[tracerIdx][globalDofIdx][1] = solution_concentration[cartDofIdx];
314 solTracerConcentration_[tracerIdx][globalDofIdx] = solution_concentration[cartDofIdx];
318 else if (tracer.solution_tvdp.has_value()) {
319 enableSolTracers_[tracerIdx] = true;
320 const auto& solution_tvdp = tracer.solution_tvdp.value();
321 for (std::size_t globalDofIdx = 0; globalDofIdx < numGridDof; ++globalDofIdx) {
322 tracerConcentration_[tracerIdx][globalDofIdx][1] =
323 solution_tvdp.evaluate( "TRACER_CONCENTRATION",
324 centroids_(globalDofIdx)[2]);
325 solTracerConcentration_[tracerIdx][globalDofIdx] =
326 solution_tvdp.evaluate( "TRACER_CONCENTRATION",
327 centroids_(globalDofIdx)[2]);
332 enableSolTracers_[tracerIdx] = false;
333 OpmLog::warning(fmt::format( "No TBLKS or TVDPS given for solution tracer {}. "
334 "Initial values set to zero. ", tracer.name));
335 for (std::size_t globalDofIdx = 0; globalDofIdx < numGridDof; ++globalDofIdx) {
336 tracerConcentration_[tracerIdx][globalDofIdx][1] = 0.0;
337 solTracerConcentration_[tracerIdx][globalDofIdx] = 0.0;
343 enableSolTracers_[tracerIdx] = false;
344 for (std::size_t globalDofIdx = 0; globalDofIdx < numGridDof; ++globalDofIdx) {
345 tracerConcentration_[tracerIdx][globalDofIdx][1] = 0.0;
346 solTracerConcentration_[tracerIdx][globalDofIdx] = 0.0;
352 tracerMatrix_ = std::make_unique<TracerMatrix>(numGridDof, numGridDof, TracerMatrix::random);
355 using NeighborSet = std::set<unsigned>;
356 std::vector<NeighborSet> neighbors(numGridDof);
358 Stencil stencil(gridView_, dofMapper_);
359 for ( const auto& elem : elements(gridView_)) {
360 stencil.update(elem);
362 for ( unsigned primaryDofIdx = 0; primaryDofIdx < stencil.numPrimaryDof(); ++primaryDofIdx) {
363 unsigned myIdx = stencil.globalSpaceIndex(primaryDofIdx);
365 for ( unsigned dofIdx = 0; dofIdx < stencil.numDof(); ++dofIdx) {
366 unsigned neighborIdx = stencil.globalSpaceIndex(dofIdx);
367 neighbors[myIdx].insert(neighborIdx);
373 for ( unsigned dofIdx = 0; dofIdx < numGridDof; ++ dofIdx) {
374 tracerMatrix_->setrowsize(dofIdx, neighbors[dofIdx].size());
376 tracerMatrix_->endrowsizes();
381 for ( unsigned dofIdx = 0; dofIdx < numGridDof; ++ dofIdx) {
382 for ( const auto& index : neighbors[dofIdx]) {
383 tracerMatrix_->addindex(dofIdx, index);
386 tracerMatrix_->endindices();
389template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
394 Scalar tolerance = 1e-2;
399 prm. put( "maxiter", maxIter);
400 prm. put( "tol", tolerance);
401 prm. put( "verbosity", verbosity);
402 prm. put( "solver", std::string( "bicgstab"));
403 prm. put( "preconditioner.type", std::string( "paroverilu0"));
406 if(gridView_.grid().comm().size() > 1)
408 auto [tracerOperator, solver] =
409 createParallelFlexibleSolver<TracerVector>(gridView_.grid(), M, prm);
410 (void) tracerOperator;
413 solver->apply(x, b, result);
416 return result.converged;
421 using TracerSolver = Dune::BiCGSTABSolver<TracerVector>;
422 using TracerOperator = Dune::MatrixAdapter<TracerMatrix,TracerVector,TracerVector>;
423 using TracerScalarProduct = Dune::SeqScalarProduct<TracerVector>;
424 using TracerPreconditioner = Dune::SeqILU< TracerMatrix,TracerVector,TracerVector>;
426 TracerOperator tracerOperator(M);
427 TracerScalarProduct tracerScalarProduct;
428 TracerPreconditioner tracerPreconditioner(M, 0, 1);
430 TracerSolver solver (tracerOperator, tracerScalarProduct,
431 tracerPreconditioner, tolerance, maxIter,
435 solver.apply(x, b, result);
438 return result.converged;
444template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
448 OPM_TIMEBLOCK(tracerSolve);
449 const Scalar tolerance = 1e-2;
450 const int maxIter = 100;
451 const int verbosity = 0;
454 if (gridView_.grid().comm().size() > 1)
457 prm. put( "maxiter", maxIter);
458 prm. put( "tol", tolerance);
459 prm. put( "verbosity", verbosity);
460 prm. put( "solver", std::string( "bicgstab"));
461 prm. put( "preconditioner.type", std::string( "paroverilu0"));
462 auto [tracerOperator, solver] =
463 createParallelFlexibleSolver<TracerVector>(gridView_.grid(), M, prm);
464 (void) tracerOperator;
465 bool converged = true;
466 for (std::size_t nrhs = 0; nrhs < b.size(); ++nrhs) {
469 solver->apply(x[nrhs], b[nrhs], result);
470 converged = (converged && result.converged);
477 using TracerSolver = Dune::BiCGSTABSolver<TracerVector>;
478 using TracerOperator = Dune::MatrixAdapter<TracerMatrix,TracerVector,TracerVector>;
479 using TracerScalarProduct = Dune::SeqScalarProduct<TracerVector>;
480 using TracerPreconditioner = Dune::SeqILU<TracerMatrix,TracerVector,TracerVector>;
482 if (std::ranges::all_of(b, []( const auto& v) { return v.infinity_norm() == 0.0; }))
487 TracerOperator tracerOperator(M);
488 TracerScalarProduct tracerScalarProduct;
489 TracerPreconditioner tracerPreconditioner(M, 0, 1);
491 TracerSolver solver (tracerOperator, tracerScalarProduct,
492 tracerPreconditioner, tolerance, maxIter,
495 bool converged = true;
496 for (std::size_t nrhs = 0; nrhs < b.size(); ++nrhs) {
499 solver.apply(x[nrhs], b[nrhs], result);
500 converged = (converged && result.converged);
508template< class Gr id, class Gr idView, class DofMapper, class Stencil, class Flu idSystem, class Scalar>
512 OPM_TIMEBLOCK(tracerSolveReorder);
521 OpmLog::debug(fmt::format( "Tracer reorder solver: {} components, {} loops, "
522 "largest component {} cells, {} Gauss-Seidel sweeps",
523 statistics.numComponents,
525 statistics.largestComponent,
526 statistics.maxIterations));
528 catch ( const std::runtime_error& e) {
529 OpmLog::warning(fmt::format( "Tracer reorder solver failed: {}", e.what()));
532 const auto& comm = gridView_.comm();
533 if (comm.min( static_cast<int>(solved)) == 0) {
538 if (comm.size() > 1) {
540 gridView_.communicate(handle, Dune::InteriorBorder_All_Interface,
541 Dune::ForwardCommunication);
A datahandle sending data located in multiple vectors.
Definition: CollectDataOnIORank.hpp:50
Definition: FlexibleSolver.hpp:45
void setEnableSolTracers(int tracerIdx, bool enableSolTracer) Definition: GenericTracerModel_impl.hpp:157
Opm::BlockVector< Dune::FieldVector< Scalar, 2 > > TracerVector Definition: GenericTracerModel.hpp:60
int numTracers() const Return the number of tracers considered by the tracerModel. Definition: GenericTracerModel_impl.hpp:164
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:229
std::string sname(int tracerIdx) const Definition: GenericTracerModel_impl.hpp:178
void setSolTracerConcentration(int tracerIdx, int globalDofIdx, Scalar value) Definition: GenericTracerModel_impl.hpp:149
std::string wellfname(int tracerIdx) const Definition: GenericTracerModel_impl.hpp:185
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:104
const std::string & name(int tracerIdx) const Return the tracer name. Definition: GenericTracerModel_impl.hpp:222
const std::vector< bool > & enableSolTracers() const Definition: GenericTracerModel_impl.hpp:206
Phase phase(int tracerIdx) const Definition: GenericTracerModel_impl.hpp:199
std::string wellsname(int tracerIdx) const Definition: GenericTracerModel_impl.hpp:192
std::string fname(int tracerIdx) const Definition: GenericTracerModel_impl.hpp:171
bool linearSolveReorder_(const TracerMatrix &M, std::vector< TracerVector > &x, std::vector< TracerVector > &b) Solve the tracer equations cell by cell in the order of the flow. Definition: GenericTracerModel_impl.hpp:510
bool linearSolve_(const TracerMatrix &M, TracerVector &x, TracerVector &b) Definition: GenericTracerModel_impl.hpp:391
Opm::BCRSMatrix< Opm::MatrixBlock< Scalar, 2, 2 > > TracerMatrix Definition: GenericTracerModel.hpp:59
void setFreeTracerConcentration(int tracerIdx, int globalDofIdx, Scalar value) Definition: GenericTracerModel_impl.hpp:141
Scalar solTracerConcentration(int tracerIdx, int globalDofIdx) const Definition: GenericTracerModel_impl.hpp:130
bool linearSolveBatchwise_(const TracerMatrix &M, std::vector< TracerVector > &x, std::vector< TracerVector > &b) Definition: GenericTracerModel_impl.hpp:446
Scalar freeTracerConcentration(int tracerIdx, int globalDofIdx) const Return the tracer concentration for tracer index and global DofIdx. Definition: GenericTracerModel_impl.hpp:119
Scalar currentConcentration_(const Well &eclWell, const std::string &trName, const SummaryState &summaryState) const Definition: GenericTracerModel_impl.hpp:213
Hierarchical collection of key/value pairs. Definition: PropertyTree.hpp:39
void put(const std::string &key, const T &data)
A data handle sending multiple data store in vectors attached to cells. Definition: VectorVectorDataHandle.hpp:52
Phase Phase indices for reservoir coupling, we currently only support black-oil phases (oil,... Definition: ReservoirCoupling.hpp:184
Definition: blackoilbioeffectsmodules.hh:45
Dune::InverseOperatorResult InverseOperatorResult Definition: GpuBridge.hpp:32
UpwindTransportStatistics solveReordered(const Matrix &matrix, const Vectors &rhs, Vectors &x, const UpwindTransportSettings &settings={}) Solve the block system A x_q = b_q described in the file comment. Definition: UpwindTransportSolver.hpp:716
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: GenericTracerModel_impl.hpp:70
Dune::FlexibleSolver< TracerOperator > type Definition: GenericTracerModel_impl.hpp:73
Dune::OverlappingSchwarzOperator< M, V, V, Comm > TracerOperator Definition: GenericTracerModel_impl.hpp:72
Dune::OwnerOverlapCopyCommunication< int, int > Comm Definition: GenericTracerModel_impl.hpp:71
|