GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GModelSpectralFunc.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GModelSpectralFunc.hpp - Spectral function model class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2010-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 GModelSpectralFunc.hpp
23  * @brief Spectral function model class definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GMODELSPECTRALFUNC_HPP
28 #define GMODELSPECTRALFUNC_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <vector>
32 #include <string>
33 #include "GModelSpectral.hpp"
34 #include "GModelPar.hpp"
35 #include "GEnergy.hpp"
36 #include "GNodeArray.hpp"
37 #include "GFilename.hpp"
38 
39 /* __ Forward declarations _______________________________________________ */
40 class GRan;
41 class GTime;
42 class GEnergies;
43 class GXmlElement;
44 
45 
46 /***********************************************************************//**
47  * @class GModelSpectralFunc
48  *
49  * @brief Spectral function model class
50  *
51  * This class implements an arbitrary function as the spectral model
52  * component. The model is defined by
53  *
54  * \f[
55  * S_{\rm E}(E | t) = {\tt m\_norm}
56  * \f]
57  *
58  * where
59  * - \f${\tt m\_norm}\f$ is the normalization of the function.
60  ***************************************************************************/
62 
63 public:
64  // Constructors and destructors
65  GModelSpectralFunc(void);
66  GModelSpectralFunc(const GFilename& filename, const double& norm);
67  GModelSpectralFunc(const GModelSpectral& model, const GEnergies& energies);
68  explicit GModelSpectralFunc(const GXmlElement& xml);
70  virtual ~GModelSpectralFunc(void);
71 
72  // Operators
73  virtual GModelSpectralFunc& operator=(const GModelSpectralFunc& model);
74 
75  // Implemented pure virtual base class methods
76  virtual void clear(void);
77  virtual GModelSpectralFunc* clone(void) const;
78  virtual std::string classname(void) const;
79  virtual std::string type(void) const;
80  virtual double eval(const GEnergy& srcEng,
81  const GTime& srcTime = GTime(),
82  const bool& gradients = false) const;
83  virtual double flux(const GEnergy& emin,
84  const GEnergy& emax) const;
85  virtual double eflux(const GEnergy& emin,
86  const GEnergy& emax) const;
87  virtual GEnergy mc(const GEnergy& emin,
88  const GEnergy& emax,
89  const GTime& time,
90  GRan& ran) const;
91  virtual void read(const GXmlElement& xml);
92  virtual void write(GXmlElement& xml) const;
93  virtual std::string print(const GChatter& chatter = NORMAL) const;
94 
95  // Other methods
96  int nodes(void) const;
97  bool is_empty(void) const;
98  void append(const GEnergy& energy, const double& intensity);
99  void insert(const GEnergy& energy, const double& intensity);
100  void remove(const int& index);
101  void reserve(const int& num);
102  void extend(const GModelSpectralFunc& filefct);
103  GEnergy energy(const int& index) const;
104  void energy(const int& index, const GEnergy& energy);
105  double intensity(const int& index) const;
106  void intensity(const int& index, const double& intensity);
107  const GFilename& filename(void) const;
108  void filename(const GFilename& filename);
109  double norm(void) const;
110  void norm(const double& norm);
111  void save(const GFilename& filename,
112  const bool& clobber = false) const;
113 
114 protected:
115  // Protected methods
116  void init_members(void);
117  void copy_members(const GModelSpectralFunc& model);
118  void free_members(void);
119  void load_nodes(const GFilename& filename);
120  void set_cache(void) const;
121  void mc_update(const GEnergy& emin, const GEnergy& emax) const;
122 
123  // Protected members
124  GModelPar m_norm; //!< Normalization factor
125  mutable GNodeArray m_lin_nodes; //!< Energy nodes of function
126  mutable GNodeArray m_log_nodes; //!< log10(Energy) nodes of function
127  std::vector<double> m_lin_values; //!< Function values at nodes
128  std::vector<double> m_log_values; //!< log10(Function) values at nodes
129  mutable GFilename m_filename; //!< Name of file function
130 
131  // Cached members used for pre-computations
132  mutable std::vector<double> m_prefactor; //!< Power-law normalisations
133  mutable std::vector<double> m_gamma; //!< Power-law indices
134  mutable std::vector<double> m_epivot; //!< Power-law pivot energies
135  mutable std::vector<double> m_flux; //!< Photon fluxes
136  mutable std::vector<double> m_eflux; //!< Energy fluxes
137 
138  // Cached members for MC
139  mutable GEnergy m_mc_emin; //!< Minimum energy
140  mutable GEnergy m_mc_emax; //!< Maximum energy
141  mutable std::vector<double> m_mc_cum; //!< Cumulative distribution
142  mutable std::vector<double> m_mc_min; //!< Lower boundary for MC
143  mutable std::vector<double> m_mc_max; //!< Upper boundary for MC
144  mutable std::vector<double> m_mc_exp; //!< Exponent for MC
145 };
146 
147 
148 /***********************************************************************//**
149  * @brief Return class name
150  *
151  * @return String containing the class name ("GModelSpectralFunc").
152  ***************************************************************************/
153 inline
154 std::string GModelSpectralFunc::classname(void) const
155 {
156  return ("GModelSpectralFunc");
157 }
158 
159 
160 /***********************************************************************//**
161  * @brief Return model type
162  *
163  * @return "FileFunction".
164  *
165  * Returns the type of the spectral function model.
166  ***************************************************************************/
167 inline
168 std::string GModelSpectralFunc::type(void) const
169 {
170  return "FileFunction";
171 }
172 
173 
174 /***********************************************************************//**
175  * @brief Return number of nodes in file function
176  *
177  * @return Number of nodes in file function.
178  *
179  * Returns the number of nodes in the file function model.
180  ***************************************************************************/
181 inline
183 {
184  return (int)m_lin_nodes.size();
185 }
186 
187 
188 /***********************************************************************//**
189  * @brief Signals if there are nodes in file function
190  *
191  * @return True if file function is empty, false otherwise.
192  *
193  * Signals if the file function does not contain any node.
194  ***************************************************************************/
195 inline
197 {
198  return (m_lin_nodes.is_empty());
199 }
200 
201 
202 /***********************************************************************//**
203  * @brief Reserves space for nodes in file function
204  *
205  * @param[in] num Number of nodes
206  *
207  * Reserves space for @p num nodes in file function.
208  ***************************************************************************/
209 inline
210 void GModelSpectralFunc::reserve(const int& num)
211 {
212  m_lin_nodes.reserve(num);
213  m_log_nodes.reserve(num);
214  m_lin_values.reserve(num);
215  m_log_values.reserve(num);
216  return;
217 }
218 
219 
220 /***********************************************************************//**
221  * @brief Return normalization factor
222  *
223  * @return Normalization factor.
224  *
225  * Returns the normalization factor.
226  ***************************************************************************/
227 inline
228 double GModelSpectralFunc::norm(void) const
229 {
230  return (m_norm.value());
231 }
232 
233 
234 /***********************************************************************//**
235  * @brief Set normalization factor
236  *
237  * @param[in] norm Normalization factor.
238  *
239  * Sets the normalization factor.
240  ***************************************************************************/
241 inline
242 void GModelSpectralFunc::norm(const double& norm)
243 {
244  m_norm.value(norm);
245  return;
246 }
247 
248 
249 /***********************************************************************//**
250  * @brief Return file name
251  *
252  * @return Name of node file.
253  *
254  * Returns the name of the file function node file.
255  ***************************************************************************/
256 inline
258 {
259  return (m_filename);
260 }
261 
262 
263 /***********************************************************************//**
264  * @brief Loads nodes from node file and set filename
265  *
266  * @param[in] filename Node file name.
267  *
268  * Loads the nodes from a file function node file and sets the filename.
269  ***************************************************************************/
270 inline
272 {
273  load_nodes(filename);
274  return;
275 }
276 
277 #endif /* GMODELSPECTRALFUNC_HPP */
std::vector< double > m_gamma
Power-law indices.
void mc_update(const GEnergy &emin, const GEnergy &emax) const
Set MC pre-computation cache.
int nodes(void) const
Return number of nodes in file function.
GEnergy m_mc_emax
Maximum energy.
int size(void) const
Return number of nodes in node array.
Definition: GNodeArray.hpp:192
virtual double flux(const GEnergy &emin, const GEnergy &emax) const
Returns model photon flux between emin, emax
std::vector< double > m_lin_values
Function values at nodes.
double norm(const GVector &vector)
Computes vector norm.
Definition: GVector.cpp:864
Node array class.
Definition: GNodeArray.hpp:60
Energy value class definition.
Abstract spectral model base class.
bool is_empty(void) const
Signals if there are nodes in file function.
std::vector< double > m_mc_max
Upper boundary for MC.
std::vector< double > m_mc_exp
Exponent for MC.
XML element node class.
Definition: GXmlElement.hpp:48
GEnergy energy(const int &index) const
Return energy of node.
virtual void read(const GXmlElement &xml)
Read model from XML element.
void reserve(const int &num)
Reserves space for nodes in node array.
Definition: GNodeArray.hpp:220
Random number generator class.
Definition: GRan.hpp:44
Time class.
Definition: GTime.hpp:55
void load_nodes(const GFilename &filename)
Load nodes from file.
void free_members(void)
Delete class members.
const GFilename & filename(void) const
Return file name.
Energy container class.
Definition: GEnergies.hpp:60
std::vector< double > m_log_values
log10(Function) values at nodes
virtual void write(GXmlElement &xml) const
Write model into XML element.
virtual std::string classname(void) const
Return class name.
bool is_empty(void) const
Signals if there are no nodes in node array.
Definition: GNodeArray.hpp:206
Model parameter class interface definition.
GFilename m_filename
Name of file function.
Model parameter class.
Definition: GModelPar.hpp:87
virtual ~GModelSpectralFunc(void)
Destructor.
std::vector< double > m_eflux
Energy fluxes.
void insert(const GEnergy &energy, const double &intensity)
Insert node into file function.
Node array class interface definition.
void save(const GFilename &filename, const bool &clobber=false) const
Save file function in ASCII file.
void reserve(const int &num)
Reserves space for nodes in file function.
std::vector< double > m_mc_min
Lower boundary for MC.
GEnergy m_mc_emin
Minimum energy.
virtual void clear(void)
Clear file function.
Filename class.
Definition: GFilename.hpp:62
void set_cache(void) const
Set pre-computation cache.
GNodeArray m_log_nodes
log10(Energy) nodes of function
GModelPar m_norm
Normalization factor.
virtual double eflux(const GEnergy &emin, const GEnergy &emax) const
Returns model energy flux between emin, emax
GChatter
Definition: GTypemaps.hpp:33
void copy_members(const GModelSpectralFunc &model)
Copy class members.
virtual std::string type(void) const
Return model type.
std::vector< double > m_epivot
Power-law pivot energies.
Abstract spectral model base class interface definition.
double intensity(const int &index) const
Return intensity of node.
Spectral function model class.
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_mc_cum
Cumulative distribution.
virtual GModelSpectralFunc * clone(void) const
Clone file function.
double value(void) const
Return parameter value.
std::vector< double > m_prefactor
Power-law normalisations.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print file function information.
void init_members(void)
Initialise class members.
GModelSpectralFunc(void)
Void constructor.
GNodeArray m_lin_nodes
Energy nodes of function.
void extend(const GModelSpectralFunc &filefct)
Append file function.
void append(const GEnergy &energy, const double &intensity)
Append node to file function.
virtual GModelSpectralFunc & operator=(const GModelSpectralFunc &model)
Assignment operator.
double norm(void) const
Return normalization factor.
virtual double eval(const GEnergy &srcEng, const GTime &srcTime=GTime(), const bool &gradients=false) const
Evaluate function.
Filename class interface definition.
Class that handles energies in a unit independent way.
Definition: GEnergy.hpp:48
std::vector< double > m_flux
Photon fluxes.