Debian Codegraph @

python/demo/undocumented/dg-advection-diffusion/demo_dg-advection-diffusion.py

raw
1 """ Steady state advection-diffusion equation,
2 discontinuous formulation using full upwinding.
3
4 Implemented in python from cpp demo by Johan Hake.
5 """
6
7 # Copyright (C) 2008 Johan Hake
8 #
9 # This file is part of DOLFIN.
10 #
11 # DOLFIN is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU Lesser General Public License as published by
13 # the Free Software Foundation, either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # DOLFIN is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU Lesser General Public License for more details.
20 #
21 # You should have received a copy of the GNU Lesser General Public License
22 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
23 #
24 # First added: 2008-06-19
25 # Last changed: 2008-12-23
26
27 from dolfin import *
28 import matplotlib.pyplot as plt
29
30
31 # FIXME: Make mesh ghosted
32 parameters["ghost_mode"] = "shared_facet"
33
34 class DirichletBoundary(SubDomain):
35 def inside(self, x, on_boundary):
36 return abs(x[0] - 1.0) < DOLFIN_EPS and on_boundary
37
38 # Load mesh
39 mesh = Mesh("../unitsquare_64_64.xml.gz")
40
41 # Defining the function spaces
42 V_dg = FunctionSpace(mesh, "DG", 1)
43 V_cg = FunctionSpace(mesh, "CG", 1)
44 V_u = VectorFunctionSpace(mesh, "CG", 2)
45
46 # Create velocity Function
47 u = Function(V_u, "../unitsquare_64_64_velocity.xml.gz")
48
49 # Test and trial functions
50 v = TestFunction(V_dg)
51 phi = TrialFunction(V_dg)
52
53 # Diffusivity
54 kappa = Constant(0.0)
55
56 # Source term
57 f = Constant(0.0)
58
59 # Penalty term
60 alpha = Constant(5.0)
61
62 # Mesh-related functions
63 n = FacetNormal(mesh)
64 h = CellDiameter(mesh)
65 h_avg = (h('+') + h('-'))/2
66
67 # ( dot(v, n) + |dot(v, n)| )/2.0
68 un = (dot(u, n) + abs(dot(u, n)))/2.0
69
70 # Bilinear form
71 a_int = dot(grad(v), kappa*grad(phi) - u*phi)*dx
72
73 a_fac = kappa*(alpha/h('+'))*dot(jump(v, n), jump(phi, n))*dS \
74 - kappa*dot(avg(grad(v)), jump(phi, n))*dS \
75 - kappa*dot(jump(v, n), avg(grad(phi)))*dS
76
77 a_vel = dot(jump(v), un('+')*phi('+') - un('-')*phi('-') )*dS + dot(v, un*phi)*ds
78
79 a = a_int + a_fac + a_vel
80
81 # Linear form
82 L = v*f*dx
83
84 # Set up boundary condition (apply strong BCs)
85 g = Expression("sin(pi*5.0*x[1])", degree=2)
86 bc = DirichletBC(V_dg, g, DirichletBoundary(), "geometric")
87
88 # Solution function
89 phi_h = Function(V_dg)
90
91 # Assemble and apply boundary conditions
92 A = assemble(a)
93 b = assemble(L)
94 bc.apply(A, b)
95
96 # Solve system
97 solve(A, phi_h.vector(), b)
98
99 # Project solution to a continuous function space
100 up = project(phi_h, V=V_cg)
101
102 file = File("temperature.pvd")
103 file << up
104
105 # Plot solution
106 plot(up)
107 plt.show()