/
SetSampleMaterial.cpp
239 lines (217 loc) · 11.6 KB
/
SetSampleMaterial.cpp
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
// 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 +
#include "MantidDataHandling/SetSampleMaterial.h"
#include "MantidAPI/ExperimentInfo.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/Sample.h"
#include "MantidAPI/Workspace.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include "MantidKernel/Atom.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/EnabledWhenProperty.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MandatoryValidator.h"
#include "MantidKernel/Material.h"
#include "MantidKernel/PhysicalConstants.h"
using namespace Mantid::PhysicalConstants;
namespace Mantid::DataHandling {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(SetSampleMaterial)
const std::string SetSampleMaterial::name() const { return "SetSampleMaterial"; }
int SetSampleMaterial::version() const { return (1); }
const std::string SetSampleMaterial::category() const { return "Sample"; }
using namespace Mantid::DataHandling;
using namespace Mantid::API;
using namespace Kernel;
/**
* Initialize the algorithm
*/
void SetSampleMaterial::init() {
using namespace Mantid::Kernel;
declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("InputWorkspace", "", Direction::InOut),
"The workspace with which to associate the sample ");
declareProperty("ChemicalFormula", "", "The chemical formula, see examples in documentation");
declareProperty("AtomicNumber", 0, "The atomic number");
declareProperty("MassNumber", 0, "Mass number if ion (use 0 for default mass number)");
auto mustBePositive = std::make_shared<BoundedValidator<double>>();
mustBePositive->setLower(0.0);
declareProperty("SampleNumberDensity", EMPTY_DBL(), mustBePositive,
"This number density of the sample in number of "
"atoms or formula units per cubic Angstrom will be used instead of "
"calculated");
declareProperty("SampleEffectiveNumberDensity", EMPTY_DBL(), mustBePositive,
"Defines the effective number density of the sample, which is "
"related to the number density and packing fraction.");
declareProperty("SamplePackingFraction", EMPTY_DBL(), mustBePositive,
"Defines the packing fraction of the sample which can be used "
"to calculate the number density and the effective number density");
declareProperty("ZParameter", EMPTY_DBL(), mustBePositive, "Number of formula units in unit cell");
declareProperty("UnitCellVolume", EMPTY_DBL(), mustBePositive,
"Unit cell volume in Angstoms^3. Will be calculated from the "
"OrientedLattice if not supplied.");
declareProperty("CoherentXSection", EMPTY_DBL(), mustBePositive,
"This coherent cross-section for the sample "
"material in barns will be used instead of tabulated");
declareProperty("IncoherentXSection", EMPTY_DBL(), mustBePositive,
"This incoherent cross-section for the sample "
"material in barns will be used instead of tabulated");
declareProperty("AttenuationXSection", EMPTY_DBL(), mustBePositive,
"This absorption cross-section for the sample "
"material in barns will be used instead of tabulated");
declareProperty("ScatteringXSection", EMPTY_DBL(), mustBePositive,
"Optional: This total scattering cross-section (coherent + "
"incoherent) for the sample material in barns will be used "
"instead of tabulated");
const std::vector<std::string> extensions{".DAT"};
declareProperty(std::make_unique<FileProperty>("AttenuationProfile", "", FileProperty::OptionalLoad, extensions),
"The path name of the file containing the attenuation profile");
declareProperty(std::make_unique<FileProperty>("XRayAttenuationProfile", "", FileProperty::OptionalLoad, extensions),
"The path name of the file containing the Xray attenuation profile");
declareProperty("SampleMassDensity", EMPTY_DBL(), mustBePositive,
"Measured mass density in g/cubic cm of the sample "
"to be used to calculate the effective number density.");
declareProperty("SampleMass", EMPTY_DBL(), mustBePositive,
"Measured mass in g of the sample. This is used with the SampleVolume "
"to calculate the number density.");
declareProperty("SampleVolume", EMPTY_DBL(), mustBePositive,
"Measured volume in gm^3 of the sample. This is used with the SampleMass "
"to calculate the number density.");
const std::vector<std::string> units({"Atoms", "Formula Units"});
declareProperty("NumberDensityUnit", units.front(), std::make_shared<StringListValidator>(units),
"Choose which units SampleNumberDensity referes to.");
// Perform Group Associations.
std::string formulaGrp("By Formula or Atomic Number");
setPropertyGroup("ChemicalFormula", formulaGrp);
setPropertyGroup("AtomicNumber", formulaGrp);
setPropertyGroup("MassNumber", formulaGrp);
std::string densityGrp("Sample Density");
setPropertyGroup("SampleNumberDensity", densityGrp);
setPropertyGroup("SampleEffectiveNumberDensity", densityGrp);
setPropertyGroup("SamplePackingFraction", densityGrp);
setPropertyGroup("NumberDensityUnit", densityGrp);
setPropertyGroup("ZParameter", densityGrp);
setPropertyGroup("UnitCellVolume", densityGrp);
setPropertyGroup("SampleMassDensity", densityGrp);
setPropertyGroup("SampleMass", densityGrp);
setPropertyGroup("SampleVolume", densityGrp);
std::string specificValuesGrp("Override Cross Section Values");
setPropertyGroup("CoherentXSection", specificValuesGrp);
setPropertyGroup("IncoherentXSection", specificValuesGrp);
setPropertyGroup("AttenuationXSection", specificValuesGrp);
setPropertyGroup("ScatteringXSection", specificValuesGrp);
setPropertyGroup("AttenuationProfile", specificValuesGrp);
setPropertyGroup("XRayAttenuationProfile", specificValuesGrp);
// Extra property settings
setPropertySettings("ChemicalFormula",
std::make_unique<Kernel::EnabledWhenProperty>("AtomicNumber", Kernel::IS_DEFAULT));
setPropertySettings("AtomicNumber",
std::make_unique<Kernel::EnabledWhenProperty>("ChemicalFormula", Kernel::IS_DEFAULT));
setPropertySettings("MassNumber",
std::make_unique<Kernel::EnabledWhenProperty>("ChemicalFormula", Kernel::IS_DEFAULT));
setPropertySettings("NumberDensityUnit",
std::make_unique<Kernel::EnabledWhenProperty>("SampleNumberDensity", Kernel::IS_NOT_DEFAULT));
}
std::map<std::string, std::string> SetSampleMaterial::validateInputs() {
params.chemicalSymbol = getPropertyValue("ChemicalFormula");
params.atomicNumber = getProperty("AtomicNumber");
params.massNumber = getProperty("MassNumber");
params.numberDensity = getProperty("SampleNumberDensity");
params.numberDensityEffective = getProperty("SampleEffectiveNumberDensity");
params.packingFraction = getProperty("SamplePackingFraction");
params.zParameter = getProperty("ZParameter");
params.unitCellVolume = getProperty("UnitCellVolume");
params.massDensity = getProperty("SampleMassDensity");
params.mass = getProperty("SampleMass");
params.volume = getProperty("SampleVolume");
params.coherentXSection = getProperty("CoherentXSection");
params.incoherentXSection = getProperty("IncoherentXSection");
params.attenuationXSection = getProperty("AttenuationXSection");
params.scatteringXSection = getProperty("ScatteringXSection");
params.attenuationProfileFileName = getPropertyValue("AttenuationProfile");
params.xRayAttenuationProfileFileName = getPropertyValue("XRayAttenuationProfile");
const std::string numberDensityUnit = getProperty("NumberDensityUnit");
if (numberDensityUnit == "Atoms") {
params.numberDensityUnit = MaterialBuilder::NumberDensityUnit::Atoms;
} else {
params.numberDensityUnit = MaterialBuilder::NumberDensityUnit::FormulaUnits;
}
auto result = ReadMaterial::validateInputs(params);
return result;
}
/**
* Add the cross sections to the neutron atom if they are not-empty
* numbers. All values are in barns.
*
* @param neutron The neutron to update
* @param coh_xs Coherent cross section
* @param inc_xs Incoherent cross section
* @param abs_xs Absorption cross section
* @param tot_xs Total scattering cross section
*/
void SetSampleMaterial::fixNeutron(NeutronAtom &neutron, double coh_xs, double inc_xs, double abs_xs, double tot_xs) {
if (!isEmpty(coh_xs))
neutron.coh_scatt_xs = coh_xs;
if (!isEmpty(inc_xs))
neutron.inc_scatt_xs = inc_xs;
if (!isEmpty(abs_xs))
neutron.abs_scatt_xs = abs_xs;
if (!isEmpty(tot_xs))
neutron.tot_scatt_xs = tot_xs;
}
/**
* Execute the algorithm
*/
void SetSampleMaterial::exec() {
// Get the input workspace
Workspace_sptr workspace = getProperty("InputWorkspace");
// an ExperimentInfo object has a sample
ExperimentInfo_sptr expInfo = std::dynamic_pointer_cast<ExperimentInfo>(workspace);
if (!expInfo) {
throw std::runtime_error("InputWorkspace does not have a sample object");
}
ReadMaterial reader;
reader.setMaterialParameters(params);
// get the scattering information - this will override table values
// create the material
auto material = reader.buildMaterial();
// calculate derived values
const double bcoh_avg_sq = material->cohScatterLengthSqrd(); // <b>
const double btot_sq_avg = material->totalScatterLengthSqrd(); // <b^2>
double normalizedLaue = (btot_sq_avg - bcoh_avg_sq) / bcoh_avg_sq;
if (btot_sq_avg == bcoh_avg_sq)
normalizedLaue = 0.;
// set the material but leave the geometry unchanged
auto shapeObject = std::shared_ptr<Geometry::IObject>(expInfo->sample().getShape().cloneWithMaterial(*material));
expInfo->mutableSample().setShape(shapeObject);
g_log.information() << "Sample number density ";
if (isEmpty(material->numberDensity())) {
g_log.information() << "was not specified\n";
} else {
g_log.information() << "= " << material->numberDensity() << " atoms/Angstrom^3\n";
}
g_log.information() << "Cross sections for wavelength = " << NeutronAtom::ReferenceLambda << " Angstroms\n"
<< " Coherent " << material->cohScatterXSection() << " barns\n"
<< " Incoherent " << material->incohScatterXSection() << " barns\n"
<< " Total " << material->totalScatterXSection() << " barns\n"
<< " Absorption " << material->absorbXSection() << " barns\n"
<< "PDF terms\n"
<< " <b_coh>^2 = " << bcoh_avg_sq << "\n"
<< " <b_tot^2> = " << btot_sq_avg << "\n"
<< " L = " << normalizedLaue << "\n";
if (isDefault("SampleNumberDensity") && isDefault("SampleMassDensity")) {
g_log.information("Unknown value for number density");
} else {
const double rho = material->numberDensity();
double smu = material->totalScatterXSection() * rho;
double amu = material->absorbXSection(NeutronAtom::ReferenceLambda) * rho;
g_log.information() << "Anvred LinearScatteringCoef = " << smu << " 1/cm\n"
<< "Anvred LinearAbsorptionCoef = " << amu << " 1/cm\n";
}
// Done!
progress(1);
}
} // namespace Mantid::DataHandling