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);
143 const std::size_t numCells = this->intensiveQuantityCache_[timeIdx].size();
144 std::vector<IntensiveQuantities*> outIqPtrs(numCells);
145 for (std::size_t i = 0; i < numCells; ++i) {
146 outIqPtrs[i] = &this->intensiveQuantityCache_[timeIdx][i];
148 gpuIntensiveQuantitiesDispatcher_->materializeHostIntensiveQuantities(
149 timeIdx, outIqPtrs.data(), numCells);
150 for (std::size_t i = 0; i < numCells; ++i) {
151 this->setIntensiveQuantitiesCacheEntryValidity(i, timeIdx,
true);
159#pragma omp parallel for
163 ElementContext elemCtx(this->simulator_);
164 for (
const auto& elem : chunk) {
165 elemCtx.updatePrimaryStencil(elem);
166 elemCtx.updatePrimaryIntensiveQuantities(timeIdx);
170 this->simulator_.vanguard().grid().comm());
173 ElementContext elemCtx(this->simulator_);
174 for (
const auto& elem : elements(this->gridView_)) {
175 elemCtx.updatePrimaryStencil(elem);
176 elemCtx.updatePrimaryIntensiveQuantities(timeIdx);
183 if (!this->enableIntensiveQuantityCache_) {
193 ElementContext elemCtx(this->simulator_);
196 if (elemIt->partitionType() != Dune::OverlapEntity) {
199 const Element& elem = *elemIt;
200 elemCtx.updatePrimaryStencil(elem);
202 const std::size_t numPrimaryDof = elemCtx.numPrimaryDof(timeIdx);
203 for (
unsigned dofIdx = 0; dofIdx < numPrimaryDof; ++dofIdx) {
204 const unsigned globalIndex = elemCtx.globalSpaceIndex(dofIdx, timeIdx);
205 this->setIntensiveQuantitiesCacheEntryValidity(globalIndex, timeIdx,
false);
208 elemCtx.updatePrimaryIntensiveQuantities(0);
212 this->simulator_.vanguard().grid().comm());
215 template <
class Gr
idSubDomain>
218 if (!this->enableIntensiveQuantityCache_) {
222 using GridViewType =
decltype(gridSubDomain.view);
228 ElementContext elemCtx(this->simulator_);
231 if (elemIt->partitionType() != Dune::InteriorEntity) {
234 const Element& elem = *elemIt;
235 elemCtx.updatePrimaryStencil(elem);
237 const std::size_t numPrimaryDof = elemCtx.numPrimaryDof(timeIdx);
238 for (
unsigned dofIdx = 0; dofIdx < numPrimaryDof; ++dofIdx) {
239 const unsigned globalIndex = elemCtx.globalSpaceIndex(dofIdx, timeIdx);
240 this->setIntensiveQuantitiesCacheEntryValidity(globalIndex, timeIdx,
false);
243 elemCtx.updatePrimaryIntensiveQuantities(0);
259 ParentType::updateFailed();
266 if (!this->enableIntensiveQuantityCache_) [[unlikely]] {
267 throwIntensiveQuantityError_(
true);
270 assert(timeIdx < this->cachedIntensiveQuantityHistorySize());
271 const auto* intquant = this->cachedIntensiveQuantities(globalIdx, timeIdx);
272 if (!intquant) [[unlikely]] {
273 throwIntensiveQuantityError_(
false);
280 [[noreturn]]
static void throwIntensiveQuantityError_(
bool cacheDisabled)
283 OPM_THROW(std::logic_error,
284 "Intensive quantity cache is disabled; "
285 "use --enable-intensive-quantity-cache=true");
287 OPM_THROW(std::logic_error,
"Intensive quantites need to be updated in code");
292 bool hasGpuPropertyAssemblyBridge()
const
294 if constexpr (Opm::gpuistl::GpuBlackoilIntensiveQuantitiesDispatcherSupport<TypeTag>::value) {
295 return useGpuIntensiveQuantitiesDispatcher_ && gpuIntensiveQuantitiesDispatcher_
296 && gpuIntensiveQuantitiesDispatcher_->hasDeviceModelView();
301 template <
class T = TypeTag>
302 std::enable_if_t<Opm::gpuistl::GpuBlackoilIntensiveQuantitiesDispatcherSupport<T>::value,
303 const typename Opm::gpuistl::GpuBlackoilIntensiveQuantitiesDispatcher<T>::Bridge&>
304 gpuPropertyAssemblyBridge()
const
306 if (!hasGpuPropertyAssemblyBridge()) {
307 OPM_THROW(std::logic_error,
"GPU property/assembly bridge has no valid device IQ model");
309 return gpuIntensiveQuantitiesDispatcher_->bridge();
315 ParentType::advanceTimeLevel();
317 if constexpr (Opm::gpuistl::GpuBlackoilIntensiveQuantitiesDispatcherSupport<TypeTag>::value) {
318 if (gpuIntensiveQuantitiesDispatcher_) {
319 gpuIntensiveQuantitiesDispatcher_->advanceTimeLevel();
327 template <EclMultiplexerApproach ApproachArg>
328 using EMD = EclMultiplexerDispatch<ApproachArg>;
333 switch (this->simulator_.problem().materialLawManager().threePhaseApproach()) {
334 case EclMultiplexerApproach::Stone1:
335 updateCachedIntQuants1<EclMultiplexerDispatch<EclMultiplexerApproach::Stone1>>(timeIdx);
338 case EclMultiplexerApproach::Stone2:
339 updateCachedIntQuants1<EMD<EclMultiplexerApproach::Stone2>>(timeIdx);
342 case EclMultiplexerApproach::Default:
343 updateCachedIntQuants1<EMD<EclMultiplexerApproach::Default>>(timeIdx);
346 case EclMultiplexerApproach::TwoPhase:
347 updateCachedIntQuants1<EMD<EclMultiplexerApproach::TwoPhase>>(timeIdx);
350 case EclMultiplexerApproach::OnePhase:
351 updateCachedIntQuants1<EMD<EclMultiplexerApproach::OnePhase>>(timeIdx);
356 template <
class EMDArg>
360 if (this->simulator_.problem().materialLawManager().satCurveIsAllPiecewiseLinear()) {
361 using PL = SatCurveMultiplexerDispatch<SatCurveMultiplexerApproach::PiecewiseLinear>;
362 updateCachedIntQuantsLoop<EMDArg, PL>(timeIdx);
365 updateCachedIntQuantsLoop<EMDArg>(timeIdx);
370 template <
class ...Args>
373 const auto& elementMapper = this->simulator_.model().elementMapper();
375#pragma omp parallel for
379 for (
const auto& elem : chunk) {
385 template <
class ...Args>
389 auto& intquant = this->intensiveQuantityCache_[timeIdx][globalIdx];
391 intquant.template update<Args...>(this->simulator_.problem(), this->solution(timeIdx)[globalIdx], globalIdx, timeIdx);
393 this->intensiveQuantityCacheUpToDate_[timeIdx][globalIdx] = 1;
399 bool useGpuIntensiveQuantitiesDispatcher_{
false};
400 using GpuDispatcherStorage = std::conditional_t<
401 Opm::gpuistl::GpuBlackoilIntensiveQuantitiesDispatcherSupport<TypeTag>::value,
402 std::unique_ptr<Opm::gpuistl::GpuBlackoilIntensiveQuantitiesDispatcher<TypeTag>>,
404 mutable GpuDispatcherStorage gpuIntensiveQuantitiesDispatcher_{};
406 void runGpuIntensiveQuantitiesDispatcher_(
const unsigned timeIdx)
const
408 if constexpr (Opm::gpuistl::GpuBlackoilIntensiveQuantitiesDispatcherSupport<TypeTag>::value) {
409 if (!gpuIntensiveQuantitiesDispatcher_) {
410 gpuIntensiveQuantitiesDispatcher_ =
412 Opm::gpuistl::GpuBlackoilIntensiveQuantitiesDispatcher<TypeTag>>();
414 const auto& sol = this->solution(timeIdx);
415 gpuIntensiveQuantitiesDispatcher_->update(this->simulator_.problem(), sol, timeIdx);
#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:371
void invalidateAndUpdateIntensiveQuantities(unsigned timeIdx, const GridSubDomain &gridSubDomain) const
Definition: FIBlackoilModel.hpp:216
void updateCachedIntQuants(const unsigned timeIdx) const
Definition: FIBlackoilModel.hpp:330
void updateSingleCachedIntQuantUnchecked(const unsigned globalIdx, const unsigned timeIdx) const
Definition: FIBlackoilModel.hpp:386
void advanceTimeLevel()
Definition: FIBlackoilModel.hpp:313
void invalidateAndUpdateIntensiveQuantitiesOverlap(unsigned timeIdx) const
Definition: FIBlackoilModel.hpp:181
void updateFailed()
Called by the update() method if it was unsuccessful. This is primary a hook which the actual model c...
Definition: FIBlackoilModel.hpp:253
const IntensiveQuantities & intensiveQuantities(unsigned globalIdx, unsigned timeIdx) const
Definition: FIBlackoilModel.hpp:264
void updateCachedIntQuants1(const unsigned timeIdx) const
Definition: FIBlackoilModel.hpp:357
EclMultiplexerDispatch< ApproachArg > EMD
Definition: FIBlackoilModel.hpp:328
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:396
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.