Commit 9d6081b74abaadb1f2119984e738c03c04ffcaae

Authored by Jürgen Knödlseder
1 parent 4f5f06d9

Add GModelSpatial::has_free_pars() method (#2715)

1 1 New Features and Important Changes in GammaLib 1.6.0
2 2  
3   -1 November 2018
  3 +6 November 2018
4 4  
5 5  
6 6 1. Introduction
... ... @@ -70,6 +70,7 @@ The following methods have been added:
70 70 - GObservation::has_events()
71 71 - GModelSpatialPointSource::type(std::string&)
72 72 - GModelSpectralPlaw::type(std::string&)
  73 +- GModelSpatial::has_free_pars()
73 74 - GModelSpatialRadialProfileGauss(GSkyDir&, double&)
74 75 - GModelSpatialRadialProfileGauss::sigma()
75 76 - GSkyDir::cos_dist()
... ... @@ -205,6 +206,9 @@ None
205 206  
206 207 8. Model module
207 208 ---------------
  209 +Add GModelSpatial::has_free_pars() method that checks whether a spatial
  210 +model component has free model parameters or not (#2715).
  211 +
208 212 The GModelSpectralExponential class was added that implements an exponential
209 213 function that can be used for EBL studies (#2454).
210 214  
... ...
include/GModelSpatial.hpp
1 1 /***************************************************************************
2 2 * GModelSpatial.hpp - Spatial model abstract base class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2009-2016 by Juergen Knoedlseder *
  4 + * copyright (C) 2009-2018 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -107,6 +107,7 @@ public:
107 107 GModelPar& at(const int& index);
108 108 const GModelPar& at(const int& index) const;
109 109 bool has_par(const std::string& name) const;
  110 + bool has_free_pars(void) const;
110 111 int size(void) const;
111 112 void autoscale(void);
112 113  
... ...
pyext/GModelSpatial.i
... ... @@ -61,6 +61,7 @@ public:
61 61  
62 62 // Methods
63 63 bool has_par(const std::string& name) const;
  64 + bool has_free_pars(void) const;
64 65 int size(void) const;
65 66 void autoscale(void);
66 67 };
... ...
src/model/GModelSpatial.cpp
1 1 /***************************************************************************
2 2 * GModelSpatial.cpp - Abstract spatial model base class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2009-2015 by Juergen Knoedlseder *
  4 + * copyright (C) 2009-2018 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -267,6 +267,29 @@ bool GModelSpatial::has_par(const std::string& name) const
267 267  
268 268  
269 269 /***********************************************************************//**
  270 + * @brief Checks if the spatial model has free parameters
  271 + *
  272 + * @return True if spatial model has free parameters.
  273 + ***************************************************************************/
  274 +bool GModelSpatial::has_free_pars(void) const
  275 +{
  276 + // Initialise result flag
  277 + bool has_free_pars = false;
  278 +
  279 + // Search for free parameters
  280 + for (int i = 0; i < size(); ++i) {
  281 + if (m_pars[i]->is_free()) {
  282 + has_free_pars = true;
  283 + break;
  284 + }
  285 + }
  286 +
  287 + // Return result
  288 + return has_free_pars;
  289 +}
  290 +
  291 +
  292 +/***********************************************************************//**
270 293 * @brief Autoscale parameters
271 294 *
272 295 * Sets the scale factors for all parameters so that the values are unity.
... ...
test/test_GModel.cpp
... ... @@ -659,6 +659,31 @@ void TestGModel::test_point_source(void)
659 659 test_value(model4[keyname].gradient(), 0.8);
660 660 }
661 661  
  662 + // Test size() method
  663 + GModelSpatialPointSource model5;
  664 + test_value(model5.size(), 2, "Test size() method.");
  665 +
  666 + // Test has_par() method
  667 + test_assert(model5.has_par("RA"), "Test has_par() method for \"RA\"");
  668 + test_assert(model5.has_par("DEC"), "Test has_par() method for \"DEC\"");
  669 + test_assert(!model5.has_par("XXX"), "Test has_par() method for \"XXX\"");
  670 +
  671 + // Test has_free_pars() method
  672 + model5["RA"].fix();
  673 + model5["DEC"].fix();
  674 + test_assert(!model5.has_free_pars(), "Test has_free_pars() method for fixed parameters");
  675 + model5["DEC"].free();
  676 + test_assert(model5.has_free_pars(), "Test has_free_pars() method for free parameters");
  677 +
  678 + // Test autoscale() method
  679 + model5["RA"].value(180.0);
  680 + model5["DEC"].value(-30.0);
  681 + model5.autoscale();
  682 + test_value(model5["RA"].scale(), 180.0, "Test autoscale() method (\"RA\" scale).");
  683 + test_value(model5["DEC"].scale(), -30.0, "Test autoscale() method (\"DEC\" scale).");
  684 + test_value(model5["RA"].factor_value(), 1.0, "Test autoscale() method (\"RA\" factor).");
  685 + test_value(model5["DEC"].factor_value(), 1.0, "Test autoscale() method (\"DEC\" factor).");
  686 +
662 687 // Exit test
663 688 return;
664 689 }
... ...