Newer
Older
#ifndef MANTID_DATAHANDLING_LOADGSASINSTRUMENTFILETEST_H_
#define MANTID_DATAHANDLING_LOADGSASINSTRUMENTFILETEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidDataHandling/LoadGSASInstrumentFile.h"
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
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidAPI/TableRow.h"
#include "MantidDataHandling/LoadInstrument.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/Component.h"
#include "MantidGeometry/Instrument/FitParameter.h"
#include <fstream>
#include <Poco/File.h>
using Mantid::DataHandling::LoadGSASInstrumentFile;
using namespace Mantid;
using namespace Mantid::DataHandling;
using namespace Mantid::DataObjects;
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace std;
class LoadGSASInstrumentFileTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static LoadGSASInstrumentFileTest *createSuite() { return new LoadGSASInstrumentFileTest(); }
static void destroySuite( LoadGSASInstrumentFileTest *suite ) { delete suite; }
//----------------------------------------------------------------------------------------------
/** Test import from a 1-bank prm file
*/
void test_1BankCase()
{
// 1. Generate file
string filename("Test1Bank.prm");
generate1BankPrmFile(filename);
// 2. Load
LoadGSASInstrumentFile alg;
alg.initialize();
alg.setProperty("Filename", filename);
alg.setProperty("OutputTableWorkspace", "Test1BankTable");
TS_ASSERT_THROWS_NOTHING(alg.execute());
TS_ASSERT(alg.isExecuted());
TableWorkspace_sptr outws = boost::dynamic_pointer_cast<TableWorkspace>(
AnalysisDataService::Instance().retrieve("Test1BankTable"));
TS_ASSERT(outws);
TS_ASSERT_EQUALS(outws->columnCount(), 2);
TS_ASSERT_EQUALS(outws->rowCount(), getExpectedNumberOfRows() );
// 3. Verify name and value
map<string, double> parammap;
parseTableWorkspace(outws, parammap);
TS_ASSERT_EQUALS(parammap.count("Beta0"),1);
TS_ASSERT_EQUALS(parammap.count("Sig1"),1);
TS_ASSERT_EQUALS(parammap.count("Gam1"),1);
TS_ASSERT_DELTA(parammap["Beta0"], 31.793, 0.001);
TS_ASSERT_DELTA(parammap["Sig1"], 176.802, 0.001);
TS_ASSERT_DELTA(parammap["Gam1"], 0.007, 0.0001);
// 4. Clean
AnalysisDataService::Instance().remove("Test1BankTable");
Poco::File("Test1Bank.prm").remove();
return;
}
//----------------------------------------------------------------------------------------------
/** Test import from a 2-bank prm file
*/
void test_2BankCase()
{
// 1. Generate file
string filename("Test2Bank.prm");
generate2BankPrmFile(filename);
// 2. Load
LoadGSASInstrumentFile alg;
alg.initialize();
alg.setProperty("Filename", filename);
alg.setProperty("OutputTableWorkspace", "Test2BankTable");
TS_ASSERT_THROWS_NOTHING(alg.execute());
TS_ASSERT(alg.isExecuted());
TableWorkspace_sptr outws = boost::dynamic_pointer_cast<TableWorkspace>(
AnalysisDataService::Instance().retrieve("Test2BankTable"));
TS_ASSERT(outws);
TS_ASSERT_EQUALS(outws->columnCount(), 3);
TS_ASSERT_EQUALS(outws->rowCount(), getExpectedNumberOfRows());
// 3. Verify name and value
map<string, double> parammap1;
parseTableWorkspace(outws, parammap1);
TS_ASSERT_EQUALS(parammap1.count("Alph1"),1);
TS_ASSERT_EQUALS(parammap1.count("Sig2"),1);
TS_ASSERT_EQUALS(parammap1.count("Gam1"),1);
TS_ASSERT_DELTA(parammap1["Alph1"], 0.21, 0.0001);
TS_ASSERT_DELTA(parammap1["Sig2"], 0.0, 0.0001);
TS_ASSERT_DELTA(parammap1["Gam1"], 0.007, 0.00001);
map<string, double> parammap2;
parseTableWorkspace2(outws, parammap2);
TS_ASSERT_EQUALS(parammap2.count("Alph1"),1);
TS_ASSERT_EQUALS(parammap2.count("Sig2"),1);
TS_ASSERT_EQUALS(parammap2.count("Gam1"),1);
TS_ASSERT_DELTA(parammap2["Alph1"], 0.22, 0.0001);
TS_ASSERT_DELTA(parammap2["Sig2"], -1.34662, 0.0001);
TS_ASSERT_DELTA(parammap2["Gam1"], 3.61229, 0.00001);
// 4. Clean
AnalysisDataService::Instance().remove("Test2BankTable");
Poco::File("Test2Bank.prm").remove();
return;
}
//----------------------------------------------------------------------------------------------
/** Test import of ALFBE, GAMMA and SIGMA parameters
* and check they are given their expected names.
*/
void test_ags_parameters()
{
// 1. Generate file
string filename("TestAGS.prm");
generate1BankPrmFile(filename);
// 2. Load
LoadGSASInstrumentFile alg;
alg.initialize();
alg.setProperty("Filename", filename);
alg.setProperty("OutputTableWorkspace", "TestAGSTable");
TS_ASSERT_THROWS_NOTHING(alg.execute());
TS_ASSERT(alg.isExecuted());
TableWorkspace_sptr outws = boost::dynamic_pointer_cast<TableWorkspace>(
AnalysisDataService::Instance().retrieve("TestAGSTable"));
TS_ASSERT(outws);
// 3. Verify names
map<string, double> parammap;
parseTableWorkspace(outws, parammap);
// 3a. ALFBE
TS_ASSERT_EQUALS(parammap.count("Alph0"),1);
TS_ASSERT_EQUALS(parammap.count("Beta0"),1);
TS_ASSERT_EQUALS(parammap.count("Alph1"),1);
TS_ASSERT_EQUALS(parammap.count("Beta1"),1);
// 3b. GAMMA
TS_ASSERT_EQUALS(parammap.count("Gam2"),1);
TS_ASSERT_EQUALS(parammap.count("Gam1"),1);
TS_ASSERT_EQUALS(parammap.count("Gam0"),1);
// 3c. SIGMA
TS_ASSERT_EQUALS(parammap.count("Sig2"),1);
TS_ASSERT_EQUALS(parammap.count("Sig1"),1);
TS_ASSERT_EQUALS(parammap.count("Sig0"),1);
// 4. Clean
AnalysisDataService::Instance().remove("TestAGSTable");
Poco::File("TestAGS.prm").remove();
return;
}
//----------------------------------------------------------------------------------------------
/** Parse a TableWorkspace to a map
*/
void parseTableWorkspace(TableWorkspace_sptr tablews, map<string, double>& parammap)
{
parammap.clear();
size_t numrows = tablews->rowCount();
for (size_t i = 0; i < numrows; ++i)
{
TableRow row = tablews->getRow(i);
double value;
string name;
row >> name >> value;
parammap.insert(make_pair(name, value));
}
return;
}
//----------------------------------------------------------------------------------------------
/** Parse a TableWorkspace's 2nd bank to a map
*/
void parseTableWorkspace2(TableWorkspace_sptr tablews, map<string, double>& parammap)
{
parammap.clear();
size_t numrows = tablews->rowCount();
for (size_t i = 0; i < numrows; ++i)
{
TableRow row = tablews->getRow(i);
double value1, value2;
string name;
row >> name >> value1 >> value2;
parammap.insert(make_pair(name, value2));
}
return;
}
//----------------------------------------------------------------------------------------------
/** Generate a 1 bank .prm file
*/
void generate1BankPrmFile(string filename)
{
ofstream ofile;
ofile.open(filename.c_str());
if (ofile.is_open())
{
ofile << "COMM Test file with one bank \n";
ofile << "INS BANK 1 \n";
ofile << "INS HTYPE PNTR \n";
ofile << "COMM5678901234567890 \n";
ofile << "INS 1 ICONS 746.96 -0.24 3.04 \n";
ofile << "INS 1BNKPAR 2.3696 9.39 0.00 .00000 .3000 1 1 \n";
ofile << "INS 1I ITYP 0 1.000 25.000 2 \n";
ofile << "INS 1PRCF 2 15 0.00100 \n";
ofile << "COMM The next 15 parameters as in wiki page CreateIkedaCarpenterParametersGSAS \n";
ofile << "INS 1PRCF 1 0.000000E+00 0.210000E+00 0.317927E+02 0.514205E+02 \n";
ofile << "INS 1PRCF 2 0.100000E+00 0.176802E+03 0.000000E+00 0.000000E+00 \n";
ofile << "INS 1PRCF 3 0.007000E+00 0.008000E+00 0.000000E+00 0.000000E+00 \n";
ofile << "INS 1PRCF 4 0.000000E+00 0.000000E+00 0.000000E+00 \n";
ofile.close();
}
else
{
throw runtime_error("Unable to open file to write.");
}
return;
}
//----------------------------------------------------------------------------------------------
/** Generate a 2 bank .irf file
*/
void generate2BankPrmFile(string filename)
{
ofstream ofile;
ofile.open(filename.c_str());
if (ofile.is_open())
{
ofile << "COMM Test file with two banks \n";
ofile << "INS BANK 2 \n";
ofile << "INS HTYPE PNTR \n";
ofile << "COMM5678901234567890 \n";
ofile << "INS 1 ICONS 746.96 -0.24 3.04 \n";
ofile << "INS 1BNKPAR 2.3696 9.39 0.00 .00000 .3000 1 1 \n";
ofile << "INS 1I ITYP 0 1.000 25.000 2 \n";
ofile << "INS 1PRCF 2 15 0.00100 \n";
ofile << "INS 1PRCF 1 0.000000E+00 0.210000E+00 0.317927E+02 0.514205E+02 \n";
ofile << "INS 1PRCF 2 0.100000E+00 0.176802E+03 0.000000E+00 0.000000E+00 \n";
ofile << "INS 1PRCF 3 0.007000E+00 0.000000E+00 0.000000E+00 0.000000E+00 \n";
ofile << "INS 1PRCF 4 0.000000E+00 0.000000E+00 0.000000E+00 \n";
ofile << "INS 2 ICONS 1482.98 0.98 12.65 \n";
ofile << "INS 2BNKPAR 1.7714 17.98 0.00 .00000 .3000 1 1 \n";
ofile << "INS 2I ITYP 0 1.000 21.000 2 \n";
ofile << "INS 2PRCF 2 15 0.00100 \n";
ofile << "INS 2PRCF 1 0.001000E+00 0.220000E+00 0.327927E+02 0.524205E+02 \n";
ofile << "INS 2PRCF 2 0.200000E+00 0.295572E+03 -0.134662E+01 0.000000E+00 \n";
ofile << "INS 2PRCF 3 0.361229E+01 0.000000E+00 0.000000E+00 0.000000E+00 \n";
ofile << "INS 2PRCF 4 0.000000E+00 0.000000E+00 0.000000E+00 \n";
ofile.close();
}
else
{
throw runtime_error("Unable to open file to write.");
}
return;
}
/* Return the number of rows the table must have
*/
int getExpectedNumberOfRows()
{
return 12; // Change this value if you add or remove any rows from the OutputTableWorkspace
}
private:
std::string wsName; // For Workspace property
};
#endif /* MANTID_DATAHANDLING_LOADGSASINSTRUMENTFILETEST_H_ */