Shadow Simulations#

This section is based on the TES beamline example.

Hint: See the List of predefined simulations in Sirepo for examples of identifiers for different beamlines.

Run “Beamline” Shadow3 simulations#

This example repeats the first simulation in “Optical Elements as Ophyd Objects”, but instead uses Shadow3 to run the simulation.

Start ipython and run the following where sim_id is the UID for the simulation we are working with:

[1]:
from sirepo_bluesky import prepare_re_env

%run -i $prepare_re_env.__file__

from sirepo_bluesky.sirepo_bluesky import SirepoBluesky
from sirepo_bluesky.sirepo_ophyd import BeamStatisticsReport, create_classes

connection = SirepoBluesky("http://localhost:8000")

data, schema = connection.auth("shadow", sim_id="00000002")
classes, objects = create_classes(connection=connection)
globals().update(**objects)

print(f"Number of points before change: {data['models']['simulation']['npoint']}")
data["models"]["simulation"]["npoint"] = 1000000
print(f"Number of points after change: {data['models']['simulation']['npoint']}")
aperture.horizontalSize.kind = "hinted"
w9.duration.kind = "hinted"

(uid,) = RE(bp.scan([w9], aperture.horizontalSize, 0, 2, 6))

hdr = db[uid]
tbl = hdr.table(fill=True)
print(tbl)

w9_image = np.array(list(hdr.data("w9_image")))
Number of points before change: 100000
Number of points after change: 1000000


Transient Scan ID: 1     Time: 2023-08-19 16:54:12
Persistent Unique Scan ID: '37b7be0b-dd3a-45ce-b135-5ca6f4e4409c'
New stream: 'primary'
+-----------+------------+-------------------------+-------------+------------+
|   seq_num |       time | aperture_horizontalSize | w9_duration |    w9_flux |
+-----------+------------+-------------------------+-------------+------------+
|         1 | 16:54:33.8 |                   0.000 |      19.323 |      0.000 |
|         2 | 16:54:52.1 |                   0.400 |      16.232 |   5138.520 |
|         3 | 16:55:10.5 |                   0.800 |      16.225 |  10251.164 |
|         4 | 16:55:28.9 |                   1.200 |      16.284 |  15406.536 |
|         5 | 16:55:47.4 |                   1.600 |      16.243 |  20619.667 |
|         6 | 16:56:05.7 |                   2.000 |      16.231 |  25819.524 |
+-----------+------------+-------------------------+-------------+------------+
generator scan ['37b7be0b'] (scan num: 1)



                                 time  \
seq_num
1       2023-08-19 16:54:33.845823765
2       2023-08-19 16:54:52.169906378
3       2023-08-19 16:55:10.515789747
4       2023-08-19 16:55:28.922255754
5       2023-08-19 16:55:47.416069984
6       2023-08-19 16:56:05.779393435

                                       w9_sirepo_data_json  \
seq_num
1        {"models": {"beamStatisticsReport": {"driftDiv...
2        {"models": {"beamStatisticsReport": {"driftDiv...
3        {"models": {"beamStatisticsReport": {"driftDiv...
4        {"models": {"beamStatisticsReport": {"driftDiv...
5        {"models": {"beamStatisticsReport": {"driftDiv...
6        {"models": {"beamStatisticsReport": {"driftDiv...

                                       w9_sirepo_data_hash  w9_duration  \
seq_num
1        b21f59de0f2a128774aa69e087d8d88f0ab9550be9fd2c...    19.322850
2        864811b059113828eb518392d146dbb54038113349986b...    16.232152
3        1173d167fcee94f49ca78a7e5ed8fe7b18acd1c2acb1f4...    16.225222
4        41596e16c15c160b3c583046992f9862d540682e059e30...    16.284376
5        3f4a3bef8d820d8a683aec278b6ee918c3d56c6e5f5386...    16.243160
6        5cdff1353a7620b0feb848b2e0f296bfeb07d2e33feee3...    16.230777

                                                  w9_image    w9_shape  \
seq_num
1        [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...  [100, 100]
2        [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...  [100, 100]
3        [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...  [100, 100]
4        [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...  [100, 100]
5        [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...  [100, 100]
6        [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...  [100, 100]

              w9_flux   w9_mean      w9_x      w9_y  w9_fwhm_x  w9_fwhm_y  \
seq_num
1            0.000000  0.000000       NaN       NaN        NaN        NaN
2         5138.520058  0.513852  0.197259 -0.152572   1.108809   0.701000
3        10251.164164  1.025116  0.196433 -0.145201   1.141733   0.681383
4        15406.536271  1.540654  0.201374 -0.137640   1.186571   0.659964
5        20619.667190  2.061967  0.232234 -0.137607   1.242215   0.634798
6        25819.524119  2.581952  0.264168 -0.132240   1.296800   0.613439

         w9_photon_energy                       w9_horizontal_extent  \
seq_num
1              2500.00041                          [-1000.0, 1000.0]
2              2500.00041   [-1.2336298496814044, 1.405929122101391]
3              2500.00041  [-1.3001551718158006, 1.4759384530988693]
4              2500.00041  [-1.3684114360191872, 1.5466178480457953]
5              2500.00041  [-1.4309472940233552, 1.6109566331651013]
6              2500.00041   [-1.5036858677506284, 1.695592271987839]

                                 w9_vertical_extent  w9_id w9_title w9_type  \
seq_num
1                                 [-1000.0, 1000.0]   12.0       W9   watch
2         [-0.7103102913794072, 0.4591117629407924]   12.0       W9   watch
3        [-0.7162298148739806, 0.47877690974846177]   12.0       W9   watch
4        [-0.7162298148739806, 0.49331718361385357]   12.0       W9   watch
5        [-0.7162298148739806, 0.49331718361385357]   12.0       W9   watch
6         [-0.7162298148739806, 0.5058855096921268]   12.0       W9   watch

         w9_element_position  aperture_horizontalSize
seq_num
1                     57.142                      0.0
2                     57.142                      0.4
3                     57.142                      0.8
4                     57.142                      1.2
5                     57.142                      1.6
6                     57.142                      2.0
../_images/notebooks_shadow_3_1.png
[2]:
from mpl_toolkits.axes_grid1 import ImageGrid

print(f"Data shape: {w9_image.shape}")
num_frames = w9_image.shape[0]
ncols = 2
nrows = int(np.ceil(num_frames / ncols))

fig = plt.figure()
grid = ImageGrid(
    fig,
    (1, 2, ncols, nrows),
    nrows_ncols=(nrows, ncols),
    axes_pad=0.5,
    aspect=False,
)

for ax, im in zip(grid, w9_image[:, ...]):
    ax.imshow(im, aspect="auto")
Data shape: (6, 100, 100)
../_images/notebooks_shadow_4_1.png

Run “Beam Statistics” Sirepo/Shadow app simulations#

In this example, we scan the toroidal mirror’s radius and collect the results of the “Beam Statistics” report on the “Beamline” page of Sirepo/Shadow app. At the end of the execution, we provide the timing report for these simulations.

[3]:
from sirepo_bluesky import prepare_re_env

%run -i $prepare_re_env.__file__

from sirepo_bluesky.sirepo_bluesky import SirepoBluesky
from sirepo_bluesky.sirepo_ophyd import BeamStatisticsReport, create_classes

connection = SirepoBluesky("http://localhost:8000")

data, schema = connection.auth("shadow", sim_id="00000002")

classes, objects = create_classes(connection=connection)
globals().update(**objects)

bsr = BeamStatisticsReport(name="bsr", connection=connection)

toroid.r_maj.kind = "hinted"
bsr.duration.kind = "hinted"

scan_range = (10_000, 50_000, 21)

(uid,) = RE(bp.scan([bsr], toroid.r_maj, *scan_range))

hdr = db[uid]
tbl = hdr.table()
print(tbl)


Transient Scan ID: 1     Time: 2023-08-19 16:56:16
Persistent Unique Scan ID: '4d82ddc6-bbec-4dfe-ab5e-14dd090798b0'
New stream: 'primary'
+-----------+------------+--------------+--------------+
|   seq_num |       time | toroid_r_maj | bsr_duration |
+-----------+------------+--------------+--------------+
|         1 | 16:56:19.7 |    10000.000 |        3.066 |
|         2 | 16:56:22.8 |    12000.000 |        3.059 |
|         3 | 16:56:25.9 |    14000.000 |        3.060 |
|         4 | 16:56:29.1 |    16000.000 |        3.058 |
|         5 | 16:56:32.2 |    18000.000 |        3.058 |
|         6 | 16:56:35.3 |    20000.000 |        3.062 |
|         7 | 16:56:38.5 |    22000.000 |        3.060 |
|         8 | 16:56:41.6 |    24000.000 |        3.057 |
|         9 | 16:56:44.7 |    26000.000 |        3.060 |
|        10 | 16:56:47.9 |    28000.000 |        3.058 |
|        11 | 16:56:51.0 |    30000.000 |        3.058 |
|        12 | 16:56:54.1 |    32000.000 |        3.059 |
|        13 | 16:56:57.2 |    34000.000 |        3.058 |
|        14 | 16:57:00.4 |    36000.000 |        3.059 |
|        15 | 16:57:03.5 |    38000.000 |        3.059 |
|        16 | 16:57:06.6 |    40000.000 |        3.059 |
|        17 | 16:57:09.8 |    42000.000 |        3.058 |
|        18 | 16:57:12.9 |    44000.000 |        3.060 |
|        19 | 16:57:16.0 |    46000.000 |        3.056 |
|        20 | 16:57:19.2 |    48000.000 |        3.060 |
|        21 | 16:57:22.3 |    50000.000 |        3.058 |
+-----------+------------+--------------+--------------+
generator scan ['4d82ddc6'] (scan num: 1)



                                 time  toroid_r_maj  \
seq_num
1       2023-08-19 16:56:19.729102850       10000.0
2       2023-08-19 16:56:22.864973783       12000.0
3       2023-08-19 16:56:25.998396873       14000.0
4       2023-08-19 16:56:29.127140045       16000.0
5       2023-08-19 16:56:32.259178162       18000.0
6       2023-08-19 16:56:35.388878584       20000.0
7       2023-08-19 16:56:38.519377708       22000.0
8       2023-08-19 16:56:41.646494389       24000.0
9       2023-08-19 16:56:44.779374123       26000.0
10      2023-08-19 16:56:47.910736322       28000.0
11      2023-08-19 16:56:51.040197372       30000.0
12      2023-08-19 16:56:54.167011499       32000.0
13      2023-08-19 16:56:57.293066263       34000.0
14      2023-08-19 16:57:00.420836687       36000.0
15      2023-08-19 16:57:03.549652338       38000.0
16      2023-08-19 16:57:06.679706335       40000.0
17      2023-08-19 16:57:09.808405876       42000.0
18      2023-08-19 16:57:12.942813396       44000.0
19      2023-08-19 16:57:16.070492268       46000.0
20      2023-08-19 16:57:19.203583717       48000.0
21      2023-08-19 16:57:22.331832647       50000.0

                                      bsr_sirepo_data_json  \
seq_num
1        {"models": {"beamStatisticsReport": {"driftDiv...
2        {"models": {"beamStatisticsReport": {"driftDiv...
3        {"models": {"beamStatisticsReport": {"driftDiv...
4        {"models": {"beamStatisticsReport": {"driftDiv...
5        {"models": {"beamStatisticsReport": {"driftDiv...
6        {"models": {"beamStatisticsReport": {"driftDiv...
7        {"models": {"beamStatisticsReport": {"driftDiv...
8        {"models": {"beamStatisticsReport": {"driftDiv...
9        {"models": {"beamStatisticsReport": {"driftDiv...
10       {"models": {"beamStatisticsReport": {"driftDiv...
11       {"models": {"beamStatisticsReport": {"driftDiv...
12       {"models": {"beamStatisticsReport": {"driftDiv...
13       {"models": {"beamStatisticsReport": {"driftDiv...
14       {"models": {"beamStatisticsReport": {"driftDiv...
15       {"models": {"beamStatisticsReport": {"driftDiv...
16       {"models": {"beamStatisticsReport": {"driftDiv...
17       {"models": {"beamStatisticsReport": {"driftDiv...
18       {"models": {"beamStatisticsReport": {"driftDiv...
19       {"models": {"beamStatisticsReport": {"driftDiv...
20       {"models": {"beamStatisticsReport": {"driftDiv...
21       {"models": {"beamStatisticsReport": {"driftDiv...

                                      bsr_sirepo_data_hash  bsr_duration  \
seq_num
1        23384cb65d21308e202362ab7fcf312822bb1880b9c3f4...      3.065517
2        47908827c35d92e6cf7b4d5196ab8189fa8ead530449dc...      3.059052
3        778cb1b625d1399eeb84dca1ab0e849beb572b6ceb571e...      3.059620
4        c748573d5063a7cf69aeb17f787aa22d5d76b94a5bbbbe...      3.057929
5        3aced853bd345b13438f1f4842bc6c9bb20dadd19a2b37...      3.058033
6        70e525b680558671de280fc7123d090c7edf86654b7a05...      3.061732
7        7d360f61203fd15abeaf658c58cdc5a86a906d84b24df6...      3.060186
8        2644f0030b40981dd2ac70cb09069be1a103b30c57585a...      3.057071
9        c544e7fef721b07417014fcd6256d745abd819fb255fd2...      3.059635
10       8acf44c01070626f2dfe8f14a8f1c82c1dc54ce4439dd6...      3.058188
11       e3d9eef16674a150d3b09b8ac6044d6d8643816d79efc4...      3.058486
12       c39fc9a2e76af52670ec5ec7c22280553700367864f300...      3.059361
13       7cc5747f57774f0085e5db12f68edc716fb899ea6678c2...      3.058004
14       265dd233bb319ee2d21e764949c113c406edb1a83d0ed7...      3.058593
15       486c25ef692b50505eebf3dc6ee6d45d3838c32d58777f...      3.058763
16       b97c739828f7a04e65fd966eba32174a830d545e6802d5...      3.058876
17       9577121f52402a3482dfdd269a6586add965fb64240b66...      3.058004
18       f52211c94bd668ee0272940ac83bcf8ee1519ff6a7094b...      3.060068
19       45002de58bf16bd5f10fa211ccc6e77a44bb113a595d0e...      3.056486
20       07d13e3a7473027d0f8cb683ddc8f62ca53695ab9b4655...      3.059801
21       ef48ecfb7c098bb9530aea89096cf8b6f7aa588f06e12a...      3.057635

                                                bsr_report
seq_num
1        {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
2        {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
3        {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
4        {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
5        {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
6        {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
7        {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
8        {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
9        {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
10       {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
11       {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
12       {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
13       {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
14       {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
15       {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
16       {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
17       {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
18       {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
19       {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
20       {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
21       {"angxpzp": [0.0, -1.7793448589854113e-20, -3....
../_images/notebooks_shadow_6_1.png
[4]:
import json
import matplotlib.pyplot as plt

for idx in range(1, tbl["toroid_r_maj"].size + 1):
    d = json.loads(tbl["bsr_report"][idx])
    plt.figure(figsize=(16, 4))
    plt.plot(d["s"], d["sigmax"], label="Horizonal RMS Beam Size [m]")
    plt.plot(d["s"], d["sigmaz"], label="Vertical RMS Beam Size [m]")
    plt.xlabel("Distance Along Beamline [m]")
    plt.legend()
    plt.title(f"toroid_r_maj = {tbl['toroid_r_maj'][idx]:.0f}")
/tmp/ipykernel_5424/2351232241.py:6: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`). Consider using `matplotlib.pyplot.close()`.
  plt.figure(figsize=(16, 4))
../_images/notebooks_shadow_7_1.png
../_images/notebooks_shadow_7_2.png
../_images/notebooks_shadow_7_3.png
../_images/notebooks_shadow_7_4.png
../_images/notebooks_shadow_7_5.png
../_images/notebooks_shadow_7_6.png
../_images/notebooks_shadow_7_7.png
../_images/notebooks_shadow_7_8.png
../_images/notebooks_shadow_7_9.png
../_images/notebooks_shadow_7_10.png
../_images/notebooks_shadow_7_11.png
../_images/notebooks_shadow_7_12.png
../_images/notebooks_shadow_7_13.png
../_images/notebooks_shadow_7_14.png
../_images/notebooks_shadow_7_15.png
../_images/notebooks_shadow_7_16.png
../_images/notebooks_shadow_7_17.png
../_images/notebooks_shadow_7_18.png
../_images/notebooks_shadow_7_19.png
../_images/notebooks_shadow_7_20.png
../_images/notebooks_shadow_7_21.png
[5]:
data = np.array(tbl["time"].diff(), dtype=float)[1:] / 1e9

fig = plt.figure()
ax = fig.add_subplot()
ax.plot(np.linspace(*scan_range)[1:], data, label="Duration of simulations")
ax.set_ylabel("Duration of simulations [s]")
ax.set_xlabel("Torus Major Radius [m]")
ax.axhline(data.mean(), color="k", label="Averaged duration")
ax.legend()
title = (
    f"Shadow TES simulation\n"
    f"RE(bp.scan([bsr], toroid.r_maj, "
    f"{', '.join([str(x) for x in scan_range])}))"
)
ax.set_title(title)
plt.show()
../_images/notebooks_shadow_8_0.png