138 bool operator()(
const std::pair<std::string,KeywordLocation>& pair,
139 const std::string& str)
const
141 return std::get<0>(pair) < str;
143 bool operator()(
const std::pair<std::string,KeywordLocation>& pair1,
144 const std::pair<std::string,KeywordLocation>& pair2)
const
146 return std::get<0>(pair1) < std::get<0>(pair2);
148 bool operator()(
const std::string& str,
149 const std::pair<std::string,KeywordLocation>& pair)
const
151 return str < std::get<0>(pair);
155 using WelSegsSet = std::set<std::pair<std::string,KeywordLocation>,
PairComp>;
158 explicit Schedule(std::shared_ptr<const Python> python_handle);
165 std::shared_ptr<const Python> python,
166 const std::optional<int>& output_interval = {},
177 std::shared_ptr<const Python> python,
178 const std::optional<int>& output_interval = {},
179 const RestartIO::RstState* rst =
nullptr,
180 const TracerConfig* tracer_config =
nullptr);
182 Schedule(
const Deck& deck,
183 const EclipseGrid& grid,
184 const FieldPropsManager& fp,
185 const Runspec &runspec,
186 std::shared_ptr<const Python> python,
187 const std::optional<int>& output_interval = {},
188 const RestartIO::RstState* rst =
nullptr,
189 const TracerConfig* tracer_config =
nullptr);
191 Schedule(
const Deck& deck,
192 const EclipseState& es,
193 const ParseContext& parseContext,
195 std::shared_ptr<const Python> python,
196 const std::optional<int>& output_interval = {},
197 const RestartIO::RstState* rst =
nullptr);
199 template <
typename T>
200 Schedule(
const Deck& deck,
201 const EclipseState& es,
202 const ParseContext& parseContext,
204 std::shared_ptr<const Python> python,
205 const std::optional<int>& output_interval = {},
206 const RestartIO::RstState* rst =
nullptr);
208 Schedule(
const Deck& deck,
209 const EclipseState& es,
210 std::shared_ptr<const Python> python,
211 const std::optional<int>& output_interval = {},
212 const RestartIO::RstState* rst =
nullptr);
215 Schedule(
const Deck& deck,
216 const EclipseState& es,
217 const std::optional<int>& output_interval = {},
218 const RestartIO::RstState* rst =
nullptr);
220 static Schedule serializationTestObject();
226 std::time_t getStartTime()
const;
227 std::time_t posixStartTime()
const;
228 std::time_t posixEndTime()
const;
229 std::time_t simTime(std::size_t timeStep)
const;
230 double seconds(std::size_t timeStep)
const;
231 double stepLength(std::size_t timeStep)
const;
232 std::optional<int> exitStatus()
const;
233 const UnitSystem& getUnits()
const {
return this->m_static.m_unit_system; }
234 const Runspec& runspec()
const {
return this->m_static.m_runspec; }
236 std::size_t numWells()
const;
237 std::size_t numWells(std::size_t timestep)
const;
238 bool hasWell(
const std::string& wellName)
const;
239 bool hasWell(
const std::string& wellName, std::size_t timeStep)
const;
241 WellMatcher wellMatcher(std::size_t report_step)
const;
242 std::function<std::unique_ptr<SegmentMatcher>()> segmentMatcherFactory(std::size_t report_step)
const;
243 std::vector<std::string> wellNames(
const std::string& pattern, std::size_t timeStep,
const std::vector<std::string>& matching_wells = {})
const;
244 std::vector<std::string> wellNames(
const std::string& pattern)
const;
245 std::vector<std::string> wellNames(std::size_t timeStep)
const;
246 std::vector<std::string> wellNames()
const;
248 bool hasGroup(
const std::string& groupName, std::size_t timeStep)
const;
249 std::vector<std::string> groupNames(
const std::string& pattern, std::size_t timeStep)
const;
250 std::vector<std::string> groupNames(std::size_t timeStep)
const;
251 std::vector<std::string> groupNames(
const std::string& pattern)
const;
252 std::vector<std::string> groupNames()
const;
262 std::vector<const Group*> restart_groups(std::size_t timeStep)
const;
264 std::vector<std::string> changed_wells(std::size_t reportStep)
const;
265 const Well& getWell(std::size_t well_index, std::size_t timeStep)
const;
266 const Well& getWell(
const std::string& wellName, std::size_t timeStep)
const;
267 const Well& getWellatEnd(
const std::string& well_name)
const;
269 std::unordered_set<int> getAquiferFluxSchedule()
const;
270 std::vector<Well> getWells(std::size_t timeStep)
const;
271 std::vector<Well> getWellsatEnd()
const;
272 void shut_well(
const std::string& well_name, std::size_t report_step);
273 void stop_well(
const std::string& well_name, std::size_t report_step);
274 void open_well(
const std::string& well_name, std::size_t report_step);
275 void applyWellProdIndexScaling(
const std::string& well_name,
const std::size_t reportStep,
const double scalingFactor);
277 std::vector<const Group*> getChildGroups2(
const std::string& group_name, std::size_t timeStep)
const;
278 std::vector<Well> getChildWells2(
const std::string& group_name, std::size_t timeStep)
const;
279 WellProducerCMode getGlobalWhistctlMmode(std::size_t timestep)
const;
281 const UDQConfig& getUDQConfig(std::size_t timeStep)
const;
282 void evalAction(
const SummaryState& summary_state, std::size_t timeStep);
284 GTNode groupTree(std::size_t report_step)
const;
285 GTNode groupTree(
const std::string& root_node, std::size_t report_step)
const;
286 const Group& getGroup(
const std::string& groupName, std::size_t timeStep)
const;
288 std::optional<std::size_t> first_RFT()
const;
293 void filterConnections(
const ActiveGridCells& grid);
294 std::size_t size()
const;
296 bool write_rst_file(std::size_t report_step)
const;
297 const std::map< std::string, int >& rst_keywords(
size_t timestep )
const;
306 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);
314 SimulatorUpdate runPyAction(std::size_t reportStep,
const Action::PyAction& pyaction, Action::State& action_state, EclipseState& ecl_state, SummaryState& summary_state);
317 const GasLiftOpt& glo(std::size_t report_step)
const;
319 bool operator==(
const Schedule& data)
const;
320 std::shared_ptr<const Python> python()
const;
323 const ScheduleState& back()
const;
324 const ScheduleState& operator[](std::size_t index)
const;
325 std::vector<ScheduleState>::const_iterator begin()
const;
326 std::vector<ScheduleState>::const_iterator end()
const;
327 void create_next(
const time_point& start_time,
const std::optional<time_point>& end_time);
328 void create_next(
const ScheduleBlock& block);
329 void create_first(
const time_point& start_time,
const std::optional<time_point>& end_time);
338 static bool cmp(
const Schedule& sched1,
const Schedule& sched2, std::size_t report_step);
339 void applyKeywords(std::vector<DeckKeyword*>& keywords, std::size_t timeStep);
341 template<
class Serializer>
342 void serializeOp(Serializer& serializer)
344 serializer(this->m_static);
345 serializer(this->m_sched_deck);
346 serializer(this->action_wgnames);
347 serializer(this->exit_status);
348 serializer(this->snapshots);
349 serializer(this->restart_output);
350 serializer(this->completed_cells);
352 this->
template pack_unpack<PAvg>(serializer);
353 this->
template pack_unpack<WellTestConfig>(serializer);
354 this->
template pack_unpack<GConSale>(serializer);
355 this->
template pack_unpack<GConSump>(serializer);
356 this->
template pack_unpack<GroupEconProductionLimits>(serializer);
357 this->
template pack_unpack<WListManager>(serializer);
358 this->
template pack_unpack<Network::ExtNetwork>(serializer);
359 this->
template pack_unpack<Network::Balance>(serializer);
360 this->
template pack_unpack<RPTConfig>(serializer);
361 this->
template pack_unpack<Action::Actions>(serializer);
362 this->
template pack_unpack<UDQActive>(serializer);
363 this->
template pack_unpack<UDQConfig>(serializer);
364 this->
template pack_unpack<NameOrder>(serializer);
365 this->
template pack_unpack<GroupOrder>(serializer);
366 this->
template pack_unpack<GuideRateConfig>(serializer);
367 this->
template pack_unpack<GasLiftOpt>(serializer);
368 this->
template pack_unpack<RFTConfig>(serializer);
369 this->
template pack_unpack<RSTConfig>(serializer);
371 this->
template pack_unpack_map<int, VFPProdTable>(serializer);
372 this->
template pack_unpack_map<int, VFPInjTable>(serializer);
373 this->
template pack_unpack_map<std::string, Group>(serializer);
374 this->
template pack_unpack_map<std::string, Well>(serializer);
377 template <
typename T,
class Serializer>
378 void pack_unpack(Serializer& serializer) {
379 std::vector<T> value_list;
380 std::vector<std::size_t> index_list;
382 if (serializer.isSerializing())
383 this->
template pack_state<T>(value_list, index_list);
385 serializer(value_list);
386 serializer(index_list);
388 if (!serializer.isSerializing())
389 this->
template unpack_state<T>(value_list, index_list);
392 template <
typename T>
393 std::vector<std::pair<std::size_t, T>> unique()
const {
394 std::vector<std::pair<std::size_t, T>> values;
395 for (std::size_t index = 0; index < this->snapshots.size(); index++) {
396 const auto& member = this->snapshots[index].get<T>();
397 const auto& value = member.get();
398 if (values.empty() || !(value == values.back().second))
399 values.push_back( std::make_pair(index, value));
405 template <
typename T>
406 void pack_state(std::vector<T>& value_list, std::vector<std::size_t>& index_list)
const {
407 auto unique_values = this->
template unique<T>();
408 for (
auto& [index, value] : unique_values) {
409 value_list.push_back( std::move(value) );
410 index_list.push_back( index );
415 template <
typename T>
416 void unpack_state(
const std::vector<T>& value_list,
const std::vector<std::size_t>& index_list) {
417 std::size_t unique_index = 0;
418 while (unique_index < value_list.size()) {
419 const auto& value = value_list[unique_index];
420 const auto& first_index = index_list[unique_index];
421 auto last_index = this->snapshots.size();
422 if (unique_index < (value_list.size() - 1))
423 last_index = index_list[unique_index + 1];
425 auto& target_state = this->snapshots[first_index];
426 target_state.get<T>().update( std::move(value) );
427 for (std::size_t index=first_index + 1; index < last_index; index++)
428 this->snapshots[index].get<T>().update( target_state.get<T>() );
435 template <
typename K,
typename T,
class Serializer>
436 void pack_unpack_map(Serializer& serializer) {
437 std::vector<T> value_list;
438 std::vector<std::size_t> index_list;
440 if (serializer.isSerializing())
441 pack_map<K,T>(value_list, index_list);
443 serializer(value_list);
444 serializer(index_list);
446 if (!serializer.isSerializing())
447 unpack_map<K,T>(value_list, index_list);
451 template <
typename K,
typename T>
452 void pack_map(std::vector<T>& value_list,
453 std::vector<std::size_t>& index_list) {
455 const auto& last_map = this->snapshots.back().get_map<K,T>();
456 std::vector<K> key_list{ last_map.keys() };
457 std::unordered_map<K,T> current_value;
459 for (std::size_t index = 0; index < this->snapshots.size(); index++) {
460 auto& state = this->snapshots[index];
461 const auto& current_map = state.template get_map<K,T>();
462 for (
const auto& key : key_list) {
463 auto& value = current_map.get_ptr(key);
465 auto it = current_value.find(key);
466 if (it == current_value.end() || !(*value == it->second)) {
467 value_list.push_back( *value );
468 index_list.push_back( index );
470 current_value[key] = *value;
478 template <
typename K,
typename T>
479 void unpack_map(
const std::vector<T>& value_list,
480 const std::vector<std::size_t>& index_list) {
482 std::unordered_map<K, std::vector<std::pair<std::size_t, T>>> storage;
483 for (std::size_t storage_index = 0; storage_index < value_list.size(); storage_index++) {
484 const auto& value = value_list[storage_index];
485 const auto& time_index = index_list[storage_index];
487 storage[ value.name() ].emplace_back( time_index, value );
490 for (
const auto& [key, values] : storage) {
491 for (std::size_t unique_index = 0; unique_index < values.size(); unique_index++) {
492 const auto& [time_index, value] = values[unique_index];
493 auto last_index = this->snapshots.size();
494 if (unique_index < (values.size() - 1))
495 last_index = values[unique_index + 1].first;
497 auto& map_value = this->snapshots[time_index].template get_map<K,T>();
498 map_value.update(std::move(value));
500 for (std::size_t index=time_index + 1; index < last_index; index++) {
501 auto& forward_map = this->snapshots[index].template get_map<K,T>();
502 forward_map.update( key, map_value );
508 friend std::ostream& operator<<(std::ostream& os,
const Schedule& sched);
509 void dump_deck(std::ostream& os)
const;
512 struct HandlerContext {
514 const ScheduleBlock& block;
515 const DeckKeyword& keyword;
516 const std::size_t currentStep;
517 const std::vector<std::string>& matching_wells;
518 const bool actionx_mode;
519 const ParseContext& parseContext;
521 SimulatorUpdate* sim_update{
nullptr};
522 const std::unordered_map<std::string, double>* target_wellpi{
nullptr};
523 std::unordered_map<std::string, double>* wpimult_global_factor{
nullptr};
524 WelSegsSet* welsegs_wells{
nullptr};
525 std::set<std::string>* compsegs_wells{
nullptr};
526 const ScheduleGrid& grid;
530 HandlerContext(
const ScheduleBlock& block_,
531 const DeckKeyword& keyword_,
532 const ScheduleGrid& grid_,
533 const std::size_t currentStep_,
534 const std::vector<std::string>& matching_wells_,
536 const ParseContext& parseContext_,
538 SimulatorUpdate* sim_update_,
539 const std::unordered_map<std::string, double>* target_wellpi_,
540 std::unordered_map<std::string, double>* wpimult_global_factor_,
541 WelSegsSet* welsegs_wells_,
542 std::set<std::string>* compsegs_wells_)
545 , currentStep(currentStep_)
546 , matching_wells(matching_wells_)
547 , actionx_mode(actionx_mode_)
548 , parseContext(parseContext_)
550 , sim_update(sim_update_)
551 , target_wellpi(target_wellpi_)
552 , wpimult_global_factor(wpimult_global_factor_)
553 , welsegs_wells(welsegs_wells_)
554 , compsegs_wells(compsegs_wells_)
558 void affected_well(
const std::string& well_name);
559 void record_well_structure_change();
562 void welsegs_handled(
const std::string& well_name)
565 welsegs_wells->insert({well_name, keyword.location()});
569 void compsegs_handled(
const std::string& well_name)
572 compsegs_wells->insert(well_name);
582 ScheduleStatic m_static;
583 ScheduleDeck m_sched_deck;
584 Action::WGNames action_wgnames;
585 std::optional<int> exit_status;
586 std::vector<ScheduleState> snapshots;
587 WriteRestartFileEvents restart_output;
588 CompletedCells completed_cells;
590 void load_rst(
const RestartIO::RstState& rst,
591 const TracerConfig& tracer_config,
592 const ScheduleGrid& grid,
593 const FieldPropsManager& fp);
594 void addWell(Well well);
595 void addWell(
const std::string& wellName,
596 const std::string& group,
599 Phase preferredPhase,
600 const std::optional<double>& refDepth,
601 double drainageRadius,
603 bool automaticShutIn,
605 WellGasInflowEquation gas_inflow,
606 std::size_t timeStep,
607 Connection::Order wellConnectionOrder);
608 bool updateWPAVE(
const std::string& wname, std::size_t report_step,
const PAvg& pavg);
610 void updateGuideRateModel(
const GuideRateModel& new_model, std::size_t report_step);
611 GTNode groupTree(
const std::string& root_node, std::size_t report_step, std::size_t level,
const std::optional<std::string>& parent_name)
const;
612 bool checkGroups(
const ParseContext& parseContext, ErrorGuard& errors);
613 bool updateWellStatus(
const std::string& well, std::size_t reportStep, WellStatus status, std::optional<KeywordLocation> = {});
614 void addWellToGroup(
const std::string& group_name,
const std::string& well_name , std::size_t timeStep);
615 void iterateScheduleSection(std::size_t load_start,
616 std::size_t load_end,
617 const ParseContext& parseContext,
619 const ScheduleGrid& grid,
620 const std::unordered_map<std::string, double> * target_wellpi,
621 const std::string& prefix,
622 const bool log_to_debug =
false);
623 void addACTIONX(
const Action::ActionX& action);
624 void addGroupToGroup(
const std::string& parent_group,
const std::string& child_group);
625 void addGroup(
const std::string& groupName , std::size_t timeStep);
626 void addGroup(Group group);
627 void addGroup(
const RestartIO::RstGroup& rst_group, std::size_t timeStep);
628 void addWell(
const std::string& wellName,
const DeckRecord& record, std::size_t timeStep, Connection::Order connection_order);
629 void checkIfAllConnectionsIsShut(std::size_t currentStep);
630 void end_report(std::size_t report_step);
633 void handleKeyword(std::size_t currentStep,
634 const ScheduleBlock& block,
635 const DeckKeyword& keyword,
636 const ParseContext& parseContext,
638 const ScheduleGrid& grid,
639 const std::vector<std::string>& matching_wells,
641 SimulatorUpdate* sim_update,
642 const std::unordered_map<std::string, double>* target_wellpi,
643 std::unordered_map<std::string, double>* wpimult_global_factor =
nullptr,
644 WelSegsSet* welsegs_wells =
nullptr,
645 std::set<std::string>* compsegs_wells =
nullptr);
647 void prefetch_cell_properties(
const ScheduleGrid& grid,
const DeckKeyword& keyword);
648 void store_wgnames(
const DeckKeyword& keyword);
649 std::vector<std::string> wellNames(
const std::string& pattern,
const HandlerContext& context);
650 std::vector<std::string> wellNames(
const std::string& pattern, std::size_t timeStep,
const std::vector<std::string>& matching_wells, InputErrorAction error_action, ErrorGuard& errors,
const KeywordLocation& location)
const;
651 void invalidNamePattern(
const std::string& namePattern,
const HandlerContext& context)
const;
652 static std::string formatDate(std::time_t t);
653 std::string simulationDays(std::size_t currentStep)
const;
654 void applyGlobalWPIMULT(
const std::unordered_map<std::string, double>& wpimult_global_factor);
656 bool must_write_rst_file(std::size_t report_step)
const;
658 void applyEXIT(
const DeckKeyword&, std::size_t currentStep);
659 SimulatorUpdate applyAction(std::size_t reportStep,
const std::string& action_name,
const std::vector<std::string>& matching_wells);
676 bool handleNormalKeyword(HandlerContext& handlerContext);
679 void handlePYACTION(
const DeckKeyword&);
680 void handleWELPIRuntime(HandlerContext&);
684 void handleAQUCT (HandlerContext&);
685 void handleAQUFETP (HandlerContext&);
686 void handleAQUFLUX (HandlerContext&);
687 void handleBCProp (HandlerContext&);
688 void handleBRANPROP (HandlerContext&);
689 void handleCOMPDAT (HandlerContext&);
690 void handleCOMPLUMP (HandlerContext&);
691 void handleCOMPORD (HandlerContext&);
692 void handleCOMPSEGS (HandlerContext&);
693 void handleCOMPTRAJ (HandlerContext&);
694 void handleCSKIN (HandlerContext&);
695 void handleDRSDT (HandlerContext&);
696 void handleDRSDTCON (HandlerContext&);
697 void handleDRSDTR (HandlerContext&);
698 void handleDRVDT (HandlerContext&);
699 void handleDRVDTR (HandlerContext&);
700 void handleEXIT (HandlerContext&);
701 void handleGCONINJE (HandlerContext&);
702 void handleGCONPROD (HandlerContext&);
703 void handleGCONSALE (HandlerContext&);
704 void handleGCONSUMP (HandlerContext&);
705 void handleGECON (HandlerContext&);
706 void handleGEFAC (HandlerContext&);
707 void handleGEOKeyword(HandlerContext&);
708 void handleGLIFTOPT (HandlerContext&);
709 void handleGPMAINT (HandlerContext&);
710 void handleGRUPNET (HandlerContext&);
711 void handleGRUPTREE (HandlerContext&);
712 void handleGUIDERAT (HandlerContext&);
713 void handleLIFTOPT (HandlerContext&);
714 void handleLINCOM (HandlerContext&);
715 void handleMESSAGES (HandlerContext&);
716 void handleMXUNSUPP (HandlerContext&);
717 void handleNETBALAN (HandlerContext&);
718 void handleNEXTSTEP (HandlerContext&);
719 void handleNODEPROP (HandlerContext&);
720 void handleNUPCOL (HandlerContext&);
721 void handleRPTONLY (HandlerContext&);
722 void handleRPTONLYO (HandlerContext&);
723 void handleRPTRST (HandlerContext&);
724 void handleRPTSCHED (HandlerContext&);
725 void handleTUNING (HandlerContext&);
726 void handleSAVE (HandlerContext&);
727 void handleSUMTHIN (HandlerContext&);
728 void handleUDQ (HandlerContext&);
729 void handleVAPPARS (HandlerContext&);
730 void handleVFPINJ (HandlerContext&);
731 void handleVFPPROD (HandlerContext&);
732 void handleWCONHIST (HandlerContext&);
733 void handleWCONINJE (HandlerContext&);
734 void handleWCONINJH (HandlerContext&);
735 void handleWCONPROD (HandlerContext&);
736 void handleWECON (HandlerContext&);
737 void handleWEFAC (HandlerContext&);
738 void handleWELOPEN (HandlerContext&);
739 void handleWELPI (HandlerContext&);
740 void handleWELSEGS (HandlerContext&);
741 void handleWELSPECS (HandlerContext&);
742 void handleWELTARG (HandlerContext&);
743 void handleWELTRAJ (HandlerContext&);
744 void handleWFOAM (HandlerContext&);
745 void handleWGRUPCON (HandlerContext&);
746 void handleWHISTCTL (HandlerContext&);
747 void handleWINJTEMP (HandlerContext&);
748 void handleWLIFTOPT (HandlerContext&);
749 void handleWLIST (HandlerContext&);
750 void handleWMICP (HandlerContext&);
751 void handleWPAVE (HandlerContext&);
752 void handleWPAVEDEP (HandlerContext&);
753 void handleWVFPDP (HandlerContext&);
754 void handleWVFPEXP (HandlerContext&);
755 void handleWWPAVE (HandlerContext&);
756 void handleWPIMULT (HandlerContext&);
757 void handleWINJCLN (HandlerContext&);
758 void handleWINJDAM (HandlerContext&);
759 void handleWINJFCNC (HandlerContext&);
760 void handleWINJMULT (HandlerContext&);
761 void handleWPMITAB (HandlerContext&);
762 void handleWPOLYMER (HandlerContext&);
763 void handleWRFT (HandlerContext&);
764 void handleWRFTPLT (HandlerContext&);
765 void handleWSALT (HandlerContext&);
766 void handleWSEGITER (HandlerContext&);
767 void handleWSEGSICD (HandlerContext&);
768 void handleWSEGAICD (HandlerContext&);
769 void handleWSEGVALV (HandlerContext&);
770 void handleWSKPTAB (HandlerContext&);
771 void handleWSOLVENT (HandlerContext&);
772 void handleWTEMP (HandlerContext&);
773 void handleWTEST (HandlerContext&);
774 void handleWTMULT (HandlerContext&);
775 void handleWTRACER (HandlerContext&);