GammaLib 2.0.0
Loading...
Searching...
No Matches
GModelTemporal.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GModelTemporal.cpp - Abstract temporal 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 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
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
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
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::invalid_argument
134 * Parameter with specified name not found.
135 ***************************************************************************/
136GModelPar& 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
146 // Throw exception if parameter name was not found
147 if (index >= size()) {
148 std::string msg = "Model parameter \""+name+"\" not found in model. "
149 "Please specify a valid model parameter name.";
151 }
152
153 // Return reference
154 return *(m_pars[index]);
155}
156
157
158/***********************************************************************//**
159 * @brief Returns reference to model parameter (const version)
160 *
161 * @param[in] name Parameter name.
162 *
163 * @exception GException::invalid_argument
164 * Parameter with specified name not found.
165 ***************************************************************************/
166const GModelPar& GModelTemporal::operator[](const std::string& name) const
167{
168 // Get parameter index
169 int index = 0;
170 for (; index < size(); ++index) {
171 if (m_pars[index]->name() == name) {
172 break;
173 }
174 }
175
176 // Throw exception if parameter name was not found
177 if (index >= size()) {
178 std::string msg = "Model parameter \""+name+"\" not found in model. "
179 "Please specify a valid model parameter name.";
181 }
182
183 // Return reference
184 return *(m_pars[index]);
185}
186
187
188/*==========================================================================
189 = =
190 = Public methods =
191 = =
192 ==========================================================================*/
193
194/***********************************************************************//**
195 * @brief Returns model parameter
196 *
197 * @param[in] index Parameter index [0,...,size()[.
198 * @return Model parameter.
199 *
200 * @exception GException::out_of_range
201 * Parameter index is out of range.
202 *
203 * Returns model parameter with @p index range checking.
204 ***************************************************************************/
206{
207 // Compile option: raise exception if index is out of range
208 if (index < 0 || index >= size()) {
209 throw GException::out_of_range(G_AT, "Parameter index", index, size());
210 }
211
212 // Return reference
213 return *(m_pars[index]);
214}
215
216
217/***********************************************************************//**
218 * @brief Returns model parameter (const version)
219 *
220 * @param[in] index Parameter index [0,...,size()[.
221 * @return Model parameter.
222 *
223 * @exception GException::out_of_range
224 * Parameter index is out of range.
225 *
226 * Returns model parameter with @p index range checking.
227 ***************************************************************************/
228const GModelPar& GModelTemporal::at(const int& index) const
229{
230 // Compile option: raise exception if index is out of range
231 if (index < 0 || index >= size()) {
232 throw GException::out_of_range(G_AT, "Parameter index", index, size());
233 }
234
235 // Return reference
236 return *(m_pars[index]);
237}
238
239
240/***********************************************************************//**
241 * @brief Checks if parameter name exists
242 *
243 * @param[in] name Parameter name.
244 * @return True if parameter with specified @p name exists.
245 *
246 * Searches all parameter names for a match with the specified @p name. If
247 * the specified name has been found, true is returned.
248 ***************************************************************************/
249bool GModelTemporal::has_par(const std::string& name) const
250{
251 // Default found flag to false
252 bool found = false;
253
254 // Search for parameter name
255 for (int i = 0; i < size(); ++i) {
256 if (m_pars[i]->name() == name) {
257 found = true;
258 break;
259 }
260 }
261
262 // Return
263 return found;
264}
265
266
267/***********************************************************************//**
268 * @brief Autoscale parameters
269 *
270 * Sets the scale factors for all parameters so that the values are unity.
271 ***************************************************************************/
273{
274 // Loop over all parameters
275 for (int i = 0; i < m_pars.size(); ++i) {
276 if (m_pars[i] != NULL) {
277 m_pars[i]->autoscale();
278 }
279 }
280
281 // Return
282 return;
283}
284
285
286/*==========================================================================
287 = =
288 = Private methods =
289 = =
290 ==========================================================================*/
291
292/***********************************************************************//**
293 * @brief Initialise class members
294 ***************************************************************************/
296{
297 // Initialise members
298 m_pars.clear();
299
300 // Return
301 return;
302}
303
304
305/***********************************************************************//**
306 * @brief Copy class members
307 *
308 * @param[in] model Temporal model.
309 ***************************************************************************/
311{
312 // Copy members
313 m_pars = model.m_pars;
314
315 // Return
316 return;
317}
318
319
320/***********************************************************************//**
321 * @brief Delete class members
322 ***************************************************************************/
324{
325 // Return
326 return;
327}
#define G_AT
#define G_ACCESS
Exception handler interface definition.
Abstract temporal model base class interface definition.
Model parameter class.
Definition GModelPar.hpp:87
Abstract temporal model base class.
virtual GModelTemporal & operator=(const GModelTemporal &model)
Assignment operator.
GModelTemporal(void)
Void constructor.
bool has_par(const std::string &name) const
Checks if parameter name exists.
void free_members(void)
Delete class members.
int size(void) const
Return number of parameters.
void copy_members(const GModelTemporal &model)
Copy class members.
void init_members(void)
Initialise class members.
virtual GModelPar & operator[](const int &index)
Returns model parameter.
std::vector< GModelPar * > m_pars
Parameter pointers.
void autoscale(void)
Autoscale parameters.
GModelPar & at(const int &index)
Returns model parameter.
virtual ~GModelTemporal(void)
Destructor.