Skip to content

Interacting with CoilPy

Florian Wechsung edited this page Apr 1, 2022 · 2 revisions

Caoxiang Zhu wrote a great python package CoilPy that includes many useful functions for working with coils, reading and writing makegrid files etc.

Reading a makegrid file via CoilPy

In order to read coils from makegrid files such as this one or this one:

import numpy as np
import coilpy
filename = 'coils.w7x_std_mc_zhu'
# filename = 'coils.w7x_focus_mpadidar'
focuscoils = coilpy.coils.Coil().read_makegrid(filename).data
w7x_curves = []
w7x_currents = []
for i, c in enumerate(focuscoils[:5]):
    xyz = np.vstack((c.x, c.y, c.z)).T[:-1,:]
    n = xyz.shape[0]
    newcurve = CurveXYZFourier(np.linspace(0, 1, n, endpoint=False), order)
    newcurve.least_squares_fit(xyz)
    w7x_curves.append(newcurve)
    w7x_currents.append(Current(c.I))

Writing out finite build coils for visualization in Paraview

import numpy as np
def coilpy_plot(curves, filename, height=0.1, width=0.1):
    def wrap(data):
        return np.concatenate([data, [data[0]]])
    xx = [wrap(c.gamma()[:, 0]) for c in curves]
    yy = [wrap(c.gamma()[:, 1]) for c in curves]
    zz = [wrap(c.gamma()[:, 2]) for c in curves]
    II = [1. for _ in curves]
    names = [i for i in range(len(curves))]
    from coilpy import Coil
    coils = Coil(xx, yy, zz, II, names, names)
    coils.toVTK(filename, line=False, height=height, width=width)