# Calibration for a building model parameters

In this tutorial we will use the BuildingModelLibrary with the JuliaSimModelOptimizer to calibrate the parameters of a complex building simulation to experimental data.

using BuildingModelLibrary
using JuliaSimModelOptimizer
using ModelingToolkit
using OrdinaryDiffEq
using Unitful
using DataFrames, CSV
using OptimizationOptimJL
using DataInterpolations

model = initialize_model()
nothing # hide
<< @example-block not executed in draft mode >>

First, we will read the experimental data coresponding to measurements of the states of the system, such as individual room temperatures. The fluid temperature for some rooms, one of the parameters of the system is unknown and we want to find it using our model and the experimental data.

For the experimental data, we'll consider a time interval of 5 days. The model is using SI units, so we'll have to convert the time to seconds.

day = ustrip(u"s", 24u"hr")
n_days = 5.0
t0 = 2.0 * day
tend = t0 + n_days

nothing # hide
<< @example-block not executed in draft mode >>

We can now define a experiment corresponding to our (experimental) data

experiment = Experiment(data, model; tspan=(t0, tend), alg=QNDF(autodiff=false))
<< @example-block not executed in draft mode >>

Now we define an inverse problem in which we specify what parameters we want to recover and their bounds.

invprob = InverseProblem([experiment], model,
[
@nonamespace(model.T_fluids_1₊k) => (270.0, 280.0),
@nonamespace(model.T_fluids_2₊k) => (270.0, 280.0),
@nonamespace(model.T_fluids_3₊k) => (270.0, 280.0)
]
)
<< @example-block not executed in draft mode >>

To calibrate the model to data we now just need to specify what algorithm we want to use and after that call calibrate.

alg = SplineCollocate(maxiters=1000, optimizer=BFGS(), interp=CubicSpline)
result = calibrate(invprob, alg)
uconvert.(u"°C", result.*u"K")
<< @example-block not executed in draft mode >>

As we can see, we succesfully recovered our unknown parameters from data.