def contour_singlebox(phi2, d, mu1, mu2, Vrad, Vc, q): ro = 8.5 vo = Vc p = potential.LogarithmicHaloPotential(q=q, normalize=1) # --------------------------------------------------------------- # evaluating the x,y,z and vx,vy,vz from the function variables # --------------------------------------------------------------- phi1 = 34.776756565525091 # degrees lf, bf = mw.phi12_to_lb(phi1, phi2, degree=True) xf, yf, zf = bovy_coords.lbd_to_XYZ(lf, bf, d, degree=True) # guessed initial position in cylindrical coordinate Rf, zcylf, phif = xyz_to_cyl(xf, yf, zf) # convert proper motion in phi1 and phi2 coordinates to # proper motion in Galactic (vl,vb) coordinate vl, vb = mw.pmphi12_to_pmllpmbb(mu1, mu2, phi1, phi2, degree=True) # convert the vl, vb to proper motion in cartesian coordinate vxf, vyf, vzf = bovy_coords.vrpmllpmbb_to_vxvyvz(Vrad, vl, vb, lf, bf, d, degree=True) # convert vx,vy,vz to be in cylindrical coordinates vrf, vtf, vzff = vxvyvz_to_vrvtvz(xf, yf, zf, vxf, vyf, vzf) o = Orbit(vxvv=[Rf / ro, vrf / vo, vtf / vo, zcylf / ro, vzff / vo, phif], ro=ro, vo=vo) global time o.integrate(time, p) time = np.linspace(0.0, 1e1, 1e4) # Orbit in Galactic coordinates lval = o.ll(time, ro=ro, obs=[ro, 0.0, 0.0]) bval = o.bb(time, ro=ro, obs=[ro, 0.0, 0.0]) pmll = o.pmll(time, ro=ro, obs=[ro, 0.0, 0.0, -10.0, vo + 5.25, 7.17]) pmbb = o.pmbb(time, ro=ro, obs=[ro, 0.0, 0.0, -10.0, vo + 5.25, 7.17]) galpy_vel = mw.pmllpmbb_to_pmphi12(pmll, pmbb, lval, bval, degree=True) vrad_galpy = o.vlos(time, ro=ro, obs=[ro, 0.0, 0.0, -10.0, vo + 5.25, 7.17]) phi12 = mw.lb_to_phi12(lval, bval, degree=True) phi12[phi12[:, 0] > 180, 0] -= 360.0 L_pos = likelihood_all_test_sum(phi12[:, 0], phi1_pos, x_err_pos, phi12[:, 1], phi2_pos, phi2_err, time) L_dist = likelihood_all_test_sum(phi12[:, 0], phi1_dist, x_err_dist, o.dist(time), dist, dist_err, time) L_vrad = likelihood_all_test_sum(phi12[:, 0], phi1_vrad, x_err_vrad, vrad_galpy, Vrad, V_err, time) L_mu1 = likelihood_all_test_sum(phi12[:, 0], phi1_mu, x_err_mu, galpy_vel.T[0], mu1, sigma_mu, time) L_mu2 = likelihood_all_test_sum(phi12[:, 0], phi1_mu, x_err_mu, galpy_vel.T[1], mu2, sigma_mu, time) L_total = L_pos + L_dist + L_vrad + L_mu1 + L_mu2 return L_total
def orb_fit(params, find_posvel): if find_posvel: pos, vel = conversion_likelihood_fig(params) xi,yi,zi = pos[0], pos[1], pos[2] vxi,vyi,vzi = vel[0], vel[1], vel[2] else: xi,yi,zi = np.array([3.41,13.00,9.58]) vxi,vyi,vzi = np.array([200.4,-162.6,13.9]) Ri,zcyli,phii = xyz_to_cyl(xi,yi,zi) vri,vti,vzcyli = vxvyvz_to_vrvtvz(xi,yi,zi,vxi,vyi,vzi) p = potential.LogarithmicHaloPotential(q=0.9,normalize=1) ts = 1000 # number of timesteps time = np.linspace(0.,1e1,ts) o = Orbit(vxvv=[Ri/ro,vri/vo,vti/vo,zcyli/ro,vzcyli/vo,phii],ro=ro,vo=vo) o.integrate(time,p) lval = o.ll(time,ro = ro,obs= [ro,0.,0.]) bval = o.bb(time,ro = ro,obs= [ro,0.,0.]) phi12 = mw.lb_to_phi12(lval,bval,degree=True) phi12[phi12[:,0] > 180,0]-= 360. pmll = o.pmll(time, ro = ro, obs=[ro,0.,0.,0.,vo,0.]) pmbb = o.pmbb(time, ro = ro, obs=[ro,0.,0.,0.,vo,0.]) galpy_vel = mw.pmllpmbb_to_pmphi12(pmll,pmbb,lval,bval,degree=True) distvals = o.dist(time) Vrad = o.vlos(time) mu1 = galpy_vel.T[0] mu2 = galpy_vel.T[1] return phi12, distvals, Vrad, mu1, mu2
def vxvyvz_to_pmphi12(x, y, z, vx, vy, vz, degree): ''' Parameters ---------------------------------------------------- x, y, z : position in cartesian coordinate vx, vy, vz : velocity in cartesian coordinate Return ---------------------------------------------------- proper motion in stream coordinate as an array and the radial velocity as a number ''' l, b, d = bovy_coords.XYZ_to_lbd(x, y, z, degree = degree) vr, vl, vb = bovy_coords.vxvyvz_to_vrpmllpmbb(vx,vy,vz, l, b, d, degree = degree) vphi1, vphi2 = mw.pmllpmbb_to_pmphi12(vl, vb, l, b, degree = degree) return np.array([vphi1,vphi2]), vr
def optimizer_func(input,Vc,q): ''' Parameters ---------------------------------------------------- phi2: phi2 in degrees D: distance in kpc mu_phi1, mu_phi2: proper motion in phi1 and phi2 coordinates Vrad: radial velocity Return ---------------------------------------------------- Log likelihood using the specified Vc,q, phi2, D, proper motions in stream coordinates and the radial velocity. ''' phi2 = input[0] D = input[1] mu_phi1 = input[2] mu_phi2 = input[3] Vrad = input[4] print "vc in function:", Vc print "q in function:" , q # choose a value of phi2 for the given phi1 (phi1 should stay constant from # initial conditions obtained above in degrees phi1i = -30. #phi12i_kop[0] phi2i = phi2 # convert the phi1 and phi2 to be in cylindrical coordinate lf, bf = mw.phi12_to_lb(phi1i, phi2i, degree=True) xf, yf, zf = bovy_coords.lbd_to_XYZ(lf, bf, D, degree = True) # guessed initial position in cylindrical coordinate Rf,zcylf,phif = xyz_to_cyl(xf, yf, zf) # convert proper motion in phi1 and phi2 coordinates to # proper motion in Galactic (vl,vb) coordinate vl,vb = mw.pmphi12_to_pmllpmbb(mu_phi1, mu_phi2, phi1i, phi2i, degree = True) # convert the vl, vb to proper motion in cartesian coordinate vxf, vyf, vzf = bovy_coords.vrpmllpmbb_to_vxvyvz(Vrad, vl, vb, lf, bf, D, degree = True) # convert vx,vy,vz to be in cylindrical coordinates vrf, vtf, vzff = vxvyvz_to_vrvtvz(xf, yf, zf, vxf, vyf, vzf) # use the above initial positions to calculate the potential and orbit ro = 8.5 vo = Vc p = potential.LogarithmicHaloPotential(q = q,normalize = 1) o = Orbit(vxvv=[Rf/ro, vrf/vo, vtf/vo, zcylf/ro, vzff/vo, phif], ro = ro, vo = vo) o.integrate(time_glob,p) time = np.linspace(0.,1e1,1e4) # compute the likelihood with the above initial position lval = o.ll(time, ro = ro, obs = [ro,0.,0.]) bval = o.bb(time, ro = ro, obs = [ro,0.,0.]) pmll = o.pmll(time, ro = ro, obs = [ro, 0., 0., -10., vo+5.25, 7.17]) pmbb = o.pmbb(time, ro = ro, obs = [ro, 0., 0., -10., vo+5.25, 7.17]) galpy_vel = mw.pmllpmbb_to_pmphi12(pmll,pmbb,lval,bval,degree = True) vrad_galpy = o.vlos(time, ro = ro, obs = [ro, 0., 0., -10., vo+5.25, 7.17]) phi12 = mw.lb_to_phi12(lval, bval, degree = True) phi12[phi12[:,0] > 180,0]-= 360. # calculating likelihood for each set of parameters L_pos = likelihood_all_test_sum(phi12[:,0], phi1_pos, x_err_pos, phi12[:,1], phi2_pos, phi2_err, time) L_dist = likelihood_all_test_sum(phi12[:,0], phi1_dist, x_err_dist, o.dist(time), dist, dist_err, time) L_vrad = likelihood_all_test_sum(phi12[:,0], phi1_vrad, x_err_vrad, vrad_galpy, Vrad_kop, V_err, time) L_mu1 = likelihood_all_test_sum(phi12[:,0], phi1_mu, x_err_mu, galpy_vel.T[0], mu1, sigma_mu, time) L_mu2 = likelihood_all_test_sum(phi12[:,0], phi1_mu, x_err_mu, galpy_vel.T[1], mu2, sigma_mu, time) print "L position:", L_pos print "L dist:" , L_dist print "L vrad:" , L_vrad print "L mu1:" , L_mu1 print "L mu2:" , L_mu2 print L_total = L_pos + L_dist + L_vrad + L_mu1 + L_mu2 # chi^2 = -2ln(L) where ln(L) = L_total chi2 = -2. * L_total print print "chi2: ",chi2 print print "input:", input return chi2