Tutorial on ebcpy
Assume the following situation. From an experiment we have gathered following data:
We want to use the data as an input to a simulation. However, as visible, the data is noisy and thus may lead to instability of our simulation.
First we will load modules supporting this tutorial. Note that you should install matplotlib first if not already happenend, as only this tutorial needs matplotlib. For usage of ebcpy, you don't need it.
Data Types
Let's specify the path to our measurement data and load it.
If you're familiar with python and DataFrames, you will ask yourself: Why do I need the TimeSeriesData-Class? We implemented this class to combine the powerful pandas.DataFrame class with new functions for an easy usage in the context of Building Energy Systems for three main reasons:
Most data in our case is Time-Dependent, therefore functions for easy conversion between seconds (for simulation) and Timestamps (for measurements) is needed
Most data is stored in files. Typically .hdf and .mat is used, .csv and even .xlsx files occur frequently. Pandas offers different functions (
from_hdf,from_csv) but no function for loading Modelica-Result files.Working with energy related data, you most likely are interested in understanding where a value comes from. For this reason, we base the TimeSeriesData-Class on the pandas MultiColumn-Feature. Every variable (e.g. temperature) will get tags for specific reasons. The default being "raw", assuming that data loaded from a file without tags most likely is unprocessed.
Note how the loaded measurement got the tag 'raw'. Besides that, it's a simple DataFrame. Next, let's adjust the noisy data for the usage in a simulation tool like Modelica.
For this reason, we load the preprocessing module of ebcpy.
Preprocessing
We want to manipulate the values in the DataFrame only. The easiest way to use the processing functions is to call the TimeSeriesData functions:
An alternative is to call the preprocessing module. Let's check the current state of the DataFrame first and then use the preprocessing module:
Now, we can apply either a filter or a moving-average. To further understand differences between both options, adjust the parameters in the box below and rerun the cell. You will see how the output changes. It's up to you, which function you use for your data, a general appraisal cannot be made.
Note: The syntax to get values for an existing variable with a new tag, you have to call:
In this case:
Now, let's assume we want to know when to turn on a certain device based on our measurements. Looking at our data, we know some device turned on or off if the temperature rises above some 34 °C. To reproduce the signal for our simulation, we can use the create_on_off_signal method. For this example we use the default tag "raw", however you may also try "moving_average" or other created tags.
If we are satisfied with our results, we may want to save our process in a file. For this case, the TimeSeriesData-Class holds a save-function:
Let's re-load the data and check if everything was saved correctly and the load-function works.
Let's get a closer look at the DataFrame-Object.
We have two variables, the measured temperature and the created signal. Furthermore, we created new tags, allowing us to always recap where our data came from.
As you may have noticed, the measurement was made for 1 hour with a 1 second interval. Let's assume you want to run the simulation with a bigger time-interval. To not only use the data, but also compare results at a later stage, the preprocessing module holds a function for you. clean_and_space_equally_time_series takes your data and resamples it to the desired frequency. Try different inputs, like "1s", "30s", "2min", "1h" or others.
Be aware that upsampling your data will create artificial values to fill the gaps. In contrast, downsampling is a valid and secure method.
See here for allowed frequencies like "15min".
Conversion
Now, let's get to the simulation part. If you are familiar with Modelica, you know inputs from files may either be in .txt or .mat format. Both options are supported in ebcpy.
Let's take our preprocessed data and convert it to first to .txt, then to .mat:
Dymola API
Now, we will run a simulation using the DymolaAPI-Class. Make sure you have Dymola with a valid license installed on your machine to get this to run.
If you are new to the research at the EBC-Institute, the AixLib is a good starting point for modelling. Most systems in the area of building energy systems have already been implemented.
Note: If you have Dymola installed in an unusual path (e.g. on Windows outside of C:\Program Files (and for 64bit-systems C:\Program Files (x86))) you have to provide the path of your dymola interface and the dymola-executable. Add the following kwargs to the code above to do so:
Let's run a simulation for one minute (60 s) and look at the output:
You may have noticed that our simulation is not equal to our measurement.
To minimize the error between simulation and measurement, the EBC institute offers the python framework AixCaliBuHa. AixCaliBuHa offers different calibrators, all based on the Optimizer in ebcpy. The underlying data-structure is the data_types module.
You can find AixCaliBuHa here
Optimization
As mentioned above, ebcpy provides an Optimizer. Currently, we use the optimizer primarily for calibration. However, the optimizer is capable of solving other problems as well.
We offer an easy to use API for some open-source solvers (scipy, dlib) as well as own implementation of existings methods (currenlty in development).
Note: If you have a reoccuring task of optimization with a similar objective function, the use of this framework may make sense for you. If you just optimize once, this won't be much of a help.
Let's assume we have some function and we want to approximate a quadratic formula to it.
Let's find the optimal parameters by creating our own Optimizer. You may want to pass own arguments to the class for usage in the objective. Just overwrite the __init__ of the Optimizer. Depending on your use-case, you may want to pass x0 and bounds as well. Some solvers don't require initial values. Boundaries are mostly required.
In order to see the advantage of the optimizer class, see the solutions for different frameworks below.
Note: In order to use the framework dlib_minimize, you might need to install dlib first.
If you have any questions or encounter bugs, please feel free to raise an issue! We hope this tutorial made the use case and usage of ebcpy clear to you. We also refer to the python examples in the examples folder. Here you can also test the objects within in the framework.