def Example_list(): from pySRU.ElectronBeam import ElectronBeam beam_ESRF = ElectronBeam(Electron_energy=6.0, I_current=0.2) ESRF18 = Undulator(K=1.68, period_length=0.018, length=2.0) X = np.linspace(0.0, 0.0002, 1001) Y = np.linspace(0.0, 0.0002, 1001) simulation_test = create_simulation(magnetic_structure=ESRF18, electron_beam=beam_ESRF, X=X, Y=Y, XY_are_list=True) simulation_test.print_parameters() simulation_test.trajectory.plot_3D() simulation_test.radiation.plot() simulation_test.calculate_on_ring_number(ring_number_max=2) simulation_test.radiation.plot() simulation_test.radiation.plot_ring() observation_angle = np.linspace( 0.0, simulation_test.source.angle_ring_number(1, 2), 51) # TODO verifier quelle marche bien simulation_test.calculate_for_observation_angles( XY_are_list=True, observation_angle=observation_angle) simulation_test.radiation.plot()
def test_create_trajectory(self): # test le trajectoire ana lytic (des valeurs special # le Beta doit est constant pour certaine methode # les produi scalaire de l'acc avec la vitesse est ... # difference max entre deux trajectoire K = 1.87 lambda_u = 0.035 L = 0.035 * 14 E = 1.3 I = 1.0 undulator = Undulator(K=K, period_length=lambda_u, length=L) beam = ElectronBeam(Electron_energy=E, I_current=I) source = SourceUndulatorPlane(undulator=undulator, electron_beam=beam) fact_test = TrajectoryFactory(Nb_pts=201, method=TRAJECTORY_METHOD_ODE) traj_test = fact_test.create_from_source(source=source) self.assertFalse(fact_test.initial_condition is None) self.assertTrue( all(fact_test.initial_condition == source.choose_initial_contidion_automatic())) self.assertTrue(fact_test.method == TRAJECTORY_METHOD_ODE) scalar_product = traj_test.v_x * traj_test.a_x + traj_test.a_z * traj_test.v_z self.assertAlmostEqual(np.abs(scalar_product).max(), 0.0, 3)
def test_magn_field(self): beam_test = ElectronBeam(Electron_energy=1.3, I_current=1.0) beam_ESRF = ElectronBeam(Electron_energy=6.0, I_current=0.2) und_test = Undulator(K=1.87, period_length=0.035, length=0.035 * 14) ESRF18 = Undulator(K=1.68, period_length=0.018, length=2.) self.create_magn_field_undulator_test( magnetic_structure=und_test, electron_beam=beam_test, method_traj=TRAJECTORY_METHOD_ODE) print("und_test, ok") self.create_magn_field_undulator_test( magnetic_structure=ESRF18, electron_beam=beam_ESRF, method_traj=TRAJECTORY_METHOD_ODE) print("esrf18, ok")
def Example_meshgrid(): from pySRU.ElectronBeam import ElectronBeam print( "======================================================================" ) print( "====== Undulator U18 from ESRF with K=1.68 =======" ) print( "======================================================================" ) beam_ESRF = ElectronBeam(Electron_energy=6.0, I_current=0.2) ESRF18 = Undulator(K=1.68, period_length=0.018, length=2.0) # # radiation in a defined meah # print('create radiation a given screen (40mm x 40mm @ 100 m )') X = np.linspace(-0.02, 0.02, 101) Y = np.linspace(-0.02, 0.02, 101) distance = 100.0 simulation_test = create_simulation( magnetic_structure=ESRF18, electron_beam=beam_ESRF, magnetic_field=None, photon_energy=None, traj_method=TRAJECTORY_METHOD_ANALYTIC, Nb_pts_trajectory=None, rad_method=RADIATION_METHOD_APPROX_FARFIELD, Nb_pts_radiation=101, initial_condition=None, distance=distance, XY_are_list=False, X=X, Y=Y) simulation_test.print_parameters() simulation_test.trajectory.plot_2D() simulation_test.radiation.plot( title=" radiation in a defined screen (40mm x 40mm @ 100 m )") # # up to a maximum X and Y # print('create simulation for a given maximum X and Y ') simulation_test = create_simulation( magnetic_structure=ESRF18, electron_beam=beam_ESRF, traj_method=TRAJECTORY_METHOD_ANALYTIC, rad_method=RADIATION_METHOD_APPROX_FARFIELD, distance=100, X=0.01, Y=0.01) simulation_test.radiation.plot( title='simulation for a maximum X=0.01 and Y=0.01')
def test_copy_undulator(self): K = 1.87 period_length = 0.035 length = 0.035 * 14 undulator = Undulator(K=K, period_length=period_length, length=length) undulator2 = undulator.copy() undulator2.K = 1.5 self.assertEqual(undulator.K, 1.87) self.assertEqual(undulator2.K, 1.5) self.assertEqual(undulator.period_number(), 14)
def Example_minimum(): from pySRU.ElectronBeam import ElectronBeam print( "======================================================================" ) print( "====== Undulator from X-ray data booklet =======" ) print( "====== fig 2.5 in http://xdb.lbl.gov/Section2/Sec_2-1.html =======" ) print( "======================================================================" ) # note that the flux in the reference fig 2.6 is a factor 10 smaller than the calculated here. # This factor comes from the units: # here: phot / s / A / 0.1%bw / (mrad)^2 # ref : phot / s / A / 1%bw / (0.1 mrad)^2 undulator_test = Undulator(K=1.87, period_length=0.035, length=0.035 * 14) electron_beam_test = ElectronBeam(Electron_energy=1.3, I_current=1.0) simulation_test = create_simulation( magnetic_structure=undulator_test, electron_beam=electron_beam_test, magnetic_field=None, photon_energy=None, traj_method=TRAJECTORY_METHOD_ANALYTIC, Nb_pts_trajectory=None, rad_method=RADIATION_METHOD_APPROX_FARFIELD, Nb_pts_radiation=101, initial_condition=None, distance=None, XY_are_list=False, X=None, Y=None) simulation_test.print_parameters() simulation_test.trajectory.plot_3D(title="Electron Trajectory") simulation_test.radiation.plot(title="Flux in far field vs angle") # second calculation, change distance and also change the grid to accept the central cone simulation_test.change_distance(D=100) simulation_test.calculate_on_central_cone() simulation_test.radiation.plot(title="New distance: %3.1f m" % simulation_test.radiation.distance)
def test_create_radiation_undulator(self): undulator_test = Undulator(K=1.87, period_length=0.035, length=0.035 * 14) electron_beam_test = ElectronBeam(Electron_energy=1.3, I_current=1.0) source_test = SourceUndulatorPlane(undulator=undulator_test, electron_beam=electron_beam_test) traj_fact = TrajectoryFactory(Nb_pts=1001, method=TRAJECTORY_METHOD_ANALYTIC) traj = traj_fact.create_from_source(source_test) rad_fact = RadiationFactory( photon_frequency=source_test.harmonic_frequency(1), method=RADIATION_METHOD_NEAR_FIELD) rad = rad_fact.create_for_one_relativistic_electron(trajectory=traj, source=source_test) self.assertFalse(rad.X is None) self.assertFalse(rad.Y is None) self.assertFalse(rad.distance is None) rad_fact.method = RADIATION_METHOD_APPROX_FARFIELD rad2 = rad_fact.create_for_one_relativistic_electron( trajectory=traj, source=source_test) self.assertTrue(rad2.distance == None) rad2 = rad_fact.create_for_one_relativistic_electron( trajectory=traj, source=source_test, distance=rad.distance) self.assertFalse(rad.distance == None) err = rad.difference_with(rad2) self.assertTrue(rad.XY_are_similar_to(rad2)) self.assertTrue(rad.XY_are_similar_to(err)) self.assertTrue(rad.distance == rad2.distance) self.assertTrue(err.distance == rad2.distance) self.assertGreaterEqual(err.intensity.min(), 0.0) self.assertLessEqual(err.max(), rad.max() * 1e-1) # srio changed 1e-3 by 1e-1 traj_test2 = TrajectoryFactory( Nb_pts=1001, method=TRAJECTORY_METHOD_ODE, initial_condition=traj_fact.initial_condition).create_from_source( source_test) rad3 = rad_fact.create_for_one_relativistic_electron( trajectory=traj_test2, source=source_test, distance=rad.distance) err = rad2.difference_with(rad3) self.assertLessEqual(err.max(), rad2.max() * 1e-3)
def Example_spectrum_on_central_cone(): from pySRU.ElectronBeam import ElectronBeam print( "======================================================================" ) print( "====== Undulator U18 from ESRF with K=1.68 =======" ) print( "======================================================================" ) beam_ESRF = ElectronBeam(Electron_energy=6.0, I_current=0.2) ESRF18 = Undulator(K=1.68, period_length=0.018, length=2.0) simulation_test = create_simulation( magnetic_structure=ESRF18, electron_beam=beam_ESRF, magnetic_field=None, photon_energy=None, traj_method=TRAJECTORY_METHOD_ANALYTIC, Nb_pts_trajectory=None, rad_method=RADIATION_METHOD_APPROX_FARFIELD, Nb_pts_radiation=101, initial_condition=None, distance=None, XY_are_list=False, X=np.array([0]), Y=np.array([0])) harmonic_number = 1 x, y = simulation_test.calculate_spectrum_central_cone( harmonic_number=harmonic_number, theoretical_value=True, abscissas_array=None, use_eV=1) # dump file filename = "spectrum.spec" f = open(filename, 'w') f.write("#F %s\n\n#S 1 undulator flux using pySRU\n" % filename) f.write( "#N 2\n#L Photon energy [eV] Flux on central cone for harmonic %d [phot/s/0.1bw]\n" % harmonic_number) for i, xi in enumerate(x): f.write("%f %g\n" % (xi, y[i])) f.close() print("File written to disk: %s" % filename)
def test_create_magnetic_field(self): K = 1.87 lambda_u = 0.035 L = 0.035 * 12 undulator = Undulator(K=K, period_length=lambda_u, length=L) Z = np.linspace(-1., 1., 101) Y = 0.0 X = 0.0 B = undulator.create_magnetic_field(harmonic_number=1) By = B.By(Z, Y, X) self.assertTrue(By.shape == ((101, ))) Y = Z By = B.By(Z, Y, X) self.assertEqual(By.shape, ((101, )))
def Exemple_FARFIELD(): from pySRU.MagneticStructureUndulatorPlane import MagneticStructureUndulatorPlane as Undulator from pySRU.ElectronBeam import ElectronBeam from pySRU.SourceUndulatorPlane import SourceUndulatorPlane from pySRU.TrajectoryFactory import TrajectoryFactory,TRAJECTORY_METHOD_ODE,TRAJECTORY_METHOD_ANALYTIC import time undulator_test = Undulator(K=1.87, period_length=0.035, length=0.035 * 14) electron_beam_test = ElectronBeam(Electron_energy=1.3, I_current=1.0) magnetic_field_test = undulator_test.create_magnetic_field() magnetic_field_test.plot_z(0,0,np.linspace(-0.035 * (14+8) / 2,0.035 * (14+8) / 2,500)) source_test = SourceUndulatorPlane(undulator=undulator_test, electron_beam=electron_beam_test, magnetic_field=magnetic_field_test) traj = TrajectoryFactory(Nb_pts=2000, method=TRAJECTORY_METHOD_ODE).create_from_source(source_test) t0 = time.time() Rad = RadiationFactory(photon_frequency=source_test.harmonic_frequency(1), method=RADIATION_METHOD_APPROX_FARFIELD, Nb_pts=101 ).create_for_one_relativistic_electron(trajectory=traj, source=source_test) print("Elapsed time in RadiationFactory: ",time.time()-t0) print('Screen distance :') print(Rad.distance) print("screen shape ") print(Rad.intensity.shape) print('X max :') print(Rad.X.max()) print('Y max :') print(Rad.Y.max()) print('intensity max ()') print(Rad.max()) print('plot') Rad.plot(title="FAR FIELD")
def Example_spectrum_on_axis(): from pySRU.ElectronBeam import ElectronBeam print( "======================================================================" ) print( "====== Undulator U18 from ESRF with K=1.68 =======" ) print( "======================================================================" ) beam_ESRF = ElectronBeam(Electron_energy=6.0, I_current=0.2) ESRF18 = Undulator(K=1.68, period_length=0.018, length=2.0) # note that distance=None is important to get results in angles and therefore flux in ph/s/0.1%bw/mrad2 simulation_test = create_simulation( magnetic_structure=ESRF18, electron_beam=beam_ESRF, magnetic_field=None, photon_energy=None, traj_method=TRAJECTORY_METHOD_ANALYTIC, Nb_pts_trajectory=None, rad_method=RADIATION_METHOD_APPROX_FARFIELD, Nb_pts_radiation=101, initial_condition=None, distance=None, XY_are_list=False, X=np.array([0]), Y=np.array([0])) x, y = simulation_test.calculate_spectrum_on_axis(use_eV=1, do_plot=1) # dump file filename = "spectrum.spec" f = open(filename, 'w') f.write("#F %s\n\n#S 1 undulator flux using pySRU\n" % filename) f.write( "#N 2\n#L Photon energy [eV] Flux on axis [phot/s/0.1%bw/mrad2]\n") for i in range(x.size): f.write("%f %g\n" % (x[i], y[i])) f.close() print("File written to disk: %s" % filename)
def test_copy(self): K = 1.87 lambda_u = 0.035 L = 0.035 * 14 E = 1.3 I = 1.0 undulator = Undulator(K=K, period_length=lambda_u, length=L) beam = ElectronBeam(Electron_energy=E, I_current=I) source = SourceUndulatorPlane(undulator=undulator, electron_beam=beam) source2 = source.copy() source2.electron_beam.I_current = 0.3 self.assertEqual(source.I_current(), 1.0) self.assertEqual(source2.I_current(), 0.3) self.assertAlmostEqual( source.harmonic_frequency(1) / 1e17, 2.5346701615509917, 5) self.assertAlmostEqual(source.Lorentz_factor(), 2544.0367765521196, 2) self.assertAlmostEqual(source.electron_speed(), 0.99999992274559524, 5) self.assertEqual(source.magnetic_structure.period_number(), 14) self.assertAlmostEqual(source.choose_distance_automatic(2), 49.000000000000, 10)
def Example_meshgrid_on_central_cone_and_rings(): from pySRU.ElectronBeam import ElectronBeam print( "======================================================================" ) print( "====== Undulator U18 from ESRF with K=1.68 =======" ) print( "======================================================================" ) beam_ESRF = ElectronBeam(Electron_energy=6.0, I_current=0.2) ESRF18 = Undulator(K=1.68, period_length=0.018, length=2.0) # # radiation in a defined mesh with only one point to save time # distance = None simulation_test = create_simulation( magnetic_structure=ESRF18, electron_beam=beam_ESRF, magnetic_field=None, photon_energy=None, traj_method=TRAJECTORY_METHOD_ANALYTIC, Nb_pts_trajectory=None, rad_method=RADIATION_METHOD_APPROX_FARFIELD, Nb_pts_radiation=101, initial_condition=None, distance=distance, XY_are_list=False, X=np.array([0]), Y=np.array([0])) # # central cone # harmonic_number = 1 print( 'create radiation in a screen including central cone for harmonic %d' % harmonic_number) simulation_test.calculate_on_central_cone(harmonic_number=harmonic_number, npoints_x=50, npoints_y=50) simulation_test.print_parameters() simulation_test.radiation.plot(title=( "radiation in a screen including central cone, harmonic %d,at D=" + repr(distance)) % harmonic_number) # # up to a given ring # harmonic_number = 1 ring_number = 1 print('create radiation a given screen including ring %d for harmonic %d' % (ring_number, harmonic_number)) simulation_test.calculate_until_ring_number( harmonic_number=harmonic_number, ring_number=ring_number, XY_are_list=False, npoints=51) simulation_test.print_parameters() simulation_test.radiation.plot( title=" radiation in a screen containing harmonic %d up to ring %d ring" % (harmonic_number, ring_number))
method = ' Trajectory from integration on the magnetic field' return method def print_parameters(self): print("Trajectory ") print(' method : %s' % self.get_method()) print(' number of points : %d' % self.Nb_pts) print(' initial position (x,y,z) : ') print(self.initial_condition[3:6]) print(' initial velocity (x,y,z) ') print(self.initial_condition[0:3]) if __name__ == "__main__": from SourceUndulatorPlane import SourceUndulatorPlane undulator_test = Undulator(K=1.87, period_length=0.035, length=0.035 * 14) electron_beam_test = ElectronBeam(Electron_energy=1.3e9, I_current=1.0) source_test = SourceUndulatorPlane(undulator=undulator_test, electron_beam=electron_beam_test) print( 'Create trajectory with autamatic choice of initial condition and automatic magnetic field' ) trajectory_fact_ODE = TrajectoryFactory(Nb_pts=20000, method=TRAJECTORY_METHOD_ODE) trajectory1 = trajectory_fact_ODE.create_from_source(source_test) print(' ') print('trajectory 1 created with ODE method') print(trajectory_fact_ODE.print_parameters())
if __name__ == "__main__": from srxraylib.plot.gol import set_qt set_qt() print("======================================================================") print("====== Undulator U18 from ESRF with K=1.68 =======") print("======================================================================") beam_ALSU = ElectronBeam(Electron_energy=2.0, I_current=0.5) # ESRF18 = Undulator(K=1.68, period_length=0.018, length=2.0) id = Undulator(K=2.0, period_length=0.4, length=1.6) # # radiation in a defined mesh with only one point to save time # distance = None simulation_test = create_simulation(magnetic_structure=id, electron_beam=beam_ALSU, magnetic_field=None, photon_energy=None, traj_method=TRAJECTORY_METHOD_ANALYTIC, Nb_pts_trajectory=None, rad_method=RADIATION_METHOD_APPROX_FARFIELD, Nb_pts_radiation=101, initial_condition=None, distance=distance,
from pySRU.Source import Source from pySRU.Simulation import Simulation ,create_simulation from pySRU.TrajectoryFactory import TRAJECTORY_METHOD_ANALYTIC,TRAJECTORY_METHOD_ODE from pySRU.RadiationFactory import RADIATION_METHOD_NEAR_FIELD, RADIATION_METHOD_APPROX_FARFIELD,RADIATION_METHOD_APPROX eV_to_J=1.602176487e-19 ###################################################### #BM_test=MagneticStructureBendingMagnet(Bo=0.8, div=5e-3, R=25.0) E_1=7876.0 beam_test=ElectronBeam(Electron_energy=1.3, I_current=1.0) beam_ESRF=ElectronBeam(Electron_energy=6.0, I_current=0.2) und_test=Undulator( K = 1.87, period_length= 0.035, length=0.035 * 14) ESRF18=Undulator( K = 1.68, period_length = 0.018, length=2.0) ESRFBM=BM(Bo=0.8,horizontale_divergeance=0.005,electron_energy=6.0) vx= 2e-4 vz= np.sqrt(beam_test.electron_speed()**2-vx**2)*codata.c # initial_cond=np.array([ vx*codata.c, 0.00000000e+00 ,vz , 0.0 , 0.0 ,-0.42,]) #X=np.linspace(-0.02,0.02,150) #Y=np.linspace(-0.02,0.02,150) sim_test = create_simulation(magnetic_structure=und_test, electron_beam=beam_test, traj_method=TRAJECTORY_METHOD_ANALYTIC, rad_method=RADIATION_METHOD_APPROX_FARFIELD, Nb_pts_trajectory=1000,distance=100) sim_test.calculate_on_central_cone() sim_test.change_energy_eV(E=2000)
# # simulation_test.radiation.plot(title="Flux in far field vs angle") print( "======================================================================" ) print( "====== Undulator U18 from ESRF with K=1.68 =======" ) print( "======================================================================" ) beam_ESRF = ElectronBeam(Electron_energy=6.0, I_current=0.2) # ESRF18 = Undulator(K=1.68, period_length=0.018, length=2.0) ESRF18 = Undulator(K=1.0, period_length=0.1, length=1.0) # # radiation in a defined mesh with only one point to save time # distance = None simulation_test = create_simulation( magnetic_structure=ESRF18, electron_beam=beam_ESRF, magnetic_field=None, photon_energy=None, traj_method=TRAJECTORY_METHOD_ANALYTIC, Nb_pts_trajectory=None, rad_method=RADIATION_METHOD_APPROX_FARFIELD, Nb_pts_radiation=101,
def Example_spectrum_on_slit(): from pySRU.ElectronBeam import ElectronBeam print( "======================================================================" ) print( "====== Undulator U18 from ESRF with K=1.68 =======" ) print( "======================================================================" ) beam_ESRF = ElectronBeam(Electron_energy=6.0, I_current=0.2) ESRF18 = Undulator(K=1.68, period_length=0.018, length=2.0) print( 'create radiation a given screen (.025 x .025 mrad) accepting central cone' ) is_quadrant = 1 distance = None # using angles if is_quadrant: X = np.linspace(0, 0.0125e-3, 26) Y = np.linspace(0, 0.0125e-3, 26) else: X = np.linspace(-0.0125e-3, 0.0125e-3, 51) Y = np.linspace(-0.0125e-3, 0.0125e-3, 51) if distance != None: X *= distance Y *= distance simulation_test = create_simulation( magnetic_structure=ESRF18, electron_beam=beam_ESRF, magnetic_field=None, photon_energy=None, traj_method=TRAJECTORY_METHOD_ANALYTIC, Nb_pts_trajectory=None, rad_method=RADIATION_METHOD_APPROX_FARFIELD, Nb_pts_radiation=101, initial_condition=None, distance=distance, XY_are_list=False, X=X, Y=Y) simulation_test.print_parameters() simulation_test.radiation.plot( title=("radiation in a screen for first harmonic")) print("Integrated flux at resonance: %g photons/s/0.1bw" % (simulation_test.radiation.integration(is_quadrant=is_quadrant))) x, y = simulation_test.calculate_spectrum_on_slit(abscissas_array=None, use_eV=1, is_quadrant=is_quadrant) # dump file filename = "spectrum.spec" f = open(filename, 'w') f.write("#F %s\n\n#S 1 undulator flux using pySRU\n" % filename) f.write("#N 2\n#L Photon energy [eV] Flux on slit [phot/s/0.1%bw]\n") for i in range(x.size): f.write("%f %g\n" % (x[i], y[i])) f.close() print("File written to disk: %s" % filename)
def test_main(self): beam_test = ElectronBeam(Electron_energy=1.3, I_current=1.0) beam_ESRF = ElectronBeam(Electron_energy=6.0, I_current=0.2) und_test = Undulator(K=1.87, period_length=0.035, length=0.035 * 14) ESRF18 = Undulator(K=1.68, period_length=0.018, length=2.0) ##ESRFBM = BM(E=6.0e9, Bo=0.8, div=5e-3, R=25.0, I=0.2) self.simul_undulator_theoric( magnetic_struc=und_test, electron_beam=beam_test, method_rad=RADIATION_METHOD_APPROX_FARFIELD, method_traj=TRAJECTORY_METHOD_ODE) print("Intensity ok") print(' undulator test ') self.simul_undulator_near_to_farfield( magnetic_struc=und_test, electron_beam=beam_test, method_traj=TRAJECTORY_METHOD_ANALYTIC) print("TRAJECTORY_METHOD_ANALYTIC ok") self.simul_undulator_near_to_farfield( magnetic_struc=und_test, electron_beam=beam_test, method_traj=TRAJECTORY_METHOD_ODE) print("TRAJECTORY_METHOD_ODE ok") self.simul_undulator_traj_method( magnetic_struc=und_test, electron_beam=beam_test, method_rad=RADIATION_METHOD_APPROX_FARFIELD) print('APPROX FARFIELD ok') self.simul_undulator_traj_method( magnetic_struc=und_test, electron_beam=beam_test, method_rad=RADIATION_METHOD_NEAR_FIELD) print('NEAR FIELD ok') print(' ') print('undulator ESRF18') self.simul_undulator_near_to_farfield( magnetic_struc=ESRF18, electron_beam=beam_ESRF, method_traj=TRAJECTORY_METHOD_ANALYTIC) print("TRAJECTORY_METHOD_ANALYTIC ok") self.simul_undulator_near_to_farfield( magnetic_struc=ESRF18, electron_beam=beam_ESRF, method_traj=TRAJECTORY_METHOD_ODE) print("TRAJECTORY_METHOD_ODE ok") #TODO marche pas ? self.simul_undulator_traj_method( magnetic_struc=ESRF18, electron_beam=beam_ESRF, method_rad=RADIATION_METHOD_APPROX_FARFIELD) print('APPROX FARFIELD ok') self.simul_undulator_traj_method( magnetic_struc=ESRF18, electron_beam=beam_ESRF, method_rad=RADIATION_METHOD_NEAR_FIELD) print('NEAR FIELD ok')