Skip to content

Commit c834cbd

Browse files
committed
Simplifies detect_plates_vzcheck
This commit mainly reduces explicit looping in this routine.
1 parent ad8e30d commit c834cbd

File tree

1 file changed

+44
-63
lines changed

1 file changed

+44
-63
lines changed

stagpy/plates.py

Lines changed: 44 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -11,78 +11,61 @@
1111
from .stagyydata import StagyyData
1212

1313

14-
def detect_plates_vzcheck(step, seuil_memz):
14+
def detect_plates_vzcheck(step, vz_thres_ratio=0):
1515
"""Detect plates and check with vz and plate size."""
16-
v_z = step.fields['v3'][0, :, :, 0]
16+
v_z = step.fields['v3'][0, :-1, :, 0]
1717
v_x = step.fields['v2'][0, :, :, 0]
1818
tcell = step.fields['T'][0, :, :, 0]
1919
n_z = step.geom.nztot
2020
nphi = step.geom.nptot
21-
rcmb = step.geom.rcmb
22-
radius = step.geom.r_centers
23-
radiusgrid = step.geom.r_walls
21+
r_c = step.geom.r_centers
22+
r_w = step.geom.r_walls
2423
dphi = 1 / nphi
2524

26-
# calculing temperature on the grid and vz_mean
27-
vz_mean = 0
28-
tgrid = np.zeros((nphi, n_z + 1))
29-
tgrid[:, 0] = 1
30-
for i_z in range(1, n_z):
31-
for phi in range(nphi):
32-
tgrid[phi, i_z] = (
33-
tcell[phi, i_z - 1] *
34-
(radiusgrid[i_z] - radius[i_z - 1]) + tcell[phi, i_z] *
35-
(-radiusgrid[i_z] + radius[i_z])) / (radius[i_z] -
36-
radius[i_z - 1])
37-
vz_mean += abs(v_z[phi, i_z]) / (nphi * n_z)
38-
3925
flux_c = n_z * [0]
40-
for i_z in range(1, n_z - 1):
41-
for phi in range(nphi):
42-
flux_c[i_z] += (tgrid[phi, i_z] - step.timeinfo.loc['Tmean']) * \
43-
v_z[phi, i_z] * radiusgrid[i_z] * dphi
26+
for i_z in range(0, n_z):
27+
flux_c[i_z] = np.sum((tcell[:, i_z] - step.timeinfo.loc['Tmean']) *
28+
v_z[:, i_z]) * r_w[i_z] * dphi
4429

45-
# checking stagnant lid
30+
# checking stagnant lid, criterion seems weird!
4631
if all(abs(flux_c[i_z]) <= np.max(flux_c) / 50
4732
for i_z in range(n_z - n_z // 20, n_z)):
4833
raise error.StagnantLidError(step.sdat)
49-
else:
50-
# verifying horizontal plate speed and closeness of plates
51-
dvphi = nphi * [0]
52-
dvx_thres = 16 * step.timeinfo.loc['vrms']
53-
54-
for phi in range(0, nphi):
55-
dvphi[phi] = (v_x[phi, n_z - 1] -
56-
v_x[phi - 1, n_z - 1]) / ((1 + rcmb) * dphi)
57-
limits = []
58-
for phi in range(0, nphi - nphi // 33):
59-
mark = all(abs(dvphi[i]) <= abs(dvphi[phi])
60-
for i in range(phi - nphi // 33, phi + nphi // 33))
61-
if mark and abs(dvphi[phi]) >= dvx_thres:
62-
limits.append(phi)
63-
for phi in range(nphi - nphi // 33 + 1, nphi):
64-
mark = all(abs(dvphi[i]) <= abs(dvphi[phi])
65-
for i in range(phi - nphi // 33 - nphi,
66-
phi + nphi // 33 - nphi))
67-
if mark and abs(dvphi[phi]) >= dvx_thres:
68-
limits.append(phi)
69-
70-
# verifying vertical speed
71-
k = 0
72-
for i in range(len(limits)):
73-
vzm = 0
74-
phi = limits[i - k]
75-
for i_z in range(1 if phi == nphi - 1 else 0, n_z):
76-
vzm += (abs(v_z[phi, i_z]) +
77-
abs(v_z[phi - 1, i_z]) +
78-
abs(v_z[(phi + 1) % nphi, i_z])) / (n_z * 3)
79-
80-
vz_thres = vz_mean * 0.1 + seuil_memz / 2 if seuil_memz else 0
81-
if vzm < vz_thres:
82-
limits.remove(phi)
83-
k += 1
84-
85-
return limits, nphi, dvphi, vz_thres, v_x[:, n_z - 1]
34+
35+
# verifying horizontal plate speed and closeness of plates
36+
vphi_surf = v_x[:, -1]
37+
dvphi = np.diff(vphi_surf)
38+
dvphi = np.append(dvphi, vphi_surf[0] - vphi_surf[-1])
39+
dvphi /= (r_c[-1] * dphi)
40+
dvx_thres = 16 * step.timeinfo.loc['vrms']
41+
42+
limits = []
43+
for phi in range(0, nphi):
44+
mark = all(abs(dvphi[i % nphi]) <= abs(dvphi[phi])
45+
for i in range(phi - nphi // 33, phi + nphi // 33))
46+
if mark and abs(dvphi[phi]) >= dvx_thres:
47+
limits.append(phi)
48+
49+
# verifying vertical velocity
50+
vz_thres = 0
51+
if vz_thres_ratio > 0:
52+
vz_mean = (np.sum(step.rprofs['vzabs'].values * np.diff(r_w))
53+
/ (r_w[-1] - r_w[0]))
54+
vz_thres = vz_mean * vz_thres_ratio
55+
k = 0
56+
for i in range(len(limits)):
57+
vzm = 0
58+
phi = limits[i - k]
59+
for i_z in range(1 if phi == nphi - 1 else 0, n_z):
60+
vzm += (abs(v_z[phi, i_z]) +
61+
abs(v_z[phi - 1, i_z]) +
62+
abs(v_z[(phi + 1) % nphi, i_z])) / (n_z * 3)
63+
64+
if vzm < vz_thres:
65+
limits.remove(phi)
66+
k += 1
67+
68+
return limits, nphi, dvphi, vphi_surf
8669

8770

8871
def detect_plates(step, vrms_surface, fids, time):
@@ -777,7 +760,6 @@ def cmd():
777760
conf.scaling.factors['Pa'] = 'M'
778761
main_plates(sdat)
779762
else:
780-
seuil_memz = 0
781763
nb_plates = []
782764
time = []
783765
ch2o = []
@@ -790,8 +772,7 @@ def cmd():
790772
ch2o.append(step.timeinfo.loc[0])
791773
istart = step.isnap if istart is None else istart
792774
iend = step.isnap
793-
limits, nphi, dvphi, seuil_memz, vphi_surf =\
794-
detect_plates_vzcheck(step, seuil_memz)
775+
limits, nphi, dvphi, vphi_surf = detect_plates_vzcheck(step)
795776
water_profile = np.mean(step.fields['wtr'][0, :, :, 0], axis=0)
796777
limits.sort()
797778
sizeplates = [limits[0] + nphi - limits[-1]]

0 commit comments

Comments
 (0)