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]:
(107150,
18459,
('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: 556236
[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 = 22.99639982334626
CG iteration 2, residual = 0.1049569560238616
CG iteration 3, residual = 0.02038422932730914
CG iteration 4, residual = 0.012368412463337095
CG iteration 5, residual = 0.006053610952621916
CG iteration 6, residual = 0.0034234977101989467
CG iteration 7, residual = 0.002205565945040564
CG iteration 8, residual = 0.0013744906360623188
CG iteration 9, residual = 0.0010173281543801429
CG iteration 10, residual = 0.0007969750901381435
CG iteration 11, residual = 0.0006327980994527887
CG iteration 12, residual = 0.0005281589382802694
CG iteration 13, residual = 0.0004303778872292668
CG iteration 14, residual = 0.0003027207537420602
CG iteration 15, residual = 0.0002172504124545531
CG iteration 16, residual = 0.0001527988713010019
CG iteration 17, residual = 9.95512456549633e-05
CG iteration 18, residual = 6.017963285783497e-05
CG iteration 19, residual = 4.136627148315449e-05
CG iteration 20, residual = 2.8974119651836556e-05
CG iteration 21, residual = 2.1441530442585764e-05
CG iteration 22, residual = 1.367776713224595e-05
CG iteration 23, residual = 8.77302914480557e-06
CG iteration 24, residual = 5.66059979193197e-06
CG iteration 25, residual = 3.832050502670067e-06
CG iteration 26, residual = 2.5641386867585024e-06
CG iteration 27, residual = 1.916909984229134e-06
CG iteration 28, residual = 1.4199557256436474e-06
CG iteration 29, residual = 1.0551870301443544e-06
CG iteration 30, residual = 7.707411469311877e-07
CG iteration 31, residual = 6.210097988501716e-07
CG iteration 32, residual = 4.878246726437449e-07
CG iteration 33, residual = 3.5967728705107006e-07
CG iteration 34, residual = 2.7415160411242986e-07
CG iteration 35, residual = 2.386833450427857e-07
CG iteration 36, residual = 1.6299592602259292e-07
CG iteration 37, residual = 1.0798162695895414e-07
CG iteration 38, residual = 7.495748565655477e-08
CG iteration 39, residual = 4.960586585509574e-08
CG iteration 40, residual = 3.2165429503559815e-08
CG iteration 41, residual = 2.065469235324968e-08
CG iteration 42, residual = 1.4885155632081339e-08
CG iteration 43, residual = 1.0485798960778322e-08
CG iteration 44, residual = 6.950580886851842e-09
CG iteration 45, residual = 4.6298006251639725e-09
CG iteration 46, residual = 4.0296313618222145e-09
CG iteration 47, residual = 2.9873671831738023e-09
CG iteration 48, residual = 2.2415238912350577e-09
CG iteration 49, residual = 1.7503078225060242e-09
CG iteration 50, residual = 1.2349006369153915e-09
CG iteration 51, residual = 9.418587062335387e-10
CG iteration 52, residual = 7.001463088280356e-10
CG iteration 53, residual = 5.135963127877007e-10
CG iteration 54, residual = 3.500577927631049e-10
CG iteration 55, residual = 2.5144924467000875e-10
CG iteration 56, residual = 1.8328844460528985e-10
CG iteration 57, residual = 1.3787074438147903e-10
CG iteration 58, residual = 9.468676228811782e-11
CG iteration 59, residual = 7.44440552299063e-11
CG iteration 60, residual = 5.476100116500388e-11
CG iteration 61, residual = 3.265158408464961e-11
CG iteration 62, residual = 2.385446477031367e-11
CG iteration 63, residual = 1.7728367544847488e-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});
[ ]: