Debian Codegraph @

test/unit/symbolics/test_reduce_vartype.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2010 Kristian B. Oelgaard
3 #
4 # This file is part of FFC.
5 #
6 # FFC is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Lesser General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # FFC is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with FFC. If not, see <http://www.gnu.org/licenses/>.
18
19 import pytest
20 import time
21
22 # FFC modules
23 from ffc.quadrature.symbolics import *
24 from ffc.quadrature.cpp import format, set_float_formatting
25 from ffc.parameters import FFC_PARAMETERS
26 set_float_formatting(FFC_PARAMETERS['precision'])
27
28
29 def testReduceVarType():
30 f1 = FloatValue(1)
31 f2 = FloatValue(2)
32 f3 = FloatValue(3)
33 f5 = FloatValue(5)
34 fm4 = FloatValue(-4)
35
36 B0 = Symbol("B0", BASIS)
37 B1 = Symbol("B1", BASIS)
38 Bm4 = Product([fm4, B1])
39 B5 = Product([f5, B0])
40
41 I0 = Symbol("I0", IP)
42 I1 = Symbol("I1", IP)
43 I2 = Symbol("I2", IP)
44 I5 = Product([f5, I0])
45
46 G0 = Symbol("G0", GEO)
47 G1 = Symbol("G1", GEO)
48 G2 = Symbol("G2", GEO)
49 G3 = Product([f3, G0])
50
51 C0 = Symbol("C0", CONST)
52 C2 = Product([f2, C0])
53
54 p0 = Product([B0, I5])
55 p1 = Product([B0, B1])
56
57 S0 = Sum([B0, I5])
58 S1 = Sum([p0, p1])
59 S2 = Sum([B0, B1])
60 S3 = Sum([B0, p0])
61 S4 = Sum([f5, p0])
62 S5 = Sum([I0, G0])
63
64 F0 = Fraction(B0, I5).expand()
65 F1 = Fraction(p1, I5).expand()
66 F2 = Fraction(G3, S2).expand()
67 F3 = Fraction(G3, S3).expand()
68 F4 = Fraction(I1, Sum([I1, I0]))
69 F5 = Fraction(S5, I1)
70 F6 = Fraction(I0,
71 Sum([
72 Fraction(Sum([I0, I1]), Sum([G0, G1])),
73 Fraction(Sum([I1, I2]), Sum([G1, G2])),
74 ]))
75
76 r0 = B0.reduce_vartype(BASIS)
77 r1 = B0.reduce_vartype(CONST)
78
79 rp0 = p0.reduce_vartype(BASIS)
80 rp1 = p0.reduce_vartype(IP)
81 rp2 = p1.reduce_vartype(BASIS)
82 rp3 = p1.reduce_vartype(GEO)
83
84 rs0 = S0.reduce_vartype(BASIS)
85 rs1 = S0.reduce_vartype(IP)
86 rs2 = S1.reduce_vartype(BASIS)
87 rs3 = S4.reduce_vartype(BASIS)
88 rs4 = S4.reduce_vartype(CONST)
89
90 rf0 = F0.reduce_vartype(BASIS)
91 rf1 = F1.reduce_vartype(BASIS)
92 rf2 = F0.reduce_vartype(IP)
93 rf3 = F2.reduce_vartype(BASIS)
94 rf4 = F3.reduce_vartype(BASIS)
95 rf5 = F4.reduce_vartype(IP)
96 rf6 = F5.reduce_vartype(IP)
97 rf7 = F6.reduce_vartype(IP)
98
99 assert [(B0, f1)] == r0
100 assert [((), B0)] == r1
101
102 assert [(B0, I5)] == rp0
103 assert [(I0, B5)] == rp1
104 assert [(p1, f1)] == rp2
105 assert [((), p1)] == rp3
106
107 assert ((), I5) == rs0[0]
108 assert (B0, f1) == rs0[1]
109 assert (I0, f5) == rs1[1]
110 assert ((), B0) == rs1[0]
111 assert (Product([B0, B1]), f1) == rs2[1]
112 assert (B0, I5) == rs2[0]
113 assert ((), f5) == rs3[0]
114 assert (B0, I5) == rs3[1]
115 assert (f5, Sum([f1, Product([B0, I0])])) == rs4[0]
116
117 assert [(B0, Fraction(FloatValue(0.2), I0))] == rf0
118 assert [(Product([B0, B1]), Fraction(FloatValue(0.2), I0))] == rf1
119 assert [(Fraction(f1, I0), Product([FloatValue(0.2), B0]))] == rf2
120 assert [(Fraction(f1, S2), G3)] == rf3
121 assert [(Fraction(f1, B0), Fraction(G3, Sum([I5, f1])))] == rf4
122 assert F4 == rf5[0][0]
123 assert FloatValue(1) == rf5[0][1]
124 assert Fraction(I0, I1) == rf6[1][0]
125 assert f1 == rf6[1][1]
126 assert Fraction(f1, I1) == rf6[0][0]
127 assert G0 == rf6[0][1]
128 assert F6 == rf7[0][0]
129 assert f1 == rf7[0][1]
130
131 expr = Sum([Symbol('W1', GEO), Fraction(Symbol('det', GEO), Sum([Symbol('F0', IP), Symbol('K_11', GEO)]))])
132 red = expr.expand().reduce_vartype(IP)
133 vals = []
134 for ip in red:
135 ip_dec, geo = ip
136 if ip_dec and geo:
137 vals.append(Product([ip_dec, geo]))
138 elif geo:
139 vals.append(geo)
140 elif ip_dec:
141 vals.append(ip_dec)
142 comb = Sum(vals).expand()
143 K_11 = 1.4
144 F0 = 1.5
145 W1 = 1.9
146 det = 2.1
147 assert round(eval(str(expr)) - eval(str(comb)), 10) == 0.0