GammaLib 2.0.0
Loading...
Searching...
No Matches
GModelSpatialRadial.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GModelSpatialRadial.cpp - Abstract radial spatial model base class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2011-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 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"
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
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
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 ***************************************************************************/
93 GModelSpatial(model)
94{
95 // Initialise 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 ***************************************************************************/
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.6331" min="-360" max="360" free="0" />
203 * <parameter name="DEC" scale="1" value="22.0145" min="-90" max="90" free="0" />
204 * ...
205 * </spatialModel>
206 *
207 * or
208 *
209 * <spatialModel type="...">
210 * <parameter name="GLON" scale="1" value="184.5575" min="-360" max="360" free="0" />
211 * <parameter name="GLAT" scale="1" value="-5.7843" min="-90" max="90" free="0" />
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_lon.read(*ra);
227 m_lat.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_lon.read(*glon);
240 m_lat.read(*glat);
241
242 }
243
244 // Return
245 return;
246}
247
248
249/***********************************************************************//**
250 * @brief Write model into XML element
251 *
252 * @param[in] xml XML element into which model information is written.
253 *
254 * Depending on the coordinate system, write the radial source information
255 * into an XML element with the following format
256 *
257 * <spatialModel type="...">
258 * <parameter name="RA" scale="1" value="83.6331" min="-360" max="360" free="0" />
259 * <parameter name="DEC" scale="1" value="22.0145" min="-90" max="90" free="0" />
260 * ...
261 * </spatialModel>
262 *
263 * or
264 *
265 * <spatialModel type="PointSource">
266 * <parameter name="GLON" scale="1" value="184.5575" min="-360" max="360" free="0" />
267 * <parameter name="GLAT" scale="1" value="-5.7843" min="-90" max="90" free="0" />
268 * ...
269 * </spatialModel>
270 *
271 ***************************************************************************/
273{
274 // Check model type
276
277 // Get or create parameters
280
281 // Write parameters
282 m_lon.write(*lon);
283 m_lat.write(*lat);
284
285 // Return
286 return;
287}
288
289
290/***********************************************************************//**
291 * @brief Return position of radial spatial model
292 *
293 * @return Radial spatial model sky direction.
294 *
295 * Returns the sky direction of the radial spatial model.
296 ***************************************************************************/
298{
299 // Get longitude and latitude values
300 double lon = m_lon.value();
301 double lat = m_lat.value();
302
303 // If longitude or latitude values have changed then update sky
304 // direction cache
305 if ((lon != m_last_lon) || (lat != m_last_lat)) {
306
307 // Update last values
308 m_last_lon = lon;
309 m_last_lat = lat;
310
311 // Update sky direction dependent on model coordinate system
312 if (is_celestial()) {
314 }
315 else {
317 }
318
319 } // endif: update of sky direction cache required
320
321 // Return sky direction
322 return (m_dir);
323}
324
325
326/***********************************************************************//**
327 * @brief Set position of radial spatial model
328 *
329 * @param[in] dir Sky direction of radial spatial model.
330 *
331 * Sets the sky direction of the radial spatial model.
332 ***************************************************************************/
334{
335 // Assign sky direction depending on the model coordinate system
336 if (is_celestial()) {
339 }
340 else {
341 m_lon.value(dir.l_deg());
342 m_lat.value(dir.b_deg());
343 }
344
345 // Return
346 return;
347}
348
349
350/*==========================================================================
351 = =
352 = Private methods =
353 = =
354 ==========================================================================*/
355
356/***********************************************************************//**
357 * @brief Initialise class members
358 ***************************************************************************/
360{
361 // Initialise Right Ascension
362 m_lon.clear();
363 m_lon.name("RA");
364 m_lon.unit("deg");
365 m_lon.fix();
366 m_lon.scale(1.0);
367 m_lon.gradient(0.0);
368 m_lon.has_grad(false);
369
370 // Initialise Declination
371 m_lat.clear();
372 m_lat.name("DEC");
373 m_lat.unit("deg");
374 m_lat.fix();
375 m_lat.scale(1.0);
376 m_lat.gradient(0.0);
377 m_lat.has_grad(false);
378
379 // Set parameter pointer(s)
380 m_pars.clear();
381 m_pars.push_back(&m_lon);
382 m_pars.push_back(&m_lat);
383
384 // Initialise cache
385 m_dir.clear();
386 m_last_lon = -9999.0;
387 m_last_lat = -9999.0;
388
389 // Return
390 return;
391}
392
393
394/***********************************************************************//**
395 * @brief Copy class members
396 *
397 * @param[in] model Radial spatial model.
398 ***************************************************************************/
400{
401 // Copy members
402 m_lon = model.m_lon;
403 m_lat = model.m_lat;
404
405 // Set parameter pointer(s)
406 m_pars.clear();
407 m_pars.push_back(&m_lon);
408 m_pars.push_back(&m_lat);
409
410 // Copy cache
411 m_dir = model.m_dir;
412 m_last_lon = model.m_last_lon;
413 m_last_lat = model.m_last_lat;
414
415 // Return
416 return;
417}
418
419
420/***********************************************************************//**
421 * @brief Delete class members
422 ***************************************************************************/
424{
425 // Return
426 return;
427}
#define G_WRITE
#define G_READ
Exception handler interface definition.
Abstract radial spatial model base class interface definition.
void write(GXmlElement &xml) const
Set or update parameter attributes in XML element.
void read(const GXmlElement &xml)
Extract parameter attributes from XML element.
Abstract radial spatial model base class.
void free_members(void)
Delete class members.
double m_last_lon
Last longitude.
bool is_celestial(void) const
Check if model holds celestial coordinates.
void copy_members(const GModelSpatialRadial &model)
Copy class members.
double m_last_lat
Last latitude.
GModelPar m_lon
Right Ascension or Galactic longitude (deg)
virtual void write(GXmlElement &xml) const
Write model into XML element.
virtual void read(const GXmlElement &xml)
Read model from XML element.
GModelSpatialRadial(void)
Void constructor.
void init_members(void)
Initialise class members.
GSkyDir m_dir
Sky direction representing parameters.
virtual double eval(const double &theta, const GEnergy &energy, const GTime &time, const bool &gradients=false) const =0
virtual GModelSpatialRadial & operator=(const GModelSpatialRadial &model)
Assignment operator.
const GSkyDir & dir(void) const
Return position of radial spatial model.
GModelPar m_lat
Declination or Galactic latitude (deg)
virtual ~GModelSpatialRadial(void)
Destructor.
Abstract spatial model base class.
std::string type(void) const
Return model type.
std::vector< GModelPar * > m_pars
Parameter pointers.
virtual GModelSpatial & operator=(const GModelSpatial &model)
Assignment operator.
const double & scale(void) const
Return parameter scale.
bool has_grad(void) const
Signal if parameter gradient is computed analytically.
const std::string & unit(void) const
Return parameter unit.
void fix(void)
Fix a parameter.
double gradient(void) const
Return parameter gradient.
void clear(void)
Clear parameter.
double value(void) const
Return parameter value.
const std::string & name(void) const
Return parameter name.
Class that handles photons.
Definition GPhoton.hpp:47
const GTime & time(void) const
Return photon time.
Definition GPhoton.hpp:134
const GSkyDir & dir(void) const
Return photon sky direction.
Definition GPhoton.hpp:110
const GEnergy & energy(void) const
Return photon energy.
Definition GPhoton.hpp:122
Sky direction class.
Definition GSkyDir.hpp:62
void lb_deg(const double &l, const double &b)
Set galactic sky direction (degrees)
Definition GSkyDir.cpp:278
double dec_deg(void) const
Returns Declination in degrees.
Definition GSkyDir.hpp:256
double ra_deg(void) const
Returns Right Ascension in degrees.
Definition GSkyDir.hpp:229
void radec_deg(const double &ra, const double &dec)
Set equatorial sky direction (degrees)
Definition GSkyDir.cpp:224
double b_deg(void) const
Returns galactic latitude in degrees.
Definition GSkyDir.hpp:202
void clear(void)
Clear sky direction.
Definition GSkyDir.cpp:164
double dist(const GSkyDir &dir) const
Compute angular distance between sky directions in radians.
Definition GSkyDir.hpp:271
double l_deg(void) const
Return galactic longitude in degrees.
Definition GSkyDir.hpp:175
XML element node class.
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
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
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
void xml_check_type(const std::string &origin, GXmlElement &xml, const std::string &type)
Checks the model type.
Definition GTools.cpp:1819