Commit b84e41a8e764ed6aa1324c00aeb8b7639fe23345
1 parent
94cf8055
Implements GModelSpectralGauss eval_gradients method and changes the GModelSpect…
…ralGauss mc method as suggested
Showing
2 changed files
with
4 additions
and
73 deletions
include/GModelSpectralGauss.hpp
| @@ -127,7 +127,6 @@ protected: | @@ -127,7 +127,6 @@ protected: | ||
| 127 | GModelPar m_mean; //!< Gaussian mean energy | 127 | GModelPar m_mean; //!< Gaussian mean energy |
| 128 | GModelPar m_sigma; //!< Gaussian energy width | 128 | GModelPar m_sigma; //!< Gaussian energy width |
| 129 | 129 | ||
| 130 | - // Cached members used for pre-computations | ||
| 131 | mutable double m_last_norm; //!< Last Normalization value | 130 | mutable double m_last_norm; //!< Last Normalization value |
| 132 | mutable double m_last_mean; //!< Last Mean value | 131 | mutable double m_last_mean; //!< Last Mean value |
| 133 | mutable double m_last_sigma; //!< Last Sigma value | 132 | mutable double m_last_sigma; //!< Last Sigma value |
| @@ -189,7 +188,7 @@ GEnergy GModelSpectralGauss::mean(void) const | @@ -189,7 +188,7 @@ GEnergy GModelSpectralGauss::mean(void) const | ||
| 189 | /***********************************************************************//** | 188 | /***********************************************************************//** |
| 190 | * @brief Set mean energy | 189 | * @brief Set mean energy |
| 191 | * | 190 | * |
| 192 | - * @param[in] mean Mean energy. | 191 | + * @param[in] Mean energy. |
| 193 | ***************************************************************************/ | 192 | ***************************************************************************/ |
| 194 | inline | 193 | inline |
| 195 | void GModelSpectralGauss::mean(const GEnergy& mean) | 194 | void GModelSpectralGauss::mean(const GEnergy& mean) |
| @@ -215,7 +214,7 @@ GEnergy GModelSpectralGauss::sigma(void) const | @@ -215,7 +214,7 @@ GEnergy GModelSpectralGauss::sigma(void) const | ||
| 215 | /***********************************************************************//** | 214 | /***********************************************************************//** |
| 216 | * @brief Set energy width | 215 | * @brief Set energy width |
| 217 | * | 216 | * |
| 218 | - * @param[in] sigma Energy width | 217 | + * @param[in] Energy width |
| 219 | ***************************************************************************/ | 218 | ***************************************************************************/ |
| 220 | inline | 219 | inline |
| 221 | void GModelSpectralGauss::sigma(const GEnergy& sigma) | 220 | void GModelSpectralGauss::sigma(const GEnergy& sigma) |
| @@ -224,4 +223,5 @@ void GModelSpectralGauss::sigma(const GEnergy& sigma) | @@ -224,4 +223,5 @@ void GModelSpectralGauss::sigma(const GEnergy& sigma) | ||
| 224 | return; | 223 | return; |
| 225 | } | 224 | } |
| 226 | 225 | ||
| 226 | + | ||
| 227 | #endif /* GMODELSPECTRALGAUSS_HPP */ | 227 | #endif /* GMODELSPECTRALGAUSS_HPP */ |
src/model/GModelSpectralGauss.cpp
| @@ -240,12 +240,7 @@ GModelSpectralGauss* GModelSpectralGauss::clone(void) const | @@ -240,12 +240,7 @@ GModelSpectralGauss* GModelSpectralGauss::clone(void) const | ||
| 240 | * Evaluates | 240 | * Evaluates |
| 241 | * | 241 | * |
| 242 | * \f[ | 242 | * \f[ |
| 243 | -<<<<<<< HEAD | ||
| 244 | - * \frac{dN}{dE} = \frac{\norm}{\sqrt{2\pi}\sigma} | ||
| 245 | - * \exp(\frac{(E-\bar{E})^2}{2\sigma^2}) | ||
| 246 | -======= | ||
| 247 | * \frac{dN}{dE}=\frac{\norm}{\sqrt{2\pi}\sigma}\exp(\frac{-(E-\bar{E})^2}{2\sigma^2}) | 243 | * \frac{dN}{dE}=\frac{\norm}{\sqrt{2\pi}\sigma}\exp(\frac{-(E-\bar{E})^2}{2\sigma^2}) |
| 248 | ->>>>>>> Implements eval_gradients in GModelSpectralGauss - not yet checked | ||
| 249 | * \f] | 244 | * \f] |
| 250 | ***************************************************************************/ | 245 | ***************************************************************************/ |
| 251 | double GModelSpectralGauss::eval(const GEnergy& srcEng, | 246 | double GModelSpectralGauss::eval(const GEnergy& srcEng, |
| @@ -276,11 +271,7 @@ double GModelSpectralGauss::eval(const GEnergy& srcEng, | @@ -276,11 +271,7 @@ double GModelSpectralGauss::eval(const GEnergy& srcEng, | ||
| 276 | * This method simply calls the eval() method as no analytical gradients will | 271 | * This method simply calls the eval() method as no analytical gradients will |
| 277 | * be computed. See the eval() method for details. | 272 | * be computed. See the eval() method for details. |
| 278 | * | 273 | * |
| 279 | -<<<<<<< HEAD | ||
| 280 | * TODO: update docstring. | 274 | * TODO: update docstring. |
| 281 | -======= | ||
| 282 | - * TODO: Implement this | ||
| 283 | ->>>>>>> efa5ea566f0bf64aca32f7e22a66761cbccf2f80 | ||
| 284 | ***************************************************************************/ | 275 | ***************************************************************************/ |
| 285 | double GModelSpectralGauss::eval_gradients(const GEnergy& srcEng, | 276 | double GModelSpectralGauss::eval_gradients(const GEnergy& srcEng, |
| 286 | const GTime& srcTime) | 277 | const GTime& srcTime) |
| @@ -293,7 +284,6 @@ double GModelSpectralGauss::eval_gradients(const GEnergy& srcEng, | @@ -293,7 +284,6 @@ double GModelSpectralGauss::eval_gradients(const GEnergy& srcEng, | ||
| 293 | // Update the evaluation cache | 284 | // Update the evaluation cache |
| 294 | update_eval_cache(srcEng); | 285 | update_eval_cache(srcEng); |
| 295 | 286 | ||
| 296 | -<<<<<<< HEAD | ||
| 297 | // Compute function terms | 287 | // Compute function terms |
| 298 | double term1 = (norm / sigma) * gammalib::inv_sqrt2pi; | 288 | double term1 = (norm / sigma) * gammalib::inv_sqrt2pi; |
| 299 | double term2 = (1 / sigma) * gammalib::inv_sqrt2pi; | 289 | double term2 = (1 / sigma) * gammalib::inv_sqrt2pi; |
| @@ -313,24 +303,6 @@ double GModelSpectralGauss::eval_gradients(const GEnergy& srcEng, | @@ -313,24 +303,6 @@ double GModelSpectralGauss::eval_gradients(const GEnergy& srcEng, | ||
| 313 | double g_sigma = - term5 * std::exp(- term3) * (1 - (2 * term3)); | 303 | double g_sigma = - term5 * std::exp(- term3) * (1 - (2 * term3)); |
| 314 | 304 | ||
| 315 | // Set gradients | 305 | // Set gradients |
| 316 | -======= | ||
| 317 | - // Compute function value | ||
| 318 | - double term1 = (norm / sigma) * gammalib::inv_sqrt2pi; | ||
| 319 | - double term2 = (energy - mean) * (energy - mean) / (2 * sigma * sigma); | ||
| 320 | - double value = term1 * std::exp(- term2); | ||
| 321 | - | ||
| 322 | - // Compute partial derivatives with respect to the parameter factor | ||
| 323 | - // values. The partial derivatives with respect to the parameter | ||
| 324 | - // values are obtained by division by the scale factor. | ||
| 325 | - double g_norm = (m_norm.is_free()) | ||
| 326 | - ? m_norm.scale() * m_last_power : 0.0; | ||
| 327 | - double g_mean = (m_index.is_free()) | ||
| 328 | - ? value * m_index.scale() * std::log(m_last_e_norm) : 0.0; | ||
| 329 | - double g_sigma = (m_ecut.is_free()) | ||
| 330 | - ? value * m_last_e_cut / m_ecut.factor_value() : 0.0; | ||
| 331 | - | ||
| 332 | - // Set gradients | ||
| 333 | ->>>>>>> efa5ea566f0bf64aca32f7e22a66761cbccf2f80 | ||
| 334 | m_norm.factor_gradient(g_norm); | 306 | m_norm.factor_gradient(g_norm); |
| 335 | m_mean.factor_gradient(g_mean); | 307 | m_mean.factor_gradient(g_mean); |
| 336 | m_sigma.factor_gradient(g_sigma); | 308 | m_sigma.factor_gradient(g_sigma); |
| @@ -338,24 +310,14 @@ double GModelSpectralGauss::eval_gradients(const GEnergy& srcEng, | @@ -338,24 +310,14 @@ double GModelSpectralGauss::eval_gradients(const GEnergy& srcEng, | ||
| 338 | // Compile option: Check for NaN/Inf | 310 | // Compile option: Check for NaN/Inf |
| 339 | #if defined(G_NAN_CHECK) | 311 | #if defined(G_NAN_CHECK) |
| 340 | if (gammalib::is_notanumber(value) || gammalib::is_infinite(value)) { | 312 | if (gammalib::is_notanumber(value) || gammalib::is_infinite(value)) { |
| 341 | -<<<<<<< HEAD | ||
| 342 | std::cout << "*** ERROR: GModelSpectralGauss::eval_gradients"; | 313 | std::cout << "*** ERROR: GModelSpectralGauss::eval_gradients"; |
| 343 | -======= | ||
| 344 | - std::cout << "*** ERROR: GModelSpectralExpPlaw::eval_gradients"; | ||
| 345 | ->>>>>>> efa5ea566f0bf64aca32f7e22a66761cbccf2f80 | ||
| 346 | std::cout << "(srcEng=" << srcEng; | 314 | std::cout << "(srcEng=" << srcEng; |
| 347 | std::cout << ", srcTime=" << srcTime << "):"; | 315 | std::cout << ", srcTime=" << srcTime << "):"; |
| 348 | std::cout << " NaN/Inf encountered"; | 316 | std::cout << " NaN/Inf encountered"; |
| 349 | std::cout << " (value=" << value; | 317 | std::cout << " (value=" << value; |
| 350 | -<<<<<<< HEAD | ||
| 351 | std::cout << ", norm=" << m_last_norm; | 318 | std::cout << ", norm=" << m_last_norm; |
| 352 | std::cout << ", mean=" << m_last_mean; | 319 | std::cout << ", mean=" << m_last_mean; |
| 353 | std::cout << ", Sigma=" << m_last_sigma; | 320 | std::cout << ", Sigma=" << m_last_sigma; |
| 354 | -======= | ||
| 355 | - std::cout << ", e_norm=" << m_last_e_norm; | ||
| 356 | - std::cout << ", e_cut=" << m_last_e_cut; | ||
| 357 | - std::cout << ", power=" << m_last_power; | ||
| 358 | ->>>>>>> efa5ea566f0bf64aca32f7e22a66761cbccf2f80 | ||
| 359 | std::cout << ")" << std::endl; | 321 | std::cout << ")" << std::endl; |
| 360 | } | 322 | } |
| 361 | #endif | 323 | #endif |
| @@ -385,6 +347,7 @@ double GModelSpectralGauss::eval_gradients(const GEnergy& srcEng, | @@ -385,6 +347,7 @@ double GModelSpectralGauss::eval_gradients(const GEnergy& srcEng, | ||
| 385 | * - \f$S_{\rm E}(E | t)\f$ is the spectral model (ph/cm2/s/MeV). | 347 | * - \f$S_{\rm E}(E | t)\f$ is the spectral model (ph/cm2/s/MeV). |
| 386 | * The integration is done analytically. | 348 | * The integration is done analytically. |
| 387 | ***************************************************************************/ | 349 | ***************************************************************************/ |
| 350 | + | ||
| 388 | double GModelSpectralGauss::flux(const GEnergy& emin, | 351 | double GModelSpectralGauss::flux(const GEnergy& emin, |
| 389 | const GEnergy& emax) const | 352 | const GEnergy& emax) const |
| 390 | { | 353 | { |
| @@ -525,7 +488,6 @@ GEnergy GModelSpectralGauss::mc(const GEnergy& emin, | @@ -525,7 +488,6 @@ GEnergy GModelSpectralGauss::mc(const GEnergy& emin, | ||
| 525 | return GEnergy(energy, "MeV"); | 488 | return GEnergy(energy, "MeV"); |
| 526 | } | 489 | } |
| 527 | 490 | ||
| 528 | - | ||
| 529 | /***********************************************************************//** | 491 | /***********************************************************************//** |
| 530 | * @brief Read model from XML element | 492 | * @brief Read model from XML element |
| 531 | * | 493 | * |
| @@ -595,7 +557,6 @@ void GModelSpectralGauss::read(const GXmlElement& xml) | @@ -595,7 +557,6 @@ void GModelSpectralGauss::read(const GXmlElement& xml) | ||
| 595 | return; | 557 | return; |
| 596 | } | 558 | } |
| 597 | 559 | ||
| 598 | - | ||
| 599 | /***********************************************************************//** | 560 | /***********************************************************************//** |
| 600 | * @brief Write model into XML element | 561 | * @brief Write model into XML element |
| 601 | * | 562 | * |
| @@ -834,16 +795,11 @@ double GModelSpectralGauss::eflux_kernel::eval(const double& energy) | @@ -834,16 +795,11 @@ double GModelSpectralGauss::eflux_kernel::eval(const double& energy) | ||
| 834 | * | 795 | * |
| 835 | * Updates the precomputation cache for eval() and eval_gradients() methods. | 796 | * Updates the precomputation cache for eval() and eval_gradients() methods. |
| 836 | * | 797 | * |
| 837 | -<<<<<<< HEAD | ||
| 838 | -======= | ||
| 839 | - * TODO integrate this to correct form | ||
| 840 | ->>>>>>> efa5ea566f0bf64aca32f7e22a66761cbccf2f80 | ||
| 841 | ***************************************************************************/ | 798 | ***************************************************************************/ |
| 842 | void GModelSpectralGauss::update_eval_cache(const GEnergy& energy) const | 799 | void GModelSpectralGauss::update_eval_cache(const GEnergy& energy) const |
| 843 | { | 800 | { |
| 844 | // Get parameter values (takes 3 multiplications which are difficult | 801 | // Get parameter values (takes 3 multiplications which are difficult |
| 845 | // to avoid) | 802 | // to avoid) |
| 846 | -<<<<<<< HEAD | ||
| 847 | double norm = m_norm.value(); | 803 | double norm = m_norm.value(); |
| 848 | double mean = m_mean.value(); | 804 | double mean = m_mean.value(); |
| 849 | double sigma = m_sigma.value(); | 805 | double sigma = m_sigma.value(); |
| @@ -864,31 +820,6 @@ void GModelSpectralGauss::update_eval_cache(const GEnergy& energy) const | @@ -864,31 +820,6 @@ void GModelSpectralGauss::update_eval_cache(const GEnergy& energy) const | ||
| 864 | m_last_norm = eng / m_last_norm; | 820 | m_last_norm = eng / m_last_norm; |
| 865 | m_last_mean = eng / m_last_mean; | 821 | m_last_mean = eng / m_last_mean; |
| 866 | m_last_sigma = eng/ m_last_sigma; | 822 | m_last_sigma = eng/ m_last_sigma; |
| 867 | -======= | ||
| 868 | - double index = m_index.value(); | ||
| 869 | - double ecut = m_ecut.value(); | ||
| 870 | - double pivot = m_pivot.value(); | ||
| 871 | - | ||
| 872 | - // If the energy or one of the parameters index, cut-off or pivot | ||
| 873 | - // energy has changed then recompute the cache | ||
| 874 | - if ((m_last_energy != energy) || | ||
| 875 | - (m_last_index != index) || | ||
| 876 | - (m_last_ecut != ecut) || | ||
| 877 | - (m_last_pivot != pivot)) { | ||
| 878 | - | ||
| 879 | - // Store actual energy and parameter values | ||
| 880 | - m_last_energy = energy; | ||
| 881 | - m_last_index = index; | ||
| 882 | - m_last_ecut = ecut; | ||
| 883 | - m_last_pivot = pivot; | ||
| 884 | - | ||
| 885 | - // Compute and store value | ||
| 886 | - double eng = energy.MeV(); | ||
| 887 | - m_last_e_norm = eng / m_last_pivot; | ||
| 888 | - m_last_e_cut = eng / m_last_ecut; | ||
| 889 | - m_last_power = std::pow(m_last_e_norm, m_last_index) * | ||
| 890 | - std::exp(-m_last_e_cut); | ||
| 891 | ->>>>>>> efa5ea566f0bf64aca32f7e22a66761cbccf2f80 | ||
| 892 | 823 | ||
| 893 | } // endif: recomputation was required | 824 | } // endif: recomputation was required |
| 894 | 825 |