00 raypyng

This is the main raypyng workflow example. It scans a dipole beamline over photon energy and exit-slit size, lets raypyng analyze the exported rays, and then plots the combined recap file written in the simulation folder.

The simulation script:

from raypyng import Simulate
import numpy as np
import os

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

    rml = sim.rml
    beamline = sim.rml.beamline

    # define the values of the parameters to scan
    energy   = np.arange(200, 7201, 500)
    SlitSize = np.array([0.1, 0.2])
    cff      = np.array([2.25])
    nrays    = 5e4

    params = [
        {beamline.Dipole.photonEnergy: energy},
        {beamline.ExitSlit.totalHeight: SlitSize},
        {beamline.PG.cFactor: cff},
        {beamline.Dipole.numberRays: nrays},
    ]

    sim.params = params
    sim.simulation_name = 'raypyng'
    sim.repeat = 1
    sim.analyze = False
    sim.raypyng_analysis = True

    sim.exports = [
        {beamline.Dipole: ['RawRaysOutgoing']},
        {beamline.DetectorAtFocus: ['RawRaysOutgoing']},
    ]

    sim.run(multiprocessing="auto", force=True, remove_rawrays=True)



        

The eval script:

"""Plot photon energy vs bandwidth and flux for the raypyng example."""

import os

import matplotlib

matplotlib.use("Agg")  # headless: never open a window

import matplotlib.pyplot as plt  # noqa: E402
import pandas as pd  # noqa: E402

if __name__ == "__main__":
    this_file_dir = os.path.dirname(os.path.realpath(__file__))
    csv_path = os.path.join(
        this_file_dir,
        "RAYPy_Simulation_raypyng",
        "DetectorAtFocus_RawRaysOutgoing.csv",
    )
    df = pd.read_csv(csv_path)
    title = "raypyng: bandwidth and flux vs energy"
    fig, axs = plt.subplots(2, 1, figsize=(10, 8))
    for slit_size in sorted(df["ExitSlit.totalHeight"].unique()):
        sub = df[df["ExitSlit.totalHeight"] == slit_size]
        sub = sub.sort_values("PhotonEnergy")
        label = f"ExitSlit.totalHeight = {slit_size}"
        axs[0].plot(sub["PhotonEnergy"], sub["Bandwidth"], marker=".", label=label)
        axs[1].plot(sub["PhotonEnergy"], sub["PhotonFlux"], marker=".", label=label)

    axs[0].set(xlabel="Photon energy [eV]", ylabel="Bandwidth [eV]", title=title)
    axs[1].set(xlabel="Photon energy [eV]", ylabel="Photon flux [ph/s]", title="Flux vs photon energy")
    for ax in axs:
        ax.grid(True, alpha=0.3)
        ax.legend()
    fig.tight_layout()
    out_png = os.path.join(this_file_dir, "eval_raypyng.png")
    fig.savefig(out_png, dpi=150)
    print("[eval] saved:", out_png)

Result:

raypyng example plot