BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
FitPrintService Class Reference
Collaboration diagram for FitPrintService:

Public Member Functions

 FitPrintService ()
 
void print (const FitObjective &objective)
 

Private Member Functions

std::string iterationHeaderString (const FitObjective &objective)
 
std::string wallTimeString ()
 
std::string parameterString (const FitObjective &objective)
 
std::string fitResultString (const FitObjective &objective)
 

Private Attributes

WallclockTimer m_run_time
 
WallclockTimer m_last_call_time
 

Detailed Description

Prints fit statistics to standard output during minimizer iterations.

Definition at line 25 of file FitPrintService.h.

Constructor & Destructor Documentation

◆ FitPrintService()

FitPrintService::FitPrintService ( )
default

Member Function Documentation

◆ print()

void FitPrintService::print ( const FitObjective objective)

Definition at line 39 of file FitPrintService.cpp.

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 }
bool isFirstIteration() const
bool isCompleted() const
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

References fitResultString(), FitObjective::isCompleted(), FitObjective::isFirstIteration(), iterationHeaderString(), m_last_call_time, m_run_time, parameterString(), WallclockTimer::start(), and wallTimeString().

Here is the call graph for this function:

◆ iterationHeaderString()

std::string FitPrintService::iterationHeaderString ( const FitObjective objective)
private

Definition at line 58 of file FitPrintService.cpp.

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 }
IterationInfo iterationInfo() const
unsigned iterationCount() const
Returns current number of minimizer iterations.
double chi2() const
std::string scientific(const T value, int n=10)
Returns scientific string representing given value of any numeric type.
Definition: StringUtils.h:54

References IterationInfo::chi2(), IterationInfo::iterationCount(), FitObjective::iterationInfo(), and StringUtils::scientific().

Referenced by print().

Here is the call graph for this function:

◆ wallTimeString()

std::string FitPrintService::wallTimeString ( )
private

Definition at line 69 of file FitPrintService.cpp.

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 }
double runTime() const
returns run time in sec.

References m_last_call_time, WallclockTimer::runTime(), WallclockTimer::start(), and WallclockTimer::stop().

Referenced by print().

Here is the call graph for this function:

◆ parameterString()

std::string FitPrintService::parameterString ( const FitObjective objective)
private

Definition at line 81 of file FitPrintService.cpp.

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 }
Fit::Parameters parameters() const
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)

References FitObjective::iterationInfo(), anonymous_namespace{BoxCompositionBuilder.cpp}::length, anonymous_namespace{FitPrintService.cpp}::length_of_longest_name(), StringUtils::padRight(), IterationInfo::parameters(), and StringUtils::scientific().

Referenced by print().

Here is the call graph for this function:

◆ fitResultString()

std::string FitPrintService::fitResultString ( const FitObjective objective)
private

Definition at line 97 of file FitPrintService.cpp.

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 }
Fit::MinimizerResult minimizerResult() const
std::string toString() const
Returns multi-line string representing minimization results.

References m_run_time, FitObjective::minimizerResult(), WallclockTimer::runTime(), WallclockTimer::stop(), and Fit::MinimizerResult::toString().

Referenced by print().

Here is the call graph for this function:

Member Data Documentation

◆ m_run_time

WallclockTimer FitPrintService::m_run_time
private

Definition at line 38 of file FitPrintService.h.

Referenced by fitResultString(), and print().

◆ m_last_call_time

WallclockTimer FitPrintService::m_last_call_time
private

Definition at line 39 of file FitPrintService.h.

Referenced by print(), and wallTimeString().


The documentation for this class was generated from the following files: