This page was generated from unit-2.4-Maxwell/Maxwell.ipynb.

2.4 Maxwell’s Equations

[Peter Monk: "Finite Element Methods for Maxwell’s Equations"]

Magnetostatic field generated by a permanent magnet

With the magnetic flux density \(B\), the magnetic field \(H\), and a given magnetization \(M\), the equations of magnetostatics are

\[\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 combining the equations, 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 netgen.occ import *
from ngsolve import *
from ngsolve.webgui import Draw

Geometric model and meshing of a bar magnet. The magnet is cut out of the air box, and Glue connects both solids to one geometry with a conforming interface:

[2]:
box = Box((-3,-3,-3), (3,3,3))
box.faces.name = "outer"

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

air = box - magnet
air.solids.name = "air"

shape = Glue([air, magnet])
Draw (shape, clipping={ "z" : -1, "function" : True })

mesh = shape.GenerateMesh(maxh=2, curvaturesafety=1).Curve(3)
[3]:
mesh.GetMaterials(), mesh.GetBoundaries()
[3]:
(('air', 'magnet'),
 ('outer',
  'outer',
  'outer',
  'outer',
  'outer',
  'outer',
  'default',
  'default',
  'default'))

Define the finite element space and the variational equation.

  • The vector potential is not unique, we may add an arbitrary gradient field. A cheap possibility for gauging is to remove all high order basis functions which are gradients (flag nograds).

  • The gradients within the lowest order edge element space are still there. To obtain a regular system matrix, we regularize by adding a very small \(L_2\) term.

[4]:
fes = HCurl(mesh, order=3, nograds=True)
print ("ndof =", fes.ndof)
u,v = fes.TnT()

mu0 = 4*pi*1e-7
mur = mesh.MaterialCF({"magnet" : 1000}, default=1)
nu = 1/(mu0*mur)
mag = mesh.MaterialCF({"magnet" : (1,0,0)}, default=(0,0,0))

lhs = nu*curl(u)*curl(v)*dx + 1e-8*nu*u*v*dx
rhs = mag*curl(v) * dx("magnet")
ndof = 33432

We solve by a conjugate gradient iteration with a BDDC preconditioner. The essential boundary condition \(A \times n = 0\) on the outer boundary is given as an argument. Solve assembles the system, sets up the preconditioner, and solves, all using task-parallelization:

[5]:
gfu = Solve(lhs==rhs, u[BND("outer")]==(0,0,0),
            preconditioners.BDDC(),
            solvers.CGSolver.Creator(printrates=True, tol=1e-8))
CG iteration 1, residual = 0.004836623167885015
CG iteration 2, residual = 0.002246954265573345
CG iteration 3, residual = 0.002830066886512432
CG iteration 4, residual = 0.0015270995845501941
CG iteration 5, residual = 0.0013602068312717173
CG iteration 6, residual = 0.0009161808802969581
CG iteration 7, residual = 0.0006896337721122215
CG iteration 8, residual = 0.0006000524481125523
CG iteration 9, residual = 0.00041445501067867744
CG iteration 10, residual = 0.0003163154426014032
CG iteration 11, residual = 0.0002543007377738126
CG iteration 12, residual = 0.00018106238295077302
CG iteration 13, residual = 0.0001533864040405583
CG iteration 14, residual = 0.00010661446827380029
CG iteration 15, residual = 7.112145252677773e-05
CG iteration 16, residual = 5.643800625476917e-05
CG iteration 17, residual = 4.097251844430763e-05
CG iteration 18, residual = 3.308602321984472e-05
CG iteration 19, residual = 2.3080253168371063e-05
CG iteration 20, residual = 1.49942309400795e-05
CG iteration 21, residual = 1.1261012353353388e-05
CG iteration 22, residual = 7.4163660842981734e-06
CG iteration 23, residual = 5.2499088915510446e-06
CG iteration 24, residual = 3.764096566308379e-06
CG iteration 25, residual = 2.4410987259660412e-06
CG iteration 26, residual = 2.9094847565736497e-06
CG iteration 27, residual = 1.6390861869487853e-06
CG iteration 28, residual = 1.0244399228633437e-06
CG iteration 29, residual = 8.016637482344015e-07
CG iteration 30, residual = 5.293970037985414e-07
CG iteration 31, residual = 3.765334438461627e-07
CG iteration 32, residual = 2.7493924021655567e-07
CG iteration 33, residual = 1.8664072027703245e-07
CG iteration 34, residual = 1.3669030495972952e-07
CG iteration 35, residual = 9.59157631316092e-08
CG iteration 36, residual = 6.532228983605607e-08
CG iteration 37, residual = 4.410705080040278e-08
CG iteration 38, residual = 2.9584661354680354e-08
CG iteration 39, residual = 2.1269333335335355e-08
CG iteration 40, residual = 1.4025556476108366e-08
CG iteration 41, residual = 8.876428976764054e-09
CG iteration 42, residual = 6.455787350099097e-09
CG iteration 43, residual = 5.244312678130083e-09
CG iteration 44, residual = 4.122747919775087e-09
CG iteration 45, residual = 2.7224672171809243e-09
CG iteration 46, residual = 3.2777094157244417e-09
CG iteration 47, residual = 1.5528452160382382e-09
CG iteration 48, residual = 1.0328503550817528e-09
CG iteration 49, residual = 6.823658799168402e-10
CG iteration 50, residual = 4.919552809964914e-10
CG iteration 51, residual = 3.6606046154954623e-10
CG iteration 52, residual = 3.3532938751184526e-10
CG iteration 53, residual = 1.7869903007788144e-10
CG iteration 54, residual = 1.195857074337359e-10
CG iteration 55, residual = 8.061156654478116e-11
CG iteration 56, residual = 7.863876055579725e-11
CG iteration 57, residual = 4.628771482084463e-11

The magnetic flux density is \(B = \Curl A\):

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