Debian Codegraph @

python/test/unit/fem/test_formsplitter.py

raw
1 #!/usr/bin/env py.test
2
3 """Unit tests for the extract_blocks function (based on FormSplitter ufl class)"""
4
5 # Copyright (C) 2011 Johan Hake
6 #
7 # This file is part of DOLFIN.
8 #
9 # DOLFIN is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU Lesser General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # DOLFIN is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public License
20 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
21 #
22 # Modified by Cecile Daversin-Catty 2017
23 #
24 # First added: 2017-07-25
25 # Last changed: 2018-04-10
26
27 import pytest
28 from dolfin import *
29 from ufl_legacy.log import UFLException
30
31 from dolfin_utils.test import skip_in_parallel, fixture
32 from dolfin.fem.assembling import _create_dolfin_form
33
34 @fixture
35 def marker():
36 cube = UnitCubeMesh(8, 8, 8)
37 marker = MeshFunction("size_t", cube, cube.topology().dim(), 0)
38 for c in cells(cube):
39 marker[c] = c.midpoint().x() < 0.5
40 return marker
41
42 @fixture
43 def mesh1(marker):
44 return MeshView.create(marker, 1)
45
46 @fixture
47 def mesh2(marker):
48 return MeshView.create(marker, 0)
49
50 @fixture
51 def space(mesh1,mesh2):
52 W1 = FunctionSpace(mesh1, "Lagrange", 1)
53 W2 = FunctionSpace(mesh2, "Lagrange", 2)
54 return MixedFunctionSpace( W1, W2 )
55
56 @fixture
57 def u(space):
58 return TrialFunctions(space)
59
60 @fixture
61 def v(space):
62 return TestFunctions(space)
63
64 @fixture
65 def a(u,v):
66 return inner(grad(u[0]), grad(v[0]))*dx + inner(grad(u[1]), grad(v[1]))*dx
67
68 @fixture
69 def f():
70 return Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=2)
71
72 @fixture
73 def L(f,v):
74 return f*v[0]*dx + f*v[1]*dx
75
76 @fixture
77 def a_blocks(a):
78 return extract_blocks(a)
79
80 @fixture
81 def L_blocks(L):
82 return extract_blocks(L)
83
84 @skip_in_parallel
85 def test_size(space, a_blocks, L_blocks):
86 assert len(L_blocks) == space.num_sub_spaces()
87 assert len(a_blocks) == len(L_blocks)*len(L_blocks)
88
89 @skip_in_parallel
90 def test_indices(space, a, L, a_blocks, L_blocks):
91 for i in range(space.num_sub_spaces()):
92 assert extract_blocks(L,i) == L_blocks[i]
93 for j in range(space.num_sub_spaces()):
94 assert extract_blocks(a,i,j) == a_blocks[i*space.num_sub_spaces()+j]
95
96 @skip_in_parallel
97 def test_spaces(space, a_blocks, L_blocks):
98 for i in range(space.num_sub_spaces()):
99 L_form = _create_dolfin_form(L_blocks[i])
100 a_form = _create_dolfin_form(a_blocks[i*space.num_sub_spaces() + i])
101 assert a_form.function_spaces[0] == L_form.function_spaces[0]
102
103
104
105
106