GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GModelSpectralPlaw.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GModelSpectralPlaw.hpp - Spectral power law model class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2009-2016 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 GModelSpectralPlaw.hpp
23  * @brief Power law spectral model class interface definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GMODELSPECTRALPLAW_HPP
28 #define GMODELSPECTRALPLAW_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include "GModelSpectral.hpp"
33 #include "GModelPar.hpp"
34 #include "GEnergy.hpp"
35 
36 /* __ Forward declarations _______________________________________________ */
37 class GRan;
38 class GTime;
39 class GXmlElement;
40 
41 
42 /***********************************************************************//**
43  * @class GModelSpectralPlaw
44  *
45  * @brief Power law spectral model class
46  *
47  * This class implements a power law spectrum. The model is defined by
48  *
49  * \f[
50  * S_{\rm E}(E | t) = {\tt m\_norm}
51  * \left( \frac{E}{\tt m\_pivot} \right)^{\tt m\_index}
52  * \f]
53  *
54  * where
55  * - \f${\tt m\_norm}\f$ is the normalization or prefactor,
56  * - \f${\tt m\_index}\f$ is the spectral index, and
57  * - \f${\tt m\_pivot}\f$ is the pivot energy.
58  ***************************************************************************/
60 
61 public:
62  // Constructors and destructors
63  GModelSpectralPlaw(void);
64  GModelSpectralPlaw(const std::string& type,
65  const std::string& prefactor,
66  const std::string& index,
67  const std::string& pivot);
68  GModelSpectralPlaw(const double& prefactor,
69  const double& index,
70  const GEnergy& pivot);
71  explicit GModelSpectralPlaw(const GXmlElement& xml);
73  virtual ~GModelSpectralPlaw(void);
74 
75  // Operators
76  virtual GModelSpectralPlaw& operator=(const GModelSpectralPlaw& model);
77 
78  // Implemented pure virtual methods
79  virtual void clear(void);
80  virtual GModelSpectralPlaw* clone(void) const;
81  virtual std::string classname(void) const;
82  virtual std::string type(void) const;
83  virtual double eval(const GEnergy& srcEng,
84  const GTime& srcTime = GTime(),
85  const bool& gradients = false) const;
86  virtual double flux(const GEnergy& emin,
87  const GEnergy& emax) const;
88  virtual double eflux(const GEnergy& emin,
89  const GEnergy& emax) const;
90  virtual GEnergy mc(const GEnergy& emin,
91  const GEnergy& emax,
92  const GTime& time,
93  GRan& ran) const;
94  virtual void read(const GXmlElement& xml);
95  virtual void write(GXmlElement& xml) const;
96  virtual std::string print(const GChatter& chatter = NORMAL) const;
97 
98  // Other methods
99  void type(const std::string& type);
100  double prefactor(void) const;
101  double index(void) const;
102  GEnergy pivot(void) const;
103  void prefactor(const double& prefactor);
104  void index(const double& index);
105  void pivot(const GEnergy& pivot);
106 
107 protected:
108  // Protected methods
109  void init_members(void);
110  void copy_members(const GModelSpectralPlaw& model);
111  void free_members(void);
112  void update_eval_cache(const GEnergy& energy) const;
113  void update_mc_cache(const GEnergy& emin, const GEnergy& emax) const;
114 
115  // Protected members
116  std::string m_type; //!< Model type
117  GModelPar m_norm; //!< Normalization factor
118  GModelPar m_index; //!< Spectral index
119  GModelPar m_pivot; //!< Pivot energy
120 
121  // Cached members used for pre-computations
122  mutable GEnergy m_last_energy; //!< Last energy value
123  mutable double m_last_norm; //!< Last norm parameter
124  mutable double m_last_index; //!< Last index parameter
125  mutable double m_last_pivot; //!< Last pivot parameter
126  mutable double m_last_e_norm; //!< Last E/Epivot value
127  mutable double m_last_log_e_norm; //!< Last ln(E/Epivot) value
128  mutable double m_last_power; //!< Last power value
129  mutable double m_mc_emin; //!< Minimum energy
130  mutable double m_mc_emax; //!< Maximum energy
131  mutable double m_mc_exponent; //!< Exponent (index+1)
132  mutable double m_mc_pow_emin; //!< Power of minimum energy
133  mutable double m_mc_pow_ewidth; //!< Power of energy width
134 };
135 
136 
137 /***********************************************************************//**
138  * @brief Return class name
139  *
140  * @return String containing the class name ("GModelSpectralPlaw").
141  ***************************************************************************/
142 inline
143 std::string GModelSpectralPlaw::classname(void) const
144 {
145  return ("GModelSpectralPlaw");
146 }
147 
148 
149 /***********************************************************************//**
150  * @brief Return model type
151  *
152  * @return Model type.
153  *
154  * Returns the type of the spectral power law model.
155  ***************************************************************************/
156 inline
157 std::string GModelSpectralPlaw::type(void) const
158 {
159  return (m_type);
160 }
161 
162 
163 /***********************************************************************//**
164  * @brief Set model type
165  *
166  * @param[in] type Model type.
167  *
168  * Set the type of the spectral power law model.
169  ***************************************************************************/
170 inline
171 void GModelSpectralPlaw::type(const std::string& type)
172 {
173  m_type = type;
174  return;
175 }
176 
177 
178 /***********************************************************************//**
179  * @brief Return pre factor
180  *
181  * @return Pre factor (ph/cm2/s/MeV).
182  *
183  * Returns the pre factor.
184  ***************************************************************************/
185 inline
187 {
188  return (m_norm.value());
189 }
190 
191 
192 /***********************************************************************//**
193  * @brief Set pre factor
194  *
195  * @param[in] prefactor Pre factor (ph/cm2/s/MeV).
196  *
197  * Sets the pre factor.
198  ***************************************************************************/
199 inline
200 void GModelSpectralPlaw::prefactor(const double& prefactor)
201 {
202  m_norm.value(prefactor);
203  return;
204 }
205 
206 
207 /***********************************************************************//**
208  * @brief Return power law index
209  *
210  * @return Power law index.
211  *
212  * Returns the power law index.
213  ***************************************************************************/
214 inline
215 double GModelSpectralPlaw::index(void) const
216 {
217  return (m_index.value());
218 }
219 
220 
221 /***********************************************************************//**
222  * @brief Set power law index
223  *
224  * @param[in] index Power law index.
225  *
226  * Sets the power law index.
227  ***************************************************************************/
228 inline
229 void GModelSpectralPlaw::index(const double& index)
230 {
231  m_index.value(index);
232  return;
233 }
234 
235 
236 /***********************************************************************//**
237  * @brief Return pivot energy
238  *
239  * @return Pivot energy.
240  *
241  * Returns the pivot energy.
242  ***************************************************************************/
243 inline
245 {
246  GEnergy energy;
247  energy.MeV(m_pivot.value());
248  return energy;
249 }
250 
251 
252 /***********************************************************************//**
253  * @brief Set pivot energy
254  *
255  * @param[in] pivot Pivot energy.
256  *
257  * Sets the pivot energy.
258  ***************************************************************************/
259 inline
261 {
262  m_pivot.value(pivot.MeV());
263  return;
264 }
265 
266 #endif /* GMODELSPECTRALPLAW_HPP */
void update_eval_cache(const GEnergy &energy) const
Update eval precomputation cache.
virtual GModelSpectralPlaw & operator=(const GModelSpectralPlaw &model)
Assignment operator.
virtual GEnergy mc(const GEnergy &emin, const GEnergy &emax, const GTime &time, GRan &ran) const
Returns Monte Carlo energy between [emin, emax].
virtual ~GModelSpectralPlaw(void)
Destructor.
Energy value class definition.
Abstract spectral model base class.
std::string m_type
Model type.
double m_last_log_e_norm
Last ln(E/Epivot) value.
void update_mc_cache(const GEnergy &emin, const GEnergy &emax) const
Update Monte Carlo pre computation cache.
XML element node class.
Definition: GXmlElement.hpp:48
double m_last_index
Last index parameter.
Random number generator class.
Definition: GRan.hpp:44
double MeV(void) const
Return energy in MeV.
Definition: GEnergy.cpp:321
Time class.
Definition: GTime.hpp:55
Power law spectral model class.
double m_last_power
Last power value.
Model parameter class interface definition.
double m_mc_exponent
Exponent (index+1)
Model parameter class.
Definition: GModelPar.hpp:87
virtual void clear(void)
Clear spectral power law model.
virtual void read(const GXmlElement &xml)
Read model from XML element.
GEnergy pivot(void) const
Return pivot energy.
double m_last_e_norm
Last E/Epivot value.
double m_mc_emin
Minimum energy.
GModelSpectralPlaw(void)
Void constructor.
GChatter
Definition: GTypemaps.hpp:33
double prefactor(void) const
Return pre factor.
virtual GModelSpectralPlaw * clone(void) const
Clone spectral power law model.
void init_members(void)
Initialise class members.
virtual double eflux(const GEnergy &emin, const GEnergy &emax) const
Returns model energy flux between emin, emax
GEnergy m_last_energy
Last energy value.
Abstract spectral model base class interface definition.
virtual double flux(const GEnergy &emin, const GEnergy &emax) const
Returns model photon flux between emin, emax
double m_mc_pow_emin
Power of minimum energy.
void free_members(void)
Delete class members.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print powerlaw information.
double m_last_pivot
Last pivot parameter.
double value(void) const
Return parameter value.
GModelPar m_pivot
Pivot energy.
virtual std::string classname(void) const
Return class name.
virtual double eval(const GEnergy &srcEng, const GTime &srcTime=GTime(), const bool &gradients=false) const
Evaluate function.
double m_mc_pow_ewidth
Power of energy width.
double index(void) const
Return power law index.
double m_mc_emax
Maximum energy.
virtual std::string type(void) const
Return model type.
GModelPar m_norm
Normalization factor.
GModelPar m_index
Spectral index.
virtual void write(GXmlElement &xml) const
Write model into XML element.
double m_last_norm
Last norm parameter.
Class that handles energies in a unit independent way.
Definition: GEnergy.hpp:48
void copy_members(const GModelSpectralPlaw &model)
Copy class members.