| 1 |
# -*- coding: utf-8 -*- |
| 2 |
""" |
| 3 |
Tests of dolfin Expression formatting. |
| 4 |
""" |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
import ufl_legacy as ufl |
| 9 |
from ufl_legacy.constantvalue import as_ufl |
| 10 |
|
| 11 |
import numpy |
| 12 |
|
| 13 |
import hashlib |
| 14 |
|
| 15 |
from ffc.uflacs.params import default_parameters |
| 16 |
from ffc.uflacs.backends.dolfin.expression import format_dolfin_expression |
| 17 |
from ffc.uflacs.backends.dolfin.dolfin_compiler import compile_dolfin_expression_body |
| 18 |
|
| 19 |
|
| 20 |
# FIXME: Make tests with dolfin optional |
| 21 |
@pytest.fixture |
| 22 |
def dolfin(): |
| 23 |
import dolfin |
| 24 |
return dolfin |
| 25 |
|
| 26 |
|
| 27 |
# For test comparisons |
| 28 |
dolfin_expression_minimal = """\ |
| 29 |
class MyExpression: public Expression |
| 30 |
{ |
| 31 |
public: |
| 32 |
|
| 33 |
virtual void eval(Array<double>& values, |
| 34 |
const Array<double>& x) const |
| 35 |
{ |
| 36 |
|
| 37 |
} |
| 38 |
};""" |
| 39 |
|
| 40 |
dolfin_expression_with_constructor1 = """\ |
| 41 |
class MyExpression: public Expression |
| 42 |
{ |
| 43 |
public: |
| 44 |
MyExpression(): Expression(2) {} |
| 45 |
|
| 46 |
virtual void eval(Array<double>& values, |
| 47 |
const Array<double>& x) const |
| 48 |
{ |
| 49 |
|
| 50 |
} |
| 51 |
};""" |
| 52 |
|
| 53 |
dolfin_expression_with_constructor2 = """\ |
| 54 |
class MyExpression: public Expression |
| 55 |
{ |
| 56 |
public: |
| 57 |
MyExpression(): Expression(2, 3) {} |
| 58 |
|
| 59 |
virtual void eval(Array<double>& values, |
| 60 |
const Array<double>& x) const |
| 61 |
{ |
| 62 |
|
| 63 |
} |
| 64 |
};""" |
| 65 |
|
| 66 |
dolfin_expression_with_members = """\ |
| 67 |
class MyExpression: public Expression |
| 68 |
{ |
| 69 |
public: |
| 70 |
|
| 71 |
virtual void eval(Array<double>& values, |
| 72 |
const Array<double>& x) const |
| 73 |
{ |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
double c; |
| 78 |
boost::shared_ptr<MeshFunction<double> > mf; |
| 79 |
boost::shared_ptr<GenericFunction> gf; |
| 80 |
boost::shared_ptr<Function> f; |
| 81 |
};""" |
| 82 |
|
| 83 |
dolfin_expression_with_members2 = """\ |
| 84 |
class MyExpression: public Expression |
| 85 |
{ |
| 86 |
public: |
| 87 |
|
| 88 |
virtual void eval(Array<double>& values, |
| 89 |
const Array<double>& x) const |
| 90 |
{ |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
double c0; |
| 95 |
double c1; |
| 96 |
boost::shared_ptr<MeshFunction<double> > mf0; |
| 97 |
boost::shared_ptr<MeshFunction<double> > mf1; |
| 98 |
boost::shared_ptr<GenericFunction> gf0; |
| 99 |
boost::shared_ptr<GenericFunction> gf1; |
| 100 |
boost::shared_ptr<Function> f0; |
| 101 |
boost::shared_ptr<Function> f1; |
| 102 |
};""" |
| 103 |
|
| 104 |
dolfin_expression_with_cell = """\ |
| 105 |
class MyExpression: public Expression |
| 106 |
{ |
| 107 |
public: |
| 108 |
|
| 109 |
void eval(Array<double>& values, const Array<double>& x, |
| 110 |
const ufc::cell& ufc_cell) const |
| 111 |
{ |
| 112 |
// Emulate tabulate_tensor arguments |
| 113 |
const ufc::cell& c = ufc_cell; |
| 114 |
Array<double> & A = values; |
| 115 |
|
| 116 |
//dolfin_assert(ufc_cell.local_facet >= 0); |
| 117 |
|
| 118 |
values[0] = 1.23; |
| 119 |
} |
| 120 |
};""" |
| 121 |
|
| 122 |
|
| 123 |
other_stuff = """ |
| 124 |
// Getting cell data |
| 125 |
const uint gd = cell.geometric_dimension; |
| 126 |
const uint td = cell.topological_dimension; |
| 127 |
const uint cell_index = cell.entity_indices[td][0]; |
| 128 |
|
| 129 |
// Tabulation of mesh function values |
| 130 |
double mf0v = (*mf0)[cell_index]; |
| 131 |
|
| 132 |
// Evaluation of functions |
| 133 |
Array<double> w0v(1); |
| 134 |
w0->eval(w0v, x, cell); |
| 135 |
|
| 136 |
// Writing output values |
| 137 |
values[0] = w0v[0]*mf0v; |
| 138 |
values[1] = w0v[0]+mf0v; |
| 139 |
""" |
| 140 |
|
| 141 |
#class DolfinExpressionFormatterTest(UflTestCase): |
| 142 |
#"""Tests of Dolfin C++ Expression formatting utilities.""" |
| 143 |
|
| 144 |
def test_dolfin_expression_with_cell(dolfin): |
| 145 |
e = dolfin.Expression(cppcode=dolfin_expression_with_cell) |
| 146 |
assert e.ufl_shape == () |
| 147 |
values = numpy.zeros((1,)) |
| 148 |
x = numpy.asarray((0.2, 0.2)) |
| 149 |
mesh = dolfin.UnitSquareMesh(1, 1) |
| 150 |
cell = dolfin.Cell(mesh, 0) |
| 151 |
e.eval_cell(values, x, cell) |
| 152 |
assert values[0] == 1.23 |
| 153 |
#assert e((1.0,2.0)) == 1.23 # FAILS! Need dolfin to dispatch this to eval_cell with the proper Cell found from x for this to work. |
| 154 |
|
| 155 |
def test_dolfin_expression_constructors(): |
| 156 |
code = format_dolfin_expression(classname="MyExpression", |
| 157 |
shape=()) |
| 158 |
assert code == dolfin_expression_minimal |
| 159 |
code = format_dolfin_expression(classname="MyExpression", |
| 160 |
shape=(2,)) |
| 161 |
assert code == dolfin_expression_with_constructor1 |
| 162 |
code = format_dolfin_expression(classname="MyExpression", |
| 163 |
shape=(2, 3)) |
| 164 |
assert code == dolfin_expression_with_constructor2 |
| 165 |
|
| 166 |
def test_dolfin_expression_members(): |
| 167 |
code = format_dolfin_expression(classname="MyExpression", |
| 168 |
shape=(), |
| 169 |
constants=('c',), |
| 170 |
mesh_functions=('mf',), |
| 171 |
generic_functions=('gf',), |
| 172 |
functions=('f',), |
| 173 |
) |
| 174 |
assert code == dolfin_expression_with_members |
| 175 |
code = format_dolfin_expression(classname="MyExpression", |
| 176 |
shape=(), |
| 177 |
constants=('c0', 'c1'), |
| 178 |
mesh_functions=('mf0', 'mf1'), |
| 179 |
generic_functions=('gf0', 'gf1'), |
| 180 |
functions=('f0', 'f1'), |
| 181 |
) |
| 182 |
assert code == dolfin_expression_with_members2 |
| 183 |
|
| 184 |
def test_explicit_dolfin_expression_compilation(dolfin): |
| 185 |
code = format_dolfin_expression(classname="MyExpression", |
| 186 |
shape=(2,), |
| 187 |
eval_body=['values[0] = x[0];', |
| 188 |
'values[1] = x[1];'], |
| 189 |
constants=('c0', 'c1'), |
| 190 |
mesh_functions=('mf0', 'mf1'), |
| 191 |
generic_functions=('gf0', 'gf1'), |
| 192 |
functions=('f0', 'f1'), |
| 193 |
) |
| 194 |
expr = dolfin.Expression(cppcode=code) |
| 195 |
assert hasattr(expr, 'c0') |
| 196 |
assert hasattr(expr, 'c1') |
| 197 |
assert hasattr(expr, 'mf0') |
| 198 |
assert hasattr(expr, 'mf1') |
| 199 |
assert hasattr(expr, 'gf0') |
| 200 |
assert hasattr(expr, 'gf1') |
| 201 |
assert hasattr(expr, 'f0') |
| 202 |
assert hasattr(expr, 'f1') |
| 203 |
#dolfin.plot(expr, mesh=dolfin.UnitSquareMesh(10,10), interactive=True) |
| 204 |
|
| 205 |
|
| 206 |
def flattened_nonempty_lines(lines): |
| 207 |
res = [] |
| 208 |
for l in lines: |
| 209 |
if isinstance(l, list): |
| 210 |
res.extend(flattened_nonempty_lines(l)) |
| 211 |
elif l: |
| 212 |
res.append(l) |
| 213 |
else: |
| 214 |
pass |
| 215 |
return res |
| 216 |
|
| 217 |
#class Ufl2DolfinExpressionCompilerTest(UflTestCase): |
| 218 |
#"""Tests of Dolfin C++ Expression compilation from UFL expressions.""" |
| 219 |
|
| 220 |
def check_dolfin_expression_compilation(uexpr, expected_lines, expected_values, members={}): |
| 221 |
parameters = default_parameters() |
| 222 |
# Compile expression |
| 223 |
compiled_lines, member_names = compile_dolfin_expression_body(uexpr, parameters) |
| 224 |
|
| 225 |
# Check expected compilation output |
| 226 |
if flattened_nonempty_lines(compiled_lines) != expected_lines: |
| 227 |
print('\n'*5) |
| 228 |
print("Upcoming failure, expected lines:") |
| 229 |
print('\n'.join(expected_lines)) |
| 230 |
print("\nActual lines:") |
| 231 |
print('\n'.join(flattened_nonempty_lines(compiled_lines))) |
| 232 |
print('\n'*5) |
| 233 |
assert flattened_nonempty_lines(compiled_lines) == expected_lines |
| 234 |
|
| 235 |
# Add debugging lines to print scalar expressions at end of Expression eval call |
| 236 |
if 0: |
| 237 |
printme = ["x[0]", "values[0]"] |
| 238 |
compiled_lines += ['std::cout '] + [' << "%s = " << %s << std::endl' % (pm, pm) for pm in printme] + ['<< std::endl;'] |
| 239 |
|
| 240 |
# Wrap it in a dolfin::Expression class |
| 241 |
shape = uexpr.ufl_shape |
| 242 |
name = "MyExpression_%s" % hashlib.md5(str(hash(uexpr))).hexdigest() |
| 243 |
code = format_dolfin_expression(classname=name, |
| 244 |
shape=shape, |
| 245 |
eval_body=compiled_lines, |
| 246 |
**member_names) |
| 247 |
if 0: |
| 248 |
print("SKIPPING REST OF TEST") |
| 249 |
return |
| 250 |
|
| 251 |
# Try to compile it with DOLFIN |
| 252 |
import dolfin |
| 253 |
dexpr = dolfin.Expression(cppcode=code) |
| 254 |
|
| 255 |
# Connect compiled dolfin::Expression object with dolfin::Function instances |
| 256 |
for name, value in members.items(): |
| 257 |
setattr(dexpr, name, value) |
| 258 |
|
| 259 |
# Evaluate and assert compiled value! |
| 260 |
for x, v in expected_values: |
| 261 |
u = dexpr(x) # TODO: Not sure if this works |
| 262 |
if not hasattr(u, '__len__'): |
| 263 |
u = (u,) |
| 264 |
diff = numpy.array(u) - numpy.array(v) |
| 265 |
if numpy.dot(diff, diff) > 1e-13: |
| 266 |
print("u =", u) |
| 267 |
print("v =", v) |
| 268 |
print("diff =", diff) |
| 269 |
assert numpy.dot(diff, diff) < 1e-14 |
| 270 |
|
| 271 |
def test_dolfin_expression_compilation_of_scalar_literal(): |
| 272 |
# Define some literal ufl expression |
| 273 |
uexpr = as_ufl(3.14) |
| 274 |
|
| 275 |
# Define expected output from compilation |
| 276 |
expected_lines = ['double s[1];', |
| 277 |
's[0] = 3.14;', |
| 278 |
'values[0] = s[0];'] |
| 279 |
|
| 280 |
# Define expected evaluation values: [(x,value), (x,value), ...] |
| 281 |
expected_values = [((0.0, 0.0), (3.14,)), |
| 282 |
((0.6, 0.7), (3.14,)), |
| 283 |
] |
| 284 |
|
| 285 |
# Execute all tests |
| 286 |
check_dolfin_expression_compilation(uexpr, expected_lines, expected_values) |
| 287 |
|
| 288 |
def test_dolfin_expression_compilation_of_vector_literal(): |
| 289 |
# Define some literal ufl expression |
| 290 |
uexpr = ufl.as_vector((1.23, 7.89)) |
| 291 |
|
| 292 |
# Define expected output from compilation |
| 293 |
expected_lines = ['double s[2];', |
| 294 |
's[0] = 1.23;', |
| 295 |
's[1] = 7.89;', |
| 296 |
'values[0] = s[0];', |
| 297 |
'values[1] = s[1];'] |
| 298 |
|
| 299 |
# Define expected evaluation values: [(x,value), (x,value), ...] |
| 300 |
expected_values = [((0.0, 0.0), (1.23, 7.89)), |
| 301 |
((0.6, 0.7), (1.23, 7.89)), |
| 302 |
] |
| 303 |
|
| 304 |
# Execute all tests |
| 305 |
check_dolfin_expression_compilation(uexpr, expected_lines, expected_values) |
| 306 |
|
| 307 |
def test_dolfin_expression_compilation_of_x(): |
| 308 |
# Define some ufl expression |
| 309 |
x = ufl.SpatialCoordinate(ufl.triangle) |
| 310 |
uexpr = 2*x |
| 311 |
|
| 312 |
# Define expected output from compilation |
| 313 |
expected_lines = ['double s[2];', |
| 314 |
's[0] = 2 * x[0];', |
| 315 |
's[1] = 2 * x[1];', |
| 316 |
'values[0] = s[0];', |
| 317 |
'values[1] = s[1];'] |
| 318 |
|
| 319 |
# Define expected evaluation values: [(x,value), (x,value), ...] |
| 320 |
expected_values = [((0.0, 0.0), (0.0, 0.0)), |
| 321 |
((0.6, 0.7), (1.2, 1.4)), |
| 322 |
] |
| 323 |
|
| 324 |
# Execute all tests |
| 325 |
check_dolfin_expression_compilation(uexpr, expected_lines, expected_values) |
| 326 |
|
| 327 |
def test_dolfin_expression_compilation_of_coefficient(dolfin): |
| 328 |
|
| 329 |
# Define some ufl expression with PyDOLFIN coefficients |
| 330 |
mesh = dolfin.UnitSquareMesh(3, 3) |
| 331 |
# Using quadratic element deliberately for accuracy |
| 332 |
V = dolfin.FunctionSpace(mesh, "CG", 2) |
| 333 |
u = dolfin.Function(V) |
| 334 |
u.interpolate(dolfin.Expression("x[0]*x[1]")) |
| 335 |
uexpr = 2*u |
| 336 |
|
| 337 |
# Define expected output from compilation |
| 338 |
expected_lines = ['double s[1];', |
| 339 |
'Array<double> v_w0(1);', |
| 340 |
'w0->eval(v_w0, x);', |
| 341 |
's[0] = 2 * v_w0[0];', |
| 342 |
'values[0] = s[0];'] |
| 343 |
|
| 344 |
# Define expected evaluation values: [(x,value), (x,value), ...] |
| 345 |
expected_values = [((0.0, 0.0), (0.0,)), |
| 346 |
((0.6, 0.7), (2*0.6*0.7,)), |
| 347 |
] |
| 348 |
|
| 349 |
# Execute all tests |
| 350 |
check_dolfin_expression_compilation(uexpr, expected_lines, expected_values, members={'w0':u}) |
| 351 |
|
| 352 |
def test_dolfin_expression_compilation_of_algebraic_operators(dolfin): |
| 353 |
|
| 354 |
# Define some PyDOLFIN coefficients |
| 355 |
mesh = dolfin.UnitSquareMesh(3, 3) |
| 356 |
# Using quadratic element deliberately for accuracy |
| 357 |
V = dolfin.FunctionSpace(mesh, "CG", 2) |
| 358 |
u = dolfin.Function(V) |
| 359 |
u.interpolate(dolfin.Expression("x[0]*x[1]")) |
| 360 |
|
| 361 |
# Define ufl expression with algebraic operators |
| 362 |
uexpr = (2*u + 3.14*u**2) / 4 |
| 363 |
|
| 364 |
# Define expected output from compilation |
| 365 |
# TODO: Test with and without variables |
| 366 |
# Fully split version: |
| 367 |
expected_lines = ['double s[4];', |
| 368 |
'Array<double> v_w0(1);', |
| 369 |
'w0->eval(v_w0, x);', |
| 370 |
's[0] = 2 * v_w0[0];', |
| 371 |
's[1] = pow(v_w0[0], 2);', |
| 372 |
's[2] = 3.14 * s[1];', |
| 373 |
's[3] = s[2] / 4;', |
| 374 |
'values[0] = s[3];'] |
| 375 |
# Oneliner version (no reuse in this expression): |
| 376 |
expected_lines = ['double s[1];', |
| 377 |
'Array<double> v_w0(1);', |
| 378 |
'w0->eval(v_w0, x);', |
| 379 |
's[0] = (3.14 * pow(v_w0[0], 2) + 2 * v_w0[0]) / 4;', |
| 380 |
'values[0] = s[0];'] |
| 381 |
|
| 382 |
# Define expected evaluation values: [(x,value), (x,value), ...] |
| 383 |
x, y = 0.6, 0.7; expected = (2*x*y + 3.14*(x*y)**2) / 4 |
| 384 |
expected_values = [((0.0, 0.0), (0.0,)), |
| 385 |
((x, y), (expected,)), |
| 386 |
] |
| 387 |
|
| 388 |
# Execute all tests |
| 389 |
check_dolfin_expression_compilation(uexpr, expected_lines, expected_values, members={'w0':u}) |
| 390 |
|
| 391 |
|
| 392 |
def test_dolfin_expression_compilation_of_math_functions(dolfin): |
| 393 |
|
| 394 |
# Define some PyDOLFIN coefficients |
| 395 |
mesh = dolfin.UnitSquareMesh(3, 3) |
| 396 |
# Using quadratic element deliberately for accuracy |
| 397 |
V = dolfin.FunctionSpace(mesh, "CG", 2) |
| 398 |
u = dolfin.Function(V) |
| 399 |
u.interpolate(dolfin.Expression("x[0]*x[1]")) |
| 400 |
w0 = u |
| 401 |
|
| 402 |
# Define ufl expression with math functions |
| 403 |
v = abs(ufl.cos(u))/2 + 0.02 |
| 404 |
uexpr = ufl.sin(u) + ufl.tan(v) + ufl.exp(u) + ufl.ln(v) + ufl.atan(v) + ufl.acos(v) + ufl.asin(v) |
| 405 |
|
| 406 |
#print dolfin.assemble(uexpr**2*dolfin.dx, mesh=mesh) # 11.7846508409 |
| 407 |
|
| 408 |
# Define expected output from compilation |
| 409 |
ucode = 'v_w0[0]' |
| 410 |
vcode = '0.02 + fabs(cos(v_w0[0])) / 2' |
| 411 |
funcs = 'asin(%(v)s) + (acos(%(v)s) + (atan(%(v)s) + (log(%(v)s) + (exp(%(u)s) + (sin(%(u)s) + tan(%(v)s))))))' |
| 412 |
oneliner = funcs % {'u':ucode, 'v':vcode} |
| 413 |
|
| 414 |
# Oneliner version (ignoring reuse): |
| 415 |
expected_lines = ['double s[1];', |
| 416 |
'Array<double> v_w0(1);', |
| 417 |
'w0->eval(v_w0, x);', |
| 418 |
's[0] = %s;' % oneliner, |
| 419 |
'values[0] = s[0];'] |
| 420 |
#cppcode = format_dolfin_expression(classname="DebugExpression", shape=(), eval_body=expected_lines) |
| 421 |
#print '-'*100 |
| 422 |
#print cppcode |
| 423 |
#print '-'*100 |
| 424 |
#dolfin.plot(dolfin.Expression(cppcode=cppcode, mesh=mesh)) |
| 425 |
#dolfin.interactive() |
| 426 |
|
| 427 |
# Split version (handles reuse of v, no other reuse): |
| 428 |
expected_lines = ['double s[2];', |
| 429 |
'Array<double> v_w0(1);', |
| 430 |
'w0->eval(v_w0, x);', |
| 431 |
's[0] = %s;' % (vcode,), |
| 432 |
's[1] = %s;' % (funcs % {'u':ucode,'v':'s[0]'},), |
| 433 |
'values[0] = s[1];'] |
| 434 |
|
| 435 |
# Define expected evaluation values: [(x,value), (x,value), ...] |
| 436 |
import math |
| 437 |
x, y = 0.6, 0.7 |
| 438 |
u = x*y |
| 439 |
v = abs(math.cos(u))/2 + 0.02 |
| 440 |
v0 = .52 |
| 441 |
expected0 = math.tan(v0) + 1 + math.log(v0) + math.atan(v0) + math.acos(v0) + math.asin(v0) |
| 442 |
expected = math.sin(u) + math.tan(v) + math.exp(u) + math.log(v) + math.atan(v) + math.acos(v) + math.asin(v) |
| 443 |
expected_values = [((0.0, 0.0), (expected0,)), |
| 444 |
((x, y), (expected,)), |
| 445 |
] |
| 446 |
|
| 447 |
# Execute all tests |
| 448 |
check_dolfin_expression_compilation(uexpr, expected_lines, expected_values, members={'w0':w0}) |
| 449 |
|
| 450 |
def test_dolfin_expression_compilation_of_dot_with_index_notation(dolfin): |
| 451 |
|
| 452 |
# Define some PyDOLFIN coefficients |
| 453 |
mesh = dolfin.UnitSquareMesh(3, 3) |
| 454 |
# Using quadratic element deliberately for accuracy |
| 455 |
V = dolfin.VectorFunctionSpace(mesh, "CG", 2) |
| 456 |
u = dolfin.Function(V) |
| 457 |
u.interpolate(dolfin.Expression(("x[0]", "x[1]"))) |
| 458 |
|
| 459 |
# Define ufl expression with the simplest possible index notation |
| 460 |
uexpr = ufl.dot(u, u) # Needs expand_compounds, not testing here |
| 461 |
|
| 462 |
uexpr = u[0]*u[0] + u[1]*u[1] # Works |
| 463 |
|
| 464 |
i, = ufl.indices(1) |
| 465 |
uexpr = u[i]*u[i] # Works |
| 466 |
|
| 467 |
# Define expected output from compilation |
| 468 |
# Oneliner version (no reuse in this expression): |
| 469 |
xc, yc = ('v_w0[0]', 'v_w0[1]') |
| 470 |
expected_lines = ['double s[1];', |
| 471 |
'Array<double> v_w0(2);', |
| 472 |
'w0->eval(v_w0, x);', |
| 473 |
's[0] = pow(%(x)s, 2) + pow(%(y)s, 2);' % {'x':xc, 'y':yc}, |
| 474 |
'values[0] = s[0];'] |
| 475 |
|
| 476 |
# Define expected evaluation values: [(x,value), (x,value), ...] |
| 477 |
x, y = (0.6, 0.7) |
| 478 |
expected = (x*x+y*y) |
| 479 |
expected_values = [((0.0, 0.0), (0.0,)), |
| 480 |
((x, y), (expected,)), |
| 481 |
] |
| 482 |
|
| 483 |
# Execute all tests |
| 484 |
check_dolfin_expression_compilation(uexpr, expected_lines, expected_values, members={'w0':u}) |
| 485 |
|
| 486 |
def test_dolfin_expression_compilation_of_index_notation(dolfin): |
| 487 |
|
| 488 |
# Define some PyDOLFIN coefficients |
| 489 |
mesh = dolfin.UnitSquareMesh(3, 3) |
| 490 |
# Using quadratic element deliberately for accuracy |
| 491 |
V = dolfin.VectorFunctionSpace(mesh, "CG", 2) |
| 492 |
u = dolfin.Function(V) |
| 493 |
u.interpolate(dolfin.Expression(("x[0]", "x[1]"))) |
| 494 |
|
| 495 |
# Define ufl expression with index notation |
| 496 |
i, j = ufl.indices(2) |
| 497 |
uexpr = ((u[0]*u + u[1]*u)[i]*u[j])*(u[i]*u[j]) |
| 498 |
|
| 499 |
# Define expected output from compilation |
| 500 |
# Split version (reusing all product terms correctly!): |
| 501 |
xc, yc = ('v_w0[0]', 'v_w0[1]') |
| 502 |
svars = {'a':'s[2]', 'b':'s[4]', 'xx':'s[0]', 'yy':'s[3]', 'xy':'s[1]', 'x':xc, 'y':yc} |
| 503 |
expected_lines = ['double s[6];', |
| 504 |
'Array<double> v_w0(2);', |
| 505 |
'w0->eval(v_w0, x);', |
| 506 |
's[0] = pow(%(x)s, 2);' % svars, |
| 507 |
's[1] = %(x)s * %(y)s;' % svars, |
| 508 |
's[2] = %(xx)s + %(xy)s;' % svars, |
| 509 |
's[3] = pow(%(y)s, 2);' % svars, |
| 510 |
's[4] = %(yy)s + %(xy)s;' % svars, |
| 511 |
's[5] = (%(xx)s * (%(x)s * %(a)s) + %(xy)s * (%(x)s * %(b)s)) + (%(yy)s * (%(y)s * %(b)s) + %(xy)s * (%(y)s * %(a)s));' % svars, |
| 512 |
'values[0] = s[5];'] |
| 513 |
# Making the above line correct was a nightmare because of operator sorting in ufl... Hoping it stays stable! |
| 514 |
|
| 515 |
# Define expected evaluation values: [(x,value), (x,value), ...] |
| 516 |
x, y = (0.6, 0.7) |
| 517 |
a = x*x+y*x |
| 518 |
b = x*y+y*y |
| 519 |
expected = (a*x)*(x*x) + (a*y)*(x*y) + (b*x)*(y*x) + (b*y)*(y*y) |
| 520 |
expected_values = [((0.0, 0.0), (0.0,)), |
| 521 |
((x, y), (expected,)), |
| 522 |
] |
| 523 |
|
| 524 |
# Execute all tests |
| 525 |
check_dolfin_expression_compilation(uexpr, expected_lines, expected_values, members={'w0':u}) |