Debian Codegraph @

python/test/unit/la/test_nullspace.py

raw
1 "Unit tests for nullspace test"
2
3 # Copyright (C) 2014 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 from dolfin import *
21 import pytest
22 from dolfin_utils.test import *
23
24 backends = ["PETSc", pytest.param(("Eigen"), marks=skip_in_parallel)]
25
26 def build_elastic_nullspace(V, x):
27 """Function to build nullspace for 2D/3D elasticity"""
28
29 # Get geometric dim
30 gdim = V.mesh().geometry().dim()
31 assert gdim == 2 or gdim == 3
32
33 # Set dimension of nullspace
34 dim = 3 if gdim == 2 else 6
35
36 # Create list of vectors for null space
37 nullspace_basis = [x.copy() for i in range(dim)]
38
39 # Build translational null space basis
40 for i in range(gdim):
41 V.sub(i).dofmap().set(nullspace_basis[i], 1.0);
42
43 # Build rotational null space basis
44 if gdim == 2:
45 V.sub(0).set_x(nullspace_basis[2], -1.0, 1);
46 V.sub(1).set_x(nullspace_basis[2], 1.0, 0);
47 elif gdim == 3:
48 V.sub(0).set_x(nullspace_basis[3], -1.0, 1);
49 V.sub(1).set_x(nullspace_basis[3], 1.0, 0);
50
51 V.sub(0).set_x(nullspace_basis[4], 1.0, 2);
52 V.sub(2).set_x(nullspace_basis[4], -1.0, 0);
53
54 V.sub(2).set_x(nullspace_basis[5], 1.0, 1);
55 V.sub(1).set_x(nullspace_basis[5], -1.0, 2);
56
57 for x in nullspace_basis:
58 x.apply("insert")
59
60 return VectorSpaceBasis(nullspace_basis)
61
62
63 def build_broken_elastic_nullspace(V, x):
64 """Function to build incorrect null space for 2D elasticity"""
65
66 # Create list of vectors for null space
67 nullspace_basis = [x.copy() for i in range(4)]
68
69 # Build translational null space basis
70 V.sub(0).dofmap().set(nullspace_basis[0], 1.0);
71 V.sub(1).dofmap().set(nullspace_basis[1], 1.0);
72
73 # Build rotational null space basis
74 V.sub(0).set_x(nullspace_basis[2], -1.0, 1);
75 V.sub(1).set_x(nullspace_basis[2], 1.0, 0);
76
77 # Add vector that is not in nullspace
78 V.sub(1).set_x(nullspace_basis[3], 1.0, 1);
79
80 for x in nullspace_basis:
81 x.apply("insert")
82 return VectorSpaceBasis(nullspace_basis)
83
84
85 def test_nullspace_orthogonal():
86 """Test that null spaces orthogonalisation"""
87 meshes = [UnitSquareMesh(12, 12), UnitCubeMesh(4, 4, 4)]
88 for mesh in meshes:
89 for p in range(1, 4):
90 V = VectorFunctionSpace(mesh, 'CG', p)
91 zero = Constant([0.0]*mesh.geometry().dim())
92 L = dot(TestFunction(V), zero)*dx
93 x = assemble(L)
94
95 # Build nullspace
96 null_space = build_elastic_nullspace(V, x)
97
98 assert not null_space.is_orthogonal()
99 assert not null_space.is_orthonormal()
100
101 # Orthogonalise nullspace
102 null_space.orthonormalize()
103
104 # Checl that null space basis is orthonormal
105 assert null_space.is_orthogonal()
106 assert null_space.is_orthonormal()
107
108
109 @pytest.mark.parametrize('backend', backends)
110 def test_nullspace_check(backend):
111 # Check whether backend is available
112 if not has_linear_algebra_backend(backend):
113 pytest.skip('Need %s as backend to run this test' % backend)
114
115 # Set linear algebra backend
116 prev_backend = parameters["linear_algebra_backend"]
117 parameters["linear_algebra_backend"] = backend
118
119 # Mesh
120 mesh = UnitSquareMesh(12, 12)
121
122 # Elasticity form
123 V = VectorFunctionSpace(mesh, 'CG', 1)
124 u, v = TrialFunction(V), TestFunction(V)
125 a = inner(sym(grad(u)), grad(v))*dx
126
127 # Assemble matrix and create compatible vector
128 A = assemble(a)
129 x = Vector()
130 A.init_vector(x, 1)
131
132 # Create null space basis and test
133 null_space = build_elastic_nullspace(V, x)
134 assert in_nullspace(A, null_space)
135 assert in_nullspace(A, null_space, "right")
136 assert in_nullspace(A, null_space, "left")
137
138 # Create incorect null space basis and test
139 null_space = build_broken_elastic_nullspace(V, x)
140 assert not in_nullspace(A, null_space)
141 assert not in_nullspace(A, null_space, "right")
142 assert not in_nullspace(A, null_space, "left")
143
144 # Reset backend
145 parameters["linear_algebra_backend"] = prev_backend