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

Commit 95a0109

Browse files
committed
helper physics functions
1 parent d94d17f commit 95a0109

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed

day_8/physics_functions.py

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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(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+
iWave = 5
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(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+
iWave = 5
137+
138+
# intensity is a function of altitude (for a given wavelength):
139+
intensity = intensity_inf[iWave] * np.exp(-tau[iWave][:])
140+
Qeuv = Qeuv + \
141+
efficiency * \
142+
density_in_m3 * \
143+
intensity * \
144+
cross_section[iWave] * \
145+
energies[iWave]
146+
147+
return Qeuv
148+
149+
#-----------------------------------------------------------------------------
150+
# calculate rho given densities of O (and N2 and O2)
151+
#-----------------------------------------------------------------------------
152+
153+
def calc_rho(density_o, mass_o):
154+
rho = density_o * mass_o * cAMU_
155+
return rho
156+
157+
#-----------------------------------------------------------------------------
158+
# calculate cp. We have hard coded this to be 1500, which is for O, but
159+
# we could pass densities and vibrational states and get the real Cp.
160+
#-----------------------------------------------------------------------------
161+
162+
def calculate_cp():
163+
cp = 1500.0
164+
return cp
165+
166+
#-----------------------------------------------------------------------------
167+
# calculate dT/dt from Q and rho:
168+
#-----------------------------------------------------------------------------
169+
170+
def convert_Q_to_dTdt(Qeuv, rho, cp):
171+
dTdt = Qeuv / (rho * cp)
172+
return dTdt
173+

0 commit comments

Comments
 (0)