Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists_files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,7 @@ list (APPEND PUBLIC_HEADER_FILES
opm/models/ptflash/flashindices.hh
opm/models/ptflash/flashintensivequantities.hh
opm/models/ptflash/flashlocalresidual.hh
opm/models/ptflash/flashlocalresidualtpfa.hh
opm/models/ptflash/flashmodel.hh
opm/models/ptflash/flashnewtonmethod.hh
opm/models/ptflash/flashparameters.hh
Expand Down Expand Up @@ -1119,6 +1120,7 @@ list (APPEND PUBLIC_HEADER_FILES
opm/simulators/flow/FacePropertiesTPSA_impl.hpp
opm/simulators/flow/FemCpGridCompat.hpp
opm/simulators/flow/FIBlackoilModel.hpp
opm/simulators/flow/FICompositionalModel.hpp
opm/simulators/flow/GpuFlowProblem.hpp
opm/simulators/flow/SimpleFIBlackOilModel.hpp
opm/simulators/flow/FIPContainer.hpp
Expand Down
24 changes: 24 additions & 0 deletions flowexperimental/comp/flow_comp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
#include <opm/material/fluidsystems/GenericOilGasWaterFluidSystem.hpp>

#include <opm/models/discretization/common/baseauxiliarymodule.hh>
#include <opm/models/discretization/common/tpfalinearizer.hh>
#include <opm/models/nonlinear/newtonmethod.hh>
#include <opm/models/ptflash/flashlocalresidualtpfa.hh>
#include <opm/models/ptflash/flashmodel.hh>

#include <opm/simulators/flow/Main.hpp>
#include <opm/simulators/flow/FICompositionalModel.hpp>
#include <opm/simulators/flow/FlowProblemComp.hpp>
#include <opm/simulators/flow/FlowProblemCompProperties.hpp>
#include <opm/simulators/flow/NewTranFluxModule.hpp>
Expand Down Expand Up @@ -163,6 +166,27 @@ template<class TypeTag, int NumComp, bool EnableWater>
struct FluxModule<TypeTag, TTag::FlowCompProblem<NumComp, EnableWater>>
{ using type = NewTranFluxModule<TypeTag>; };

template<class TypeTag, int NumComp, bool EnableWater>
struct Model<TypeTag, TTag::FlowCompProblem<NumComp, EnableWater>>
{ using type = FICompositionalModel<TypeTag>; };

template<class TypeTag, int NumComp, bool EnableWater>
struct Linearizer<TypeTag, TTag::FlowCompProblem<NumComp, EnableWater>>
{ using type = TpfaLinearizer<TypeTag>; };

template<class TypeTag, int NumComp, bool EnableWater>
struct LocalResidual<TypeTag, TTag::FlowCompProblem<NumComp, EnableWater>>
{ using type = FlashLocalResidualTPFA<TypeTag>; };

//! The TPFA linearizer assembles total rates, not rates per unit volume
template<class TypeTag, int NumComp, bool EnableWater>
struct UseVolumetricResidual<TypeTag, TTag::FlowCompProblem<NumComp, EnableWater>>
{ static constexpr bool value = false; };

template<class TypeTag, int NumComp, bool EnableWater>
struct RunAssemblyOnGpu<TypeTag, TTag::FlowCompProblem<NumComp, EnableWater>>
{ static constexpr bool value = false; };

template <class TypeTag, int NumComp, bool EnableWater>
struct FlashSolver<TypeTag, TTag::FlowCompProblem<NumComp, EnableWater>> {
private:
Expand Down
2 changes: 2 additions & 0 deletions flowexperimental/comp/wells/CompWellInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class CompWellInterface

const std::vector<std::size_t>& cells() const { return well_cells_; }

const std::vector<RateVector>& connectionRates() const { return connectionRates_; }

virtual void apply(BVector& r) const = 0;

/// Ax = Ax - C^T D^-1 B x, the well part of the reservoir operator
Expand Down
6 changes: 6 additions & 0 deletions flowexperimental/comp/wells/CompWellModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ class CompWellModel : public WellConnectionAuxiliaryModule<TypeTag, CompWellMode
void endEpisode() {}

void computeTotalRatesForDof(RateVector& /*rate*/, unsigned /*globalIdx*/) const;

// Adds the well sources to the residual and the diagonal Jacobian blocks in
// one pass, which the TPFA linearizer does with --separate-sparse-source-terms.
void addReservoirSourceTerms(GlobalEqVector& residual,
const std::vector<typename SparseMatrixAdapter::MatrixBlock*>&
diagMatAddress) const;
//
[[nodiscard]] data::Wells wellData() const;

Expand Down
35 changes: 26 additions & 9 deletions flowexperimental/comp/wells/CompWellModel_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,15 +374,7 @@ void
CompWellModel<TypeTag>::
beginIteration()
{
// do we need to do every iteration here?
const auto& grid = simulator_.vanguard().grid();
const auto& gridView = grid.leafGridView();
ElementContext elemCtx(simulator_);
for (const auto& elem : elements(gridView, Dune::Partitions::interior)) {
elemCtx.updatePrimaryStencil(elem);
elemCtx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
}

// The model has updated the cached intensive quantities, which the wells read.
assemble(simulator_.timeStepSize());
}

Expand Down Expand Up @@ -429,6 +421,31 @@ computeTotalRatesForDof(RateVector& rate,
}
}

template <typename TypeTag>
void
CompWellModel<TypeTag>::
addReservoirSourceTerms(GlobalEqVector& residual,
const std::vector<typename SparseMatrixAdapter::MatrixBlock*>&
diagMatAddress) const
{
// Not parallelised with OpenMP: a cell perforated by several wells would be
// written by more than one thread.
for (const auto& well : well_container_) {
const auto& cells = well->cells();
const auto& rates = well->connectionRates();
for (std::size_t con = 0; con < rates.size(); ++con) {
const auto cellIdx = cells[con];
auto rate = rates[con];
rate *= -1.0;
VectorBlockType res(0.0);
typename SparseMatrixAdapter::MatrixBlock bMat(0.0);
simulator_.model().linearizer().setResAndJacobi(res, bMat, rate);
residual[cellIdx] += res;
*diagMatAddress[cellIdx] += bMat;
}
}
}

template<typename TypeTag>
void
CompWellModel<TypeTag>::
Expand Down
2 changes: 1 addition & 1 deletion opm/models/discretization/common/tpfalinearizer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ private:
const auto& exFluidState = problem_().boundaryFluidState(myIdx, dir_id);
BoundaryConditionDataCPU bcdata {type,
massrate,
exFluidState.pvtRegionIndex(),
problem_().pvtRegionIndex(myIdx),
bfIndex,
bf.area(),
bf.integrationPos()[dimWorld - 1],
Expand Down
138 changes: 99 additions & 39 deletions opm/models/ptflash/flashintensivequantities.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <opm/input/eclipse/EclipseState/Grid/FaceDir.hpp>

#include <opm/material/Constants.hpp>
#include <opm/material/common/MathToolbox.hpp>
#include <opm/material/common/Valgrind.hpp>
#include <opm/material/constraintsolvers/PTFlashMethod.hpp>
#include <opm/material/fluidstates/CompositionalFluidState.hpp>
Expand Down Expand Up @@ -96,6 +97,8 @@ class FlashIntensiveQuantities
using Evaluation = GetPropType<TypeTag, Properties::Evaluation>;
using FluidSystem = GetPropType<TypeTag, Properties::FluidSystem>;
using FlashSolver = GetPropType<TypeTag, Properties::FlashSolver>;
using ParameterCache = typename FluidSystem::template ParameterCache<Evaluation>;
using Toolbox = MathToolbox<Evaluation>;

using ComponentVector = Dune::FieldVector<Evaluation, numComponents>;
using DimMatrix = Dune::FieldMatrix<Scalar, dimWorld, dimWorld>;
Expand All @@ -107,6 +110,8 @@ class FlashIntensiveQuantities
public:
//! The type of the object returned by the fluidState() method
using FluidState = CompositionalFluidState<Evaluation, FluidSystem, enableEnergy>;
//! Fluid state without derivatives, e.g. for boundary conditions
using ScalarFluidState = CompositionalFluidState<Scalar, FluidSystem, enableEnergy>;

/// Minimum overall mole fraction of a component; the flash needs every
/// component present.
Expand Down Expand Up @@ -134,6 +139,82 @@ public:
const auto& priVars = elemCtx.primaryVars(dofIdx, timeIdx);
const auto& problem = elemCtx.problem();

// Get initial K and L from storage initially (if enabled), else from the
// previous time step
const auto* hint = elemCtx.thermodynamicHint(dofIdx, timeIdx);
if (!hint && timeIdx == 0) {
hint = elemCtx.thermodynamicHint(dofIdx, 1);
}

ParameterCache paramCache(problem.getEosType());
updatePhaseProperties_(problem, priVars, elemCtx.globalSpaceIndex(dofIdx, timeIdx), timeIdx,
hint, problem.pvtRegionIndex(elemCtx, dofIdx, timeIdx),
problem.materialLawParams(elemCtx, dofIdx, timeIdx), paramCache);

const Scalar rockCompressibility = problem.rockCompressibility(elemCtx, dofIdx, timeIdx);
updatePorosity_(priVars, timeIdx, problem.porosity(elemCtx, dofIdx, timeIdx),
rockCompressibility,
rockCompressibility > 0.0
? problem.rockReferencePressure(elemCtx, dofIdx, timeIdx) : 0.0);

// intrinsic permeability
intrinsicPerm_ = problem.intrinsicPermeability(elemCtx, dofIdx, timeIdx);

// update the quantities specific for the velocity model
FluxIntensiveQuantities::update_(elemCtx, dofIdx, timeIdx);

// energy related quantities
EnergyIntensiveQuantities::update_(fluidState_, paramCache, elemCtx, dofIdx, timeIdx);

// update the diffusion specific quantities of the intensive quantities
DiffusionIntensiveQuantities::update_(fluidState_, paramCache, elemCtx, dofIdx, timeIdx);
}

/*!
* \brief Update the quantities of a cell from its primary variables, without
* an element context.
*/
template <class Problem, class PrimaryVariables>
void update(const Problem& problem,
const PrimaryVariables& priVars,
unsigned globalIdx,
unsigned timeIdx)
{
// the energy and diffusion modules need an element context
static_assert(!enableEnergy);
static_assert(!enableDiffusion);

this->extrusionFactor_ = problem.extrusionFactor();
const Scalar T = problem.temperature(globalIdx, timeIdx);
fluidState_.setTemperature(Toolbox::createConstant(T));

// No fallback to the previous time step as in the element-context update:
// the element-context cache refresh never provided one.
ParameterCache paramCache(problem.getEosType());
updatePhaseProperties_(problem, priVars, globalIdx, timeIdx,
problem.model().thermodynamicHint(globalIdx, timeIdx),
problem.pvtRegionIndex(globalIdx),
problem.materialLawParams(globalIdx), paramCache);

const Scalar rockCompressibility = problem.rockCompressibility(globalIdx);
updatePorosity_(priVars, timeIdx, problem.porosity(globalIdx, timeIdx), rockCompressibility,
rockCompressibility > 0.0 ? problem.rockReferencePressure(globalIdx) : 0.0);

intrinsicPerm_ = problem.intrinsicPermeability(globalIdx);
}

private:
// The flash and the phase properties, shared by both update() overloads
template <class Problem, class PrimaryVariables, class IntensiveQuantities>
void updatePhaseProperties_(const Problem& problem,
const PrimaryVariables& priVars,
unsigned globalIdx,
unsigned timeIdx,
const IntensiveQuantities* hint,
unsigned pvtRegionIdx,
const MaterialLawParams& materialParams,
ParameterCache& paramCache)
{
const Scalar flashTolerance = Parameters::Get<Parameters::FlashTolerance<Scalar>>();
const int flashVerbosity = Parameters::Get<Parameters::FlashVerbosity>();
const auto ptFlashMethod =
Expand Down Expand Up @@ -171,8 +252,6 @@ public:
fluidState_.setPressure(phaseIdx, p);
}

// Get initial K and L from storage initially (if enabled)
const auto* hint = elemCtx.thermodynamicHint(dofIdx, timeIdx);
if (hint) {
for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
const Evaluation& Ktmp = hint->fluidState().K(compIdx);
Expand All @@ -181,16 +260,6 @@ public:
const Evaluation& Ltmp = hint->fluidState().L();
fluidState_.setLvalue(Ltmp);
}
else if (timeIdx == 0 && elemCtx.thermodynamicHint(dofIdx, 1)) {
// checking the storage cache
const auto& hint2 = elemCtx.thermodynamicHint(dofIdx, 1);
for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
const Evaluation& Ktmp = hint2->fluidState().K(compIdx);
fluidState_.setKvalue(compIdx, Ktmp);
}
const Evaluation& Ltmp = hint2->fluidState().L();
fluidState_.setLvalue(Ltmp);
}
else {
for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
const Evaluation Ktmp = fluidState_.wilsonK_(compIdx);
Expand All @@ -205,7 +274,7 @@ public:
/////////////
if (flashVerbosity >= 1) {
OpmLog::debug(fmt::format("Updating the intensive quantities for cell {}",
elemCtx.globalSpaceIndex(dofIdx, timeIdx)));
globalIdx));
}
const auto& eos_type = problem.getEosType();
FlashSolver::solve(fluidState_, ptFlashMethod, flashTolerance, eos_type, flashVerbosity);
Expand All @@ -221,15 +290,14 @@ public:
getValue(fluidState_.moleFraction(FluidSystem::gasPhaseIdx, compIdx)));
}
OpmLog::debug(fmt::format("After the flash for cell {}: liquid fraction = {}\n{}",
elemCtx.globalSpaceIndex(dofIdx, timeIdx),
globalIdx,
getValue(fluidState_.L()),
phaseCompositions));
}

// Update phases
typename FluidSystem::template ParameterCache<Evaluation> paramCache(eos_type);
// the water properties of the cell's PVT region
paramCache.setPvtRegionIndex(problem.pvtRegionIndex(elemCtx, dofIdx, timeIdx));
paramCache.setPvtRegionIndex(pvtRegionIdx);
paramCache.updatePhase(fluidState_, FluidSystem::oilPhaseIdx);
paramCache.updatePhase(fluidState_, FluidSystem::gasPhaseIdx);

Expand Down Expand Up @@ -285,7 +353,7 @@ public:
OpmLog::debug(fmt::format("Flash phase properties for cell {}: "
"oil saturation = {}, gas saturation = {}, "
"oil molar volume = {}, gas molar volume = {}",
elemCtx.globalSpaceIndex(dofIdx, timeIdx),
globalIdx,
getValue(So),
getValue(Sg),
getValue(Vm_L),
Expand All @@ -295,7 +363,6 @@ public:
/////////////
// Compute rel. perm and viscosity and densities
/////////////
const MaterialLawParams& materialParams = problem.materialLawParams(elemCtx, dofIdx, timeIdx);

// calculate relative permeability
MaterialLaw::relativePermeabilities(relativePermeability_,
Expand All @@ -320,35 +387,28 @@ public:
const Evaluation& rho = FluidSystem::density(fluidState_, paramCache, phaseIdx);
fluidState_.setDensity(phaseIdx, rho);
}
}

/////////////
// Compute the remaining quantities
/////////////

// porosity, scaled with the pressure by the rock compressibility of the
// cell's region as in the black-oil model
porosity_ = problem.porosity(elemCtx, dofIdx, timeIdx);
const Scalar rockCompressibility = problem.rockCompressibility(elemCtx, dofIdx, timeIdx);
// porosity, scaled with the pressure by the rock compressibility of the
// cell's region as in the black-oil model
template <class PrimaryVariables>
void updatePorosity_(const PrimaryVariables& priVars,
unsigned timeIdx,
const Evaluation& referencePorosity,
Scalar rockCompressibility,
Scalar rockRefPressure)
{
porosity_ = referencePorosity;
if (rockCompressibility > 0.0) {
const Scalar rockRefPressure = problem.rockReferencePressure(elemCtx, dofIdx, timeIdx);
const Evaluation p = priVars.makeEvaluation(pressure0Idx, timeIdx);
const Evaluation x = rockCompressibility * (p - rockRefPressure);
porosity_ *= 1.0 + x + 0.5 * x * x;
}
Valgrind::CheckDefined(porosity_);

// intrinsic permeability
intrinsicPerm_ = problem.intrinsicPermeability(elemCtx, dofIdx, timeIdx);

// update the quantities specific for the velocity model
FluxIntensiveQuantities::update_(elemCtx, dofIdx, timeIdx);

// energy related quantities
EnergyIntensiveQuantities::update_(fluidState_, paramCache, elemCtx, dofIdx, timeIdx);

// update the diffusion specific quantities of the intensive quantities
DiffusionIntensiveQuantities::update_(fluidState_, paramCache, elemCtx, dofIdx, timeIdx);
}

public:

/*!
* \copydoc ImmiscibleIntensiveQuantities::fluidState
*/
Expand Down
Loading