MultiDomainFunctionTest.h 11.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
#ifndef MULTIDOMAINFUNCTIONTEST_H_
#define MULTIDOMAINFUNCTIONTEST_H_

#include "MantidAPI/FunctionDomain1D.h"
#include "MantidAPI/FunctionValues.h"
#include "MantidAPI/MultiDomainFunction.h"
#include "MantidAPI/JointDomain.h"
#include "MantidAPI/IFunction1D.h"
#include "MantidAPI/ParamFunction.h"
10
#include "MantidAPI/FunctionFactory.h"
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

#include <cxxtest/TestSuite.h>
#include <boost/make_shared.hpp>
#include <algorithm>
#include <iostream>

using namespace Mantid;
using namespace Mantid::API;

class MultiDomainFunctionTest_Function: public virtual IFunction1D, public virtual ParamFunction
{
public:
  MultiDomainFunctionTest_Function():IFunction1D(),ParamFunction()
  {
    this->declareParameter("A",0);
    this->declareParameter("B",0);
  }
  virtual std::string name() const {return "MultiDomainFunctionTest_Function";}
protected:
  virtual void function1D(double* out, const double* xValues, const size_t nData)const
  {
    const double A = getParameter(0);
    const double B = getParameter(1);

    for(size_t i = 0; i < nData; ++i)
    {
      double x = xValues[i];
      out[i] = A + B * x;
    }
  }
  virtual void functionDeriv1D(Jacobian* out, const double* xValues, const size_t nData)
  {
    for(size_t i = 0; i < nData; ++i)
    {
      double x = xValues[i];
      out->set(i,0,x);
      out->set(i,1,1.0);
    }
  }
};

52
53
DECLARE_FUNCTION(MultiDomainFunctionTest_Function);

54
55
56
class MultiDomainFunctionTest : public CxxTest::TestSuite
{
public:
57
58
59
60
61
  // This pair of boilerplate methods prevent the suite being created statically
  // This means the constructor isn't called when running other tests
  static MultiDomainFunctionTest *createSuite() { return new MultiDomainFunctionTest(); }
  static void destroySuite( MultiDomainFunctionTest *suite ) { delete suite; }

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  MultiDomainFunctionTest()
  {
    multi.addFunction(boost::make_shared<MultiDomainFunctionTest_Function>());
    multi.addFunction(boost::make_shared<MultiDomainFunctionTest_Function>());
    multi.addFunction(boost::make_shared<MultiDomainFunctionTest_Function>());

    multi.getFunction(0)->setParameter("A",0);
    multi.getFunction(0)->setParameter("B",1);

    multi.getFunction(1)->setParameter("A",1);
    multi.getFunction(1)->setParameter("B",2);

    multi.getFunction(2)->setParameter("A",2);
    multi.getFunction(2)->setParameter("B",3);

Roman Tolchenov's avatar
Roman Tolchenov committed
77
78
79
    domain.addDomain(boost::make_shared<FunctionDomain1DVector>(0,1,9));
    domain.addDomain(boost::make_shared<FunctionDomain1DVector>(1,2,10));
    domain.addDomain(boost::make_shared<FunctionDomain1DVector>(2,3,11));
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

  }

  void test_calc_domain0_only()
  {
    multi.setDomainIndex(0,0);
    multi.setDomainIndices(1,std::vector<size_t>());
    multi.setDomainIndices(2,std::vector<size_t>());
    //multi.setDomainIndex(1,1);
    //multi.setDomainIndex(2,2);

    //std::vector<size_t> ii;
    //ii.push_back(0);
    //ii.push_back(1);
    //multi.setDomainIndices(1,ii);
    //ii.clear();
    //ii.push_back(0);
    //ii.push_back(2);
    //multi.setDomainIndices(2,ii);

    FunctionValues values(domain);
    multi.function(domain,values);

    const double A = multi.getFunction(0)->getParameter("A");
    const double B = multi.getFunction(0)->getParameter("B");
Roman Tolchenov's avatar
Roman Tolchenov committed
105
    const FunctionDomain1D& d = static_cast<const FunctionDomain1D&>(domain.getDomain(0));
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
    for(size_t i = 0; i < 9; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d[i]);
    }
    for(size_t i = 9; i < 19; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), 0);
    }
    for(size_t i = 19; i < 30; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), 0);
    }

  }

  void test_calc_domain1_only()
  {
    multi.setDomainIndex(0,1);
    multi.setDomainIndices(1,std::vector<size_t>());
    multi.setDomainIndices(2,std::vector<size_t>());

    FunctionValues values(domain);
    multi.function(domain,values);

    const double A = multi.getFunction(0)->getParameter("A");
    const double B = multi.getFunction(0)->getParameter("B");
Roman Tolchenov's avatar
Roman Tolchenov committed
132
    const FunctionDomain1D& d = static_cast<const FunctionDomain1D&>(domain.getDomain(1));
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
    for(size_t i = 0; i < 9; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), 0);
    }
    for(size_t i = 9; i < 19; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d[i-9]);
    }
    for(size_t i = 19; i < 30; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), 0);
    }
  }

  void test_calc_domain2_only()
  {
    multi.setDomainIndex(0,2);
    multi.setDomainIndices(1,std::vector<size_t>());
    multi.setDomainIndices(2,std::vector<size_t>());

    FunctionValues values(domain);
    multi.function(domain,values);

    const double A = multi.getFunction(0)->getParameter("A");
    const double B = multi.getFunction(0)->getParameter("B");
Roman Tolchenov's avatar
Roman Tolchenov committed
158
    const FunctionDomain1D& d = static_cast<const FunctionDomain1D&>(domain.getDomain(2));
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
    for(size_t i = 0; i < 9; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), 0);
    }
    for(size_t i = 9; i < 19; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), 0);
    }
    for(size_t i = 19; i < 30; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d[i-19]);
    }

  }

  void test_calc_all_domains()
  {
    multi.clearDomainIndices();
    multi.setDomainIndices(1,std::vector<size_t>());
    multi.setDomainIndices(2,std::vector<size_t>());

    FunctionValues values(domain);
    multi.function(domain,values);

    const double A = multi.getFunction(0)->getParameter("A");
    const double B = multi.getFunction(0)->getParameter("B");
Roman Tolchenov's avatar
Roman Tolchenov committed
185
    const FunctionDomain1D& d0 = static_cast<const FunctionDomain1D&>(domain.getDomain(0));
186
187
188
189
    for(size_t i = 0; i < 9; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d0[i]);
    }
Roman Tolchenov's avatar
Roman Tolchenov committed
190
    const FunctionDomain1D& d1 = static_cast<const FunctionDomain1D&>(domain.getDomain(1));
191
192
193
194
    for(size_t i = 9; i < 19; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d1[i-9]);
    }
Roman Tolchenov's avatar
Roman Tolchenov committed
195
    const FunctionDomain1D& d2 = static_cast<const FunctionDomain1D&>(domain.getDomain(2));
196
197
198
199
200
201
202
    for(size_t i = 19; i < 30; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d2[i-19]);
    }

  }

203
204
205
206
207
208
209
210
211
212
213
214
215
216
  void test_set_wrong_index()
  {
    multi.setDomainIndices(1,std::vector<size_t>());
    multi.setDomainIndices(2,std::vector<size_t>());

    FunctionValues values(domain);

    multi.setDomainIndex(0,3);
    TS_ASSERT_THROWS( multi.function(domain,values), std::invalid_argument );

    multi.setDomainIndex(0,4);
    TS_ASSERT_THROWS( multi.function(domain,values), std::invalid_argument );
  }

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  void test_calc()
  {
    multi.setDomainIndex(0,0);
    std::vector<size_t> ii;
    ii.push_back(0);
    ii.push_back(1);
    multi.setDomainIndices(1,ii);
    ii.clear();
    ii.push_back(0);
    ii.push_back(2);
    multi.setDomainIndices(2,ii);

    FunctionValues values(domain);
    multi.function(domain,values);

    double A = multi.getFunction(0)->getParameter("A") + 
      multi.getFunction(1)->getParameter("A") + 
      multi.getFunction(2)->getParameter("A");
    double B = multi.getFunction(0)->getParameter("B") + 
      multi.getFunction(1)->getParameter("B") + 
      multi.getFunction(2)->getParameter("B");
Roman Tolchenov's avatar
Roman Tolchenov committed
238
    const FunctionDomain1D& d0 = static_cast<const FunctionDomain1D&>(domain.getDomain(0));
239
240
241
242
243
244
245
    for(size_t i = 0; i < 9; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d0[i]);
    }

    A = multi.getFunction(1)->getParameter("A");
    B = multi.getFunction(1)->getParameter("B");
Roman Tolchenov's avatar
Roman Tolchenov committed
246
    const FunctionDomain1D& d1 = static_cast<const FunctionDomain1D&>(domain.getDomain(1));
247
248
249
250
251
252
253
    for(size_t i = 9; i < 19; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d1[i-9]);
    }

    A = multi.getFunction(2)->getParameter("A");
    B = multi.getFunction(2)->getParameter("B");
Roman Tolchenov's avatar
Roman Tolchenov committed
254
    const FunctionDomain1D& d2 = static_cast<const FunctionDomain1D&>(domain.getDomain(2));
255
256
257
258
259
260
261
    for(size_t i = 19; i < 30; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d2[i-19]);
    }

  }

262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
  void test_attribute()
  {
    multi.clearDomainIndices();
    multi.setLocalAttributeValue(0,"domains","i");
    multi.setLocalAttributeValue(1,"domains","0,1");
    multi.setLocalAttributeValue(2,"domains","0,2");

    FunctionValues values(domain);
    multi.function(domain,values);

    double A = multi.getFunction(0)->getParameter("A") + 
      multi.getFunction(1)->getParameter("A") + 
      multi.getFunction(2)->getParameter("A");
    double B = multi.getFunction(0)->getParameter("B") + 
      multi.getFunction(1)->getParameter("B") + 
      multi.getFunction(2)->getParameter("B");
Roman Tolchenov's avatar
Roman Tolchenov committed
278
    const FunctionDomain1D& d0 = static_cast<const FunctionDomain1D&>(domain.getDomain(0));
279
280
281
282
283
284
285
    for(size_t i = 0; i < 9; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d0[i]);
    }

    A = multi.getFunction(1)->getParameter("A");
    B = multi.getFunction(1)->getParameter("B");
Roman Tolchenov's avatar
Roman Tolchenov committed
286
    const FunctionDomain1D& d1 = static_cast<const FunctionDomain1D&>(domain.getDomain(1));
287
288
289
290
291
292
293
    for(size_t i = 9; i < 19; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d1[i-9]);
    }

    A = multi.getFunction(2)->getParameter("A");
    B = multi.getFunction(2)->getParameter("B");
Roman Tolchenov's avatar
Roman Tolchenov committed
294
    const FunctionDomain1D& d2 = static_cast<const FunctionDomain1D&>(domain.getDomain(2));
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
    for(size_t i = 19; i < 30; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d2[i-19]);
    }

  }

  void test_attribute_in_FunctionFactory()
  {
    std::string ini = "composite=MultiDomainFunction;"
      "name=MultiDomainFunctionTest_Function,A=0,B=1,$domains=i;"
      "name=MultiDomainFunctionTest_Function,A=1,B=2,$domains=(0,1);"
      "name=MultiDomainFunctionTest_Function,A=2,B=3,$domains=(0,2)"
      ;
    auto mfun = boost::dynamic_pointer_cast<CompositeFunction>(FunctionFactory::Instance().createInitialized(ini));

    FunctionValues values(domain);
    mfun->function(domain,values);

    double A = mfun->getFunction(0)->getParameter("A") + 
      mfun->getFunction(1)->getParameter("A") + 
      mfun->getFunction(2)->getParameter("A");
    double B = mfun->getFunction(0)->getParameter("B") + 
      mfun->getFunction(1)->getParameter("B") + 
      mfun->getFunction(2)->getParameter("B");
Roman Tolchenov's avatar
Roman Tolchenov committed
320
    const FunctionDomain1D& d0 = static_cast<const FunctionDomain1D&>(domain.getDomain(0));
321
322
323
324
325
326
327
328
329
330
331
    double checksum = 0;
    for(size_t i = 0; i < 9; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d0[i]);
      checksum += values.getCalculated(i);
    }
    TS_ASSERT_DIFFERS(checksum,0);

    checksum = 0;
    A = mfun->getFunction(1)->getParameter("A");
    B = mfun->getFunction(1)->getParameter("B");
Roman Tolchenov's avatar
Roman Tolchenov committed
332
    const FunctionDomain1D& d1 = static_cast<const FunctionDomain1D&>(domain.getDomain(1));
333
334
335
336
337
338
339
340
341
342
    for(size_t i = 9; i < 19; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d1[i-9]);
      checksum += values.getCalculated(i);
    }
    TS_ASSERT_DIFFERS(checksum,0);

    checksum = 0;
    A = mfun->getFunction(2)->getParameter("A");
    B = mfun->getFunction(2)->getParameter("B");
Roman Tolchenov's avatar
Roman Tolchenov committed
343
    const FunctionDomain1D& d2 = static_cast<const FunctionDomain1D&>(domain.getDomain(2));
344
345
346
347
348
349
350
351
352
    for(size_t i = 19; i < 30; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d2[i-19]);
      checksum += values.getCalculated(i);
    }
    TS_ASSERT_DIFFERS(checksum,0);

  }

353
354
355
356
357
358
private:
  MultiDomainFunction multi;
  JointDomain domain;
};

#endif /*MULTIDOMAINFUNCTIONTEST_H_*/