# 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.

:

from ngsolve import *
from ngsolve.webgui import Draw
from netgen.csg import *


Geometric model and meshing of a bar magnet:

:

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.Draw()
mesh = Mesh(geo.GenerateMesh(maxh=2, curvaturesafety=1))
mesh.Curve(3)

:

<ngsolve.comp.Mesh at 0x7fe660c44770>

:

mesh.GetMaterials(), mesh.GetBoundaries()

:

(('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')

:

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:

:

with TaskManager():
a.Assemble()
f.Assemble()


Finally, declare GridFunction and solve by preconditioned CG iteration:

:

gfu = GridFunction(fes)
solvers.CG(sol=gfu.vec, rhs=f.vec, mat=a.mat, pre=c.mat)

CG iteration 1, residual = 0.004832539962849045
CG iteration 2, residual = 0.002880198465291354
CG iteration 3, residual = 0.0022636840717767852
CG iteration 4, residual = 0.001859678344364029
CG iteration 5, residual = 0.0013706814431410205
CG iteration 6, residual = 0.0009629315328718026
CG iteration 7, residual = 0.0006784248172513831
CG iteration 8, residual = 0.0004537347998869364
CG iteration 9, residual = 0.00033739845235846664
CG iteration 10, residual = 0.0002663636696667052
CG iteration 11, residual = 0.0001578586571670999
CG iteration 12, residual = 0.00010596525810194436
CG iteration 13, residual = 7.44061175787656e-05
CG iteration 14, residual = 5.021776232098462e-05
CG iteration 15, residual = 3.5654571353773105e-05
CG iteration 16, residual = 2.1652775592701068e-05
CG iteration 17, residual = 1.6241088584150997e-05
CG iteration 18, residual = 1.0173867299011818e-05
CG iteration 19, residual = 7.247797858043356e-06
CG iteration 20, residual = 4.782087690156268e-06
CG iteration 21, residual = 3.2977747041203313e-06
CG iteration 22, residual = 2.314739233498242e-06
CG iteration 23, residual = 1.4401158736142453e-06
CG iteration 24, residual = 1.0157332304414966e-06
CG iteration 25, residual = 6.9561468365613e-07
CG iteration 26, residual = 5.735603773129097e-07
CG iteration 27, residual = 4.819977440628884e-07
CG iteration 28, residual = 2.827924633926215e-07
CG iteration 29, residual = 1.8371561605958603e-07
CG iteration 30, residual = 1.257604889599221e-07
CG iteration 31, residual = 8.212387441175045e-08
CG iteration 32, residual = 5.655902150502707e-08
CG iteration 33, residual = 3.7186008435791836e-08
CG iteration 34, residual = 2.3956217034115502e-08
CG iteration 35, residual = 1.6498257419525723e-08
CG iteration 36, residual = 1.0759738081270056e-08
CG iteration 37, residual = 7.165063589613274e-09
CG iteration 38, residual = 4.497597037167306e-09
CG iteration 39, residual = 2.9156780336471023e-09
CG iteration 40, residual = 1.946000589813606e-09
CG iteration 41, residual = 1.4015949539260364e-09
CG iteration 42, residual = 8.454818473809155e-10
CG iteration 43, residual = 5.885203398814239e-10
CG iteration 44, residual = 3.705671594330149e-10
CG iteration 45, residual = 2.493256246600145e-10
CG iteration 46, residual = 2.0079145467839528e-10
CG iteration 47, residual = 1.8195267916535897e-10
CG iteration 48, residual = 1.1822545528861697e-10
CG iteration 49, residual = 8.660589023428943e-11
CG iteration 50, residual = 5.526492495555454e-11
CG iteration 51, residual = 3.6521565511420846e-11
CG iteration 52, residual = 2.364955387681782e-11
CG iteration 53, residual = 1.632141141572625e-11
CG iteration 54, residual = 1.0677567665112286e-11
CG iteration 55, residual = 7.155946800387104e-12
CG iteration 56, residual = 4.7963209421849095e-12
CG iteration 57, residual = 3.098941072996776e-12
CG iteration 58, residual = 2.050021497478519e-12
CG iteration 59, residual = 1.389267496168541e-12
CG iteration 60, residual = 8.940758598866272e-13
CG iteration 61, residual = 5.96741492012964e-13
CG iteration 62, residual = 3.9036838537116003e-13
CG iteration 63, residual = 2.57816043186781e-13
CG iteration 64, residual = 1.7607504115822358e-13
CG iteration 65, residual = 1.1418650819363984e-13
CG iteration 66, residual = 7.743572659615549e-14
CG iteration 67, residual = 7.709147220515484e-14
CG iteration 68, residual = 4.941020304022615e-14
CG iteration 69, residual = 2.99412256813019e-14
CG iteration 70, residual = 1.9986590794819125e-14
CG iteration 71, residual = 1.4264467127671023e-14
CG iteration 72, residual = 1.0311781395521509e-14
CG iteration 73, residual = 6.31375113514909e-15
CG iteration 74, residual = 4.176362949096459e-15

:

# 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)

:

BaseWebGuiScene

[ ]:



[ ]: