pymfm.control.algorithms package

Submodules

pymfm.control.algorithms.optimization_based module

pymfm.control.algorithms.optimization_based.P_net_after_kW_lower_bound(model, t)[source]

The P_net_after_kW lower bound constraint. If there is a lower bound for timestamp t, it limits the P_net_after_kW (= P_imp_kW - P_exp_kW) accordingly.

Parameters:
  • model – The pyomo model.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.P_net_after_kW_upper_bound(model, t)[source]

The P_net_after_kW upper bound constraint. If there is a upper bound for timestamp t, it limits the P_net_after_kW (= P_imp_kW - P_exp_kW) accordingly.

Parameters:
  • model – The pyomo model.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.bat_charging(model, n, t)[source]

The battery charging/discharging constraint. Updates the state of charge (SoC) of the battery for the next timestamp t accordingly.

Parameters:
  • model – The pyomo model.

  • n – The battery index.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.bat_final_SoC(model, n)[source]

The battery final state of charge (SoC) constraint. Secures that batteries reach their final desired SoC at the very end timestamp t. If the battery type is household (hbes), then final desired SoC to be reached at the timestamp t, where daylight ends (day_end).

Parameters:
  • model – The pyomo model.

  • n – The battery index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.bat_init_SoC(model, n)[source]

The battery initial state of charge constraint. Initializes the initial state of charges of the batteries to the first timestamp t.

Parameters:
  • model – The pyomo model.

  • n – The battery index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.bat_max_SoC(model, n, t)[source]

The battery maximum state of charge (SoC) constraint. Limits the SoC of the batteries according to their maximum allowed SoCs.

Parameters:
  • model – The pyomo model.

  • n – The battery index.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.bat_max_ch_power(model, n, t)[source]

The battery maximum charging power constraint. Limits the charging power of the batteries according to their maximum charging powers.

Parameters:
  • model – The pyomo model.

  • n – The battery index.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.bat_max_dis_power(model, n, t)[source]

The battery maximum discharging power constraint. Limits the discharging power of the batteries according to their maximum discharging powers.

Parameters:
  • model – The pyomo model.

  • n – The battery index.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.bat_min_SoC(model, n, t)[source]

The battery minimum state of charge (SoC) constraint. Limits the SoC of the batteries according to their minimum allowed SoCs.

Parameters:
  • model – The pyomo model.

  • n – The battery index.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.bulk_energy(model)[source]

The bulk energy constraint.

Parameters:

model – The pyomo model.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.ch_dis_binary(model, n, t)[source]

The charge/discharge binary constraint. Auxiliary constraint used to prevent batteries from being both charged and discharged at the same timestamp t.

Parameters:
  • model – The pyomo model.

  • n – The battery index.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.deficit_case_1(model, t)[source]

First deficit case constraint. If you are short on power in a timestamp t, you are not allowed to import more than you do before beeing controlled.

Parameters:
  • model – The pyomo model.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.deficit_case_2(model, n, t)[source]

Second deficit case constraint. If you are short on power in a timestamp t, you are not allowed to charge any of the batteries.

Parameters:
  • model – The pyomo model.

  • n – The battery index.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.hbes_avoid_diss(model, n, t)[source]

The constraint to avoid discharging of the household batteries (hbes). Household batteries are not allowed to be discharged during the optimization horizon.

Parameters:
  • model – The pyomo model.

  • n – The battery index.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.imp_exp_binary(model, t)[source]

The import/export binary constraint. Auxiliary constraint used to prevent prevent both export and import at the same timestamp t.

Parameters:
  • model – The pyomo model.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.obj_rule(model)[source]

The objective function. Objective: Minimize the power exchange with the grid (Minimum interaction with the grid) Power import and export as well as their peak values (alpha) are minimized. :param model: The pyomo model. :return: The objective function itself.

pymfm.control.algorithms.optimization_based.penalty_for_exp(model, t)[source]

Penalty constraint for exports. Added to prevent unnecessary minimal exports. Penalty variable alpha_exp is in the objective function, represents the peak export and shall be minimized.

Parameters:
  • model – The pyomo model.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.penalty_for_imp(model, t)[source]

Penalty constraint for imports. Added to prevent unnecessary minimal imports. Penalty variable alpha_imp is in the objective function, reprsents the peak import, and shall be trying to be minimized.

Parameters:
  • model – The pyomo model.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.power_balance(model, t)[source]

The power balance constraint.

Parameters:
  • model – The pyomo model.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.prep_output_df(pv_profile: Series, P_bat_kW_df: DataFrame, P_bat_total_kW: Series, SoC_bat_df: DataFrame, P_net_after_kW: Series, df_forecasts: DataFrame, P_net_after_kW_upperb: Series, P_net_after_kW_lowerb: Series)[source]

Prepare the output DataFrame of scheduling optimization based mode.

Parameters:
  • pv_profile (pd.Series) – containing the PV profile.

  • P_bat_kW_df (DataFrame) – containing battery power for different nodes.

  • P_bat_total_kW (pd.Series) – containing the total battery power.

  • SoC_bat_df (DataFrame) – containing battery state of charge for different nodes.

  • P_net_after_kW (pd.Series) – containing net power after control.

  • df_forecasts (DataFrame) – containing forecasted data.

  • P_net_after_kW_upperb (pd.Series) – containing upper bounds for net power after control.

  • P_net_after_kW_lowerb (pd.Series) – containing lower bounds for net power after control.

Returns:

output_df – containing prepared output data.

Return type:

DataFrame

pymfm.control.algorithms.optimization_based.pv_curtailment_constr(model, t)[source]

The PV generation curtailment constraint. If curtailment is allowed, PV production will be kept below its limits (PV forecast).

Parameters:
  • model – The pyomo model.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.scheduling(P_load_gen: Series, df_battery: DataFrame, day_end: datetime, bulk_data: Bulk, P_net_after_kW_limits: DataFrame, pv_curtailment: bool) Tuple[Series, DataFrame, Series, DataFrame, Series, Series, Series, Tuple[str, str]][source]

The scheduling optimization function which acts upon the load and generation forecast data considering battery specifications, optimization horizon, and power boundaries. Depending on the input data, bulk delivery/reception and PV curtailment can also be satisfied.

Parameters:
  • P_load_gen (pd.Series) – load and generation forecast time series of float type.

  • df_battery (pd.DataFrame) – battery specifications of float and string types.

  • day_end (datetime) – user-defined end of the day (datetime) till which household batteries should reach maximum SoC. By default, its value is set to then sun-set time.

  • bulk_data (Bulk) – Class related to the bulk delivery/reception of energy from batteries including bulk_start and _end datetime and the bulk_energy_kWh float.

  • P_net_after_kW_limits (pd.DataFrame) – consisiting of upper and lower bound float time series (kW) and the integer identifiers for the existance of any upper or lower bounds.

  • pv_curtailment (bool) –

    If true, PV generation can be curtailed.

    (solver.status, solver.termination_condition),

Returns:

pv_profile: Series containing the PV (Photovoltaic) profile. P_bat_kW_df: DataFrame containing battery power for different nodes. P_bat_total_kW: Series containing the total battery power. SoC_bat_df: DataFrame containing battery state of charge for different nodes. P_net_after_kW: Series containing net power after control. P_net_after_kW_upperb: Series containing upper bounds for net power after control. P_net_after_kW_lowerb: Series containing lower bounds for net power after control. (str, str): status and details from the solver

Return type:

Tuple[ pd.Series, pd.DataFrame, pd.Series, pd.DataFrame, pd.Series, pd.Series, pd.Series, Tuple[Any, Any], ]

pymfm.control.algorithms.optimization_based.surplus_case_1(model, t)[source]

First surplus case constraint. In case of power surplus, batteries should not be charged with a power more than exported power in any timestamp t. The goal here is to be sure that batteries are not being charged with the imported power and just with the power surplus.

Parameters:
  • model – The pyomo model.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.optimization_based.surplus_case_2(model, t)[source]

Second surplus case constraint. In case of power surplus, it is not allowed to import power.

Parameters:
  • model – The pyomo model.

  • t – The timestamp index.

Returns:

The constraint itself.

pymfm.control.algorithms.rule_based module

pymfm.control.algorithms.rule_based.near_real_time(measurements_request_dict: dict, battery_specs: BatterySpecs)[source]

For this operation mode, rule based logic is implemented on the net power measurement of the microgrid respecting battery boundaries. In case of a (near) real time power requests from the microgrid, this request is resecpted by the microgrid community battery energy storage (cbes).

Parameters:
  • measurements_request_dict (dict of dict) – In the measurement_request dictionary, for each time stamp (datetime), the corresponding float values for the requested (P_req_kW) and measured (P_net_meas_kW) net power consumption of the microgrid (in kW).

  • battery_specs (pymfm.control.utils.data_input.BatterySpecs) – BatterySpecs class and the corresponding pydantic model representing string values of battery “type” and “id” and float values of initital SoC in %, maximum charging and discharging powers in kW, min and max SoC in %, battery capacity in kWh, and (dis)charging efficiency (0<efficiency<=1)

Returns:

output – In the output dictionary and for each measurement “timestamp” (datetime), the corresponding initial SoC “initial_SoC_bat_%” and final SoC “SoC_bat_%” before and after control action in % (float), community battery energy storage cbes power setpoint “P_bat_kW” in kW (float), and net power consumption before “P_net_meas_kW” and after “P_net_after_kW” control action in kW (float) are returned.

Return type:

dict

pymfm.control.algorithms.rule_based.scheduling(P_load_gen: Series, battery_specs: BatterySpecs, delta_T: timedelta)[source]

For the scheduling operation mode and with the rule based logic, the same control method as in (near) real time is implemented. However, this logic is implemented on the net power forecast profile of the microgrid and not on the power measured at each instance.

Parameters:
  • P_load_gen (pd.Series) – load and generation forecast time series of float type

  • battery_specs (param) – BatterySpecs class and the corresponding pydantic model representing string values of battery “type” and “id” and float values of initital SoC (between 0 and 1), maximum charging and discharging powers in kW, min and max SoC (between 0 and 1), battery capacity in kWh, and (dis)charging efficiency (0<efficiency<=1)

  • delta_T (timedelta) – Pandas TimeDelta object (in day unit) representing time intervals of the forecast time series.

Returns:

output_ds – In the output Pandas series and for each forecast timestamp, the corresponding net power consumption before “P_net_before_kW” and after “P_net_after_kW” control action in kW, community battery energy storage (cbes) power setpoint in kW , battery SoC in % “SoC_bat” and its associated energy in kWs “bat_energy_kWs”, and imported “import_kW” and exported “export_kW” powers afer control action in kW are reported.

Return type:

pd.Series

Module contents