Data-Driven Thermal Models for Buildings

Csaba Zagoni
Analytics Vidhya
Published in
12 min readDec 1, 2020

--

DarkGreyBox: An open-source python building thermal model inspired by Genetic Algorithms and Machine Learning

Figure 1: Five RC-equivalent thermal model structures compete for the lowest RMSE during fitting to observed data (orange) in a total of 140 pipelines (blue) — model with the lowest error is shown in dark blue (Image by author)

Why is this important?

Constructing simple, accurate and easy-to-interpret thermal models for existing buildings is essential in reducing the environmental impact of our built environment. Think about a large office building that is currently heated by natural gas boilers — how would you evaluate the potential reduction in carbon-dioxide emissions in case the boilers were replaced by a heat pump system which have greatly varying efficiencies at different flow temperatures?

Another interesting area to investigate is demand-side response (DSR). Imagine that the proposed heat pump system is connected to the National Grid Carbon Intensity API, which provides 24-hour forecasts of electricity carbon intensity at a 30-minute temporal resolution. Would it be possible to use the building as a “thermal battery” i.e. slightly increase the temperature of the building during the periods with low carbon intensities and let the building “discharge” during the carbon intensity peaks without significantly affecting the thermal comfort of its occupants?

The thermal response model of the building is a key component in both the above examples. However, buildings are complicated systems and modelling their thermal behaviour is certainly not straightforward.

What options have we got?

In general, modelling approaches can be divided into three categories:

  • White box models — we have full knowledge of how the system behaves
  • Black box models — we have no knowledge of how the system behaves
  • Grey box models — we have some knowledge of how the system behaves

Let’s see how we could interpret our building’s thermal response model given these three categories.

Figure 2: White-box, black-box and grey-box models (Image by author)

In white-box modelling the thermal behaviour of the building and its associated heating system is assumed to be known and deterministic. Its advantage is that its results are easy to interpret, however, the construction of the model requires a really detailed knowledge of the building construction (exact thermal properties of each structural material, thermal bridges, air change rate etc.). Also, there is no straightforward way to adjust the model based on measured data resulting in relatively high errors when modelling existing building performance. Therefore, these models are usually used at the planning stage, where the necessary details are easily accessible but no measured data are available yet.

Machine Learning models are essentially black-box models where based on measured data, the input variables (e.g. weather, internal temperature setpoints) are mapped to the output variables (e.g. actual internal temperature, energy consumption of the heating system). Many different ML algorithms are suitable to carry out this mapping yielding low modelling errors at the expense of the complex mapping being very difficult / impossible to interpret by humans. Usually it is also not trivial for widely used LP/MILP optimisation frameworks to encapsulate such models.

Grey-box modelling is the middle ground between white-box and black-box modelling combining the easy model interpretability of the former with the ability to fit to measured data of the latter. The mathematical thermal response model usually consists of a system of continuous-time stochastic differential equations and discrete-time measurement equations¹.

Meet the RC-Equivalent Thermal Model

A specific type of grey-box thermal model for buildings — called the RC-equivalent thermal model — mathematically describes the modelled building as a simplified thermal system parameterised by thermal resistances, heat capacities and energy inputs analogous to an electrical RC-circuit. The unknown parameters of the thermal models are estimated by using maximum likelihood estimation and results are compared using statistical methods (likelihood ratio tests, autocorrelation functions and cumulated periodograms).

“A grey-box model is established using a combination of prior physical knowledge and statistics, i.e. information embedded in data. The prior physical knowledge is formulated by a set of first-order stochastic differential equations, also called a stochastic linear state-space model in continuous time. The equations describe a lumped model of the heat dynamics of the building.”²

Figure 3: TiTeThRia RC-equivalent thermal model (Image by author)

A wide range of thermal RC-equivalent configurations can be used to model the heat dynamics of buildings. Simply put, these models describe the mapping between the internal temperature (Ti) and the heating system thermal energy output (Ph) given the ambient temperature (Ta) profile (and possibly other external factors e.g. solar irradiation etc.). The figure above shows an RC-equivalent thermal model commonly referred to as TiTeThRia based on its model parameters. To do the mapping described above, this particular model also maintains the internal states of the heating distribution system temperature (Th) and the temperature of the building envelope (Te) and is described by the following set of differential equations:

where:

  • Ci is the heat capacity of the internal air volume
  • Ce is the heat capacity of the building envelope (e.g. the external walls)
  • Ch is the heat capacity of the heating distribution system (e.g. pipes, radiators and their water content)
  • Rih is the thermal resistance between the internal air and the heating distribution system
  • Rie is the thermal resistance between the internal air and the building envelope
  • Rea is the thermal resistance between the building envelope and the ambient air
  • Ria is the thermal resistance between the internal air and the ambient air

The massive advantages of using a model like this over a ML (i.e. black-box) model — e.g. a deep sequence-to-sequence model — are that it is easily interpreted by humans and that it slots easily into other modelling frameworks. Construct a heat pump model in a MILP framework and the grey-box building thermal model just slots in as a set of linear differential equations with a handful of parameters. Doing this with a deep ML model would be quite tricky.

Figure 4: Grey-box vs. black-box parameter space (Image by author)

While the above image is certainly a slight exaggeration, it illustrates the point that grey box model parameters are much more limited in number and much easier to interpret when compared to black box models.

Unfortunately, grey-box models come with their own set of disadvantages:

Initial Condition Values

Successfully finding the global optima of the model parameters relies on them having sensible initial condition values at the start of the fitting process. These have to be estimated based on calculations not dissimilar to white-box modelling. In reality we often lack the detailed knowledge of the building’s thermal properties and therefore our initial conditions are likely to be far from ideal.

Excitation Signal

For the above statistical approach to work well, a specific excitation signal needs to be injected into the building to aid model convergence and performance evaluation³. In most cases we only have access to measured data and cannot expect that the proposal to inject a thermal excitation signal into the building is going to go down well with the building manager.

Model Complexity and Amount of Data

The higher the model complexity and the amount of data we are feeding the model, the less likely that the model will converge.

In a real-life situation we would want to simply plug our measured data into our model and expect it to produce good results. Unfortunately, due to the points explained above the conventional statistical approach to grey-box model fitting process frequently (almost always) falls over and produces no or unusable results.

What if we could take the structure that the existing grey-box model provides (i.e. the RC-equivalent thermal model), spice it up with a pinch of Genetic Algorithms and drop it into a Machine Learning-inspired framework?

Introducing DarkGreyBox

Coming from a Machine Learning background I can think about the RC-equivalent thermal model as a sort of custom ML algorithm. We’ve got some input data fed to a DarkGreyBox model that has certain weights (the RC parameters) and a certain structure inside (described by the set of differential equations) which produces some output data.

We can train a DarkGreyBox model by showing it a set of input-output samples and minimising its internal objective function. As opposed to the usual feature-engineering

— where we manually transform the input data for easier access to valuable information — we are doing something that could be described as “algorithmic engineering”.

If we are fine making that transformation in our heads, a whole new world of possibilities opens up: access to all the ML frameworks’ readily available workflows and tooling.

All we’ve done is that we’ve mixed a bit more black in our grey-box model.

So how do we make sure that the training of our DarkGreyBox model goes smoothly and we don’t get the same issues about model complexity leading to non-convergence?

This is where a bit of inspiration from Genetic Algorithms (GA) may help. You can read a good intro in this article:

The problem we are facing is that on a large dataset with a large parameter space finding the global optima is challenging. As we have no idea about the initial conditions of the models’ parameters our fitting process is likely to fall over except for maybe the simplest model structures. We can try to reduce the complexity of the problem — so that our fitter has an easier job to do — by either reducing model complexity by having a smaller number of parameters (we do not want to do that) or by fitting to less data. If our fitting process will converge when we feed it less data, we can “excite” those initial conditions to move from the default values towards the global optima and try fitting for the entire training set.

Figure 6. DarkGreyBox pipeline architecture (Image by author)

The figure above demonstrates how this works:

  1. Practically, we are splitting the training data set into N much shorter prefit data sets and prefit a particular model structure to those - this will be our initial population. Some of them will fail but the ones that converge will have parameter results that are closer to the global optima than our default initial conditions.
  2. We can also reduce the number of candidates moving forward by applying a filter with an error threshold after the prefit stage - this is the equivalent to a fitness function.
  3. The convergence and the following filtering form our selection.
  4. Now we can train the models on the entire training data set with these prefit parameter results as the new initial conditions. Most likely some of our pipelines will arrive at the same parameter results - this is akin to termination.
  5. Successful pipelines can then make predictions for the test set. Model performance can be evaluated using the usual ML approaches. Nothing stops us from defining multiple model structures as parallel pipelines: and ranking based on a chosen error metric will provide options for the best performing trained models.

Note that there is no crossover or mutation implemented at this point in DarkGreyBox as it seemed overkill for this problem.

Demo Time!

The input data for this demo consists of 33-days worth of measured hourly internal temperature (Ti), ambient temperature (Ta) and heating system thermal power output (Ph) data as shown on figure 7.

Note: This demo data is intentionally far from ideal and particularly challenging to model with low errors. It is taken from a building where there are many factors influencing the heat dynamics of the building that are not accounted for in the modelling (solar gains, passive gains, hot water demand, gas use in the canteen etc.). The time period is relatively short to maintain a reasonable solution time for the demo, however, it includes an also challenging holiday period when the heating system is shut down for a whole week.

Figure 7: Internal temperature (Ti), ambient temperature (Ta) and heating system thermal power output (Ph) (Image by author)

Machine learning frameworks provide a wide range of functionality and DarkGreyBox allows us to tap into these features easily.

As usual in ML, first we want to split our dataset into training and test data (the fitting process will only ever see the training data and we can use the test data to evaluate how well the models generalise/perform on new data). The code below takes the input data spanning 33 days and splits them so that the test set will span 5 days.

Next, we need to define the initial conditions of the model parameters. The initial condition of the internal temperature Ti is the first element of our Ti measured data and hence we do not allow it to vary. We do not need to have any preliminary knowledge of the other parameters' initial conditions and so they can be set to a default value.

We can instantiate a range of models to be evaluated — in this case we are setting up 5 different models starting from the simplest Ti to TiTeThRia with medium complexity.

We need to split our training data set into manageable chunks for the prefit stage. sklearn is quite helpful here again, in case we wanted to define the prefit sets to be 1 day (24 hours) long:

To evaluate the performance of our models we will need to define an error metric. Again, there is a wide range of options available from sklearn:

We can define a prefit “survival threshold” to trim the number of model fits during the training process. Any models achieving a higher error during the prefit stage will not make it into the training stage.

Under the hood, DarkGreyBox uses lmfit.minimize to fit the thermal model parameters to the training data by passing it a custom objective function defined by the model class. The fit method used by lmfit.minimize can be customised as well, and while the standard Nelder-Mead and Levenberg-Marquardt methods work well in general, it is worth experimenting with others. See here for a list of supported methods: https://lmfit.github.io/lmfit-py/fitting.html#fit-methods-table

Setting up is ready! We only need to call the darkgreyfit convenience pipeline function that delegates the work to the prefit, train and test low-level procedures running on multiple processes and reducing / cleaning the interim results. The returned pandas.DataFrame contains the fit models and their train/test performance evaluation.

Results

In this specific demo:

  1. Initial Population: 5 model structures were prefit to 28x 24-hour data chunks resulting in 140 pipelines
  2. Fitness Function: The RMSE of a prefit had to be < 2˚C to survive
  3. Selection: 110 pipelines survived the prefit selection and made it into training on the entire training set
  4. (No Crossover or Mutation)
  5. Termination: Out of the 110 pipelines trained, 11 distinct model results were found (i.e. many pipelines were arriving at the same parameter set results)
Table 1: Details of 11 distinct models’ training results

TiTeThRia Model Results

Perhaps unsurprisingly, the most complex model structure ( TiTeThRia) had the lowest training errors overall.

Figure 8: TiTeThRia model results on the training set (Image by author)
Figure 9: TiTeThRia model results on the test set (Image by author)

The results show that the TiTeThRia model has a good fit on the training set (R2=0.9612) and the errors are low for both the training and test sets (RMSE 0.3719 and 0.3383 respectively) indicating that the model generalises well for data it hasn't seen before (no overfitting problem).

Ti Model Results

It is also interesting to see how the least complex model structure (Ti) performs:

Figure 10: Ti model results on the training set (Image by author)
Figure 11: Ti model results on the test set (Image by author)

In contrast to the TiTeThRia model, the Ti model has a poor fit on the training set (R2=0.7902) which is clear from the temperature plots as well. Due to low model complexity there is a definite underfitting problem going in here, which of course also results in poor test set performance. This is hardly surprising, as the Ti model only keeps internal states of the internal temperatures (Ti) and it is unaware of the internal states of the heating system / building envelope temperatures (Th and Te respectively) - both of which are important for describing the system components' thermal inertia.

Next Steps

Of course this is just a demo, and the models could be further improved by e.g.:

  • adding heat gain of the building envelope due to solar radiation
  • adding heat gain of the interior due to solar radiation via the window apertures
  • adding an interior building medium component (e.g. internal walls that are not part of the building envelope)
  • adding a temperature sensor component
  • etc., etc.

Full Tutorials

See the entire tutorial (and more) on github:

https://github.com/czagoni/darkgreybox/blob/master/docs/tutorials/darkgrey_poc_demo_04.ipynb

References

[1] G. Bauwens, S. Roels, Characterizing Thermal Performance of Buildings Using Dynamic Model Identification (2013), online at https://web.ornl.gov/sci/buildings/conf-archive/2013%20B12%20papers/020-Bauwens.pdf, accessed 14/07/2020

[2] P. Bacher, H. Madsen, Identifying suitable models for the heat dynamics of buildings (2011), Energy and Buildings. 43. 1511–1522. 10.1016/j.enbuild.2011.02.005.

[3] H. Madsen, J. M. Schultz, Short time determination of the heat dynamics of buildings (1993), Thermal Insulation Laboratory Report №243., Technical University of Denmark

--

--

Csaba Zagoni
Analytics Vidhya

Data Scientist at Upside Energy and Environmental Advisor at Greenpeace UK. Strategic guidance & data-driven insight to drive innovation on future of green tech