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