GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GModelSpectralNodes.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GModelSpectralNodes.hpp - Spectral nodes model class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2012-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 GModelSpectralNodes.hpp
23  * @brief Spectral nodes model class definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GMODELSPECTRALNODES_HPP
28 #define GMODELSPECTRALNODES_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <vector>
32 #include <string>
33 #include "GModelSpectral.hpp"
34 #include "GModelPar.hpp"
35 #include "GNodeArray.hpp"
36 #include "GEnergy.hpp"
37 
38 /* __ Forward declarations _______________________________________________ */
39 class GRan;
40 class GTime;
41 class GEnergies;
42 class GXmlElement;
43 
44 
45 /***********************************************************************//**
46  * @class GModelSpectralNodes
47  *
48  * @brief Spectral nodes model class
49  *
50  * This class implements a piecewise power law between spectral nodes
51  *
52  * \f[
53  * ({\tt m\_energies[i]}, {\tt m\_values[i]})
54  * \f]
55  *
56  * where
57  * - \f${\tt m\_energies[i]}\f$ is the energy, and
58  * - \f${\tt m\_values[i]}\f$ is the intensity (in photons/cm2/s/MeV)
59  * of node \f$i\f$.
60  *
61  * For a given energy \f$E\f$, the piecewise power law is computing by
62  * finding the bracketing energies
63  * \f${\tt m\_energies[i]} <= E <= {\tt m\_energies[i+1]}\f$ and computing
64  *
65  * \f[
66  * S_{\rm E}(E | t) =
67  * 10^{(\log {\tt m\_values[i]}) w_{i} +
68  * (\log {\tt m\_values[i+1]}) w_{i+1}}
69  * \f]
70  *
71  * where
72  * - \f${\tt m\_values[i]}\f$ is the intensity of node \f$i\f$,
73  * - \f${\tt m\_values[i+1]}\f$ is the intensity of node \f$i+1\f$,
74  * - \f$w_{i}\f$ is the weighting of node \f$i\f$, and
75  * - \f$w_{i+1}\f$ is the weighting of node \f$i+1\f$.
76  *
77  * The weightings \f$w_{i}\f$ and \f$w_{i+1}\f$ are computed by linear
78  * interpolation (in the log-log plane) between the nodes
79  * \f$(\log {\tt m\_energies[i]}, \log{\tt m\_values[i]})\f$
80  * and
81  * \f$(\log {\tt m\_energies[i+1]}, \log{\tt m\_values[i+1]})\f$
82  * to the requested energy \f$\log E\f$.
83  ***************************************************************************/
85 
86 public:
87  // Constructors and destructors
88  GModelSpectralNodes(void);
89  GModelSpectralNodes(const GModelSpectral& model, const GEnergies& energies);
90  explicit GModelSpectralNodes(const GXmlElement& xml);
92  virtual ~GModelSpectralNodes(void);
93 
94  // Operators
95  virtual GModelSpectralNodes& operator=(const GModelSpectralNodes& model);
96 
97  // Implemented pure virtual base class methods
98  virtual void clear(void);
99  virtual GModelSpectralNodes* clone(void) const;
100  virtual std::string classname(void) const;
101  virtual std::string type(void) const;
102  virtual double eval(const GEnergy& srcEng,
103  const GTime& srcTime = GTime(),
104  const bool& gradients = false) const;
105  virtual double flux(const GEnergy& emin,
106  const GEnergy& emax) const;
107  virtual double eflux(const GEnergy& emin,
108  const GEnergy& emax) const;
109  virtual GEnergy mc(const GEnergy& emin,
110  const GEnergy& emax,
111  const GTime& time,
112  GRan& ran) const;
113  virtual void read(const GXmlElement& xml);
114  virtual void write(GXmlElement& xml) const;
115  virtual std::string print(const GChatter& chatter = NORMAL) const;
116 
117  // Other methods
118  int nodes(void) const;
119  void append(const GEnergy& energy, const double& intensity);
120  void insert(const int& index, const GEnergy& energy,
121  const double& intensity);
122  void remove(const int& index);
123  void reserve(const int& num);
124  void extend(const GModelSpectralNodes& nodes);
125  GEnergy energy(const int& index) const;
126  void energy(const int& index, const GEnergy& energy);
127  double intensity(const int& index) const;
128  void intensity(const int& index, const double& intensity);
129  double error(const int& index) const;
130 
131 protected:
132  // Protected methods
133  void init_members(void);
134  void copy_members(const GModelSpectralNodes& model);
135  void free_members(void);
136  void update_pars(void);
137  void set_cache(void) const;
138  void set_eval_cache(void) const;
139  void set_flux_cache(void) const;
140  void update_eval_cache(void) const;
141  void update_flux_cache(void) const;
142  void mc_update(const GEnergy& emin, const GEnergy& emax) const;
143 
144  // Protected members
145  std::vector<GModelPar> m_energies; //!< Node energies
146  std::vector<GModelPar> m_values; //!< Node values
147 
148  // Evaluation cache
149  mutable std::vector<double> m_old_energies; //!< Old energies
150  mutable std::vector<double> m_old_values; //!< Old values
151  mutable GNodeArray m_log_energies; //!< log10(energy) of nodes
152  mutable std::vector<double> m_log_values; //!< log10(value) of nodes
153 
154  // Flux computation cache
155  mutable GNodeArray m_lin_energies; //!< Energy of nodes
156  mutable std::vector<double> m_lin_values; //!< Values of nodes
157  mutable std::vector<double> m_prefactor; //!< Power-law normalisations
158  mutable std::vector<double> m_gamma; //!< Power-law indices
159  mutable std::vector<double> m_epivot; //!< Power-law pivot energies
160  mutable std::vector<double> m_flux; //!< Photon fluxes
161  mutable std::vector<double> m_eflux; //!< Energy fluxes
162 
163  // Cached members for MC
164  mutable GEnergy m_mc_emin; //!< Minimum energy
165  mutable GEnergy m_mc_emax; //!< Maximum energy
166  mutable std::vector<double> m_mc_cum; //!< Cumulative distribution
167  mutable std::vector<double> m_mc_min; //!< Lower boundary for MC
168  mutable std::vector<double> m_mc_max; //!< Upper boundary for MC
169  mutable std::vector<double> m_mc_exp; //!< Exponent for MC
170 };
171 
172 
173 /***********************************************************************//**
174  * @brief Return class name
175  *
176  * @return String containing the class name ("GModelSpectralNodes").
177  ***************************************************************************/
178 inline
179 std::string GModelSpectralNodes::classname(void) const
180 {
181  return ("GModelSpectralNodes");
182 }
183 
184 
185 /***********************************************************************//**
186  * @brief Return model type
187  *
188  * @return "NodeFunction".
189  *
190  * Returns the type of the spectral node function model.
191  ***************************************************************************/
192 inline
193 std::string GModelSpectralNodes::type(void) const
194 {
195  return "NodeFunction";
196 }
197 
198 
199 /***********************************************************************//**
200  * @brief Return number of nodes
201  *
202  * @return Number of nodes.
203  *
204  * Returns the number of nodes in the node function model.
205  ***************************************************************************/
206 inline
208 {
209  return (int)m_energies.size();
210 }
211 
212 #endif /* GMODELSPECTRALNODES_HPP */
std::vector< double > m_flux
Photon fluxes.
void update_flux_cache(void) const
Update flux computation cache.
virtual GEnergy mc(const GEnergy &emin, const GEnergy &emax, const GTime &time, GRan &ran) const
Returns MC energy between [emin, emax].
std::vector< double > m_eflux
Energy fluxes.
Node array class.
Definition: GNodeArray.hpp:60
virtual GModelSpectralNodes & operator=(const GModelSpectralNodes &model)
Assignment operator.
Energy value class definition.
Abstract spectral model base class.
virtual std::string type(void) const
Return model type.
void insert(const int &index, const GEnergy &energy, const double &intensity)
Insert node.
void extend(const GModelSpectralNodes &nodes)
Append nodes from node function.
std::vector< double > m_mc_max
Upper boundary for MC.
std::vector< double > m_old_energies
Old energies.
XML element node class.
Definition: GXmlElement.hpp:48
Random number generator class.
Definition: GRan.hpp:44
virtual double flux(const GEnergy &emin, const GEnergy &emax) const
Returns model photon flux between emin, emax
virtual double eflux(const GEnergy &emin, const GEnergy &emax) const
Returns model energy flux between emin, emax
std::vector< double > m_epivot
Power-law pivot energies.
Time class.
Definition: GTime.hpp:55
std::vector< double > m_mc_cum
Cumulative distribution.
void reserve(const int &num)
Reserve space for nodes.
GNodeArray m_lin_energies
Energy of nodes.
Energy container class.
Definition: GEnergies.hpp:60
Spectral nodes model class.
std::vector< double > m_log_values
log10(value) of nodes
std::vector< GModelPar > m_values
Node values.
void set_flux_cache(void) const
Set flux computation cache.
Model parameter class interface definition.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print node function information.
void append(const GEnergy &energy, const double &intensity)
Append node.
Node array class interface definition.
virtual GModelSpectralNodes * clone(void) const
Clone spectral nodes model.
void copy_members(const GModelSpectralNodes &model)
Copy class members.
virtual void write(GXmlElement &xml) const
Write model into XML element.
virtual double eval(const GEnergy &srcEng, const GTime &srcTime=GTime(), const bool &gradients=false) const
Evaluate function.
GModelSpectralNodes(void)
Void constructor.
virtual ~GModelSpectralNodes(void)
Destructor.
std::vector< double > m_mc_exp
Exponent for MC.
void update_pars(void)
Update parameter mapping.
void set_eval_cache(void) const
Set evaluation cache.
GChatter
Definition: GTypemaps.hpp:33
GEnergy m_mc_emax
Maximum energy.
GEnergy energy(const int &index) const
Return node energy.
std::vector< double > m_lin_values
Values of nodes.
virtual std::string classname(void) const
Return class name.
Abstract spectral model base class interface definition.
double intensity(const int &index) const
Return node intensity.
std::vector< double > m_mc_min
Lower boundary for MC.
void free_members(void)
Delete class members.
double error(const int &index) const
Return intensity error of node.
void update_eval_cache(void) const
Update evaluation cache.
virtual void clear(void)
Clear spectral nodes model.
std::vector< GModelPar > m_energies
Node energies.
void init_members(void)
Initialise class members.
int nodes(void) const
Return number of nodes.
std::vector< double > m_prefactor
Power-law normalisations.
GEnergy m_mc_emin
Minimum energy.
virtual void read(const GXmlElement &xml)
Read model from XML element.
void mc_update(const GEnergy &emin, const GEnergy &emax) const
Set MC pre-computation cache.
void set_cache(void) const
Set pre-computation cache.
std::vector< double > m_old_values
Old values.
Class that handles energies in a unit independent way.
Definition: GEnergy.hpp:48
std::vector< double > m_gamma
Power-law indices.
GNodeArray m_log_energies
log10(energy) of nodes