Debian Codegraph @

test/uflacs/crosslanguage/xtest_tabulate_tensor_body.py

raw
1 #!/usr/bin/env py.test
2 # -*- coding: utf-8 -*-
3
4 import ufl_legacy as ufl
5 from ufl_legacy import *
6 #from ufl_legacy import product
7
8 from ffc.uflacs.backends.toy.toy_compiler import compile_expression
9
10 def compile_expression0(expr):
11 code = ""
12 return code
13
14 def compile_expression1(expr):
15 code = compile_expression(expr, "")
16 return code
17
18 def compile_tabulate_tensor_body(integral):
19 # TODO: Handle measure type etc.
20 if isinstance(integral, Form):
21 integral, = integral.integrals()
22 expr = integral.integrand()
23 code = compile_expression(expr, "")
24 return code
25
26 def test_interval_tabten_x_given(gtest):
27 """Test code generation of body of the ufc function:
28
29 void tabulate_tensor(
30 double* A,
31 const double * const * w,
32 const double * coordinate_dofs,
33 std::size_t num_points,
34 const double * const * points
35 ) const;
36
37 """
38
39 pre = """
40 mock_cell mc;
41 mc.fill_reference_interval(1);
42
43 double * coordinate_dofs = mc.coordinate_dofs;
44 double A[1] = { 0.0 };
45 std::size_t num_points = 1;
46 double points[1] = { 1.2 };
47
48 // "Fetching" a point
49 double * x = points;
50 """
51
52 post = """
53 ASSERT_EQ(A[0], 1.2);
54 """
55
56 cell = interval
57
58 x = SpatialCoordinate(cell)[0]
59 expr = x
60 integral = expr*dP
61
62 code = compile_tabulate_tensor_body(integral)
63
64 gtest.add(pre + code + post)
65
66 def test_interval_tabten_dg0_given(gtest):
67 """Test code generation of body of the ufc function:
68
69 void tabulate_tensor(
70 double* A,
71 const double * const * w,
72 const double * coordinate_dofs,
73 std::size_t num_points,
74 const double * const * points
75 ) const;
76
77 with mock values for DG0/Real coefficients in w[][].
78
79 """
80
81 pre = """
82 mock_cell mc;
83 mc.fill_reference_interval(1);
84 mc.coordinate_dofs[0*mc.geometric_dimension + 0] = 0.1;
85 mc.coordinate_dofs[1*mc.geometric_dimension + 0] = 0.2;
86
87 double A[1];
88 memset(A, 0, sizeof(A));
89
90 double w[2][2] = { { 1.2, 0.0 }, // second value here is unused
91 { 2.0, 3.0 } };
92
93 double * coordinate_dofs = mc.coordinate_dofs;
94 std::size_t num_points = 1;
95 double points[1] = { 0.15 };
96
97 // "Fetching" a point
98 double * x = points;
99 """
100
101 post = """
102 ASSERT_EQ(A[0], 0.15*1.2*(2.0+3.0));
103 """
104
105 cell = interval
106 x = SpatialCoordinate(cell)[0]
107
108 V = VectorElement("DG", cell, 0, dim=2)
109 w0 = Constant(cell, count=0)
110 w1 = Coefficient(V, count=1)
111
112 expr = x*w0*(w1[0] + w1[1])
113
114 integral = expr*dP
115
116 code = compile_tabulate_tensor_body(integral)
117
118 gtest.add(pre + code + post)
119
120 def xtest_interval_tabten_geometry_mappings(gtest):
121 """Test code generation of body of the ufc function:
122
123 void tabulate_tensor(
124 double* A,
125 const double * const * w,
126 const double * coordinate_dofs
127 ) const;
128 """
129 pass
130
131 def xtest_interval_geometry_expressions(gtest):
132 """Test code generation of geometry expressions on an interval.
133
134 """
135
136 pre = """
137 mock_cell mc;
138 mc.fill_reference_interval(1);
139 mc.coordinate_dofs[0*mc.geometric_dimension + 0] = 0.2;
140 mc.coordinate_dofs[1*mc.geometric_dimension + 0] = 0.1;
141
142 double * coordinate_dofs = mc.coordinate_dofs;
143 double A[1];
144 memset(A, 0, sizeof(A));
145
146 """
147
148 post = """
149 // Check that geometric quantities are declared and computed correctly
150 ASSERT_EQ(x[0], TODO);
151 ASSERT_EQ(xi[0], TODO);
152 ASSERT_EQ(J[0], -0.1);
153 ASSERT_EQ(K[0], -10.0);
154 ASSERT_EQ(detJ, -0.1);
155 ASSERT_EQ(volume, 0.1);
156 ASSERT_EQ(circumradius, TODO);
157
158 // Check that geometric quantities have been placed in the output array
159 std::size_t gd = mc.geometric_dimension;
160 std::size_t td = mc.topological_dimension;
161 double * AA = A;
162 ASSERT_EQ(AA[0], x[0]); AA += gd;
163 ASSERT_EQ(AA[0], xi[0]); AA += td;
164 ASSERT_EQ(AA[0], J[0]); AA += gd * td;
165 ASSERT_EQ(AA[0], K[0]); AA += td * gd;
166 ASSERT_EQ(AA[0], detJ); AA += 1;
167 ASSERT_EQ(AA[0], volume); AA += 1;
168 ASSERT_EQ(AA[0], circumradius); AA += 1;
169 """
170 cell = interval
171
172 gd = cell.geometric_dimension()
173 td = cell.topological_dimension()
174
175 values = []
176 values.extend(SpatialCoordinate(cell)[i] for i in range(gd))
177 values.extend(LocalCoordinate(cell)[i] for i in range(td))
178 values.extend(GeometryJacobian(cell)[i, j] for i in range(gd) for i in range(td))
179 values.extend(InverseGeometryJacobian(cell)[i, j] for i in range(td) for i in range(gd))
180 values.append(GeometryJacobianDeterminant(cell))
181 values.append(CellVolume(cell))
182 values.append(Circumradius(cell))
183
184 expr = as_vector(values)
185
186 code = compile_expression1(expr)
187
188 gtest.add(pre + code + post)
189
190 def test_tabulate_tensor_interval_facet(gtest):
191 """Test code generation of body of the ufc function:
192
193 void tabulate_tensor(
194 double* A,
195 const double * const * w,
196 const double * coordinate_dofs,
197 unsigned int facet) const;
198
199 """
200
201 pre = """
202 mock_cell mc;
203 mc.fill_reference_interval(1);
204 mc.coordinate_dofs[0*mc.geometric_dimension + 0] = 0.1;
205 mc.coordinate_dofs[1*mc.geometric_dimension + 0] = 0.2;
206
207 double A[1];
208 memset(A, 0, sizeof(A));
209
210 double w[1][2] = { { 2.0, 3.0 } };
211
212 double * coordinate_dofs = mc.coordinate_dofs;
213
214 unsigned int facet = 0;
215 """
216
217 post = """
218 // TODO
219 """
220
221 code = "// TODO"
222
223 gtest.add(pre + code + post)
224
225 def test_tabulate_tensor_interval_interior_facet(gtest):
226 """Test code generation of body of the ufc function:
227
228 void tabulate_tensor(
229 double* A,
230 const double * const * w,
231 const double * coordinate_dofs0,
232 const double * coordinate_dofs1,
233 unsigned int facet0,
234 unsigned int facet1) const;
235 """
236
237 pre = """
238 mock_cell mc0;
239 mc0.fill_reference_interval(1);
240 mc0.coordinate_dofs[0*mc0.geometric_dimension + 0] = 0.1;
241 mc0.coordinate_dofs[1*mc0.geometric_dimension + 0] = 0.2;
242
243 mock_cell mc1;
244 mc1.fill_reference_interval(1);
245 mc1.coordinate_dofs[0*mc1.geometric_dimension + 0] = 0.2;
246 mc1.coordinate_dofs[1*mc1.geometric_dimension + 0] = 0.3;
247
248 double A[2];
249 memset(A, 0, sizeof(A));
250
251 double w[1][4] = { { 2.0, 3.0,
252 4.0, 5.0 } };
253
254 double * coordinate_dofs0 = mc0.coordinate_dofs;
255 double * coordinate_dofs1 = mc1.coordinate_dofs;
256
257 unsigned int facet0 = 1;
258 unsigned int facet1 = 0;
259 """
260
261 post = """
262 // TODO
263 """
264
265 code = "// TODO"
266
267 gtest.add(pre + code + post)