-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added bloptools example to documentation
- Loading branch information
1 parent
39a7d09
commit 0773698
Showing
3 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ Examples | |
notebooks/srw.ipynb | ||
notebooks/shadow.ipynb | ||
notebooks/madx.ipynb | ||
notebooks/optimization.ipynb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Optimize the beamline\n", | ||
"\n", | ||
"In this example, we use the `bloptools` package to optimize the beamline.\n", | ||
"\n", | ||
"We set up the simulation as we normally do:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from sirepo_bluesky import prepare_re_env\n", | ||
"\n", | ||
"%run -i $prepare_re_env.__file__\n", | ||
"\n", | ||
"from sirepo_bluesky.sirepo_bluesky import SirepoBluesky\n", | ||
"from sirepo_bluesky.sirepo_ophyd import create_classes\n", | ||
"\n", | ||
"connection = SirepoBluesky(\"http://localhost:8000\")\n", | ||
"\n", | ||
"data, schema = connection.auth(\"shadow\", sim_id=\"00000002\")\n", | ||
"classes, objects = create_classes(connection=connection)\n", | ||
"globals().update(**objects)\n", | ||
"\n", | ||
"bec.disable_plots()\n", | ||
"\n", | ||
"aperture.horizontalSize.kind = \"hinted\"\n", | ||
"w9.duration.kind = \"hinted\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We can then instantiate an `Agent` from the `bloptools` package; because it can run any experiment defined with a Bluesky plan, we can give it any of our Sirepo components to use as degrees of freedom. We define the objectives as to maximize the flux density of the beamline." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from bloptools.bayesian import Agent, DOF, Objective\n", | ||
"\n", | ||
"dofs = [\n", | ||
" DOF(\n", | ||
" device=kbh.x_rot,\n", | ||
" name=\"KBH rotation offset\",\n", | ||
" limits=(-5e-2, +5e-2),\n", | ||
" tags=[\"kb\", \"kbv\"],\n", | ||
" units=\"deg\",\n", | ||
" ),\n", | ||
" DOF(\n", | ||
" device=kbv.x_rot,\n", | ||
" name=\"KBV rotation offset\",\n", | ||
" limits=(-5e-2, +5e-2),\n", | ||
" tags=[\"kb\", \"kbv\"],\n", | ||
" units=\"deg\",\n", | ||
" ),\n", | ||
"]\n", | ||
"\n", | ||
"# composite\n", | ||
"objectives = [\n", | ||
" Objective(key=\"w9_flux\", name=\"total flux\", limits=(100, np.inf), log=True),\n", | ||
" Objective(key=\"w9_fwhm_x\", name=\"horizontal FWHM\", minimize=True, log=True),\n", | ||
" Objective(key=\"w9_fwhm_y\", name=\"vertical FWHM\", minimize=True, log=True),\n", | ||
"]\n", | ||
"\n", | ||
"\n", | ||
"agent = Agent(\n", | ||
" dofs=dofs,\n", | ||
" dets=[w9],\n", | ||
" objectives=objectives,\n", | ||
" db=db,\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We start by running a scan over a quasi-random sample of input parameters, and plot the result:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"RE(agent.learn(acq_func=\"qr\", n=24))\n", | ||
"agent.plot_objectives()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We then use a more intelligent strategy (\"$q$-expected mean\") to narrow in on the optimum." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"RE(agent.learn(\"qem\", iterations=4))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We can see the convergence of our model:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"agent.plot_history()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters