coilpy.coils#

class coilpy.coils.Coil(xx=[], yy=[], zz=[], II=[], names=[], groups=[])[source]#

Bases: object

Python object for a set of coils.

Parameters:
  • xx (list, optional) – Coil data in x-coordinates. Defaults to [[]].

  • yy (list, optional) – Coil data in y-coordinates. Defaults to [[]].

  • zz (list, optional) – Coil data in z-coordinates. Defaults to [[]].

  • II (list, optional) – Coil currents. Defaults to [[]].

  • names (list, optional) – Coil names. Defaults to [[]].

  • groups (list, optional) – Coil groups. Defaults to [[]].

A convenient way for construction is to use self.read_makegrid(filename), like

`` coil = CoilSet.read_makegrid(‘coils.sth’) ``

Each coil is stored in self.data in the format of coilpy.coils.SingleCoil.

You can plot the coilset using self.plot.

The coilset can be saved in the format of MAKEGRID using self.save_makegrid and saved as VTK files using self.toVTK.

bfield(pos, method='hanson_hirshman')[source]#

Compute the magnetic field from a coil set

Parameters:
  • pos (array_like) – Evaluation points, shape is (npoints,3) or (3,).

  • method (str, optional) –

    Biot-Savrt computing function. one of the follows: “hanson_hirshman”: Hanson-Hirshman expression. “biot_savart”: Native Biot-Savart with tagent pre-calculated.

    The tangent can be computed using SingleCoil.fourier_tanget or SingleCoil.spline_tanget (with different orders).

    Defaults to “hanson_hirshman”.

Returns:

The computed magnetic field, shape (npoints,3).

Return type:

array_like

plot(irange=[], engine='pyplot', plot2d=False, ax=None, fig=None, show=True, **kwargs)[source]#

Plot coils in mayavi or matplotlib or plotly.

Parameters:
  • irange (list, optional) – Coil list to be plotted. Defaults to [].

  • engine (string, optional) – Plotting engine. One of {‘pyplot’, ‘mayavi’, ‘plotly’}. Defaults to “pyplot”.

  • plot2d (bool, optional) – If plotting with finite size. Defaults to False.

  • fig (, optional) – figure to be plotted on. Defaults to None.

  • ax (, optional) – axis to be plotted on. Defaults to None.

  • show (bool, optional) – if show the plotly figure immediately. Defaults to True.

  • kwargs (dict, optional) – Keyword dict for plotting settings.

classmethod read_gpec_coils(filename, current=1.0)[source]#

Read coils from GPEC files.

Parameters:
  • filename (str) – File name.

  • current (float, optional) – Coil current. Defaults to 1.0.

Returns:

Coil object.

Return type:

Coil

classmethod read_makegrid(filename)[source]#

Read coils from the MAKEGRID format.

Parameters:

filename (str) – file path and name

Raises:

IOError – Check if file exists

Returns:

the python class Coil

Return type:

Coil class

save_gpec_coils(filename, split=True, nw=1, **kwargs)[source]#

Write the data in standard ascii format for GPEC

Parameters:
  • filename (str) – path (if split==True) or file name to be saved.

  • split (bool, optional) – write each coil into a separate file. Defaults to True

  • nw (integer, optional) – number of windings. Defaults to 1.

save_makegrid(filename, nfp=1, **kwargs)[source]#

Write coils in the MAKEGRID format.

Parameters:
  • filename (str) – File name and path.

  • nfp (int, optional) – Number of toroidal periodicity. Defaults to 1.

toVTK(vtkname, line=True, height=0.1, width=0.1, **kwargs)[source]#

Write entire coil set into a VTK file

Parameters:
  • vtkname (str) – VTK filename.

  • line (bool, optional) – Save coils as polylines or surfaces. Defaults to True.

  • height (float, optional) – Rectangle height when expanded to a finite cross-section. Defaults to 0.1.

  • width (float, optional) – Rectangle width when expanded to a finite cross-section. Defaults to 0.1.

  • kwargs (dict) – Optional kwargs passed to “polyLinesToVTK” or “meshio.Mesh.write”.

class coilpy.coils.SingleCoil(x=[], y=[], z=[], I=0.0, name='coil1', group=1)[source]#

Bases: object

Python class representing a single coil as discrete points in Cartesian coordinates.

x#
Type:

Data in x-coordinate

Args#

x (list, optional): Data in x-coordinate. Defaults to []. y (list, optional): Data in y-coordinate. Defaults to []. z (list, optional): Data in z-coordinate Defaults to []. I (float, optional): Coil current. Defaults to 0.0. name (str, optional): Coil name. Defaults to “coil1”. group (int, optional): Coil group for labeling. Defaults to 1.

bfield(pos)[source]#

Calculate the magnetic field at an arbitrary point using self.dt.

Parameters:

pos (list) – Evaluation point in Cartesian coordinates.

Returns:

B vector produced by the coil.

Return type:

numpy.ndarray

bfield_HH(pos, **kwargs)[source]#

Calculate B field at an arbitrary point using the Hanson-Hirshman expression

Parameters:

pos (list) – Cartesian coordinates for the evaluation point.

Returns:

B vector produced by the coil.

Return type:

numpy.ndarray

bfield_fd(pos)[source]#

Calculate the magnetic field at an arbitrary point using finite difference.

Parameters:

pos (list) – Evaluation point in Cartesian coordinates.

Returns:

B vector produced by the coil.

Return type:

numpy.ndarray

biot_savart(pos)[source]#
fourier_tangent()[source]#

Approximate the tangent using Fourier representation.

hanson_hirshman(pos)[source]#

Wrapper for the fortran code biotsavart.hanson_hirshman

Parameters:

pos (ndarray, (n,3)) – Evaluation points in space

Returns:

Magnetic field at the evaluation point

Return type:

ndarray, (n,3)

interpolate(num=256, kind='fft', nf=-1)[source]#

Interpolate to get more data points.

Parameters:
  • num (int, optional) – The total number of points after interpolation. Defaults to 256.

  • kind (str, optional) – Specifies the kind of interpolation, could be ‘fft’ or scipy.interp1d.kind. Defaults to ‘fft’.

  • nf (int, optional) – Number of truncated Fourier modes. Defaults to -1.

magnify(ratio)[source]#

Magnify the coil with a ratio.

Parameters:

ratio (float) – The magnifying ratio.

plot(engine='mayavi', fig=None, ax=None, show=True, **kwargs)[source]#

Plot the coil in a specified engine.

Parameters:
  • engine (str, optional) – Plot enginer, could be {pyplot, mayavi, plotly}. Defaults to “mayavi”.

  • fig (, optional) – Figure to be plotted on. Defaults to None.

  • ax (matplotlib.axis, optional) – Axis to be plotted on. Defaults to None.

  • show (bool, optional) – If show the plotly figure immediately. Defaults to True.

Raises:

ValueError – Invalid engine option, should be one of {pyplot, mayavi, plotly}.

plot2d(engine='mayavi', fig=None, ax=None, show=True, width=0.1, height=0.1, frame='centroid', **kwargs)[source]#

Plot the coil with finite size.

Parameters:
  • engine (str, optional) – Plot enginer, could be {pyplot, mayavi, plotly}. Defaults to “mayavi”.

  • fig (, optional) – Figure to be plotted on. Defaults to None.

  • ax (matplotlib.axis, optional) – Axis to be plotted on. Defaults to None.

  • show (bool, optional) – If show the plotly figure immediately. Defaults to True.

  • width (float, optional) – Coil width. Defaults to 0.1.

  • height (float, optional) – Coil height. Defaults to 0.1.

  • frame (str, optional) – Finite-build frame, could be one of (“centroid”, “frenet”, “parallel”). Defaults to “centroid”.

rectangle(width=0.1, height=0.1, frame='centroid', **kwargs)[source]#

Expand single coil filament to a finite-build coil.

Parameters:
  • width (float, optional) – Coil width. Defaults to 0.1.

  • height (float, optional) – Coil height. Defaults to 0.1.

  • frame (str, optional) – Finite-build frame, could be one of (“centroid”, “frenet”, “parallel”). Defaults to “centroid”.

Returns:

x-coordiante for plotting as a mesh. numpy.ndarry: y-coordiante for plotting as a mesh. numpy.ndarry: z-coordiante for plotting as a mesh.

Return type:

numpy.ndarry

spline_tangent(order=3, der=1)[source]#

Calculate the tangent of coil using spline interpolation

Parameters:

order (int, optional) – Order of spline interpolation used. Defaults to 3.

toVTK(vtkname, **kwargs)[source]#

Write the coil as a VTK file

Parameters:

vtkname (string) – VTK filename