Debian Codegraph @

test/uflacs/unit/xtest_ufl_shapes_and_indexing.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """Tests of utilities for dealing with ufl indexing and components vs
3 flattened index spaces.
4
5 """
6
7 from ufl_legacy import *
8 from ufl_legacy import product
9 from ufl_legacy.permutation import compute_indices
10
11 from ffc.uflacs.analysis.indexing import (map_indexed_arg_components,
12 map_component_tensor_arg_components)
13 from ffc.uflacs.analysis.graph_symbols import (map_list_tensor_symbols,
14 map_transposed_symbols,
15 get_node_symbols)
16 from ffc.uflacs.analysis.graph import build_graph
17
18 from operator import eq as equal
19
20
21 def test_map_indexed_arg_components():
22 W = TensorElement("CG", triangle, 1)
23 A = Coefficient(W)
24 i, j = indices(2)
25
26 # Ordered indices:
27 d = map_indexed_arg_components(A[i, j])
28 assert equal(d, [0, 1, 2, 3])
29
30 # Swapped ordering of indices:
31 d = map_indexed_arg_components(A[j, i])
32 assert equal(d, [0, 2, 1, 3])
33
34
35 def test_map_indexed_arg_components2():
36
37 # This was the previous return type, copied here to preserve the
38 # test without having to rewrite
39 def map_indexed_arg_components2(Aii):
40 c1, c2 = map_indexed_to_arg_components(Aii)
41 d = [None] * len(c1)
42 for k in range(len(c1)):
43 d[c1[k]] = k
44 return d
45
46 W = TensorElement("CG", triangle, 1)
47 A = Coefficient(W)
48 i, j = indices(2)
49
50 # Ordered indices:
51 d = map_indexed_arg_components2(A[i, j])
52 assert equal(d, [0, 1, 2, 3])
53
54 # Swapped ordering of indices:
55 d = map_indexed_arg_components2(A[j, i])
56 assert equal(d, [0, 2, 1, 3])
57
58
59 def test_map_componenttensor_arg_components():
60 W = TensorElement("CG", triangle, 1)
61 A = Coefficient(W)
62 i, j = indices(2)
63
64 # Ordered indices:
65 d = map_component_tensor_arg_components(as_tensor(2 * A[i, j], (i, j)))
66 assert equal(d, [0, 1, 2, 3])
67
68 # Swapped ordering of indices:
69 d = map_component_tensor_arg_components(as_tensor(2 * A[i, j], (j, i)))
70 assert equal(d, [0, 2, 1, 3])
71
72
73 def test_map_list_tensor_symbols():
74 U = FiniteElement("CG", triangle, 1)
75 u = Coefficient(U)
76 A = as_tensor(((u + 1, u + 2, u + 3), (u**2 + 1, u**2 + 2, u**2 + 3)))
77 # Would be nicer to refactor build_graph a bit so we could call
78 # map_list_tensor_symbols directly...
79 G = build_graph([A], DEBUG=False)
80 s1 = list(get_node_symbols(A, G.e2i, G.V_symbols))
81 s2 = [get_node_symbols(e, G.e2i, G.V_symbols)[0] for e in (u + 1, u + 2, u + 3, u**2 + 1, u**2 + 2, u**2 + 3)]
82 assert s1 == s2
83
84
85 def test_map_transposed_symbols():
86 W = TensorElement("CG", triangle, 1)
87 w = Coefficient(W)
88 A = w.T
89 # Would be nicer to refactor build_graph a bit so we could call
90 # map_transposed_symbols directly...
91 G = build_graph([A], DEBUG=False)
92 s1 = list(get_node_symbols(A, G.e2i, G.V_symbols))
93 s2 = list(get_node_symbols(w, G.e2i, G.V_symbols))
94 s2[1], s2[2] = s2[2], s2[1]
95 assert s1 == s2
96
97 W = TensorElement("CG", tetrahedron, 1)
98 w = Coefficient(W)
99 A = w.T
100 # Would be nicer to refactor build_graph a bit so we could call
101 # map_transposed_symbols directly...
102 G = build_graph([A], DEBUG=False)
103 s1 = list(get_node_symbols(A, G.e2i, G.V_symbols))
104 s2 = list(get_node_symbols(w, G.e2i, G.V_symbols))
105 s2[1], s2[2], s2[5], s2[3], s2[6], s2[7] = s2[3], s2[6], s2[7], s2[1], s2[2], s2[5]
106 assert s1 == s2