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]:
(102257,
 17615,
 ('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: 530927
[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.008998820119857
CG iteration 2, residual = 0.06988003930874345
CG iteration 3, residual = 0.01496984571896791
CG iteration 4, residual = 0.007252337195710505
CG iteration 5, residual = 0.003710028246438778
CG iteration 6, residual = 0.002130068466278179
CG iteration 7, residual = 0.0012697828851886864
CG iteration 8, residual = 0.0008757189442734334
CG iteration 9, residual = 0.0006696650423987791
CG iteration 10, residual = 0.0004589381932212908
CG iteration 11, residual = 0.0003183562396119098
CG iteration 12, residual = 0.0002500877712914438
CG iteration 13, residual = 0.00019706336958579928
CG iteration 14, residual = 0.00015011124405734391
CG iteration 15, residual = 0.00011264780779040921
CG iteration 16, residual = 8.233370826386584e-05
CG iteration 17, residual = 5.637400415075671e-05
CG iteration 18, residual = 3.618806801009453e-05
CG iteration 19, residual = 2.3650902550420152e-05
CG iteration 20, residual = 1.8764310692916786e-05
CG iteration 21, residual = 1.1814795100835271e-05
CG iteration 22, residual = 7.327152020389476e-06
CG iteration 23, residual = 4.747380801945001e-06
CG iteration 24, residual = 2.881060111107218e-06
CG iteration 25, residual = 1.8228379014071265e-06
CG iteration 26, residual = 1.1624167163447057e-06
CG iteration 27, residual = 7.498866906107936e-07
CG iteration 28, residual = 5.40510744119201e-07
CG iteration 29, residual = 4.685700130075377e-07
CG iteration 30, residual = 3.589700673053489e-07
CG iteration 31, residual = 2.3717853749163597e-07
CG iteration 32, residual = 1.713546008657958e-07
CG iteration 33, residual = 1.3610734853024877e-07
CG iteration 34, residual = 1.0296359895572725e-07
CG iteration 35, residual = 7.415092350665769e-08
CG iteration 36, residual = 5.072141343893094e-08
CG iteration 37, residual = 3.357707903776247e-08
CG iteration 38, residual = 2.583842728900428e-08
CG iteration 39, residual = 2.0462035682517784e-08
CG iteration 40, residual = 1.2609273327440352e-08
CG iteration 41, residual = 8.312336691457681e-09
CG iteration 42, residual = 5.560945896260205e-09
CG iteration 43, residual = 3.840761950206585e-09
CG iteration 44, residual = 2.5944074430260563e-09
CG iteration 45, residual = 1.7028794674389456e-09
CG iteration 46, residual = 1.2197450259266713e-09
CG iteration 47, residual = 1.0870981465906003e-09
CG iteration 48, residual = 6.646663230026281e-10
CG iteration 49, residual = 4.41532510974095e-10
CG iteration 50, residual = 3.1641116026338004e-10
CG iteration 51, residual = 2.2915248566682225e-10
CG iteration 52, residual = 1.7608103437042967e-10
CG iteration 53, residual = 1.2875693444699597e-10
CG iteration 54, residual = 1.0238421795735824e-10
CG iteration 55, residual = 8.5468621979443e-11
CG iteration 56, residual = 5.476111941371823e-11
CG iteration 57, residual = 4.071796598282668e-11
CG iteration 58, residual = 2.7964889349857818e-11
CG iteration 59, residual = 1.8208988680392568e-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});
[ ]: