Solving the Equation and Writing the Code
You will need to refresh the browser for the equations to show
Objectives
- Combine equations from the electrical circuit model into a differential equation
- Solving the differential equation in discrete time steps
- Program a solver of the differential equation in Python
Creating the Equation
Let's use the 1R1C model from the previous chapter
Which has the equation of
Discrete Integration
To keep things basic, we will discretely integrate this using the forward Euler method with a time step of
Which gives us the discrete equation of
represents the current indoor temperature and is the indoor temperature of the next time step
Boundary Conditions
The only unknown boundary condition here is the outdoor air temperature. For this next exercise lets assume that we have a constant outdoor air temperature of 8C, and a starting indoor temperature of 20C.
We also need a time step , let's set it as 15minutes for now. Smaller timesteps will give our results more resolution but will also take longer to solbe
Writing the code
Let's now bring this into python
# Import Libraries for Plotting
import numpy as np
import matplotlib.pyplot as plt
# Set Boundary Conditions
T_out = 8.0 # Constant outdoor temperature [C]
T_in = 20.0 # Starting internal temperature [C]
list_T_in = [T_in] # Initialise a list for collecting teperature data for plotting
#Set Building Parameters
C_m = 500.0 #[Wh/K]
R_total = 0.057 # [K/W]
# Set Integration Parameters
dt = 0.25 # delta time [hours]
simulation_time = 24.0 # length of simulation[hours]
for k in range (int(simulation_time / dt)):
T_in = T_in + ((T_out - T_in)/(C_m * R_total)) * dt
list_T_in.append(T_in)
#Plotting
plt.plot(np.arange(0.0, simulation_time+dt, dt), list_T_in, )
plt.xlabel('Time (Hours)')
plt.ylabel('Indoor Temperature (C)')
plt.show()
Interpreting the results
Congratulations! You have just written your own basic building simulator
Physically, what you have modeled is a small insulated building, that has an internal temperature of 20C with no heating, no occupants, and no sunlight. The outdoor temperature remains constant at 8C. The line shows how the internal temperature drops over a 24 hour period
Playing with the parameters
Try now increase the simulation time to a week = 168 hours
Note how the temperature continues to drop towards the outdoor air temperature of 8 degrees and then flattens out. This is typical RC model behaviour
Task
- Try modifying the values of the values of the resistance and capacitance. What happens?
- Change the outdoor air temperature to 30C