test_core.py 5.19 KB
Newer Older
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
1
2
import numpy as np
from tempfile import NamedTemporaryFile
3
import pickle
4
5
import unittest
import tfields
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
6

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

9

10
class Base_Check(object):
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
11
    """
12
    Testing derivatives of Tensors
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
13
14
15
16
    """
    _inst = None

    def test_self_equality(self):
17
        # Test equality
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
18
19
20
        self.assertTrue(self._inst.equal(self._inst))

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

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

    def test_spericalTrafo(self):
33
        # Test coordinate transformations in circle
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
34
35
36
37
        transformer = self._inst.copy()

        transformer.transform(tfields.bases.SPHERICAL)
        transformer.transform(tfields.bases.CARTESIAN)
38
        self.assertTrue(self._inst.equal(transformer, atol=ATOL))
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
39
40

    def test_basic_merge(self):
Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
41
        # create 3 copies with different coord_sys
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
42
43
44
45
        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)
46

Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
47
        # merge them and check that the first coord_sys is taken
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
48
        obj = type(self._inst).merged(*merge_list)
49
        self.assertTrue(obj.coord_sys == tfields.bases.CARTESIAN)
50

Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
51
        # check that all copies are the same also with new coord_sys
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
52
53
54
55
        for i in range(len(merge_list)):
            value = np.allclose(merge_list[0],
                                obj[i * len(self._inst): (i + 1) *
                                    len(self._inst)],
56
                                atol=ATOL)
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
57
58
            self.assertTrue(value)

59
        obj_cs = type(self._inst).merged(*merge_list, coord_sys=tfields.bases.CYLINDER)
60
61
62
63
64
65
        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
66

67
68
69
70
71
    def test_pickle(self):
        with NamedTemporaryFile(suffix='.pickle') as out_file:
            pickle.dump(self._inst,
                        out_file)
            out_file.flush()
Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
72
73
74
            out_file.seek(0)

            reloaded = pickle.load(out_file)
75
76
77

        self.assertTrue(self._inst.equal(reloaded))

Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
78
79
80
    def tearDown(self):
        del self._inst

Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
81

82
class Tensor_Fields_Check(object):
Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
83
84
85
86
87
88
89
90
91
92
    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)

93

Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
94
95
96
97
98
"""
EMPTY TESTS
"""


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


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


class TensorMaps_Empty_Test(TensorFields_Empty_Test):
    def setUp(self):
        self._fields = []
        self._inst = tfields.TensorMaps([], dim=3)
        self._maps = []
        self._maps_fields = []
116

Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
117
118
119
120

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

Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
126
127
128
        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
129
130
131
132
133

class TensorMaps_Copy_Test(TensorMaps_Empty_Test):
    def setUp(self):
        base = [(-1, 1, 3)] * 3
        tensors = tfields.Tensors.grid(*base)
134
        self._fields = [tfields.Tensors.grid(*base, coord_sys='cylinder'),
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
                        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()