-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththermodynamics.py
291 lines (210 loc) · 6.92 KB
/
thermodynamics.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from numpy import interp,pi,cos,sin,arctan2,sqrt,linspace,min,exp,log,where
#-----------------------------------------------------------------------
# Here we go. A set of functions that I use from time to time to calculate
# the basic stuff that I'm sick of doing over and over! I'm going to
# endeavour to include references and global constants to make it all nice
# and legible.
#-----------------------------------------------------------------------
Rs_da=287.05 # Specific gas const for dry air, J kg^{-1} K^{-1}
Rs_v=461.51 # Specific gas const for water vapour, J kg^{-1} K^{-1}
Cp_da=1004.6 # Specific heat at constant pressure for dry air
Cv_da=719. # Specific heat at constant volume for dry air
Cp_v=1870. # Specific heat at constant pressure for water vapour
Cv_v=1410. # Specific heat at constant volume for water vapour
Cp_lw=4218 # Specific heat at constant pressure for liquid water
Epsilon=0.622 # Epsilon=Rs_da/Rs_v; The ratio of the gas constants
degCtoK=273.15 # Temperature offset between K and C (deg C)
rho_w=1000. # Liquid Water density kg m^{-3}
grav=9.81 # Gravity, m s^{-2}
Lv=2.5e6 # Latent Heat of vaporisation
boltzmann=5.67e-8 # Stefan-Boltzmann constant
mv=18.0153 # Mean molar mass of water vapor(g/mol)
_devel="working"
def Theta(tempk,pres,pref=100000.):
"""Potential Temperature
INPUTS:
tempk (K)
pres (Pa)
pref: Reference pressure (default 100000 Pa)
OUTPUTS: Theta (K)
Source: Wikipedia
Prints a warning if a pressure value below 2000 Pa input, to ensure
that the units were input correctly.
"""
try:
minpres=min(pres)
except TypeError:
minpres=pres
if minpres<2000:
print "WARNING: P<2000 Pa; did you input a value in hPa?"
return tempk*(pref/pres)**(Rs_da/Cp_da)
def TempK(theta,pres,pref=100000.):
"""Inverts Theta function."""
try:
minpres=min(pres)
except TypeError:
minpres=pres
if minpres<2000:
print "WARNING: P<2000 Pa; did you input a value in hPa?"
return theta*(pres/pref)**(Rs_da/Cp_da)
def ThetaE():
"""Equivalent potential temperature"""
raise NotImplementedError
def ThetaV(tempk,pres,e):
"""Virtual Potential Temperature
INPUTS
tempk (K)
pres (Pa)
e: Water vapour pressure (Pa) (Optional)
"""
mixr=MixRatio(e,pres)
theta=Theta(tempk,pres)
return theta*(1+mixr/Epsilon)/(1+mixr)
def GammaW(tempk,pres,e=None):
"""Function to calculate the moist adiabatic lapse rate (deg C/Pa) based
on the temperature, pressure, and rh of the environment.
INPUTS:
tempk (K)
pres (Pa)
RH (%)
RETURNS:
GammaW: The moist adiabatic lapse rate (Dec C/Pa)
"""
tempc=tempk-degCtoK
es=SatVap(tempc)
ws=MixRatio(es,pres)
if e is None:
# assume saturated
e=es
w=MixRatio(e,pres)
tempv=VirtualTempFromMixR(tempk,w)
latent=Latentc(tempc)
A=1.0+latent*ws/(Rs_da*tempk)
B=1.0+Epsilon*latent*latent*ws/(Cp_da*Rs_da*tempk*tempk)
Rho=pres/(Rs_da*tempv)
Gamma=(A/B)/(Cp_da*Rho)
return Gamma
def DensityHumid(tempk,pres,e):
"""Density of moist air.
This is a bit more explicit ant less confusing than the method below.
INPUTS:
tempk: Temperature (K)
pres: static pressure (Pa)
mixr: mixing ratio (kg/kg)
OUTPUTS:
rho_air (kg/m^3)
SOURCE: http://en.wikipedia.org/wiki/Density_of_air
"""
pres_da=pres-e
rho_da=pres_da/(Rs_da*tempk)
rho_wv=e/(Rs_v*tempk)
return rho_da+rho_wv
def Density(tempk,pres,mixr):
"""Density of moist air
INPUTS:
tempk: Temperature (K)
pres: static pressure (Pa)
mixr: mixing ratio (kg/kg)
OUTPUTS:
rho_air (kg/m^3)
"""
virtualT=VirtualTempFromMixR(tempk,mixr)
return pres/(Rs_da*virtualT)
def VirtualTemp(tempk,pres,e):
"""Virtual Temperature
INPUTS:
tempk: Temperature (K)
e: vapour pressure (Pa)
p: static pressure (Pa)
OUTPUTS:
tempv: Virtual temperature (K)
SOURCE: hmmmm (Wikipedia)."""
tempvk=tempk/(1-(e/pres)*(1-Epsilon))
return tempvk
def VirtualTempFromMixR(tempk,mixr):
"""Virtual Temperature
INPUTS:
tempk: Temperature (K)
mixr: Mixing Ratio (kg/kg)
OUTPUTS:
tempv: Virtual temperature (K)
SOURCE: hmmmm (Wikipedia). This is an approximation
based on a m
"""
return tempk*(1.0+0.6*mixr)
def Latentc(tempc):
"""Latent heat of condensation (vapourisation)
INPUTS:
tempc (C)
OUTPUTS:
L_w (J/kg)
SOURCE:
http://en.wikipedia.org/wiki/Latent_heat#Latent_heat_for_condensation_of_water
"""
return 1000*(2500.8 - 2.36*tempc + 0.0016*tempc**2 - 0.00006*tempc**3)
def SatVap(tempc,phase="liquid"):
"""Calculate saturation vapour pressure over liquid water and/or ice.
INPUTS:
tempc: (C)
phase: ['liquid'],'ice'. If 'liquid', do simple dew point. If 'ice',
return saturation vapour pressure as follows:
Tc>=0: es = es_liquid
Tc <0: es = es_ice
RETURNS: e_sat (Pa)
SOURCE: http://cires.colorado.edu/~voemel/vp.html (#2:
CIMO guide (WMO 2008), modified to return values in Pa)
This formulation is chosen because of its appealing simplicity,
but it performs very well with respect to the reference forms
at temperatures above -40 C. At some point I'll implement Goff-Gratch
(from the same resource).
"""
over_liquid=6.112*exp(17.67*tempc/(tempc+243.12))*100.
over_ice=6.112*exp(22.46*tempc/(tempc+272.62))*100.
# return where(tempc<0,over_ice,over_liquid)
if phase=="liquid":
# return 6.112*exp(17.67*tempc/(tempc+243.12))*100.
return over_liquid
elif phase=="ice":
# return 6.112*exp(22.46*tempc/(tempc+272.62))*100.
return where(tempc<0,over_ice,over_liquid)
else:
raise NotImplementedError
def MixRatio(e,p):
"""Mixing ratio of water vapour
INPUTS
e (Pa) Water vapor pressure
p (Pa) Ambient pressure
RETURNS
qv (kg kg^-1) Water vapor mixing ratio`
"""
return Epsilon*e/(p-e)
def MixR2VaporPress(qv,p):
"""Return Vapor Pressure given Mixing Ratio and Pressure
INPUTS
qv (kg kg^-1) Water vapor mixing ratio`
p (Pa) Ambient pressure
RETURNS
e (Pa) Water vapor pressure
"""
return qv*p/(Epsilon+qv)
def VaporPressure(dwpt):
"""Water vapor pressure
INPUTS
dwpt (C) Dew Point Temperature (for SATURATION vapor
pressure use tempc)
RETURNS
e (Pa) Water Vapor Pressure
SOURCE:
Bolton, Monthly Weather Review, 1980, p 1047, eq. (10)
"""
return 611.2*exp(17.67*dwpt/(243.5+dwpt))
def DewPoint(e):
""" Use Bolton's (1980, MWR, p1047) formulae to find tdew.
INPUTS:
e (Pa) Water Vapor Pressure
OUTPUTS:
Td (C)
"""
ln_ratio=log(e/611.2)
Td=((17.67-ln_ratio)*degCtoK+243.5*ln_ratio)/(17.67-ln_ratio)
return Td-degCtoK