Real-Time Energy-Balance Model (Interactive)¶
Interactive Model with Sliders¶
This code starts with the most normal Earth circumstances: albedo of 0.32, solar flux of 1373 watts per meter squared, and epsilon of 0.78. It then animates a line that acts as an active signal that can be adjsuted by sliders for those three properties. This serves to illustrate how the temperature is affected by changing properties.
#Import libraries
%matplotlib widget
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button
import matplotlib.animation as animation
#constants
C = 2.08e8
alpha = 0.32
epsilon = 0.78 #key value
sigma = 5.67e-8
S = 1373
T_a = 242.4
#step array
step = []
temp = []
#Create figure and make space for sliders
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
plt.title('Energy Balance Model')
plt.subplots_adjust(left=0.25, bottom=0.25)
#Sliders
#Each slider has its own axis, and takes the
#min val, max val, and initial val
axflux = plt.axes([0.1, 0.25, 0.0225, 0.63])
slider_flux = Slider(
ax=axflux,
label="Flux",
valmin=900,
valmax=1600,
valinit=1373,
orientation="vertical"
)
axalpha = plt.axes([0.25, 0.05, 0.65, 0.03])
slider_alpha = Slider(
ax=axalpha,
label="Albedo",
valmin=0.1,
valmax=0.6,
valinit=0.32,
orientation="horizontal"
)
axemis = plt.axes([0.25, 0.1, 0.65, 0.03])
slider_emis = Slider(
ax = axemis,
label = "Emissivity",
valmin = 0.5,
valmax = 0.9,
valinit = 0.78,
orientation= "horizontal"
)
#Animation function. Updates for each "i"
def animate(i):
step.append(i)
temp.append((((1 - slider_alpha.val)*(slider_flux.val) + (slider_emis.val*sigma*T_a**4))/(4*sigma))**(1/4))
ax1.set_ylim(220,320)
ax1.plot(step, temp, color = 'green')
ax1.set_xlabel('Step')
ax1.set_ylabel('Temperature (K)')
ani = animation.FuncAnimation(fig, animate, interval = 80)
plt.show()