Skip to content
Snippets Groups Projects
PyChopTest.py 3.48 KiB
Newer Older
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
#   NScD Oak Ridge National Laboratory, European Spallation Source,
#   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
Duc Le's avatar
Duc Le committed
"""Test suite for the PyChop package
"""
import unittest
import numpy as np

# Import mantid to setup the python paths to the bundled scripts
import mantid
from PyChop import PyChop2

class PyChop2Tests(unittest.TestCase):

Duc Le's avatar
Duc Le committed
    # Tests the Fermi chopper instruments
    def test_pychop_fermi(self):
        instnames = ['maps', 'mari', 'merlin']
Duc Le's avatar
Duc Le committed
        res = []
        flux = []
        for inc, instname in enumerate(instnames):
            chopobj = PyChop2(instname)
            # Code should give an error if the chopper settings and Ei have
            # not been set.
            self.assertRaises(ValueError, chopobj.getResolution)
Duc Le's avatar
Duc Le committed
            chopobj.setChopper('s', 200)
Duc Le's avatar
Duc Le committed
            chopobj.setEi(18)
            rr, ff = chopobj.getResFlux(np.linspace(0,17,10))
            res.append(rr)
            flux.append(ff)
Duc Le's avatar
Duc Le committed
        # Checks that the flux should be highest for MERLIN, MARI and MAPS in that order
        self.assertGreater(flux[2], flux[1])
Duc Le's avatar
Duc Le committed
        # Note that MAPS has been upgraded so now should have higher flux than MARI.
        self.assertGreater(flux[0], flux[1])
Duc Le's avatar
Duc Le committed
        # Checks that the resolution should be best for MAPS, MARI, and MERLIN in that order
        # actually MAPS and MARI resolutions are very close (previous error in MAPS distances
        # meant that MARI was calculated to have a better resolution, but it *should* be MAPS)
        self.assertLess(res[0][0], res[1][0])
        self.assertLess(res[1][0], res[2][0])
Duc Le's avatar
Duc Le committed
        # Now tests the standalone function
        for inc, instname in enumerate(instnames):
Duc Le's avatar
Duc Le committed
            rr, ff = PyChop2.calculate(instname, 's', 200, 18, 0)
            self.assertAlmostEqual(rr[0], res[inc][0], places=7)
            self.assertAlmostEqual(ff, flux[inc], places=7)

    # Tests the different variants of LET
    def test_pychop_let(self):
        variants = ['High flux', 'Intermediate', 'High resolution']
Duc Le's avatar
Duc Le committed
        res = []
        flux = []
        for inc, variant in enumerate(variants):
            chopobj = PyChop2('LET', variant)
luz.paz's avatar
luz.paz committed
            # Checks that it instantiates the correct variant
Duc Le's avatar
Duc Le committed
            self.assertTrue(variant in chopobj.getChopper())
            # Code should give an error if the chopper settings and Ei have
            # not been set.
            self.assertRaises(ValueError, chopobj.getResolution)
            chopobj.setFrequency(200)
            chopobj.setEi(18)
            rr, ff = chopobj.getResFlux(np.linspace(0,17,10))
            res.append(rr)
            flux.append(ff)
        # Checks that the flux should be highest for 'High flux', then 'Intermediate', 'High resolution'
        self.assertGreater(flux[0], flux[1])
        self.assertGreaterEqual(flux[1], flux[2])
        # Checks that the resolution should be best for 'High resolution', then 'Intermediate', 'High flux'
        self.assertLessEqual(res[2][0], res[1][0])
        self.assertLessEqual(res[1][0], res[0][0]) 
Duc Le's avatar
Duc Le committed
        # Now tests the standalone function
        for inc, variant in enumerate(variants):
            rr, ff = PyChop2.calculate('LET', variant, 200, 18, 0)
Duc Le's avatar
Duc Le committed
            self.assertAlmostEqual(rr[0], res[inc][0], places=7)
            self.assertAlmostEqual(ff, flux[inc], places=7)

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