Magnetic fields#

Here we illustrate how to define and work with magnetic fields, using the atomsmltr.environment.fields.magnetic subpackage

magnetic field offset#

from atomsmltr.environment import MagneticOffset

# offset pointing along y, with amplitude of 0.1T
mag_offset = MagneticOffset(field_value=(0,0.1,0), tag="offset")

# print infos
mag_offset.print_info()

# check value at one position
display(mag_offset.get_value((1,2,3)))
─────────────────────────
| Magnetic field offset |
─────────────────────────
. Parameters :
  β”œβ”€β”€ type : constant field
  β”œβ”€β”€ tag : offset
  β”œβ”€β”€ field_value (T) : [0.  0.1 0. ]
  └── norm (T) : 0.1
array([0. , 0.1, 0. ])

magnetic field gradient#

from atomsmltr.environment import MagneticGradient
import numpy as np
import matplotlib.pyplot as plt

# offset pointing along x, with a slope of 0.5T/m along z
mag_gradient = MagneticGradient(
    origin=(0, 0, 0),
    slope=0.5,
    gradient_direction=(0, 0, 1),
    field_direction=(1, 0, 0),
    offset=0,
    tag="gradient",
)

# print infos
mag_gradient.print_info()

# compute values along z
zlist = np.linspace(-5,5,1000)
r = np.array([[0,0,z] for z in zlist])
B = mag_gradient.get_value(r)

# get components
Bx, By, Bz = B.T

# plot
plt.figure(figsize=(4,3))
plt.plot(zlist, Bx, label="Bx")
plt.plot(zlist, By, label="By")
plt.plot(zlist, Bz, label="Bz")
plt.grid()
plt.ylabel("Field value (T)")
plt.xlabel("z (m)")
plt.legend()
plt.show()
───────────────────────────
| Magnetic field gradient |
───────────────────────────
. Parameters :
  β”œβ”€β”€ type : perfect gradient
  β”œβ”€β”€ tag : gradient
  β”œβ”€β”€ slope (T/m) : 0.5
  β”œβ”€β”€ gradient direction : [0. 0. 1.]
  β”œβ”€β”€ field direction : [1. 0. 0.]
  β”œβ”€β”€ origin (m) : [0 0 0]
  └── offset (T) :   0
../../_images/3ade841225f8472f7b7d64fdd1d2f1ed5b3ef19b36458df0af78c639c75e471b.png

we can also use some built-in plot functions

limits = (-10, 10, -10, 10, -10, 10)
Npoints = (2, 3, 10)
mag_gradient.plot3D(
    limits=limits,
    Npoints=Npoints,
    show=True,
    color="C2",
    normalize=True,
    scale=5,
)
../../_images/3f04479823e966dcd325213a283677135cafebe13977a5df22303d4485d1d5c6.png
<Axes3D: xlabel='x', ylabel='y', zlabel='z'>

magnetic field quadrupole#

from atomsmltr.environment import MagneticQuadrupoleX

quadX = MagneticQuadrupoleX(origin=(0, 0, 0), slope=0.5)

Npoints = 50
limits = (-10, 10, -10, 10)
fig, axes = plt.subplots(1, 3, figsize=(10, 3), tight_layout=True)
quadX.plot2D(plane="XY", limits=limits, Npoints=Npoints, ax=axes[0])
quadX.plot2D(plane="YZ", limits=limits, Npoints=Npoints, ax=axes[1])
quadX.plot2D(plane="ZX", limits=limits, Npoints=Npoints, ax=axes[2])
plt.show()
../../_images/d17216a5e452b8a9319410e1a2561b88bf4dea3afb94a1ff7937a3e6b2898a3c.png

We can also get values β€˜by hand’ and plot them

from atomsmltr.environment import MagneticQuadrupoleZ
import numpy as np
import matplotlib.pyplot as plt

# init magnetic field
magfield = MagneticQuadrupoleZ((0,0,0), 0.1)

# prepare a grid of positions, spanning over the (x,y) plane at z=0
grid = np.mgrid[-10:10:100j, -10:10:101j, 0:0:1j]
grid = np.squeeze(grid)  # remove extra dimensions
X, Y, _ = grid
X, Y = X.T, Y.T 
position = grid.T

# compute field values
B = magfield.get_value(position)
B_norm = magfield.get_norm(position)

# get components
Bx, By, Bz = B.T 
Bx, By, Bz = Bx.T, By.T, Bz.T

# plot

fig, axes = plt.subplots(2, 2, figsize=(8,8), tight_layout=True)

axes[0][0].pcolormesh(X, Y, Bx, cmap="seismic")
axes[0][0].set_title("Bx")
axes[0][1].pcolormesh(X, Y, By, cmap="seismic")
axes[0][1].set_title("By")
axes[1][0].pcolormesh(X, Y, Bz, cmap="seismic", vmin=-1, vmax=1)
axes[1][0].set_title("Bz")
axes[1][1].pcolormesh(X, Y, B_norm)
axes[1][1].set_title("||B||")

for ax in axes.ravel():
    ax.set_xlabel("x")
    ax.set_ylabel("y")
plt.show()
../../_images/0b7ce259a939c414ecc34b6137dfc380887ff3ef089ec482005b2354d1873ab0.png