test_field.py 6.01 KB
Newer Older
1
import numpy as np
Martin Bauer's avatar
Martin Bauer committed
2
import pytest
3
import sympy as sp
Martin Bauer's avatar
Martin Bauer committed
4

5
import pystencils as ps
6
from pystencils.field import Field, FieldType, layout_string_to_tuple
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30


def test_field_basic():
    f = Field.create_generic('f', spatial_dimensions=2)
    assert FieldType.is_generic(f)
    assert f['E'] == f[1, 0]
    assert f['N'] == f[0, 1]
    assert '_' in f.center._latex('dummy')

    f = Field.create_fixed_size('f', (10, 10), strides=(80, 8), dtype=np.float64)
    assert f.spatial_strides == (10, 1)
    assert f.index_strides == ()
    assert f.center_vector == sp.Matrix([f.center])

    f = Field.create_fixed_size('f', (8, 8, 2, 2), index_dimensions=2)
    assert f.center_vector == sp.Matrix([[f(0, 0), f(0, 1)],
                                         [f(1, 0), f(1, 1)]])
    field_access = f[1, 1]
    assert field_access.nr_of_coordinates == 2
    assert field_access.offset_name == 'NE'
    neighbor = field_access.neighbor(coord_id=0, offset=-2)
    assert neighbor.offsets == (-1, 1)
    assert '_' in neighbor._latex('dummy')

31 32 33
    f = Field.create_fixed_size('f', (8, 8, 2, 2, 2), index_dimensions=3)
    assert f.center_vector == sp.Matrix([[[f(i, j, k) for k in range(2)] for j in range(2)] for i in range(2)])

34 35 36 37 38 39 40 41 42 43 44
    f = Field.create_generic('f', spatial_dimensions=5, index_dimensions=2)
    field_access = f[1, -1, 2, -3, 0](1, 0)
    assert field_access.offsets == (1, -1, 2, -3, 0)
    assert field_access.index == (1, 0)


def test_error_handling():
    struct_dtype = np.dtype([('a', np.int32), ('b', np.float64), ('c', np.uint32)])
    Field.create_generic('f', spatial_dimensions=2, index_dimensions=0, dtype=struct_dtype)
    with pytest.raises(ValueError) as e:
        Field.create_generic('f', spatial_dimensions=2, index_dimensions=1, dtype=struct_dtype)
45
    assert 'index dimension' in str(e.value)
46 47 48 49 50

    arr = np.array([[1, 2.0, 3], [1, 2.0, 3]], dtype=struct_dtype)
    Field.create_from_numpy_array('f', arr, index_dimensions=0)
    with pytest.raises(ValueError) as e:
        Field.create_from_numpy_array('f', arr, index_dimensions=1)
51
    assert 'Structured arrays' in str(e.value)
52 53 54 55 56

    arr = np.zeros([3, 3, 3])
    Field.create_from_numpy_array('f', arr, index_dimensions=2)
    with pytest.raises(ValueError) as e:
        Field.create_from_numpy_array('f', arr, index_dimensions=3)
57
    assert 'Too many' in str(e.value)
58 59 60 61

    Field.create_fixed_size('f', (3, 2, 4), index_dimensions=0, dtype=struct_dtype, layout='reverse_numpy')
    with pytest.raises(ValueError) as e:
        Field.create_fixed_size('f', (3, 2, 4), index_dimensions=1, dtype=struct_dtype, layout='reverse_numpy')
62
    assert 'Structured arrays' in str(e.value)
63 64 65 66

    f = Field.create_fixed_size('f', (10, 10))
    with pytest.raises(ValueError) as e:
        f[1]
67
    assert 'Wrong number of spatial indices' in str(e.value)
68 69 70 71

    f = Field.create_generic('f', spatial_dimensions=2, index_shape=(3,))
    with pytest.raises(ValueError) as e:
        f(3)
72
    assert 'out of bounds' in str(e.value)
73 74 75 76

    f = Field.create_fixed_size('f', (10, 10, 3, 4), index_dimensions=2)
    with pytest.raises(ValueError) as e:
        f(3, 0)
77
    assert 'out of bounds' in str(e.value)
78 79 80

    with pytest.raises(ValueError) as e:
        f(1, 0)(1, 0)
81
    assert 'Indexing an already indexed' in str(e.value)
82 83 84

    with pytest.raises(ValueError) as e:
        f(1)
85
    assert 'Wrong number of indices' in str(e.value)
86 87 88

    with pytest.raises(ValueError) as e:
        Field.create_generic('f', spatial_dimensions=2, layout='wrong')
89
    assert 'Unknown layout descriptor' in str(e.value)
90 91 92 93

    assert layout_string_to_tuple('fzyx', dim=4) == (3, 2, 1, 0)
    with pytest.raises(ValueError) as e:
        layout_string_to_tuple('wrong', dim=4)
94
    assert 'Unknown layout descriptor' in str(e.value)
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


def test_decorator_scoping():
    dst = ps.fields('dst : double[2D]')

    def f1():
        a = sp.Symbol("a")

        def f2():
            b = sp.Symbol("b")

            @ps.kernel
            def decorated_func():
                dst[0, 0] @= a + b

            return decorated_func

        return f2

    assert f1()(), ps.Assignment(dst[0, 0], sp.Symbol("a") + sp.Symbol("b"))


def test_string_creation():
    x, y, z = ps.fields('  x(4),    y(3,5) z : double[  3,  47]')
    assert x.index_shape == (4,)
    assert y.index_shape == (3, 5)
    assert z.spatial_shape == (3, 47)
122 123 124 125 126 127 128 129 130 131 132


def test_itemsize():

    x = ps.fields('x: float32[1d]')
    y = ps.fields('y:  float64[2d]')
    i = ps.fields('i:  int16[1d]')

    assert x.itemsize == 4
    assert y.itemsize == 8
    assert i.itemsize == 2
Michael Kuron's avatar
Michael Kuron committed
133 134 135 136


def test_staggered():

137
    # D2Q5
138
    j1, j2, j3 = ps.fields('j1(2), j2(2,2), j3(2,2,2) : double[2D]', field_type=FieldType.STAGGERED)
Michael Kuron's avatar
Michael Kuron committed
139 140

    assert j1[0, 1](1) == j1.staggered_access((0, sp.Rational(1, 2)))
141 142
    assert j1[1, 1](1) == j1.staggered_access((1, sp.Rational(1, 2)))
    assert j1[0, 2](1) == j1.staggered_access((0, sp.Rational(3, 2)))
Michael Kuron's avatar
Michael Kuron committed
143
    assert j1[0, 1](1) == j1.staggered_access("N")
144
    assert j1[0, 0](1) == j1.staggered_access("S")
145
    assert j1.staggered_vector_access("N") == sp.Matrix([j1.staggered_access("N")])
Michael Kuron's avatar
Michael Kuron committed
146 147 148

    assert j2[0, 1](1, 1) == j2.staggered_access((0, sp.Rational(1, 2)), 1)
    assert j2[0, 1](1, 1) == j2.staggered_access("N", 1)
149
    assert j2.staggered_vector_access("N") == sp.Matrix([j2.staggered_access("N", 0), j2.staggered_access("N", 1)])
Michael Kuron's avatar
Michael Kuron committed
150 151 152

    assert j3[0, 1](1, 1, 1) == j3.staggered_access((0, sp.Rational(1, 2)), (1, 1))
    assert j3[0, 1](1, 1, 1) == j3.staggered_access("N", (1, 1))
153 154
    assert j3.staggered_vector_access("N") == sp.Matrix([[j3.staggered_access("N", (i, j))
                                                        for j in range(2)] for i in range(2)])
155 156 157 158 159 160

    # D2Q9
    k = ps.fields('k(4) : double[2D]', field_type=FieldType.STAGGERED)

    assert k[1, 1](2) == k.staggered_access("NE")
    assert k[0, 0](2) == k.staggered_access("SW")
161 162

    assert k[0, 0](3) == k.staggered_access("NW")
163 164 165 166 167

    # sign reversed when using as flux field
    r = ps.fields('r(2) : double[2D]', field_type=FieldType.STAGGERED_FLUX)
    assert r[0, 0](0) == r.staggered_access("W")
    assert -r[1, 0](0) == r.staggered_access("E")