Newer
Older
#include "MantidKernel/MaterialBuilder.h"
#include "MantidKernel/Atom.h"
#include "MantidKernel/EmptyValues.h"
#include "MantidKernel/NeutronAtom.h"
#include "MantidKernel/make_unique.h"
#include <boost/make_shared.hpp>
#include <iostream>
namespace Mantid {
using PhysicalConstants::Atom;
using PhysicalConstants::NeutronAtom;
using PhysicalConstants::getAtom;
using PhysicalConstants::getNeutronAtom;
namespace Kernel {
namespace {
inline bool isEmpty(const boost::optional<double> value) {
if (!value)
return true;
return (value == Mantid::EMPTY_DBL());
}
}
/**
* Constructor
*/
MaterialBuilder::MaterialBuilder()
: m_name(), m_formula(), m_atomicNo(), m_massNo(0), m_numberDensity(),
m_zParam(), m_cellVol(), m_massDensity(), m_totalXSection(),
m_cohXSection(), m_incXSection(), m_absSection() {}
/**
* 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 checmical formula 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.");
}
typedef Material::ChemicalFormula ChemicalFormula;
try {
m_formula = Mantid::Kernel::make_unique<ChemicalFormula>(
ChemicalFormula(Material::parseChemicalFormula(formula)));
"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) {
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
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setNumberDensity(double rho) {
m_numberDensity = rho;
return *this;
}
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
/**
* Set the number of atoms in the unit cell
* @param zparam Number of atoms
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setZParameter(double zparam) {
if (m_massDensity) {
throw std::runtime_error("MaterialBuilder::setZParameter() - Mass density "
"already set, cannot use Z parameter as well.");
}
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) {
if (m_massDensity) {
throw std::runtime_error(
"MaterialBuilder::setUnitCellVolume() - Mass density "
"already set, cannot use unit cell volume as well.");
}
m_cellVol = cellVolume;
return *this;
}
/**
* Set the mass density of the sample and calculate the density from this
* @param massDensity The mass density value
* @return A reference to the this object to allow chaining
*/
MaterialBuilder &MaterialBuilder::setMassDensity(double massDensity) {
if (m_zParam) {
throw std::runtime_error("MaterialBuilder::setMassDensity() - Z parameter "
"already set, cannot use mass density as well.");
}
if (m_cellVol) {
throw std::runtime_error(
"MaterialBuilder::setMassDensity() - Unit cell "
"volume already set, cannot use mass density as well.");
}
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;
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
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;
}
/**
* Build the new Material object from the current set of options
* @return A new Material object
*/
Material MaterialBuilder::build() const {
Material::ChemicalFormula formula;
double density;
formula = Material::ChemicalFormula(*m_formula);
formula = createCompositionFromAtomicNumber();
} else {
throw std::runtime_error("MaterialBuilder::createNeutronAtom() - Please "
"specify one of chemical formula or atomic "
"number.");
}
density = getOrCalculateRho(formula);
if (hasOverrideNeutronProperties()) {
PhysicalConstants::NeutronAtom neutron = generateCustomNeutron();
return Material(m_name, neutron, density);
return Material(m_name, formula, density);
}
}
/**
* Create the NeutronAtom object from the atomic number
* @return A new NeutronAtom object with the defined proprties
*/
MaterialBuilder::createCompositionFromAtomicNumber() const {
Material::FormulaUnit unit {boost::make_shared<PhysicalConstants::Atom>(getAtom(static_cast<uint16_t>(m_atomicNo.get()),
static_cast<uint16_t>(m_massNo))), 1.};
Material::ChemicalFormula formula;
formula.push_back(unit);
return formula;
}
/**
* Return the manually set density or calculate it from other parameters
* @param totalNumAtoms Total number of atoms
* @param rmm The relative molecular mass
* @return The number density
*/
double MaterialBuilder::getOrCalculateRho(const Material::ChemicalFormula &formula) const {
// first calculate the total number of atoms
double totalNumAtoms = 0.;
for (const auto &formulaUnit : formula) {
totalNumAtoms += formulaUnit.multiplicity;
}
if (m_numberDensity) {
return m_numberDensity.get();
} else if (m_zParam && m_cellVol) {
return totalNumAtoms * m_zParam.get() / m_cellVol.get();
} else if (m_massDensity) {
double rmm = 0.;
for ( const auto & formulaUnit : formula) {
rmm += formulaUnit.atom->mass * formulaUnit.multiplicity;
}
return (m_massDensity.get() / rmm) * PhysicalConstants::N_A * 1e-24;
} else if(m_formula && m_formula->size() == 1) {
return m_formula->at(0).atom->number_density;
} else {
return EMPTY_DBL();
}
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
bool MaterialBuilder::hasOverrideNeutronProperties() const {
if (!isEmpty(m_totalXSection))
return true;
if (!isEmpty(m_cohXSection))
return true;
if (!isEmpty(m_incXSection))
return true;
if (!isEmpty(m_absSection))
return true;
return false;
}
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;
} else {
double totalNumAtoms = 0.;
for (const auto & formulaUnit : *m_formula) {
neutronAtom = neutronAtom + formulaUnit.multiplicity * formulaUnit.atom->neutron;
totalNumAtoms += formulaUnit.multiplicity;
}
neutronAtom = (1. / totalNumAtoms) * neutronAtom;
}
neutronAtom.a_number = 0; // signifies custom neutron atom
overrideNeutronProperties(neutronAtom);
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