/Framework/PythonInterface/test/python/plugins/algorithms/PoldiCreatePeaksFromFileTest.py

https://github.com/mantidproject/mantid · Python · 180 lines · 135 code · 31 blank · 14 comment · 4 complexity · 25edebe31b78554b8ec3498a8f43e248 MD5 · raw file

  1. # Mantid Repository : https://github.com/mantidproject/mantid
  2. #
  3. # Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
  4. # NScD Oak Ridge National Laboratory, European Spallation Source,
  5. # Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
  6. # SPDX - License - Identifier: GPL - 3.0 +
  7. # pylint: disable=no-init,invalid-name,too-many-public-methods
  8. import unittest
  9. from testhelpers import assertRaisesNothing
  10. from testhelpers.tempfile_wrapper import TemporaryFileHelper
  11. from mantid.kernel import *
  12. from mantid.api import *
  13. from mantid.simpleapi import *
  14. class PoldiCreatePeaksFromFileTest(unittest.TestCase):
  15. testname = None
  16. def __init__(self, *args):
  17. unittest.TestCase.__init__(self, *args)
  18. def test_Init(self):
  19. assertRaisesNothing(self, AlgorithmManager.create, ("PoldiCreatePeaksFromFile"))
  20. def test_FileOneCompoundOneAtom(self):
  21. fileHelper = TemporaryFileHelper("""Silicon {
  22. Lattice: 5.43 5.43 5.43 90.0 90.0 90.0
  23. Spacegroup: F d -3 m
  24. Atoms: {
  25. Si 0 0 0 1.0 0.05
  26. }
  27. }""")
  28. ws = PoldiCreatePeaksFromFile(fileHelper.getName(), 0.7, 10.0)
  29. # Check output GroupWorkspace
  30. self.assertEqual(ws.getNumberOfEntries(), 1)
  31. self.assertTrue(ws.contains("Silicon"))
  32. # Check that the ouput is identical to what's expected
  33. ws_expected = PoldiCreatePeaksFromCell("F d -3 m", "Si 0 0 0 1.0 0.05", a=5.43, LatticeSpacingMin=0.7)
  34. si_ws = AnalysisDataService.retrieve("Silicon")
  35. self._tablesAreEqual(si_ws, ws_expected)
  36. # Clean up
  37. self._cleanWorkspaces([ws, ws_expected])
  38. def test_FileOneCompoundTwoAtoms(self):
  39. # It's the same structure and the same reflections, just the structure factors are different
  40. fileHelper = TemporaryFileHelper("""SiliconCarbon {
  41. Lattice: 5.43 5.43 5.43 90.0 90.0 90.0
  42. Spacegroup: F d -3 m
  43. Atoms: {
  44. Si 0 0 0 0.9 0.05
  45. C 0 0 0 0.1 0.05
  46. }
  47. # Comment
  48. }""")
  49. ws = PoldiCreatePeaksFromFile(fileHelper.getName(), 0.7, 10.0)
  50. self.assertEqual(ws.getNumberOfEntries(), 1)
  51. self.assertTrue(ws.contains("SiliconCarbon"))
  52. ws_expected = PoldiCreatePeaksFromCell("F d -3 m", "Si 0 0 0 0.9 0.05; C 0 0 0 0.1 0.05", a=5.43,
  53. LatticeSpacingMin=0.7)
  54. si_ws = AnalysisDataService.retrieve("SiliconCarbon")
  55. self._tablesAreEqual(si_ws, ws_expected)
  56. # Clean up
  57. self._cleanWorkspaces([ws, ws_expected])
  58. def test_FileTwoCompounds(self):
  59. # Using two imaginary structures to check that two compounds are parsed correctly as well
  60. fileHelper = TemporaryFileHelper("""SiliconCarbon {
  61. Lattice: 5.43 5.43 5.43 90.0 90.0 120.0
  62. Spacegroup: P 63/m m c
  63. Atoms: {
  64. Si 0 0 0 0.9 0.05
  65. C 0 0 0 0.1 0.05
  66. }
  67. }
  68. Silicon {
  69. Lattice: 5.43 5.43 5.43 90.0 90.0 90.0
  70. Spacegroup: F d -3 m
  71. Atoms: {
  72. Si 1/2 1/2 0 1.0 0.05
  73. }
  74. }""")
  75. ws = PoldiCreatePeaksFromFile(fileHelper.getName(), 0.7, 10.0)
  76. self.assertEqual(ws.getNumberOfEntries(), 2)
  77. self.assertTrue(ws.contains("SiliconCarbon"))
  78. self.assertTrue(ws.contains("Silicon"))
  79. self._cleanWorkspaces([ws])
  80. def test_FileFaultyLatticeStrings(self):
  81. fhLatticeMissing = TemporaryFileHelper("""Silicon {
  82. Spacegroup: F d -3 m
  83. Atoms: {
  84. Si 0 0 0 1.0 0.05
  85. }
  86. }""")
  87. fhNoLattice = TemporaryFileHelper("""Silicon {
  88. Lattice:
  89. Spacegroup: F d -3 m
  90. Atoms: {
  91. Si 0 0 0 1.0 0.05
  92. }
  93. }""")
  94. fhInvalidLattice = TemporaryFileHelper("""Silicon {
  95. Lattice: invalid
  96. Spacegroup: F d -3 m
  97. Atoms: {
  98. Si 0 0 0 1.0 0.05
  99. }
  100. }""")
  101. self.assertRaises(RuntimeError, PoldiCreatePeaksFromFile, *(fhLatticeMissing.getName(), 0.7, 10.0, 'ws'))
  102. self.assertRaises(RuntimeError, PoldiCreatePeaksFromFile, *(fhNoLattice.getName(), 0.7, 10.0, 'ws'))
  103. self.assertRaises(RuntimeError, PoldiCreatePeaksFromFile, *(fhInvalidLattice.getName(), 0.7, 10.0, 'ws'))
  104. def test_FileFaultySpaceGroupStrings(self):
  105. fhSgMissing = TemporaryFileHelper("""Silicon {
  106. Lattice: 5.43 5.43 5.43 90.0 90.0 90.0
  107. Atoms: {
  108. Si 0 0 0 1.0 0.05
  109. }
  110. }""")
  111. fhSgInvalid = TemporaryFileHelper("""Silicon {
  112. Lattice: 5.43 5.43 5.43 90.0 90.0 90.0
  113. Spacegroup: invalid
  114. Atoms: {
  115. Si 0 0 0 1.0 0.05
  116. }
  117. }""")
  118. self.assertRaises(RuntimeError, PoldiCreatePeaksFromFile, *(fhSgMissing.getName(), 0.7, 10.0, 'ws'))
  119. self.assertRaises(RuntimeError, PoldiCreatePeaksFromFile, *(fhSgInvalid.getName(), 0.7, 10.0, 'ws'))
  120. def test_FileFaultyAtomStrings(self):
  121. fhAtomsMissing = TemporaryFileHelper("""Silicon {
  122. Lattice: 5.43 5.43 5.43 90.0 90.0 90.0
  123. Spacegroup: F d -3 m
  124. }""")
  125. fhAtomsNoBraces = TemporaryFileHelper("""Silicon {
  126. Lattice: 5.43 5.43 5.43 90.0 90.0 90.0
  127. Spacegroup: invalid
  128. Atoms:
  129. Sis 0 0 0 1.0 0.05
  130. }""")
  131. fhAtomsEmpty = TemporaryFileHelper("""Silicon {
  132. Lattice: 5.43 5.43 5.43 90.0 90.0 90.0
  133. Spacegroup: invalid
  134. Atoms: { }
  135. }""")
  136. self.assertRaises(RuntimeError, PoldiCreatePeaksFromFile, *(fhAtomsMissing.getName(), 0.7, 10.0, 'ws'))
  137. self.assertRaises(RuntimeError, PoldiCreatePeaksFromFile, *(fhAtomsNoBraces.getName(), 0.7, 10.0, 'ws'))
  138. self.assertRaises(RuntimeError, PoldiCreatePeaksFromFile, *(fhAtomsEmpty.getName(), 0.7, 10.0, 'ws'))
  139. def _tablesAreEqual(self, lhs, rhs):
  140. self.assertEqual(lhs.rowCount(), rhs.rowCount(), msg="Row count of tables is different")
  141. for r in range(lhs.rowCount()):
  142. self.assertEqual(lhs.row(r), rhs.row(r), "Row " + str(r) + " of tables differ.")
  143. def _cleanWorkspaces(self, wsList):
  144. for ws in wsList:
  145. DeleteWorkspace(ws)
  146. if __name__ == '__main__':
  147. # Only test if algorithm is registered (pyparsing dependency).
  148. if AlgorithmFactory.exists("PoldiCreatePeaksFromFile"):
  149. unittest.main()