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


import sympy  # NOQA: F401

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

12

Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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)
31
        self.assertTrue(self._inst.equal(transformer, atol=ATOL))
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
32
33
34
35
        if len(self._inst) > 0:
            self.assertFalse(np.array_equal(self._inst, transformer))

        transformer.transform(tfields.bases.CARTESIAN)
36
        self.assertTrue(self._inst.equal(transformer, atol=ATOL))
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
37
38
39
40
41
42
43
44
45

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

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

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

        obj_cs = type(self._inst).merged(*merge_list, coordSys=tfields.bases.CYLINDER)
63
64
65
66
67
68
        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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

    def tearDown(self):
        del self._inst

"""
EMPTY TESTS
"""


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


class TensorFields_Empty_Test(Tensors_Empty_Test):
    def setUp(self):
        self._fields = []
        self._inst = tfields.TensorFields([], dim=3)

    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)
    

class TensorFields_Copy_Test(TensorFields_Empty_Test):
    def setUp(self):
        base = [(-5, 5, 11)] * 3
        self._fields = [tfields.Tensors.grid(*base, coordSys='cylinder'),
                        tfields.Tensors(range(11**3))]
        tensors = tfields.Tensors.grid(*base)
        self._inst = tfields.TensorFields(tensors, *self._fields)


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

class TensorMaps_Copy_Test(TensorMaps_Empty_Test):
    def setUp(self):
        base = [(-1, 1, 3)] * 3
        tensors = tfields.Tensors.grid(*base)
        self._fields = [tfields.Tensors.grid(*base, coordSys='cylinder'),
                        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()