def testAxi_phiIsNone(): R = 1; z = 0; phi = 1.1; scf = SCFPotential() assert scf(R,z,None) == scf(R,z,phi), "The axisymmetric potential does not work at phi=None" assert scf.dens(R,z,None) == scf.dens(R,z,phi), "The axisymmetric density does not work at phi=None" assert scf.Rforce(R,z,None) == scf.Rforce(R,z,phi), "The axisymmetric Rforce does not work at phi=None" assert scf.zforce(R,z,None) == scf.zforce(R,z,phi), "The axisymmetric zforce does not work at phi=None" assert scf.phiforce(R,z,None) == scf.phiforce(R,z,phi), "The axisymmetric phiforce does not work at phi=None"
def test_coeffs_AsinNone_LnotequalM(): Acos = numpy.ones((2, 3, 4)) try: SCFPotential(Acos=Acos) raise Exception("Expected RuntimeError") except RuntimeError: pass
def test_coeffs_AsinNone_AcosNotaxisym(): Acos = numpy.ones((2, 3, 3)) try: SCFPotential(Acos=Acos) raise Exception("Expected RuntimeError") except RuntimeError: pass
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)
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)
def test_coeffs_toolittledimensions(): Acos = numpy.ones((10, 2)) try: SCFPotential(Acos=Acos) raise Exception("Expected RuntimeError") except RuntimeError: pass
def testArrayBroadcasting(): scf = SCFPotential() R = numpy.ones((10, 20, 2)) z = numpy.linspace(0, numpy.pi, 10)[:, None, None] phi = numpy.zeros((10, 20))[:, :, None] ArrayTest(scf, [R, z, phi])
def test_coeffs_Acos_L_M_notLowerTriangular(): Acos = numpy.ones((2, 3, 3)) Asin = numpy.zeros((2, 3, 3)) try: SCFPotential(Acos=Acos, Asin=Asin) raise Exception("Expected RuntimeWarning") except RuntimeWarning: pass
def test_coeffs_AsinShape_notequal_AcosShape(): Acos = numpy.ones((2, 3, 3)) Asin = numpy.ones((2, 2, 2)) try: SCFPotential(Acos=Acos, Asin=Asin) raise Exception("Expected RuntimeError") except RuntimeError: pass
def calc_potential(self, sample=True, frac=0.01, a=[27.68, 3.41, 0.536], N=[5, 15, 3], L=[5, 15, 3]): try: print('data/equil_potential_coefficients_N' + str(N[0]) + str(N[1]) + str(N[2]) + '.txt') with open( 'data/equil_potential_coefficients_N' + str(N[0]) + str(N[1]) + str(N[2]) + '.txt', 'rb') as f: Acos, Asin = np.load(f, allow_pickle=True) pot = [ SCFPotential(Acos=Acos[i], Asin=Asin[i], a=a[i] / 8.) for i in range(3) ] except: ncomp = [self.Nh, self.Nd, self.Nb] Acos, Asin = np.empty((2, 3), dtype='object') pot = np.empty(3, dtype='object') fullsample = self.sample(frac=frac) masses = list(set(fullsample[:, 0])) for i, n in enumerate(ncomp): samples = fullsample[tuple([fullsample[:, 0] == masses[i]])] Acos[i], Asin[i] = scf_compute_coeffs_nbody( samples[:, 1:4].T / self.MLU, samples[:, 0] / self.MMU, N[i], L[i], a=a[i] / 8.) pot[i] = SCFPotential(Acos=Acos[i], Asin=Asin[i], a=a[i] / 8.) coeff = np.vstack([Acos, Asin]) with open( 'data/equil_potential_coefficients_N' + str(N[0]) + str(N[1]) + str(N[2]) + '.txt', 'wb') as f: np.save(f, coeff) self.pot = list(pot) return list(pot)
def test_FutureWarning_multid_indexing(): scf = SCFPotential() array = numpy.linspace(0, 3, 100) #Turn warnings into errors to test for them import warnings with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always",FutureWarning) ArrayTest(scf,[array,1.,0]) raisedWarning= False for wa in w: raisedWarning= ('Using a non-tuple sequence for multidimensional indexing is deprecated' in str(wa.message)) if raisedWarning: break assert not raisedWarning, "SCFPotential should not raise 'FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated ...', but did" return None
def test_coeffs_AsinNone_MequalsL(): Acos = numpy.zeros((2, 3, 3)) Asin = None SCFPotential(Acos=Acos, Asin=Asin)
def test_phiforceMatches_nfw(): nfw = potential.NFWPotential() Acos, Asin = potential.scf_compute_coeffs_spherical(rho_NFW, 10) scf = SCFPotential(amp=1, Acos=Acos, Asin=Asin) assertmsg = "Comparing the azimuth force of NFW Potential with SCF fails at R={0}, Z={1}, phi={2}" compareFunctions(nfw.phiforce, scf.phiforce, assertmsg)
def test_zforceMatches_nfw(): nfw = potential.NFWPotential() Acos, Asin = potential.scf_compute_coeffs_spherical(rho_NFW, 50, a=50) scf = SCFPotential(amp=1, Acos=Acos, Asin=Asin, a=50) assertmsg = "Comparing the vertical force of NFW Potential with SCF fails at R={0}, Z={1}, phi={2}" compareFunctions(nfw.zforce, scf.zforce, assertmsg, eps=1e-3)
def test_potentialMatches_nfw(): nfw = potential.NFWPotential() Acos, Asin = potential.scf_compute_coeffs_spherical(rho_NFW, 50, a=50) scf = SCFPotential(amp=1, Acos=Acos, Asin=Asin, a=50) assertmsg = "Comparing nfw with SCF fails at R={0}, Z={1}, phi={2}" compareFunctions(nfw, scf, assertmsg, eps=1e-4)
def test_densMatches_axi_density2(): Acos, Asin = potential.scf_compute_coeffs_axi(axi_density2, 50, 3) scf = SCFPotential(amp=1, Acos=Acos, Asin=Asin) assertmsg = "Comparing axi_density2 with SCF fails at R={0}, Z={1}, phi={2}" compareFunctions(axi_density2, scf.dens, assertmsg, eps=1e-3)
def testArray_phiArray(): scf = SCFPotential() array = numpy.linspace(0, 3, 100) ArrayTest(scf, [1., 1., array])
def testArray_RArray(): scf = SCFPotential() array = numpy.linspace(0, 3, 100) ArrayTest(scf, [array, 1., 0])
def test_densMatches_zeeuw(): Acos, Asin = potential.scf_compute_coeffs_spherical(rho_Zeeuw, 10) scf = SCFPotential(amp=1, Acos=Acos, Asin=Asin) assertmsg = "Comparing the density of Zeeuw's perfect ellipsoid with SCF fails at R={0}, Z={1}, phi={2}" compareFunctions(rho_Zeeuw, scf.dens, assertmsg)
def MWPotentialSCFbar(mbar, Acos, Asin, rs=1., normalize=False, pat_speed=40., fin_phi_deg=27., t_stream_age=5., t_on=2., tgrow=2): ''' t_stream_age : age of the stream/max stripping time t_on: time in Gyr in the past at which the bar acquired full strength tgrow: no of bar periods it took the bar to grow to full strength starting at tform ''' #setup the full strength bar and axisymmetric "bar" a = rs / ro omegaP = pat_speed * (ro / vo) fin_phi = np.radians(fin_phi_deg) t_stream_age = t_stream_age / bovy_conversion.time_in_Gyr(vo, ro) Tbar = 2. * np.pi / omegaP #bar period in galpy units. t_on = t_on / bovy_conversion.time_in_Gyr(vo, ro) tsteady = tgrow * Tbar tform = t_on + tsteady init_phi = fin_phi - omegaP * t_stream_age / bovy_conversion.time_in_Gyr( vo, ro) mrat = mbar / 10.**10. #10^10 mass of bar used to compute Acos and Asin static_bar = SCFPotential(amp=mrat, Acos=Acos, Asin=Asin, a=a, normalize=normalize) #Note only m=0 terms are considered static_axi_bar = SCFPotential(amp=mrat, Acos=np.atleast_3d(Acos[:, :, 0]), a=a) barrot = potential.SolidBodyRotationWrapperPotential(pot=static_bar, omega=omegaP, ro=ro, vo=vo, pa=init_phi) if mbar <= 5. * 10**9.: MWP2014SCFbar = [ MWPotential2014[0], MiyamotoNagaiPotential(amp=(6.8 - mrat) * 10.**10 * u.Msun, a=3. / 8., b=0.28 / 8.), MWPotential2014[2], barrot ] turn_physical_off(MWP2014SCFbar) #setup the corresponding axisymmetric bar MWP2014SCFnobar = [ MWPotential2014[0], MiyamotoNagaiPotential(amp=(6.8 - mrat) * 10.**10 * u.Msun, a=3. / 8., b=0.28 / 8.), MWPotential2014[2], static_axi_bar ] turn_physical_off(MWP2014SCFnobar) else: MWP2014SCFbar = [ MiyamotoNagaiPotential(amp=(6.8 + 0.5 - mrat) * 10.**10 * u.Msun, a=3. / 8., b=0.28 / 8.), MWPotential2014[2], barrot ] turn_physical_off(MWP2014SCFbar) MWP2014SCFnobar = [ MiyamotoNagaiPotential(amp=(6.8 + 0.5 - mrat) * 10.**10 * u.Msun, a=3. / 8., b=0.28 / 8.), MWPotential2014[2], static_axi_bar ] turn_physical_off(MWP2014SCFnobar) #setup Dehnen smooth growth wrapper for the bar #convert to galpy units #if t_on >= t_pal5_age, then Pal 5 sees the bar as always on if t_on >= t_stream_age: return (MWP2014SCFbar, MWP2014SCFnobar) elif tform >= t_stream_age: print("tform > age of Pal 5 stream") elif tform < t_stream_age: #change tform in the past, i.e. instead of from today, to time in the future from 5 Gyr in the past tform = t_stream_age - tform MWbar_grow = DehnenWrap(amp=1., pot=MWP2014SCFbar, tform=tform, tsteady=tsteady) MWaxibar_destroy = DehnenWrap(amp=-1., pot=MWP2014SCFnobar, tform=tform, tsteady=tsteady) growbarpot = [MWbar_grow, MWP2014SCFnobar, MWaxibar_destroy] turn_physical_off(growbarpot) return (growbarpot, MWP2014SCFnobar)