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)
[2]:
<ngsolve.comp.Mesh at 0x7fbe90107360>
[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.004832539962849044
CG iteration 2, residual = 0.0028801984652913576
CG iteration 3, residual = 0.0022636840717767887
CG iteration 4, residual = 0.0018596783443640323
CG iteration 5, residual = 0.0013706814431410244
CG iteration 6, residual = 0.0009629315328718044
CG iteration 7, residual = 0.0006784248172513844
CG iteration 8, residual = 0.00045373479988693665
CG iteration 9, residual = 0.00033739845235846496
CG iteration 10, residual = 0.0002663636696667055
CG iteration 11, residual = 0.00015785865716710023
CG iteration 12, residual = 0.00010596525810194438
CG iteration 13, residual = 7.44061175787656e-05
CG iteration 14, residual = 5.021776232098457e-05
CG iteration 15, residual = 3.565457135377312e-05
CG iteration 16, residual = 2.1652775592701095e-05
CG iteration 17, residual = 1.624108858415097e-05
CG iteration 18, residual = 1.0173867299011049e-05
CG iteration 19, residual = 7.247797858028786e-06
CG iteration 20, residual = 4.782087689828882e-06
CG iteration 21, residual = 3.2977746976815405e-06
CG iteration 22, residual = 2.314739102794552e-06
CG iteration 23, residual = 1.4401128684713557e-06
CG iteration 24, residual = 1.0156516566727117e-06
CG iteration 25, residual = 6.940215642294402e-07
CG iteration 26, residual = 5.562970934439689e-07
CG iteration 27, residual = 4.888377864791663e-07
CG iteration 28, residual = 2.836568979371289e-07
CG iteration 29, residual = 1.8373164186474e-07
CG iteration 30, residual = 1.2576082980946328e-07
CG iteration 31, residual = 8.212388064838976e-08
CG iteration 32, residual = 5.655902163310334e-08
CG iteration 33, residual = 3.7186008437999495e-08
CG iteration 34, residual = 2.3956217034147292e-08
CG iteration 35, residual = 1.649825741953613e-08
CG iteration 36, residual = 1.0759738081318984e-08
CG iteration 37, residual = 7.165063589964294e-09
CG iteration 38, residual = 4.49759703939341e-09
CG iteration 39, residual = 2.915678058181384e-09
CG iteration 40, residual = 1.9460008191034248e-09
CG iteration 41, residual = 1.4015961502014474e-09
CG iteration 42, residual = 8.4548815411425e-10
CG iteration 43, residual = 5.885747520446316e-10
CG iteration 44, residual = 3.709782171421443e-10
CG iteration 45, residual = 2.5427433707284386e-10
CG iteration 46, residual = 2.3200927074679477e-10
CG iteration 47, residual = 1.769061075022179e-10
CG iteration 48, residual = 1.2566080985620388e-10
CG iteration 49, residual = 8.651021406704228e-11
CG iteration 50, residual = 5.448715589036837e-11
CG iteration 51, residual = 3.6456561632892606e-11
CG iteration 52, residual = 2.3644601306351893e-11
CG iteration 53, residual = 1.632112055443135e-11
CG iteration 54, residual = 1.0677538626241308e-11
CG iteration 55, residual = 7.155944901973948e-12
CG iteration 56, residual = 4.796320743527555e-12
CG iteration 57, residual = 3.09894106409676e-12
CG iteration 58, residual = 2.0500215125011804e-12
CG iteration 59, residual = 1.3892675406898827e-12
CG iteration 60, residual = 8.940760024384415e-13
CG iteration 61, residual = 5.967418871766879e-13
CG iteration 62, residual = 3.9036966406082175e-13
CG iteration 63, residual = 2.578202027415977e-13
CG iteration 64, residual = 1.7608696618084245e-13
CG iteration 65, residual = 1.1427827667269481e-13
CG iteration 66, residual = 7.94126193397043e-14
CG iteration 67, residual = 8.370121419341783e-14
CG iteration 68, residual = 4.779015141193887e-14
CG iteration 69, residual = 3.02175377160167e-14
CG iteration 70, residual = 2.0649275410209195e-14
CG iteration 71, residual = 1.5092246946772476e-14
CG iteration 72, residual = 1.0241513395010533e-14
CG iteration 73, residual = 6.2678125057915295e-15
CG iteration 74, residual = 4.174936861881933e-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
[ ]: