|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import sys |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import scipy.linalg as la |
| 7 | +import scipy.stats as stats |
| 8 | +import matplotlib |
| 9 | +matplotlib.use('Agg') |
| 10 | +import matplotlib.pyplot as plt |
| 11 | +import netCDF4 as nc4 |
| 12 | +import numdifftools as ndt |
| 13 | + |
| 14 | +from mg2 import wv_sat_methods as wsm |
| 15 | +from mg2 import micro_mg2_0 as mg |
| 16 | + |
| 17 | +from mg2_constants import * |
| 18 | + |
| 19 | +blocksize = 4096 |
| 20 | +num_files = 12 |
| 21 | +end_column = 48601 |
| 22 | + |
| 23 | +splits = [(blocksize*i, blocksize*(i+1)-1) for i in range(num_files)] |
| 24 | +splits[-1] = (splits[-1][0], end_column) |
| 25 | + |
| 26 | +EVALS_FILE_NAMES = ["/home/santos/Data/Jacobian_cutoff_{}-{}.nc".format(split[0], split[1]) |
| 27 | + for split in splits] |
| 28 | + |
| 29 | +efiles = [] |
| 30 | +for name in EVALS_FILE_NAMES: |
| 31 | + efiles.append(nc4.Dataset(name, 'r')) |
| 32 | + |
| 33 | +plt.autoscale(tight=True) |
| 34 | +cmap = plt.get_cmap('Reds') |
| 35 | + |
| 36 | +total_evalues = 0 |
| 37 | +for efile in efiles: |
| 38 | + total_evalues += 10*len(efile.dimensions['num_cell']) |
| 39 | + |
| 40 | +all_evalues = np.zeros((total_evalues,), dtype='c16') |
| 41 | + |
| 42 | +i = 0 |
| 43 | +for efile in efiles: |
| 44 | + num_cell = len(efile.dimensions['num_cell']) |
| 45 | + all_evalues[i:i+10*num_cell] = efile["eigenvalues"][:,:]["real"].flatten() + \ |
| 46 | + efile["eigenvalues"][:,:]["imag"].flatten()*1.j |
| 47 | + i += 10*num_cell |
| 48 | + |
| 49 | + |
| 50 | +# Plot values in the complex plane. |
| 51 | +max_exp = 1. |
| 52 | +cutoff_exp = -5. |
| 53 | +cutoff = 10.**cutoff_exp |
| 54 | +log_polar_evalues = np.zeros((total_evalues, 2)) |
| 55 | +for i in range(total_evalues): |
| 56 | + abs_eval = np.abs(all_evalues[i]) |
| 57 | + if abs_eval > cutoff: |
| 58 | + log_polar_evalues[i,0] = np.log10(abs_eval) - cutoff_exp |
| 59 | +log_polar_evalues[:,1] = np.angle(all_evalues) |
| 60 | + |
| 61 | +cart_log_evalues = np.zeros((total_evalues, 2)) |
| 62 | +cart_log_evalues[:,0] = log_polar_evalues[:,0] * np.cos(log_polar_evalues[:,1]) |
| 63 | +cart_log_evalues[:,1] = log_polar_evalues[:,0] * np.sin(log_polar_evalues[:,1]) |
| 64 | + |
| 65 | +max_plotval = max_exp - cutoff_exp |
| 66 | + |
| 67 | +nbins = 51 |
| 68 | +assert nbins % 2 == 1 |
| 69 | +bins = np.linspace(-max_plotval, max_plotval, nbins+1) |
| 70 | + |
| 71 | +plot_data, _, _ = np.histogram2d(cart_log_evalues[:,1], cart_log_evalues[:,0], |
| 72 | + bins=[bins, bins]) |
| 73 | + |
| 74 | +plt.pcolor(bins, bins, plot_data, edgecolors='k', cmap=cmap) |
| 75 | +circle_rad = np.log10(1./300.)-cutoff_exp |
| 76 | +circle_x = circle_rad * np.cos(np.linspace(0., 2.*np.pi, 1001)) |
| 77 | +circle_y = circle_rad * np.sin(np.linspace(0., 2.*np.pi, 1001)) |
| 78 | +plt.plot(circle_x, circle_y) |
| 79 | +converge_rad = 1./300. |
| 80 | +converge_x = converge_rad * (np.cos(np.linspace(0., 2.*np.pi, 1001)) - 1.) |
| 81 | +converge_y = converge_rad * np.sin(np.linspace(0., 2.*np.pi, 1001)) |
| 82 | +converge_abs = np.sqrt(converge_x**2 + converge_y**2) |
| 83 | +converge_angle = np.arctan2(converge_y, converge_x) |
| 84 | +plot_abs = np.maximum(np.log10(converge_abs) - cutoff_exp, 0.) |
| 85 | +plot_x = plot_abs * np.cos(converge_angle) |
| 86 | +plot_y = plot_abs * np.sin(converge_angle) |
| 87 | +plt.plot(plot_x, plot_y) |
| 88 | +plt.axvline(x=0., color='k', linewidth=2.) |
| 89 | +plt.axhline(y=0., color='k', linewidth=2.) |
| 90 | +plt.axis([-max_plotval, max_plotval, -max_plotval, max_plotval]) |
| 91 | +ticks = np.arange(-max_plotval, max_plotval+1., 1) |
| 92 | +# The "0.01" hack below is there to ensure that "0" outputs a 10 with a positive sign. |
| 93 | +tickvals = ["${}^{{{}}}$".format(int(np.sign(i+0.01)*10),int(abs(i)+cutoff_exp)) for i in ticks] |
| 94 | +ax = plt.gca() |
| 95 | +ax.set_xticks(ticks) |
| 96 | +ax.set_xticklabels(tickvals) |
| 97 | +ax.set_yticks(ticks) |
| 98 | +ax.set_yticklabels(tickvals) |
| 99 | +plt.clim(vmin=0., vmax=1.e4) |
| 100 | +plt.colorbar() |
| 101 | +plt.savefig('./complex_eigenvalues.png') |
| 102 | +plt.close() |
| 103 | + |
| 104 | +midpoint = nbins // 2 |
| 105 | +real_zeros = np.zeros((nbins,)) |
| 106 | +near_real_zeros = np.zeros((nbins,)) |
| 107 | +imag_zeros = np.zeros((nbins,)) |
| 108 | + |
| 109 | +for i in range(total_evalues): |
| 110 | + real_part = cart_log_evalues[i,0] |
| 111 | + if real_part < bins[0] or real_part >= bins[-1]: |
| 112 | + continue |
| 113 | + bin_index = nbins*2 |
| 114 | + for j in range(nbins): |
| 115 | + if real_part < bins[j+1]: |
| 116 | + bin_index = j |
| 117 | + break |
| 118 | + if all_evalues[i].imag == 0: |
| 119 | + real_zeros[bin_index] += 1 |
| 120 | + elif np.abs(all_evalues[i].imag) <= 1.e-5: |
| 121 | + near_real_zeros[bin_index] += 1 |
| 122 | + else: |
| 123 | + imag_zeros[bin_index] += 1 |
| 124 | + |
| 125 | +all_zeros = real_zeros + near_real_zeros + imag_zeros |
| 126 | +bin_centers = (bins[:-1] + bins[1:]) / 2 |
| 127 | + |
| 128 | +plt.bar(bin_centers, all_zeros, |
| 129 | + width=(bins[1]-bins[0]), color='r', label='All zeros') |
| 130 | +plt.bar(bin_centers, real_zeros, |
| 131 | + width=(bins[1]-bins[0]), color='b', label='Real zeros') |
| 132 | +plt.legend(loc='best') |
| 133 | +ticks = np.arange(-max_plotval, max_plotval+1., 1) |
| 134 | +tickvals = ["${}^{{{}}}$".format(int(np.sign(i+0.01)*10),int(abs(i)+cutoff_exp)) for i in ticks] |
| 135 | +ax = plt.gca() |
| 136 | +ax.set_xlim(left=bins[0], right=bins[-1]) |
| 137 | +ax.set_xticks(ticks) |
| 138 | +ax.set_xticklabels(tickvals) |
| 139 | +plt.savefig('./complex_vertint.png') |
| 140 | +plt.close() |
| 141 | + |
| 142 | +plt.plot(bin_centers, real_zeros / all_zeros, color='k') |
| 143 | +ticks = np.arange(-max_plotval, max_plotval+1., 1) |
| 144 | +tickvals = ["${}^{{{}}}$".format(int(np.sign(i+0.01)*10),int(abs(i)+cutoff_exp)) for i in ticks] |
| 145 | +ax = plt.gca() |
| 146 | +ax.set_xticks(ticks) |
| 147 | +ax.set_xticklabels(tickvals) |
| 148 | +plt.savefig('./complex_ratio.png') |
| 149 | +plt.close() |
| 150 | + |
| 151 | +plt.bar(bin_centers, all_zeros, |
| 152 | + width=(bins[1]-bins[0]), color='r', label='All zeros') |
| 153 | +plt.bar(bin_centers, real_zeros + near_real_zeros, |
| 154 | + width=(bins[1]-bins[0]), color='b', label='Real zeros') |
| 155 | +plt.legend(loc='best') |
| 156 | +ticks = np.arange(-max_plotval, max_plotval+1., 1) |
| 157 | +tickvals = ["${}^{{{}}}$".format(int(np.sign(i+0.01)*10),int(abs(i)+cutoff_exp)) for i in ticks] |
| 158 | +ax = plt.gca() |
| 159 | +ax.set_xlim(left=bins[0], right=bins[-1]) |
| 160 | +ax.set_xticks(ticks) |
| 161 | +ax.set_xticklabels(tickvals) |
| 162 | +plt.savefig('./complex_vertint_cutoff.png') |
| 163 | +plt.close() |
| 164 | + |
| 165 | +plt.plot(bin_centers, (real_zeros + near_real_zeros) / all_zeros, color='k') |
| 166 | +ticks = np.arange(-max_plotval, max_plotval+1., 1) |
| 167 | +tickvals = ["${}^{{{}}}$".format(int(np.sign(i+0.01)*10),int(abs(i)+cutoff_exp)) for i in ticks] |
| 168 | +ax = plt.gca() |
| 169 | +ax.set_xticks(ticks) |
| 170 | +ax.set_xticklabels(tickvals) |
| 171 | +plt.savefig('./complex_ratio_cutoff.png') |
| 172 | +plt.close() |
0 commit comments