# 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 ngsolve.webgui import Draw
from netgen.occ 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
box = Box( (-3,-3,-3), (3,3,3))
box.faces.name = "outer"

magnet = Cylinder((-1,0,0),X, r=0.3, h=2)
magnet.mat("magnet")
magnet.faces.col = (1,0,0)

air = box-magnet
air.mat("air")
shape = Glue([air,magnet])
geo = OCCGeometry(shape)

Draw (shape, clipping={ "z" : -1, "function":True})

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 = 33152


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)
solvers.CG(sol=gfu.vec, rhs=f.vec, mat=a.mat, pre=c.mat, printrates=True)

CG iteration 1, residual = 0.004846766222367085
CG iteration 2, residual = 0.0040639697312232935
CG iteration 3, residual = 0.0032905813899856673
CG iteration 4, residual = 0.002260752974010918
CG iteration 5, residual = 0.001975789805749375
CG iteration 6, residual = 0.0011507895290807893
CG iteration 7, residual = 0.0008639313194826871
CG iteration 8, residual = 0.000632744963070505
CG iteration 9, residual = 0.0004949733353699854
CG iteration 10, residual = 0.0004076054117917489
CG iteration 11, residual = 0.0003164866829728307
CG iteration 12, residual = 0.000183939558108776
CG iteration 13, residual = 0.0001371358912324507
CG iteration 14, residual = 8.753130615567434e-05
CG iteration 15, residual = 6.404693879352452e-05
CG iteration 16, residual = 4.2351565176251985e-05
CG iteration 17, residual = 2.6160777585792708e-05
CG iteration 18, residual = 1.953410597460895e-05
CG iteration 19, residual = 1.3009584997202532e-05
CG iteration 20, residual = 1.752809517367949e-05
CG iteration 21, residual = 8.208752221426402e-06
CG iteration 22, residual = 5.68928577672111e-06
CG iteration 23, residual = 3.828313859923506e-06
CG iteration 24, residual = 2.5126080774683227e-06
CG iteration 25, residual = 1.913303417368894e-06
CG iteration 26, residual = 1.188280736516943e-06
CG iteration 27, residual = 8.414430157468936e-07
CG iteration 28, residual = 5.733996800227516e-07
CG iteration 29, residual = 3.8562931679029926e-07
CG iteration 30, residual = 2.8126225607914247e-07
CG iteration 31, residual = 1.7869684723147825e-07
CG iteration 32, residual = 1.280546743340606e-07
CG iteration 33, residual = 8.478012656880828e-08
CG iteration 34, residual = 8.311449398719149e-08
CG iteration 35, residual = 5.2615512822676425e-08
CG iteration 36, residual = 4.836295631598999e-08
CG iteration 37, residual = 3.984402782051532e-08
CG iteration 38, residual = 2.172935454623557e-08
CG iteration 39, residual = 1.4472057265612359e-08
CG iteration 40, residual = 9.696407716918529e-09
CG iteration 41, residual = 9.04253273927926e-09
CG iteration 42, residual = 6.056516152350813e-09
CG iteration 43, residual = 3.6174790466052552e-09
CG iteration 44, residual = 2.6221779004036926e-09
CG iteration 45, residual = 1.7228405505837176e-09
CG iteration 46, residual = 1.1298889681365487e-09
CG iteration 47, residual = 7.95417099967774e-10
CG iteration 48, residual = 5.005044003173977e-10
CG iteration 49, residual = 3.5051613494393116e-10
CG iteration 50, residual = 2.2704908017952083e-10
CG iteration 51, residual = 1.5443058433739116e-10
CG iteration 52, residual = 1.259617747973012e-10
CG iteration 53, residual = 1.2747921741303117e-10
CG iteration 54, residual = 6.066774892667487e-11
CG iteration 55, residual = 4.031799033916462e-11
CG iteration 56, residual = 2.7648906740733777e-11
CG iteration 57, residual = 1.7867535865019894e-11
CG iteration 58, residual = 1.2083461041421486e-11
CG iteration 59, residual = 7.834620594819816e-12
CG iteration 60, residual = 4.993358218010071e-12
CG iteration 61, residual = 3.697429735752754e-12
CG iteration 62, residual = 3.548483353411799e-12
CG iteration 63, residual = 2.0666281375694636e-12
CG iteration 64, residual = 1.2748340734845056e-12
CG iteration 65, residual = 8.770368715547265e-13
CG iteration 66, residual = 6.098863577187414e-13
CG iteration 67, residual = 4.931028390624091e-13
CG iteration 68, residual = 3.8228032427159537e-13
CG iteration 69, residual = 2.309507123506426e-13
CG iteration 70, residual = 3.1199662953448745e-13
CG iteration 71, residual = 1.432650418674305e-13
CG iteration 72, residual = 8.917572410980933e-14
CG iteration 73, residual = 5.863172316268e-14
CG iteration 74, residual = 4.0246819757539294e-14
CG iteration 75, residual = 2.6754017177809847e-14
CG iteration 76, residual = 1.7722764630176315e-14
CG iteration 77, residual = 1.1619609251949212e-14
CG iteration 78, residual = 7.602507974176761e-15
CG iteration 79, residual = 4.8290606055292345e-15

[7]:

Draw (curl(gfu), mesh, "B-field", draw_surf=False, \
clipping = { "z" : -1, "function":True}, \
vectors = { "grid_size":50}, min=0, max=2e-5);

[ ]:



[ ]: