Skip to content
This repository was archived by the owner on Jul 25, 2023. It is now read-only.

Commit cfeb7f6

Browse files
committed
the more complete solution
1 parent 33c4969 commit cfeb7f6

File tree

3 files changed

+401
-0
lines changed

3 files changed

+401
-0
lines changed

day_8/Solution/helper_functions.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python
2+
3+
import numpy as np
4+
import re
5+
from datetime import datetime
6+
import matplotlib.pyplot as plt
7+
8+
#-----------------------------------------------------------------------------
9+
# Read in the EUV CSV file. Get out:
10+
# - short and long wavelengths
11+
# - EUV parameters
12+
# - Absorption cross sections for N2, O2, and O
13+
#-----------------------------------------------------------------------------
14+
15+
def read_euv_csv_file(file):
16+
17+
fpin = open(file, 'r')
18+
19+
iColStart = 5
20+
iColEnd = -1
21+
22+
for line in fpin:
23+
cols = line.split(',')
24+
if (cols[0] == 'Short'):
25+
short = np.asarray(cols[iColStart:iColEnd], dtype = float)
26+
if (cols[0] == 'Long'):
27+
long = np.asarray(cols[iColStart:iColEnd], dtype = float)
28+
if (cols[0] == 'F74113'):
29+
f74113 = np.asarray(cols[iColStart:iColEnd], dtype = float)
30+
if (cols[0] == 'AFAC'):
31+
afac = np.asarray(cols[iColStart:iColEnd], dtype = float)
32+
if (cols[0] == 'N2' and cols[2] == 'abs'):
33+
n2cs = np.asarray(cols[iColStart:iColEnd], dtype = float)
34+
if (cols[0] == 'O2' and cols[2] == 'abs'):
35+
o2cs = np.asarray(cols[iColStart:iColEnd], dtype = float)
36+
if (cols[0] == 'O' and cols[2] == 'abs'):
37+
ocs = np.asarray(cols[iColStart:iColEnd], dtype = float)
38+
39+
fpin.close()
40+
41+
data = {'short': short * 1e-10,
42+
'long': long * 1e-10,
43+
'f74113': f74113 * 1e9 * 1e4, # /cm2 to /m2
44+
'afac': afac,
45+
'ocross': ocs * 1e-22,
46+
'o2cross': o2cs * 1e-22,
47+
'n2cross': n2cs * 1e-22}
48+
49+
return data
50+
51+
#-----------------------------------------------------------------------------
52+
# Plot out spectrum to a file
53+
#-----------------------------------------------------------------------------
54+
55+
def plot_spectrum(wavelengths_in_m, intensities, filename):
56+
57+
fig = plt.figure(figsize = (10,5))
58+
ax = fig.add_subplot(111)
59+
60+
ax.scatter(wavelengths_in_m * 1e10, intensities)
61+
ax.set_xlabel('Wavelength (A)')
62+
ax.set_ylabel('Intensity (photons/m2/s)')
63+
64+
fig.savefig(filename)
65+
plt.close()
66+
67+
#-----------------------------------------------------------------------------
68+
# Plot var as a function of altitutde
69+
#-----------------------------------------------------------------------------
70+
71+
def plot_value_vs_alt(alts, values, filename, var_name, is_log = False):
72+
73+
fig = plt.figure(figsize = (5,10))
74+
ax = fig.add_subplot(111)
75+
76+
ax.plot(values, alts)
77+
ax.set_xlabel(var_name)
78+
ax.set_ylabel('Altitude (km)')
79+
if (is_log):
80+
ax.set_xscale('log')
81+
82+
fig.savefig(filename)
83+
plt.close()
84+
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
#!/usr/bin/env python
2+
3+
import numpy as np
4+
5+
#-----------------------------------------------------------------------------
6+
# Define some useful constants
7+
#-----------------------------------------------------------------------------
8+
9+
cKB_ = 1.38064852e-23
10+
cRE_ = 6371.0
11+
cG_ = 9.81
12+
cAMU_ = 1.6726219e-27
13+
cH_ = 6.6261e-34
14+
cC_ = 2.9979e8
15+
cE_ = 1.6022e-19
16+
cD2R_ = 3.1415927 / 180.0
17+
18+
#-----------------------------------------------------------------------------
19+
# calculate the scale height, which is KT/mg, where:
20+
# k - boltzmann's constant
21+
# t = temperature
22+
# m = mass
23+
# g = gravity
24+
# Feed in
25+
#-----------------------------------------------------------------------------
26+
27+
def calc_scale_height(mass_in_amu, alt_in_km, temp_in_k):
28+
29+
gravity = cG_ * (cRE_ / (cRE_ + alt_in_km))
30+
mass_in_kg = mass_in_amu * cAMU_
31+
h_in_km = cKB_ * temp_in_k / mass_in_kg / gravity / 1000.0;
32+
33+
return h_in_km
34+
35+
#-----------------------------------------------------------------------------
36+
# Calculate a hydrostatic solution:
37+
# n(i+1) = n(i) * t(i)/t(i+1) * exp(dz / H)
38+
# where:
39+
# n = density
40+
# t = temperature
41+
# dz = delta-z (delta altitude)
42+
# H = scale-height
43+
#-----------------------------------------------------------------------------
44+
45+
def calc_hydrostatic(density_bc, scale_height_in_km, temp_in_k, alt_in_km):
46+
47+
nAlts = len(alt_in_km)
48+
density = np.zeros(nAlts)
49+
50+
# define our boundary condition
51+
density[0] = density_bc
52+
for iAlt in range(1, nAlts):
53+
tRatio = temp_in_k[iAlt - 1] / temp_in_k[iAlt]
54+
dz = alt_in_km[iAlt] - alt_in_km[iAlt - 1]
55+
h = scale_height_in_km[iAlt]
56+
density[iAlt] = density[iAlt-1] * tRatio * np.exp(-dz / h)
57+
58+
return density
59+
60+
#-----------------------------------------------------------------------------
61+
# EUVAC - take F107 and F107a and return solar spectrum
62+
# See Schunk and Nagy, page 259
63+
#-----------------------------------------------------------------------------
64+
65+
def EUVAC(f74113, afac, f107, f107a):
66+
p = (f107 + f107a)/2.0
67+
if (p < 80):
68+
p = 80.0
69+
intensity = f74113 * (1.0 + afac * (p-80))
70+
return intensity
71+
72+
#-----------------------------------------------------------------------------
73+
# Take a wavelength in m and convert it to energy in Joules:
74+
# e = plank's constant * speed of light / wavelength
75+
#-----------------------------------------------------------------------------
76+
77+
def convert_wavelength_to_joules(wavelength):
78+
energy = cH_ * cC_ / wavelength
79+
return energy
80+
81+
#-----------------------------------------------------------------------------
82+
# Calculate Tau for a given species, given:
83+
# - solar zenity angle (in degrees)
84+
# - density in /m3
85+
# - scale height in km
86+
# - cross sections in /m2
87+
#-----------------------------------------------------------------------------
88+
89+
def calc_tau_complete(SZA_in_deg, density_in_m3, scale_height_in_km, cross_section):
90+
91+
# We are only going to do this for a single wavelength for now!
92+
93+
cs = cross_section[0]
94+
95+
# convert scale height to m:
96+
h = scale_height_in_km * 1000.0
97+
98+
# convert SZA to radians:
99+
sza = SZA_in_deg * cD2R_
100+
101+
# calculate integrated density (which is density * scale height):
102+
integrated_density = density_in_m3 * h
103+
104+
nWaves = len(cross_section)
105+
nAlts = len(density_in_m3)
106+
tau = np.zeros((nWaves, nAlts))
107+
108+
# calculate Tau:
109+
for iWave in range(nWaves):
110+
tau[iWave][:] = integrated_density * cross_section[iWave]
111+
112+
return tau
113+
114+
#-----------------------------------------------------------------------------
115+
# Calculate energy deposition as a function of altitude, given:
116+
# - density of a given species in /m3 as a function of altitude
117+
# - intensity at infinity as a function of wavelength
118+
# - tau as a function of wavelength and altitude
119+
# - cross_section of the given species as a function of wavelength
120+
# - energies of the wavelengths
121+
# - efficiency of the heating (say 30%)
122+
#-----------------------------------------------------------------------------
123+
124+
def calculate_Qeuv_complete(density_in_m3,
125+
intensity_inf,
126+
tau,
127+
cross_section,
128+
energies,
129+
efficiency):
130+
131+
nAlts = len(density_in_m3)
132+
nWaves = len(intensity_inf)
133+
134+
Qeuv = np.zeros(nAlts)
135+
136+
for iWave in range(nWaves):
137+
# intensity is a function of altitude (for a given wavelength):
138+
intensity = intensity_inf[iWave] * np.exp(-tau[iWave][:])
139+
Qeuv = Qeuv + \
140+
efficiency * \
141+
density_in_m3 * \
142+
intensity * \
143+
cross_section[iWave] * \
144+
energies[iWave]
145+
146+
return Qeuv
147+
148+
#-----------------------------------------------------------------------------
149+
# calculate rho given densities of O (and N2 and O2)
150+
#-----------------------------------------------------------------------------
151+
152+
def calc_rho(density_o, mass_o):
153+
rho = density_o * mass_o * cAMU_
154+
return rho
155+
156+
#-----------------------------------------------------------------------------
157+
# calculate cp. We have hard coded this to be 1500, which is for O, but
158+
# we could pass densities and vibrational states and get the real Cp.
159+
#-----------------------------------------------------------------------------
160+
161+
def calculate_cp():
162+
cp = 1500.0
163+
return cp
164+
165+
#-----------------------------------------------------------------------------
166+
# calculate dT/dt from Q and rho:
167+
#-----------------------------------------------------------------------------
168+
169+
def convert_Q_to_dTdt(Qeuv, rho, cp):
170+
dTdt = Qeuv / (rho * cp)
171+
return dTdt
172+
173+
#-----------------------------------------------------------------------------
174+
# Calculate the real rho (ignore!!!)
175+
#-----------------------------------------------------------------------------
176+
177+
def calc_rho_complete(density_o, mass_o,
178+
density_o2, mass_o2,
179+
density_n2, mass_n2):
180+
rho = density_o * mass_o * cAMU_
181+
rho += density_o2 * mass_o2 * cAMU_
182+
rho += density_n2 * mass_n2 * cAMU_
183+
return rho

0 commit comments

Comments
 (0)