BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
MinimizerUtils.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Fit/Tools/MinimizerUtils.cpp
6 //! @brief Declares namespace MinimizerUtils.
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 <algorithm>
17 #include <cmath>
18 #include <limits>
19 #include <sstream>
20 
21 std::string MinimizerUtils::toString(const std::vector<std::string>& v, const std::string& delim)
22 {
23  std::stringstream s;
24  std::for_each(v.begin(), v.end(),
25  [&s, &delim](const std::string& elem) { s << elem << delim; });
26  return s.str();
27 }
28 
29 //! Returns translation of GSL error code to string.
30 
31 std::map<int, std::string> MinimizerUtils::gslErrorDescriptionMap()
32 {
33  std::map<int, std::string> result;
34 
35  result[0] = "OK, valid minimum";
36  result[-2] = "iteration has not converged";
37  result[1] = "input domain error, e.g sqrt(-1)";
38  result[2] = "output range error, e.g. exp(1e100)";
39  result[3] = "invalid pointer";
40  result[4] = "invalid argument supplied by user";
41  result[5] = "generic failure";
42  result[6] = "factorization failed";
43  result[7] = "sanity check failed - shouldn't happen";
44  result[8] = "malloc failed";
45  result[9] = "problem with user-supplied function";
46  result[10] = "iterative process is out of control";
47  result[11] = "exceeded max number of iterations";
48  result[12] = "tried to divide by zero";
49  result[13] = "user specified an invalid tolerance";
50  result[14] = "failed to reach the specified tolerance";
51  result[15] = "underflow";
52  result[16] = "overflow ";
53  result[17] = "loss of accuracy";
54  result[18] = "failed because of roundoff error";
55  result[19] = "matrix, vector lengths are not conformant";
56  result[20] = "matrix not square";
57  result[21] = "apparent singularity detected";
58  result[22] = "integral or series is divergent";
59  result[23] = "requested feature is not supported by the hardware";
60  result[24] = "requested feature not (yet) implemented";
61  result[25] = "cache limit exceeded";
62  result[26] = "table limit exceeded";
63  result[27] = "iteration is not making progress towards solution";
64  result[28] = "jacobian evaluations are not improving the solution";
65  result[29] = "cannot reach the specified tolerance in F";
66  result[30] = "cannot reach the specified tolerance in X";
67  result[31] = "cannot reach the specified tolerance in gradient";
68 
69  return result;
70 }
71 
72 std::string MinimizerUtils::gslErrorDescription(int errorCode)
73 {
74  static std::map<int, std::string> errorDescription = gslErrorDescriptionMap();
75 
76  auto it = errorDescription.find(errorCode);
77  if (it != errorDescription.end())
78  return it->second;
79 
80  return "Unknown error";
81 }
82 
83 bool MinimizerUtils::numbersDiffer(double a, double b, double tol)
84 {
85  constexpr double eps = std::numeric_limits<double>::epsilon();
86  if (tol < 1)
87  throw std::runtime_error("MinimizerUtils::numbersDiffer() -> Error.Not intended for tol<1");
88  return std::abs(a - b) > eps * std::max(tol * eps, std::abs(b));
89 }
90 
91 //! Returns horizontal line of 80 characters length with section name in it.
92 
93 std::string MinimizerUtils::sectionString(const std::string& sectionName, size_t report_width)
94 {
95  if (sectionName.empty())
96  return std::string(report_width, '-') + "\n";
97  // to make "--- SectionName ------------------------------"
98  std::string prefix(3, '-');
99  std::string body = std::string(" ") + sectionName + " ";
100  std::string postfix(report_width - body.size() - prefix.size(), '-');
101  std::ostringstream result;
102  result << prefix << body << postfix << std::endl;
103  return result.str();
104 }
Declares namespace MinimizerUtils.
std::string toString(const std::vector< std::string > &v, const std::string &delim="")
std::string sectionString(const std::string &sectionName="", size_t report_width=80)
Returns horizontal line of 80 characters length with section name in it.
bool numbersDiffer(double a, double b, double tol)
std::map< int, std::string > gslErrorDescriptionMap()
Returns translation of GSL error code to string.
std::string gslErrorDescription(int errorCode)