GammaLib 2.1.0.dev
Loading...
Searching...
No Matches
GModelSpatialElliptical.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GModelSpatialElliptical.cpp - Abstract elliptical spatial model class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2013-2024 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
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
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 Elliptical 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 Elliptical spatial model.
129 * @return Elliptical 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 elliptical model.
167 *
168 * Evaluates the elliptical 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 and position angle (in radians)
178 const GSkyDir& srcDir = photon.dir();
179 double theta = dir().dist(srcDir);
180 double posang = dir().posang(srcDir, coordsys());
181
182 // Evaluate model
183 double value = eval(theta, posang, photon.energy(), photon.time(), gradients);
184
185 // Return result
186 return value;
187}
188
189
190/***********************************************************************//**
191 * @brief Read model from XML element
192 *
193 * @param[in] xml XML element.
194 *
195 * Reads the elliptical source location and position angle information from
196 * an XML element in the following format
197 *
198 * <spatialModel type="...">
199 * <parameter name="RA" scale="1" value="83.63" min="-360" max="360" free="1"/>
200 * <parameter name="DEC" scale="1" value="22.01" min="-90" max="90" free="1"/>
201 * <parameter name="PA" scale="1" value="45.0" min="-360" max="360" free="1"/>
202 * ...
203 * </spatialModel>
204 *
205 * or
206 *
207 * <spatialModel type="...">
208 * <parameter name="GLON" scale="1" value="83.63" min="-360" max="360" free="1"/>
209 * <parameter name="GLAT" scale="1" value="22.01" min="-90" max="90" free="1"/>
210 * <parameter name="PA" scale="1" value="45.0" min="-360" max="360" free="1"/>
211 * ...
212 * </spatialModel>
213 *
214 ***************************************************************************/
216{
217 // Read RA/DEC parameters
218 if (gammalib::xml_has_par(xml, "RA") && gammalib::xml_has_par(xml, "DEC")) {
219
220 // Get parameters
221 const GXmlElement* ra = gammalib::xml_get_par(G_READ, xml, "RA");
222 const GXmlElement* dec = gammalib::xml_get_par(G_READ, xml, "DEC");
223
224 // Read parameters
225 m_lon.read(*ra);
226 m_lat.read(*dec);
227
228 }
229
230 // ... otherwise read GLON/GLAT parameters
231 else {
232
233 // Get parameters
234 const GXmlElement* glon = gammalib::xml_get_par(G_READ, xml, "GLON");
235 const GXmlElement* glat = gammalib::xml_get_par(G_READ, xml, "GLAT");
236
237 // Read parameters
238 m_lon.read(*glon);
239 m_lat.read(*glat);
240
241 }
242
243 // Get other parameters
245
246 // Read other parameters
247 m_posangle.read(*pa);
248
249 // Return
250 return;
251}
252
253
254/***********************************************************************//**
255 * @brief Write model into XML element
256 *
257 * @param[in] xml XML element into which model information is written.
258 *
259 * Depending on the coordinate system, write the elliptical source
260 * information into an XML element with the following format
261 *
262 * <spatialModel type="...">
263 * <parameter name="RA" scale="1" value="83.6331" min="-360" max="360" free="0" />
264 * <parameter name="DEC" scale="1" value="22.0145" min="-90" max="90" free="0" />
265 * <parameter name="PA" scale="1" value="45.0" min="-360" max="360" free="1" />
266 * ...
267 * </spatialModel>
268 *
269 * or
270 *
271 * <spatialModel type="PointSource">
272 * <parameter name="GLON" scale="1" value="184.5575" min="-360" max="360" free="0" />
273 * <parameter name="GLAT" scale="1" value="-5.7843" min="-90" max="90" free="0" />
274 * <parameter name="PA" scale="1" value="45.0" min="-360" max="360" free="1" />
275 * ...
276 * </spatialModel>
277 *
278 ***************************************************************************/
280{
281 // Verify model type
283
284 // Get or create parameters
288
289 // Write parameters
290 m_lon.write(*lon);
291 m_lat.write(*lat);
292 m_posangle.write(*pa);
293
294 // Return
295 return;
296}
297
298
299/***********************************************************************//**
300 * @brief Return position of elliptical spatial model
301 *
302 * @return Elliptical spatial model sky direction.
303 *
304 * Returns the sky direction of the elliptical spatial model.
305 ***************************************************************************/
307{
308 // Get longitude and latitude values
309 double lon = m_lon.value();
310 double lat = m_lat.value();
311
312 // If longitude or latitude values have changed then update sky
313 // direction cache
314 if ((lon != m_last_lon) || (lat != m_last_lat)) {
315
316 // Update last values
317 m_last_lon = lon;
318 m_last_lat = lat;
319
320 // Update sky direction dependent on model coordinate system
321 if (is_celestial()) {
323 }
324 else {
326 }
327
328 } // endif: update of sky direction cache required
329
330 // Return sky direction
331 return (m_dir);
332}
333
334
335/***********************************************************************//**
336 * @brief Set position of radial spatial model
337 *
338 * @param[in] dir Sky direction of radial spatial model.
339 *
340 * Sets the sky direction of the radial spatial model.
341 ***************************************************************************/
343{
344 // Assign sky direction depending on the model coordinate system
345 if (is_celestial()) {
348 }
349 else {
350 m_lon.value(dir.l_deg());
351 m_lat.value(dir.b_deg());
352 }
353
354 // Return
355 return;
356}
357
358
359/*==========================================================================
360 = =
361 = Private methods =
362 = =
363 ==========================================================================*/
364
365/***********************************************************************//**
366 * @brief Initialise class members
367 ***************************************************************************/
369{
370 // Initialise Right Ascension
371 m_lon.clear();
372 m_lon.name("RA");
373 m_lon.unit("deg");
374 m_lon.fix();
375 m_lon.scale(1.0);
376 m_lon.gradient(0.0);
377 m_lon.has_grad(false);
378
379 // Initialise Declination
380 m_lat.clear();
381 m_lat.name("DEC");
382 m_lat.unit("deg");
383 m_lat.fix();
384 m_lat.scale(1.0);
385 m_lat.gradient(0.0);
386 m_lat.has_grad(false);
387
388 // Initialise Position Angle
390 m_posangle.name("PA");
391 m_posangle.unit("deg");
392 m_posangle.fix();
393 m_posangle.scale(1.0);
394 m_posangle.gradient(0.0);
395 m_posangle.has_grad(false);
396
397 // Initialise semi-major axis
399 m_semimajor.name("MajorRadius");
400 m_semimajor.unit("deg");
401 m_semimajor.value(2.778e-4); // 1 arcsec
402 m_semimajor.min(2.778e-4); // 1 arcsec
404 m_semimajor.scale(1.0);
406 m_semimajor.has_grad(false); // Elliptical components never have gradients
407
408 // Initialise semi-minor axis
410 m_semiminor.name("MinorRadius");
411 m_semiminor.unit("deg");
412 m_semiminor.value(2.778e-4); // 1 arcsec
413 m_semiminor.min(2.778e-4); // 1 arcsec
415 m_semiminor.scale(1.0);
417 m_semiminor.has_grad(false); // Elliptical components never have gradients
418
419 // Set parameter pointer(s)
420 m_pars.clear();
421 m_pars.push_back(&m_lon);
422 m_pars.push_back(&m_lat);
423 m_pars.push_back(&m_posangle);
424 m_pars.push_back(&m_semimajor);
425 m_pars.push_back(&m_semiminor);
426
427 // Initialise cache
428 m_dir.clear();
429 m_last_lon = -9999.0;
430 m_last_lat = -9999.0;
431
432 // Return
433 return;
434}
435
436
437/***********************************************************************//**
438 * @brief Copy class members
439 *
440 * @param[in] model Radial spatial model.
441 ***************************************************************************/
443{
444 // Copy members
445 m_lon = model.m_lon;
446 m_lat = model.m_lat;
447 m_posangle = model.m_posangle;
448 m_semiminor = model.m_semiminor;
449 m_semimajor = model.m_semimajor;
450
451 // Set parameter pointer(s)
452 m_pars.clear();
453 m_pars.push_back(&m_lon);
454 m_pars.push_back(&m_lat);
455 m_pars.push_back(&m_posangle);
456 m_pars.push_back(&m_semimajor);
457 m_pars.push_back(&m_semiminor);
458
459 // Copy cache
460 m_dir = model.m_dir;
461 m_last_lon = model.m_last_lon;
462 m_last_lat = model.m_last_lat;
463
464 // Return
465 return;
466}
467
468
469/***********************************************************************//**
470 * @brief Delete class members
471 ***************************************************************************/
473{
474 // Return
475 return;
476}
#define G_WRITE
#define G_READ
Exception handler interface definition.
Abstract elliptical 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 elliptical spatial model base class.
void init_members(void)
Initialise class members.
void copy_members(const GModelSpatialElliptical &model)
Copy class members.
GModelPar m_semiminor
Semi-minor axis of ellipse (deg)
virtual void write(GXmlElement &xml) const
Write model into XML element.
virtual ~GModelSpatialElliptical(void)
Destructor.
bool is_celestial(void) const
Check if model holds celestial coordinates.
GModelPar m_semimajor
Semi-major axis of ellipse (deg)
GModelPar m_lat
Declination or Galactic latitude (deg)
virtual double eval(const double &theta, const double &posangle, const GEnergy &energy, const GTime &time, const bool &gradients=false) const =0
void free_members(void)
Delete class members.
GSkyDir m_dir
Sky direction representing parameters.
virtual GModelSpatialElliptical & operator=(const GModelSpatialElliptical &model)
Assignment operator.
GModelPar m_lon
Right Ascension or Galactic longitude (deg)
GModelSpatialElliptical(void)
Void constructor.
const GSkyDir & dir(void) const
Return position of elliptical spatial model.
std::string coordsys(void) const
Return coordinate system.
virtual void read(const GXmlElement &xml)
Read model from XML element.
GModelPar m_posangle
Position angle from North, counterclockwise (deg)
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.
void free(void)
Free a parameter.
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.
double min(void) const
Return parameter minimum boundary.
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:299
double dec_deg(void) const
Returns Declination in degrees.
Definition GSkyDir.hpp:258
double ra_deg(void) const
Returns Right Ascension in degrees.
Definition GSkyDir.hpp:231
void radec_deg(const double &ra, const double &dec)
Set equatorial sky direction (degrees)
Definition GSkyDir.cpp:245
double b_deg(void) const
Returns galactic latitude in degrees.
Definition GSkyDir.hpp:204
void clear(void)
Clear sky direction.
Definition GSkyDir.cpp:185
double dist(const GSkyDir &dir) const
Compute angular distance between sky directions in radians.
Definition GSkyDir.hpp:273
double l_deg(void) const
Return galactic longitude in degrees.
Definition GSkyDir.hpp:177
double posang(const GSkyDir &dir, const std::string &coordsys="CEL") const
Compute position angle between sky directions in radians.
Definition GSkyDir.cpp:1300
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:1708
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:1656
bool xml_has_par(const GXmlElement &xml, const std::string &name)
Checks if parameter with given name in XML element exists.
Definition GTools.cpp:1615
void xml_check_type(const std::string &origin, GXmlElement &xml, const std::string &type)
Checks the model type.
Definition GTools.cpp:1838