This page was generated from wta/coil.ipynb.

Magnetostatics

[1]:
from netgen.occ import *
from ngsolve import *
from ngsolve.webgui import Draw
from netgen.webgui import Draw as DrawGeo
import math

model of the coil:

[2]:
cyl = Cylinder((0,0,0), Z, r=0.01, h=0.03).faces[0]
heli = Edge(Segment((0,0), (12*math.pi, 0.03)), cyl)
ps = heli.start
vs = heli.start_tangent
pe = heli.end
ve = heli.end_tangent

e1 = Segment((0,0,-0.03), (0,0,-0.01))
c1 = BezierCurve( [(0,0,-0.01), (0,0,0), ps-vs, ps])
e2 = Segment((0,0,0.04), (0,0,0.06))
c2 = BezierCurve( [pe, pe+ve, (0,0,0.03), (0,0,0.04)])
spiral = Wire([e1, c1, heli, c2, e2])
circ = Face(Wire([Circle((0,0,-0.03), Z, 0.001)]))
coil = Pipe(spiral, circ)

coil.faces.maxh=0.2
coil.faces.name="coilbnd"
coil.faces.Max(Z).name="in"
coil.faces.Min(Z).name="out"
coil.mat("coil")
crosssection = coil.faces.Max(Z).mass
[3]:
DrawGeo (coil);
[4]:
box = Box((-0.04,-0.04,-0.03), (0.04,0.04,0.06))
box.faces.name = "outer"
air = box-coil
air.mat("air");

mesh-generation of coil and air-box:

[5]:
geo = OCCGeometry(Glue([coil,air]))
with TaskManager():
    mesh = Mesh(geo.GenerateMesh(meshsize.coarse, maxh=0.01)).Curve(3)

Draw (mesh, clipping={"y":1, "z":0, "dist":0.012});

checking mesh data materials and boundaries:

[6]:
mesh.ne, mesh.nv, mesh.GetMaterials(), mesh.GetBoundaries()
[6]:
(144582,
 24605,
 ('coil', 'air'),
 ('out',
  'coilbnd',
  'coilbnd',
  'coilbnd',
  'coilbnd',
  'coilbnd',
  'in',
  'outer',
  'outer',
  'outer',
  'outer',
  'outer',
  'outer'))

Solve a potential problem to determine current density in wire:

on the domain \(\Omega_{\text{coil}}\): \begin{eqnarray*} j & = & \sigma \nabla \Phi \\ \operatorname{div} j & = & 0 \end{eqnarray*} port boundary conditions: \begin{eqnarray*} \Phi & = & 0 \qquad \qquad \text{on } \Gamma_{\text{out}}, \\ j_n & = & \frac{1}{|S|} \quad \qquad \text{on } \Gamma_{\text{in}}, \end{eqnarray*} and \(j_n=0\) else

[7]:
fespot = H1(mesh, order=3, definedon="coil", dirichlet="out")
phi,psi = fespot.TnT()
sigma = 58.7e6
bfa = BilinearForm(sigma*grad(phi)*grad(psi)*dx).Assemble()
inv = bfa.mat.Inverse(freedofs=fespot.FreeDofs(), inverse="sparsecholesky")
lff = LinearForm(1/crosssection*psi*ds("in")).Assemble()
gfphi = GridFunction(fespot)
gfphi.vec.data = inv * lff.vec
[8]:
Draw (gfphi, draw_vol=False, clipping={"y":1, "z":0, "dist":0.012});

Solve magnetostatic problem:

current source is current from potential equation:

\[\int \mu^{-1} \operatorname{curl} u \cdot \operatorname{curl} v \, dx = \int j \cdot v \, dx\]
[9]:
fes = HCurl(mesh, order=2, nograds=True)
print ("HCurl dofs:", fes.ndof)
u,v = fes.TnT()
mu = 4*math.pi*1e-7
a = BilinearForm(1/mu*curl(u)*curl(v)*dx+1e-6/mu*u*v*dx)
pre = Preconditioner(a, "bddc")
f = LinearForm(sigma*grad(gfphi)*v*dx("coil"))
with TaskManager():
    a.Assemble()
    f.Assemble()
HCurl dofs: 749548
[10]:
from ngsolve.krylovspace import CGSolver
inv = CGSolver(a.mat, pre, printrates=True)
gfu = GridFunction(fes)
with TaskManager():
    gfu.vec.data = inv * f.vec
CG iteration 1, residual = 23.409176244341065
CG iteration 2, residual = 0.08414452316402087
CG iteration 3, residual = 0.015867939889513436
CG iteration 4, residual = 0.00841594764408625
CG iteration 5, residual = 0.004834080019057161
CG iteration 6, residual = 0.0028715064438109466
CG iteration 7, residual = 0.0017063890944111191
CG iteration 8, residual = 0.0011033542561527466
CG iteration 9, residual = 0.0007961860102167149
CG iteration 10, residual = 0.0005870580392334658
CG iteration 11, residual = 0.00046390695182524957
CG iteration 12, residual = 0.0003840920264340831
CG iteration 13, residual = 0.00031221386728641124
CG iteration 14, residual = 0.00024331752609729827
CG iteration 15, residual = 0.0001923030271022093
CG iteration 16, residual = 0.00013573464979898807
CG iteration 17, residual = 9.545963063331575e-05
CG iteration 18, residual = 6.612276897086978e-05
CG iteration 19, residual = 4.387858060274155e-05
CG iteration 20, residual = 2.9284459905017907e-05
CG iteration 21, residual = 2.0155891387011336e-05
CG iteration 22, residual = 1.3933547743590292e-05
CG iteration 23, residual = 9.65491040363652e-06
CG iteration 24, residual = 6.846048089420252e-06
CG iteration 25, residual = 5.157138014436224e-06
CG iteration 26, residual = 3.8267487821729026e-06
CG iteration 27, residual = 2.9009270217727013e-06
CG iteration 28, residual = 2.3989277728090867e-06
CG iteration 29, residual = 1.9480223214871407e-06
CG iteration 30, residual = 1.5674591166884298e-06
CG iteration 31, residual = 1.2180765922169559e-06
CG iteration 32, residual = 8.757994081875547e-07
CG iteration 33, residual = 6.39373326560306e-07
CG iteration 34, residual = 4.445958083092085e-07
CG iteration 35, residual = 3.201717133314209e-07
CG iteration 36, residual = 2.21985605365471e-07
CG iteration 37, residual = 1.551108234596413e-07
CG iteration 38, residual = 1.0545561107811832e-07
CG iteration 39, residual = 7.264001699267896e-08
CG iteration 40, residual = 5.3957298082338e-08
CG iteration 41, residual = 3.740730921082214e-08
CG iteration 42, residual = 2.5988974359179067e-08
CG iteration 43, residual = 1.9920040492749385e-08
CG iteration 44, residual = 1.5896599083306363e-08
CG iteration 45, residual = 1.1931851930136754e-08
CG iteration 46, residual = 9.059578086092112e-09
CG iteration 47, residual = 6.854227613275559e-09
CG iteration 48, residual = 5.251314229139318e-09
CG iteration 49, residual = 3.835945928744865e-09
CG iteration 50, residual = 2.7383855515087517e-09
CG iteration 51, residual = 2.0219850194910504e-09
CG iteration 52, residual = 1.5435339208206604e-09
CG iteration 53, residual = 1.087949582768204e-09
CG iteration 54, residual = 7.83001579356138e-10
CG iteration 55, residual = 5.542621783778301e-10
CG iteration 56, residual = 3.885237854876907e-10
CG iteration 57, residual = 2.793614796123077e-10
CG iteration 58, residual = 2.0724240727953002e-10
CG iteration 59, residual = 1.5333584383366826e-10
CG iteration 60, residual = 1.1097960165286367e-10
CG iteration 61, residual = 8.182945349964942e-11
CG iteration 62, residual = 6.37060891916331e-11
CG iteration 63, residual = 4.8598252490609266e-11
CG iteration 64, residual = 3.377392435078756e-11
CG iteration 65, residual = 2.4790384215829576e-11
CG iteration 66, residual = 1.948687326494267e-11
[11]:
Draw (curl(gfu), mesh, draw_surf=False, \
      min=0, max=3e-4, clipping = { "y":1, "z" : 0, "function":False}, vectors = { "grid_size":100});
[ ]: