test_core.py 5.11 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

Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Base_Test(object):
    """
    Testing derivatives of Points3D
    """
    _inst = None

    def test_self_equality(self):
        """
        Test equality
        """
        self.assertTrue(self._inst.equal(self._inst))

    def test_cylinderTrafo(self):
        """
        Test coordinate transformations in circle
        """
        transformer = self._inst.copy()
        transformer.transform(tfields.bases.CYLINDER)
32
        self.assertTrue(tfields.Tensors(self._inst).equal(transformer, atol=ATOL))
33
        self.assertTrue(self._inst.equal(transformer, atol=ATOL))
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
34
35
36
37
        if len(self._inst) > 0:
            self.assertFalse(np.array_equal(self._inst, transformer))

        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
41
42
43
44
45
46
47

    def test_spericalTrafo(self):
        """
        Test coordinate transformations in circle
        """
        transformer = self._inst.copy()

        transformer.transform(tfields.bases.SPHERICAL)
        transformer.transform(tfields.bases.CARTESIAN)
48
        self.assertTrue(self._inst.equal(transformer, atol=ATOL))
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
49
50
51
52
53
54
55

    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)
56
        self.assertTrue(obj.coord_sys == tfields.bases.CARTESIAN)
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
57
58
59
60
        for i in range(len(merge_list)):
            value = np.allclose(merge_list[0],
                                obj[i * len(self._inst): (i + 1) *
                                    len(self._inst)],
61
                                atol=ATOL)
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
62
63
            self.assertTrue(value)

64
        obj_cs = type(self._inst).merged(*merge_list, coord_sys=tfields.bases.CYLINDER)
65
66
67
68
69
70
        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
71

72
73
74
75
76
77
78
79
80
    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
81
82
83
    def tearDown(self):
        del self._inst

Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
84
85
86
87
88
89
90
91
92
93
94
95

class Tensor_Fields_Test(object):
    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)

96

Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
97
98
99
100
101
102
103
104
105
106
"""
EMPTY TESTS
"""


class Tensors_Empty_Test(Base_Test, unittest.TestCase):
    def setUp(self):
        self._inst = tfields.Tensors([], dim=3)


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


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
119
120
121
122
123
    

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

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

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