Пример #1
0
def phase_resampling():
    '''
    Precessional phase resampling. The magnidute of the total spin S is sampled
    according to |dS/dt|^-1, which correspond to a flat distribution in t(S).
    Output is saved in ./phase_resampling.pdf and data stored in
    `precession.storedir'/phase_resampling_.dat


    **Run using**

        import precession.test
        precession.test.phase_resampling()
    '''

    fig = pylab.figure(figsize=(6, 6))  #Create figure object and axes
    ax_tS = fig.add_axes([0, 0, 0.6, 0.6])  #bottom-left
    ax_td = fig.add_axes([0.65, 0, 0.3, 0.6])  #bottom-right
    ax_Sd = fig.add_axes([0, 0.65, 0.6, 0.3])  #top-left

    q = 0.5  # Mass ratio. Must be q<=1.
    chi1 = 0.3  # Primary spin. Must be chi1<=1
    chi2 = 0.9  # Secondary spin. Must be chi2<=1
    M, m1, m2, S1, S2 = precession.get_fixed(q, chi1,
                                             chi2)  # Total-mass units M=1
    r = 200. * M  # Separation. Must be r>10M for PN to be valid
    J = 3.14  # Magnitude of J: Jmin<J<Jmax as given by J_lim
    xi = -0.01  # Effective spin: xi_low<xi<xi_up as given by xi_allowed
    Sb_min, Sb_max = precession.Sb_limits(xi, J, q, S1, S2, r)  # Limits in S
    tau = precession.precession_period(xi, J, q, S1, S2,
                                       r)  # Precessional period
    d = 2000  # Size of the statistical sample

    precession.make_temp()  # Create store directory, if necessary
    filename = precession.storedir + "/phase_resampling.dat"  # Output file name
    if not os.path.isfile(filename):  # Compute and store data if not present
        out = open(filename, "w")
        out.write("# q chi1 chi2 r J xi d\n")  # Write header
        out.write("# " +
                  ' '.join([str(x)
                            for x in (q, chi1, chi2, r, J, xi, d)]) + "\n")

        # S and t values for the S(t) plot
        S_vals = numpy.linspace(Sb_min, Sb_max, d)
        t_vals = numpy.array([
            abs(
                precession.t_of_S(Sb_min, S, Sb_min, Sb_max, xi, J, q, S1, S2,
                                  r)) for S in S_vals
        ])
        # Sample values of S from |dt/dS|. Distribution should be flat in t.
        S_sample = numpy.array(
            [precession.samplingS(xi, J, q, S1, S2, r) for i in range(d)])
        t_sample = numpy.array([
            abs(
                precession.t_of_S(Sb_min, S, Sb_min, Sb_max, xi, J, q, S1, S2,
                                  r)) for S in S_sample
        ])
        # Continuous distributions (normalized)
        S_distr = numpy.array([
            2. * abs(precession.dtdS(S, xi, J, q, S1, S2, r) / tau)
            for S in S_vals
        ])
        t_distr = numpy.array([2. / tau for t in t_vals])

        out.write("# S_vals t_vals S_sample t_sample S_distr t_distr\n")
        for Sv, tv, Ss, ts, Sd, td in zip(S_vals, t_vals, S_sample, t_sample,
                                          S_distr, t_distr):
            out.write(' '.join([str(x)
                                for x in (Sv, tv, Ss, ts, Sd, td)]) + "\n")
        out.close()
    else:  # Read
        S_vals, t_vals, S_sample, t_sample, S_distr, t_distr = numpy.loadtxt(
            filename, unpack=True)

    # Rescale all time values by 10^-6, for nicer plotting
    tau *= 1e-6
    t_vals *= 1e-6
    t_sample *= 1e-6
    t_distr /= 1e-6

    ax_tS.plot(S_vals, t_vals, c='blue', lw=2)  # S(t) curve
    ax_td.plot(t_distr, t_vals, lw=2., c='red')  # Continous distribution P(t)
    ax_Sd.plot(S_vals, S_distr, lw=2., c='red')  # Continous distribution P(S)
    ax_td.hist(t_sample,
               bins=60,
               range=(0, tau / 2.),
               normed=True,
               histtype='stepfilled',
               color="blue",
               alpha=0.4,
               orientation="horizontal")  # Histogram P(t)
    ax_Sd.hist(S_sample,
               bins=60,
               range=(Sb_min, Sb_max),
               normed=True,
               histtype='stepfilled',
               color="blue",
               alpha=0.4)  # Histogram P(S)

    # Options for nice plotting
    ax_tS.set_xlim(Sb_min, Sb_max)
    ax_tS.set_ylim(0, tau / 2.)
    ax_tS.set_xlabel("$S/M^2$")
    ax_tS.set_ylabel("$t/(10^6 M)$")
    ax_td.set_xlim(0, 0.5)
    ax_td.set_ylim(0, tau / 2.)
    ax_td.set_xlabel("$P(t)$")
    ax_td.set_yticklabels([])
    ax_Sd.set_xlim(Sb_min, Sb_max)
    ax_Sd.set_ylim(0, 20)
    ax_Sd.set_xticklabels([])
    ax_Sd.set_ylabel("$P(S)$")

    fig.savefig("phase_resampling.pdf", bbox_inches='tight')  # Save pdf file
Пример #2
0
def spin_angles():
    '''
    Binary dynamics on the precessional timescale. The spin angles
    theta1,theta2, DeltaPhi and theta12 are computed and plotted against the
    time variable, which is obtained integrating dS/dt. The morphology is also
    detected as indicated in the legend of the plot. Output is saved in
    ./spin_angles.pdf.

    **Run using**

        import precession.test
        precession.test.spin_angles()
    '''

    fig = pylab.figure(figsize=(6, 6))  # Create figure object and axes
    ax_t1 = fig.add_axes([0, 1.95, 0.9, 0.5])  # first (top)
    ax_t2 = fig.add_axes([0, 1.3, 0.9, 0.5])  # second
    ax_dp = fig.add_axes([0, 0.65, 0.9, 0.5])  # third
    ax_t12 = fig.add_axes([0, 0, 0.9, 0.5])  # fourth (bottom)

    q = 0.7  # Mass ratio. Must be q<=1.
    chi1 = 0.6  # Primary spin. Must be chi1<=1
    chi2 = 1.  # Secondary spin. Must be chi2<=1
    M, m1, m2, S1, S2 = precession.get_fixed(q, chi1,
                                             chi2)  # Total-mass units M=1
    r = 20 * M  # Separation. Must be r>10M for PN to be valid
    J = 0.94  # Magnitude of J: Jmin<J<Jmax as given by J_lim
    xi_vals = [-0.41, -0.3,
               -0.22]  # Effective spin: xi_low<xi<xi_up as given by xi_allowed

    for xi, color in zip(xi_vals,
                         ['blue', 'green', 'red']):  # Loop over three binaries

        tau = precession.precession_period(xi, J, q, S1, S2, r)  # Period
        morphology = precession.find_morphology(xi, J, q, S1, S2,
                                                r)  # Morphology
        if morphology == -1: labelm = "${\\rm L}0$"
        elif morphology == 1: labelm = "${\\rm L}\\pi$"
        elif morphology == 0: labelm = "${\\rm C}$"
        Sb_min, Sb_max = precession.Sb_limits(xi, J, q, S1, S2,
                                              r)  # Limits in S
        S_vals = numpy.linspace(Sb_min, Sb_max,
                                1000)  # Create array, from S- to S+
        S_go = S_vals  # First half of the precession cycle: from S- to S+
        t_go = map(lambda x: precession.t_of_S(
            S_go[0], x, Sb_min, Sb_max, xi, J, q, S1, S2, r, 0, sign=-1.),
                   S_go)  # Compute time values. Assume t=0 at S-
        t1_go, t2_go, dp_go, t12_go = zip(*[
            precession.parametric_angles(S, J, xi, q, S1, S2, r) for S in S_go
        ])  # Compute the angles.
        dp_go = [-dp
                 for dp in dp_go]  # DeltaPhi<=0 in the first half of the cycle
        S_back = S_vals[::
                        -1]  # Second half of the precession cycle: from S+ to S-
        t_back = map(
            lambda x: precession.t_of_S(S_back[0],
                                        x,
                                        Sb_min,
                                        Sb_max,
                                        xi,
                                        J,
                                        q,
                                        S1,
                                        S2,
                                        r,
                                        t_go[-1],
                                        sign=1.), S_back
        )  # Compute time, start from the last point of the first half t_go[-1]
        t1_back, t2_back, dp_back, t12_back = zip(*[
            precession.parametric_angles(S, J, xi, q, S1, S2, r)
            for S in S_back
        ])  # Compute the angles. DeltaPhi>=0 in the second half of the cycle

        for ax, vec_go, vec_back in zip(
            [ax_t1, ax_t2, ax_dp, ax_t12], [t1_go, t2_go, dp_go, t12_go],
            [t1_back, t2_back, dp_back, t12_back]):  # Plot all curves
            ax.plot([t / tau for t in t_go],
                    vec_go,
                    c=color,
                    lw=2,
                    label=labelm)
            ax.plot([t / tau for t in t_back], vec_back, c=color, lw=2)

        # Options for nice plotting
        for ax in [ax_t1, ax_t2, ax_dp, ax_t12]:
            ax.set_xlim(0, 1)
            ax.set_xlabel("$t/\\tau$")
            ax.set_xticks(numpy.linspace(0, 1, 5))
        for ax in [ax_t1, ax_t2, ax_t12]:
            ax.set_ylim(0, numpy.pi)
            ax.set_yticks(numpy.linspace(0, numpy.pi, 5))
            ax.set_yticklabels(
                ["$0$", "$\\pi/4$", "$\\pi/2$", "$3\\pi/4$", "$\\pi$"])
        ax_dp.set_ylim(-numpy.pi, numpy.pi)
        ax_dp.set_yticks(numpy.linspace(-numpy.pi, numpy.pi, 5))
        ax_dp.set_yticklabels(
            ["$-\\pi$", "$-\\pi/2$", "$0$", "$\\pi/2$", "$\\pi$"])
        ax_t1.set_ylabel("$\\theta_1$")
        ax_t2.set_ylabel("$\\theta_2$")
        ax_t12.set_ylabel("$\\theta_{12}$")
        ax_dp.set_ylabel("$\\Delta\\Phi$")
        ax_t1.legend(
            loc='lower right',
            fontsize=18)  # Fill the legend with the precessional morphology

    fig.savefig("spin_angles.pdf", bbox_inches='tight')  # Save pdf file