test_core.py 5.05 KB
Newer Older
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
1
2
3
4
5
import tfields
import numpy as np
from sympy.abc import x, y
import unittest
from tempfile import NamedTemporaryFile
6
import pickle
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
7
8
9
10


import sympy  # NOQA: F401

11
ATOL = 1e-8
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
12

13

14
class Base_Check(object):
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
15
    """
16
    Testing derivatives of Tensors
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
17
18
19
20
    """
    _inst = None

    def test_self_equality(self):
21
        # Test equality
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
22
23
24
        self.assertTrue(self._inst.equal(self._inst))

    def test_cylinderTrafo(self):
25
        # Test coordinate transformations in circle
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
26
27
        transformer = self._inst.copy()
        transformer.transform(tfields.bases.CYLINDER)
28
        self.assertTrue(tfields.Tensors(self._inst).equal(transformer, atol=ATOL))
29
        self.assertTrue(self._inst.equal(transformer, atol=ATOL))
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
30
31
32
33
        if len(self._inst) > 0:
            self.assertFalse(np.array_equal(self._inst, transformer))

        transformer.transform(tfields.bases.CARTESIAN)
34
        self.assertTrue(self._inst.equal(transformer, atol=ATOL))
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
35
36

    def test_spericalTrafo(self):
37
        # Test coordinate transformations in circle
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
38
39
40
41
        transformer = self._inst.copy()

        transformer.transform(tfields.bases.SPHERICAL)
        transformer.transform(tfields.bases.CARTESIAN)
42
        self.assertTrue(self._inst.equal(transformer, atol=ATOL))
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
43
44
45
46
47
48
49

    def test_basic_merge(self):
        merge_list = [self._inst.copy() for i in range(3)]
        merge_list[0].transform(tfields.bases.CARTESIAN)
        merge_list[1].transform(tfields.bases.CYLINDER)
        merge_list[2].transform(tfields.bases.SPHERICAL)
        obj = type(self._inst).merged(*merge_list)
50
        self.assertTrue(obj.coord_sys == tfields.bases.CARTESIAN)
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
51
52
53
54
        for i in range(len(merge_list)):
            value = np.allclose(merge_list[0],
                                obj[i * len(self._inst): (i + 1) *
                                    len(self._inst)],
55
                                atol=ATOL)
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
56
57
            self.assertTrue(value)

58
        obj_cs = type(self._inst).merged(*merge_list, coord_sys=tfields.bases.CYLINDER)
59
60
61
62
63
64
        for i in range(len(merge_list)):
            value = np.allclose(merge_list[1],
                                obj_cs[i * len(self._inst): (i + 1) *
                                       len(self._inst)],
                                atol=ATOL)
            self.assertTrue(value)
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
65

66
67
68
69
70
71
72
73
74
    def test_pickle(self):
        with NamedTemporaryFile(suffix='.pickle') as out_file:
            pickle.dump(self._inst,
                        out_file)
            out_file.flush()

            reloaded = pickle.load(open(out_file.name, 'rb'))
        self.assertTrue(self._inst.equal(reloaded))

Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
75
76
77
    def tearDown(self):
        del self._inst

Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
78

79
class Tensor_Fields_Check(object):
Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
80
81
82
83
84
85
86
87
88
89
    def test_fields(self):
        # field is of type list
        self.assertTrue(isinstance(self._inst.fields, list))
        self.assertTrue(len(self._inst.fields) == len(self._fields))

        for field, target_field in zip(self._inst.fields, self._fields):
            self.assertTrue(np.array_equal(field, target_field))
            # fields are copied not reffered by a pointer
            self.assertFalse(field is target_field)

90

Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
91
92
93
94
95
"""
EMPTY TESTS
"""


96
class Tensors_Empty_Test(Base_Check, unittest.TestCase):
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
97
98
99
100
    def setUp(self):
        self._inst = tfields.Tensors([], dim=3)


101
class TensorFields_Empty_Test(Tensors_Empty_Test, Tensor_Fields_Check):
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
102
103
104
    def setUp(self):
        self._fields = []
        self._inst = tfields.TensorFields([], dim=3)
105
106
107
108
109
110
111
112


class TensorMaps_Empty_Test(TensorFields_Empty_Test):
    def setUp(self):
        self._fields = []
        self._inst = tfields.TensorMaps([], dim=3)
        self._maps = []
        self._maps_fields = []
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
113
114
115
116
117
    

class TensorFields_Copy_Test(TensorFields_Empty_Test):
    def setUp(self):
        base = [(-5, 5, 11)] * 3
118
        self._fields = [tfields.Tensors.grid(*base, coord_sys='cylinder'),
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
119
120
121
122
                        tfields.Tensors(range(11**3))]
        tensors = tfields.Tensors.grid(*base)
        self._inst = tfields.TensorFields(tensors, *self._fields)

Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
123
124
125
        self.assertTrue(self._fields[0].coord_sys, 'cylinder')
        self.assertTrue(self._fields[1].coord_sys, 'cartesian')

Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
126
127
128
129
130

class TensorMaps_Copy_Test(TensorMaps_Empty_Test):
    def setUp(self):
        base = [(-1, 1, 3)] * 3
        tensors = tfields.Tensors.grid(*base)
131
        self._fields = [tfields.Tensors.grid(*base, coord_sys='cylinder'),
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
                        tfields.Tensors(range(len(tensors)))]
        self._maps_tensors = [[[0, 0, 0],
                               [1, 2, 3],
                               [1, 5, 9]],
                              [[0, 4],
                               [1, 3]],
                              [[42]]]
        self._maps_fields = [[[42., 21., 11]],
                             [[3, 25]],
                             [[111]]]
        self._maps = [tfields.TensorFields(map_tensors,
                                           *map_fields) for map_tensors,
                      map_fields in zip(self._maps_tensors, self._maps_fields)]
        self._inst = tfields.TensorMaps(tensors, *self._fields,
                                        maps=self._maps)


if __name__ == '__main__':
    unittest.main()