GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GModelPar.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GModelPar.cpp - Model parameter class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2009-2020 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GModelPar.cpp
23  * @brief GModelPar class implementation.
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GException.hpp"
32 #include "GModelPar.hpp"
33 #include "GTools.hpp"
34 
35 /* __ Method name definitions ____________________________________________ */
36 #define G_CONSTRUCT "GModelPar::GModelPar(std::string&, double&, double&)"
37 #define G_FACTOR_VALUE "GModelPar::factor_value(double&)"
38 #define G_FACTOR_MIN "GModelPar::factor_min(double&)"
39 #define G_FACTOR_MAX "GModelPar::factor_max(double&)"
40 #define G_SCALE "GModelPar::scale(double&)"
41 #define G_READ "GModelPar::read(GXmlElement&)"
42 
43 /* __ Macros _____________________________________________________________ */
44 
45 /* __ Coding definitions _________________________________________________ */
46 
47 /* __ Debug definitions __________________________________________________ */
48 
49 
50 /*==========================================================================
51  = =
52  = Constructors/destructors =
53  = =
54  ==========================================================================*/
55 
56 /***********************************************************************//**
57  * @brief Void constructor
58  ***************************************************************************/
60 {
61  // Initialise members
62  init_members();
63 
64  // Return
65  return;
66 }
67 
68 
69 /***********************************************************************//**
70  * @brief Parameter constructor
71  *
72  * @param[in] name Parameter name.
73  * @param[in] value Parameter value.
74  *
75  * Constructs a parameter from a parameter @p name and a parameter @p value.
76  *
77  * The parameter is auto-scaled, which for a @p value that differs from zero
78  * sets the scale factor to @p value and the @p factor_value to unity. For a
79  * @p value of zero, the scale factor will be set to unity and the
80  * @p factor_value will be set to @p value.
81  ***************************************************************************/
82 GModelPar::GModelPar(const std::string& name, const double& value) :
83  GOptimizerPar(name, value)
84 {
85  // Initialise members
86  init_members();
87 
88  // Return
89  return;
90 }
91 
92 
93 /***********************************************************************//**
94  * @brief Parameter constructor
95  *
96  * @param[in] name Parameter name.
97  * @param[in] factor Parameter value factor.
98  * @param[in] scale Parameter scaling (non-zero value).
99  *
100  * @exception GException::invalid_argument
101  * Sacle factor of 0 specified.
102  *
103  * Constructs a parameter from a parameter @p name, value @p factor
104  * and @p scale factor. The @p scale factor needs to be a non-zero value.
105  * If the @p scale factor is zero, an exception is thrown.
106  ***************************************************************************/
107 GModelPar::GModelPar(const std::string& name,
108  const double& factor,
109  const double& scale) :
110  GOptimizerPar(name, factor, scale)
111 
112 {
113  // Initialise members
114  init_members();
115 
116  // Return
117  return;
118 }
119 
120 
121 /***********************************************************************//**
122  * @brief Copy constructor
123  *
124  * @param[in] par Model parameter.
125  ***************************************************************************/
127 {
128  // Initialise members
129  init_members();
130 
131  // Copy members
132  copy_members(par);
133 
134  // Return
135  return;
136 }
137 
138 
139 /***********************************************************************//**
140  * @brief Destructor
141  ***************************************************************************/
143 {
144  // Free members
145  free_members();
146 
147  // Return
148  return;
149 }
150 
151 
152 /*==========================================================================
153  = =
154  = Operators =
155  = =
156  ==========================================================================*/
157 
158 /***********************************************************************//**
159  * @brief Assignment operator
160  *
161  * @param[in] par Model parameter.
162  * @return Model parameter.
163  ***************************************************************************/
165 {
166  // Execute only if object is not identical
167  if (this != &par) {
168 
169  // Copy base class members
170  this->GOptimizerPar::operator=(par);
171 
172  // Free members
173  free_members();
174 
175  // Initialise members
176  init_members();
177 
178  // Copy members
179  copy_members(par);
180 
181  } // endif: object was not identical
182 
183  // Return this object
184  return *this;
185 }
186 
187 
188 /*==========================================================================
189  = =
190  = Public methods =
191  = =
192  ==========================================================================*/
193 
194 /***********************************************************************//**
195  * @brief Clone model parameter
196  *
197  * @return Pointer to deep copy of model parameter.
198  ***************************************************************************/
200 {
201  // Clone model parameter
202  return new GModelPar(*this);
203 }
204 
205 
206 /***********************************************************************//**
207  * @brief Extract parameter attributes from XML element
208  *
209  * @param[in] xml XML element
210  *
211  * @exception GException::invalid_value
212  * Invalid combination of parameter attributes encountered.
213  *
214  * Extracts the parameter attributes from an XML element of the form
215  *
216  * <parameter name=".." value=".." error=".." scale=".." min=".." max="..' free="..">
217  *
218  * Each of the attributes are optional, with the following scheme for
219  * assigning default values in case that the attribute was not found:
220  *
221  * @p name sets @p m_name (defaults to "Unknown")
222  * @p value sets @p m_factor_value (defaults to 0.0)
223  * @p error sets @p m_factor_error (defaults to 0.0)
224  * @p scale sets @p m_scale (defaults to 1.0)
225  * @p min sets @p m_factor_min (will remove_min() if not found)
226  * @p max sets @p m_factor_max (will remove_max() if not found)
227  * @p free sets @p m_free (papameter will be fixed if not found)
228  ***************************************************************************/
229 void GModelPar::read(const GXmlElement& xml)
230 {
231  // Get name
232  std::string arg = xml.attribute("name");
233  if (arg != "") {
234  m_name = arg;
235  }
236  else {
237  m_name = "Unknown";
238  }
239 
240  // Get value
241  arg = xml.attribute("value");
242  if (arg != "") {
244  }
245  else {
246  m_factor_value = 0.0;
247  }
248 
249  // Get error
250  arg = xml.attribute("error");
251  if (arg != "") {
253  }
254  else {
255  m_factor_error = 0.0;
256  }
257 
258  // Get scale factor
259  arg = xml.attribute("scale");
260  if (arg != "") {
262  }
263  else {
264  m_scale = 1.0;
265  }
266 
267  // Get min
268  arg = xml.attribute("min");
269  if (arg != "") {
271  }
272  else {
274  }
275 
276  // Get max
277  arg = xml.attribute("max");
278  if (arg != "") {
280  }
281  else {
283  }
284 
285  // Get free
286  if (xml.attribute("free") == "1" ||
287  gammalib::tolower(xml.attribute("free")) == "true") {
288  free();
289  }
290  else {
291  fix();
292  }
293 
294  // If there is a minimum and maximum, make sure that the maximum is
295  // not smaller than the minimum
296  if (has_min() && has_max()) {
297  if (min() > max()) {
298  std::string msg = "The model parameter \""+m_name+
299  "\" in the XML document has a minimum boundary "+
300  gammalib::str(min())+
301  " that is larger than the maximum boundary "+
302  gammalib::str(max())+".\n"+xml.print();
303  throw GException::invalid_value(G_READ, msg);
304  }
305  }
306 
307  // If there is a minimum, make sure that the value is not below it
308  if (has_min() && (value() < min())) {
309  std::string msg = "The model parameter \""+m_name+
310  "\" in the XML document has a value "+
311  gammalib::str(value())+
312  " that is smaller than the minimum boundary "+
313  gammalib::str(min())+".\n"+xml.print();
314  throw GException::invalid_value(G_READ, msg);
315  }
316 
317  // If there is a maximum, make sure that the value is not above it
318  if (has_max() && (value() > max())) {
319  std::string msg = "The model parameter \""+m_name+
320  "\" in the XML document has a value "+
321  gammalib::str(value())+
322  " that is larger than the maximum boundary "+
323  gammalib::str(max())+".\n"+xml.print();
324  throw GException::invalid_value(G_READ, msg);
325  }
326 
327  // Return
328  return;
329 }
330 
331 
332 /***********************************************************************//**
333  * @brief Set or update parameter attributes in XML element
334  *
335  * @param[in] xml XML element.
336  *
337  * Sets or updates the parameter attributes in an XML element of the form
338  *
339  * <parameter name=".." value=".." error=".." scale=".." min=".." max="..' free="..">
340  *
341  * The following attributes will be set:
342  *
343  * @p name
344  * @p value
345  * @p error (only in case that the parameter is free)
346  * @p scale
347  * @p min (only in case that a minimum exists)
348  * @p max (only in case that a maximum exists)
349  * @p free
350  ***************************************************************************/
352 {
353  // Set name
354  xml.attribute("name", m_name);
355 
356  // Set value
357  xml.attribute("value", gammalib::str(m_factor_value));
358 
359  // Set error (only if parameter is free)
360  if (is_free()) {
361  xml.attribute("error", gammalib::str(m_factor_error));
362  }
363 
364  // Set scale
365  xml.attribute("scale", gammalib::str(m_scale));
366 
367  // Set minimum
368  if (has_factor_min()) {
369  xml.attribute("min", gammalib::str(factor_min()));
370  }
371 
372  // Set maximum
373  if (has_factor_max()) {
374  xml.attribute("max", gammalib::str(factor_max()));
375  }
376 
377  // Set free/fix flag
378  if (is_free()) {
379  xml.attribute("free", "1");
380  }
381  else {
382  xml.attribute("free", "0");
383  }
384 
385  // Return
386  return;
387 }
388 
389 
390 /*==========================================================================
391  = =
392  = Private methods =
393  = =
394  ==========================================================================*/
395 
396 /***********************************************************************//**
397  * @brief Initialise class members
398  ***************************************************************************/
400 {
401  // Return
402  return;
403 }
404 
405 
406 /***********************************************************************//**
407  * @brief Copy class members
408  *
409  * @param[in] par Model parameter.
410  ***************************************************************************/
412 {
413  // Return
414  return;
415 }
416 
417 
418 /***********************************************************************//**
419  * @brief Delete class members
420  ***************************************************************************/
422 {
423  // Return
424  return;
425 }
void init_members(void)
Initialise class members.
Definition: GModelPar.cpp:399
GModelPar * clone(void) const
Clone model parameter.
Definition: GModelPar.cpp:199
virtual std::string print(const GChatter &chatter=NORMAL, const int &indent=0) const
Print XML element.
bool has_factor_max(void) const
Signal if parameter has maximum factor boundary.
GModelPar(void)
Void constructor.
Definition: GModelPar.cpp:59
void write(GXmlElement &xml) const
Set or update parameter attributes in XML element.
Definition: GModelPar.cpp:351
bool has_factor_min(void) const
Signal if parameter has minimum factor boundary.
XML element node class.
Definition: GXmlElement.hpp:48
double max(void) const
Return parameter maximum boundary.
Gammalib tools definition.
double min(void) const
Return parameter minimum boundary.
bool is_free(void) const
Signal if parameter is free.
void copy_members(const GModelPar &par)
Copy class members.
Definition: GModelPar.cpp:411
GModelPar & operator=(const GModelPar &par)
Assignment operator.
Definition: GModelPar.cpp:164
Model parameter class interface definition.
Model parameter class.
Definition: GModelPar.hpp:87
virtual ~GModelPar(void)
Destructor.
Definition: GModelPar.cpp:142
const GXmlAttribute * attribute(const int &index) const
Return attribute.
double m_scale
Parameter scaling (true = factor * scale)
const double & factor_max(void) const
Return parameter maximum factor boundary.
void free(void)
Free a parameter.
void remove_factor_max(void)
Removes maximum factor boundary.
void remove_factor_min(void)
Removes minimum factor boundary.
void fix(void)
Fix a parameter.
GOptimizerPar & operator=(const GOptimizerPar &par)
Assignment operator.
double m_factor_value
Parameter factor value.
double m_factor_error
Uncertainty in parameter factor value.
std::string m_name
Parameter name.
bool has_max(void) const
Signal if parameter has maximum boundary.
const double & factor_min(void) const
Return parameter minimum factor boundary.
#define G_READ
Definition: GModelPar.cpp:41
void read(const GXmlElement &xml)
Extract parameter attributes from XML element.
Definition: GModelPar.cpp:229
double value(void) const
Return parameter value.
bool has_min(void) const
Signal if parameter has minimum boundary.
Exception handler interface definition.
std::string tolower(const std::string &s)
Convert string to lower case.
Definition: GTools.cpp:955
void free_members(void)
Delete class members.
Definition: GModelPar.cpp:421
double todouble(const std::string &arg)
Convert string into double precision value.
Definition: GTools.cpp:926
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:489
Optimizer parameter class.