forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
IndirectAnnulusAbsorption.py
314 lines (260 loc) · 15.5 KB
/
IndirectAnnulusAbsorption.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# 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, PropertyMode, Progress, WorkspaceGroupProperty
from mantid.kernel import (StringMandatoryValidator, Direction, logger, IntBoundedValidator,
FloatBoundedValidator, StringListValidator, MaterialBuilder)
class IndirectAnnulusAbsorption(DataProcessorAlgorithm):
# Sample variables
_sample_ws_name = ''
_sample_chemical_formula = ''
_sample_density_type = None
_sample_density = 0.0
_sample_inner_radius = 0.0
_sample_outer_radius = 0.0
# Container variables
_can_ws_name = ''
_can_chemical_formula = ''
_can_density_type = None
_can_density = 0.0
_can_inner_radius = 0.0
_can_outer_radius = 0.0
_use_can_corrections = False
_can_scale = 0.0
_output_ws = None
_ass_ws = None
_abs_ws = None
_acc_ws = None
_events = 0
def category(self):
return "Workflow\\Inelastic;CorrectionFunctions\\AbsorptionCorrections;Workflow\\MIDAS"
def summary(self):
return "Calculates indirect absorption corrections for an annulus 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='SampleInnerRadius', defaultValue=0.2,
validator=FloatBoundedValidator(0.0),
doc='Sample radius')
self.declareProperty(name='SampleOuterRadius', defaultValue=0.25,
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='Chemical formula for the can')
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='CanInnerRadius', defaultValue=0.19,
validator=FloatBoundedValidator(0.0),
doc='Sample radius')
self.declareProperty(name='CanOuterRadius', defaultValue=0.26,
validator=FloatBoundedValidator(0.0),
doc='Sample 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)
sample_thickness = self._sample_outer_radius - self._sample_inner_radius
logger.information('Sample thickness: ' + str(sample_thickness))
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)
AnnularRingAbsorption(InputWorkspace=sample_wave_ws,
OutputWorkspace=self._ass_ws,
SampleHeight=3.0,
SampleThickness=sample_thickness,
CanInnerRadius=self._can_inner_radius,
CanOuterRadius=self._can_outer_radius,
SampleChemicalFormula=self._sample_chemical_formula,
SampleNumberDensity=self._sample_density,
NumberOfWavelengthPoints=10,
EventsPerPoint=self._events)
group = self._ass_ws
if self._can_ws_name is not None:
can1_wave_ws = '__can1_wave'
can2_wave_ws = '__can2_wave'
ConvertUnits(InputWorkspace=self._can_ws_name, OutputWorkspace=can1_wave_ws,
Target='Wavelength', EMode='Indirect', EFixed=efixed, EnableLogging=False)
if self._can_scale != 1.0:
logger.information('Scaling container by: ' + str(self._can_scale))
Scale(InputWorkspace=can1_wave_ws, OutputWorkspace=can1_wave_ws, Factor=self._can_scale, Operation='Multiply')
CloneWorkspace(InputWorkspace=can1_wave_ws, OutputWorkspace=can2_wave_ws, EnableLogging=False)
can_thickness_1 = self._sample_inner_radius - self._can_inner_radius
can_thickness_2 = self._can_outer_radius - self._sample_outer_radius
logger.information('Container thickness: %f & %f' % (can_thickness_1, can_thickness_2))
if self._use_can_corrections:
prog.report('Calculating container corrections')
Divide(LHSWorkspace=sample_wave_ws, RHSWorkspace=self._ass_ws, OutputWorkspace=sample_wave_ws)
if self._can_density_type == 'Mass Density':
builder = MaterialBuilder()
mat = builder.setFormula(self._can_chemical_formula).setMassDensity(self._can_density).build()
self._can_density = mat.numberDensity
SetSampleMaterial(can1_wave_ws, ChemicalFormula=self._can_chemical_formula,
SampleNumberDensity=self._can_density)
AnnularRingAbsorption(InputWorkspace=can1_wave_ws,
OutputWorkspace='__Acc1',
SampleHeight=3.0,
SampleThickness=can_thickness_1,
CanInnerRadius=self._can_inner_radius,
CanOuterRadius=self._sample_outer_radius,
SampleChemicalFormula=self._can_chemical_formula,
SampleNumberDensity=self._can_density,
NumberOfWavelengthPoints=10,
EventsPerPoint=self._events)
SetSampleMaterial(can2_wave_ws, ChemicalFormula=self._can_chemical_formula, SampleNumberDensity=self._can_density)
AnnularRingAbsorption(InputWorkspace=can2_wave_ws,
OutputWorkspace='__Acc2',
SampleHeight=3.0,
SampleThickness=can_thickness_2,
CanInnerRadius=self._sample_inner_radius,
CanOuterRadius=self._can_outer_radius,
SampleChemicalFormula=self._can_chemical_formula,
SampleNumberDensity=self._can_density,
NumberOfWavelengthPoints=10,
EventsPerPoint=self._events)
Multiply(LHSWorkspace='__Acc1', RHSWorkspace='__Acc2', OutputWorkspace=self._acc_ws, EnableLogging=False)
DeleteWorkspace('__Acc1', EnableLogging=False)
DeleteWorkspace('__Acc2', EnableLogging=False)
Divide(LHSWorkspace=can1_wave_ws, RHSWorkspace=self._acc_ws, OutputWorkspace=can1_wave_ws)
Minus(LHSWorkspace=sample_wave_ws, RHSWorkspace=can1_wave_ws, OutputWorkspace=sample_wave_ws)
group += ',' + self._acc_ws
else:
prog.report('Calculating can scaling')
Minus(LHSWorkspace=sample_wave_ws, RHSWorkspace=can1_wave_ws, OutputWorkspace=sample_wave_ws)
Divide(LHSWorkspace=sample_wave_ws, RHSWorkspace=self._ass_ws, OutputWorkspace=sample_wave_ws)
DeleteWorkspace(can1_wave_ws, EnableLogging=False)
DeleteWorkspace(can2_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)
prog.report('Recording sample logs')
sample_log_workspaces = [self._output_ws, self._ass_ws]
sample_logs = [('sample_shape', 'annulus'),
('sample_filename', self._sample_ws_name),
('sample_inner', self._sample_inner_radius),
('sample_outer', self._sample_outer_radius),
('can_inner', self._can_inner_radius),
('can_outer', self._can_outer_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_1', can_thickness_1))
sample_logs.append(('container_thickness_2', can_thickness_2))
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 Ass 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_inner_radius = self.getProperty('SampleInnerRadius').value
self._sample_outer_radius = self.getProperty('SampleOuterRadius').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_inner_radius = self.getProperty('CanInnerRadius').value
self._can_outer_radius = self.getProperty('CanOuterRadius').value
self._can_scale = self.getProperty('CanScaleFactor').value
self._events = self.getProperty('Events').value
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._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'
# Geometry validation: can inner < sample inner < sample outer < can outer
if self._sample_inner_radius < self._can_inner_radius:
issues['SampleInnerRadius'] = 'Must be greater than CanInnerRadius'
if self._sample_outer_radius < self._sample_inner_radius:
issues['SampleOuterRadius'] = 'Must be greater than SampleInnerRadius'
if self._can_outer_radius < self._sample_outer_radius:
issues['CanOuterRadius'] = 'Must be greater than SampleOuterRadius'
return issues
# Register algorithm with Mantid
AlgorithmFactory.subscribe(IndirectAnnulusAbsorption)