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

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, printrates=True)
CG iteration 1, residual = 0.004841340224441174
CG iteration 2, residual = 0.005042736495448068
CG iteration 3, residual = 0.0031497474909857512
CG iteration 4, residual = 0.0028071729924236356
CG iteration 5, residual = 0.0018044283205905956
CG iteration 6, residual = 0.0012102064412063715
CG iteration 7, residual = 0.0009603395596834526
CG iteration 8, residual = 0.0008701989504798994
CG iteration 9, residual = 0.0006325610483462701
CG iteration 10, residual = 0.0004532678408037593
CG iteration 11, residual = 0.0003176167389469657
CG iteration 12, residual = 0.00023160559528488924
CG iteration 13, residual = 0.00020737408634251107
CG iteration 14, residual = 0.0001349457986190073
CG iteration 15, residual = 8.950801806764289e-05
CG iteration 16, residual = 6.500932996141846e-05
CG iteration 17, residual = 8.659514774161963e-05
CG iteration 18, residual = 4.157937685517677e-05
CG iteration 19, residual = 2.8961826501991005e-05
CG iteration 20, residual = 1.9763305817064516e-05
CG iteration 21, residual = 1.2931661258282447e-05
CG iteration 22, residual = 9.125442581333783e-06
CG iteration 23, residual = 6.593115124192446e-06
CG iteration 24, residual = 4.346076508519143e-06
CG iteration 25, residual = 3.1836366760241654e-06
CG iteration 26, residual = 2.0408071278065453e-06
CG iteration 27, residual = 1.5202325805269122e-06
CG iteration 28, residual = 9.964662266089055e-07
CG iteration 29, residual = 7.360510515953737e-07
CG iteration 30, residual = 1.1441956360015575e-06
CG iteration 31, residual = 4.736928236891684e-07
CG iteration 32, residual = 3.3257832069280024e-07
CG iteration 33, residual = 3.3174611512063335e-07
CG iteration 34, residual = 1.8770464307206297e-07
CG iteration 35, residual = 1.2819341798999095e-07
CG iteration 36, residual = 8.23621917166297e-08
CG iteration 37, residual = 5.807280066717039e-08
CG iteration 38, residual = 3.836533772136315e-08
CG iteration 39, residual = 2.7371944159629097e-08
CG iteration 40, residual = 1.8854028647308098e-08
CG iteration 41, residual = 1.2817577806018927e-08
CG iteration 42, residual = 1.0227097501401256e-08
CG iteration 43, residual = 1.0109443594602094e-08
CG iteration 44, residual = 9.785635684209541e-09
CG iteration 45, residual = 5.253537467047873e-09
CG iteration 46, residual = 3.4068718197928277e-09
CG iteration 47, residual = 2.3999463903514265e-09
CG iteration 48, residual = 2.076495217203371e-09
CG iteration 49, residual = 1.478184903679473e-09
CG iteration 50, residual = 9.655516204207866e-10
CG iteration 51, residual = 6.139916430514107e-10
CG iteration 52, residual = 4.1375572021273776e-10
CG iteration 53, residual = 2.7945131359786245e-10
CG iteration 54, residual = 1.912123141610743e-10
CG iteration 55, residual = 1.289325648056294e-10
CG iteration 56, residual = 9.317574225546365e-11
CG iteration 57, residual = 1.4393861438799417e-10
CG iteration 58, residual = 6.024560678069745e-11
CG iteration 59, residual = 4.2056802687961763e-11
CG iteration 60, residual = 4.186638025000874e-11
CG iteration 61, residual = 2.2046788525074996e-11
CG iteration 62, residual = 1.4627412969170844e-11
CG iteration 63, residual = 9.555444082250084e-12
CG iteration 64, residual = 6.71163227140419e-12
CG iteration 65, residual = 4.350798222101794e-12
CG iteration 66, residual = 3.1814987223973984e-12
CG iteration 67, residual = 2.0771565341399867e-12
CG iteration 68, residual = 1.3365698974407397e-12
CG iteration 69, residual = 1.1181223808680385e-12
CG iteration 70, residual = 1.2233483418179988e-12
CG iteration 71, residual = 5.397935913062001e-13
CG iteration 72, residual = 3.7764633981371623e-13
CG iteration 73, residual = 2.637369944617166e-13
CG iteration 74, residual = 2.1848036540009493e-13
CG iteration 75, residual = 1.605416131141611e-13
CG iteration 76, residual = 9.118600196631236e-14
CG iteration 77, residual = 6.827893969830972e-14
CG iteration 78, residual = 6.567790389931019e-14
CG iteration 79, residual = 3.751832638034663e-14
CG iteration 80, residual = 2.4735283661535153e-14
CG iteration 81, residual = 1.6192481469409217e-14
CG iteration 82, residual = 1.2270199002105186e-14
CG iteration 83, residual = 1.2127664863809162e-14
CG iteration 84, residual = 1.0446546427046613e-14
CG iteration 85, residual = 6.1608012690535476e-15
CG iteration 86, residual = 5.695075632496466e-15
CG iteration 87, residual = 3.437765936899774e-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);
[ ]:

[ ]: