Skip to content

Commit c9c7eae

Browse files
committed
Add all scripts used for revised paper.
1 parent 17bca7b commit c9c7eae

8 files changed

+2137
-95
lines changed

jacobian_plotter.py

Lines changed: 88 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#num_files = 1
2424
#end_column = 4095
2525

26+
timescale_type = 'eigenvalue'
27+
#timescale_type = 'depletion_time'
28+
2629
splits = [(blocksize*i, blocksize*(i+1)-1) for i in range(num_files)]
2730
splits[-1] = (splits[-1][0], end_column)
2831

@@ -35,6 +38,10 @@
3538
efiles.append(nc4.Dataset(name, 'r'))
3639
cfile = nc4.Dataset(CLUSTER_FILE_NAME, 'r')
3740

41+
if timescale_type == 'depletion_time':
42+
HIST_FILE_NAME = "/home/santos/Data/MG2_data_collection.cam.h1.0001-01-06-00000.nc"
43+
hfile = nc4.Dataset(HIST_FILE_NAME, 'r')
44+
3845
nproc = len(efiles[0].dimensions['nproc'])
3946
ncluster = len(cfile.dimensions['ncluster'])
4047

@@ -60,6 +67,20 @@
6067
iqs = 8
6168
ins = 9
6269

70+
if timescale_type == 'depletion_time':
71+
# Just a dumb way to get a 10-item list so we can change it.
72+
hvars = list(range(10))
73+
hvars[it] = hfile.variables["MG2IN_T"]
74+
hvars[iq] = hfile.variables["MG2IN_Q"]
75+
hvars[iqc] = hfile.variables["MG2IN_QC"]
76+
hvars[inc] = hfile.variables["MG2IN_NC"]
77+
hvars[iqi] = hfile.variables["MG2IN_QI"]
78+
hvars[ini] = hfile.variables["MG2IN_NI"]
79+
hvars[iqr] = hfile.variables["MG2IN_QR"]
80+
hvars[inr] = hfile.variables["MG2IN_NR"]
81+
hvars[iqs] = hfile.variables["MG2IN_QS"]
82+
hvars[ins] = hfile.variables["MG2IN_NS"]
83+
6384
def calc_twmd(vec):
6485
av = np.abs(vec)
6586
return (av[iq] + av[iqc] + av[iqi] + av[iqr] + av[iqs]) * 0.5
@@ -111,10 +132,9 @@ def calc_twmd(vec):
111132
}
112133

113134
# Need a way to translate processes in a different order.
114-
process_name_data = nc4.chartostring(efiles[0]["process_names"][:])
115135
name_map = np.zeros((nproc,), dtype='u4')
116136
for i in range(nproc):
117-
name_string = process_name_data[i].decode("ascii")
137+
name_string = efiles[0]["process_names"][i]
118138
for j in range(nproc):
119139
if name_string == short_names[j]:
120140
name_map[j] = i
@@ -128,6 +148,7 @@ def calc_twmd(vec):
128148
q_cutoff = 1.e-10
129149
# equivalent to number of large rain particles made of q_cutoff mass.
130150
n_cutoff = q_cutoff / (np.pi/6. * 1000. * (400.e-6)**3)
151+
print("Number cutoff (1/kg/s): ", n_cutoff)
131152

132153
def process_is_relevant(tends):
133154
if calc_twmd(tends) >= q_cutoff:
@@ -156,27 +177,35 @@ def process_is_relevant(tends):
156177
column = efile["cell_coords"][ci,0]
157178
level = efile["cell_coords"][ci,1]
158179
tends = efile["process_rates"][ci,:,:]
159-
# Actually use the eigenvalues
160-
evals = efile["eigenvalues"][ci,:]["real"]
161-
# Process tendency association method
162-
#total_tends = efile["total_rates"][ci,:]
163-
#associations = np.zeros((10, nproc))
164-
#for j in range(nproc):
165-
# associations[:,j] = calc_twmd(tends[:,j]) / calc_twmd(total_tends)
166-
# Regular association method
167-
associations = efile["associations"][ci,:,:]
168180
c = label[0,level,column]
169181
cluster_cases[c] += 1
170-
for i in range(10):
171-
evalues_all[c].append(evals[i])
172-
maxproc = np.argmax(associations[i,:])
173-
if process_is_relevant(tends[:,maxproc]):
174-
evalues_rel[c].append(evals[i])
175-
for j in range(nproc):
176-
if (associations[i,name_map[j]] >= cutoff2 and process_is_relevant(tends[:,name_map[j]])):
177-
evalues2[c][short_names[j]].append(np.real(evals[i]))
178-
for j2 in range(nproc):
179-
evalue_correlation[short_names[j]][short_names[j2]] += associations[i,name_map[j2]]
182+
# Actually use the eigenvalues
183+
if timescale_type == 'eigenvalue':
184+
evals = efile["eigenvalues"][ci,:]["real"]
185+
associations = efile["associations"][ci,:,:]
186+
for i in range(10):
187+
evalues_all[c].append(evals[i])
188+
maxproc = np.argmax(associations[i,:])
189+
if process_is_relevant(tends[:,maxproc]):
190+
evalues_rel[c].append(evals[i])
191+
for j in range(nproc):
192+
if (associations[i,name_map[j]] >= cutoff2 and process_is_relevant(tends[:,name_map[j]])):
193+
evalues2[c][short_names[j]].append(np.real(evals[i]))
194+
for j2 in range(nproc):
195+
evalue_correlation[short_names[j]][short_names[j2]] += associations[i,name_map[j2]]
196+
elif timescale_type == 'depletion_time':
197+
for i in range(nproc):
198+
if not process_is_relevant(tends[:,name_map[i]]):
199+
continue
200+
timescale = 1.e100
201+
for j in range(10):
202+
if tends[j,name_map[i]] < 0.:
203+
this_timescale = hvars[j][0, level, column] / tends[j,name_map[i]]
204+
if np.abs(this_timescale) < np.abs(timescale):
205+
timescale = this_timescale
206+
evalues_all[c].append(1./timescale)
207+
evalues_rel[c].append(1./timescale)
208+
evalues2[c][short_names[i]].append(1./timescale)
180209

181210
#Convert the correlation sum to an average.
182211
evalue_corr_array = np.zeros((nproc,nproc))
@@ -191,6 +220,11 @@ def process_is_relevant(tends):
191220
if i == j:
192221
evalue_corr_array[i,j] -= 0.5
193222

223+
if timescale_type == 'eigenvalue':
224+
suffix = ''
225+
elif timescale_type == 'depletion_time':
226+
suffix = '_depletion'
227+
194228
# Number of bins for plotting purposes.
195229
nbins = 50
196230

@@ -218,30 +252,30 @@ def process_is_relevant(tends):
218252
plt.xlabel("Eigenvalue (1/s)")
219253
plt.xlim(1./max_t, 1./min_t)
220254
plt.ylabel("Number of eigenvalues")
221-
plt.savefig('./time_hist_all_values_pos.eps')
255+
plt.savefig('./time_hist_all_values_pos{}.eps'.format(suffix))
222256
plt.close()
223257

224258
# Histogram of all negative eigenvalues.
225-
bins = np.logspace(np.log10(1./max_t), np.log10(1./min_t), nbins+1)
259+
bins = -np.logspace(np.log10(1./min_t), np.log10(1./max_t), nbins+1)
226260
evalues_all_clusters = []
227261
evalues_all_rel = []
228262
evalues_all_assoc = []
229263
for c in range(ncluster):
230-
evalues_all_clusters += [-t for t in evalues_all[c] if -t > 1./max_t and -t < 1./min_t]
231-
evalues_all_rel += [-t for t in evalues_rel[c] if -t > 1./max_t and -t < 1./min_t]
264+
evalues_all_clusters += [t for t in evalues_all[c] if t > -1./min_t and t < -1./max_t]
265+
evalues_all_rel += [t for t in evalues_rel[c] if t > -1./min_t and t < -1./max_t]
232266
for name in short_names:
233-
evalues_all_assoc += [-t for t in evalues2[c][name] if -t > 1./max_t and -t < 1./min_t]
234-
plt.hist(evalues_all_clusters, bins=bins)
235-
plt.hist(evalues_all_rel, bins=bins)
236-
plt.hist(evalues_all_assoc, bins=bins)
237-
plt.axvline(x=1./300., color='k', linewidth=2.)
267+
evalues_all_assoc += [t for t in evalues2[c][name] if t > -1./min_t and t < -1./max_t]
268+
plt.hist(evalues_all_clusters, bins=bins, color='b', edgecolor='k')
269+
plt.hist(evalues_all_rel, bins=bins, color='g', edgecolor='k')
270+
plt.hist(evalues_all_assoc, bins=bins, color='r', edgecolor='k')
271+
plt.axvline(x=-1./300., color='k', linewidth=2.)
238272
plt.title("Number of negative eigenvalues \n(based on {} eigenvalues)"
239273
.format(len(evalues_all_clusters)))
240-
plt.gca().set_xscale("log")
274+
plt.gca().set_xscale("symlog", linthreshx=1./max_t)
241275
plt.xlabel("Eigenvalue (1/s)")
242-
plt.xlim(1./min_t, 1./max_t)
276+
plt.xlim(-1./min_t, -1./max_t)
243277
plt.ylabel("Number of eigenvalues")
244-
plt.savefig('./time_hist_all_values_neg.eps')
278+
plt.savefig('./time_hist_all_values_neg{}.eps'.format(suffix))
245279
plt.close()
246280

247281
# Histogram of all near-zero eigenvalues.
@@ -263,7 +297,7 @@ def process_is_relevant(tends):
263297
plt.xlim(-1./max_t, 1./max_t)
264298
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
265299
plt.ylabel("Number of eigenvalues")
266-
plt.savefig('./time_hist_all_values_n0.eps')
300+
plt.savefig('./time_hist_all_values_n0{}.eps'.format(suffix))
267301
plt.close()
268302

269303
# Now broken out by cluster.
@@ -297,27 +331,27 @@ def process_is_relevant(tends):
297331
ax2.set_yticks(cind)
298332
ax2.set_yticklabels([str(i) for i in eig_count],
299333
fontdict={'verticalalignment': 'bottom'})
300-
plt.savefig('./time_hist_cluster_2D_pos.eps')
334+
plt.savefig('./time_hist_cluster_2D_pos{}.eps'.format(suffix))
301335
plt.close()
302336

303337
# Histogram of all negative eigenvalues.
304-
bins = np.logspace(np.log10(1./max_t), np.log10(1./min_t), nbins+1)
338+
bins = -np.logspace(np.log10(1./min_t), np.log10(1./max_t), nbins+1)
305339
hist_values = np.zeros((ncluster, nbins))
306340
eig_count = []
307341
for c in range(ncluster):
308-
hist_values[c,:], _ = np.histogram([-t for t in evalues_all[c] if -t > 1./max_t and -t < 1./min_t], bins=bins)
342+
hist_values[c,:], _ = np.histogram([t for t in evalues_all[c] if -t > 1./max_t and -t < 1./min_t], bins=bins)
309343
row_norm = hist_values[c,:].sum()
310344
print("Cluster {} has {} negative eigenvalues.".format(c, row_norm))
311345
eig_count.append(int(row_norm))
312346
if row_norm != 0:
313347
hist_values[c,:] /= row_norm
314348
plt.pcolor(bins, cind, hist_values, edgecolors='k', cmap=cmap)
315-
plt.axvline(x=1./300., color='k', linewidth=2.)
349+
plt.axvline(x=-1./300., color='k', linewidth=2.)
316350
plt.title("PDF of negative eigenvalues by cluster \n(based on {} eigenvalues)"
317351
.format(len(evalues_all_clusters)))
318-
plt.gca().set_xscale("log")
352+
plt.gca().set_xscale("symlog", linthreshx=1./max_t)
319353
plt.xlabel("Eigenvalue (1/s)")
320-
plt.xlim(1./min_t, 1./max_t)
354+
plt.xlim(-1./min_t, -1./max_t)
321355
plt.ylabel("Cluster Index")
322356
ax = plt.gca()
323357
ax.set_yticks(cind)
@@ -330,7 +364,7 @@ def process_is_relevant(tends):
330364
ax2.set_yticks(cind)
331365
ax2.set_yticklabels([str(i) for i in eig_count],
332366
fontdict={'verticalalignment': 'bottom'})
333-
plt.savefig('./time_hist_cluster_2D_neg.eps')
367+
plt.savefig('./time_hist_cluster_2D_neg{}.eps'.format(suffix))
334368
plt.close()
335369

336370
# Histogram of all near-zero eigenvalues.
@@ -362,7 +396,7 @@ def process_is_relevant(tends):
362396
ax2.set_yticks(cind)
363397
ax2.set_yticklabels([str(i) for i in eig_count],
364398
fontdict={'verticalalignment': 'bottom'})
365-
plt.savefig('./time_hist_cluster_2D_n0.eps')
399+
plt.savefig('./time_hist_cluster_2D_n0{}.eps'.format(suffix))
366400
plt.close()
367401

368402
# Now broken up by process.
@@ -399,29 +433,29 @@ def process_is_relevant(tends):
399433
ax2.set_yticks(pind)
400434
ax2.set_yticklabels([str(i) for i in eig_count],
401435
fontdict={'verticalalignment': 'top'})
402-
plt.savefig('./time_hist_process_2D_pos.eps')
436+
plt.savefig('./time_hist_process_2D_pos{}.eps'.format(suffix))
403437
plt.close()
404438

405439
# Histogram of all negative eigenvalues.
406-
bins = np.logspace(np.log10(1./max_t), np.log10(1./min_t), nbins+1)
440+
bins = -np.logspace(np.log10(1./min_t), np.log10(1./max_t), nbins+1)
407441
hist_values = np.zeros((nproc, nbins))
408442
eig_count = []
409443
for i in range(nproc):
410444
neg_evalues = []
411445
for c in range(ncluster):
412-
neg_evalues += [-t for t in evalues2[c][short_names[i]] if -t > 1./max_t and -t < 1./min_t]
446+
neg_evalues += [t for t in evalues2[c][short_names[i]] if -t > 1./max_t and -t < 1./min_t]
413447
hist_values[i,:], _ = np.histogram(neg_evalues, bins=bins)
414448
row_norm = hist_values[i,:].sum()
415449
eig_count.append(int(row_norm))
416450
print("Process {} has {} negative eigenvalues.".format(process_names[short_names[i]], row_norm))
417451
if row_norm != 0:
418452
hist_values[i,:] /= row_norm
419453
plt.pcolor(bins, pind, hist_values, edgecolors='k', cmap=cmap)
420-
plt.axvline(x=1./300., color='k', linewidth=2.)
454+
plt.axvline(x=-1./300., color='k', linewidth=2.)
421455
plt.title("PDF of negative eigenvalues by process")
422-
plt.gca().set_xscale("log")
456+
plt.gca().set_xscale("symlog", linthreshx=1./max_t)
423457
plt.xlabel("Eigenvalue (1/s)")
424-
plt.xlim(1./min_t, 1./max_t)
458+
plt.xlim(-1./min_t, -1./max_t)
425459
ax = plt.gca()
426460
ax.set_yticks(pind)
427461
ax.set_yticklabels((process_names[name] for name in short_names),
@@ -436,7 +470,7 @@ def process_is_relevant(tends):
436470
ax2.set_yticks(pind)
437471
ax2.set_yticklabels([str(i) for i in eig_count],
438472
fontdict={'verticalalignment': 'top'})
439-
plt.savefig('./time_hist_process_2D_neg.eps')
473+
plt.savefig('./time_hist_process_2D_neg{}.eps'.format(suffix))
440474
plt.close()
441475

442476
# Histogram of all near-zero eigenvalues.
@@ -472,7 +506,7 @@ def process_is_relevant(tends):
472506
ax2.set_yticks(pind)
473507
ax2.set_yticklabels([str(i) for i in eig_count],
474508
fontdict={'verticalalignment': 'top'})
475-
plt.savefig('./time_hist_process_2D_n0.eps')
509+
plt.savefig('./time_hist_process_2D_n0{}.eps'.format(suffix))
476510
plt.close()
477511

478512
# Correlation array.
@@ -496,7 +530,7 @@ def process_is_relevant(tends):
496530
plt.subplots_adjust(left=0.25)
497531
plt.ylabel("Primary process")
498532
plt.colorbar()
499-
plt.savefig('./process_association.eps')
533+
plt.savefig('./process_association{}.eps'.format(suffix))
500534
plt.close()
501535

502536

@@ -530,7 +564,7 @@ def process_is_relevant(tends):
530564
ax.tick_params('y', direction='out')
531565
plt.subplots_adjust(left=0.25)
532566
plt.colorbar()
533-
plt.savefig('./time_hist_2D_pos_c{}.eps'.format(c))
567+
plt.savefig('./time_hist_2D_pos_c{}{}.eps'.format(c, suffix))
534568
plt.close()
535569

536570
neg_evalues2 = {}
@@ -567,7 +601,7 @@ def process_is_relevant(tends):
567601
plt.ylabel("Process")
568602
plt.clim(vmin=0., vmax=0.2)
569603
plt.colorbar()
570-
plt.savefig('./time_hist_2D_neg_c{}.eps'.format(c))
604+
plt.savefig('./time_hist_2D_neg_c{}{}.eps'.format(c, suffix))
571605
plt.close()
572606

573607
n0_evalues2 = {}
@@ -603,5 +637,5 @@ def process_is_relevant(tends):
603637
plt.ylabel("Process")
604638
plt.clim(vmin=0., vmax=0.2)
605639
plt.colorbar()
606-
plt.savefig('./time_hist_2D_n0_c{}.eps'.format(c))
640+
plt.savefig('./time_hist_2D_n0_c{}{}.eps'.format(c, suffix))
607641
plt.close()

0 commit comments

Comments
 (0)