Ticket #8176: IndirectTransmission.py

File IndirectTransmission.py, 4.5 KB (added by Samuel Jackson, 7 years ago)

Script to be added

Line 
1"""*WIKI*
2
3Calculates the scattering & transmission for Indirect Geometry spectrometers. The sample chemical formula is input for the SetSampleMaterial algorithm to calculate the cross-sections.
4The instrument analyser reflection is selected to obtain the wavelength to calculate the absorption cross-section. The sample number density & thickness is input to then calculate the percentage scattering & transmission.
5
6*WIKI*"""
7
8from mantid.simpleapi import *
9from mantid.api import PythonAlgorithm, AlgorithmFactory
10from mantid.kernel import StringListValidator, StringMandatoryValidator
11from mantid import config
12from IndirectCommon import StartTime, EndTime
13import os.path, math
14
15class IndirectTransmission(PythonAlgorithm):
16 
17        def category(self):
18                return "Workflow\\MIDAS;PythonAlgorithms"
19
20        def PyInit(self):
21                self.declareProperty(name='Instrument',defaultValue='IRIS',validator=StringListValidator(['IRIS','OSIRIS']), doc='Instrument')
22                self.declareProperty(name='Analyser',defaultValue='graphite',validator=StringListValidator(['graphite','fmica']), doc='Analyser')
23                self.declareProperty(name='Reflection',defaultValue='002',validator=StringListValidator(['002','004']), doc='Reflection')
24                self.declareProperty(name='Chemical Formula',defaultValue='',validator=StringMandatoryValidator(), doc='Sample chemical formula')
25                self.declareProperty(name='Number Density', defaultValue=0.1, doc='Number denisty. Default=0.1')
26                self.declareProperty(name='Thickness', defaultValue=0.1, doc='Sample thickness. Default=0.1')
27 
28        def PyExec(self):
29
30                StartTime('IndirectTransmission')
31               
32                instrumentName = self.getPropertyValue('Instrument')
33                analyser = self.getPropertyValue('Analyser')
34                reflection = self.getPropertyValue('Reflection')
35                formula = self.getPropertyValue('Chemical Formula')
36                density = self.getPropertyValue('Number Density')
37                thickness = self.getPropertyValue('Thickness')
38               
39                #Load instrument defintion file
40                idfDir = config['instrumentDefinition.directory']
41                idf = idfDir + instrumentName + '_Definition.xml'
42                workspace = '__empty_'+instrumentName
43                LoadEmptyInstrument(OutputWorkspace=workspace, Filename=idf)
44
45                #Load instrument parameter file
46                nameStem = instrumentName + '_' + analyser + '_' + reflection
47                ipf = idfDir + nameStem + '_Parameters.xml'
48                LoadParameterFile(Workspace=workspace, Filename=ipf)
49
50                #Get efixed value
51                instrument = mtd[workspace].getInstrument()
52                efixed = instrument.getNumberParameter('efixed-val')[0]
53
54                logger.notice('Analyser : ' +analyser+reflection +' with energy = ' + str(efixed))
55
56                result = SetSampleMaterial(InputWorkspace=workspace,ChemicalFormula=formula)
57
58                #elastic wavelength
59                wave=1.8*math.sqrt(25.2429/efixed)
60
61                absorptionXSection = result[5]*wave/1.7982
62                coherentXSection = result[4]
63                incoherentXSection = result[3]
64                scatteringXSection = result[3]
65
66                thickness = float(thickness)
67                density = float(density)
68
69                totalXSection = absorptionXSection + scatteringXSection
70
71                transmission = math.exp(-density*totalXSection*thickness)
72                scattering = 1.0 - math.exp(-density*scatteringXSection*thickness)
73               
74                #Create table workspace to store calculations
75                tableWs = nameStem + '_Transmission'
76                tableWs = CreateEmptyTableWorkspace(OutputWorkspace=tableWs)
77                tableWs.addColumn("str", "Name")
78                tableWs.addColumn("str", "Value")
79
80                #Add values to table workspace
81                tableWs.addRow(['Absorption Xsection at wavelength ', str(wave) +
82                        ' A = '+str(absorptionXSection)])
83                tableWs.addRow(['Coherent Xsection', str(coherentXSection)])
84                tableWs.addRow(['Incoherent Xsection', str(incoherentXSection)])
85                tableWs.addRow(['Total scattering Xsection', str(scatteringXSection)])
86                tableWs.addRow(['Number density', str(density)])
87                tableWs.addRow(['Thickness', str(thickness)])
88                tableWs.addRow(['Transmission (abs+scatt)', str(transmission)])
89                tableWs.addRow(['Total scattering', str(scattering)])
90
91                #Output values to the results log
92                logger.notice('Absorption Xsection at wavelength ' + str(wave) +
93                        ' A = '+str(absorptionXSection))
94                logger.notice('Coherent Xsection = '+ str(coherentXSection))
95                logger.notice('Incoherent Xsection = ' + str(incoherentXSection))
96                logger.notice('Total scattering Xsection = ' + str(scatteringXSection))
97                logger.notice('Number density = ' + str(density))
98                logger.notice('Thickness = ' + str(thickness))
99                logger.notice('Transmission (abs+scatt) = ' +str(transmission))
100                logger.notice('Total scattering = ' +str(scattering))
101
102                #remove idf/ipf workspace
103                DeleteWorkspace(workspace)
104
105                EndTime('IndirectTransmission')
106
107# Register algorithm with Mantid
108AlgorithmFactory.subscribe(IndirectTransmission)