Debian Codegraph @

test/uflacs/unit/test_factorization.py

raw
1 # -*- coding: utf-8 -*-
2 """
3 Tests of algorithm for factorization of integrand w.r.t. Argument terms.
4 """
5
6 from ufl_legacy import *
7 from ffc.uflacs.analysis.factorization import compute_argument_factorization
8
9 # TODO: Restructure these tests using py.test fixtures and parameterization?
10
11
12 def compare_compute_argument_factorization(SV, SV_deps, expected_AV, expected_FV, expected_IM):
13 SV_targets = [len(SV) - 1]
14 rank = max(len(k) for k in expected_IM.keys())
15 argument_factorizations, modified_arguments, FV, FV_deps, FV_targets = \
16 compute_argument_factorization(SV, SV_deps, SV_targets, rank)
17 argument_factorization, = argument_factorizations
18
19 AV = modified_arguments
20 IM = argument_factorization
21
22 assert AV == expected_AV
23 if 0:
24 for n in range(1, min(len(FV), len(expected_FV))):
25 assert FV[:n] == expected_FV[:n]
26 assert FV == expected_FV
27 assert IM == expected_IM
28
29
30 def test_compute_argument_factorization():
31 V = FiniteElement("CG", triangle, 1)
32 u = TrialFunction(V)
33 v = TestFunction(V)
34 a, b, c, d, e, f, g = [Coefficient(V, count=k) for k in range(7)]
35
36 zero = as_ufl(0.0)
37 one = as_ufl(1.0)
38 two = as_ufl(2)
39
40 # Test workaround for hack in factorization:
41 FVpre = [zero, one, two]
42 offset = len(FVpre)
43
44 # Test basic non-argument terminal
45 SV = [f]
46 SV_deps = [()]
47 AV = []
48 FV = FVpre + [f]
49 IM = {(): 0 + offset}
50 compare_compute_argument_factorization(SV, SV_deps, AV, FV, IM)
51
52 # Test basic non-argument sum
53 SV = [f, g, f + g]
54 SV_deps = [(), (), (0, 1)]
55 AV = []
56 FV = FVpre + [f, g, f + g]
57 IM = {(): 2 + offset}
58 compare_compute_argument_factorization(SV, SV_deps, AV, FV, IM)
59
60 # Test basic non-argument product
61 SV = [f, g, f * g]
62 SV_deps = [(), (), (0, 1)]
63 AV = []
64 FV = FVpre + [f, g, f * g]
65 IM = {(): 2 + offset}
66 compare_compute_argument_factorization(SV, SV_deps, AV, FV, IM)
67
68 # Test basic single-argument-only expression
69 SV = [v]
70 SV_deps = [()]
71 AV = [v]
72 FV = FVpre + []
73 IM = {(0,): 1} # v == AV[0] * FV[1]
74 compare_compute_argument_factorization(SV, SV_deps, AV, FV, IM)
75
76 # Test basic coefficient-argument product
77 SV = [f, v, f * v]
78 SV_deps = [(), (), (0, 1)]
79 AV = [v]
80 FV = FVpre + [f]
81 IM = {(0,): offset} # f*v == AV[0] * FV[1]
82 compare_compute_argument_factorization(SV, SV_deps, AV, FV, IM)
83
84 # Test basic argument product
85 SV = [u, v, u * v]
86 SV_deps = [(), (), (0, 1)]
87 AV = [v, u] # Test function < trial function
88 FV = FVpre + []
89 IM = {(0, 1): 1} # v*u == (AV[0] * AV[1]) * FV[1]
90 compare_compute_argument_factorization(SV, SV_deps, AV, FV, IM)
91
92 # Test coefficient-argument products
93 SV = [u, f, v, (f * v), u * (f * v)]
94 SV_deps = [(), (), (), (1, 2), (0, 3)]
95 AV = [v, u]
96 FV = FVpre + [f]
97 IM = {(0, 1): 0 + offset} # f*(u*v) == (AV[0] * AV[1]) * FV[2]
98 compare_compute_argument_factorization(SV, SV_deps, AV, FV, IM)
99
100 # Test more complex situation
101 SV = [u, u.dx(0), v, # 0..2
102 a, b, c, d, e, # 3..7
103 a * u, b * u.dx(0), # 8..9
104 c * v, d * v, # 10..11
105 a * u + b * u.dx(0), # 12
106 c * v + d * v, # 13
107 e * (a * u + b * u.dx(0)), # 14
108 (e * (a * u + b * u.dx(0))) * (c * v + d * v), # 15
109 ]
110 SV_deps = [(), (), (),
111 (), (), (), (), (),
112 (0, 3), (1, 4),
113 (2, 5), (2, 6),
114 (8, 9),
115 (10, 11),
116 (7, 12),
117 (13, 14),
118 ]
119 AV = [v, u, u.dx(0)]
120 FV = FVpre + [a, b, c, d, e, # 0..5
121 c + d, # 6, introduced by SV[13]
122 e * a, # 7, introduced by SV[14]
123 e * b, # 8, introduced by SV[14]
124 (e * a) * (c + d), # 9
125 (e * b) * (c + d), # 10
126 ]
127 IM = {(0, 1): 8 + offset, # (a*e)*(c+d)*(u*v) == (AV[0] * AV[2]) * FV[13]
128 (0, 2): 9 + offset} # (b*e)*(c+d)*(u.dx(0)*v) == (AV[1] * AV[2]) * FV[12]
129 compare_compute_argument_factorization(SV, SV_deps, AV, FV, IM)