Skip to content

Commit

Permalink
implement changes of issue #59
Browse files Browse the repository at this point in the history
adjsut examples to include experiment configuration
  • Loading branch information
Beeser committed Aug 23, 2022
1 parent be2a901 commit 17c8589
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 805 deletions.
2 changes: 1 addition & 1 deletion ebcpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"""
# Pull the useful classes to the top Level
from .data_types import TimeSeriesData, TimeSeries
from .simulationapi.simulation_api_new import FMU_Discrete # todo: adjust
from .simulationapi.simulation_api_new import FMU_Discrete, FMU_API, DymolaAPI # todo: adjust

__version__ = '0.3.1'
604 changes: 305 additions & 299 deletions ebcpy/simulationapi/simulation_api_new.py

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions examples/e2_fmu_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ def main(
# ######################### Simulation API Instantiation ##########################
# %% Setup the FMU-API:
model_name = pathlib.Path(__file__).parent.joinpath("data", "HeatPumpSystemWithInput.fmu")
fmu_api = FMU_API(model_name=model_name,
cd=cd,
# Organize settings in configuration dict
config_dict = {
'file_path': model_name,
'cd': cd,
}
fmu_api = FMU_API(config_dict,
n_cpu=n_cpu,
log_fmu=log_fmu)
print("Number of variables:", len(fmu_api.variables))
Expand Down Expand Up @@ -149,6 +153,6 @@ def main(
main(
n_cpu=2,
log_fmu=False,
n_sim=2,
n_sim=50,
output_interval=100
)
32 changes: 18 additions & 14 deletions examples/e2a_fmu_discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def run(self, x_act, x_set):
return self.y


# plotting format settings
def plotting_fmt():
"""
Adjusts the plotting format
Expand Down Expand Up @@ -202,10 +203,10 @@ def plotting_fmt():
# The do_step function returns True, when stop time is reached and thus breaks the loop
while not system.finished:
# Call controller
# (for advanced control strategies that require previous results, use the attribute sim_res) # todo: find soultion with output_step # fixme consider returning the last n values for mpc if n==1 return dict, otherwise list of dicts
# (for advanced control strategies that require previous results, use the attribute sim_res and adjust output_interval)
ctr_action = ctr.run(res_step['bus.processVar'], input_df.loc[system.current_time]['bus.setPoint'])
# Apply control action to system and perform simulation step
res_step = system.do_step_wrapper(input_step={'bus.controlOutput': ctr_action})
res_step = system.inp_step_read(input_step={'bus.controlOutput': ctr_action})

# ################# Read Simulation Results ###################################################
# simulation results stored in the attribute 'sim_res' can be returned calling 'get_results()'
Expand Down Expand Up @@ -234,11 +235,10 @@ def plotting_fmt():
print('Study B: System FMU with Controller FMU')
while not system.finished:
# Call controller and extract control output
# (for advanced control strategies that require previous results, use the attribute sim_res)
ctr_action = controller.do_step_wrapper(
input_step={'bus.processVar': res_step['bus.processVar']})['bus.controlOutput'] # fixme: controller schreibt auch results dataframe -> eigentlich unnötig -> Klassenframe??
# (for advanced control strategies that require previous results, use the attribute sim_res and adjust output_interval)
ctr_action = controller.inp_step_read(input_step={'bus.processVar': res_step['bus.processVar']})['bus.controlOutput'] # fixme: controller schreibt auch results dataframe -> eigentlich unnötig -> Klassenframe??
# write controller output to system FMU as well as pre-known inputs and perform step
res_step = system.do_step_wrapper(input_step={'bus.controlOutput': ctr_action})
res_step = system.inp_step_read(input_step={'bus.controlOutput': ctr_action})

# read simulation results
results_B = system.get_results()
Expand All @@ -250,7 +250,6 @@ def plotting_fmt():
FMU_Discrete.close_all()

# ###################### Plot Results #########################################
# todo: apply same range on all plots (copy from first) and get rid of x tick labels
# apply plotting format settings
plotting_fmt()

Expand All @@ -260,9 +259,12 @@ def plotting_fmt():
for i in range(len(cases)):
axes = axes_mat[:, i]
axes[0].plot(time_index_out, cases[i]['bus.processVar'] - 273.15, label='mea', color='b')
axes[0].plot(time_index, setpoint - 273.15, label='set', color='r') # fixme: setpoint not available in results
axes[0].plot(time_index, setpoint - 273.15, label='set', color='r')
axes[0].set_ylim(15,22)
axes[1].plot(time_index_out, cases[i]['bus.controlOutput'], label='control output', color='b')
axes[1].set_ylim(-0.05, 0.2)
axes[2].plot(time_index_out, cases[i]['bus.disturbance[1]'] - 273.15, label='dist', color='b')
axes[2].set_ylim(0,40)

# x label
axes[2].set_xlabel('Time / s')
Expand All @@ -281,8 +283,8 @@ def plotting_fmt():
if i > 0:
# ignore y labels for all but the first
ax.set_yticklabels([])
for i in range(2):
axes[i].set_xticklabels([])
for k in range(2):
axes[k].set_xticklabels([])

plt.tight_layout()
plt.show()
Expand All @@ -301,6 +303,9 @@ def plotting_fmt():
# The FMU_Discrete class includes basic FMU handler utilities previously found in aku's fmu handler skript
# They are demonstrated in the following with a heat pump system fmu

# !!! The _do_step() base function does not append the results to the "sim_res" attribute !!!
# !!! The _do_step() base function does not consider long-term input data (attribute "input_table") !!!

# Instantiate Simulation API for fmu
hp_fmu = FMU_Discrete({'cd': cd,
'file_path': pathlib.Path(__file__).parent.joinpath("data", "HeatPumpSystemWithInput.fmu"),
Expand All @@ -326,10 +331,10 @@ def plotting_fmt():
# simulation loop
# the ambient temperature is altered during the simulation

# Note that simulating the fmu with altering inputs that are known in advance
# Note that for simulating the fmu with altering inputs that are known in advance
# the simulation api for continuous fmu simulation is a better choice.

while not finished:
while not hp_fmu.finished:
# read fmu state
res = hp_fmu._read_variables(variables)
res.update({'SimTime': hp_fmu.current_time})
Expand All @@ -340,8 +345,7 @@ def plotting_fmt():
else:
hp_fmu._set_variables({'TDryBul': 5+273.15})
# perform simulation step
# Note that calling the basic do_step() function, no input data (attribute "input_table") would be set
finished = hp_fmu._do_step()
hp_fmu._do_step()

# close fmu
hp_fmu.close()
Expand Down
7 changes: 4 additions & 3 deletions examples/e2a_fmu_do_step_simulation_old.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ def run(self, x_act, x_set):
# Call controller (for advanced control strategies that require previous results, use the attribute sim_res)
ctr_action = ctr.run(res_step['bus.processVar'], input_data.loc[sys.current_time]['bus.setPoint']) # .values[0]) # fixme: only makes sense if
# Apply control action to system and perform simulation step
res_step = sys.do_step_wrapper(input_step={'bus.controlOutput': ctr_action}) # # fixme consider returning the last n values for mpc if n==1 return dict, otherwise list of dicts
res_step = sys.inp_step_read(input_step={
'bus.controlOutput': ctr_action}) # # fixme consider returning the last n values for mpc if n==1 return dict, otherwise list of dicts

# ---- Results ---------
# return simulation results as pd data frame
Expand Down Expand Up @@ -199,9 +200,9 @@ def run(self, x_act, x_set):
# ------ Simulation Loop -------
while not sys.finished:
# call controller (for advanced control strategies that require previous results, use the attribute sim_res)
ctr_action = ctr.do_step_wrapper(input_step={'bus.processVar': res_step['bus.processVar']})['bus.controlOutput'] # fixme: controller schreibt auch results dataframe -> eigentlich unnötig
ctr_action = ctr.inp_step_read(input_step={'bus.processVar': res_step['bus.processVar']})['bus.controlOutput'] # fixme: controller schreibt auch results dataframe -> eigentlich unnötig
# write controller output to system FMU as well as pre-known inputs and perform step
res_step = sys.do_step_wrapper(input_step={'bus.controlOutput': ctr_action})
res_step = sys.inp_step_read(input_step={'bus.controlOutput': ctr_action})

# sys.close()
# ctr.close()
Expand Down
12 changes: 8 additions & 4 deletions examples/e3_dymola_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ def main(

# ######################### Simulation API Instantiation ##########################
# %% Setup the Dymola-API:
# organize settings in configuration dict
config_dict = {
'model_name': 'AixLib.Systems.HeatPumpSystems.Examples.HeatPumpSystem',
'cd': cd,
'packages': [aixlib_mo]
}
dym_api = DymolaAPI(
model_name="AixLib.Systems.HeatPumpSystems.Examples.HeatPumpSystem",
cd=cd,
config_dict,
n_cpu=n_cpu,
packages=[aixlib_mo],
show_window=True,
n_restart=-1,
equidistant_output=False,
Expand Down Expand Up @@ -166,5 +170,5 @@ def main(
# TODO-User: Change the AixLib path!
main(
aixlib_mo=r"D:\02_workshop\AixLib\AixLib\package.mo",
n_cpu=1
n_cpu=2
)
4 changes: 0 additions & 4 deletions examples/results/FMU_API.log

This file was deleted.

Loading

0 comments on commit 17c8589

Please sign in to comment.