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\):

\[\DeclareMathOperator{\Grad}{grad} \DeclareMathOperator{\Curl}{curl} \DeclareMathOperator{\Div}{div} B = \mu (H + M), \quad \Div B = 0, \quad \Curl H = 0\]

Introducing a vector-potential \(A\) such that \(B = \Curl A\), and putting equations together we get

\[\Curl \mu^{-1} \Curl A = \Curl M\]

In weak form: Find \(A \in H(\Curl)\) such that

\[\int \mu^{-1} \Curl A \Curl v = \int M \Curl v \qquad \forall \, v \in H(\Curl)\]

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.00477075126876544
iteration 1 error = 0.0024813274399324444
iteration 2 error = 0.0018918095766121902
iteration 3 error = 0.0012587090378891408
iteration 4 error = 0.0011364536233189827
iteration 5 error = 0.0008195457478849468
iteration 6 error = 0.000606082533665785
iteration 7 error = 0.0004534128861287406
iteration 8 error = 0.0003578513184707282
iteration 9 error = 0.00023195184860900917
iteration 10 error = 0.0001549175184189427
iteration 11 error = 9.808156355275179e-05
iteration 12 error = 7.798886269681475e-05
iteration 13 error = 4.431269578808662e-05
iteration 14 error = 3.2828814963349786e-05
iteration 15 error = 2.2920087554376923e-05
iteration 16 error = 1.5012379279908422e-05
iteration 17 error = 1.0290586276555355e-05
iteration 18 error = 6.797403084573706e-06
iteration 19 error = 5.324080434202804e-06
iteration 20 error = 3.09351078102974e-06
iteration 21 error = 2.273250129978479e-06
iteration 22 error = 1.5139195317352708e-06
iteration 23 error = 9.97680981027404e-07
iteration 24 error = 6.972869052375683e-07
iteration 25 error = 4.7100016242040974e-07
iteration 26 error = 3.0898824460183814e-07
iteration 27 error = 2.1201213065562944e-07
iteration 28 error = 1.4360554950774503e-07
iteration 29 error = 9.43853219598531e-08
iteration 30 error = 6.136548661519338e-08
iteration 31 error = 4.3555243251406966e-08
iteration 32 error = 2.9553578537965576e-08
iteration 33 error = 1.9220168010079484e-08
iteration 34 error = 1.2598471208569028e-08
iteration 35 error = 8.479989016094172e-09
iteration 36 error = 6.190293154567026e-09
iteration 37 error = 5.064969106855898e-09
iteration 38 error = 3.455486956996175e-09
iteration 39 error = 2.1650981838578555e-09
iteration 40 error = 1.3843499884425112e-09
iteration 41 error = 9.570059851086016e-10
iteration 42 error = 6.401944810750973e-10
iteration 43 error = 4.4178252857683684e-10
iteration 44 error = 2.9240958776083393e-10
iteration 45 error = 1.9121317371386516e-10
iteration 46 error = 1.3144534309627687e-10
iteration 47 error = 1.0400451395016937e-10
iteration 48 error = 7.121911319654021e-11
iteration 49 error = 4.658179248050262e-11
iteration 50 error = 2.7998060673408523e-11
iteration 51 error = 2.0715813336279864e-11
iteration 52 error = 1.3207581010437518e-11
iteration 53 error = 8.907483761439408e-12
iteration 54 error = 5.392263612225756e-12
iteration 55 error = 3.8295855219979785e-12
iteration 56 error = 2.357244650541162e-12
iteration 57 error = 1.5611089133619365e-12
iteration 58 error = 1.0214140350613527e-12
iteration 59 error = 6.905124625371591e-13
iteration 60 error = 4.408330447865086e-13
iteration 61 error = 2.9088823188288326e-13
iteration 62 error = 1.8760210602649888e-13
iteration 63 error = 1.270450659319718e-13
iteration 64 error = 1.2293508093134863e-13
iteration 65 error = 6.67988844441204e-14
iteration 66 error = 4.5518540925557174e-14
iteration 67 error = 2.845061844347912e-14
iteration 68 error = 1.854195875367168e-14
iteration 69 error = 1.2779673867479633e-14
iteration 70 error = 7.891977164941417e-15
iteration 71 error = 5.4011411014509825e-15
iteration 72 error = 3.827666186215267e-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)