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
302 double func = std::pow(10.0, arg);
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)
562 ? std::exp(std::log(u * (e_max - e_min) + e_min) /
m_mc_exp[inx])
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.";
672 std::string msg =
"Specified energy "+
energy.
print()+
" is not "
673 "positive. Please append only nodes with positive "
681 "not positive. Please append only nodes with positive "
718 if (energy_MeV <= 0.0) {
719 std::string msg =
"Specified energy "+
energy.
print()+
" is not "
720 "positive. Please append only nodes with positive "
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 "+
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()) {
966 "not positive. Please set only positive intensities.";
996 for (
int i = 0; i <
nodes(); ++i) {
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) {
1228 log10energy = std::log10(csv.
real(i,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) {
1239 log10value = std::log10(csv.
real(i,1));
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) {
1304 double epivot = std::sqrt(emin*emax);
1307 double gamma = std::log(fmin/fmax) / std::log(emin/emax);
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) {
Comma-separated values table class definition.
Energy container class definition.
Exception handler interface definition.
const GModelSpectralFunc g_spectral_func_seed
Spectral function model class definition.
Spectral model registry class definition.
Random number generator class definition.
double norm(const GVector &vector)
Computes vector norm.
Comma-separated values table class.
const int & nrows(void) const
Return number of rows.
double real(const int &row, const int &col) const
Get double precision value.
void save(const GFilename &filename, const std::string &sep=" ", const bool &clobber=false) const
Save CSV table.
const int & ncols(void) const
Return number of columns.
int size(void) const
Return number of energies in container.
Class that handles energies in a unit independent way.
double MeV(void) const
Return energy in MeV.
double log10MeV(void) const
Return log10 of energy in MeV.
std::string print(const GChatter &chatter=NORMAL) const
Print energy.
void clear(void)
Clear instance.
std::string url(void) const
Return Uniform Resource Locator (URL)
bool is_empty(void) const
Signal if filename is empty.
void clear(void)
Clear file name.
void write(GXmlElement &xml) const
Set or update parameter attributes in XML element.
void read(const GXmlElement &xml)
Extract parameter attributes from XML element.
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 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.
Interface definition for the spectral model registry class.
Abstract spectral model base class.
virtual double eval(const GEnergy &srcEng, const GTime &srcTime=GTime(), const bool &gradients=false) const =0
void free_members(void)
Delete class members.
virtual GModelSpectral & operator=(const GModelSpectral &model)
Assignment operator.
std::vector< GModelPar * > m_pars
Parameter pointers.
int size(void) const
Return number of parameters.
void init_members(void)
Initialise class members.
void set_value(const double &value) const
Set indices and weighting factors for interpolation.
double interpolate(const double &value, const std::vector< double > &vector) const
Interpolate value.
const int & inx_left(void) const
Returns left node index.
void remove(const int &index)
Remove one node into array.
void insert(const int &index, const double &node)
Insert one node into array.
void clear(void)
Clear node array.
void append(const double &node)
Append one node to array.
bool is_free(void) const
Signal if parameter is free.
void free(void)
Free a parameter.
const double & scale(void) const
Return parameter scale.
bool has_grad(void) const
Signal if parameter gradient is computed analytically.
void range(const double &min, const double &max)
Set minimum and maximum parameter boundaries.
const double & factor_gradient(void) const
Return parameter factor gradient.
double gradient(void) const
Return parameter gradient.
void clear(void)
Clear parameter.
double value(void) const
Return parameter value.
const std::string & name(void) const
Return parameter name.
Random number generator class.
double uniform(void)
Returns random double precision floating value in range 0 to 1.
const GXmlAttribute * attribute(const int &index) const
Return attribute.
void check_energy_interval(const std::string &origin, const GEnergy &emin, const GEnergy &emax)
Checks energy interval.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
bool is_infinite(const double &x)
Signal if argument is infinite.
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.
bool is_notanumber(const double &x)
Signal if argument is not a number.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
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.
GXmlElement * xml_need_par(const std::string &origin, GXmlElement &xml, const std::string &name)
Return pointer to parameter with given name in XML element.
GFilename xml_file_reduce(const GXmlElement &xml, const std::string &filename)
Reduce file name provided for writing as XML attribute.
void xml_check_parnum(const std::string &origin, const GXmlElement &xml, const int &number)
Checks number of parameters.
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.
GFilename xml_file_expand(const GXmlElement &xml, const std::string &filename)
Expand file name provided as XML attribute for loading.
void xml_check_type(const std::string &origin, GXmlElement &xml, const std::string &type)
Checks the model type.