/Framework/PythonInterface/test/python/mantid/geometry/SpaceGroupTest.py

https://github.com/wdzhou/mantid · Python · 164 lines · 127 code · 36 blank · 1 comment · 2 complexity · 233ff787c1e3796cbb090d0919e243a8 MD5 · raw file

  1. # pylint: disable=no-init,invalid-name,too-many-public-methods
  2. from __future__ import (absolute_import, division, print_function)
  3. import unittest
  4. from mantid.geometry import SpaceGroupFactory, UnitCell
  5. class SpaceGroupTest(unittest.TestCase):
  6. def test_creation(self):
  7. self.assertRaises(ValueError, SpaceGroupFactory.createSpaceGroup, "none")
  8. SpaceGroupFactory.createSpaceGroup("I m -3 m")
  9. def test_interface(self):
  10. spaceGroup = SpaceGroupFactory.createSpaceGroup("P -1")
  11. self.assertEquals(spaceGroup.getHMSymbol(), "P -1")
  12. self.assertEquals(spaceGroup.getOrder(), 2)
  13. symOpStrings = spaceGroup.getSymmetryOperationStrings()
  14. self.assertEqual(len(symOpStrings), 2)
  15. self.assertTrue("x,y,z" in symOpStrings)
  16. self.assertTrue("-x,-y,-z" in symOpStrings)
  17. def test_isAllowedUnitCell_cubic(self):
  18. cubic = UnitCell(6, 6, 6)
  19. tetragonal = UnitCell(6, 6, 6.01)
  20. sg = SpaceGroupFactory.createSpaceGroup('P m -3')
  21. self.assertTrue(sg.isAllowedUnitCell(cubic))
  22. self.assertFalse(sg.isAllowedUnitCell(tetragonal))
  23. def test_isAllowedUnitCell_trigonal(self):
  24. rhombohedral = UnitCell(5, 5, 5, 80, 80, 80)
  25. hexagonal = UnitCell(5, 5, 4, 90, 90, 120)
  26. sgRh = SpaceGroupFactory.createSpaceGroup('R -3 c :r')
  27. sgHex = SpaceGroupFactory.createSpaceGroup('R -3 c')
  28. sgP6m = SpaceGroupFactory.createSpaceGroup('P 6/m')
  29. self.assertTrue(sgRh.isAllowedUnitCell(rhombohedral))
  30. self.assertFalse(sgRh.isAllowedUnitCell(hexagonal))
  31. self.assertTrue(sgHex.isAllowedUnitCell(hexagonal))
  32. self.assertFalse(sgHex.isAllowedUnitCell(rhombohedral))
  33. self.assertTrue(sgP6m.isAllowedUnitCell(hexagonal))
  34. self.assertFalse(sgP6m.isAllowedUnitCell(rhombohedral))
  35. def test_equivalentPositions_Triclinic(self):
  36. wyckoffs = [([0.3, 0.4, 0.45], 2),
  37. ([0.5, 0.5, 0.5], 1),
  38. ([0.0, 0.5, 0.5], 1),
  39. ([0.5, 0.0, 0.5], 1),
  40. ([0.5, 0.5, 0.0], 1),
  41. ([0.5, 0.0, 0.0], 1),
  42. ([0.0, 0.5, 0.0], 1),
  43. ([0.0, 0.0, 0.5], 1),
  44. ([0.0, 0.0, 0.0], 1)]
  45. spaceGroup = SpaceGroupFactory.createSpaceGroup("P -1")
  46. self.checkWyckoffPositions(spaceGroup, wyckoffs)
  47. def test_equivalentPositions_Monoclinic(self):
  48. wyckoffs = [([0.3, 0.4, 0.45], 8),
  49. ([0.0, 0.4, 0.25], 4),
  50. ([0.25, 0.25, 0.5], 4),
  51. ([0.25, 0.25, 0.0], 4),
  52. ([0.0, 0.5, 0.0], 4),
  53. ([0.0, 0.0, 0.0], 4)]
  54. spaceGroup = SpaceGroupFactory.createSpaceGroup("C 1 2/c 1")
  55. self.checkWyckoffPositions(spaceGroup, wyckoffs)
  56. def test_equivalentPositions_Orthorhombic(self):
  57. wyckoffs = [([0.3, 0.4, 0.45], 16),
  58. ([0.3, 0.25, 0.45], 8),
  59. ([0.0, 0.4, 0.45], 8),
  60. ([0.25, 0.4, 0.25], 8),
  61. ([0.3, 0.0, 0.0], 8),
  62. ([0.0, 0.25, 0.45], 4),
  63. ([0.25, 0.25, 0.75], 4),
  64. ([0.25, 0.25, 0.25], 4),
  65. ([0.0, 0.0, 0.5], 4),
  66. ([0.0, 0.0, 0.0], 4)]
  67. spaceGroup = SpaceGroupFactory.createSpaceGroup("I m m a")
  68. self.checkWyckoffPositions(spaceGroup, wyckoffs)
  69. def test_equivalentPositions_Tetragonal(self):
  70. wyckoffs = [([0.3, 0.4, 0.45], 32),
  71. ([0.3, 0.3, 0.25], 16),
  72. ([0.25, 0.4, 0.125], 16),
  73. ([0.0, 0.0, 0.45], 16),
  74. ([0.0, 0.25, 0.125], 16),
  75. ([0.0, 0.0, 0.25], 8),
  76. ([0.0, 0.0, 0.0], 8)]
  77. spaceGroup = SpaceGroupFactory.createSpaceGroup("I 41/a c d")
  78. self.checkWyckoffPositions(spaceGroup, wyckoffs)
  79. def test_equivalentPositions_Trigonal(self):
  80. wyckoffs = [([0.3, 0.4, 0.45], 36),
  81. ([0.3, 0.0, 0.25], 18),
  82. ([0.5, 0.0, 0.0], 18),
  83. ([0.0, 0.0, 0.45], 12),
  84. ([0.0, 0.0, 0.0], 6),
  85. ([0.0, 0.0, 0.25], 6)]
  86. spaceGroup = SpaceGroupFactory.createSpaceGroup("R -3 c")
  87. self.checkWyckoffPositions(spaceGroup, wyckoffs)
  88. def test_equivalentPositions_Hexagonal(self):
  89. wyckoffs = [([0.3, 0.4, 0.45], 24),
  90. ([0.3, 0.6, 0.45], 12),
  91. ([0.3, 0.4, 0.25], 12),
  92. ([0.3, 0.0, 0.0], 12),
  93. ([0.3, 0.6, 0.25], 6),
  94. ([0.5, 0.0, 0.0], 6),
  95. ([1. / 3., 2. / 3., 0.45], 4),
  96. ([0.0, 0.0, 0.45], 4),
  97. ([1. / 3, 2. / 3., 0.75], 2),
  98. ([1. / 3, 2. / 3., 0.25], 2),
  99. ([0.0, 0.0, 0.25], 2),
  100. ([0.0, 0.0, 0.0], 2)]
  101. spaceGroup = SpaceGroupFactory.createSpaceGroup("P 63/m m c")
  102. self.checkWyckoffPositions(spaceGroup, wyckoffs)
  103. def test_equivalentPositions_Cubic(self):
  104. wyckoffs = [([0.3, 0.4, 0.45], 96),
  105. ([0.3, 0.25, 0.25], 48),
  106. ([0.3, 0.0, 0.0], 48),
  107. ([0.3, 0.3, 0.3], 32),
  108. ([0.25, 0.0, 0.0], 24),
  109. ([0.0, 0.25, 0.25], 24),
  110. ([0.25, 0.25, 0.25], 8),
  111. ([0.0, 0.0, 0.0], 8)]
  112. spaceGroup = SpaceGroupFactory.createSpaceGroup("F -4 3 c")
  113. self.checkWyckoffPositions(spaceGroup, wyckoffs)
  114. def test_to_string(self):
  115. spaceGroup = SpaceGroupFactory.createSpaceGroup("F -4 3 c")
  116. expected_str = "Space group with Hermann-Mauguin symbol: "\
  117. "F -4 3 c"
  118. expected_repr = "SpaceGroupFactory.createSpaceGroup(\"F -4 3 c\")"
  119. self.assertEqual(expected_str, str(spaceGroup))
  120. self.assertEqual(expected_repr, spaceGroup.__repr__())
  121. newSpaceGroup = eval(spaceGroup.__repr__())
  122. self.assertEqual(spaceGroup.getHMSymbol(), newSpaceGroup.getHMSymbol())
  123. def checkWyckoffPositions(self, spaceGroup, wyckoffs):
  124. for wp in wyckoffs:
  125. equivalentPositions = spaceGroup.getEquivalentPositions(wp[0])
  126. self.assertEqual(len(equivalentPositions), wp[1])
  127. if __name__ == '__main__':
  128. unittest.main()