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