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

Public Member Functions

 ParameterPool ()
 
virtual ~ParameterPool ()
 
ParameterPoolclone () const
 
void copyToExternalPool (const std::string &prefix, ParameterPool *other_pool) const
 
void clear ()
 
size_t size () const
 
bool empty () const
 
RealParameteraddParameter (RealParameter *newPar)
 
RealParameterparameter (const std::string &name)
 
const RealParameterparameter (const std::string &name) const
 
const std::vector< RealParameter * > parameters () const
 
std::vector< RealParameter * > getMatchedParameters (const std::string &pattern) const
 
RealParametergetUniqueMatch (const std::string &pattern) const
 
void setParameterValue (const std::string &name, double value)
 
int setMatchedParametersValue (const std::string &wildcards, double value)
 
void setUniqueMatchValue (const std::string &pattern, double value)
 
std::vector< std::string > parameterNames () const
 
void removeParameter (const std::string &name)
 
const RealParameteroperator[] (size_t index) const
 
RealParameteroperator[] (size_t index)
 
virtual void transferToCPP ()
 

Private Member Functions

virtual void print (std::ostream &ostr) const
 
void report_find_matched_parameters_error (const std::string &pattern) const
 
void report_set_value_error (const std::string &parname, double value, std::string message={}) const
 
size_t check_index (size_t index) const
 

Private Attributes

std::vector< RealParameter * > m_params
 

Friends

std::ostream & operator<< (std::ostream &ostr, const ParameterPool &obj)
 

Detailed Description

Container with parameters for IParameterized object.

Definition at line 29 of file ParameterPool.h.

Constructor & Destructor Documentation

◆ ParameterPool()

ParameterPool::ParameterPool ( )
default

Constructs an empty parameter pool.

Referenced by clone().

◆ ~ParameterPool()

ParameterPool::~ParameterPool ( )
virtual

Definition at line 29 of file ParameterPool.cpp.

30 {
31  clear();
32 }
void clear()
Clears the parameter map.

References clear().

Here is the call graph for this function:

Member Function Documentation

◆ clone()

ParameterPool * ParameterPool::clone ( ) const
virtual

Returns a literal clone.

Implements ICloneable.

Definition at line 36 of file ParameterPool.cpp.

37 {
38  auto result = new ParameterPool();
39  for (auto par : m_params)
40  result->addParameter(par->clone());
41  return result;
42 }
std::vector< RealParameter * > m_params
Definition: ParameterPool.h:84
ParameterPool()
Constructs an empty parameter pool.

References m_params, and ParameterPool().

Here is the call graph for this function:

◆ copyToExternalPool()

void ParameterPool::copyToExternalPool ( const std::string &  prefix,
ParameterPool other_pool 
) const

Copies parameters of given pool to other pool, prepeding prefix to the parameter names.

Definition at line 72 of file ParameterPool.cpp.

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 }
RealParameter & addParameter(RealParameter *newPar)
Adds parameter to the pool, and returns reference to the input pointer.
Wraps a parameter of type double.
Definition: RealParameter.h:32
RealParameter * clone(const std::string &new_name="") const

References addParameter(), RealParameter::clone(), and m_params.

Referenced by INode::createParameterTree().

Here is the call graph for this function:

◆ clear()

void ParameterPool::clear ( )

Clears the parameter map.

Definition at line 46 of file ParameterPool.cpp.

47 {
48  for (auto* par : m_params)
49  delete par;
50  m_params.clear();
51 }

References m_params.

Referenced by SampleBuilderNode::borrow_builder_parameters(), INode::INode(), SampleBuilderNode::reset(), and ~ParameterPool().

◆ size()

size_t ParameterPool::size ( ) const
inline

Returns number of parameters in the pool.

Definition at line 42 of file ParameterPool.h.

42 { return m_params.size(); }

References m_params.

Referenced by empty().

◆ empty()

bool ParameterPool::empty ( ) const
inline

Definition at line 43 of file ParameterPool.h.

43 { return size() == 0; }
size_t size() const
Returns number of parameters in the pool.
Definition: ParameterPool.h:42

References size().

Referenced by IParameterized::IParameterized().

Here is the call graph for this function:

◆ addParameter()

RealParameter & ParameterPool::addParameter ( RealParameter newPar)

Adds parameter to the pool, and returns reference to the input pointer.

Returning the input pointer allows us to concatenate function calls like pool->addParameter( new RealParameter(...) ).setLimits(-1,+1).setFixed().setUnit("nm")

Definition at line 58 of file ParameterPool.cpp.

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 }
const std::string & getName() const
Definition: IParameter.h:49

References IParameter< T >::getName(), and m_params.

Referenced by copyToExternalPool().

Here is the call graph for this function:

◆ parameter() [1/2]

RealParameter * ParameterPool::parameter ( const std::string &  name)

Returns parameter with given name.

Definition at line 93 of file ParameterPool.cpp.

94 {
95  return const_cast<RealParameter*>(static_cast<const ParameterPool*>(this)->parameter(name));
96 }
Container with parameters for IParameterized object.
Definition: ParameterPool.h:30
RealParameter * parameter(const std::string &name)
Returns parameter with given name.

Referenced by removeParameter(), report_set_value_error(), and setParameterValue().

◆ parameter() [2/2]

const RealParameter * ParameterPool::parameter ( const std::string &  name) const

Returns parameter with given name.

Definition at line 82 of file ParameterPool.cpp.

83 {
84  for (const auto* par : m_params)
85  if (par->getName() == name)
86  return par;
87 
88  return nullptr;
89 }

References m_params.

◆ parameters()

const std::vector<RealParameter*> ParameterPool::parameters ( ) const
inline

Returns full vector of parameters.

Definition at line 52 of file ParameterPool.h.

52 { return m_params; }

References m_params.

Referenced by pyfmt2::argumentList(), and anonymous_namespace{NodeUtils.cpp}::poolToString().

◆ getMatchedParameters()

std::vector< RealParameter * > ParameterPool::getMatchedParameters ( const std::string &  pattern) const

Returns nonempty vector of parameters that match the pattern ('*' allowed), or throws.

Definition at line 100 of file ParameterPool.cpp.

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 }
void report_find_matched_parameters_error(const std::string &pattern) const
reports error while finding parameters matching given name.
bool matchesPattern(const std::string &text, const std::string &wildcardPattern)
Returns true if text matches pattern with wildcards '*' and '?'.
Definition: StringUtils.cpp:20

References m_params, StringUtils::matchesPattern(), and report_find_matched_parameters_error().

Referenced by getUniqueMatch(), and setMatchedParametersValue().

Here is the call graph for this function:

◆ getUniqueMatch()

RealParameter * ParameterPool::getUniqueMatch ( const std::string &  pattern) const

Returns the one parameter that matches the pattern (wildcards '*' allowed), or throws.

Definition at line 114 of file ParameterPool.cpp.

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 }
std::vector< RealParameter * > getMatchedParameters(const std::string &pattern) const
Returns nonempty vector of parameters that match the pattern ('*' allowed), or throws.

References getMatchedParameters().

Referenced by ParameterUtils::poolParameterUnits().

Here is the call graph for this function:

◆ setParameterValue()

void ParameterPool::setParameterValue ( const std::string &  name,
double  value 
)

Sets parameter value.

Definition at line 128 of file ParameterPool.cpp.

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 }
void report_set_value_error(const std::string &parname, double value, std::string message={}) const
Reports error while setting parname to given value.

References parameter(), and report_set_value_error().

Here is the call graph for this function:

◆ setMatchedParametersValue()

int ParameterPool::setMatchedParametersValue ( const std::string &  wildcards,
double  value 
)

Sets value of the nonzero parameters that match pattern ('*' allowed), or throws.

Definition at line 147 of file ParameterPool.cpp.

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 }

References getMatchedParameters(), report_find_matched_parameters_error(), and report_set_value_error().

Referenced by DistributionHandler::setParameterToMeans(), IParameterized::setParameterValue(), DistributionHandler::setParameterValues(), and setUniqueMatchValue().

Here is the call graph for this function:

◆ setUniqueMatchValue()

void ParameterPool::setUniqueMatchValue ( const std::string &  pattern,
double  value 
)

Sets value of the one parameter that matches pattern ('*' allowed), or throws.

Definition at line 165 of file ParameterPool.cpp.

166 {
167  if (setMatchedParametersValue(pattern, value) != 1)
168  throw Exceptions::RuntimeErrorException("ParameterPool::setUniqueMatchValue: pattern '"
169  + pattern + "' is not unique");
170 }
int setMatchedParametersValue(const std::string &wildcards, double value)
Sets value of the nonzero parameters that match pattern ('*' allowed), or throws.

References setMatchedParametersValue().

Referenced by ParticleDistribution::generateParticles().

Here is the call graph for this function:

◆ parameterNames()

std::vector< std::string > ParameterPool::parameterNames ( ) const

Definition at line 172 of file ParameterPool.cpp.

173 {
174  std::vector<std::string> result;
175  for (const auto* par : m_params)
176  result.push_back(par->getName());
177  return result;
178 }

References m_params.

◆ removeParameter()

void ParameterPool::removeParameter ( const std::string &  name)

Removes parameter with given name from the pool.

Definition at line 182 of file ParameterPool.cpp.

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 }

References m_params, and parameter().

Here is the call graph for this function:

◆ operator[]() [1/2]

const RealParameter * ParameterPool::operator[] ( size_t  index) const

Definition at line 190 of file ParameterPool.cpp.

191 {
192  return m_params[check_index(index)];
193 }
size_t check_index(size_t index) const

References check_index(), and m_params.

Here is the call graph for this function:

◆ operator[]() [2/2]

RealParameter * ParameterPool::operator[] ( size_t  index)

Definition at line 195 of file ParameterPool.cpp.

196 {
197  return const_cast<RealParameter*>(static_cast<const ParameterPool*>(this)->operator[](index));
198 }

◆ print()

void ParameterPool::print ( std::ostream &  ostr) const
privatevirtual

Definition at line 200 of file ParameterPool.cpp.

201 {
202  for (const auto* par : m_params)
203  ostr << "'" << par->getName() << "'"
204  << ":" << par->value() << "\n";
205 }

References m_params.

◆ report_find_matched_parameters_error()

void ParameterPool::report_find_matched_parameters_error ( const std::string &  pattern) const
private

reports error while finding parameters matching given name.

Definition at line 208 of file ParameterPool.cpp.

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 }

References m_params.

Referenced by getMatchedParameters(), and setMatchedParametersValue().

◆ report_set_value_error()

void ParameterPool::report_set_value_error ( const std::string &  parname,
double  value,
std::string  message = {} 
) const
private

Reports error while setting parname to given value.

Definition at line 220 of file ParameterPool.cpp.

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 }
RealLimits limits() const

References RealParameter::limits(), and parameter().

Referenced by setMatchedParametersValue(), and setParameterValue().

Here is the call graph for this function:

◆ check_index()

size_t ParameterPool::check_index ( size_t  index) const
private

Definition at line 231 of file ParameterPool.cpp.

232 {
233  if (index >= m_params.size())
234  throw std::runtime_error("ParameterPool::check_index() -> Error. Index out of bounds");
235  return index;
236 }

References m_params.

Referenced by operator[]().

◆ transferToCPP()

virtual void ICloneable::transferToCPP ( )
inlinevirtualinherited

Used for Python overriding of clone (see swig/tweaks.py)

Definition at line 34 of file ICloneable.h.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  ostr,
const ParameterPool obj 
)
friend

Definition at line 64 of file ParameterPool.h.

65  {
66  obj.print(ostr);
67  return ostr;
68  }
virtual void print(std::ostream &ostr) const

Member Data Documentation

◆ m_params


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