From 8da842d1f3b9259dc422752f1421dfb9561a5d13 Mon Sep 17 00:00:00 2001 From: Kai Bao Date: Thu, 24 Jul 2014 17:06:43 +0200 Subject: [PATCH 1/3] Commit for saving. --- .../SimulatorFullyImplicitBlackoil.hpp | 12 ++++--- .../SimulatorFullyImplicitBlackoil_impl.hpp | 36 +++++++++++-------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/opm/autodiff/SimulatorFullyImplicitBlackoil.hpp b/opm/autodiff/SimulatorFullyImplicitBlackoil.hpp index efad51ff6..7f991f457 100644 --- a/opm/autodiff/SimulatorFullyImplicitBlackoil.hpp +++ b/opm/autodiff/SimulatorFullyImplicitBlackoil.hpp @@ -37,6 +37,8 @@ namespace Opm class SimulatorTimer; class BlackoilState; class WellStateFullyImplicitBlackoil; + class EclipseState; + class EclipseWriter; struct SimulatorReport; /// Class collecting all necessary components for a two-phase simulation. @@ -68,15 +70,18 @@ namespace Opm /// \param[in] well_manager well manager, may manage no (null) wells /// \param[in] linsolver linear solver /// \param[in] gravity if non-null, gravity vector + /// \param[in] eclipse_state + /// \param[in] output_writer SimulatorFullyImplicitBlackoil(const parameter::ParameterGroup& param, const Grid& grid, BlackoilPropsAdInterface& props, const RockCompressibility* rock_comp_props, - WellsManager& wells_manager, NewtonIterationBlackoilInterface& linsolver, const double* gravity, const bool disgas, - const bool vapoil ); + const bool vapoil, + std::shared_ptr eclipse_state, + EclipseWriter& output_writer); /// Run the simulation. /// This will run succesive timesteps until timer.done() is true. It will @@ -86,8 +91,7 @@ namespace Opm /// \param[in,out] well_state state of wells: bhp, perforation rates /// \return simulation report, with timing data SimulatorReport run(SimulatorTimer& timer, - BlackoilState& state, - WellStateFullyImplicitBlackoil& well_state); + BlackoilState& state); private: class Impl; diff --git a/opm/autodiff/SimulatorFullyImplicitBlackoil_impl.hpp b/opm/autodiff/SimulatorFullyImplicitBlackoil_impl.hpp index fcad56038..a10054d14 100644 --- a/opm/autodiff/SimulatorFullyImplicitBlackoil_impl.hpp +++ b/opm/autodiff/SimulatorFullyImplicitBlackoil_impl.hpp @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -68,11 +69,12 @@ namespace Opm NewtonIterationBlackoilInterface& linsolver, const double* gravity, bool has_disgas, - bool has_vapoil ); + bool has_vapoil, + std::shared_ptr eclipse_state, + EclipseWriter& output_writer); SimulatorReport run(SimulatorTimer& timer, - BlackoilState& state, - WellStateFullyImplicitBlackoil& well_state); + BlackoilState& state); private: // Data. @@ -89,14 +91,16 @@ namespace Opm const Grid& grid_; BlackoilPropsAdInterface& props_; const RockCompressibility* rock_comp_props_; - WellsManager& wells_manager_; - const Wells* wells_; const double* gravity_; // Solvers DerivedGeology geo_; FullyImplicitBlackoilSolver solver_; // Misc. data std::vector allcells_; + // eclipse_state + std::shared_ptr eclipse_state_; + // output_writer + EclipseWriter& output_writer_; }; @@ -111,10 +115,12 @@ namespace Opm NewtonIterationBlackoilInterface& linsolver, const double* gravity, const bool has_disgas, - const bool has_vapoil ) + const bool has_vapoil, + std::shared_ptr eclipse_state, + EclipseWriter& output_writer) { - pimpl_.reset(new Impl(param, grid, props, rock_comp_props, wells_manager, linsolver, gravity, has_disgas, has_vapoil)); + pimpl_.reset(new Impl(param, grid, props, rock_comp_props, wells_manager, linsolver, gravity, has_disgas, has_vapoil, eclipse_state, output_writer)); } @@ -123,10 +129,9 @@ namespace Opm template SimulatorReport SimulatorFullyImplicitBlackoil::run(SimulatorTimer& timer, - BlackoilState& state, - WellStateFullyImplicitBlackoil& well_state) + BlackoilState& state) { - return pimpl_->run(timer, state, well_state); + return pimpl_->run(timer, state); } @@ -198,15 +203,17 @@ namespace Opm NewtonIterationBlackoilInterface& linsolver, const double* gravity, const bool has_disgas, - const bool has_vapoil) + const bool has_vapoil, + std::shared_ptr eclipse_state, + EclipseWriter& output_writer) : grid_(grid), props_(props), rock_comp_props_(rock_comp_props), - wells_manager_(wells_manager), - wells_(wells_manager.c_wells()), gravity_(gravity), geo_(grid_, props_, gravity_), solver_(param, grid_, props_, geo_, rock_comp_props, *wells_manager.c_wells(), linsolver, has_disgas, has_vapoil) + eclipse_state_(eclipse_state), + output_writer_(output_writer) /* param.getDefault("nl_pressure_residual_tolerance", 0.0), param.getDefault("nl_pressure_change_tolerance", 1.0), param.getDefault("nl_pressure_maxiter", 10), @@ -242,8 +249,7 @@ namespace Opm template SimulatorReport SimulatorFullyImplicitBlackoil::Impl::run(SimulatorTimer& timer, - BlackoilState& state, - WellStateFullyImplicitBlackoil& well_state) + BlackoilState& state) { // Initialisation. std::vector porevol; From 1e8d5e65b7147f17dbe8817eeb8bb1593a4ed38d Mon Sep 17 00:00:00 2001 From: Kai Bao Date: Fri, 25 Jul 2014 19:13:23 +0200 Subject: [PATCH 2/3] Finishing changing. Testing Later --- examples/sim_fibo_ad.cpp | 58 ++++-------- .../SimulatorFullyImplicitBlackoil_impl.hpp | 93 +++++++++---------- 2 files changed, 58 insertions(+), 93 deletions(-) diff --git a/examples/sim_fibo_ad.cpp b/examples/sim_fibo_ad.cpp index 83294e9fd..2e792cce3 100644 --- a/examples/sim_fibo_ad.cpp +++ b/examples/sim_fibo_ad.cpp @@ -193,52 +193,26 @@ try // initialize variables simtimer.init(timeMap); - SimulatorReport fullReport; - for (size_t reportStepIdx = 0; reportStepIdx < timeMap->numTimesteps(); ++reportStepIdx) { - // Report on start of a report step. - std::cout << "\n" - << "---------------------------------------------------------------\n" - << "-------------- Starting report step " << reportStepIdx << " --------------\n" - << "---------------------------------------------------------------\n" - << "\n"; + // @@@ HACK: we should really make a new well state and + // properly transfer old well state to it every epoch, + // since number of wells may change etc. - WellsManager wells(eclipseState, - reportStepIdx, - *grid->c_grid(), - props->permeability()); - if (reportStepIdx == 0) { - // @@@ HACK: we should really make a new well state and - // properly transfer old well state to it every epoch, - // since number of wells may change etc. - well_state.init(wells.c_wells(), state); - } + SimulatorFullyImplicitBlackoil simulator(param, + *grid->c_grid(), + *new_props, + rock_comp->isActive() ? rock_comp.get() : 0, + *fis_solver, + grav, + deck->hasKeyword("DISGAS"), + deck->hasKeyword("VAPOIL"), + eclipseState, + outputWriter); - simtimer.setCurrentStepNum(reportStepIdx); + std::cout << "\n\n================ Starting main simulation loop ===============\n" + << std::flush; - if (reportStepIdx == 0) { - outputWriter.writeInit(simtimer); - outputWriter.writeTimeStep(simtimer, state, well_state.basicWellState()); - } - - // Create and run simulator. - SimulatorFullyImplicitBlackoil simulator(param, - *grid->c_grid(), - *new_props, - rock_comp->isActive() ? rock_comp.get() : 0, - wells, - *fis_solver, - grav, - deck->hasKeyword("DISGAS"), - deck->hasKeyword("VAPOIL") ); - - SimulatorReport episodeReport = simulator.run(simtimer, state, well_state); - - ++simtimer; - - outputWriter.writeTimeStep(simtimer, state, well_state.basicWellState()); - fullReport += episodeReport; - } + SimulatorReport fullReport = simulator.run(simtimer, state); std::cout << "\n\n================ End of simulation ===============\n\n"; fullReport.report(std::cout); diff --git a/opm/autodiff/SimulatorFullyImplicitBlackoil_impl.hpp b/opm/autodiff/SimulatorFullyImplicitBlackoil_impl.hpp index a10054d14..2c4f9eaf3 100644 --- a/opm/autodiff/SimulatorFullyImplicitBlackoil_impl.hpp +++ b/opm/autodiff/SimulatorFullyImplicitBlackoil_impl.hpp @@ -78,7 +78,7 @@ namespace Opm private: // Data. - + const parameter::ParameterGroup param_; // Parameters for output. bool output_; bool output_vtk_; @@ -94,9 +94,12 @@ namespace Opm const double* gravity_; // Solvers DerivedGeology geo_; - FullyImplicitBlackoilSolver solver_; + // FullyImplicitBlackoilSolver solver_; + NewtonIterationBlackoilInterface& solver_; // Misc. data std::vector allcells_; + const bool has_disgas_; + const bool has_vapoil_; // eclipse_state std::shared_ptr eclipse_state_; // output_writer @@ -199,19 +202,22 @@ namespace Opm const Grid& grid, BlackoilPropsAdInterface& props, const RockCompressibility* rock_comp_props, - WellsManager& wells_manager, NewtonIterationBlackoilInterface& linsolver, const double* gravity, const bool has_disgas, const bool has_vapoil, std::shared_ptr eclipse_state, EclipseWriter& output_writer) - : grid_(grid), + : param_(param), + grid_(grid), props_(props), rock_comp_props_(rock_comp_props), gravity_(gravity), geo_(grid_, props_, gravity_), - solver_(param, grid_, props_, geo_, rock_comp_props, *wells_manager.c_wells(), linsolver, has_disgas, has_vapoil) + // solver_(param, grid_, props_, geo_, rock_comp_props, *wells_manager.c_wells(), linsolver, has_disgas, has_vapoil) + slover_(linsolver), + has_disgas_(has_disgas), + has_vapoil_(has_vapoil), eclipse_state_(eclipse_state), output_writer_(output_writer) /* param.getDefault("nl_pressure_residual_tolerance", 0.0), @@ -261,17 +267,14 @@ namespace Opm // const double tot_porevol_init = std::accumulate(porevol.begin(), porevol.end(), 0.0); std::vector initial_porevol = porevol; + WellStateFullyImplicitBlackoil well_state; + // Main simulation loop. Opm::time::StopWatch solver_timer; double stime = 0.0; Opm::time::StopWatch step_timer; Opm::time::StopWatch total_timer; total_timer.start(); - std::vector fractional_flows; - std::vector well_resflows_phase; - if (wells_) { - well_resflows_phase.resize((wells_->number_of_phases)*(wells_->number_of_wells), 0.0); - } std::fstream tstep_os; if (output_) { std::string filename = output_dir_ + "/step_timing.param"; @@ -281,6 +284,21 @@ namespace Opm // Report timestep and (optionally) write state to disk. step_timer.start(); timer.report(std::cout); + + WellsManager wells_manager(eclipse_state_, + timer.currentStepNum(), + grid_, + props_.permeability()); + + const Wells *wells = wells_manager.c_wells(); + + if (timer.currentStepNum() == 0) { + well_state.init(wells, state); + output_writer_.writeInit(timer, state, well_state.basicWellState()); + } else { + // TODO: add a function to update the well_state here. + } + if (output_ && (timer.currentStepNum() % output_interval_ == 0)) { if (output_vtk_) { outputStateVtk(grid_, state, timer.currentStepNum(), output_dir_); @@ -292,50 +310,23 @@ namespace Opm SimulatorReport sreport; - // Solve pressure equation. - // if (check_well_controls_) { - // computeFractionalFlow(props_, allcells_, - // state.pressure(), state.surfacevol(), state.saturation(), - // fractional_flows); - // wells_manager_.applyExplicitReinjectionControls(well_resflows_phase, well_resflows_phase); - // } + FullyImplicitBlackoilSolver solver( param_, grid_, props_, geo_, rock_comp_props_, *wells, linsolver_, has_disgas_, has_vapoil_); - bool well_control_passed = !check_well_controls_; - int well_control_iteration = 0; - do { - // Run solver. - solver_timer.start(); - std::vector initial_pressure = state.pressure(); - solver_.step(timer.currentStepLength(), state, well_state); + // Run solver. + solver_timer.start(); + std::vector initial_pressure = state.pressure(); + solver.step(timer.currentStepLength(), state, well_state); - // Stop timer and report. - solver_timer.stop(); - const double st = solver_timer.secsSinceStart(); - std::cout << "Fully implicit solver took: " << st << " seconds." << std::endl; + // Stop timer and report. + solver_timer.stop(); + const double st = solver_timer.secsSinceStart(); + std::cout << "Fully implicit solver took: " << st << " seconds." << std::endl; - stime += st; - sreport.pressure_time = st; - - // Optionally, check if well controls are satisfied. - if (check_well_controls_) { - std::cout << "Checking well conditions." << std::endl; - // For testing we set surface := reservoir - well_control_passed = wells_manager_.conditionsMet(well_state.bhp(), well_state.wellRates(), well_state.wellRates()); - ++well_control_iteration; - if (!well_control_passed && well_control_iteration > max_well_control_iterations_) { - OPM_THROW(std::runtime_error, "Could not satisfy well conditions in " << max_well_control_iterations_ << " tries."); - } - if (!well_control_passed) { - std::cout << "Well controls not passed, solving again." << std::endl; - } else { - std::cout << "Well conditions met." << std::endl; - } - } - } while (!well_control_passed); + stime += st; + sreport.pressure_time = st; // Update pore volumes if rock is compressible. if (rock_comp_props_ && rock_comp_props_->isActive()) { - initial_porevol = porevol; computePorevolume(AutoDiffGrid::numCells(grid_), AutoDiffGrid::beginCellVolumes(grid_), props_.porosity(), *rock_comp_props_, state.pressure(), porevol); } @@ -354,9 +345,9 @@ namespace Opm tstep_os.close(); } - // advance to next timestep before reporting at this location - // ++timer; // Commented out since this has temporarily moved to the main() function. - break; // this is a temporary measure + output_writer_.writeTimeStep(timer, state, well_state.basicWellState()); + + ++timer; } total_timer.stop(); From eb3445e6a18a65ff1f44544ac28a77df9d18aed5 Mon Sep 17 00:00:00 2001 From: Kai Bao Date: Sun, 27 Jul 2014 17:31:00 +0200 Subject: [PATCH 3/3] Finishing compliation. --- examples/sim_fibo_ad.cpp | 2 +- opm/autodiff/SimulatorFullyImplicitBlackoil_impl.hpp | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/examples/sim_fibo_ad.cpp b/examples/sim_fibo_ad.cpp index 2e792cce3..b4024e3ce 100644 --- a/examples/sim_fibo_ad.cpp +++ b/examples/sim_fibo_ad.cpp @@ -198,7 +198,7 @@ try // since number of wells may change etc. - SimulatorFullyImplicitBlackoil simulator(param, + SimulatorFullyImplicitBlackoil simulator(param, *grid->c_grid(), *new_props, rock_comp->isActive() ? rock_comp.get() : 0, diff --git a/opm/autodiff/SimulatorFullyImplicitBlackoil_impl.hpp b/opm/autodiff/SimulatorFullyImplicitBlackoil_impl.hpp index 2c4f9eaf3..a5d0d352c 100644 --- a/opm/autodiff/SimulatorFullyImplicitBlackoil_impl.hpp +++ b/opm/autodiff/SimulatorFullyImplicitBlackoil_impl.hpp @@ -65,7 +65,6 @@ namespace Opm const Grid& grid, BlackoilPropsAdInterface& props, const RockCompressibility* rock_comp_props, - WellsManager& wells_manager, NewtonIterationBlackoilInterface& linsolver, const double* gravity, bool has_disgas, @@ -114,7 +113,6 @@ namespace Opm const Grid& grid, BlackoilPropsAdInterface& props, const RockCompressibility* rock_comp_props, - WellsManager& wells_manager, NewtonIterationBlackoilInterface& linsolver, const double* gravity, const bool has_disgas, @@ -123,7 +121,7 @@ namespace Opm EclipseWriter& output_writer) { - pimpl_.reset(new Impl(param, grid, props, rock_comp_props, wells_manager, linsolver, gravity, has_disgas, has_vapoil, eclipse_state, output_writer)); + pimpl_.reset(new Impl(param, grid, props, rock_comp_props, linsolver, gravity, has_disgas, has_vapoil, eclipse_state, output_writer)); } @@ -215,7 +213,7 @@ namespace Opm gravity_(gravity), geo_(grid_, props_, gravity_), // solver_(param, grid_, props_, geo_, rock_comp_props, *wells_manager.c_wells(), linsolver, has_disgas, has_vapoil) - slover_(linsolver), + solver_(linsolver), has_disgas_(has_disgas), has_vapoil_(has_vapoil), eclipse_state_(eclipse_state), @@ -294,7 +292,7 @@ namespace Opm if (timer.currentStepNum() == 0) { well_state.init(wells, state); - output_writer_.writeInit(timer, state, well_state.basicWellState()); + output_writer_.writeInit(timer); } else { // TODO: add a function to update the well_state here. } @@ -310,7 +308,7 @@ namespace Opm SimulatorReport sreport; - FullyImplicitBlackoilSolver solver( param_, grid_, props_, geo_, rock_comp_props_, *wells, linsolver_, has_disgas_, has_vapoil_); + FullyImplicitBlackoilSolver solver(param_, grid_, props_, geo_, rock_comp_props_, *wells, solver_, has_disgas_, has_vapoil_); // Run solver. solver_timer.start();