FitTest.h 86.5 KB
Newer Older
1
2
3
4
#ifndef CURVEFITTING_FITTEST_H_
#define CURVEFITTING_FITTEST_H_

#include "MantidTestHelpers/FakeObjects.h"
5
#include <cxxtest/TestSuite.h>
6

7
#include "FitTestHelpers.h"
8
9
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/FuncMinimizerFactory.h"
10
#include "MantidAPI/FunctionFactory.h"
11
#include "MantidAPI/IFuncMinimizer.h"
12
#include "MantidAPI/IPawleyFunction.h"
Shahroz Ahmed's avatar
Shahroz Ahmed committed
13
#include "MantidAPI/TableRow.h"
14
#include "MantidAPI/WorkspaceFactory.h"
15
#include "MantidCurveFitting/Algorithms/Fit.h"
16
#include "MantidCurveFitting/Functions/PawleyFunction.h"
Shahroz Ahmed's avatar
Shahroz Ahmed committed
17
#include "MantidDataObjects/TableWorkspace.h"
Shahroz Ahmed's avatar
Shahroz Ahmed committed
18
#include "MantidDataObjects/Workspace2D.h"
19

20
#include "MantidTestHelpers/FunctionCreationHelper.h"
21
#include "MantidTestHelpers/MultiDomainFunctionHelper.h"
22
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
23

24
#include <Poco/File.h>
25
26

using namespace Mantid;
27
using namespace Mantid::Kernel;
28
using namespace Mantid::API;
29
using namespace Mantid::CurveFitting;
30
using namespace Mantid::CurveFitting::Algorithms;
Shahroz Ahmed's avatar
Shahroz Ahmed committed
31
using namespace Mantid::DataObjects;
32

33
34
using Mantid::TestHelpers::MultiDomainFunctionTest_Function;

35
36
37
38
39
40
namespace {
class TestMinimizer : public API::IFuncMinimizer {
public:
  /// Constructor setting a value for the relative error acceptance
  /// (default=0.01)
  TestMinimizer() {
41
42
43
44
    declareProperty(
        Kernel::make_unique<API::WorkspaceProperty<API::MatrixWorkspace>>(
            "SomeOutput", "abc", Kernel::Direction::Output),
        "Name of the output Workspace holding some output.");
45
  }
46

47
  /// Overloading base class methods.
48
  std::string name() const override { return "TestMinimizer"; }
49
  /// Do one iteration.
50
  bool iterate(size_t iter) override {
51
    m_data[iter] = iter;
52

53
54
55
56
57
58
    if (iter >= m_data.size() - 1) {
      API::MatrixWorkspace_sptr ws = API::WorkspaceFactory::Instance().create(
          "Workspace2D", 1, m_data.size(), m_data.size());
      auto &Y = ws->dataY(0);
      for (size_t i = 0; i < Y.size(); ++i) {
        Y[i] = static_cast<double>(m_data[i]);
59
      }
60
61
62
63
64
65
66
      setProperty("SomeOutput", ws);
      return false;
    }
    return true;
  }

  /// Return current value of the cost function
67
  double costFunctionVal() override { return 0.0; }
68
  /// Initialize minimizer.
69
  void initialize(API::ICostFunction_sptr, size_t maxIterations = 0) override {
70
71
    m_data.resize(maxIterations);
  }
72

73
74
75
private:
  std::vector<size_t> m_data;
};
76

77
DECLARE_FUNCMINIMIZER(TestMinimizer, TestMinimizer)
78
79
}

80
class FitTest : public CxxTest::TestSuite {
81
82
83
84
public:
  // This pair of boilerplate methods prevent the suite being created statically
  // This means the constructor isn't called when running other tests
  static FitTest *createSuite() { return new FitTest(); }
85
86
87
  static void destroySuite(FitTest *suite) { delete suite; }

  FitTest() {
88
89
90
91
    // need to have DataObjects loaded
    FrameworkManager::Instance();
  }

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  void test_empty_function() {
    boost::shared_ptr<Mantid::API::MultiDomainFunction> multi;

    auto alg = Mantid::API::AlgorithmManager::Instance().create("Fit");
    alg->initialize();
    TS_ASSERT_THROWS(
        alg->setProperty("Function",
                         boost::dynamic_pointer_cast<IFunction>(multi)),
        std::invalid_argument);
  }

  void test_empty_function_str() {
    auto alg = Mantid::API::AlgorithmManager::Instance().create("Fit");
    alg->initialize();
    TS_ASSERT_THROWS(alg->setPropertyValue("Function", ""),
                     std::invalid_argument);
  }

110
111
112
  // Test that Fit copies minimizer's output properties to Fit
  // Test that minimizer's iterate(iter) method is called maxIteration times
  //  and iter passed to iterate() has values within 0 <= iter < maxIterations
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  void test_minimizer_output() {
    API::MatrixWorkspace_sptr ws =
        API::WorkspaceFactory::Instance().create("Workspace2D", 1, 1, 1);
    Fit fit;
    fit.initialize();

    fit.setProperty("Function", "name=LinearBackground");
    fit.setProperty("InputWorkspace", ws);
    fit.setProperty("MaxIterations", 99);
    fit.setProperty("Minimizer", "TestMinimizer,SomeOutput=MinimizerOutput");
    fit.setProperty("CreateOutput", true);

    fit.execute();
    TS_ASSERT(fit.existsProperty("SomeOutput"));
    TS_ASSERT_EQUALS(fit.getPropertyValue("SomeOutput"), "MinimizerOutput");
    TS_ASSERT(
        API::AnalysisDataService::Instance().doesExist("MinimizerOutput"));
130

131
132
133
134
135
136
137
138
139
140
141
    API::MatrixWorkspace_sptr outWS =
        API::AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "MinimizerOutput");
    TS_ASSERT(outWS);
    auto &y = outWS->readY(0);
    TS_ASSERT_EQUALS(y.size(), 99);
    for (size_t iter = 0; iter < 99; ++iter) {
      TS_ASSERT_EQUALS(y[iter], static_cast<double>(iter));
    }

    API::AnalysisDataService::Instance().clear();
142
143
  }

144
145
  // Test that minimizer's output is'n passed to Fit if no other output is
  // created.
146
  // Other output are: fitting parameters table, calculated values.
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  // To create output either CreateOutput must be set to true or Output be set
  // to non-empty string
  void test_minimizer_output_not_passed_to_Fit() {
    API::MatrixWorkspace_sptr ws =
        API::WorkspaceFactory::Instance().create("Workspace2D", 1, 1, 1);
    Fit fit;
    fit.initialize();

    fit.setProperty("Function", "name=LinearBackground");
    fit.setProperty("InputWorkspace", ws);
    fit.setProperty("MaxIterations", 99);
    fit.setProperty("Minimizer", "TestMinimizer,SomeOutput=MinimizerOutput");

    fit.execute();
    TS_ASSERT(!fit.existsProperty("SomeOutput"));
    TS_ASSERT(
        !API::AnalysisDataService::Instance().doesExist("MinimizerOutput"));
  }
165
166
167
168
169
170
171
172
173
174
175
176
177
178

  void test_function_Abragam() {

    // create mock data to test against
    int ndata = 21;
    API::MatrixWorkspace_sptr ws = API::WorkspaceFactory::Instance().create(
        "Workspace2D", 1, ndata, ndata);
    Mantid::MantidVec &x = ws->dataX(0);
    Mantid::MantidVec &y = ws->dataY(0);
    Mantid::MantidVec &e = ws->dataE(0);
    for (int i = 0; i < ndata; i++) {
      x[i] = static_cast<double>(i);
      e[i] = 0.01;
    }
179
180
181
182
183
    y = {0.212132034,  0.110872429,  -0.004130004, -0.107644046, -0.181984622,
         -0.218289678, -0.215908947, -0.180739307, -0.123016506, -0.054943061,
         0.011526466,  0.066481012,  0.103250678,  0.118929645,  0.114251678,
         0.092934753,  0.060672555,  0.023977227,  -0.010929869, -0.039018774,
         -0.057037526};
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

    Fit fit;
    fit.initialize();
    fit.setProperty("Function", "name=Abragam");
    fit.setProperty("InputWorkspace", ws);
    fit.execute();

    // Test the goodness of the fit
    double chi2 = fit.getProperty("OutputChi2overDoF");
    TS_ASSERT_DELTA(chi2, 0.000001, 0.000001);

    // Test the fitting parameters
    IFunction_sptr func = fit.getProperty("Function");
    TS_ASSERT_DELTA(func->getParameter("A"), 0.3, 0.001);
    TS_ASSERT_DELTA(func->getParameter("Omega"), 0.4, 0.001);
    TS_ASSERT_DELTA(func->getParameter("Phi"), M_PI / 4.0, 0.01); // 45 degrees
    TS_ASSERT_DELTA(func->getParameter("Sigma"), 0.2, 0.001);
    TS_ASSERT_DELTA(func->getParameter("Tau"), 2.0, 0.01);
  }
203
204
205
206

  void test_function_ExpDecayMuon() {

    // Mock data
207
    int ndata = 19;
208
209
210
211
212
213
214
215
216
    API::MatrixWorkspace_sptr ws = API::WorkspaceFactory::Instance().create(
        "Workspace2D", 1, ndata, ndata);
    Mantid::MantidVec &x = ws->dataX(0);
    Mantid::MantidVec &y = ws->dataY(0);
    Mantid::MantidVec &e = ws->dataE(0);
    for (int i = 0; i < ndata; i++) {
      x[i] = static_cast<double>(i);
      e[i] = 1.0;
    }
Raquel Alvarez Banos's avatar
Raquel Alvarez Banos committed
217
218
219
220
221
    y = {5.0, 3.582656552869, 2.567085595163, 1.839397205857, 1.317985690579,
         0.9443780141878, 0.6766764161831, 0.484859839322, 0.347417256114,
         0.2489353418393, 0.1783699667363, 0.1278076660325, 0.09157819444367,
         0.0656186436847, 0.04701781275748, 0.03368973499543, 0.02413974996916,
         0.01729688668232, 0.01239376088333};
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

    Fit fit;
    fit.initialize();
    fit.setProperty("Function", "name=ExpDecayMuon");
    fit.setProperty("InputWorkspace", ws);
    fit.execute();

    // Test the goodness of the fit
    double chi2 = fit.getProperty("OutputChi2overDoF");
    TS_ASSERT_DELTA(chi2, 0.0001, 0.0001);

    // Test the fitting parameters
    IFunction_sptr func = fit.getProperty("Function");
    TS_ASSERT_DELTA(func->getParameter("A"), 5, 0.0001);
    TS_ASSERT_DELTA(func->getParameter("Lambda"), 0.3333, 0.001);
  }
238
239
240
241
242
243
244
245
246
247
248
249

  void test_function_ExpDecayOsc() {

    // Mock data
    int ndata = 20;
    const double sqrh = 0.70710678; // cos( 45 degrees )

    API::MatrixWorkspace_sptr ws = API::WorkspaceFactory::Instance().create(
        "Workspace2D", 1, ndata, ndata);
    Mantid::MantidVec &x = ws->dataX(0);
    Mantid::MantidVec &y = ws->dataY(0);
    Mantid::MantidVec &e = ws->dataE(0);
250
    for (int i = 0; i < ndata; i++) {
251
252
253
      x[i] = static_cast<double>(i);
      e[i] = 1.;
    }
Raquel Alvarez Banos's avatar
Raquel Alvarez Banos committed
254
255
256
257
258
259
    y = {5 * sqrh, 0.0, -2.567085595163 * sqrh, -1.839397205857,
         -1.317985690579 * sqrh, 0.0, 0.6766764161831 * sqrh, 0.484859839322,
         0.347417256114 * sqrh, 0.0, -0.1783699667363 * sqrh, -0.1278076660325,
         -0.09157819444367 * sqrh, 0.0, 0.04701781275748 * sqrh,
         0.03368973499543, 0.02413974996916 * sqrh, 0.0,
         -0.01239376088333 * sqrh, 0.0};
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275

    Fit fit;
    fit.initialize();
    fit.setProperty("Function", "name=ExpDecayOsc");
    fit.setProperty("InputWorkspace", ws);
    fit.setProperty("Constraints", "0.01 < Frequency < 0.2, 0.01 < Phi < 1.0");
    fit.execute();

    // Test the fitting parameters
    IFunction_sptr func = fit.getProperty("Function");
    TS_ASSERT_DELTA(func->getParameter("A"), 5, 0.01);
    TS_ASSERT_DELTA(func->getParameter("Lambda"), 1 / 3.0, 0.01);
    TS_ASSERT_DELTA(func->getParameter("Frequency"), 1 / 8.0,
                    0.01);                                    // Period of 8
    TS_ASSERT_DELTA(func->getParameter("Phi"), M_PI_4, 0.01); // 45 degrees
  }
276
277
278
279

  void test_function_GausDecay() {

    // Mock data
280
    int ndata = 18;
281
282
283
284
285
286
287
288
289
    API::MatrixWorkspace_sptr ws = API::WorkspaceFactory::Instance().create(
        "Workspace2D", 1, ndata, ndata);
    Mantid::MantidVec &x = ws->dataX(0);
    Mantid::MantidVec &y = ws->dataY(0);
    Mantid::MantidVec &e = ws->dataE(0);
    for (int i = 0; i < ndata; i++) {
      x[i] = static_cast<double>(i - 8);
      e[i] = 1.0;
    }
290
291
    y = {0.01,  0.16,  1.2,   5.6,  18.2, 43.68, 80.08, 114.4, 128.7,
         114.4, 80.08, 43.68, 18.2, 5.6,  1.2,   0.16,  0.01,  0.00};
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307

    Fit fit;
    fit.initialize();
    fit.setProperty("Function", "name=GausDecay");
    fit.setProperty("InputWorkspace", ws);
    fit.execute();

    // Test the goodness of the fit
    double chi2 = fit.getProperty("OutputChi2overDoF");
    TS_ASSERT_DELTA(chi2, 0.0, 1.0);

    // Test the fitting parameters
    IFunction_sptr func = fit.getProperty("Function");
    TS_ASSERT_DELTA(func->getParameter("A"), 129.194, 0.001);
    TS_ASSERT_DELTA(func->getParameter("Sigma"), 0.348, 0.001);
  }
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322

  void test_function_GausOsc() {

    // Mock data
    int ndata = 18;
    API::MatrixWorkspace_sptr ws = API::WorkspaceFactory::Instance().create(
        "Workspace2D", 1, ndata, ndata);
    Mantid::MantidVec &x = ws->dataX(0);
    Mantid::MantidVec &y = ws->dataY(0);
    Mantid::MantidVec &e = ws->dataE(0);
    for (int i = 0; i < ndata; i++) {
      x[i] = static_cast<double>(i - 8);
      e[i] = 1.0;
    }
    const double sqrh = 0.70710678; // cos( 45 degrees )
323
324
325
    y = {0.01 * sqrh,  0.00,  -1.2 * sqrh,  -5.6, -18.2 * sqrh,  0.0,
         80.08 * sqrh, 114.4, 128.7 * sqrh, 0.0,  -80.08 * sqrh, -43.68,
         -18.2 * sqrh, 0.0,   1.2 * sqrh,   0.16, 0.01 * sqrh,   0.00};
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344

    Fit fit;
    fit.initialize();
    fit.setProperty("Function", "name=GausOsc");
    fit.setProperty("InputWorkspace", ws);
    fit.execute();

    // Test the goodness of the fit
    double chi2 = fit.getProperty("OutputChi2overDoF");
    TS_ASSERT_DELTA(chi2, 0.0, 1.0);

    // Test the fitting parameters
    IFunction_sptr func = fit.getProperty("Function");
    TS_ASSERT_DELTA(func->getParameter("A"), 129.300, 0.001);
    TS_ASSERT_DELTA(func->getParameter("Sigma"), 0.348, 0.001);
    TS_ASSERT_DELTA(func->getParameter("Frequency"), 1 / 8.0,
                    0.01);                                    // Period of 8
    TS_ASSERT_DELTA(func->getParameter("Phi"), M_PI_4, 0.01); //  45 degrees
  }
345

346
347
348
349
350
351
352
353
354
  void test_function_Keren() {

    // Mock data
    int ndata = 41;
    API::MatrixWorkspace_sptr ws = API::WorkspaceFactory::Instance().create(
        "Workspace2D", 1, ndata, ndata);
    Mantid::MantidVec &x = ws->dataX(0);
    Mantid::MantidVec &y = ws->dataY(0);
    Mantid::MantidVec &e = ws->dataE(0);
355
356
357
    for (int i = 0; i < ndata; i++) {
      x[i] = 0.146785 * static_cast<double>(i);
    }
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
    y = {1,        0.950342, 0.875263, 0.848565, 0.859885, 0.8632,   0.839704,
         0.808929, 0.790497, 0.782535, 0.772859, 0.75648,  0.738228, 0.723282,
         0.711316, 0.69916,  0.685455, 0.671399, 0.658356, 0.646277, 0.634338,
         0.622165, 0.610055, 0.598363, 0.587083, 0.575999, 0.565007, 0.554178,
         0.543602, 0.533278, 0.523147, 0.513177, 0.503385, 0.493792, 0.484394,
         0.475175, 0.466123, 0.45724,  0.448529, 0.439988, 0.43161};
    e = {0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01,
         0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01,
         0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01,
         0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01};

    Fit fit;
    fit.initialize();
    fit.setProperty("Function", "name=Keren, Field=80, Fluct=0.2, Delta=0.2");
    fit.setProperty("InputWorkspace", ws);
    fit.setProperty("WorkspaceIndex", 0);

    TS_ASSERT_THROWS_NOTHING(fit.execute());
    TS_ASSERT(fit.isExecuted());
    std::string status = fit.getPropertyValue("OutputStatus");
    TS_ASSERT_EQUALS("success", status);

    // check the output
    const double field = 100;
382
383
    const double delta = Mantid::PhysicalConstants::MuonGyromagneticRatio *
                         field * 2.0 * M_PI * 0.2;
384
385
386
387
388
389
390
    const double fluct = delta;
    IFunction_sptr out = fit.getProperty("Function");
    TS_ASSERT_DELTA(out->getParameter("Field"), field, 0.001);
    TS_ASSERT_DELTA(out->getParameter("Delta"), delta, 0.001);
    TS_ASSERT_DELTA(out->getParameter("Fluct"), fluct, 0.001);
  }

391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
  void test_function_StaticKuboToyabe() {

    // Mock data
    int ndata = 18;
    API::MatrixWorkspace_sptr ws = API::WorkspaceFactory::Instance().create(
        "Workspace2D", 1, ndata, ndata);
    Mantid::MantidVec &x = ws->dataX(0);
    Mantid::MantidVec &y = ws->dataY(0);
    Mantid::MantidVec &e = ws->dataE(0);
    for (int i = 0; i < ndata; i++) {
      x[i] = static_cast<double>(i);
      e[i] = 0.01;
    }
    // Calculated with A = 0.24 and Delta = 0.16 on an Excel spreadsheet
    y = {0.24,        0.233921146, 0.216447929, 0.189737312, 0.156970237,
         0.121826185, 0.08791249,  0.058260598, 0.034976545, 0.019090369,
         0.01060189,  0.008680652, 0.011954553, 0.018817301, 0.027696749,
         0.037247765, 0.046457269, 0.054669182};

    Fit fit;
    fit.initialize();
    fit.setProperty("Function", "name=StaticKuboToyabe");
    fit.setProperty("InputWorkspace", ws);
    fit.execute();

    // Test the goodness of the fit
    double chi2 = fit.getProperty("OutputChi2overDoF");
    TS_ASSERT_DELTA(chi2, 0.0001, 0.0001);

    // Test the fitting parameters
    IFunction_sptr func = fit.getProperty("Function");
    TS_ASSERT_DELTA(func->getParameter("A"), 0.24, 0.001);
    TS_ASSERT_DELTA(func->getParameter("Delta"), 0.16, 0.001);
  }
425
426
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_function_StaticKuboToyabeTimesExpDecay() {

    // Mock data
    int ndata = 15;
    API::MatrixWorkspace_sptr ws = API::WorkspaceFactory::Instance().create(
        "Workspace2D", 1, ndata, ndata);
    Mantid::MantidVec &x = ws->dataX(0);
    Mantid::MantidVec &y = ws->dataY(0);
    Mantid::MantidVec &e = ws->dataE(0);
    for (int i = 0; i < ndata; i++) {
      x[i] = static_cast<double>(i);
      e[i] = 1.0;
    }
    // A = 0.24, Delta = 0.16, Lambda = 0.1
    y = {0.24,       0.211661,   0.177213,   0.140561,   0.10522,
         0.0738913,  0.0482474,  0.0289314,  0.015716,   0.00776156,
         0.00390022, 0.00288954, 0.00360064, 0.00512831, 0.00682993};

    Fit fit;
    fit.initialize();
    fit.setProperty("Function", "name=StaticKuboToyabeTimesExpDecay");
    fit.setProperty("InputWorkspace", ws);
    fit.execute();

    // Test the goodness of the fit
    double chi2 = fit.getProperty("OutputChi2overDoF");
    TS_ASSERT_DELTA(chi2, 0.0001, 0.0001);

    // Test the fitting parameters
    IFunction_sptr func = fit.getProperty("Function");
    TS_ASSERT_DELTA(func->getParameter("A"), 0.24, 0.0001);
    TS_ASSERT_DELTA(func->getParameter("Delta"), 0.16, 0.001);
    TS_ASSERT_DELTA(func->getParameter("Lambda"), 0.1, 0.001);
  }
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494

  void test_function_StaticKuboToyabeTimesGausDecay() {

    // Mock data
    int ndata = 15;
    API::MatrixWorkspace_sptr ws = API::WorkspaceFactory::Instance().create(
        "Workspace2D", 1, ndata, ndata);
    Mantid::MantidVec &x = ws->dataX(0);
    Mantid::MantidVec &y = ws->dataY(0);
    Mantid::MantidVec &e = ws->dataE(0);
    for (int i = 0; i < ndata; i++) {
      x[i] = static_cast<double>(i);
      e[i] = 1.0;
    }
    // A = 0.24, Delta = 0.16, Sigma = 0.1
    y = {0.24,       0.231594,   0.207961,   0.173407,   0.133761,
         0.0948783,  0.0613345,  0.035692,   0.0184429,  0.0084925,
         0.00390022, 0.00258855, 0.00283237, 0.00347216, 0.00390132};

    Fit fit;
    fit.initialize();
    fit.setProperty("Function", "name=StaticKuboToyabeTimesGausDecay");
    fit.setProperty("InputWorkspace", ws);
    fit.execute();

    // Test the goodness of the fit
    double chi2 = fit.getProperty("OutputChi2overDoF");
    TS_ASSERT_DELTA(chi2, 0.0001, 0.0001);

    // Test the fitting parameters
    IFunction_sptr func = fit.getProperty("Function");
    TS_ASSERT_DELTA(func->getParameter("A"), 0.24, 0.0001);
    TS_ASSERT_DELTA(func->getParameter("Delta"), 0.16, 0.001);
    TS_ASSERT_DELTA(func->getParameter("Sigma"), 0.1, 0.001);
  }
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532

  void test_function_StaticKuboToyabeTimesStretchExp() {

    // Mock data
    int ndata = 18;
    API::MatrixWorkspace_sptr ws = API::WorkspaceFactory::Instance().create(
        "Workspace2D", 1, ndata, ndata);
    Mantid::MantidVec &x = ws->dataX(0);
    Mantid::MantidVec &y = ws->dataY(0);
    Mantid::MantidVec &e = ws->dataE(0);
    for (int i = 0; i < ndata; i++) {
      x[i] = static_cast<double>(i);
      e[i] = 1.0;
    }
    // Calculated with A = 0.24, Delta = 0.06, Lambda = 0.63 and
    // Beta = 0.63
    y = {0.24,        0.113248409, 0.074402367, 0.052183632, 0.037812471,
         0.027927981, 0.020873965, 0.015717258, 0.011885418, 0.009005914,
         0.006825573, 0.005166593, 0.003900885, 0.002934321, 0.002196637,
         0.001634742, 0.001208136, 0.000885707};

    Fit fit;
    fit.initialize();
    fit.setProperty("Function", "name=StaticKuboToyabeTimesStretchExp");
    fit.setProperty("InputWorkspace", ws);
    fit.execute();

    // Test the goodness of the fit
    double chi2 = fit.getProperty("OutputChi2overDoF");
    TS_ASSERT_DELTA(chi2, 0.0001, 0.0001);

    // Test the fitting parameters
    IFunction_sptr func = fit.getProperty("Function");
    TS_ASSERT_DELTA(func->getParameter("A"), 0.24, 0.0001);
    TS_ASSERT_DELTA(func->getParameter("Delta"), 0.06, 0.001);
    TS_ASSERT_DELTA(func->getParameter("Lambda"), 0.63, 0.001);
    TS_ASSERT_LESS_THAN(func->getParameter("Beta"), 1.00);
  }
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571

  void test_function_StretchExpMuon() {

    // Mock data
    int ndata = 20;
    API::MatrixWorkspace_sptr ws = API::WorkspaceFactory::Instance().create(
        "Workspace2D", 1, ndata, ndata);
    Mantid::MantidVec &x = ws->dataX(0);
    Mantid::MantidVec &y = ws->dataY(0);
    Mantid::MantidVec &e = ws->dataE(0);
    // values extracted from y(x)=2*exp(-(x/4)^0.5)
    y = {2,          1.2130613,  0.98613738, 0.84124005, 0.73575888,
         0.65384379, 0.58766531, 0.53273643, 0.48623347, 0.44626032,
         0.41148132, 0.38092026, 0.35384241, 0.32968143, 0.30799199,
         0.28841799, 0.27067057, 0.25451242, 0.2397465,  0.22620756};
    for (int i = 0; i < ndata; i++) {
      x[i] = static_cast<double>(i);
      e[i] = 0.1 * y[i];
    }

    Fit fit;
    fit.initialize();
    fit.setProperty("Function",
                    "name=StretchExpMuon, A=1.5, Lambda=0.2, Beta=0.4");
    fit.setProperty("InputWorkspace", ws);
    fit.setPropertyValue("StartX", "0");
    fit.setPropertyValue("EndX", "19");
    fit.execute();

    // Test the goodness of the fit
    double chi2 = fit.getProperty("OutputChi2overDoF");
    TS_ASSERT_DELTA(chi2, 0.001, 0.001);

    // Test the fitting parameters
    IFunction_sptr func = fit.getProperty("Function");
    TS_ASSERT_DELTA(func->getParameter("A"), 2.0, 0.02);
    TS_ASSERT_DELTA(func->getParameter("Lambda"), 0.25, 0.0025);
    TS_ASSERT_DELTA(func->getParameter("Beta"), 0.5, 0.05);
  }
572

573
574
  void test_function_EndErfc() {

Shahroz Ahmed's avatar
Shahroz Ahmed committed
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
    // Mock data
    int ndata = 13;
    API::MatrixWorkspace_sptr ws = API::WorkspaceFactory::Instance().create(
        "Workspace2D", 1, ndata, ndata);
    Mantid::MantidVec &x = ws->dataX(0);
    Mantid::MantidVec &y = ws->dataY(0);
    Mantid::MantidVec &e = ws->dataE(0);
    // values extracted from y(x)=2*exp(-(x/4)^0.5)
    y = {1, 3, 4, 28, 221, 872, 1495, 1832, 1830, 1917, 2045, 1996, 0};
    for (int i = 0; i < ndata; i++) {
      x[i] = static_cast<double>(5 * i);
      e[i] = 1.0;
    }

    Fit fit;
    fit.initialize();
    fit.setProperty("Function", "name=EndErfc, A=2000, B=50, C=6, D=0");
    fit.setProperty("InputWorkspace", ws);
    fit.setPropertyValue("StartX", "5");
    fit.setPropertyValue("EndX", "55");
    fit.execute();

    double dummy = fit.getProperty("OutputChi2overDoF");
    TS_ASSERT_DELTA(dummy, 0.0001, 20000);

600
    // Test the goodness of the fit. This is fit looks really bad
601
    double chi2 = fit.getProperty("OutputChi2overDoF");
602
    TS_ASSERT_DELTA(chi2, 4000, 500);
603

Shahroz Ahmed's avatar
Shahroz Ahmed committed
604
605
606
607
608
    IFunction_sptr out = fit.getProperty("Function");
    TS_ASSERT_DELTA(out->getParameter("A"), 1000, 30.0);
    TS_ASSERT_DELTA(out->getParameter("B"), 26, 0.1);
    TS_ASSERT_DELTA(out->getParameter("C"), 7.7, 0.1);
    TS_ASSERT_DELTA(out->getParameter("D"), 0, 0.1);
609
610
  }

611
  void test_function_ProductFunction() {
612

Shahroz Ahmed's avatar
Shahroz Ahmed committed
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
    // Mock data
    int ndata = 30;
    API::MatrixWorkspace_sptr ws = API::WorkspaceFactory::Instance().create(
        "Workspace2D", 1, ndata, ndata);
    Mantid::MantidVec &x = ws->dataX(0);
    Mantid::MantidVec &y = ws->dataY(0);
    Mantid::MantidVec &e = ws->dataE(0);
    x = {0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9,
         1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9,
         2, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9};
    y = {0.001362, 0.00434468, 0.0127937, 0.0347769, 0.0872653, 0.202138,
         0.432228, 0.853165,   1.55457,   2.61483,   4.06006,   5.8194,
         7.69982,  9.40459,    10.6036,   11.0364,   10.6036,   9.40459,
         7.69982,  5.8194,     4.06006,   2.61483,   1.55457,   0.853165,
         0.432228, 0.202138,   0.0872653, 0.0347769, 0.0127937, 0.00434468};
    e.assign(ndata, 0.1);

    Mantid::CurveFitting::Algorithms::Fit fit;
    fit.initialize();

    fit.setPropertyValue(
        "Function", "composite=ProductFunction,NumDeriv=false;name="
                    "Gaussian,Height=3,PeakCentre=1,Sigma=0.5,ties=(Height="
                    "3.0,PeakCentre=1.0,Sigma=0.5);name=Gaussian,Height=15,"
                    "PeakCentre=2.5,Sigma=0.5,ties=(Sigma=0.5)");
    fit.setProperty("InputWorkspace", ws);
    fit.setPropertyValue("WorkspaceIndex", "0");

    // execute fit
    TS_ASSERT_THROWS_NOTHING(TS_ASSERT(fit.execute()))
    TS_ASSERT(fit.isExecuted());

    // test the output from fit is what you expect

    double dummy = fit.getProperty("OutputChi2overDoF");
    TS_ASSERT_DELTA(dummy, 0.0, 0.01);

    Mantid::API::IFunction_sptr outF = fit.getProperty("Function");

    TS_ASSERT_DELTA(outF->getParameter("f0.PeakCentre"), 1.0, 0.001);
    TS_ASSERT_DELTA(outF->getParameter("f0.Height"), 3.0, 0.001);
    TS_ASSERT_DELTA(outF->getParameter("f0.Sigma"), 0.5, 0.001);
    TS_ASSERT_DELTA(outF->getParameter("f1.PeakCentre"), 2.0, 0.001);
    TS_ASSERT_DELTA(outF->getParameter("f1.Height"), 10.0, 0.01);
    TS_ASSERT_DELTA(outF->getParameter("f1.Sigma"), 0.5, 0.001);
658
  }
Shahroz Ahmed's avatar
Shahroz Ahmed committed
659

660
  void setUp() override {
Shahroz Ahmed's avatar
Shahroz Ahmed committed
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
    std::string resFileName = "ResolutionTestResolution.res";
    std::ofstream fil(resFileName.c_str());

    double N = 117;
    double DX = 10;
    double X0 = -DX / 2;
    double dX = DX / (N - 1);
    double resS = acos(0.);
    double resH = 3;
    double yErr = 0;
    double y0 = 0;

    for (int i = 0; i < N; i++) {
      double x = X0 + i * dX;
      double y = resH * exp(-x * x * resS);
      double err = fabs(y - y0) / 10;
      yErr = std::max(err, yErr);
      fil << x << ' ' << y << " 0\n";
      y0 = y;
    }
681
  }
Shahroz Ahmed's avatar
Shahroz Ahmed committed
682

683
  void tearDown() override {
Shahroz Ahmed's avatar
Shahroz Ahmed committed
684
685
686
687
688
    std::string resFileName = "ResolutionTestResolution.res";
    Poco::File phandle(resFileName);
    if (phandle.exists()) {
      phandle.remove();
    }
689
  }
Shahroz Ahmed's avatar
Shahroz Ahmed committed
690

691
  void test_resolution_fit() {
692

Shahroz Ahmed's avatar
Shahroz Ahmed committed
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
    const int nX = 100;
    const int nY = nX - 1;

    MatrixWorkspace_sptr ws = boost::dynamic_pointer_cast<MatrixWorkspace>(
        WorkspaceFactory::Instance().create("Workspace2D", 1, nX, nY));

    const double dx = 10 / 99;

    Mantid::MantidVec &X = ws->dataX(0);
    Mantid::MantidVec &Y = ws->dataY(0);
    Mantid::MantidVec &E = ws->dataE(0);

    X = {0.000000, 0.101010, 0.202020, 0.303030, 0.404040, 0.505051, 0.606061,
         0.707071, 0.808081, 0.909091, 1.010101, 1.111111, 1.212121, 1.313131,
         1.414141, 1.515152, 1.616162, 1.717172, 1.818182, 1.919192, 2.020202,
         2.121212, 2.222222, 2.323232, 2.424242, 2.525253, 2.626263, 2.727273,
         2.828283, 2.929293, 3.030303, 3.131313, 3.232323, 3.333333, 3.434343,
         3.535354, 3.636364, 3.737374, 3.838384, 3.939394, 4.040404, 4.141414,
         4.242424, 4.343434, 4.444444, 4.545455, 4.646465, 4.747475, 4.848485,
         4.949495, 5.050505, 5.151515, 5.252525, 5.353535, 5.454545, 5.555556,
         5.656566, 5.757576, 5.858586, 5.959596, 6.060606, 6.161616, 6.262626,
         6.363636, 6.464646, 6.565657, 6.666667, 6.767677, 6.868687, 6.969697,
         7.070707, 7.171717, 7.272727, 7.373737, 7.474747, 7.575758, 7.676768,
         7.777778, 7.878788, 7.979798, 8.080808, 8.181818, 8.282828, 8.383838,
         8.484848, 8.585859, 8.686869, 8.787879, 8.888889, 8.989899, 9.090909,
         9.191919, 9.292929, 9.393939, 9.494949, 9.595960, 9.696970, 9.797980,
         9.898990};
    Y = {0.000000, 0.000000, 0.000000, 0.000000, 0.000001, 0.000001, 0.000002,
         0.000004, 0.000006, 0.000012, 0.000021, 0.000036, 0.000063, 0.000108,
         0.000183, 0.000305, 0.000503, 0.000818, 0.001314, 0.002084, 0.003262,
         0.005041, 0.007692, 0.011586, 0.017229, 0.025295, 0.036664, 0.052465,
         0.074121, 0.103380, 0.142353, 0.193520, 0.259728, 0.344147, 0.450195,
         0.581418, 0.741323, 0.933166, 1.159690, 1.422842, 1.723466, 2.061013,
         2.433271, 2.836167, 3.263660, 3.707743, 4.158590, 4.604836, 5.034009,
         5.433072, 5.789067, 6.089806, 6.324555, 6.484675, 6.564144, 6.559937,
         6.472215, 6.304315, 6.062539, 5.755762, 5.394893, 4.992230, 4.560768,
         4.113514, 3.662855, 3.220017, 2.794656, 2.394584, 2.025646, 1.691721,
         1.394844, 1.135414, 0.912461, 0.723946, 0.567061, 0.438516, 0.334790,
         0.252343, 0.187776, 0.137950, 0.100055, 0.071644, 0.050648, 0.035348,
         0.024356, 0.016568, 0.011127, 0.007378, 0.004829, 0.003121, 0.001991,
         0.001254, 0.000780, 0.000479, 0.000290, 0.000174, 0.000103, 0.000060,
         0.000034};
    E.assign(nY, 1.0);

    X.back() = X[98] + dx;
    AnalysisDataService::Instance().add("ResolutionTest_WS", ws);

    Algorithms::Fit fit;
    fit.initialize();

    fit.setPropertyValue(
        "Function", "composite=Convolution,"
                    "FixResolution=true,NumDeriv=true;name=Resolution,FileName="
                    "\"ResolutionTestResolution.res\","
                    "WorkspaceIndex=0;name=ResolutionTest_Gauss,c=5,h=2,s=1");
    fit.setPropertyValue("InputWorkspace", "ResolutionTest_WS");
    fit.setPropertyValue("WorkspaceIndex", "0");
    fit.execute();
751
752
  }

753
  void getStretchExpMockData(Mantid::MantidVec &y, Mantid::MantidVec &e) {
Shahroz Ahmed's avatar
Shahroz Ahmed committed
754
755
756
757
758
759
760
761
762
    // values extracted from y(x)=2*exp(-(x/4)^0.5)
    y = {2,          1.2130613,  0.98613738, 0.84124005, 0.73575888,
         0.65384379, 0.58766531, 0.53273643, 0.48623347, 0.44626032,
         0.41148132, 0.38092026, 0.35384241, 0.32968143, 0.30799199,
         0.28841799, 0.27067057, 0.25451242, 0.2397465,  0.22620756};

    std::transform(
        y.begin(), y.end(), e.begin(),
        std::bind(std::multiplies<double>(), std::placeholders::_1, 0.1));
763
  }
Shahroz Ahmed's avatar
Shahroz Ahmed committed
764

765
  void test_function_StretchExp_Against_MockData() {
Shahroz Ahmed's avatar
Shahroz Ahmed committed
766
767
768
769
770
771
772
773
774
775
776
777
778
    Algorithms::Fit alg2;
    TS_ASSERT_THROWS_NOTHING(alg2.initialize());
    TS_ASSERT(alg2.isInitialized());

    // create mock data to test against
    std::string wsName = "StretchExpMockData";
    int histogramNumber = 1;
    int timechannels = 20;
    Workspace_sptr ws = WorkspaceFactory::Instance().create(
        "Workspace2D", histogramNumber, timechannels, timechannels);
    Workspace2D_sptr ws2D = boost::dynamic_pointer_cast<Workspace2D>(ws);
    // in this case, x-values are just the running index
    auto &x = ws2D->dataX(0);
779
    for (int i = 0; i < timechannels; i++)
Shahroz Ahmed's avatar
Shahroz Ahmed committed
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
      x[i] = 1.0 * i + 0.00001;

    Mantid::MantidVec &y = ws2D->dataY(0); // y-values (counts)
    Mantid::MantidVec &e = ws2D->dataE(0); // error values of counts
    getStretchExpMockData(y, e);

    // put this workspace in the data service
    TS_ASSERT_THROWS_NOTHING(
        AnalysisDataService::Instance().addOrReplace(wsName, ws2D));

    alg2.setPropertyValue(
        "Function",
        "name=StretchExp, Height=1.5, Lifetime=5.0, Stretching=0.4");

    // Set which spectrum to fit against and initial starting values
    alg2.setPropertyValue("InputWorkspace", wsName);
    alg2.setPropertyValue("WorkspaceIndex", "0");
    alg2.setPropertyValue("StartX", "0");
    alg2.setPropertyValue("EndX", "19");

    // execute fit
    TS_ASSERT_THROWS_NOTHING(TS_ASSERT(alg2.execute()))

    TS_ASSERT(alg2.isExecuted());

    // test the output from fit is what you expect
    double dummy = alg2.getProperty("OutputChi2overDoF");
    TS_ASSERT_DELTA(dummy, 0.001, 0.001);

    IFunction_sptr out = alg2.getProperty("Function");
    // golden standard y(x)=2*exp(-(x/4)^0.5)
    // allow for a 1% error in Height and Lifetime, and 10% error in the
    // Stretching exponent
    TS_ASSERT_DELTA(out->getParameter("Height"), 2.0, 0.02);
    TS_ASSERT_DELTA(out->getParameter("Lifetime"), 4.0, 0.04);
    TS_ASSERT_DELTA(out->getParameter("Stretching"), 0.5, 0.05);

    // check its categories
    const std::vector<std::string> categories = out->categories();
    TS_ASSERT(categories.size() == 1);
    TS_ASSERT(categories[0] == "General");

    AnalysisDataService::Instance().remove(wsName);
823
  }
Shahroz Ahmed's avatar
Shahroz Ahmed committed
824

825
  void test_function_convolution_fit_resolution() {
Shahroz Ahmed's avatar
Shahroz Ahmed committed
826

Shahroz Ahmed's avatar
Shahroz Ahmed committed
827
828
    boost::shared_ptr<WorkspaceTester> data =
        boost::make_shared<WorkspaceTester>();
829
    data->initialize(1, 100, 100);
Shahroz Ahmed's avatar
Shahroz Ahmed committed
830
831
832
833
834

    auto &x = data->dataX(0);
    auto &y = data->dataY(0);
    auto &e = data->dataE(0);

835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
    y = {0.00679397551246448, 0.00684266083126313, 0.00698285916556982,
         0.00719965548825388, 0.00747519954546736, 0.00779445649068509,
         0.00814796531751759, 0.0085316132498512,  0.00894499942724,
         0.00938983058044737, 0.0098689280357672,  0.0103857911674609,
         0.0109444805899566,  0.0115496436468315,  0.0122065986210473,
         0.0129214505517302,  0.0137012349575442,  0.0145540939647495,
         0.0154894928726603,  0.0165184880798197,  0.0176540608380039,
         0.01891153608981,    0.0203091122610038,  0.021868537134057,
         0.0236159780401305,  0.0255831534292171,  0.0278088202944704,
         0.0303407524938984,  0.0332384060776671,  0.0365765613911014,
         0.0404503783689891,  0.0449825362752094,  0.0503335145708212,
         0.0567167210280417,  0.0644212970503862,  0.0738474209204705,
         0.085562497139828,   0.10039290319273,    0.119576178650528,
         0.145011665152563,   0.17965292804199,    0.228047317644744,
         0.296874083423821,   0.394987350612542,   0.532006328704948,
         0.714364415633021,   0.938739703160756,   1.18531948194073,
         1.41603503739802,    1.58257225395956,    1.64354644127685,
         1.58257225395956,    1.41603503739802,    1.18531948194073,
         0.938739703160756,   0.714364415633021,   0.532006328704948,
         0.394987350612542,   0.296874083423821,   0.228047317644743,
         0.17965292804199,    0.145011665152563,   0.119576178650528,
         0.10039290319273,    0.085562497139828,   0.0738474209204706,
         0.0644212970503863,  0.0567167210280418,  0.0503335145708214,
         0.0449825362752095,  0.0404503783689893,  0.0365765613911016,
         0.0332384060776675,  0.0303407524938988,  0.0278088202944705,
         0.0255831534292172,  0.0236159780401305,  0.0218685371340571,
         0.0203091122610038,  0.0189115360898101,  0.0176540608380039,
         0.0165184880798196,  0.0154894928726603,  0.0145540939647495,
         0.0137012349575443,  0.0129214505517302,  0.0122065986210471,
         0.0115496436468314,  0.0109444805899566,  0.0103857911674609,
         0.00986892803576708, 0.00938983058044717, 0.00894499942723977,
         0.00853161324985108, 0.0081479653175175,  0.00779445649068496,
         0.00747519954546727, 0.00719965548825406, 0.00698285916556974,
         0.00684266083126313};
Shahroz Ahmed's avatar
Shahroz Ahmed committed
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917

    x = {-10,  -9.8, -9.6, -9.4, -9.2, -9,   -8.8, -8.6, -8.4, -8.2, -8,   -7.8,
         -7.6, -7.4, -7.2, -7,   -6.8, -6.6, -6.4, -6.2, -6,   -5.8, -5.6, -5.4,
         -5.2, -5,   -4.8, -4.6, -4.4, -4.2, -4,   -3.8, -3.6, -3.4, -3.2, -3,
         -2.8, -2.6, -2.4, -2.2, -2,   -1.8, -1.6, -1.4, -1.2, -1,   -0.8, -0.6,
         -0.4, -0.2, 0,    0.2,  0.4,  0.6,  0.8,  1,    1.2,  1.4,  1.6,  1.8,
         2,    2.2,  2.4,  2.6,  2.8,  3,    3.2,  3.4,  3.6,  3.8,  4,    4.2,
         4.4,  4.6,  4.8,  5,    5.2,  5.4,  5.6,  5.8,  6,    6.2,  6.4,  6.6,
         6.8,  7,    7.2,  7.4,  7.6,  7.8,  8,    8.2,  8.4,  8.6,  8.8,  9,
         9.2,  9.4,  9.6,  9.8};

    e.assign(y.size(), 1);

    Algorithms::Fit fit;
    fit.initialize();
    // fit.setPropertyValue("Function", conv->asString());

    fit.setPropertyValue("Function",
                         "composite=Convolution,FixResolution=true,NumDeriv="
                         "true;name=ConvolutionTest_Gauss,c=0,h=0.5,s=0.5;"
                         "name=ConvolutionTest_Lorentz,c=0,h=1,w=1");
    fit.setProperty("InputWorkspace", data);
    fit.setProperty("WorkspaceIndex", 0);
    fit.execute();

    IFunction_sptr out = fit.getProperty("Function");
    // by default convolution keeps parameters of the resolution (function
    // #0)
    // fixed
    TS_ASSERT_EQUALS(out->getParameter("f0.h"), 0.5);
    TS_ASSERT_EQUALS(out->getParameter("f0.s"), 0.5);
    // fit is not very good
    TS_ASSERT_LESS_THAN(0.1, fabs(out->getParameter("f1.w") - 1));

    Algorithms::Fit fit1;
    fit1.initialize();
    fit1.setProperty("Function",
                     "composite=Convolution,FixResolution=false,NumDeriv="
                     "true;name=ConvolutionTest_Gauss,c=0,h=0.5,s=0.5;"
                     "name=ConvolutionTest_Lorentz,c=0,h=1,w=1");
    fit1.setProperty("InputWorkspace", data);
    fit1.setProperty("WorkspaceIndex", 0);
    fit1.execute();

    out = fit1.getProperty("Function");
    // resolution parameters change and close to the initial values

    TS_ASSERT_DELTA(out->getParameter("f0.s"), 2.0, 0.0001);
    TS_ASSERT_DELTA(out->getParameter("f1.w"), 0.5, 0.0001);
918
919
920
921
  }

  void test_function_crystal_field_peaks_fit() {

Duc Le's avatar
Duc Le committed
922
923
924
    // Changed output intensity from barn to mb/sr
    const double c_mbsr = 79.5774715459;

Shahroz Ahmed's avatar
Shahroz Ahmed committed
925
926
927
928
929
    auto data = TableWorkspace_sptr(new TableWorkspace);
    data->addColumn("double", "Energy");
    data->addColumn("double", "Intensity");

    TableRow row = data->appendRow();
Duc Le's avatar
Duc Le committed
930
    row << 0.0 << 2.74937 * c_mbsr;
Shahroz Ahmed's avatar
Shahroz Ahmed committed
931
    row = data->appendRow();
Duc Le's avatar
Duc Le committed
932
    row << 29.3261 << 0.7204 * c_mbsr;
Shahroz Ahmed's avatar
Shahroz Ahmed committed
933
    row = data->appendRow();
Duc Le's avatar
Duc Le committed
934
    row << 44.3412 << 0.429809 * c_mbsr;
Shahroz Ahmed's avatar
Shahroz Ahmed committed
935
936
937
938
939

    Fit fit;
    fit.initialize();
    fit.setProperty("Function",
                    "name=CrystalFieldPeaks,Ion=Ce,Symmetry=Ci,Temperature="
Duc Le's avatar
Duc Le committed
940
                    "44,ToleranceEnergy=1e-10,ToleranceIntensity=0.1,"
Shahroz Ahmed's avatar
Shahroz Ahmed committed
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
                    "BmolX=0,BmolY=0,BmolZ=0,BextX=0,BextY=0,BextZ=0,B20=0."
                    "37,B21=0,B22=3.9,B40=-0.03,B41=0,B42=-0.11,B43=0,B44=-"
                    "0.12,B60=0,B61=0,B62=0,B63=0,B64=0,B65=0,B66=0,IB21=0,"
                    "IB22=0,IB41=0,IB42=0,IB43=0,IB44=0,IB61=0,IB62=0,IB63="
                    "0,IB64=0,IB65=0,IB66=0,IntensityScaling=1");

    fit.setProperty("Ties", "BmolX=0,BmolY=0,BmolZ=0,BextX=0,BextY=0,BextZ="
                            "0,B21=0,B41=0,B43=0,B60=0,B61=0,B62=0,B63=0,"
                            "B64=0,B65=0,B66=0,IB21=0,IB22=0,IB41=0,IB42=0,"
                            "IB43=0,IB44=0,IB61=0,IB62=0,IB63=0,IB64=0,"
                            "IB65=0,IB66=0,IntensityScaling=1");

    fit.setProperty("InputWorkspace", data);
    fit.setProperty("DataColumn", "Energy");
    fit.setProperty("DataColumn_1", "Intensity");
    fit.setProperty("Output", "out");
    fit.execute();

    Mantid::API::IFunction_sptr outF = fit.getProperty("Function");

Duc Le's avatar
Duc Le committed
961
962
963
964
965
    TS_ASSERT_DELTA(outF->getParameter("B20"), 0.366336, 0.0001 * c_mbsr);
    TS_ASSERT_DELTA(outF->getParameter("B22"), 3.98132, 0.0001 * c_mbsr);
    TS_ASSERT_DELTA(outF->getParameter("B40"), -0.0304001, 0.0001 * c_mbsr);
    TS_ASSERT_DELTA(outF->getParameter("B42"), -0.119605, 0.0001 * c_mbsr);
    TS_ASSERT_DELTA(outF->getParameter("B44"), -0.130124, 0.0001 * c_mbsr);
Shahroz Ahmed's avatar
Shahroz Ahmed committed
966
967
968
969
970
971
972
973
974
975
976
977
978

    ITableWorkspace_sptr output =
        AnalysisDataService::Instance().retrieveWS<ITableWorkspace>(
            "out_Workspace");
    TS_ASSERT(output);
    if (output) {
      TS_ASSERT_EQUALS(output->rowCount(), 3);
      TS_ASSERT_EQUALS(output->columnCount(), 4);
      auto column = output->getColumn("Energy");
      TS_ASSERT_DELTA(column->toDouble(0), 0.0, 0.0001);
      TS_ASSERT_DELTA(column->toDouble(1), 29.3261, 0.0001);
      TS_ASSERT_DELTA(column->toDouble(2), 44.3412, 0.0001);
      column = output->getColumn("Intensity");
Duc Le's avatar
Duc Le committed
979
980
981
      TS_ASSERT_DELTA(column->toDouble(0), 2.74937 * c_mbsr, 0.0001 * c_mbsr);
      TS_ASSERT_DELTA(column->toDouble(1), 0.7204 * c_mbsr, 0.0001 * c_mbsr);
      TS_ASSERT_DELTA(column->toDouble(2), 0.429809 * c_mbsr, 0.0001 * c_mbsr);
Shahroz Ahmed's avatar
Shahroz Ahmed committed
982
983
984
985
986
      column = output->getColumn("Energy_calc");
      TS_ASSERT_DELTA(column->toDouble(0), 0.0, 0.0001);
      TS_ASSERT_DELTA(column->toDouble(1), 29.3261, 0.0001);
      TS_ASSERT_DELTA(column->toDouble(2), 44.3412, 0.0001);
      column = output->getColumn("Intensity_calc");
Duc Le's avatar
Duc Le committed
987
988
989
      TS_ASSERT_DELTA(column->toDouble(0), 2.74937 * c_mbsr, 0.0001 * c_mbsr);
      TS_ASSERT_DELTA(column->toDouble(1), 0.7204 * c_mbsr, 0.0001 * c_mbsr);
      TS_ASSERT_DELTA(column->toDouble(2), 0.429809 * c_mbsr, 0.0001 * c_mbsr);
Shahroz Ahmed's avatar
Shahroz Ahmed committed
990
    }
Shahroz Ahmed's avatar
Shahroz Ahmed committed
991
  }
992

Shahroz Ahmed's avatar
Shahroz Ahmed committed
993
  void test_function_exp_decay_fit() {
Shahroz Ahmed's avatar
Shahroz Ahmed committed
994
995
996
997
998
    Algorithms::Fit alg2;
    TS_ASSERT_THROWS_NOTHING(alg2.initialize());
    TS_ASSERT(alg2.isInitialized());

    // create mock data to test against
999
1000
    const std::string wsName = "ExpDecayMockData";
    const int histogramNumber = 1;
For faster browsing, not all history is shown. View entire blame