9#ifndef PYTHON_MODULE_HPP
10#define PYTHON_MODULE_HPP
13#include <condition_variable>
17#include <pybind11/pybind11.h>
18#include <pybind11/stl.h>
125 std::string config_filepath;
128 throw std::runtime_error(
"Simulation already running. Call vacuum() before re-initializing.");
135 if (args.contains(
"config")) {
136 config_filepath = args[
"config"].cast<std::string>();
138 config_filepath =
"../config/simulation_config.json";
141 if (args.contains(
"pvar")) {
143 throw std::runtime_error(
"Parameter variations cannot be defined when using the python interface!");
148 if (args.contains(
"scenario")) {
149 scenario_id = args[
"scenario"].cast<
unsigned long>();
154 if (args.contains(
"stop-on-cc-err") && args[
"stop-on-cc-err"].cast<
bool>()) {
160 if (args.contains(
"n_threads")) {
161 unsigned int n_threads = args[
"n_threads"].cast<uint>();
163 if (n_threads == 1) {
164 std::cerr <<
"Warning: Defining only 1 working thread is useless.\n";
170 if (args.contains(
"work-stealing") && args[
"work-stealing"].cast<
bool>()) {
174 if (args.contains(
"max-parallel-opti-vars")) {
175 unsigned long n_vars = args[
"max-parallel-opti-vars"].cast<
unsigned long>();
179 if (args.contains(
"cu-output")) {
180 std::string cu_output = args[
"cu-output"].cast<std::string>();
181 if (cu_output ==
"no" || cu_output ==
"off") {
183 }
else if (cu_output ==
"single") {
185 }
else if (cu_output ==
"sl") {
188 throw std::invalid_argument(
"Invalid option for cu-output");
194 if (args.contains(
"st-output")) {
195 std::string st_output = args[
"st-output"].cast<std::string>();
196 if (st_output ==
"no" || st_output ==
"off") {
198 }
else if (st_output ==
"on") {
201 throw std::invalid_argument(
"Invalid option for st-output");
207 if (args.contains(
"ccmd-output")) {
208 std::string val = args[
"ccmd-output"].cast<std::string>();
209 if (val ==
"no" || val ==
"off") {
211 }
else if (val ==
"all" || val ==
"on") {
214 throw std::invalid_argument(
"Invalid option for ccmd-output");
220 if (args.contains(
"ev-output")) {
221 std::string val = args[
"ev-output"].cast<std::string>();
222 if (val ==
"no" || val ==
"off") {
224 }
else if (val ==
"all") {
227 throw std::invalid_argument(
"Invalid option for ev-output");
233 if (args.contains(
"repetitions")) {
234 throw std::runtime_error(
"Repetitions cannot be defined when using the python interface");
239 if (args.contains(
"seed")) {
240 unsigned int seed = args[
"seed"].cast<
unsigned int>();
245 if (args.contains(
"weekly-metrics")) {
246 std::string wm_mode = args[
"weekly-metrics"].cast<std::string>();
247 if (wm_mode ==
"off" || wm_mode ==
"no") {
249 }
else if (wm_mode ==
"on" || wm_mode ==
"yes") {
252 throw std::invalid_argument(
"Invalid option for weekly-metrics");
256 if (args.contains(
"cu-output-selection")) {
257 std::string selected_CUs_str = args[
"cu-output-selection"].cast<std::string>();
258 std::stringstream strstr(selected_CUs_str);
260 while (std::getline(strstr, token,
',')) {
264 throw std::invalid_argument(
"Invalid cu-output-selection token: " + token);
270 if (args.contains(
"suof")) {
276 std::cout <<
"Initializing the simulation for scenario ID " <<
scenario_id << std::endl;
283 throw std::runtime_error(
"Error loading config file.");
286 throw std::runtime_error(
"Error loading expansion matrix.");
289 throw std::runtime_error(
"Invalid expansion matrix.");
296 throw std::runtime_error(
"Error loading database.");
300 std::cerr <<
"\nThese variables are NOT initialized:" << std::endl;
303 std::cerr <<
"" << std::endl;
304 throw std::runtime_error(
"Some global variables are not initialized!");
328 throw std::runtime_error(
"Error during simulation run!");
330 }
catch (
const std::exception& e) {
331 std::cerr <<
"Exception in simulation worker: " << e.what() << std::endl;
333 std::cerr <<
"Unknown exception in simulation worker." << std::endl;
345 throw std::runtime_error(
"Error: Simulation has not been started!");
388 s.controlUnitID = controlUnitID;
391 s.has_cntrl_bs = cu->
has_bs();
392 s.has_cntrl_hp = cu->
has_hp();
393 s.has_cntrl_evchst = cu->
has_cs();
412 const std::vector<const EVFSM*>& ev_list = cs->
get_listOfEVs();
414 for (
size_t i = 0; i < 3; ++i) {
417 if (i < ev_list.size()) {
418 const EVFSM* ev = ev_list[i];
425 ev_state_struct.ev_soc = battery ? battery->
get_SOC() : 0.0;
426 ev_state_struct.ev_soe = battery ? battery->
get_SOE() : 0.0;
427 ev_state_struct.ev_maxP_kW = battery ? battery->
get_maxP_kW() : 0.0;
428 ev_state_struct.ev_maxE_kWh = battery ? battery->
get_maxE_kWh() : 0.0;
436 s.ev_states.push_back(ev_state_struct);
463 inline void sendCommands(
unsigned long controlUnitID, pybind11::dict commands) {
465 throw std::runtime_error(
"Error: Simulation has not been started!");
476 double p_bs_kW = 0.0;
477 double p_hp_kW = 0.0;
478 std::vector<double> p_ev_kW;
479 if (commands.contains(
"p_bs_kW")) {
480 p_bs_kW = commands[
"p_bs_kW"].cast<
double>();
482 if (commands.contains(
"p_hp_kW")) {
483 p_hp_kW = commands[
"p_hp_kW"].cast<
double>();
485 if (commands.contains(
"p_ev_kW")) {
486 p_ev_kW = commands[
"p_ev_kW"].cast<std::vector<double>>();
490 cu->send_control_commands_from_py_interface(p_bs_kW, p_hp_kW, p_ev_kW);
505 throw std::runtime_error(
"Error: Simulation has not been started!");
508 throw std::runtime_error(
"Error: Simulation already finished!");
538 throw std::runtime_error(
"Error: Simulation has not been started!");
568 throw std::runtime_error(
"Error: Simulation has not been started!");
571 throw std::runtime_error(
"Error: Simulation has not been finished!");
Definition components.h:188
double get_maxP_kW() const
Returns the maximum available capacity of the battery. For the current power, see ComponentBS::get_cu...
Definition components.h:200
double get_SOC() const
Returns the current state of charge of the battery.
Definition components.h:194
double get_maxE_kWh() const
Returns the maximum available capacity of the battery. For the current state of energy / charge,...
Definition components.h:199
double get_SOE() const
Returns the current amount of energy stored in the battery. Equivalent to ComponentBS::get_currentCha...
Definition components.h:195
Definition components.h:337
std::vector< const EVFSM * > get_listOfEVs() const
Returns a reference to the internal list of EVs.
Definition components.h:352
unsigned long get_n_EVs() const
Returns the number of connected EVs if the component is enabled, otherwise 0 is returned.
Definition components.cpp:843
float get_rated_power_without_AUX() const
Definition components.h:264
static void VacuumStaticVariables()
Definition components.cpp:786
const std::vector< double > * get_future_max_consumption_kWh() const
Definition components.h:63
const std::vector< double > * get_future_min_power_kW() const
Definition components.h:281
const std::vector< double > * get_future_min_consumption_kWh() const
Definition components.h:73
const std::vector< double > * get_future_max_power_kW() const
Definition components.h:274
double get_currentDemand_kW() const
Definition components.h:252
double get_kWp() const
Returns the total peak power, summed over all sections. See also ComponentPV::get_kWp_per_section().
Definition components.h:141
float get_currentGeneration_kW() const
Definition components.h:142
const ComponentHP * get_component_HP() const
Returns a pointer to heat pump component, or nullptr if no such component is added.
Definition units.h:231
bool has_cs() const
Returns true, if the unit has an EV charging station connected (in data or simulated)
Definition units.cpp:345
double get_current_demand_wo_BS_or_gen_kW() const
Returns the current local demand of all components excluding the BESS and also not considering the lo...
Definition units.h:199
const ComponentPV * get_component_PV() const
Returns a pointer to PV component, or nullptr if no such component is added.
Definition units.h:233
bool has_bs() const
Returns true, if the control unit has a battery storage system attachted (in data or simulated).
Definition units.cpp:325
bool has_hp() const
Returns true, if the control unit has a heat pump attachted (in data or simulated).
Definition units.cpp:335
size_t get_internal_id() const
Returns the (consecutive) internal ID of the control unit.
Definition units.h:212
static ControlUnit * GetInstancePublicIDWE(unsigned long unitID)
Returns instance with a public unitID. Throws an error, if untiID is not defined.
Definition units.cpp:1597
static void ResetAllInternalStates()
Definition units.cpp:1616
const ComponentCS * get_component_CS() const
Returns a pointer to EV charging station component, or nullptr if no such component is added.
Definition units.h:232
static void VacuumInstancesAndStaticVariables()
Definition units.cpp:1567
bool has_pv() const
Returns true, if the control unit has a PV attachted (in data or simulated).
Definition units.cpp:315
const ComponentBS * get_component_BS() const
Returns a pointer to battery storage component, or nullptr if no such component is added.
Definition units.h:230
Definition components.h:431
static void SetSeed(unsigned int seed)
Sets the seed for the EVFSM-class random number generator.
Definition components.cpp:1489
const std::vector< double > * get_future_max_consumption_kWh() const
Definition components.h:63
EVState get_current_state() const
Definition components.h:437
const ComponentBS * get_battery() const
Returns a reference to the battery component of the EV.
Definition components.h:445
const std::vector< double > * get_future_max_power_kW() const
Returns the maximum power of this EV per time step in the controller horizon. Attention: Returned obj...
Definition components.h:442
const std::vector< double > * get_future_min_consumption_kWh() const
Definition components.h:73
static void VacuumStaticVariables()
Definition components.cpp:1485
double get_currentDemand_kW() const
Gets the current charging power in kW; Only valid after calling EVFSM::setDemandToProfileData() or EV...
Definition components.h:443
static void set_compute_weekly_metrics(bool mode)
Definition global.cpp:560
static bool get_use_prices_time_series_ia()
Return weather the electricity prices time series (if it is available in the data) should be used or ...
Definition global.h:286
static void set_repetitions_selected(bool value)
Definition global.cpp:576
static bool AllVariablesInitialized()
Definition global.cpp:260
static void set_stop_on_cc_err(bool value)
Definition global.cpp:607
static const std::string & get_structure_database_name()
Definition global.h:293
static void set_max_parallel_opti_vars(unsigned long value)
Definition global.cpp:1219
static void set_output_mode_per_cu(global::OutputModePerCU mode)
Definition global.cpp:1156
static void InitializeStaticVariables()
Definition global.cpp:247
static float get_demand_tariff()
Definition global.h:266
static void set_pvar_vals(bool pvar_set, int pvarID)
Definition global.cpp:567
static void set_n_threads(unsigned int value)
Definition global.cpp:592
static void DeleteStaticVariables()
Definition global.cpp:251
static void set_create_substation_output(bool value)
Definition global.cpp:1244
static const std::string & get_input_path()
Definition global.h:291
static const std::string & get_cache_dir_path()
Returns the path for the cache directory - it this was NOT set, it will return the default value whic...
Definition global.h:295
static void set_create_control_cmd_output(bool value)
Definition global.cpp:1251
static void set_work_stealing(bool value)
Definition global.cpp:600
static void set_create_ev_detailed_output(bool value)
Definition global.cpp:1258
static void set_seed(unsigned int value)
Definition global.cpp:552
static void PrintUninitializedVariables()
Prints all variable names to stdout, that are not initialized.
Definition global.cpp:355
static HPProfileIDCache & GetInstance()
Definition cache_helper.hpp:39
void setCacheFilename(const std::string &filename)
Definition cache_helper.hpp:47
void saveCacheFile()
Definition cache_helper.hpp:77
static void VacuumInstancesAndStaticVariables()
Definition units.cpp:1912
void setCacheFilename(const std::string &filename)
Definition cache_helper.hpp:187
static PVProfileIDCache & GetInstance()
Definition cache_helper.hpp:179
void saveCacheFile()
Definition cache_helper.hpp:221
static void VacuumInstancesAndStaticVariables()
Definition units.cpp:146
bool load_config_file(unsigned long scenario_id, std::string &filepath)
Load the config file, that is passed as command line argument.
bool load_data_from_central_database(const char *filepath)
Load the complete simulation structure from the central database, and also load the recorded time ser...
Definition setup_and_dataloading.cpp:1247
void output_variable_values(std::ostream ¤t_outstream)
Outputs the current simulation configuration and all parameter settings to the specified output strea...
Definition setup_and_dataloading.cpp:1693
bool load_expansion_matrix(float expansion_matrix[16][16])
Definition sac_planning.cpp:164
bool verify_expansion_matrix(float expansion_matrix[16][16])
Definition sac_planning.cpp:219
unsigned long n_ts_between_flushs
Number of timesteps between the flush of the output buffers.
Definition global.h:53
void print_uninitialized_variables()
Prints all variable names to stdout, that are not initialized.
Definition global.cpp:69
bool all_variables_initialized()
Checks if all variables are initialized.
Definition global.cpp:14
void vacuum()
Deletes all global variables in the end.
Definition global.cpp:30
const float * eprices_local_ts
Reference to the time series of the energy prices (for residential customers) for grid demanded energ...
Definition global.h:60
std::vector< unsigned long > unitIDs_selected_for_output
A vector containing a list of all unitIDs selected for output. If this vector is empty (as it is by d...
Definition global.h:62
Definition python_module.hpp:27
void vacuum()
Cleans up the simulation, finished the working thread and releases resources.
Definition python_module.hpp:566
std::atomic< bool > g_next_step_requested
Flag indicating that a new simulation step should be executed.
Definition python_module.hpp:93
std::atomic< bool > g_simulation_idling
Flag indicating whether the simulation is currently idling or working.
Definition python_module.hpp:90
unsigned long expansion_matrix_abs_freq[16][16]
Variable storing the expansion matrix with absolute frequencies - if using as stand-alone simulation,...
Definition python_module.hpp:99
void initialize_simulation(pybind11::dict args)
Initializes and starts the simulation from Python.
Definition python_module.hpp:124
void sendCommands(unsigned long controlUnitID, pybind11::dict commands)
Sends a control command to a given control unit.
Definition python_module.hpp:463
std::atomic< unsigned long > g_atomic_next_tsID
Shared variable indicating the next time step ID to be executed when calling pyconn::run_one_step()
Definition python_module.hpp:95
std::atomic< bool > g_sim_started
States if the working thread for the simulation has been started.
Definition python_module.hpp:86
std::atomic< bool > g_simulation_main_part_started
Flag indicating whether the simulation has finished the SAC planning and addition.
Definition python_module.hpp:89
void run_one_step()
Executes a single simulation time step.
Definition python_module.hpp:502
unsigned long scenario_id
Variable storing the selected scenario ID - if using as stand-alone simulation, this is placed inside...
Definition python_module.hpp:100
SimulationControlUnitState getState(unsigned long controlUnitID)
Retrieves the state of a control unit.
Definition python_module.hpp:378
std::atomic< bool > g_restart_simulation
Flag indicating to restart the simulation after a reset of all internal states.
Definition python_module.hpp:96
bool simulation_finished()
Checks if the simulation reached its end.
Definition python_module.hpp:358
std::mutex g_mtx_simulation
Mutex protecting synchronization between the simulation thread and Python calls.
Definition python_module.hpp:91
std::condition_variable g_cv_sim_state_update
Condition variable to notify and to wait for changes in the simulation / python code state.
Definition python_module.hpp:92
unsigned long get_next_timestep_id()
Returns the timestep ID of the next time step.
Definition python_module.hpp:343
float expansion_matrix_rel_freq[16][16]
Variable storing the expansion matrix with relative frequencies - if using as stand-alone simulation,...
Definition python_module.hpp:98
void reset_simulation_to_start()
Resets the simulation and all internal states to the first time step.
Definition python_module.hpp:536
std::thread g_sim_worker
The worker thread for the simulation that is started by pyconn::initialize_simulation()
Definition python_module.hpp:85
std::atomic< bool > g_worker_threads_shutdown_cmd
Flag indicating that all worker threads should shoutdown and the simulation should be finished....
Definition python_module.hpp:94
std::atomic< bool > g_sim_finished
States if the simulation has reached its final state (and cannot proceed anymore, or must be reseted ...
Definition python_module.hpp:87
bool runCompleteSimulation(float expansion_matrix_rel_freq[16][16], unsigned long expansion_matrix_abs_freq[16][16], const unsigned long scenario_id)
Definition simulation_logic.cpp:471
Represents the state of a single control unit during a simulation time step.
Definition python_module.hpp:51
std::vector< double > hp_future_min_consumption_kWh
Definition python_module.hpp:70
unsigned long internalID
Definition python_module.hpp:53
std::vector< SimulationEVState > ev_states
Definition python_module.hpp:73
double bs_soc
Definition python_module.hpp:60
bool has_cntrl_bs
Definition python_module.hpp:55
unsigned long timestepID
Definition python_module.hpp:58
unsigned long controlUnitID
Definition python_module.hpp:52
float pv_currentGeneration_kW
Definition python_module.hpp:76
double household_demand
Definition python_module.hpp:78
float hp_rated_power_kW
Definition python_module.hpp:65
unsigned long n_EVs
Definition python_module.hpp:74
double electricity_price
Definition python_module.hpp:79
float pv_kWp
Definition python_module.hpp:77
double bs_soe
Definition python_module.hpp:61
double bs_maxE_kWh
Definition python_module.hpp:63
bool has_pv
Definition python_module.hpp:54
std::vector< double > hp_future_max_consumption_kWh
Definition python_module.hpp:69
bool has_cntrl_evchst
Definition python_module.hpp:57
bool has_cntrl_hp
Definition python_module.hpp:56
double bs_maxP_kW
Definition python_module.hpp:62
std::vector< double > hp_future_max_power_kW
Definition python_module.hpp:67
std::vector< double > hp_future_min_power_kW
Definition python_module.hpp:68
double hp_current_demand_kW
Definition python_module.hpp:66
Represents the state of a single EV during a simulation time step.
Definition python_module.hpp:32
double ev_maxE_kWh
Definition python_module.hpp:38
std::vector< double > ev_future_min_consumption_kWh
Definition python_module.hpp:41
double ev_current_demand_kW
Definition python_module.hpp:34
std::vector< double > ev_future_max_power_kW
Definition python_module.hpp:39
double ev_soc
Definition python_module.hpp:35
std::vector< double > ev_future_max_consumption_kWh
Definition python_module.hpp:40
double ev_soe
Definition python_module.hpp:36
double ev_maxP_kW
Definition python_module.hpp:37
EVState ev_state
Definition python_module.hpp:33
EVState
Definition vehicles.h:42