GammaLib 2.0.0
Loading...
Searching...
No Matches
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 _______________________________________________ */
40class GRan;
41class GTime;
42class GEnergies;
43class 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
63public:
64 // Constructors and destructors
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
114protected:
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 ***************************************************************************/
153inline
154std::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 ***************************************************************************/
167inline
168std::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 ***************************************************************************/
181inline
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 ***************************************************************************/
195inline
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 ***************************************************************************/
209inline
210void 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 ***************************************************************************/
227inline
228double 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 ***************************************************************************/
241inline
243{
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 ***************************************************************************/
256inline
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 ***************************************************************************/
270inline
272{
274 return;
275}
276
277#endif /* GMODELSPECTRALFUNC_HPP */
Energy value class definition.
Filename class interface definition.
Model parameter class interface definition.
Abstract spectral model base class interface definition.
Node array class interface definition.
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
double norm(const GVector &vector)
Computes vector norm.
Definition GVector.cpp:864
Energy container class.
Definition GEnergies.hpp:60
Class that handles energies in a unit independent way.
Definition GEnergy.hpp:48
Filename class.
Definition GFilename.hpp:62
Model parameter class.
Definition GModelPar.hpp:87
Spectral function model class.
GModelPar m_norm
Normalization factor.
virtual void clear(void)
Clear file function.
GNodeArray m_log_nodes
log10(Energy) nodes of function
virtual double flux(const GEnergy &emin, const GEnergy &emax) const
Returns model photon flux between [emin, emax] (units: ph/cm2/s)
std::vector< double > m_flux
Photon fluxes.
int nodes(void) const
Return number of nodes in file function.
void init_members(void)
Initialise class members.
void save(const GFilename &filename, const bool &clobber=false) const
Save file function in ASCII file.
GEnergy energy(const int &index) const
Return energy of node.
virtual std::string type(void) const
Return model type.
virtual void read(const GXmlElement &xml)
Read model from XML element.
std::vector< double > m_eflux
Energy fluxes.
void set_cache(void) const
Set pre-computation cache.
void copy_members(const GModelSpectralFunc &model)
Copy class members.
void insert(const GEnergy &energy, const double &intensity)
Insert node into file function.
double intensity(const int &index) const
Return intensity of node.
std::vector< double > m_prefactor
Power-law normalisations.
virtual ~GModelSpectralFunc(void)
Destructor.
std::vector< double > m_mc_max
Upper boundary for MC.
virtual GModelSpectralFunc * clone(void) const
Clone file function.
virtual std::string classname(void) const
Return class name.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print file function information.
std::vector< double > m_lin_values
Function values at nodes.
virtual double eflux(const GEnergy &emin, const GEnergy &emax) const
Returns model energy flux between [emin, emax] (units: erg/cm2/s)
std::vector< double > m_mc_min
Lower boundary for MC.
void remove(const int &index)
Remove node from file function.
const GFilename & filename(void) const
Return file name.
void extend(const GModelSpectralFunc &filefct)
Append file function.
void load_nodes(const GFilename &filename)
Load nodes from file.
virtual double eval(const GEnergy &srcEng, const GTime &srcTime=GTime(), const bool &gradients=false) const
Evaluate function.
double norm(void) const
Return normalization factor.
std::vector< double > m_epivot
Power-law pivot energies.
void reserve(const int &num)
Reserves space for nodes in file function.
GEnergy m_mc_emax
Maximum energy.
std::vector< double > m_mc_cum
Cumulative distribution.
void mc_update(const GEnergy &emin, const GEnergy &emax) const
Set MC pre-computation cache.
std::vector< double > m_log_values
log10(Function) values at nodes
void append(const GEnergy &energy, const double &intensity)
Append node to file function.
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_gamma
Power-law indices.
GFilename m_filename
Name of file function.
GNodeArray m_lin_nodes
Energy nodes of function.
virtual GModelSpectralFunc & operator=(const GModelSpectralFunc &model)
Assignment operator.
std::vector< double > m_mc_exp
Exponent for MC.
virtual void write(GXmlElement &xml) const
Write model into XML element.
bool is_empty(void) const
Signals if there are nodes in file function.
void free_members(void)
Delete class members.
GEnergy m_mc_emin
Minimum energy.
GModelSpectralFunc(void)
Void constructor.
Abstract spectral model base class.
Node array class.
bool is_empty(void) const
Signals if there are no nodes in node array.
void reserve(const int &num)
Reserves space for nodes in node array.
int size(void) const
Return number of nodes in node array.
double value(void) const
Return parameter value.
Random number generator class.
Definition GRan.hpp:44
Time class.
Definition GTime.hpp:55
XML element node class.