30#ifndef OPM_FLOW_PROBLEM_COMP_HPP
31#define OPM_FLOW_PROBLEM_COMP_HPP
38#include <opm/material/fluidstates/CompositionalFluidState.hpp>
40#include <opm/material/thermal/EclThermalLawManager.hpp>
42#include <opm/input/eclipse/EclipseState/Compositional/CompositionalConfig.hpp>
56template <
class TypeTag>
86 using InitialFluidState = CompositionalFluidState<Scalar, FluidSystem>;
103 Parameters::SetDefault<Parameters::NewtonTolerance<Scalar>>(1e-7);
108 auto& simulator = this->simulator();
109 const auto& eclState = simulator.vanguard().eclState();
110 return eclState.compositionalConfig().eosType(0);
118 , thresholdPressures_(simulator)
120 eclWriter_ = std::make_unique<EclWriterType>(simulator);
121 enableEclOutput_ = Parameters::Get<Parameters::EnableEclOutput>();
132 FlowProblemType::finishInit();
134 auto& simulator = this->simulator();
136 auto finishTransmissibilities = [updated =
false,
this]()
mutable {
141 [&vg = this->simulator().vanguard()](
const unsigned int it) {
return vg.gridIdxToEquilGridIdx(it); });
146 finishTransmissibilities();
148 if (enableEclOutput_) {
149 eclWriter_->setTransmissibilities(&simulator.problem().eclTransmissibilities());
150 std::function<
unsigned int(
unsigned int)> equilGridToGrid = [&simulator](
unsigned int i) {
151 return simulator.vanguard().gridEquilIdxToGridIdx(i);
153 eclWriter_->extractOutputTransAndNNC(equilGridToGrid);
156 const auto& eclState = simulator.vanguard().eclState();
157 const auto& schedule = simulator.vanguard().schedule();
160 simulator.setStartTime(schedule.getStartTime());
161 simulator.setEndTime(schedule.simTime(schedule.size() - 1));
167 simulator.setEpisodeIndex(-1);
168 simulator.setEpisodeLength(0.0);
175 const auto& tuning = schedule[0].tuning();
182 if (FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx)
183 && FluidSystem::phaseIsActive(FluidSystem::gasPhaseIdx)) {
187 this->
readRockParameters_(simulator.vanguard().cellCenterDepths(), [&simulator](
const unsigned idx) {
188 std::array<int, dim> coords;
189 simulator.vanguard().cartesianCoordinate(idx, coords);
190 std::ranges::transform(coords, coords.begin(),
191 [](const auto c) { return c + 1; });
198 if (enableEclOutput_) {
199 eclWriter_->writeInit();
202 const auto& initconfig = eclState.getInitConfig();
203 if (initconfig.restartRequested())
210 if constexpr (getPropValue<TypeTag, Properties::EnablePolymer>()) {
211 const auto& vanguard = this->simulator().vanguard();
212 const auto& gridView = vanguard.gridView();
213 int numElements = gridView.size(0);
214 this->
polymer_.maxAdsorption.resize(numElements, 0.0);
228 if (this->enableVtkOutput_() && eclState.getIOConfig().initOnly()) {
229 simulator.setTimeStepSize(0.0);
236 if (!initconfig.restartRequested()) {
237 simulator.startNextEpisode(schedule.seconds(1));
238 simulator.setEpisodeIndex(0);
239 simulator.setTimeStepIndex(0);
248 FlowProblemType::endTimeStep();
251 this->eclWriter_->mutableOutputModule().invalidateLocalData();
254 const auto& grid = this->simulator().vanguard().gridView().grid();
256 using GridType = std::remove_cv_t<std::remove_reference_t<
decltype(grid)>>;
257 constexpr bool isCpGrid = std::is_same_v<GridType, Dune::CpGrid>;
258 if (!isCpGrid || (grid.maxLevel() == 0)) {
259 this->eclWriter_->evalSummaryState(! this->episodeWillBeOver());
264 if (enableEclOutput_){
265 eclWriter_->writeReports(timer);
275 FlowProblemType::writeOutput(verbose);
277 if (! this->enableEclOutput_) {
281 const auto isSubStep = !this->episodeWillBeOver();
283 if (!isSubStep || Parameters::Get<Parameters::EnableWriteAllSolutions>()) {
284 auto localCellData = data::Solution {};
286 this->eclWriter_->writeOutput(std::move(localCellData), isSubStep,
287 this->simulator().vanguard().schedule()
288 .exitStatus().has_value());
297 template <
class Context>
299 const Context& context,
303 OPM_TIMEBLOCK_LOCAL(eclProblemBoundary, Subsystem::Assembly);
304 if (!context.intersection(spaceIdx).boundary())
309 if (this->nonTrivialBoundaryConditions()) {
310 throw std::logic_error(
"boundary condition is not supported by compostional modeling yet");
320 template <
class Context>
323 const unsigned globalDofIdx = context.globalSpaceIndex(spaceIdx, timeIdx);
324 const auto& initial_fs = initialFluidStates_[globalDofIdx];
325 Opm::CompositionalFluidState<Scalar, FluidSystem> fs;
326 for (
unsigned p = 0; p < numPhases; ++p) {
328 fs.setPressure(p, initial_fs.pressure(p));
331 fs.setSaturation(p, initial_fs.saturation(p));
334 fs.setTemperature(initial_fs.temperature(p));
338 if (!zmf_initialization_) {
339 for (
unsigned p = 0; p < numPhases; ++p) {
340 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
341 fs.setMoleFraction(p, compIdx, initial_fs.moleFraction(p, compIdx));
346 const auto& eos_type = getEosType();
347 typename FluidSystem::template ParameterCache<Scalar> paramCache(eos_type);
348 paramCache.updatePhase(fs, FluidSystem::oilPhaseIdx);
349 paramCache.updatePhase(fs, FluidSystem::gasPhaseIdx);
350 fs.setDensity(FluidSystem::oilPhaseIdx, FluidSystem::density(fs, paramCache, FluidSystem::oilPhaseIdx));
351 fs.setDensity(FluidSystem::gasPhaseIdx, FluidSystem::density(fs, paramCache, FluidSystem::gasPhaseIdx));
354 Dune::FieldVector<Scalar, numComponents> z(0.0);
356 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
357 if (Indices::waterEnabled && phaseIdx ==
static_cast<unsigned int>(waterPhaseIdx)){
360 const auto saturation = fs.saturation(phaseIdx);
361 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
362 Scalar tmp = fs.molarity(phaseIdx, compIdx) * saturation;
363 tmp = max(tmp, 1e-8);
369 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
370 fs.setMoleFraction(compIdx, z[compIdx]);
374 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
375 fs.setMoleFraction(compIdx, initial_fs.moleFraction(compIdx));
380 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
381 const auto& Ktmp = fs.wilsonK_(compIdx);
382 fs.setKvalue(compIdx, Ktmp);
385 const Scalar& Ltmp = -1.0;
388 values.assignNaive(fs);
397 {
return initialFluidStates_[globalDofIdx]; }
400 {
return initialFluidStates_; }
403 {
return initialFluidStates_; }
407 assert( !thresholdPressures_.enableThresholdPressure() &&
408 " Threshold Pressures are not supported by compostional simulation ");
409 return thresholdPressures_;
413 {
return *eclWriter_; }
416 {
return *eclWriter_; }
419 {
return eclWriter_->setSubStepReport(report); }
422 {
return eclWriter_->setSimulationReport(report); }
426 OPM_TIMEBLOCK(finalizeOutput);
431 template<
class Serializer>
435 serializer(*eclWriter_);
446 throw std::logic_error(
"Equilibration is not supported by compositional modeling yet");
451 throw std::logic_error(
"Restarting is not supported by compositional modeling yet");
456 readExplicitInitialConditionCompositional_();
461 const auto& simulator = this->simulator();
462 const auto& vanguard = simulator.vanguard();
463 const auto& eclState = vanguard.eclState();
464 const auto& fp = eclState.fieldProps();
465 const bool has_pressure = fp.has_double(
"PRESSURE");
467 throw std::runtime_error(
"The ECL input file requires the presence of the PRESSURE "
468 "keyword if the model is initialized explicitly");
470 const bool has_xmf = fp.has_double(
"XMF");
471 const bool has_ymf = fp.has_double(
"YMF");
472 const bool has_zmf = fp.has_double(
"ZMF");
473 if ( !has_zmf && !(has_xmf && has_ymf) ) {
474 throw std::runtime_error(
"The ECL input file requires the presence of ZMF or XMF and YMF "
475 "keyword if the model is initialized explicitly");
478 if (has_zmf && (has_xmf || has_ymf)) {
479 throw std::runtime_error(
"The ECL input file can not handle explicit initialization "
480 "with both ZMF and XMF or YMF");
483 if (has_xmf != has_ymf) {
484 throw std::runtime_error(
"The ECL input file needs XMF and YMF combined to do the explicit "
485 "initializtion when using XMF or YMF");
488 const bool has_temp = fp.has_double(
"TEMPI");
491 assert(fp.has_double(
"SGAS"));
493 std::size_t numDof = this->model().numGridDof();
495 initialFluidStates_.resize(numDof);
497 std::vector<double> waterSaturationData;
498 std::vector<double> gasSaturationData;
499 std::vector<double> soilData;
500 std::vector<double> pressureData;
501 std::vector<double> tempiData;
503 const bool water_active = FluidSystem::phaseIsActive(waterPhaseIdx);
504 const bool gas_active = FluidSystem::phaseIsActive(gasPhaseIdx);
505 const bool oil_active = FluidSystem::phaseIsActive(oilPhaseIdx);
507 if (water_active && Indices::numPhases > 2)
508 waterSaturationData = fp.get_double(
"SWAT");
510 waterSaturationData.resize(numDof);
512 pressureData = fp.get_double(
"PRESSURE");
515 tempiData = fp.get_double(
"TEMPI");
521 gasSaturationData = fp.get_double(
"SGAS");
523 gasSaturationData.resize(numDof);
525 for (std::size_t dofIdx = 0; dofIdx < numDof; ++dofIdx) {
526 auto& dofFluidState = initialFluidStates_[dofIdx];
529 Scalar temperatureLoc = tempiData[dofIdx];
530 assert(std::isfinite(temperatureLoc) && temperatureLoc > 0);
531 dofFluidState.setTemperature(temperatureLoc);
534 dofFluidState.setSaturation(FluidSystem::gasPhaseIdx,
535 gasSaturationData[dofIdx]);
538 dofFluidState.setSaturation(FluidSystem::oilPhaseIdx,
540 - waterSaturationData[dofIdx]
541 - gasSaturationData[dofIdx]);
544 dofFluidState.setSaturation(FluidSystem::waterPhaseIdx,
545 waterSaturationData[dofIdx]);
551 const Scalar pressure = pressureData[dofIdx];
554 const std::array<Scalar, numPhases> pc = {0};
555 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
556 if (!FluidSystem::phaseIsActive(phaseIdx))
559 if (Indices::oilEnabled)
560 dofFluidState.setPressure(phaseIdx, pressure + (pc[phaseIdx] - pc[oilPhaseIdx]));
561 else if (Indices::gasEnabled)
562 dofFluidState.setPressure(phaseIdx, pressure + (pc[phaseIdx] - pc[gasPhaseIdx]));
563 else if (Indices::waterEnabled)
565 dofFluidState.setPressure(phaseIdx, pressure);
568 if (has_xmf && has_ymf) {
569 const auto& xmfData = fp.get_double(
"XMF");
570 const auto& ymfData = fp.get_double(
"YMF");
571 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
572 const std::size_t data_idx = compIdx * numDof + dofIdx;
573 const Scalar xmf = xmfData[data_idx];
574 const Scalar ymf = ymfData[data_idx];
576 dofFluidState.setMoleFraction(FluidSystem::oilPhaseIdx, compIdx, xmf);
577 dofFluidState.setMoleFraction(FluidSystem::gasPhaseIdx, compIdx, ymf);
582 zmf_initialization_ =
true;
583 const auto& zmfData = fp.get_double(
"ZMF");
584 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
585 const std::size_t data_idx = compIdx * numDof + dofIdx;
586 const Scalar zmf = zmfData[data_idx];
587 dofFluidState.setMoleFraction(compIdx, zmf);
590 const auto ymf = (dofFluidState.saturation(FluidSystem::gasPhaseIdx) > 0.) ? zmf :
Scalar{0};
591 dofFluidState.setMoleFraction(FluidSystem::gasPhaseIdx, compIdx, ymf);
594 const auto xmf = (dofFluidState.saturation(FluidSystem::oilPhaseIdx) > 0.) ? zmf :
Scalar{0};
595 dofFluidState.setMoleFraction(FluidSystem::oilPhaseIdx, compIdx, xmf);
604 void handleSolventBC(
const BCProp::BCFace& , RateVector& )
const override
606 throw std::logic_error(
"solvent is disabled for compositional modeling and you're trying to add solvent to BC");
609 void handlePolymerBC(
const BCProp::BCFace& , RateVector& )
const override
611 throw std::logic_error(
"polymer is disabled for compositional modeling and you're trying to add polymer to BC");
614 void handleMicrBC(
const BCProp::BCFace& , RateVector& )
const override
616 throw std::logic_error(
"MICP is disabled for compositional modeling and you're trying to add microbes to BC");
619 void handleOxygBC(
const BCProp::BCFace& , RateVector& )
const override
621 throw std::logic_error(
"MICP is disabled for compositional modeling and you're trying to add oxygen to BC");
624 void handleUreaBC(
const BCProp::BCFace& , RateVector& )
const override
626 throw std::logic_error(
"MICP is disabled for compositional modeling and you're trying to add urea to BC");
629 FlowThresholdPressure<TypeTag> thresholdPressures_;
631 std::vector<InitialFluidState> initialFluidStates_;
633 bool zmf_initialization_ {
false};
635 bool enableEclOutput_{
false};
636 std::unique_ptr<EclWriterType> eclWriter_;
Collects necessary output values and pass it to opm-common's ECL output.
Definition: EclWriter.hpp:122
static void registerParameters()
Definition: EclWriter.hpp:147
PolymerSolutionContainer< Scalar > polymer_
Definition: FlowGenericProblem.hpp:353
Scalar initialTimeStepSize_
Definition: FlowGenericProblem.hpp:365
bool enableTuning_
Definition: FlowGenericProblem.hpp:364
void readRockParameters_(const std::vector< Scalar > &cellCenterDepths, std::function< std::array< int, 3 >(const unsigned)> ijkIndex)
Definition: FlowGenericProblem_impl.hpp:153
std::vector< Scalar > maxOilSaturation_
Definition: FlowGenericProblem.hpp:354
void initFluidSystem_()
Definition: FlowGenericProblem_impl.hpp:549
Scalar maxTimeStepAfterWellEvent_
Definition: FlowGenericProblem.hpp:366
This problem simulates an input file given in the data format used by the commercial ECLiPSE simulato...
Definition: FlowProblemComp.hpp:58
void writeOutput(bool verbose) override
Write the requested quantities of the current solution into the output files.
Definition: FlowProblemComp.hpp:273
const std::vector< InitialFluidState > & initialFluidStates() const
Definition: FlowProblemComp.hpp:402
void finishInit()
Called by the Opm::Simulator in order to initialize the problem.
Definition: FlowProblemComp.hpp:127
Opm::CompositionalConfig::EOSType getEosType() const
Definition: FlowProblemComp.hpp:106
FlowProblemComp(Simulator &simulator)
Definition: FlowProblemComp.hpp:116
void writeReports(const SimulatorTimer &timer)
Definition: FlowProblemComp.hpp:263
void readExplicitInitialCondition_() override
Definition: FlowProblemComp.hpp:454
void readExplicitInitialConditionCompositional_()
Definition: FlowProblemComp.hpp:459
void endTimeStep() override
Called by the simulator after each time integration.
Definition: FlowProblemComp.hpp:246
const EclWriterType & eclWriter() const
Definition: FlowProblemComp.hpp:412
std::vector< InitialFluidState > & initialFluidStates()
Definition: FlowProblemComp.hpp:399
const FlowThresholdPressure< TypeTag > & thresholdPressure() const
Definition: FlowProblemComp.hpp:405
void readEclRestartSolution_()
Definition: FlowProblemComp.hpp:449
void finalizeOutput()
Definition: FlowProblemComp.hpp:424
const InitialFluidState & initialFluidState(unsigned globalDofIdx) const
Definition: FlowProblemComp.hpp:396
void boundary(BoundaryRateVector &values, const Context &context, unsigned spaceIdx, unsigned) const
Evaluate the boundary conditions for a boundary segment.
Definition: FlowProblemComp.hpp:298
void initial(PrimaryVariables &values, const Context &context, unsigned spaceIdx, unsigned timeIdx) const
Evaluate the initial value for a control volume.
Definition: FlowProblemComp.hpp:321
void readEquilInitialCondition_() override
Definition: FlowProblemComp.hpp:444
void serializeOp(Serializer &serializer)
Definition: FlowProblemComp.hpp:432
void updateExplicitQuantities_(int, int, bool) override
Definition: FlowProblemComp.hpp:439
void addToSourceDense(RateVector &, unsigned, unsigned) const override
Definition: FlowProblemComp.hpp:391
void setSubStepReport(const SimulatorReportSingle &report)
Definition: FlowProblemComp.hpp:418
static void registerParameters()
Registers all available parameters for the problem and the model.
Definition: FlowProblemComp.hpp:96
EclWriterType & eclWriter()
Definition: FlowProblemComp.hpp:415
void setSimulationReport(const SimulatorReport &report)
Definition: FlowProblemComp.hpp:421
This problem simulates an input file given in the data format used by the commercial ECLiPSE simulato...
Definition: FlowProblem.hpp:95
GetPropType< TypeTag, Properties::Evaluation > Evaluation
Definition: FlowProblem.hpp:160
virtual void writeOutput(bool verbose)
Write the requested quantities of the current solution into the output files.
Definition: FlowProblem.hpp:523
unsigned pvtRegionIndex(const Context &context, unsigned spaceIdx, unsigned timeIdx) const
Returns the index of the relevant region for thermodynmic properties.
Definition: FlowProblem.hpp:906
Scalar porosity(const Context &context, unsigned spaceIdx, unsigned timeIdx) const
Definition: FlowProblem.hpp:706
GetPropType< TypeTag, Properties::Vanguard > Vanguard
Definition: FlowProblem.hpp:108
@ numComponents
Definition: FlowProblem.hpp:118
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: FlowProblem.hpp:102
void initGravity_(const EclipseState &eclState)
Set the gravity vector from the run's configuration.
Definition: FlowProblem.hpp:1627
GetPropType< TypeTag, Properties::RateVector > RateVector
Definition: FlowProblem.hpp:149
@ waterPhaseIdx
Definition: FlowProblem.hpp:140
GetPropType< TypeTag, Properties::Indices > Indices
Definition: FlowProblem.hpp:109
GetPropType< TypeTag, Properties::Simulator > Simulator
Definition: FlowProblem.hpp:150
@ dimWorld
Definition: FlowProblem.hpp:113
@ gasPhaseIdx
Definition: FlowProblem.hpp:138
@ numPhases
Definition: FlowProblem.hpp:117
void readThermalParameters_()
Definition: FlowProblem.hpp:1481
@ dim
Definition: FlowProblem.hpp:112
GetPropType< TypeTag, Properties::GridView > GridView
Definition: FlowProblem.hpp:103
static void registerParameters()
Registers all available parameters for the problem and the model.
Definition: FlowProblem.hpp:191
void updatePffDofData_()
Definition: FlowProblem.hpp:1649
@ oilPhaseIdx
Definition: FlowProblem.hpp:139
GetPropType< TypeTag, Properties::PrimaryVariables > PrimaryVariables
Definition: FlowProblem.hpp:148
Vanguard::TransmissibilityType transmissibilities_
Definition: FlowProblem.hpp:1851
virtual void readInitialCondition_()
Definition: FlowProblem.hpp:1557
GetPropType< TypeTag, Properties::FluidSystem > FluidSystem
Definition: FlowProblem.hpp:105
GetPropType< TypeTag, Properties::MaterialLaw > MaterialLaw
Definition: FlowProblem.hpp:158
void readMaterialParameters_()
Definition: FlowProblem.hpp:1441
This class calculates the threshold pressure for grid faces according to the Eclipse Reference Manual...
Definition: FlowThresholdPressure.hpp:59
Definition: SimulatorTimer.hpp:38
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
Definition: SimulatorReport.hpp:125
A struct for returning timing data from a simulator to its caller.
Definition: SimulatorReport.hpp:34