forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaterialTest.h
261 lines (225 loc) · 10.7 KB
/
MaterialTest.h
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
// 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 +
#pragma once
#include <cmath>
#include <cxxtest/TestSuite.h>
#include <stdexcept>
#include "MantidFrameworkTestHelpers/NexusTestHelper.h"
#include "MantidKernel/Atom.h"
#include "MantidKernel/Material.h"
#include "MantidKernel/NeutronAtom.h"
using Mantid::Kernel::Material;
using Mantid::PhysicalConstants::getNeutronAtom;
using Mantid::PhysicalConstants::NeutronAtom;
class MaterialTest : public CxxTest::TestSuite {
public:
void test_Empty_Constructor() {
Material empty;
TS_ASSERT_EQUALS(empty.name(), "");
TS_ASSERT_EQUALS(empty.numberDensity(), 0.0);
TS_ASSERT_EQUALS(empty.temperature(), 0.0);
TS_ASSERT_EQUALS(empty.pressure(), 0.0);
const double lambda(2.1);
TS_ASSERT_EQUALS(empty.absorbXSection(lambda), 0.0);
}
// common code for comparing scattering information for material made of a
// single atom
void checkMatching(const Material &material, const NeutronAtom &atom) {
TS_ASSERT_DELTA(material.cohScatterXSection(), atom.coh_scatt_xs, 1.e-4);
TS_ASSERT_DELTA(material.incohScatterXSection(), atom.inc_scatt_xs, 1.e-4);
TS_ASSERT_DELTA(material.totalScatterXSection(), atom.tot_scatt_xs, 1.e-4);
TS_ASSERT_DELTA(material.absorbXSection(), atom.abs_scatt_xs, 1.e-4);
TS_ASSERT_DELTA(material.cohScatterLength(), atom.coh_scatt_length, 1.e-4);
TS_ASSERT_DELTA(material.incohScatterLength(), atom.inc_scatt_length, 1.e-4);
TS_ASSERT_DELTA(material.totalScatterLength(), atom.tot_scatt_length, 1.e-4);
TS_ASSERT_DELTA(material.cohScatterLengthReal(), atom.coh_scatt_length_real, 1.e-4);
TS_ASSERT_DELTA(material.cohScatterLengthImg(), atom.coh_scatt_length_img, 1.e-4);
TS_ASSERT_DELTA(material.incohScatterLengthReal(), atom.inc_scatt_length_real, 1.e-4);
TS_ASSERT_DELTA(material.incohScatterLengthImg(), atom.inc_scatt_length_img, 1.e-4);
const double cohReal = atom.coh_scatt_length_real;
const double cohImag = atom.coh_scatt_length_img;
TS_ASSERT_DELTA(material.cohScatterLengthSqrd(), cohReal * cohReal + cohImag * cohImag, 1.e-4);
const double totXS = atom.tot_scatt_xs;
TS_ASSERT_DELTA(material.totalScatterLengthSqrd(), 25. * totXS / M_PI, 1.e-4);
}
void test_Vanadium() {
const std::string name("Vanadium");
const double numberDensity = 0.072;
NeutronAtom atom = getNeutronAtom(23);
Material material(name, atom, numberDensity);
TS_ASSERT_EQUALS(material.name(), name);
TS_ASSERT_EQUALS(material.numberDensity(), numberDensity);
TS_ASSERT_EQUALS(material.numberDensityEffective(), numberDensity);
TS_ASSERT_EQUALS(material.packingFraction(), 1.);
TS_ASSERT_EQUALS(material.totalAtoms(), 1.)
TS_ASSERT_EQUALS(material.temperature(), 300);
TS_ASSERT_EQUALS(material.pressure(), Mantid::PhysicalConstants::StandardAtmosphere);
// check everything with (default) reference wavelength
checkMatching(material, atom);
// check everything against another wavelength, only affects absorption
const double lambda(2.1);
TS_ASSERT_DELTA(material.absorbXSection(lambda), 5.93, 1e-02);
const double distance(0.05);
TS_ASSERT_DELTA(material.attenuation(distance, lambda), 0.01884, 1e-4);
}
// highly absorbing material
void test_Gadolinium() {
const std::string name("Gadolinium");
const double numberDensity = 0.0768; // mass density is 7.90 g/cm3
NeutronAtom atom = getNeutronAtom(64);
Material material(name, atom, numberDensity);
TS_ASSERT_EQUALS(material.name(), name);
TS_ASSERT_EQUALS(material.numberDensity(), numberDensity);
TS_ASSERT_EQUALS(material.numberDensityEffective(), numberDensity);
TS_ASSERT_EQUALS(material.packingFraction(), 1.);
TS_ASSERT_EQUALS(material.totalAtoms(), 1.)
// check everything with (default) reference wavelength
checkMatching(material, atom);
const double totLength = material.totalScatterLength();
TS_ASSERT_DELTA(.04 * M_PI * totLength * totLength, material.totalScatterXSection(), 1.e-4);
}
// "null scatterer" has only incoherent scattering
void test_TiZr() {
const std::string name("TiZr");
const double numberDensity = 0.542;
const double packingFraction = .6;
Material TiZr(name, Material::parseChemicalFormula("Ti2.082605 Zr"), numberDensity, packingFraction);
TS_ASSERT_EQUALS(TiZr.name(), name);
TS_ASSERT_EQUALS(TiZr.numberDensity(), numberDensity);
TS_ASSERT_EQUALS(TiZr.numberDensityEffective(), numberDensity * packingFraction);
TS_ASSERT_EQUALS(TiZr.packingFraction(), packingFraction);
TS_ASSERT_DELTA(TiZr.totalAtoms(), 3.082605, 1.e-5); // string to double changes value
TS_ASSERT_EQUALS(TiZr.cohScatterLengthImg(), 0.);
TS_ASSERT_DELTA(TiZr.cohScatterLengthReal(), 0., 1.e-5);
TS_ASSERT_EQUALS(TiZr.cohScatterLength(),
TiZr.cohScatterLengthReal()); // there is no imaginary part
TS_ASSERT_DELTA(TiZr.cohScatterXSection(), 0., 1.e-5);
TS_ASSERT_DELTA(TiZr.totalScatterXSection(), TiZr.cohScatterXSection() + TiZr.incohScatterXSection(), 1.e-5);
const double cohReal = TiZr.cohScatterLengthReal();
const double cohImag = TiZr.cohScatterLengthImg();
TS_ASSERT_DELTA(TiZr.cohScatterLengthSqrd(), cohReal * cohReal + cohImag * cohImag, 1.e-4);
const double totXS = TiZr.totalScatterXSection();
TS_ASSERT_DELTA(TiZr.totalScatterLengthSqrd(), 25. * totXS / M_PI, 1.e-4);
const double distance(0.05);
TS_ASSERT_DELTA(TiZr.attenuation(distance), 0., 1e-4);
}
/** Save then re-load from a NXS file */
void test_nexus() {
Material testA("testMaterial", Mantid::PhysicalConstants::getNeutronAtom(23, 0), 0.072, 2., 273, 1.234);
NexusTestHelper th(true);
th.createFile("MaterialTest.nxs");
TS_ASSERT_THROWS_NOTHING(testA.saveNexus(th.file.get(), "material"););
Material testB;
th.reopenFile();
TS_ASSERT_THROWS_NOTHING(testB.loadNexus(th.file.get(), "material"););
TS_ASSERT_EQUALS(testB.name(), "testMaterial");
TS_ASSERT_DELTA(testB.numberDensity(), 0.072, 1e-6);
TS_ASSERT_DELTA(testB.temperature(), 273, 1e-6);
TS_ASSERT_DELTA(testB.pressure(), 1.234, 1e-6);
// This (indirectly) checks that the right element was found
const double lambda(2.1);
TS_ASSERT_DELTA(testB.absorbXSection(lambda), 5.93, 1e-02);
}
void test_nexus_emptyMaterial() {
Material testA;
NexusTestHelper th(true);
th.createFile("MaterialTest.nxs");
TS_ASSERT_THROWS_NOTHING(testA.saveNexus(th.file.get(), "material"););
Material testB;
th.reopenFile();
TS_ASSERT_THROWS_NOTHING(testB.loadNexus(th.file.get(), "material"););
}
void test_parseMaterial() {
Material::ChemicalFormula cf;
cf = Material::parseChemicalFormula("F14");
TS_ASSERT_EQUALS(cf.size(), 1);
TS_ASSERT_EQUALS(cf[0].atom->symbol, "F");
TS_ASSERT_EQUALS(cf[0].atom->a_number, 0);
TS_ASSERT_EQUALS(cf[0].multiplicity, 14);
cf = Material::parseChemicalFormula("(F14)");
TS_ASSERT_EQUALS(cf.size(), 1);
TS_ASSERT_EQUALS(cf[0].atom->symbol, "F");
TS_ASSERT_EQUALS(cf[0].atom->a_number, 14);
TS_ASSERT_EQUALS(cf[0].multiplicity, 1);
cf = Material::parseChemicalFormula("C15");
TS_ASSERT_EQUALS(cf.size(), 1);
TS_ASSERT_EQUALS(cf[0].atom->symbol, "C");
TS_ASSERT_EQUALS(cf[0].atom->a_number, 0);
TS_ASSERT_EQUALS(cf[0].multiplicity, 15);
cf = Material::parseChemicalFormula("(C15)");
TS_ASSERT_EQUALS(cf.size(), 1);
TS_ASSERT_EQUALS(cf[0].atom->symbol, "C");
TS_ASSERT_EQUALS(cf[0].atom->a_number, 15);
TS_ASSERT_EQUALS(cf[0].multiplicity, 1);
cf = Material::parseChemicalFormula("H2 O");
TS_ASSERT_EQUALS(cf.size(), 2);
TS_ASSERT_EQUALS(cf[0].atom->symbol, "H");
TS_ASSERT_EQUALS(cf[0].atom->a_number, 0);
TS_ASSERT_EQUALS(cf[0].multiplicity, 2);
TS_ASSERT_EQUALS(cf[1].atom->symbol, "O");
TS_ASSERT_EQUALS(cf[1].atom->a_number, 0);
TS_ASSERT_EQUALS(cf[1].multiplicity, 1);
cf = Material::parseChemicalFormula("(H1)2 O");
TS_ASSERT_EQUALS(cf.size(), 2);
TS_ASSERT_EQUALS(cf[0].atom->symbol, "H");
TS_ASSERT_EQUALS(cf[0].atom->a_number, 1);
TS_ASSERT_EQUALS(cf[0].multiplicity, 2);
TS_ASSERT_EQUALS(cf[1].atom->symbol, "O");
TS_ASSERT_EQUALS(cf[1].atom->a_number, 0);
TS_ASSERT_EQUALS(cf[1].multiplicity, 1);
cf = Material::parseChemicalFormula("D2 O");
TS_ASSERT_EQUALS(cf.size(), 2);
TS_ASSERT_EQUALS(cf[0].atom->symbol, "H");
TS_ASSERT_EQUALS(cf[0].atom->a_number, 2);
TS_ASSERT_EQUALS(cf[0].multiplicity, 2);
TS_ASSERT_EQUALS(cf[1].atom->symbol, "O");
TS_ASSERT_EQUALS(cf[1].atom->a_number, 0);
TS_ASSERT_EQUALS(cf[1].multiplicity, 1);
cf = Material::parseChemicalFormula("H2 O");
TS_ASSERT_EQUALS(cf.size(), 2);
TS_ASSERT_EQUALS(cf[0].atom->symbol, "H");
TS_ASSERT_EQUALS(cf[0].atom->a_number, 0);
TS_ASSERT_EQUALS(cf[0].multiplicity, 2);
TS_ASSERT_EQUALS(cf[1].atom->symbol, "O");
TS_ASSERT_EQUALS(cf[1].atom->a_number, 0);
TS_ASSERT_EQUALS(cf[1].multiplicity, 1);
cf = Material::parseChemicalFormula("H2-O");
TS_ASSERT_EQUALS(cf.size(), 2);
TS_ASSERT_EQUALS(cf[0].atom->symbol, "H");
TS_ASSERT_EQUALS(cf[0].atom->a_number, 0);
TS_ASSERT_EQUALS(cf[0].multiplicity, 2);
TS_ASSERT_EQUALS(cf[1].atom->symbol, "O");
TS_ASSERT_EQUALS(cf[1].atom->a_number, 0);
TS_ASSERT_EQUALS(cf[1].multiplicity, 1);
TS_ASSERT_THROWS(cf = Material::parseChemicalFormula("H2*O"), const std::runtime_error &);
TS_ASSERT_EQUALS(cf.size(), 2);
TS_ASSERT_EQUALS(cf[0].atom->symbol, "H");
TS_ASSERT_EQUALS(cf[0].atom->a_number, 0);
TS_ASSERT_EQUALS(cf[0].multiplicity, 2);
TS_ASSERT_EQUALS(cf[1].atom->symbol, "O");
TS_ASSERT_EQUALS(cf[1].atom->a_number, 0);
TS_ASSERT_EQUALS(cf[1].multiplicity, 1);
cf = Material::parseChemicalFormula("(Li7)2");
TS_ASSERT_EQUALS(cf.size(), 1);
TS_ASSERT_EQUALS(cf[0].atom->symbol, "Li");
TS_ASSERT_EQUALS(cf[0].atom->a_number, 7);
TS_ASSERT_EQUALS(cf[0].multiplicity, 2);
cf = Material::parseChemicalFormula("Y-Ba2-Cu3-O6.56");
TS_ASSERT_EQUALS(cf.size(), 4);
for (const auto &formulaUnit : cf) {
TS_ASSERT_EQUALS(formulaUnit.atom->a_number, 0);
}
TS_ASSERT_EQUALS(cf[0].atom->symbol, "Y");
TS_ASSERT_EQUALS(cf[0].multiplicity, 1);
TS_ASSERT_EQUALS(cf[1].atom->symbol, "Ba");
TS_ASSERT_EQUALS(cf[1].multiplicity, 2);
TS_ASSERT_EQUALS(cf[2].atom->symbol, "Cu");
TS_ASSERT_EQUALS(cf[2].multiplicity, 3);
TS_ASSERT_EQUALS(cf[3].atom->symbol, "O");
TS_ASSERT_DELTA(cf[3].multiplicity, 6.56, .01);
}
};