| 1 |
"""Unit tests for interpolation using LagrangeInterpolator""" |
| 2 |
|
| 3 |
# Copyright (C) 2014 Mikael Mortensen |
| 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: 2014-02-18 |
| 21 |
# Last changed: |
| 22 |
|
| 23 |
import pytest |
| 24 |
import numpy |
| 25 |
from dolfin import * |
| 26 |
|
| 27 |
|
| 28 |
class Quadratic2D(UserExpression): |
| 29 |
def eval(self, values, x): |
| 30 |
values[0] = x[0]*x[0] + x[1]*x[1] + 1.0 |
| 31 |
|
| 32 |
|
| 33 |
class Quadratic3D(UserExpression): |
| 34 |
def eval(self, values, x): |
| 35 |
values[0] = x[0]*x[0] + x[1]*x[1] + x[2]*x[2] + 1.0 |
| 36 |
|
| 37 |
|
| 38 |
def test_functional2D(): |
| 39 |
"""Test integration of function interpolated in non-matching meshes""" |
| 40 |
|
| 41 |
f = Quadratic2D(degree=2) |
| 42 |
|
| 43 |
# Interpolate quadratic function on course mesh |
| 44 |
mesh0 = UnitSquareMesh(8, 8) |
| 45 |
V0 = FunctionSpace(mesh0, "Lagrange", 2) |
| 46 |
u0 = Function(V0) |
| 47 |
LagrangeInterpolator.interpolate(u0, f) |
| 48 |
|
| 49 |
# Interpolate FE function on finer mesh |
| 50 |
mesh1 = UnitSquareMesh(31, 31) |
| 51 |
V1 = FunctionSpace(mesh1, "Lagrange", 2) |
| 52 |
u1 = Function(V1) |
| 53 |
|
| 54 |
LagrangeInterpolator.interpolate(u1, u0) |
| 55 |
assert round(assemble(u0*dx) - assemble(u1*dx), 10) == 0 |
| 56 |
|
| 57 |
mesh1 = UnitSquareMesh(30, 30) |
| 58 |
V1 = FunctionSpace(mesh1, "Lagrange", 2) |
| 59 |
u1 = Function(V1) |
| 60 |
LagrangeInterpolator.interpolate(u1, u0) |
| 61 |
assert round(assemble(u0*dx) - assemble(u1*dx), 10) == 0 |
| 62 |
|
| 63 |
|
| 64 |
def test_functional3D(): |
| 65 |
"""Test integration of function interpolated in non-matching meshes""" |
| 66 |
|
| 67 |
f = Quadratic3D(degree=2) |
| 68 |
|
| 69 |
# Interpolate quadratic function on course mesh |
| 70 |
mesh0 = UnitCubeMesh(4, 4, 4) |
| 71 |
V0 = FunctionSpace(mesh0, "Lagrange", 2) |
| 72 |
u0 = Function(V0) |
| 73 |
LagrangeInterpolator.interpolate(u0, f) |
| 74 |
|
| 75 |
# Interpolate FE function on finer mesh |
| 76 |
mesh1 = UnitCubeMesh(11, 11, 11) |
| 77 |
V1 = FunctionSpace(mesh1, "Lagrange", 2) |
| 78 |
u1 = Function(V1) |
| 79 |
LagrangeInterpolator.interpolate(u1, u0) |
| 80 |
assert round(assemble(u0*dx) - assemble(u1*dx), 10) == 0 |
| 81 |
|
| 82 |
mesh1 = UnitCubeMesh(10, 11, 10) |
| 83 |
V1 = FunctionSpace(mesh1, "Lagrange", 2) |
| 84 |
u1 = Function(V1) |
| 85 |
LagrangeInterpolator.interpolate(u1, u0) |
| 86 |
assert round(assemble(u0*dx) - assemble(u1*dx), 10) == 0 |