Debian Codegraph @

test/uflacs/unit/xtest_ufl_to_cpp_formatting.py

raw
1 # -*- coding: utf-8 -*-
2 """
3 Tests of ufl to C++ expression formatting rules.
4
5 These rules can format a subset of the UFL expression language directly to C++.
6 The subset is currently not clearly defined, but it's basically only scalar operations
7 and the set of operators in UFL that coincide with C operators and cmath functions.
8 """
9
10 import ufl_legacy as ufl
11 from ufl_legacy.constantvalue import as_ufl
12 from ufl_legacy.algorithms import preprocess_expression, expand_indices
13
14 from ffc.uflacs.codeutils.cpp_expr_formatting_rules import CppFormattingRules
15 from ffc.uflacs.codeutils.expr_formatter import ExprFormatter
16
17
18 def expr2cpp(expr, variables=None):
19 "This is a test specific function for formatting ufl to C++."
20
21 # Preprocessing expression before applying formatting.
22 # In a compiler, one should probably assume that these
23 # have been applied and use ExprFormatter directly.
24 expr_data = preprocess_expression(expr)
25
26 # Applying expand_indices instead of going through
27 # compiler algorithm to get scalar valued expressions
28 # that can be directlly formatted into C++
29 expr = expand_indices(expr_data.preprocessed_expr)
30
31 # This formatter is a multifunction implementing target
32 # specific C++ formatting rules
33 cpp_formatter = ToyCppLanguageFormatter(dh, ir)
34
35 # This final formatter implements a generic framework handling indices etc etc.
36 expr_formatter = ExprFormatter(cpp_formatter, variables or {})
37
38 # Finally perform the formatting
39 code = expr_formatter.visit(expr)
40 return code
41
42
43 def test_cpp_formatting_of_literals():
44 # Test literals
45 assert expr2cpp(ufl.as_ufl(2)) == "2"
46 assert expr2cpp(ufl.as_ufl(3.14)) == '3.14'
47 assert expr2cpp(ufl.as_ufl(0)) == "0"
48 # These are actually converted to int before formatting:
49 assert expr2cpp(ufl.Identity(2)[0, 0]) == "1"
50 assert expr2cpp(ufl.Identity(2)[0, 1]) == "0"
51 assert expr2cpp(ufl.Identity(2)[1, 0]) == "0"
52 assert expr2cpp(ufl.Identity(2)[1, 1]) == "1"
53 assert expr2cpp(ufl.PermutationSymbol(3)[1, 2, 3]) == "1"
54 assert expr2cpp(ufl.PermutationSymbol(3)[2, 1, 3]) == "-1"
55 assert expr2cpp(ufl.PermutationSymbol(3)[1, 1, 3]) == "0"
56
57
58 def test_cpp_formatting_of_geometry():
59 # Test geometry quantities (faked for testing!)
60 x = ufl.SpatialCoordinate(ufl.interval)[0]
61 assert expr2cpp(x) == "x[0]"
62 x, y = ufl.SpatialCoordinate(ufl.triangle)
63 assert expr2cpp(x) == "x[0]"
64 assert expr2cpp(y) == "x[1]"
65 nx, ny = ufl.FacetNormal(ufl.triangle)
66 assert expr2cpp(nx) == "n[0]"
67 assert expr2cpp(ny) == "n[1]"
68 Kv = ufl.CellVolume(ufl.triangle)
69 assert expr2cpp(Kv) == "volume"
70 Kh = ufl.CellDiameter(ufl.triangle)
71 assert expr2cpp(Kh) == "diameter"
72 Kr = ufl.Circumradius(ufl.triangle)
73 assert expr2cpp(Kr) == "circumradius"
74
75
76 def test_cpp_formatting_of_form_arguments():
77 # Test form arguments (faked for testing!)
78 V = ufl.FiniteElement("CG", ufl.triangle, 1)
79 f = ufl.Coefficient(V, count=0)
80 assert expr2cpp(f) == "w0"
81 v = ufl.TestFunction(V)
82 assert expr2cpp(v) == "v0"
83
84 V = ufl.VectorElement("CG", ufl.triangle, 1)
85 f = ufl.Coefficient(V, count=1)
86 assert expr2cpp(f[0]) == "w0[0]" # Renumbered to 0...
87 v = ufl.Argument(V, number=3)
88 assert expr2cpp(v[1]) == "v3[1]" # NOT renumbered to 0...
89
90 V = ufl.TensorElement("CG", ufl.triangle, 1)
91 f = ufl.Coefficient(V, count=2)
92 assert expr2cpp(f[1, 0]) == "w0[1][0]" # Renumbered to 0...
93 v = ufl.Argument(V, number=3)
94 assert expr2cpp(v[0, 1]) == "v3[0][1]" # NOT renumbered to 0...
95
96 # TODO: Test mixed functions
97 # TODO: Test tensor functions with symmetries
98
99
100 def test_cpp_formatting_of_arithmetic():
101 x, y = ufl.SpatialCoordinate(ufl.triangle)
102 # Test basic arithmetic operators
103 assert expr2cpp(x + 3) == "3 + x[0]"
104 assert expr2cpp(x * 2) == "2 * x[0]"
105 assert expr2cpp(x / 2) == "x[0] / 2"
106 assert expr2cpp(x * x) == "pow(x[0], 2)" # TODO: Will gcc optimize this to x*x for us?
107 assert expr2cpp(x**3) == "pow(x[0], 3)"
108 # TODO: Test all basic operators
109
110
111 def test_cpp_formatting_of_cmath():
112 x, y = ufl.SpatialCoordinate(ufl.triangle)
113 # Test cmath functions
114 assert expr2cpp(ufl.exp(x)) == "exp(x[0])"
115 assert expr2cpp(ufl.ln(x)) == "log(x[0])"
116 assert expr2cpp(ufl.sqrt(x)) == "sqrt(x[0])"
117 assert expr2cpp(abs(x)) == "fabs(x[0])"
118 assert expr2cpp(ufl.sin(x)) == "sin(x[0])"
119 assert expr2cpp(ufl.cos(x)) == "cos(x[0])"
120 assert expr2cpp(ufl.tan(x)) == "tan(x[0])"
121 assert expr2cpp(ufl.asin(x)) == "asin(x[0])"
122 assert expr2cpp(ufl.acos(x)) == "acos(x[0])"
123 assert expr2cpp(ufl.atan(x)) == "atan(x[0])"
124
125
126 def test_cpp_formatting_of_derivatives():
127 xx = ufl.SpatialCoordinate(ufl.triangle)
128 x, y = xx
129 # Test derivatives of basic operators
130 assert expr2cpp(x.dx(0)) == "1"
131 assert expr2cpp(x.dx(1)) == "0"
132 assert expr2cpp(ufl.grad(xx)[0, 0]) == "1"
133 assert expr2cpp(ufl.grad(xx)[0, 1]) == "0"
134 assert expr2cpp(ufl.sin(x).dx(0)) == "cos(x[0])"
135
136 # Test derivatives of target specific test fakes
137 V = ufl.FiniteElement("CG", ufl.triangle, 1)
138 f = ufl.Coefficient(V, count=0)
139 assert expr2cpp(f.dx(0)) == "d1_w0[0]"
140 v = ufl.Argument(V, number=3)
141 assert expr2cpp(v.dx(1)) == "d1_v3[1]" # NOT renumbered to 0...
142 # TODO: Test more derivatives
143 # TODO: Test variable derivatives using diff
144
145
146 def test_cpp_formatting_of_conditionals():
147 x, y = ufl.SpatialCoordinate(ufl.triangle)
148 # Test conditional expressions
149 assert expr2cpp(ufl.conditional(ufl.lt(x, 2), y, 3)) \
150 == "x[0] < 2 ? x[1]: 3"
151 assert expr2cpp(ufl.conditional(ufl.gt(x, 2), 4 + y, 3)) \
152 == "x[0] > 2 ? 4 + x[1]: 3"
153 assert expr2cpp(ufl.conditional(ufl.And(ufl.le(x, 2), ufl.ge(y, 4)), 7, 8)) \
154 == "x[0] <= 2 && x[1] >= 4 ? 7: 8"
155 assert expr2cpp(ufl.conditional(ufl.Or(ufl.eq(x, 2), ufl.ne(y, 4)), 7, 8)) \
156 == "x[0] == 2 || x[1] != 4 ? 7: 8"
157 # TODO: Some tests of nested conditionals with correct precedences?
158
159
160 def test_cpp_formatting_precedence_handling():
161 x, y = ufl.SpatialCoordinate(ufl.triangle)
162 # Test precedence handling with sums
163 # Note that the automatic sorting is reflected in formatting!
164 assert expr2cpp(y + (2 + x)) == "x[1] + (2 + x[0])"
165 assert expr2cpp((x + 2) + y) == "x[1] + (2 + x[0])"
166
167 assert expr2cpp((2 + x) + (3 + y)) == "(2 + x[0]) + (3 + x[1])"
168
169 assert expr2cpp((x + 3) + 2 + y) == "x[1] + (2 + (3 + x[0]))"
170 assert expr2cpp(2 + (x + 3) + y) == "x[1] + (2 + (3 + x[0]))"
171 assert expr2cpp(2 + (3 + x) + y) == "x[1] + (2 + (3 + x[0]))"
172 assert expr2cpp(y + (2 + (3 + x))) == "x[1] + (2 + (3 + x[0]))"
173
174 assert expr2cpp(2 + x + 3 + y) == "x[1] + (3 + (2 + x[0]))"
175 assert expr2cpp(2 + x + 3 + y) == "x[1] + (3 + (2 + x[0]))"
176
177 # Test precedence handling with divisions
178 # This is more stable than sums since there is no sorting.
179 assert expr2cpp((x / 2) / 3) == "(x[0] / 2) / 3"
180 assert expr2cpp(x / (y / 3)) == "x[0] / (x[1] / 3)"
181 assert expr2cpp((x / 2) / (y / 3)) == "(x[0] / 2) / (x[1] / 3)"
182 assert expr2cpp(x / (2 / y) / 3) == "(x[0] / (2 / x[1])) / 3"
183
184 # Test precedence handling with highest level types
185 assert expr2cpp(ufl.sin(x)) == "sin(x[0])"
186 assert expr2cpp(ufl.cos(x + 2)) == "cos(2 + x[0])"
187 assert expr2cpp(ufl.tan(x / 2)) == "tan(x[0] / 2)"
188 assert expr2cpp(ufl.acos(x + 3 * y)) == "acos(x[0] + 3 * x[1])"
189 assert expr2cpp(ufl.asin(ufl.atan(x**4))) == "asin(atan(pow(x[0], 4)))"
190 assert expr2cpp(ufl.sin(y) + ufl.tan(x)) == "sin(x[1]) + tan(x[0])"
191
192 # Test precedence handling with mixed types
193 assert expr2cpp(3 * (2 + x)) == "3 * (2 + x[0])"
194 assert expr2cpp((2 * x) + (3 * y)) == "2 * x[0] + 3 * x[1]"
195 assert expr2cpp(2 * (x + 3) * y) == "x[1] * (2 * (3 + x[0]))"
196 assert expr2cpp(2 * (x + 3)**4 * y) == "x[1] * (2 * pow(3 + x[0], 4))"
197 # TODO: More tests covering all types and more combinations!
198
199
200 def test_cpp_formatting_with_variables():
201 x, y = ufl.SpatialCoordinate(ufl.triangle)
202 # Test user-provided C variables for subexpressions
203 # we can use variables for x[0], and sum, and power
204 assert expr2cpp(x**2 + y**2, variables={x**2: 'x2', y**2: 'y2'}) == "x2 + y2"
205 assert expr2cpp(x**2 + y**2, variables={x: 'z', y**2: 'y2'}) == "pow(z, 2) + y2"
206 assert expr2cpp(x**2 + y**2, variables={x**2 + y**2: 'q'}) == "q"
207 # we can use variables in conditionals
208 assert expr2cpp(ufl.conditional(ufl.Or(ufl.eq(x, 2), ufl.ne(y, 4)), 7, 8),
209 variables={ufl.eq(x, 2): 'c1', ufl.ne(y, 4): 'c2'}) == "c1 || c2 ? 7: 8"
210 # we can replace coefficients (formatted by user provided code)
211 V = ufl.FiniteElement("CG", ufl.triangle, 1)
212 f = ufl.Coefficient(V, count=0)
213 assert expr2cpp(f, variables={f: 'f'}) == "f"
214 assert expr2cpp(f**3, variables={f: 'f'}) == "pow(f, 3)"
215 # variables do not replace derivatives of variable expressions
216 assert expr2cpp(f.dx(0), variables={f: 'f'}) == "d1_w0[0]"
217
218 # This test depends on which differentiation algorithm is in use
219 # in UFL, representing derivatives as SpatialDerivative or Grad:
220 # variables do replace variable expressions that are themselves derivatives
221 assert expr2cpp(f.dx(0), variables={f.dx(0): 'df', ufl.grad(f)[0]: 'df'}) == "df"
222 assert expr2cpp(ufl.grad(f)[0], variables={f.dx(0): 'df', ufl.grad(f)[0]: 'df'}) == "df"
223
224 # TODO: Test variables in more situations with indices and derivatives
225
226 # TODO: Test various compound operators