def testConvolutionVisual(self): sigma_matrix = SigmaWaist(sigma_x=3e-6, sigma_y=1e-6, sigma_x_prime=5e-6, sigma_y_prime=5e-6) x_coordinates = np.linspace(-1e-5, 1e-5, 50) y_coordinates = np.linspace(-1e-5, 1e-5, 50) wavenumber = 1e+11 e_field = createGaussian2D(sigma_x=1.0e-6, sigma_y=1.0e-6, x_coordinates=x_coordinates, y_coordinates=y_coordinates) e_field = e_field + 0j af = AutocorrelationBuilder(N_e=0.0000001, sigma_matrix=sigma_matrix, field=e_field, x_coordinates=x_coordinates, y_coordinates=y_coordinates, k=wavenumber) f = np.zeros((x_coordinates.shape[0], y_coordinates.shape[0]), dtype=np.complex128) # damping along y should be higher than along x because sigma_x > sigma_y for the density. for y in (0.0, 1.5e-6, -1.5e-6): for i_x_1, x_1 in enumerate(x_coordinates): for i_x_2, x_2 in enumerate(x_coordinates): r_1 = np.array([x_1 + x_2, y]) r_2 = np.array([x_1 - x_2, y]) f[i_x_1, i_x_2] = af.evaluate(r_1, r_2) plotSurface(x_coordinates, x_coordinates, f, False) for x in (0.0, 1.5e-6, -1.5e-6): for i_y_1, y_1 in enumerate(y_coordinates): for i_y_2, y_2 in enumerate(y_coordinates): r_1 = np.array([x, y_1 + y_2]) r_2 = np.array([x, y_1 - y_2]) f[i_y_1, i_y_2] = af.evaluate(r_1, r_2) plotSurface(y_coordinates, y_coordinates, f, False)
def testNumerical(self): x_coordinates_gaussian = np.linspace(-3e-7, 3e-7, 100) y_coordinates_gaussian = np.linspace(-3e-7, 3e-7, 100) field = createGaussian2D(sigma_x=1e-7, sigma_y=1e-7, x_coordinates=x_coordinates_gaussian, y_coordinates=y_coordinates_gaussian) sigma_matrix = SigmaWaist(sigma_x=3e-6, sigma_y=1e-6, sigma_x_prime=5e-6, sigma_y_prime=4e-6) wavenumber = 1e+11 tester = NumericalPhaseSpaceDensity( N_e=1, field_x_coordinates=x_coordinates_gaussian, field_y_coordinates=y_coordinates_gaussian, efield=field, sigma_matrix=sigma_matrix, z=0.0, delta=0.0, wavenumber=wavenumber) x_coordinates = np.linspace(-10e-6, 10e-6, 15) y_coordinates = np.linspace(-10e-6, 10e-6, 15) work_buff = np.zeros((x_coordinates.shape[0], y_coordinates.shape[0]), dtype=np.complex128) for i_x, x in enumerate(x_coordinates): for i_y, y in enumerate(y_coordinates): r_1 = np.array([x, y]) r_2 = r_1 work_buff[i_x, i_y] = tester.evaluate(r_1, r_2) plotSurface(x_coordinates, y_coordinates, work_buff, True)
def testNumerical(self): x_coordinates_gaussian = np.linspace(-3e-7, 3e-7, 100) y_coordinates_gaussian = np.linspace(-3e-7, 3e-7, 100) field = createGaussian2D(sigma_x=1e-7, sigma_y=1e-7, x_coordinates=x_coordinates_gaussian, y_coordinates=y_coordinates_gaussian) sigma_matrix = SigmaWaist(sigma_x=3e-6, sigma_y=1e-6, sigma_x_prime=5e-6, sigma_y_prime=4e-6) wavenumber = 1e+11 tester = NumericalPhaseSpaceDensity(N_e=1, field_x_coordinates=x_coordinates_gaussian, field_y_coordinates=y_coordinates_gaussian, efield=field, sigma_matrix=sigma_matrix, z=0.0, delta=0.0, wavenumber=wavenumber) x_coordinates = np.linspace(-10e-6, 10e-6, 15) y_coordinates = np.linspace(-10e-6, 10e-6, 15) work_buff = np.zeros((x_coordinates.shape[0], y_coordinates.shape[0]), dtype=np.complex128) for i_x, x in enumerate(x_coordinates): for i_y, y in enumerate(y_coordinates): r_1 = np.array([x, y]) r_2 = r_1 work_buff[i_x, i_y] = tester.evaluate(r_1, r_2) plotSurface(x_coordinates, y_coordinates, work_buff, True)
def plotSRWVertical(self): from comsyl.math.utils import plotSurface plotSurface(self._y_coordinates, self._y_coordinates, np.abs(self.SRWVertical()))
def plotSRWHorizontal(self): from comsyl.math.utils import plotSurface plotSurface(self._x_coordinates, self._x_coordinates, np.abs(self.SRWHorizontal()))
def plotSmallerDisplacement(self): from comsyl.math.utils import plotSurface plotSurface(self._x_coordinates, self._y_coordinates, np.abs(self.smallerDisplacement()))
def plotPlaneForOriginHole(self): from comsyl.math.utils import plotSurface plotSurface(self._x_coordinates, self._y_coordinates, np.abs(self.planeForOriginHole()))
def testGaussian(self): x = np.linspace(-5, 5, 100) y = np.linspace(-5, 5, 100) f = createGaussian2D(1.0, 1.0, x, y) plotSurface(x, y, f, False)
def testConvolution(self): Sigma_x = 0.75e-6 Sigma_y = 1e-6 sigma_matrix = SigmaWaist(sigma_x=Sigma_x, sigma_y=Sigma_y, sigma_x_prime=1e-60, sigma_y_prime=1e-60, sigma_dd=0.89e-03) x_coordinates = np.linspace(-0.5e-5, 0.5e-5, 200) y_coordinates = np.linspace(-0.5e-5, 0.5e-5, 200) wavenumber = 1e+11 sigma_x = 1.0e-6 sigma_y = 1.0e-6 e_field = createGaussian2D(sigma_x=sigma_x, sigma_y=sigma_y, x_coordinates=x_coordinates, y_coordinates=y_coordinates) e_field = e_field + 0j e_field = e_field[np.newaxis, :, :] tester_x = ConvolutionTester(Sigma_x=Sigma_x, A=2.0, sigma_x=sigma_x) tester_y = ConvolutionTester(Sigma_x=Sigma_y, A=2.0, sigma_x=sigma_y) af = AutocorrelationBuilder(N_e=0.0000001, sigma_matrix=sigma_matrix, weighted_fields=e_field, x_coordinates=x_coordinates, y_coordinates=y_coordinates, k=wavenumber) f = np.zeros((x_coordinates.shape[0], y_coordinates.shape[0]), dtype=np.complex128) t = np.zeros_like(f) # Test along y slices for y in y_coordinates: for i_x_1, x_1 in enumerate(x_coordinates): for i_x_2, x_2 in enumerate(x_coordinates): r_1 = np.array([x_1 + x_2, y]) r_2 = np.array([x_1 - x_2, y]) f[i_x_1, i_x_2] = af.evaluate(r_1, r_2) t[i_x_1, i_x_2] = tester_x.evaluateAnalytical( r_1[0], r_2[0]) * tester_y.evaluateAnalytical( r_1[1], r_2[1]) f /= norm2D(x_coordinates, x_coordinates, f) t /= norm2D(x_coordinates, x_coordinates, t) diff = norm2D(x_coordinates, x_coordinates, f - t) plotSurface(x_coordinates, x_coordinates, f) plotSurface(x_coordinates, x_coordinates, t) self.assertLess(diff, 1e-10) # Test along x slices for x in x_coordinates: for i_y_1, y_1 in enumerate(y_coordinates): for i_y_2, y_2 in enumerate(y_coordinates): r_1 = np.array([x, y_1 + y_2]) r_2 = np.array([x, y_1 - y_2]) f[i_y_1, i_y_2] = af.evaluate(r_1, r_2) t[i_y_1, i_y_2] = tester_x.evaluateAnalytical( r_1[0], r_2[0]) * tester_y.evaluateAnalytical( r_1[1], r_2[1]) f /= norm2D(y_coordinates, y_coordinates, f) t /= norm2D(y_coordinates, y_coordinates, t) diff = norm2D(y_coordinates, y_coordinates, f - t) print(diff) self.assertLess(diff, 1e-10)
def testGaussianSchellModel(self): n_points = 61 x_coordinates = np.linspace(-10, 10, n_points) y_coordinates = np.linspace(-10, 10, n_points) eigenmoder = Eigenmoder(x_coordinates, y_coordinates) gsm = TracedGSM(A=1, sigma_s_x=0.9, sigma_g_x=1.0, sigma_s_y=1.5, sigma_g_y=1.5, x_coordinates=x_coordinates, y_coordinates=y_coordinates) eigenvalues_x = np.array( [gsm._mode_x.beta(i) for i in range(n_points)]) eigenvalues_y = np.array( [gsm._mode_y.beta(i) for i in range(n_points)]) i_c = 0 array_size = eigenvalues_x.shape[0] * eigenvalues_y.shape[0] f = np.zeros(array_size, dtype=np.complex128) ind = np.zeros((array_size, 2), dtype=np.int) for i_x, e_x in enumerate(eigenvalues_x): for i_y, e_y in enumerate(eigenvalues_y): f[i_c] = e_x * e_y ind[i_c, :] = (i_x, i_y) i_c += 1 ind_sort = f.argsort()[::-1] builder = MatrixBuilder(x_coordinates, y_coordinates) builder._mode_element_wise = True #work_matrix = builder._createParallelMatrix(gsm.evaluate_r1) work_matrix = builder._createParallelMatrix(gsm.evaluate) twoform = eigenmoder.eigenmodes(work_matrix) #twoform.save("gsm2dtest.npz") x_index = np.abs(x_coordinates).argmin() y_index = np.abs(y_coordinates).argmin() for i_e, eigenmode in enumerate(twoform.eigenvectors()): # t = np.zeros_like(eigenmode) # t[:-1, :-1] = eigenmode[1:, 1:] # eigenmode = t n, m = ind[ind_sort[i_e], :] mode = gsm.phi_nm(n, m, x_coordinates, y_coordinates) norm_mode = norm2D(x_coordinates, y_coordinates, mode) norm_eigenmode = norm2D(x_coordinates, y_coordinates, eigenmode) print(norm_mode, norm_eigenmode) norm_mode = mode.max() mode /= norm_mode normed_eigenmode = eigenmode / eigenmode.max() #norm_eigenmode diff_norm_eig = np.sum(np.abs(mode - normed_eigenmode)) diff_norm_neg = np.sum(np.abs(-1 * mode - normed_eigenmode)) if diff_norm_eig > diff_norm_neg: normed_eigenmode *= -1 normed_eigenmode /= np.abs(normed_eigenmode).max() mode /= np.abs(mode).max() # import matplotlib.pyplot as plt # plt.plot(x_coordinates, mode[:, y_index]) # plt.plot(x_coordinates, normed_eigenmode[:, y_index]) # plt.show() # plt.plot(y_coordinates, mode[x_index, :]) # plt.plot(y_coordinates, normed_eigenmode[x_index, :]) # plt.show() # from comsyl.math.utils import plotSurface # plotSurface(x_coordinates, y_coordinates, mode) # plotSurface(x_coordinates, y_coordinates, normed_eigenmode) # plotSurface(x_coordinates, y_coordinates, mode - normed_eigenmode) diff_norm = norm2D(x_coordinates, y_coordinates, normed_eigenmode - mode) print( "%i: diff norm (n:%i, m: %i); beta %e %e" % (i_e, n, m, gsm.beta(n, m), twoform.eigenvalues()[i_e]), diff_norm) if i_e < 50: #self.assertLess(diff_norm, 1e-8) if diff_norm > 1e-8: from comsyl.math.utils import plotSurface plotSurface(x_coordinates, y_coordinates, mode) plotSurface(x_coordinates, y_coordinates, normed_eigenmode) plotSurface(x_coordinates, y_coordinates, mode - normed_eigenmode) import matplotlib.pyplot as plt plt.plot(x_coordinates, mode[:, y_index]) plt.plot(x_coordinates, normed_eigenmode[:, y_index]) plt.show() plt.plot(y_coordinates, mode[x_index, :]) plt.plot(y_coordinates, normed_eigenmode[x_index, :]) plt.show()