| 1 |
""" |
| 2 |
This program demonstrates interpolation onto a mesh which is not |
| 3 |
completely covered by the original mesh. This situation may arise |
| 4 |
during mesh refinement as a result of round-off errors. |
| 5 |
|
| 6 |
When interpolating, DOLFIN tries to locate each point in the original |
| 7 |
mesh. If that fails, the closest cell is found and the values |
| 8 |
extrapolated from there. |
| 9 |
""" |
| 10 |
|
| 11 |
# Copyright (C) 2010 Anders Logg |
| 12 |
# |
| 13 |
# This file is part of DOLFIN. |
| 14 |
# |
| 15 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 16 |
# it under the terms of the GNU Lesser General Public License as published by |
| 17 |
# the Free Software Foundation, either version 3 of the License, or |
| 18 |
# (at your option) any later version. |
| 19 |
# |
| 20 |
# DOLFIN is distributed in the hope that it will be useful, |
| 21 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 |
# GNU Lesser General Public License for more details. |
| 24 |
# |
| 25 |
# You should have received a copy of the GNU Lesser General Public License |
| 26 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 27 |
|
| 28 |
from dolfin import * |
| 29 |
import matplotlib.pyplot as plt |
| 30 |
|
| 31 |
|
| 32 |
# Set option to allow extrapolation |
| 33 |
parameters["allow_extrapolation"] = True |
| 34 |
|
| 35 |
# Create mesh and function space |
| 36 |
mesh = UnitSquareMesh(16, 16) |
| 37 |
V = FunctionSpace(mesh, "CG", 1) |
| 38 |
|
| 39 |
# Create a function on the original mesh |
| 40 |
f = Expression("sin(5.0*x[0])*sin(5.0*x[1])", degree=2) |
| 41 |
v = interpolate(f, V) |
| 42 |
|
| 43 |
# Refine mesh and create a new function space |
| 44 |
refined_mesh = refine(mesh) |
| 45 |
W = FunctionSpace(refined_mesh, "CG", 1) |
| 46 |
|
| 47 |
# Displace mesh slightly |
| 48 |
x = mesh.coordinates() |
| 49 |
x += 0.1 |
| 50 |
|
| 51 |
# Interpolate to the new mesh |
| 52 |
w = interpolate(v, W) |
| 53 |
|
| 54 |
# Plot function |
| 55 |
plot(w) |
| 56 |
plt.show() |