Debian Codegraph @

python/demo/undocumented/sub-function-assignment/demo_sub-function-assignment.py

raw
1 """This script demonstrate how to use sub function assignment."""
2
3 # Copyright (C) 2013 Johan Hake
4 #
5 # This file is part of DOLFIN.
6 #
7 # DOLFIN is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # DOLFIN is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # First added: 2013-11-08
21 # Last changed: 2013-11-08
22
23 from dolfin import *
24 import time
25 import matplotlib.pyplot as plt
26
27
28 # Create mesh and define function spaces
29 mesh = UnitSquareMesh(256, 256)
30 V = FunctionSpace(mesh, "CG", 1)
31 VV = VectorFunctionSpace(mesh, "CG", 1)
32
33 # Define function
34 v0 = Expression("sin(2*pi*x[0])*sin(2*pi*x[1])", degree=2)
35 v1 = Expression("cos(2*pi*x[0])*cos(2*pi*x[1])", degree=2)
36
37 vv1 = Function(VV)
38 vv2 = Function(VV)
39 vv3 = Function(VV)
40
41 u0 = Function(V)
42 u1 = Function(V)
43
44 u0.interpolate(v0)
45 u1.interpolate(v1)
46
47 # Compute projection (L2-projection)
48 t0 = time.time()
49 vv0 = project(as_vector((u0, u1)), V=VV)
50 tp = time.time() - t0
51
52 # Compute interpolation (evaluating dofs)
53 t0 = time.time()
54 vv1.interpolate(Expression(("v0", "v1"), v0=v0, v1=v1, degree=2))
55 ti = time.time() - t0
56
57 # Assign mixed function from two scalar functions
58 t0 = time.time()
59 assign(vv2, [u0, u1])
60 ta0 = time.time() - t0
61
62 # Assign mixed function from two scalar functions using FunctionAssigner
63 assigner = FunctionAssigner(VV, [V, V])
64 t0 = time.time()
65 assigner.assign(vv3, [u0, u1])
66 ta1 = time.time() - t0
67
68 # Plot functions
69 plt.figure()
70 plot(vv0, title="Projection; time=%.3fs" % tp)
71 plt.figure()
72 plot(vv1, title="Interpolation; time=%.3fs" % ti)
73 plt.figure()
74 plot(vv2, title="Assignment; time=%.3fs" % ta0)
75 plt.figure()
76 plot(vv3, title="Assignment (cached); time=%.3fs" % ta1)
77 plt.show()