| 1 |
"""Unit tests for the fem interface""" |
| 2 |
|
| 3 |
# Copyright (C) 2016 Chris Richardson |
| 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 |
import pytest |
| 21 |
import numpy as np |
| 22 |
from dolfin import * |
| 23 |
|
| 24 |
from dolfin_utils.test import * |
| 25 |
|
| 26 |
def test_scalar_p1(): |
| 27 |
meshc = UnitCubeMesh(2, 2, 2) |
| 28 |
meshf = UnitCubeMesh(3, 4, 5) |
| 29 |
|
| 30 |
Vc = FunctionSpace(meshc, "CG", 1) |
| 31 |
Vf = FunctionSpace(meshf, "CG", 1) |
| 32 |
|
| 33 |
u = Expression("x[0] + 2*x[1] + 3*x[2]", degree=1) |
| 34 |
uc = interpolate(u, Vc) |
| 35 |
uf = interpolate(u, Vf) |
| 36 |
|
| 37 |
mat = PETScDMCollection.create_transfer_matrix(Vc, Vf) |
| 38 |
Vuc = Function(Vf) |
| 39 |
mat.mult(uc.vector(), Vuc.vector()) |
| 40 |
as_backend_type(Vuc.vector()).update_ghost_values() |
| 41 |
|
| 42 |
diff = Function(Vf) |
| 43 |
diff.assign(Vuc - uf) |
| 44 |
assert diff.vector().norm("l2") < 1.0e-12 |
| 45 |
|
| 46 |
def test_scalar_p1_scaled_mesh(): |
| 47 |
# Make coarse mesh smaller than fine mesh |
| 48 |
meshc = UnitCubeMesh(2, 2, 2) |
| 49 |
for x in meshc.coordinates(): |
| 50 |
x *= 0.9 |
| 51 |
meshf = UnitCubeMesh(3, 4, 5) |
| 52 |
|
| 53 |
Vc = FunctionSpace(meshc, "CG", 1) |
| 54 |
Vf = FunctionSpace(meshf, "CG", 1) |
| 55 |
|
| 56 |
u = Expression("x[0] + 2*x[1] + 3*x[2]", degree=1) |
| 57 |
uc = interpolate(u, Vc) |
| 58 |
uf = interpolate(u, Vf) |
| 59 |
|
| 60 |
mat = PETScDMCollection.create_transfer_matrix(Vc, Vf) |
| 61 |
Vuc = Function(Vf) |
| 62 |
mat.mult(uc.vector(), Vuc.vector()) |
| 63 |
as_backend_type(Vuc.vector()).update_ghost_values() |
| 64 |
|
| 65 |
diff = Function(Vf) |
| 66 |
diff.assign(Vuc - uf) |
| 67 |
print(diff.vector().norm("l2")) |
| 68 |
assert diff.vector().norm("l2") < 1.0e-12 |
| 69 |
|
| 70 |
# Now make coarse mesh larger than fine mesh |
| 71 |
for x in meshc.coordinates(): |
| 72 |
x *= 1.5 |
| 73 |
uc = interpolate(u, Vc) |
| 74 |
|
| 75 |
mat = PETScDMCollection.create_transfer_matrix(Vc, Vf) |
| 76 |
mat.mult(uc.vector(), Vuc.vector()) |
| 77 |
as_backend_type(Vuc.vector()).update_ghost_values() |
| 78 |
|
| 79 |
diff.assign(Vuc - uf) |
| 80 |
assert diff.vector().norm("l2") < 1.0e-12 |
| 81 |
|
| 82 |
def test_scalar_p2(): |
| 83 |
meshc = UnitCubeMesh(2, 2, 2) |
| 84 |
meshf = UnitCubeMesh(3, 4, 5) |
| 85 |
|
| 86 |
Vc = FunctionSpace(meshc, "CG", 2) |
| 87 |
Vf = FunctionSpace(meshf, "CG", 2) |
| 88 |
|
| 89 |
u = Expression("x[0]*x[2] + 2*x[1]*x[0] + 3*x[2]", degree=2) |
| 90 |
uc = interpolate(u, Vc) |
| 91 |
uf = interpolate(u, Vf) |
| 92 |
|
| 93 |
mat = PETScDMCollection.create_transfer_matrix(Vc, Vf) |
| 94 |
Vuc = Function(Vf) |
| 95 |
mat.mult(uc.vector(), Vuc.vector()) |
| 96 |
as_backend_type(Vuc.vector()).update_ghost_values() |
| 97 |
|
| 98 |
diff = Function(Vf) |
| 99 |
diff.assign(Vuc - uf) |
| 100 |
assert diff.vector().norm("l2") < 1.0e-12 |
| 101 |
|
| 102 |
def test_vector_p1_2d(): |
| 103 |
meshc = UnitSquareMesh(3, 3) |
| 104 |
meshf = UnitSquareMesh(5, 5) |
| 105 |
|
| 106 |
Vc = VectorFunctionSpace(meshc, "CG", 1) |
| 107 |
Vf = VectorFunctionSpace(meshf, "CG", 1) |
| 108 |
|
| 109 |
u = Expression(("x[0] + 2*x[1]", "4*x[0]"), degree=1) |
| 110 |
uc = interpolate(u, Vc) |
| 111 |
uf = interpolate(u, Vf) |
| 112 |
|
| 113 |
mat = PETScDMCollection.create_transfer_matrix(Vc, Vf) |
| 114 |
Vuc = Function(Vf) |
| 115 |
mat.mult(uc.vector(), Vuc.vector()) |
| 116 |
as_backend_type(Vuc.vector()).update_ghost_values() |
| 117 |
|
| 118 |
diff = Function(Vf) |
| 119 |
diff.assign(Vuc - uf) |
| 120 |
assert diff.vector().norm("l2") < 1.0e-12 |
| 121 |
|
| 122 |
def test_vector_p2_2d(): |
| 123 |
meshc = UnitSquareMesh(5, 4) |
| 124 |
meshf = UnitSquareMesh(5, 8) |
| 125 |
|
| 126 |
Vc = VectorFunctionSpace(meshc, "CG", 2) |
| 127 |
Vf = VectorFunctionSpace(meshf, "CG", 2) |
| 128 |
|
| 129 |
u = Expression(("x[0] + 2*x[1]*x[0]", "4*x[0]*x[1]"), degree=2) |
| 130 |
uc = interpolate(u, Vc) |
| 131 |
uf = interpolate(u, Vf) |
| 132 |
|
| 133 |
mat = PETScDMCollection.create_transfer_matrix(Vc, Vf) |
| 134 |
Vuc = Function(Vf) |
| 135 |
mat.mult(uc.vector(), Vuc.vector()) |
| 136 |
as_backend_type(Vuc.vector()).update_ghost_values() |
| 137 |
|
| 138 |
diff = Function(Vf) |
| 139 |
diff.assign(Vuc - uf) |
| 140 |
assert diff.vector().norm("l2") < 1.0e-12 |
| 141 |
|
| 142 |
def test_vector_p1_3d(): |
| 143 |
meshc = UnitCubeMesh(2, 3, 4) |
| 144 |
meshf = UnitCubeMesh(3, 4, 5) |
| 145 |
|
| 146 |
Vc = VectorFunctionSpace(meshc, "CG", 1) |
| 147 |
Vf = VectorFunctionSpace(meshf, "CG", 1) |
| 148 |
|
| 149 |
u = Expression(("x[0] + 2*x[1]", "4*x[0]", "3*x[2] + x[0]"), degree=1) |
| 150 |
uc = interpolate(u, Vc) |
| 151 |
uf = interpolate(u, Vf) |
| 152 |
|
| 153 |
mat = PETScDMCollection.create_transfer_matrix(Vc, Vf) |
| 154 |
Vuc = Function(Vf) |
| 155 |
mat.mult(uc.vector(), Vuc.vector()) |
| 156 |
as_backend_type(Vuc.vector()).update_ghost_values() |
| 157 |
|
| 158 |
diff = Function(Vf) |
| 159 |
diff.assign(Vuc - uf) |
| 160 |
assert diff.vector().norm("l2") < 1.0e-12 |
| 161 |
|
| 162 |
def test_taylor_hood_cube(): |
| 163 |
pytest.xfail("Problem with Mixed Function Spaces") |
| 164 |
meshc = UnitCubeMesh(2, 2, 2) |
| 165 |
meshf = UnitCubeMesh(3, 4, 5) |
| 166 |
|
| 167 |
Ve = VectorElement("CG", meshc.ufl_cell(), 2) |
| 168 |
Qe = FiniteElement("CG", meshc.ufl_cell(), 1) |
| 169 |
Ze = MixedElement([Ve, Qe]) |
| 170 |
|
| 171 |
Zc = FunctionSpace(meshc, Ze) |
| 172 |
Zf = FunctionSpace(meshf, Ze) |
| 173 |
|
| 174 |
z = Expression(("x[0]*x[1]", "x[1]*x[2]", "x[2]*x[0]", "x[0] + 3*x[1] + x[2]"), degree=2) |
| 175 |
zc = interpolate(z, Zc) |
| 176 |
zf = interpolate(z, Zf) |
| 177 |
|
| 178 |
mat = PETScDMCollection.create_transfer_matrix(Zc, Zf) |
| 179 |
Zuc = Function(Zf) |
| 180 |
mat.mult(zc.vector(), Zuc.vector()) |
| 181 |
as_backend_type(Zuc.vector()).update_ghost_values() |
| 182 |
|
| 183 |
diff = Function(Zf) |
| 184 |
diff.assign(Zuc - zf) |
| 185 |
assert diff.vector().norm("l2") < 1.0e-12 |