def test_computeincoherentdos(self): ws = CreateSampleWorkspace() # Will fail unless the input workspace has Q and DeltaE axes. with self.assertRaises(RuntimeError): ws_DOS = ComputeIncoherentDOS(ws) ws = CreateSampleWorkspace(XUnit = 'DeltaE') with self.assertRaises(RuntimeError): ws_DOS = ComputeIncoherentDOS(ws) # Creates a workspace with two optic phonon modes at +E and -E with Q^2 dependence and population correct for T=300K ws = self.createPhononWS(300, 5, 'DeltaE') # This should work! ws_DOS = ComputeIncoherentDOS(ws) self.assertEquals(ws_DOS.getAxis(0).getUnit().unitID(), 'DeltaE') self.assertEquals(ws_DOS.getNumberHistograms(), 1) # Checks that it works if the workspace has |Q| along x instead of y, and converts energy to wavenumber ws = Transpose(ws) ws_DOS = ComputeIncoherentDOS(ws, Temperature = 300, EnergyBinning = '-20, 0.2, 20', Wavenumbers = True) self.assertEquals(ws_DOS.getAxis(0).getUnit().unitID(), 'DeltaE_inWavenumber') self.assertEquals(ws_DOS.blocksize(), 200) # Checks that the Bose factor correction is ok. dos_eplus = np.max(ws_DOS.readY(0)[100:200]) dos_eminus = np.max(ws_DOS.readY(0)[:100]) self.assertAlmostEqual(dos_eplus / dos_eminus, 1., places=1) # Check that unit conversion from cm^-1 to meV works and also that conversion to states/meV is done ws = self.convertToWavenumber(ws) SetSampleMaterial(ws, 'Al') ws_DOSn = ComputeIncoherentDOS(ws, EnergyBinning = '-160, 1.6, 160', StatesPerEnergy = True) self.assertTrue('states' in ws_DOSn.YUnitLabel()) self.assertEquals(ws_DOSn.getAxis(0).getUnit().unitID(), 'DeltaE') material = ws.sample().getMaterial() factor = material.relativeMolecularMass() / (material.totalScatterXSection() * 1000) * 4 * np.pi self.assertAlmostEqual(np.max(ws_DOSn.readY(0)) / (np.max(ws_DOS.readY(0))*factor), 1., places=1)
def test_computeincoherentdos(self): ws = CreateSampleWorkspace() # Will fail unless the input workspace has Q and DeltaE axes. with self.assertRaises(RuntimeError): ws_DOS = ComputeIncoherentDOS(ws) ws = CreateSampleWorkspace(XUnit = 'DeltaE') with self.assertRaises(RuntimeError): ws_DOS = ComputeIncoherentDOS(ws) # Creates a workspace with two optic phonon modes at +E and -E with Q^2 dependence and population correct for T=300K ws = self.createPhononWS(300, 5, 'DeltaE') # This should work! ws_DOS = ComputeIncoherentDOS(ws) self.assertEqual(ws_DOS.getAxis(0).getUnit().unitID(), 'DeltaE') self.assertEqual(ws_DOS.getNumberHistograms(), 1) # Checks that it works if the workspace has |Q| along x instead of y, and converts energy to wavenumber ws = Transpose(ws) ws_DOS = ComputeIncoherentDOS(ws, Temperature = 300, EnergyBinning = '-20, 0.2, 20', Wavenumbers = True) self.assertEqual(ws_DOS.getAxis(0).getUnit().unitID(), 'DeltaE_inWavenumber') self.assertEqual(ws_DOS.blocksize(), 200) # Checks that the Bose factor correction is ok. dos_eplus = np.max(ws_DOS.readY(0)[100:200]) dos_eminus = np.max(ws_DOS.readY(0)[:100]) self.assertAlmostEqual(dos_eplus / dos_eminus, 1., places=1) # Check that unit conversion from cm^-1 to meV works and also that conversion to states/meV is done ws = self.convertToWavenumber(ws) SetSampleMaterial(ws, 'Al') ws_DOSn = ComputeIncoherentDOS(ws, EnergyBinning = '-160, 1.6, 160', StatesPerEnergy = True) self.assertTrue('states' in ws_DOSn.YUnitLabel()) self.assertEqual(ws_DOSn.getAxis(0).getUnit().unitID(), 'DeltaE') material = ws.sample().getMaterial() factor = material.relativeMolecularMass() / (material.totalScatterXSection() * 1000) * 4 * np.pi self.assertAlmostEqual(np.max(ws_DOSn.readY(0)) / (np.max(ws_DOS.readY(0))*factor), 1., places=1)
def test_multiple_histograms(self): energyBins = np.arange(-7., 7., 0.13) qs = np.array([1.1, 1.3, 1.5, 1.7]) EFixed = 8. Ys = np.ones(3 * (len(energyBins) - 1)) Es = Ys verticalAxis = [str(q) for q in qs] ws = CreateWorkspace(energyBins, Ys, Es, NSpec=3, UnitX='DeltaE', VerticalAxisUnit='MomentumTransfer', VerticalAxisValues=verticalAxis, StoreInADS=False) LoadInstrument(ws, InstrumentName='IN4', RewriteSpectraMap=False, StoreInADS=False) AddSampleLog(ws, LogName='Ei', LogText=str(EFixed), LogType='Number', LogUnit='meV', StoreInADS=False) dos = ComputeIncoherentDOS(ws, EnergyBinning='Emin, Emax', StoreInADS=False) self.assertEquals(dos.getNumberHistograms(), 1) self.assertEquals(dos.getAxis(0).getUnit().unitID(), 'DeltaE') dos_Xs = dos.readX(0) self.assertEquals(len(dos_Xs), len(energyBins)) dos_Ys = dos.readY(0) dos_Es = dos.readE(0) g1 = self.compute(qs[0:2], energyBins) g2 = self.compute(qs[1:3], energyBins) g3 = self.compute(qs[2:4], energyBins) g = (g1 + g2 + g3) / 3 gE = np.sqrt(g1**2 + g2**2 + g3**2) / 3 np.testing.assert_equal(dos_Xs, energyBins) for i in range(len(dos_Ys)): self.assertAlmostEquals(dos_Ys[i], g[i]) self.assertAlmostEquals(dos_Es[i], gE[i])
def test_multiple_histograms(self): energyBins = np.arange(-7., 7., 0.13) qs = np.array([1.1, 1.3, 1.5, 1.7]) EFixed = 8. Ys = np.ones(3 * (len(energyBins) - 1)) Es = Ys verticalAxis = [str(q) for q in qs] ws = CreateWorkspace(energyBins, Ys, Es, NSpec=3, UnitX='DeltaE', VerticalAxisUnit='MomentumTransfer', VerticalAxisValues=verticalAxis, StoreInADS=False) LoadInstrument(ws, InstrumentName='IN4', RewriteSpectraMap=False, StoreInADS=False) AddSampleLog(ws, LogName='Ei', LogText=str(EFixed), LogType='Number', LogUnit='meV', StoreInADS=False) dos = ComputeIncoherentDOS(ws, EnergyBinning='Emin, Emax', StoreInADS=False) self.assertEqual(dos.getNumberHistograms(), 1) self.assertEqual(dos.getAxis(0).getUnit().unitID(), 'DeltaE') dos_Xs = dos.readX(0) self.assertEqual(len(dos_Xs), len(energyBins)) dos_Ys = dos.readY(0) dos_Es = dos.readE(0) g1 = self.compute(qs[0:2], energyBins) g2 = self.compute(qs[1:3], energyBins) g3 = self.compute(qs[2:4], energyBins) g = (g1 + g2 + g3) / 3 gE = np.sqrt(g1**2 + g2**2 + g3**2) / 3 np.testing.assert_equal(dos_Xs, energyBins) for i in range(len(dos_Ys)): self.assertAlmostEquals(dos_Ys[i], g[i]) self.assertAlmostEquals(dos_Es[i], gE[i])
def test_computation_nontransposed_TwoThetaW(self): energyBins = np.arange(-7., 7., 0.13) twoThetas = np.array([17.]) ws = self.unitySTwoThetaWSingleHistogram(energyBins, twoThetas) dos = ComputeIncoherentDOS(ws, EnergyBinning='Emin, Emax', StoreInADS=False) self.assertEquals(dos.getNumberHistograms(), 1) self.assertEquals(dos.getAxis(0).getUnit().unitID(), 'DeltaE') dos_Xs = dos.readX(0) self.assertEquals(len(dos_Xs), len(energyBins)) dos_Ys = dos.readY(0) dos_Es = dos.readE(0) g = self.computeFromTwoTheta(twoThetas, energyBins) np.testing.assert_equal(dos_Xs, energyBins) for i in range(len(dos_Ys)): self.assertAlmostEquals(dos_Ys[i], g[i]) self.assertAlmostEquals(dos_Es[i], g[i])
def test_computation_nontransposed_TwoThetaW(self): energyBins = np.arange(-7., 7., 0.13) twoThetas = np.array([17.]) ws = self.unitySTwoThetaWSingleHistogram(energyBins, twoThetas) dos = ComputeIncoherentDOS(ws, EnergyBinning='Emin, Emax', StoreInADS=False) self.assertEqual(dos.getNumberHistograms(), 1) self.assertEqual(dos.getAxis(0).getUnit().unitID(), 'DeltaE') dos_Xs = dos.readX(0) self.assertEqual(len(dos_Xs), len(energyBins)) dos_Ys = dos.readY(0) dos_Es = dos.readE(0) g = self.computeFromTwoTheta(twoThetas, energyBins) np.testing.assert_equal(dos_Xs, energyBins) for i in range(len(dos_Ys)): self.assertAlmostEquals(dos_Ys[i], g[i]) self.assertAlmostEquals(dos_Es[i], g[i])
def test_nondefault_temperature(self): energyBins = np.arange(-7., 7., 0.13) qs = np.array([2.3]) temperature = 666.7 ws = self.unitySQWSingleHistogram(energyBins, qs) dos = ComputeIncoherentDOS(ws, Temperature=temperature, EnergyBinning='Emin, Emax', StoreInADS=False) self.assertEquals(dos.getNumberHistograms(), 1) self.assertEquals(dos.getAxis(0).getUnit().unitID(), 'DeltaE') dos_Xs = dos.readX(0) self.assertEquals(len(dos_Xs), len(energyBins)) dos_Ys = dos.readY(0) dos_Es = dos.readE(0) g = self.compute(qs, energyBins, temperature=temperature) np.testing.assert_equal(dos_Xs, energyBins) for i in range(len(dos_Ys)): self.assertAlmostEquals(dos_Ys[i], g[i]) self.assertAlmostEquals(dos_Es[i], g[i])
def test_nonzero_MDS(self): energyBins = np.arange(-7., 7., 0.13) qs = np.array([2.3]) msd = 5.5 ws = self.unitySQWSingleHistogram(energyBins, qs) dos = ComputeIncoherentDOS(ws, MeanSquareDisplacement=msd, EnergyBinning='Emin, Emax', StoreInADS=False) self.assertEquals(dos.getNumberHistograms(), 1) self.assertEquals(dos.getAxis(0).getUnit().unitID(), 'DeltaE') dos_Xs = dos.readX(0) self.assertEquals(len(dos_Xs), len(energyBins)) dos_Ys = dos.readY(0) dos_Es = dos.readE(0) g = self.compute(qs, energyBins, msd=msd) np.testing.assert_equal(dos_Xs, energyBins) for i in range(len(dos_Ys)): self.assertAlmostEquals(dos_Ys[i], g[i], delta=g[i] * 1e-12) self.assertAlmostEquals(dos_Es[i], g[i], delta=g[i] * 1e-12)
def test_computation_transposed_QW(self): energyBins = np.arange(-7., 7., 0.13) qs = np.array([2.15, 2.25]) ws = self.unitySQWSingleHistogram(energyBins, qs) ws = Transpose(ws, StoreInADS=False) dos = ComputeIncoherentDOS(ws, EnergyBinning='Emin, Emax', StoreInADS=False) self.assertEquals(dos.getNumberHistograms(), 1) self.assertEquals(dos.getAxis(0).getUnit().unitID(), 'DeltaE') dos_Xs = dos.readX(0) self.assertEquals(len(dos_Xs), len(energyBins)) dos_Ys = dos.readY(0) dos_Es = dos.readE(0) g = self.compute(qs, energyBins) np.testing.assert_equal(dos_Xs, energyBins) for i in range(len(dos_Ys)): self.assertAlmostEquals(dos_Ys[i], g[i]) self.assertAlmostEquals(dos_Es[i], g[i])
def test_nondefault_temperature(self): energyBins = np.arange(-7., 7., 0.13) qs = np.array([2.3]) temperature = 666.7 ws = self.unitySQWSingleHistogram(energyBins, qs) dos = ComputeIncoherentDOS(ws, Temperature=temperature, EnergyBinning='Emin, Emax', StoreInADS=False) self.assertEqual(dos.getNumberHistograms(), 1) self.assertEqual(dos.getAxis(0).getUnit().unitID(), 'DeltaE') dos_Xs = dos.readX(0) self.assertEqual(len(dos_Xs), len(energyBins)) dos_Ys = dos.readY(0) dos_Es = dos.readE(0) g = self.compute(qs, energyBins, temperature=temperature) np.testing.assert_equal(dos_Xs, energyBins) for i in range(len(dos_Ys)): self.assertAlmostEquals(dos_Ys[i], g[i]) self.assertAlmostEquals(dos_Es[i], g[i])
def test_nonzero_MDS(self): energyBins = np.arange(-7., 7., 0.13) qs = np.array([2.3]) msd = 5.5 ws = self.unitySQWSingleHistogram(energyBins, qs) dos = ComputeIncoherentDOS(ws, MeanSquareDisplacement=msd, EnergyBinning='Emin, Emax', StoreInADS=False) self.assertEqual(dos.getNumberHistograms(), 1) self.assertEqual(dos.getAxis(0).getUnit().unitID(), 'DeltaE') dos_Xs = dos.readX(0) self.assertEqual(len(dos_Xs), len(energyBins)) dos_Ys = dos.readY(0) dos_Es = dos.readE(0) g = self.compute(qs, energyBins, msd=msd) np.testing.assert_equal(dos_Xs, energyBins) for i in range(len(dos_Ys)): self.assertAlmostEquals(dos_Ys[i], g[i], delta=g[i] * 1e-12) self.assertAlmostEquals(dos_Es[i], g[i], delta=g[i] * 1e-12)
def test_computation_transposed_QW(self): energyBins = np.arange(-7., 7., 0.13) qs = np.array([2.15, 2.25]) ws = self.unitySQWSingleHistogram(energyBins, qs) ws = Transpose(ws, StoreInADS=False) dos = ComputeIncoherentDOS(ws, EnergyBinning='Emin, Emax', StoreInADS=False) self.assertEqual(dos.getNumberHistograms(), 1) self.assertEqual(dos.getAxis(0).getUnit().unitID(), 'DeltaE') dos_Xs = dos.readX(0) self.assertEqual(len(dos_Xs), len(energyBins)) dos_Ys = dos.readY(0) dos_Es = dos.readE(0) g = self.compute(qs, energyBins) np.testing.assert_equal(dos_Xs, energyBins) for i in range(len(dos_Ys)): self.assertAlmostEquals(dos_Ys[i], g[i]) self.assertAlmostEquals(dos_Es[i], g[i])