forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaterialBuilder.cpp
436 lines (392 loc) · 15.2 KB
/
MaterialBuilder.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// 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 "MantidKernel/MaterialBuilder.h"
#include "MantidKernel/Atom.h"
#include "MantidKernel/EmptyValues.h"
#include "MantidKernel/NeutronAtom.h"
#include <memory>
#include <numeric>
namespace Mantid {
using PhysicalConstants::Atom;
using PhysicalConstants::getAtom;
using PhysicalConstants::NeutronAtom;
namespace Kernel {
namespace {
inline bool isEmpty(const boost::optional<double> &value) { return !value || value == Mantid::EMPTY_DBL(); }
constexpr auto LARGE_LAMBDA = 100; // Lambda likely to be beyond max lambda in
// any measured spectra. In Angstroms
} // namespace
/**
* Constructor
*/
MaterialBuilder::MaterialBuilder()
: m_name(), m_formula(), m_atomicNo(), m_massNo(0), m_numberDensity(), m_packingFraction(), m_zParam(), m_cellVol(),
m_massDensity(), m_totalXSection(), m_cohXSection(), m_incXSection(), m_absSection(),
m_numberDensityUnit(NumberDensityUnit::Atoms) {}
/**
* Set the string name given to the material
* @param name Human-readable name of the material. Empty string not allowed
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setName(const std::string &name) {
if (name.empty()) {
throw std::invalid_argument("MaterialBuilder::setName() - Empty name not allowed.");
}
m_name = name;
return *this;
}
/**
* Set the chemical formula of the material
* @param formula Human-readable name of the material
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setFormula(const std::string &formula) {
if (m_name.empty()) {
m_name = formula;
}
if (m_atomicNo) {
throw std::runtime_error("MaterialBuilder::setFormula() - Atomic no. "
"already set, cannot use formula aswell.");
}
if (formula.empty()) {
throw std::invalid_argument("MaterialBuilder::setFormula() - Empty formula provided.");
}
using ChemicalFormula = Material::ChemicalFormula;
try {
m_formula = ChemicalFormula(ChemicalFormula(Material::parseChemicalFormula(formula)));
} catch (std::runtime_error &exc) {
throw std::invalid_argument("MaterialBuilder::setFormula() - Unable to parse chemical formula: " +
std::string(exc.what()));
}
return *this;
}
/**
* Set the type of atom by its atomic number
* @param atomicNumber Z-number of the atom
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setAtomicNumber(int atomicNumber) {
if (!m_formula.empty()) {
throw std::runtime_error("MaterialBuilder::setAtomicNumber() - Formula "
"already set, cannot use atomic number aswell.");
}
m_atomicNo = atomicNumber;
return *this;
}
/**
* Set the isotope by mass number
* @param massNumber Isotope number of the atom
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setMassNumber(int massNumber) {
m_massNo = massNumber;
return *this;
}
/**
* Set the number density of the sample in atoms or formula units / Angstrom^3
* @param rho density of the sample in atoms or formula units / Angstrom^3
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setNumberDensity(double rho) {
if (rho != Mantid::EMPTY_DBL())
m_numberDensity = rho;
return *this;
}
/**
* Set the unit for number density
* @param unit atoms or formula units / Anstrom^3
* @return A reference to this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setNumberDensityUnit(NumberDensityUnit unit) {
m_numberDensityUnit = unit;
return *this;
}
/**
* Set the effective number density of the sample in atoms or formula units /
* Angstrom^3
* @param rho_eff effective density of the sample in atoms or formula units /
* Angstrom^3
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setEffectiveNumberDensity(double rho_eff) {
if (rho_eff != Mantid::EMPTY_DBL())
m_numberDensityEff = rho_eff;
return *this;
}
/**
* Set the packing fraction of the material (default is 1). This is used to
* infer the effective number density
*/
MaterialBuilder &MaterialBuilder::setPackingFraction(double fraction) {
if (fraction != Mantid::EMPTY_DBL())
m_packingFraction = fraction;
return *this;
}
/**
* Set the number of formula units in the unit cell
* @param zparam Number of formula units
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setZParameter(double zparam) {
m_zParam = zparam;
return *this;
}
/**
* Set the volume of unit cell
* @param cellVolume The volume of the unit cell
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setUnitCellVolume(double cellVolume) {
m_cellVol = cellVolume;
return *this;
}
/**
* Set the mass density of the sample in g / cc
* @param massDensity The mass density in g / cc
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setMassDensity(double massDensity) {
m_massDensity = massDensity;
return *this;
}
/**
* Set a value for the total scattering cross section
* @param xsec Value of the cross section
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setTotalScatterXSection(double xsec) {
if (xsec != Mantid::EMPTY_DBL())
m_totalXSection = xsec;
return *this;
}
/**
* Set a value for the coherent scattering cross section
* @param xsec Value of the cross section
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setCoherentXSection(double xsec) {
m_cohXSection = xsec;
return *this;
}
/**
* Set a value for the incoherent scattering cross section
* @param xsec Value of the cross section
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setIncoherentXSection(double xsec) {
m_incXSection = xsec;
return *this;
}
/**
* Set a value for the absorption cross section
* @param xsec Value of the cross section
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setAbsorptionXSection(double xsec) {
m_absSection = xsec;
return *this;
}
/**
* Set a value for the attenuation profile filename
* @param filename Name of the file containing the attenuation profile
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setAttenuationProfileFilename(std::string filename) {
if (!filename.empty()) {
m_attenuationProfileFileName = filename;
}
return *this;
}
/**
* Set a value for the attenuation profile filename
* @param filename Name of the file containing the attenuation profile
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setXRayAttenuationProfileFilename(std::string filename) {
if (!filename.empty()) {
m_xRayAttenuationProfileFileName = filename;
}
return *this;
}
/**
* Set a value for the attenuation profile search path
* @param path Path to search
*/
void MaterialBuilder::setAttenuationSearchPath(std::string path) { m_attenuationFileSearchPath = std::move(path); }
/**
* Build the new Material object from the current set of options
* @return A new Material object
*/
Material MaterialBuilder::build() const {
Material::ChemicalFormula formula;
if (!m_formula.empty()) {
formula = Material::ChemicalFormula(m_formula);
} else if (m_atomicNo) {
formula = createCompositionFromAtomicNumber();
} else if (!m_totalXSection || !m_cohXSection || !m_incXSection || !m_absSection || !m_numberDensity) {
throw std::runtime_error("Please specify one of chemical formula or atomic "
"number or all cross sections and a number "
"density.");
}
const auto density_struct = getOrCalculateRhoAndPacking(formula);
std::unique_ptr<Material> material;
if (hasOverrideNeutronProperties()) {
PhysicalConstants::NeutronAtom neutron = generateCustomNeutron();
material =
std::make_unique<Material>(m_name, neutron, density_struct.number_density, density_struct.packing_fraction);
} else {
material =
std::make_unique<Material>(m_name, formula, density_struct.number_density, density_struct.packing_fraction);
}
if (m_attenuationProfileFileName) {
AttenuationProfile materialAttenuation(m_attenuationProfileFileName.get(), m_attenuationFileSearchPath,
material.get(), LARGE_LAMBDA);
material->setAttenuationProfile(materialAttenuation);
}
if (m_xRayAttenuationProfileFileName) {
// don't supply a material so that extrapolation using the neutron tabulated
// attenuation data is turned off
AttenuationProfile materialAttenuation(m_xRayAttenuationProfileFileName.get(), m_attenuationFileSearchPath, nullptr,
-1);
material->setXRayAttenuationProfile(materialAttenuation);
}
return *material;
}
/**
* Create the NeutronAtom object from the atomic number
* @return A new NeutronAtom object with the defined proprties
*/
Material::ChemicalFormula MaterialBuilder::createCompositionFromAtomicNumber() const {
Material::FormulaUnit unit{std::make_shared<PhysicalConstants::Atom>(
getAtom(static_cast<uint16_t>(m_atomicNo.get()), static_cast<uint16_t>(m_massNo))),
1.};
Material::ChemicalFormula formula;
formula.emplace_back(unit);
return formula;
}
/**
* Return the manually set density or calculate it from other parameters
* @param formula The chemical formula to calculate the number density from
* @return The number density in atoms / Angstrom^3
*/
MaterialBuilder::density_packing
MaterialBuilder::getOrCalculateRhoAndPacking(const Material::ChemicalFormula &formula) const {
// set packing fraction and both number densities to zero to start
density_packing result{0., 0., 0.};
// get the packing fraction
if (m_packingFraction)
result.packing_fraction = m_packingFraction.get();
// if effective density has been specified
if (m_numberDensityEff)
result.effective_number_density = m_numberDensityEff.get();
// total number of atoms is used in both density calculations
const double totalNumAtoms =
std::accumulate(formula.cbegin(), formula.cend(), 0.,
[](double n, const Material::FormulaUnit &f) { return n + f.multiplicity; });
// calculate the number density by one of many ways
if (m_numberDensity) {
result.number_density = m_numberDensity.get();
if (m_numberDensityUnit == NumberDensityUnit::FormulaUnits && totalNumAtoms > 0.) {
result.number_density = m_numberDensity.get() * totalNumAtoms;
}
} else if (m_zParam && m_cellVol) {
result.number_density = totalNumAtoms * m_zParam.get() / m_cellVol.get();
} else if (!m_formula.empty() && m_formula.size() == 1) {
result.number_density = m_formula.front().atom->number_density;
}
// calculate the effective number density
if (m_massDensity) {
// g / cc -> atoms / Angstrom^3
const double rmm =
std::accumulate(formula.cbegin(), formula.cend(), 0.,
[](double sum, const Material::FormulaUnit &f) { return sum + f.atom->mass * f.multiplicity; });
result.effective_number_density = (m_massDensity.get() * totalNumAtoms / rmm) * PhysicalConstants::N_A * 1e-24;
}
// count the number of values that were set and generate errors
int count = 0;
if (result.packing_fraction > 0.)
count++;
if (result.effective_number_density > 0.)
count++;
if (result.number_density > 0.)
count++;
// use this information to set the "missing" of the 3
if (count == 0) {
throw std::runtime_error("The number density could not be determined. Please "
"provide the number density, ZParameter and unit "
"cell volume or mass density.");
} else if (count == 1) {
result.packing_fraction = 1.;
if (result.number_density > 0.)
result.effective_number_density = result.number_density;
else if (result.effective_number_density > 0.)
result.number_density = result.effective_number_density;
else
throw std::runtime_error("Must specify the number density in some way");
} else if (count == 2) {
if (result.number_density > 0.) {
if (result.effective_number_density > 0.)
result.packing_fraction = result.effective_number_density / result.number_density;
else if (result.packing_fraction > 0.)
result.effective_number_density = result.packing_fraction * result.number_density;
} else if (result.effective_number_density > 0.) {
if (result.number_density > 0.)
result.packing_fraction = result.effective_number_density / result.number_density;
else if (result.packing_fraction > 0.)
result.number_density = result.effective_number_density / result.packing_fraction;
}
// do something
} else if (count == 3) {
throw std::runtime_error("The number density and effective density were over-determined");
}
return result;
}
bool MaterialBuilder::hasOverrideNeutronProperties() const {
return !isEmpty(m_totalXSection) || !isEmpty(m_cohXSection) || !isEmpty(m_incXSection) || !isEmpty(m_absSection);
}
PhysicalConstants::NeutronAtom MaterialBuilder::generateCustomNeutron() const {
NeutronAtom neutronAtom(0, 0., 0., 0., 0., 0., 0.);
// generate the default neutron
if (m_atomicNo) {
auto atom = getAtom(static_cast<uint16_t>(m_atomicNo.get()), static_cast<uint16_t>(m_massNo));
neutronAtom = atom.neutron;
overrideNeutronProperties(neutronAtom);
} else if (!m_formula.empty()) {
double totalNumAtoms = 0.;
for (const auto &formulaUnit : m_formula) {
neutronAtom = neutronAtom + formulaUnit.multiplicity * formulaUnit.atom->neutron;
totalNumAtoms += formulaUnit.multiplicity;
}
neutronAtom = (1. / totalNumAtoms) * neutronAtom;
overrideNeutronProperties(neutronAtom);
} else {
neutronAtom.coh_scatt_xs = *m_cohXSection;
neutronAtom.inc_scatt_xs = *m_incXSection;
neutronAtom.tot_scatt_xs = *m_totalXSection;
neutronAtom.abs_scatt_xs = *m_absSection;
calculateScatteringLengths(neutronAtom);
}
neutronAtom.a_number = 0; // signifies custom neutron atom
neutronAtom.z_number = 0; // signifies custom neutron atom
return neutronAtom;
}
/**
* Override default neutron properties with those supplied
* @param neutron A reference to a NeutronAtom object
*/
void MaterialBuilder::overrideNeutronProperties(PhysicalConstants::NeutronAtom &neutron) const {
if (!isEmpty(m_totalXSection))
neutron.tot_scatt_xs = m_totalXSection.get();
if (!isEmpty(m_cohXSection))
neutron.coh_scatt_xs = m_cohXSection.get();
if (!isEmpty(m_incXSection))
neutron.inc_scatt_xs = m_incXSection.get();
if (!isEmpty(m_absSection))
neutron.abs_scatt_xs = m_absSection.get();
}
} // namespace Kernel
} // namespace Mantid