GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GModelTemporalConst.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GModelTemporalConst.cpp - Temporal constant model class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2009-2018 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"
33 #include "GModelTemporalConst.hpp"
35 
36 /* __ Constants __________________________________________________________ */
37 
38 /* __ Globals ____________________________________________________________ */
40 const 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
65  init_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
85  init_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
111  m_norm.value(norm);
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  ***************************************************************************/
247 double 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  ***************************************************************************/
280 GTimes 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  * @exception GExpection::invalid_value
319  * Invalid XML format encountered.
320  *
321  * Writes the temporal information from an XML element having the format
322  *
323  * <temporal type="Constant">
324  * <parameter name="Normalization" scale="1" value="1" min="0.1" max="10" free="1"/>
325  * </temporal>
326  ***************************************************************************/
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  * @exception GExpection::invalid_value
346  * Invalid XML format encountered.
347  *
348  * Writes the temporal information into an XML element in the format
349  *
350  * <temporal type="Constant">
351  * <parameter name="Normalization" scale="1" value="1" min="0.1" max="10" free="1"/>
352  * </temporal>
353  ***************************************************************************/
355 {
356  // Set model type
357  if (xml.attribute("type") == "") {
358  xml.attribute("type", type());
359  }
360 
361  // Verify model type
362  if (xml.attribute("type") != type()) {
364  "Temporal model is not of type \""+type()+"\".");
365  }
366 
367  // Get XML parameters
369 
370  // Write parameters
371  m_norm.write(*norm);
372 
373  // Return
374  return;
375 }
376 
377 
378 /***********************************************************************//**
379  * @brief Print constant information
380  *
381  * @param[in] chatter Chattiness.
382  * @return String containing model information.
383  ***************************************************************************/
384 std::string GModelTemporalConst::print(const GChatter& chatter) const
385 {
386  // Initialise result string
387  std::string result;
388 
389  // Continue only if chatter is not silent
390  if (chatter != SILENT) {
391 
392  // Append header
393  result.append("=== GModelTemporalConst ===");
394 
395  // Append information
396  result.append("\n"+gammalib::parformat("Number of parameters"));
397  result.append(gammalib::str(size()));
398  for (int i = 0; i < size(); ++i) {
399  result.append("\n"+m_pars[i]->print(chatter));
400  }
401 
402  } // endif: chatter was not silent
403 
404  // Return result
405  return result;
406 }
407 
408 
409 /*==========================================================================
410  = =
411  = Private methods =
412  = =
413  ==========================================================================*/
414 
415 /***********************************************************************//**
416  * @brief Initialise class members
417  ***************************************************************************/
419 {
420  // Initialise normalisation parameter
421  m_norm.clear();
422  m_norm.name("Normalization");
423  m_norm.unit("(relative value)");
424  m_norm.value(1.0);
425  m_norm.fix();
426  m_norm.gradient(0.0);
427  m_norm.has_grad(true);
428 
429  // Set parameter pointer(s)
430  m_pars.clear();
431  m_pars.push_back(&m_norm);
432 
433  // Return
434  return;
435 }
436 
437 
438 /***********************************************************************//**
439  * @brief Copy class members
440  *
441  * @param[in] model Constant temporal model
442  ***************************************************************************/
444 {
445  // Copy members
446  m_norm = model.m_norm;
447 
448  // Set parameter pointer(s)
449  m_pars.clear();
450  m_pars.push_back(&m_norm);
451 
452  // Return
453  return;
454 }
455 
456 
457 /***********************************************************************//**
458  * @brief Delete class members
459  ***************************************************************************/
461 {
462  // Return
463  return;
464 }
Constant temporal model class interface definition.
const double & factor_gradient(void) const
Return parameter gradient factor.
double norm(const GVector &vector)
Computes vector norm.
Definition: GVector.cpp:821
std::vector< GModelPar * > m_pars
Parameter pointers.
const std::string & name(void) const
Return parameter name.
double gradient(void) const
Return parameter gradient.
virtual std::string type(void) const
Return model type.
Abstract temporal model base class.
void write(GXmlElement &xml) const
Set or update parameter attributes in XML element.
Definition: GModelPar.cpp:353
virtual GModelTemporalConst & operator=(const GModelTemporalConst &model)
Assignment operator.
XML element node class.
Definition: GXmlElement.hpp:47
Random number generator class.
Definition: GRan.hpp:44
Time class.
Definition: GTime.hpp:54
Gammalib tools definition.
virtual GModelTemporalConst * clone(void) const
Clone constant temporal model.
bool is_free(void) const
Signal if parameter is free.
void copy_members(const GModelTemporalConst &model)
Copy class members.
virtual void write(GXmlElement &xml) const
Write model into XML element.
Time container class.
Definition: GTimes.hpp:45
virtual void clear(void)
Clear constant temporal model.
const double & scale(void) const
Return parameter scale.
virtual ~GModelTemporalConst(void)
Destructor.
const GModelTemporalConst g_temporal_const_seed
bool has_grad(void) const
Signal if parameter gradient is computed analytically.
const GXmlAttribute * attribute(const int &index) const
Return attribute.
#define G_READ
void fix(void)
Fix a parameter.
GModelPar m_norm
Constant.
int size(void) const
Return number of parameters.
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:1511
virtual std::string print(const GChatter &chatter=NORMAL) const
Print constant information.
void clear(void)
Clear parameter.
GModelTemporalConst(void)
Void constructor.
GChatter
Definition: GTypemaps.hpp:33
void init_members(void)
Initialise class members.
#define G_WRITE
void free_members(void)
Delete class members.
Interface definition for the temporal model registry class.
const double & secs(void) const
Return time in seconds in native reference (TT)
Definition: GTime.hpp:153
Temporal model registry class definition.
void read(const GXmlElement &xml)
Extract parameter attributes from XML element.
Definition: GModelPar.cpp:229
double value(void) const
Return parameter value.
virtual GModelTemporal & operator=(const GModelTemporal &model)
Assignment operator.
const std::string & unit(void) const
Return parameter unit.
Exception handler interface definition.
virtual void read(const GXmlElement &xml)
Read model from XML element.
double norm(void) const
Return normalization factor.
virtual double eval(const GTime &srcTime, const bool &gradients=false) const
Evaluate function.
void init_members(void)
Initialise class members.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1020
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:1560
void append(const GTime &time)
Append time to container.
Definition: GTimes.cpp:214
Constant temporal model class.
void free_members(void)
Delete class members.
double exp(const double &lambda)
Returns exponential deviates.
Definition: GRan.cpp:291
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:411
virtual GTimes mc(const double &rate, const GTime &tmin, const GTime &tmax, GRan &ran) const
Returns vector of random event times.