GammaLib 2.0.0
Loading...
Searching...
No Matches
GModelTemporalConst.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GModelTemporalConst.cpp - Temporal constant model 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 GModelTemporalConst.cpp
23 * @brief Constant temporal model class interface implementation
24 * @author Juergen Knoedlseder
25 */
26
27/* __ Includes ___________________________________________________________ */
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include "GTools.hpp"
32#include "GException.hpp"
35
36/* __ Constants __________________________________________________________ */
37
38/* __ Globals ____________________________________________________________ */
40const GModelTemporalRegistry g_temporal_const_registry(&g_temporal_const_seed);
41
42/* __ Method name definitions ____________________________________________ */
43#define G_READ "GModelTemporalConst::read(GXmlElement&)"
44#define G_WRITE "GModelTemporalConst::write(GXmlElement&)"
45
46/* __ Macros _____________________________________________________________ */
47
48/* __ Coding definitions _________________________________________________ */
49
50/* __ Debug definitions __________________________________________________ */
51
52
53/*==========================================================================
54 = =
55 = Constructors/destructors =
56 = =
57 ==========================================================================*/
58
59/***********************************************************************//**
60 * @brief Void constructor
61 ***************************************************************************/
63{
64 // Initialise members
66
67 // Return
68 return;
69}
70
71
72/***********************************************************************//**
73 * @brief XML constructor
74 *
75 * @param[in] xml XML element.
76 *
77 * Constructs constant temporal model by extracting information from an XML
78 * element. See the read() method for more information about the expected
79 * structure of the XML element.
80 ***************************************************************************/
83{
84 // Initialise members
86
87 // Read information from XML element
88 read(xml);
89
90 // Return
91 return;
92}
93
94
95
96
97/***********************************************************************//**
98 * @brief Value constructor
99 *
100 * @param[in] norm Normalization factor.
101 *
102 * Constructs constant temporal model by setting the normalization factor.
103 ***************************************************************************/
106{
107 // Initialise members
108 init_members();
109
110 // Set normalization factor
112
113 // Return
114 return;
115}
116
117
118
119
120/***********************************************************************//**
121 * @brief Copy constructor
122 *
123 * @param[in] model Constant temporal model
124 ***************************************************************************/
126 GModelTemporal(model)
127{
128 // Initialise members
129 init_members();
130
131 // Copy members
132 copy_members(model);
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] model Constant temporal model.
162 * @return Constant temporal model.
163 ***************************************************************************/
165{
166 // Execute only if object is not identical
167 if (this != &model) {
168
169 // Copy base class members
170 this->GModelTemporal::operator=(model);
171
172 // Free members
173 free_members();
174
175 // Initialise members
176 init_members();
177
178 // Copy members
179 copy_members(model);
180
181 } // endif: object was not identical
182
183 // Return
184 return *this;
185}
186
187
188/*==========================================================================
189 = =
190 = Public methods =
191 = =
192 ==========================================================================*/
193
194/***********************************************************************//**
195 * @brief Clear constant temporal model
196 ***************************************************************************/
198{
199 // Free class members (base and derived classes, derived class first)
200 free_members();
202
203 // Initialise members
205 init_members();
206
207 // Return
208 return;
209}
210
211
212/***********************************************************************//**
213 * @brief Clone constant temporal model
214 *
215 * @return Pointer to deep copy of constant temporal model.
216 ***************************************************************************/
218{
219 // Clone constant temporal model
220 return new GModelTemporalConst(*this);
221}
222
223
224/***********************************************************************//**
225 * @brief Evaluate function
226 *
227 * @param[in] srcTime True photon arrival time (not used).
228 * @param[in] gradients Compute gradients?
229 * @return Value to temporal model.
230 *
231 * Computes
232 *
233 * \f[
234 * S_{\rm t}(t) = {\tt m\_norm}
235 * \f]
236 *
237 * where
238 * \f${\tt m\_norm}\f$ is the normalization constant.
239 *
240 * If the @p gradients flag is true the method will also evaluate the partial
241 * derivatives of the model with respect to the normalization parameter using
242 *
243 * \f[
244 * \frac{\delta S_{\rm t}(t)}{\delta {\tt m\_norm}} = 1
245 * \f]
246 ***************************************************************************/
247double GModelTemporalConst::eval(const GTime& srcTime,
248 const bool& gradients) const
249{
250 // Compute function value
251 double value = m_norm.value();
252
253 // Optionally compute partial derivatives
254 if (gradients) {
255
256 // Compute partial derivatives of the parameter values
257 double g_norm = (m_norm.is_free()) ? m_norm.scale() : 0.0;
258
259 // Set factor gradient
260 m_norm.factor_gradient(g_norm);
261
262 } // endif: computed partial derivatives
263
264 // Return
265 return value;
266}
267
268
269/***********************************************************************//**
270 * @brief Returns vector of random event times
271 *
272 * @param[in] rate Mean event rate (events per second).
273 * @param[in] tmin Minimum event time.
274 * @param[in] tmax Maximum event time.
275 * @param[in,out] ran Random number generator.
276 *
277 * This method returns a vector of random event times assuming a constant
278 * event rate that is specified by the rate parameter.
279 ***************************************************************************/
280GTimes GModelTemporalConst::mc(const double& rate, const GTime& tmin,
281 const GTime& tmax, GRan& ran) const
282{
283 // Allocates empty vector of times
284 GTimes times;
285
286 // Compute event rate (in events per seconds)
287 double lambda = rate * norm();
288
289 // Initialise start and stop times in seconds
290 double time = tmin.secs();
291 double tstop = tmax.secs();
292
293 // Generate events until maximum event time is exceeded
294 while (time <= tstop) {
295
296 // Simulate next event time
297 time += ran.exp(lambda);
298
299 // Add time if it is not beyond the stop time
300 if (time <= tstop) {
301 GTime event;
302 event.secs(time);
303 times.append(event);
304 }
305
306 } // endwhile: loop until stop time is reached
307
308 // Return vector of times
309 return times;
310}
311
312
313/***********************************************************************//**
314 * @brief Read model from XML element
315 *
316 * @param[in] xml XML element.
317 *
318 * Writes the temporal information from an XML element having the format
319 *
320 * <temporal type="Constant">
321 * <parameter name="Normalization" scale="1" value="1" min="0.1" max="10" free="1"/>
322 * </temporal>
323 ***************************************************************************/
325{
326 // Verify number of model parameters
328
329 // Get parameter pointers
331
332 // Read parameters
333 m_norm.read(*norm);
334
335 // Return
336 return;
337}
338
339
340/***********************************************************************//**
341 * @brief Write model into XML element
342 *
343 * @param[in] xml XML element.
344 *
345 * Writes the temporal information into an XML element in the format
346 *
347 * <temporal type="Constant">
348 * <parameter name="Normalization" scale="1" value="1" min="0.1" max="10" free="1"/>
349 * </temporal>
350 ***************************************************************************/
352{
353 // Verify model type
355
356 // Get XML parameters
358
359 // Write parameters
360 m_norm.write(*norm);
361
362 // Return
363 return;
364}
365
366
367/***********************************************************************//**
368 * @brief Print constant information
369 *
370 * @param[in] chatter Chattiness.
371 * @return String containing model information.
372 ***************************************************************************/
373std::string GModelTemporalConst::print(const GChatter& chatter) const
374{
375 // Initialise result string
376 std::string result;
377
378 // Continue only if chatter is not silent
379 if (chatter != SILENT) {
380
381 // Append header
382 result.append("=== GModelTemporalConst ===");
383
384 // Append information
385 result.append("\n"+gammalib::parformat("Number of parameters"));
386 result.append(gammalib::str(size()));
387 for (int i = 0; i < size(); ++i) {
388 result.append("\n"+m_pars[i]->print(chatter));
389 }
390
391 } // endif: chatter was not silent
392
393 // Return result
394 return result;
395}
396
397
398/*==========================================================================
399 = =
400 = Private methods =
401 = =
402 ==========================================================================*/
403
404/***********************************************************************//**
405 * @brief Initialise class members
406 ***************************************************************************/
408{
409 // Initialise normalisation parameter
410 m_norm.clear();
411 m_norm.name("Normalization");
412 m_norm.unit("(relative value)");
413 m_norm.value(1.0);
414 m_norm.fix();
415 m_norm.gradient(0.0);
416 m_norm.has_grad(true);
417
418 // Set parameter pointer(s)
419 m_pars.clear();
420 m_pars.push_back(&m_norm);
421
422 // Return
423 return;
424}
425
426
427/***********************************************************************//**
428 * @brief Copy class members
429 *
430 * @param[in] model Constant temporal model
431 ***************************************************************************/
433{
434 // Copy members
435 m_norm = model.m_norm;
436
437 // Set parameter pointer(s)
438 m_pars.clear();
439 m_pars.push_back(&m_norm);
440
441 // Return
442 return;
443}
444
445
446/***********************************************************************//**
447 * @brief Delete class members
448 ***************************************************************************/
450{
451 // Return
452 return;
453}
#define G_WRITE
#define G_READ
Exception handler interface definition.
const GModelTemporalConst g_temporal_const_seed
Constant temporal model class interface definition.
Temporal model registry class definition.
Gammalib tools definition.
GChatter
Definition GTypemaps.hpp:33
@ SILENT
Definition GTypemaps.hpp:34
double norm(const GVector &vector)
Computes vector norm.
Definition GVector.cpp:864
void write(GXmlElement &xml) const
Set or update parameter attributes in XML element.
void read(const GXmlElement &xml)
Extract parameter attributes from XML element.
Constant temporal model class.
virtual GModelTemporalConst & operator=(const GModelTemporalConst &model)
Assignment operator.
virtual double eval(const GTime &srcTime, const bool &gradients=false) const
Evaluate function.
virtual std::string type(void) const
Return model type.
GModelTemporalConst(void)
Void constructor.
virtual void write(GXmlElement &xml) const
Write model into XML element.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print constant information.
GModelPar m_norm
Constant.
void copy_members(const GModelTemporalConst &model)
Copy class members.
void free_members(void)
Delete class members.
virtual void read(const GXmlElement &xml)
Read model from XML element.
void init_members(void)
Initialise class members.
virtual ~GModelTemporalConst(void)
Destructor.
virtual GTimes mc(const double &rate, const GTime &tmin, const GTime &tmax, GRan &ran) const
Returns vector of random event times.
virtual void clear(void)
Clear constant temporal model.
virtual GModelTemporalConst * clone(void) const
Clone constant temporal model.
double norm(void) const
Return normalization factor.
Interface definition for the temporal model registry class.
Abstract temporal model base class.
virtual GModelTemporal & operator=(const GModelTemporal &model)
Assignment operator.
void free_members(void)
Delete class members.
int size(void) const
Return number of parameters.
void init_members(void)
Initialise class members.
std::vector< GModelPar * > m_pars
Parameter pointers.
bool is_free(void) const
Signal if parameter is free.
const double & scale(void) const
Return parameter scale.
bool has_grad(void) const
Signal if parameter gradient is computed analytically.
const std::string & unit(void) const
Return parameter unit.
const double & factor_gradient(void) const
Return parameter factor gradient.
void fix(void)
Fix a parameter.
double gradient(void) const
Return parameter gradient.
void clear(void)
Clear parameter.
double value(void) const
Return parameter value.
const std::string & name(void) const
Return parameter name.
Random number generator class.
Definition GRan.hpp:44
double exp(const double &lambda)
Returns exponential deviates.
Definition GRan.cpp:291
Time class.
Definition GTime.hpp:55
const double & secs(void) const
Return time in seconds in native reference (TT)
Definition GTime.hpp:156
Time container class.
Definition GTimes.hpp:45
void append(const GTime &time)
Append time to container.
Definition GTimes.cpp:216
XML element node class.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition GTools.cpp:1143
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition GTools.cpp:489
const GXmlElement * xml_get_par(const std::string &origin, const GXmlElement &xml, const std::string &name)
Return pointer to parameter with given name in XML element.
Definition GTools.cpp:1689
GXmlElement * xml_need_par(const std::string &origin, GXmlElement &xml, const std::string &name)
Return pointer to parameter with given name in XML element.
Definition GTools.cpp:1637
void xml_check_parnum(const std::string &origin, const GXmlElement &xml, const int &number)
Checks number of parameters.
Definition GTools.cpp:1777
void xml_check_type(const std::string &origin, GXmlElement &xml, const std::string &type)
Checks the model type.
Definition GTools.cpp:1819