Ejemplo n.º 1
0
def test_nemo_HernquistPotential():
    hp = potential.HernquistPotential(normalize=1., a=3.)
    tmax = 3.
    vo, ro = 210., 7.5
    o = Orbit([1., 0.25, 1.4, 0.3, -0.1, 0.4], ro=ro, vo=vo)
    run_orbitIntegration_comparison(o, hp, tmax, vo, ro)
    return None
Ejemplo n.º 2
0
def setup_potential_kuepper(Mhalo: float, a: float) -> List[Potential]:
    """Set up Kuepper et al. (2015) Potential.

    Parameters
    ----------
    Mhalo: float
        mass / 10^12 Msun
    a: float
        scale length / kpc,

    Returns
    -------
    potential : list
        HernquistPotential + MiyamotoNagaiPotential + NFWPotential

    """
    pot: List[Potential] = [
        potential.HernquistPotential(amp=3.4e10 * u.Msun, a=0.7 * u.kpc),
        potential.MiyamotoNagaiPotential(amp=1e11 * u.Msun,
                                         a=6.5 * u.kpc,
                                         b=0.26 * u.kpc),
        potential.NFWPotential(amp=Mhalo * 1e12 * u.Msun, a=a * u.kpc),
    ]

    return pot
Ejemplo n.º 3
0
def test_isotropic_hernquist_singlerphi_is_atsinglephi():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    dfh= isotropicHernquistdf(pot=pot)
    numpy.random.seed(10)
    samp= dfh.sample(R=1.3,z=0.,phi=numpy.pi-0.3,n=100000)
    assert numpy.all(numpy.fabs(samp.phi()-numpy.pi+0.3) < 1e-8), 'Sampling a spherical distribution function at a single r and phi oes not produce orbits at a single phi'
    return None
Ejemplo n.º 4
0
def compare_dens_nbody_spherical():
    N = int(1e6)
    Mh = 11.
    ah = 50. / 8.
    m = Mh / N
    factor = 1.
    nsamp = 10
    Norder = 10

    hern = potential.HernquistPotential(amp=2 * Mh, a=ah)
    hern.turn_physical_off()
    hdf = df.isotropicHernquistdf(hern)
    samples = [hdf.sample(n=N) for i in range(nsamp)]

    positions = numpy.array(
        [[samples[i].x(), samples[i].y(), samples[i].z() * factor]
         for i in range(nsamp)])

    c = numpy.zeros((nsamp, Norder, 1, 1))
    s = numpy.zeros((nsamp, Norder, 1, 1))
    for i in range(nsamp):
        c[i], s[i] = potential.scf_compute_coeffs_spherical_nbody(
            m=m * numpy.ones(N), pos=positions[i], N=Norder, a=ah)

    cc, ss = potential.scf_compute_coeffs_spherical(N=Norder,
                                                    a=ah,
                                                    dens=hern.dens)

    # Check that the difference between the coefficients is within the standard deviation
    assert (cc - numpy.mean(c, axis=0) < numpy.std(c, axis=0)).all()
Ejemplo n.º 5
0
def test_isotropic_hernquist_meanvr_directint():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    dfh= isotropicHernquistdf(pot=pot)
    tol= 1e-8
    check_meanvr_directint(dfh,pot,tol,beta=0.,rmin=pot._scale/10.,
                           rmax=pot._scale*10.,bins=31)
    return None
Ejemplo n.º 6
0
    def setup_class(cls):
        """Setup fixtures for testing."""
        super().setup_class()

        nx = ny = nz = 76  # must be int and even
        nxr0 = nyr0 = nzr0 = 2.3 * 2

        X, Y, Z = (np.array(
            np.meshgrid(
                np.linspace(-nxr0 / 2, nxr0 / 2, nx),
                np.linspace(-nyr0 / 2, nyr0 / 2, ny),
                np.linspace(-nzr0 / 2, nzr0 / 2, nz),
                indexing="ij",
            ), ) * 1)
        XYZ = coord.CartesianRepresentation(X, Y, Z, unit=u.kpc)

        # THIRD PARTY
        import galpy.potential as gpot

        cls.potential = gpot.HernquistPotential()
        cls.meshgrid = XYZ

        cls.inst = cls.obj(
            PotentialWrapper(cls.potential),
            cls.meshgrid,
            total_mass=10 * u.solMass,
        )
Ejemplo n.º 7
0
def test_potentialMatches_hernquist():
    h = potential.HernquistPotential()
    Acos, Asin = potential.scf_compute_coeffs_spherical(
        sphericalHernquistDensity, 10)
    scf = SCFPotential()
    assertmsg = "Comparing the potential of Hernquist Potential with SCF fails at R={0}, Z={1}, phi={2}"
    compareFunctions(h, scf, assertmsg)
Ejemplo n.º 8
0
def test_phiforceMatches_hernquist():
    h = potential.HernquistPotential()
    Acos, Asin = potential.scf_compute_coeffs_spherical(
        sphericalHernquistDensity, 1)
    scf = SCFPotential(amp=1, Acos=Acos, Asin=Asin)
    assertmsg = "Comparing the azimuth force of Hernquist Potential with SCF fails at R={0}, Z={1}, phi={2}"
    compareFunctions(h.phiforce, scf.phiforce, assertmsg)
Ejemplo n.º 9
0
def test_anisotropic_hernquist_energyoutofbounds():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    betas= [-0.7,-0.5,-0.4,0.,0.3,0.5]
    for beta in betas:
        dfh= constantbetaHernquistdf(pot=pot,beta=beta)
        assert numpy.all(numpy.fabs(dfh((numpy.arange(0.1,10.,0.1),1.1))) < 1e-8), 'Evaluating the isotropic Hernquist DF at E > 0 does not give zero'
        assert numpy.all(numpy.fabs(dfh((pot(0,0)-1e-4,1.1))) < 1e-8), 'Evaluating the isotropic Hernquist DF at E < -GM/a does not give zero'
    return None
Ejemplo n.º 10
0
def test_isotropic_hernquist_unittransfer():
    from galpy.util import conversion
    ro, vo= 9., 210.
    pot= potential.HernquistPotential(amp=2.,a=1.3,ro=ro,vo=vo)
    dfh= isotropicHernquistdf(pot=pot)
    phys= conversion.get_physical(dfh,include_set=True)
    assert phys['roSet'], "sphericaldf's ro not set when that of the underlying potential is set"
    assert phys['voSet'], "sphericaldf's vo not set when that of the underlying potential is set"
    assert numpy.fabs(phys['ro']-ro) < 1e-8, "Potential's unit system not correctly transfered to sphericaldf's"
    assert numpy.fabs(phys['vo']-vo) < 1e-8, "Potential's unit system not correctly transfered to sphericaldf's"
    # Following should not be on
    pot= potential.HernquistPotential(amp=2.,a=1.3)
    dfh= isotropicHernquistdf(pot=pot)
    phys= conversion.get_physical(dfh,include_set=True)
    assert not phys['roSet'], "sphericaldf's ro set when that of the underlying potential is not set"
    assert not phys['voSet'], "sphericaldf's vo set when that of the underlying potential is not set"
    return None
Ejemplo n.º 11
0
def test_isotropic_hernquist_unitsofsamples():
    from galpy.util import conversion
    ro, vo= 9., 210.
    pot= potential.HernquistPotential(amp=2.,a=1.3,ro=ro,vo=vo)
    dfh= isotropicHernquistdf(pot=pot)
    samp= dfh.sample(n=100)
    assert conversion.get_physical(samp,include_set=True)['roSet'], 'Orbit samples from spherical DF with units on do not have units on'
    assert conversion.get_physical(samp,include_set=True)['voSet'], 'Orbit samples from spherical DF with units on do not have units on'
    assert numpy.fabs(conversion.get_physical(samp,include_set=True)['ro']-ro) < 1e-8, 'Orbit samples from spherical DF with units on do not have correct ro'
    assert numpy.fabs(conversion.get_physical(samp,include_set=True)['vo']-vo) < 1e-8, 'Orbit samples from spherical DF with units on do not have correct vo'
    # Also test a case where they should be off
    pot= potential.HernquistPotential(amp=2.,a=1.3)
    dfh= isotropicHernquistdf(pot=pot)
    samp= dfh.sample(n=100)
    assert not conversion.get_physical(samp,include_set=True)['roSet'], 'Orbit samples from spherical DF with units off do not have units off'
    assert not conversion.get_physical(samp,include_set=True)['voSet'], 'Orbit samples from spherical DF with units off do not have units off'
    return None
Ejemplo n.º 12
0
def test_isotropic_hernquist_sigmar():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    dfh= isotropicHernquistdf(pot=pot)
    numpy.random.seed(10)
    samp= dfh.sample(n=300000)
    tol= 0.05
    check_sigmar_against_jeans(samp,pot,tol,beta=0.,
                               rmin=pot._scale/10.,rmax=pot._scale*10.,bins=31)
    return None
Ejemplo n.º 13
0
def test_isotropic_hernquist_sigmar_directint_forcevmoment():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    dfh= isotropicHernquistdf(pot=pot)
    tol= 1e-5
    check_sigmar_against_jeans_directint_forcevmoment(dfh,pot,tol,beta=0.,
                                                      rmin=pot._scale/10.,
                                                      rmax=pot._scale*10.,
                                                      bins=31)
    return None
Ejemplo n.º 14
0
def test_isotropic_hernquist_dens_directint():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    dfh= isotropicHernquistdf(pot=pot)
    tol= 1e-8
    check_dens_directint(dfh,pot,tol,
                         lambda r: pot.dens(r,0)/(2.3/2.), # need to divide by mass
                         rmin=pot._scale/10.,
                         rmax=pot._scale*10.,bins=31)
    return None
Ejemplo n.º 15
0
def test_isotropic_hernquist_beta():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    dfh= isotropicHernquistdf(pot=pot)
    numpy.random.seed(10)
    samp= dfh.sample(n=1000000)
    tol= 6*1e-2
    check_beta(samp,pot,tol,beta=0.,
               rmin=pot._scale/10.,rmax=pot._scale*10.,bins=31)
    return None
Ejemplo n.º 16
0
def test_osipkovmerritt_hernquist_meanvr_directint():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    ras= [0.3,2.3,5.7]
    for ra in ras:
        dfh= osipkovmerrittHernquistdf(pot=pot,ra=ra)
        tol= 1e-8
        check_meanvr_directint(dfh,pot,tol,rmin=pot._scale/10.,
                               rmax=pot._scale*10.,bins=6)
    return None
Ejemplo n.º 17
0
def test_staeckel_fudge_delta():
    import galpy.potential as galpy_pot
    from galpy.actionAngle import estimateDeltaStaeckel

    ro = 8.1 * u.kpc
    vo = 229 * u.km / u.s

    paired_potentials = []

    # Miyamoto-Nagai
    potential = gp.MiyamotoNagaiPotential(m=6e10 * u.Msun,
                                          a=3 * u.kpc,
                                          b=0.3 * u.kpc,
                                          units=galactic)
    amp = (G * potential.parameters['m']).to_value(vo**2 * ro)
    a = potential.parameters['a'].to_value(ro)
    b = potential.parameters['b'].to_value(ro)
    galpy_potential = galpy_pot.MiyamotoNagaiPotential(amp=amp,
                                                       a=a,
                                                       b=b,
                                                       ro=ro,
                                                       vo=vo)
    paired_potentials.append((potential, galpy_potential))

    # Hernquist
    potential = gp.HernquistPotential(m=6e10 * u.Msun,
                                      c=0.3 * u.kpc,
                                      units=galactic)
    amp = (G * potential.parameters['m']).to_value(vo**2 * ro)
    a = potential.parameters['c'].to_value(ro)
    galpy_potential = galpy_pot.HernquistPotential(amp=amp, a=a, ro=ro, vo=vo)
    paired_potentials.append((potential, galpy_potential))

    # NFW
    potential = gp.NFWPotential(m=6e11 * u.Msun,
                                r_s=15.6 * u.kpc,
                                units=galactic)
    amp = (G * potential.parameters['m']).to_value(vo**2 * ro)
    a = potential.parameters['r_s'].to_value(ro)
    galpy_potential = galpy_pot.NFWPotential(amp=amp, a=a, ro=ro, vo=vo)
    paired_potentials.append((potential, galpy_potential))

    # TEST:
    N = 1024
    rnd = np.random.default_rng(42)
    w = PhaseSpacePosition(pos=rnd.uniform(-10, 10, size=(3, N)) * u.kpc,
                           vel=rnd.uniform(-100, 100, size=(3, N)) * u.km /
                           u.s)

    R = w.cylindrical.rho.to_value(ro)
    z = w.z.to_value(ro)

    for p, galpy_p in paired_potentials:
        galpy_deltas = estimateDeltaStaeckel(galpy_p, R, z, no_median=True)
        gala_deltas = get_staeckel_fudge_delta(p, w).value
        print(p, np.allclose(gala_deltas, galpy_deltas))
        assert np.allclose(gala_deltas, galpy_deltas, atol=1e-6)
Ejemplo n.º 18
0
def test_isotropic_hernquist_incompatibleunits():
    pot= potential.HernquistPotential(amp=2.,a=1.3,ro=9.,vo=210.)
    with pytest.raises(RuntimeError):
        dfh= isotropicHernquistdf(pot=pot,ro=8.,vo=210.)
    with pytest.raises(RuntimeError):
        dfh= isotropicHernquistdf(pot=pot,ro=9.,vo=230.)
    with pytest.raises(RuntimeError):
        dfh= isotropicHernquistdf(pot=pot,ro=8.,vo=230.)
    return None
Ejemplo n.º 19
0
def test_anisotropic_hernquist_meanvr_directint():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    betas= [-0.7,-0.5,-0.4,0.,0.3,0.5]
    for beta in betas:
        dfh= constantbetaHernquistdf(pot=pot,beta=beta)
        tol= 1e-8
        check_meanvr_directint(dfh,pot,tol,beta=beta,rmin=pot._scale/10.,
                               rmax=pot._scale*10.,bins=31)
    return None
Ejemplo n.º 20
0
def test_osipkovmerritt_hernquist_Qoutofbounds():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    ras= [0.3,2.3,5.7]
    for ra in ras:
        dfh= osipkovmerrittHernquistdf(pot=pot,ra=ra)
        assert numpy.all(numpy.fabs(dfh((numpy.arange(0.1,10.,0.1),1.1))) < 1e-8), 'Evaluating the Osipkov-Merritt Hernquist DF at E > 0 does not give zero'
        # The next one is not actually a physical orbit...
        assert numpy.all(numpy.fabs(dfh((pot(0,0)-1e-1,0.1))) < 1e-8), 'Evaluating the Osipkov-Merritt Hernquist DF at E < -GM/a does not give zero'
        assert numpy.all(numpy.fabs(dfh((-1e-4,1.1))) < 1e-8), 'Evaluating the Osipkov-Merritt Hernquist DF at Q < 0 does not give zero'
    return None
Ejemplo n.º 21
0
def test_isotropic_hernquist_singler_sigmar():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    dfh= isotropicHernquistdf(pot=pot)
    numpy.random.seed(10)
    for r in [0.3,1.3,2.3]:
        samp= dfh.sample(R=r,z=0.,n=100000)
        tol= 0.01
        check_sigmar_against_jeans(samp,pot,tol,beta=0.,
                                   rmin=r-0.1,rmax=r+0.1,bins=1)
    return None
Ejemplo n.º 22
0
def test_anisotropic_hernquist_beta():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    betas= [-0.7,-0.5,-0.4,0.,0.3,0.5]
    for beta in betas:
        dfh= constantbetaHernquistdf(pot=pot,beta=beta)
        numpy.random.seed(10)
        samp= dfh.sample(n=1000000)
        tol= 8*1e-2 * (beta > -0.7) + 0.12 * (beta == -0.7)
        check_beta(samp,pot,tol,beta=beta,
                   rmin=pot._scale/10.,rmax=pot._scale*10.,bins=31)
    return None
Ejemplo n.º 23
0
def test_osipkovmerritt_hernquist_beta_directint():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    ras= [0.3,2.3,5.7]
    for ra in ras:
        dfh= osipkovmerrittHernquistdf(pot=pot,ra=ra)
        tol= 1e-8
        check_beta_directint(dfh,tol,beta=lambda r: 1./(1.+ra**2./r**2.),
                             rmin=pot._scale/10.,
                             rmax=pot._scale*10.,
                             bins=6)
    return None
Ejemplo n.º 24
0
def test_anisotropic_hernquist_negdf():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    # beta > 0.5 has negative DF parts
    dfh= constantbetaHernquistdf(pot=pot,beta=0.7)
    with pytest.warns(None) as record:
        samp= dfh.sample(n=100)
    raisedWarning= False
    for rec in record:
        # check that the message matches
        raisedWarning+= (str(rec.message.args[0]) == "The DF appears to have negative regions; we'll try to ignore these for sampling the DF, but this may adversely affect the generated samples. Proceed with care!")
    assert raisedWarning, "Using an anisotropic Hernquist DF that has negative parts should have raised a warning, but didn't"
Ejemplo n.º 25
0
def test_osipkovmerritt_hernquist_dens_directint():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    ras= [0.3,2.3,5.7]
    for ra in ras:
        dfh= osipkovmerrittHernquistdf(pot=pot,ra=ra)
        tol= 1e-5
        check_dens_directint(dfh,pot,tol,
                             lambda r: pot.dens(r,0)/(2.3/2.), # need to divide by mass
                             rmin=pot._scale/10.,
                             rmax=pot._scale*10.,bins=6)
    return None
Ejemplo n.º 26
0
def test_anisotropic_hernquist_dens_directint():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    betas= [-0.7,-0.5,-0.4,0.,0.3,0.5]
    for beta in betas:
        dfh= constantbetaHernquistdf(pot=pot,beta=beta)
        tol= 1e-7
        check_dens_directint(dfh,pot,tol,
                             lambda r: pot.dens(r,0)/(2.3/2.), # need to divide by mass
                             rmin=pot._scale/10.,
                             rmax=pot._scale*10.,bins=31)
    return None
Ejemplo n.º 27
0
def test_osipkovmerritt_hernquist_beta():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    ras= [0.3,2.3,5.7]
    for ra in ras:
        dfh= osipkovmerrittHernquistdf(pot=pot,ra=ra)
        numpy.random.seed(10)
        samp= dfh.sample(n=1000000)
        tol= 0.06
        check_beta(samp,pot,tol,beta=lambda r: 1./(1.+ra**2./r**2.),
                   rmin=pot._scale/10.,rmax=pot._scale*10.,bins=31)
    return None
Ejemplo n.º 28
0
def test_anisotropic_hernquist_dens_massprofile():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    betas= [-0.7,-0.5,-0.4,0.,0.3,0.5]
    for beta in betas:
        dfh= constantbetaHernquistdf(pot=pot,beta=beta)
        numpy.random.seed(10)
        samp= dfh.sample(n=100000)
        tol= 5*1e-3
        check_spherical_massprofile(samp,lambda r: pot.mass(r)\
                                    /pot.mass(numpy.amax(samp.r())),
                                    tol,skip=1000)
    return None
Ejemplo n.º 29
0
def test_anisotropic_hernquist_sigmar():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    betas= [-0.7,-0.5,-0.4,0.,0.3,0.5]
    for beta in betas:
        dfh= constantbetaHernquistdf(pot=pot,beta=beta)
        numpy.random.seed(10)
        samp= dfh.sample(n=100000)
        tol= 0.05
        check_sigmar_against_jeans(samp,pot,tol,beta=beta,
                                   rmin=pot._scale/10.,rmax=pot._scale*10.,
                                   bins=31)
    return None
Ejemplo n.º 30
0
def test_isotropic_hernquist_dens_massprofile():
    pot= potential.HernquistPotential(amp=2.3,a=1.3)
    dfh= isotropicHernquistdf(pot=pot)
    numpy.random.seed(10)
    samp= dfh.sample(n=100000)
    tol= 5*1e-3
    check_spherical_massprofile(samp,
                                lambda r: pot.mass(r)\
                                   /pot.mass(numpy.amax(samp.r()),
                                             ),
                                tol,skip=1000)
    return None