Debian Codegraph @

python/test/unit/fem/test_discrete_operators.py

raw
1 """Unit tests for the DiscreteOperator class"""
2
3 # Copyright (C) 2015 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 import pytest
21 import numpy as np
22 from dolfin import *
23
24 from dolfin_utils.test import *
25
26
27 def test_gradient():
28 """Test discrete gradient computation (typically used for curl-curl
29 AMG preconditioners"""
30
31 def compute_discrete_gradient(mesh):
32 V = FunctionSpace(mesh, "Lagrange", 1)
33 W = FunctionSpace(mesh, "Nedelec 1st kind H(curl)", 1)
34
35 G = DiscreteOperators.build_gradient(W, V)
36 num_edges = mesh.num_entities_global(1)
37 assert G.size(0) == num_edges
38 assert G.size(1) == mesh.num_entities_global(0)
39 assert round(G.norm("frobenius") - sqrt(2.0*num_edges), 8) == 0.0
40
41 meshes = [UnitSquareMesh(11, 6), UnitCubeMesh(4, 3, 7)]
42 for mesh in meshes:
43 compute_discrete_gradient(mesh)
44
45
46 def test_incompatible_spaces():
47 "Test that error is thrown when function spaces are not compatible"
48
49 mesh = UnitSquareMesh(13, 7)
50 V = FunctionSpace(mesh, "Lagrange", 1)
51 W = FunctionSpace(mesh, "Nedelec 1st kind H(curl)", 1)
52 with pytest.raises(RuntimeError):
53 G = DiscreteOperators.build_gradient(V, W)
54 with pytest.raises(RuntimeError):
55 G = DiscreteOperators.build_gradient(V, V)
56 with pytest.raises(RuntimeError):
57 G = DiscreteOperators.build_gradient(W, W)
58
59 V = FunctionSpace(mesh, "Lagrange", 2)
60 with pytest.raises(RuntimeError):
61 G = DiscreteOperators.build_gradient(W, V)