test_blockforest.py 3.64 KB
Newer Older
1
import unittest
2
3
import numpy as np
from waLBerla import field, createUniformBlockGrid, AABB
4
5


6
class BlockforestModuleTest(unittest.TestCase):
7
8
9

    def testMemoryManagement1(self):
        """Testing correct reference counting of block data"""
10
        blocks = createUniformBlockGrid(blocks=(1, 1, 1), cellsPerBlock=(2, 2, 2))
Markus Holzer's avatar
Markus Holzer committed
11
        field.addToStorage(blocks, "TestField", np.int64)
12
13
        f = blocks[0]["TestField"]
        strides_before = f.strides
14
        del blocks
15
        # create another block structure - this has triggered segfault
16
        # when previous blockstructure was already freed
17
        blocks = createUniformBlockGrid(blocks=(1, 1, 1), cellsPerBlock=(2, 2, 2))  # noqa: F841
18

19
20
        # The first block structure must exist here, since we hold a reference to block data
        # if it would have been deleted already f.strides should lead to segfault or invalid values
21
        self.assertEqual(strides_before, f.strides)
22

23
    def testMemoryManagement2(self):
24
        """Testing correct reference counting of block data
25
           Holding only a numpy array pointing to a waLBerla field should still hold the blockstructure alive"""
26
        blocks = createUniformBlockGrid(blocks=(1, 1, 1), cellsPerBlock=(2, 2, 2))
Markus Holzer's avatar
Markus Holzer committed
27
        field.addToStorage(blocks, "TestField", np.int64)
28
        npf = field.toArray(blocks[0]["TestField"])
Markus Holzer's avatar
testing    
Markus Holzer committed
29
        npf[:, :, :] = 42
30
        del blocks
31
        # create another block structure - this has triggered segfault
32
        # when previous blockstructure was already freed
33
        blocks = createUniformBlockGrid(blocks=(1, 1, 1), cellsPerBlock=(2, 2, 2))  # noqa: F841
Markus Holzer's avatar
testing    
Markus Holzer committed
34
        self.assertEqual(npf[0, 0, 0], 42)
35
36
37
38

    def testMemoryManagement3(self):
        """Same as testMemoryManagement2, but with iterators"""
        blocks = createUniformBlockGrid(blocks=(1, 1, 1), cellsPerBlock=(2, 2, 2))
Markus Holzer's avatar
Markus Holzer committed
39
        field.addToStorage(blocks, "TestField", np.int64)
40
41
42
43
44
        for block in blocks:
            for name in block.fieldNames:
                if name == "TestField":
                    f = block[name]
                    npf = field.toArray(f)
Markus Holzer's avatar
testing    
Markus Holzer committed
45
        npf[:, :, :] = 42
46
47
        del blocks, block, name, f
        blocks = createUniformBlockGrid(blocks=(1, 1, 1), cellsPerBlock=(2, 2, 2))  # noqa: F841
Markus Holzer's avatar
testing    
Markus Holzer committed
48
        self.assertEqual(npf[0, 0, 0], 42)
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

    def testExceptions(self):
        """Check that the right exceptions are thrown when nonexistent or non-convertible fields are accessed"""
        blocks = createUniformBlockGrid(blocks=(1, 1, 1), cellsPerBlock=(2, 2, 2))
        with self.assertRaises(ValueError) as cm:
            blocks[0]["cell bounding box"]
        self.assertEqual(str(cm.exception), "This blockdata is not accessible from Python")
        with self.assertRaises(IndexError) as cm:
            blocks[0]["nonexistent"]
        self.assertEqual(str(cm.exception), "No blockdata with the given name found")

    def testGeneralFunctionality(self):
        blocks = createUniformBlockGrid(blocks=(1, 1, 1), cellsPerBlock=(2, 2, 2))
        self.assertEqual(blocks.getNumberOfLevels(), 1)

        aabb = blocks.getDomain
        aabb2 = AABB(1.0, 1.0, 1.0, 1.2, 1.2, 1.2)

        self.assertEqual(aabb.min, (0.0, 0.0, 0.0))
        self.assertEqual(aabb.max, (2.0, 2.0, 2.0))
        self.assertEqual(aabb.size, (2.0, 2.0, 2.0))
        self.assertEqual(aabb.empty, False)
        self.assertEqual(aabb.volume, 8.0)
        self.assertEqual(aabb.center, (1.0, 1.0, 1.0))
        self.assertEqual(aabb.contains(aabb2), True)
        self.assertEqual(aabb2.contains(aabb), False)
        self.assertEqual(aabb2.contains((1.2, 1.2, 1.2)), False)
        self.assertEqual(aabb2.contains((1.1, 1.1, 1.1)), True)
77
78
79
80


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