Skip to content

Commit 0a307ea

Browse files
Fixed convolve_density in impedance model
1 parent b7f4e18 commit 0a307ea

File tree

4 files changed

+266
-28
lines changed

4 files changed

+266
-28
lines changed

docs/examples/wakefields/resistive_wall_impedance.ipynb

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@
179179
"outputs": [],
180180
"source": [
181181
"# Compute wakefield arrays (slow due to numerical integration)\n",
182-
"zs = np.linspace(0, 200e-6, 30)\n",
182+
"# Wake is defined for z < 0 (behind the source particle)\n",
183+
"zs = -np.linspace(1e-6, 200e-6, 30) # negative z values\n",
183184
"Wz_flat = imp_flat.wakefield(zs, k_max=1e6)\n",
184185
"Wz_round = imp_round.wakefield(zs, k_max=1e6)"
185186
]
@@ -192,9 +193,9 @@
192193
"outputs": [],
193194
"source": [
194195
"fig, ax = plt.subplots(figsize=(8, 4))\n",
195-
"ax.plot(zs * 1e6, Wz_flat * 1e-12, label=\"Flat\")\n",
196-
"ax.plot(zs * 1e6, Wz_round * 1e-12, \"--\", label=\"Round\")\n",
197-
"ax.set_xlabel(r\"$z$ (µm)\")\n",
196+
"ax.plot(-zs * 1e6, Wz_flat * 1e-12, label=\"Flat\") # plot as distance behind source\n",
197+
"ax.plot(-zs * 1e6, Wz_round * 1e-12, \"--\", label=\"Round\")\n",
198+
"ax.set_xlabel(r\"Distance behind source $|z|$ (µm)\")\n",
198199
"ax.set_ylabel(r\"$W(z)$ (V/pC/m)\")\n",
199200
"ax.legend()\n",
200201
"ax.set_title(\"Wakefield Comparison: Flat vs Round Geometry\")"
@@ -251,11 +252,10 @@
251252
"outputs": [],
252253
"source": [
253254
"# Evaluate pseudomode wakefields (very fast!)\n",
254-
"zs_fine = np.linspace(0, 200e-6, 200)\n",
255-
"\n",
256-
"# Note: pseudomode expects negative z (trailing the source)\n",
257-
"Wz_pseudo_flat = wake_flat.pseudomode(-zs_fine)\n",
258-
"Wz_pseudo_round = wake_round.pseudomode(-zs_fine)"
255+
"# Both methods use z < 0 for trailing particles\n",
256+
"zs_fine = -np.linspace(1e-6, 200e-6, 200) # negative z values\n",
257+
"Wz_pseudo_flat = wake_flat.pseudomode(zs_fine)\n",
258+
"Wz_pseudo_round = wake_round.pseudomode(zs_fine)"
259259
]
260260
},
261261
{
@@ -269,17 +269,17 @@
269269
"fig, axes = plt.subplots(1, 2, figsize=(12, 5))\n",
270270
"\n",
271271
"ax = axes[0]\n",
272-
"ax.plot(zs * 1e6, Wz_flat * 1e-12, \"o\", label=\"Numerical\", markersize=5)\n",
273-
"ax.plot(zs_fine * 1e6, Wz_pseudo_flat * 1e-12, \"-\", label=\"Pseudomode\", alpha=0.8)\n",
274-
"ax.set_xlabel(r\"$z$ (µm)\")\n",
272+
"ax.plot(-zs * 1e6, Wz_flat * 1e-12, \"o\", label=\"Numerical\", markersize=5)\n",
273+
"ax.plot(-zs_fine * 1e6, Wz_pseudo_flat * 1e-12, \"-\", label=\"Pseudomode\", alpha=0.8)\n",
274+
"ax.set_xlabel(r\"Distance behind source $|z|$ (µm)\")\n",
275275
"ax.set_ylabel(r\"$W(z)$ (V/pC/m)\")\n",
276276
"ax.legend()\n",
277277
"ax.set_title(\"Flat Geometry\")\n",
278278
"\n",
279279
"ax = axes[1]\n",
280-
"ax.plot(zs * 1e6, Wz_round * 1e-12, \"o\", label=\"Numerical\", markersize=5)\n",
281-
"ax.plot(zs_fine * 1e6, Wz_pseudo_round * 1e-12, \"-\", label=\"Pseudomode\", alpha=0.8)\n",
282-
"ax.set_xlabel(r\"$z$ (µm)\")\n",
280+
"ax.plot(-zs * 1e6, Wz_round * 1e-12, \"o\", label=\"Numerical\", markersize=5)\n",
281+
"ax.plot(-zs_fine * 1e6, Wz_pseudo_round * 1e-12, \"-\", label=\"Pseudomode\", alpha=0.8)\n",
282+
"ax.set_xlabel(r\"Distance behind source $|z|$ (µm)\")\n",
283283
"ax.set_ylabel(r\"$W(z)$ (V/pC/m)\")\n",
284284
"ax.legend()\n",
285285
"ax.set_title(\"Round Geometry\")\n",
@@ -307,7 +307,7 @@
307307
"source": [
308308
"%%timeit -n 1 -r 3\n",
309309
"# Numerical integration for 20 points\n",
310-
"z_test = np.linspace(0, 100e-6, 20)\n",
310+
"z_test = -np.linspace(1e-6, 100e-6, 20)\n",
311311
"_ = imp_flat.wakefield(z_test, k_max=1e6)"
312312
]
313313
},
@@ -320,8 +320,8 @@
320320
"source": [
321321
"%%timeit -n 1000 -r 3\n",
322322
"# Pseudomode for 1000 points\n",
323-
"z_test = np.linspace(0, 100e-6, 1000)\n",
324-
"_ = wake_flat.pseudomode(-z_test)"
323+
"z_test = -np.linspace(1e-6, 100e-6, 1000)\n",
324+
"_ = wake_flat.pseudomode(z_test)"
325325
]
326326
},
327327
{
@@ -341,6 +341,18 @@
341341
"- Working with unusual parameter regimes\n",
342342
"- Need access to the impedance spectrum $Z(k)$"
343343
]
344+
},
345+
{
346+
"cell_type": "markdown",
347+
"id": "592c26c6",
348+
"metadata": {},
349+
"source": []
350+
},
351+
{
352+
"cell_type": "markdown",
353+
"id": "b76be5fd",
354+
"metadata": {},
355+
"source": []
344356
}
345357
],
346358
"metadata": {

docs/examples/wakefields/resistive_wall_wakefield.ipynb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
"- Bane, Stupakov, Tu, [EPAC 2006 THPCH073](https://accelconf.web.cern.ch/e06/PAPERS/THPCH073.PDF)"
1919
]
2020
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"id": "dbaddcb4-a6ff-42ac-b55a-4395b4cb9ef0",
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"%load_ext autoreload\n",
29+
"%autoreload 2"
30+
]
31+
},
2132
{
2233
"cell_type": "code",
2334
"execution_count": null,
@@ -262,6 +273,75 @@
262273
"P.drift_to_z()\n",
263274
"P.wakefield_plot(wake_ref, figsize=(12, 4))"
264275
]
276+
},
277+
{
278+
"cell_type": "code",
279+
"execution_count": null,
280+
"id": "d867b96b-4047-405a-85ca-008185bb8d11",
281+
"metadata": {},
282+
"outputs": [],
283+
"source": [
284+
"P.drift_to_t()\n",
285+
"H, b = P.histogramdd(\"z\", bins=100)\n",
286+
"dz = np.diff(b[0])[0]\n",
287+
"density = H / dz\n",
288+
"n = len(H)\n",
289+
"z = np.arange(0, n, 1) * dz\n",
290+
"\n",
291+
"v = wake.convolve_density(density, dz=dz)\n",
292+
"plt.plot(z, v)\n",
293+
"\n",
294+
"wp = -wake.pseudomode.particle_kicks(P.z, P.weight)\n",
295+
"zp = P.z - P.z.min()\n",
296+
"plt.scatter(zp, wp)"
297+
]
298+
},
299+
{
300+
"cell_type": "code",
301+
"execution_count": null,
302+
"id": "fdbe6d76-b402-4e5f-ae9c-fa78a3a0456e",
303+
"metadata": {},
304+
"outputs": [],
305+
"source": [
306+
"from pmd_beamphysics.wakefields import ResistiveWallImpedance"
307+
]
308+
},
309+
{
310+
"cell_type": "code",
311+
"execution_count": null,
312+
"id": "9834d67e-b2cb-4312-b71e-453dcbb4e99a",
313+
"metadata": {},
314+
"outputs": [],
315+
"source": []
316+
},
317+
{
318+
"cell_type": "code",
319+
"execution_count": null,
320+
"id": "2b8e3018-d376-45c1-a77c-671eb1153c22",
321+
"metadata": {},
322+
"outputs": [],
323+
"source": [
324+
"imp = ResistiveWallImpedance(\n",
325+
" radius=0.0025,\n",
326+
" conductivity=6.5e7, # σ [S/m]\n",
327+
" relaxation_time=27e-15, # τ [s]\n",
328+
" geometry=\"round\",\n",
329+
")\n",
330+
"\n",
331+
"wi = imp.convolve_density(density, dz=dz)\n",
332+
"plt.plot(z, wi)"
333+
]
334+
},
335+
{
336+
"cell_type": "code",
337+
"execution_count": null,
338+
"id": "c4ab6d5c-9ff5-4a83-b119-0b01710b0134",
339+
"metadata": {},
340+
"outputs": [],
341+
"source": [
342+
"plt.plot(z, wi)\n",
343+
"plt.plot(z, v)"
344+
]
265345
}
266346
],
267347
"metadata": {

pmd_beamphysics/wakefields/resistive_wall.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,10 +729,10 @@ def convolve_density(self, density, dz, offset=0):
729729
----------
730730
731731
density: ndarray
732-
charge density array
732+
charge density array in C / m
733733
734734
dz: float
735-
array spacing
735+
array spacing in m
736736
737737
offset : float
738738
Offset coordinates for the center of the grid in [m]. Default: 0

0 commit comments

Comments
 (0)