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