1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
|
__authors__ = "Martin Sandve Alnæs"
__date__ = "2009-02-13 -- 2009-02-13"
import math
import numpy as np
import pytest
from utils import LagrangeElement
from ufl import (
Argument,
Coefficient,
FunctionSpace,
Identity,
Mesh,
SpatialCoordinate,
as_matrix,
as_vector,
cos,
cross,
det,
dev,
dot,
exp,
i,
indices,
inner,
j,
ln,
outer,
sin,
skew,
sqrt,
sym,
tan,
tetrahedron,
tr,
triangle,
)
from ufl.constantvalue import ConstantValue, as_ufl
class CustomConstant(ConstantValue):
def __init__(self, value):
super().__init__()
self._value = value
@property
def ufl_shape(self):
return ()
def evaluate(self, x, mapping, component, index_values):
return self._value
def __repr__(self):
return f"CustomConstant({self._value})"
def testScalars():
s = as_ufl(123)
e = s((5, 7))
v = 123
assert e == v
def testZero():
s = as_ufl(0)
e = s((5, 7))
v = 0
assert e == v
def testIdentity():
cell = triangle
ident = Identity(cell.topological_dimension())
s = 123 * ident[0, 0]
e = s((5, 7))
v = 123
assert e == v
s = 123 * ident[1, 0]
e = s((5, 7))
v = 0
assert e == v
def testCoords():
cell = triangle
domain = Mesh(LagrangeElement(cell, 1, (2,)))
x = SpatialCoordinate(domain)
s = x[0] + x[1]
e = s((5, 7))
v = 5 + 7
assert e == pytest.approx(v)
def testFunction1():
cell = triangle
element = LagrangeElement(cell, 1)
domain = Mesh(LagrangeElement(cell, 1, (2,)))
space = FunctionSpace(domain, element)
f = Coefficient(space)
s = 3 * f
e = s((5, 7), {f: 123})
v = 3 * 123
assert e == pytest.approx(v)
def testFunction2():
cell = triangle
element = LagrangeElement(cell, 1)
domain = Mesh(LagrangeElement(cell, 1, (2,)))
space = FunctionSpace(domain, element)
f = Coefficient(space)
def g(x):
return x[0]
s = 3 * f
e = s((5, 7), {f: g})
v = 3 * 5
assert e == v
def testArgument2():
cell = triangle
element = LagrangeElement(cell, 1)
domain = Mesh(LagrangeElement(cell, 1, (2,)))
space = FunctionSpace(domain, element)
f = Argument(space, 2)
def g(x):
return x[0]
s = 3 * f
e = s((5, 7), {f: g})
v = 3 * 5
assert e == v
def testAlgebra():
cell = triangle
domain = Mesh(LagrangeElement(cell, 1, (2,)))
x = SpatialCoordinate(domain)
s = 3 * (x[0] + x[1]) - 7 + x[0] ** (x[1] / 2)
e = s((5, 7))
v = 3 * (5.0 + 7.0) - 7 + 5.0 ** (7.0 / 2)
assert e == v
def testConstant():
"""Test that constant division doesn't discard the complex type in the case the value is
a numpy complex type, not a native python complex type.
"""
_a = np.complex128(1 + 1j)
_b = np.complex128(-3 + 2j)
a = CustomConstant(_a)
b = CustomConstant(_b)
expr = a / b
e = expr(())
expected = complex(_a) / complex(_b)
assert e == pytest.approx(expected)
def testIndexSum():
cell = triangle
domain = Mesh(LagrangeElement(cell, 1, (2,)))
x = SpatialCoordinate(domain)
(i,) = indices(1)
s = x[i] * x[i]
e = s((5, 7))
v = 5**2 + 7**2
assert e == pytest.approx(v)
def testIndexSum2():
cell = triangle
domain = Mesh(LagrangeElement(cell, 1, (2,)))
x = SpatialCoordinate(domain)
ident = Identity(cell.topological_dimension())
i, j = indices(2)
s = (x[i] * x[j]) * ident[i, j]
e = s((5, 7))
# v = sum_i sum_j x_i x_j delta_ij = x_0 x_0 + x_1 x_1
v = 5**2 + 7**2
assert e == pytest.approx(v)
def testMathFunctions():
domain = Mesh(LagrangeElement(triangle, 1, (2,)))
x = SpatialCoordinate(domain)[0]
s = sin(x)
e = s((5, 7))
v = math.sin(5)
assert e == v
s = cos(x)
e = s((5, 7))
v = math.cos(5)
assert e == pytest.approx(v)
s = tan(x)
e = s((5, 7))
v = math.tan(5)
assert e == pytest.approx(v)
s = ln(x)
e = s((5, 7))
v = math.log(5)
assert e == pytest.approx(v)
s = exp(x)
e = s((5, 7))
v = math.exp(5)
assert e == pytest.approx(v)
s = sqrt(x)
e = s((5, 7))
v = math.sqrt(5)
assert e == pytest.approx(v)
def testListTensor():
domain = Mesh(LagrangeElement(triangle, 1, (2,)))
x, y = SpatialCoordinate(domain)
m = as_matrix([[x, y], [-y, -x]])
s = m[0, 0] + m[1, 0] + m[0, 1] + m[1, 1]
e = s((5, 7))
v = 0
assert e == pytest.approx(v)
s = m[0, 0] * m[1, 0] * m[0, 1] * m[1, 1]
e = s((5, 7))
v = 5**2 * 7**2
assert e == pytest.approx(v)
def testComponentTensor1():
domain = Mesh(LagrangeElement(triangle, 1, (2,)))
x = SpatialCoordinate(domain)
m = as_vector(x[i], i)
s = m[0] * m[1]
e = s((5, 7))
v = 5 * 7
assert e == pytest.approx(v)
def testComponentTensor2():
domain = Mesh(LagrangeElement(triangle, 1, (2,)))
x = SpatialCoordinate(domain)
xx = outer(x, x)
m = as_matrix(xx[i, j], (i, j))
s = m[0, 0] + m[1, 0] + m[0, 1] + m[1, 1]
e = s((5, 7))
v = 5 * 5 + 5 * 7 + 5 * 7 + 7 * 7
assert e == pytest.approx(v)
def testComponentTensor3():
domain = Mesh(LagrangeElement(triangle, 1, (2,)))
x = SpatialCoordinate(domain)
xx = outer(x, x)
m = as_matrix(xx[i, j], (i, j))
s = m[0, 0] * m[1, 0] * m[0, 1] * m[1, 1]
e = s((5, 7))
v = 5 * 5 * 5 * 7 * 5 * 7 * 7 * 7
assert e == pytest.approx(v)
def testCoefficient():
V = LagrangeElement(triangle, 1)
domain = Mesh(LagrangeElement(triangle, 1, (2,)))
space = FunctionSpace(domain, V)
f = Coefficient(space)
e = f**2
def eval_f(x):
return x[0] * x[1] ** 2
assert e((3, 7), {f: eval_f}) == pytest.approx((3 * 7**2) ** 2)
def testCoefficientDerivative():
V = LagrangeElement(triangle, 1)
domain = Mesh(LagrangeElement(triangle, 1, (2,)))
space = FunctionSpace(domain, V)
f = Coefficient(space)
e = f.dx(0) ** 2 + f.dx(1) ** 2
def eval_f(x, derivatives):
if not derivatives:
return eval_f.c * x[0] * x[1] ** 2
# assume only first order derivative
(d,) = derivatives
if d == 0:
return eval_f.c * x[1] ** 2
if d == 1:
return eval_f.c * x[0] * 2 * x[1]
# shows how to attach data to eval_f
eval_f.c = 5
assert e((3, 7), {f: eval_f}) == pytest.approx((5 * 7**2) ** 2 + (5 * 3 * 2 * 7) ** 2)
def test_dot():
domain = Mesh(LagrangeElement(triangle, 1, (2,)))
x = SpatialCoordinate(domain)
s = dot(x, 2 * x)
e = s((5, 7))
v = 2 * (5 * 5 + 7 * 7)
assert e == pytest.approx(v)
def test_inner():
domain = Mesh(LagrangeElement(triangle, 1, (2,)))
x = SpatialCoordinate(domain)
xx = as_matrix(((2 * x[0], 3 * x[0]), (2 * x[1], 3 * x[1])))
s = inner(xx, 2 * xx)
e = s((5, 7))
v = 2 * ((5 * 2) ** 2 + (5 * 3) ** 2 + (7 * 2) ** 2 + (7 * 3) ** 2)
assert e == pytest.approx(v)
def test_outer():
domain = Mesh(LagrangeElement(triangle, 1, (2,)))
x = SpatialCoordinate(domain)
xx = outer(outer(x, x), as_vector((2, 3)))
s = inner(xx, 2 * xx)
e = s((5, 7))
v = 2 * (5**2 + 7**2) ** 2 * (2**2 + 3**2)
assert e == pytest.approx(v)
def test_cross():
domain = Mesh(LagrangeElement(tetrahedron, 1, (3,)))
x = SpatialCoordinate(domain)
xv = (3, 5, 7)
# Test cross product of triplets of orthogonal
# vectors, where |a x b| = |a| |b|
ts = [
[as_vector((x[0], 0, 0)), as_vector((0, x[1], 0)), as_vector((0, 0, x[2]))],
[as_vector((x[0], x[1], 0)), as_vector((x[1], -x[0], 0)), as_vector((0, 0, x[2]))],
[as_vector((0, x[0], x[1])), as_vector((0, x[1], -x[0])), as_vector((x[2], 0, 0))],
[as_vector((x[0], 0, x[1])), as_vector((x[1], 0, -x[0])), as_vector((0, x[2], 0))],
]
for t in ts:
for a in range(3):
for b in range(3):
cab = cross(t[a], t[b])
dab = dot(cab, cab)
eab = dab(xv)
tna = dot(t[a], t[a])(xv)
tnb = dot(t[b], t[b])(xv)
vab = tna * tnb if a != b else 0
assert eab == pytest.approx(vab)
def xtest_dev():
domain = Mesh(LagrangeElement(triangle, 1, (2,)))
x = SpatialCoordinate(domain)
xv = (5, 7)
xx = outer(x, x)
s1 = dev(2 * xx)
s2 = 2 * (xx - xx.T) # FIXME
e = inner(s1, s1)(xv)
v = inner(s2, s2)(xv)
assert e == pytest.approx(v)
def test_skew():
domain = Mesh(LagrangeElement(triangle, 1, (2,)))
x = SpatialCoordinate(domain)
xv = (5, 7)
xx = outer(x, x)
s1 = skew(2 * xx)
s2 = xx - xx.T
e = inner(s1, s1)(xv)
v = inner(s2, s2)(xv)
assert e == pytest.approx(v)
def test_sym():
domain = Mesh(LagrangeElement(triangle, 1, (2,)))
x = SpatialCoordinate(domain)
xv = (5, 7)
xx = outer(x, x)
s1 = sym(2 * xx)
s2 = xx + xx.T
e = inner(s1, s1)(xv)
v = inner(s2, s2)(xv)
assert e == pytest.approx(v)
def test_tr():
domain = Mesh(LagrangeElement(triangle, 1, (2,)))
x = SpatialCoordinate(domain)
xv = (5, 7)
xx = outer(x, x)
s = tr(2 * xx)
e = s(xv)
v = 2 * sum(xv[i] ** 2 for i in (0, 1))
assert e == pytest.approx(v)
def test_det2D():
domain = Mesh(LagrangeElement(triangle, 1, (2,)))
x = SpatialCoordinate(domain)
xv = (5, 7)
a, b = 6.5, -4
xx = as_matrix(((x[0], x[1]), (a, b)))
s = det(2 * xx)
e = s(xv)
v = 2**2 * (5 * b - 7 * a)
assert e == pytest.approx(v)
def xtest_det3D(): # FIXME
x = SpatialCoordinate(tetrahedron)
xv = (5, 7, 9)
a, b, c = 6.5, -4, 3
d, e, f = 2, 3, 4
xx = as_matrix(((x[0], x[1], x[2]), (a, b, c), (d, e, f)))
s = det(2 * xx)
e = s(xv)
v = 2**3 * (xv[0] * (b * f - e * c) - xv[1] * (a * f - c * d) + xv[2] * (a * e - b * d))
assert e == pytest.approx(v)
def test_cofac():
pass # TODO
def test_inv():
pass # TODO
|