GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GModelSpatialRadial.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GModelSpatialRadial.cpp - Abstract radial spatial model base class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2011-2018 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GModelSpatialRadial.cpp
23  * @brief Abstract radial spatial model base class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GException.hpp"
32 #include "GModelSpatialRadial.hpp"
33 
34 /* __ Method name definitions ____________________________________________ */
35 #define G_READ "GModelSpatialRadial::read(GXmlElement&)"
36 #define G_WRITE "GModelSpatialRadial::write(GXmlElement&)"
37 
38 /* __ Macros _____________________________________________________________ */
39 
40 /* __ Coding definitions _________________________________________________ */
41 
42 /* __ Debug definitions __________________________________________________ */
43 
44 
45 /*==========================================================================
46  = =
47  = Constructors/destructors =
48  = =
49  ==========================================================================*/
50 
51 /***********************************************************************//**
52  * @brief Void constructor
53  ***************************************************************************/
55 {
56  // Initialise members
57  init_members();
58 
59  // Return
60  return;
61 }
62 
63 
64 /***********************************************************************//**
65  * @brief XML constructor
66  *
67  * @param[in] xml XML element.
68  *
69  * Constructs a radial spatial model by extracting information from an XML
70  * element. See the read() method for more information about the expected
71  * structure of the XML element.
72  ***************************************************************************/
75 {
76  // Initialise members
77  init_members();
78 
79  // Read information from XML element
80  read(xml);
81 
82  // Return
83  return;
84 }
85 
86 
87 /***********************************************************************//**
88  * @brief Copy constructor
89  *
90  * @param[in] model Radial spatial model.
91  ***************************************************************************/
94 {
95  // Initialise members
96  init_members();
97 
98  // Copy members
99  copy_members(model);
100 
101  // Return
102  return;
103 }
104 
105 
106 /***********************************************************************//**
107  * @brief Destructor
108  ***************************************************************************/
110 {
111  // Free members
112  free_members();
113 
114  // Return
115  return;
116 }
117 
118 
119 /*==========================================================================
120  = =
121  = Operators =
122  = =
123  ==========================================================================*/
124 
125 /***********************************************************************//**
126  * @brief Assignment operator
127  *
128  * @param[in] model Radial spatial model.
129  * @return Radial spatial model.
130  ***************************************************************************/
132 {
133  // Execute only if object is not identical
134  if (this != &model) {
135 
136  // Copy base class members
137  this->GModelSpatial::operator=(model);
138 
139  // Free members
140  free_members();
141 
142  // Initialise members
143  init_members();
144 
145  // Copy members
146  copy_members(model);
147 
148  } // endif: object was not identical
149 
150  // Return
151  return *this;
152 }
153 
154 
155 /*==========================================================================
156  = =
157  = Public methods =
158  = =
159  ==========================================================================*/
160 
161 /***********************************************************************//**
162  * @brief Return model value
163  *
164  * @param[in] photon Incident Photon.
165  * @param[in] gradients Compute gradients?
166  * @return Value of spatial radial model.
167  *
168  * Evaluates the radial spatial model value for a specific incident
169  * @p photon.
170  *
171  * If the @p gradients flag is true the method will also compute the
172  * parameter gradients for all model parameters.
173  ***************************************************************************/
174 double GModelSpatialRadial::eval(const GPhoton& photon,
175  const bool& gradients) const
176 {
177  // Compute distance from source (in radians)
178  double theta = photon.dir().dist(dir());
179 
180  // Evaluate model
181  double value = eval(theta, photon.energy(), photon.time(), gradients);
182 
183  // Return result
184  return value;
185 }
186 
187 
188 /***********************************************************************//**
189  * @brief Read model from XML element
190  *
191  * @param[in] xml XML element.
192  *
193  * @exception GException::model_invalid_parnum
194  * Invalid number of model parameters found in XML element.
195  * @exception GException::model_invalid_parnames
196  * Invalid model parameter names found in XML element.
197  *
198  * Reads the radial source location and position angle information from an
199  * XML element in the following format
200  *
201  * <spatialModel type="...">
202  * <parameter name="RA" scale="1" value="83.63" min="-360" max="360" free="1"/>
203  * <parameter name="DEC" scale="1" value="22.01" min="-90" max="90" free="1"/>
204  * ...
205  * </spatialModel>
206  *
207  * or
208  *
209  * <spatialModel type="...">
210  * <parameter name="GLON" scale="1" value="83.63" min="-360" max="360" free="1"/>
211  * <parameter name="GLAT" scale="1" value="22.01" min="-90" max="90" free="1"/>
212  * ...
213  * </spatialModel>
214  *
215  ***************************************************************************/
217 {
218  // Read RA/DEC parameters
219  if (gammalib::xml_has_par(xml, "RA") && gammalib::xml_has_par(xml, "DEC")) {
220 
221  // Get parameters
222  const GXmlElement* ra = gammalib::xml_get_par(G_READ, xml, "RA");
223  const GXmlElement* dec = gammalib::xml_get_par(G_READ, xml, "DEC");
224 
225  // Read parameters
226  m_ra.read(*ra);
227  m_dec.read(*dec);
228 
229  }
230 
231  // ... otherwise read GLON/GLAT parameters
232  else {
233 
234  // Get parameters
235  const GXmlElement* glon = gammalib::xml_get_par(G_READ, xml, "GLON");
236  const GXmlElement* glat = gammalib::xml_get_par(G_READ, xml, "GLAT");
237 
238  // Read parameters
239  m_ra.read(*glon);
240  m_dec.read(*glat);
241 
242  // Convert into RA/DEC
243  GSkyDir dir;
244  dir.lb_deg(ra(), dec()),
245  m_ra.value(dir.ra_deg());
246  m_dec.value(dir.dec_deg());
247 
248  // Set names to RA/DEC
249  m_ra.name("RA");
250  m_dec.name("DEC");
251 
252  }
253 
254  // Return
255  return;
256 }
257 
258 
259 /***********************************************************************//**
260  * @brief Write model into XML element
261  *
262  * @param[in] xml XML element into which model information is written.
263  *
264  * @exception GException::model_invalid_spatial
265  * Existing XML element is not of type 'GaussFunction'
266  * @exception GException::model_invalid_parnum
267  * Invalid number of model parameters found in XML element.
268  * @exception GException::model_invalid_parnames
269  * Invalid model parameter names found in XML element.
270  *
271  * Writes the radial source location and position angle information into an
272  * XML element in the following format
273  *
274  * <spatialModel type="...">
275  * <parameter name="RA" scale="1" value="83.63" min="-360" max="360" free="1"/>
276  * <parameter name="DEC" scale="1" value="22.01" min="-90" max="90" free="1"/>
277  * ...
278  * </spatialModel>
279  *
280  * @todo The case that an existing spatial XML element with "GLON" and "GLAT"
281  * as coordinates is not supported.
282  ***************************************************************************/
284 {
285  // Set model type
286  if (xml.attribute("type") == "") {
287  xml.attribute("type", type());
288  }
289 
290  // Verify model type
291  if (xml.attribute("type") != type()) {
293  "Radial model is not of type \""+type()+"\".");
294  }
295 
296  // Get or create parameters
299 
300  // Write parameters
301  m_ra.write(*ra);
302  m_dec.write(*dec);
303 
304  // Return
305  return;
306 }
307 
308 
309 /***********************************************************************//**
310  * @brief Return position of radial spatial model
311  ***************************************************************************/
313 {
314  // Allocate sky direction
315  GSkyDir srcDir;
316 
317  // Set sky direction
318  srcDir.radec_deg(ra(), dec());
319 
320  // Return direction
321  return srcDir;
322 }
323 
324 
325 /***********************************************************************//**
326  * @brief Set position of radial spatial model
327  ***************************************************************************/
329 {
330  // Assign Right Ascension and Declination
331  m_ra.value(dir.ra_deg());
332  m_dec.value(dir.dec_deg());
333 
334  // Return
335  return;
336 }
337 
338 
339 /*==========================================================================
340  = =
341  = Private methods =
342  = =
343  ==========================================================================*/
344 
345 /***********************************************************************//**
346  * @brief Initialise class members
347  ***************************************************************************/
349 {
350  // Initialise Right Ascension
351  m_ra.clear();
352  m_ra.name("RA");
353  m_ra.unit("deg");
354  m_ra.fix();
355  m_ra.scale(1.0);
356  m_ra.gradient(0.0);
357  m_ra.has_grad(false);
358 
359  // Initialise Declination
360  m_dec.clear();
361  m_dec.name("DEC");
362  m_dec.unit("deg");
363  m_dec.fix();
364  m_dec.scale(1.0);
365  m_dec.gradient(0.0);
366  m_dec.has_grad(false);
367 
368  // Set parameter pointer(s)
369  m_pars.clear();
370  m_pars.push_back(&m_ra);
371  m_pars.push_back(&m_dec);
372 
373  // Return
374  return;
375 }
376 
377 
378 /***********************************************************************//**
379  * @brief Copy class members
380  *
381  * @param[in] model Radial spatial model.
382  ***************************************************************************/
384 {
385  // Copy members
386  m_ra = model.m_ra;
387  m_dec = model.m_dec;
388 
389  // Set parameter pointer(s)
390  m_pars.clear();
391  m_pars.push_back(&m_ra);
392  m_pars.push_back(&m_dec);
393 
394  // Return
395  return;
396 }
397 
398 
399 /***********************************************************************//**
400  * @brief Delete class members
401  ***************************************************************************/
403 {
404  // Return
405  return;
406 }
double dec_deg(void) const
Returns Declination in degrees.
Definition: GSkyDir.hpp:250
const std::string & name(void) const
Return parameter name.
double gradient(void) const
Return parameter gradient.
void write(GXmlElement &xml) const
Set or update parameter attributes in XML element.
Definition: GModelPar.cpp:353
Abstract radial spatial model base class interface definition.
void copy_members(const GModelSpatialRadial &model)
Copy class members.
XML element node class.
Definition: GXmlElement.hpp:47
virtual GModelSpatial & operator=(const GModelSpatial &model)
Assignment operator.
GModelSpatialRadial(void)
Void constructor.
GModelPar m_dec
Declination (deg)
void init_members(void)
Initialise class members.
Class that handles photons.
Definition: GPhoton.hpp:47
const double & scale(void) const
Return parameter scale.
virtual ~GModelSpatialRadial(void)
Destructor.
virtual double eval(const double &theta, const GEnergy &energy, const GTime &time, const bool &gradients=false) const =0
bool has_grad(void) const
Signal if parameter gradient is computed analytically.
const GXmlAttribute * attribute(const int &index) const
Return attribute.
void free_members(void)
Delete class members.
std::vector< GModelPar * > m_pars
Parameter pointers.
void fix(void)
Fix a parameter.
GXmlElement * xml_need_par(const std::string &origin, GXmlElement &xml, const std::string &name)
Return pointer to parameter with given name in XML element.
Definition: GTools.cpp:1513
virtual void read(const GXmlElement &xml)
Read model from XML element.
double dec(void) const
Return Declination of model centre.
void clear(void)
Clear parameter.
double ra_deg(void) const
Returns Right Ascension in degrees.
Definition: GSkyDir.hpp:223
const GTime & time(void) const
Return photon time.
Definition: GPhoton.hpp:134
double ra(void) const
Return Right Ascencion of model centre.
void radec_deg(const double &ra, const double &dec)
Set equatorial sky direction (degrees)
Definition: GSkyDir.cpp:202
bool xml_has_par(const GXmlElement &xml, const std::string &name)
Checks if parameter with given name in XML element exists.
Definition: GTools.cpp:1475
GModelPar m_ra
Right Ascension (deg)
virtual void write(GXmlElement &xml) const
Write model into XML element.
virtual std::string type(void) const =0
virtual GModelSpatialRadial & operator=(const GModelSpatialRadial &model)
Assignment operator.
GSkyDir dir(void) const
Return position of radial spatial model.
#define G_WRITE
void read(const GXmlElement &xml)
Extract parameter attributes from XML element.
Definition: GModelPar.cpp:229
double value(void) const
Return parameter value.
const std::string & unit(void) const
Return parameter unit.
void lb_deg(const double &l, const double &b)
Set galactic sky direction (degrees)
Definition: GSkyDir.cpp:256
const GEnergy & energy(void) const
Return photon energy.
Definition: GPhoton.hpp:122
Exception handler interface definition.
double dist(const GSkyDir &dir) const
Compute angular distance between sky directions in radians.
Definition: GSkyDir.hpp:265
Abstract spatial model base class.
Abstract radial spatial model base class.
#define G_READ
Sky direction class.
Definition: GSkyDir.hpp:62
const GSkyDir & dir(void) const
Return photon sky direction.
Definition: GPhoton.hpp:110
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.
Definition: GTools.cpp:1562