| 1 |
"""Unit tests for dP assembly""" |
| 2 |
|
| 3 |
# Copyright (C) 2014 Johan Hake |
| 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 |
|
| 21 |
import pytest |
| 22 |
import os |
| 23 |
import numpy as np |
| 24 |
from dolfin import * |
| 25 |
|
| 26 |
from dolfin_utils.test import * |
| 27 |
|
| 28 |
|
| 29 |
@pytest.fixture(params=[1, 2, 3]) |
| 30 |
def dim(request): |
| 31 |
return request.param |
| 32 |
|
| 33 |
|
| 34 |
def _create_dp_problem(dim): |
| 35 |
assert dim in [1, 2, 3] |
| 36 |
if dim == 1: |
| 37 |
mesh = UnitIntervalMesh(20) |
| 38 |
elif dim == 2: |
| 39 |
mesh = UnitSquareMesh(10, 10) |
| 40 |
else: |
| 41 |
mesh = UnitCubeMesh(4, 4, 4) |
| 42 |
|
| 43 |
P1 = FiniteElement("P", mesh.ufl_cell(), 1) |
| 44 |
V = FunctionSpace(mesh, P1) |
| 45 |
VV = FunctionSpace(mesh, P1*P1) |
| 46 |
|
| 47 |
# Create expression used to interpolate initial data |
| 48 |
y_dim = 1 if dim > 1 else 0 |
| 49 |
z_dim = 2 if dim > 2 else (1 if dim > 1 else 0) |
| 50 |
e = Expression("x[0] + 2*x[{}] + x[{}]".format(y_dim, z_dim), degree=2) |
| 51 |
ee = Expression(["x[0]+x[{}]".format(z_dim), "x[0]*x[{}]+x[{}]".format(y_dim, z_dim)], degree=2) |
| 52 |
|
| 53 |
# Create coefficients |
| 54 |
u = interpolate(e, V) |
| 55 |
uu = interpolate(ee, VV) |
| 56 |
|
| 57 |
# Test and trial spaces |
| 58 |
v = TestFunction(V) |
| 59 |
vv = TestFunction(VV) |
| 60 |
U = TrialFunction(V) |
| 61 |
UU = TrialFunction(VV) |
| 62 |
|
| 63 |
# Subdomains |
| 64 |
subdomain = AutoSubDomain(lambda x, on_boundary: x[0] <= 0.5) |
| 65 |
disjoint_subdomain = AutoSubDomain(lambda x, on_boundary: x[0] > 0.5) |
| 66 |
vertex_domain = MeshFunction("size_t", mesh, 0, 0) |
| 67 |
subdomain.mark(vertex_domain, 1) |
| 68 |
bc = DirichletBC(VV, Constant((0, 0)), disjoint_subdomain) |
| 69 |
dPP = dP(subdomain_data=vertex_domain) |
| 70 |
|
| 71 |
return (u, uu), (v, vv), (U, UU), dPP, bc |
| 72 |
|
| 73 |
|
| 74 |
def test_scalar_assemble(dim): |
| 75 |
eps = 1000*DOLFIN_EPS |
| 76 |
|
| 77 |
(u, uu), (v, vv), (U, UU), dPP, bc = _create_dp_problem(dim) |
| 78 |
|
| 79 |
scalar_value = assemble(u*dP) |
| 80 |
assert abs(scalar_value-u.vector().sum()) < eps |
| 81 |
|
| 82 |
scalar_value = assemble((uu[0]+uu[1])*dPP) |
| 83 |
assert abs(scalar_value-uu.vector().sum()) < eps |
| 84 |
|
| 85 |
scalar_value = assemble((uu[0]+uu[1])*dPP(1)) |
| 86 |
bc.apply(uu.vector()) |
| 87 |
assert abs(scalar_value-uu.vector().sum()) < eps |
| 88 |
|
| 89 |
|
| 90 |
def test_vector_assemble(dim): |
| 91 |
eps = 1000*DOLFIN_EPS |
| 92 |
|
| 93 |
(u, uu), (v, vv), (U, UU), dPP, bc = _create_dp_problem(dim) |
| 94 |
|
| 95 |
# In parallel vec.get_local() will return only local to process values |
| 96 |
vec = assemble(u*v*dPP) |
| 97 |
assert sum(np.absolute(vec.get_local() - u.vector().get_local())) < eps |
| 98 |
|
| 99 |
vec = assemble(inner(uu, vv)*dP) |
| 100 |
assert sum(np.absolute(vec.get_local() - uu.vector().get_local())) < eps |
| 101 |
|
| 102 |
vec = assemble(inner(uu, vv)*dPP(1)) |
| 103 |
bc.apply(uu.vector()) |
| 104 |
assert sum(np.absolute(vec.get_local() - uu.vector().get_local())) < eps |
| 105 |
|
| 106 |
|
| 107 |
def test_matrix_assemble(dim): |
| 108 |
eps = 1000*DOLFIN_EPS |
| 109 |
|
| 110 |
(u, uu), (v, vv), (U, UU), dPP, bc = _create_dp_problem(dim) |
| 111 |
|
| 112 |
# Scalar assemble |
| 113 |
mat = assemble(u*v*U*dPP) |
| 114 |
|
| 115 |
# Create a numpy matrix based on the local size of the vector |
| 116 |
# and populate it with values from local vector |
| 117 |
loc_range = u.vector().local_range() |
| 118 |
vec_mat = np.zeros_like(mat.array()) |
| 119 |
vec_mat[range(loc_range[1] - loc_range[0]), |
| 120 |
range(loc_range[0], loc_range[1])] = u.vector().get_local() |
| 121 |
|
| 122 |
assert np.sum(np.absolute(mat.array() - vec_mat)) < eps |
| 123 |
|
| 124 |
# Vector assemble |
| 125 |
mat = assemble((uu[0]*vv[0]*UU[0] + uu[1]*vv[1]*UU[1])*dPP) |
| 126 |
|
| 127 |
# Create a numpy matrix based on the local size of the vector |
| 128 |
# and populate it with values from local vector |
| 129 |
loc_range = uu.vector().local_range() |
| 130 |
vec_mat = np.zeros_like(mat.array()) |
| 131 |
vec_mat[range(loc_range[1] - loc_range[0]), |
| 132 |
range(loc_range[0], loc_range[1])] = uu.vector().get_local() |
| 133 |
|
| 134 |
assert np.sum(np.absolute(mat.array() - vec_mat)) < eps |