def PyExec(self):
        self._setup()
        if not self.getProperty("InputWorkspace").isDefault:
            self._output_ws = CloneWorkspace(Inputworkspace=self._input_ws,
                                             OutputWorkspace=self._output_ws,
                                             StoreInADS=False)
        else:
            self._output_ws = CreateWorkspace(NSpec=1, DataX=[0], DataY=[0],
                                              UnitX='Wavelength', Distribution=False,
                                              StoreInADS=False)
            self._output_ws = Rebin(InputWorkspace=self._output_ws,
                                    Params=self._bin_params,
                                    StoreInADS=False)

        if self._output_ws.isDistribution():
            ConvertFromDistribution(Workspace=self._output_ws,
                                    StoreInADS=False)

        self._output_ws = ConvertToPointData(InputWorkspace=self._output_ws,
                                             StoreInADS=False)
        self._output_ws = ConvertUnits(InputWorkspace=self._output_ws,
                                       Target='Wavelength',
                                       EMode='Elastic',
                                       StoreInADS=False)

        if self.getProperty('Alpha').isDefault:
            if self._density_type == 'Mass Density':
                SetSampleMaterial(
                    InputWorkspace=self._output_ws,
                    ChemicalFormula=self._chemical_formula,
                    SampleMassDensity=self._density,
                    StoreInADS=False)
                self._density = self._output_ws.sample().getMaterial().numberDensityEffective
            elif self._density_type == 'Number Density':
                SetSampleMaterial(
                    InputWorkspace=self._output_ws,
                    ChemicalFormula=self._chemical_formula,
                    SampleNumberDensity=self._density,
                    StoreInADS=False)
            else:
                raise RuntimeError(f'Unknown "DensityType": {self._density_type}')
            if self.getProperty('MeasuredEfficiency').isDefault:
                self._calculate_area_density_from_density()
            else:
                self._calculate_area_density_from_efficiency()
            self._calculate_alpha_absXS_term()
            if self._xsection_type == "TotalXSection":
                self._calculate_alpha_scatXS_term()

        wavelengths = self._output_ws.readX(0)
        efficiency = self._calculate_efficiency(wavelengths)
        for histo in range(self._output_ws.getNumberHistograms()):
            self._output_ws.setY(histo, efficiency)

        self.setProperty('OutputWorkspace', self._output_ws)
 def test_simple(self):
     input_x = np.array([0.1, 0.2, 0.3, 0.4, 0.5])
     input_y = np.array([1., 2, 3, 2, 1])
     input_e = np.array([0.1, 0.14, 0.17, 0.14, 0.1])
     sq = CreateWorkspace(DataX=input_x,
                          DataY=input_y,
                          DataE=input_e,
                          UnitX='MomentumTransfer')
     SetSampleMaterial(InputWorkspace=sq, ChemicalFormula='Ar')
     fq = PDConvertReciprocalSpace(InputWorkspace=sq,
                                   To='F(Q)',
                                   From='S(Q)')
     x = fq.readX(0)
     y = fq.readY(0)
     e = fq.readE(0)
     self.assertTrue(np.array_equal(x, input_x))
     self.assertTrue(np.array_equal(y, input_x * (input_y - 1)))
     self.assertTrue(np.array_equal(e, input_x * input_e))
     fkq = PDConvertReciprocalSpace(InputWorkspace=sq,
                                    To='FK(Q)',
                                    From='S(Q)')
     x = fkq.readX(0)
     y = fkq.readY(0)
     e = fkq.readE(0)
     bsq = sq.sample().getMaterial().cohScatterLengthSqrd()
     self.assertTrue(np.array_equal(x, input_x))
     self.assertTrue(np.allclose(y, bsq * (input_y - 1)))
     self.assertTrue(np.allclose(e, bsq * input_e))
 def runTest(self):
     # load data for bank 1 (incl. monitor spectra 1-5)
     van = load_data_and_normalise('WISH/input/11_4/WISH00019612.raw',
                                   outputWorkspace="van")
     # create Abs Correction for V
     shape = '''<sphere id="V-sphere">
         <centre x="0.0"  y="0.0" z="0.0" />
         <radius val="0.0025"/>
         </sphere>'''
     CreateSampleShape(InputWorkspace=van, ShapeXML=shape)
     SetSampleMaterial(InputWorkspace=van,
                       SampleNumberDensity=0.0119,
                       ScatteringXSection=5.197,
                       AttenuationXSection=4.739,
                       ChemicalFormula='V0.95 Nb0.05')
     abs_cor = AbsorptionCorrection(InputWorkspace=van, ElementSize=0.5)
     # correct Vanadium run for absorption
     van = Divide(LHSWorkspace=van,
                  RHSWorkspace=abs_cor,
                  OutputWorkspace=van)
     # smooth data
     SmoothNeighbours(InputWorkspace=van,
                      OutputWorkspace=van,
                      Radius=3,
                      NumberOfNeighbours=6)
     SmoothData(InputWorkspace=van, OutputWorkspace=van, NPoints=300)
Exemple #4
0
    def test_material(self):
        ws = CreateWorkspace(DataX=[1], DataY=[1], StoreInADS=False)
        sample = ws.sample()
        SetSampleMaterial(ws, "Al2 O3", SampleMassDensity=4, StoreInADS=False)
        material = sample.getMaterial()

        self.assertAlmostEqual(material.numberDensity, 0.1181, places=4)
        self.assertAlmostEqual(material.relativeMolecularMass(),
                               101.961,
                               places=3)

        atoms, numatoms = material.chemicalFormula()

        self.assertEqual(len(atoms), len(numatoms))
        self.assertEqual(len(atoms), 2)
        self.assertEqual(numatoms[0], 2)
        self.assertEqual(numatoms[1], 3)

        xs0 = atoms[0].neutron()
        xs1 = atoms[1].neutron()
        # the correct way to calculate for coherent cross section
        # is to average the scattering lengths then convert to a cross section
        b_real = (xs0['coh_scatt_length_real'] * 2 +
                  xs1['coh_scatt_length_real'] * 3) / 5
        b_imag = (xs0['coh_scatt_length_img'] * 2 +
                  xs1['coh_scatt_length_img'] * 3) / 5
        xs = .04 * pi * (b_real * b_real + b_imag * b_imag)
        self.assertAlmostEquals(material.cohScatterXSection(), xs, places=4)
 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_simple(self):
        input_x = np.array([0.1, 0.2, 0.3, 0.4, 0.5])
        input_y = np.array([1., 2, 3, 2, 1])
        input_e = np.array([0.1, 0.14, 0.17, 0.14, 0.1])
        Gr = CreateWorkspace(DataX=input_x, DataY=input_y, DataE=input_e)
        SetSampleMaterial(InputWorkspace=Gr, ChemicalFormula='Ar')
        GKr = PDConvertRealSpace(InputWorkspace=Gr, To='GK(r)', From='G(r)')
        x = GKr.readX(0)
        y = GKr.readY(0)
        e = GKr.readE(0)

        bsq = Gr.sample().getMaterial().cohScatterLengthSqrd()
        rho = Gr.sample().getMaterial().numberDensity
        factor = bsq / (4. * np.pi * rho)
        self.assertTrue(np.array_equal(x, input_x))
        self.assertTrue(np.allclose(y, factor * input_y / input_x))
        self.assertTrue(np.allclose(e, factor * input_e / input_x))
Exemple #7
0
    def test_material(self):
        SetSampleMaterial(self._ws,"Al2 O3",SampleMassDensity=4)
        material = self._ws.sample().getMaterial()

        self.assertAlmostEqual(material.numberDensity, 0.1181, places=4)
        self.assertAlmostEqual(material.relativeMolecularMass(), 101.961, places=3)

        atoms, numatoms = material.chemicalFormula()

        self.assertEquals(len(atoms), len(numatoms))
        self.assertEquals(len(atoms), 2)
        self.assertEquals(numatoms[0], 2)
        self.assertEquals(numatoms[1], 3)

        xs0 = atoms[0].neutron()
        xs1 = atoms[1].neutron()
        xs = ( xs0['coh_scatt_xs']*2 + xs1['coh_scatt_xs']*3 ) / 5
        self.assertAlmostEquals(material.cohScatterXSection(), xs, places=4)
    def test_material_already_defined(self):
        SetSampleMaterial(InputWorkspace=self._red_ws,
                          ChemicalFormula='H2-O',
                          SampleMassDensity=1.0)

        corrected = SimpleShapeMonteCarloAbsorption(
            InputWorkspace=self._red_ws,
            MaterialAlreadyDefined=True,
            EventsPerPoint=200,
            BeamHeight=3.5,
            BeamWidth=4.0,
            Height=2.0,
            Shape='FlatPlate',
            Width=2.0,
            Thickness=2.0)

        self._test_corrections_workspace(corrected)
        CompareWorkspaces(self._corrected_flat_plate,
                          corrected,
                          Tolerance=1e-6)
Exemple #9
0
    def setUp(self):
        # Create a workspace and change the sample material to non-trivial values.
        workspace_name = "testWS"
        CreateSampleWorkspace(OutputWorkspace=workspace_name)
        self.ws = AnalysisDataService.retrieve(workspace_name)
        self.formula = "C"
        self.number_density = 3.4e21
        self.absorption_xs = 3.5
        self.scattering_xs = 6.7
        self.coherent_xs = 3.2
        self.incoherent_xs = 3.4

        SetSampleMaterial(InputWorkspace=self.ws,
                          ChemicalFormula=self.formula,
                          SampleNumberDensity=self.number_density,
                          AttenuationXSection=self.absorption_xs,
                          ScatteringXSection=self.scattering_xs,
                          CoherentXSection=self.coherent_xs,
                          IncoherentXSection=self.incoherent_xs)

        self.presenter = SampleMaterialDialogPresenter(self.ws)
    incident_ws = 'incident_ws'
    GetIncidentSpectrumFromMonitor(runs[0], OutputWorkspace=incident_ws)

    # Fit incident spectrum
    incident_fit = 'incident_fit'
    fit_type = opts['FitSpectrumWith']
    FitIncidentSpectrum(InputWorkspace=incident_ws,
                        OutputWorkspace=incident_fit,
                        FitSpectrumWith=fit_type,
                        BinningForFit=opts['LambdaBinningForFit'],
                        BinningForCalc=opts['LambdaBinningForCalc'],
                        PlotDiagnostics=opts['PlotFittingDiagnostics'])

    # Set sample info
    SetSampleMaterial(incident_fit, ChemicalFormula=str(sample['Material']))
    CalculateElasticSelfScattering(InputWorkspace=incident_fit)
    atom_species = GetSampleSpeciesInfo(incident_fit)

    # Parameters for NOMAD detectors by bank
    L1 = 19.5
    banks = collections.OrderedDict()
    banks[0] = {'L2': 2.01, 'theta': 15.10}
    banks[1] = {'L2': 1.68, 'theta': 31.00}
    banks[2] = {'L2': 1.14, 'theta': 65.00}
    banks[3] = {'L2': 1.11, 'theta': 120.40}
    banks[4] = {'L2': 0.79, 'theta': 150.10}
    banks[5] = {'L2': 2.06, 'theta': 8.60}

    L2 = [x['L2'] for bank, x in banks.iteritems()]
    Polar = [x['theta'] for bank, x in banks.iteritems()]