BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
MinimizerOptions.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Fit/Minimizer/MinimizerOptions.cpp
6 //! @brief Implements class MinimizerOptions.
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 "Fit/Tools/StringUtils.h"
17 #include <iostream>
18 #include <sstream>
19 #include <stdexcept>
20 
21 namespace
22 {
23 const std::string delimeter = ";";
24 }
25 
27 {
28  std::ostringstream result;
29  for (auto option : m_options) {
30  result << option->name() << "=" << option->value() << delimeter;
31  }
32  return result.str();
33 }
34 
35 void MinimizerOptions::setOptionString(const std::string& options)
36 {
37  // splits multiple option string "Strategy=1;Tolerance=0.01;"
38  std::vector<std::string> tokens = StringUtils::split(options, delimeter);
39  try {
40  for (std::string opt : tokens)
41  if (!opt.empty())
42  processCommand(opt);
43  } catch (std::exception& ex) {
44  std::ostringstream ostr;
45  ostr << "MinimizerOptions::setOptions() -> Error. Can't parse option string '" << options
46  << "'.\n, error message '" << ex.what() << "'";
47  throw std::runtime_error(ostr.str());
48  }
49 }
50 
51 //! Process single option string 'Tolerance=0.01' and sets the value
52 //! to corresponding MultiOption
53 
54 void MinimizerOptions::processCommand(const std::string& command)
55 {
56  std::vector<std::string> tokens = StringUtils::split(command, "=");
57  if (tokens.size() != 2)
58  throw std::runtime_error("MinimizerOptions::processOption() -> Can't parse option '"
59  + command + "'");
60 
61  std::string name = tokens[0];
62  std::string value = tokens[1];
63 
65  opt->setFromString(value);
66 }
Declares class MinimizerOptions.
Defines a few helper functions.
void setOptionString(const std::string &options)
Set options from their string representation.
std::string toOptionString() const
Returns string with all options (i.e. "Strategy=1;Tolerance=0.01;")
void processCommand(const std::string &command)
Process single option string 'Tolerance=0.01' and sets the value to corresponding MultiOption.
std::shared_ptr< MultiOption > option_t
container_t m_options
option_t option(const std::string &optionName)
std::vector< std::string > split(const std::string &text, const std::string &delimeter)
Split string into vector of string using delimeter.
Definition: StringUtils.cpp:57