This page was generated from unit-2.4-Maxwell/Maxwell.ipynb.
2.4 Maxwell’s Equations¶
[Peter Monk: “Finite Elements for Maxwell’s Equations”]
Magnetostatic field generated by a permanent magnet¶
magnetic flux \(B\), magnetic field \(H\), given magnetization \(M\):
Introducing a vector-potential \(A\) such that \(B = \Curl A\), and putting equations together we get
In weak form: Find \(A \in H(\Curl)\) such that
Usually, the permeability \(\mu\) is given as \(\mu = \mu_r \mu_0\), with \(\mu_0 = 4 \pi 10^{-7}\) the permeability of vacuum.
[1]:
from ngsolve import *
from ngsolve.webgui import Draw
from netgen.csg import *
Geometric model and meshing of a bar magnet:
[2]:
box = OrthoBrick(Pnt(-3,-3,-3),Pnt(3,3,3)).bc("outer")
magnet = Cylinder(Pnt(-1,0,0),Pnt(1,0,0), 0.3) * OrthoBrick(Pnt(-1,-3,-3),Pnt(1,3,3))
air = box - magnet
geo = CSGeometry()
geo.Add (air.mat("air"), transparent=True)
geo.Add (magnet.mat("magnet").maxh(1), col=(0.3,0.3,0.1))
geo.Draw()
mesh = Mesh(geo.GenerateMesh(maxh=2, curvaturesafety=1))
mesh.Curve(3)
[3]:
mesh.GetMaterials(), mesh.GetBoundaries()
[3]:
(('air', 'magnet'),
('outer',
'outer',
'outer',
'outer',
'outer',
'outer',
'default',
'default',
'default'))
Define space, forms and preconditioner.
To obtain a regular system matrix, we regularize by adding a very small \(L_2\) term.
We solve magnetostatics, so we can gauge by adding and arbitrary gradient field. A cheap possibility is to delete all basis-functions which are gradients (flag ‘nograds’)
[4]:
fes = HCurl(mesh, order=3, dirichlet="outer", nograds=True)
print ("ndof =", fes.ndof)
u,v = fes.TnT()
from math import pi
mu0 = 4*pi*1e-7
mur = mesh.MaterialCF({"magnet" : 1000}, default=1)
a = BilinearForm(fes)
a += 1/(mu0*mur)*curl(u)*curl(v)*dx + 1e-8/(mu0*mur)*u*v*dx
c = Preconditioner(a, "bddc")
f = LinearForm(fes)
mag = mesh.MaterialCF({"magnet" : (1,0,0)}, default=(0,0,0))
f += mag*curl(v) * dx("magnet")
ndof = 25068
Assemble system and setup preconditioner using task-parallelization:
[5]:
with TaskManager():
a.Assemble()
f.Assemble()
Finally, declare GridFunction and solve by preconditioned CG iteration:
[6]:
gfu = GridFunction(fes)
with TaskManager():
solvers.CG(sol=gfu.vec, rhs=f.vec, mat=a.mat, pre=c.mat)
WARNING: maxsteps is deprecated, use maxiter instead!
CG iteration 1, residual = 0.004832539962846936
CG iteration 2, residual = 0.0028801984653651423
CG iteration 3, residual = 0.002263684072723672
CG iteration 4, residual = 0.0018596783600588452
CG iteration 5, residual = 0.0013706814384952797
CG iteration 6, residual = 0.0009629315332397587
CG iteration 7, residual = 0.0006784248173953654
CG iteration 8, residual = 0.0004537347999527595
CG iteration 9, residual = 0.000337398451839248
CG iteration 10, residual = 0.00026636366952020234
CG iteration 11, residual = 0.00015785865717409756
CG iteration 12, residual = 0.00010596525796416715
CG iteration 13, residual = 7.44061174410246e-05
CG iteration 14, residual = 5.0217762117148595e-05
CG iteration 15, residual = 3.5654571375899435e-05
CG iteration 16, residual = 2.1652775596128536e-05
CG iteration 17, residual = 1.6241088605443372e-05
CG iteration 18, residual = 1.0173867259498864e-05
CG iteration 19, residual = 7.24779785021993e-06
CG iteration 20, residual = 4.7820876820365e-06
CG iteration 21, residual = 3.2977747059315207e-06
CG iteration 22, residual = 2.3147392338440797e-06
CG iteration 23, residual = 1.4401160279259737e-06
CG iteration 24, residual = 1.0157373924478058e-06
CG iteration 25, residual = 6.956957984318871e-07
CG iteration 26, residual = 5.743875997307028e-07
CG iteration 27, residual = 4.816645694230397e-07
CG iteration 28, residual = 2.8275784179253673e-07
CG iteration 29, residual = 1.8371497920347415e-07
CG iteration 30, residual = 1.2576047556566884e-07
CG iteration 31, residual = 8.212387441403988e-08
CG iteration 32, residual = 5.6559021523954035e-08
CG iteration 33, residual = 3.718600861256169e-08
CG iteration 34, residual = 2.3956217080972418e-08
CG iteration 35, residual = 1.6498257479573722e-08
CG iteration 36, residual = 1.0759738128785314e-08
CG iteration 37, residual = 7.1650636162360495e-09
CG iteration 38, residual = 4.4975970571976655e-09
CG iteration 39, residual = 2.9156782018001608e-09
CG iteration 40, residual = 1.9460021366933924e-09
CG iteration 41, residual = 1.401602918120345e-09
CG iteration 42, residual = 8.455241919359091e-10
CG iteration 43, residual = 5.888978773117989e-10
CG iteration 44, residual = 3.7373200691044424e-10
CG iteration 45, residual = 2.891862003440795e-10
CG iteration 46, residual = 2.718697045670646e-10
CG iteration 47, residual = 1.8258611299418648e-10
CG iteration 48, residual = 1.294258109802755e-10
CG iteration 49, residual = 8.426943352739301e-11
CG iteration 50, residual = 5.405990348934008e-11
CG iteration 51, residual = 3.642582822325816e-11
CG iteration 52, residual = 2.364230094283021e-11
CG iteration 53, residual = 1.632098589140525e-11
CG iteration 54, residual = 1.0677525055426633e-11
CG iteration 55, residual = 7.155944081048331e-12
CG iteration 56, residual = 4.796320759249521e-12
CG iteration 57, residual = 3.0989410839442837e-12
CG iteration 58, residual = 2.050021488719326e-12
CG iteration 59, residual = 1.3892674862404394e-12
CG iteration 60, residual = 8.940758349568674e-13
CG iteration 61, residual = 5.967414534034755e-13
CG iteration 62, residual = 3.9036954050171446e-13
CG iteration 63, residual = 2.5785753469470916e-13
CG iteration 64, residual = 1.7714629715770015e-13
CG iteration 65, residual = 1.3819826532102035e-13
CG iteration 66, residual = 1.27158754693511e-13
CG iteration 67, residual = 6.77609280393292e-14
CG iteration 68, residual = 4.543500348490224e-14
CG iteration 69, residual = 2.979889633795351e-14
CG iteration 70, residual = 1.981589848554712e-14
CG iteration 71, residual = 1.3915273645253666e-14
CG iteration 72, residual = 1.0273623179930565e-14
CG iteration 73, residual = 6.3417658826560825e-15
CG iteration 74, residual = 4.1772708468856615e-15
[7]:
# the vector potential is not supposed to look nice
Draw (gfu, mesh, "vector-potential", draw_surf=False, clipping=True)
Draw (curl(gfu), mesh, "B-field", draw_surf=False, clipping=True)
Draw (1/(mu0*mur)*curl(gfu)-mag, mesh, "H-field", draw_surf=False, clipping=True)
[7]:
BaseWebGuiScene
[ ]: