28 #include <unordered_map>
29 #include <unordered_set>
35 #include <opm/input/eclipse/EclipseState/Runspec.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/GTNode.hpp>
39 #include <opm/input/eclipse/Schedule/Group/GuideRateConfig.hpp>
40 #include <opm/input/eclipse/Schedule/MessageLimits.hpp>
41 #include <opm/input/eclipse/Schedule/Network/ExtNetwork.hpp>
42 #include <opm/input/eclipse/Schedule/RPTConfig.hpp>
43 #include <opm/input/eclipse/Schedule/ScheduleDeck.hpp>
44 #include <opm/input/eclipse/Schedule/ScheduleState.hpp>
45 #include <opm/input/eclipse/Schedule/Well/PAvg.hpp>
46 #include <opm/input/eclipse/Schedule/Well/Well.hpp>
47 #include <opm/input/eclipse/Schedule/Well/WellTestConfig.hpp>
48 #include <opm/input/eclipse/Schedule/Well/WellMatcher.hpp>
49 #include <opm/input/eclipse/Schedule/WriteRestartFileEvents.hpp>
50 #include <opm/input/eclipse/Schedule/CompletedCells.hpp>
51 #include <opm/input/eclipse/Schedule/ScheduleDeck.hpp>
52 #include <opm/input/eclipse/Schedule/ScheduleState.hpp>
53 #include <opm/input/eclipse/Schedule/RPTConfig.hpp>
54 #include <opm/input/eclipse/Schedule/Action/SimulatorUpdate.hpp>
55 #include <opm/input/eclipse/Schedule/Action/WGNames.hpp>
56 #include <opm/input/eclipse/Parser/ParseContext.hpp>
58 #include <opm/input/eclipse/Python/Python.hpp>
60 #include <opm/input/eclipse/Units/UnitSystem.hpp>
64 class ActiveGridCells;
69 class FieldPropsManager;
71 class SCHEDULESection;
76 namespace RestartIO {
struct RstState; }
80 std::shared_ptr<const Python> m_python_handle;
81 std::string m_input_path;
87 std::optional<int> output_interval;
90 bool gaslift_opt_active{
false};
94 explicit ScheduleStatic(std::shared_ptr<const Python> python_handle) :
95 m_python_handle(python_handle)
102 const std::optional<int>& output_interval_,
106 template<
class Serializer>
109 m_deck_message_limits.serializeOp(serializer);
110 this->rst_info.serializeOp(serializer);
111 m_runspec.serializeOp(serializer);
112 m_unit_system.serializeOp(serializer);
113 serializer(this->m_input_path);
114 rst_info.serializeOp(serializer);
115 rst_config.serializeOp(serializer);
116 serializer(this->output_interval);
117 serializer(this->gaslift_opt_active);
122 auto python = std::make_shared<Python>(Python::Enable::OFF);
124 st.m_deck_message_limits = MessageLimits::serializeObject();
125 st.m_runspec = Runspec::serializeObject();
126 st.m_unit_system = UnitSystem::newFIELD();
127 st.m_input_path =
"Some/funny/path";
128 st.rst_config = RSTConfig::serializeObject();
129 st.rst_info = ScheduleRestartInfo::serializeObject();
134 return this->m_input_path == other.m_input_path &&
135 this->m_deck_message_limits == other.m_deck_message_limits &&
136 this->m_unit_system == other.m_unit_system &&
137 this->rst_config == other.rst_config &&
138 this->rst_info == other.rst_info &&
139 this->gaslift_opt_active == other.gaslift_opt_active &&
140 this->m_runspec == other.m_runspec;
148 explicit Schedule(std::shared_ptr<const Python> python_handle);
155 std::shared_ptr<const Python> python,
156 const std::optional<int>& output_interval = {},
167 std::shared_ptr<const Python> python,
168 const std::optional<int>& output_interval = {},
176 std::shared_ptr<const Python> python,
177 const std::optional<int>& output_interval = {},
185 std::shared_ptr<const Python> python,
186 const std::optional<int>& output_interval = {},
189 template <
typename T>
194 std::shared_ptr<const Python> python,
195 const std::optional<int>& output_interval = {},
200 std::shared_ptr<const Python> python,
201 const std::optional<int>& output_interval = {},
207 const std::optional<int>& output_interval = {},
216 time_t getStartTime()
const;
217 time_t posixStartTime()
const;
218 time_t posixEndTime()
const;
219 time_t simTime(std::size_t timeStep)
const;
220 double seconds(std::size_t timeStep)
const;
221 double stepLength(std::size_t timeStep)
const;
222 std::optional<int> exitStatus()
const;
223 const UnitSystem& getUnits()
const {
return this->m_static.m_unit_system; }
224 const Runspec& runspec()
const {
return this->m_static.m_runspec; }
226 std::size_t numWells()
const;
227 std::size_t numWells(std::size_t timestep)
const;
228 bool hasWell(
const std::string& wellName)
const;
229 bool hasWell(
const std::string& wellName, std::size_t timeStep)
const;
231 WellMatcher wellMatcher(std::size_t report_step)
const;
232 std::vector<std::string> wellNames(
const std::string& pattern, std::size_t timeStep,
const std::vector<std::string>& matching_wells = {})
const;
233 std::vector<std::string> wellNames(
const std::string& pattern)
const;
234 std::vector<std::string> wellNames(std::size_t timeStep)
const;
235 std::vector<std::string> wellNames()
const;
237 bool hasGroup(
const std::string& groupName, std::size_t timeStep)
const;
238 std::vector<std::string> groupNames(
const std::string& pattern, std::size_t timeStep)
const;
239 std::vector<std::string> groupNames(std::size_t timeStep)
const;
240 std::vector<std::string> groupNames(
const std::string& pattern)
const;
241 std::vector<std::string> groupNames()
const;
251 std::vector<const Group*> restart_groups(std::size_t timeStep)
const;
253 std::vector<std::string> changed_wells(std::size_t reportStep)
const;
254 const Well& getWell(std::size_t well_index, std::size_t timeStep)
const;
255 const Well& getWell(
const std::string& wellName, std::size_t timeStep)
const;
256 const Well& getWellatEnd(
const std::string& well_name)
const;
257 std::vector<Well> getWells(std::size_t timeStep)
const;
258 std::vector<Well> getWellsatEnd()
const;
259 void shut_well(
const std::string& well_name, std::size_t report_step);
260 void stop_well(
const std::string& well_name, std::size_t report_step);
261 void open_well(
const std::string& well_name, std::size_t report_step);
262 void applyWellProdIndexScaling(
const std::string& well_name,
const std::size_t reportStep,
const double scalingFactor);
264 std::vector<const Group*> getChildGroups2(
const std::string& group_name, std::size_t timeStep)
const;
265 std::vector<Well> getChildWells2(
const std::string& group_name, std::size_t timeStep)
const;
266 Well::ProducerCMode getGlobalWhistctlMmode(std::size_t timestep)
const;
268 const UDQConfig& getUDQConfig(std::size_t timeStep)
const;
269 void evalAction(
const SummaryState& summary_state, std::size_t timeStep);
271 GTNode groupTree(std::size_t report_step)
const;
272 GTNode groupTree(
const std::string& root_node, std::size_t report_step)
const;
273 const Group& getGroup(
const std::string& groupName, std::size_t timeStep)
const;
275 std::optional<std::size_t> first_RFT()
const;
281 std::size_t size()
const;
283 bool write_rst_file(std::size_t report_step)
const;
284 const std::map< std::string, int >& rst_keywords(
size_t timestep )
const;
293 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);
304 const GasLiftOpt& glo(std::size_t report_step)
const;
306 bool operator==(
const Schedule& data)
const;
307 std::shared_ptr<const Python> python()
const;
312 std::vector<ScheduleState>::const_iterator begin()
const;
313 std::vector<ScheduleState>::const_iterator end()
const;
314 void create_next(
const time_point& start_time,
const std::optional<time_point>& end_time);
316 void create_first(
const time_point& start_time,
const std::optional<time_point>& end_time);
325 static bool cmp(
const Schedule& sched1,
const Schedule& sched2, std::size_t report_step);
326 void applyKeywords(std::vector<DeckKeyword*>& keywords, std::size_t timeStep);
328 template<
class Serializer>
331 m_sched_deck.serializeOp(serializer);
332 serializer.vector(snapshots);
333 m_static.serializeOp(serializer);
334 restart_output.serializeOp(serializer);
335 this->completed_cells.serializeOp(serializer);
336 this->action_wgnames.serializeOp(serializer);
338 pack_unpack<PAvg, Serializer>(serializer);
339 pack_unpack<WellTestConfig, Serializer>(serializer);
340 pack_unpack<GConSale, Serializer>(serializer);
341 pack_unpack<GConSump, Serializer>(serializer);
342 pack_unpack<WListManager, Serializer>(serializer);
343 pack_unpack<Network::ExtNetwork, Serializer>(serializer);
344 pack_unpack<Network::Balance, Serializer>(serializer);
345 pack_unpack<RPTConfig, Serializer>(serializer);
346 pack_unpack<Action::Actions, Serializer>(serializer);
347 pack_unpack<UDQActive, Serializer>(serializer);
348 pack_unpack<UDQConfig, Serializer>(serializer);
349 pack_unpack<NameOrder, Serializer>(serializer);
350 pack_unpack<GroupOrder, Serializer>(serializer);
351 pack_unpack<GuideRateConfig, Serializer>(serializer);
352 pack_unpack<GasLiftOpt, Serializer>(serializer);
353 pack_unpack<RFTConfig, Serializer>(serializer);
354 pack_unpack<RSTConfig, Serializer>(serializer);
356 pack_unpack_map<int, VFPProdTable, Serializer>(serializer);
357 pack_unpack_map<int, VFPInjTable, Serializer>(serializer);
358 pack_unpack_map<std::string, Group, Serializer>(serializer);
359 pack_unpack_map<std::string, Well, Serializer>(serializer);
362 template <
typename T,
class Serializer>
364 std::vector<T> value_list;
365 std::vector<std::size_t> index_list;
367 if (serializer.isSerializing())
368 pack_state<T>(value_list, index_list);
370 serializer.vector(value_list);
371 serializer.template vector<std::size_t, false>(index_list);
373 if (!serializer.isSerializing())
374 unpack_state<T>(value_list, index_list);
377 template <
typename T>
378 std::vector<std::pair<std::size_t, T>> unique()
const {
379 std::vector<std::pair<std::size_t, T>> values;
380 for (std::size_t index = 0; index < this->snapshots.size(); index++) {
381 const auto& member = this->snapshots[index].get<T>();
382 const auto& value = member.get();
383 if (values.empty() || !(value == values.back().second))
384 values.push_back( std::make_pair(index, value));
390 template <
typename T>
391 void pack_state(std::vector<T>& value_list, std::vector<std::size_t>& index_list)
const {
392 auto unique_values = this->unique<T>();
393 for (
auto& [index, value] : unique_values) {
394 value_list.push_back( std::move(value) );
395 index_list.push_back( index );
400 template <
typename T>
401 void unpack_state(
const std::vector<T>& value_list,
const std::vector<std::size_t>& index_list) {
402 std::size_t unique_index = 0;
403 while (unique_index < value_list.size()) {
404 const auto& value = value_list[unique_index];
405 const auto& first_index = index_list[unique_index];
406 auto last_index = this->snapshots.size();
407 if (unique_index < (value_list.size() - 1))
408 last_index = index_list[unique_index + 1];
410 auto& target_state = this->snapshots[first_index];
411 target_state.get<T>().update( std::move(value) );
412 for (std::size_t index=first_index + 1; index < last_index; index++)
413 this->snapshots[index].get<T>().update( target_state.get<T>() );
420 template <
typename K,
typename T,
class Serializer>
421 void pack_unpack_map(
Serializer& serializer) {
422 std::vector<T> value_list;
423 std::vector<std::size_t> index_list;
425 if (serializer.isSerializing())
426 pack_map<K,T>(value_list, index_list);
428 serializer.vector(value_list);
429 serializer(index_list);
431 if (!serializer.isSerializing())
432 unpack_map<K,T>(value_list, index_list);
436 template <
typename K,
typename T>
437 void pack_map(std::vector<T>& value_list,
438 std::vector<std::size_t>& index_list) {
440 const auto& last_map = this->snapshots.back().get_map<K,T>();
441 std::vector<K> key_list{ last_map.keys() };
442 std::unordered_map<K,T> current_value;
444 for (std::size_t index = 0; index < this->snapshots.size(); index++) {
445 auto& state = this->snapshots[index];
446 const auto& current_map = state.template get_map<K,T>();
447 for (
const auto& key : key_list) {
448 auto& value = current_map.get_ptr(key);
450 auto it = current_value.find(key);
451 if (it == current_value.end() || !(*value == it->second)) {
452 value_list.push_back( *value );
453 index_list.push_back( index );
455 current_value[key] = *value;
463 template <
typename K,
typename T>
464 void unpack_map(
const std::vector<T>& value_list,
465 const std::vector<std::size_t>& index_list) {
467 std::unordered_map<K, std::vector<std::pair<std::size_t, T>>> storage;
468 for (std::size_t storage_index = 0; storage_index < value_list.size(); storage_index++) {
469 const auto& value = value_list[storage_index];
470 const auto& time_index = index_list[storage_index];
472 storage[ value.name() ].emplace_back( time_index, value );
475 for (
const auto& [key, values] : storage) {
476 for (std::size_t unique_index = 0; unique_index < values.size(); unique_index++) {
477 const auto& [time_index, value] = values[unique_index];
478 auto last_index = this->snapshots.size();
479 if (unique_index < (values.size() - 1))
480 last_index = values[unique_index + 1].first;
482 auto& map_value = this->snapshots[time_index].template get_map<K,T>();
483 map_value.update(std::move(value));
485 for (std::size_t index=time_index + 1; index < last_index; index++) {
486 auto& forward_map = this->snapshots[index].template get_map<K,T>();
487 forward_map.update( key, map_value );
493 friend std::ostream& operator<<(std::ostream& os,
const Schedule& sched);
494 void dump_deck(std::ostream& os)
const;
497 struct HandlerContext {
500 const std::size_t currentStep;
501 const std::vector<std::string>& matching_wells;
502 const bool actionx_mode;
506 const std::unordered_map<std::string, double> * target_wellpi;
512 const std::size_t currentStep_,
513 const std::vector<std::string>& matching_wells_,
518 const std::unordered_map<std::string, double> * target_wellpi_)
521 , currentStep(currentStep_)
522 , matching_wells(matching_wells_)
523 , actionx_mode(actionx_mode_)
524 , parseContext(parseContext_)
526 , sim_update(sim_update_)
527 , target_wellpi(target_wellpi_)
531 void affected_well(
const std::string& well_name) {
532 if (this->sim_update)
533 this->sim_update->affected_wells.insert(well_name);
541 std::optional<int> exit_status;
542 std::vector<ScheduleState> snapshots;
550 void addWell(
Well well);
551 void addWell(
const std::string& wellName,
552 const std::string& group,
555 Phase preferredPhase,
556 const std::optional<double>& refDepth,
557 double drainageRadius,
559 bool automaticShutIn,
561 Well::GasInflowEquation gas_inflow,
562 std::size_t timeStep,
563 Connection::Order wellConnectionOrder);
564 bool updateWPAVE(
const std::string& wname, std::size_t report_step,
const PAvg& pavg);
566 void updateGuideRateModel(
const GuideRateModel& new_model, std::size_t report_step);
567 GTNode groupTree(
const std::string& root_node, std::size_t report_step, std::size_t level,
const std::optional<std::string>& parent_name)
const;
569 bool updateWellStatus(
const std::string& well, std::size_t reportStep, Well::Status status, std::optional<KeywordLocation> = {});
570 void addWellToGroup(
const std::string& group_name,
const std::string& well_name , std::size_t timeStep);
571 void iterateScheduleSection(std::size_t load_start,
572 std::size_t load_end,
576 const std::unordered_map<std::string, double> * target_wellpi,
577 const std::string& prefix);
579 void addGroupToGroup(
const std::string& parent_group,
const std::string& child_group);
580 void addGroup(
const std::string& groupName , std::size_t timeStep);
581 void addGroup(
Group group);
583 void addWell(
const std::string& wellName,
const DeckRecord& record, std::size_t timeStep, Connection::Order connection_order);
584 void checkIfAllConnectionsIsShut(std::size_t currentStep);
585 void end_report(std::size_t report_step);
586 void handleKeyword(std::size_t currentStep,
591 const std::vector<std::string>& matching_wells,
594 const std::unordered_map<std::string, double> * target_wellpi);
598 std::vector<std::string> wellNames(
const std::string& pattern,
const HandlerContext& context);
599 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;
600 void invalidNamePattern(
const std::string& namePattern,
const HandlerContext& context)
const;
601 static std::string formatDate(std::time_t t);
602 std::string simulationDays(std::size_t currentStep)
const;
604 bool must_write_rst_file(std::size_t report_step)
const;
606 void applyEXIT(
const DeckKeyword&, std::size_t currentStep);
607 SimulatorUpdate applyAction(std::size_t reportStep,
const std::string& action_name,
const std::vector<std::string>& matching_wells);
624 bool handleNormalKeyword(HandlerContext& handlerContext);
628 void handleWELPIRuntime(HandlerContext&);
632 void handleBRANPROP (HandlerContext&);
633 void handleCOMPDAT (HandlerContext&);
634 void handleCOMPLUMP (HandlerContext&);
635 void handleCOMPORD (HandlerContext&);
636 void handleCOMPSEGS (HandlerContext&);
637 void handleDRSDT (HandlerContext&);
638 void handleDRSDTCON (HandlerContext&);
639 void handleDRSDTR (HandlerContext&);
640 void handleDRVDT (HandlerContext&);
641 void handleDRVDTR (HandlerContext&);
642 void handleEXIT (HandlerContext&);
643 void handleGCONINJE (HandlerContext&);
644 void handleGCONPROD (HandlerContext&);
645 void handleGCONSALE (HandlerContext&);
646 void handleGCONSUMP (HandlerContext&);
647 void handleGEFAC (HandlerContext&);
648 void handleGEOKeyword(HandlerContext&);
649 void handleGLIFTOPT (HandlerContext&);
650 void handleGPMAINT (HandlerContext&);
651 void handleGRUPNET (HandlerContext&);
652 void handleGRUPTREE (HandlerContext&);
653 void handleGUIDERAT (HandlerContext&);
654 void handleLIFTOPT (HandlerContext&);
655 void handleLINCOM (HandlerContext&);
656 void handleMESSAGES (HandlerContext&);
657 void handleMXUNSUPP (HandlerContext&);
658 void handleNETBALAN (HandlerContext&);
659 void handleNEXTSTEP (HandlerContext&);
660 void handleNODEPROP (HandlerContext&);
661 void handleNUPCOL (HandlerContext&);
662 void handleRPTONLY (HandlerContext&);
663 void handleRPTONLYO (HandlerContext&);
664 void handleRPTRST (HandlerContext&);
665 void handleRPTSCHED (HandlerContext&);
666 void handleTUNING (HandlerContext&);
667 void handleSAVE (HandlerContext&);
668 void handleSUMTHIN (HandlerContext&);
669 void handleUDQ (HandlerContext&);
670 void handleVAPPARS (HandlerContext&);
671 void handleVFPINJ (HandlerContext&);
672 void handleVFPPROD (HandlerContext&);
673 void handleWCONHIST (HandlerContext&);
674 void handleWCONINJE (HandlerContext&);
675 void handleWCONINJH (HandlerContext&);
676 void handleWCONPROD (HandlerContext&);
677 void handleWECON (HandlerContext&);
678 void handleWEFAC (HandlerContext&);
679 void handleWELOPEN (HandlerContext&);
680 void handleWELPI (HandlerContext&);
681 void handleWELSEGS (HandlerContext&);
682 void handleWELSPECS (HandlerContext&);
683 void handleWELTARG (HandlerContext&);
684 void handleWFOAM (HandlerContext&);
685 void handleWGRUPCON (HandlerContext&);
686 void handleWHISTCTL (HandlerContext&);
687 void handleWINJTEMP (HandlerContext&);
688 void handleWLIFTOPT (HandlerContext&);
689 void handleWLIST (HandlerContext&);
690 void handleWMICP (HandlerContext&);
691 void handleWPAVE (HandlerContext&);
692 void handleWPAVEDEP (HandlerContext&);
693 void handleWVFPEXP (HandlerContext&);
694 void handleWWPAVE (HandlerContext&);
695 void handleWPIMULT (HandlerContext&);
696 void handleWPMITAB (HandlerContext&);
697 void handleWPOLYMER (HandlerContext&);
698 void handleWRFT (HandlerContext&);
699 void handleWRFTPLT (HandlerContext&);
700 void handleWSALT (HandlerContext&);
701 void handleWSEGITER (HandlerContext&);
702 void handleWSEGSICD (HandlerContext&);
703 void handleWSEGAICD (HandlerContext&);
704 void handleWSEGVALV (HandlerContext&);
705 void handleWSKPTAB (HandlerContext&);
706 void handleWSOLVENT (HandlerContext&);
707 void handleWTEMP (HandlerContext&);
708 void handleWTEST (HandlerContext&);
709 void handleWTMULT (HandlerContext&);
710 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:35
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:439
Definition: ScheduleDeck.hpp:55
Definition: ScheduleDeck.hpp:125
Definition: ScheduleGrid.hpp:29
Definition: ScheduleState.hpp:81
Definition: Schedule.hpp:145
Definition: Serializer.hpp:38
Definition: SummaryState.hpp:69
Definition: TracerConfig.hpp:31
Definition: UDQConfig.hpp:51
Definition: UnitSystem.hpp:34
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:93
Definition: Schedule.hpp:79
Definition: SimulatorUpdate.hpp:35