BornAgain  1.19.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 reflection and scattering
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 const std::string delimeter = ";";
23 }
24 
26 {
27  std::ostringstream result;
28  for (auto option : m_options) {
29  result << option->name() << "=" << option->value() << delimeter;
30  }
31  return result.str();
32 }
33 
34 void MinimizerOptions::setOptionString(const std::string& options)
35 {
36  // splits multiple option string "Strategy=1;Tolerance=0.01;"
37  std::vector<std::string> tokens = mumufit::stringUtils::split(options, delimeter);
38  try {
39  for (std::string opt : tokens)
40  if (!opt.empty())
41  processCommand(opt);
42  } catch (std::exception& ex) {
43  std::ostringstream ostr;
44  ostr << "MinimizerOptions::setOptions() -> Error. Can't parse option string '" << options
45  << "'.\n, error message '" << ex.what() << "'";
46  throw std::runtime_error(ostr.str());
47  }
48 }
49 
50 //! Process single option string 'Tolerance=0.01' and sets the value
51 //! to corresponding MultiOption
52 
53 void MinimizerOptions::processCommand(const std::string& command)
54 {
55  std::vector<std::string> tokens = mumufit::stringUtils::split(command, "=");
56  if (tokens.size() != 2)
57  throw std::runtime_error("MinimizerOptions::processOption() -> Can't parse option '"
58  + command + "'");
59 
60  std::string name = tokens[0];
61  std::string value = tokens[1];
62 
64  opt->setFromString(value);
65 }
Defines a few helper functions.
Declares class MinimizerOptions.
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)
QString const & name(EShape k)
Definition: particles.cpp:21
std::vector< std::string > split(const std::string &text, const std::string &delimeter)
Split string into vector of string using delimeter.
Definition: StringUtils.cpp:51