28#ifndef OPM_TEMPERATURE_MODEL_HPP
29#define OPM_TEMPERATURE_MODEL_HPP
31#include <opm/common/OpmLog/OpmLog.hpp>
50template<
class TypeTag,
class MyTypeTag>
59template<
typename Scalar,
typename IndexTraits>
class WellState;
66template <class TypeTag, bool enableTempV = getPropValue<TypeTag, Properties::EnergyModuleType>() == EnergyModules::SequentialImplicitThermal >
68 GetPropType<TypeTag, Properties::GridView>,
69 GetPropType<TypeTag, Properties::DofMapper>,
70 GetPropType<TypeTag, Properties::Stencil>,
71 GetPropType<TypeTag, Properties::FluidSystem>,
72 GetPropType<TypeTag, Properties::Scalar>>
89 using TemperatureEvaluation = DenseAd::Evaluation<Scalar,1>;
94 using IndexTraits =
typename FluidSystem::IndexTraitsType;
100 enum { numEq = getPropValue<TypeTag, Properties::NumEq>() };
101 enum { numPhases = FluidSystem::numPhases };
102 enum { waterPhaseIdx = FluidSystem::waterPhaseIdx };
103 enum { oilPhaseIdx = FluidSystem::oilPhaseIdx };
104 enum { gasPhaseIdx = FluidSystem::gasPhaseIdx };
108 :
BaseType(simulator.vanguard().gridView(),
109 simulator.vanguard().eclState(),
110 simulator.vanguard().cartesianIndexMapper(),
111 simulator.model().dofMapper())
117 const unsigned int numCells =
simulator_.model().numTotalDof();
127 for (
unsigned globI = 0; globI < numCells; ++globI) {
135 const auto& elemMapper =
simulator_.model().elementMapper();
146 const unsigned int numCells =
simulator_.model().numTotalDof();
147 for (
unsigned globI = 0; globI < numCells; ++globI) {
154 const int nw =
simulator_.problem().wellModel().wellState().numWells();
168 const unsigned int numCells =
simulator_.model().numTotalDof();
169 for (
unsigned globI = 0; globI < numCells; ++globI) {
177 const int nw = wellState.
numWells();
178 for (
auto wellID = 0*nw; wellID < nw; ++wellID) {
179 auto& ws = wellState.
well(wellID);
188 template <
class Restarter>
198 template <
class Restarter>
206 const unsigned int numCells =
simulator_.model().numTotalDof();
208 #pragma omp parallel for
210 for (
unsigned globI = 0; globI < numCells; ++globI) {
211 Scalar storage = 0.0;
219 const int max_iter = 20;
220 const int min_iter = 1;
221 bool is_converged =
false;
223 for (
int iter = 0; iter < max_iter; ++iter) {
225 if (iter >= min_iter &&
converged(iter)) {
232 const auto msg = fmt::format(
"Temperature model (TEMP): Newton did not converge after {} iterations. \n"
233 "The Simulator will continue to the next step with an unconverged solution.",max_iter);
240 const unsigned int numCells =
simulator_.model().numTotalDof();
241 EnergyVector dx(numCells);
244 if (
simulator_.gridView().comm().rank() == 0) {
245 OpmLog::warning(
"Temp model: Linear solver did not converge. Temperature values not updated.");
249 for (
unsigned globI = 0; globI < numCells; ++globI) {
260 Scalar maxNorm = 0.0;
261 Scalar sumNorm = 0.0;
262 const auto tolerance_cnv_energy_strict = Parameters::Get<Parameters::ToleranceCnvEnergy<Scalar>>();
263 const auto& elemMapper =
simulator_.model().elementMapper();
266 Scalar sum_pv_not_converged = 0.0;
267 for (
const auto& elem : elements(
simulator_.gridView(), Dune::Partitions::interior)) {
268 unsigned globI = elemMapper.index(elem);
269 const auto pvValue =
simulator_.problem().referencePorosity(globI, 0)
272 const Scalar scaled_norm = dt * std::abs(this->
energyVector_[globI])/ pvValue;
273 maxNorm = max(maxNorm, scaled_norm);
274 sumNorm += scaled_norm;
275 if (!isNumericalAquiferCell(elem)) {
276 if (scaled_norm > tolerance_cnv_energy_strict) {
277 sum_pv_not_converged += pvValue;
282 maxNorm =
simulator_.gridView().comm().max(maxNorm);
283 sumNorm =
simulator_.gridView().comm().sum(sumNorm);
284 sum_pv =
simulator_.gridView().comm().sum(sum_pv);
288 sum_pv_not_converged =
simulator_.gridView().comm().sum(sum_pv_not_converged);
289 Scalar relaxed_max_pv_fraction = Parameters::Get<Parameters::RelaxedMaxPvFraction<Scalar>>();
290 const bool relax = (sum_pv_not_converged / sum_pv) < relaxed_max_pv_fraction;
291 const auto tolerance_energy_balance = relax? Parameters::Get<Parameters::ToleranceEnergyBalanceRelaxed<Scalar>>():
293 const bool tolerance_cnv_energy = relax? Parameters::Get<Parameters::ToleranceCnvEnergyRelaxed<Scalar>>():
294 tolerance_cnv_energy_strict;
296 const auto msg = fmt::format(
"Temperature model (TEMP): Newton iter {}: "
297 "CNV(E): {:.1e}, EB: {:.1e}",
298 iter, maxNorm, sumNorm);
300 if (maxNorm < tolerance_cnv_energy && sumNorm < tolerance_energy_balance) {
301 const auto msg2 = fmt::format(
"Temperature model (TEMP): Newton converged after {} iterations"
309 template<
class LhsEval>
313 const auto& poro = decay<LhsEval>(intQuants.porosity());
315 const auto& fs = intQuants.fluidState();
316 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++ phaseIdx) {
317 if (!FluidSystem::phaseIsActive(phaseIdx)) {
321 const auto& u = decay<LhsEval>(fs.internalEnergy(phaseIdx));
322 const auto& S = decay<LhsEval>(fs.saturation(phaseIdx));
323 const auto& rho = decay<LhsEval>(fs.density(phaseIdx));
325 storage += poro*S*u*rho;
329 const Scalar rockFraction = intQuants.rockFraction();
330 const auto& uRock = decay<LhsEval>(intQuants.rockInternalEnergy());
331 storage += rockFraction*uRock;
332 storage*= getPropValue<TypeTag, Properties::BlackOilEnergyScalingFactor>();
335 template <
class Res
idualNBInfo>
337 const ResidualNBInfo& res_nbinfo,
340 const IntensiveQuantities& intQuantsIn =
intQuants_[globI];
341 const IntensiveQuantities& intQuantsEx =
intQuants_[globJ];
343 RateVector darcyFlux(0.0);
344 LocalResidual::computeFlux(tmp, darcyFlux, globI, globJ, intQuantsIn, intQuantsEx,
345 res_nbinfo,
simulator_.problem().moduleParams());
346 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++ phaseIdx) {
347 if (!FluidSystem::phaseIsActive(phaseIdx)) {
351 const unsigned activeCompIdx =
352 FluidSystem::canonicalToActiveCompIdx(FluidSystem::solventComponentIndex(phaseIdx));
354 bool inIsUp = darcyFlux[activeCompIdx] > 0;
355 const IntensiveQuantities& up = inIsUp ? intQuantsIn : intQuantsEx;
356 const auto& fs = up.fluidState();
358 flux += fs.enthalpy(phaseIdx)
359 * fs.density(phaseIdx)
360 * darcyFlux[activeCompIdx];
363 flux += getValue(fs.enthalpy(phaseIdx))
364 * getValue(fs.density(phaseIdx))
365 * getValue(darcyFlux[activeCompIdx]);
368 flux *= getPropValue<TypeTag, Properties::BlackOilEnergyScalingFactor>();
371 template <
class Res
idualNBInfo>
373 const ResidualNBInfo& res_nbinfo,
374 Evaluation& heatFlux)
376 const IntensiveQuantities& intQuantsIn =
intQuants_[globI];
377 const IntensiveQuantities& intQuantsEx =
intQuants_[globJ];
378 const Scalar inAlpha =
simulator_.problem().thermalHalfTransmissibility(globI, globJ);
379 const Scalar outAlpha =
simulator_.problem().thermalHalfTransmissibility(globJ, globI);
380 short interiorDofIdx = 0;
381 short exteriorDofIdx = 1;
382 EnergyModule::ExtensiveQuantities::updateEnergy(heatFlux,
388 intQuantsIn.fluidState(),
389 intQuantsEx.fluidState(),
392 res_nbinfo.faceArea);
393 heatFlux *= getPropValue<TypeTag, Properties::BlackOilEnergyScalingFactor>()*res_nbinfo.faceArea;
401 const unsigned int numCells =
simulator_.model().numTotalDof();
403#pragma omp parallel for
405 for (
unsigned globI = 0; globI < numCells; ++globI) {
406 Scalar volume =
simulator_.model().dofTotalVolume(globI);
407 Scalar storefac = volume / dt;
408 Evaluation storage = 0.0;
411 (*this->
energyMatrix_)[globI][globI][0][0] += storefac * storage.derivative(Indices::temperatureIdx);
414 const auto& neighborInfo =
simulator_.model().linearizer().getNeighborInfo();
416#pragma omp parallel for
418 for (
unsigned globI = 0; globI < numCells; ++globI) {
419 const auto& nbInfos = neighborInfo[globI];
420 for (
const auto& nbInfo : nbInfos) {
421 unsigned globJ = nbInfo.neighbor;
422 assert(globJ != globI);
425 Evaluation flux = 0.0;
428 (*this->
energyMatrix_)[globI][globI][0][0] += flux.derivative(Indices::temperatureIdx);
429 (*this->
energyMatrix_)[globJ][globI][0][0] -= flux.derivative(Indices::temperatureIdx);
432 Evaluation heatFlux = 0.0;
435 (*this->
energyMatrix_)[globI][globI][0][0] += heatFlux.derivative(Indices::temperatureIdx);
436 (*this->
energyMatrix_)[globJ][globI][0][0] -= heatFlux.derivative(Indices::temperatureIdx);
441 const auto& wellPtrs =
simulator_.problem().wellModel().localNonshutWells();
442 for (
const auto& wellPtr : wellPtrs) {
448 bool enableDriftCompensation = Parameters::Get<Parameters::EnableDriftCompensationTemp>();
449 if (enableDriftCompensation) {
450 for (
unsigned globalDofIdx = 0; globalDofIdx < numCells; ++globalDofIdx) {
451 auto dofDriftRate = problem.drift()[globalDofIdx]/dt;
452 const auto& fs =
intQuants_[globalDofIdx].fluidState();
453 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++ phaseIdx) {
454 const unsigned activeCompIdx =
455 FluidSystem::canonicalToActiveCompIdx(FluidSystem::solventComponentIndex(phaseIdx));
456 auto drift_hrate = dofDriftRate[activeCompIdx]*getValue(fs.enthalpy(phaseIdx)) * getValue(fs.density(phaseIdx)) / getValue(fs.invB(phaseIdx));
457 this->
energyVector_[globalDofIdx] -= drift_hrate*getPropValue<TypeTag, Properties::BlackOilEnergyScalingFactor>();
462 if (
simulator_.gridView().comm().size() > 1) {
478 const auto& eclWell = well.wellEcl();
479 std::size_t well_index =
simulator_.problem().wellModel().wellState().index(well.name()).value();
480 const auto& ws =
simulator_.problem().wellModel().wellState().well(well_index);
482 for (std::size_t i = 0; i < ws.perf_data.size(); ++i) {
483 const auto globI = ws.perf_data.cell_index[i];
485 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++ phaseIdx) {
486 if (!FluidSystem::phaseIsActive(phaseIdx)) {
490 Evaluation rate = well.volumetricSurfaceRateForConnection(globI, phaseIdx);
491 if (rate > 0 && eclWell.isInjector()) {
492 fs.setTemperature(eclWell.inj_temperature());
493 const auto& rho = FluidSystem::density(fs, phaseIdx, fs.pvtRegionIndex());
494 fs.setDensity(phaseIdx, rho);
495 const auto& h = FluidSystem::enthalpy(fs, phaseIdx, fs.pvtRegionIndex());
496 fs.setEnthalpy(phaseIdx, h);
497 rate *= getValue(fs.enthalpy(phaseIdx)) * getValue(fs.density(phaseIdx)) / getValue(fs.invB(phaseIdx));
499 const Evaluation d = 1.0 - fs.Rv() * fs.Rs();
500 if (phaseIdx == gasPhaseIdx && d > 0) {
501 const auto& oilrate = well.volumetricSurfaceRateForConnection(globI, oilPhaseIdx);
502 rate -= oilrate * getValue(fs.Rs());
505 if (phaseIdx == oilPhaseIdx && d > 0) {
506 const auto& gasrate = well.volumetricSurfaceRateForConnection(globI, gasPhaseIdx);
507 rate -= gasrate * getValue(fs.Rv());
510 rate *= fs.enthalpy(phaseIdx) * getValue(fs.density(phaseIdx)) / getValue(fs.invB(phaseIdx));
513 rate *= getPropValue<TypeTag, Properties::BlackOilEnergyScalingFactor>();
515 (*this->
energyMatrix_)[globI][globI][0][0] -= rate.derivative(Indices::temperatureIdx);
528template <
class TypeTag>
543 template <
class Restarter>
553 template <
class Restarter>
Contains the high level supplements required to extend the black oil model by energy.
Definition: blackoilenergymodules.hh:60
Definition: GenericTemperatureModel.hpp:52
Opm::GenericTemperatureModel< GetPropType< TypeTag, Properties::Grid >, GetPropType< TypeTag, Properties::GridView >, GetPropType< TypeTag, Properties::DofMapper >, GetPropType< TypeTag, Properties::Stencil >, GetPropType< TypeTag, Properties::FluidSystem >, GetPropType< TypeTag, Properties::Scalar > >::energyVector_ EnergyVector energyVector_
Definition: GenericTemperatureModel.hpp:87
Opm::GenericTemperatureModel< GetPropType< TypeTag, Properties::Grid >, GetPropType< TypeTag, Properties::GridView >, GetPropType< TypeTag, Properties::DofMapper >, GetPropType< TypeTag, Properties::Stencil >, GetPropType< TypeTag, Properties::FluidSystem >, GetPropType< TypeTag, Properties::Scalar > >::energy_rates_ std::vector< GetPropType< TypeTag, Properties::Scalar > > energy_rates_
Definition: GenericTemperatureModel.hpp:90
Opm::GenericTemperatureModel< GetPropType< TypeTag, Properties::Grid >, GetPropType< TypeTag, Properties::GridView >, GetPropType< TypeTag, Properties::DofMapper >, GetPropType< TypeTag, Properties::Stencil >, GetPropType< TypeTag, Properties::FluidSystem >, GetPropType< TypeTag, Properties::Scalar > >::energyMatrix_ std::unique_ptr< EnergyMatrix > energyMatrix_
Definition: GenericTemperatureModel.hpp:88
Opm::GenericTemperatureModel< GetPropType< TypeTag, Properties::Grid >, GetPropType< TypeTag, Properties::GridView >, GetPropType< TypeTag, Properties::DofMapper >, GetPropType< TypeTag, Properties::Stencil >, GetPropType< TypeTag, Properties::FluidSystem >, GetPropType< TypeTag, Properties::Scalar > >::EnergyMatrix Dune::BCRSMatrix< Opm::MatrixBlock< GetPropType< TypeTag, Properties::Scalar >, 1, 1 > > EnergyMatrix
Definition: GenericTemperatureModel.hpp:54
Opm::GenericTemperatureModel< GetPropType< TypeTag, Properties::Grid >, GetPropType< TypeTag, Properties::GridView >, GetPropType< TypeTag, Properties::DofMapper >, GetPropType< TypeTag, Properties::Stencil >, GetPropType< TypeTag, Properties::FluidSystem >, GetPropType< TypeTag, Properties::Scalar > >::linearSolve_ bool linearSolve_(const EnergyMatrix &M, EnergyVector &x, EnergyVector &b)
Definition: GenericTemperatureModel_impl.hpp:165
Opm::GenericTemperatureModel< GetPropType< TypeTag, Properties::Grid >, GetPropType< TypeTag, Properties::GridView >, GetPropType< TypeTag, Properties::DofMapper >, GetPropType< TypeTag, Properties::Stencil >, GetPropType< TypeTag, Properties::FluidSystem >, GetPropType< TypeTag, Properties::Scalar > >::EnergyVector Dune::BlockVector< Dune::FieldVector< GetPropType< TypeTag, Properties::Scalar >, 1 > > EnergyVector
Definition: GenericTemperatureModel.hpp:55
Opm::GenericTemperatureModel< GetPropType< TypeTag, Properties::Grid >, GetPropType< TypeTag, Properties::GridView >, GetPropType< TypeTag, Properties::DofMapper >, GetPropType< TypeTag, Properties::Stencil >, GetPropType< TypeTag, Properties::FluidSystem >, GetPropType< TypeTag, Properties::Scalar > >::doTemp bool doTemp()
Definition: GenericTemperatureModel.hpp:59
Opm::GenericTemperatureModel< GetPropType< TypeTag, Properties::Grid >, GetPropType< TypeTag, Properties::GridView >, GetPropType< TypeTag, Properties::DofMapper >, GetPropType< TypeTag, Properties::Stencil >, GetPropType< TypeTag, Properties::FluidSystem >, GetPropType< TypeTag, Properties::Scalar > >::doInit void doInit(std::size_t numGridDof)
Initialize all internal data structures needed by the temperature module.
Definition: GenericTemperatureModel_impl.hpp:115
Opm::GenericTemperatureModel< GetPropType< TypeTag, Properties::Grid >, GetPropType< TypeTag, Properties::GridView >, GetPropType< TypeTag, Properties::DofMapper >, GetPropType< TypeTag, Properties::Stencil >, GetPropType< TypeTag, Properties::FluidSystem >, GetPropType< TypeTag, Properties::Scalar > >::maxTempChange_ GetPropType< TypeTag, Properties::Scalar > maxTempChange_
Definition: GenericTemperatureModel.hpp:92
Opm::GenericTemperatureModel< GetPropType< TypeTag, Properties::Grid >, GetPropType< TypeTag, Properties::GridView >, GetPropType< TypeTag, Properties::DofMapper >, GetPropType< TypeTag, Properties::Stencil >, GetPropType< TypeTag, Properties::FluidSystem >, GetPropType< TypeTag, Properties::Scalar > >::temperature_ std::vector< GetPropType< TypeTag, Properties::Scalar > > temperature_
Definition: GenericTemperatureModel.hpp:89
void beginTimeStep()
Definition: TemperatureModel.hpp:558
void init()
Definition: TemperatureModel.hpp:557
const Scalar temperature(size_t) const
Definition: TemperatureModel.hpp:559
void deserialize(Restarter &)
This method restores the complete state of the temperature from disk.
Definition: TemperatureModel.hpp:554
void serialize(Restarter &)
This method writes the complete state of all temperature to the hard disk.
Definition: TemperatureModel.hpp:544
TemperatureModel(Simulator &)
Definition: TemperatureModel.hpp:536
A class which handles sequential implicit solution of the energy equation as specified in by TEMP.
Definition: TemperatureModel.hpp:73
void serialize(Restarter &)
This method writes the complete state of all temperature to the hard disk.
Definition: TemperatureModel.hpp:189
void assembleEquationWell(const Well &well)
Definition: TemperatureModel.hpp:476
std::vector< int > interiorRows_
Definition: TemperatureModel.hpp:524
EnergyVector storage1_
Definition: TemperatureModel.hpp:521
bool converged(const int iter)
Definition: TemperatureModel.hpp:257
void computeHeatFluxTerm(unsigned globI, unsigned globJ, const ResidualNBInfo &res_nbinfo, Evaluation &heatFlux)
Definition: TemperatureModel.hpp:372
void computeStorageTerm(unsigned globI, LhsEval &storage)
Definition: TemperatureModel.hpp:310
void deserialize(Restarter &)
This method restores the complete state of the temperature from disk.
Definition: TemperatureModel.hpp:199
void endTimeStep(WellStateType &wellState)
Informs the temperature model that a time step has just been finished.
Definition: TemperatureModel.hpp:161
const Simulator & simulator_
Definition: TemperatureModel.hpp:520
void beginTimeStep()
Definition: TemperatureModel.hpp:139
void advanceTemperatureFields()
Definition: TemperatureModel.hpp:217
void updateStorageCache()
Definition: TemperatureModel.hpp:203
void assembleEquations()
Definition: TemperatureModel.hpp:396
void solveAndUpdate()
Definition: TemperatureModel.hpp:238
std::vector< IntensiveQuantities > intQuants_
Definition: TemperatureModel.hpp:522
void computeFluxTerm(unsigned globI, unsigned globJ, const ResidualNBInfo &res_nbinfo, Evaluation &flux)
Definition: TemperatureModel.hpp:336
std::vector< int > overlapRows_
Definition: TemperatureModel.hpp:523
TemperatureModel(Simulator &simulator)
Definition: TemperatureModel.hpp:107
void init()
Definition: TemperatureModel.hpp:115
Definition: WellState.hpp:66
int numWells() const
Definition: WellState.hpp:99
const SingleWellState< Scalar, IndexTraits > & well(std::size_t well_index) const
Definition: WellState.hpp:290
Provides data handles for parallel communication which operate on DOFs.
auto Get(bool errorIfNotRegistered=true)
Retrieve a runtime parameter.
Definition: parametersystem.hpp:187
Definition: blackoilmodel.hh:80
void findOverlapAndInterior(const Grid &grid, const Mapper &mapper, std::vector< int > &overlapRows, std::vector< int > &interiorRows)
Find the rows corresponding to overlap cells.
Definition: findOverlapRowsAndColumns.hpp:92
Definition: blackoilbioeffectsmodules.hh:43
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.
Definition: AquiferGridUtils.hpp:35
Definition: BlackoilModelParameters.hpp:61
Definition: TemperatureModel.hpp:51
a tag to mark properties as undefined
Definition: propertysystem.hh:38