05 beamwaist
This example traces the beam through the line and renders the beamwaist along the optical elements. The simulation prepares the result folder, and the eval script loads those results and writes the final figure next to the example scripts.
The simulation script:
"""Beamwaist example — simulation step.
Traces the beam through every optical element at a single photon energy and
saves the combined beamwaist image (``xh.txt`` / ``yh.txt``) into this folder's
``RAYPy_Simulation_Beamwaist`` directory. Run ``eval_beamwaist.py`` afterwards
to produce the 2D plot from the saved trace.
"""
import os
from raypyng.beamwaist import PlotBeamwaist
from raypyng.simulate import Simulate
if __name__ == '__main__':
this_file_dir = os.path.dirname(os.path.realpath(__file__))
rml_file = os.path.join(this_file_dir, '..', 'rml', 'dipole_beamline.rml')
sim = Simulate(rml_file, hide=True)
sim.path = this_file_dir # write output inside this example's own folder
sim_folder = 'Beamwaist'
bw = PlotBeamwaist(sim_folder, sim)
# PlotBeamwaist.directory is CWD-relative by default; make it absolute so it
# matches where Simulate (sim.path) writes, regardless of the caller's CWD.
bw.directory = os.path.join(this_file_dir, bw.directory)
energy = 500
nrays = 1000
bw.simulate_beamline(energy, nrays=nrays, force=True)
bw.define_hist(lim=20, step=.5) # lim should be larger than max beam waist
bw.define_zstep(step_z=100) # in mm, step in optical direction to trace RAYS
bw.reduce_Nrays(factor=1) # this reduces the n of rays by the factor you set
bw.trace_beamwaist(save_results=True)
The eval script:
"""Plot the beamwaist for the beamwaist example."""
import os
from raypyng.beamwaist import PlotBeamwaist
from raypyng.simulate import Simulate
if __name__ == '__main__':
this_file_dir = os.path.dirname(os.path.realpath(__file__))
rml_file = os.path.join(this_file_dir, '..', 'rml', 'dipole_beamline.rml')
sim = Simulate(rml_file, hide=True)
sim.path = this_file_dir
sim_folder = 'Beamwaist'
bw = PlotBeamwaist(sim_folder, sim)
bw.directory = os.path.join(this_file_dir, bw.directory)
bw.define_hist(lim=20, step=.5)
bw.define_zstep(step_z=100)
bw.reduce_Nrays(factor=1)
# load the trace produced by simulation_beamwaist.py
bw.load_previous_results()
bw.change_name(new_name='Dip', pos=0)
bw.change_name(new_name='Focus', pos=8)
bw.plot(
save_img=True,
img_name='eval_beamwaist',
extension='.png',
show_img=False,
annotate_OE=True,
debug=False,
)
source_png = os.path.join(this_file_dir, 'RAYPy_Simulation_Beamwaist', 'eval_beamwaist.png')
target_png = os.path.join(this_file_dir, 'eval_beamwaist.png')
os.replace(source_png, target_png)
print('Saved beamwaist plot:', target_png)
Result: