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