MultiDomainFunctionTest.h 14.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
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
namespace {

class JacobianToTestNumDeriv: public Jacobian {
  size_t n[3];
  size_t np;
public:
  double off_diag;
  JacobianToTestNumDeriv():np(2),off_diag(0.0){
    // sizes of the three domains
    n[0] =  9;
    n[1] = 10;
    n[2] = 11;
  }
  void set(size_t iY, size_t iP, double value){
    // domain index the data point #iY comes from
    size_t jY = 2;
    size_t size = 0;
    for(size_t k = 0;k < 3;++k)
    {
      size += n[k];
      if ( iY < size )
      {
        jY = k;
        break;
      }
    }
    // domain index of function that has parameter #iP
    auto jP = iP / np;
    if ( jY != jP ) off_diag += value;
  }
84
  double get(size_t, size_t){
85
86
87
88
89
90
    return 0.0;
  }
};

}

91
92
93
class MultiDomainFunctionTest : public CxxTest::TestSuite
{
public:
94
95
96
97
98
  // 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; }

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  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
114
115
116
    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));
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

  }

  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
142
    const FunctionDomain1D& d = static_cast<const FunctionDomain1D&>(domain.getDomain(0));
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
    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
169
    const FunctionDomain1D& d = static_cast<const FunctionDomain1D&>(domain.getDomain(1));
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
    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
195
    const FunctionDomain1D& d = static_cast<const FunctionDomain1D&>(domain.getDomain(2));
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
    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
222
    const FunctionDomain1D& d0 = static_cast<const FunctionDomain1D&>(domain.getDomain(0));
223
224
225
226
    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
227
    const FunctionDomain1D& d1 = static_cast<const FunctionDomain1D&>(domain.getDomain(1));
228
229
230
231
    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
232
    const FunctionDomain1D& d2 = static_cast<const FunctionDomain1D&>(domain.getDomain(2));
233
234
235
236
237
238
239
    for(size_t i = 19; i < 30; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d2[i-19]);
    }

  }

240
241
242
243
244
245
246
247
248
249
250
251
252
253
  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 );
  }

254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  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
275
    const FunctionDomain1D& d0 = static_cast<const FunctionDomain1D&>(domain.getDomain(0));
276
277
278
279
280
281
282
    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
283
    const FunctionDomain1D& d1 = static_cast<const FunctionDomain1D&>(domain.getDomain(1));
284
285
286
287
288
289
290
    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
291
    const FunctionDomain1D& d2 = static_cast<const FunctionDomain1D&>(domain.getDomain(2));
292
293
294
295
296
297
298
    for(size_t i = 19; i < 30; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d2[i-19]);
    }

  }

299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  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
315
    const FunctionDomain1D& d0 = static_cast<const FunctionDomain1D&>(domain.getDomain(0));
316
317
318
319
320
321
322
    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
323
    const FunctionDomain1D& d1 = static_cast<const FunctionDomain1D&>(domain.getDomain(1));
324
325
326
327
328
329
330
    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
331
    const FunctionDomain1D& d2 = static_cast<const FunctionDomain1D&>(domain.getDomain(2));
332
333
334
    for(size_t i = 19; i < 30; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d2[i-19]);
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
    }

  }

  void test_attribute_domain_range()
  {
    multi.clearDomainIndices();
    multi.setLocalAttributeValue(0,"domains","0-2");
    return;
    multi.setLocalAttributeValue(1,"domains","i");
    multi.setLocalAttributeValue(2,"domains","i");

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

    double A = multi.getFunction(0)->getParameter("A");
    double B = multi.getFunction(0)->getParameter("B");
    const FunctionDomain1D& d0 = static_cast<const FunctionDomain1D&>(domain.getDomain(0));
    for(size_t i = 0; i < 9; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d0[i]);
    }

    A = multi.getFunction(0)->getParameter("A") + multi.getFunction(1)->getParameter("A");
    B = multi.getFunction(0)->getParameter("B") + multi.getFunction(1)->getParameter("B");
    const FunctionDomain1D& d1 = static_cast<const FunctionDomain1D&>(domain.getDomain(1));
    for(size_t i = 9; i < 19; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d1[i-9]);
    }

    A = multi.getFunction(0)->getParameter("A") + multi.getFunction(2)->getParameter("A");
    B = multi.getFunction(0)->getParameter("B") + multi.getFunction(2)->getParameter("B");
    const FunctionDomain1D& d2 = static_cast<const FunctionDomain1D&>(domain.getDomain(2));
    for(size_t i = 19; i < 30; ++i)
    {
      TS_ASSERT_EQUALS(values.getCalculated(i), A + B * d2[i-19]);
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
    }

  }

  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
394
    const FunctionDomain1D& d0 = static_cast<const FunctionDomain1D&>(domain.getDomain(0));
395
396
397
398
399
400
401
402
403
404
405
    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
406
    const FunctionDomain1D& d1 = static_cast<const FunctionDomain1D&>(domain.getDomain(1));
407
408
409
410
411
412
413
414
415
416
    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
417
    const FunctionDomain1D& d2 = static_cast<const FunctionDomain1D&>(domain.getDomain(2));
418
419
420
421
422
423
424
425
426
    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);

  }

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
  void test_derivatives_for_tied_parameters()
  {
    multi.clearDomainIndices();
    multi.setDomainIndex(0,0);
    multi.setDomainIndex(1,1);
    multi.setDomainIndex(2,2);
    {
      JacobianToTestNumDeriv jacobian;
      multi.functionDeriv(domain,jacobian);
      TS_ASSERT_EQUALS( jacobian.off_diag, 0.0 );
    }
    multi.setAttributeValue("NumDeriv",true);
    {
      JacobianToTestNumDeriv jacobian;
      multi.functionDeriv(domain,jacobian);
      TS_ASSERT_EQUALS( jacobian.off_diag, 0.0 );
    }
    multi.tie("f1.A","f0.A");
    multi.tie("f2.A","f0.A");
    multi.setAttributeValue("NumDeriv",false);
    {
      JacobianToTestNumDeriv jacobian;
      multi.functionDeriv(domain,jacobian);
      TS_ASSERT_EQUALS( jacobian.off_diag, 0.0 );
    }
    multi.setAttributeValue("NumDeriv",true);
    {
      JacobianToTestNumDeriv jacobian;
      multi.functionDeriv(domain,jacobian);
      TS_ASSERT_DIFFERS( jacobian.off_diag, 0.0 );
    }
  }

460
461
462
463
464
465
private:
  MultiDomainFunction multi;
  JointDomain domain;
};

#endif /*MULTIDOMAINFUNCTIONTEST_H_*/