-
Notifications
You must be signed in to change notification settings - Fork 11
/
atmosphere.py
156 lines (120 loc) · 4.65 KB
/
atmosphere.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"Models for atmospheric quantities"
import numpy as np
from gpkit import Variable, Model, units, SignomialsEnabled
from gpkit.tools import te_exp_minus1
from gpkit.constraints.tight import TightConstraintSet as TCS
# pylint: disable=bad-whitespace
GAS_CONSTANT = 287 # [J/(kg*K)]
GRAVITATIONAL_ACCEL = 9.81 # [m/s^2]
g = Variable('g', GRAVITATIONAL_ACCEL, 'm/s^2', 'Gravitational acceleration')
h = Variable('h', 'm', 'Altitude')
mu = Variable('\\mu', 'kg/(m*s)', 'Dynamic viscosity')
p = Variable('p', 'Pa', 'Pressure')
R = Variable('R', GAS_CONSTANT, 'J/(kg*K)', 'Specific gas constant (air)')
rho = Variable('\\rho', 'kg/m^3', 'Density')
T = Variable('T', 'K', 'Temperature')
class Troposphere(Model):
"""
Density and dynamic viscosity as a function of altitude based on
standard atmosphere model for the Troposphere
Assumptions: only valid to the top of the Troposphere (~11km)
References:
Anderson, Introduction to Flight
https://en.wikipedia.org/wiki/Density_of_air#Altitude
http://www.digitaldutch.com/atmoscalc/index.htm
Arguments
---------
min_rho: bool
If true, model expects downward external pressure on rho
If false, model expects upward external pressure on rho
"""
def __init__(self, min_rho=True, **kwargs):
self.min_rho = min_rho
Lval = 0.0065 # [K/m]
th = GRAVITATIONAL_ACCEL/(GAS_CONSTANT*Lval) # [-]
L = Variable('L', Lval, 'K/m', 'Temperature lapse rate')
p_0 = Variable('p_0', 101325, 'Pa', 'Pressure at sea level')
T_0 = Variable('T_0', 288.15, 'K', 'Temperature at sea level')
if min_rho:
objective = rho # minimize density
else:
objective = 1/rho # maximize density
# Temperature lapse rate constraint
if min_rho:
with SignomialsEnabled():
constraints = TCS([T_0 <= T + L*h])
else:
constraints = TCS([T_0 >= T + L*h])
constraints += [h <= 11000*units.m,
h >= 1E-6*units.m,
# Pressure-altitude relation
(p/p_0)**(1/th) == T/T_0,
# Ideal gas law
rho == p/(R*T),
]
su = Sutherland()
lc = su.link(constraints)
Model.__init__(self, objective, lc, **kwargs)
@classmethod
def test(cls):
m = cls()
if m.min_rho:
m.localsolve()
else:
m.solve()
class Tropopause(Model):
"""
Density and dynamic viscosity as a function of altitude based on
standard atmosphere model for the Tropopause
Assumptions: Only valid in the Tropopause (11 km - 20 km)
References:
Anderson, Introduction to Flight
http://www.digitaldutch.com/atmoscalc/index.htm
"""
def __init__(self, **kwargs):
T_tp = 216.65
k = GRAVITATIONAL_ACCEL/(GAS_CONSTANT*T_tp)
p11 = Variable('p_{11}', 22630, 'Pa', 'Pressure at 11 km')
objective = 1/rho # maximize density
constraints = [h >= 11*units.km,
h <= 20*units.km,
# Temperature is constant in the tropopause
T == T_tp,
# Pressure-altitude relation, using taylor series exp
TCS([np.exp(k*11000)*p11/p >=
1 + te_exp_minus1(g/(R*T)*h, 15)], reltol=1E-4),
# Ideal gas law
rho == p/(R*T),
]
su = Sutherland()
lc = su.link(constraints)
Model.__init__(self, objective, lc, **kwargs)
@classmethod
def test(cls):
cls().solve()
class Sutherland(Model):
"""
Dynamic viscosity (mu) as a function of temperature
References:
http://www-mdp.eng.cam.ac.uk/web/library/enginfo/aerothermal_dvd_only/aero/
atmos/atmos.html
http://www.cfd-online.com/Wiki/Sutherland's_law
"""
def __init__(self, **kwargs):
T_s = Variable('T_s', 110.4, "K", "Sutherland Temperature")
C_1 = Variable('C_1', 1.458E-6, "kg/(m*s*K^0.5)",
'Sutherland coefficient')
t_plus_ts_approx = (T + T_s).mono_approximation({T: 288.15,
T_s: T_s.value})
objective = mu
constraints = [t_plus_ts_approx * mu == C_1 * T**1.5]
Model.__init__(self, objective, constraints, **kwargs)
@classmethod
def test(cls):
m = cls()
m.substitutions.update({"T": 288})
m.solve()
if __name__ == "__main__":
Troposphere.test()
Tropopause.test()
Sutherland.test()