Professional Documents
Culture Documents
Sample of Work Code
Sample of Work Code
Sample of Work Code
Develop an algorithm in Jupyter to show the effects of planetary temperature variations due to the
solar heat flux, planetary composition, and distance from the host star. This will include a base
model of Earth’s temperature and seasonal variations. I will use this information summarize and
compare Earth to Mars to see the differences with living on Earth to Mars. An overview of Earth,
including the solar heat flux, radiation levels, and water cycle will conclude the importance of
planetary health and why Earth is habitable. The temperature differences with Earth due to polar ice
cap melting will also be compared.
By Kassandra Gurule
# Load needed modules for Earth Base Models and Mars Model
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
%matplotlib inline
In [2]:
# Calculation for Earth's heat capacity at 100 meters deep - 71% ocean and 29
% silicate land
volume = 4 * np.pi * R_e**2 * depth # Earth’s volume - top 100 meters
HC_earth = 0.71 * (c_h2o * volume * rho_h2o) + 0.29 * (c_sil * volume * rho_s
il ) # Earth's heat capacity (J K-1)
HC_bar = HC_earth/(4*np.pi*R_e**2) # Earth's heat capacity per unit
area (J m-2 K-1)
In [4]:
# Set the initial conditions and create data for the needed plot
n = 300 # Number of segments for plot
y_inter = np.linspace(-1,1,n+1) # Position of interfaces for plot
y_mid = (y_inter[0:n]+y_inter[1:n+1])/2 # Position of midpoints for plot
d_y = y_inter[1]-y_inter[0] # Segment length for plot
model_latitude = np.arcsin(y_mid)/deg2rad # Latitude of Earth at segment ce
nters for plot
#Solar Flux Functions Needed for latitudinal varying solar radiation of Earth
def solar_y(y):
s_1=1.241-0.723*y**2
return(s_1)
# Temperature and time dependent albedo that changes by Latitude over 100 Ear
th years
def alpha_time(t,time_end,y,T,option=1):
if option==3:
a_0 = albedo_1(t,time_end,y)
else:
a_0 = albedo_earth(y)
for i in range(n):
if T[i] <=(273.15-25):
a_0[i]=0.55
else:
a_0[i]=a_0[i]
type(a_0)
return a_0
In [8]:
In [9]:
# Call the time integrator to solve the equation numerically for the graph
solution_1 = solve_ivp(earth_latitude_temp_time, (0,time_end), T_0, "Radau",
timesteps, max_step=d_t)
# Extract time and temperature fields from the solution needed for the graph
time_latitude_temp_time = solution_1.t
temp_latitude_temp_time = solution_1.y
In [10]:
#conversions needed for calculations for Earth Model with no Polar Ice Caps
deg2rad = np.pi/180.0 # converstion factor - degrees to radians
days_e = 24*3600 # Number of seconds per Earth day
yrs_sec = 365.25*24*3600 # Time - number of seconds in one year
# Calculation for Earth's heat capacity at 100 meters deep – No Polar Ice Caps
# Calculation for Earth's heat capacity at 72.7% ocean (1.7% increase from Po
lar Ice Caps completely melting) and 27.3% silicate land at 100 meter depth f
or the heat capacity of the oceans
volume_2 = 4 * np.pi * R_e**2 * depth_2 # Top 100 meters of earth volume
(depth)
HC_earth_2 = 0.71 * (c_h2o * volume_2 * rho_h2o) + 0.29 * (c_sil * volume_2 *
rho_sil ) # Earth's heat capacity (J K-1)
HC_bar_2 = HC_earth_2/(4*np.pi*R_e**2) # Earth's heat capacity – no Po
lar Ice Caps - per unit area (J m-2 K-1)
In [14]:
# Set the initial conditions and create data for needed plot
n = 300 # Number of segments for plot
y_inter = np.linspace(-1,1,n+1) # Position of interfaces for plot
y_mid = (y_inter[0:n]+y_inter[1:n+1])/2 # Position of midpoints for plot
d_y = y_inter[1]-y_inter[0] # Segment length for plot
model_latitude = np.arcsin(y_mid)/deg2rad # Latitude of Earth at segme
nt centers for plot
# Set initial conditions for the model of Earth with no Polar Ice Caps
T_0 = 288 + 0*y_mid # Using the constant initial temperat
ure of Earth
In [16]:
# Solar Flux Functions Needed for Latitudinal varying solar radiation for Ear
th Model
def solar_y(y):
s_1=1.241-0.723*y**2
return(s_1)
# Solar Radiation Calculations for Latitudes and Seasons on Earth with no Pol
ar Ice Caps
def solar_y_seasonal(t,time_end,y):
time_days = t/days_e # t is in seconds, this converts t to Earth days
julian_day = np.mod(time_days,365) # subtracts the number of whole Earth
years from t
delta_1 = 23.45*np.cos(2*np.pi*(julian_day-172)/365) # Calculate declinat
ion angle for JD on Earth at 23.5 degree axis tilt (starts on January 1)
# The declination angle is the angle at which the Sun is directly overhead fo
r any given Julian day
delta_array_1 = np.repeat(delta_1,n)
value = -np.tan(delta_array_1*deg2rad)*np.tan(model_latitude*deg2rad)
value = np.where((value<-1.0),-1.0,value)
value = np.where((value>1.0),1.0,value)
Hr_0=np.arccos(value) # hour angle
Q_0 = (S_0/np.pi)*(Hr_0*np.sin(model_latitude*deg2rad)*np.sin(delta_array
_1*deg2rad) + np.cos(model_latitude*deg2rad)*np.cos(delta_array_1*deg2rad)*np
.sin(Hr_0))
return(Q_0)
In [17]:
# Create Functions to Calculate the Albedo of Earth with no Polar Ice Caps -
Earth changing albedo by the Latitude calculated
def albedo_earth(y):
P_1 = 1/2* (3*y**2-1)
albedo = alpha + 0.25*P_1
return(albedo)
# Temperature and time dependent albedo that changes by Latitude for Earth wi
th no Polar Ice Caps
def alpha_time(t,time_end,y,T,option=1):
if option==3:
a_0 = albedo_1(t,time_end,y)
else:
a_0 = albedo_earth(y)
for i in range(n):
if T[i] <=(273.15-25):
a_0[i]=0.55
else:
a_0[i]=a_0[i]
type(a_0)
return a_0
In [18]:
In [19]:
# Compute the right side of the equation needed for graph of Earth Temperatur
e variations over 100 Earth years with no Polar Ice Caps
def earth_latitude_temp_time_2(t,T):
# Calculate the energy flux at each interface needed for each of Earth's
latitudes
F_1 = np.zeros(n+1)
F_1[1:n] = -D_0*(1-y_mid[1:n]**2)*(T[1:n]-T[0:n-1])/d_y
F_1[0] = 0
F_1[n] = 0
# Calculate the diffusion term at the segment centers - NPI
diffusion_earth = -(F_1[1:n+1]-F_1[0:n])/d_y
# Compute the right side of the equation needed for graph of Earth Temper
ature variations over 100 Earth years
heat_diffusion_2 = diffusion_earth/HC_bar_2
earth_albedo_temp_time_2 = (solar_y_seasonal(t,time_end,y_mid))*(1-alpha_time
(t,time_end,y_mid,T))/HC_bar_2 # with albedo and solar parameterization inclu
ded for the declination angle of each latitude - NPI
lat_range_2 = (1-(earth_emm/2))*sigma*T**4/HC_bar
right_eq_2 = earth_albedo_temp_time_2 - lat_range_2 + heat_diffusion_2
return right_eq_2
# Call the time integrator to solve the equation numerically for the graph
solution_2 = solve_ivp(earth_latitude_temp_time_2, (0,time_end), T_0, "Radau"
, timesteps, max_step=d_t)
# Extract time and temperature fields from the solution needed for the graph
time_latitude_temp_time_2 = solution_2.t
temp_latitude_temp_time_2 = solution_2.y
In [20]:
# Plot final temperature vs latitude for Earth with melted Polar Ice Caps
figure = plt.figure(figsize=(10,5))
plt.plot(model_latitude, temp_latitude_temp_time_2[:,-1],label='Temperature o
f Earth per Latitude - No Ice Polar Caps');
plt.xlabel('Earth Latitude')
plt.ylabel('Earth Temperature')
plt.title('Earth Base Model of Surface Temperature vs Latitude - No Polar Ice
Caps')
plt.legend()
plt.savefig('earth_base_model_surface_temperature_vs_latitude_100 - NPI.jpg')
plt.show()
In [21]:
# Plot temperature vs time for one latitude of Earth Model with no Polar Ice
Caps
fig = plt.figure(figsize=(20,10))
plt.plot(time_latitude_temp_time_2/yrs_sec, temp_latitude_temp_time_2[-90,:],
label='Temperature at Earth Equator: Based on No Polar Ice Caps');
plt.xlabel('Time (years)')
plt.ylabel('Temperature')
plt.title('Base Model of Earth: Surface Temperature for the Equator Over 100
Years - No Polar Ice Caps')
plt.legend()
plt.savefig('earth_base_model_surface_temperature_for_the_equator_100 - NPI.j
pg')
plt.show()
# Plot temperature vs time for several latitudes of Earth Model with no Polar
Ice Caps
fig = plt.figure(figsize=(30,15))
plt.plot(time_latitude_temp_time_2/yrs_sec, temp_latitude_temp_time_2[150,:],
color='red',linewidth=4,label='Equator') # 0 deg N, band 150
plt.plot(time_latitude_temp_time_2/yrs_sec, temp_latitude_temp_time_2[188,:],
color='orange',linewidth=4,label='Tropics') # 23 deg N, band 188
plt.plot(time_latitude_temp_time_2/yrs_sec, temp_latitude_temp_time_2[225,:],
color='green',linewidth=4,label='Mid Latitudes') # 45 deg N, band 225
plt.plot(time_latitude_temp_time_2/yrs_sec, temp_latitude_temp_time_2[260,:],
color='blue',linewidth=4,label='High Latitudes') # 66 def N, band 260
plt.xlabel('Time (years)',fontsize=25)
xticks=np.arange(0,101,5)
plt.xticks(xticks,fontsize=25)
plt.ylabel('Temperature (K)',fontsize=25)
yticks = np.arange(270,320,5)
plt.yticks(yticks,fontsize=25)
plt.title('Base Model of Earth: Surface Temperature over 100 Years - No Polar
Ice Caps',fontsize=30)
plt.legend(loc='lower right',fontsize=25)
plt.savefig('earth_base_model_surface_temperature_for_latitudes_100 - NPI.jpg
')
plt.show()
Model for Mars
In [22]:
# Calculation for Mars's heat capacity at 60% silicate and 40% basalt rock
volume_3 = 4 * np.pi * R_mars**2 * depth_mars
# Top 70 meters of Mars's volume (depth)
HC_mars = .40 * (c_bas * volume_3 * rho_bas) + 0.60 * (c_sil * volume_3 * rho
_sil ) # Mars's heat capacity (J K-1)
HC_bar_mars = HC_mars/(4*np.pi*R_mars**2) # Mars's heat capacity per uni
t area (J m-2 K-1)
In [24]:
# Set the initial conditions and create data for plot for Mars
n = 300 # Number of segments
y_inter = np.linspace(-1,1,n+1) # Position of interfaces
y_mid = (y_inter[0:n]+y_inter[1:n+1])/2 # Position of midpoints
d_y = y_inter[1]-y_inter[0] # Segment length
model_latitude = np.arcsin(y_mid)/deg2rad # Latitude of Mars at segment cen
ters
# Solar Flux Functions Needed for Mars Model with latitudinal varying solar r
adiation
def solar_mars(y):
s_2=1.241-0.723*y**2
return(s_2)
# Temperature and time dependent albedo that changes by Latitude for Mars
def alpha_time_mars(t,time_end_mars,y,T_1,option=1):
if option==3:
a_1 = albedo_2(t,time_end_mars,y)
else:
a_1 = albedo_mars(y)
for i in range(n):
if T_1[i] <=(215-15):
a_1[i]=0.55
else:
a_1[i]=a_1[i]
type(a_1)
return a_1
In [28]:
In [29]:
# Compute the right side of the equation needed for graph of Mars Temperature
variations over 100 Earth years
def mars_latitude_temp_time(t,T):
# Calculate the energy flux at each interface needed for each of Mars’s l
atitudes
F_m = np.zeros(n+1)
F_m[1:n] = -D_1*(1-y_mid[1:n]**2)*(T[1:n]-T[0:n-1])/d_y
F_m[0] = 0
F_m[n] = 0
# Calculate the diffusion term at the segment centers - Mars
diffusion_mars = -(F_m[1:n+1]-F_m[0:n])/d_y
# Compute the right side of the equation needed for graph of Mars Tempera
ture variations over 20 Mars years
heat_diffusion_m = diffusion_mars/HC_bar_mars
mars_albedo_temp_time = (solar_mars_seasonal(t,time_end_mars,y_mid))*(1-a
lpha_time_mars(t,time_end_mars,y_mid,T))/HC_bar_mars # with albedo and solar
parameterization included for the declination angle of each latitude - Mars
lat_range_m = (1-(mars_emm/2))*sigma*T_1**4/HC_bar_mars
right_eq_m = mars_albedo_temp_time - lat_range_m + heat_diffusion_m
return right_eq_m
# Call the time integrator to solve the equation numerically for the graph
solution_m = solve_ivp(mars_latitude_temp_time, (0,time_end_mars), T_1, "Rada
u", timesteps_mars, max_step=d_t)
# Extract time and temperature fields from the solution needed for the graph
time_latitude_temp_time_m = solution_m.t
temp_latitude_temp_time_m = solution_m.y
In [30]: