コード例 #1
0
ファイル: backpropagation.py プロジェクト: twguest/WPG
def back_propagate(params):
    """
    Propagate pulse from file params[0] at the distance params[1] and save result to HDF5 file.
    If output files exists - skip calculations.    
    """
    (input_path, distance, propagation_parameters) = params
    input_dir, input_file_name = os.path.split(input_path)

    out_file_name = "{}_{:0.4f}.h5".format(
        ".".join(input_file_name.split(".")[:-1]), distance
    )
    out_path = os.path.join(input_dir, out_file_name)

    if os.path.exists(out_path):
        return

    wf_L1 = Wavefront()
    wf_L1.load_hdf5(input_path)

    drift1 = optical_elements.Drift(distance)

    srwl_bl1 = SRWLOptC([drift1,], [propagation_parameters,])
    bl1 = Beamline(srwl_bl1)

    wpg.srwlib.srwl.SetRepresElecField(wf_L1._srwl_wf, "f")
    bl1.propagate(wf_L1)
    wpg.srwlib.srwl.SetRepresElecField(wf_L1._srwl_wf, "t")

    fit_gaussian_pulse(wf_L1)
    wf_L1.store_hdf5(out_path)
    del wf_L1

    gc.collect()

    return out_path
コード例 #2
0
def forward_propagate(root_dir, distance, propagation_parameters):
    """
    Forward_propagate_wavefront
    the result will saved in root_dir\distance\distance.h5 file
    
    :param root_dir: directory, where '0.h' file located
    :param distance: distance to forward propagate initial wvefront
    :param propagation_parameters: SRW propagation parameters
    """

    out_dir = os.path.join(root_dir, '{:0.4f}'.format(distance))
    mkdir_p(out_dir)

    out_file_name = '{:0.4f}.h5'.format(distance)
    out_path = os.path.join(out_dir, out_file_name)

    if os.path.exists(out_path):
        print('File exists: {}. Skiping.'.format(out_path))
        return out_path

    ppDrift0 = propagation_parameters

    drift0 = optical_elements.Drift(distance)
    srwl_bl0 = SRWLOptC([
        drift0,
    ], [
        ppDrift0,
    ])
    bl0 = Beamline(srwl_bl0)

    # forward propagate to L0 meters
    wf_L0 = Wavefront()
    wf_L0.load_hdf5(os.path.join(root_dir, '0.h5'))

    tmin = wf_L0.params.Mesh.sliceMin
    tmax = wf_L0.params.Mesh.sliceMax
    wf_L0.params.Mesh.sliceMin = -(tmax - tmin) / 2
    wf_L0.params.Mesh.sliceMax = (tmax - tmin) / 2

    # wpg.srwlib.srwl.ResizeElecField(wf_L0._srwl_wf, 't',[0,3.,1.])

    wpg.srwlib.srwl.SetRepresElecField(wf_L0._srwl_wf, 'f')
    bl0.propagate(wf_L0)
    wpg.srwlib.srwl.SetRepresElecField(wf_L0._srwl_wf, 't')
    fit_gaussian_pulse(wf_L0)
    wf_L0.store_hdf5(out_path)

    print('Save file : {}'.format(out_path))

    del wf_L0
    return out_path
コード例 #3
0
ファイル: exfel_spb_kb_beamline.py プロジェクト: samoylv/WPG
def get_beamline():
    """ Setup and return the WPG.Beamline object representing the SPB/SFX nanofocus beamline (KB mirrors).

    :return: beamline
    :rtype: wpg.Beamline
    """

    # Distances
    distance0 = 246.5
    distance1 = 683.5
    distance = distance0 + distance1

    # Focal lengths.
    f_hfm = 3.0  # nominal focal length for HFM KB
    f_vfm = 1.9  # nominal focal length for VFM KB
    distance_hfm_vfm = f_hfm - f_vfm
    distance_foc = 1. / (1. / f_vfm + 1. / (distance + distance_hfm_vfm))

    # Mirror incidence angles
    theta_om = 3.5e-3  # offset mirrors incidence angle
    theta_kb = 3.5e-3  # KB mirrors incidence angle

    # Mirror lengths
    om_mirror_length = 0.8
    om_clear_ap = om_mirror_length * theta_om

    kb_mirror_length = 0.9
    kb_clear_ap = kb_mirror_length * theta_kb

    # Drifts.
    drift0 = optical_elements.Drift(distance0)
    drift1 = optical_elements.Drift(distance1)
    drift_in_kb = optical_elements.Drift(distance_hfm_vfm)
    drift_to_foc = optical_elements.Drift(distance_foc)

    # Mirror apertures.
    ap0 = optical_elements.Aperture('r', 'a', 5.0e-4, 5.0e-4)
    ap1 = optical_elements.Aperture('r', 'a', om_clear_ap, 2 * om_clear_ap)
    ap_kb = optical_elements.Aperture('r', 'a', kb_clear_ap, kb_clear_ap)

    # Mirror definitions.
    hfm = optical_elements.Mirror_elliptical(orient='x',
                                             p=distance,
                                             q=(distance_hfm_vfm +
                                                distance_foc),
                                             thetaE=theta_kb,
                                             theta0=theta_kb,
                                             length=0.9)
    vfm = optical_elements.Mirror_elliptical(orient='y',
                                             p=(distance + distance_hfm_vfm),
                                             q=distance_foc,
                                             thetaE=theta_kb,
                                             theta0=theta_kb,
                                             length=0.9)

    # Mirror profiles.
    wf_dist_om = optical_elements.Mirror_plane(orient='x',
                                               theta=theta_om,
                                               length=om_mirror_length,
                                               range_xy=2 * om_clear_ap,
                                               filename=os.path.join(
                                                   mirror_data_dir,
                                                   'mirror2.dat'),
                                               scale=2.,
                                               bPlot=False)

    wf_dist_hfm = optical_elements.Mirror_plane(orient='x',
                                                theta=theta_kb,
                                                length=kb_mirror_length,
                                                range_xy=kb_clear_ap,
                                                filename=os.path.join(
                                                    mirror_data_dir,
                                                    'mirror1.dat'),
                                                scale=2.,
                                                bPlot=False)

    wf_dist_vfm = optical_elements.Mirror_plane(orient='y',
                                                theta=theta_kb,
                                                length=kb_mirror_length,
                                                range_xy=kb_clear_ap,
                                                filename=os.path.join(
                                                    mirror_data_dir,
                                                    'mirror2.dat'),
                                                scale=2.,
                                                bPlot=False)

    # Assemble the beamline with PP parameters.
    bl0 = Beamline()

    ### Aperture to resample. Increase sampling frequency to get Fresnel zone between 7 and 10 px.
    bl0.append(
        ap0, Use_PP(semi_analytical_treatment=0, zoom=1.0, sampling=1. / 0.4))
    #### Increase sampling again, reduce ROI => ROI ~10 fwhm, 700x700 sampling.
    bl0.append(
        drift0,
        Use_PP(semi_analytical_treatment=1, zoom=0.5, sampling=1.5 / 0.28))

    ####
    bl0.append(ap1, Use_PP(zoom=1.0, sampling=1.0))
    bl0.append(wf_dist_om, Use_PP())

    ###
    bl0.append(drift1,
               Use_PP(semi_analytical_treatment=1, zoom=1.0, sampling=2.0))

    ###
    bl0.append(ap_kb, Use_PP())
    bl0.append(hfm, Use_PP())
    bl0.append(wf_dist_hfm, Use_PP())

    ###
    bl0.append(drift_in_kb, Use_PP(semi_analytical_treatment=1))

    bl0.append(vfm, Use_PP())
    bl0.append(wf_dist_vfm, Use_PP())

    ###
    bl0.append(
        drift_to_foc,
        Use_PP(semi_analytical_treatment=1,
               zoom_h=0.1,
               sampling_h=1.2,
               zoom_v=0.05,
               sampling_v=1.2))

    ###bl0.append(ap0,   Use_PP(semi_analytical_treatment=0,
    ##                         #zoom=14.4,
    ##                         #sampling=1/1.6))
    ###bl0.append(drift0,Use_PP(semi_analytical_treatment=0))
    ###bl0.append(ap1, Use_PP(zoom=0.8))
    ###bl0.append(wf_dist_om, Use_PP())
    ###bl0.append(drift1, Use_PP(semi_analytical_treatment=1))
    ###bl0.append(ap_kb,  Use_PP(zoom = 6.4, sampling = 1/16.))
    ###bl0.append(hfm, Use_PP())
    ###bl0.append(wf_dist_hfm, Use_PP())
    ###bl0.append(drift_in_kb, Use_PP(semi_analytical_treatment=1))
    ###bl0.append(vfm, Use_PP())
    ###bl0.append(wf_dist_vfm, Use_PP())
    ###bl0.append(drift_to_foc, Use_PP(semi_analytical_treatment=1))

    # All done, return.
    return bl0
コード例 #4
0
ファイル: s2e_orig_beamline.py プロジェクト: samoylv/WPG
def get_beamline():
    """ Setup and return the WPG.Beamline object representing the SPB/SFX nanofocus beamline (KB mirrors).

    :return: beamline
    :rtype: wpg.Beamline
    """

    # Distances
    distance0 = 300.0
    distance1 = 630.0
    distance = distance0 + distance1

    # Focal lengths.
    f_hfm    = 3.0          # nominal focal length for HFM KB
    f_vfm    = 1.9          # nominal focal length for VFM KB
    distance_hfm_vfm = f_hfm - f_vfm
    distance_foc =  1. /(1./f_vfm + 1. / (distance + distance_hfm_vfm))

    # Mirror incidence angles
    theta_om = 3.5e-3       # offset mirrors incidence angle
    theta_kb = 3.5e-3       # KB mirrors incidence angle

    # Mirror lengths
    om_mirror_length = 0.8;
    om_clear_ap = om_mirror_length*theta_om

    kb_mirror_length = 0.9;
    kb_clear_ap = kb_mirror_length*theta_kb

    # Drifts.
    drift0 = optical_elements.Drift(distance0)
    drift1 = optical_elements.Drift(distance1)
    drift_in_kb = optical_elements.Drift(distance_hfm_vfm)
    drift_to_foc = optical_elements.Drift(distance_foc)

    # Mirror apertures.
    ap0   = optical_elements.Aperture('r','a', 120.e-6, 120.e-6)
    ap1   = optical_elements.Aperture('r','a', om_clear_ap, 2*om_clear_ap)
    ap_kb = optical_elements.Aperture('r','a', kb_clear_ap, kb_clear_ap)

    # Mirror definitions.
    hfm = optical_elements.Mirror_elliptical(
                    orient='x',
                    p=distance,
                    q=(distance_hfm_vfm+distance_foc),
                    thetaE=theta_kb,
                    theta0=theta_kb,
                    length=kb_mirror_length,
                    )
    vfm = optical_elements.Mirror_elliptical(
                    orient='y',
                    p=(distance+distance_hfm_vfm),
                    q=distance_foc,
                    thetaE=theta_kb,
                    theta0=theta_kb,
                    length=kb_mirror_length,
                    )


    # Mirror profiles.
    wf_dist_om = optical_elements.Mirror_plane(orient='x',
                              theta=theta_om,
                              length=om_mirror_length,
                              range_xy=2*om_clear_ap,
                              filename=os.path.join(mirror_data_dir, 'mirror2.dat'),
                              scale=2.,
                              bPlot=False)

    wf_dist_hfm = optical_elements.Mirror_plane(orient='x',
                              theta=theta_kb,
                              length=kb_mirror_length,
                              range_xy=kb_clear_ap,
                              filename=os.path.join(mirror_data_dir, 'mirror1.dat'),
                              scale=2.,
                              bPlot=False)

    wf_dist_vfm = optical_elements.Mirror_plane(orient='y',
                              theta=theta_kb,
                              length=kb_mirror_length,
                              range_xy=kb_clear_ap,
                              filename=os.path.join(mirror_data_dir, 'mirror2.dat'),
                              scale=2.,
                              bPlot=False)

    # Assemble the beamline with PP parameters.
    bl0 = Beamline()
    bl0.append(ap0,   Use_PP(semi_analytical_treatment=0,
                            zoom=14.4,
                            sampling=1/1.6))
    bl0.append(drift0,Use_PP(semi_analytical_treatment=0))
    bl0.append(ap1, Use_PP(zoom=0.8))
    bl0.append(wf_dist_om, Use_PP())
    bl0.append(drift1, Use_PP(semi_analytical_treatment=1))
    bl0.append(ap_kb,  Use_PP(zoom = 6.4, sampling = 1/16.))
    bl0.append(hfm, Use_PP())
    bl0.append(wf_dist_hfm, Use_PP())
    bl0.append(drift_in_kb, Use_PP(semi_analytical_treatment=1))
    bl0.append(vfm, Use_PP())
    bl0.append(wf_dist_vfm, Use_PP())
    bl0.append(drift_to_foc, Use_PP(semi_analytical_treatment=1))

    # All done, return.
    return bl0