47 #define G_FLUX "GModelSpectralFunc::flux(GEnergy&, GEnergy&)"
48 #define G_EFLUX "GModelSpectralFunc::eflux(GEnergy&, GEnergy&)"
49 #define G_MC "GModelSpectralFunc::mc(GEnergy&, GEnergy&, GTime&, GRan&)"
50 #define G_READ "GModelSpectralFunc::read(GXmlElement&)"
51 #define G_WRITE "GModelSpectralFunc::write(GXmlElement&)"
52 #define G_APPEND "GModelSpectralFunc::append(GEnergy&, double&)"
53 #define G_INSERT "GModelSpectralFunc::insert(GEnergy&, double&)"
54 #define G_REMOVE "GModelSpectralFunc::remove(int&)"
55 #define G_ENERGY1 "GModelSpectralFunc::energy(int&)"
56 #define G_ENERGY2 "GModelSpectralFunc::energy(int&, GEnergy&)"
57 #define G_INTENSITY1 "GModelSpectralFunc::intensity(int&)"
58 #define G_INTENSITY2 "GModelSpectralFunc::intensity(int&, double&)"
59 #define G_LOAD_NODES "GModelSpectralFunc::load_nodes(GFilename&)"
158 for (
int i = 0; i < energies.
size(); ++i) {
214 if (
this != &model) {
296 const GTime& srcTime,
297 const bool& gradients)
const
319 #if defined(G_NAN_CHECK)
321 std::cout <<
"*** ERROR: GModelSpectralFunc::eval";
322 std::cout <<
"(srcEng=" << srcEng;
323 std::cout <<
", srcTime=" << srcTime <<
"):";
324 std::cout <<
" NaN/Inf encountered";
325 std::cout <<
" (value=" << value;
326 std::cout <<
", norm=" <<
norm();
327 std::cout <<
", func=" << func;
328 std::cout <<
")" << std::endl;
363 double e_min = emin.
MeV();
364 double e_max = emax.
MeV();
377 if (inx_emin == inx_emax) {
392 int i_start = (e_min <
m_lin_nodes[0]) ? inx_emin : inx_emin+1;
402 for (
int i = i_start; i < inx_emax; ++i) {
451 double e_min = emin.
MeV();
452 double e_max = emax.
MeV();
465 if (inx_emin == inx_emax) {
481 int i_start = (e_min <
m_lin_nodes[0]) ? inx_emin : inx_emin+1;
492 for (
int i = i_start; i < inx_emax; ++i) {
549 for (inx =
m_mc_cum.size()-1; inx > 0; --inx) {
561 double eng = (u > 0.0)
570 double eng =
std::exp(u * (e_max - e_min) + e_min);
662 std::string msg =
"Specified energy "+energy.
print()+
" is not "
663 "larger than the energy "+last.
print()+
" of the "
664 "last node of the file function. Please append "
665 "nodes in increasing order of energy.";
671 if (energy.
MeV() <= 0.0) {
672 std::string msg =
"Specified energy "+energy.
print()+
" is not "
673 "positive. Please append only nodes with positive "
679 if (intensity <= 0.0) {
680 std::string msg =
"Specified intensity "+
gammalib::str(intensity)+
" is "
681 "not positive. Please append only nodes with positive "
715 double energy_MeV = energy.
MeV();
718 if (energy_MeV <= 0.0) {
719 std::string msg =
"Specified energy "+energy.
print()+
" is not "
720 "positive. Please append only nodes with positive "
726 if (intensity <= 0.0) {
727 std::string msg =
"Specified intensity "+
gammalib::str(intensity)+
" is "
728 "not positive. Please append only nodes with positive "
735 for (
int i = 0; i <
nodes(); ++i) {
737 std::string msg =
"A node with the specified energy "+
738 energy.
print()+
" exists already in the file "
739 "function. Please insert only nodes with "
740 "energies that do not yet exist.";
776 #if defined(G_RANGE_CHECK)
777 if (index < 0 || index >=
nodes()) {
809 int num = filefct.
nodes();
815 for (
int i = 0; i < num; ++i) {
842 #if defined(G_RANGE_CHECK)
843 if (index < 0 || index >=
nodes()) {
874 #if defined(G_RANGE_CHECK)
875 if (index < 0 || index >=
nodes()) {
884 std::string msg =
"Specified energy "+energy.
print()+
" is not "
885 "larger than energy "+precedent.
print()+
" of "
886 "precedent node. Please specify an energy that "
887 "is larger than "+precedent.
print()+
".";
893 if (index <
nodes()-1) {
896 std::string msg =
"Specified energy "+energy.
print()+
" is not "
897 "smaller than energy "+following.
print()+
" of "
898 "following node. Please specify an energy that "
899 "is smaller than "+following.
print()+
".";
930 #if defined(G_RANGE_CHECK)
931 if (index < 0 || index >=
nodes()) {
957 #if defined(G_RANGE_CHECK)
958 if (index < 0 || index >=
nodes()) {
964 if (intensity <= 0.0) {
965 std::string msg =
"Specified intensity "+
gammalib::str(intensity)+
" is "
966 "not positive. Please set only positive intensities.";
996 for (
int i = 0; i <
nodes(); ++i) {
1002 csv.
save(filename,
" ", clobber);
1027 result.append(
"=== GModelSpectralFunc ===");
1036 for (
int i = 0; i <
size(); ++i) {
1188 std::string msg =
"Empty file function ASCII file name specified. "
1189 "Please specify a valid file function ASCII file "
1201 if (csv.
nrows() < 2) {
1202 std::string msg =
"File function ASCII file \""+filename.
url()+
1204 " rows but at least 2 rows are required. Please "
1205 "specify a file function ASCII file with at "
1211 if (csv.
ncols() < 2) {
1212 std::string msg =
"File function ASCII file \""+filename.
url()+
1214 " columns but at least 2 columns are required. "
1215 "Please specify a file function ASCII file with "
1216 "at least two columns.";
1221 double last_energy = 0.0;
1222 for (
int i = 0; i < csv.
nrows(); ++i) {
1227 if (csv.
real(i,0) > 0.0) {
1231 std::string msg =
"Non-positive energy "+
1234 "function ASCII file. Please specify only "
1235 "positive energies in file function.";
1238 if (csv.
real(i,1) > 0.0) {
1242 std::string msg =
"Non-positive intensity "+
1245 "function ASCII file. Please specify only "
1246 "positive intensities in file function.";
1251 if (csv.
real(i,0) <= last_energy) {
1254 "function ASCII file is equal or smaller "
1255 "than preceding energy "+
1257 "monotonically increasing energies in the "
1258 "file function ASCII file.";
1269 last_energy = csv.
real(i,0);
1294 for (
int i = 0; i <
nodes()-1; ++i) {
1310 double prefactor = fmin /
std::pow(emin/epivot, gamma);
1313 double flux = prefactor *
1317 double eflux = prefactor *
1363 double e_min = emin.
MeV();
1364 double e_max = emax.
MeV();
1367 if (e_max > e_min) {
1382 if (inx_emin == inx_emax) {
1401 int i_start = (e_min <
m_lin_nodes[0]) ? inx_emin : inx_emin+1;
1415 for (
int i = i_start; i < inx_emax; ++i) {
1437 for (
int i = 1; i <
m_mc_cum.size(); ++i) {
1442 for (
int i = 0; i <
m_mc_cum.size(); ++i) {
1448 for (
int i = 0; i <
m_mc_cum.size(); ++i) {
1451 double exponent =
m_mc_exp[i] + 1.0;
1454 if (
std::abs(exponent) > 1.0e-11) {
1458 if (exponent < -50.0) {
1466 else if (exponent > +50.0) {
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.
void remove(const int &index)
Remove node from file function.
void save(const GFilename &filename, const std::string &sep=" ", const bool &clobber=false) const
Save CSV table.
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.
const double & factor_gradient(void) const
Return parameter factor gradient.
double norm(const GVector &vector)
Computes vector norm.
const std::string & name(void) const
Return parameter name.
Random number generator class definition.
Abstract spectral model base class.
bool is_empty(void) const
Signals if there are nodes in file function.
double gradient(void) const
Return parameter gradient.
std::vector< double > m_mc_max
Upper boundary for MC.
int size(void) const
Return number of parameters.
GVector abs(const GVector &vector)
Computes absolute of vector elements.
virtual GModelSpectral & operator=(const GModelSpectral &model)
Assignment operator.
void write(GXmlElement &xml) const
Set or update parameter attributes in XML element.
virtual double eval(const GEnergy &srcEng, const GTime &srcTime=GTime(), const bool &gradients=false) const =0
std::vector< GModelPar * > m_pars
Parameter pointers.
bool is_empty(void) const
Signal if filename is empty.
std::vector< double > m_mc_exp
Exponent for MC.
GEnergy energy(const int &index) const
Return energy of node.
virtual void read(const GXmlElement &xml)
Read model from XML element.
Spectral model registry class definition.
void clear(void)
Clear node array.
Random number generator class.
Comma-separated values table class.
double MeV(void) const
Return energy in MeV.
void load_nodes(const GFilename &filename)
Load nodes from file.
void free_members(void)
Delete class members.
void set_value(const double &value) const
Set indices and weighting factors for interpolation.
GFilename xml_file_reduce(const GXmlElement &xml, const std::string &filename)
Reduce file name provided for writing as XML attribute.
const GFilename & filename(void) const
Return file name.
double plaw_energy_flux(const double &emin, const double &emax, const double &epivot, const double &gamma)
Compute energy flux between two energies for a power law.
bool is_free(void) const
Signal if parameter is free.
std::vector< double > m_log_values
log10(Function) values at nodes
virtual void write(GXmlElement &xml) const
Write model into XML element.
double log10MeV(void) const
Return log10 of energy in MeV.
bool is_notanumber(const double &x)
Signal if argument is not a number.
const double & scale(void) const
Return parameter scale.
GFilename m_filename
Name of file function.
bool is_infinite(const double &x)
Signal if argument is infinite.
virtual ~GModelSpectralFunc(void)
Destructor.
const int & ncols(void) const
Return number of columns.
std::vector< double > m_eflux
Energy fluxes.
void insert(const GEnergy &energy, const double &intensity)
Insert node into file function.
GVector sqrt(const GVector &vector)
Computes square root of vector elements.
void remove(const int &index)
Remove one node into array.
bool has_grad(void) const
Signal if parameter gradient is computed analytically.
const GXmlAttribute * attribute(const int &index) const
Return attribute.
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::string print(const GChatter &chatter=NORMAL) const
Print energy.
std::vector< double > m_mc_min
Lower boundary for MC.
GEnergy m_mc_emin
Minimum energy.
void free(void)
Free a parameter.
virtual void clear(void)
Clear file function.
Energy container class definition.
void set_cache(void) const
Set pre-computation cache.
GNodeArray m_log_nodes
log10(Energy) nodes of function
Spectral function model class definition.
int size(void) const
Return number of energies in container.
GXmlElement * xml_need_par(const std::string &origin, GXmlElement &xml, const std::string &name)
Return pointer to parameter with given name in XML element.
GModelPar m_norm
Normalization factor.
GVector log(const GVector &vector)
Computes natural logarithm of vector elements.
double uniform(void)
Returns random double precision floating value in range 0 to 1.
void check_energy_interval(const std::string &origin, const GEnergy &emin, const GEnergy &emax)
Checks energy interval.
void clear(void)
Clear parameter.
virtual double eflux(const GEnergy &emin, const GEnergy &emax) const
Returns model energy flux between emin, emax
double interpolate(const double &value, const std::vector< double > &vector) const
Interpolate value.
const int & inx_left(void) const
Returns left node index.
const int & nrows(void) const
Return number of rows.
void copy_members(const GModelSpectralFunc &model)
Copy class members.
void xml_check_parnum(const std::string &origin, const GXmlElement &xml, const int &number)
Checks number of parameters.
Interface definition for the spectral model registry class.
virtual std::string type(void) const
Return model type.
std::vector< double > m_epivot
Power-law pivot energies.
double intensity(const int &index) const
Return intensity of node.
double real(const int &row, const int &col) const
Get double precision value.
Spectral function model class.
std::string url(void) const
Return Uniform Resource Locator (URL)
void clear(void)
Clear file name.
void init_members(void)
Initialise class members.
virtual GEnergy mc(const GEnergy &emin, const GEnergy &emax, const GTime &time, GRan &ran) const
Returns MC energy between [emin, emax].
void insert(const int &index, const double &node)
Insert one node into array.
std::vector< double > m_mc_cum
Cumulative distribution.
void range(const double &min, const double &max)
Set minimum and maximum parameter boundaries.
void read(const GXmlElement &xml)
Extract parameter attributes from XML element.
virtual GModelSpectralFunc * clone(void) const
Clone file function.
double value(void) const
Return parameter value.
GVector pow(const GVector &vector, const double &power)
Computes tanh of vector elements.
std::vector< double > m_prefactor
Power-law normalisations.
Exception handler interface definition.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print file function information.
void init_members(void)
Initialise class members.
GVector exp(const GVector &vector)
Computes exponential of vector elements.
GModelSpectralFunc(void)
Void constructor.
GNodeArray m_lin_nodes
Energy nodes of function.
void extend(const GModelSpectralFunc &filefct)
Append file function.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
void append(const double &node)
Append one node to array.
const GModelSpectralFunc g_spectral_func_seed
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.
const GXmlElement * xml_get_par(const std::string &origin, const GXmlElement &xml, const std::string &name)
Return pointer to parameter with given name in XML element.
void free_members(void)
Delete class members.
void clear(void)
Clear instance.
virtual double eval(const GEnergy &srcEng, const GTime &srcTime=GTime(), const bool &gradients=false) const
Evaluate function.
Comma-separated values table class definition.
GVector log10(const GVector &vector)
Computes base10 logarithm of vector elements.
Class that handles energies in a unit independent way.
double plaw_photon_flux(const double &emin, const double &emax, const double &epivot, const double &gamma)
Compute photon flux between two energies for a power law.
GFilename xml_file_expand(const GXmlElement &xml, const std::string &filename)
Expand file name provided as XML attribute for loading.
std::vector< double > m_flux
Photon fluxes.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
void xml_check_type(const std::string &origin, GXmlElement &xml, const std::string &type)
Checks the model type.