title | author | date | bibliography | header-includes | abstract |
---|---|---|---|---|---|
Introduction to MPC |
Vasken Dermardiros |
March 31, 2022 |
library.bib |
\usepackage{tikz,pgfplots}
|
Document introduces the background and implementation of an MPC strategy designed to be used in rooftop unit (RTU) commercial or simple residential unit applications. Underlying model is a grey-box physics-based model which is linear with optimization framework to be convex or non-convex when actions are binary. \newline Part of the literature presented herein comes from published or open-sourced documentations produced by myself. See [my list of publications](http://vderm.github.io) and [personal GitHub repo](https://github.com/vderm). |
Model-based predictive controls is an optimization-based control framework where control actions are optimized over a prediction horizon to minimize a cost function given constraints. It relies on a model describing the system dynamics in combination of constraints limiting the actions to allowable or desired ranges. Future estimates of exogenous inputs and disturbances are included, eg. weather forecast, occupants' behaviours.
The MPC framework makes the operating intent explicit: the cost is what needs to be minimized, the constraints are the limits of the system and the variables are what can be controlled. In the conventional scheduled operation, the control desires are implicit in how the sequences are programmed and can be cumbersome to comprehend especially if they contain many heuristics. The owner can only hope occupants will remain comfortable during a demand response event; whereas using MPC, the cost function would force to reduce demand during a demand response event but the constraints would guarantee comfort. What is interesting from the outcomes of MPC is, that in some cases, well-known heuristic rules are discovered by the algorithm: demand limiting, economizer cycles, temperature reset and pre-conditioning of spaces and so on.
The objective here is not to teach you optimization or even what is model-based predictive controls. There are just too many excellent references out there, I would not want to embarrass myself by producing low grade explanations.
You can start by reading this webpage made by grad students from Berkeley's MPC lab, this document demonstrates a simple application that uses CVX which is in part developed by Prof. Stephen Boyd from Stanford. He also has his lectures on convex optimization posted online -- free -- along with his co-authored book, which is excellent -- also free. You can also take a look at this article from the OptiControl team of Switzerland.
What is demo-ed in this notebook is not the "state of the art", it, however, provides an easy way to try a few ideas. If your objective is speed, I suggest you look into using compiled solvers tailored to the problem, e.g. CVXGEN, for non-commercial use. There are also other commercial solvers like GUROBI and CPLEX among many.
Finally, there's also a very interesting approach on linking model calibration with controls by relying on auto-differentiation libraries such as jax or a lot of the tools from Julia's SciML library. In these cases, we would set up a model, train the parameters by using gradient descent on the gradient of the model parameters given the fit loss function, once the model is fit, we would swap the train loss function with the controls loss function and take the gradient instead relative to the controllable inputs. Definitely approaches worth exploring!
I will start by showing what an optimization looks like since too many throw around that word not knowing what they're talking about. Running 10'000 simulations with different parameters and returning the best is not an optimization; it's more of a design exploration. In that sense, genetic algorithms cannot guarantee an optimal result more so than a very good design. Genetic algorithms are explorative algorithms, whereas optimization is exploitative. A hybrid is possible -- see memetic evolutionary algorithms.
The function
The solution $x^$ is called the optimal solution iff $f(x^) \leq f(x)\forallx$. (If the equality holds, then more than one optimal exists and
Specifically, the design of an MPC algorithm requires a discrete time predictive model of the system to be controlled, weights or coefficients of the cost function of the optimization problem, and constraints. When applying the optimal control sequence to the real system the resulting evolution must be as close as possible to the time-series prediction. In such a dynamic optimization problem, the objective or cost function,
We would want to represent the MPC problem in a simple way, so how are things done in the controls literature? One popular approach is using a state-space representation, also see the Wiki page for it! The most general state-space representation of a linear system with
where:
Which can be simplified for an explicit discrete time-invariant system as:
But we make another simplifying assumption: the temperature of the space is directly measured and represents the state of the system -- so called "ground truth dynamics". In other words, matrix
And for the sake of completeness, I added the
The goal is to model the building physics and represent it in a state-space representation which make things quite convenient. There are other neat things in the representation, but I'll invite you to read the state-space representation wiki page.
Straight to it. Here's a representation of a small space or room as a thermal circuit.
In the thermal analogy, a voltage is temperature and current is heat flux. The exterior temperature node,
The HVAC system, $\dot{Q}{\textrm{HVAC}}$, is the only variable we can control. We could use On-Off controls, Proportional control, PI, PID, or use a schedule, but they don't explicitly consider thermal mass. In this illustration, the HVAC represents both heating and cooling -- cooling would have a negative heat input. The solar and other loads, $\dot{Q}{\textrm{solar+loads}}$, are put together since these are input we cannot control but can, sometimes, measure. The heat balance at the node is given in the explicit scheme:
$\displaystyle\frac{C_{\textrm{room}}}{\Delta t} (T(t+1) - T(t)) = U_\textrm{effective} (T_{\textrm{exterior}}(t) - T(t)) + \dot{Q}\textrm{HVAC}(t) + \dot{Q}\textrm{solar+loads}(t)$
And so the temperature at the next timestep can be determined:
$T(t+1) = T(t) + \displaystyle\frac{\Delta t}{C_{\textrm{room}}} \left[ U_\textrm{effective} (T_{\textrm{exterior}}(t) - T(t)) + \dot{Q}\textrm{HVAC}(t) + \dot{Q}\textrm{solar+loads}(t) \right]$
Of course, this is a simplified representation of a space. We are attempting to represent the thermal behaviour of a zone or room with only 2 variables! We are also exploring on adding in a neural network (could also be a Gaussian process model) to cover the residual effects not captured by this model.
Although in SI units (don't get me started on Freedom units!) the correct unit for energy is Joules, temperature in Kelvin, time in seconds, power in Watts (J/s); we would prefer to instead use kWh for energy, $^\circ$C for temperature, hours for time and kW for power. Why? So that the parameters
For a relatively small building, you can expect to have a thermal capacitance in the order of
The idea here is to get:
$T(t+1) = T(t) + \displaystyle\frac{\Delta t}{C_{\textrm{room}}} \left[ U_\textrm{effective} (T_{\textrm{exterior}}(t) - T(t)) + \dot{Q}\textrm{HVAC}(t) + \dot{Q}\textrm{solar+loads}(t) \right]$
to look like:
What to start thinking about is to realize the meaning of
$T(t+1) =
\left[1 - \displaystyle\frac{\Delta tU_\textrm{effective}}{C_{\textrm{room}}} \right] { T(t) } +
\left[\displaystyle\frac{\Delta t}{C_{\textrm{room}}} \right]
{ \dot{Q}_\textrm{HVAC}(t) } +
\left[ \displaystyle \frac{\Delta tU_\textrm{effective}}{C_{\textrm{room}}},~\displaystyle \frac{\Delta t}{C_{\textrm{room}}} \right]
\begin{Bmatrix}
T_{\textrm{exterior}}(t) \
\dot{Q}_\textrm{solar+loads}(t)
\end{Bmatrix}$
Do you see the pattern? Just to make it clear:
$\mathbf {w} (\cdot ) \colon \begin{Bmatrix} T_{\textrm{exterior}} \ \dot{Q}_\textrm{solar+loads} \end{Bmatrix}$
${\displaystyle \mathbf {C} (\cdot )} \colon \left[ \displaystyle \frac{\Delta tU_\textrm{effective}}{C_{\textrm{room}}},\displaystyle \frac{\Delta t}{C_{\textrm{room}}} \right]$
It's a sum of matrix-matrix or matrix-vector multiplications. If we had
For demonstration, let's have an example with three zones where they are assumed to be all adjacent to one another. Note,
$\mathbf {x} (\cdot ) \colon \begin{Bmatrix} T_{\textrm{1}} \ T_{\textrm{2}} \ T_{\textrm{3}} \ \end{Bmatrix}$
$\mathbf {u} (\cdot ) \colon \begin{Bmatrix} \dot{Q}\textrm{HVAC,~1} \ \dot{Q}\textrm{HVAC,~2} \ \dot{Q}_\textrm{HVAC,~3} \ \end{Bmatrix}$
$\mathbf {w} (\cdot ) \colon \begin{Bmatrix} T_{\textrm{exterior}} \ \dot{q}\textrm{solar} \ \dot{q}\textrm{loads} \end{Bmatrix}$
${\displaystyle \mathbf {A} (\cdot )} \colon
\begin{bmatrix}
1 - \displaystyle\frac{\Delta tU_\textrm{1,2}}{C_{\textrm{1}}}
- \displaystyle\frac{\Delta tU_\textrm{1,3}}{C_{\textrm{1}}}
- \displaystyle\frac{\Delta tU_\textrm{1,exterior}}{C_{\textrm{1}}}, &
\displaystyle\frac{\Delta tU_\textrm{1,2}}{C_{\textrm{1}}}, &
\displaystyle\frac{\Delta tU_\textrm{1,3}}{C_{\textrm{1}}} \ \
\displaystyle\frac{\Delta tU_\textrm{1,2}}{C_{\textrm{2}}}, &
1 - \displaystyle\frac{\Delta tU_\textrm{1,2}}{C_{\textrm{2}}}
- \displaystyle\frac{\Delta tU_\textrm{2,3}}{C_{\textrm{2}}}
- \displaystyle\frac{\Delta tU_\textrm{2,exterior}}{C_{\textrm{2}}}, &
\displaystyle\frac{\Delta tU_\textrm{2,3}}{C_{\textrm{2}}} \ \
\displaystyle\frac{\Delta tU_\textrm{1,3}}{C_{\textrm{3}}}, &
\displaystyle\frac{\Delta tU_\textrm{2,3}}{C_{\textrm{3}}}, &
1 - \displaystyle\frac{\Delta tU_\textrm{1,3}}{C_{\textrm{3}}}
- \displaystyle\frac{\Delta tU_\textrm{2,3}}{C_{\textrm{3}}}
- \displaystyle\frac{\Delta t~U_\textrm{3,exterior}}{C_{\textrm{3}}}
\end{bmatrix}$
${\displaystyle \mathbf {B} (\cdot )} \colon \begin{bmatrix} \displaystyle\frac{\eta_1 \Delta t}{C_{\textrm{1}}}, & 0, & 0 \ \ 0, & \displaystyle\frac{\eta_2 \Delta t}{C_{\textrm{2}}}, & 0 \ \ 0, & 0, & \displaystyle\frac{\eta_3 \Delta t}{C_{\textrm{3}}} \end{bmatrix}$
${\displaystyle \mathbf {C} (\cdot )} \colon
\begin{bmatrix}
\displaystyle\frac{\Delta tU_\textrm{1,exterior}}{C_{\textrm{1}}}, & \displaystyle\frac{\sigma_1 \Delta t}{C_{\textrm{1}}}, & \displaystyle\frac{\lambda_1 \Delta t}{C_{\textrm{1}}} \ \
\displaystyle\frac{\Delta tU_\textrm{2,exterior}}{C_{\textrm{2}}}, & \displaystyle\frac{\sigma_2 \Delta t}{C_{\textrm{2}}}, & \displaystyle\frac{\lambda_2 \Delta t}{C_{\textrm{2}}} \ \
\displaystyle\frac{\Delta t~U_\textrm{3,exterior}}{C_{\textrm{3}}}, & \displaystyle\frac{\sigma_3 \Delta t}{C_{\textrm{3}}}, & \displaystyle\frac{\lambda_3 \Delta t}{C_{\textrm{3}}}
\end{bmatrix}$
where:
There's an obvious pattern. We can codify this. Don't let your dreams be dreams, just do it!
${\displaystyle \mathbf {A} (\cdot )} \colon
\begin{bmatrix}
1 - \displaystyle\frac{\Delta tU_\textrm{1,2}}{C_{\textrm{1}}}
- \displaystyle\frac{\Delta tU_\textrm{1,3}}{C_{\textrm{1}}}
- \displaystyle\frac{\Delta tU_\textrm{1,exterior}}{C_{\textrm{1}}}, &
\displaystyle\frac{\Delta tU_\textrm{1,2}}{C_{\textrm{1}}}, &
\displaystyle\frac{\Delta tU_\textrm{1,3}}{C_{\textrm{1}}} \ \
\displaystyle\frac{\Delta tU_\textrm{1,2}}{C_{\textrm{2}}}, &
1 - \displaystyle\frac{\Delta tU_\textrm{1,2}}{C_{\textrm{2}}}
- \displaystyle\frac{\Delta tU_\textrm{2,3}}{C_{\textrm{2}}}
- \displaystyle\frac{\Delta tU_\textrm{2,exterior}}{C_{\textrm{2}}}, &
\displaystyle\frac{\Delta tU_\textrm{2,3}}{C_{\textrm{2}}} \ \
\displaystyle\frac{\Delta tU_\textrm{1,3}}{C_{\textrm{3}}}, &
\displaystyle\frac{\Delta tU_\textrm{2,3}}{C_{\textrm{3}}}, &
1 - \displaystyle\frac{\Delta tU_\textrm{1,3}}{C_{\textrm{3}}}
- \displaystyle\frac{\Delta tU_\textrm{2,3}}{C_{\textrm{3}}}
- \displaystyle\frac{\Delta t~U_\textrm{3,exterior}}{C_{\textrm{3}}}
\end{bmatrix}$
The minimum that the user would need to specify is the lower triangle. With this, we can rebuild the full
${\displaystyle \mathbf {A_{\textrm{lower}}} (\cdot )} \colon \begin{bmatrix} & & \ \ U_\textrm{1,2} & & \ \ U_\textrm{1,3} & U_\textrm{2,3} & \end{bmatrix}$
And we can get to
${\displaystyle \mathbf{A_{\textrm{outer}}}} = {\displaystyle \mathbf{A_{\textrm{lower}}} + \mathbf{A_{\textrm{lower}}}^T} = \begin{bmatrix} & & \ \ U_\textrm{1,2} & & \ \ U_\textrm{1,3} & U_\textrm{2,3} & \end{bmatrix} + \begin{bmatrix} & U_\textrm{1,2} & U_\textrm{1,3} \ \ & & U_\textrm{2,3} \ \ & & \end{bmatrix} = \begin{bmatrix} & U_\textrm{1,2} & U_\textrm{1,3} \ \ U_\textrm{1,2} & & U_\textrm{2,3} \ \ U_\textrm{1,3} & U_\textrm{2,3} & \end{bmatrix}$
Sum the rows of
${\displaystyle \mathbf{A_{\textrm{diagonal}}}} = \texttt{diag} \begin{Bmatrix} \sum(U_{i,j}){i=1} + U\textrm{1,exterior} \ \ \sum(U_{i,j}){i=2} + U\textrm{2,exterior} \ \ \sum(U_{i,j}){i=3} + U\textrm{3,exterior} \end{Bmatrix} = \texttt{diag} \begin{Bmatrix} U_\textrm{1,2} + U_\textrm{1,3} + U_\textrm{1,exterior} \ \ U_\textrm{1,2} + U_\textrm{2,3} + U_\textrm{2,exterior} \ \ U_\textrm{1,3} + U_\textrm{2,3} + U_\textrm{3,exterior} \end{Bmatrix}$
And lastly:
$\mathbf {A} = \mathbb{I} + \Delta t \times \begin{Bmatrix} \displaystyle\frac{1}{C_{\textrm{1}}} \ \ \displaystyle\frac{1}{C_{\textrm{2}}} \ \ \displaystyle\frac{1}{C_{\textrm{3}}} \end{Bmatrix} \odot \left( \mathbf{A_{\textrm{outer}}}-\mathbf{A_{\textrm{diagonal}}} \right)$
${\displaystyle \mathbf {B} (\cdot )} \colon \begin{bmatrix} \displaystyle\frac{\eta_1 \Delta t}{C_{\textrm{1}}} & & \ \ & \displaystyle\frac{\eta_2 \Delta t}{C_{\textrm{2}}} & \ \ & & \displaystyle\frac{\eta_3 \Delta t}{C_{\textrm{3}}} \end{bmatrix} = \Delta t \times \begin{Bmatrix} \displaystyle\frac{1}{C_{\textrm{1}}} \ \ \displaystyle\frac{1}{C_{\textrm{2}}} \ \ \displaystyle\frac{1}{C_{\textrm{3}}} \end{Bmatrix} \odot \texttt{diag} \begin{Bmatrix} \eta_1 \ \ \eta_2 \ \ \eta_3 \end{Bmatrix}$
Generally speaking, in an RTU, we have ON/OFF states for the heating and cooling coils and usually have 1, 2 or more stages per mode. So, the controls are weather a stage is ON or OFF. There's also a fan to consider which is always ON if a coil is energized or simply ON by itself to circulate the air in the space below or to bring in fresh air. The cooling coil is also responsible for dehumidification in hot humid summers and could require heating to bring the very cold dry air temperature back up a little. We'll touch on that later.
So to get to a thermal equivalent:
$\mathbf {u} (\cdot ) \colon \begin{Bmatrix} \dot{Q}\textrm{HVAC,~1} \ \dot{Q}\textrm{HVAC,~2} \ \dot{Q}_\textrm{HVAC,~3} \ \end{Bmatrix}$
We will rely on PowerKit™ to retrieve the thermal output of the different stages of HVAC (note that matrix is transposed to fit on page):
$\mathbf {u_power^T} (\cdot ) \colon \begin{bmatrix} \dot{Q}\textrm{COOL-STG-1,~1} & & \ \dot{Q}\textrm{COOL-STG-2,~1} & & \ \dot{Q}\textrm{HEAT-STG-1,~1} & & \ \dot{Q}\textrm{HEAT-STG-2,~1} & & \ & \dot{Q}\textrm{COOL-STG-1,~2} & \ & \dot{Q}\textrm{COOL-STG-2,~2} & \ & \dot{Q}\textrm{HEAT-STG-1,~2} & \ & \dot{Q}\textrm{HEAT-STG-2,~2} & \ & & \dot{Q}\textrm{COOL-STG-1,~3} \ & & \dot{Q}\textrm{COOL-STG-2,~3} \ & & \dot{Q}\textrm{HEAT-STG-1,~3} \ & & \dot{Q}\textrm{HEAT-STG-2,~3} \end{bmatrix}$
$\mathbf {u_sts} (\cdot ) \colon \begin{Bmatrix} \bm{1}\textrm{COOL-STG-1,~1} \ \bm{1}\textrm{COOL-STG-2,~1} \ \bm{1}\textrm{HEAT-STG-1,~1} \ \bm{1}\textrm{HEAT-STG-2,~1} \ \ \bm{1}\textrm{COOL-STG-1,~2} \ \bm{1}\textrm{COOL-STG-2,~2} \ \bm{1}\textrm{HEAT-STG-1,~2} \ \bm{1}\textrm{HEAT-STG-2,~2} \ \ \bm{1}\textrm{COOL-STG-1,~3} \ \bm{1}\textrm{COOL-STG-2,~3} \ \bm{1}\textrm{HEAT-STG-1,~3} \ \bm{1}\textrm{HEAT-STG-2,~3} \ \end{Bmatrix}$
Finally,
The only tricky one in
${\displaystyle \mathbf {C} (\cdot )} \colon
\begin{bmatrix}
\displaystyle\frac{\Delta tU_\textrm{1,exterior}}{C_{\textrm{1}}}, & \displaystyle\frac{\sigma_1 \Delta t}{C_{\textrm{1}}}, & \displaystyle\frac{\lambda_1 \Delta t}{C_{\textrm{1}}} \ \
\displaystyle\frac{\Delta tU_\textrm{2,exterior}}{C_{\textrm{2}}}, & \displaystyle\frac{\sigma_2 \Delta t}{C_{\textrm{2}}}, & \displaystyle\frac{\lambda_2 \Delta t}{C_{\textrm{2}}} \ \
\displaystyle\frac{\Delta t~U_\textrm{3,exterior}}{C_{\textrm{3}}}, & \displaystyle\frac{\sigma_3 \Delta t}{C_{\textrm{3}}}, & \displaystyle\frac{\lambda_3 \Delta t}{C_{\textrm{3}}}
\end{bmatrix} =
\Delta t \times
\begin{Bmatrix}
\displaystyle\frac{1}{C_{\textrm{1}}} \ \
\displaystyle\frac{1}{C_{\textrm{2}}} \ \
\displaystyle\frac{1}{C_{\textrm{3}}}
\end{Bmatrix} \odot
\left[
\begin{Bmatrix}
U_\textrm{1,exterior} \ \
U_\textrm{2,exterior} \ \
U_\textrm{3,exterior}
\end{Bmatrix},
\begin{Bmatrix}
\sigma_1 \ \
\sigma_2 \ \
\sigma_3
\end{Bmatrix} ,
\begin{Bmatrix}
\lambda_1 \ \
\lambda_2 \ \
\lambda_3
\end{Bmatrix}
\right]$
Quite simply, just do
We could minimize the mean squared error of many-step predictions relative to the model parameters via gradient descent using the true gradient. Assuming all parameters contained in
Variable
In the general sense, MPC is solving a running cost function
With constraints on the system state, controls, their rate of change, and so on. It's very flexible given the need of the application. Ideally, all of the cost functions and constraints would be convex to solve the problem extremely quickly.
The model is an equality constraint given by:
For this small example, the current time is assumed to be
What do we want to actually minimize? Is it energy? Power? If we have the rate structure of the utility, we can weigh the energy and powers based on their $ amounts and get a utility cost. How about comfort. Should it be a function to minimize? Or should it be a hard constraint to satisfy? Things get a bit tricky when mixing two objectives with dissimilar units, like comfort and cost. We would need to add a factor the weigh them appropriately and it would be like saying "what is the loss of productivity ($) given discomfort". That's what this factor represents. If we have an employee at minimum wage and the CEO of a multi-billion dollar company, would this weight be different? Do you see why this is sometimes a sensitive topic?
I prefer not to mix cost and comfort. Pick either one or the other and make the non-objective be an inequality constraint with a slack variable.
Here are some interesting cost functions:
-
Weighted sum ::
$\underset{u}{\textrm{min}} ~ \sum_{k=0}^{N-1}{c(k) u(k)}, ~c: \text{cost weight, electrical or gas rate if } u(k) \text{ is in electrical or gas input}$ -
Weighted norm ::
$\underset{u}{\textrm{min}} ~ c \left\lVert u \right\rVert_n, ~c: \text{norm cost weight, }~n: \text{norm: } \infty \text{ for peak, others depending on needs}$ -
Reference tracking ::
$\underset{u}{\textrm{min}} ~ \left\lVert u - r \right\rVert_n, ~n: \text{norm: 1 or 2}$ -
Reference repelling ::
$\underset{u}{\textrm{min}} ~ \left\lVert u + r\right\rVert_n, ~n: \text{norm: 1 or 2}$
The weighted sum and weighted norm combined can be used to represent Hydro-Quebec Rate M; to also include the previous peak, we can remove the previous peak from
The reference tracking can be useful for grid regularizing cases where a utility would want a building to follow a certain consumption reference profile. We could also use this in a hierarchical setting where a high-level optimizer decides on the load profiles of the assets underneath (buildings, zones, etc.) and then the assets would do what is possible to match the profile without violating any other constraints.
The reference repelling could serve as a way to reduce consumption in a building when rates are high (the rates would be the reference profile we would want to repel from). We would need to make sure the units somehow match through normalization. There would also be a need to reduce
I won't list all the constraints possible because you can be very creative and novel about how to formulate these.
-
Initial condition ::
$x(t=0) = a$ -
Terminal condition ::
$x(t=N) = b$ -
State dynamics ::
${\displaystyle {x} (k+1)=\mathbf {A} {x} (k)+\mathbf {B} {u} (k)} +\mathbf {C} {w} (k) ,~k \in 0, \dots ,~N-1$ -
Limits (lower, upper; less than equal or less than) ::
$\underline{a} \leq a < \overline{a}$ -
Slacked limits ::
$\underline{a} - s_a \leq a < \overline{a} + s_a$ , where$s_a$ is the slack given to$a$ and$0 \leq s_a \leq s_{max}$ restricts the amount of slack given -
Rate of change (continuous) ::
$\left\lVert a(k+1) - a(k) \right\rVert_n \leq a_{slew}$ -
Rate of change (discrete) :: moving_window$(a,\textrm{steps}) \leq a_{slew}$
I've found the easiest way to handle stochasticity is to run MPC in a sample-based approach by sampling all the coefficients and forecasts and assuming them to be fixed for the MPC run. I would then run MPC many many times for that given timestep. You would then collect a lot of optimal control profiles and state trajectories. Might be a fun exercise for you to think about how to collapse those into one profile (since we don't live on infinite planes and can only do 1 thing. Yes yes I know, part of me also wanted to become a professional 1a yoyo master and travel the world demoing my sick yellow airplanes a.k.a. kamikaze 2 into a mannequin double slack combo).
Heat conduction through a medium is governed by Fourier's Law of heat conduction (or sometimes referred as Fourier's Law of heat diffusion).
Starting from a generalized case for multi-dimensional heat transfer the heat conduction equation can be reworked into a finite difference approximation of the partial differential equation (Dermardiros, 2015). The analytical solution can seldom be calculated for the majority of cases and an approximation is necessary.
Simplified to a 1-dimensional system in the
Assuming a constant density of the material, by performing the inner derivative and applying the Product Law of calculus, we obtain:
The specific heat,
And so,
For the case where conductivity is constant, then the equation simplifies to:
Since the above equation can only be solved analytically for certain cases, a finite difference approximation of the partial differential equation is necessary. Writing the 1st order forward difference equation for the left hand side:
Now, the central difference equation for the right hand side 2nd order differential equation:
$\displaystyle \left( \frac{\partial^2 T}{\partial x^2} \right)^{t+\vartheta}i = \frac{T^{t+\vartheta}{i+1} - 2T^{t+\vartheta}i + T^{t+\vartheta}{i-1}}{\Delta x^2} + O(\Delta x)^2$
Using the finite difference approximations, and rearranging the equation for the future timestep, we obtain:
$T^{t+1}i = T^{t}i + \displaystyle \frac{k \Delta t}{\rho c_p(T) \Delta x^2} \left[ (1-\vartheta)(T^{t}{i+1} - 2T^{t}i + T^{t}{i-1}) + \vartheta (T^{t+1}{i+1} - 2T^{t+1}i + T^{t+1}{i-1}) \right]$
Now, let's look at a case where the neighbouring nodes have a fixed boundary temperature,
$T^t_{i-1} = T^t_{i+1} = T^{t+1}{i-1} = T^{t+1}{i-1} = T_{bound}$
$T^{t+1}i \leq T{bound}$
And since,
$\displaystyle \frac{k \Delta t}{\rho c_p(T) \Delta x^2} = \frac{T^{t+1}i-T^t_i}{(1-\vartheta)(T^{t}{i+1} - 2T^{t}i + T^{t}{i-1}) + \vartheta (T^{t+1}_{i+1} - 2T^{t+1}i + T^{t+1}{i-1})}$
To assure numerical stability in the solution, the timestep must be chosen according to the previous equation.
For the Explicit case where
For the Crank-Nicholson case where
Finally, for the Implicit case where
These timesteps assure numerical stability. For the Implicit case, there is no restriction on timestep, however, for very large timesteps, the solution may oscillate. Although the oscillations will eventually dampen, large timesteps do not guarantee physically plausible solutions.
Additionally, these timesteps are for 1-dimensional heat transfer -- which is typically the case for buildings. For 2-d and 3-d heat transfer, the explicit method finite difference method stability criterion will differ. (Its derivation is beyond the scope of this document.) Final note, for 2-d and 3-d heat transfer, the ordering of the nodes may need to be carefully considered to assure the matrix can be inverted is not ill-conditionned.
For a given node
where,
As an example where
Generalizing for a system, the equation can be written in a matrix form:
$\begin{bmatrix} \displaystyle
\sum_j^N{U_{1j}}+\sum_k^M{U_{1k}}+\frac{C_1}{\Delta t} & -U_{12} & \dots & -U_{1N} \
\vdots & \vdots & \ddots & \vdots \
-U_{N1} & -U_{N2} & \dots & \displaystyle
\sum_j^N{U_{Nj}}+\sum_k^M{U_{Nk}}+\frac{C_N}{\Delta t}
\end{bmatrix}
\begin{Bmatrix} T_1 \ \vdots \ T_N \end{Bmatrix}^{t+1} =
\begin{Bmatrix} \displaystyle \dot{Q}1 + \sum_k^M{(U{1kk}T_{kk})}+\frac{C_1}{\Delta t}T_1^t \ \vdots \ \displaystyle \dot{Q}N + \sum_k^M{(U{Nkk}T_{kk})}+\frac{C_N}{\Delta t}T_N^t \end{Bmatrix}$,
where,
Similarly, for the explicit case, the equations can be written in matrix form:
$\begin{Bmatrix} T_1 \ \vdots \ T_N \end{Bmatrix}^{t+1} = \begin{Bmatrix} \displaystyle \frac{\Delta t}{C_1} \ \vdots \ \displaystyle \frac{\Delta t}{C_N} \end{Bmatrix} \odot \left( \begin{bmatrix} \displaystyle -\sum_j^N{U_{1j}}-\sum_k^M{U_{1k}}+\frac{C_1}{\Delta t} & U_{12} & \dots & U_{1N} \ \vdots & \vdots & \ddots & \vdots \ U_{N1} & U_{N2} & \dots & \displaystyle -\sum_j^N{U_{Nj}}-\sum_k^M{U_{Nk}}+\frac{C_N}{\Delta t} \end{bmatrix} \begin{Bmatrix} T_1 \ \vdots \ T_N \end{Bmatrix}^t + \begin{Bmatrix} \displaystyle \dot{Q}1 + \sum_k^M{(U{1kk}T_{kk})} \ \vdots \ \displaystyle \dot{Q}N + \sum_k^M{(U{Nkk}T_{kk})} \end{Bmatrix} \right),$
where,
There are times, however, when a thermal node will have negligeable thermal capacitance
Performing an energy balance at the thermal node with negligeable thermal capacitance, the finite difference equation becomes:
$T_i^{t+1} = \displaystyle \frac{\displaystyle \sum_{j}{[U_{ij}^t \ T_j^t]} + \displaystyle \sum_{k}{[U_{ikk}^t \ T_{kk}^t]} + \dot{Q}i}{\displaystyle \sum{j}{U_{ij}^t} + \displaystyle \sum_{k}{U_{ikk}^t}}$
//EOF