BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
MinimizerTestPlan.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Fit/TestEngine/MinimizerTestPlan.cpp
6 //! @brief Implements class MinimizerTestPlan
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
16 #include "Fit/Kernel/Parameters.h"
17 #include "Fit/TestEngine/Numeric.h"
18 #include <cmath>
19 #include <iostream>
20 #include <sstream>
21 
22 using namespace Fit;
23 
24 MinimizerTestPlan::MinimizerTestPlan(const std::string& name) : m_name(name) {}
25 
27 
28 void MinimizerTestPlan::addParameter(const Parameter& param, double expected_value,
29  double tolerance)
30 {
31  m_parameter_plan.push_back(ParameterPlan(param, expected_value, tolerance));
32 }
33 
34 //! Returns fit parameters which will be used as initial one for the minimization.
35 
37 {
38  Parameters result;
39  for (const auto& plan : m_parameter_plan)
40  result.add(plan.fitParameter());
41 
42  return result;
43 }
44 
45 //! Return vector of expected parameter values.
46 
47 std::vector<double> MinimizerTestPlan::expectedValues() const
48 {
49  std::vector<double> result;
50  for (const auto& plan : m_parameter_plan)
51  result.push_back(plan.expectedValue());
52 
53  return result;
54 }
55 
56 //! Returns true if given values coincide with expected fit parameter values.
57 
58 bool MinimizerTestPlan::valuesAsExpected(const std::vector<double>& values) const
59 {
60  bool success(true);
61 
62  if (m_parameter_plan.size() != values.size())
63  throw std::runtime_error("FunctionTestPlan::valuesAsExpected() -> Error. Sizes differ.");
64 
65  size_t index(0);
66  std::ostringstream text;
67  for (const auto& plan : m_parameter_plan) {
68  double diff = Numeric::GetRelativeDifference(values[index], plan.expectedValue());
69 
70  bool diff_ok(true);
71  if (diff > plan.tolerance())
72  diff_ok = false;
73 
74  text << plan.fitParameter().name() << " found:" << values[index]
75  << " expected:" << plan.expectedValue() << " diff:" << diff << " "
76  << (diff_ok ? "OK" : "FAILED") << "\n";
77 
78  success &= diff_ok;
79 
80  ++index;
81  }
82 
83  std::cout << text.str();
84 
85  return success;
86 }
Defines class MinimizerTestPlan.
Defines constants and "almost equal" in namespace Numeric.
Defines class Parameters.
A fittable parameter with value, error, step, and limits.
Definition: Parameter.h:28
A collection of fit parameters.
Definition: Parameters.h:28
void add(const Parameter &par)
Definition: Parameters.cpp:22
std::vector< double > expectedValues() const
Return vector of expected parameter values.
void addParameter(const Fit::Parameter &param, double expected_value, double tolerance=0.01)
MinimizerTestPlan(const std::string &name)
bool valuesAsExpected(const std::vector< double > &values) const
Returns true if given values coincide with expected fit parameter values.
Fit::Parameters parameters() const
Returns fit parameters which will be used as initial one for the minimization.
virtual ~MinimizerTestPlan()
std::vector< ParameterPlan > m_parameter_plan
Defines initial settings of single fit parameter and the final value which has to be found in the cou...
Definition: ParameterPlan.h:24
Objective function types.
double GetRelativeDifference(double a, double b)
Returns the safe relative difference, which is 2(|a-b|)/(|a|+|b|) except in special cases.
Definition: Numeric.cpp:32