"""
Goals of this part of the examples:
1. Learn how to use the `FMU_API`
2. Understand model variables
3. Learn how to change variables to store (`result_names`)
4. Learn how to change parameters of a simulation
5. Learn how to change inputs of a simulation
6. Learn how to run simulations in parallel
"""
import pathlib
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from ebcpy import FMU_API
def main(
working_directory=None,
n_cpu=1,
log_fmu=True,
n_sim=5,
output_interval=100,
with_plot=True
):
"""
Arguments of this example:
:param str working_directory:
Path in which to store the output.
Default is the examples\results folder
:param int n_cpu:
Number of processes to use
:param bool log_fmu:
Whether to get the FMU log output
:param int n_sim:
Number of simulations to run
:param int output_interval:
Output interval / step size of the simulation
:param bool with_plot:
Show the plot at the end of the script. Default is True.
"""
if working_directory is None:
working_directory = pathlib.Path(__file__).parent.joinpath("results")
model_name = pathlib.Path(__file__).parent.joinpath("data", "HeatPumpSystemWithInput.fmu")
fmu_api = FMU_API(model_name=model_name,
working_directory=working_directory,
n_cpu=n_cpu,
log_fmu=log_fmu)
print("Number of variables:", len(fmu_api.variables))
print("Number of outputs:", len(fmu_api.outputs))
print("Number of inputs:", len(fmu_api.inputs))
print("Number of parameters:", len(fmu_api.parameters))
print("Number of states:", len(fmu_api.states))
print("Variables to store when simulating:", fmu_api.result_names)
print("Outputs of the fmu", fmu_api.outputs)
print("Supported setup options:", fmu_api.get_simulation_setup_fields())
fmu_api.sim_setup.start_time = 0
fmu_api.sim_setup.stop_time = 3600
fmu_api.sim_setup.output_interval = output_interval
simulation_setup = {"start_time": 0,
"stop_time": 3600,
"output_interval": output_interval}
fmu_api.set_sim_setup(sim_setup=simulation_setup)
print(fmu_api.parameters['heaCap.C'])
hea_cap_c = fmu_api.parameters['heaCap.C'].value
sizings = np.linspace(0.1, 10, n_sim)
parameters = []
for sizing in sizings:
parameters.append({"heaCap.C": hea_cap_c * sizing})
print("Inputs names are:", fmu_api.inputs)
time_index = np.arange(
fmu_api.sim_setup.start_time,
fmu_api.sim_setup.stop_time,
fmu_api.sim_setup.output_interval
)
t_dry_bulb = np.sin(time_index/3600*np.pi) * 10 + 263.15
df_inputs = pd.DataFrame({"TDryBul": t_dry_bulb}, index=time_index)
print("Results that will be stored", fmu_api.result_names)
fmu_api.result_names = ["heatCap.T", "TDryBul"]
fmu_api.result_names = ["heaCap.T", "TDryBul"]
print("Results that will be stored", fmu_api.result_names)
results = fmu_api.simulate(parameters=parameters,
inputs=df_inputs)
fmu_api.close()
fig, ax = plt.subplots(2, sharex=True)
ax[0].set_ylabel("TDryBul in K")
ax[1].set_ylabel("T_Cap in K")
ax[1].set_xlabel("Time in s")
ax[0].plot(df_inputs, label="Inputs", linestyle="--")
for res, sizing in zip(results, sizings):
ax[0].plot(res['TDryBul'])
ax[1].plot(res['heaCap.T'], label=sizing)
for _ax in ax:
_ax.legend(bbox_to_anchor=(1, 1.05), loc="upper left")
if with_plot:
plt.show()
file = fmu_api.save_for_reproduction(
title="FMUTest",
log_message="This is just an example.")
print("ZIP-File to reproduce all this:", file)
if __name__ == '__main__':
main(
n_cpu=5,
log_fmu=False,
n_sim=5,
output_interval=100
)