GModelSpatialEllipticalDisk.cpp
22.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/***************************************************************************
* GModelSpatialEllipticalDisk.cpp - Elliptical disk source model class *
* ----------------------------------------------------------------------- *
* copyright (C) 2013 by Michael Mayer *
* ----------------------------------------------------------------------- *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
***************************************************************************/
/**
* @file GModelSpatialEllipticalDisk.cpp
* @brief Elliptical disk model class implementation
* @author Michael Mayer
*/
/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "GException.hpp"
#include "GTools.hpp"
#include "GModelSpatialEllipticalDisk.hpp"
#include "GModelSpatialRadialDisk.hpp"
#include "GModelSpatialRegistry.hpp"
/* __ Constants __________________________________________________________ */
/* __ Globals ____________________________________________________________ */
const GModelSpatialEllipticalDisk g_elliptical_disk_seed;
const GModelSpatialRegistry g_elliptical_disk_registry(&g_elliptical_disk_seed);
/* __ Method name definitions ____________________________________________ */
#define G_READ "GModelSpatialEllipticalDisk::read(GXmlElement&)"
#define G_WRITE "GModelSpatialEllipticalDisk::write(GXmlElement&)"
/* __ Macros _____________________________________________________________ */
/* __ Coding definitions _________________________________________________ */
/* __ Debug definitions __________________________________________________ */
/*==========================================================================
= =
= Constructors/destructors =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Void constructor
***************************************************************************/
GModelSpatialEllipticalDisk::GModelSpatialEllipticalDisk(void) :
GModelSpatialElliptical()
{
// Initialise members
init_members();
// Return
return;
}
/***********************************************************************//**
* @brief Disk constructor
*
* @param[in] dir Sky position of disk centre.
* @param[in] semiminor Semi-minor axis (degrees).
* @param[in] semimajor Semi-major axis (degrees).
* @param[in] posangle Position angle of semi-major axis (degrees).
*
* Construct elliptical disk model from model parameters.
***************************************************************************/
GModelSpatialEllipticalDisk::GModelSpatialEllipticalDisk(const GSkyDir& dir,
const double& semiminor,
const double& semimajor,
const double& posangle) :
GModelSpatialElliptical()
{
// Initialise members
init_members();
// Assign parameters
this->dir(dir);
this->semiminor(semiminor);
this->semimajor(semimajor);
this->posangle(posangle);
// Return
return;
}
/***********************************************************************//**
* @brief XML constructor
*
* @param[in] xml XML element.
*
* Creates instance of Elliptical disk model by extracting information from
* an XML element. See GModelSpatialEllipticalDisk::read() for more
* information about the expected structure of the XML element.
***************************************************************************/
GModelSpatialEllipticalDisk::GModelSpatialEllipticalDisk(const GXmlElement& xml) :
GModelSpatialElliptical()
{
// Initialise members
init_members();
// Read information from XML element
read(xml);
// Return
return;
}
/***********************************************************************//**
* @brief Copy constructor
*
* @param[in] model Elliptical disk model.
***************************************************************************/
GModelSpatialEllipticalDisk::GModelSpatialEllipticalDisk(const GModelSpatialEllipticalDisk& model) :
GModelSpatialElliptical(model)
{
// Initialise members
init_members();
// Copy members
copy_members(model);
// Return
return;
}
/***********************************************************************//**
* @brief Destructor
***************************************************************************/
GModelSpatialEllipticalDisk::~GModelSpatialEllipticalDisk(void)
{
// Free members
free_members();
// Return
return;
}
/*==========================================================================
= =
= Operators =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Assignment operator
*
* @param[in] model Elliptical disk model.
* @return Elliptical disk model.
***************************************************************************/
GModelSpatialEllipticalDisk& GModelSpatialEllipticalDisk::operator=(const GModelSpatialEllipticalDisk& model)
{
// Execute only if object is not identical
if (this != &model) {
// Copy base class members
this->GModelSpatialElliptical::operator=(model);
// Free members
free_members();
// Initialise members
init_members();
// Copy members
copy_members(model);
} // endif: object was not identical
// Return
return *this;
}
/*==========================================================================
= =
= Public methods =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Clear instance
***************************************************************************/
void GModelSpatialEllipticalDisk::clear(void)
{
// Free class members (base and derived classes, derived class first)
free_members();
this->GModelSpatialElliptical::free_members();
this->GModelSpatial::free_members();
// Initialise members
this->GModelSpatial::init_members();
this->GModelSpatialElliptical::init_members();
init_members();
// Return
return;
}
/***********************************************************************//**
* @brief Clone instance
*
* @return Pointer to deep copy of elliptical disk model.
***************************************************************************/
GModelSpatialEllipticalDisk* GModelSpatialEllipticalDisk::clone(void) const
{
return new GModelSpatialEllipticalDisk(*this);
}
/***********************************************************************//**
* @brief Evaluate function (in units of sr^-1)
*
* @param[in] theta Angular distance from disk centre (radians).
* @param[in] posangle Position angle (counterclockwise from North) (radians).
*
* Evaluates the spatial component for an elliptical disk source model. The
* disk source model is an elliptical function \f$f(\theta, \phi)\f$, where
* \f$\theta\f$ is the angular separation between elliptical disk centre and
* the actual location and \f$\phi\f$ the position angle with respect to the
* ellipse centre, counted counterclockwise from North.
*
* The function \f$f(\theta, \phi)\f$ is given by
*
* \f[
* f(\theta,\phi) = \left \{
* \begin{array}{l l}
* {\tt m\_norm}
* & \mbox{if} \, \, \theta \le \theta_0 \\
* \\
* 0 & \mbox{else}
* \end{array}
* \right .
* \f]
*
* where \f$\theta_0\f$ is the effective radius of the ellipse on the sphere
* given by
*
* \f[\theta_0\ =
* \frac{ab}{\sqrt{b^2 \cos^2(\phi-\phi_0) + a^2 \sin^2(\phi-\phi_0)}}\f]
*
* and
* \f$a\f$ is the semi-major axis of the ellipse,
* \f$b\f$ is the semi-minor axis, and
* \f$\phi_0\f$ is the position angle of the ellipse, counted
* counterclockwise from North.
*
* The normalisation constant \f${\tt m\_norm}\f$ which is the inverse of the
* solid angle subtended by an ellipse is given by
*
* @todo Quote formula for ellipse solid angle
*
* (see the update() method).
***************************************************************************/
double GModelSpatialEllipticalDisk::eval(const double& theta,
const double& posangle) const
{
// Initialise value
double value = 0.0;
// Continue only if we're inside circle enclosing the ellipse
if (theta <= theta_max()) {
// Update precomputation cache
update();
// Perform computations
double diff_angle = posangle - m_posangle.value()*deg2rad;
double cosinus = std::cos(diff_angle);
double sinus = std::sin(diff_angle);
double arg1 = m_semiminor_rad * cosinus;
double arg2 = m_semimajor_rad * sinus;
double r_ell = m_semiminor_rad * m_semimajor_rad /
std::sqrt(arg1*arg1 + arg2*arg2);
// Set value
value = (theta <= r_ell) ? m_norm : 0.0;
// Compile option: Check for NaN/Inf
#if defined(G_NAN_CHECK)
if (isnotanumber(value) || isinfinite(value)) {
std::cout << "*** ERROR: GModelSpatialEllipticalDisk::eval";
std::cout << "(theta=" << theta << "): NaN/Inf encountered";
std::cout << "(posangle=" << posangle << "): NaN/Inf encountered";
std::cout << " (value=" << value;
std::cout << ", R_ellipse=" << r_ell;
std::cout << ", diff_angle=" << diff_angle;
std::cout << ", m_norm=" << m_norm;
std::cout << ")" << std::endl;
}
#endif
} // endif: position was inside enclosing circle
// Return value
return value;
}
/***********************************************************************//**
* @brief Evaluate function and gradients (in units of sr^-1)
*
* @param[in] theta Angular distance from disk centre (radians).
* @param[in] posangle Position angle (counterclockwise from North) (radians).
*
* Evaluates the function value. No gradient computation is implemented as
* Elliptical models will be convolved with the instrument response and thus
* require the numerical computation of the derivatives.
*
* See the eval() method for more information.
***************************************************************************/
double GModelSpatialEllipticalDisk::eval_gradients(const double& theta,
const double& posangle) const
{
// Return value
return (eval(theta, posangle));
}
/***********************************************************************//**
* @brief Returns MC sky direction
*
* @param[in] ran Random number generator.
*
* Draws an arbitrary sky position from the 2D disk distribution.
* @todo test function
***************************************************************************/
GSkyDir GModelSpatialEllipticalDisk::mc(GRan& ran) const
{
// Update precomputation cache
update();
// Initialise a radial disk model with radius of the semimajor axis
GModelSpatialRadialDisk disk = GModelSpatialRadialDisk(dir(), semimajor());
// Initialise SkyDir
GSkyDir dir = GSkyDir();
// Draw randomly from the radial disk
// and reject the value if its outside the ellipse
do {
// Set SkyDir to random position
dir = disk.mc(ran);
} while(GModelSpatialElliptical::eval(dir) <= 0.0);
// Return SkyDir
return dir;
}
/***********************************************************************//**
* @brief Return maximum model radius (in radians)
*
* @return Returns maximum model radians.
***************************************************************************/
double GModelSpatialEllipticalDisk::theta_max(void) const
{
// Set maximum model radius
double theta_max = (semimajor() > semiminor()) ?
semimajor()*deg2rad : semiminor()*deg2rad;
// Return value
return theta_max;
}
/***********************************************************************//**
* @brief Read model from XML element
*
* @param[in] xml XML element.
*
* @exception GException::model_invalid_parnum
* Invalid number of model parameters found in XML element.
* @exception GException::model_invalid_parnames
* Invalid model parameter names found in XML element.
*
* Read the elliptical disk information from an XML element. The XML element
* is required to have 5 parameters.
* The position is named either "RA" and "DEC" or "GLON" and "GLAT", the
* rotation angle "PA", the semi-semiminor axis "MinorRadius" and the semi-semimajor
* axis "MajorRadius"
*
* @todo Implement a test of the ellipse boundary. The axes
* and axes minimum should be >0.
***************************************************************************/
void GModelSpatialEllipticalDisk::read(const GXmlElement& xml)
{
// Determine number of parameter nodes in XML element
int npars = xml.elements("parameter");
// Verify that XML element has exactly 5 parameters
if (xml.elements() != 5 || npars != 5) {
throw GException::model_invalid_parnum(G_READ, xml,
"Elliptical disk model requires exactly 5 parameters.");
}
// Read disk location
GModelSpatialElliptical::read(xml);
// Extract model parameters
int npar[2] = {0, 0};
for (int i = 0; i < npars; ++i) {
// Get parameter element
const GXmlElement* par = xml.element("parameter", i);
// Handle semiminor radius
if (par->attribute("name") == "MinorRadius") {
// Read parameter
m_semiminor.read(*par);
// Increment parameter counter
npar[0]++;
}
// Handle semimajor radius
else if (par->attribute("name") == "MajorRadius") {
// Read parameter
m_semimajor.read(*par);
// Increment parameter counter
npar[1]++;
}
} // endfor: looped over all parameters
// Verify that all parameters were found
if (npar[0] != 1 || npar[1] != 1) {
throw GException::model_invalid_parnames(G_READ, xml,
"Elliptical disk model requires \"MinorRadius\" and"
" \"MajorRadius\" parameters.");
}
// Return
return;
}
/***********************************************************************//**
* @brief Write model into XML element
*
* @param[in] xml XML element into which model information is written.
*
* @exception GException::model_invalid_parnum
* Invalid number of model parameters found in XML element.
* @exception GException::model_invalid_parnames
* Invalid model parameter names found in XML element.
*
* Write the Disk source information into an XML element. The XML element
* will have 5 parameter leafs named "RA", "DEC", "PA", "MinorRadius" and
* "MajorRadius"
***************************************************************************/
void GModelSpatialEllipticalDisk::write(GXmlElement& xml) const
{
// Write disk location
GModelSpatialElliptical::write(xml);
// If XML element has 3 nodes (which should be the location and PA nodes)
// then append 2 parameter nodes
if (xml.elements() == 3) {
xml.append(GXmlElement("parameter name=\"MinorRadius\""));
xml.append(GXmlElement("parameter name=\"MajorRadius\""));
}
// Determine number of parameter nodes in XML element
int npars = xml.elements("parameter");
// Verify that XML element has exactly 5 parameters
if (xml.elements() != 5 || npars != 5) {
throw GException::model_invalid_parnum(G_WRITE, xml,
"Elliptical Disk model requires exactly 5 parameters.");
}
// Set or update model parameter attributes
int npar[2] = {0, 0};
for (int i = 0; i < npars; ++i) {
// Get parameter element
GXmlElement* par = xml.element("parameter", i);
// Handle semiminor radius
if (par->attribute("name") == "MinorRadius") {
// Write parameter
m_semiminor.write(*par);
// Increment parameter counter
npar[0]++;
}
// Handle semimajor radius
else if (par->attribute("name") == "MajorRadius") {
// Write parameter
m_semimajor.write(*par);
// Increment parameter counter
npar[1]++;
}
} // endfor: looped over all parameters
// Check of all required parameters are present
if (npar[0] != 1 || npar[1] != 1) {
throw GException::model_invalid_parnames(G_WRITE, xml,
"Elliptical disk model requires \"MinorRadius\" and"
" \"MajorRadius\" parameters.");
}
// Return
return;
}
/***********************************************************************//**
* @brief Print information
*
* @return String containing model information.
***************************************************************************/
std::string GModelSpatialEllipticalDisk::print(void) const
{
// Initialise result string
std::string result;
// Append header
result.append("=== GModelSpatialEllipticalDisk ===\n");
// Append parameters
result.append(parformat("Number of parameters")+str(size()));
for (int i = 0; i < size(); ++i) {
result.append("\n"+m_pars[i]->print());
}
// Return result
return result;
}
/*==========================================================================
= =
= Private methods =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Initialise class members
***************************************************************************/
void GModelSpatialEllipticalDisk::init_members(void)
{
// Initialise semi-minor axis
m_semiminor.clear();
m_semiminor.name("MinorRadius");
m_semiminor.unit("deg");
m_semiminor.value(2.778e-4); // 1 arcsec
m_semiminor.min(2.778e-4); // 1 arcsec
m_semiminor.free();
m_semiminor.scale(1.0);
m_semiminor.gradient(0.0);
m_semiminor.hasgrad(false); // Elliptical components never have gradients
// Initialise semi-major axis
m_semimajor.clear();
m_semimajor.name("MajorRadius");
m_semimajor.unit("deg");
m_semimajor.value(2.778e-4); // 1 arcsec
m_semimajor.min(2.778e-4); // 1 arcsec
m_semimajor.free();
m_semimajor.scale(1.0);
m_semimajor.gradient(0.0);
m_semimajor.hasgrad(false); // Elliptical components never have gradients
// Set parameter pointer(s)
m_pars.push_back(&m_semiminor);
m_pars.push_back(&m_semimajor);
// Initialise precomputation cache. Note that zero values flag
// uninitialised as a zero radius is not meaningful
m_last_semiminor = 0.0;
m_last_semimajor = 0.0;
m_semiminor_rad = 0.0;
m_semimajor_rad = 0.0;
m_norm = 0.0;
// Return
return;
}
/***********************************************************************//**
* @brief Copy class members
*
* @param[in] model Elliptical disk model.
*
* We do not have to push back the members on the parameter stack as this
* should have been done by init_members() that was called before. Otherwise
* we would have the radius twice on the stack.
***************************************************************************/
void GModelSpatialEllipticalDisk::copy_members(const GModelSpatialEllipticalDisk& model)
{
// Copy members
m_semiminor = model.m_semiminor;
m_semimajor = model.m_semimajor;
// Copy precomputation cache
m_last_semiminor = model.m_last_semiminor;
m_last_semimajor = model.m_last_semimajor;
m_semiminor_rad = model.m_semiminor_rad;
m_semimajor_rad = model.m_semimajor_rad;
m_norm = model.m_norm;
// Return
return;
}
/***********************************************************************//**
* @brief Delete class members
***************************************************************************/
void GModelSpatialEllipticalDisk::free_members(void)
{
// Return
return;
}
/***********************************************************************//**
* @brief Update precomputation cache
*
* Computes the normalization
* \f[{\tt m\_norm} = \frac{1}{2 \pi (1 - \cos a) (1 - \cos b)}\f]
* @todo check this formula
***************************************************************************/
void GModelSpatialEllipticalDisk::update() const
{
// Update if one axis has changed has changed
if (m_last_semiminor != semiminor() || m_last_semimajor != semimajor()) {
// Store last values
m_last_semiminor = semiminor();
m_last_semimajor = semimajor();
// Compute axes in radians
m_semiminor_rad = semiminor() * deg2rad;
m_semimajor_rad = semimajor() * deg2rad;
// Perform precomputations
double denom = twopi * std::sqrt((1 - std::cos(m_semiminor_rad)) *
(1 - std::cos(m_semimajor_rad)));
m_norm = (denom > 0.0) ? 1.0 / denom : 0.0;
} // endif: update required
// Return
return;
}