def afterScript(complexPlotsIs, complexPlotsEs, complexPlotsPCAs): if what == 'rays': return if not isMono: return dump = [] NN = len(complexPlotsEs) for ic, (complexPlotIs, complexPlotEs, complexPlotPCAs) in enumerate( zip(complexPlotsIs, complexPlotsEs, complexPlotsPCAs)): x = complexPlotIs.xaxis.binCenters y = complexPlotIs.yaxis.binCenters Ixy = complexPlotIs.total2D Exy4D = complexPlotEs.total4D print("solving eigenvalue problem, {0} of {1}...".format(ic+1, NN)) start = time.time() wN, vN = rco.calc_eigen_modes_4D(Exy4D) stop = time.time() print("the eigenvalue problem has taken {0:.4f} s".format(stop-start)) print("solving PCA problem, {0} of {1}...".format(ic+1, NN)) Es = complexPlotPCAs.field3D start = time.time() wPCA, vPCA = rco.calc_eigen_modes_PCA(Es) stop = time.time() print("the PCA problem has taken {0:.4f} s".format(stop-start)) dump.append([x, y, Ixy, wN, vN, Es, wPCA, vPCA]) pickleName = '{0}-res.pickle'.format(prefix) with open(pickleName, 'wb') as f: pickle.dump(dump, f, protocol=2) print("Done")
def afterScript(complexPlotsIs, complexPlotsEs, complexPlotsPCAs): if what == 'rays': return if not isMono: return dump = [] NN = len(complexPlotsEs) for ic, (complexPlotIs, complexPlotEs, complexPlotPCAs) in enumerate( zip(complexPlotsIs, complexPlotsEs, complexPlotsPCAs)): x = complexPlotIs.xaxis.binCenters y = complexPlotIs.yaxis.binCenters Ixy = complexPlotIs.total2D Exy4D = complexPlotEs.total4D print("solving eigenvalue problem, {0} of {1}...".format(ic + 1, NN)) start = time.time() wN, vN = rco.calc_eigen_modes_4D(Exy4D) stop = time.time() print("the eigenvalue problem has taken {0:.4f} s".format(stop - start)) print("solving PCA problem, {0} of {1}...".format(ic + 1, NN)) Es = complexPlotPCAs.field3D start = time.time() wPCA, vPCA = rco.calc_eigen_modes_PCA(Es) stop = time.time() print("the PCA problem has taken {0:.4f} s".format(stop - start)) dump.append([x, y, Ixy, wN, vN, Es, wPCA, vPCA]) pickleName = '{0}-res.pickle'.format(prefix) with open(pickleName, 'wb') as f: pickle.dump(dump, f, protocol=2) print("Done")
def plotFocus(): pickleName = '{0}-res.pickle'.format(prefix) with open(pickleName, 'rb') as f: dump = pickle.load(f) # normalize over all images: norm = 0. for ic, d in enumerate(dFocus): Es = dump[ic][3] dmax = ((np.abs(Es)**2).sum(axis=0) / Es.shape[0]).max() if norm < dmax: norm = dmax norm = norm**0.5 NN = len(dFocus) for ic, d in enumerate(dFocus): x, y, Ixy, Es = dump[ic] scrStr = 'screen at f+{0:.1f} µm'.format(d*1000) print("solving PCA problem, {0} of {1}...".format(ic+1, NN)) start = time.time() wPCA, vPCA = rco.calc_eigen_modes_PCA(Es) stop = time.time() print("the PCA problem has taken {0} s".format(stop-start)) print("Top 4 eigen values (PCA) = {0}".format(wPCA)) figP = rco.plot_eigen_modes(x, y, wPCA, vPCA, xlabel='x (µm)', ylabel='z (µm)') figP.suptitle('Principal components of one-electron images, ' + scrStr, fontsize=11) figP.savefig('PCA-{0}-{1:02d}.png'.format(prefix, ic)) xarr, xnam = (x, 'x') if cut.startswith('hor') else (y, 'z') xdata = rco.calc_1D_coherent_fraction(Es/norm, xnam, xarr) fig2D, figXZ = rco.plot_1D_degree_of_coherence( xdata, xnam, xarr, "nm", isIntensityNormalized=True, locLegend='lower left') fig2D.suptitle('Mutual intensity, ' + scrStr, size=11) figXZ.suptitle('Intensity and Degree of Coherence, ' + scrStr, size=11) fig2D.savefig('MutualI-{0}-{1:02d}.png'.format(prefix, ic)) figXZ.savefig('IDOC-{0}-{1:02d}.png'.format(prefix, ic)) print("Done") plt.show()
def afterScript(plots, complexPlotsPCA, beamLine): if beamLine.source.trajectory is not None: import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.gca(projection='3d') tr = beamLine.source.trajectory ax.plot(tr[2] / 1e3, tr[0] * 1e3, tr[1] * 1e3) ax.set_xlabel(r"$z$, m") ax.set_ylabel(u"$x$, µm") ax.set_zlabel(u"$y$, µm") plt.title("trajectory 3D") plt.savefig(prefix + '0trajectory' + suffix + '.png') if not complexPlotsPCA: return if not ('mono' in prefix): return dump = [] start = time.time() NN = len(complexPlotsPCA) for ic, complexPlotPCA in enumerate(complexPlotsPCA): x = complexPlotPCA.xaxis.binCenters y = complexPlotPCA.yaxis.binCenters if repeats >= 4: print("solving PCA problem, {0} of {1}...".format(ic + 1, NN)) Es = complexPlotPCA.field3D start = time.time() wPCA, vPCA = rco.calc_eigen_modes_PCA(Es) stop = time.time() print("the PCA problem has taken {0:.4f} s".format(stop - start)) dump.append([repeats, x, y, wPCA, vPCA]) pickleName = '{0}-{1}repeats.pickle'.format(prefix, repeats) with open(pickleName, 'wb') as f: pickle.dump(dump, f, protocol=2) print("Done") if beamLine.source.trajectory is not None: plt.show()
def afterScript(plots, complexPlotsPCA, beamLine): if beamLine.source.trajectory is not None: import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.gca(projection='3d') tr = beamLine.source.trajectory ax.plot(tr[2]/1e3, tr[0]*1e3, tr[1]*1e3) ax.set_xlabel(r"$z$, m") ax.set_ylabel(u"$x$, µm") ax.set_zlabel(u"$y$, µm") plt.title("trajectory 3D") plt.savefig(prefix + '0trajectory' + suffix + '.png') if not complexPlotsPCA: return if not ('mono' in prefix): return dump = [] start = time.time() NN = len(complexPlotsPCA) for ic, complexPlotPCA in enumerate(complexPlotsPCA): x = complexPlotPCA.xaxis.binCenters y = complexPlotPCA.yaxis.binCenters if repeats >= 4: print("solving PCA problem, {0} of {1}...".format(ic+1, NN)) Es = complexPlotPCA.field3D start = time.time() wPCA, vPCA = rco.calc_eigen_modes_PCA(Es) stop = time.time() print("the PCA problem has taken {0:.4f} s".format(stop-start)) dump.append([repeats, x, y, wPCA, vPCA]) pickleName = '{0}-{1}repeats.pickle'.format(prefix, repeats) with open(pickleName, 'wb') as f: pickle.dump(dump, f, protocol=2) print("Done") if beamLine.source.trajectory is not None: plt.show()
def main(): und = rs.Undulator(**kw) for harmonic in harmonics: Eh = harmonic * und.E1 # Eh = Eh * (1 - 1./harmonic/und.Np) # detuned to harmonic's maximum und.eMin = Eh * (1 - bw/2) und.eMax = Eh * (1 + bw/2) und.reset() energy = np.ones(repeats) * Eh st = 'st' if harmonic == 1 else 'nd' if harmonic == 2 else 'rd'\ if harmonic == 3 else 'th' for energySpread in energySpreads: und.eEspread = energySpread for emittancex in emittances: if energySpread > 1e-12: ses = '{0:.1e}'.format(energySpread) ses = ses[:-2] + ses[-1] # removes "0" from power else: ses = '0' txt = '{0}{1} harmonic ({2:.0f} eV){5}{3} energy spread{5}' +\ '$\\epsilon_x$ = {4:.0f} pmrad' cap = txt.format(harmonic, st, Eh, ses, emittancex, ', ') baseName = 'h{0:02d}-esp{1}-em{2:04.0f}'.format( harmonic, energySpread, emittancex) emittancez = 10 if emittancex > 1e-12 else 0 und.dx = np.sqrt(emittancex*betaX) * 1e-3 # in mm und.dz = np.sqrt(emittancez*betaZ) * 1e-3 # in mm und.dxprime = \ emittancex*1e-9 / und.dx if und.dx > 0 else 0. # rad und.dzprime = \ emittancez*1e-9 / und.dz if und.dz > 0 else 0. # rad Es, Ep = und.multi_electron_stack(energy, theta, psi) print("Es.shape", Es.shape) k = binsx * binsz D = np.array(Es).reshape((repeats, k), order='F').T J = np.dot(D, D.T.conjugate()) # / repeats print("solving eigenvalue problem...") start = time.time() dotc4 = rco.calc_degree_of_transverse_coherence_4D(J) # print('dotc4', dotc4) wN, vN = rco.calc_eigen_modes_4D(J, eigenN=4) stop = time.time() print("the eigenvalue problem has taken {0:.4} s".format( stop-start)) print("vN.shape", vN.shape) print("Top 4 eigen values (4D) = {0}".format(wN)) figE4 = rco.plot_eigen_modes(theta*p, psi*p, wN, vN, xlabel='x (mm)', ylabel='z (mm)') figE4.axes[0].xaxis.set_ticklabels([]) figE4.axes[1].xaxis.set_ticklabels([]) figE4.suptitle('Eigen modes of mutual intensity,\n' + cap, fontsize=11) plt.text(0.05, 0.05, 'DoTC={0:.3f}'.format(dotc4), transform=figE4.axes[0].transAxes, ha='left', va='bottom', color='w', size=10) figE4.savefig('Modes-{0}-{1}.png'.format('s', baseName)) print("solving PCA problem...") start = time.time() dotcP = rco.calc_degree_of_transverse_coherence_PCA(Es) # print('dotcP', dotcP) wPCA, vPCA = rco.calc_eigen_modes_PCA(Es, eigenN=4) stop = time.time() print("the PCA problem has taken {0:.4} s".format(stop-start)) print("vPCA.shape", vPCA.shape) print("Top 4 eigen values (PCA) = {0}".format(wPCA)) figEP = rco.plot_eigen_modes(theta*p, psi*p, wPCA, vPCA, xlabel='x (mm)', ylabel='z (mm)') figEP.axes[0].xaxis.set_ticklabels([]) figEP.axes[1].xaxis.set_ticklabels([]) figEP.suptitle('Principal components of one-electron images,\n' + cap, fontsize=11) plt.text(0.05, 0.05, 'DoTC={0:.3f}'.format(dotcP), transform=figEP.axes[0].transAxes, ha='left', va='bottom', color='w', size=10) figEP.savefig('Components-{0}-{1}.png'.format('s', baseName)) xdata = rco.calc_1D_coherent_fraction(Es, 'x', theta*p, p) ydata = rco.calc_1D_coherent_fraction(Es, 'z', psi*p, p) fig2D1, figXZ = rco.plot_1D_degree_of_coherence( xdata, 'x', theta*p) fig2D2, figXZ = rco.plot_1D_degree_of_coherence( ydata, 'z', psi*p, fig2=figXZ) fig2D1.suptitle('Mutual intensity for horizontal cut,\n ' + cap, size=11) fig2D2.suptitle('Mutual intensity for vertical cut,\n ' + cap, size=11) figXZ.suptitle('Intensity and Degree of Coherence,\n ' + cap, size=11) fig2D1.savefig('MutualI-x-{0}-{1}.png'.format('s', baseName)) fig2D2.savefig('MutualI-z-{0}-{1}.png'.format('s', baseName)) figXZ.savefig('DOC-{0}-{1}.png'.format('s', baseName)) plt.show()
def main(): und = rs.Undulator(**kw) for harmonic in harmonics: Eh = harmonic * und.E1 # Eh = Eh * (1 - 1./harmonic/und.Np) # detuned to harmonic's maximum und.eMin = Eh * (1 - bw / 2) und.eMax = Eh * (1 + bw / 2) und.reset() energy = np.ones(repeats) * Eh st = 'st' if harmonic == 1 else 'nd' if harmonic == 2 else 'rd'\ if harmonic == 3 else 'th' for energySpread in energySpreads: und.eEspread = energySpread for emittancex in emittances: if energySpread > 1e-12: ses = '{0:.1e}'.format(energySpread) ses = ses[:-2] + ses[-1] # removes "0" from power else: ses = '0' txt = '{0}{1} harmonic ({2:.0f} eV){5}{3} energy spread{5}' +\ '$\\epsilon_x$ = {4:.0f} pmrad' cap = txt.format(harmonic, st, Eh, ses, emittancex, ', ') baseName = 'h{0:02d}-esp{1}-em{2:04.0f}'.format( harmonic, energySpread, emittancex) emittancez = 10 if emittancex > 1e-12 else 0 und.dx = np.sqrt(emittancex * betaX) * 1e-3 # in mm und.dz = np.sqrt(emittancez * betaZ) * 1e-3 # in mm und.dxprime = \ emittancex*1e-9 / und.dx if und.dx > 0 else 0. # rad und.dzprime = \ emittancez*1e-9 / und.dz if und.dz > 0 else 0. # rad Es, Ep = und.multi_electron_stack(energy, theta, psi) print("Es.shape", Es.shape) k = binsx * binsz D = np.array(Es).reshape((repeats, k), order='F').T J = np.dot(D, D.T.conjugate()) # / repeats print("solving eigenvalue problem...") start = time.time() dotc4 = rco.calc_degree_of_transverse_coherence_4D(J) # print('dotc4', dotc4) wN, vN = rco.calc_eigen_modes_4D(J, eigenN=4) stop = time.time() print( "the eigenvalue problem has taken {0:.4} s".format(stop - start)) print("vN.shape", vN.shape) print("Top 4 eigen values (4D) = {0}".format(wN)) figE4 = rco.plot_eigen_modes(theta * p, psi * p, wN, vN, xlabel='x (mm)', ylabel='z (mm)') figE4.axes[0].xaxis.set_ticklabels([]) figE4.axes[1].xaxis.set_ticklabels([]) figE4.suptitle('Eigen modes of mutual intensity,\n' + cap, fontsize=11) plt.text(0.05, 0.05, 'DoTC={0:.3f}'.format(dotc4), transform=figE4.axes[0].transAxes, ha='left', va='bottom', color='w', size=10) figE4.savefig('Modes-{0}-{1}.png'.format('s', baseName)) print("solving PCA problem...") start = time.time() dotcP = rco.calc_degree_of_transverse_coherence_PCA(Es) # print('dotcP', dotcP) wPCA, vPCA = rco.calc_eigen_modes_PCA(Es, eigenN=4) stop = time.time() print("the PCA problem has taken {0:.4} s".format(stop - start)) print("vPCA.shape", vPCA.shape) print("Top 4 eigen values (PCA) = {0}".format(wPCA)) figEP = rco.plot_eigen_modes(theta * p, psi * p, wPCA, vPCA, xlabel='x (mm)', ylabel='z (mm)') figEP.axes[0].xaxis.set_ticklabels([]) figEP.axes[1].xaxis.set_ticklabels([]) figEP.suptitle( 'Principal components of one-electron images,\n' + cap, fontsize=11) plt.text(0.05, 0.05, 'DoTC={0:.3f}'.format(dotcP), transform=figEP.axes[0].transAxes, ha='left', va='bottom', color='w', size=10) figEP.savefig('Components-{0}-{1}.png'.format('s', baseName)) xdata = rco.calc_1D_coherent_fraction(Es, 'x', theta * p, p) ydata = rco.calc_1D_coherent_fraction(Es, 'z', psi * p, p) fig2D1, figXZ = rco.plot_1D_degree_of_coherence( xdata, 'x', theta * p) fig2D2, figXZ = rco.plot_1D_degree_of_coherence(ydata, 'z', psi * p, fig2=figXZ) fig2D1.suptitle('Mutual intensity for horizontal cut,\n ' + cap, size=11) fig2D2.suptitle('Mutual intensity for vertical cut,\n ' + cap, size=11) figXZ.suptitle('Intensity and Degree of Coherence,\n ' + cap, size=11) fig2D1.savefig('MutualI-x-{0}-{1}.png'.format('s', baseName)) fig2D2.savefig('MutualI-z-{0}-{1}.png'.format('s', baseName)) figXZ.savefig('DOC-{0}-{1}.png'.format('s', baseName)) plt.show()