forked from pressel/pycles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadiation.pyx
173 lines (144 loc) · 6.95 KB
/
Radiation.pyx
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
#!python
#cython: boundscheck=False
#cython: wraparound=False
#cython: initializedcheck=False
#cython: cdivision=True
cimport Grid
cimport ReferenceState
cimport PrognosticVariables
cimport DiagnosticVariables
from NetCDFIO cimport NetCDFIO_Stats
cimport ParallelMPI
import numpy as np
cimport numpy as np
from libc.math cimport pow, cbrt, exp
import cython
from thermodynamic_functions cimport cpm_c
include 'parameters.pxi'
cdef class Radiation:
def __init__(self, namelist, ParallelMPI.ParallelMPI Pa):
casename = namelist['meta']['casename']
if casename == 'DYCOMS_RF01':
self.scheme = RadiationDyCOMS_RF01()
else:
self.scheme = RadiationNone()
return
cpdef initialize(self, Grid.Grid Gr, NetCDFIO_Stats NS, ParallelMPI.ParallelMPI Pa):
self.scheme.initialize(Gr, NS, Pa)
return
cpdef update(self, Grid.Grid Gr, ReferenceState.ReferenceState Ref,
PrognosticVariables.PrognosticVariables PV, DiagnosticVariables.DiagnosticVariables DV,
ParallelMPI.ParallelMPI Pa):
self.scheme.update(Gr, Ref, PV, DV, Pa)
return
cpdef stats_io(self, Grid.Grid Gr, ReferenceState.ReferenceState Ref,
PrognosticVariables.PrognosticVariables PV, DiagnosticVariables.DiagnosticVariables DV,
NetCDFIO_Stats NS, ParallelMPI.ParallelMPI Pa):
self.scheme.stats_io(Gr, PV, DV, NS, Pa)
return
cdef class RadiationNone:
def __init__(self):
return
cpdef initialize(self, Grid.Grid Gr, NetCDFIO_Stats NS, ParallelMPI.ParallelMPI Pa):
return
cpdef update(self, Grid.Grid Gr, ReferenceState.ReferenceState Ref,
PrognosticVariables.PrognosticVariables PV, DiagnosticVariables.DiagnosticVariables DV,
ParallelMPI.ParallelMPI Pa):
return
cpdef stats_io(self, Grid.Grid Gr, ReferenceState.ReferenceState Ref,
PrognosticVariables.PrognosticVariables PV, DiagnosticVariables.DiagnosticVariables DV,
NetCDFIO_Stats NS, ParallelMPI.ParallelMPI Pa):
return
cdef class RadiationDyCOMS_RF01:
def __init__(self):
self.alpha_z = 1.0
self.kap = 85.0
self.f0 = 70.0
self.f1 = 22.0
self.divergence = 3.75e-6
self.z_pencil = ParallelMPI.Pencil()
return
cpdef initialize(self, Grid.Grid Gr, NetCDFIO_Stats NS, ParallelMPI.ParallelMPI Pa):
self.z_pencil.initialize(Gr, Pa, 2)
return
cpdef update(self, Grid.Grid Gr, ReferenceState.ReferenceState Ref,
PrognosticVariables.PrognosticVariables PV, DiagnosticVariables.DiagnosticVariables DV,
ParallelMPI.ParallelMPI Pa):
cdef:
Py_ssize_t imin = Gr.dims.gw
Py_ssize_t jmin = Gr.dims.gw
Py_ssize_t kmin = Gr.dims.gw
Py_ssize_t imax = Gr.dims.nlg[0] - Gr.dims.gw
Py_ssize_t jmax = Gr.dims.nlg[1] - Gr.dims.gw
Py_ssize_t kmax = Gr.dims.nlg[2] - Gr.dims.gw
Py_ssize_t pi, i, j, k, ijk, ishift, jshift
Py_ssize_t istride = Gr.dims.nlg[1] * Gr.dims.nlg[2]
Py_ssize_t jstride = Gr.dims.nlg[2]
Py_ssize_t ql_shift = DV.get_varshift(Gr, 'ql')
Py_ssize_t qt_shift = PV.get_varshift(Gr, 'qt')
Py_ssize_t s_shift = PV.get_varshift(Gr, 's')
Py_ssize_t t_shift = DV.get_varshift(Gr, 'temperature')
Py_ssize_t gw = Gr.dims.gw
double [:, :] ql_pencils = self.z_pencil.forward_double(& Gr.dims, Pa, & DV.values[ql_shift])
double [:, :] qt_pencils = self.z_pencil.forward_double(& Gr.dims, Pa, & PV.values[qt_shift])
double[:, :] f_rad = np.empty((self.z_pencil.n_local_pencils, Gr.dims.n[2] + 1), dtype=np.double, order='c')
double[:, :] f_heat = np.empty((self.z_pencil.n_local_pencils, Gr.dims.n[2]), dtype=np.double, order='c')
double[:] heating_rate = np.zeros((Gr.dims.npg,), dtype=np.double, order='c')
double q_0
double q_1
double zi
double rhoi
double dz = Gr.dims.dx[2]
double dzi = Gr.dims.dxi[2]
double[:] z = Gr.z
double[:] rho = Ref.rho0
double[:] rho_half = Ref.rho0_half
with nogil:
for pi in xrange(self.z_pencil.n_local_pencils):
# Compute zi (level of 8.0 g/kg isoline of qt)
for k in xrange(Gr.dims.n[2]):
if qt_pencils[pi, k] > 8e-3:
zi = z[gw + k]
rhoi = rho_half[gw + k]
# Now compute the third term on RHS of Stevens et al 2005
# (equation 3)
f_rad[pi, 0] = rhoi * cpd * self.divergence * self.alpha_z * (cbrt(pow(z[gw - 1] - zi, 4)) / 4.0
+ zi * cbrt(z[gw - 1] - zi))
for k in xrange(Gr.dims.n[2]):
f_rad[pi, k + 1] = rhoi * cpd * self.divergence * self.alpha_z * (cbrt(pow(z[gw + k] - zi, 4)) / 4.0
+ zi * cbrt(z[gw + k] - zi))
# Compute the second term on RHS of Stevens et al. 2005
# (equation 3)
q_1 = 0.0
f_rad[pi, 0] += self.f1 * exp(-q_1)
for k in xrange(1, Gr.dims.n[2] + 1):
q_1 += self.kap * \
rho_half[gw + k - 1] * ql_pencils[pi, k - 1] * dz
f_rad[pi, k] += self.f1 * exp(-q_1)
# Compute the first term on RHS of Stevens et al. 2005
# (equation 3)
q_0 = 0.0
f_rad[pi, Gr.dims.n[2]] += self.f0 * exp(-q_0)
for k in xrange(Gr.dims.n[2] - 1, -1, -1):
q_0 += self.kap * rho_half[gw + k] * ql_pencils[pi, k] * dz
f_rad[pi, k] += self.f0 * exp(-q_0)
for k in xrange(Gr.dims.n[2]):
f_heat[pi, k] = - \
(f_rad[pi, k + 1] - f_rad[pi, k]) * dzi / rho_half[k]
# Now transpose the flux pencils
self.z_pencil.reverse_double(& Gr.dims, Pa, f_heat, & heating_rate[0])
# Now update entropy tendencies
with nogil:
for i in xrange(imin, imax):
ishift = i * istride
for j in xrange(jmin, jmax):
jshift = j * jstride
for k in xrange(kmin, kmax):
ijk = ishift + jshift + k
PV.tendencies[
s_shift + ijk] += heating_rate[ijk] / DV.values[ijk + t_shift]
return
cpdef stats_io(self, Grid.Grid Gr, ReferenceState.ReferenceState Ref,
PrognosticVariables.PrognosticVariables PV, DiagnosticVariables.DiagnosticVariables DV,
NetCDFIO_Stats NS, ParallelMPI.ParallelMPI Pa):
return