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