In this tutorial the implementation of inhomogeneous Dirichlet boundary conditions using a Dirichlet lift ansatz is shown.
Now we derive the corresponding variational formulation of the introduced problem: find such that
In order to use MUMPS as a solver, we need to import MPI.
The mesh is read from three files containing the coordinates, the elements and the boundary attributes of the mesh.
If there is an error reading those files, then an exception error message is returned.
else, the mesh is plotted using scaling factor of 100, a greyscale of 1.0 and one point per edge.
The Dirichlet lift and its gradient are defined.
Using the mesh and the homogeneous Dirichlet boundary conditions, the space can be built. We refine the space two times and set the polynomial degree to three. Then the elements of the space are built and the space is plotted.
The right hand side is computed, containing the integrals of the source term, the Dirichlet lift and its gradient.
In order to add the Dirichlet lift to the solution of the homogeneous Dirichlet problem we transform the solution, that is given as a vector related to the basis functions of space
, into an element formula, that can be evaluated at each point in every cell.
We save the solutions, the homogeneous Dirichlet solution and the Dirichlet lift in a MAT-file. To this end, the shape functions are computed on equidistant points using the trapezoidal quadrature rule.
Mesh: Import2dMesh(ncell = 1)
RHS Vector:
Vector(121, [-7.813458e-01, -1.331759e-01, -1.446913e-04, -8.977796e-02, -1.975711e-02, -1.504745e-02, -1.103641e-04, -3.490666e-03, 4.167881e-05, -1.009906e+00, -1.735826e-01, 3.466376e-04, -1.628611e-01, 8.126026e-03, -2.796551e-02, 4.594644e-05, -1.436936e-03, 1.759809e-05, -8.586824e-01, -6.786590e-01, -1.598694e-01, 5.110721e-03, -1.388192e-01, -6.395889e-03, -1.248594e-01, -3.466673e-03, -2.580936e-02, 8.124231e-04, -1.244348e-03, 5.860425e-05, -8.122847e-02, -1.558898e-02, -1.461248e-02, 2.876690e-04, -3.028574e-03, 1.411786e-04, -7.813458e-01, -1.331759e-01, -1.446913e-04, -1.628611e-01, 8.126026e-03, -2.796551e-02, 4.594644e-05, 1.436936e-03, -1.759809e-05, -8.977796e-02, -1.975711e-02, -1.504745e-02, -1.103641e-04, 3.490666e-03, -4.167881e-05, -6.786590e-01, -8.122847e-02, -1.558898e-02, -1.248594e-01, -3.466673e-03, -1.461248e-02, 2.876690e-04, 3.028574e-03, -1.411786e-04, -1.388192e-01, -6.395889e-03, -2.580936e-02, 8.124231e-04, 1.244348e-03, -5.860425e-05, -4.822921e-01, -5.869802e-01, -9.922001e-02, 6.668103e-03, -9.533732e-02, 3.713134e-03, -1.236099e-01, -9.222017e-03, -2.002021e-02, 1.475922e-03, 8.660092e-04, -9.094635e-05, -6.180870e-02, -9.103448e-03, -1.221454e-02, 6.601945e-04, 2.115183e-03, -2.198685e-04, -6.016121e-02, -8.736539e-03, -8.218690e-03, 9.139240e-04, 8.895484e-04, -2.644021e-04, -7.032423e-02, -1.180407e-02, -1.147942e-02, 1.894019e-03, 3.595180e-04, -1.091848e-04, -4.822921e-01, -9.922001e-02, 6.668103e-03, -6.180870e-02, -9.103448e-03, -1.221454e-02, 6.601945e-04, -2.115183e-03, 2.198685e-04, -9.533732e-02, 3.713134e-03, -2.002021e-02, 1.475922e-03, -8.660092e-04, 9.094635e-05, -6.016121e-02, -8.736539e-03, -1.147942e-02, 1.894019e-03, -3.595180e-04, 1.091848e-04, -8.218690e-03, 9.139240e-04, -8.895484e-04, 2.644021e-04])
System Matrix:
SparseMatrix(121x121, HashedSparseMatrix: 1521 (10.3886%) entries bound.)
Solver:
Mumps(n = 121)
Solution:
FrmE_Sum(ElementFormulaVector<1>(
hp2D::Value<
double>(), Vector(121, [-5.387654e-01, -3.006218e-01, 1.564124e-02, -1.425659e-01, -4.809499e-03, -1.088256e-01, 1.611971e-02, 8.625588e-03, -6.891406e-03, -7.373583e-01, -4.048944e-01, 1.925875e-02, -1.941122e-01, 2.767690e-03, -1.016416e-01, 3.259733e-03, -1.569518e-03, 2.857100e-04, -8.461932e-01, -6.183710e-01, -2.393483e-01, 9.380839e-03, -2.230022e-01, -2.981412e-03, -1.722831e-01, -6.882467e-03, -6.528885e-02, 2.490903e-03, -1.108972e-03, -1.050398e-05, -1.618117e-01, -6.560393e-03, -3.530271e-02, 1.727733e-03, -4.305751e-03, 3.179923e-04, -5.387655e-01, -3.006218e-01, 1.564124e-02, -1.941122e-01, 2.767687e-03, -1.016416e-01, 3.259733e-03, 1.569518e-03, -2.857100e-04, -1.425659e-01, -4.809502e-03, -1.088256e-01, 1.611971e-02, -8.625588e-03, 6.891407e-03, -6.183710e-01, -1.618117e-01, -6.560394e-03, -1.722831e-01, -6.882469e-03, -3.530271e-02, 1.727733e-03, 4.305752e-03, -3.179924e-04, -2.230022e-01, -2.981412e-03, -6.528885e-02, 2.490903e-03, 1.108972e-03, 1.050400e-05, -4.161721e-01, -5.639852e-01, -1.159405e-01, 2.828855e-03, -1.454700e-01, 1.444007e-03, -1.596678e-01, -4.353042e-03, -4.288914e-02, 1.375478e-03, 4.954845e-04, -1.172436e-04, -1.224176e-01, -1.613845e-03, -2.602445e-02, -1.262254e-04, 2.824662e-03, 6.029993e-05, -1.041387e-01, 1.097789e-03, -7.650684e-02, -1.372658e-02, -1.375732e-02, -7.269106e-03, -1.270253e-01, -1.308806e-03, -2.361722e-02, 2.517779e-03, -5.561139e-04, 1.292617e-04, -4.161721e-01, -1.159405e-01, 2.828853e-03, -1.224176e-01, -1.613847e-03, -2.602445e-02, -1.262255e-04, -2.824662e-03, -6.029997e-05, -1.454700e-01, 1.444005e-03, -4.288914e-02, 1.375478e-03, -4.954845e-04, 1.172437e-04, -1.041387e-01, 1.097788e-03, -2.361722e-02, 2.517779e-03, 5.561139e-04, -1.292618e-04, -7.650684e-02, -1.372658e-02, 1.375732e-02, 7.269106e-03])) + ParsedFormula<
Real>((sin(
pi*x)*cos(
pi/2*y))))
2 from libconceptspy
import concepts
3 from libconceptspy
import hp1D
4 from libconceptspy
import hp2D
5 from libconceptspy
import graphics
11 SOURCEDIR = os.environ[
'CONCEPTSPATH']
16 elms = SOURCEDIR +
"/applications/unitsquareElements.dat",
17 boundary= SOURCEDIR +
"/applications/unitsquareEdges.dat"
22 print(
"Mesh import error")
24 graphics.MeshEPS_r(msh = mesh, filename =
"mesh.eps", scale=100, greyscale=1.0, nPoints=1)
26 DirichletLift = concepts.ParsedFormula_r(formula=
"(sin(pi*x)*cos(pi/2*y))")
27 DirichletLiftGradx = concepts.ParsedFormula_r(formula=
"(pi*cos(pi*x)*cos(pi/2*y))")
28 DirichletLiftGrady = concepts.ParsedFormula_r(formula=
"(-pi/2*sin(pi*x)*sin(pi/2*y))")
32 boundaryConditions.add(attrib = i, bcObject =
concepts.Boundary(type = concepts.Boundary.DIRICHLET))
38 graphics.MeshEPS_r(spc = space, filename =
"space.eps", scale=100, greyscale=1.0, nPoints=1 )
40 lform = hp2D.Riesz_r(frm = concepts.ParsedFormula_r( formula =
"(-5*exp(-(x-0.5)*(x-0.5)-(y-0.5)*(y-0.5)))" ) )
41 rhs = concepts.Vector_r(spc = space, lf = lform)
43 lformDirichletGrad = hp2D.GradLinearForm_r(frm1=DirichletLiftGradx, frm2=DirichletLiftGrady)
44 rhsDirichletGrad = concepts.Vector_r(spc = space, lf = lformDirichletGrad)
46 lformDirichlet = hp2D.Riesz_r(frm = DirichletLift)
47 rhsDirichlet = concepts.Vector_r(spc = space, lf = lformDirichlet)
49 rhs = rhs - rhsDirichletGrad - rhsDirichlet
50 print(
'RHS Vector:\n')
54 A = concepts.SparseMatrix_r(spc = space, bf = la)
57 id = hp2D.Identity_r()
58 M = concepts.SparseMatrix_r(spc = space, bf = id)
61 print(
'\n System Matrix:\n')
64 solver = concepts.Mumps_r(A = A)
65 sol = concepts.Vector_r(spc = space)
66 solver(fncY = rhs, fncX = sol)
71 solFormula = concepts.ElementFormulaVector_1(spc = space, v = sol, f = hp2D.Value_r_r())
72 print (
'\nSolution:\n')
73 print(concepts.ElementFormulaContainer_r_r(solFormula)+concepts.ElementFormulaContainer_r_r(DirichletLift))
76 hp2D.IntegrableQuad.setTensor( concepts.TRAPEZE,
True, 8 )
77 space.recomputeShapefunctions()
81 filename =
"inhomDirichlet",
82 frm = concepts.ElementFormulaContainer_r_r(solFormula)+concepts.ElementFormulaContainer_r_r(DirichletLift))
88 if __name__ ==
"__main__":