forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Material.cpp
669 lines (572 loc) · 23 KB
/
Material.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
// 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/Material.h"
#include "MantidKernel/Atom.h"
#include "MantidKernel/AttenuationProfile.h"
#include "MantidKernel/StringTokenizer.h"
#include <NeXusFile.hpp>
#include <boost/lexical_cast.hpp>
#include <memory>
#include <numeric>
namespace Mantid {
namespace Kernel {
using tokenizer = Mantid::Kernel::StringTokenizer;
using str_pair = std::pair<std::string, std::string>;
using PhysicalConstants::Atom;
using PhysicalConstants::getAtom;
using PhysicalConstants::NeutronAtom;
namespace {
constexpr double INV_FOUR_PI = 1. / (4. * M_PI);
inline double scatteringLength(const double real, const double imag) {
double length;
if (imag == 0.) {
length = std::abs(real);
} else if (real == 0.) {
length = std::abs(imag);
} else {
length = std::hypot(real, imag);
}
if (!std::isnormal(length)) {
return 0.;
} else {
return length;
}
}
inline double scatteringXS(const double realLength, const double imagLength) {
double lengthSqrd = (realLength * realLength) + (imagLength * imagLength);
if (!std::isnormal(lengthSqrd)) {
return 0.;
} else {
return .04 * M_PI * lengthSqrd;
}
}
} // namespace
Mantid::Kernel::Material::FormulaUnit::FormulaUnit(const std::shared_ptr<PhysicalConstants::Atom> &atom,
const double multiplicity)
: atom(atom), multiplicity(multiplicity) {}
Mantid::Kernel::Material::FormulaUnit::FormulaUnit(const PhysicalConstants::Atom &atom, const double multiplicity)
: atom(std::make_shared<PhysicalConstants::Atom>(atom)), multiplicity(multiplicity) {}
/**
* Construct an "empty" material. Everything returns zero
*/
Material::Material()
: m_name(), m_chemicalFormula(), m_atomTotal(0.0), m_numberDensity(0.0), m_packingFraction(1.0), m_temperature(0.0),
m_pressure(0.0), m_linearAbsorpXSectionByWL(0.0), m_totalScatterXSection(0.0) {}
/**
* Construct a material object
* @param name :: The name of the material
* @param formula :: The chemical formula
* @param numberDensity :: Density in atoms / Angstrom^3
* @param packingFraction :: Packing fraction of material
* @param temperature :: The temperature in Kelvin (Default = 300K)
* @param pressure :: Pressure in kPa (Default: 101.325 kPa)
*/
Material::Material(const std::string &name, const ChemicalFormula &formula, const double numberDensity,
const double packingFraction, const double temperature, const double pressure)
: m_name(name), m_atomTotal(0.0), m_numberDensity(numberDensity), m_packingFraction(packingFraction),
m_temperature(temperature), m_pressure(pressure) {
m_chemicalFormula.assign(formula.begin(), formula.end());
this->countAtoms();
this->calculateLinearAbsorpXSectionByWL();
this->calculateTotalScatterXSection();
}
/**
* Construct a material object
* @param name :: The name of the material
* @param atom :: The neutron atom to take scattering infrmation from
* @param numberDensity :: Density in atoms / Angstrom^3
* @param packingFraction :: Packing fraction of material
* @param temperature :: The temperature in Kelvin (Default = 300K)
* @param pressure :: Pressure in kPa (Default: 101.325 kPa)
*/
Material::Material(const std::string &name, const PhysicalConstants::NeutronAtom &atom, const double numberDensity,
const double packingFraction, const double temperature, const double pressure)
: m_name(name), m_chemicalFormula(), m_atomTotal(1.0), m_numberDensity(numberDensity),
m_packingFraction(packingFraction), m_temperature(temperature), m_pressure(pressure) {
if (atom.z_number == 0) { // user specified atom
m_chemicalFormula.emplace_back(atom, 1.);
} else if (atom.a_number > 0) { // single isotope
m_chemicalFormula.emplace_back(getAtom(atom.z_number, atom.a_number), 1.);
} else { // isotopic average
m_chemicalFormula.emplace_back(atom, 1.);
}
this->calculateLinearAbsorpXSectionByWL();
this->calculateTotalScatterXSection();
}
// update the total atom count
void Material::countAtoms() {
m_atomTotal =
std::accumulate(std::begin(m_chemicalFormula), std::end(m_chemicalFormula), 0.,
[](double subtotal, const FormulaUnit &right) { return subtotal + right.multiplicity; });
}
/**
* Calculate the absorption cross section for a given wavelength
* according to Sears eqn 14. Store result as a cross section per wavelength
* to enable the result to be reused to calculate the cross section for
* specific wavelengths (assuming linear dependence on the wavelength)
* with the reference wavelength = NeutronAtom::ReferenceLambda angstroms.
*/
void Material::calculateLinearAbsorpXSectionByWL() {
double weightedTotal;
if (m_chemicalFormula.size() == 1) {
weightedTotal = m_chemicalFormula.front().atom->neutron.abs_scatt_xs;
} else {
weightedTotal = std::accumulate(std::begin(m_chemicalFormula), std::end(m_chemicalFormula), 0.,
[](double subtotal, const FormulaUnit &right) {
return subtotal + right.atom->neutron.abs_scatt_xs * right.multiplicity;
}) /
m_atomTotal;
}
if (!std::isnormal(weightedTotal)) {
weightedTotal = 0.;
}
m_linearAbsorpXSectionByWL = weightedTotal / PhysicalConstants::NeutronAtom::ReferenceLambda;
}
// calculate the total scattering x section (by wavelength) following Sears
// eqn 13.
void Material::calculateTotalScatterXSection() {
double weightedTotal;
if (m_chemicalFormula.size() == 1)
weightedTotal = m_chemicalFormula.front().atom->neutron.tot_scatt_xs;
else {
weightedTotal = std::accumulate(std::begin(m_chemicalFormula), std::end(m_chemicalFormula), 0.,
[](double subtotal, const FormulaUnit &right) {
return subtotal + right.atom->neutron.tot_scatt_xs * right.multiplicity;
}) /
m_atomTotal;
}
if (!std::isnormal(weightedTotal)) {
m_totalScatterXSection = 0.;
} else {
m_totalScatterXSection = weightedTotal;
}
}
void Material::setAttenuationProfile(AttenuationProfile attenuationOverride) {
m_attenuationOverride = std::move(attenuationOverride);
}
void Material::setXRayAttenuationProfile(AttenuationProfile attenuationProfile) {
m_xRayAttenuationProfile = std::move(attenuationProfile);
}
/**
* Returns the name
* @returns A string containing the name of the material
*/
const std::string &Material::name() const { return m_name; }
const Material::ChemicalFormula &Material::chemicalFormula() const { return m_chemicalFormula; }
/**
* Get the number density
* @returns The number density of the material in atoms / Angstrom^3
*/
double Material::numberDensity() const { return m_numberDensity; }
/**
* Get the effective number density
* @returns The number density of the material in atoms / Angstrom^3
*/
double Material::numberDensityEffective() const { return m_numberDensity * m_packingFraction; }
/**
* Get the packing fraction. This should be a number 0<f<=1. However,
* this is sometimes used as a fudge factor and is allowed to vary 0<f<2.
* @returns The packing fraction
*/
double Material::packingFraction() const { return m_packingFraction; }
/**
* Get the temperature
* @returns The temperature of the material in Kelvin
*/
double Material::temperature() const { return m_temperature; }
/**
* Get the pressure
* @returns The pressure of the material
*/
double Material::pressure() const { return m_pressure; }
/**
* Get the coherent scattering cross section according to Sears eqn 7.
*
* @returns The value of the coherent scattering cross section.
*/
double Material::cohScatterXSection() const {
if (m_chemicalFormula.size() == 1)
return m_chemicalFormula.front().atom->neutron.coh_scatt_xs;
return scatteringXS(cohScatterLengthReal(), cohScatterLengthImg());
}
/**
* Get the incoherent scattering cross section according to Sears eqn 16
*
* @returns The value of the coherent scattering cross section.
*/
double Material::incohScatterXSection() const {
if (m_chemicalFormula.size() == 1)
return m_chemicalFormula.front().atom->neutron.inc_scatt_xs;
return totalScatterXSection() - cohScatterXSection();
}
/**
* Get the total scattering cross section
*
* @returns The value of the total scattering cross section.
*/
double Material::totalScatterXSection() const { return m_totalScatterXSection; }
/**
* Get the absorption cross section for a given wavelength
* @param lambda :: The wavelength to evaluate the cross section
* @returns The value of the absoprtion cross section at
* the given wavelength
*/
double Material::absorbXSection(const double lambda) const { return m_linearAbsorpXSectionByWL * lambda; }
/**
* @param lambda Wavelength (Angstroms) to compute the attenuation (default =
* reference lambda)
* @return The attenuation coefficient in m-1
*/
double Material::attenuationCoefficient(const double lambda) const {
if (!m_attenuationOverride) {
return 100 * numberDensityEffective() * (totalScatterXSection() + absorbXSection(lambda));
} else {
return m_attenuationOverride->getAttenuationCoefficient(lambda);
}
}
/**
* @param distance Distance (m) travelled
* @param lambda Wavelength (Angstroms) to compute the attenuation (default =
* reference lambda)
* @return The dimensionless attenuation factor
*/
double Material::attenuation(const double distance, const double lambda) const {
return exp(-attenuationCoefficient(lambda) * distance);
}
/**
* @param distance Distance (m) travelled
* @param energy KeV to compute the attenuation
* @return The dimensionless attenuation factor
*/
double Material::xRayAttenuation(const double distance, const double energy) const {
if (m_xRayAttenuationProfile) {
return exp(-m_xRayAttenuationProfile->getAttenuationCoefficient(energy) * distance);
} else {
throw std::runtime_error("xRayAttenuationProfile override not set");
}
}
/*
* @returns true if m_xRayAttenuationOverride is set and false if not
*/
bool Material::hasValidXRayAttenuationProfile() {
if (m_xRayAttenuationProfile) {
return true;
} else {
return false;
}
}
// NOTE: the angstrom^-2 to barns and the angstrom^-1 to cm^-1
// will cancel for mu to give units: cm^-1
double Material::linearAbsorpCoef(const double lambda) const {
return absorbXSection(lambda) * 100. * numberDensityEffective();
}
// This must match the values that come from the scalar version
std::vector<double> Material::linearAbsorpCoef(std::vector<double>::const_iterator lambdaBegin,
std::vector<double>::const_iterator lambdaEnd) const {
const double densityTerm = 100. * numberDensityEffective();
std::vector<double> linearCoef(std::distance(lambdaBegin, lambdaEnd));
std::transform(lambdaBegin, lambdaEnd, linearCoef.begin(),
[densityTerm, this](const double lambda) { return densityTerm * this->absorbXSection(lambda); });
return linearCoef;
}
/// According to Sears eqn 12
double Material::cohScatterLength(const double lambda) const {
UNUSED_ARG(lambda);
if (m_chemicalFormula.size() == 1)
return m_chemicalFormula.front().atom->neutron.coh_scatt_length;
// these have already accounted for single atom case
return scatteringLength(cohScatterLengthReal(), cohScatterLengthImg());
}
/// According to Sears eqn 7
double Material::incohScatterLength(const double lambda) const {
UNUSED_ARG(lambda);
if (m_chemicalFormula.size() == 1)
return m_chemicalFormula.front().atom->neutron.inc_scatt_length;
return scatteringLength(incohScatterLengthReal(), incohScatterLengthImg());
}
/// Sears eqn 12
double Material::cohScatterLengthReal(const double lambda) const {
UNUSED_ARG(lambda);
if (m_chemicalFormula.size() == 1)
return m_chemicalFormula.front().atom->neutron.coh_scatt_length_real;
const double weightedTotal =
std::accumulate(std::begin(m_chemicalFormula), std::end(m_chemicalFormula), 0.,
[](double subtotal, const FormulaUnit &right) {
return subtotal + right.atom->neutron.coh_scatt_length_real * right.multiplicity;
}) /
m_atomTotal;
if (!std::isnormal(weightedTotal)) {
return 0.;
} else {
return weightedTotal;
}
}
/// Sears eqn 12
double Material::cohScatterLengthImg(const double lambda) const {
UNUSED_ARG(lambda);
if (m_chemicalFormula.size() == 1)
return m_chemicalFormula.front().atom->neutron.coh_scatt_length_img;
const double weightedTotal =
std::accumulate(std::begin(m_chemicalFormula), std::end(m_chemicalFormula), 0.,
[](double subtotal, const FormulaUnit &right) {
return subtotal + right.atom->neutron.coh_scatt_length_img * right.multiplicity;
}) /
m_atomTotal;
if (!std::isnormal(weightedTotal)) {
return 0.;
} else {
return weightedTotal;
}
}
/// Not explicitly in Sears, but following eqn 12
double Material::incohScatterLengthReal(const double lambda) const {
UNUSED_ARG(lambda);
if (m_chemicalFormula.size() == 1)
return m_chemicalFormula.front().atom->neutron.inc_scatt_length_real;
const double weightedTotal =
std::accumulate(std::begin(m_chemicalFormula), std::end(m_chemicalFormula), 0.,
[](double subtotal, const FormulaUnit &right) {
return subtotal + right.atom->neutron.inc_scatt_length_real * right.multiplicity;
}) /
m_atomTotal;
if (!std::isnormal(weightedTotal)) {
return 0.;
} else {
return weightedTotal;
}
}
/// Not explicitly in Sears, but following eqn 12
double Material::incohScatterLengthImg(const double lambda) const {
UNUSED_ARG(lambda);
if (m_chemicalFormula.size() == 1)
return m_chemicalFormula.front().atom->neutron.inc_scatt_length_img;
const double weightedTotal =
std::accumulate(std::begin(m_chemicalFormula), std::end(m_chemicalFormula), 0.,
[](double subtotal, const FormulaUnit &right) {
return subtotal + right.atom->neutron.inc_scatt_length_img * right.multiplicity;
}) /
m_atomTotal;
if (!std::isnormal(weightedTotal)) {
return 0.;
} else {
return weightedTotal;
}
}
/// Sears eqn 13
double Material::totalScatterLength(const double lambda) const {
UNUSED_ARG(lambda);
if (m_chemicalFormula.size() == 1)
return m_chemicalFormula.front().atom->neutron.tot_scatt_length;
const double crossSection = totalScatterXSection();
return 10. * std::sqrt(crossSection) * INV_FOUR_PI;
}
double Material::cohScatterLengthSqrd(const double lambda) const {
UNUSED_ARG(lambda);
// these have already acconted for single atom case
const double real = this->cohScatterLengthReal();
const double imag = this->cohScatterLengthImg();
double lengthSqrd;
if (imag == 0.) {
lengthSqrd = real * real;
} else if (real == 0.) {
lengthSqrd = imag * imag;
} else {
lengthSqrd = real * real + imag * imag;
}
if (!std::isnormal(lengthSqrd)) {
return 0.;
} else {
return lengthSqrd;
}
}
double Material::incohScatterLengthSqrd(const double lambda) const {
UNUSED_ARG(lambda);
// cross section has this properly averaged already
const double crossSection = incohScatterXSection();
// 1 barn = 100 fm^2
return 100. * crossSection * INV_FOUR_PI;
}
double Material::totalScatterLengthSqrd(const double lambda) const {
UNUSED_ARG(lambda);
// cross section has this properly averaged already
const double crossSection = totalScatterXSection();
// 1 barn = 100 fm^2
return 100. * crossSection * INV_FOUR_PI;
}
/** Save the object to an open NeXus file.
* @param file :: open NeXus file
* @param group :: name of the group to create
*/
void Material::saveNexus(::NeXus::File *file, const std::string &group) const {
file->makeGroup(group, "NXdata", true);
file->putAttr("version", 2);
file->putAttr("name", m_name);
// determine how the information will be stored
std::string style = "formula"; // default is a chemical formula
if (m_chemicalFormula.empty()) {
style = "empty";
} else if (m_chemicalFormula.size() == 1) {
if (m_chemicalFormula[0].atom->symbol == "user") {
style = "userdefined";
}
}
file->putAttr("formulaStyle", style);
// write the actual information out
if (style == "formula") {
std::stringstream formula;
for (const auto &formulaUnit : m_chemicalFormula) {
if (formulaUnit.atom->a_number != 0) {
formula << "(";
}
formula << formulaUnit.atom->symbol;
if (formulaUnit.atom->a_number != 0) {
formula << formulaUnit.atom->a_number << ")";
}
formula << formulaUnit.multiplicity << " ";
}
file->writeData("chemical_formula", formula.str());
} else if (style == "userdefined") {
file->writeData("coh_scatt_length_real", m_chemicalFormula[0].atom->neutron.coh_scatt_length_real);
file->writeData("coh_scatt_length_img", m_chemicalFormula[0].atom->neutron.coh_scatt_length_img);
file->writeData("inc_scatt_length_real", m_chemicalFormula[0].atom->neutron.inc_scatt_length_real);
file->writeData("inc_scatt_length_img", m_chemicalFormula[0].atom->neutron.inc_scatt_length_img);
file->writeData("coh_scatt_xs", m_chemicalFormula[0].atom->neutron.coh_scatt_xs);
file->writeData("inc_scatt_xs", m_chemicalFormula[0].atom->neutron.inc_scatt_xs);
file->writeData("tot_scatt_xs", m_chemicalFormula[0].atom->neutron.tot_scatt_xs);
file->writeData("abs_scatt_xs", m_chemicalFormula[0].atom->neutron.abs_scatt_xs);
file->writeData("tot_scatt_length", m_chemicalFormula[0].atom->neutron.tot_scatt_length);
file->writeData("coh_scatt_length", m_chemicalFormula[0].atom->neutron.coh_scatt_length);
file->writeData("inc_scatt_length", m_chemicalFormula[0].atom->neutron.inc_scatt_length);
}
file->writeData("number_density", m_numberDensity);
file->writeData("packing_fraction", m_packingFraction);
file->writeData("temperature", m_temperature);
file->writeData("pressure", m_pressure);
file->closeGroup();
}
/** Load the object from an open NeXus file.
* @param file :: open NeXus file
* @param group :: name of the group to open
*/
void Material::loadNexus(::NeXus::File *file, const std::string &group) {
file->openGroup(group, "NXdata");
file->getAttr("name", m_name);
int version;
file->getAttr("version", version);
if (version == 1) {
// Find the element
uint16_t element_Z, element_A;
file->readData("element_Z", element_Z);
file->readData("element_A", element_A);
try {
m_chemicalFormula.clear();
if (element_Z > 0) {
m_chemicalFormula.emplace_back(getAtom(element_Z, element_A), 1);
} else {
m_chemicalFormula.emplace_back(Mantid::PhysicalConstants::getNeutronAtom(element_Z, element_A), 1);
}
} catch (std::runtime_error &) { /* ignore and use the default */
}
} else if (version == 2) {
std::string style;
file->getAttr("formulaStyle", style);
if (style == "formula") {
std::string formula;
file->readData("chemical_formula", formula);
this->m_chemicalFormula = Material::parseChemicalFormula(formula);
this->countAtoms();
} else if (style == "userdefined") {
NeutronAtom neutron;
file->readData("coh_scatt_length_real", neutron.coh_scatt_length_real);
file->readData("coh_scatt_length_img", neutron.coh_scatt_length_img);
file->readData("inc_scatt_length_real", neutron.inc_scatt_length_real);
file->readData("inc_scatt_length_img", neutron.inc_scatt_length_img);
file->readData("coh_scatt_xs", neutron.coh_scatt_xs);
file->readData("inc_scatt_xs", neutron.inc_scatt_xs);
file->readData("tot_scatt_xs", neutron.tot_scatt_xs);
file->readData("abs_scatt_xs", neutron.abs_scatt_xs);
file->readData("tot_scatt_length", neutron.tot_scatt_length);
file->readData("coh_scatt_length", neutron.coh_scatt_length);
file->readData("inc_scatt_length", neutron.inc_scatt_length);
m_chemicalFormula.emplace_back(std::make_shared<Atom>(neutron), 1);
}
// the other option is empty which does not need to be addressed
} else {
throw std::runtime_error("Only know how to read version 1 or 2 for Material");
}
this->countAtoms();
this->calculateLinearAbsorpXSectionByWL();
this->calculateTotalScatterXSection();
file->readData("number_density", m_numberDensity);
try {
file->readData("packing_fraction", m_packingFraction);
} catch (std::runtime_error &) {
m_packingFraction = 1.;
}
file->readData("temperature", m_temperature);
file->readData("pressure", m_pressure);
file->closeGroup();
}
namespace { // anonymous namespace to hide the function
str_pair getAtomName(const std::string &text) // TODO change to get number after letters
{
// one character doesn't need
if (text.size() <= 1)
return std::make_pair(text, "");
// check the second character
const char *s;
s = text.c_str();
if ((s[1] >= '0' && s[1] <= '9') || s[1] == '.')
return std::make_pair(text.substr(0, 1), text.substr(1));
else
return std::make_pair(text.substr(0, 2), text.substr(2));
}
} // namespace
Material::ChemicalFormula Material::parseChemicalFormula(const std::string &chemicalSymbol) {
Material::ChemicalFormula CF;
tokenizer tokens(chemicalSymbol, " -", Mantid::Kernel::StringTokenizer::TOK_IGNORE_EMPTY);
for (const auto &atom : tokens) {
try {
std::string name;
float numberAtoms = 1;
uint16_t aNumber = 0;
// split out the isotope bit
if (atom.find('(') != std::string::npos) {
// error check
size_t end = atom.find(')');
if (end == std::string::npos) {
std::stringstream msg;
msg << "Failed to parse isotope \"" << atom << "\"";
throw std::runtime_error(msg.str());
}
// get the number of atoms
std::string numberAtomsStr = atom.substr(end + 1);
if (!numberAtomsStr.empty())
numberAtoms = boost::lexical_cast<float>(numberAtomsStr);
// split up the atom and isotope number
name = atom.substr(1, end - 1);
str_pair temp = getAtomName(name);
name = temp.first;
aNumber = boost::lexical_cast<uint16_t>(temp.second);
} else // for non-isotopes
{
str_pair temp = getAtomName(atom);
name = temp.first;
if (!temp.second.empty())
numberAtoms = boost::lexical_cast<float>(temp.second);
}
CF.emplace_back(getAtom(name, aNumber), static_cast<double>(numberAtoms));
} catch (boost::bad_lexical_cast &e) {
std::stringstream msg;
msg << "While trying to parse atom \"" << atom << "\" encountered bad_lexical_cast: " << e.what();
throw std::runtime_error(msg.str());
}
}
return CF;
}
} // namespace Kernel
} // namespace Mantid