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.00483662316788501
CG iteration 2, residual = 0.0022469542655733474
CG iteration 3, residual = 0.002830066886512432
CG iteration 4, residual = 0.0015270995845501922
CG iteration 5, residual = 0.0013602068312717163
CG iteration 6, residual = 0.0009161808802969563
CG iteration 7, residual = 0.0006896337721122208
CG iteration 8, residual = 0.000600052448112551
CG iteration 9, residual = 0.00041445501067867614
CG iteration 10, residual = 0.00031631544260140096
CG iteration 11, residual = 0.00025430073777380765
CG iteration 12, residual = 0.0001810623829507699
CG iteration 13, residual = 0.00015338640404055748
CG iteration 14, residual = 0.00010661446827380063
CG iteration 15, residual = 7.112145252677754e-05
CG iteration 16, residual = 5.6438006254768924e-05
CG iteration 17, residual = 4.097251844430732e-05
CG iteration 18, residual = 3.308602321984366e-05
CG iteration 19, residual = 2.3080253168364944e-05
CG iteration 20, residual = 1.4994230939951694e-05
CG iteration 21, residual = 1.1261012349612746e-05
CG iteration 22, residual = 7.4163659597457106e-06
CG iteration 23, residual = 5.249904600900988e-06
CG iteration 24, residual = 3.7639343066110976e-06
CG iteration 25, residual = 2.4367196431772443e-06
CG iteration 26, residual = 2.8616428410813373e-06
CG iteration 27, residual = 1.6486311126936401e-06
CG iteration 28, residual = 1.0245922531616527e-06
CG iteration 29, residual = 8.016666717151094e-07
CG iteration 30, residual = 5.293970497190489e-07
CG iteration 31, residual = 3.7653344470396733e-07
CG iteration 32, residual = 2.74939240229934e-07
CG iteration 33, residual = 1.8664072027729363e-07
CG iteration 34, residual = 1.3669030495968688e-07
CG iteration 35, residual = 9.59157631312501e-08
CG iteration 36, residual = 6.532228983270978e-08
CG iteration 37, residual = 4.4107050754240324e-08
CG iteration 38, residual = 2.9584660504400173e-08
CG iteration 39, residual = 2.1269324197185575e-08
CG iteration 40, residual = 1.4025407090476477e-08
CG iteration 41, residual = 8.87296410290126e-09
CG iteration 42, residual = 6.3811575087068195e-09
CG iteration 43, residual = 4.071972938298003e-09
CG iteration 44, residual = 3.309232278376451e-09
CG iteration 45, residual = 3.499733798831112e-09
CG iteration 46, residual = 2.184273375969862e-09
CG iteration 47, residual = 1.7711807091027606e-09
CG iteration 48, residual = 1.0456684466343542e-09
CG iteration 49, residual = 6.832488475387787e-10
CG iteration 50, residual = 4.974439525442159e-10
CG iteration 51, residual = 3.9427634462077275e-10
CG iteration 52, residual = 3.2878550057212926e-10
CG iteration 53, residual = 1.7677565065402182e-10
CG iteration 54, residual = 1.2024810256928e-10
CG iteration 55, residual = 9.01642844250281e-11
CG iteration 56, residual = 8.046266014707327e-11
CG iteration 57, residual = 4.460436182591891e-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);