| 1 |
"""This script demonstrates the L2 projection of a function onto a |
| 2 |
non-matching mesh.""" |
| 3 |
|
| 4 |
# Copyright (C) 2009 Garth N. Wells |
| 5 |
# |
| 6 |
# This file is part of DOLFIN. |
| 7 |
# |
| 8 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU Lesser General Public License as published by |
| 10 |
# the Free Software Foundation, either version 3 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
# DOLFIN is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU Lesser General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU Lesser General Public License |
| 19 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
# |
| 21 |
# Modified by Anders Logg 2011 |
| 22 |
# |
| 23 |
# First added: 2009-10-10 |
| 24 |
# Last changed: 2012-11-12 |
| 25 |
|
| 26 |
from dolfin import * |
| 27 |
import matplotlib.pyplot as plt |
| 28 |
|
| 29 |
|
| 30 |
# Create mesh and define function spaces |
| 31 |
mesh0 = UnitSquareMesh(16, 16) |
| 32 |
mesh1 = UnitSquareMesh(64, 64) |
| 33 |
|
| 34 |
# Create expression on P3 |
| 35 |
u0 = Expression("sin(10.0*x[0])*sin(10.0*x[1])", degree=3) |
| 36 |
|
| 37 |
# Define projection space |
| 38 |
P1 = FunctionSpace(mesh1, "CG", 1) |
| 39 |
|
| 40 |
# Define projection variation problem |
| 41 |
v = TestFunction(P1) |
| 42 |
u1 = TrialFunction(P1) |
| 43 |
a = v*u1*dx |
| 44 |
L = v*u0*dx |
| 45 |
|
| 46 |
# Compute solution |
| 47 |
u1 = Function(P1) |
| 48 |
solve(a == L, u1) |
| 49 |
|
| 50 |
# Plot functions |
| 51 |
plt.figure() |
| 52 |
plot(u0, mesh=mesh0, title="u0") |
| 53 |
plt.figure() |
| 54 |
plot(u1, title="u1") |
| 55 |
plt.show() |