11#!/usr/bin/env python
22
3+ import sys
4+
35import numpy as np
46import scipy .linalg as la
57import scipy .stats as stats
1416
1517from mg2_constants import *
1618
17- HIST_FILE_NAME = "/g/g14/santos36 /Data/MG2_data_collection.cam.h1.0001-01-06-00000.nc"
18- CLUSTER_FILE_NAME = "/g/g14/santos36 /Data/MG2_data_collection.10_cluster_labels.0001-01-06-00000.nc"
19+ HIST_FILE_NAME = "/home/santos /Data/MG2_data_collection.cam.h1.0001-01-06-00000.nc"
20+ CLUSTER_FILE_NAME = "/home/santos /Data/MG2_data_collection.10_cluster_labels.0001-01-06-00000.nc"
1921
2022hfile = nc4 .Dataset (HIST_FILE_NAME , 'r' )
2123cfile = nc4 .Dataset (CLUSTER_FILE_NAME , 'r' )
101103frzdep_loc = np .empty ((1 , frzdep .shape [1 ]), order = 'F' )
102104
103105timestep = 1.e-8
106+ evap_col_steps = 1
107+ evap_steps = 1
108+ col_steps = 1
109+ auto_accr_steps = 1
110+ auto_steps = 1
111+ accr_steps = 1
104112
105113# Indices for each variable in the state array.
106114it = 0
@@ -187,6 +195,7 @@ def s_inv_mult(state):
187195 "iaccr" ,
188196 "rnagg" ,
189197 "snagg" ,
198+ "cnact" ,
190199 "anadj" ,
191200]
192201process_names = {
@@ -229,9 +238,10 @@ def mg2_tendencies(level, t, q, qc, nc, qi, ni, qr, nr, qs, ns):
229238 nrout2 , nsout2 , drout2 , dsout2 , freqs , freqr , nfice , qcrat , \
230239 nadjtot , ncmeitot , mnudeptot , nnudeptot , npsacwstot , nnuccctot , nnuccttot , \
231240 nsacwitot , nnuccrtot , mnuccritot , nnuccritot , npracstot , npratot , nprctot , \
232- nprc1tot , npraitot , nprcitot , nsubrtot , nraggtot , nsaggtot , errstring , \
233- prer_evap \
234- = mg .micro_mg_tend (timestep , t [0 ,level ], q [0 ,level ], qc [0 ,level ], qi [0 ,level ], nc [0 ,level ],
241+ nprc1tot , npraitot , nprcitot , nsubrtot , nraggtot , nsaggtot , subqr , subnr , \
242+ subdr , errstring , prer_evap \
243+ = mg .micro_mg_tend (evap_col_steps , evap_steps , col_steps , auto_accr_steps , auto_steps , accr_steps , timestep , t [0 ,level ],
244+ q [0 ,level ], qc [0 ,level ], qi [0 ,level ], nc [0 ,level ],
235245 ni [0 ,level ], qr [0 ,level ], qs [0 ,level ], nr [0 ,level ], ns [0 ,level ],
236246 relvar_loc [0 ,level ], accre_enhan_loc [0 ,level ], p_loc [0 ,level ], pdel_loc [0 ,level ],
237247 precipf_loc [0 ,level ], liqcldf_loc [0 ,level ], icecldf_loc [0 ,level ], naai_loc [0 ,level ],
@@ -386,37 +396,14 @@ def run_mg2(state, level):
386396 my_ns [0 ,level ] = max (state [ins ], 1.e-12 )
387397 tends = mg2_tendencies (level , my_t , my_q , my_qc , my_nc , my_qi , my_ni , my_qr ,
388398 my_nr , my_qs , my_ns )
389- return s_inv_mult (tends ["Total" ])
399+ output = np .zeros ((10 * (len (ind )+ 1 ),))
400+ output [0 :10 ] = s_inv_mult (tends ["Total" ])
401+ for i in range (len (ind )):
402+ output [10 * (i + 1 ):10 * (i + 2 )] = s_inv_mult (tends [short_names [i ]])
403+ return output
390404
391405ind = np .arange (len (short_names ))
392406
393- column = 856
394-
395- t_loc [:,:] = t [0 ,:,column ]
396- q_loc [:,:] = q [0 ,:,column ]
397- qc_loc [:,:] = qc [0 ,:,column ]
398- qi_loc [:,:] = qi [0 ,:,column ]
399- nc_loc [:,:] = nc [0 ,:,column ]
400- ni_loc [:,:] = ni [0 ,:,column ]
401- qr_loc [:,:] = qr [0 ,:,column ]
402- qs_loc [:,:] = qs [0 ,:,column ]
403- nr_loc [:,:] = nr [0 ,:,column ]
404- ns_loc [:,:] = ns [0 ,:,column ]
405- relvar_loc [:,:] = relvar [0 ,:,column ]
406- accre_enhan_loc [:,:] = accre_enhan [0 ,:,column ]
407- p_loc [:,:] = p [0 ,:,column ]
408- pdel_loc [:,:] = pdel [0 ,:,column ]
409- precipf_loc [:,:] = precipf [0 ,:,column ]
410- liqcldf_loc [:,:] = liqcldf [0 ,:,column ]
411- icecldf_loc [:,:] = icecldf [0 ,:,column ]
412- naai_loc [:,:] = naai [0 ,:,column ]
413- npccn_loc [:,:] = npccn [0 ,:,column ]
414- rndst_loc [:,:,:] = rndst [0 ,:,column :column + 1 ,:].transpose ([1 , 0 , 2 ])
415- nacon_loc [:,:,:] = nacon [0 ,:,column :column + 1 ,:].transpose ([1 , 0 , 2 ])
416- frzimm_loc [:,:] = frzimm [0 ,:,column ]
417- frzcnt_loc [:,:] = frzcnt [0 ,:,column ]
418- frzdep_loc [:,:] = frzdep [0 ,:,column ]
419-
420407def get_modes_at_level (level ):
421408 state = np .zeros ((10 ,))
422409 state [it ] = t_loc [0 ,level ]
@@ -433,15 +420,24 @@ def get_modes_at_level(level):
433420 qr_loc , nr_loc , qs_loc , ns_loc )
434421
435422 j_mg2 = ndt .Jacobian (run_mg2 , method = 'forward' , order = 2 )
436- j_loc = s . dot ( sq . dot ( la . solve_triangular ( sr , j_mg2 (s_inv_mult (state ),
437- level ) .T , trans = 'T' )).T )
423+ j_locs_dimless = j_mg2 (s_inv_mult (state ), level )
424+ j_loc = s . dot ( sq . dot ( la . solve_triangular ( sr , j_locs_dimless [ 0 : 10 ,:] .T , trans = 'T' )).T )
438425 evals , evecs = la .eig (j_loc )
439426 # Sometimes la.eig spits out complex eigenvalues unnecessarily.
440427 if all (np .imag (evals ) == 0. ):
441428 evals = np .real (evals )
442429 if (np .imag (evecs ) == 0. ).all ():
443430 evecs = np .real (evecs )
444- return tends , evals , evecs
431+ associations = np .zeros ((10 , len (ind )))
432+ for i in range (len (ind )):
433+ j_proc_loc = s .dot (sq .dot (la .solve_triangular (sr , j_locs_dimless [10 * (i + 1 ):10 * (i + 2 ),:].T , trans = 'T' )).T )
434+ j_proc_j_basis = la .solve (evecs , j_proc_loc .dot (evecs ))
435+ associations [:,i ] = np .abs (np .diag (j_proc_j_basis ))
436+ for i in range (10 ):
437+ assoc_norm = la .norm (associations [i ,:], 1 )
438+ if assoc_norm != 0. :
439+ associations [i ,:] /= assoc_norm
440+ return tends , evals , evecs , associations
445441
446442def calculate_indices (tends , evals , evecs ):
447443 strengths = la .solve (evecs , tends ["Total" ])
@@ -490,7 +486,7 @@ def evec_bar_graph(evals, strengths, participations):
490486cutoff2 = 0.5
491487cutoff3 = 0.9
492488tend_cutoff = 1.e-10
493- num_columns = 2048
489+ num_columns = 128
494490
495491plt .autoscale (tight = True )
496492
@@ -514,7 +510,8 @@ def evec_bar_graph(evals, strengths, participations):
514510singular_counter = 0
515511
516512for column in range (num_columns ):
517- print ("On column: " , column )
513+ # if column % 16 == 0:
514+ print ("On column: " , column , file = sys .stderr )
518515 t_loc [:,:] = t [0 ,:,column ]
519516 q_loc [:,:] = q [0 ,:,column ]
520517 qc_loc [:,:] = qc [0 ,:,column ]
@@ -544,7 +541,7 @@ def evec_bar_graph(evals, strengths, participations):
544541 icecldf_loc , mgncol = 1 , nlev = lev )
545542 for level in range (lev ):
546543 c = label [0 ,level ,column ]
547- tends , evals , evecs = get_modes_at_level (level )
544+ tends , evals , evecs , associations = get_modes_at_level (level )
548545 tot_t = tends ["Total" ]
549546 tend_norm = (abs (tot_t [iq ]) + abs (tot_t [iqc ]) + abs (tot_t [iqi ]) +
550547 abs (tot_t [iqr ]) + abs (tot_t [iqs ])) / 2.
@@ -561,14 +558,14 @@ def evec_bar_graph(evals, strengths, participations):
561558 for i in range (len (evals )):
562559 evalues_all [c ].append (np .real (evals [i ]))
563560 for j in range (len (short_names )):
564- if np .abs (participations [i ,j ]) >= cutoff1 :
561+ if np .abs (associations [i ,j ]) >= cutoff1 :
565562 evalues1 [c ][short_names [j ]].append (np .real (evals [i ]))
566- if np .abs (participations [i ,j ]) >= cutoff2 :
563+ if np .abs (associations [i ,j ]) >= cutoff2 :
567564 if np .real (evals [i ]) < - 0.1 :
568565 evalues2 [c ][short_names [j ]].append (np .real (evals [i ]))
569566 for j2 in range (len (short_names )):
570- evalue_correlation [short_names [j ]][short_names [j2 ]] += np .abs (participations [i ,j2 ])
571- if np .abs (participations [i ,j ]) >= cutoff3 :
567+ evalue_correlation [short_names [j ]][short_names [j2 ]] += np .abs (associations [i ,j2 ])
568+ if np .abs (associations [i ,j ]) >= cutoff3 :
572569 evalues3 [c ][short_names [j ]].append (np .real (evals [i ]))
573570
574571#Convert the correlation sum to an average.
@@ -610,7 +607,7 @@ def evec_bar_graph(evals, strengths, participations):
610607plt .xlabel ("Eigenvalue (1/s)" )
611608plt .xlim (1. / max_t , 1. / min_t )
612609plt .ylabel ("Number of modes" )
613- plt .savefig ('./time_hist_all_values_pos .eps' )
610+ plt .savefig ('./time_hist_all_values_pos_new .eps' )
614611plt .close ()
615612
616613# Histogram of all negative eigenvalues.
@@ -631,7 +628,7 @@ def evec_bar_graph(evals, strengths, participations):
631628plt .xlabel ("Eigenvalue (1/s)" )
632629plt .xlim (1. / min_t , 1. / max_t )
633630plt .ylabel ("Number of modes" )
634- plt .savefig ('./time_hist_all_values_neg .eps' )
631+ plt .savefig ('./time_hist_all_values_neg_new .eps' )
635632plt .close ()
636633
637634# Histogram of all near-zero eigenvalues.
@@ -652,7 +649,7 @@ def evec_bar_graph(evals, strengths, participations):
652649plt .xlim (- 1. / max_t , 1. / max_t )
653650plt .ticklabel_format (style = 'sci' , axis = 'x' , scilimits = (0 ,0 ))
654651plt .ylabel ("Number of modes" )
655- plt .savefig ('./time_hist_all_values_n0 .eps' )
652+ plt .savefig ('./time_hist_all_values_n0_new .eps' )
656653plt .close ()
657654
658655# Now broken out by cluster.
@@ -676,7 +673,7 @@ def evec_bar_graph(evals, strengths, participations):
676673plt .ylabel ("Cluster Index" )
677674plt .clim (vmin = 0. , vmax = 0.2 )
678675plt .colorbar ()
679- plt .savefig ('./time_hist_cluster_2D_pos .eps' )
676+ plt .savefig ('./time_hist_cluster_2D_pos_new .eps' )
680677plt .close ()
681678
682679# Histogram of all negative eigenvalues.
@@ -697,7 +694,7 @@ def evec_bar_graph(evals, strengths, participations):
697694plt .ylabel ("Cluster Index" )
698695plt .clim (vmin = 0. , vmax = 0.2 )
699696plt .colorbar ()
700- plt .savefig ('./time_hist_cluster_2D_neg .eps' )
697+ plt .savefig ('./time_hist_cluster_2D_neg_new .eps' )
701698plt .close ()
702699
703700# Histogram of all near-zero eigenvalues.
@@ -718,7 +715,7 @@ def evec_bar_graph(evals, strengths, participations):
718715plt .ylabel ("Cluster Index" )
719716plt .clim (vmin = 0. , vmax = 0.2 )
720717plt .colorbar ()
721- plt .savefig ('./time_hist_cluster_2D_n0 .eps' )
718+ plt .savefig ('./time_hist_cluster_2D_n0_new .eps' )
722719plt .close ()
723720
724721# Now broken up by process.
@@ -748,7 +745,7 @@ def evec_bar_graph(evals, strengths, participations):
748745plt .ylabel ("Process" )
749746plt .clim (vmin = 0. , vmax = 0.2 )
750747plt .colorbar ()
751- plt .savefig ('./time_hist_process_2D_pos .eps' )
748+ plt .savefig ('./time_hist_process_2D_pos_new .eps' )
752749plt .close ()
753750
754751# Histogram of all negative eigenvalues.
@@ -777,7 +774,7 @@ def evec_bar_graph(evals, strengths, participations):
777774plt .ylabel ("Process" )
778775plt .clim (vmin = 0. , vmax = 0.2 )
779776plt .colorbar ()
780- plt .savefig ('./time_hist_process_2D_neg .eps' )
777+ plt .savefig ('./time_hist_process_2D_neg_new .eps' )
781778plt .close ()
782779
783780# Histogram of all near-zero eigenvalues.
@@ -806,7 +803,7 @@ def evec_bar_graph(evals, strengths, participations):
806803plt .ylabel ("Process" )
807804plt .clim (vmin = 0. , vmax = 0.2 )
808805plt .colorbar ()
809- plt .savefig ('./time_hist_process_2D_n0 .eps' )
806+ plt .savefig ('./time_hist_process_2D_n0_new .eps' )
810807plt .close ()
811808
812809# Correlation array.
@@ -829,7 +826,7 @@ def evec_bar_graph(evals, strengths, participations):
829826plt .subplots_adjust (left = 0.25 )
830827plt .ylabel ("Primary process" )
831828plt .colorbar ()
832- plt .savefig ('./process_association .eps' )
829+ plt .savefig ('./process_association_new .eps' )
833830plt .close ()
834831
835832
@@ -877,7 +874,7 @@ def evec_bar_graph(evals, strengths, participations):
877874 ax .tick_params ('y' , direction = 'out' )
878875 plt .subplots_adjust (left = 0.25 )
879876 plt .colorbar ()
880- plt .savefig ('./time_hist_2D_pos_c{}.eps' .format (c ))
877+ plt .savefig ('./time_hist_2D_pos_c{}_new .eps' .format (c ))
881878 plt .close ()
882879
883880 neg_evalues2 = {}
@@ -912,7 +909,7 @@ def evec_bar_graph(evals, strengths, participations):
912909 ax .tick_params ('y' , direction = 'out' )
913910 plt .subplots_adjust (left = 0.25 )
914911 plt .colorbar ()
915- plt .savefig ('./time_hist_2D_neg_c{}.eps' .format (c ))
912+ plt .savefig ('./time_hist_2D_neg_c{}_new .eps' .format (c ))
916913 plt .close ()
917914
918915 n0_evalues2 = {}
@@ -948,7 +945,7 @@ def evec_bar_graph(evals, strengths, participations):
948945 ax .tick_params ('y' , direction = 'out' )
949946 plt .subplots_adjust (left = 0.25 )
950947 plt .colorbar ()
951- plt .savefig ('./time_hist_2D_n0_c{}.eps' .format (c ))
948+ plt .savefig ('./time_hist_2D_n0_c{}_new .eps' .format (c ))
952949 plt .close ()
953950
954951
@@ -963,7 +960,7 @@ def evec_bar_graph(evals, strengths, participations):
963960plt .title ("Number of grid points with given net water mass tendency" )
964961plt .xlabel ("Net water tendency" )
965962plt .ylabel ("Number of grid points" )
966- plt .savefig ('./tend_hist .eps' )
963+ plt .savefig ('./tend_hist_new .eps' )
967964plt .close ()
968965
969966# Log version of plot
@@ -975,7 +972,7 @@ def evec_bar_graph(evals, strengths, participations):
975972plt .title ("Number of grid points with given net water mass tendency" )
976973plt .xlabel ("Net water tendency" )
977974plt .ylabel ("Number of grid points" )
978- plt .savefig ('./tend_hist_log .eps' )
975+ plt .savefig ('./tend_hist_log_new .eps' )
979976plt .close ()
980977
981978cutoff_score = stats .percentileofscore (tend_norms , tend_cutoff )
0 commit comments