GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GModelSpectral.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GModelSpectral.cpp - Abstract spectral model base class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2009-2021 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 GModelSpectral.cpp
23  * @brief Abstract spectral model base 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 "GModelSpectral.hpp"
33 #include "GModelPar.hpp"
34 
35 /* __ Method name definitions ____________________________________________ */
36 #define G_ACCESS "GModelSpectral::operator[](std::string&)"
37 #define G_AT "GModelPar& GModelSpectral::at(int&)"
38 
39 /* __ Macros _____________________________________________________________ */
40 
41 /* __ Coding definitions _________________________________________________ */
42 
43 /* __ Debug definitions __________________________________________________ */
44 
45 
46 /*==========================================================================
47  = =
48  = Constructors/destructors =
49  = =
50  ==========================================================================*/
51 
52 /***********************************************************************//**
53  * @brief Void constructor
54  ***************************************************************************/
56 {
57  // Initialise members
58  init_members();
59 
60  // Return
61  return;
62 }
63 
64 
65 /***********************************************************************//**
66  * @brief Copy constructor
67  *
68  * @param[in] model Spectral model.
69  ***************************************************************************/
71 {
72  // Initialise members
73  init_members();
74 
75  // Copy members
76  copy_members(model);
77 
78  // Return
79  return;
80 }
81 
82 
83 /***********************************************************************//**
84  * @brief Destructor
85  ***************************************************************************/
87 {
88  // Free members
89  free_members();
90 
91  // Return
92  return;
93 }
94 
95 
96 /*==========================================================================
97  = =
98  = Operators =
99  = =
100  ==========================================================================*/
101 
102 /***********************************************************************//**
103  * @brief Assignment operator
104  *
105  * @param[in] model Spectral model.
106  * @return Spectral model.
107  ***************************************************************************/
109 {
110  // Execute only if object is not identical
111  if (this != &model) {
112 
113  // Free members
114  free_members();
115 
116  // Initialise private members for clean destruction
117  init_members();
118 
119  // Copy members
120  copy_members(model);
121 
122  } // endif: object was not identical
123 
124  // Return
125  return *this;
126 }
127 
128 
129 /***********************************************************************//**
130  * @brief Returns reference to model parameter
131  *
132  * @param[in] name Parameter name.
133  *
134  * @exception GException::invalid_argument
135  * Parameter with specified name not found.
136  ***************************************************************************/
137 GModelPar& GModelSpectral::operator[](const std::string& name)
138 {
139  // Get parameter index
140  int index = 0;
141  for (; index < size(); ++index) {
142  if (m_pars[index]->name() == name) {
143  break;
144  }
145  }
146 
147  // Throw exception if parameter name was not found
148  if (index >= size()) {
149  std::string msg = "Model parameter \""+name+"\" not found in model. "
150  "Please specify a valid model parameter name.";
152  }
153 
154  // Return reference
155  return *(m_pars[index]);
156 }
157 
158 
159 /***********************************************************************//**
160  * @brief Returns reference to model parameter (const version)
161  *
162  * @param[in] name Parameter name.
163  *
164  * @exception GException::invalid_argument
165  * Parameter with specified name not found.
166  ***************************************************************************/
167 const GModelPar& GModelSpectral::operator[](const std::string& name) const
168 {
169  // Get parameter index
170  int index = 0;
171  for (; index < size(); ++index) {
172  if (m_pars[index]->name() == name) {
173  break;
174  }
175  }
176 
177  // Throw exception if parameter name was not found
178  if (index >= size()) {
179  std::string msg = "Model parameter \""+name+"\" not found in model. "
180  "Please specify a valid model parameter name.";
182  }
183 
184  // Return reference
185  return *(m_pars[index]);
186 }
187 
188 
189 /*==========================================================================
190  = =
191  = Public methods =
192  = =
193  ==========================================================================*/
194 
195 /***********************************************************************//**
196  * @brief Returns model parameter
197  *
198  * @param[in] index Parameter index [0,...,size()[.
199  * @return Model parameter.
200  *
201  * @exception GException::out_of_range
202  * Parameter index is out of range.
203  *
204  * Returns model parameter with @p index range checking.
205  ***************************************************************************/
206 GModelPar& GModelSpectral::at(const int& index)
207 {
208  // Compile option: raise exception if index is out of range
209  if (index < 0 || index >= size()) {
210  throw GException::out_of_range(G_AT, "Parameter index", index, size());
211  }
212 
213  // Return reference
214  return *(m_pars[index]);
215 }
216 
217 
218 /***********************************************************************//**
219  * @brief Returns model parameter (const version)
220  *
221  * @param[in] index Parameter index [0,...,size()[.
222  * @return Model parameter.
223  *
224  * @exception GException::out_of_range
225  * Parameter index is out of range.
226  *
227  * Returns model parameter with @p index range checking.
228  ***************************************************************************/
229 const GModelPar& GModelSpectral::at(const int& index) const
230 {
231  // Compile option: raise exception if index is out of range
232  if (index < 0 || index >= size()) {
233  throw GException::out_of_range(G_AT, "Parameter index", index, size());
234  }
235 
236  // Return reference
237  return *(m_pars[index]);
238 }
239 
240 
241 /***********************************************************************//**
242  * @brief Checks if parameter name exists
243  *
244  * @param[in] name Parameter name.
245  * @return True if parameter with specified @p name exists.
246  *
247  * Searches all parameter names for a match with the specified @p name. If
248  * the specified name has been found, true is returned.
249  ***************************************************************************/
250 bool GModelSpectral::has_par(const std::string& name) const
251 {
252  // Default found flag to false
253  bool found = false;
254 
255  // Search for parameter name
256  for (int i = 0; i < size(); ++i) {
257  if (m_pars[i]->name() == name) {
258  found = true;
259  break;
260  }
261  }
262 
263  // Return
264  return found;
265 }
266 
267 
268 /***********************************************************************//**
269  * @brief Autoscale parameters
270  *
271  * Sets the scale factors for all parameters so that the values are unity.
272  ***************************************************************************/
274 {
275  // Loop over all parameters
276  for (int i = 0; i < m_pars.size(); ++i) {
277  if (m_pars[i] != NULL) {
278  m_pars[i]->autoscale();
279  }
280  }
281 
282  // Return
283  return;
284 }
285 
286 
287 /*==========================================================================
288  = =
289  = Private methods =
290  = =
291  ==========================================================================*/
292 
293 /***********************************************************************//**
294  * @brief Initialise class members
295  ***************************************************************************/
297 {
298  // Initialise members
299  m_pars.clear();
300 
301  // Return
302  return;
303 }
304 
305 
306 /***********************************************************************//**
307  * @brief Copy class members
308  *
309  * @param[in] model Spectral model.
310  ***************************************************************************/
312 {
313  // Copy members
314  m_pars = model.m_pars;
315 
316  // Return
317  return;
318 }
319 
320 
321 /***********************************************************************//**
322  * @brief Delete class members
323  ***************************************************************************/
325 {
326  // Return
327  return;
328 }
Abstract spectral model base class.
int size(void) const
Return number of parameters.
virtual GModelSpectral & operator=(const GModelSpectral &model)
Assignment operator.
std::vector< GModelPar * > m_pars
Parameter pointers.
GModelSpectral(void)
Void constructor.
virtual ~GModelSpectral(void)
Destructor.
#define G_AT
Model parameter class interface definition.
Model parameter class.
Definition: GModelPar.hpp:87
GModelPar & at(const int &index)
Returns model parameter.
bool has_par(const std::string &name) const
Checks if parameter name exists.
void copy_members(const GModelSpectral &model)
Copy class members.
#define G_ACCESS
void autoscale(void)
Autoscale parameters.
virtual GModelPar & operator[](const int &index)
Returns model parameter.
Abstract spectral model base class interface definition.
void init_members(void)
Initialise class members.
Exception handler interface definition.
void free_members(void)
Delete class members.