23. Implement our own system assembling#
In this tutorial we
write integrators for \(\int_T f v dx\) and \(\int_T \nabla u \nabla v dx\):
put together element matrices to the global system matrix:
The implementation is available on github: TUWien-ASC/NGS-myassembling
from ngsolve import *
from ngsolve.webgui import Draw
import myassembling
dir (myassembling)
Loading myassembling library
['MyAssembleMatrix',
'MyLaplace',
'MySource',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__spec__']
mesh = Mesh(unit_square.GenerateMesh(maxh=0.2))
fes = H1(mesh, order=2, dirichlet=".*")
u, v = fes.TnT()
23.1. use our own integrators for element matrix calculation:#
a = BilinearForm(fes)
a += myassembling.MyLaplace(1)
a.Assemble()
f = LinearForm(fes)
f += myassembling.MySource(x)
f.Assemble();
gfu = GridFunction(fes)
gfu.vec.data = a.mat.Inverse(fes.FreeDofs()) * f.vec
Draw (gfu);
23.2. we can inspect the element matrix:#
mylap = myassembling.MyLaplace(1)
ei = ElementId(VOL,17)
mylap.CalcElementMatrix(fes.GetFE(ei), mesh.GetTrafo(ei))
0.651196 -0.417484 -0.233712 -0.0695807 0.108533 -0.0389519
-0.417484 0.65156 -0.234076 0.108593 -0.0695807 -0.0390126
-0.233712 -0.234076 0.467787 -0.0390126 -0.0389519 0.0779646
-0.0695807 0.108593 -0.0390126 0.0368863 -0.0173952 -0.00975316
0.108533 -0.0695807 -0.0389519 -0.0173952 0.0368863 -0.00973798
-0.0389519 -0.0390126 0.0779646 -0.00975316 -0.00973798 0.0368863
23.3. use our own matrix assembling function:#
for l in range(4): mesh.Refine()
fes.Update()
gfu.Update()
f.Assemble();
mymatrix = myassembling.MyAssembleMatrix(fes, myassembling.MyLaplace(CF(1)))
# mymatrix = myassembling.MyAssembleMatrix(fes, (grad(u)*grad(v)*dx)[0].MakeBFI())
if fes.ndof < 100000:
gfu.vec.data = mymatrix.Inverse(fes.FreeDofs()) * f.vec
Draw (gfu);
We assemble matrix
23.4. Exercise:#
Implement
MyAssembleVectorfor building the right hand side vector for \(\int_\Omega f v \, dx\)Implement
MyNeumannIntegratorfor evaluating \(\int_{\partial \Omega} g v \, ds\)Have a look into thread-parallel assembling at NGSolve-itutorials unit 9.2