Commit 67eba5d73b60ea8348b72ae052334882eb0e091f

Authored by Jürgen Knödlseder
1 parent e64cb7de

Change model type from "PowerLaw2" to "PowerLawPhotonFlux" (#1818)

This is an anticipation of the renaming of the spectral models that will come in release 1.2.
include/GModelSpectralPlaw2.hpp
... ... @@ -76,6 +76,7 @@ class GModelSpectralPlaw2 : public GModelSpectral {
76 76 public:
77 77 // Constructors and destructors
78 78 GModelSpectralPlaw2(void);
  79 + GModelSpectralPlaw2(const bool& dummy, const std::string& type);
79 80 explicit GModelSpectralPlaw2(const double& integral,
80 81 const double& index,
81 82 const GEnergy& emin,
... ... @@ -126,6 +127,7 @@ protected:
126 127 void update(const GEnergy& srcEng) const;
127 128  
128 129 // Protected members
  130 + std::string m_type; //!< Model type
129 131 GModelPar m_integral; //!< Integral flux
130 132 GModelPar m_index; //!< Spectral index
131 133 GModelPar m_emin; //!< Lower energy limit (MeV)
... ... @@ -165,14 +167,14 @@ std::string GModelSpectralPlaw2::classname(void) const
165 167 /***********************************************************************//**
166 168 * @brief Return model type
167 169 *
168   - * @return "PowerLaw2".
  170 + * @return Model type.
169 171 *
170 172 * Returns the type of the spectral power law model.
171 173 ***************************************************************************/
172 174 inline
173 175 std::string GModelSpectralPlaw2::type(void) const
174 176 {
175   - return "PowerLaw2";
  177 + return (m_type);
176 178 }
177 179  
178 180  
... ...
inst/lat/test/data/p6v3/source_model.xml
... ... @@ -16,7 +16,7 @@
16 16 </spatialModel>
17 17 </source>
18 18 <source type="PointSource" name="Crab">
19   - <spectrum type="PowerLaw2">
  19 + <spectrum type="PowerLawPhotonFlux">
20 20 <parameter scale="1e-07" name="Integral" min="1e-05" max="1000.0" value="1.0" free="1"/>
21 21 <parameter scale="1.0" name="Index" min="-5.0" max="-0.1" value="-2.0" free="1"/>
22 22 <parameter scale="1.0" name="LowerLimit" min="10.0" max="1000000.0" value="100.0" free="0"/>
... ...
inst/lat/test/data/p7v6/source_model.xml
... ... @@ -16,7 +16,7 @@
16 16 </spatialModel>
17 17 </source>
18 18 <source type="PointSource" name="Crab">
19   - <spectrum type="PowerLaw2">
  19 + <spectrum type="PowerLawPhotonFlux">
20 20 <parameter scale="1e-07" name="Integral" min="1e-07" max="1000.0" value="1.0" free="1"/>
21 21 <parameter scale="1.0" name="Index" min="-5.0" max="+5.0" value="-2.0" free="1"/>
22 22 <parameter scale="1.0" name="LowerLimit" min="10.0" max="1000000.0" value="100.0" free="0"/>
... ...
inst/lat/test/data/p8v2/source_model.xml
... ... @@ -16,7 +16,7 @@
16 16 </spatialModel>
17 17 </source>
18 18 <source type="PointSource" name="Crab">
19   - <spectrum type="PowerLaw2">
  19 + <spectrum type="PowerLawPhotonFlux">
20 20 <parameter scale="1e-07" name="Integral" min="1e-07" max="1000.0" value="1.0" free="1"/>
21 21 <parameter scale="1.0" name="Index" min="-5.0" max="+5.0" value="-2.0" free="1"/>
22 22 <parameter scale="1.0" name="LowerLimit" min="10.0" max="1000000.0" value="100.0" free="0"/>
... ...
src/model/GModelSpectralPlaw2.cpp
... ... @@ -41,6 +41,10 @@
41 41 /* __ Globals ____________________________________________________________ */
42 42 const GModelSpectralPlaw2 g_spectral_plaw2_seed;
43 43 const GModelSpectralRegistry g_spectral_plaw2_registry(&g_spectral_plaw2_seed);
  44 +#if defined(G_LEGACY_XML_FORMAT)
  45 +const GModelSpectralPlaw2 g_spectral_plaw2_legacy_seed(true, "PowerLaw2");
  46 +const GModelSpatialRegistry g_spectral_plaw2_legacy_registry(&g_spectral_plaw2_legacy_seed);
  47 +#endif
44 48  
45 49 /* __ Method name definitions ____________________________________________ */
46 50 #define G_FLUX "GModelSpectralPlaw2::flux(GEnergy&, GEnergy&)"
... ... @@ -64,6 +68,8 @@ const GModelSpectralRegistry g_spectral_plaw2_registry(&amp;g_spectral_plaw2_seed);
64 68  
65 69 /***********************************************************************//**
66 70 * @brief Void constructor
  71 + *
  72 + * Constructs empty power law photon flux model.
67 73 ***************************************************************************/
68 74 GModelSpectralPlaw2::GModelSpectralPlaw2(void) : GModelSpectral()
69 75 {
... ... @@ -76,6 +82,29 @@ GModelSpectralPlaw2::GModelSpectralPlaw2(void) : GModelSpectral()
76 82  
77 83  
78 84 /***********************************************************************//**
  85 + * @brief Model type constructor
  86 + *
  87 + * @param[in] dummy Dummy flag.
  88 + * @param[in] type Model type.
  89 + *
  90 + * Constructs empty power law photon flux model by specifying a model @p type.
  91 + ***************************************************************************/
  92 +GModelSpectralPlaw2::GModelSpectralPlaw2(const bool& dummy,
  93 + const std::string& type) :
  94 + GModelSpectral()
  95 +{
  96 + // Initialise members
  97 + init_members();
  98 +
  99 + // Set model type
  100 + m_type = type;
  101 +
  102 + // Return
  103 + return;
  104 +}
  105 +
  106 +
  107 +/***********************************************************************//**
79 108 * @brief Constructor
80 109 *
81 110 * @param[in] integral Integral flux (ph/cm2/s).
... ... @@ -599,7 +628,7 @@ GEnergy GModelSpectralPlaw2::mc(const GEnergy&amp; emin,
599 628 * Reads the spectral information from an XML element. The format of the XML
600 629 * elements is
601 630 *
602   - * <spectrum type="PowerLaw2">
  631 + * <spectrum type="PowerLawPhotonFlux">
603 632 * <parameter name="Integral" scale=".." value=".." min=".." max=".." free=".."/>
604 633 * <parameter name="Index" scale=".." value=".." min=".." max=".." free=".."/>
605 634 * <parameter name="LowerLimit" scale=".." value=".." min=".." max=".." free=".."/>
... ... @@ -676,7 +705,7 @@ void GModelSpectralPlaw2::read(const GXmlElement&amp; xml)
676 705 * Writes the spectral information into an XML element. The format of the XML
677 706 * element is
678 707 *
679   - * <spectrum type="PowerLaw2">
  708 + * <spectrum type="PowerLawPhotonFlux">
680 709 * <parameter name="Integral" scale=".." value=".." min=".." max=".." free=".."/>
681 710 * <parameter name="Index" scale=".." value=".." min=".." max=".." free=".."/>
682 711 * <parameter name="LowerLimit" scale=".." value=".." min=".." max=".." free=".."/>
... ... @@ -687,13 +716,13 @@ void GModelSpectralPlaw2::write(GXmlElement&amp; xml) const
687 716 {
688 717 // Set model type
689 718 if (xml.attribute("type") == "") {
690   - xml.attribute("type", "PowerLaw2");
  719 + xml.attribute("type", type());
691 720 }
692 721  
693 722 // Verify model type
694   - if (xml.attribute("type") != "PowerLaw2") {
  723 + if (xml.attribute("type") != type()) {
695 724 throw GException::model_invalid_spectral(G_WRITE, xml.attribute("type"),
696   - "Spectral model is not of type \"PowerLaw2\".");
  725 + "Spectral model is not of type \""+type()+"\".");
697 726 }
698 727  
699 728 // If XML element has 0 nodes then append 4 parameter nodes
... ... @@ -797,6 +826,9 @@ std::string GModelSpectralPlaw2::print(const GChatter&amp; chatter) const
797 826 ***************************************************************************/
798 827 void GModelSpectralPlaw2::init_members(void)
799 828 {
  829 + // Initialise model type
  830 + m_type = "PowerLawPhotonFlux";
  831 +
800 832 // Initialise integral flux
801 833 m_integral.clear();
802 834 m_integral.name("Integral");
... ... @@ -876,6 +908,7 @@ void GModelSpectralPlaw2::init_members(void)
876 908 void GModelSpectralPlaw2::copy_members(const GModelSpectralPlaw2& model)
877 909 {
878 910 // Copy members
  911 + m_type = model.m_type;
879 912 m_integral = model.m_integral;
880 913 m_index = model.m_index;
881 914 m_emin = model.m_emin;
... ...
test/data/model_point_plaw2.xml
1 1 <?xml version="1.0" standalone="no"?>
2 2 <source_library title="source library">
3 3 <source name="Crab" type="PointSource">
4   - <spectrum type="PowerLaw2">
  4 + <spectrum type="PowerLawPhotonFlux">
5 5 <parameter scale="1e-07" name="Integral" min="1e-07" max="1000.0" value="1.0" free="1"/>
6 6 <parameter scale="1.0" name="Index" min="-5.0" max="+5.0" value="-2.0" free="1"/>
7 7 <parameter scale="1.0" name="LowerLimit" min="10.0" max="1000000.0" value="100.0" free="0"/>
... ...
test/test_GModel.cpp
... ... @@ -1584,79 +1584,54 @@ void TestGModel::test_plaw(void)
1584 1584 void TestGModel::test_plaw2(void)
1585 1585 {
1586 1586 // Test void constructor
1587   - test_try("Test void constructor");
1588   - try {
1589   - GModelSpectralPlaw2 model;
1590   - test_assert(model.type() == "PowerLaw2",
1591   - "Model type \"PowerLaw2\" expected.");
1592   - test_try_success();
1593   - }
1594   - catch (std::exception &e) {
1595   - test_try_failure(e);
1596   - }
  1587 + GModelSpectralPlaw2 model1;
  1588 + test_value(model1.type(), "PowerLawPhotonFlux", "Check type of void model.");
1597 1589  
1598 1590 // Test value constructor
1599   - test_try("Test value constructor");
1600   - try {
1601   - GModelSpectralPlaw2 model(2.0, -2.1, GEnergy(10.0, "MeV"), GEnergy(100.0, "MeV"));
1602   - test_value(model.integral(), 2.0);
1603   - test_value(model.index(), -2.1);
1604   - test_value(model.emin().MeV(), 10.0);
1605   - test_value(model.emax().MeV(), 100.0);
1606   - test_try_success();
1607   - }
1608   - catch (std::exception &e) {
1609   - test_try_failure(e);
1610   - }
  1591 + GModelSpectralPlaw2 model2(2.0, -2.1, GEnergy(10.0, "MeV"), GEnergy(100.0, "MeV"));
  1592 + test_value(model2.integral(), 2.0);
  1593 + test_value(model2.index(), -2.1);
  1594 + test_value(model2.emin().MeV(), 10.0);
  1595 + test_value(model2.emax().MeV(), 100.0);
1611 1596  
1612 1597 // Test XML constructor and value
1613   - test_try("Test XML constructor, value and gradients");
1614   - try {
1615   - // Test XML constructor
1616   - GXml xml(m_xml_model_point_plaw2);
1617   - GXmlElement* element = xml.element(0)->element(0)->element("spectrum", 0);
1618   - GModelSpectralPlaw2 model(*element);
1619   - test_value(model.size(), 4);
1620   - test_assert(model.type() == "PowerLaw2", "Expected \"PowerLaw2\"");
1621   - test_value(model.integral(), 1.0e-7);
1622   - test_value(model.index(), -2.0);
1623   - test_value(model.emin().MeV(), 100.0);
1624   - test_value(model.emax().MeV(), 500000.0);
  1598 + GXml xml(m_xml_model_point_plaw2);
  1599 + GXmlElement* element = xml.element(0)->element(0)->element("spectrum", 0);
  1600 + GModelSpectralPlaw2 model3(*element);
  1601 + test_value(model3.size(), 4);
  1602 + test_value(model3.type(), "PowerLawPhotonFlux", "Check model type.");
  1603 + test_value(model3.integral(), 1.0e-7);
  1604 + test_value(model3.index(), -2.0);
  1605 + test_value(model3.emin().MeV(), 100.0);
  1606 + test_value(model3.emax().MeV(), 500000.0);
  1607 +
  1608 + // Test integral method
  1609 + model3.integral(2.1e-7);
  1610 + test_value(model3.integral(), 2.1e-7);
  1611 +
  1612 + // Test index method
  1613 + model3.index(-2.3);
  1614 + test_value(model3.index(), -2.3);
  1615 +
  1616 + // Test emin method
  1617 + model3.emin(GEnergy(10.0, "MeV"));
  1618 + test_value(model3.emin().MeV(), 10.0);
  1619 +
  1620 + // Test emax method
  1621 + model3.emax(GEnergy(10.0, "MeV"));
  1622 + test_value(model3.emax().MeV(), 10.0);
1625 1623  
1626   - // Test integral method
1627   - model.integral(2.1e-7);
1628   - test_value(model.integral(), 2.1e-7);
1629   -
1630   - // Test index method
1631   - model.index(-2.3);
1632   - test_value(model.index(), -2.3);
1633   -
1634   - // Test emin method
1635   - model.emin(GEnergy(10.0, "MeV"));
1636   - test_value(model.emin().MeV(), 10.0);
1637   -
1638   - // Test emax method
1639   - model.emax(GEnergy(10.0, "MeV"));
1640   - test_value(model.emax().MeV(), 10.0);
1641   -
1642   - // Test operator access
1643   - const char* strarray[] = {"Integral", "Index", "LowerLimit", "UpperLimit"};
1644   - for (int i = 0; i < 4; ++i) {
1645   - std::string keyname(strarray[i]);
1646   - model[keyname].remove_range(); // To allow setting of any value
1647   - model[keyname].value(2.1);
1648   - model[keyname].error(1.9);
1649   - model[keyname].gradient(0.8);
1650   - test_value(model[keyname].value(), 2.1);
1651   - test_value(model[keyname].error(), 1.9);
1652   - test_value(model[keyname].gradient(), 0.8);
1653   - }
1654   -
1655   - // Success if we reached this point
1656   - test_try_success();
1657   - }
1658   - catch (std::exception &e) {
1659   - test_try_failure(e);
  1624 + // Test operator access
  1625 + const char* strarray[] = {"Integral", "Index", "LowerLimit", "UpperLimit"};
  1626 + for (int i = 0; i < 4; ++i) {
  1627 + std::string keyname(strarray[i]);
  1628 + model3[keyname].remove_range(); // To allow setting of any value
  1629 + model3[keyname].value(2.1);
  1630 + model3[keyname].error(1.9);
  1631 + model3[keyname].gradient(0.8);
  1632 + test_value(model3[keyname].value(), 2.1);
  1633 + test_value(model3[keyname].error(), 1.9);
  1634 + test_value(model3[keyname].gradient(), 0.8);
1660 1635 }
1661 1636  
1662 1637 // Exit test
... ...