def test_new_float(): d = pkarray.new_float() assert 0 == len(d), \ 'new_float without initializer should be empty' d = pkarray.new_float([3, 5]) assert 2 == len(d), \ 'new_float with initializer, should be non-zero' assert float(5) == d[1], \ 'new_float should intitialize to a float'
def simulate(): t0_setting = time.time() B = 0.4 LeffBM = 4.0 BM = srw.SRWLMagFldM(B, 1, "n", LeffBM) magFldCnt = srw.SRWLMagFldC([BM], pkarray.new_double([0]), pkarray.new_double([0]), pkarray.new_double([0])) eBeam = srw.SRWLPartBeam() eBeam.Iavg = 0.5 eBeam.partStatMom1.x = 0.0 eBeam.partStatMom1.y = 0.0 eBeam.partStatMom1.z = 0.0 eBeam.partStatMom1.xp = 0.0 eBeam.partStatMom1.yp = 0.0 eBeam.partStatMom1.gamma = 3.0 / 0.51099890221e-03 eBeam.arStatMom2[0] = 127.346e-06 ** 2 eBeam.arStatMom2[1] = -10.85e-09 eBeam.arStatMom2[2] = 92.3093e-06 ** 2 eBeam.arStatMom2[3] = 13.4164e-06 ** 2 eBeam.arStatMom2[4] = 0.0072e-09 eBeam.arStatMom2[5] = 0.8022e-06 ** 2 eBeam.arStatMom2[10] = 0.89e-03 ** 2 wfr = srw.SRWLWfr() wfr.allocate(1, 10, 10) distSrcLens = 5.0 wfr.mesh.zStart = distSrcLens wfr.mesh.eStart = 0.5 * 0.123984 wfr.mesh.eFin = wfr.mesh.eStart horAng = 0.1 wfr.mesh.xStart = -0.5 * horAng * distSrcLens wfr.mesh.xFin = 0.5 * horAng * distSrcLens verAng = 0.02 wfr.mesh.yStart = -0.5 * verAng * distSrcLens wfr.mesh.yFin = 0.5 * verAng * distSrcLens wfr.partBeam = eBeam distLensImg = distSrcLens focLen = wfr.mesh.zStart * distLensImg / (distSrcLens + distLensImg) optLens = srw.SRWLOptL(_Fx=focLen, _Fy=focLen) optDrift = srw.SRWLOptD(distLensImg) propagParLens = [1, 1, 1.0, 0, 0, 1.0, 2.0, 1.0, 2.0, 0, 0, 0] propagParDrift = [1, 1, 1.0, 0, 0, 1.0, 1.0, 1.0, 1.0, 0, 0, 0] optBL = srw.SRWLOptC([optLens, optDrift], [propagParLens, propagParDrift]) pkdc("parameters done in {}s", round(time.time() - t0_setting)) meth = 2 relPrec = 0.01 zStartInteg = 0 zEndInteg = 0 npTraj = 20000 useTermin = 1 sampFactNxNyForProp = 0.7 arPrecSR = [meth, relPrec, zStartInteg, zEndInteg, npTraj, useTermin, sampFactNxNyForProp] pkdc("Performing initial electric field calculation...") srw.srwl.CalcElecFieldSR(wfr, 0, magFldCnt, arPrecSR) pkdc("Extracting intensity from calculated electric field and saving it to file(s)") mesh0 = copy.deepcopy(wfr.mesh) arI0 = pkarray.new_float([0] * mesh0.nx * mesh0.ny) srw.srwl.CalcIntFromElecField(arI0, wfr, 6, 0, 3, mesh0.eStart, 0, 0) pkdc("Simulating single-electron electric field wavefront propagation...") srw.srwl.PropagElecField(wfr, optBL) return wfr
def main(): wavefront = test_simulation() mesh = copy.deepcopy(wavefront.mesh) intensity = pkarray.new_float([0] * mesh.nx * mesh.ny) srw.srwl.CalcIntFromElecField(intensity, wavefront, 6, 1, 3, mesh.eStart, 0, 0) import matplotlib.pyplot as plt dim_x = np.linspace(mesh.xStart, mesh.xFin, mesh.nx) dim_y = np.linspace(mesh.yStart, mesh.yFin, mesh.ny) intensity = np.array(intensity).reshape((mesh.ny,mesh.nx)) plt.pcolormesh(dim_x, dim_y, intensity) plt.title('Real space for infrared example') plt.colorbar() plt.show()
def simulate(params, msg_callback=lambda _: _): """Run a multi-particle (thick) simulation and return results Args: params (OrderedMapping): configuration parameters msg_callback (function): Called at various stages to log output Returns: OrderedMapping: results and params (see code for format) """ p = pkcollections.OrderedMapping() for k in 'simulation_kind', 'wavefront': v = params[k] p[k] = v.value if hasattr(v, 'value') else v pkcollections.mapping_merge( p, srw_params.to_undulator_multi_particle(params.undulator)) p.beam = srw_params.to_beam(params.beam) p.stkF = srw_params.to_wavefront_multi_particle(p.wavefront) p.stkP = srw_params.to_wavefront_multi_particle(p.wavefront) p.ar_prec_f = srw_params.to_flux_precision(params.precision) p.ar_prec_p = srw_params.to_power_precision(params.precision) p.arPrecPar = [1] #General Precision parameters for Trajectory calculation: p.fieldInterpMeth = 4 p.plots = [] msg_callback('Performing trajectory calculation') _trajectory(p) if params.simulation_kind == 'E': msg_callback('Performing Electric Field (spectrum vs photon energy) calculation') msg_callback('Extracting Intensity from calculated Electric Field') srwlib.srwl.CalcStokesUR(p.stkF, p.beam, p.und, p.ar_prec_f) p.plots.append([ uti_plot.uti_plot1d, p.stkF.arS, [p.stkF.mesh.eStart, p.stkF.mesh.eFin, p.stkF.mesh.ne], [ 'Photon Energy [eV]', 'Flux [ph/s/.1%bw]', 'Flux through Finite Aperture', ], ]) elif params.simulation_kind == 'X': msg_callback('Performing Power Density calculation (from field) vs x-coordinate calculation') srwlib.srwl.CalcPowDenSR(p.stkP, p.beam, 0, p.magFldCnt, p.ar_prec_p) msg_callback('Extracting Intensity from calculated Electric Field') p.plotMeshX = [1000*p.stkP.mesh.xStart, 1000*p.stkP.mesh.xFin, p.stkP.mesh.nx] p.powDenVsX = pkarray.new_float([0]*p.stkP.mesh.nx) for i in xrange(p.stkP.mesh.nx): powDenVsX[i] = p.stkP.arS[p.stkP.mesh.nx*int(p.stkP.mesh.ny*0.5) + i] p.plots.append([ uti_plot.uti_plot1d, p.powDenVsX, p.plotMeshX, [ 'Horizontal Position [mm]', 'Power Density [W/mm^2]', 'Power Density\n(horizontal cut at y = 0)', ], ]) elif params.simulation_kind == 'Y': msg_callback('Performing Power Density calculation (from field) vs x-coordinate calculation') srwlib.srwl.CalcPowDenSR(p.stkP, p.beam, 0, p.magFldCnt, p.ar_prec_p) msg_callback('Extracting Intensity from calculated Electric Field') p.plotMeshY = [1000*p.stkP.mesh.yStart, 1000*p.stkP.mesh.yFin, p.stkP.mesh.ny] p.powDenVsY = pkarray.new_float([0]*p.stkP.mesh.ny) for i in xrange(p.stkP.mesh.ny): p.powDenVsY[i] = p.stkP.arS[p.stkP.mesh.ny*int(p.stkP.mesh.nx*0.5) + i] p.plots.append([ uti_plot.uti_plot1d, p.powDenVsY, p.plotMeshY, [ 'Vertical Position [mm]', 'Power Density [W/mm^2]', 'Power Density\n(vertical cut at x = 0)', ], ]) elif params.simulation_kind == 'X_AND_Y': msg_callback('Performing Electric Field (intensity vs x- and y-coordinate) calculation') srwlib.srwl.CalcPowDenSR(p.stkP, p.beam, 0, p.magFldCnt, p.ar_prec_p) msg_callback('Extracting Intensity from calculated Electric Field') p.plotMeshX = [1000*p.stkP.mesh.xStart, 1000*p.stkP.mesh.xFin, p.stkP.mesh.nx] p.plotMeshY = [1000*p.stkP.mesh.yStart, 1000*p.stkP.mesh.yFin, p.stkP.mesh.ny] p.plots.append([ uti_plot.uti_plot2d, p.stkP.arS, p.plotMeshX, p.plotMeshY, [ 'Horizontal Position [mm]', 'Vertical Position [mm]', 'Power Density', ], ]) else: raise AssertionError('{}: invalid simulation_kind'.format(params.simulation_kind)) return p
def simulate(): t0_setting = time.time() B = 0.4 LeffBM = 4. BM = srw.SRWLMagFldM(B, 1, 'n', LeffBM) magnetic_field_container = srw.SRWLMagFldC( [BM], pkarray.new_double([0]), pkarray.new_double([0]), pkarray.new_double([0]), ) eBeam = srw.SRWLPartBeam() eBeam.Iavg = 0.5 eBeam.partStatMom1.x = 0. eBeam.partStatMom1.y = 0. eBeam.partStatMom1.z = 0. eBeam.partStatMom1.xp = 0. eBeam.partStatMom1.yp = 0. eBeam.partStatMom1.gamma = 3./0.51099890221e-03 eBeam.arStatMom2[0] = 127.346e-06 ** 2 eBeam.arStatMom2[1] = -10.85e-09 eBeam.arStatMom2[2] = 92.3093e-06 ** 2 eBeam.arStatMom2[3] = 13.4164e-06 ** 2 eBeam.arStatMom2[4] = 0.0072e-09 eBeam.arStatMom2[5] = 0.8022e-06 ** 2 eBeam.arStatMom2[10] = 0.89e-03 ** 2 wavefront = srw.SRWLWfr() wavefront.allocate(1, 10, 10) distSrcLens = 5. wavefront.mesh.zStart = distSrcLens wavefront.mesh.eStart = 0.5 * 0.123984 wavefront.mesh.eFin = wavefront.mesh.eStart horAng = 0.1 wavefront.mesh.xStart = -0.5 * horAng * distSrcLens wavefront.mesh.xFin = 0.5 * horAng * distSrcLens verAng = 0.02 wavefront.mesh.yStart = -0.5 * verAng * distSrcLens wavefront.mesh.yFin = 0.5 * verAng * distSrcLens wavefront.partBeam = eBeam distLensImg = distSrcLens focLen = wavefront.mesh.zStart * distLensImg / (distSrcLens + distLensImg) optBL = _container( srw.SRWLOptL(_Fx=focLen, _Fy=focLen), dict( horizontal_resolution_factor_when_resizing=2., vertical_resolution_factor_when_resizing=2., ), srw.SRWLOptD(distLensImg), {}, ) pkdc('parameters done in {}s', round(time.time() - t0_setting)) meth = 2 relPrec = 0.01 zStartInteg = 0 zEndInteg = 0 npTraj = 20000 useTermin = 1 sampFactNxNyForProp = 0.7 arPrecSR = [meth, relPrec, zStartInteg, zEndInteg, npTraj, useTermin, sampFactNxNyForProp] pkdc('Performing initial electric field calculation...') srw.srwl.CalcElecFieldSR(wavefront, 0, magnetic_field_container, arPrecSR) pkdc('Extracting intensity from calculated electric field and saving it to file(s)') mesh0 = copy.deepcopy(wavefront.mesh) arI0 = pkarray.new_float([0] * mesh0.nx * mesh0.ny) srw.srwl.CalcIntFromElecField(arI0, wavefront, 6, 0, 3, mesh0.eStart, 0, 0) pkdc('Simulating single-electron electric field wavefront propagation...') srw.srwl.PropagElecField(wavefront, optBL) return wavefront
def simulate(params, msg_callback): """Run a single-particle (thin) simulation and return results Args: params (OrderedMapping): map of parameters msg_callback (function): Called at various stages to log output Returns: OrderedMapping: results and params (see code for format) """ p = pkcollections.OrderedMapping() for k in 'polarization', 'intensity', 'simulation_kind', 'wavefront': v = params[k] p[k] = v.value if hasattr(v, 'value') else v pkcollections.mapping_merge( p, srw_params.to_undulator_single_particle(params.undulator)) p.arPrecPar = srw_params.to_precision_single_particle(params.precision) p.wfrE = srw_params.to_wavefront_single_particle(p.wavefront) p.wfrE.partBeam = srw_params.to_beam(params.beam) p.wfrXY = srw_params.to_wavefront_single_particle(p.wavefront) p.wfrXY.partBeam = srw_params.to_beam(params.beam) p.beam = srw_params.to_beam(params.beam) p.plots = [] skv = p.simulation_kind if params.simulation_kind == 'E': msg_callback('Performing Electric Field (spectrum vs photon energy) calculation') srwlib.srwl.CalcElecFieldSR(p.wfrE, 0, p.magFldCnt, p.arPrecPar) msg_callback('Extracting Intensity from calculated Electric Field') p.arI1 = pkarray.new_float([0]*p.wfrE.mesh.ne) srwlib.srwl.CalcIntFromElecField( p.arI1, p.wfrE, p.polarization, p.intensity, skv, p.wfrE.mesh.eStart, 0, 0) p.plots.append([ uti_plot.uti_plot1d, p.arI1, [p.wfrE.mesh.eStart, p.wfrE.mesh.eFin, p.wfrE.mesh.ne], ['Photon energy, eV','Spectral intensity, ph/s/0.1%BW','Intensity vs photon energy'], ]) elif params.simulation_kind == 'X': msg_callback('Performing Electric Field (intensity vs x-coordinate) calculation') srwlib.srwl.CalcElecFieldSR(p.wfrXY, 0, p.magFldCnt, p.arPrecPar) msg_callback('Extracting Intensity from calculated Electric Field') p.arI1 = pkarray.new_float([0]*p.wfrXY.mesh.nx) srwlib.srwl.CalcIntFromElecField(p.arI1, p.wfrXY, p.polarization, p.intensity, skv, 0, p.wfrXY.mesh.xStart, 0) p.plots.append([ uti_plot.uti_plot1d, p.arI1, [p.wfrXY.mesh.xStart, p.wfrXY.mesh.xFin, p.wfrXY.mesh.nx], ['Horizontal Position [m]','Spectral intensity, ph/s/0.1%BW','Intensity vs x-coordinate'], ]) elif params.simulation_kind == 'Y': msg_callback('Performing Electric Field (intensity vs y-coordinate) calculation') srwlib.srwl.CalcElecFieldSR(p.wfrXY, 0, p.magFldCnt, p.arPrecPar) msg_callback('Extracting Intensity from calculated Electric Field') p.arI1 = pkarray.new_float([0]*p.wfrXY.mesh.ny) srwlib.srwl.CalcIntFromElecField(p.arI1, p.wfrXY, p.polarization, p.intensity, skv, 0, p.wfrXY.mesh.yStart, 0) p.plots.append([ uti_plot.uti_plot1d, p.arI1, [p.wfrXY.mesh.yStart, p.wfrXY.mesh.yFin, p.wfrXY.mesh.ny], ['Vertical Position [m]','Spectral intensity, ph/s/0.1%BW','Intensity vs y-coordinate'], ]) elif params.simulation_kind == 'X_AND_Y': msg_callback('Performing Electric Field (intensity vs x- and y-coordinate) calculation') srwlib.srwl.CalcElecFieldSR(p.wfrXY, 0, p.magFldCnt, p.arPrecPar) msg_callback('Extracting Intensity from calculated Electric Field') p.arI1 = pkarray.new_float([0]*p.wfrXY.mesh.nx*p.wfrXY.mesh.ny) srwlib.srwl.CalcIntFromElecField(p.arI1, p.wfrXY, p.polarization, p.intensity, skv, p.wfrXY.mesh.eStart, p.wfrXY.mesh.xStart, p.wfrXY.mesh.yStart) p.plots.append([ uti_plot.uti_plot2d, p.arI1, [1*p.wfrXY.mesh.xStart, 1*p.wfrXY.mesh.xFin, p.wfrXY.mesh.nx], [1*p.wfrXY.mesh.yStart, 1*p.wfrXY.mesh.yFin, p.wfrXY.mesh.ny], ['Horizontal Position [m]', 'Vertical Position [m]', 'Intensity at ' + str(wfrXY.mesh.eStart) + ' eV'], ]), elif params.simulation_kind == 'E_AND_X': msg_callback('Performing Electric Field (intensity vs energy- and x-coordinate) calculation') srwlib.srwl.CalcElecFieldSR(p.wfrXY, 0, p.magFldCnt, p.arPrecPar) msg_callback('* Extracting Intensity from calculated Electric Field') p.arI1 = pkarray.new_float([0]*p.wfrXY.mesh.ne*p.wfrXY.mesh.nx) srwlib.srwl.CalcIntFromElecField(p.arI1, p.wfrXY, p.polarization, p.intensity, skv, p.wfrXY.mesh.eStart, p.wfrXY.mesh.xStart, p.wfrXY.mesh.yStart) p.plots.append([ uti_plot.uti_plot1d, p.arI1, [1*p.wfrXY.mesh.eStart, 1*p.wfrXY.mesh.eFin, p.wfrXY.mesh.ne], [1*p.wfrXY.mesh.xStart, 1*p.wfrXY.mesh.xFin, p.wfrXY.mesh.nx], ['Energy [eV]', 'Horizontal Position [m]', 'Intensity integrated from ' + str(p.wfrXY.mesh.yStart) + ' to ' + str(p.wfrXY.mesh.yFin) + ' ,m in y-coordinate'], ]) elif params.simulation_kind == 'E_AND_Y': msg_callback('Performing Electric Field (intensity vs energy- and y-coordinate) calculation') srwlib.srwl.CalcElecFieldSR(p.wfrXY, 0, p.magFldCnt, p.arPrecPar) msg_callback('Extracting Intensity from calculated Electric Field') p.arI1 = pkarray.new_float([0]*p.wfrXY.mesh.ne*p.wfrXY.mesh.ny) srwlib.srwl.CalcIntFromElecField(p.arI1, p.wfrXY, p.polarization, p.intensity, skv, p.wfrXY.mesh.eStart, p.wfrXY.mesh.xStart, p.wfrXY.mesh.yStart) p.plots.append([ uti_plot.uti_plot1d, p.arI1, [1*p.wfrXY.mesh.eStart, 1*p.wfrXY.mesh.eFin, p.wfrXY.mesh.ne], [1*p.wfrXY.mesh.yStart, 1*p.wfrXY.mesh.yFin, p.wfrXY.mesh.ny], ['Energy [eV]', 'Vertical Position [m]', 'Intensity integrated from ' + str(p.wfrXY.mesh.xStart) + ' to ' + str(p.wfrXY.mesh.xFin)+ ' ,m in x-coordinate'], ]) else: raise AssertionError('{}: invalid p.simulation_kind'.format(params.simulation_kind)) return p