Debian Codegraph @

test/uflacs/unit/test_ufc_backend.py

raw
1 # -*- coding: utf-8 -*-
2
3 import pytest
4
5 import numpy
6
7 from ffc.uflacs.backends.ufc.generators import *
8
9 import ffc.uflacs.language.cnodes as L
10
11 # from ffc.cpp import set_float_formatting
12 # set_float_formatting(precision=8)
13
14
15 # TODO: Make this a feature of dijitso: dijitso show-function modulehash functionname
16 def extract_function(name, code):
17 lines = code.split("\n")
18 n = len(lines)
19 begin = None
20 body = None
21 for i in range(n):
22 if (name + "(") in lines[i]:
23 for j in range(i, n):
24 if lines[j] == "{":
25 begin = i
26 body = j
27 break
28 break
29 if begin is None:
30 return "didnt find %s" % (name,)
31 end = n
32 for i in range(body, n):
33 if lines[i] == "}":
34 end = i + 1
35 break
36 sublines = lines[begin:end]
37 return '\n'.join(sublines)
38
39
40 def basic_class_properties(classname):
41 ir = {
42 "classname": classname,
43 "constructor": "",
44 "constructor_arguments": "",
45 "initializer_list": "",
46 "destructor": "",
47 "members": "",
48 "preamble": "",
49 "jit": False,
50 }
51 return ir
52
53
54 def mock_parameters():
55 return {"precision": 8}
56
57
58 def mock_form_ir():
59 ir = basic_class_properties("mock_form_classname")
60 ir.update({
61 "id": 0,
62 "prefix": "Foo",
63 "signature": "mock form signature",
64 "rank": 2,
65 "num_coefficients": 3,
66 "original_coefficient_position": [0, 2],
67 })
68
69 ir.update({
70 "create_coordinate_finite_element": ["mock_coordinate_finite_element_classname"],
71 "create_coordinate_dofmap": ["mock_coordinate_dofmap_classname"],
72 "create_coordinate_mapping": ["mock_coordinate_mapping_classname"],
73 "create_finite_element": ["mock_finite_element_classname_%d" % (i,) for i in range(ir["num_coefficients"])],
74 "create_dofmap": ["mock_dofmap_classname_%d" % (i,) for i in range(ir["num_coefficients"])],
75 })
76
77 # These are the method names in ufc::form that are specialized for each integral type
78 template = "max_%s_subdomain_id"
79 for i, integral_type in enumerate(ufc_integral_types):
80 key = template % integral_type
81 ir[key] = i # just faking some integers
82
83 template = "has_%s_integrals"
84 for i, integral_type in enumerate(ufc_integral_types):
85 key = template % integral_type
86 ir[key] = (i % 2 == 0) # faking some bools
87
88 template = "create_%s_integral"
89 for i, integral_type in enumerate(ufc_integral_types):
90 key = template % integral_type
91 subdomain_ids = list(range(i))
92 classnames = [key.replace("create_", "") + str(j) for j in subdomain_ids]
93 ir[key] = (subdomain_ids, classnames)
94
95 template = "create_default_%s_integral"
96 for i, integral_type in enumerate(ufc_integral_types):
97 key = template % integral_type
98 classname = key.replace("create_", "")
99 ir[key] = classname
100
101 return ir
102
103
104 def mock_dofmap_ir():
105 # Note: The mock data here is not necessarily consistent,
106 # it just needs to have the right types to exercise the code generation.
107 ir = basic_class_properties("mock_dofmap_classname")
108 num_dofs_per_entity = [1, 1, 1]
109 entity_dofs = [[(0,), (1,), (2,)],
110 [(3,), (4,), (5,)],
111 [(6,)]]
112 entity_closure_dofs = {
113 (0, 0): [0],
114 (0, 1): [1],
115 (0, 2): [2],
116 (1, 0): [0, 1],
117 (1, 1): [0, 2],
118 (1, 2): [1, 2],
119 (2, 0): [0, 1, 2],
120 }
121 ir.update({
122 "signature": "mock element signature",
123 "geometric_dimension": 3,
124 "topological_dimension": 2,
125 "global_dimension": ([3, 2, 1], 4),
126 "tabulate_dofs": ([[[(0,), (1,), (2,)], [(3,), (4,), (5,)], [(6,)]], None], [7, 1], True, [False, True]),
127 "tabulate_facet_dofs": [[0, 1, 2], [1, 2, 3], [0, 2, 3]],
128 "tabulate_entity_dofs": (entity_dofs, num_dofs_per_entity),
129 "tabulate_entity_closure_dofs": (entity_closure_dofs, entity_dofs, num_dofs_per_entity),
130 "needs_mesh_entities": [True, False, True],
131 "num_global_support_dofs": 1,
132 "num_element_support_dofs": 6,
133 "num_element_dofs": 7,
134 "num_entity_dofs": num_dofs_per_entity,
135 "num_entity_closure_dofs": [3, 6, 10],
136 "num_facet_dofs": 7,
137 "num_sub_dofmaps": 3,
138 "create_sub_dofmap": ["mock_dofmap_classname_sub_%d" % (i,) for i in range(3)],
139 })
140 return ir
141
142
143 def mock_evaluate_basis_ir():
144 dofs_data = [
145 {
146 "embedded_degree": 5,
147 "num_components": 2,
148 "num_expansion_members": 7,
149 "coeffs": [list(range(20, 27)), list(range(30, 37))],
150 "reference_offset": 3,
151 "physical_offset": 5,
152 "mapping": "affine",
153 "dmats": numpy.zeros([]), # FIXME: Mock dmats data
154 },
155 {
156 "embedded_degree": 1,
157 "num_components": 2,
158 "num_expansion_members": 7,
159 "coeffs": [list(range(7)), list(range(10, 17))],
160 "reference_offset": 3,
161 "physical_offset": 5,
162 "mapping": "affine",
163 "dmats": numpy.zeros([]), # FIXME: Mock dmats data
164 }
165 ]
166 data = {
167 "cellname": "triangle",
168 "geometric_dimension": 3,
169 "topological_dimension": 2,
170 "reference_value_size": 3,
171 "physical_value_size": 2,
172 "dofs_data": dofs_data,
173 "needs_oriented": True,
174 "space_dimension": 14,
175 "max_degree": 5,
176 }
177 return data
178
179
180 @pytest.mark.skipif(True, reason="mock ir for evaluate basis is currently incomplete")
181 def test_mock_evaluate_basis():
182 from ffc.uflacs.backends.ufc.evaluatebasis import generate_evaluate_reference_basis
183 import ffc.uflacs.language.cnodes as L
184 parameters = mock_parameters()
185 data = mock_evaluate_basis_ir() # FIXME: mock data is currently incomplete
186 code = generate_evaluate_reference_basis(L, data, parameters)
187 print(code)
188
189
190 def mock_finite_element_ir():
191 # Note: The mock data here is not necessarily consistent,
192 # it just needs to have the right types to exercise the code generation.
193 ir = basic_class_properties("mock_finite_element_classname")
194 ebir = mock_evaluate_basis_ir()
195 ir.update({
196 "signature": "mock element signature",
197 "cell_shape": "mock_cell_shape",
198 "geometric_dimension": 3,
199 "topological_dimension": 2,
200 "degree": 2,
201 "family": "Lagrange",
202 "value_dimension": (3, 3),
203 "reference_value_dimension": (2, 2),
204 "space_dimension": 6,
205 "tabulate_dof_coordinates": {"gdim": 3, "tdim": 2, "points": [(0.0, 0.0), (0.0, 1.0), (1.0, 0.0)]},
206 "evaluate_reference_basis": ebir,
207 "evaluate_reference_basis_derivatives": ebir,
208 "evaluate_basis": ebir,
209 "evaluate_basis_derivatives": ebir,
210 "evaluate_basis_all": ebir,
211 "evaluate_basis_derivatives_all": ebir,
212 "evaluate_dof": "fixme",
213 "evaluate_dofs": "fixme",
214 "interpolate_vertex_values": "fixme",
215 "num_sub_elements": 3,
216 "create_sub_element": ["mock_finite_element_classname_sub_%d" % (i,) for i in range(3)],
217 })
218 return ir
219
220
221 def mock_integral_ir():
222 # Note: The mock data here is not necessarily consistent,
223 # it just needs to have the right types to exercise the code generation.
224 ir = basic_class_properties("mock_integral_classname")
225 ir.update({
226 "representation": "uflacs",
227 "integral_metadata": [{"dummy": "integral metadata"}],
228 "integrals_metadata": {"dummy": "integrals metadata"},
229 "prefix": "Foo",
230 "enabled_coefficients": [True, False, True],
231 "tabulate_tensor": " mock_body_of_tabulate_tensor();",
232 "num_cells": 1,
233 })
234 return ir
235
236
237 def mock_coordinate_mapping_ir():
238 ir = basic_class_properties("mock_coordinate_mapping_classname")
239 ir.update({
240 "signature": "mock_coordinate_mapping_signature",
241 "cell_shape": "mock_cell_shape",
242 "geometric_dimension": 3,
243 "topological_dimension": 2,
244 "create_coordinate_finite_element": "mock_coordinate_finite_element_classname",
245 "create_coordinate_dofmap": "mock_coordinate_dofmap_classname",
246 "scalar_coordinate_finite_element_classname": "mock_scalar_coordinate_finite_element_classname",
247 "coordinate_element_degree": 2,
248 "num_scalar_coordinate_element_dofs": 5,
249 "tables": {"x0": numpy.ones((5,)),
250 "xm": numpy.ones((5,)),
251 "J0": numpy.ones((2, 5)),
252 "Jm": numpy.ones((2, 5)), }
253 })
254 return ir
255
256
257 def compile_mock_coordinate_mapping():
258 parameters = mock_parameters()
259 ir = mock_coordinate_mapping_ir()
260 gen = ufc_coordinate_mapping()
261 return gen.generate(L, ir, parameters)
262
263
264 def compile_mock_form():
265 parameters = mock_parameters()
266 ir = mock_form_ir()
267 gen = ufc_form()
268 return gen.generate(L, ir, parameters)
269
270
271 def compile_mock_dofmap():
272 parameters = mock_parameters()
273 ir = mock_dofmap_ir()
274 gen = ufc_dofmap()
275 return gen.generate(L, ir, parameters)
276
277
278 def compile_mock_finite_element():
279 parameters = mock_parameters()
280 ir = mock_finite_element_ir()
281 gen = ufc_finite_element()
282 return gen.generate(L, ir, parameters)
283
284
285 def compile_mock_integral(integral_type):
286 parameters = mock_parameters()
287 ir = mock_integral_ir()
288 gen = eval("ufc_%s_integral" % integral_type)()
289 return gen.generate(L, ir, parameters)
290
291
292 def compile_mock_all():
293 mocks = [compile_mock_integral(integral_type) for integral_type in ufc_integral_types]
294 mocks += [compile_mock_form(), compile_mock_dofmap(), compile_mock_finite_element()]
295 return '\n\n'.join(mocks)
296
297
298 def test_mock_coordinate_mapping():
299 h, cpp = compile_mock_coordinate_mapping()
300 print(h)
301 print(cpp)
302
303
304 def test_mock_form():
305 h, cpp = compile_mock_form()
306 print(h)
307 print(cpp)
308
309
310 def test_mock_dofmap():
311 h, cpp = compile_mock_dofmap()
312 print(h)
313 print(cpp)
314
315
316 @pytest.mark.skipif(True, reason="mock ir for evaluate basis is currently incomplete")
317 def test_mock_finite_element():
318 h, cpp = compile_mock_finite_element()
319 print(h)
320 print(cpp)
321
322
323 def test_mock_integral():
324 for integral_type in ufc_integral_types:
325 h, cpp = compile_mock_integral(integral_type)
326 print(h)
327 print(cpp)
328
329
330 def test_foo_integral_properties():
331 parameters = mock_parameters()
332 ir = mock_form_ir()
333 assert "cell_integral" in ufc_form.create_cell_integral.__doc__
334 assert "return" in str(ufc_form().create_cell_integral(L, ir, parameters))
335
336
337 def test_mock_extract_function():
338 h, cpp = compile_mock_coordinate_mapping()
339 name = "compute_reference_coordinates"
340 print("/// Extracted", name, ":")
341 print("/// begin")
342 print(extract_function(name, cpp))
343 print("/// end")
344
345
346 def test_debug_by_printing_extracted_function():
347 h, cpp = compile_mock_coordinate_mapping()
348 # name = "compute_reference_coordinates"
349 # name = "compute_physical_coordinates"
350 # name = "compute_jacobians"
351 name = "compute_jacobian_inverses"
352 print("/// Extracted", name, ":")
353 print("/// begin")
354 print(extract_function(name, cpp))
355 print("/// end")
356
357
358 """
359 Missing:
360
361 finite_element:
362 evaluate_basis*
363 evaluate_dof
364 interpolate_vertex_values
365 Ok? tabulate_dof_coordinates
366
367 integrals:
368 tabulate_tensor
369
370 all:
371 everything with classnames
372 """