Debian Codegraph @

python/demo/undocumented/dg-poisson/demo_dg-poisson.py

raw
1 """This demo program solves Poisson's equation
2
3 - div grad u(x, y) = f(x, y)
4
5 on the unit square with source f given by
6
7 f(x, y) = -100*exp(-((x - 0.5)^2 + (y - 0.5)^2)/0.02)
8
9 and boundary conditions given by
10
11 u(x, y) = u0 on x = 0 and x = 1
12 du/dn(x, y) = g on y = 0 and y = 1
13
14 where
15
16 u0 = x + 0.25*sin(2*pi*x)
17 g = (y - 0.5)**2
18
19 using a discontinuous Galerkin formulation (interior penalty method).
20 """
21
22 # Copyright (C) 2007 Kristian B. Oelgaard
23 #
24 # This file is part of DOLFIN.
25 #
26 # DOLFIN is free software: you can redistribute it and/or modify
27 # it under the terms of the GNU Lesser General Public License as published by
28 # the Free Software Foundation, either version 3 of the License, or
29 # (at your option) any later version.
30 #
31 # DOLFIN is distributed in the hope that it will be useful,
32 # but WITHOUT ANY WARRANTY; without even the implied warranty of
33 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 # GNU Lesser General Public License for more details.
35 #
36 # You should have received a copy of the GNU Lesser General Public License
37 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
38
39 from dolfin import *
40
41
42 # FIXME: Make mesh ghosted
43 parameters["ghost_mode"] = "shared_facet"
44
45 # Define class marking Dirichlet boundary (x = 0 or x = 1)
46 class DirichletBoundary(SubDomain):
47 def inside(self, x, on_boundary):
48 return on_boundary and near(x[0]*(1 - x[0]), 0)
49
50 # Define class marking Neumann boundary (y = 0 or y = 1)
51 class NeumanBoundary(SubDomain):
52 def inside(self, x, on_boundary):
53 return on_boundary and near(x[1]*(1 - x[1]), 0)
54
55 # Create mesh and define function space
56 mesh = UnitSquareMesh(24, 24)
57 V = FunctionSpace(mesh, 'DG', 1)
58
59 # Define test and trial functions
60 u = TrialFunction(V)
61 v = TestFunction(V)
62
63 # Define normal vector and mesh size
64 n = FacetNormal(mesh)
65 h = CellDiameter(mesh)
66 h_avg = (h('+') + h('-'))/2
67
68 # Define the source term f, Dirichlet term u0 and Neumann term g
69 f = Expression('-100.0*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)', degree=2)
70 u0 = Expression('x[0] + 0.25*sin(2*pi*x[1])', degree=2)
71 g = Expression('(x[1] - 0.5)*(x[1] - 0.5)', degree=2)
72
73 # Mark facets of the mesh
74 boundaries = MeshFunction('size_t', mesh, mesh.topology().dim()-1, 0)
75 NeumanBoundary().mark(boundaries, 2)
76 DirichletBoundary().mark(boundaries, 1)
77
78 # Define outer surface measure aware of Dirichlet and Neumann boundaries
79 ds = Measure('ds', domain=mesh, subdomain_data=boundaries)
80
81 # Define parameters
82 alpha = 4.0
83 gamma = 8.0
84
85 # Define variational problem
86 a = dot(grad(v), grad(u))*dx \
87 - dot(avg(grad(v)), jump(u, n))*dS \
88 - dot(jump(v, n), avg(grad(u)))*dS \
89 + alpha/h_avg*dot(jump(v, n), jump(u, n))*dS \
90 - dot(grad(v), u*n)*ds(1) \
91 - dot(v*n, grad(u))*ds(1) \
92 + (gamma/h)*v*u*ds(1)
93 L = v*f*dx - u0*dot(grad(v), n)*ds(1) + (gamma/h)*u0*v*ds(1) + g*v*ds(2)
94
95 # Compute solution
96 u = Function(V)
97 solve(a == L, u)
98 print("Solution vector norm (0): {!r}".format(u.vector().norm("l2")))