| 1 |
"""Unit tests for the function library""" |
| 2 |
|
| 3 |
# Copyright (C) 2007 Anders Logg |
| 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: 2007-05-24 |
| 21 |
# Last changed: 2011-01-28 |
| 22 |
|
| 23 |
import pytest |
| 24 |
from numpy import array |
| 25 |
from dolfin import * |
| 26 |
from ufl_legacy import UFLException |
| 27 |
|
| 28 |
def test_name_argument(): |
| 29 |
u = Constant(1.0) |
| 30 |
v = Constant(1.0, name="v") |
| 31 |
assert u.name() == "f_%d" % u.count() |
| 32 |
assert v.name() == "v" |
| 33 |
assert str(v) == "v" |
| 34 |
|
| 35 |
|
| 36 |
def testConstantInit(): |
| 37 |
c0 = Constant(1.) |
| 38 |
c1 = Constant([2, 3], interval) |
| 39 |
c2 = Constant([[2, 3], [3, 4]], triangle) |
| 40 |
c3 = Constant(array([2, 3]), tetrahedron) |
| 41 |
c4 = Constant([[2, 3], [3, 4]], quadrilateral) |
| 42 |
c5 = Constant(array([2, 3]), hexahedron) |
| 43 |
|
| 44 |
# FIXME: |
| 45 |
assert c0.cell() is None |
| 46 |
assert c1.cell() == interval |
| 47 |
assert c2.cell() == triangle |
| 48 |
assert c3.cell() == tetrahedron |
| 49 |
assert c4.cell() == quadrilateral |
| 50 |
assert c5.cell() == hexahedron |
| 51 |
|
| 52 |
assert c0.ufl_shape == () |
| 53 |
assert c1.ufl_shape == (2,) |
| 54 |
assert c2.ufl_shape == (2, 2) |
| 55 |
assert c3.ufl_shape == (2,) |
| 56 |
assert c4.ufl_shape == (2, 2) |
| 57 |
assert c5.ufl_shape == (2,) |
| 58 |
|
| 59 |
|
| 60 |
def testGrad(): |
| 61 |
import ufl_legacy as ufl |
| 62 |
zero = ufl.constantvalue.Zero((2, 3)) |
| 63 |
c0 = Constant(1.) |
| 64 |
c3 = Constant(array([2, 3]), tetrahedron) |
| 65 |
|
| 66 |
def gradient(c): |
| 67 |
return grad(c) |
| 68 |
with pytest.raises(UFLException): |
| 69 |
grad(c0) |
| 70 |
assert zero == gradient(c3) |
| 71 |
|
| 72 |
|
| 73 |
@pytest.mark.parametrize('mesh_factory', [(UnitCubeMesh, (8, 8, 8)), (UnitCubeMesh.create, (8, 8, 8, CellType.Type.hexahedron))]) |
| 74 |
def test_compute_vertex_values(mesh_factory): |
| 75 |
from numpy import zeros, all, array |
| 76 |
|
| 77 |
func, args = mesh_factory |
| 78 |
mesh = func(*args) |
| 79 |
|
| 80 |
e0 = Constant(1) |
| 81 |
e1 = Constant((1, 2, 3)) |
| 82 |
|
| 83 |
# e0_values = zeros(mesh.num_vertices(),dtype='d') |
| 84 |
# e1_values = zeros(mesh.num_vertices()*3,dtype='d') |
| 85 |
|
| 86 |
e0_values = e0.compute_vertex_values(mesh) |
| 87 |
e1_values = e1.compute_vertex_values(mesh) |
| 88 |
|
| 89 |
assert all(e0_values == 1) |
| 90 |
assert all(e1_values[:mesh.num_vertices()] == 1) |
| 91 |
assert all(e1_values[mesh.num_vertices():mesh.num_vertices()*2] == 2) |
| 92 |
assert all(e1_values[mesh.num_vertices()*2:mesh.num_vertices()*3] == 3) |
| 93 |
|
| 94 |
|
| 95 |
def test_values(): |
| 96 |
import numpy as np |
| 97 |
|
| 98 |
c0 = Constant(1.) |
| 99 |
c0_vals = c0.values() |
| 100 |
assert np.all(c0_vals == np.array([1.], dtype=np.double)) |
| 101 |
|
| 102 |
c1 = Constant((1., 2.)) |
| 103 |
c1_vals = c1.values() |
| 104 |
assert np.all(c1_vals == np.array([1., 2.], dtype=np.double)) |
| 105 |
|
| 106 |
c2 = Constant((1., 2., 3.)) |
| 107 |
c2_vals = c2.values() |
| 108 |
assert np.all(c2_vals == np.array([1., 2., 3.], dtype=np.double)) |
| 109 |
|
| 110 |
|
| 111 |
def test_str(): |
| 112 |
c0 = Constant(1.) |
| 113 |
c0.str(False) |
| 114 |
c0.str(True) |
| 115 |
|
| 116 |
c1 = Constant((1., 2., 3.)) |
| 117 |
c1.str(False) |
| 118 |
c1.str(True) |
| 119 |
|
| 120 |
|
| 121 |
def test_assign(): |
| 122 |
c0 = Constant(1.) |
| 123 |
assert c0.values() == (1,) |
| 124 |
c0.assign(Constant(3)) |
| 125 |
assert c0.values() == (3,) |
| 126 |
|
| 127 |
c1 = Constant([1, 2]) |
| 128 |
assert (c1.values() == (1, 2)).all() |
| 129 |
c1.assign(Constant([3, 4])) |
| 130 |
assert (c1.values() == (3, 4)).all() |