Debian Codegraph @

python/test/unit/function/test_nonmatching_interpolation.py

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