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
Introducing a vector potential \(A\) such that \(B = \Curl A\), and combining the equations, we get
In weak form: Find \(A \in H(\Curl)\) such that
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.004836623167885012
CG iteration 2, residual = 0.0022469542655733517
CG iteration 3, residual = 0.002830066886512437
CG iteration 4, residual = 0.0015270995845501957
CG iteration 5, residual = 0.0013602068312717193
CG iteration 6, residual = 0.0009161808802969595
CG iteration 7, residual = 0.0006896337721122232
CG iteration 8, residual = 0.0006000524481125536
CG iteration 9, residual = 0.00041445501067867766
CG iteration 10, residual = 0.0003163154426014028
CG iteration 11, residual = 0.0002543007377738109
CG iteration 12, residual = 0.0001810623829507721
CG iteration 13, residual = 0.00015338640404055824
CG iteration 14, residual = 0.00010661446827380052
CG iteration 15, residual = 7.112145252677771e-05
CG iteration 16, residual = 5.6438006254769174e-05
CG iteration 17, residual = 4.097251844430736e-05
CG iteration 18, residual = 3.3086023219840616e-05
CG iteration 19, residual = 2.308025316834059e-05
CG iteration 20, residual = 1.4994230939428038e-05
CG iteration 21, residual = 1.1261012334269657e-05
CG iteration 22, residual = 7.416365448852375e-06
CG iteration 23, residual = 5.2498870012898e-06
CG iteration 24, residual = 3.7632685963690455e-06
CG iteration 25, residual = 2.4185860201506365e-06
CG iteration 26, residual = 2.5858862341523977e-06
CG iteration 27, residual = 1.7145242180669019e-06
CG iteration 28, residual = 1.025758412142538e-06
CG iteration 29, residual = 8.016891005092968e-07
CG iteration 30, residual = 5.293974020403745e-07
CG iteration 31, residual = 3.765334512854189e-07
CG iteration 32, residual = 2.7493924033257806e-07
CG iteration 33, residual = 1.866407202793409e-07
CG iteration 34, residual = 1.3669030495972608e-07
CG iteration 35, residual = 9.591576313125859e-08
CG iteration 36, residual = 6.532228983277075e-08
CG iteration 37, residual = 4.4107050755070476e-08
CG iteration 38, residual = 2.9584660519746296e-08
CG iteration 39, residual = 2.1269324364402363e-08
CG iteration 40, residual = 1.4025409927946896e-08
CG iteration 41, residual = 8.873036246716194e-09
CG iteration 42, residual = 6.3831085964506894e-09
CG iteration 43, residual = 4.137113418982394e-09
CG iteration 44, residual = 4.34701490515022e-09
CG iteration 45, residual = 3.0567595637488787e-09
CG iteration 46, residual = 2.662376700473752e-09
CG iteration 47, residual = 1.6151380244338663e-09
CG iteration 48, residual = 1.0357148900693343e-09
CG iteration 49, residual = 6.833055332351249e-10
CG iteration 50, residual = 4.992821316841829e-10
CG iteration 51, residual = 4.0197358373163444e-10
CG iteration 52, residual = 3.2627805992461534e-10
CG iteration 53, residual = 1.7657856709572054e-10
CG iteration 54, residual = 1.2127836484182306e-10
CG iteration 55, residual = 9.95206381561507e-11
CG iteration 56, residual = 7.667108322017756e-11
CG iteration 57, residual = 4.445502420291781e-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);