Debian Codegraph @

test/uflacs/unit/test_ufl_to_cnodes.py

raw
1 # -*- coding: utf-8 -*-
2
3 import ufl_legacy as ufl
4 from ufl_legacy import *
5 from ufl_legacy import as_ufl
6
7 import ffc.uflacs.language
8 from ffc.uflacs.language.ufl_to_cnodes import UFL2CNodesTranslatorCpp, UFL2CNodesTranslatorC
9
10
11 def test_ufl_to_cnodes():
12
13 L = ffc.uflacs.language.cnodes
14 translate = UFL2CNodesTranslatorCpp(L)
15
16 f = ufl.CellVolume(ufl.triangle)
17 g = ufl.CellVolume(ufl.triangle)
18 h = ufl.CellVolume(ufl.triangle)
19
20 x = L.Symbol("x")
21 y = L.Symbol("y")
22 z = L.Symbol("z")
23
24 examples = [
25 (f + g, (x, y), "x + y"),
26 # (f-g, (x,y), "x - y"), # - is not currently a UFL operator
27 (f * g, (x, y), "x * y"),
28 (f / g, (x, y), "x / y"),
29 (f**g, (x, y), "std::pow(x, y)"),
30 (exp(f), (x,), "std::exp(x)"),
31 (ln(f), (x,), "std::log(x)"),
32 (abs(f), (x,), "std::abs(x)"),
33 (sqrt(f), (x,), "std::sqrt(x)"),
34 #(cbrt(f), (x,), "std::cbrt(x)"),
35 (sin(f), (x,), "std::sin(x)"),
36 (cos(f), (x,), "std::cos(x)"),
37 (tan(f), (x,), "std::tan(x)"),
38 (asin(f), (x,), "std::asin(x)"),
39 (acos(f), (x,), "std::acos(x)"),
40 (atan(f), (x,), "std::atan(x)"),
41 (sinh(f), (x,), "std::sinh(x)"),
42 (cosh(f), (x,), "std::cosh(x)"),
43 (tanh(f), (x,), "std::tanh(x)"),
44 (erf(f), (x,), "std::erf(x)"),
45 #(erfc(f), (x,), "std::erfc(x)"),
46 (min_value(f, g), (x, y), "std::min(x, y)"),
47 (max_value(f, g), (x, y), "std::max(x, y)"),
48 (bessel_I(1, g), (x, y), "boost::math::cyl_bessel_i(x, y)"),
49 (bessel_J(1, g), (x, y), "boost::math::cyl_bessel_j(x, y)"),
50 (bessel_K(1, g), (x, y), "boost::math::cyl_bessel_k(x, y)"),
51 (bessel_Y(1, g), (x, y), "boost::math::cyl_neumann(x, y)"),
52 (f < g, (x, y), "x < y"),
53 (f > g, (x, y), "x > y"),
54 (f <= g, (x, y), "x <= y"),
55 (f >= g, (x, y), "x >= y"),
56 (eq(f, g), (x, y), "x == y"),
57 (ne(f, g), (x, y), "x != y"),
58 (And(f < g, f > g), (x, y), "x && y"),
59 (Or(f < g, f > g), (x, y), "x || y"),
60 (Not(f < g), (x,), "!x"),
61 (conditional(f < g, g, h), (x, y, z), "x ? y : z"),
62 ]
63 for expr, args, code in examples:
64 # Warning: This test is subtle: translate will look at the type of expr and
65 # ignore its operands, i.e. not translating the full tree but only one level.
66 assert str(translate(expr, *args)) == code
67
68
69 # C specific translation:
70 translate = UFL2CNodesTranslatorC(L)
71 examples = [
72 (sin(f), (x,), "sin(x)"),
73 (f**g, (x, y), "pow(x, y)"),
74 (exp(f), (x,), "exp(x)"),
75 (abs(f), (x,), "fabs(x)"),
76 (min_value(f, g), (x, y), "fmin(x, y)"),
77 (max_value(f, g), (x, y), "fmax(x, y)"),
78 ]
79 for expr, args, code in examples:
80 # Warning: This test is subtle: translate will look at the type of expr and
81 # ignore its operands, i.e. not translating the full tree but only one level.
82 assert str(translate(expr, *args)) == code