GammaLib  1.7.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-2016 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  m_has_min = true;
272  }
273  else {
274  remove_min();
275  }
276 
277  // Get max
278  arg = xml.attribute("max");
279  if (arg != "") {
281  m_has_max = true;
282  }
283  else {
284  remove_max();
285  }
286 
287  // Get free
288  if (xml.attribute("free") == "1" ||
289  gammalib::tolower(xml.attribute("free")) == "true") {
290  free();
291  }
292  else {
293  fix();
294  }
295 
296  // If there is a minimum and maximum, make sure that the maximum is
297  // not smaller than the minimum
298  if (m_has_min && m_has_max) {
299  if (m_factor_min > m_factor_max) {
300  std::string msg = "The model parameter \""+m_name+
301  "\" in the XML document has a minimum boundary "+
303  " that is larger than the maximum boundary "+
304  gammalib::str(m_factor_max)+".\n"+xml.print();
305  throw GException::invalid_value(G_READ, msg);
306  }
307  }
308 
309  // If there is a minimum, make sure that the value is not below it
311  std::string msg = "The model parameter \""+m_name+
312  "\" in the XML document has a value "+
314  " that is smaller than the minimum boundary "+
315  gammalib::str(m_factor_min)+".\n"+xml.print();
316  throw GException::invalid_value(G_READ, msg);
317  }
318 
319  // If there is a maximum, make sure that the value is not above it
321  std::string msg = "The model parameter \""+m_name+
322  "\" in the XML document has a value "+
324  " that is larger than the maximum boundary "+
325  gammalib::str(m_factor_max)+".\n"+xml.print();
326  throw GException::invalid_value(G_READ, msg);
327  }
328 
329  // Return
330  return;
331 }
332 
333 
334 /***********************************************************************//**
335  * @brief Set or update parameter attributes in XML element
336  *
337  * @param[in] xml XML element.
338  *
339  * Sets or updates the parameter attributes in an XML element of the form
340  *
341  * <parameter name=".." value=".." error=".." scale=".." min=".." max="..' free="..">
342  *
343  * The following attributes will be set:
344  *
345  * @p name
346  * @p value
347  * @p error (only in case that the parameter is free)
348  * @p scale
349  * @p min (only in case that a minimum exists)
350  * @p max (only in case that a maximum exists)
351  * @p free
352  ***************************************************************************/
354 {
355  // Set name
356  xml.attribute("name", m_name);
357 
358  // Set value
359  xml.attribute("value", gammalib::str(m_factor_value));
360 
361  // Set error (only if parameter is free)
362  if (is_free()) {
363  xml.attribute("error", gammalib::str(m_factor_error));
364  }
365 
366  // Set scale
367  xml.attribute("scale", gammalib::str(m_scale));
368 
369  // Set minimum
370  if (has_min()) {
371  xml.attribute("min", gammalib::str(m_factor_min));
372  }
373 
374  // Set maximum
375  if (has_max()) {
376  xml.attribute("max", gammalib::str(m_factor_max));
377  }
378 
379  // Set free/fix flag
380  if (is_free()) {
381  xml.attribute("free", "1");
382  }
383  else {
384  xml.attribute("free", "0");
385  }
386 
387  // Return
388  return;
389 }
390 
391 
392 /*==========================================================================
393  = =
394  = Private methods =
395  = =
396  ==========================================================================*/
397 
398 /***********************************************************************//**
399  * @brief Initialise class members
400  ***************************************************************************/
402 {
403  // Return
404  return;
405 }
406 
407 
408 /***********************************************************************//**
409  * @brief Copy class members
410  *
411  * @param[in] par Model parameter.
412  ***************************************************************************/
414 {
415  // Return
416  return;
417 }
418 
419 
420 /***********************************************************************//**
421  * @brief Delete class members
422  ***************************************************************************/
424 {
425  // Return
426  return;
427 }
void init_members(void)
Initialise class members.
Definition: GModelPar.cpp:401
void remove_min(void)
Removes minimum boundary.
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.
GModelPar(void)
Void constructor.
Definition: GModelPar.cpp:59
void write(GXmlElement &xml) const
Set or update parameter attributes in XML element.
Definition: GModelPar.cpp:353
XML element node class.
Definition: GXmlElement.hpp:47
double m_factor_min
Parameter minimum factor.
Gammalib tools definition.
bool is_free(void) const
Signal if parameter is free.
void copy_members(const GModelPar &par)
Copy class members.
Definition: GModelPar.cpp:413
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)
void free(void)
Free a parameter.
void fix(void)
Fix a parameter.
GOptimizerPar & operator=(const GOptimizerPar &par)
Assignment operator.
bool m_has_min
Parameter has minimum boundary.
double m_factor_value
Parameter value factor.
void remove_max(void)
Removes maximum boundary.
double m_factor_error
Uncertainty in parameter value factor.
std::string m_name
Parameter name.
double m_factor_max
Parameter maximum factor.
bool m_has_max
Parameter has maximum boundary.
bool has_max(void) const
Signal if parameter has maximum boundary.
#define G_READ
Definition: GModelPar.cpp:41
void read(const GXmlElement &xml)
Extract parameter attributes from XML element.
Definition: GModelPar.cpp:229
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:834
void free_members(void)
Delete class members.
Definition: GModelPar.cpp:423
double todouble(const std::string &arg)
Convert string into double precision value.
Definition: GTools.cpp:805
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:413
Optimizer parameter class.