diff --git a/docs/examples/particle_examples.ipynb b/docs/examples/particle_examples.ipynb index 60afff8..fef7f5d 100644 --- a/docs/examples/particle_examples.ipynb +++ b/docs/examples/particle_examples.ipynb @@ -7,16 +7,6 @@ "# openPMD beamphysics examples" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%load_ext autoreload\n", - "%autoreload 2" - ] - }, { "cell_type": "code", "execution_count": null, @@ -387,57 +377,6 @@ "P.resample(1000).plot(\"x\", \"px\", bins=100)" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Splitting\n", - "\n", - "It is often useful to split particles along a key dimension for analysis. This method will split into chunks with approximately equal numbers of particles." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "P.split(n_chunks=3, key=\"z\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Fractional split will use weights to partition the particles. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "P.fractional_split(fractions=[0.1, 0.9], key=\"z\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This is useful for splitting particles into head, core, and tail parts. Here, the 5% of the charge is in the tail, 90% is in the core, and 5 % is in the head:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "for p in P.fractional_split(key=\"z\", fractions=[0.05, 0.95]):\n", - " p.plot(\"z\", \"energy\", bins=100, figsize=(3, 3))" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -662,14 +601,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:18.461634Z", - "iopub.status.busy": "2024-10-17T23:20:18.461561Z", - "iopub.status.idle": "2024-10-17T23:20:18.462964Z", - "shell.execute_reply": "2024-10-17T23:20:18.462746Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import h5py\n", @@ -679,14 +611,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:18.464211Z", - "iopub.status.busy": "2024-10-17T23:20:18.464142Z", - "iopub.status.idle": "2024-10-17T23:20:18.473958Z", - "shell.execute_reply": "2024-10-17T23:20:18.473683Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "newh5file = \"particles.h5\"\n", @@ -708,14 +633,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:18.475337Z", - "iopub.status.busy": "2024-10-17T23:20:18.475258Z", - "iopub.status.idle": "2024-10-17T23:20:18.478010Z", - "shell.execute_reply": "2024-10-17T23:20:18.477801Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "for key in [\"x\", \"px\", \"y\", \"py\", \"z\", \"pz\", \"t\", \"status\", \"weight\", \"id\"]:\n", @@ -733,14 +651,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:18.479257Z", - "iopub.status.busy": "2024-10-17T23:20:18.479167Z", - "iopub.status.idle": "2024-10-17T23:20:18.483688Z", - "shell.execute_reply": "2024-10-17T23:20:18.483461Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "P2 == P" @@ -756,14 +667,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:18.484951Z", - "iopub.status.busy": "2024-10-17T23:20:18.484870Z", - "iopub.status.idle": "2024-10-17T23:20:19.077238Z", - "shell.execute_reply": "2024-10-17T23:20:19.076925Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "P.write_astra(\"astra.dat\")" @@ -772,14 +676,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:19.078673Z", - "iopub.status.busy": "2024-10-17T23:20:19.078564Z", - "iopub.status.idle": "2024-10-17T23:20:19.205207Z", - "shell.execute_reply": "2024-10-17T23:20:19.204803Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "!head astra.dat" @@ -795,14 +692,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:19.207284Z", - "iopub.status.busy": "2024-10-17T23:20:19.206964Z", - "iopub.status.idle": "2024-10-17T23:20:19.217583Z", - "shell.execute_reply": "2024-10-17T23:20:19.217277Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "P.write(\"particles.h5\")" @@ -827,14 +717,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:19.219281Z", - "iopub.status.busy": "2024-10-17T23:20:19.219174Z", - "iopub.status.idle": "2024-10-17T23:20:19.628355Z", - "shell.execute_reply": "2024-10-17T23:20:19.628099Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "P.plot(\"x\")" @@ -850,14 +733,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:19.629649Z", - "iopub.status.busy": "2024-10-17T23:20:19.629563Z", - "iopub.status.idle": "2024-10-17T23:20:19.849704Z", - "shell.execute_reply": "2024-10-17T23:20:19.849459Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "P.slice_plot(\"norm_emit_x\", \"norm_emit_y\", ylim=(0, 1e-6))" @@ -866,14 +742,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:19.850981Z", - "iopub.status.busy": "2024-10-17T23:20:19.850871Z", - "iopub.status.idle": "2024-10-17T23:20:20.158771Z", - "shell.execute_reply": "2024-10-17T23:20:20.158531Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "P.plot(\"z\", \"x\")" @@ -889,14 +758,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:20.160286Z", - "iopub.status.busy": "2024-10-17T23:20:20.160188Z", - "iopub.status.idle": "2024-10-17T23:20:20.334573Z", - "shell.execute_reply": "2024-10-17T23:20:20.334321Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "P.slice_plot(\"sigma_x\", slice_key=\"Jx\")" @@ -912,14 +774,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:20.335992Z", - "iopub.status.busy": "2024-10-17T23:20:20.335891Z", - "iopub.status.idle": "2024-10-17T23:20:20.643612Z", - "shell.execute_reply": "2024-10-17T23:20:20.643380Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "P.plot(\"x\", \"px\", ellipse=True)" @@ -935,14 +790,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:20.645203Z", - "iopub.status.busy": "2024-10-17T23:20:20.645094Z", - "iopub.status.idle": "2024-10-17T23:20:21.062886Z", - "shell.execute_reply": "2024-10-17T23:20:21.062635Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "fig = P.plot(\"x\", return_figure=True)\n", @@ -961,14 +809,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:21.064216Z", - "iopub.status.busy": "2024-10-17T23:20:21.064132Z", - "iopub.status.idle": "2024-10-17T23:20:21.139837Z", - "shell.execute_reply": "2024-10-17T23:20:21.139604Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import copy\n", @@ -990,14 +831,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:21.141053Z", - "iopub.status.busy": "2024-10-17T23:20:21.140946Z", - "iopub.status.idle": "2024-10-17T23:20:21.494659Z", - "shell.execute_reply": "2024-10-17T23:20:21.494422Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "P.plot(\"delta_z\", \"delta_p\", figsize=(8, 6))" @@ -1013,14 +847,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:21.496329Z", - "iopub.status.busy": "2024-10-17T23:20:21.496222Z", - "iopub.status.idle": "2024-10-17T23:20:21.599826Z", - "shell.execute_reply": "2024-10-17T23:20:21.599600Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "H, edges = P.histogramdd(\"delta_z\", \"delta_p\", bins=(150, 150))\n", @@ -1029,6 +856,57 @@ "plt.imshow(H.T, origin=\"lower\", extent=extent, aspect=\"auto\", vmin=1e-15, cmap=cmap)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Splitting\n", + "\n", + "It is often useful to split particles along a key dimension for analysis. This method will split into chunks with approximately equal numbers of particles." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "P.split(n_chunks=3, key=\"z\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Fractional split will use weights to partition the particles. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "P.fractional_split(fractions=[0.1, 0.9], key=\"z\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is useful for splitting particles into head, core, and tail parts. Here, the 5% of the charge is in the tail, 90% is in the core, and 5 % is in the head:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for p in P.fractional_split(key=\"z\", fractions=[0.05, 0.95]):\n", + " p.plot(\"z\", \"energy\", bins=100, figsize=(3, 3))" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1041,14 +919,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:21.601117Z", - "iopub.status.busy": "2024-10-17T23:20:21.601028Z", - "iopub.status.idle": "2024-10-17T23:20:21.603036Z", - "shell.execute_reply": "2024-10-17T23:20:21.602808Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "from pmd_beamphysics import particle_paths\n", @@ -1068,14 +939,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:21.604195Z", - "iopub.status.busy": "2024-10-17T23:20:21.604119Z", - "iopub.status.idle": "2024-10-17T23:20:21.606826Z", - "shell.execute_reply": "2024-10-17T23:20:21.606618Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "ppaths = particle_paths(h5)\n", @@ -1092,14 +956,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:21.608025Z", - "iopub.status.busy": "2024-10-17T23:20:21.607926Z", - "iopub.status.idle": "2024-10-17T23:20:21.610352Z", - "shell.execute_reply": "2024-10-17T23:20:21.610162Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "ph5 = h5[ppaths[0]]\n", @@ -1116,14 +973,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:21.611431Z", - "iopub.status.busy": "2024-10-17T23:20:21.611344Z", - "iopub.status.idle": "2024-10-17T23:20:21.615151Z", - "shell.execute_reply": "2024-10-17T23:20:21.614931Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "for component in all_components(ph5):\n", @@ -1142,12 +992,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:20:21.616286Z", - "iopub.status.busy": "2024-10-17T23:20:21.616194Z", - "iopub.status.idle": "2024-10-17T23:20:21.618548Z", - "shell.execute_reply": "2024-10-17T23:20:21.618357Z" - }, "tags": [] }, "outputs": [],