BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ParameterPool.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Param/Base/ParameterPool.cpp
6 //! @brief Implements standard mix-in ParameterPool.
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/Utils/Assert.h"
17 #include "Base/Utils/StringUtils.h"
19 #include <algorithm>
20 #include <iostream>
21 #include <sstream>
22 #include <stdexcept>
23 
24 //! Constructs an empty parameter pool.
25 
27 
29 {
30  clear();
31 }
32 
33 //! Returns a literal clone.
34 
36 {
37  auto result = new ParameterPool();
38  for (auto par : m_params)
39  result->addParameter(par->clone());
40  return result;
41 }
42 
43 //! Clears the parameter map.
44 
46 {
47  for (auto* par : m_params)
48  delete par;
49  m_params.clear();
50 }
51 
52 //! Adds parameter to the pool, and returns reference to the input pointer.
53 
54 //! Returning the input pointer allows us to concatenate function calls like
55 //! pool->addParameter( new RealParameter(...) ).setLimits(-1,+1).setFixed().setUnit("nm")
56 
58 {
59  for (const auto* par : m_params)
60  if (par->getName() == newPar->getName())
61  throw std::runtime_error("ParameterPool::addParameter() -> Error. "
62  "Parameter '"
63  + newPar->getName() + "' is already registered");
64  m_params.push_back(newPar);
65  return *newPar;
66 }
67 
68 //! Copies parameters of given pool to _other_ pool, prepeding _prefix_ to the parameter names.
69 
70 void ParameterPool::copyToExternalPool(const std::string& prefix, ParameterPool* other_pool) const
71 {
72  for (const auto* par : m_params) {
73  RealParameter* new_par = par->clone(prefix + par->getName());
74  other_pool->addParameter(new_par);
75  }
76 }
77 
78 //! Returns parameter with given _name_.
79 
80 const RealParameter* ParameterPool::parameter(const std::string& name) const
81 {
82  for (const auto* par : m_params)
83  if (par->getName() == name)
84  return par;
85 
86  return nullptr;
87 }
88 
89 //! Returns parameter with given _name_.
90 
92 {
93  return const_cast<RealParameter*>(static_cast<const ParameterPool*>(this)->parameter(name));
94 }
95 
96 //! Returns nonempty vector of parameters that match the _pattern_ ('*' allowed), or throws.
97 
98 std::vector<RealParameter*> ParameterPool::getMatchedParameters(const std::string& pattern) const
99 {
100  std::vector<RealParameter*> result;
101  // loop over all parameters in the pool
102  for (auto* par : m_params)
103  if (StringUtils::matchesPattern(par->getName(), pattern))
104  result.push_back(par);
105  if (result.empty())
107  return result;
108 }
109 
110 //! Returns the one parameter that matches the _pattern_ (wildcards '*' allowed), or throws.
111 
112 RealParameter* ParameterPool::getUniqueMatch(const std::string& pattern) const
113 {
114  std::vector<RealParameter*> matches = getMatchedParameters(pattern);
115  if (matches.empty())
116  throw std::runtime_error("ParameterPool::getUniqueMatch: there is no match for '" + pattern
117  + "'");
118  if (matches.size() != 1)
119  throw std::runtime_error("ParameterPool::getUniqueMatch: pattern '" + pattern
120  + "' is not unique");
121  return matches[0];
122 }
123 
124 //! Sets parameter value.
125 
126 void ParameterPool::setParameterValue(const std::string& name, double value)
127 {
128  if (RealParameter* par = parameter(name)) {
129  try {
130  par->setValue(value);
131  } catch (const std::runtime_error& e) {
132  report_set_value_error(name, value, e.what());
133  }
134  } else {
135  std::ostringstream message;
136  message << "ParameterPool::getParameter() -> Warning. No parameter with name '" + name
137  + "'\n"
138  << "Available parameters:" << *this;
139  throw std::runtime_error(message.str());
140  }
141 }
142 
143 //! Sets value of the nonzero parameters that match _pattern_ ('*' allowed), or throws.
144 
145 int ParameterPool::setMatchedParametersValue(const std::string& pattern, double value)
146 {
147  int npars = 0;
148  for (RealParameter* par : getMatchedParameters(pattern)) {
149  try {
150  par->setValue(value);
151  npars++;
152  } catch (const std::runtime_error& e) {
153  report_set_value_error(par->getName(), value, e.what());
154  }
155  }
156  if (npars == 0)
158  return npars;
159 }
160 
161 //! Sets value of the one parameter that matches _pattern_ ('*' allowed), or throws.
162 
163 void ParameterPool::setUniqueMatchValue(const std::string& pattern, double value)
164 {
165  if (setMatchedParametersValue(pattern, value) != 1)
166  throw std::runtime_error("ParameterPool::setUniqueMatchValue: pattern '" + pattern
167  + "' is not unique");
168 }
169 
170 std::vector<std::string> ParameterPool::parameterNames() const
171 {
172  std::vector<std::string> result;
173  for (const auto* par : m_params)
174  result.push_back(par->getName());
175  return result;
176 }
177 
178 //! Removes parameter with given name from the pool.
179 
180 void ParameterPool::removeParameter(const std::string& name)
181 {
182  if (RealParameter* par = parameter(name)) {
183  m_params.erase(std::remove(m_params.begin(), m_params.end(), par), m_params.end());
184  delete par;
185  }
186 }
187 
188 const RealParameter* ParameterPool::operator[](size_t index) const
189 {
190  return m_params[check_index(index)];
191 }
192 
194 {
195  return const_cast<RealParameter*>(static_cast<const ParameterPool*>(this)->operator[](index));
196 }
197 
198 void ParameterPool::print(std::ostream& ostr) const
199 {
200  for (const auto* par : m_params)
201  ostr << "'" << par->getName() << "'"
202  << ":" << par->value() << "\n";
203 }
204 
205 //! reports error while finding parameters matching given name.
206 void ParameterPool::report_find_matched_parameters_error(const std::string& pattern) const
207 {
208  std::ostringstream ostr;
209  ostr << "ParameterPool::find_matched_parameters_error() -> Error! ";
210  ostr << "No parameters matching pattern '" << pattern
211  << "' have been found. Existing keys are:" << std::endl;
212  for (const auto* par : m_params)
213  ostr << "'" << par->getName() << "'\n";
214  throw std::runtime_error(ostr.str());
215 }
216 
217 //! Reports error while setting parname to given value.
218 void ParameterPool::report_set_value_error(const std::string& parname, double value,
219  std::string message) const
220 {
221  std::ostringstream ostr;
222  ostr << "ParameterPool::set_value_error() -> Attempt to set value " << value;
223  ostr << " for parameter '" << parname << "' failed.";
224  ostr << " Parameter limits: '" << parameter(parname)->limits() << "'.\n";
225  ostr << "Original exception message: " << message << std::endl;
226  throw std::runtime_error(ostr.str());
227 }
228 
229 size_t ParameterPool::check_index(size_t index) const
230 {
231  if (index >= m_params.size())
232  throw std::runtime_error("ParameterPool::check_index() -> Error. Index out of bounds");
233  return index;
234 }
Defines the macro ASSERT.
Defines a few helper functions.
Defines class ParameterPool.
Defines class RealParameter.
const std::string & getName() const
Definition: IParameter.h:49
Container with parameters for IParametricComponent object.
Definition: ParameterPool.h:29
std::vector< RealParameter * > m_params
Definition: ParameterPool.h:83
virtual void print(std::ostream &ostr) const
RealParameter * parameter(const std::string &name)
Returns parameter with given name.
virtual ~ParameterPool()
void report_set_value_error(const std::string &parname, double value, std::string message={}) const
Reports error while setting parname to given value.
void report_find_matched_parameters_error(const std::string &pattern) const
reports error while finding parameters matching given name.
RealParameter & addParameter(RealParameter *newPar)
Adds parameter to the pool, and returns reference to the input pointer.
void setUniqueMatchValue(const std::string &pattern, double value)
Sets value of the one parameter that matches pattern ('*' allowed), or throws.
void copyToExternalPool(const std::string &prefix, ParameterPool *other_pool) const
Copies parameters of given pool to other pool, prepeding prefix to the parameter names.
void clear()
Clears the parameter map.
std::vector< std::string > parameterNames() const
ParameterPool * clone() const
Returns a literal clone.
int setMatchedParametersValue(const std::string &wildcards, double value)
Sets value of the nonzero parameters that match pattern ('*' allowed), or throws.
RealParameter * operator[](size_t index)
ParameterPool()
Constructs an empty parameter pool.
void setParameterValue(const std::string &name, double value)
Sets parameter value.
void removeParameter(const std::string &name)
Removes parameter with given name from the pool.
std::vector< RealParameter * > getMatchedParameters(const std::string &pattern) const
Returns nonempty vector of parameters that match the pattern ('*' allowed), or throws.
size_t check_index(size_t index) const
RealParameter * getUniqueMatch(const std::string &pattern) const
Returns the one parameter that matches the pattern (wildcards '*' allowed), or throws.
Wraps a parameter of type double.
Definition: RealParameter.h:31
RealLimits limits() const
RealParameter * clone(const std::string &new_name="") const
MVVM_MODEL_EXPORT bool remove(const std::string &path)
Removes file or empty directory.
Definition: fileutils.cpp:57
QString const & name(EShape k)
Definition: particles.cpp:21
bool matchesPattern(const std::string &text, const std::string &wildcardPattern)
Returns true if text matches pattern with wildcards '*' and '?'.
Definition: StringUtils.cpp:20