BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ParameterPool Class Reference

Container with parameters for IParametricComponent object. More...

Inheritance diagram for ParameterPool:
[legend]
Collaboration diagram for ParameterPool:
[legend]

Public Member Functions

 ParameterPool ()
 Constructs an empty parameter pool. More...
 
virtual ~ParameterPool ()
 
RealParameteraddParameter (RealParameter *newPar)
 Adds parameter to the pool, and returns reference to the input pointer. More...
 
void clear ()
 Clears the parameter map. More...
 
ParameterPoolclone () const
 Returns a literal clone. More...
 
void copyToExternalPool (const std::string &prefix, ParameterPool *other_pool) const
 Copies parameters of given pool to other pool, prepeding prefix to the parameter names. More...
 
bool empty () const
 
std::vector< RealParameter * > getMatchedParameters (const std::string &pattern) const
 Returns nonempty vector of parameters that match the pattern ('*' allowed), or throws. More...
 
RealParametergetUniqueMatch (const std::string &pattern) const
 Returns the one parameter that matches the pattern (wildcards '*' allowed), or throws. More...
 
RealParameteroperator[] (size_t index)
 
const RealParameteroperator[] (size_t index) const
 
RealParameterparameter (const std::string &name)
 Returns parameter with given name. More...
 
const RealParameterparameter (const std::string &name) const
 Returns parameter with given name. More...
 
std::vector< std::string > parameterNames () const
 
const std::vector< RealParameter * > parameters () const
 Returns full vector of parameters. More...
 
void removeParameter (const std::string &name)
 Removes parameter with given name from the pool. More...
 
int setMatchedParametersValue (const std::string &wildcards, double value)
 Sets value of the nonzero parameters that match pattern ('*' allowed), or throws. More...
 
void setParameterValue (const std::string &name, double value)
 Sets parameter value. More...
 
void setUniqueMatchValue (const std::string &pattern, double value)
 Sets value of the one parameter that matches pattern ('*' allowed), or throws. More...
 
size_t size () const
 Returns number of parameters in the pool. More...
 
virtual void transferToCPP ()
 Used for Python overriding of clone (see swig/tweaks.py) More...
 

Private Member Functions

size_t check_index (size_t index) const
 
virtual void print (std::ostream &ostr) const
 
void report_find_matched_parameters_error (const std::string &pattern) const
 reports error while finding parameters matching given name. More...
 
void report_set_value_error (const std::string &parname, double value, std::string message={}) const
 Reports error while setting parname to given value. More...
 

Private Attributes

std::vector< RealParameter * > m_params
 

Friends

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

Detailed Description

Container with parameters for IParametricComponent 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 28 of file ParameterPool.cpp.

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

References clear().

Here is the call graph for this function:

Member Function Documentation

◆ 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 57 of file ParameterPool.cpp.

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 }
const std::string & getName() const
Definition: IParameter.h:49
std::vector< RealParameter * > m_params
Definition: ParameterPool.h:83

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

Referenced by copyToExternalPool().

Here is the call graph for this function:

◆ check_index()

size_t ParameterPool::check_index ( size_t  index) const
private

Definition at line 229 of file ParameterPool.cpp.

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

References m_params.

Referenced by operator[]().

◆ clear()

void ParameterPool::clear ( )

Clears the parameter map.

Definition at line 45 of file ParameterPool.cpp.

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

References m_params.

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

◆ clone()

ParameterPool * ParameterPool::clone ( ) const
virtual

Returns a literal clone.

Implements ICloneable.

Definition at line 35 of file ParameterPool.cpp.

36 {
37  auto result = new ParameterPool();
38  for (auto par : m_params)
39  result->addParameter(par->clone());
40  return result;
41 }
ParameterPool()
Constructs an empty parameter pool.

References ParameterPool(), and m_params.

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 70 of file ParameterPool.cpp.

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 }
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:31
RealParameter * clone(const std::string &new_name="") const

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

Here is the call graph for this function:

◆ empty()

bool ParameterPool::empty ( ) const
inline

Definition at line 42 of file ParameterPool.h.

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

References size().

Referenced by IParametricComponent::IParametricComponent().

Here is the call graph for this function:

◆ 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 98 of file ParameterPool.cpp.

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 }
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 112 of file ParameterPool.cpp.

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 }
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 ParticleDistribution::generateParticles(), and ParameterUtils::poolParameterUnits().

Here is the call graph for this function:

◆ operator[]() [1/2]

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

Definition at line 193 of file ParameterPool.cpp.

194 {
195  return const_cast<RealParameter*>(static_cast<const ParameterPool*>(this)->operator[](index));
196 }
Container with parameters for IParametricComponent object.
Definition: ParameterPool.h:29

◆ operator[]() [2/2]

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

Definition at line 188 of file ParameterPool.cpp.

189 {
190  return m_params[check_index(index)];
191 }
size_t check_index(size_t index) const

References check_index(), and m_params.

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 91 of file ParameterPool.cpp.

92 {
93  return const_cast<RealParameter*>(static_cast<const ParameterPool*>(this)->parameter(name));
94 }
RealParameter * parameter(const std::string &name)
Returns parameter with given name.
QString const & name(EShape k)
Definition: particles.cpp:21

References RealSpace::Particles::name().

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

Here is the call graph for this function:

◆ parameter() [2/2]

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

Returns parameter with given name.

Definition at line 80 of file ParameterPool.cpp.

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

References m_params, and RealSpace::Particles::name().

Here is the call graph for this function:

◆ parameterNames()

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

Definition at line 170 of file ParameterPool.cpp.

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

References m_params.

◆ parameters()

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

Returns full vector of parameters.

Definition at line 51 of file ParameterPool.h.

51 { return m_params; }

References m_params.

Referenced by pyfmt2::argumentList().

◆ print()

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

Definition at line 198 of file ParameterPool.cpp.

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

References m_params.

◆ removeParameter()

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

Removes parameter with given name from the pool.

Definition at line 180 of file ParameterPool.cpp.

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 }
MVVM_MODEL_EXPORT bool remove(const std::string &path)
Removes file or empty directory.
Definition: fileutils.cpp:57

References m_params, RealSpace::Particles::name(), parameter(), and ModelView::Utils::remove().

Here is the call graph for this function:

◆ 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 206 of file ParameterPool.cpp.

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 }

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 218 of file ParameterPool.cpp.

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

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

Referenced by setMatchedParametersValue(), and setParameterValue().

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 145 of file ParameterPool.cpp.

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

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

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

Here is the call graph for this function:

◆ setParameterValue()

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

Sets parameter value.

Definition at line 126 of file ParameterPool.cpp.

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 }

References RealSpace::Particles::name(), parameter(), and report_set_value_error().

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 163 of file ParameterPool.cpp.

164 {
165  if (setMatchedParametersValue(pattern, value) != 1)
166  throw std::runtime_error("ParameterPool::setUniqueMatchValue: pattern '" + pattern
167  + "' is not unique");
168 }
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:

◆ size()

size_t ParameterPool::size ( ) const
inline

Returns number of parameters in the pool.

Definition at line 41 of file ParameterPool.h.

41 { return m_params.size(); }

References m_params.

Referenced by empty().

◆ 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 63 of file ParameterPool.h.

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

Member Data Documentation

◆ m_params


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