28#ifndef FI_BLACK_OIL_MODEL_HPP
29#define FI_BLACK_OIL_MODEL_HPP
34#include <opm/common/ErrorMacros.hpp>
37#include <opm/grid/CpGrid.hpp>
38#include <opm/grid/utility/ElementChunks.hpp>
43#include <opm/material/fluidmatrixinteractions/EclMultiplexerMaterialParams.hpp>
60template <
typename TypeTag>
69 using Element =
typename GridView::template Codim<0>::Entity;
70 using ElementIterator =
typename GridView::template Codim<0>::Iterator;
72 numEq = getPropValue<TypeTag, Properties::NumEq>(),
73 historySize = getPropValue<TypeTag, Properties::TimeDiscHistorySize>(),
79 static constexpr bool gridIsUnchanging =
80 std::is_same_v<GetPropType<TypeTag, Properties::Grid>, Dune::CpGrid>;
82 static constexpr bool avoidElementContext = getPropValue<TypeTag, Properties::AvoidElementContext>();
88 Dune::Partitions::all,
89 ThreadManager::maxThreads())
92 if constexpr (gpuistl::GpuBlackoilIntensiveQuantitiesDispatcherSupport<TypeTag>::value) {
93 useGpuIntensiveQuantitiesDispatcher_ =
94 Parameters::Get<Parameters::ExperimentalComputePropertiesOnGpu>();
100 using Result =
decltype(std::declval<const Model&>().intensiveQuantities(0u, 0u));
101 if (std::is_reference_v<Result> && !this->enableIntensiveQuantityCache_) {
102 OPM_THROW(std::invalid_argument,
103 "--enable-intensive-quantity-cache=false is not supported "
104 "by this simulator.");
112 if constexpr (gpuistl::GpuBlackoilIntensiveQuantitiesDispatcherSupport<TypeTag>::value) {
113 Parameters::Register<Parameters::ExperimentalComputePropertiesOnGpu>
114 (
"Experimental: compute BlackOilIntensiveQuantities on the GPU "
115 "via the GpuBlackoilIntensiveQuantitiesDispatcher.");
122 if (!this->enableIntensiveQuantityCache_) {
125 this->invalidateIntensiveQuantitiesCache(timeIdx);
126 if constexpr (gridIsUnchanging) {
127 if constexpr (avoidElementContext) {
132 if constexpr (Opm::gpuistl::GpuBlackoilIntensiveQuantitiesDispatcherSupport<TypeTag>::value)
134 if (useGpuIntensiveQuantitiesDispatcher_) {
135 if constexpr (getPropValue<TypeTag, Properties::EnableDiffusion>()
136 || getPropValue<TypeTag, Properties::EnableDispersion>()) {
137 OPM_THROW(std::logic_error,
138 "GPU intensive quantities dispatcher does not support diffusion or dispersion");
140 runGpuIntensiveQuantitiesDispatcher_(timeIdx);
141 const std::size_t numCells = this->intensiveQuantityCache_[timeIdx].size();
142 for (std::size_t i = 0; i < numCells; ++i) {
143 this->setIntensiveQuantitiesCacheEntryValidity(i, timeIdx,
true);
151#pragma omp parallel for
154 ElementContext elemCtx(this->simulator_);
155 for (
const auto& elem : chunk) {
156 elemCtx.updatePrimaryStencil(elem);
157 elemCtx.updatePrimaryIntensiveQuantities(timeIdx);
161 this->simulator_.vanguard().grid().comm());
164 ElementContext elemCtx(this->simulator_);
165 for (
const auto& elem : elements(this->gridView_)) {
166 elemCtx.updatePrimaryStencil(elem);
167 elemCtx.updatePrimaryIntensiveQuantities(timeIdx);
174 if (!this->enableIntensiveQuantityCache_) {
184 ElementContext elemCtx(this->simulator_);
187 if (elemIt->partitionType() != Dune::OverlapEntity) {
190 const Element& elem = *elemIt;
191 elemCtx.updatePrimaryStencil(elem);
193 const std::size_t numPrimaryDof = elemCtx.numPrimaryDof(timeIdx);
194 for (
unsigned dofIdx = 0; dofIdx < numPrimaryDof; ++dofIdx) {
195 const unsigned globalIndex = elemCtx.globalSpaceIndex(dofIdx, timeIdx);
196 this->setIntensiveQuantitiesCacheEntryValidity(globalIndex, timeIdx,
false);
199 elemCtx.updatePrimaryIntensiveQuantities(0);
203 this->simulator_.vanguard().grid().comm());
206 template <
class Gr
idSubDomain>
209 if (!this->enableIntensiveQuantityCache_) {
213 using GridViewType =
decltype(gridSubDomain.view);
219 ElementContext elemCtx(this->simulator_);
222 if (elemIt->partitionType() != Dune::InteriorEntity) {
225 const Element& elem = *elemIt;
226 elemCtx.updatePrimaryStencil(elem);
228 const std::size_t numPrimaryDof = elemCtx.numPrimaryDof(timeIdx);
229 for (
unsigned dofIdx = 0; dofIdx < numPrimaryDof; ++dofIdx) {
230 const unsigned globalIndex = elemCtx.globalSpaceIndex(dofIdx, timeIdx);
231 this->setIntensiveQuantitiesCacheEntryValidity(globalIndex, timeIdx,
false);
234 elemCtx.updatePrimaryIntensiveQuantities(0);
250 ParentType::updateFailed();
257 if (!this->enableIntensiveQuantityCache_) [[unlikely]] {
258 throwIntensiveQuantityError_(
true);
261 assert(timeIdx < this->cachedIntensiveQuantityHistorySize());
262 const auto* intquant = this->cachedIntensiveQuantities(globalIdx, timeIdx);
263 if (!intquant) [[unlikely]] {
264 throwIntensiveQuantityError_(
false);
271 [[noreturn]]
static void throwIntensiveQuantityError_(
bool cacheDisabled)
274 OPM_THROW(std::logic_error,
275 "Intensive quantity cache is disabled; "
276 "use --enable-intensive-quantity-cache=true");
278 OPM_THROW(std::logic_error,
"Intensive quantites need to be updated in code");
285 template <EclMultiplexerApproach ApproachArg>
286 using EMD = EclMultiplexerDispatch<ApproachArg>;
291 switch (this->simulator_.problem().materialLawManager().threePhaseApproach()) {
292 case EclMultiplexerApproach::Stone1:
293 updateCachedIntQuants1<EclMultiplexerDispatch<EclMultiplexerApproach::Stone1>>(timeIdx);
296 case EclMultiplexerApproach::Stone2:
297 updateCachedIntQuants1<EMD<EclMultiplexerApproach::Stone2>>(timeIdx);
300 case EclMultiplexerApproach::Default:
301 updateCachedIntQuants1<EMD<EclMultiplexerApproach::Default>>(timeIdx);
304 case EclMultiplexerApproach::TwoPhase:
305 updateCachedIntQuants1<EMD<EclMultiplexerApproach::TwoPhase>>(timeIdx);
308 case EclMultiplexerApproach::OnePhase:
309 updateCachedIntQuants1<EMD<EclMultiplexerApproach::OnePhase>>(timeIdx);
314 template <
class EMDArg>
318 if (this->simulator_.problem().materialLawManager().satCurveIsAllPiecewiseLinear()) {
319 using PL = SatCurveMultiplexerDispatch<SatCurveMultiplexerApproach::PiecewiseLinear>;
320 updateCachedIntQuantsLoop<EMDArg, PL>(timeIdx);
323 updateCachedIntQuantsLoop<EMDArg>(timeIdx);
328 template <
class ...Args>
331 const auto& elementMapper = this->simulator_.model().elementMapper();
333#pragma omp parallel for
336 for (
const auto& elem : chunk) {
342 template <
class ...Args>
346 auto& intquant = this->intensiveQuantityCache_[timeIdx][globalIdx];
348 intquant.template update<Args...>(this->simulator_.problem(), this->solution(timeIdx)[globalIdx], globalIdx, timeIdx);
350 this->intensiveQuantityCacheUpToDate_[timeIdx][globalIdx] = 1;
356 bool useGpuIntensiveQuantitiesDispatcher_{
false};
357 using GpuDispatcherStorage = std::conditional_t<
358 Opm::gpuistl::GpuBlackoilIntensiveQuantitiesDispatcherSupport<TypeTag>::value,
359 std::unique_ptr<Opm::gpuistl::GpuBlackoilIntensiveQuantitiesDispatcher<TypeTag>>,
361 mutable GpuDispatcherStorage gpuIntensiveQuantitiesDispatcher_{};
363 void runGpuIntensiveQuantitiesDispatcher_(
const unsigned timeIdx)
const
365 if constexpr (Opm::gpuistl::GpuBlackoilIntensiveQuantitiesDispatcherSupport<TypeTag>::value) {
366 if (!gpuIntensiveQuantitiesDispatcher_) {
367 gpuIntensiveQuantitiesDispatcher_ =
369 Opm::gpuistl::GpuBlackoilIntensiveQuantitiesDispatcher<TypeTag>>();
371 using PV = GetPropType<TypeTag, Properties::PrimaryVariables>;
372 const auto& sol = this->solution(timeIdx);
373 const std::size_t numCells = this->intensiveQuantityCache_[timeIdx].size();
374 std::vector<const PV*> priVarsPtrs(numCells);
375 std::vector<IntensiveQuantities*> outIqPtrs(numCells);
376 for (std::size_t i = 0; i < numCells; ++i) {
377 priVarsPtrs[i] = &sol[i];
378 outIqPtrs[i] = &this->intensiveQuantityCache_[timeIdx][i];
380 gpuIntensiveQuantitiesDispatcher_->update(
381 this->simulator_.problem(),
#define OPM_END_PARALLEL_TRY_CATCH(prefix, comm)
Catch exception and throw in a parallel try-catch clause.
Definition: DeferredLoggingErrorHelpers.hpp:197
#define OPM_BEGIN_PARALLEL_TRY_CATCH()
Macro to setup the try of a parallel try-catch.
Definition: DeferredLoggingErrorHelpers.hpp:160
A fully-implicit black-oil flow model.
Definition: blackoilmodel.hh:329
static void registerParameters()
Register all run-time parameters for the immiscible model.
Definition: blackoilmodel.hh:379
Definition: FIBlackoilModel.hpp:62
static void registerParameters()
Definition: FIBlackoilModel.hpp:108
void updateCachedIntQuantsLoop(const unsigned timeIdx) const
Definition: FIBlackoilModel.hpp:329
void invalidateAndUpdateIntensiveQuantities(unsigned timeIdx, const GridSubDomain &gridSubDomain) const
Definition: FIBlackoilModel.hpp:207
void updateCachedIntQuants(const unsigned timeIdx) const
Definition: FIBlackoilModel.hpp:288
void updateSingleCachedIntQuantUnchecked(const unsigned globalIdx, const unsigned timeIdx) const
Definition: FIBlackoilModel.hpp:343
void invalidateAndUpdateIntensiveQuantitiesOverlap(unsigned timeIdx) const
Definition: FIBlackoilModel.hpp:172
void updateFailed()
Called by the update() method if it was unsuccessful. This is primary a hook which the actual model c...
Definition: FIBlackoilModel.hpp:244
const IntensiveQuantities & intensiveQuantities(unsigned globalIdx, unsigned timeIdx) const
Definition: FIBlackoilModel.hpp:255
void updateCachedIntQuants1(const unsigned timeIdx) const
Definition: FIBlackoilModel.hpp:315
EclMultiplexerDispatch< ApproachArg > EMD
Definition: FIBlackoilModel.hpp:286
void invalidateAndUpdateIntensiveQuantities(unsigned timeIdx) const
Definition: FIBlackoilModel.hpp:120
FIBlackOilModel(Simulator &simulator)
Definition: FIBlackoilModel.hpp:85
ElementChunks< GridView, Dune::Partitions::All > element_chunks_
Definition: FIBlackoilModel.hpp:353
A base class for fully-implicit multi-phase porous-media flow models which assume multiple fluid phas...
Definition: multiphasebasemodel.hh:161
Provides an STL-iterator like interface to iterate over the enties of a GridView in OpenMP threaded a...
Definition: threadedentityiterator.hh:42
bool isFinished(const EntityIterator &it) const
Definition: threadedentityiterator.hh:67
EntityIterator increment()
Definition: threadedentityiterator.hh:80
EntityIterator beginParallel()
Definition: threadedentityiterator.hh:54
Definition: fvbaseprimaryvariables.hh:161
Definition: blackoilbioeffectsmodules.hh:45
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
The Opm property system, traits with inheritance.