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 = 32867
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.004809678530125144
CG iteration 2, residual = 0.0033223190132442375
CG iteration 3, residual = 0.0033115883177540118
CG iteration 4, residual = 0.0027467579779245637
CG iteration 5, residual = 0.001465876527059527
CG iteration 6, residual = 0.0012167024999244261
CG iteration 7, residual = 0.000809658230957323
CG iteration 8, residual = 0.0006570076838867975
CG iteration 9, residual = 0.0004750869183442839
CG iteration 10, residual = 0.00036224214074475703
CG iteration 11, residual = 0.00025441834248532966
CG iteration 12, residual = 0.00016194989094436819
CG iteration 13, residual = 0.00011358302702051961
CG iteration 14, residual = 8.950382818007843e-05
CG iteration 15, residual = 5.394126812286551e-05
CG iteration 16, residual = 3.916041787789045e-05
CG iteration 17, residual = 2.7082318026810977e-05
CG iteration 18, residual = 1.8249635355157992e-05
CG iteration 19, residual = 1.3587045916048196e-05
CG iteration 20, residual = 9.933783668470196e-06
CG iteration 21, residual = 1.1998219206576379e-05
CG iteration 22, residual = 5.651299603914058e-06
CG iteration 23, residual = 3.5936672275827293e-06
CG iteration 24, residual = 2.5589070702097277e-06
CG iteration 25, residual = 1.9779477679038514e-06
CG iteration 26, residual = 1.2770934677229675e-06
CG iteration 27, residual = 8.740832403627853e-07
CG iteration 28, residual = 5.927228746857171e-07
CG iteration 29, residual = 4.008901703446772e-07
CG iteration 30, residual = 2.856458196958205e-07
CG iteration 31, residual = 2.0292685846593577e-07
CG iteration 32, residual = 1.3199636987359585e-07
CG iteration 33, residual = 9.850315547955077e-08
CG iteration 34, residual = 6.734297704510117e-08
CG iteration 35, residual = 4.756065968767561e-08
CG iteration 36, residual = 2.9205031515325325e-08
CG iteration 37, residual = 2.7190610698308758e-08
CG iteration 38, residual = 2.2941444769812884e-08
CG iteration 39, residual = 1.3022063515790833e-08
CG iteration 40, residual = 8.395463697882137e-09
CG iteration 41, residual = 5.711978914119383e-09
CG iteration 42, residual = 4.1810498404090986e-09
CG iteration 43, residual = 2.794949339435942e-09
CG iteration 44, residual = 1.8595331195498995e-09
CG iteration 45, residual = 1.2654495195226187e-09
CG iteration 46, residual = 8.583836341447648e-10
CG iteration 47, residual = 5.766587114144673e-10
CG iteration 48, residual = 4.2177693581573823e-10
CG iteration 49, residual = 2.761184950053426e-10
CG iteration 50, residual = 2.1705249118967374e-10
CG iteration 51, residual = 1.2859584725168993e-10
CG iteration 52, residual = 8.662718462806314e-11
CG iteration 53, residual = 7.994970280842162e-11
CG iteration 54, residual = 6.670530233220546e-11
CG iteration 55, residual = 3.474393712375054e-11
CG iteration 56, residual = 2.3558473348414453e-11
CG iteration 57, residual = 1.8664412153351024e-11
CG iteration 58, residual = 1.3432914340236123e-11
CG iteration 59, residual = 8.571783257671756e-12
CG iteration 60, residual = 5.931227488455674e-12
CG iteration 61, residual = 3.9432798758273265e-12
CG iteration 62, residual = 2.6956797970125108e-12
CG iteration 63, residual = 1.7387115295955187e-12
CG iteration 64, residual = 1.2104140063595336e-12
CG iteration 65, residual = 7.623361306717956e-13
CG iteration 66, residual = 4.941741912146277e-13
CG iteration 67, residual = 3.208597501355258e-13
CG iteration 68, residual = 2.247578188088553e-13
CG iteration 69, residual = 3.084228370567034e-13
CG iteration 70, residual = 1.3622744310357325e-13
CG iteration 71, residual = 9.423005295980765e-14
CG iteration 72, residual = 6.131238230819629e-14
CG iteration 73, residual = 4.207217018709037e-14
CG iteration 74, residual = 2.763793879453184e-14
CG iteration 75, residual = 1.7727337430286687e-14
CG iteration 76, residual = 1.2116220127287e-14
CG iteration 77, residual = 9.580853455245936e-15
CG iteration 78, residual = 6.660544024237676e-15
CG iteration 79, residual = 3.992524463942254e-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);
[ ]:
[ ]: