def get_beam(self, N=5000, POL_DEG=1.0, POL_ANGLE=0.0, F_COHER=False): rays = self.calculate_rays(N=N, POL_DEG=POL_DEG, POL_ANGLE=POL_ANGLE, F_COHER=F_COHER) return Beam.initialize_from_array(rays)
def minishadow_run_mesh_mirror(): # ; # ; ray tracing of a surface defined with a mesh using minishadow # ; results are compared with shadow3 # ; # ; # ; Runs shadow3 # ; shadow3_beam_source,shadow3_beam,oe0,oe1 = run_shadow3_from_start_files(iwrite=1) # copy source to new Beam object newbeam = Beam.initialize_from_array(shadow3_beam_source.rays.copy()) # ; # ; INPUTS # ; p = oe1.T_SOURCE # 1000.0 # source-mirror q = oe1.T_IMAGE # 300.0 # mirror-image alpha = oe1.ALPHA # 0.0 # mirror orientation angle theta_grazing = (90.0-oe1.T_INCIDENCE) * numpy.pi / 180 # 5e-3 # grazing angle, rad print("p=%f, q=%f, alpha=%f, theta_grazing=%f rad"%(p,q,alpha,theta_grazing)) mm = S4Mesh() mm.load_file("bump.dat") newbeam.rotate(alpha,axis=2) newbeam.rotate(theta_grazing,axis=1) newbeam.translation([0.0,-p*numpy.cos(theta_grazing),p*numpy.sin(theta_grazing)]) # # reflect beam in the mirror surface and dump mirr.01 # newbeam, normal, t, x1, v1, x2, v2 = mm.apply_specular_reflection_on_beam(newbeam) from shadow4tests.compatibility.beam3 import Beam3 Beam3.initialize_from_shadow4_beam(newbeam).write('minimirr.01') # # put beam in lab frame and compute image # newbeam.rotate(theta_grazing,axis=1) # TODO what about alpha? newbeam.retrace(q,resetY=True) Beam3.initialize_from_shadow4_beam(newbeam).write('ministar.01')
def get_beam(self, NRAYS=5000, SEED=123456): user_unit_to_m = 1.0 F_COHER = 0 # use_gaussian_approximation = False if self.get_magnetic_structure().use_gaussian_approximation(): return self.get_beam_in_gaussian_approximation(NRAYS=NRAYS, SEED=SEED) else: return Beam.initialize_from_array( self.__calculate_rays(user_unit_to_m=user_unit_to_m, F_COHER=F_COHER, NRAYS=NRAYS, SEED=SEED))
def get_beam(self,wavelength=1e-10): """ Returns a Beam :param wavelength: the photon wavelength in m :return: """ rays = numpy.zeros((self.get_number_of_points(),18)) rays[:,0:6] = self.get_volume() rays[:,6] = 1.0 # Es rays[:,9] = 1 # flag rays[:,10] = 2 * numpy.pi / (wavelength * 1e2) # wavenumber rays[:,11] = numpy.arange(self.get_number_of_points(),dtype=float) # index return Beam.initialize_from_array(rays)
def get_beam(self, F_COHER=0, NRAYS=5000, SEED=123456, EPSI_DX=0.0, EPSI_DZ=0.0, psi_interval_in_units_one_over_gamma=None, psi_interval_number_of_points=1001, verbose=False): return Beam.initialize_from_array(self.__calculate_rays(F_COHER=F_COHER, NRAYS=NRAYS, SEED=SEED, EPSI_DX=EPSI_DX, EPSI_DZ=EPSI_DZ, psi_interval_in_units_one_over_gamma=psi_interval_in_units_one_over_gamma, psi_interval_number_of_points=psi_interval_number_of_points, verbose=verbose))
def tests_initializars(self): print("# ") print("# initializers ") print("# ") a = Beam(N=100) self.assertEqual(100,a.get_number_of_rays()) a = Beam(array=numpy.zeros( (1000,18) )) print(a.info()) print(a.info()) self.assertEqual(1000,a.get_number_of_rays()) a = Beam.initialize_from_array(numpy.zeros( (500,18) )) print(a.info()) print(a.info()) self.assertEqual(500,a.get_number_of_rays()) a = Beam.initialize_as_pencil(200) print(a.info()) self.assertEqual(200,a.get_number_of_rays())
def get_beam(self, NRAYS=5000, SEED=123456): user_unit_to_m = 1.0 F_COHER = 0 EPSI_DX = 0.0 EPSI_DZ = 0.0 psi_interval_in_units_one_over_gamma = None psi_interval_number_of_points = 1001 verbose = True return Beam.initialize_from_array( self.__calculate_rays( user_unit_to_m=user_unit_to_m, F_COHER=F_COHER, NRAYS=NRAYS, SEED=SEED, EPSI_DX=EPSI_DX, EPSI_DZ=EPSI_DZ, psi_interval_in_units_one_over_gamma= psi_interval_in_units_one_over_gamma, psi_interval_number_of_points=psi_interval_number_of_points, verbose=verbose))
def run_hyperboloid(kind="hyperboloid"): # # shadow3 # if kind == "hyperboloid": from shadow4tests.oasys_workspaces.mirrors_branch5_hyperboloid import define_source, run_source, define_beamline, run_beamline else: raise Exception("Bad input") oe0 = define_source() beam3_source = run_source(oe0) # # shadow4 # from shadow4.syned.element_coordinates import ElementCoordinates oe = define_beamline()[0] beam4_source = Beam.initialize_from_array(beam3_source.rays) beam4 = beam4_source check_congruence(oe) # # shadow definitions # if oe.F_DEFAULT == 0: p_focus = oe.SSOUR q_focus = oe.SIMAG grazing_angle = numpy.radians(90 - oe.THETA) elif oe.F_DEFAULT == 1: p_focus = oe.T_SOURCE q_focus = oe.T_IMAGE grazing_angle = numpy.radians(90 - oe.T_INCIDENCE) is_cylinder = oe.FCYL if oe.CIL_ANG == 0: cylinder_direction = Direction.TANGENTIAL else: cylinder_direction = Direction.SAGITTAL if oe.F_CONVEX == 0: convexity = Convexity.UPWARD elif oe.F_CONVEX == 1: convexity = Convexity.DOWNWARD name = "Hyperboloid Mirror (%s) " % kind mirror1 = S4HyperboloidMirrorElement( optical_element=S4HyperboloidMirror( name=name, boundary_shape=None, surface_calculation=SurfaceCalculation.INTERNAL, is_cylinder=is_cylinder, cylinder_direction=cylinder_direction, convexity=convexity, p_focus=p_focus, q_focus=q_focus, grazing_angle=grazing_angle, # inputs related to mirror reflectivity f_reflec=oe. F_REFLEC, # reflectivity of surface: 0=no reflectivity, 1=full polarization # f_refl=0, # 0=prerefl file, 1=electric susceptibility, 2=user defined file (1D reflectivity vs angle) # # 3=user defined file (1D reflectivity vs energy), # 4=user defined file (2D reflectivity vs energy and angle) # file_refl="", # preprocessor file fir f_refl=0,2,3,4 # refraction_index=1.0 # refraction index (complex) for f_refl=1 ), coordinates=ElementCoordinates( p=oe.T_SOURCE, q=oe.T_IMAGE, angle_radial=numpy.radians(oe.T_INCIDENCE), ), ) print(mirror1.info()) # # run # beam4, mirr4 = mirror1.trace_beam(beam_in=beam4, flag_lost_value=-11000) # # compare # oe_list = define_beamline() beam3 = run_beamline(beam3_source, oe_list) plotxy(beam3, 1, 3, nbins=201, nolost=1, title="%s shadow3" % name) plotxy(beam4, 1, 3, nbins=201, nolost=1, title="%s shadow4" % name) from shadow4tests.compatibility.compare_beams import check_six_columns_mean_and_std, check_almost_equal check_six_columns_mean_and_std(beam3, beam4, do_assert=True, do_plot=False, assert_value=1e-6) check_almost_equal(beam3, beam4, do_assert=False, level=3)
# # shadow3 # from shadow4tests.oasys_workspaces.mirrors_branch3_mesh import define_source, run_source, define_beamline, run_beamline oe0 = define_source() beam3_source = run_source(oe0) # # shadow4 # oe = define_beamline()[0] beam4_source = Beam.initialize_from_array(beam3_source.rays) beam4 = beam4_source check_congruence(oe) # # shadow definitions # name = "SurfaceDataMirror" mirror1 = S4SurfaceDataMirrorElement( optical_element=S4SurfaceDataMirror( name=name, surface_data_file="../oasys_workspaces/mirrors_branch3_mesh.hdf5", boundary_shape=Rectangle( x_left=-oe.RWIDX2,
def minishadow_run_conic_mirror(shadow3_beam_source, shadow3_beam, oe0, oe1): # ; # ; ray tracing of a single conic mirror using minishadow # ; results are compared with shadow3 # ; # # copy source to new Beam object newbeam = Beam.initialize_from_array(shadow3_beam_source.rays.copy()) # ; # ; INPUTS # ; # fmirr = oe1.FMIRR # 1 p = oe1.T_SOURCE # 1000.0 # source-mirror q = oe1.T_IMAGE # 300.0 # mirror-image alpha = oe1.ALPHA # 0.0 # mirror orientation angle theta_grazing = (90.0 - oe1.T_INCIDENCE ) * numpy.pi / 180 # 5e-3 # grazing angle, rad fcyl = oe1.FCYL f_convex = oe1.F_CONVEX print("fmirr = %s, p=%f, q=%f, alpha=%f, theta_grazing=%f rad, fcyl=%d"%\ (fmirr,p,q,alpha,theta_grazing,fcyl)) # ccc = SurfaceConic() # ccc.set_sphere_from_focal_distances(p,q,theta_grazing,itype=fmirr,cylindrical=fcyl) if fmirr == 1: # sphere ccc = S4Conic.initialize_as_sphere_from_focal_distances( p, q, theta_grazing, cylindrical=fcyl, switch_convexity=f_convex) elif fmirr == 2: # Ellipsoid ccc = S4Conic.initialize_as_ellipsoid_from_focal_distances( p, q, theta_grazing, cylindrical=fcyl, switch_convexity=f_convex) elif fmirr == 3: # Toroidal raise Exception("fmirr=3 (toroidal) is NOT A CONIC surface") elif fmirr == 4: # Paraboloid ccc = S4Conic.initialize_as_paraboloid_from_focal_distances( p, q, theta_grazing, cylindrical=fcyl, switch_convexity=f_convex) elif fmirr == 5: ccc = S4Conic.initialize_as_plane() elif fmirr == 6: # Codling slit raise Exception("fmirr=6 (Codling slit) is NOT A CONIC surface") elif fmirr == 7: # Hyperboloid ccc = S4Conic.initialize_as_hyperboloid_from_focal_distances( p, q, theta_grazing, cylindrical=fcyl, switch_convexity=f_convex) elif fmirr == 8: # Cone raise Exception("fmirr=8 (Cone) is NOT A CONIC surface") else: raise Exception("fmirr invalid") print(ccc.info()) # # put beam in mirror reference system # # TODO: calculate rotation matrices? Invert them for putting back to the lab system? newbeam.rotate(alpha, axis=2) newbeam.rotate(theta_grazing, axis=1) newbeam.translation( [0.0, -p * numpy.cos(theta_grazing), p * numpy.sin(theta_grazing)]) # # # newbeam.rotate(alpha,axis=2) # # newbeam.rotate(theta_grazing,axis=1) # # newbeam.translation([0.0,-p*numpy.cos(theta_grazing),p*numpy.sin(theta_grazing)]) # # # # reflect beam in the mirror surface and dump mirr.01 # newbeam, tmp = ccc.apply_specular_reflection_on_beam(newbeam) Beam3.initialize_from_shadow4_beam(newbeam).write('minimirr.01') # # put beam in lab frame and compute image # newbeam.rotate(theta_grazing, axis=1) # TODO what about alpha? newbeam.retrace(q, resetY=True) Beam3.initialize_from_shadow4_beam(newbeam).write('ministar.01')
# patches = MultiplePatch() # patches.append_polygon([-1,-1,1,1],[-1,1,1,-1]) source3, beam3 = run_shadow3() # if do_plot: plotxy(beam3, 1, 3, nbins=100, title="SHADOW3 BEAMSTOPPER", nolost=True) beam = Beam.initialize_from_array(source3.rays) scr = S4Screen( name="Undefined", boundary_shape=patches, i_abs=False, # include absorption i_stop=True, # aperture/stop thick=0.0, # thickness of the absorber (in SI) file_abs="", # if i_abs=True, the material file (from prerefl) ) coordinates = ElementCoordinates(p=322.971 * 1e-2, q=5.0 * 1e-2) slit1 = S4ScreenElement(optical_element=scr, coordinates=coordinates) print(slit1.info())
def minishadow_run_toroid_mirror(): # ; # ; ray tracing of a single conic mirror using minishadow # ; results are compared with shadow3 # ; # # ; # ; Runs shadow3 # shadow3_beam_source,shadow3_beam,oe0,oe1 = run_shadow3_from_start_files(iwrite=1) # copy source to new Beam object newbeam = Beam.initialize_from_array(shadow3_beam_source.rays.copy()) # ; # ; INPUTS # ; # fmirr = oe1.FMIRR # 1 p = oe1.T_SOURCE # 1000.0 # source-mirror q = oe1.T_IMAGE # 300.0 # mirror-image alpha = oe1.ALPHA # 0.0 # mirror orientation angle theta_grazing = (90.0-oe1.T_INCIDENCE) * numpy.pi / 180 # 5e-3 # grazing angle, rad fcyl = oe1.FCYL f_convex = oe1.F_CONVEX print("fmirr = %s, p=%f, q=%f, alpha=%f, theta_grazing=%f rad, fcyl=%d"%\ (fmirr,p,q,alpha,theta_grazing,fcyl)) t = S4Toroid() t.set_from_focal_distances(p, q, theta_grazing) print(t.info()) # # put beam in mirror reference system # # TODO: calculate rotation matrices? Invert them for putting back to the lab system? # # THIS PART IS NOT DONE FOR TOROIDS!!!!!!!! newbeam.rotate(alpha,axis=2) newbeam.rotate(theta_grazing,axis=1) newbeam.translation([0.0,-p*numpy.cos(theta_grazing),p*numpy.sin(theta_grazing)]) # # # # # reflect beam in the mirror surface and dump mirr.01 # # newbeam, tmp = t.apply_specular_reflection_on_beam(newbeam) # newbeam.dump_shadow3_file('minimirr.01') Beam3.initialize_from_shadow4_beam(newbeam).write('minimirr.01') # # # # # put beam in lab frame and compute image # # newbeam.rotate(theta_grazing,axis=1) # TODO what about alpha? newbeam.retrace(q,resetY=True) # newbeam.dump_shadow3_file('ministar.01') Beam3.initialize_from_shadow4_beam(newbeam).write('ministar.01')
def run_conic(kind="conic"): # # shadow3 # if kind == "conic": from shadow4tests.oasys_workspaces.mirrors_branch5_conic import define_source, run_source, define_beamline, run_beamline else: raise Exception("Bad input") oe0 = define_source() beam3_source = run_source(oe0) # # shadow4 # from shadow4.syned.element_coordinates import ElementCoordinates oe = define_beamline()[0] beam4_source = Beam.initialize_from_array(beam3_source.rays) beam4 = beam4_source check_congruence(oe) # # shadow definitions # name = "Conic Mirror (%s) " % kind mirror1 = S4ConicMirrorElement( optical_element=S4ConicMirror( name=name, boundary_shape=None, conic_coefficients=oe.CCC.tolist(), # inputs related to mirror reflectivity f_reflec=oe.F_REFLEC, # reflectivity of surface: 0=no reflectivity, 1=full polarization # f_refl=0, # 0=prerefl file, 1=electric susceptibility, 2=user defined file (1D reflectivity vs angle) # # 3=user defined file (1D reflectivity vs energy), # 4=user defined file (2D reflectivity vs energy and angle) # file_refl="", # preprocessor file fir f_refl=0,2,3,4 # refraction_index=1.0 # refraction index (complex) for f_refl=1 ), coordinates=ElementCoordinates( p=oe.T_SOURCE, q=oe.T_IMAGE, angle_radial=numpy.radians(oe.T_INCIDENCE), ), ) print(mirror1.info()) # # run # beam4, mirr4 = mirror1.trace_beam(beam_in=beam4, flag_lost_value=-11000) # # compare # oe_list = define_beamline() beam3 = run_beamline(beam3_source, oe_list) plotxy(beam3, 1, 3, nbins=201, nolost=1, title="%s shadow3" % name) plotxy(beam4, 1, 3, nbins=201, nolost=1, title="%s shadow4" % name) from shadow4tests.compatibility.compare_beams import check_six_columns_mean_and_std, check_almost_equal check_six_columns_mean_and_std(beam3, beam4, do_assert=True, do_plot=False, assert_value=1e-6) check_almost_equal(beam3, beam4, do_assert = True, level=3)
def initialize_from_array(cls, array): return Beam3.initialize_from_shadow4_beam( Beam_shadow4.initialize_from_array(array))