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.404410916161655
CG iteration 2, residual = 0.06559316685254252
CG iteration 3, residual = 0.016498789821403136
CG iteration 4, residual = 0.008752142614709428
CG iteration 5, residual = 0.005181052339574175
CG iteration 6, residual = 0.0030360366367104315
CG iteration 7, residual = 0.0018272361960626404
CG iteration 8, residual = 0.0011536764911697782
CG iteration 9, residual = 0.0008191882983473236
CG iteration 10, residual = 0.0005830569600433885
CG iteration 11, residual = 0.00045032194101183795
CG iteration 12, residual = 0.00036216527984746515
CG iteration 13, residual = 0.0002813338811937514
CG iteration 14, residual = 0.0002337617250674836
CG iteration 15, residual = 0.00019132253582366602
CG iteration 16, residual = 0.0001375475888680074
CG iteration 17, residual = 0.00010032078060843518
CG iteration 18, residual = 7.283453612066483e-05
CG iteration 19, residual = 5.169577638182682e-05
CG iteration 20, residual = 3.43123602014278e-05
CG iteration 21, residual = 2.3649912829152348e-05
CG iteration 22, residual = 1.618877794814885e-05
CG iteration 23, residual = 1.0968186824208814e-05
CG iteration 24, residual = 7.674894407220893e-06
CG iteration 25, residual = 5.446615066905097e-06
CG iteration 26, residual = 3.853724407229345e-06
CG iteration 27, residual = 2.846317856793507e-06
CG iteration 28, residual = 2.1730799034884196e-06
CG iteration 29, residual = 1.7233570980590237e-06
CG iteration 30, residual = 1.3229469696971262e-06
CG iteration 31, residual = 1.0104173471087663e-06
CG iteration 32, residual = 7.706996441108397e-07
CG iteration 33, residual = 5.744638486449418e-07
CG iteration 34, residual = 4.3983901389739617e-07
CG iteration 35, residual = 3.00625938924776e-07
CG iteration 36, residual = 2.2097125073608718e-07
CG iteration 37, residual = 1.5536967819091092e-07
CG iteration 38, residual = 1.075421228652799e-07
CG iteration 39, residual = 8.165733485165434e-08
CG iteration 40, residual = 6.476888766201276e-08
CG iteration 41, residual = 4.696204682862639e-08
CG iteration 42, residual = 3.6183906198405905e-08
CG iteration 43, residual = 2.788066130581437e-08
CG iteration 44, residual = 2.1041511795128635e-08
CG iteration 45, residual = 1.5993679195478993e-08
CG iteration 46, residual = 1.2392681816225482e-08
CG iteration 47, residual = 9.298868316110824e-09
CG iteration 48, residual = 6.623094860822868e-09
CG iteration 49, residual = 4.735999396775328e-09
CG iteration 50, residual = 3.4008081129351574e-09
CG iteration 51, residual = 2.3918128331032907e-09
CG iteration 52, residual = 1.6819322853941449e-09
CG iteration 53, residual = 1.1428769572202403e-09
CG iteration 54, residual = 8.182657885587801e-10
CG iteration 55, residual = 6.30879262554262e-10
CG iteration 56, residual = 4.6052538739285094e-10
CG iteration 57, residual = 3.144693646556154e-10
CG iteration 58, residual = 2.3228141991149254e-10
CG iteration 59, residual = 1.7876434589036168e-10
CG iteration 60, residual = 1.4034005595148943e-10
CG iteration 61, residual = 1.0809156696303885e-10
CG iteration 62, residual = 8.676379017914926e-11
CG iteration 63, residual = 6.470960133662475e-11
CG iteration 64, residual = 4.543619739564984e-11
CG iteration 65, residual = 3.309283621819063e-11
CG iteration 66, residual = 2.7051155685548077e-11
CG iteration 67, residual = 1.8897371689446483e-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});
[ ]: