Interactive online version: .

[1]:

%matplotlib inline


# Magnetostatic Fields¶

An example of using PlasmaPy’s Magnetostatic class in physics subpackage.

[2]:

import astropy.units as u
import matplotlib.pyplot as plt
import numpy as np

from plasmapy.formulary import magnetostatics
from plasmapy.plasma.sources import Plasma3D


Some common magnetostatic fields can be generated and added to a plasma object. A dipole

[3]:

dipole = magnetostatics.MagneticDipole(
np.array([0, 0, 1]) * u.A * u.m * u.m, np.array([0, 0, 0]) * u.m
)
print(dipole)

MagneticDipole(moment=[0. 0. 1.]A m2, p0=[0. 0. 0.]m)


initialize a a plasma, where the magnetic field will be calculated on

[4]:

plasma = Plasma3D(
domain_x=np.linspace(-2, 2, 30) * u.m,
domain_y=np.linspace(0, 0, 1) * u.m,
domain_z=np.linspace(-2, 2, 20) * u.m,
)


add the dipole field to it

[5]:

plasma.add_magnetostatic(dipole)

X, Z = plasma.grid[0, :, 0, :], plasma.grid[2, :, 0, :]
U = plasma.magnetic_field[0, :, 0, :].value.T  # because grid uses 'ij' indexing
W = plasma.magnetic_field[2, :, 0, :].value.T  # because grid uses 'ij' indexing

[6]:

plt.figure()
plt.axis("square")
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.title(
"Dipole field in x-z plane, generated by a dipole pointing in the z direction"
)
plt.streamplot(plasma.x.value, plasma.z.value, U, W)

[6]:

<matplotlib.streamplot.StreamplotSet at 0x7ff18dee3690>


A circular current-carring wire (CircularWire)

[7]:

cw = magnetostatics.CircularWire(
np.array([0, 0, 1]), np.array([0, 0, 0]) * u.m, 1 * u.m, 1 * u.A
)
print(cw)

CircularWire(normal=[0. 0. 1.], center=[0. 0. 0.]m, radius=1.0m, current=1.0A)


initialize a a plasma, where the magnetic field will be calculated on

[8]:

plasma = Plasma3D(
domain_x=np.linspace(-2, 2, 30) * u.m,
domain_y=np.linspace(0, 0, 1) * u.m,
domain_z=np.linspace(-2, 2, 20) * u.m,
)


add the circular coil field to it

[9]:

plasma.add_magnetostatic(cw)

X, Z = plasma.grid[0, :, 0, :], plasma.grid[2, :, 0, :]
U = plasma.magnetic_field[0, :, 0, :].value.T  # because grid uses 'ij' indexing
W = plasma.magnetic_field[2, :, 0, :].value.T  # because grid uses 'ij' indexing

plt.figure()
plt.axis("square")
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.title(
"Circular coil field in x-z plane, generated by a circular coil in the x-y plane"
)
plt.streamplot(plasma.x.value, plasma.z.value, U, W)

[9]:

<matplotlib.streamplot.StreamplotSet at 0x7ff18c423b10>


a circular wire can be described as parametric equation and converted to GeneralWire

[10]:

gw_cw = cw.to_GeneralWire()

# the calculated magnetic field is close
print(gw_cw.magnetic_field([0, 0, 0]) - cw.magnetic_field([0, 0, 0]))

[ 0.00000000e+00  0.00000000e+00 -4.13416205e-12] T


A infinite straight wire (InfiniteStraightWire)

[11]:

iw = magnetostatics.InfiniteStraightWire(
np.array([0, 1, 0]), np.array([0, 0, 0]) * u.m, 1 * u.A
)
print(iw)

InfiniteStraightWire(direction=[0. 1. 0.], p0=[0. 0. 0.]m, current=1.0A)


initialize a a plasma, where the magnetic field will be calculated on

[12]:

plasma = Plasma3D(
domain_x=np.linspace(-2, 2, 30) * u.m,
domain_y=np.linspace(0, 0, 1) * u.m,
domain_z=np.linspace(-2, 2, 20) * u.m,
)

# add the infinite straight wire field to it

X, Z = plasma.grid[0, :, 0, :], plasma.grid[2, :, 0, :]
U = plasma.magnetic_field[0, :, 0, :].value.T  # because grid uses 'ij' indexing
W = plasma.magnetic_field[2, :, 0, :].value.T  # because grid uses 'ij' indexing

plt.figure()
plt.title(
"Dipole field in x-z plane, generated by a infinite straight wire "
"pointing in the y direction"
)
plt.axis("square")
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.streamplot(plasma.x.value, plasma.z.value, U, W)

[12]:

<matplotlib.streamplot.StreamplotSet at 0x7ff18c258310>