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]:
(156257,
 27080,
 ('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: 810392
[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.157797722240012
CG iteration 2, residual = 0.12242100436331015
CG iteration 3, residual = 0.01585101014123294
CG iteration 4, residual = 0.00914068353179873
CG iteration 5, residual = 0.00611096442091115
CG iteration 6, residual = 0.004228307997989789
CG iteration 7, residual = 0.0028278748761742197
CG iteration 8, residual = 0.002059026410819654
CG iteration 9, residual = 0.001575487481626564
CG iteration 10, residual = 0.0013274599765461935
CG iteration 11, residual = 0.0011734382920761965
CG iteration 12, residual = 0.00102276279497984
CG iteration 13, residual = 0.0008607928941522166
CG iteration 14, residual = 0.0006796585424016477
CG iteration 15, residual = 0.0005129992972206062
CG iteration 16, residual = 0.00038193625402089333
CG iteration 17, residual = 0.00027588198655717657
CG iteration 18, residual = 0.00019256673974232558
CG iteration 19, residual = 0.00013059797344480787
CG iteration 20, residual = 9.074297008894164e-05
CG iteration 21, residual = 6.373761432361088e-05
CG iteration 22, residual = 4.722800364810418e-05
CG iteration 23, residual = 3.395059225612076e-05
CG iteration 24, residual = 2.545737533089092e-05
CG iteration 25, residual = 1.96724381426572e-05
CG iteration 26, residual = 1.5546512462950737e-05
CG iteration 27, residual = 1.2989086705522904e-05
CG iteration 28, residual = 1.0367569681565882e-05
CG iteration 29, residual = 8.543687236817893e-06
CG iteration 30, residual = 6.975160958752505e-06
CG iteration 31, residual = 5.858866850425512e-06
CG iteration 32, residual = 4.741598640530116e-06
CG iteration 33, residual = 3.44445316585862e-06
CG iteration 34, residual = 2.544881247274234e-06
CG iteration 35, residual = 1.8356416605371233e-06
CG iteration 36, residual = 1.4359668956326936e-06
CG iteration 37, residual = 1.0931123812686175e-06
CG iteration 38, residual = 8.528100361047935e-07
CG iteration 39, residual = 6.608233592774442e-07
CG iteration 40, residual = 4.870544137933254e-07
CG iteration 41, residual = 3.7690382622914777e-07
CG iteration 42, residual = 2.9763549849241137e-07
CG iteration 43, residual = 2.3130297135171872e-07
CG iteration 44, residual = 1.8231094441661501e-07
CG iteration 45, residual = 1.4133690597949063e-07
CG iteration 46, residual = 1.061856594386072e-07
CG iteration 47, residual = 7.958526652794116e-08
CG iteration 48, residual = 5.976778113414434e-08
CG iteration 49, residual = 4.598051748024486e-08
CG iteration 50, residual = 3.644041812751473e-08
CG iteration 51, residual = 2.647096344479258e-08
CG iteration 52, residual = 1.9126728474427573e-08
CG iteration 53, residual = 1.3665528289545097e-08
CG iteration 54, residual = 1.0616143980450174e-08
CG iteration 55, residual = 8.182047235796271e-09
CG iteration 56, residual = 6.297711894780039e-09
CG iteration 57, residual = 4.789976295955418e-09
CG iteration 58, residual = 3.6555785046833823e-09
CG iteration 59, residual = 2.9104704239087684e-09
CG iteration 60, residual = 2.472509056416249e-09
CG iteration 61, residual = 2.0720170408367545e-09
CG iteration 62, residual = 1.5881961690743186e-09
CG iteration 63, residual = 1.1943725263499235e-09
CG iteration 64, residual = 8.943630115767773e-10
CG iteration 65, residual = 7.065267839158666e-10
CG iteration 66, residual = 5.463374139902761e-10
CG iteration 67, residual = 4.036341346025379e-10
CG iteration 68, residual = 2.892963201220365e-10
CG iteration 69, residual = 2.0917276596236185e-10
CG iteration 70, residual = 1.6164247757345338e-10
CG iteration 71, residual = 1.3554491514733736e-10
CG iteration 72, residual = 9.551272950222611e-11
CG iteration 73, residual = 7.122313868397942e-11
CG iteration 74, residual = 5.426293940459325e-11
CG iteration 75, residual = 4.228384867417305e-11
CG iteration 76, residual = 3.2819919057312645e-11
CG iteration 77, residual = 2.5211355940326832e-11
CG iteration 78, residual = 1.97787045559759e-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});
[ ]: