BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
FitPrintService.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Core/Fitting/FitPrintService.cpp
6 //! @brief Defines class FitPrintService.
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 "Base/Utils/StringUtils.h"
18 #include <iomanip>
19 #include <iostream>
20 #include <sstream>
21 
22 namespace {
23 
24 size_t length_of_longest_name(const mumufit::Parameters& params)
25 {
26  size_t result(0);
27  for (const auto& par : params) {
28  if (par.name().size() > result)
29  result = par.name().size();
30  }
31  return result;
32 }
33 
34 } // namespace
35 
37 
38 void FitPrintService::print(const FitObjective& objective)
39 {
40  std::ostringstream ostr;
41 
42  if (objective.isFirstIteration()) {
43  m_run_time.start();
45  }
46 
47  ostr << iterationHeaderString(objective);
48  ostr << wallTimeString();
49  ostr << parameterString(objective);
50 
51  if (objective.isCompleted())
52  ostr << fitResultString(objective);
53 
54  std::cout << ostr.str() << "\n";
55 }
56 
58 {
59  std::ostringstream result;
60 
61  result << "FitPrintService::update() -> Info."
62  << " NCall:" << objective.iterationInfo().iterationCount() << " Chi2:" << std::scientific
63  << std::setprecision(8) << objective.iterationInfo().chi2() << "\n";
64 
65  return result.str();
66 }
67 
69 {
70  std::ostringstream result;
71 
73  result << "Wall time since last call:" << std::fixed << std::setprecision(2)
74  << m_last_call_time.runTime() << "\n";
76 
77  return result.str();
78 }
79 
80 std::string FitPrintService::parameterString(const FitObjective& objective)
81 {
82  std::ostringstream result;
83 
84  const auto params = objective.iterationInfo().parameters();
85  const auto length = length_of_longest_name(params);
86 
87  for (const auto& par : params) {
88  result << StringUtils::padRight(par.name(), length) << std::scientific
89  << std::setprecision(6) << " " << par.startValue() << " " << par.limits().toString()
90  << " " << par.value() << "\n";
91  }
92 
93  return result.str();
94 }
95 
96 std::string FitPrintService::fitResultString(const FitObjective& objective)
97 {
98  std::ostringstream result;
99 
100  m_run_time.stop();
101 
102  result << "This was the last iteration." << std::endl;
103  result << "Total time spend: " << std::fixed << std::setprecision(2) << m_run_time.runTime()
104  << " sec."
105  << "\n\n";
106 
107  result << objective.minimizerResult().toString();
108  return result.str();
109 }
Defines a few helper functions.
Defines class FitObjective.
Defines class FitPrintService.
Holds vector of SimDataPairs (experimental data and simulation results) for use in fitting.
Definition: FitObjective.h:33
bool isFirstIteration() const
IterationInfo iterationInfo() const
mumufit::MinimizerResult minimizerResult() const
bool isCompleted() const
void print(const FitObjective &objective)
WallclockTimer m_last_call_time
std::string parameterString(const FitObjective &objective)
std::string fitResultString(const FitObjective &objective)
std::string iterationHeaderString(const FitObjective &objective)
std::string wallTimeString()
WallclockTimer m_run_time
unsigned iterationCount() const
Returns current number of minimizer iterations.
mumufit::Parameters parameters() const
double chi2() const
double runTime() const
returns run time in sec.
std::string toString() const
Returns multi-line string representing minimization results.
A collection of fit parameters.
Definition: Parameters.h:26
std::string scientific(const T value, int n=10)
Returns scientific string representing given value of any numeric type.
Definition: StringUtils.h:61
std::string padRight(const std::string &name, size_t length)
Returns string right-padded with blanks.
Definition: StringUtils.cpp:49