test_MWL.cpp
8.7 KB
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
/***************************************************************************
* test_MWL.cpp - Test multi-wavelength classes *
* ----------------------------------------------------------------------- *
* copyright (C) 2010-2015 by Juergen Knoedlseder *
* ----------------------------------------------------------------------- *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
***************************************************************************/
/**
* @file test_MWL.cpp
* @brief Implementation of multi-wavelength classes unit tests
* @author Juergen Knoedlseder
*/
/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <iostream>
#include "GMWLLib.hpp"
#include "test_MWL.hpp"
/* __ Namespaces _________________________________________________________ */
/* __ Globals ____________________________________________________________ */
/* __ Constants __________________________________________________________ */
const std::string datadir = PACKAGE_SOURCE"/inst/mwl/test/data";
const std::string lat_crab_model = datadir+"/crab.xml";
const std::string lat_crab_fits = datadir+"/crab.fits";
const std::string crab_model = datadir+"/crab_mwl.xml";
const std::string crab_fits = datadir+"/crab_mwl.fits";
const std::string mwl_xml = datadir+"/obs_mwl.xml";
/***********************************************************************//**
* @brief Set test methods
***************************************************************************/
void TestGMWL::set(void)
{
// Set test name
name("GMWL");
// Append tests to test suite
append(static_cast<pfunction>(&TestGMWL::test_obs), "Test observation handling");
append(static_cast<pfunction>(&TestGMWL::test_optimizer), "Test optimizer");
// Return
return;
}
/***********************************************************************//**
* @brief Clone test suite
*
* @return Pointer to deep copy of test suite.
***************************************************************************/
TestGMWL* TestGMWL::clone(void) const
{
// Clone test suite
return new TestGMWL(*this);
}
/***********************************************************************//**
* @brief Test observation handling
***************************************************************************/
void TestGMWL::test_obs(void)
{
// Set filenames
const std::string file1 = "test_mwl_obs.xml";
// Construct observation
test_try("Construct observation");
try {
GMWLObservation run1;
GMWLObservation run2(lat_crab_fits);
GMWLObservation run3 = run2;
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Load observation
test_try("Load observation");
try {
GMWLObservation run;
run.load(lat_crab_fits);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Add observation to container
test_try("Add observation to container");
try {
GMWLObservation run;
run.load(lat_crab_fits);
GObservations obs;
run.id("001");
obs.append(run);
run.id("002");
obs.append(run);
run.id("003");
obs.append(run);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test XML loading
test_try("Test XML loading");
try {
GObservations obs = GObservations(mwl_xml);
obs.save(file1);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Exit test
return;
}
/***********************************************************************//**
* @brief Test optimizer
***************************************************************************/
void TestGMWL::test_optimizer(void)
{
// Declare observations
GObservations obs;
// Load multi-wavelength observations
test_try("Load multi-wavelength observations");
try {
GMWLObservation lat(lat_crab_fits);
obs.append(lat);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Setup model
test_try("Setup model");
try {
GModels models;
models.load(lat_crab_model);
obs.models(models);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Perform LM optimization
test_try("Perform LM optimization");
try {
GLog log;
log.cout(false);
GOptimizerLM opt(&log);
opt.max_iter(1000);
obs.optimize(opt);
obs.errors(opt);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Load multi-wavelength observations
test_try("Load multi-wavelength observations");
try {
obs.clear();
GMWLObservation comptel(crab_fits+"[COMPTEL]");
GMWLObservation lat(crab_fits+"[LAT]");
GMWLObservation hess(crab_fits+"[HESS]");
obs.append(comptel);
obs.append(lat);
obs.append(hess);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Setup model
test_try("Setup model");
try {
GModels models;
models.load(crab_model);
obs.models(models);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Perform LM optimization
double fit_results[] = {83.6331, 0,
22.0145, 0,
0.00231438040883405, 0.000191759607698647,
-3.10859, 0.124729,
1, 0,
1, 0,
83.6331, 0,
22.0145, 0,
4.5050091460345e-11, 1.95885803229878e-12,
-2.13719, 0.00578641,
1000, 0,
1, 0};
test_try("Perform LM optimization");
try {
GLog log;
log.cout(false);
GOptimizerLM opt(&log);
opt.max_iter(1000);
obs.optimize(opt);
obs.errors(opt);
test_try_success();
for (int i = 0, j = 0; i < obs.models().size(); ++i) {
const GModel* model = obs.models()[i];
for (int k = 0; k < model->size(); ++k) {
GModelPar par = (*model)[k];
std::string msg = "Verify optimization result for " + par.print();
test_value(par.value(), fit_results[j++], 1.0e-4, msg);
test_value(par.error(), fit_results[j++], 1.0e-3, msg);
}
}
}
catch (std::exception &e) {
test_try_failure(e);
}
// Exit test
return;
}
/***************************************************************************
* @brief Main entry point for test executable
***************************************************************************/
int main(void)
{
// Allocate test suit container
GTestSuites testsuites("Multi-wavelength instrument specific class testing");
// Initially assume that we pass all tests
bool success = true;
// Create a test suite
TestGMWL test;
// Append test suite to the container
testsuites.append(test);
// Run the testsuites
success = testsuites.run();
// Save test report
testsuites.save("reports/GMWL.xml");
// Return success status
return (success ? 0 : 1);
}