| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
import pytest |
| 4 |
from ufl_legacy import * |
| 5 |
from ffc.uflacs.analysis.indexing import map_indexed_arg_components |
| 6 |
from ffc.uflacs.analysis.indexing import map_component_tensor_arg_components |
| 7 |
|
| 8 |
def test_map_index_arg_components(): |
| 9 |
x = SpatialCoordinate(triangle) |
| 10 |
i, j, k = indices(3) |
| 11 |
|
| 12 |
# Tensors of rank 1, 2, 3 |
| 13 |
A1 = x |
| 14 |
A2 = outer(x, x) |
| 15 |
A3 = outer(A2, x) |
| 16 |
|
| 17 |
indexed = A1[i] |
| 18 |
assert map_indexed_arg_components(indexed) == [0, 1] |
| 19 |
|
| 20 |
# Rank 2 |
| 21 |
indexed = A2[i, j] |
| 22 |
assert map_indexed_arg_components(indexed) == [0, 1, 2, 3] |
| 23 |
indexed = A2[j, i] |
| 24 |
assert map_indexed_arg_components(indexed) == [0, 2, 1, 3] |
| 25 |
|
| 26 |
# Rank 3 |
| 27 |
indexed = A3[i, j, k] |
| 28 |
assert map_indexed_arg_components(indexed) == [0, 1, 2, 3, |
| 29 |
4, 5, 6, 7] |
| 30 |
indexed = A3[j, i, k] |
| 31 |
assert map_indexed_arg_components(indexed) == [0, 1, 4, 5, |
| 32 |
2, 3, 6, 7] |
| 33 |
indexed = A3[i, k, j] |
| 34 |
assert map_indexed_arg_components(indexed) == [0, 2, 1, 3, |
| 35 |
4, 6, 5, 7] |
| 36 |
indexed = A3[j, k, i] |
| 37 |
assert map_indexed_arg_components(indexed) == [0, 2, 4, 6, |
| 38 |
1, 3, 5, 7] |
| 39 |
indexed = A3[k, i, j] |
| 40 |
assert map_indexed_arg_components(indexed) == [0, 4, 1, 5, |
| 41 |
2, 6, 3, 7] |
| 42 |
indexed = A3[k, j, i] |
| 43 |
assert map_indexed_arg_components(indexed) == [0, 4, 2, 6, |
| 44 |
1, 5, 3, 7] |
| 45 |
# Explanation: |
| 46 |
assert [ii*4+jj*2+kk # strides are determined by relative position of i,j,k |
| 47 |
for kk in range(2) # loop order matches indexing order |
| 48 |
for jj in range(2) # loop range matches index dimensions |
| 49 |
for ii in range(2)] == [0, 4, 2, 6, 1, 5, 3, 7] |
| 50 |
|
| 51 |
|
| 52 |
def test_map_component_tensor_arg_components(): |
| 53 |
x = SpatialCoordinate(triangle) |
| 54 |
i, j, k = indices(3) |
| 55 |
|
| 56 |
# Tensors of rank 1, 2, 3 |
| 57 |
A1 = x |
| 58 |
A2 = outer(x, x) |
| 59 |
A3 = outer(A2, x) |
| 60 |
Aij = A2[i,j] |
| 61 |
|
| 62 |
# Rank 1 |
| 63 |
assert map_component_tensor_arg_components((x[i]*2.0)^(i,)) == [0,1] |
| 64 |
|
| 65 |
# Rank 2 |
| 66 |
assert map_component_tensor_arg_components((A2[i,j]*2.0)^(i,j)) == [0,1,2,3] |
| 67 |
assert map_component_tensor_arg_components((A2[i,j]*2.0)^(j,i)) == [0,2,1,3] |
| 68 |
assert map_component_tensor_arg_components(A2[i,j]^(j,i)) == [0,2,1,3] |
| 69 |
|
| 70 |
# Rank 3 |
| 71 |
assert map_component_tensor_arg_components((A3[i,j,k]*2.0)^(i,j,k)) == [0,1,2,3,4,5,6,7] |
| 72 |
assert map_component_tensor_arg_components((A3[i,j,k]*2.0)^(i,k,j)) == [0,2,1,3,4,6,5,7] |
| 73 |
assert map_component_tensor_arg_components((A3[i,j,k]*2.0)^(k,i,j)) == [0,2,4,6,1,3,5,7] |
| 74 |
|
| 75 |
# Explanation: |
| 76 |
assert [ii*4+jj*2+kk # strides are determined by relative order of i,j,k in indexed expr |
| 77 |
for kk in range(2) # loop order matches un-indexing order in component tensor |
| 78 |
for ii in range(2) # loop range matches index dimensions |
| 79 |
for jj in range(2)] == [0,2,4,6,1,3,5,7] |