-
Notifications
You must be signed in to change notification settings - Fork 122
/
IndirectCylinderAbsorption.py
268 lines (216 loc) · 12.6 KB
/
IndirectCylinderAbsorption.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# 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 +
#pylint: disable=no-init, too-many-instance-attributes
from mantid.simpleapi import *
from mantid.api import DataProcessorAlgorithm, AlgorithmFactory, MatrixWorkspaceProperty, WorkspaceGroupProperty, PropertyMode, Progress
from mantid.kernel import (StringMandatoryValidator, Direction, logger, FloatBoundedValidator,
IntBoundedValidator, MaterialBuilder, StringListValidator)
class IndirectCylinderAbsorption(DataProcessorAlgorithm):
# Sample variables
_sample_ws_name = None
_sample_chemical_formula = None
_sample_density_type = None
_sample_density = None
_sample_radius = None
# Container variables
_can_ws_name = None
_use_can_corrections = None
_can_chemical_formula = None
_can_density_type = None
_can_density = None
_can_radius = None
_can_scale = None
_events = None
_output_ws = None
_abs_ws = None
_ass_ws = None
_acc_ws = None
def category(self):
return "Workflow\\Inelastic;CorrectionFunctions\\AbsorptionCorrections;Workflow\\MIDAS"
def summary(self):
return "Calculates indirect absorption corrections for a cylinder sample shape."
def PyInit(self):
# Sample options
self.declareProperty(MatrixWorkspaceProperty('SampleWorkspace', '', direction=Direction.Input),
doc='Sample workspace.')
self.declareProperty(name='SampleChemicalFormula', defaultValue='', validator=StringMandatoryValidator(),
doc='Sample chemical formula')
self.declareProperty(name='SampleDensityType', defaultValue = 'Mass Density',
validator=StringListValidator(['Mass Density', 'Number Density']),
doc = 'Use of Mass density or Number density')
self.declareProperty(name='SampleDensity', defaultValue=0.1,
doc='Mass density (g/cm^3) or Number density (atoms/Angstrom^3)')
self.declareProperty(name='SampleRadius', defaultValue=0.1,
validator=FloatBoundedValidator(0.0),
doc='Sample radius')
# Container options
self.declareProperty(MatrixWorkspaceProperty('CanWorkspace', '', optional=PropertyMode.Optional,
direction=Direction.Input),
doc='Container workspace.')
self.declareProperty(name='UseCanCorrections', defaultValue=False,
doc='Use can corrections in subtraction')
self.declareProperty(name='CanChemicalFormula', defaultValue='',
doc='Can chemical formula')
self.declareProperty(name='CanDensityType', defaultValue = 'Mass Density',
validator=StringListValidator(['Mass Density', 'Number Density']),
doc = 'Use of Mass density or Number density')
self.declareProperty(name='CanDensity', defaultValue=0.1,
doc='Mass density (g/cm^3) or Number density (atoms/Angstrom^3)')
self.declareProperty(name='CanRadius', defaultValue=0.2,
validator=FloatBoundedValidator(0.0),
doc='Can radius')
self.declareProperty(name='CanScaleFactor', defaultValue=1.0,
validator=FloatBoundedValidator(0.0),
doc='Scale factor to multiply can data')
# General options
self.declareProperty(name='Events', defaultValue=5000,
validator=IntBoundedValidator(0),
doc='Number of neutron events')
# Output options
self.declareProperty(MatrixWorkspaceProperty('OutputWorkspace', '', direction=Direction.Output),
doc='The output corrected workspace.')
self.declareProperty(WorkspaceGroupProperty('CorrectionsWorkspace', '', direction=Direction.Output,
optional=PropertyMode.Optional),
doc='The corrections workspace for scattering and absorptions in sample.')
#pylint: disable=too-many-branches
def PyExec(self):
from IndirectCommon import getEfixed
self._setup()
# Set up progress reporting
n_prog_reports = 2
if self._can_ws_name is not None:
n_prog_reports += 1
prog = Progress(self, 0.0, 1.0, n_prog_reports)
efixed = getEfixed(self._sample_ws_name)
sample_wave_ws = '__sam_wave'
ConvertUnits(InputWorkspace=self._sample_ws_name, OutputWorkspace=sample_wave_ws,
Target='Wavelength', EMode='Indirect', EFixed=efixed, EnableLogging = False)
prog.report('Calculating sample corrections')
if self._sample_density_type == 'Mass Density':
builder = MaterialBuilder()
mat = builder.setFormula(self._sample_chemical_formula).setMassDensity(self._sample_density).build()
self._sample_density = mat.numberDensity
SetSampleMaterial(sample_wave_ws, ChemicalFormula=self._sample_chemical_formula, SampleNumberDensity=self._sample_density)
prog.report('Calculating sample corrections')
CylinderAbsorption(InputWorkspace=sample_wave_ws,
OutputWorkspace=self._ass_ws,
SampleNumberDensity=self._sample_density,
NumberOfWavelengthPoints=10,
CylinderSampleHeight=3.0,
CylinderSampleRadius=self._sample_radius,
NumberOfSlices=1,
NumberOfAnnuli=10)
group = self._ass_ws
if self._can_ws_name is not None:
can_wave_ws = '__can_wave'
ConvertUnits(InputWorkspace=self._can_ws_name, OutputWorkspace=can_wave_ws,
Target='Wavelength', EMode='Indirect', EFixed=efixed, EnableLogging = False)
if self._can_scale != 1.0:
logger.information('Scaling can by: ' + str(self._can_scale))
Scale(InputWorkspace=can_wave_ws, OutputWorkspace=can_wave_ws, Factor=self._can_scale, Operation='Multiply')
can_thickness = self._can_radius - self._sample_radius
logger.information('Container thickness: ' + str(can_thickness))
if self._use_can_corrections:
# Doing can corrections
prog.report('Calculating container corrections')
Divide(LHSWorkspace=sample_wave_ws, RHSWorkspace=self._ass_ws, OutputWorkspace=sample_wave_ws)
if self._sample_density_type == 'Mass Density':
builder = MaterialBuilder()
mat = builder.setFormula(self._can_chemical_formula).setMassDensity(self._can_density).build()
self._can_density = mat.numberDensity
SetSampleMaterial(can_wave_ws, ChemicalFormula=self._can_chemical_formula, SampleNumberDensity=self._can_density)
AnnularRingAbsorption(InputWorkspace=can_wave_ws,
OutputWorkspace=self._acc_ws,
SampleHeight=3.0,
SampleThickness=can_thickness,
CanInnerRadius=0.9*self._sample_radius,
CanOuterRadius=1.1*self._can_radius,
SampleChemicalFormula=self._can_chemical_formula,
SampleNumberDensity=self._can_density,
NumberOfWavelengthPoints=10,
EventsPerPoint=self._events)
Divide(LHSWorkspace=can_wave_ws, RHSWorkspace=self._acc_ws, OutputWorkspace=can_wave_ws)
Minus(LHSWorkspace=sample_wave_ws, RHSWorkspace=can_wave_ws, OutputWorkspace=sample_wave_ws)
group += ',' + self._acc_ws
else:
# Doing simple can subtraction
prog.report('Calculating container scaling')
Minus(LHSWorkspace=sample_wave_ws, RHSWorkspace=can_wave_ws, OutputWorkspace=sample_wave_ws)
Divide(LHSWorkspace=sample_wave_ws, RHSWorkspace=self._ass_ws, OutputWorkspace=sample_wave_ws)
DeleteWorkspace(can_wave_ws, EnableLogging = False)
else:
Divide(LHSWorkspace=sample_wave_ws, RHSWorkspace=self._ass_ws, OutputWorkspace=sample_wave_ws)
ConvertUnits(InputWorkspace=sample_wave_ws, OutputWorkspace=self._output_ws,
Target='DeltaE', EMode='Indirect', EFixed=efixed, EnableLogging = False)
DeleteWorkspace(sample_wave_ws, EnableLogging = False)
# Record sample logs
prog.report('Recording sample logs')
sample_log_workspaces = [self._output_ws, self._ass_ws]
sample_logs = [('sample_shape', 'cylinder'),
('sample_filename', self._sample_ws_name),
('sample_radius', self._sample_radius)]
if self._can_ws_name is not None:
sample_logs.append(('container_filename', self._can_ws_name))
sample_logs.append(('container_scale', self._can_scale))
if self._use_can_corrections:
sample_log_workspaces.append(self._acc_ws)
sample_logs.append(('container_thickness', can_thickness))
log_names = [item[0] for item in sample_logs]
log_values = [item[1] for item in sample_logs]
for ws_name in sample_log_workspaces:
AddSampleLogMultiple(Workspace=ws_name, LogNames=log_names, LogValues=log_values, EnableLogging = False)
self.setProperty('OutputWorkspace', self._output_ws)
# Output the Abs group workspace if it is wanted, delete if not
if self._abs_ws == '':
DeleteWorkspace(self._ass_ws, EnableLogging = False)
if self._can_ws_name is not None and self._use_can_corrections:
DeleteWorkspace(self._acc_ws, EnableLogging = False)
else:
GroupWorkspaces(InputWorkspaces=group, OutputWorkspace=self._abs_ws, EnableLogging = False)
self.setProperty('CorrectionsWorkspace', self._abs_ws)
def _setup(self):
"""
Get algorithm properties.
"""
self._sample_ws_name = self.getPropertyValue('SampleWorkspace')
self._sample_chemical_formula = self.getPropertyValue('SampleChemicalFormula')
self._sample_density_type = self.getPropertyValue('SampleDensityType')
self._sample_density = self.getProperty('SampleDensity').value
self._sample_radius = self.getProperty('SampleRadius').value
self._can_ws_name = self.getPropertyValue('CanWorkspace')
if self._can_ws_name == '':
self._can_ws_name = None
self._use_can_corrections = self.getProperty('UseCanCorrections').value
self._can_chemical_formula = self.getPropertyValue('CanChemicalFormula')
self._can_density_type = self.getPropertyValue('CanDensityType')
self._can_density = self.getProperty('CanDensity').value
self._can_radius = self.getProperty('CanRadius').value
self._can_scale = self.getProperty('CanScaleFactor').value
self._events = self.getPropertyValue('Events')
self._output_ws = self.getPropertyValue('OutputWorkspace')
self._abs_ws = self.getPropertyValue('CorrectionsWorkspace')
if self._abs_ws == '':
self._ass_ws = '__ass'
self._acc_ws = '__acc'
else:
self._ass_ws = self._abs_ws + '_ass'
self._acc_ws = self._abs_ws + '_acc'
def validateInputs(self):
"""
Validate algorithm options.
"""
self._setup()
issues = dict()
if self._sample_radius > self._can_radius:
issues['CanRadius'] = 'Must be greater than SampleRadius'
if self._use_can_corrections and self._can_chemical_formula == '':
issues['CanChemicalFormula'] = 'Must be set to use can corrections'
if self._use_can_corrections and self._can_ws_name is None:
issues['UseCanCorrections'] = 'Must specify a can workspace to use can corrections'
return issues
# Register algorithm with Mantid
AlgorithmFactory.subscribe(IndirectCylinderAbsorption)