GModelSpatialRadialProfileDMZhao.cpp 29.2 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 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
/***************************************************************************
 *    GModelSpatialRadialProfileDMZhao.cpp - Zhao radial profile class     *
 * ----------------------------------------------------------------------- *
 *  copyright (C) 2016 by Nathan Kelley-Hoskins                            *
 * ----------------------------------------------------------------------- *
 *                                                                         *
 *  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 GModelSpatialRadialProfileDMZhao.cpp
 * @brief Radial DM Einasto profile model class implementation
 * @author Nathan Kelley-Hoskins
 */

/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "GException.hpp"
#include "GTools.hpp"
#include "GMath.hpp"
#include "GXmlElement.hpp"
#include "GModelSpatialRadialProfileDMZhao.hpp"
#include "GModelSpatialRegistry.hpp"

/* __ Constants __________________________________________________________ */

/* __ Globals ____________________________________________________________ */
const GModelSpatialRadialProfileDMZhao g_radial_disk_seed;
const GModelSpatialRegistry            g_radial_disk_registry(&g_radial_disk_seed);

/* __ Method name definitions ____________________________________________ */
#define G_READ         "GModelSpatialRadialProfileDMZhao::read(GXmlElement&)"
#define G_WRITE       "GModelSpatialRadialProfileDMZhao::write(GXmlElement&)"

/* __ Macros _____________________________________________________________ */

/* __ Coding definitions _________________________________________________ */

/* __ Debug definitions __________________________________________________ */


/*==========================================================================
 =                                                                         =
 =                        Constructors/destructors                         =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Void constructor
 *
 * Constructs empty radial DMZhao profile
 ***************************************************************************/
GModelSpatialRadialProfileDMZhao::GModelSpatialRadialProfileDMZhao(void) :
                                  GModelSpatialRadialProfile()
{
    // Initialise members
    init_members();

    // Return
    return;
}


/***********************************************************************//**
 * @brief XML constructor
 *
 * @param[in] xml XML element.
 *
 * Constructs radial DMZhao profile model by extracting information from
 * an XML element. See the read() method for more information about the
 * expected structure of the XML element.
 ***************************************************************************/
GModelSpatialRadialProfileDMZhao::GModelSpatialRadialProfileDMZhao(const GXmlElement& xml) :
                                  GModelSpatialRadialProfile()
{
    // Initialise members
    init_members();

    // Read information from XML element
    read(xml);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Copy constructor
 *
 * @param[in] model Radial DMZhao profile model.
 *
 * Copies radial DMZhao profile model from another radial profile model.
 ***************************************************************************/
GModelSpatialRadialProfileDMZhao::GModelSpatialRadialProfileDMZhao(const GModelSpatialRadialProfileDMZhao& model) :
                                  GModelSpatialRadialProfile(model)
{
    // Initialise members
    init_members();

    // Copy members
    copy_members(model);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Destructor
 *
 * Destructs radial DMZhao profile model.
 ***************************************************************************/
GModelSpatialRadialProfileDMZhao::~GModelSpatialRadialProfileDMZhao(void)
{
    // Free members
    free_members();

    // Return
    return;
}


/*==========================================================================
 =                                                                         =
 =                               Operators                                 =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Assignment operator
 *
 * @param[in] model Radial DMZhao profile model.
 * @return Radial DMZhao profile model.
 *
 * Assigns radial DMZhao profile model.
 ***************************************************************************/
GModelSpatialRadialProfileDMZhao& GModelSpatialRadialProfileDMZhao::operator=(const GModelSpatialRadialProfileDMZhao& model)
{
    // Execute only if object is not identical
    if (this != &model) {

        // Copy base class members
        this->GModelSpatialRadialProfile::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 radial DMZhao profile model
 *
 * Clears radial DMZhao profile model.
 ***************************************************************************/
void GModelSpatialRadialProfileDMZhao::clear(void)
{
    // Free class members
    free_members();
    this->GModelSpatialRadialProfile::free_members();
    this->GModelSpatialRadial::free_members();
    this->GModelSpatial::free_members();

    // Initialise members
    this->GModelSpatial::init_members();
    this->GModelSpatialRadial::init_members();
    this->GModelSpatialRadialProfile::init_members();
    init_members();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Clone radial DMZhao profile model
 *
 * @return Pointer to deep copy of radial DMZhao profile model.
 *
 * Returns a deep copy of the radial DMZhao profile model.
 ***************************************************************************/
GModelSpatialRadialProfileDMZhao* GModelSpatialRadialProfileDMZhao::clone(void) const
{
    // Clone radial disk model
    return new GModelSpatialRadialProfileDMZhao(*this);
}

/***********************************************************************//**
 * @brief Return minimum model radius (in radians)
 *
 * @return Minimum model radius (in radians).
 ***************************************************************************/
double GModelSpatialRadialProfileDMZhao::theta_min(void) const
{
    
    // update precomputation cache
    update();
    
    // Return value
    return m_theta_min.value();
}


/***********************************************************************//**
 * @brief Return maximum model radius (in radians)
 *
 * @return Maximum model radius (in radians).
 ***************************************************************************/
double GModelSpatialRadialProfileDMZhao::theta_max(void) const
{
    // Update precomputation cache
    update();

    // Initialize maximum theta angle
    double theta = 0.0;

    // If Earth is within the significant radius, then theta_max must
    // contain the entire profile (180deg) ...
    if (m_halo_distance.value() < m_mass_radius) {
        theta = gammalib::pi;
    }

    // ... otherwise, if the halo is far enough away (further than the
    // significant radius) then we just need to deal with the angles within
    // the sphere of the significant radius.
    else {
        theta = std::atan(m_mass_radius / m_halo_distance.value());
    }

    // Always chose the lesser of ( mass_radius theta, theta_max )
    double theta_max = m_theta_max.value() * gammalib::deg2rad;
    if (theta_max < theta) {
        theta = theta_max;
    }

    // Return value
    return theta;
}


/***********************************************************************//**
 * @brief Read model from XML element
 *
 * @param[in] xml XML element.
 *
 * Reads the DMZhao radial profile model information from an XML element.
 * The XML element shall have the format
 *
 *     <spatialModel type="DMZhaoProfile">
 *       <parameter name="RA"           scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="DEC"          scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="ScaleRadius"  scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="ScaleDensity" scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="HaloDistance" scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="Alpha"        scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="Beta"         scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="Gamma"        scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="ThetaMin"     scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="ThetaMax"     scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="CoreRadius"   scale=.. value=.. min=.. max=.. free=../>
 *     </spatialModel>
 ***************************************************************************/
void GModelSpatialRadialProfileDMZhao::read(const GXmlElement& xml)
{
    // Read halo sky coordinate
    GModelSpatialRadial::read(xml);

    // Read ScaleRadius parameter
    const GXmlElement* par1 = gammalib::xml_get_par(G_READ, xml, "ScaleRadius");
    m_scale_radius.read(*par1);
    
    // Read ScaleDensity parameter
    const GXmlElement* par2 = gammalib::xml_get_par(G_READ, xml, "ScaleDensity");
    m_scale_density.read(*par2);
    
    // Read HaloDistance parameter
    const GXmlElement* par3 = gammalib::xml_get_par(G_READ, xml, "HaloDistance");
    m_halo_distance.read(*par3);

    // Read Alpha parameter
    const GXmlElement* par4 = gammalib::xml_get_par(G_READ, xml, "Alpha");
    m_alpha.read(*par4);

    // Read Beta parameter
    const GXmlElement* par5 = gammalib::xml_get_par(G_READ, xml, "Beta");
    m_beta.read(*par5);

    // Read Gamma parameter
    const GXmlElement* par6 = gammalib::xml_get_par(G_READ, xml, "Gamma");
    m_gamma.read(*par6);

    // Read ThetaMin parameter
    const GXmlElement* par7 = gammalib::xml_get_par(G_READ, xml, "ThetaMin");
    m_theta_min.read(*par7);

    // Read ThetaMax parameter
    const GXmlElement* par8 = gammalib::xml_get_par(G_READ, xml, "ThetaMax");
    m_theta_max.read(*par8);
    
    // Read CoreRadius parameter
    const GXmlElement* par9 = gammalib::xml_get_par(G_READ, xml, "CoreRadius");
    m_core_radius.read(*par9);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Write model into XML element
 *
 * @param[in] xml XML element into which model information is written.
 *
 * Writes the DMZhao radial profile model information into an XML element.
 * The XML element will have the format 
 *
 *     <spatialModel type="DMZhaoProfile">
 *       <parameter name="RA"           scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="DEC"          scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="ScaleRadius"  scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="ScaleDensity" scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="HaloDistance" scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="Alpha"        scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="Beta"         scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="Gamma"        scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="ThetaMin"     scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="ThetaMax"     scale=.. value=.. min=.. max=.. free=../>
 *       <parameter name="CoreRadius"   scale=.. value=.. min=.. max=.. free=../>
 *     </spatialModel>
 ***************************************************************************/
void GModelSpatialRadialProfileDMZhao::write(GXmlElement& xml) const
{
    // Write DMZhao location
    GModelSpatialRadial::write(xml);

    // Write ScaleRadius parameter
    GXmlElement* par1 = gammalib::xml_need_par(G_WRITE, xml, "ScaleRadius");
    m_scale_radius.write(*par1);
    
    // Write ScaleDensity parameter
    GXmlElement* par2 = gammalib::xml_need_par(G_WRITE, xml, "ScaleDensity");
    m_scale_density.write(*par2);

    // Write HaloDistance parameter
    GXmlElement* par3 = gammalib::xml_need_par(G_WRITE, xml, "HaloDistance");
    m_halo_distance.write(*par3);

    // Write Alpha parameter
    GXmlElement* par4 = gammalib::xml_need_par(G_WRITE, xml, "Alpha");
    m_alpha.write(*par4);

    // Write Beta parameter
    GXmlElement* par5 = gammalib::xml_need_par(G_WRITE, xml, "Beta");
    m_beta.write(*par5);

    // Write Gamma parameter
    GXmlElement* par6 = gammalib::xml_need_par(G_WRITE, xml, "Gamma");
    m_gamma.write(*par6);

    // Write ThetaMax parameter
    GXmlElement* par7 = gammalib::xml_need_par(G_WRITE, xml, "ThetaMin");
    m_theta_min.write(*par7);

    // Write ThetaMax parameter
    GXmlElement* par8 = gammalib::xml_need_par(G_WRITE, xml, "ThetaMax");
    m_theta_max.write(*par8);
    
    // Write CoreRadius parameter
    GXmlElement* par9 = gammalib::xml_need_par(G_WRITE, xml, "CoreRadius");
    m_core_radius.write(*par9);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Print information
 *
 * @param[in] chatter Chattiness.
 * @return String containing model information.
 ***************************************************************************/
std::string GModelSpatialRadialProfileDMZhao::print(const GChatter& chatter) const
{
    // Initialise result string
    std::string result;

    // Continue only if chatter is not silent
    if (chatter != SILENT) {

        // Append header
        result.append("=== GModelSpatialRadialProfileDMZhao ===");

        // Append parameters
        result.append("\n"+gammalib::parformat("Number of parameters"));
        result.append(gammalib::str(size()));
        for (int i = 0; i < size(); ++i) {
            result.append("\n"+m_pars[i]->print(chatter));
        }

    } // endif: chatter was not silent

    // Return result
    return result;
}


/*==========================================================================
 =                                                                         =
 =                            Private methods                              =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Initialise class members
 ***************************************************************************/
void GModelSpatialRadialProfileDMZhao::init_members(void)
{

    // Initialise scale radius
    m_scale_radius.clear();
    m_scale_radius.name("ScaleRadius");
    m_scale_radius.unit("kpc");
    m_scale_radius.value(21.5);       // default to GC scale radius
    m_scale_radius.min(1.0e-6);       // arbitrarily chosen :/
    m_scale_radius.free();
    m_scale_radius.scale(1.0);
    m_scale_radius.gradient(0.0);
    m_scale_radius.has_grad(false);   // Radial components never have gradients
    
    // Initialise scale density
    m_scale_density.clear();
    m_scale_density.name("ScaleDensity");
    m_scale_density.unit("GeV/cm^3");
    m_scale_density.value(0.2);       // GeV/cm^3 default to GC scale density
    m_scale_density.free();
    m_scale_density.scale(1.0);
    m_scale_density.gradient(0.0);
    m_scale_density.has_grad(false);  // Radial components never have gradients

    // Initialise halo distance
    m_halo_distance.clear();
    m_halo_distance.name("HaloDistance");
    m_halo_distance.unit("kpc");
    m_halo_distance.value(7.94);     // default to GC halo distance
    m_halo_distance.min(1.0e-6);     // arbitrarily chosen
    m_halo_distance.free();
    m_halo_distance.scale(1.0);
    m_halo_distance.gradient(0.0);
    m_halo_distance.has_grad(false); // Radial components never have gradients

    // Initialise alpha
    m_alpha.clear();
    m_alpha.name("Alpha");
    m_alpha.unit("unitless");
    m_alpha.value(1.0);              // default to NFW alpha
    m_alpha.min(0.01);               // arbitrarily chosen
    m_alpha.max(10.0);               // arbitrarily chosen
    m_alpha.free();
    m_alpha.scale(1.0);
    m_alpha.gradient(0.0);
    m_alpha.has_grad(false);         // Radial components never have gradients

    // Initialise 
    m_beta.clear();
    m_beta.name("Beta");
    m_beta.unit("unitless");
    m_beta.value(3.0);               // default to NFW beta
    m_beta.min(0.01);                // arbitrarily chosen
    m_beta.max(10.0);                // arbitrarily chosen
    m_beta.free();
    m_beta.scale(1.0);
    m_beta.gradient(0.0);
    m_beta.has_grad(false);          // Radial components never have gradients

    // Initialise 
    m_gamma.clear();
    m_gamma.name("Gamma");
    m_gamma.unit("unitless");
    m_gamma.value(1.0);              // default to NFW gamma
    m_gamma.min(0.01);               // arbitrarily chosen
    m_gamma.max(10.0);               // arbitrarily chosen
    m_gamma.free();
    m_gamma.scale(1.0);
    m_gamma.gradient(0.0);
    m_gamma.has_grad(false);         // Radial components never have gradients

    // Initialise theta max 
    m_theta_min.clear();
    m_theta_min.name("ThetaMin");
    m_theta_min.unit("degrees");
    m_theta_min.value(1.0e-6);      // can only go from halo center to opposite halo center
    m_theta_min.min(1.0e-10);       // arbitrarily chosen, some halos don't converge at theta=0.0
    m_theta_min.fix();              // should always be fixed!
    m_theta_min.scale(1.0);
    m_theta_min.gradient(0.0);
    m_theta_min.has_grad(false);    // Radial components never have gradients

    // Initialise theta max 
    m_theta_max.clear();
    m_theta_max.name("ThetaMax");
    m_theta_max.unit("degrees");
    m_theta_max.value(180.0);       // can only go from halo center to opposite halo center
    m_theta_max.min(1.0e-6);        // arbitrarily chosen
    m_theta_max.fix();              // should always be fixed!
    m_theta_max.scale(1.0);
    m_theta_max.gradient(0.0);
    m_theta_max.has_grad(false);   // Radial components never have gradients
    
    // Initialise core radius
    m_core_radius.clear();
    m_core_radius.name("CoreRadius");
    m_core_radius.unit("kpc");
    m_core_radius.value(10.0);
    m_core_radius.min(0.0);
    m_core_radius.fix();
    m_core_radius.scale(1.0);
    m_core_radius.gradient(0.0);
    m_core_radius.has_grad(false);

    // Set parameter pointer(s)
    m_pars.push_back(&m_scale_radius);
    m_pars.push_back(&m_scale_density);
    m_pars.push_back(&m_halo_distance);
    m_pars.push_back(&m_alpha);
    m_pars.push_back(&m_beta);
    m_pars.push_back(&m_gamma);
    m_pars.push_back(&m_theta_min);
    m_pars.push_back(&m_theta_max);
    m_pars.push_back(&m_core_radius);
    
    // Initialize precomputation cache. Note that zero values flag
    // uninitialised, as a zero radius is not meaningful
    m_last_scale_radius     = 0.0;
    m_last_scale_density    = 0.0;
    m_mass_radius           = 0.0;
    m_scale_density_squared = 0.0;

    // Return
    return;
}


/***********************************************************************//**
 * @brief Copy class members
 *
 * @param[in] model Radial DMZhao model.
 *
 * Copies class members from another radial profile model.
 ***************************************************************************/
void GModelSpatialRadialProfileDMZhao::copy_members(const GModelSpatialRadialProfileDMZhao& model)
{
    // Copy members. 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.
    m_scale_radius  = model.m_scale_radius;
    m_scale_density = model.m_scale_density;
    m_halo_distance = model.m_halo_distance;
    m_alpha         = model.m_alpha;
    m_beta          = model.m_beta;
    m_gamma         = model.m_gamma;
    m_theta_min     = model.m_theta_min;
    m_theta_max     = model.m_theta_max;
    m_core_radius   = model.m_core_radius;

    // copy cache values
    m_last_scale_radius     = model.m_last_scale_radius;
    m_last_scale_density    = model.m_last_scale_density;
    m_mass_radius           = model.m_mass_radius;
    m_scale_density_squared = model.m_scale_density_squared ;
    
    // Return
    return;
}


/***********************************************************************//**
 * @brief Delete class members
 ***************************************************************************/
void GModelSpatialRadialProfileDMZhao::free_members(void)
{
    // Return
    return;
}


/***********************************************************************//**
 * @brief Radial profile
 *
 * @param[in] theta Angular distance from DMZhao centre (radians).
 * @return Profile value.
 ***************************************************************************/
double GModelSpatialRadialProfileDMZhao::profile_value(const double& theta) const
{
    // Update precomputation cache
    update();

    // Initialize integral value
    double value = 0.0;

    // Set up integration limits
    double los_min = m_halo_distance.value() - m_mass_radius;
    double los_max = m_halo_distance.value() + m_mass_radius;

    // Set up integral
    halo_kernel_los integrand(m_scale_radius.value(),
                              m_halo_distance.value(),
                              m_alpha.value(),
                              m_beta.value(),
                              m_gamma.value(),
                              theta,
                              m_core_radius.value());
    GIntegral integral(&integrand);
    integral.max_iter(25);

    // Set up integration boundaries
    // As there is usually an infinity at the halo center, this splits
    // the integral at the m_halo_distance.
    std::vector<double> bounds;
    bounds.push_back(los_min);
    bounds.push_back(los_max);
    bounds.push_back(m_halo_distance.value());

    // Compute value
    value = integral.romberg(bounds);

    // apply scale density squared
    // TODO: must be multiplied by the particle physics factor
    value *= m_scale_density_squared;

    // Return value
    return value;
}


/***********************************************************************//**
 * @brief Kernel for zhao halo density profile squared
 *
 * @param[in] distance from observer to point in space (meters)
 * @return Jalo density.
 *
 * Computes the value of a zhao halo density profile squared, 
 * at distance l from observer, at angle \f[\theta\f] from the halo center,
 * with a halo posessing a scale radius of \f[r_s\f] :
 * 
 * \f[
 *    f(\theta, l) = \frac{1}{ g^{\gamma} {\left( g^{\alpha} + 1 \right)}^{ \frac{\beta-\gamma}{\alpha} } }
 * \f]
 *
 * where
 *
 * \f[
 *    g = \frac{ \sqrt{l^2+d^2-2ldCos(\theta)} }{r_s}
 * \f]
 *
 * \f[ \beta \f] is the slope of the density at radii much bigger than \f[r_s\f]
 * \f[ \gamma \f] is the slope of the density profile at radii much smaller than \f[r_s\f]
 * \f[ \alpha \f] governs how large the transition region is between these two slopes.
 *   larger \f[ \alpha \f] means a smaller transition region.
 *
 * This profile is detailed in:
 *   Zhao, 1996
 *   "Analytical models for galactic nuclei"
 *   Monthly Notices of the Royal Astronomical Society, 278, 488-49
 *   http://mnras.oxfordjournals.org/content/278/2/488.short
 *
 *
 ***************************************************************************/
double GModelSpatialRadialProfileDMZhao::halo_kernel_los::eval(const double &los)
{
    // Calculate the scale distance g, the ( distance from integration point
    // to the halo center ) divided by ( the halo scale radius )
    
    // First calculate the distance of the integration point from the halo
    // center via the law of cosines
    double g  = los * los;
    g        += m_halo_distance * m_halo_distance;
    g        -= 2.0 * los * m_halo_distance * std::cos(m_theta);
    g         = std::sqrt(g);
    
    // If we have a core radius specified, all halo values inside this core
    // radius should be the same as at the core radius itself.
    if (g < m_core_radius) {
        g = m_core_radius;
    }
    
    // Finish scaling the integration point by the halo's scale radius
    g /= m_scale_radius ;
  
    // apply the zhao profile from the scaled distance to the halo center
    double f  = std::pow(g, m_alpha);
    f        += 1.0;
    f         = std::pow(f, (m_beta-m_gamma)/m_alpha);
    f        *= std::pow(g, m_gamma);
    f         = 1.0 / f;

    // Squared, for annihilating DM, would just be f if it was decaying DM
    f = f * f;

    // Return function value
    return f;
}


/***********************************************************************//**
 * @brief Update precomputation cache
 *
 * Computes the m_mass_radius calculation, determining the radius around
 * the halo that contains 99.99% of the mass. For a Zhao halo profile,
 * this is just 10.0 * scale_radius .
 ***************************************************************************/
void GModelSpatialRadialProfileDMZhao::update() const
{
    // Update if scale radius has changed
    if (m_last_scale_radius  != scale_radius() ||
        m_last_scale_density != scale_density() ) {

        // Store last values
        m_last_scale_radius = scale_radius();
        m_last_scale_density = scale_density();

        // perform precomputations
        m_mass_radius = 10.0 * scale_radius();
        m_scale_density_squared = scale_density() * scale_density() ;

    }

    // Return
    return;
}


/***********************************************************************//**
 * @brief Calculate Halo Mass Density
 *
 * @param[in] distance from halo center (kpc)
 *
 * Calculates the halo's mass density at a given radial distance from the halo
 * center.
 *
 ***************************************************************************/
double GModelSpatialRadialProfileDMZhao::mass_density(const double& radius) const
{
    // Initialize halo kernel with stored values. We use theta=0 to effectivly
    // sample the halo radially
    halo_kernel_los halo_shape(m_scale_radius.value(),
                               m_halo_distance.value(),
                               m_alpha.value(),
                               m_beta.value(),
                               m_gamma.value(),
                               0.0,
                               m_core_radius.value());
  
    // eval produces a unitless density^2, so we must take its square root
    double density = std::sqrt(halo_shape.eval(m_halo_distance.value() + radius));
    
    // Multiply in the missing scale density
    density *= m_scale_density.value();
    
    return density ;
}


/***********************************************************************//**
 * @brief Calculate J-factor
 *
 * @param[in] angle from halo center (radians)
 * @return J-factor
 *
 * Calculates the halo's J-factor at an angle from the halo center.
 ***************************************************************************/
double GModelSpatialRadialProfileDMZhao::jfactor(const double& angle) const
{
    // Integration settings
    double minradian = 0.0;
    int    npoints   = 200;
  
    // Initialize other variables
    double jfactor = 0.0;
    double dr      = (angle - minradian) / npoints;
    double r       = 0.0;
  
    // Loop over different radii in the profile
    for (int i = 0; i < npoints; ++i) {
        r        = minradian + (i * dr);
        jfactor += profile_value(r) * r * dr;
    }
  
    // J-factor = 2 * pi * Int[ profile(r) * r * dr , {r,minradian,angle} ]
    jfactor *= gammalib::twopi;

    // Return J-factor
    return jfactor;
}