77 lines
4.0 KiB
Python
77 lines
4.0 KiB
Python
#My first tijme making a plot for climate modeling.
|
|
#Hopefully, this works.
|
|
#Note that using spreadsheets for this course is optional;
|
|
#the only way to get credit is with Python3!
|
|
|
|
"""The background for this material can be found in Sections 2 and 3 of Part I
|
|
of this class. Joules are units of energy; Watts are energy flow (J/s).
|
|
The temperature of a planet is determined by balancing energy fluxes in and out of a planet.
|
|
Incoming solar heat is determined by L*(1-albedo)/4,
|
|
and outgoing infrared is calculated as epsilon * sigma * T^4."""
|
|
|
|
"""The heat capacity of the surface planet depends on the water thickness.
|
|
Using the fact that 1 gram of water warms by 1 K with a 4.2J (1 cal) of heat,
|
|
and the density of water, to calculate how many Joules of energy it takes
|
|
to raise the temperature of 1 m2 of the surface, given some water depth
|
|
in meters."""
|
|
|
|
"""The goal is to numerically simulate how the planetary temperature of a
|
|
naked planet would change through time as it approaches equilibrium
|
|
(the state at which it stops changing, which we calculated before).
|
|
The planet starts with some initial temperature.
|
|
The “heat capacity” (units of Joules / m2 K) of the planet is set by
|
|
a layer of water which absorbs heat and changes its temperature.
|
|
If the layer is very thick, it takes a lot more heat (Joules) to change the temperature.
|
|
The differential equation you are going to solve is:"""
|
|
#d(HeatContent)/dt = L*(1-alpha)/4 - epsilon * sigma * T^4
|
|
"where the heat content is related to the temperature by the heat capacity:"
|
|
#T[K] = HeatContent [J/m2] / HeatCapacity [J/m2 K]
|
|
"""The numerical method is to take time steps, extrapolating the heat content
|
|
from one step to the next using the incoming and outgoing heat fluxes,
|
|
same as you would balance a bank account by adding all the income and subtracting all the expenditures
|
|
over some time interval like a month.
|
|
The heat content "jumps" from the value at the beginning of the time step,
|
|
to the value at the end, by following the equation:"""
|
|
#HeatContent(t+1) = HeatContent(t) + dHeatContent/dT * TimeStep
|
|
"""This scheme only works if the time step is short enough so that
|
|
nothing too huge happens through the course of the time step.
|
|
Set the model up with the following constants:"""
|
|
timeStep = 40 # years; convert to seconds.
|
|
time = 0 # years; this shall denote how much time has passed in total.
|
|
T = 0 # Kelvin, according to question demands.
|
|
waterDepth = 2000 # meters; multiply by cH2O and water density.
|
|
L = 1350 # W m-2, same as J s-1 m-2 (Solar Constant) -> multiply by no. of seconds in each timestep
|
|
albedo = 0.3 # alpha
|
|
epsilon = 1
|
|
sigma = 5.67E-8 # W m-2 K-4
|
|
cH2O = 4200 # specific heat capacity of water (units of 4200 J kg-1 K-1).
|
|
rhoH2O = 1000 # Density is 1000 kg m-3.
|
|
energy_per_m2 = waterDepth * cH2O * rhoH2O #in J m-2 K-1
|
|
#heat influx and outflux must be calculated along the way.
|
|
import numpy
|
|
import matplotlib.pyplot
|
|
#Getting our lists needed with the very first variables already in:
|
|
nSteps = input("Please use 20 or more")
|
|
#apparently the autograder that the MOOC uses also mistakes strings for operators.
|
|
nSteps = int(nSteps)
|
|
time_list = [time]
|
|
temperature_list = [T]
|
|
HeatContent = 0 #only true when T = 0 Kelvin, this is the heat content of the Earth.
|
|
difference = 0
|
|
outflux = 0
|
|
for x in range(nSteps):
|
|
time = time + timeStep
|
|
time_list.append(time)
|
|
difference = (L * (1 - albedo) / 4 - (epsilon * sigma * T**4)) #W m-2
|
|
HeatContent = HeatContent + difference * 365 * 24 * 60 * 60 * timeStep #J m-2
|
|
T = (HeatContent / energy_per_m2) #Kelvin
|
|
outflux = epsilon * sigma * T**4 # as per instructions
|
|
temperature_list.append(T)
|
|
#derive answers and end with:
|
|
matplotlib.pyplot.plot(time_list, temperature_list)
|
|
matplotlib.pyplot.show()
|
|
#Give the last temperature derived by the model:
|
|
#print(temperature_list[-1], outflux)
|
|
#The tolerances in the grader for temperatures are 2-3 degrees C,
|
|
#and for the heat fluxes, +- 0.1 W/m2.
|