コード例 #1
0
def mrs(params):
    print 'ms'
    print params
    print profileclass.getmassfromzhao0(params[2], params[3], params[4],
                                        params[0], params[1], 243 / 2.163 * 2)
    return profileclass.getmassfromzhao0(params[2], params[3], params[4],
                                         params[0], params[1], 243 / 2.163 * 2)
コード例 #2
0
def chi2(params, rhoint, massint, rscale, r):

    #    if np.any(params<0) or params[3]<3 or params[2]>50  or params[1]>6 or  params[4]>10 or params[3]>100:
    #        print 'par',params
    #        return np.inf
    rhor = lambda params, r: zhaorho(r, 10**params[0], 10**params[1], params[
        2], params[3], params[4])

    w = np.where(rhoint(r) > 100)[0]
    mtot = lambda params: profileclass.getmassfromzhao0(
        params[2], params[3], params[4], 10**params[0], 10**params[1], r[w[-1]]
    )

    chi2 = 0
    npts = 0

    d2 = (np.log10(rhor(params, r[w])) - np.log10(rhoint(r[w])))**2 + (
        np.log10(mtot(params)) - np.log10(massint))**2
    if np.any(~np.isfinite(d2)):
        return np.inf

#    w=np.where((d2>0) & (d2<np.nanmean(d2)+3*np.std(d2)) & (np.isfinite(d2)))[0]
#w=np.where((d2>0) & (d2<2**2) & (np.isfinite(d2)) & (rhoint(r)>0))[0]

#return np.nansum(d2[w])/len(w)
    return np.nansum(d2)  #+params[4]**2
コード例 #3
0
def chi2(params,rhoint,massint,r):

    rhor=lambda params,r: potdenfunc.zhaorho(r,10**params[0],10**params[1],params[2],params[3],params[4])

    w=np.where(rhoint(r)>100)[0]
    mtot=lambda params: profileclass.getmassfromzhao0(params[2],params[3],params[4],10**params[0],10**params[1],r[w[-1]])


    chi2=0
    npts=0

    d2=(np.log10(rhor(params,r[w]))-np.log10(rhoint(r[w])))**2+(np.log10(mtot(params))-np.log10(massint))**2
    if np.any(~np.isfinite(d2)):
        return np.inf
                                                                                
    return np.nansum(d2)+params[4]**2
コード例 #4
0
def getafromsidm(vmax,
                 rmax,
                 age,
                 rvir,
                 gamma,
                 cross=1.0,
                 minr=.01 * units.kpc,
                 rmatch='half',
                 alpha=1,
                 beta=4,
                 mvirmatch=True):

    pnfw = profileclass.NFW(vmax=vmax, rmax=rmax, rvir=rvir)
    delta = (pnfw.mvir / (4.0 / 3 * np.pi * rvir**3)).to(units.M_sun /
                                                         units.kpc**3)
    if age == 0:
        if rmatch == 'half':
            r12 = getrhalfnfw.getrhalfnfw(pnfw.rs.value, pnfw.rvir.value)
            m12 = pnfw.mvir / 2.0
            #            rho0func=lambda rsc: p.get_mass(rmax).to(units.M_sun).value/getmassfromzhao0
            rho0func = lambda rsc: (
                pnfw.mvir.value / profileclass.getmassfromzhao0(
                    alpha, beta, gamma, 1 * units.M_sun / units.kpc**3, rsc *
                    units.kpc, rvir))
            afunc = lambda rsc: np.log10(
                profileclass.getmassfromzhao0(
                    alpha, beta, gamma,
                    rho0func(rsc) * units.M_sun / units.kpc**3, rsc * units.
                    kpc, r12 * units.kpc).value / m12.value)
            try:
                az = minimize_scalar(lambda rs: abs(afunc(rs)),
                                     bracket=[1E-6 * r12, .1 * r12,
                                              1E5 * r12])['x']
            except:
                return rmax.value / 2.163, pnfw.rho0
            return az, pnfw.rho0
        else:
            r12 = (rmax / 2.163).value
            return pnfw.rs.value, pnfw.rho0.to(units.M_sun /
                                               units.kpc**3).value
            rho12 = (pnfw.rho0.to(units.M_sun / units.kpc**3) / 4.0).value
            m12 = pnfw.get_mass(r12 * rmax.unit).to(units.M_sun).value
            rho0func = lambda rsc: rho12 * (r12 / rsc)**(gamma) * (
                1 + r12 / rsc)**(1.0 * (beta - gamma) / alpha)
            afunc = lambda rsc: np.log10(
                profileclass.getmassfromzhao0(
                    alpha, beta, gamma,
                    rho0func(rsc) * units.M_sun / units.kpc**3, rsc * units.
                    kpc, r12 * units.kpc).value / m12)
            az = minimize_scalar(lambda rs: abs(afunc(rs)),
                                 bracket=[1E-5 * r12, .1 * r12,
                                          1E5 * r12])['x']

        return rho0func(az), az

    x = SIDM_thermal_nob.get_sidm_fit_without_beffect([
        vmax.to(units.km / units.s).value,
        rmax.to(units.kpc).value, cross,
        age.to(units.Gyr).value,
        minr.to(units.kpc).value, 1.55 * rvir.to(units.kpc).value
    ])

    if rmatch == 'r1':
        rmatchuse = x[0][-1]

    rs = x[1]
    ms = x[2]
    rhos = x[3]
    #    plt.plot(rs,rhos,'g-.',label='SIDM no B',alpha=.2)
    #    plt.plot(rs,ms,'g-.',label='SIDM no B',alpha=.2)

    #    rhointerpfunc=lambda x: 10**interpolate.interp1d(np.log10(rs),np.log10(rhos))(np.log10(x))
    #    massinterpfunc=lambda x: 10**interpolate.interp1d(np.log10(rs),np.log10(ms))(np.log10(x))

    massinterpfunc = interpolate.interp1d(rs, ms)
    rhointerpfunc = interpolate.interp1d(rs, rhos)

    wrhohalf = np.where(rhos < .5 * rhos[0])[0]

    rvirsidm = minimize_scalar(
        lambda rvx: abs(
            np.log10(
                massinterpfunc(rvx) / (4.0 / 3 * np.pi * rvx**3) /
                (delta.value))),
        bracket=[.5 * rvir.value, rvir.value, 1.5 * rvir.value])['x']

    mvirsidm = massinterpfunc(rvirsidm)

    mvirsidm = pnfw.mvir.to(units.M_sun).value

    if rmatch == 'half':
        m12 = .5 * mvirsidm

    else:
        if rmatch != 'r1':
            rmatchuse = rmatch
        interpfunc = interpolate.interp1d(rs, ms)
        m12 = interpfunc(rmatchuse)
        #interpolate to get m at rmatch, this is the new m12
    w = np.where(ms < m12)[0]

    m1 = ms[w[-1]]
    m2 = ms[w[-1] + 1]
    r1 = rs[w[-1]]
    r2 = rs[w[-1] + 1]

    slope = (np.log10(m2) - np.log10(m1)) / (np.log10(r2) - np.log10(r1))

    r12 = 10**((np.log10(m12) - np.log10(m1)) / slope + np.log10(r1))
    rho12 = rhointerpfunc(r12)

    rho0 = x[0][0]
    if mvirmatch:
        #         mr=(massinterpfunc(rmatch)/pnfw.mvir.to(units.M_sun).value)**(1.0/(3.0-gamma))
        #         top=mr-1
        #         bottom=1./rvir.value-mr/rmatch
        #         print top/bottom
        #         return top/bottom
        ratio = 1.0 / 4.0
        rx = minimize_scalar(
            lambda rad: abs(rhointerpfunc(rad) - ratio * rho0),
            [min(rs), x[0][1], max(rs)])['x']
        print rx

        if gamma == 0:
            return rx / ((1 / ratio)**.25 - 1), rho0
        else:
            return minimize_scalar(lambda rhoa: abs((rx / rhoa)**gamma * (
                1 + rx / rhoa)**(3 - gamma) - ratio))['x'], rho0

    if rmatch == 'half':
        return r12 * (2**(1.0 / (3 - gamma)) - 1)

#    if rmatch=='half':
#        az=minimize_scalar(lambda rsc: abs(np.log10(profileclass.getmassfromzhao0(alpha,beta,gamma,1*units.M_sun/units.kpc**3,rsc*units.kpc,r12*units.kpc)/(profileclass.getmassfromzhao0(alpha,beta,gamma,1*units.M_sun/units.kpc**3,rsc*units.kpc,rs[-1]*units.kpc)/2.0))),bracket=[.01*r12,100*r12])['x'].value

#        a=r12*(2**(1.0/(3-gamma))-1)

#   else:

#rmx=lambda rsc: rsc*profileclass.getxmaxzhao0(alpha,beta,gamma)
#mrmx= lambda rsc: ((((vmax)**2)*rmx(rsc))/G).to(units.M_sun)
    rho0func = lambda rsc: rho12 * (r12 / rsc)**(gamma) * (1 + r12 / rsc)**(
        1.0 * (beta - gamma) / alpha)
    #    rho0func=lambda rsc: profileclass.getrho0frommvirzhao(alpha,beta,gamma,mvir,rsc,(mvir/(4.0/3*np.pi*rvir**3)))
    afunc = lambda rsc: np.log10(
        profileclass.getmassfromzhao0(
            alpha, beta, gamma,
            rho0func(rsc) * units.M_sun / units.kpc**3, rsc * units.kpc, r12 *
            units.kpc).value / m12)
    #bfunc=lambda (rh0,rsc): np.log10(profileclass.getmassfromzhao0(alpha,beta,gamma,rh0*units.M_sun/units.kpc**3,rsc*units.kpc,rmx(rsc*units.kpc)).value/(mrmx(rsc*units.kpc)).value)
    #mz=lambda (rh0,rsc): afunc([rh0,rsc])**2+bfunc([rh0,rsc])**2
    #az=minimize(mz,[ms[-1]/r12**3,r12])

    #        a=rmatchuse*((ms[-1]/m12)**(1.0/(3-gamma))-1)
    az = minimize_scalar(lambda rs: abs(afunc(rs)),
                         bracket=[1E-5 * r12, .1 * r12, 1E5 * r12])['x']

    #    rho0=x[0][0]*(minr.value/az)**(gamma)*(1+minr.value/az)**(1.0*(beta-gamma)/alpha)
    #    return az['x']
    return rho0func(az), az
コード例 #5
0
def fpwrapper(f,
              fh,
              todo=[
                  1548, 278, 838, 1233, 786, 1082, 542, 329, 1227, 885, 1076,
                  281, 924, 961, 826, 1436, 64, 1583, 999, 504, 343, 1120,
                  1204, 1106
              ],
              taufactor=1.0,
              getangmom='e',
              folder=['./']):

    if len(folder) == len(todo):
        svfolder = ['' for i in range(len(f[1].data))]

        for i in range(len(todo)):
            svfolder[todo[i]] = folder[i]

    else:
        svfolder = folder
    peris = getperi.getperi(f)

    alphastart, betastart, gammastart = 1, 4, 0

    iinf, rmaxinf, vmaxinf, mpeak, rvirinf = getinfallinfo.getinfallinfo(
        f,
        cross=1,
        minr=0.1,
        maxr=200,
        inrs=False,
        geta=False,
        mfield='mvir',
        alpha=alphastart,
        beta=betastart)

    print 'a'

    ainf0 = np.zeros(len(f[1].data))
    rho0inf0 = np.zeros(len(f[1].data))
    rscale0 = np.zeros(len(f[1].data))
    alpha0 = np.zeros(len(f[1].data))
    beta0 = np.zeros(len(f[1].data))
    gamma0 = np.zeros(len(f[1].data))

    fit0 = []

    for i in todo:
        asidm = getafromsidm.getafromsidm(vmaxinf[i] * units.km / units.s,
                                          rmaxinf[i] * units.kpc,
                                          f[1].data.time[i, iinf[i]] *
                                          units.Gyr,
                                          rvirinf[i] * units.kpc,
                                          0,
                                          alpha=alpha0[0],
                                          beta=beta0[0])
        #ainf0.append(int(asidm[0]))

        ainf0[i] = alphastart
        alpha0[i] = alphastart
        beta0[i] = betastart
        gamma0[i] = gammastart

        rho0inf0[i] = asidm[1]
        rscale0[i] = rmaxinf[i] / 2.163

        fit0.append(
            np.array([rho0inf0[i], rscale0[i], alpha0[i], beta0[i],
                      gamma0[i]]))

    print 'b'
    bestfits = []
    ifit = 0

    for i in todo:
        print 'i', i
        print peris[2][i]
        for j in range(len(peris[2][i])):
            print alpha0[i]

            ih = np.where(f[1].data.hostid0[i] == fh[1].data.id0)[0][0]
            ip = peris[2][i][j]

            tophysicalsize = 1.0 / .7 / (1 + fh[1].data.redshift[ih][ip])
            tophysicalmass = 1.0 / .7

            hostrho0 = profileclass.getnfwrho0(
                fh[1].data.mvir[ih][ip] * tophysicalmass,
                fh[1].data.rs[ih][ip] * tophysicalsize,
                fh[1].data.rvir[ih][ip] * tophysicalsize)
            muhosti = lambda x: munfw(x, hostrho0, fh[1].data.rs[ih][
                ip] * tophysicalsize, fh[1].data.rvir[ih][ip] * tophysicalsize)
            muhosthati = lambda x: muhatnfw(
                x, hostrho0, fh[1].data.rs[ih][ip] * tophysicalsize, fh[1].data
                .rvir[ih][ip] * tophysicalsize)

            whalo = np.where(np.isfinite(f[1].data.dhost[int(i)]))[0]
            nptslow = np.min([200, peris[2][i][j]] - np.min(whalo))
            nptshigh = np.min([200, np.max(whalo) - peris[2][i][j]])
            print nptslow, nptshigh
            print f[1].data.time[i][ip - nptslow:ip +
                                    nptshigh], f[1].data.dxhost[i][ip -
                                                                   nptslow:ip +
                                                                   nptshigh]

            plt.clf()
            plt.plot(
                f[1].data.time[i][ip - nptslow:ip + nptshigh],
                f[1].data.dxhost[i][ip - nptslow:ip + nptshigh] * 1000 *
                tophysicalsize)
            plt.plot(
                f[1].data.time[i][ip - nptslow:ip + nptshigh],
                f[1].data.dyhost[i][ip - nptslow:ip + nptshigh] * 1000 *
                tophysicalsize)
            plt.plot(
                f[1].data.time[i][ip - nptslow:ip + nptshigh],
                f[1].data.dzhost[i][ip - nptslow:ip + nptshigh] * 1000 *
                tophysicalsize)
            plt.savefig(svfolder[i] + 'post.png')

            print peris[1][i][j]
            print peris[1][i][j]

            print f[1].data.dxhost[i][ip - nptslow:ip + nptshigh], f[
                1].data.dyhost[i][ip - nptslow:ip +
                                  nptshigh], f[1].data.dzhost[i][ip -
                                                                 nptslow:ip +
                                                                 nptshigh]

            rthetai, thetamin, thetamax = rthetaint(
                f[1].data.dxhost[i][ip - nptslow:ip + nptshigh] * 1000 *
                tophysicalsize,
                f[1].data.dyhost[i][ip - nptslow:ip + nptshigh] * 1000 *
                tophysicalsize,
                f[1].data.dzhost[i][ip - nptslow:ip + nptshigh] * 1000 *
                tophysicalsize, f[1].data.time[i][ip - nptslow:ip + nptshigh],
                nptslow)

            print rthetai(0)
            print alpha0[i]
            plt.clf()
            plt.plot(np.linspace(-np.pi, np.pi),
                     rthetai(np.linspace(-np.pi, np.pi)))
            plt.ylim(0, 10000)
            plt.savefig(svfolder[i] + 'rtheta.png')
            angmomtot = lambda x: jthetaint(
                f[1].data.dxhost[i][ip - nptslow:ip + nptshigh] * 1000 *
                tophysicalsize, f[1].data.dyhost[i][ip - nptslow:ip + nptshigh]
                * 1000 * tophysicalsize, f[1].data.dzhost[i][
                    ip - nptslow:ip + nptshigh] * 1000 * tophysicalsize, f[1].
                data.vxwrthost[i][ip - nptslow:ip + nptshigh] / kminkpc, f[1].
                data.vywrthost[i][ip - nptslow:ip + nptshigh] / kminkpc, f[1].
                data.vzwrthost[i][ip - nptslow:ip + nptshigh] / kminkpc, f[
                    1].data.time[i][ip - nptslow:ip + nptshigh])(x)

            print 'a'
            print peris[1][i][j], rthetai(0)
            hostphi = lambda r: 4 * np.pi * hostrho0 * GN * (fh[1].data.rs[ih][
                ip] * tophysicalsize)**2 / r * np.log(1 + r / (fh[1].data.rs[
                    ih][ip] * tophysicalsize))
            if getangmom == 'e':
                sate = 0.5 / kminkpc**2 * (
                    f[1].data.vxwrthost[i][ip - 5:ip + 5]**2 +
                    f[1].data.vywrthost[i][ip - 5:ip + 5]**2 +
                    f[1].data.vzwrthost[i][ip - 5:ip + 5]**2) - hostphi(
                        f[1].data.dhost[i][ip - 5:ip + 5] * tophysicalsize)
                print 'e', sate, np.mean(sate), hostphi(
                    peris[1][i][j] * 1000 *
                    tophysicalsize), np.mean(sate) / hostphi(
                        peris[1][i][j] * 1000 * tophysicalsize)
                vp = np.sqrt(2 *
                             (np.mean(sate) -
                              hostphi(peris[1][i][j] * 1000 * tophysicalsize)))
                print 'vpp', vp, peris[1][i][
                    j] * 1000 * tophysicalsize * peris[4][i][j], np.sqrt(
                        GN * fh[1].data.mvir[ih, ip] * muhosti(rthetai(0)) /
                        rthetai(0))
                angmom = lambda x: (peris[1][i][j] * 1000 * tophysicalsize * vp
                                    ) / np.sqrt(GN * fh[1].data.rs[ih][
                                        ip] * tophysicalsize * fh[1].data.mvir[
                                            ih, ip] * tophysicalmass)
            else:
                print 'vp0', (peris[1][i][j] * 1000 *
                              tophysicalsize) * peris[4][i][j]
                angmom = lambda x: (peris[1][i][
                    j] * 1000 * tophysicalsize)**2 * peris[4][i][j] / np.sqrt(
                        GN * fh[1].data.rs[ih][ip] * tophysicalsize * fh[
                            1].data.mvir[ih, ip] * tophysicalmass)


#            angmom=lambda x:angmomtot(0)/np.sqrt(GN*fh[1].data.rs[ih][ip]*tophysicalsize*f[1].data.mvir[ih,ip]*tophysicalmass)
#            angmom=lambda x:(peris[1][i][j]*1000*tophysicalsize)**2*peris[4][i][j]/np.sqrt(GN*fh[1].data.rs[ih][ip]*tophysicalsize*f[1].data.mvir[ih,ip]*tophysicalmass)
#angmom=lambda x:np.sqrt(peris[1][i][j]*1000*tophysicalsize*muhosti(peris[1][i][j]*1000*tophysicalsize)/fh[1].data.rs[ih][ip]*tophysicalsize)
#angmom=lambda x:(peris[1][i][j]*1000*tophysicalsize*vp)/np.sqrt(GN*fh[1].data.rs[ih][ip]*tophysicalsize*f[1].data.mvir[ih,ip]*tophysicalmass)

            print alpha0[i]
            print angmom(0)
            eorbit = getetide.getetideorbit(
                muhosti,
                muhosthati,
                rthetai,
                fh[1].data.rs[ih][ip] * tophysicalsize,
                angmom,
                fh[1].data.mvir[ih, ip] * tophysicalmass,
                thetamin=thetamin,
                thetamax=thetamax,
                folder=svfolder[i])

            print alpha0[i]

            if j == 0:
                alpha = alpha0[i]
                beta = beta0[i]
                gamma = gamma0[i]
                rscale = rscale0[i]
                rho0halo = rho0inf0[i]

            print rho0halo, rscale, alpha, beta, gamma
            satpotfunc = lambda x: zhaophi(x, rho0halo, rscale, alpha, beta,
                                           gamma)  #+plummerphi(x,mstar,rstar)
            satdphidr = lambda x: dphidrzhao(x, rho0halo, rscale, alpha, beta,
                                             gamma
                                             )  #+plummerdphidr(x,mstar,rstar)
            satd2phidr2 = lambda x, d2: dphi2dr2zhao(
                x, rho0halo, rscale, alpha, beta, gamma
            )  #+plummerd2phidr2(x,mstar,rstar)
            satdrhodr = lambda x: drhodrzhao(x, rho0halo, rscale, alpha, beta,
                                             gamma)
            satd2rhodr2 = lambda x: d2rhodr2zhoa(x, rho0halo, rscale, alpha,
                                                 beta, gamma)
            print 'phi0', satpotfunc(1E-10)

            print profileclass.getmassfromzhao0(alpha, beta, gamma, rho0halo,
                                                rscale, peris[1][i][j] * 1000)
            #r12halo=minimize_scalar(lambda x: abs(profileclass.getmassfromzhao0(alpha,beta,gamma,rho0halo,rscale,x)-0.5*profileclass.getmassfromzhao0(alpha,beta,gamma,rho0halo,rscale,peris[1][i][j]*1000)),(rscale/10000,rscale,10000*rscale))['x']
            r12halo = rscale * (2**(1.0 / 3) - 1)**-1
            print r12halo
            tau12 = 1.0 / np.sqrt(GN * profileclass.getmassfromzhao0(
                alpha, beta, gamma, rho0halo, rscale, peris[1][i][j] * 1000) *
                                  3 / 8.0 / np.pi / r12halo**3)
            print tau12

            rpts = np.logspace(-1.2, 1, num=30) * rmaxinf[i] / 2.163

            #rhof=dofp.dofp(rthetai,muhosti,muhosthati,satpotfunc,satdphidr,satd2phidr2,[satdrhodr],[satd2rhodr2],fh[1].data.rs[ih][ip]*tophysicalsize,peris[4][i][j],f[1].data.mvir[ih,ip]*tophysicalmass,peris[1][i][j]*1000,rpts,gamma,angmom,orbit=eorbit,taufactor=taufactor,folder=svfolder[i],rhoparams=fit0[ifit])[0]
            rhof = dofp.dofp(rthetai,
                             muhosti,
                             muhosthati,
                             satpotfunc,
                             satdphidr,
                             satd2phidr2, [satdrhodr], [satd2rhodr2],
                             fh[1].data.rs[ih][ip] * tophysicalsize,
                             peris[4][i][j],
                             f[1].data.mvir[ih, ip] * tophysicalmass,
                             peris[1][i][j] * 1000,
                             rpts,
                             gamma,
                             angmom,
                             orbit=eorbit,
                             taufactor=taufactor,
                             folder=svfolder[i])[0]

            print rpts, rhof

            rhoint = interpolate.interp1d(rpts, rhof)
            wge0 = np.where(rhof >= 0)[0]
            mtot = np.trapz((rhof[wge0]) * rpts[wge0]**2 * 4 * np.pi,
                            x=rpts[wge0])

            print 'mt', mtot

            try:
                mins = minimize(chi2, [
                    np.log10(rho0halo) - .5,
                    np.log10(rscale), alpha, beta, gamma
                ],
                                args=(rhoint, mtot, rscale, rpts),
                                method='SLSQP',
                                bounds=[(np.log10(rho0halo) - 3,
                                         np.log10(rho0halo) + 3), (-2, 6),
                                        (.1, 3), (3, 50), (0, 3)])
            except:
                break

            plt.clf()
            plt.plot(np.logspace(-1, 1.5) * rmaxinf[i] / 2.163,
                     zhaorho(
                         np.logspace(-1, 1.5) * rmaxinf[i] / 2.163,
                         rho0inf0[i], rmaxinf[i] / 2.163, fit0[ifit][2],
                         fit0[ifit][3], fit0[ifit][4]),
                     label='before delta e')
            plt.loglog()
            plt.plot(np.logspace(-1, 1.5) * rmaxinf[i] / 2.163,
                     zhaorho(
                         np.logspace(-1, 1.5) * rmaxinf[i] / 2.163,
                         10**mins['x'][0], 10**mins['x'][1], mins['x'][2],
                         mins['x'][3], mins['x'][4]),
                     alpha=.2,
                     label='fit 1')
            plt.plot(rpts, rhoint(rpts), label='after delta e')
            plt.legend()
            plt.ylim(100, 1E9)
            p = profileclass.Zhao(
                mins['x'][2],
                mins['x'][3],
                mins['x'][4],
                rho0=10**mins['x'][0] * units.M_sun / units.kpc**3,
                rs=10**mins['x'][1] * units.kpc,
                deltavirrhou=30000 * units.M_sun / units.kpc**3)
            p0 = profileclass.Zhao(
                fit0[ifit][2],
                fit0[ifit][3],
                fit0[ifit][4],
                rho0=rho0inf0[i] * units.M_sun / units.kpc**3,
                rs=rmaxinf[i] / 2.163 * units.kpc,
                deltavirrhou=30000 * units.M_sun / units.kpc**3)
            plt.title(
                r'$v_{\rm max,f}/v_{\rm max,i}=%1.2f,~r_{\rm max,f}/r_{\rm max,i}=%1.2f$'
                % ((p.get_vmax() / p0.get_vmax()).value,
                   (p.get_rmax() / p0.get_rmax()).value))
            plt.tight_layout()
            plt.savefig(svfolder[i] + 'recoverytest.png')

            rho0halo, rscale, alpha, beta, gamma = mins['x']
            rho0halo = 10**rho0halo
            rscale = 10**rscale

            rho0inf0[i] = rho0halo
            rscale0[i] = rscale
            alpha0[i] = alpha
            beta0[i] = beta
            gamma0[i] = gamma

        try:
            bestfits.append(np.array([rho0halo, rscale, alpha, beta, gamma]))
        except:
            bestfits.append(
                np.array([rho0halo0, rscale0, alpha0, beta0, gamma0]))

        ifit = ifit + 1

    return bestfits, fit0