29 #include <unordered_map>
33 #include <opm/input/eclipse/EclipseState/Runspec.hpp>
34 #include <opm/input/eclipse/Parser/InputErrorAction.hpp>
35 #include <opm/input/eclipse/Python/Python.hpp>
36 #include <opm/input/eclipse/Schedule/GasLiftOpt.hpp>
37 #include <opm/input/eclipse/Schedule/Group/Group.hpp>
38 #include <opm/input/eclipse/Schedule/Group/GuideRateConfig.hpp>
39 #include <opm/input/eclipse/Schedule/MessageLimits.hpp>
40 #include <opm/input/eclipse/Schedule/Network/ExtNetwork.hpp>
41 #include <opm/input/eclipse/Schedule/RPTConfig.hpp>
42 #include <opm/input/eclipse/Schedule/ScheduleDeck.hpp>
43 #include <opm/input/eclipse/Schedule/ScheduleState.hpp>
44 #include <opm/input/eclipse/Schedule/Well/PAvg.hpp>
45 #include <opm/input/eclipse/Schedule/Well/Well.hpp>
46 #include <opm/input/eclipse/Schedule/Well/WellTestConfig.hpp>
47 #include <opm/input/eclipse/Schedule/WriteRestartFileEvents.hpp>
48 #include <opm/input/eclipse/Schedule/CompletedCells.hpp>
49 #include <opm/input/eclipse/Schedule/Action/SimulatorUpdate.hpp>
50 #include <opm/input/eclipse/Schedule/Action/WGNames.hpp>
51 #include <opm/input/eclipse/Units/UnitSystem.hpp>
55 class ActiveGridCells;
60 class FieldPropsManager;
63 class SCHEDULESection;
69 namespace RestartIO {
struct RstState; }
73 std::shared_ptr<const Python> m_python_handle;
74 std::string m_input_path;
80 std::optional<int> output_interval;
83 bool gaslift_opt_active{
false};
87 explicit ScheduleStatic(std::shared_ptr<const Python> python_handle) :
88 m_python_handle(python_handle)
95 const std::optional<int>& output_interval_,
99 template<
class Serializer>
102 serializer(m_deck_message_limits);
103 serializer(this->rst_info);
104 serializer(m_runspec);
105 serializer(m_unit_system);
106 serializer(this->m_input_path);
107 serializer(rst_info);
108 serializer(rst_config);
109 serializer(this->output_interval);
110 serializer(this->gaslift_opt_active);
115 auto python = std::make_shared<Python>(Python::Enable::OFF);
117 st.m_deck_message_limits = MessageLimits::serializationTestObject();
118 st.m_runspec = Runspec::serializationTestObject();
119 st.m_unit_system = UnitSystem::newFIELD();
120 st.m_input_path =
"Some/funny/path";
121 st.rst_config = RSTConfig::serializationTestObject();
122 st.rst_info = ScheduleRestartInfo::serializationTestObject();
127 return this->m_input_path == other.m_input_path &&
128 this->m_deck_message_limits == other.m_deck_message_limits &&
129 this->m_unit_system == other.m_unit_system &&
130 this->rst_config == other.rst_config &&
131 this->rst_info == other.rst_info &&
132 this->gaslift_opt_active == other.gaslift_opt_active &&
133 this->m_runspec == other.m_runspec;
141 explicit Schedule(std::shared_ptr<const Python> python_handle);
148 std::shared_ptr<const Python> python,
149 const std::optional<int>& output_interval = {},
160 std::shared_ptr<const Python> python,
161 const std::optional<int>& output_interval = {},
169 std::shared_ptr<const Python> python,
170 const std::optional<int>& output_interval = {},
178 std::shared_ptr<const Python> python,
179 const std::optional<int>& output_interval = {},
182 template <
typename T>
187 std::shared_ptr<const Python> python,
188 const std::optional<int>& output_interval = {},
193 std::shared_ptr<const Python> python,
194 const std::optional<int>& output_interval = {},
200 const std::optional<int>& output_interval = {},
203 static Schedule serializationTestObject();
209 std::time_t getStartTime()
const;
210 std::time_t posixStartTime()
const;
211 std::time_t posixEndTime()
const;
212 std::time_t simTime(std::size_t timeStep)
const;
213 double seconds(std::size_t timeStep)
const;
214 double stepLength(std::size_t timeStep)
const;
215 std::optional<int> exitStatus()
const;
216 const UnitSystem& getUnits()
const {
return this->m_static.m_unit_system; }
217 const Runspec& runspec()
const {
return this->m_static.m_runspec; }
219 std::size_t numWells()
const;
220 std::size_t numWells(std::size_t timestep)
const;
221 bool hasWell(
const std::string& wellName)
const;
222 bool hasWell(
const std::string& wellName, std::size_t timeStep)
const;
224 WellMatcher wellMatcher(std::size_t report_step)
const;
225 std::vector<std::string> wellNames(
const std::string& pattern, std::size_t timeStep,
const std::vector<std::string>& matching_wells = {})
const;
226 std::vector<std::string> wellNames(
const std::string& pattern)
const;
227 std::vector<std::string> wellNames(std::size_t timeStep)
const;
228 std::vector<std::string> wellNames()
const;
230 bool hasGroup(
const std::string& groupName, std::size_t timeStep)
const;
231 std::vector<std::string> groupNames(
const std::string& pattern, std::size_t timeStep)
const;
232 std::vector<std::string> groupNames(std::size_t timeStep)
const;
233 std::vector<std::string> groupNames(
const std::string& pattern)
const;
234 std::vector<std::string> groupNames()
const;
244 std::vector<const Group*> restart_groups(std::size_t timeStep)
const;
246 std::vector<std::string> changed_wells(std::size_t reportStep)
const;
247 const Well& getWell(std::size_t well_index, std::size_t timeStep)
const;
248 const Well& getWell(
const std::string& wellName, std::size_t timeStep)
const;
249 const Well& getWellatEnd(
const std::string& well_name)
const;
250 std::vector<Well> getWells(std::size_t timeStep)
const;
251 std::vector<Well> getWellsatEnd()
const;
252 void shut_well(
const std::string& well_name, std::size_t report_step);
253 void stop_well(
const std::string& well_name, std::size_t report_step);
254 void open_well(
const std::string& well_name, std::size_t report_step);
255 void applyWellProdIndexScaling(
const std::string& well_name,
const std::size_t reportStep,
const double scalingFactor);
257 std::vector<const Group*> getChildGroups2(
const std::string& group_name, std::size_t timeStep)
const;
258 std::vector<Well> getChildWells2(
const std::string& group_name, std::size_t timeStep)
const;
259 Well::ProducerCMode getGlobalWhistctlMmode(std::size_t timestep)
const;
261 const UDQConfig& getUDQConfig(std::size_t timeStep)
const;
262 void evalAction(
const SummaryState& summary_state, std::size_t timeStep);
264 GTNode groupTree(std::size_t report_step)
const;
265 GTNode groupTree(
const std::string& root_node, std::size_t report_step)
const;
266 const Group& getGroup(
const std::string& groupName, std::size_t timeStep)
const;
268 std::optional<std::size_t> first_RFT()
const;
274 std::size_t size()
const;
276 bool write_rst_file(std::size_t report_step)
const;
277 const std::map< std::string, int >& rst_keywords(
size_t timestep )
const;
286 SimulatorUpdate applyAction(std::size_t reportStep,
const Action::ActionX& action,
const std::vector<std::string>& matching_wells,
const std::unordered_map<std::string, double>& wellpi);
297 const GasLiftOpt& glo(std::size_t report_step)
const;
299 bool operator==(
const Schedule& data)
const;
300 std::shared_ptr<const Python> python()
const;
305 std::vector<ScheduleState>::const_iterator begin()
const;
306 std::vector<ScheduleState>::const_iterator end()
const;
307 void create_next(
const time_point& start_time,
const std::optional<time_point>& end_time);
309 void create_first(
const time_point& start_time,
const std::optional<time_point>& end_time);
318 static bool cmp(
const Schedule& sched1,
const Schedule& sched2, std::size_t report_step);
319 void applyKeywords(std::vector<DeckKeyword*>& keywords, std::size_t timeStep);
321 template<
class Serializer>
324 serializer(this->m_static);
325 serializer(this->m_sched_deck);
326 serializer(this->action_wgnames);
327 serializer(this->exit_status);
328 serializer(this->snapshots);
329 serializer(this->restart_output);
330 serializer(this->completed_cells);
332 this->
template pack_unpack<PAvg>(serializer);
333 this->
template pack_unpack<WellTestConfig>(serializer);
334 this->
template pack_unpack<GConSale>(serializer);
335 this->
template pack_unpack<GConSump>(serializer);
336 this->
template pack_unpack<WListManager>(serializer);
337 this->
template pack_unpack<Network::ExtNetwork>(serializer);
338 this->
template pack_unpack<Network::Balance>(serializer);
339 this->
template pack_unpack<RPTConfig>(serializer);
340 this->
template pack_unpack<Action::Actions>(serializer);
341 this->
template pack_unpack<UDQActive>(serializer);
342 this->
template pack_unpack<UDQConfig>(serializer);
343 this->
template pack_unpack<NameOrder>(serializer);
344 this->
template pack_unpack<GroupOrder>(serializer);
345 this->
template pack_unpack<GuideRateConfig>(serializer);
346 this->
template pack_unpack<GasLiftOpt>(serializer);
347 this->
template pack_unpack<RFTConfig>(serializer);
348 this->
template pack_unpack<RSTConfig>(serializer);
350 this->
template pack_unpack_map<int, VFPProdTable>(serializer);
351 this->
template pack_unpack_map<int, VFPInjTable>(serializer);
352 this->
template pack_unpack_map<std::string, Group>(serializer);
353 this->
template pack_unpack_map<std::string, Well>(serializer);
356 template <
typename T,
class Serializer>
358 std::vector<T> value_list;
359 std::vector<std::size_t> index_list;
362 this->
template pack_state<T>(value_list, index_list);
364 serializer(value_list);
365 serializer(index_list);
368 this->
template unpack_state<T>(value_list, index_list);
371 template <
typename T>
372 std::vector<std::pair<std::size_t, T>> unique()
const {
373 std::vector<std::pair<std::size_t, T>> values;
374 for (std::size_t index = 0; index < this->snapshots.size(); index++) {
375 const auto& member = this->snapshots[index].get<T>();
376 const auto& value = member.get();
377 if (values.empty() || !(value == values.back().second))
378 values.push_back( std::make_pair(index, value));
384 template <
typename T>
385 void pack_state(std::vector<T>& value_list, std::vector<std::size_t>& index_list)
const {
386 auto unique_values = this->
template unique<T>();
387 for (
auto& [index, value] : unique_values) {
388 value_list.push_back( std::move(value) );
389 index_list.push_back( index );
394 template <
typename T>
395 void unpack_state(
const std::vector<T>& value_list,
const std::vector<std::size_t>& index_list) {
396 std::size_t unique_index = 0;
397 while (unique_index < value_list.size()) {
398 const auto& value = value_list[unique_index];
399 const auto& first_index = index_list[unique_index];
400 auto last_index = this->snapshots.size();
401 if (unique_index < (value_list.size() - 1))
402 last_index = index_list[unique_index + 1];
404 auto& target_state = this->snapshots[first_index];
405 target_state.get<T>().update( std::move(value) );
406 for (std::size_t index=first_index + 1; index < last_index; index++)
407 this->snapshots[index].get<T>().update( target_state.get<T>() );
414 template <
typename K,
typename T,
class Serializer>
415 void pack_unpack_map(
Serializer& serializer) {
416 std::vector<T> value_list;
417 std::vector<std::size_t> index_list;
420 pack_map<K,T>(value_list, index_list);
422 serializer(value_list);
423 serializer(index_list);
426 unpack_map<K,T>(value_list, index_list);
430 template <
typename K,
typename T>
431 void pack_map(std::vector<T>& value_list,
432 std::vector<std::size_t>& index_list) {
434 const auto& last_map = this->snapshots.back().get_map<K,T>();
435 std::vector<K> key_list{ last_map.keys() };
436 std::unordered_map<K,T> current_value;
438 for (std::size_t index = 0; index < this->snapshots.size(); index++) {
439 auto& state = this->snapshots[index];
440 const auto& current_map = state.template get_map<K,T>();
441 for (
const auto& key : key_list) {
442 auto& value = current_map.get_ptr(key);
444 auto it = current_value.find(key);
445 if (it == current_value.end() || !(*value == it->second)) {
446 value_list.push_back( *value );
447 index_list.push_back( index );
449 current_value[key] = *value;
457 template <
typename K,
typename T>
458 void unpack_map(
const std::vector<T>& value_list,
459 const std::vector<std::size_t>& index_list) {
461 std::unordered_map<K, std::vector<std::pair<std::size_t, T>>> storage;
462 for (std::size_t storage_index = 0; storage_index < value_list.size(); storage_index++) {
463 const auto& value = value_list[storage_index];
464 const auto& time_index = index_list[storage_index];
466 storage[ value.name() ].emplace_back( time_index, value );
469 for (
const auto& [key, values] : storage) {
470 for (std::size_t unique_index = 0; unique_index < values.size(); unique_index++) {
471 const auto& [time_index, value] = values[unique_index];
472 auto last_index = this->snapshots.size();
473 if (unique_index < (values.size() - 1))
474 last_index = values[unique_index + 1].first;
476 auto& map_value = this->snapshots[time_index].template get_map<K,T>();
477 map_value.update(std::move(value));
479 for (std::size_t index=time_index + 1; index < last_index; index++) {
480 auto& forward_map = this->snapshots[index].template get_map<K,T>();
481 forward_map.update( key, map_value );
487 friend std::ostream& operator<<(std::ostream& os,
const Schedule& sched);
488 void dump_deck(std::ostream& os)
const;
491 struct HandlerContext {
494 const std::size_t currentStep;
495 const std::vector<std::string>& matching_wells;
496 const bool actionx_mode;
500 const std::unordered_map<std::string, double> * target_wellpi;
501 std::unordered_map<std::string, double>* wpimult_global_factor;
507 const std::size_t currentStep_,
508 const std::vector<std::string>& matching_wells_,
513 const std::unordered_map<std::string, double> * target_wellpi_,
514 std::unordered_map<std::string, double>* wpimult_global_factor_)
517 , currentStep(currentStep_)
518 , matching_wells(matching_wells_)
519 , actionx_mode(actionx_mode_)
520 , parseContext(parseContext_)
522 , sim_update(sim_update_)
523 , target_wellpi(target_wellpi_)
524 , wpimult_global_factor(wpimult_global_factor_)
528 void affected_well(
const std::string& well_name) {
529 if (this->sim_update)
530 this->sim_update->affected_wells.insert(well_name);
543 std::optional<int> exit_status;
544 std::vector<ScheduleState> snapshots;
552 void addWell(
Well well);
553 void addWell(
const std::string& wellName,
554 const std::string& group,
557 Phase preferredPhase,
558 const std::optional<double>& refDepth,
559 double drainageRadius,
561 bool automaticShutIn,
563 Well::GasInflowEquation gas_inflow,
564 std::size_t timeStep,
565 Connection::Order wellConnectionOrder);
566 bool updateWPAVE(
const std::string& wname, std::size_t report_step,
const PAvg& pavg);
568 void updateGuideRateModel(
const GuideRateModel& new_model, std::size_t report_step);
569 GTNode groupTree(
const std::string& root_node, std::size_t report_step, std::size_t level,
const std::optional<std::string>& parent_name)
const;
571 bool updateWellStatus(
const std::string& well, std::size_t reportStep, Well::Status status, std::optional<KeywordLocation> = {});
572 void addWellToGroup(
const std::string& group_name,
const std::string& well_name , std::size_t timeStep);
573 void iterateScheduleSection(std::size_t load_start,
574 std::size_t load_end,
578 const std::unordered_map<std::string, double> * target_wellpi,
579 const std::string& prefix,
580 const bool log_to_debug =
false);
582 void addGroupToGroup(
const std::string& parent_group,
const std::string& child_group);
583 void addGroup(
const std::string& groupName , std::size_t timeStep);
584 void addGroup(
Group group);
586 void addWell(
const std::string& wellName,
const DeckRecord& record, std::size_t timeStep, Connection::Order connection_order);
587 void checkIfAllConnectionsIsShut(std::size_t currentStep);
588 void end_report(std::size_t report_step);
589 void handleKeyword(std::size_t currentStep,
595 const std::vector<std::string>& matching_wells,
598 const std::unordered_map<std::string, double>* target_wellpi,
599 std::unordered_map<std::string, double>* wpimult_global_factor =
nullptr);
603 std::vector<std::string> wellNames(
const std::string& pattern,
const HandlerContext& context);
604 std::vector<std::string> wellNames(
const std::string& pattern, std::size_t timeStep,
const std::vector<std::string>& matching_wells, InputError::Action error_action,
ErrorGuard& errors,
const KeywordLocation& location)
const;
605 void invalidNamePattern(
const std::string& namePattern,
const HandlerContext& context)
const;
606 static std::string formatDate(std::time_t t);
607 std::string simulationDays(std::size_t currentStep)
const;
608 void applyGlobalWPIMULT(
const std::unordered_map<std::string, double>& wpimult_global_factor);
610 bool must_write_rst_file(std::size_t report_step)
const;
612 void applyEXIT(
const DeckKeyword&, std::size_t currentStep);
613 SimulatorUpdate applyAction(std::size_t reportStep,
const std::string& action_name,
const std::vector<std::string>& matching_wells);
630 bool handleNormalKeyword(HandlerContext& handlerContext);
634 void handleWELPIRuntime(HandlerContext&);
638 void handleBRANPROP (HandlerContext&);
639 void handleCOMPDAT (HandlerContext&);
640 void handleCOMPLUMP (HandlerContext&);
641 void handleCOMPORD (HandlerContext&);
642 void handleCOMPSEGS (HandlerContext&);
643 void handleDRSDT (HandlerContext&);
644 void handleDRSDTCON (HandlerContext&);
645 void handleDRSDTR (HandlerContext&);
646 void handleDRVDT (HandlerContext&);
647 void handleDRVDTR (HandlerContext&);
648 void handleEXIT (HandlerContext&);
649 void handleGCONINJE (HandlerContext&);
650 void handleGCONPROD (HandlerContext&);
651 void handleGCONSALE (HandlerContext&);
652 void handleGCONSUMP (HandlerContext&);
653 void handleGEFAC (HandlerContext&);
654 void handleGEOKeyword(HandlerContext&);
655 void handleGLIFTOPT (HandlerContext&);
656 void handleGPMAINT (HandlerContext&);
657 void handleGRUPNET (HandlerContext&);
658 void handleGRUPTREE (HandlerContext&);
659 void handleGUIDERAT (HandlerContext&);
660 void handleLIFTOPT (HandlerContext&);
661 void handleLINCOM (HandlerContext&);
662 void handleMESSAGES (HandlerContext&);
663 void handleMXUNSUPP (HandlerContext&);
664 void handleNETBALAN (HandlerContext&);
665 void handleNEXTSTEP (HandlerContext&);
666 void handleNODEPROP (HandlerContext&);
667 void handleNUPCOL (HandlerContext&);
668 void handleRPTONLY (HandlerContext&);
669 void handleRPTONLYO (HandlerContext&);
670 void handleRPTRST (HandlerContext&);
671 void handleRPTSCHED (HandlerContext&);
672 void handleTUNING (HandlerContext&);
673 void handleSAVE (HandlerContext&);
674 void handleSUMTHIN (HandlerContext&);
675 void handleUDQ (HandlerContext&);
676 void handleVAPPARS (HandlerContext&);
677 void handleVFPINJ (HandlerContext&);
678 void handleVFPPROD (HandlerContext&);
679 void handleWCONHIST (HandlerContext&);
680 void handleWCONINJE (HandlerContext&);
681 void handleWCONINJH (HandlerContext&);
682 void handleWCONPROD (HandlerContext&);
683 void handleWECON (HandlerContext&);
684 void handleWEFAC (HandlerContext&);
685 void handleWELOPEN (HandlerContext&);
686 void handleWELPI (HandlerContext&);
687 void handleWELSEGS (HandlerContext&);
688 void handleWELSPECS (HandlerContext&);
689 void handleWELTARG (HandlerContext&);
690 void handleWFOAM (HandlerContext&);
691 void handleWGRUPCON (HandlerContext&);
692 void handleWHISTCTL (HandlerContext&);
693 void handleWINJTEMP (HandlerContext&);
694 void handleWLIFTOPT (HandlerContext&);
695 void handleWLIST (HandlerContext&);
696 void handleWMICP (HandlerContext&);
697 void handleWPAVE (HandlerContext&);
698 void handleWPAVEDEP (HandlerContext&);
699 void handleWVFPEXP (HandlerContext&);
700 void handleWWPAVE (HandlerContext&);
701 void handleWPIMULT (HandlerContext&);
702 void handleWPMITAB (HandlerContext&);
703 void handleWPOLYMER (HandlerContext&);
704 void handleWRFT (HandlerContext&);
705 void handleWRFTPLT (HandlerContext&);
706 void handleWSALT (HandlerContext&);
707 void handleWSEGITER (HandlerContext&);
708 void handleWSEGSICD (HandlerContext&);
709 void handleWSEGAICD (HandlerContext&);
710 void handleWSEGVALV (HandlerContext&);
711 void handleWSKPTAB (HandlerContext&);
712 void handleWSOLVENT (HandlerContext&);
713 void handleWTEMP (HandlerContext&);
714 void handleWTEST (HandlerContext&);
715 void handleWTMULT (HandlerContext&);
716 void handleWTRACER (HandlerContext&);
Definition: ActionX.hpp:74
Definition: PyAction.hpp:41
Definition: WGNames.hpp:29
Simple class capturing active cells of a grid.
Definition: ActiveGridCells.hpp:36
Definition: CompletedCells.hpp:28
Definition: DeckKeyword.hpp:36
Definition: DeckRecord.hpp:32
About cell information and dimension: The actual grid information is held in a pointer to an ERT ecl_...
Definition: EclipseGrid.hpp:54
Definition: EclipseState.hpp:55
Definition: ErrorGuard.hpp:29
Definition: FieldPropsManager.hpp:38
Definition: GTNode.hpp:31
Definition: GasLiftOpt.hpp:30
Definition: GuideRateModel.hpp:29
Definition: KeywordLocation.hpp:27
Definition: MessageLimits.hpp:28
Definition: ParseContext.hpp:88
Definition: RSTConfig.hpp:196
Definition: Runspec.hpp:453
Definition: ScheduleDeck.hpp:54
Definition: ScheduleDeck.hpp:124
Definition: ScheduleGrid.hpp:29
Definition: ScheduleState.hpp:81
Definition: Schedule.hpp:138
Class for (de-)serializing.
Definition: Serializer.hpp:75
bool isSerializing() const
Returns true if we are currently doing a serialization operation.
Definition: Serializer.hpp:174
Definition: SummaryState.hpp:69
Definition: TracerConfig.hpp:31
Definition: UDQConfig.hpp:51
Definition: UnitSystem.hpp:33
Definition: WellMatcher.hpp:32
Definition: WriteRestartFileEvents.hpp:31
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:29
Definition: ScheduleDeck.hpp:92
Definition: Schedule.hpp:72
Definition: SimulatorUpdate.hpp:35