|
| 1 | +import matplotlib as mpl |
| 2 | +mpl.use('Agg') |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +import numpy as np |
| 5 | +import os |
| 6 | +from scipy.constants import c |
| 7 | +import h5py |
| 8 | + |
| 9 | +def w2dbm(W, floor=-100): |
| 10 | + """This function converts a power given in W to a power given in dBm. |
| 11 | + Inputs:: |
| 12 | + W(float): power in units of W |
| 13 | + Returns:: |
| 14 | + Power in units of dBm(float) |
| 15 | + """ |
| 16 | + if type(W) != np.ndarray: |
| 17 | + if W > 0: |
| 18 | + return 10. * np.log10(W) + 30 |
| 19 | + elif W == 0: |
| 20 | + return floor |
| 21 | + else: |
| 22 | + print(W) |
| 23 | + raise(ZeroDivisionError) |
| 24 | + a = 10. * (np.ma.log10(W)).filled(floor/10-3) + 30 |
| 25 | + return a |
| 26 | + |
| 27 | +plt.gca().get_yaxis().get_major_formatter().set_useOffset(False) |
| 28 | +plt.gca().get_xaxis().get_major_formatter().set_useOffset(False) |
| 29 | + |
| 30 | + |
| 31 | +def plotter_dbm(index, nm, sim_wind, u, U, P0_p, P0_s, f_p, f_s, which,ro, pump_wave = '',filename=None, title=None, im=0, plots = True): |
| 32 | + #u, U = np.reshape(u,(np.shape(u)[-1], np.shape(u)[0])), np.reshape(u,(np.shape(U)[-1], np.shape(U)[0])) |
| 33 | + if plots == True: |
| 34 | + fig = plt.figure(figsize=(20.0, 10.0)) |
| 35 | + |
| 36 | + plt.plot(1e-3*c/sim_wind.fv, |
| 37 | + w2dbm(np.abs(U[:,which])**2), '-*') |
| 38 | + #plt.gca().get_yaxis().get_major_formatter().set_useOffset(False) |
| 39 | + #plt.gca().get_xaxis().get_major_formatter().set_useOffset(False) |
| 40 | + plt.xlabel(r'$\lambda (nm)$', fontsize=18) |
| 41 | + plt.ylabel(r'$Spectrum (a.u.)$', fontsize=18) |
| 42 | + plt.ylim([-80, 100]) |
| 43 | + plt.xlim([np.min(sim_wind.lv), np.max(sim_wind.lv)]) |
| 44 | + plt.xlim([900, 1250]) |
| 45 | + plt.title(title) |
| 46 | + plt.grid() |
| 47 | + if type(im) != int: |
| 48 | + newax = fig.add_axes([0.8, 0.8, 0.2, 0.2], anchor='NE') |
| 49 | + newax.imshow(im) |
| 50 | + newax.axis('off') |
| 51 | + if filename == None: |
| 52 | + plt.show() |
| 53 | + else: |
| 54 | + plt.savefig('output'+pump_wave+'/output'+str(index)+'/figures/wavelength/'+filename, bbox_inched='tight') |
| 55 | + |
| 56 | + plt.close(fig) |
| 57 | + |
| 58 | + fig = plt.figure(figsize=(20.0, 10.0)) |
| 59 | + plt.plot(sim_wind.fv, w2dbm(np.abs(U[:,which])**2), '-*') |
| 60 | + #plt.gca().get_yaxis().get_major_formatter().set_useOffset(False) |
| 61 | + #plt.gca().get_xaxis().get_major_formatter().set_useOffset(False) |
| 62 | + plt.xlabel(r'$f (THz)$', fontsize=18) |
| 63 | + plt.ylabel(r'$Spectrum (a.u.)$', fontsize=18) |
| 64 | + #plt.xlim([np.min(sim_wind.fv), np.max(sim_wind.fv)]) |
| 65 | + plt.ylim([-20, 120]) |
| 66 | + #plt.xlim(270,300) |
| 67 | + plt.title(str(f_p)+' ' +str(f_s)) |
| 68 | + plt.grid() |
| 69 | + if type(im) != int: |
| 70 | + newax = fig.add_axes([0.8, 0.8, 0.2, 0.2], anchor='NE') |
| 71 | + newax.imshow(im) |
| 72 | + newax.axis('off') |
| 73 | + if filename == None: |
| 74 | + plt.show() |
| 75 | + else: |
| 76 | + plt.savefig('output'+pump_wave+'/output'+str(index)+'/figures/freequency/'+filename, bbox_inched='tight') |
| 77 | + plt.close(fig) |
| 78 | + |
| 79 | + fig = plt.figure(figsize=(20.0, 10.0)) |
| 80 | + |
| 81 | + plt.plot(sim_wind.t,np.abs(u[:, which])**2, '*-') |
| 82 | + #plt.gca().get_yaxis().get_major_formatter().set_useOffset(False) |
| 83 | + plt.title('time space') |
| 84 | + #plt.ylim([0, 160]) |
| 85 | + plt.grid() |
| 86 | + plt.xlabel(r'$t(ps)$') |
| 87 | + plt.ylabel(r'$Spectrum$') |
| 88 | + if type(im) != int: |
| 89 | + newax = fig.add_axes([0.8, 0.8, 0.2, 0.2], anchor='NE') |
| 90 | + newax.imshow(im) |
| 91 | + newax.axis('off') |
| 92 | + |
| 93 | + if filename == None: |
| 94 | + plt.show() |
| 95 | + else: |
| 96 | + |
| 97 | + plt.savefig('output'+pump_wave+'/output'+str(index)+'/figures/time/'+filename) |
| 98 | + plt.close(fig) |
| 99 | + |
| 100 | + |
| 101 | + if filename is not(None): |
| 102 | + if filename[:4] != 'port': |
| 103 | + layer = filename[-1]+'/'+filename[:-1] |
| 104 | + else: |
| 105 | + layer = filename |
| 106 | + try: |
| 107 | + |
| 108 | + save_variables('data_large', layer, filepath='output'+pump_wave+'/output'+str(index)+'/data/', U = U[:,which], t=sim_wind.t, u=u[:,which], |
| 109 | + fv=sim_wind.fv, lv=sim_wind.lv, |
| 110 | + which=which, nm=nm, P0_p=P0_p, P0_s=P0_s, f_p=f_p, f_s=f_s, ro = ro) |
| 111 | + except RuntimeError: |
| 112 | + os.system('rm output'+pump_wave+'/output'+str(index)+'/data/data_large.hdf5') |
| 113 | + save_variables('data_large', layer, filepath='output'+pump_wave+'/output'+str(index)+'/data/', U=U[:,which], t=sim_wind.t, u=u[:,which], |
| 114 | + fv=sim_wind.fv, lv=sim_wind.lv, |
| 115 | + which=which, nm=nm, P0_p=P0_p, P0_s=P0_s, f_p=f_p, f_s=f_s, ro = ro) |
| 116 | + pass |
| 117 | + |
| 118 | + return 0 |
| 119 | + |
| 120 | + |
| 121 | +def plotter_dbm_load(): |
| 122 | + # class sim_window(object): |
| 123 | + plotter_dbm(nm, sim_wind, Uabs, u, which) |
| 124 | + return None |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +def animator_pdf_maker(rounds, pump_index): |
| 129 | + """ |
| 130 | + Creates the animation and pdf of the FOPO at different parts of the FOPO |
| 131 | + using convert from imagemagic. Also removes the pngs so be carefull |
| 132 | +
|
| 133 | + """ |
| 134 | + print("making pdf's and animations.") |
| 135 | + space = ('wavelength', 'freequency', 'time') |
| 136 | + for sp in space: |
| 137 | + file_loc = 'output/output'+str(pump_index)+'/figures/'+sp+'/' |
| 138 | + strings_large = ['convert '+file_loc+'00.png '] |
| 139 | + for i in range(4): |
| 140 | + strings_large.append('convert ') |
| 141 | + for ro in range(rounds): |
| 142 | + for i in range(4): |
| 143 | + strings_large[i+1] += file_loc+str(ro)+str(i+1)+'.png ' |
| 144 | + for w in range(1, 4): |
| 145 | + if i == 5: |
| 146 | + break |
| 147 | + strings_large[0] += file_loc+str(ro)+str(w)+'.png ' |
| 148 | + for i in range(4): |
| 149 | + os.system(strings_large[i]+file_loc+str(i)+'.pdf') |
| 150 | + |
| 151 | + file_loca = file_loc+'portA/' |
| 152 | + file_locb = file_loc+'portB/' |
| 153 | + string_porta = 'convert ' |
| 154 | + string_portb = 'convert ' |
| 155 | + for i in range(rounds): |
| 156 | + string_porta += file_loca + str(i) + '.png ' |
| 157 | + string_portb += file_locb + str(i) + '.png ' |
| 158 | + |
| 159 | + string_porta += file_loca+'porta.pdf ' |
| 160 | + string_portb += file_locb+'portb.pdf ' |
| 161 | + os.system(string_porta) |
| 162 | + os.system(string_portb) |
| 163 | + |
| 164 | + for i in range(4): |
| 165 | + os.system( |
| 166 | + 'convert -delay 30 '+file_loc+str(i)+'.pdf '+file_loc+str(i)+'.mp4') |
| 167 | + os.system('convert -delay 30 ' + file_loca + |
| 168 | + 'porta.pdf ' + file_loca+'porta.mp4 ') |
| 169 | + os.system('convert -delay 30 ' + file_locb + |
| 170 | + 'portb.pdf ' + file_locb+'portb.mp4 ') |
| 171 | + |
| 172 | + for i in (file_loc, file_loca, file_locb): |
| 173 | + print('rm ' + i + '*.png') |
| 174 | + os.system('rm ' + i + '*.png') |
| 175 | + os.system('sleep 5') |
| 176 | + return None |
| 177 | + |
| 178 | + |
| 179 | +def read_variables(filename, layer, filepath=''): |
| 180 | + with h5py.File(filepath+str(filename)+'.hdf5', 'r') as f: |
| 181 | + D = {} |
| 182 | + for i in f.get(layer).keys(): |
| 183 | + try: |
| 184 | + D[str(i)] = f.get(layer + '/' + str(i)).value |
| 185 | + except AttributeError: |
| 186 | + pass |
| 187 | + return D |
| 188 | + |
| 189 | + |
| 190 | +def save_variables(filename, layers, filepath='', **variables): |
| 191 | + with h5py.File(filepath + filename + '.hdf5', 'a') as f: |
| 192 | + for i in (variables): |
| 193 | + f.create_dataset(layers+'/'+str(i), data=variables[i]) |
| 194 | + return None |
0 commit comments