test_core.py 5.25 KB
Newer Older
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
1
2
3
4
import tfields
import numpy as np
import unittest
from tempfile import NamedTemporaryFile
5
import pickle
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
42
        # create 3 copies with different coord_sys
        from IPython import embed; embed()
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
43
44
45
46
        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)
Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
47
48
        
        # merge them and check that the first coord_sys is taken
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
49
        obj = type(self._inst).merged(*merge_list)
50
        self.assertTrue(obj.coord_sys == tfields.bases.CARTESIAN)
Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
51
52
        
        # check that all copies are the same also with new coord_sys
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
53
54
55
56
        for i in range(len(merge_list)):
            value = np.allclose(merge_list[0],
                                obj[i * len(self._inst): (i + 1) *
                                    len(self._inst)],
57
                                atol=ATOL)
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
58
59
            self.assertTrue(value)

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

68
69
70
71
72
    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
73
74
75
            out_file.seek(0)

            reloaded = pickle.load(out_file)
76
77
78

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

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

Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
82

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

94

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


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


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


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
117
118
119
120
121
    

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

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

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