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\):
Introducing a vector-potential \(A\) such that \(B = \Curl A\), and putting equations together 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 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.004841340224441177
CG iteration 2, residual = 0.005042736495448081
CG iteration 3, residual = 0.003149747490985739
CG iteration 4, residual = 0.0028071729924236534
CG iteration 5, residual = 0.0018044283205905717
CG iteration 6, residual = 0.0012102064412063645
CG iteration 7, residual = 0.0009603395596834441
CG iteration 8, residual = 0.0008701989504798815
CG iteration 9, residual = 0.0006325610483462738
CG iteration 10, residual = 0.00045326784080375755
CG iteration 11, residual = 0.0003176167389469698
CG iteration 12, residual = 0.00023160559528551754
CG iteration 13, residual = 0.00020737408636192385
CG iteration 14, residual = 0.00013494579925442754
CG iteration 15, residual = 8.950807064981966e-05
CG iteration 16, residual = 6.501540028524961e-05
CG iteration 17, residual = 8.671685447268057e-05
CG iteration 18, residual = 4.156454508824304e-05
CG iteration 19, residual = 2.8961760442046852e-05
CG iteration 20, residual = 1.9763305509995757e-05
CG iteration 21, residual = 1.293166125731175e-05
CG iteration 22, residual = 9.125442581332043e-06
CG iteration 23, residual = 6.593115124202604e-06
CG iteration 24, residual = 4.346076508667583e-06
CG iteration 25, residual = 3.183636677587354e-06
CG iteration 26, residual = 2.0408071560160175e-06
CG iteration 27, residual = 1.520233042591004e-06
CG iteration 28, residual = 9.96491276677403e-07
CG iteration 29, residual = 7.383967927797615e-07
CG iteration 30, residual = 1.1507110205459653e-06
CG iteration 31, residual = 4.73109954483597e-07
CG iteration 32, residual = 3.388430295151361e-07
CG iteration 33, residual = 3.2915148279100736e-07
CG iteration 34, residual = 1.8713442407001812e-07
CG iteration 35, residual = 1.2817046772566249e-07
CG iteration 36, residual = 8.236173822524899e-08
CG iteration 37, residual = 5.80727912833576e-08
CG iteration 38, residual = 3.836539836338653e-08
CG iteration 39, residual = 2.7372783586568665e-08
CG iteration 40, residual = 1.8863173535273948e-08
CG iteration 41, residual = 1.2926702234799606e-08
CG iteration 42, residual = 1.088943609816547e-08
CG iteration 43, residual = 9.881155802585233e-09
CG iteration 44, residual = 1.0375266510988864e-08
CG iteration 45, residual = 5.136866160462744e-09
CG iteration 46, residual = 3.408506607266809e-09
CG iteration 47, residual = 2.4522223989395606e-09
CG iteration 48, residual = 2.2702390152569666e-09
CG iteration 49, residual = 1.4230277117703815e-09
CG iteration 50, residual = 9.617372701537868e-10
CG iteration 51, residual = 6.138983820918785e-10
CG iteration 52, residual = 4.137534999610876e-10
CG iteration 53, residual = 2.7945125434248717e-10
CG iteration 54, residual = 1.9121242724903383e-10
CG iteration 55, residual = 1.2895092724548237e-10
CG iteration 56, residual = 9.60362143683957e-11
CG iteration 57, residual = 1.376592555057937e-10
CG iteration 58, residual = 5.998058456837697e-11
CG iteration 59, residual = 4.1771486572089556e-11
CG iteration 60, residual = 4.2033515908537305e-11
CG iteration 61, residual = 2.206368544124714e-11
CG iteration 62, residual = 1.462776491264277e-11
CG iteration 63, residual = 9.555449840175157e-12
CG iteration 64, residual = 6.711632405541531e-12
CG iteration 65, residual = 4.3507982245304855e-12
CG iteration 66, residual = 3.181498743684312e-12
CG iteration 67, residual = 2.0771591315464e-12
CG iteration 68, residual = 1.3371061846756101e-12
CG iteration 69, residual = 1.1901596071550974e-12
CG iteration 70, residual = 1.1477775139033175e-12
CG iteration 71, residual = 5.395413918315698e-13
CG iteration 72, residual = 3.7807360930940786e-13
CG iteration 73, residual = 2.6917105419692495e-13
CG iteration 74, residual = 2.497419214566878e-13
CG iteration 75, residual = 1.5118044888173325e-13
CG iteration 76, residual = 9.122341204457403e-14
CG iteration 77, residual = 7.10394771103354e-14
CG iteration 78, residual = 6.825433117633169e-14
CG iteration 79, residual = 3.672218819307063e-14
CG iteration 80, residual = 2.4758396644604718e-14
CG iteration 81, residual = 1.6347664592600578e-14
CG iteration 82, residual = 1.2700718856790635e-14
CG iteration 83, residual = 1.1892804787569635e-14
CG iteration 84, residual = 1.0657574317351101e-14
CG iteration 85, residual = 6.093355927881354e-15
CG iteration 86, residual = 5.757268556405355e-15
CG iteration 87, residual = 3.432453256151991e-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);
[ ]:
[ ]: