FieldMesh Conversion¶
InĀ [1]:
Copied!
from beamphysics import FieldMesh
import numpy as np
import matplotlib.pyplot as plt
from beamphysics import FieldMesh
import numpy as np
import matplotlib.pyplot as plt
2D cylindrically symmetric RF gun FieldMesh¶
This data was originally generated using Superfish.
InĀ [2]:
Copied!
FM1 = FieldMesh("../data/rfgun.h5")
FM1.plot("re_E", aspect="equal", figsize=(12, 4))
FM1 = FieldMesh("../data/rfgun.h5")
FM1.plot("re_E", aspect="equal", figsize=(12, 4))
3D rectangular field from ANSYS¶
InĀ [3]:
Copied!
FM3D = FieldMesh.from_ansys_ascii_3d(
efile="../data/ansys_rfgun_2856MHz_E.dat",
hfile="../data/ansys_rfgun_2856MHz_H.dat",
frequency=2856e6,
)
FM3D
FM3D = FieldMesh.from_ansys_ascii_3d(
efile="../data/ansys_rfgun_2856MHz_E.dat",
hfile="../data/ansys_rfgun_2856MHz_H.dat",
frequency=2856e6,
)
FM3D
Out[3]:
<FieldMesh with rectangular geometry and (3, 3, 457) shape at 0x7f91cb14e710>
This will convert to 2D cylindrical:
InĀ [4]:
Copied!
FM2 = FM3D.to_cylindrical()
FM2
FM2 = FM3D.to_cylindrical()
FM2
Out[4]:
<FieldMesh with cylindrical geometry and (2, 1, 457) shape at 0x7f91cb14e850>
Spacing is different:
InĀ [5]:
Copied!
FM1.dr, FM2.dr
FM1.dr, FM2.dr
Out[5]:
(np.float64(0.00025), np.float64(0.001))
Set a scale to rotate and normalize Ez to be mostly real
InĀ [6]:
Copied!
E0 = FM2.components["electricField/z"][0, 0, 0]
FM2.scale = -np.exp(-1j * np.angle(E0)) / np.abs(E0) # - sign to agree with FM1
FM2.Ez[0, 0, 0]
E0 = FM2.components["electricField/z"][0, 0, 0]
FM2.scale = -np.exp(-1j * np.angle(E0)) / np.abs(E0) # - sign to agree with FM1
FM2.Ez[0, 0, 0]
Out[6]:
np.complex128(-1-1.890985933883628e-16j)
InĀ [7]:
Copied!
z1 = FM1.coord_vec("z")
z2 = FM2.coord_vec("z")
fig, ax = plt.subplots()
ax.plot(z1, np.real(FM1.Ez[0, 0, :]), label="Superfish")
ax.plot(z2, np.real(FM2.Ez[0, 0, :]), label="ANSYS")
ax.set_xlabel(r"$z$ (m)")
ax.set_ylabel(r"$E_z$ (V/m)")
plt.legend()
z1 = FM1.coord_vec("z")
z2 = FM2.coord_vec("z")
fig, ax = plt.subplots()
ax.plot(z1, np.real(FM1.Ez[0, 0, :]), label="Superfish")
ax.plot(z2, np.real(FM2.Ez[0, 0, :]), label="ANSYS")
ax.set_xlabel(r"$z$ (m)")
ax.set_ylabel(r"$E_z$ (V/m)")
plt.legend()
Out[7]:
<matplotlib.legend.Legend at 0x7f91cafc1400>
InĀ [8]:
Copied!
fig, ax = plt.subplots()
ax.plot(z1, np.imag(FM1.Btheta[4, 0, :]), label="superfish")
ax.plot(z2, np.imag(FM2.Btheta[1, 0, :]), label="ansys")
ax.set_title(rf"$r$ = {FM2.dr*1000} mm")
ax.set_xlabel(r"$z$ (m)")
ax.set_ylabel(r"$B_\theta$ (T)")
plt.legend()
fig, ax = plt.subplots()
ax.plot(z1, np.imag(FM1.Btheta[4, 0, :]), label="superfish")
ax.plot(z2, np.imag(FM2.Btheta[1, 0, :]), label="ansys")
ax.set_title(rf"$r$ = {FM2.dr*1000} mm")
ax.set_xlabel(r"$z$ (m)")
ax.set_ylabel(r"$B_\theta$ (T)")
plt.legend()
Out[8]:
<matplotlib.legend.Legend at 0x7f91caefc440>
The magnetic field is out of phase, so use the im_ syntax:
InĀ [9]:
Copied!
FM2.plot("im_Btheta", aspect="equal", figsize=(12, 4))
FM2.plot("im_Btheta", aspect="equal", figsize=(12, 4))
Max on-axis field:
InĀ [10]:
Copied!
np.abs(FM2.Ez[0, 0, :]).max()
np.abs(FM2.Ez[0, 0, :]).max()
Out[10]:
np.float64(1.0153992993439902)
Verify the oscillation¶
Complex fields oscillate as $e^{-i\omega t}$. For TM fields, the spatial components $E_z$ and $B_\theta$ near the axis
$\Re E_{z} = -\frac{r}{2}\frac{\omega}{c^2} \Im B_\theta$
InĀ [11]:
Copied!
def check_oscillation(FM, label=""):
c_light = 299792458.0
dr = FM.dr
omega = FM.frequency * 2 * np.pi
# Check the first off-axis grid points
z0 = FM.z
Ez0 = np.real(FM.Ez[0, 0, :])
B1 = -np.imag(FM.Btheta[1, 0, :])
plt.plot(z0, Ez0, label=r"$\Re \left( E_z\right)$")
plt.plot(
z0,
B1 * 2 / dr * c_light**2 / omega,
"--",
label=r"$-\frac{r}{2}\frac{\omega}{c^2} \Im\left(B_\theta\right)$",
)
plt.ylabel("field (V/m)")
plt.xlabel("z (m)")
plt.legend()
plt.title(rf"Complex field oscillation{label}")
def check_oscillation(FM, label=""):
c_light = 299792458.0
dr = FM.dr
omega = FM.frequency * 2 * np.pi
# Check the first off-axis grid points
z0 = FM.z
Ez0 = np.real(FM.Ez[0, 0, :])
B1 = -np.imag(FM.Btheta[1, 0, :])
plt.plot(z0, Ez0, label=r"$\Re \left( E_z\right)$")
plt.plot(
z0,
B1 * 2 / dr * c_light**2 / omega,
"--",
label=r"$-\frac{r}{2}\frac{\omega}{c^2} \Im\left(B_\theta\right)$",
)
plt.ylabel("field (V/m)")
plt.xlabel("z (m)")
plt.legend()
plt.title(rf"Complex field oscillation{label}")
InĀ [12]:
Copied!
check_oscillation(FM1, ", Superfish")
check_oscillation(FM1, ", Superfish")
InĀ [13]:
Copied!
check_oscillation(FM2, ", ANSYS")
check_oscillation(FM2, ", ANSYS")