UnitCellTest.py 4.14 KB
Newer Older
1
2
3
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4
5
#   NScD Oak Ridge National Laboratory, European Spallation Source,
#   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6
# SPDX - License - Identifier: GPL - 3.0 +
7
import unittest
8
import testhelpers
9
10
from mantid.geometry import UnitCell, AngleUnits
from mantid.kernel import V3D
11
12
import numpy as np

13

14
class UnitCellTest(unittest.TestCase):
15

16
17
    def test_invalid_parameters_throw(self):
        self.assertRaises(RuntimeError, UnitCell, 0, 0, 0, 0, 0, 0)
18

19
20
    def test_simple_constructor(self):
        u1 = UnitCell()
21
22
        self.assertEqual(u1.a1(), 1)
        self.assertEqual(u1.alpha(), 90)
23

24
25
26
        u2 = UnitCell(3,4,5)
        self.assertAlmostEquals(u2.b1(),1./3., 10)
        self.assertAlmostEquals(u2.alphastar(), 90, 10)
27

28
29
        u4 = u2
        self.assertAlmostEquals(u4.volume(),1./u2.recVolume(),10)
30
        u2.seta(3)
31
        self.assertAlmostEquals(u2.a(),3,10)
32

33
34
35
36
37
    def test_numpy_array_conversion(self):
        row0 = (0.162546756312, 0.00815256992072, -0.00145274558861)
        row1 = (row0[1], 0.028262965555, 0.00102046431298)
        row2 = (row0[2], row1[2], 0.0156808990098 )
        gstar = np.array( [row0,row1,row2] )
38

39
        u = UnitCell()
40
        testhelpers.assertRaisesNothing(self, u.recalculateFromGstar, gstar)
41
42
        self._check_cell(u)

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    def test_to_string(self):
        unit = UnitCell(3,3,3)
        expected_str = "UnitCell with lattice parameters: a = 3 b = 3 c = 3 "\
                       "alpha = 90 beta = 90 gamma = 90"
        expected_repr = "UnitCell(3, 3, 3, 90, 90, 90)"

        self.assertEqual(expected_str, str(unit))
        self.assertEqual(expected_repr, unit.__repr__())

        newUnit = eval(unit.__repr__())
        self.assertEqual(unit.a(), newUnit.a())
        self.assertEqual(unit.b(), newUnit.b())
        self.assertEqual(unit.c(), newUnit.c())

        self.assertEqual(unit.alpha(), newUnit.alpha())
        self.assertEqual(unit.beta(), newUnit.beta())
        self.assertEqual(unit.gamma(), newUnit.gamma())

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    def test_getModVec_returns_v3d(self):
        unit = UnitCell(3, 3, 3)

        self.assertTrue(isinstance(unit.getModVec(0), V3D))
        self.assertTrue(isinstance(unit.getModVec(1), V3D))
        self.assertTrue(isinstance(unit.getModVec(2), V3D))

    def test_setModVec_accepts_v3d(self):
        unit = UnitCell(3, 3, 3)

        vectors = (V3D(1,2,3), V3D(2, 1, 3), V3D(3, 2, 1))
        unit.setModVec1(vectors[0])
        unit.setModVec2(vectors[1])
        unit.setModVec3(vectors[2])

        for index, expected in enumerate(vectors):
            self.assertEquals(vectors[index], unit.getModVec(index))

    def test_getMaxOrder_returns_int(self):
        unit = UnitCell(3, 3, 3)

        self.assertTrue(isinstance(unit.getMaxOrder(), int))

    def test_setMaxOrder_accepts_int(self):
        unit = UnitCell(3, 3, 3)

        unit.setMaxOrder(2)

        self.assertEquals(2, unit.getMaxOrder())

91
92
93
94
95
96
97
    def _check_cell(self, cell):
        self.assertAlmostEqual(cell.a(),2.5,10)
        self.assertAlmostEqual(cell.b(),6,10)
        self.assertAlmostEqual(cell.c(),8,10)
        self.assertAlmostEqual(cell.alpha(),93,10)
        self.assertAlmostEqual(cell.beta(),88,10)
        self.assertAlmostEqual(cell.gamma(),97,10)
98

99
        # get the some elements of the B matrix
100
        self.assertEqual(type(cell.getB()), np.ndarray)
101
102
103
104
        self.assertAlmostEqual(cell.getB()[0][0],0.403170877311,10)
        self.assertAlmostEqual(cell.getB()[2][0],0.0,10)
        self.assertAlmostEqual(cell.getB()[0][2],-0.00360329991666,10)
        self.assertAlmostEqual(cell.getB()[2][2],0.125,10)
105

106
107
108
109
110
        # d spacing for direct lattice at (1,1,1) (will automatically check dstar)
        self.assertAlmostEqual(cell.d(1.,1.,1.),2.1227107587,10)
        self.assertAlmostEqual(cell.d(V3D(1.,1.,1.)),2.1227107587,10)
        # angle
        self.assertAlmostEqual(cell.recAngle(1,1,1,1,0,0,AngleUnits.Radians),0.471054990614,10)
111

112
113
        self.assertEqual(type(cell.getG()), np.ndarray)
        self.assertEqual(type(cell.getGstar()), np.ndarray)
114

115
116
117

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