Debian Codegraph @

python/demo/undocumented/advection-diffusion/demo_advection-diffusion.py

raw
1 # Copyright (C) 2007 Kristian B. Oelgaard
2 #
3 # This file is part of DOLFIN.
4 #
5 # DOLFIN is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # DOLFIN is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Modified by Anders Logg, 2008
19 # Modified by Johan Hake, 2008
20 # Modified by Garth N. Wells, 2009
21 #
22 # This demo solves the time-dependent convection-diffusion equation by
23 # a SUPG stabilized method. The velocity field used in the simulation
24 # is the output from the Stokes (Taylor-Hood) demo. The sub domains
25 # for the different boundary conditions are computed by the demo
26 # program in src/demo/subdomains.
27
28 from dolfin import *
29 import matplotlib.pyplot as plt
30
31 def boundary_value(n):
32 if n < 10:
33 return float(n)/10.0
34 else:
35 return 1.0
36
37 # Load mesh and subdomains
38 mesh = Mesh("../dolfin_fine.xml.gz")
39 sub_domains = MeshFunction("size_t", mesh, "../dolfin_fine_subdomains.xml.gz");
40 h = CellDiameter(mesh)
41
42 # Create FunctionSpaces
43 Q = FunctionSpace(mesh, "CG", 1)
44 V = VectorFunctionSpace(mesh, "CG", 2)
45
46 # Create velocity Function from file
47 velocity = Function(V);
48 File("../dolfin_fine_velocity.xml.gz") >> velocity
49
50 # Initialise source function and previous solution function
51 f = Constant(0.0)
52 u0 = Function(Q)
53
54 # Parameters
55 T = 5.0
56 dt = 0.1
57 t = dt
58 c = 0.00005
59
60 # Test and trial functions
61 u, v = TrialFunction(Q), TestFunction(Q)
62
63 # Mid-point solution
64 u_mid = 0.5*(u0 + u)
65
66 # Residual
67 r = u - u0 + dt*(dot(velocity, grad(u_mid)) - c*div(grad(u_mid)) - f)
68
69 # Galerkin variational problem
70 F = v*(u - u0)*dx + dt*(v*dot(velocity, grad(u_mid))*dx \
71 + c*dot(grad(v), grad(u_mid))*dx)
72
73 # Add SUPG stabilisation terms
74 vnorm = sqrt(dot(velocity, velocity))
75 F += (h/(2.0*vnorm))*dot(velocity, grad(v))*r*dx
76
77 # Create bilinear and linear forms
78 a = lhs(F)
79 L = rhs(F)
80
81 # Set up boundary condition
82 g = Constant(boundary_value(0))
83 bc = DirichletBC(Q, g, sub_domains, 1)
84
85 # Assemble matrix
86 A = assemble(a)
87 bc.apply(A)
88
89 # Create linear solver and factorize matrix
90 solver = LUSolver(A)
91
92 # Output file
93 out_file = File("results/temperature.pvd")
94
95 # Set intial condition
96 u = u0
97
98 # Time-stepping, plot initial condition.
99 i = 0
100 plt.figure()
101 plot(u, title=r"t = {0:1.1f}".format(0.0))
102 i += 1
103
104 while t - T < DOLFIN_EPS:
105 # Assemble vector and apply boundary conditions
106 b = assemble(L)
107 bc.apply(b)
108
109 # Solve the linear system (re-use the already factorized matrix A)
110 solver.solve(u.vector(), b)
111
112 # Copy solution from previous interval
113 u0 = u
114
115 # Plot solution
116 if i % 5 == 0:
117 plt.figure()
118 plot(u, title=r"t = {0:1.1f}".format(t))
119
120 # Save the solution to file
121 out_file << (u, t)
122
123 # Move to next interval and adjust boundary condition
124 t += dt
125 i += 1
126 g.assign(boundary_value(int(t/dt)))
127
128 plt.show()