GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GModelSpectralMultiplicative.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GModelSpectralMultiplicative.hpp - Multiplicative spectral model class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2016-2017 by Michael Mayer *
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 GModelSpectralMultiplicative.hpp
23  * @brief Multiplicative spectral model class interface definition
24  * @author Michael Mayer
25  */
26 
27 #ifndef GMODELSPECTRALMULTIPLICATIVE_HPP
28 #define GMODELSPECTRALMULTIPLICATIVE_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include <vector>
33 #include "GModelSpectral.hpp"
34 #include "GEnergy.hpp"
35 #include "GModelSpectralNodes.hpp"
36 #include "GFunction.hpp"
37 
38 /* __ Forward declarations _______________________________________________ */
39 class GRan;
40 class GTime;
41 class GXmlElement;
42 
43 
44 /***********************************************************************//**
45  * @class GModelSpectralMultiplicative
46  *
47  * @brief Multiplicative spectral model class
48  *
49  * This class implements a spectral model that is a multiplication spectral
50  * model components. The spectral model components can be defined in an
51  * XML file, or added using the append() method.
52  ***************************************************************************/
54 
55 public:
56  // Constructors and destructors
58  explicit GModelSpectralMultiplicative(const GXmlElement& xml);
60  virtual ~GModelSpectralMultiplicative(void);
61 
62  // Operators
64 
65  // Implemented pure virtual methods
66  virtual void clear(void);
67  virtual GModelSpectralMultiplicative* clone(void) const;
68  virtual std::string classname(void) const;
69  virtual std::string type(void) const;
70  virtual double eval(const GEnergy& srcEng,
71  const GTime& srcTime = GTime(),
72  const bool& gradients = false) const;
73  virtual double flux(const GEnergy& emin,
74  const GEnergy& emax) const;
75  virtual double eflux(const GEnergy& emin,
76  const GEnergy& emax) const;
77  virtual GEnergy mc(const GEnergy& emin,
78  const GEnergy& emax,
79  const GTime& time,
80  GRan& ran) const;
81  virtual void read(const GXmlElement& xml);
82  virtual void write(GXmlElement& xml) const;
83  virtual std::string print(const GChatter& chatter = NORMAL) const;
84 
85  // Other methods
86  void append(const GModelSpectral& spec, const std::string& name="");
87  int components(void) const;
88  const GModelSpectral* component(const int& index) const;
89  const GModelSpectral* component(const std::string& name) const;
90 
91 protected:
92  // Protected methods
93  void init_members(void);
94  void copy_members(const GModelSpectralMultiplicative& model);
95  void free_members(void);
96  void update_mc_cache(const GEnergy& emin, const GEnergy& emax) const;
97 
98  // Class to determine the integral photon flux
99  class flux_kern : public GFunction {
100  public:
101  // Constructor
102  flux_kern(std::vector<GModelSpectral*> spec) : m_spec(spec) {}
103 
104  // Method
105  double eval(const double& x) {
106  GEnergy energy(x, "MeV");
107  double value = m_spec[0]->eval(energy);
108  for (size_t i = 1 ; i < m_spec.size(); ++i) {
109  value *= m_spec[i]->eval(energy);
110  }
111  return value;
112  }
113 
114  protected:
115  // Protected members
116  std::vector<GModelSpectral*> m_spec; //!< Spectral models
117  };
118 
119  // Class to determine the integral energy flux, derived from flux_kern
120  class eflux_kern : public flux_kern {
121  public:
122  // Constructor
123  eflux_kern(std::vector<GModelSpectral*> spec) : flux_kern(spec) {}
124 
125  // Method
126  double eval(const double& x) {
127  return x * flux_kern::eval(x);
128  }
129  };
130 
131  // Protected members
132  std::string m_type; //!< Model type
133  std::vector<GModelSpectral*> m_spectral; //!< Container of spectral models
134  std::vector<std::string> m_components; //!< Names of components
135 
136  // MC cache
137  mutable GModelSpectralNodes m_mc_spectrum; //!< MC spectrum cache
138  mutable GEnergy m_mc_emin; //!< Last minimum energy
139  mutable GEnergy m_mc_emax; //!< Last maximum energy
140  mutable std::vector<double> m_mc_values; //!< Parameter values
141 };
142 
143 
144 /***********************************************************************//**
145  * @brief Return class name
146  *
147  * @return String containing the class name ("GModelSpectralMultiplicative").
148  ***************************************************************************/
149 inline
151 {
152  return ("GModelSpectralMultiplicative");
153 }
154 
155 
156 /***********************************************************************//**
157  * @brief Return model type
158  *
159  * @return Model type.
160  *
161  * Returns the type of the spectral Multiplicative model.
162  ***************************************************************************/
163 inline
164 std::string GModelSpectralMultiplicative::type(void) const
165 {
166  return (m_type);
167 }
168 
169 
170 /***********************************************************************//**
171  * @brief Return number of spectral components
172  *
173  * @return Number of model components.
174  *
175  * Returns the number of spectral components.
176  ***************************************************************************/
177 inline
179 {
180  return (m_spectral.size());
181 }
182 
183 #endif /* GMODELSPECTRALMULTIPLICATIVE_HPP */
virtual void read(const GXmlElement &xml)
Read model from XML element.
std::vector< std::string > m_components
Names of components.
virtual std::string classname(void) const
Return class name.
Energy value class definition.
Abstract spectral model base class.
GModelSpectralMultiplicative(void)
Void constructor.
Multiplicative spectral model class.
eflux_kern(std::vector< GModelSpectral * > spec)
XML element node class.
Definition: GXmlElement.hpp:47
std::vector< GModelSpectral * > m_spectral
Container of spectral models.
flux_kern(std::vector< GModelSpectral * > spec)
GEnergy m_mc_emin
Last minimum energy.
Random number generator class.
Definition: GRan.hpp:44
Spectral nodes model class definition.
int components(void) const
Return number of spectral components.
virtual GModelSpectralMultiplicative & operator=(const GModelSpectralMultiplicative &model)
Assignment operator.
virtual ~GModelSpectralMultiplicative(void)
Destructor.
Time class.
Definition: GTime.hpp:54
const GModelSpectral * component(const int &index) const
Return spectral model component by index.
virtual GModelSpectralMultiplicative * clone(void) const
Clone multiplicative spectral model model.
std::vector< double > m_mc_values
Parameter values.
Spectral nodes model class.
virtual double eflux(const GEnergy &emin, const GEnergy &emax) const
Returns model energy flux between emin, emax
void copy_members(const GModelSpectralMultiplicative &model)
Copy class members.
Single parameter function abstract base class definition.
virtual double flux(const GEnergy &emin, const GEnergy &emax) const
Returns model photon flux between emin, emax
virtual GEnergy mc(const GEnergy &emin, const GEnergy &emax, const GTime &time, GRan &ran) const
Returns Monte Carlo energy between [emin, emax].
GChatter
Definition: GTypemaps.hpp:33
void append(const GModelSpectral &spec, const std::string &name="")
Append spectral component.
virtual std::string type(void) const
Return model type.
virtual void write(GXmlElement &xml) const
Write model into XML element.
virtual void clear(void)
Clear multiplicative spectral model.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print multiplicative spectral model information.
Abstract spectral model base class interface definition.
void update_mc_cache(const GEnergy &emin, const GEnergy &emax) const
Update Monte Carlo pre computation cache.
virtual double eval(const GEnergy &srcEng, const GTime &srcTime=GTime(), const bool &gradients=false) const
Evaluate function.
GEnergy m_mc_emax
Last maximum energy.
void free_members(void)
Delete class members.
std::vector< GModelSpectral * > m_spec
Spectral models.
Single parameter function abstract base class.
Definition: GFunction.hpp:44
GModelSpectralNodes m_mc_spectrum
MC spectrum cache.
void init_members(void)
Initialise class members.
Class that handles energies in a unit independent way.
Definition: GEnergy.hpp:48