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 netgen.csg import *
import netgen.gui
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 = CoefficientFunction( [1000 if mat== "magnet" else 1
for mat in mesh.GetMaterials()])
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 = CoefficientFunction((1,0,0)) * \
CoefficientFunction( [1 if mat == "magnet" else 0 for mat in mesh.GetMaterials()])
f += SymbolicLFI(mag*curl(v), definedon=mesh.Materials("magnet"))
ndof = 25442
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)
iteration 0 error = 0.004770751268765439
iteration 1 error = 0.002481327439932441
iteration 2 error = 0.0018918095766121859
iteration 3 error = 0.0012587090378891374
iteration 4 error = 0.0011364536233189806
iteration 5 error = 0.0008195457478849454
iteration 6 error = 0.0006060825336657841
iteration 7 error = 0.00045341288612874047
iteration 8 error = 0.0003578513184707275
iteration 9 error = 0.0002319518486090087
iteration 10 error = 0.00015491751841894226
iteration 11 error = 9.808156355275149e-05
iteration 12 error = 7.798886269681454e-05
iteration 13 error = 4.43126957880865e-05
iteration 14 error = 3.282881496334968e-05
iteration 15 error = 2.292008755437688e-05
iteration 16 error = 1.5012379279908376e-05
iteration 17 error = 1.029058627655534e-05
iteration 18 error = 6.797403084573694e-06
iteration 19 error = 5.324080434202799e-06
iteration 20 error = 3.0935107810297378e-06
iteration 21 error = 2.273250129978477e-06
iteration 22 error = 1.5139195317352719e-06
iteration 23 error = 9.976809810274044e-07
iteration 24 error = 6.97286905237569e-07
iteration 25 error = 4.7100016242041445e-07
iteration 26 error = 3.0898824460188186e-07
iteration 27 error = 2.1201213065597008e-07
iteration 28 error = 1.436055495106182e-07
iteration 29 error = 9.438532198219693e-08
iteration 30 error = 6.136548690975539e-08
iteration 31 error = 4.355524587016417e-08
iteration 32 error = 2.955359950235831e-08
iteration 33 error = 1.9220319345636018e-08
iteration 34 error = 1.2600398390011384e-08
iteration 35 error = 8.501261332740978e-09
iteration 36 error = 6.326405765948375e-09
iteration 37 error = 5.21945562436939e-09
iteration 38 error = 3.402708963164988e-09
iteration 39 error = 2.1613759551110454e-09
iteration 40 error = 1.3842330232476177e-09
iteration 41 error = 9.57001636631718e-10
iteration 42 error = 6.401997079088777e-10
iteration 43 error = 4.418047121964364e-10
iteration 44 error = 2.9251344870137614e-10
iteration 45 error = 1.9183292457898926e-10
iteration 46 error = 1.3581702249395103e-10
iteration 47 error = 1.0807306532154379e-10
iteration 48 error = 7.037206003398875e-11
iteration 49 error = 4.634371676405302e-11
iteration 50 error = 2.7987893329494595e-11
iteration 51 error = 2.0715165670680275e-11
iteration 52 error = 1.3207503034451034e-11
iteration 53 error = 8.907478436129564e-12
iteration 54 error = 5.3922633861969185e-12
iteration 55 error = 3.8295855107311816e-12
iteration 56 error = 2.3572446498341535e-12
iteration 57 error = 1.561108912248126e-12
iteration 58 error = 1.02141401942791e-12
iteration 59 error = 6.905122536713913e-13
iteration 60 error = 4.408306406316907e-13
iteration 61 error = 2.9085298012959645e-13
iteration 62 error = 1.870878736280641e-13
iteration 63 error = 1.1857768957349382e-13
iteration 64 error = 9.875384775219882e-14
iteration 65 error = 7.283894802017714e-14
iteration 66 error = 4.5941048208705665e-14
iteration 67 error = 2.846343468143924e-14
iteration 68 error = 1.8541883624967635e-14
iteration 69 error = 1.2778394262403612e-14
iteration 70 error = 7.886822260821502e-15
iteration 71 error = 5.3746843123258e-15
iteration 72 error = 3.768613727323312e-15
[7]:
# the vector potential is not supposed to look nice
Draw (gfu, mesh, "vector-potential", draw_surf=False)
Draw (curl(gfu), mesh, "B-field", draw_surf=False)
Draw (1/(mu0*mur)*curl(gfu)-mag, mesh, "H-field", draw_surf=False)