GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GModelSpatialElliptical.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GModelSpatialElliptical.cpp - Abstract elliptical spatial model class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2013-2022 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 GModelSpatialElliptical.cpp
23  * @brief Abstract elliptical 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"
33 
34 /* __ Method name definitions ____________________________________________ */
35 #define G_READ "GModelSpatialElliptical::read(GXmlElement&)"
36 #define G_WRITE "GModelSpatialElliptical::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 an elliptical spatial model component by extracting information
70  * from an XML element. See the read() method for more information about the
71  * expected 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  * @brief Copy constructor
88  *
89  * @param[in] model Elliptical spatial model.
90  ***************************************************************************/
92  GModelSpatial(model)
93 {
94  // Initialise members
95  init_members();
96 
97  // Copy members
98  copy_members(model);
99 
100  // Return
101  return;
102 }
103 
104 
105 /***********************************************************************//**
106  * @brief Destructor
107  ***************************************************************************/
109 {
110  // Free members
111  free_members();
112 
113  // Return
114  return;
115 }
116 
117 
118 /*==========================================================================
119  = =
120  = Operators =
121  = =
122  ==========================================================================*/
123 
124 /***********************************************************************//**
125  * @brief Assignment operator
126  *
127  * @param[in] model Elliptical spatial model.
128  * @return Elliptical spatial model.
129  ***************************************************************************/
131 {
132  // Execute only if object is not identical
133  if (this != &model) {
134 
135  // Copy base class members
136  this->GModelSpatial::operator=(model);
137 
138  // Free members
139  free_members();
140 
141  // Initialise members
142  init_members();
143 
144  // Copy members
145  copy_members(model);
146 
147  } // endif: object was not identical
148 
149  // Return
150  return *this;
151 }
152 
153 
154 /*==========================================================================
155  = =
156  = Public methods =
157  = =
158  ==========================================================================*/
159 
160 /***********************************************************************//**
161  * @brief Return model value
162  *
163  * @param[in] photon Incident Photon.
164  * @param[in] gradients Compute gradients?
165  * @return Value of spatial elliptical model.
166  *
167  * Evaluates the elliptical spatial model value for a specific incident
168  * @p photon.
169  *
170  * If the @p gradients flag is true the method will also compute the
171  * parameter gradients for all model parameters.
172  ***************************************************************************/
174  const bool& gradients) const
175 {
176  // Compute distance from source and position angle (in radians)
177  const GSkyDir& srcDir = photon.dir();
178  double theta = dir().dist(srcDir);
179  double posang = dir().posang(srcDir); // Celestial system
180 
181  // Evaluate model
182  double value = eval(theta, posang, photon.energy(), photon.time(), gradients);
183 
184  // Return result
185  return value;
186 }
187 
188 
189 /***********************************************************************//**
190  * @brief Read model from XML element
191  *
192  * @param[in] xml XML element.
193  *
194  * Reads the elliptical source location and position angle information from
195  * an XML element in the following format
196  *
197  * <spatialModel type="...">
198  * <parameter name="RA" scale="1" value="83.63" min="-360" max="360" free="1"/>
199  * <parameter name="DEC" scale="1" value="22.01" min="-90" max="90" free="1"/>
200  * <parameter name="PA" scale="1" value="45.0" min="-360" max="360" free="1"/>
201  * ...
202  * </spatialModel>
203  *
204  * or
205  *
206  * <spatialModel type="...">
207  * <parameter name="GLON" scale="1" value="83.63" min="-360" max="360" free="1"/>
208  * <parameter name="GLAT" scale="1" value="22.01" min="-90" max="90" free="1"/>
209  * <parameter name="PA" scale="1" value="45.0" min="-360" max="360" free="1"/>
210  * ...
211  * </spatialModel>
212  *
213  ***************************************************************************/
215 {
216  // Read RA/DEC parameters
217  if (gammalib::xml_has_par(xml, "RA") && gammalib::xml_has_par(xml, "DEC")) {
218 
219  // Get parameters
220  const GXmlElement* ra = gammalib::xml_get_par(G_READ, xml, "RA");
221  const GXmlElement* dec = gammalib::xml_get_par(G_READ, xml, "DEC");
222 
223  // Read parameters
224  m_lon.read(*ra);
225  m_lat.read(*dec);
226 
227  }
228 
229  // ... otherwise read GLON/GLAT parameters
230  else {
231 
232  // Get parameters
233  const GXmlElement* glon = gammalib::xml_get_par(G_READ, xml, "GLON");
234  const GXmlElement* glat = gammalib::xml_get_par(G_READ, xml, "GLAT");
235 
236  // Read parameters
237  m_lon.read(*glon);
238  m_lat.read(*glat);
239 
240  }
241 
242  // Get other parameters
244 
245  // Read other parameters
246  m_posangle.read(*pa);
247 
248  // Return
249  return;
250 }
251 
252 
253 /***********************************************************************//**
254  * @brief Write model into XML element
255  *
256  * @param[in] xml XML element into which model information is written.
257  *
258  * Depending on the coordinate system, write the elliptical source
259  * information into an XML element with the following format
260  *
261  * <spatialModel type="...">
262  * <parameter name="RA" scale="1" value="83.6331" min="-360" max="360" free="0" />
263  * <parameter name="DEC" scale="1" value="22.0145" min="-90" max="90" free="0" />
264  * <parameter name="PA" scale="1" value="45.0" min="-360" max="360" free="1" />
265  * ...
266  * </spatialModel>
267  *
268  * or
269  *
270  * <spatialModel type="PointSource">
271  * <parameter name="GLON" scale="1" value="184.5575" min="-360" max="360" free="0" />
272  * <parameter name="GLAT" scale="1" value="-5.7843" min="-90" max="90" free="0" />
273  * <parameter name="PA" scale="1" value="45.0" min="-360" max="360" free="1" />
274  * ...
275  * </spatialModel>
276  *
277  ***************************************************************************/
279 {
280  // Verify model type
282 
283  // Get or create parameters
287 
288  // Write parameters
289  m_lon.write(*lon);
290  m_lat.write(*lat);
291  m_posangle.write(*pa);
292 
293  // Return
294  return;
295 }
296 
297 
298 /***********************************************************************//**
299  * @brief Return position of elliptical spatial model
300  *
301  * @return Elliptical spatial model sky direction.
302  *
303  * Returns the sky direction of the elliptical spatial model.
304  ***************************************************************************/
306 {
307  // Get longitude and latitude values
308  double lon = m_lon.value();
309  double lat = m_lat.value();
310 
311  // If longitude or latitude values have changed then update sky
312  // direction cache
313  if ((lon != m_last_lon) || (lat != m_last_lat)) {
314 
315  // Update last values
316  m_last_lon = lon;
317  m_last_lat = lat;
318 
319  // Update sky direction dependent on model coordinate system
320  if (is_celestial()) {
322  }
323  else {
325  }
326 
327  } // endif: update of sky direction cache required
328 
329  // Return sky direction
330  return (m_dir);
331 }
332 
333 
334 /***********************************************************************//**
335  * @brief Set position of radial spatial model
336  *
337  * @param[in] dir Sky direction of radial spatial model.
338  *
339  * Sets the sky direction of the radial spatial model.
340  ***************************************************************************/
342 {
343  // Assign sky direction depending on the model coordinate system
344  if (is_celestial()) {
345  m_lon.value(dir.ra_deg());
346  m_lat.value(dir.dec_deg());
347  }
348  else {
349  m_lon.value(dir.l_deg());
350  m_lat.value(dir.b_deg());
351  }
352 
353  // Return
354  return;
355 }
356 
357 
358 /*==========================================================================
359  = =
360  = Private methods =
361  = =
362  ==========================================================================*/
363 
364 /***********************************************************************//**
365  * @brief Initialise class members
366  ***************************************************************************/
368 {
369  // Initialise Right Ascension
370  m_lon.clear();
371  m_lon.name("RA");
372  m_lon.unit("deg");
373  m_lon.fix();
374  m_lon.scale(1.0);
375  m_lon.gradient(0.0);
376  m_lon.has_grad(false);
377 
378  // Initialise Declination
379  m_lat.clear();
380  m_lat.name("DEC");
381  m_lat.unit("deg");
382  m_lat.fix();
383  m_lat.scale(1.0);
384  m_lat.gradient(0.0);
385  m_lat.has_grad(false);
386 
387  // Initialise Position Angle
388  m_posangle.clear();
389  m_posangle.name("PA");
390  m_posangle.unit("deg");
391  m_posangle.fix();
392  m_posangle.scale(1.0);
393  m_posangle.gradient(0.0);
394  m_posangle.has_grad(false);
395 
396  // Initialise semi-major axis
397  m_semimajor.clear();
398  m_semimajor.name("MajorRadius");
399  m_semimajor.unit("deg");
400  m_semimajor.value(2.778e-4); // 1 arcsec
401  m_semimajor.min(2.778e-4); // 1 arcsec
402  m_semimajor.free();
403  m_semimajor.scale(1.0);
404  m_semimajor.gradient(0.0);
405  m_semimajor.has_grad(false); // Elliptical components never have gradients
406 
407  // Initialise semi-minor axis
408  m_semiminor.clear();
409  m_semiminor.name("MinorRadius");
410  m_semiminor.unit("deg");
411  m_semiminor.value(2.778e-4); // 1 arcsec
412  m_semiminor.min(2.778e-4); // 1 arcsec
413  m_semiminor.free();
414  m_semiminor.scale(1.0);
415  m_semiminor.gradient(0.0);
416  m_semiminor.has_grad(false); // Elliptical components never have gradients
417 
418  // Set parameter pointer(s)
419  m_pars.clear();
420  m_pars.push_back(&m_lon);
421  m_pars.push_back(&m_lat);
422  m_pars.push_back(&m_posangle);
423  m_pars.push_back(&m_semimajor);
424  m_pars.push_back(&m_semiminor);
425 
426  // Initialise cache
427  m_dir.clear();
428  m_last_lon = -9999.0;
429  m_last_lat = -9999.0;
430 
431  // Return
432  return;
433 }
434 
435 
436 /***********************************************************************//**
437  * @brief Copy class members
438  *
439  * @param[in] model Radial spatial model.
440  ***************************************************************************/
442 {
443  // Copy members
444  m_lon = model.m_lon;
445  m_lat = model.m_lat;
446  m_posangle = model.m_posangle;
447  m_semiminor = model.m_semiminor;
448  m_semimajor = model.m_semimajor;
449 
450  // Set parameter pointer(s)
451  m_pars.clear();
452  m_pars.push_back(&m_lon);
453  m_pars.push_back(&m_lat);
454  m_pars.push_back(&m_posangle);
455  m_pars.push_back(&m_semimajor);
456  m_pars.push_back(&m_semiminor);
457 
458  // Copy cache
459  m_dir = model.m_dir;
460  m_last_lon = model.m_last_lon;
461  m_last_lat = model.m_last_lat;
462 
463  // Return
464  return;
465 }
466 
467 
468 /***********************************************************************//**
469  * @brief Delete class members
470  ***************************************************************************/
472 {
473  // Return
474  return;
475 }
GModelPar m_semimajor
Semi-major axis of ellipse (deg)
virtual void write(GXmlElement &xml) const
Write model into XML element.
void free_members(void)
Delete class members.
double dec_deg(void) const
Returns Declination in degrees.
Definition: GSkyDir.hpp:256
Abstract elliptical spatial model base class.
const std::string & name(void) const
Return parameter name.
#define G_WRITE
GModelPar m_lon
Right Ascension or Galactic longitude (deg)
double m_last_lat
Last latitude.
double m_last_lon
Last longitude.
double gradient(void) const
Return parameter gradient.
void write(GXmlElement &xml) const
Set or update parameter attributes in XML element.
Definition: GModelPar.cpp:351
virtual GModelSpatialElliptical & operator=(const GModelSpatialElliptical &model)
Assignment operator.
XML element node class.
Definition: GXmlElement.hpp:48
const GSkyDir & dir(void) const
Return position of elliptical spatial model.
virtual GModelSpatial & operator=(const GModelSpatial &model)
Assignment operator.
GModelPar m_posangle
Position angle from North, counterclockwise (deg)
double min(void) const
Return parameter minimum boundary.
void init_members(void)
Initialise class members.
Class that handles photons.
Definition: GPhoton.hpp:47
const double & scale(void) const
Return parameter scale.
GModelSpatialElliptical(void)
Void constructor.
virtual ~GModelSpatialElliptical(void)
Destructor.
bool has_grad(void) const
Signal if parameter gradient is computed analytically.
bool is_celestial(void) const
Check if model holds celestial coordinates.
void free(void)
Free a parameter.
std::vector< GModelPar * > m_pars
Parameter pointers.
void fix(void)
Fix a parameter.
void copy_members(const GModelSpatialElliptical &model)
Copy class members.
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:1637
void clear(void)
Clear parameter.
double ra_deg(void) const
Returns Right Ascension in degrees.
Definition: GSkyDir.hpp:229
const GTime & time(void) const
Return photon time.
Definition: GPhoton.hpp:134
virtual void read(const GXmlElement &xml)
Read model from XML element.
GModelPar m_semiminor
Semi-minor axis of ellipse (deg)
std::string type(void) const
Return model type.
void radec_deg(const double &ra, const double &dec)
Set equatorial sky direction (degrees)
Definition: GSkyDir.cpp:224
bool xml_has_par(const GXmlElement &xml, const std::string &name)
Checks if parameter with given name in XML element exists.
Definition: GTools.cpp:1596
Abstract elliptical spatial model base class interface definition.
double posang(const GSkyDir &dir, const std::string &coordsys="CEL") const
Compute position angle between sky directions in radians.
Definition: GSkyDir.cpp:1151
void clear(void)
Clear sky direction.
Definition: GSkyDir.cpp:164
void read(const GXmlElement &xml)
Extract parameter attributes from XML element.
Definition: GModelPar.cpp:229
double value(void) const
Return parameter value.
#define G_READ
GModelPar m_lat
Declination or Galactic latitude (deg)
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:278
const GEnergy & energy(void) const
Return photon energy.
Definition: GPhoton.hpp:122
GSkyDir m_dir
Sky direction representing parameters.
Exception handler interface definition.
double dist(const GSkyDir &dir) const
Compute angular distance between sky directions in radians.
Definition: GSkyDir.hpp:271
Abstract spatial model base class.
double l_deg(void) const
Return galactic longitude in degrees.
Definition: GSkyDir.hpp:175
double b_deg(void) const
Returns galactic latitude in degrees.
Definition: GSkyDir.hpp:202
virtual double eval(const double &theta, const double &posangle, const GEnergy &energy, const GTime &time, const bool &gradients=false) const =0
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:1689
void xml_check_type(const std::string &origin, GXmlElement &xml, const std::string &type)
Checks the model type.
Definition: GTools.cpp:1819