Exemplo n.º 1
0
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')
Exemplo n.º 2
0
    def test_collimated_source(self):

        #
        #
        #
        a = SourceGridCartesian.initialize_collimated_source(
            real_space=[1., 0.0, 1.0], real_space_points=[100, 1, 100])
        print(a.info())
        beam_shadow3 = Beam3.initialize_from_shadow4_beam(a.get_beam())

        beam = a.get_beam()

        if DO_PLOT:

            from srxraylib.plot.gol import plot_scatter, set_qt
            set_qt()
            plot_scatter(beam.get_column(1), beam.get_column(3))
            import Shadow
            Shadow.ShadowTools.plotxy(beam_shadow3, 1, 3)

        print(beam.info())
Exemplo n.º 3
0
    def test_compare_preprocessor_and_internal_from_shadowvui_json_file(
            self, shadowvui_json_file=None):

        if shadowvui_json_file == None:
            tmp = \
            """
            {
            "LAMBDAU":     0.0320000015,
            "K":      0.250000000,
            "E_ENERGY":       6.03999996,
            "E_ENERGY_SPREAD":    0.00100000005,
            "NPERIODS": 50,
            "_EMIN":       10498.0000,
            "_EMAX":       10499.0000,
            "INTENSITY":      0.200000003,
            "_MAXANGLE":      0.000100,
            "_NG_E": 101,
            "_NG_T": 51,
            "_NG_P": 11,
            "NG_PLOT(1)":"0",
            "NG_PLOT(2)":"No",
            "NG_PLOT(3)":"Yes",
            "UNDUL_PHOT_FLAG(1)":"0",
            "UNDUL_PHOT_FLAG(2)":"Shadow code",
            "UNDUL_PHOT_FLAG(3)":"Urgent code",
            "UNDUL_PHOT_FLAG(4)":"SRW code",
            "UNDUL_PHOT_FLAG(5)":"Gaussian Approximation",
            "UNDUL_PHOT_FLAG(6)":"ESRF python code",
            "SEED": 36255,
            "SX":     0.0399999991,
            "SZ":    0.00100000005,
            "EX":   4.00000005E-07,
            "EZ":   3.99999989E-09,
            "_FLAG_EMITTANCE(1)":"1",
            "_FLAG_EMITTANCE(2)":"No",
            "_FLAG_EMITTANCE(3)":"Yes",
            "NRAYS": 15000,
            "F_BOUND_SOUR": 0,
            "FILE_BOUND":"NONESPECIFIED",
            "SLIT_DISTANCE":       1000.00000,
            "SLIT_XMIN":      -1.00000000,
            "SLIT_XMAX":       1.00000000,
            "SLIT_ZMIN":      -1.00000000,
            "SLIT_ZMAX":       1.00000000,
            "NTOTALPOINT": 10000000,
            "JUNK4JSON":0
            }

            """

            h = json.loads(tmp)

        #
        # clean
        #
        os.system("rm start.00 begin*.dat uphot*.dat xshundul*.sha")

        #
        # run
        #

        methods = ['preprocessor', 'internal']

        for method in methods:

            if method == 'preprocessor':
                # run using binary shadow3 (with preprocessors)
                _calculate_shadow3_beam_using_preprocessors(h)
            else:
                from syned.storage_ring.electron_beam import ElectronBeam
                from syned.storage_ring.magnetic_structures.undulator import Undulator

                #
                # syned
                #

                # su = Undulator.initialize_as_vertical_undulator(K=h["K"],period_length=h["LAMBDAU"],periods_number=h["NPERIODS"])

                ebeam = ElectronBeam(
                    energy_in_GeV=h["E_ENERGY"],
                    energy_spread=h["E_ENERGY_SPREAD"],
                    current=h["INTENSITY"],
                    number_of_bunches=1,
                    moment_xx=(1e-2 * h["SX"])**2,
                    moment_xxp=0.0,
                    moment_xpxp=(h["EX"] / h["SX"])**2,
                    moment_yy=(1e-2 * h["SZ"])**2,
                    moment_yyp=0.0,
                    moment_ypyp=(h["EZ"] / h["SZ"])**2,
                )

                u = S4Undulator(K_vertical=h["K"],
                                period_length=h["LAMBDAU"],
                                number_of_periods=h["NPERIODS"],
                                flag_emittance=int(h["_FLAG_EMITTANCE(1)"]),
                                flag_size=0,
                                emin=h["_EMIN"],
                                emax=h["_EMAX"],
                                ng_e=h["_NG_E"],
                                maxangle=h["_MAXANGLE"],
                                ng_t=h["_NG_T"],
                                ng_p=h["_NG_P"],
                                code_undul_phot="internal")

                ls = S4UndulatorLightSource(name="",
                                            electron_beam=ebeam,
                                            undulator_magnetic_structure=u)
                print(ls.info())

                beam_shadow4 = ls.get_beam(user_unit_to_m=1e-2,
                                           SEED=36255,
                                           NRAYS=h["NRAYS"])
                beam = Beam3.initialize_from_shadow4_beam(beam_shadow4)
                beam.write("begin.dat")

            os.system("cp begin.dat begin_%s.dat" % method)
            os.system("cp uphot.dat uphot_%s.dat" % method)
Exemplo n.º 4
0
def test_empty_element(
        do_plot=0,
        do_assert=True,
        do_shadow3_fortran=True,
        N=1000,
        alpha_deg=None,  # 20,    # None=rondomize
        theta1_deg=None,  # 10.0,  # None=rondomize
        theta2_deg=None,  # 170.0,  # None=rondomize
        p=None,  # 15.0,  # None=rondomize
        q=None,  # 100.0  # None=rondomize,
):

    source = SourceGeometrical()
    source.set_angular_distribution_gaussian(1e-6, 1e-6)
    beam0 = source.calculate_beam(N=N, POL_DEG=1)
    print(beam0.info())

    beam0s3 = Beam3.initialize_from_shadow4_beam(beam0)

    beam1s3 = Beam3.initialize_from_shadow4_beam(beam0)

    if alpha_deg is None: alpha_deg = numpy.random.random() * 360.0
    if theta1_deg is None: theta1_deg = numpy.random.random() * 90.0
    if theta2_deg is None: theta2_deg = numpy.random.random() * 180.0
    if p is None: p = numpy.random.random() * 100.0
    if q is None: q = numpy.random.random() * 100.0

    #
    # shadow4
    #

    empty = S4EmptyElement()
    empty.get_coordinates().set_positions(
        angle_radial=theta1_deg * numpy.pi / 180,
        angle_radial_out=theta2_deg * numpy.pi / 180,
        angle_azimuthal=alpha_deg * numpy.pi / 180,
        p=p,
        q=q)

    beam1, mirr1 = empty.trace_beam(beam0)

    #
    # shadow3
    #
    oe1 = Shadow.OE()
    oe1.ALPHA = alpha_deg
    oe1.DUMMY = 100.0
    oe1.FWRITE = 0  # 1
    oe1.F_REFRAC = 2
    oe1.T_IMAGE = q
    oe1.T_INCIDENCE = theta1_deg
    oe1.T_REFLECTION = theta2_deg
    oe1.T_SOURCE = p

    if do_shadow3_fortran:
        import os
        os.system("/bin/rm begin.dat start.01 star.01")

        beam0s3.write("begin.dat")
        oe1.write("start.01")
        f = open("systemfile.dat", "w")
        f.write("start.01\n")
        f.close()
        f = open("shadow3.inp", "w")
        f.write("trace\nsystemfile\n0\nexit\n")
        f.close()

        os.system("%s < shadow3.inp" % SHADOW3_BINARY)

        beam1f = Beam3(N=N)
        beam1f.load("star.01")

    beam1s3.traceOE(oe1, 1)

    if do_plot:
        plotxy(beam1, 4, 6, title="Image shadow4", nbins=201)
        plotxy(beam1s3, 4, 6, title="Image shadow3", nbins=201)

    print("alpha_deg, theta1_deg, theta2_deg = ", alpha_deg, theta1_deg,
          theta2_deg)
    print("p, q = ", p, q)
    print("\ncol#   shadow4  shadow3 (shadow3_fortran) (source)")
    for i in range(18):
        if do_shadow3_fortran:
            print("col%d   %f  %f  %f  %f " %
                  (i + 1, beam1.rays[0, i], beam1s3.rays[0, i],
                   beam1f.rays[0, i], beam0s3.rays[0, i]))
        else:
            print("col%d   %f  %f  " %
                  (i + 1, beam1.rays[0, i], beam1s3.rays[0, i]))

        if do_assert:
            assert_almost_equal(beam1.rays[:, i], beam1s3.rays[:, i], 4)
Exemplo n.º 5
0
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')
Exemplo n.º 6
0
    coordinates = ElementCoordinates(p=322.971 * 1e-2, q=5.0 * 1e-2)

    slit1 = S4ScreenElement(optical_element=scr, coordinates=coordinates)

    print(slit1.info())

    #
    # trace
    #

    beam2, tmp = slit1.trace_beam(beam, flag_lost_value=-101)

    #
    if do_plot:
        beam2_3 = Beam3.initialize_from_shadow4_beam(beam2)
        plotxy(beam2_3,
               1,
               3,
               nbins=100,
               title="SHADOW4 BEAMSTOPPER",
               nolost=True)

    print("col#   shadow4  shadow3")
    for i in range(18):
        if i in [0, 1, 2, 12]:
            factor = 1e2
        else:
            factor = 1.0
        print("col%d   %f  %f  " %
              (i + 1, factor * beam2.rays[10, i], beam3.rays[10, i]))
Exemplo n.º 7
0
    mirror1e = S4PlaneMirrorElement(optical_element=mirror1,
                                    coordinates=coordinates_syned)
    #
    # run
    #
    beam1, mirr1 = mirror1e.trace_beam(beam0, flag_lost_value=-11000.0)
    print(mirror1e.info())

    #
    # check
    #

    if do_plot:
        plotxy(beam3, 1, 3, title="Image 1 shadow3", nbins=101, nolost=1)
        beam1s3 = Beam3.initialize_from_shadow4_beam(beam1)
        plotxy(beam1s3, 1, 3, title="Image 1 shadow4", nbins=101, nolost=1)

    mirr3 = Beam3(N=beam0.rays.shape[0])
    mirr3.load("mirr.01")

    print("\ncol#   m-shadow4  m-shadow3  source")
    for i in range(18):
        print(
            "col%d   %20.10f  %20.10f  %20.10f  " %
            (i + 1, mirr1.rays[10, i], mirr3.rays[10, i], source3.rays[10, i]))
        if do_assert:
            assert_almost_equal(mirr1.rays[:, i], mirr3.rays[:, i], 1)

    print("\ncol#   shadow4  shadow3  source")
    for i in range(18):
Exemplo n.º 8
0
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')