zoidberg package¶
Module contents¶
Submodules¶
zoidberg.boundary module¶
Boundary objects that define an ‘outside’
- class zoidberg.boundary.NoBoundary¶
No boundary, so no points outside
- outside(x, y, z)¶
Returns True if the point is outside the boundary
- Parameters:
x, y, z (array_like) – Coordinates of the point(s) to check
- Returns:
True if point is outside boundary
- Return type:
bool
zoidberg.field module¶
- class zoidberg.field.CurvedSlab(By=1.0, Bz=0.1, xcentre=0.0, Bzprime=1.0, Rmaj=1.0)¶
Represents a magnetic field in a curved slab geometry
Magnetic field in
z = Bz + (x - xcentre) * Bzprime
x - Distance in radial direction [m] y - Azimuthal (toroidal) angle z - Height [m]
- Parameters:
By (float) – Magnetic field in y direction
Bz (float) – Magnetic field in z at xcentre (float)
xcentre (float) – Reference x coordinate
Bzprime (float) – Rate of change of Bz with x
Rmaj (float) – Major radius of the slab
- Bxfunc(x, z, phi)¶
Magnetic field in x direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
X-component of the magnetic field
- Return type:
ndarray
- Byfunc(x, z, phi)¶
Magnetic field in y direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
Y-component of the magnetic field
- Return type:
ndarray
- Bzfunc(x, z, phi)¶
Magnetic field in z direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
Z-component of the magnetic field
- Return type:
ndarray
- Rfunc(x, z, phi)¶
Major radius [meters]
Returns None if in Cartesian coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
The major radius
- Return type:
ndarray
- class zoidberg.field.DommaschkPotentials(A, R_0=1.0, B_0=1.0)¶
A magnetic field generator using the Dommaschk potentials. :Parameters: * A (Coefficient matrix for the torodial and polidial harmonics. Form: (m,l,(a,b,c,d)))
R_0 (major radius [m])
B_0 (magnetic field on axis [T])
Important Methods
—————–
Bxfunc/Byfunc/Bzfunc(x,z,y) (Returns magnetic field in radial/torodial/z-direction)
Sfunc(x,z,y) (Returns approximate magnetic surface invariant for Dommaschk potentials. Use this to visualize flux surfaces)
- Bxfunc(x, z, phi)¶
Magnetic field in x direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
X-component of the magnetic field
- Return type:
ndarray
- Byfunc(x, z, phi)¶
Magnetic field in y direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
Y-component of the magnetic field
- Return type:
ndarray
- Bzfunc(x, z, phi)¶
Magnetic field in z direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
Z-component of the magnetic field
- Return type:
ndarray
- CD(m, k)¶
- Parameters:
m (torodial harmonic)
k (summation index in D)
Returns
——–
Sympy function CD_mk (R) (Dirichlet boudary conditions)
- CN(m, k)¶
- Parameters:
m (torodial harmonic)
k (summation index in N)
Returns
——–
Sympy function CN_mk (R) (Neumann boundary conditions)
- D(m, n)¶
- Parameters:
m (torodial mode number)
n (summation index in V)
Returns
——–
Sympy function D_mn (R, Z) (Dirichlet boundary conditions)
- N(m, n)¶
- Parameters:
m (torodial mode number)
n (summation index in V)
Returns
——–
Sympy function N_mn (R, Z) (Neumann boundary conditions)
- Rfunc(x, z, phi)¶
- Parameters:
x (radial coordinates normalized to R_0)
z (binormal coordinate)
y (torodial angle normalized to 2*pi)
- Return type:
Radial coordinate x
- Sfunc(x, z, y)¶
- Parameters:
x (radial coordinates normalized to R_0)
z (binormal coordinate)
y (torodial angle normalized to 2*pi)
- Return type:
Approximate magnetic surface invariant S at location (x,z,y). This is from the original Dommaschk paper. Use to visualize flux surfaces
- U(A)¶
- Parameters:
A (Coefficient matrix for the torodial and polidial harmonics. Form: (m,l,(a,b,c,d)))
- Returns:
U
- Return type:
Superposition of all modes given in A
- U_hat(A)¶
- Parameters:
A (Coefficient matrix for the torodial and polidial harmonics. Form: (m,l,(a,b,c,d)))
- Returns:
U
- Return type:
Superposition of all modes given in A
- V(m, l, a, b, c, d)¶
- Parameters:
m (torodial mode number)
l (polodial mode number)
a,b,c,d (Coefficients for m,l-th Dommaschk potential (elements of matrix A))
Returns
——–
Sympy function V_ml
- V_hat(m, l, a, b, c, d)¶
- Parameters:
m (torodial mode number)
l (polodial mode number)
a,b,c,d (Coefficients for m,l-th Dommaschk potential (elements of matrix A))
Returns
——–
Sympy function V_hat_ml; Similar to V; needed for calculation of magnetic surface invariant S
- class zoidberg.field.GEQDSK(gfile)¶
Read a EFIT G-Eqdsk file for a toroidal equilibrium
This generates a grid in cylindrical geometry
- Parameters:
gfile (str) – Name of the file to open
- Bxfunc(x, z, phi)¶
Magnetic field in x direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
X-component of the magnetic field
- Return type:
ndarray
- Byfunc(x, z, phi)¶
Magnetic field in y direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
Y-component of the magnetic field
- Return type:
ndarray
- Bzfunc(x, z, phi)¶
Magnetic field in z direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
Z-component of the magnetic field
- Return type:
ndarray
- Rfunc(x, z, phi)¶
Major radius [meters]
Returns None if in Cartesian coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
The major radius
- Return type:
ndarray
- pressure(x, z, phi)¶
Pressure [Pascals]
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
The plasma pressure
- Return type:
ndarray
- class zoidberg.field.MagneticField¶
Represents a magnetic field in either Cartesian or cylindrical geometry
This is the base class, you probably don’t want to instantiate one of these directly. Instead, create an instance of one of the subclasses.
Functions which can be overridden
Bxfunc = Function for magnetic field in x
Bzfunc = Function for magnetic field in z
Byfunc = Function for magnetic field in y (default = 1.)
Rfunc = Function for major radius. If None, y is in meters
- boundary¶
An object with an “outside” function. See
zoidberg.boundary
- attributes¶
Contains attributes to be written to the output
- Type:
A dictionary of string -> function(x,z,phi)
See also
Slab
A straight field in normal Cartesian coordinates
CurvedSlab
A field in curvilinear coordinates
StraightStellarator
A rotating ellipse stellarator without curvature
RotatingEllipse
A rotating ellipse stellarator with curvature
VMEC
A numerical field from a VMEC equilibrium file
GEQDSK
A numerical field from an EFIT g-file
- Bmag(x, z, phi)¶
Magnitude of the magnetic field
\[Bmag = \sqrt(B_x^2 + B_y^2 + B_z^2)\]- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
The magnitude of the magnetic field
- Return type:
ndarray
- Bxfunc(x, z, phi)¶
Magnetic field in x direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
X-component of the magnetic field
- Return type:
ndarray
- Byfunc(x, z, phi)¶
Magnetic field in y direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
Y-component of the magnetic field
- Return type:
ndarray
- Bzfunc(x, z, phi)¶
Magnetic field in z direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
Z-component of the magnetic field
- Return type:
ndarray
- Rfunc(x, z, phi)¶
Major radius [meters]
Returns None if in Cartesian coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
The major radius
- Return type:
ndarray
- attributes = {}¶
- boundary = <zoidberg.boundary.NoBoundary object>¶
- field_direction(pos, ycoord, flatten=False)¶
Calculate the direction of the magnetic field Returns the change in x with phi and change in z with phi
- Parameters:
pos (ndarray) – 2-D NumPy array, with the second dimension being [x,z], with x and z in meters
ycoord (float) – Toroidal angle in radians if cylindrical coordinates, metres if Cartesian
flatten (bool, optional) – If True, return a flattened form of the vector components. This is useful for passing to
FieldTracer
- Returns:
(dx/dy, dz/dy) –
= (R*Bx/Bphi, R*Bz/Bphi)
if cylindrical= (Bx/By, Bz/By)
if Cartesian
- Return type:
list of floats or ndarray
- pressure(x, z, phi)¶
Pressure [Pascals]
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
The plasma pressure
- Return type:
ndarray
- class zoidberg.field.RotatingEllipse(xcentre=0.0, zcentre=0.0, radius=0.8, yperiod=3.141592653589793, I_coil=0.05, Btor=1.0, smooth=False, smooth_args={})¶
A “rotating ellipse” stellarator :Parameters: * xcentre (float, optional) – Middle of the domain in x [m]
zcentre (float, optional) – Middle of the domain in z [m]
radius (float, optional) – Radius of coils [meters]
yperiod (float, optional) – The period over which the coils return to their original position
I_coil (float, optional) – Current in each coil
Btor (float, optional) – Toroidal magnetic field strength
- Rfunc(x, z, phi)¶
Major radius [meters]
Returns None if in Cartesian coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
The major radius
- Return type:
ndarray
- coil(xcentre, zcentre, radius, angle, iota, I)¶
Defines a single coil :Parameters: * radius (float) – Radius to coil
angle (float) – Initial angle of coil
iota (float) – Rotational transform of coil
I (float) – Current through coil
- Return type:
(x, z) - x, z coordinates of coils along phi
- class zoidberg.field.Screwpinch(xcentre=1.5, zcentre=0.0, shear=0, yperiod=6.283185307179586, Btor=1.0)¶
- Rfunc(x, z, phi)¶
Major radius [meters]
Returns None if in Cartesian coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
The major radius
- Return type:
ndarray
- class zoidberg.field.Slab(By=1.0, Bz=0.1, xcentre=0.0, Bzprime=1.0)¶
Represents a magnetic field in an infinite flat slab
Magnetic field in
z = Bz + (x - xcentre) * Bzprime
Coordinates (x,y,z) assumed to be Cartesian, all in metres
- Parameters:
By (float, optional) – Magnetic field in y direction
Bz (float, optional) – Magnetic field in z at xcentre
xcentre (float, optional) – Reference x coordinate
Bzprime (float, optional) – Rate of change of Bz with x
- Bxfunc(x, z, phi)¶
Magnetic field in x direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
X-component of the magnetic field
- Return type:
ndarray
- Byfunc(x, z, phi)¶
Magnetic field in y direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
Y-component of the magnetic field
- Return type:
ndarray
- Bzfunc(x, z, phi)¶
Magnetic field in z direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
Z-component of the magnetic field
- Return type:
ndarray
- class zoidberg.field.SmoothedMagneticField(field, grid, xboundary=None, zboundary=None)¶
Represents a magnetic field which is smoothed so it never leaves the boundaries of a given grid.
- Parameters:
field (
zoidberg.field.MagneticField
) – A MagneticField objectgrid (
zoidberg.grid.Grid
) – A Grid objectxboundary (int, optional) – Number of grid points in x over which the magnetic field is smoothed
zboundary (int, optional) – Number of grid points in x over which the magnetic field is smoothed
- Bxfunc(x, z, phi)¶
Magnetic field in x direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
X-component of the magnetic field
- Return type:
ndarray
- Byfunc(x, z, phi)¶
Not modified by smoothing
- Rfunc(x, z, phi)¶
Major radius [meters]
Returns None if in Cartesian coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
The major radius
- Return type:
ndarray
- smooth_field_line(xa, za)¶
Linearly damp the field to be parallel to the edges of the box
Should take some parameters to adjust rate of smoothing, etc.
- class zoidberg.field.StraightStellarator(xcentre=0.0, zcentre=0.0, radius=0.8, yperiod=3.141592653589793, I_coil=0.05, smooth=False, smooth_args={})¶
A “rotating ellipse” stellarator without curvature
- Parameters:
xcentre (float, optional) – Middle of the domain in x [m]
zcentre (float, optional) – Middle of the domain in z [m]
radius (float, optional) – Radius of coils [meters]
yperiod (float, optional) – The period over which the coils return to their original position
I_coil (float, optional) – Current in each coil
- coil(xcentre, zcentre, radius, angle, iota, I)¶
Defines a single coil
- Parameters:
radius (float) – Radius to coil
angle (float) – Initial angle of coil
iota (float) – Rotational transform of coil
I (float) – Current through coil
- Return type:
(x, z) - x, z coordinates of coils along phi
- class zoidberg.field.VMEC(vmec_file, ntheta=None, nzeta=None, nr=32, nz=32)¶
A numerical magnetic field from a VMEC equilibrium file
- Parameters:
vmec_file (str) – Name of the VMEC file to read
ntheta (int, optional) – Number of theta points to use (default: use ‘mpol’ from VMEC file)n
nzeta (int, optional) – Number of zeta points to use (default: use ‘ntor’ from VMEC file)
nr (int) – Number of R points to use
nz (int) – Number of Z points to use
- Bxfunc(x, z, phi)¶
Magnetic field in x direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
X-component of the magnetic field
- Return type:
ndarray
- Byfunc(x, z, phi)¶
Magnetic field in y direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
Y-component of the magnetic field
- Return type:
ndarray
- Bzfunc(x, z, phi)¶
Magnetic field in z direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
Z-component of the magnetic field
- Return type:
ndarray
- Rfunc(x, z, phi)¶
Major radius
- cfunct(field)¶
VMEC DCT
- read_vmec_file(vmec_file, ntheta=None, nzeta=None)¶
Read a VMEC equilibrium file
- sfunct(field)¶
VMEC DST
- class zoidberg.field.W7X_VMEC(nx=512, ny=32, nz=512, x_range=[4.05, 6.55], z_range=[-1.35, 1, 35], phi_range=[0, 6.283185307179586], vmec_id='w7x_ref_171')¶
- Bxfunc(x, z, phi)¶
Magnetic field in x direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
X-component of the magnetic field
- Return type:
ndarray
- Byfunc(x, z, phi)¶
Magnetic field in y direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
Y-component of the magnetic field
- Return type:
ndarray
- Bzfunc(x, z, phi)¶
Magnetic field in z direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
Z-component of the magnetic field
- Return type:
ndarray
- Rfunc(x, z, phi)¶
Major radius [meters]
Returns None if in Cartesian coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
The major radius
- Return type:
ndarray
- field_values(r, phi, z, vmec_id='w7x_ref_171')¶
- class zoidberg.field.W7X_vacuum(nx=128, ny=32, nz=128, x_range=(4.05, 6.55), z_range=(-1.35, 1, 35), phimax=6.283185307179586, configuration=0, plot_poincare=False, include_plasma_field=False, wout_file='wout_w7x.0972_0926_0880_0852_+0000_+0000.01.00jh.nc')¶
- Bxfunc(x, z, phi)¶
Magnetic field in x direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
X-component of the magnetic field
- Return type:
ndarray
- Byfunc(x, z, phi)¶
Magnetic field in y direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
Y-component of the magnetic field
- Return type:
ndarray
- Bzfunc(x, z, phi)¶
Magnetic field in z direction at given coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
Z-component of the magnetic field
- Return type:
ndarray
- Rfunc(x, z, phi)¶
Major radius [meters]
Returns None if in Cartesian coordinates
- Parameters:
x, z, phi (array_like) – X, Z, and toroidal coordinates
- Returns:
The major radius
- Return type:
ndarray
- field_values(phi, z, configuration=0, plot_poincare=False)¶
This uses the webservices field line tracer to get the vacuum magnetic field given 3d arrrays for R, phi, and Z. Only works on IPP network
http://webservices.ipp-hgw.mpg.de/docs/fieldlinetracer.html
Contact brendan.shanahan@ipp.mpg.de for questions
- magnetic_axis(phi_axis=0, configuration=0)¶
- plasma_field(phi, z, wout_file='wout.nc')¶
This uses EXTENDER via the IPP webservices to get the magnetic field from the plasma given 3d arrrays for R, phi, and Z. Only works on IPP network
http://webservices.ipp-hgw.mpg.de/docs/extender.html
Contact brendan.shanahan@ipp.mpg.de for questions
zoidberg.fieldtracer module¶
- class zoidberg.fieldtracer.FieldTracer(field)¶
A class for following magnetic field lines
- Parameters:
field (
MagneticField
) – A Zoidberg MagneticField instance
- follow_field_lines(x_values, z_values, y_values, rtol=None)¶
Uses field_direction to follow the magnetic field from every grid (x,z) point at toroidal angle y through a change in toroidal angle dy
- Parameters:
x_values (array_like) – Starting x coordinates
z_values (array_like) – Starting z coordinates
y_values (array_like) – y coordinates to follow the field line to. y_values[0] is the starting position
rtol (float, optional) – The relative tolerance to use for the integrator. If None, use the default value
- Returns:
result – Field line ending coordinates
The first dimension is y, the last is (x,z). The middle dimensions are the same shape as [x|z]: [0,…] is the initial position […,0] are the x-values […,1] are the z-values If x_values is a scalar and z_values a 1D array, then result has the shape [len(y), len(z), 2], and vice-versa. If x_values and z_values are 1D arrays, then result has the shape [len(y), len(x), 2]. If x_values and z_values are 2D arrays, then result has the shape [len(y), x.shape[0], x.shape[1], 2].
- Return type:
numpy.ndarray
- class zoidberg.fieldtracer.FieldTracerReversible(field, rtol=1e-08, eps=1e-05, nsteps=20)¶
Traces magnetic field lines in a reversible way by using trapezoidal integration:
\[pos_{n+1} = pos_n + 0.5*( f(pos_n) + f(pos_{n+1}) )*dy\]This requires a Newton iteration to solve the nonlinear set of equations for the unknown
pos_{n+1}
.- Parameters:
field (
MagneticField
) – A Zoidberg MagneticField instancertol (float, optional) – Tolerance applied to changes in dx**2 + dz**2
eps (float, optional) – Change in x,z used to calculate finite differences of magnetic field direction
nsteps (int, optional) – Number of sub-steps between outputs
- follow_field_lines(x_values, z_values, y_values, rtol=None, eps=None, nsteps=None)¶
Uses field_direction to follow the magnetic field from every grid (x,z) point at toroidal angle y through a change in toroidal angle dy
- Parameters:
x_values (array_like) – Starting x coordinates
z_values (array_like) – Starting z coordinates
y_values (array_like) – y coordinates to follow the field line to. y_values[0] is the starting position
rtol (float, optional) – Tolerance applied to changes in dx**2 + dz**2. If None, use the default value
eps (float, optional) – Change in x,z used to calculate finite differences of magnetic field direction
nsteps (int, optional) – Number of sub-steps between outputs
- Returns:
result – Field line ending coordinates
The first dimension is y, the last is (x,z). The middle dimensions are the same shape as [x|z]: [0,…] is the initial position […,0] are the x-values […,1] are the z-values If x_values is a scalar and z_values a 1D array, then result has the shape [len(y), len(z), 2], and vice-versa. If x_values and z_values are 1D arrays, then result has the shape [len(y), len(x), 2]. If x_values and z_values are 2D arrays, then result has the shape [len(y), x.shape[0], x.shape[1], 2].
- Return type:
numpy.ndarray
- zoidberg.fieldtracer.trace_poincare(magnetic_field, xpos, zpos, yperiod, nplot=3, y_slices=None, revs=20, nover=20)¶
Trace a Poincare graph of the field lines
Does no plotting, see
zoidberg.plot.plot_poincare()
- Parameters:
magnetic_field (
MagneticField
) – Magnetic field objectxpos, zpos (array_like) – Starting X, Z locations
yperiod (float) – Length of period in y domain
nplot (int, optional) – Number of equally spaced y-slices to trace to
y_slices (list of ints) – List of y-slices to plot; overrides
nplot
revs (int, optional) – Number of revolutions (times around y)
nover (int, optional) – Over-sample. Produced additional points in y then discards. This seems to be needed for accurate results in some cases
- Returns:
coords is a Numpy array of data:
[revs, nplot, ..., R/Z]
where the first index is the revolution, second is the y slice, and last is 0 for R, 1 for Z. The middle indices are the shape of the input xpos,zpos
- Return type:
coords, y_slices
zoidberg.grid module¶
zoidberg.plot module¶
zoidberg.poloidal_grid module¶
zoidberg.progress module¶
- zoidberg.progress.update_progress(progress, barLength=10, ascii=False, **kwargs)¶
Displays or updates a console progress bar
Accepts a float between 0 and 1. Any int will be converted to a float. A value under 0 represents a ‘halt’. A value at 1 or bigger represents 100%
- Parameters:
progress (float) – Number between 0 and 1
barLength (int, optional) – Length of the progress bar
ascii (bool, optional) – If True, use ‘#’ as the progress indicator, otherwise use a Unicode character (the default)