def OutputFcn2(t, y, dy, flag):
    # don't stop
    stop = False
    trail = []
    fig = plt.figure()
    # only after sucessfull steps
    if not flag:
        return

    else:
        NP = len(y) / 2  # number of particles (assumes 2D)

        if c.rotatingFrame:

            XYrot = rotate(y, t, -1)

            if c.leaveTrail:
                for ii in range(0, NP - 1):
                    trail[:, 2 * ii] = np.array([XYrot(2 * ii - 1)], [trail[: -2, 2 * ii - 1]])
                    trail[:, 2 * ii + 1] =   np.array([XYrot(2 * ii)], [trail[: -2, 2 * ii]])

                    plt.plot(trail[:, 2 * ii - 1], trail[:, 2 * ii], '-')
                    plt.hold(True)

            for ii in range (0,NP-1):
                plt.plot(XYrot(2 * ii), XYrot(2 * ii + 1), '.')
                plt.hold(True)

            plt.plot(c.LP[:, 0], c.LP[:, 1], 'k+')
            plt.plot(c.R10(0), c.R10(1), 'ro', 'MarkerFaceColor', 'r')
            plt.plot(c.R20(0), c.R20(1), 'go', 'MarkerFaceColor', 'g')
        else:
            if c.leaveTrail:
                for ii in range (0, NP-1):
                    trail[:, 2 * ii] = np.array([y(2 * ii)],[trail[:, 2 * ii]])
                    trail[:, 2 * ii + 1] = np.array([y(2 * ii + 1)],[trail[:, 2 * ii + 1]])

                    plt.plot(trail[:, 2 * ii], trail[:, 2 * ii + 1], '-')
                    plt.hold(True)

            for ii in range (0, NP-1):
                plt.plot(y(2 * ii), y(2 * ii + 1), '.')
                plt.hold(True)

            r1 = rotate(c.R10, t, 1)
            r2 = rotate(c.R20, t, 1)

            plt.plot(r1(1), r1(2), 'ro', 'MarkerFaceColor', 'r')
            plt.plot(r2(1), r2(2), 'go', 'MarkerFaceColor', 'g')

        plt.axis('equal')
        plt.axis(c.plotLimits * [-1,1, - 1, 1])
        plt.hold(False)
        plt.pause(c.animateDelay) # flush plotting commands
        plt.draw()
        fig.canvas.flush_events()
    return stop
def integrator(masses, pos0, vel0, times, flag):
    global tEnd, wait, mu1, mu2, R10, R20, omega0, plotLimits, LP, leaveTrail, rotatingFrame
    # return returnVals
    (nargin, varargs, keywords, defaults) = inspect.getargspec(integrator)
    if nargin < 4:
        print('crtbpRKN1210 requires at least 4 parameters')
        exit()



    ##########                                                       ##########
    ##########           Initialization and Parameters               ##########
    ##########                                                       ##########



    ####################  Flags Controlling Output  ###########################
    animate = False
    rotatingFrame = False

    if nargin >= 5:
        rotatingFrame = flag(1)  # if true, transform to rotating frame
        # if false, display inertial frame

        animate = flag(2)  # if true, animate motion
        # if false, just display final trajectory

        leaveTrail = flag(3)  # if true and animation is on, past positions will
        # not be erased.  If false and animation is on,
        # no 'trail' of past positions will be left

    progressBar = False  # if true, show progress bar

    animateDelay = 0.0  # delay (in seconds) between frames of animation.
    # smaller the value, faster the animation will play

    leaveTrail = flag(3)  # if true and animation is on, past positions will
    # not be erased.  If false and animation is on,
    # no 'trail' of past positions will be left

    trailLength = 700  # if leaveTrail is true, this sets the length of
    # trail

    #########################  System Parameters  ############################


    NP = len(pos0) / 2  # number of test particles.  Assumes 2D

    M1 = masses(1)  # mass 1
    M2 = masses(2)  # mass 2
    M = M1 + M2  # total mass

    G = 1  # Gravitational Constant
    R = 1  # distance between M1 and M2 set to 1


    ################  Orbital properties of two massive bodies  ##############


    mu = G * M
    mu1 = G * M1
    mu2 = G * M2

    R10 = ([-M2 / M], [0])  # initial position of M1
    R20 = ([M1 / M], [0])  # initial position of M2

    P = 2 * math.pi * math.sqrt(R ** 3 / mu)  # period from Kepler's 3rd law
    omega0 = 2 * math.pi / P  # angular velocity of massive bodies

    plotLimits = 1.5  # limit for plotting
    tEnd = times(len(times) - 1)

    LP = lpoints(M2 / M)  # calculate Lagrange points


    ###########################   Trail Properties  ###########################

    trail = ones(trailLength, 2 * NP)
    for j in range(0, 2 * NP - 1):
        trail[:, j] = pos0(j) * ones(trailLength, 1)


    ##########                                                       ##########
    ##########                      Integrate                        ##########
    ##########                                                       ##########


    # Use Runge-Kutta 45 integrator to solve the ODE

    # initialize wait variable
    wait = True
    options = integrate.ode
    if animate:
        # options = scipy.odeset('RelTol', 1e-12, 'AbsTol', 1e-12,'outputfcn',OutputFcn2)
        options = integrate.ode(OutputFcn2).set_integrator('vode', method='bdf', order=15)
    else:  # initialize waitbar
        if progressBar:
            while (wait):
                w = bar(0, 'integrating...')
                wait = out1(0, 0, 0, w)
                # options = scipy.odeset('RelTol', 1e-12, 'AbsTol', 1e-12,'outputfcn',OutputFcn1)
                options = integrate.ode(OutputFcn1).set_integrator('vode', method='bdf', order=15)
        else:
            # options = scipy.odeset('RelTol', 1e-12, 'AbsTol', 1e-12)
            options = integrate.ode(OutputFcn1).set_integrator('vode', method='bdf', order=15)


        # Use Runge-Kutta-Nystrom integrator to solve the ODE
    tic = time.time()
    # [t,pos,vel] = rkn1210(@derivatives, times, pos0, vel0, options)
    [t, pos, vel, te, ye] = rkn1210(derivatives, times, pos0, vel0, options)
    toc = time.time() - tic
    DT = toc

    if rotatingFrame:
        for j in range(0, len(t) - 1):
            pos[j, :] = rotate(pos[j, :].transpose(), t(j), -1)


        # returnVals = [t, pos, vel, YE]
    return [t, pos, vel]
def OutputFcn2(t, y, dy, flag):
    # don't stop
    stop = False
    trail = []
    fig = plt.figure()
    # only after sucessfull steps
    if not flag:
        return

    else:
        NP = len(y) / 2  # number of particles (assumes 2D)

        if c.rotatingFrame:

            XYrot = rotate(y, t, -1)

            if c.leaveTrail:
                for ii in range(0, NP - 1):
                    trail[:, 2 * ii] = np.array([XYrot(2 * ii - 1)],
                                                [trail[:-2, 2 * ii - 1]])
                    trail[:, 2 * ii + 1] = np.array([XYrot(2 * ii)],
                                                    [trail[:-2, 2 * ii]])

                    plt.plot(trail[:, 2 * ii - 1], trail[:, 2 * ii], '-')
                    plt.hold(True)

            for ii in range(0, NP - 1):
                plt.plot(XYrot(2 * ii), XYrot(2 * ii + 1), '.')
                plt.hold(True)

            plt.plot(c.LP[:, 0], c.LP[:, 1], 'k+')
            plt.plot(c.R10(0), c.R10(1), 'ro', 'MarkerFaceColor', 'r')
            plt.plot(c.R20(0), c.R20(1), 'go', 'MarkerFaceColor', 'g')
        else:
            if c.leaveTrail:
                for ii in range(0, NP - 1):
                    trail[:, 2 * ii] = np.array([y(2 * ii)],
                                                [trail[:, 2 * ii]])
                    trail[:, 2 * ii + 1] = np.array([y(2 * ii + 1)],
                                                    [trail[:, 2 * ii + 1]])

                    plt.plot(trail[:, 2 * ii], trail[:, 2 * ii + 1], '-')
                    plt.hold(True)

            for ii in range(0, NP - 1):
                plt.plot(y(2 * ii), y(2 * ii + 1), '.')
                plt.hold(True)

            r1 = rotate(c.R10, t, 1)
            r2 = rotate(c.R20, t, 1)

            plt.plot(r1(1), r1(2), 'ro', 'MarkerFaceColor', 'r')
            plt.plot(r2(1), r2(2), 'go', 'MarkerFaceColor', 'g')

        plt.axis('equal')
        plt.axis(c.plotLimits * [-1, 1, -1, 1])
        plt.hold(False)
        plt.pause(c.animateDelay)  # flush plotting commands
        plt.draw()
        fig.canvas.flush_events()
    return stop
Exemple #4
0
def integrator(masses, pos0, vel0, times, flag):

    ##########  OutputFcn1:  Status Bar  ####################################
    #
    # the output function
    def OutputFcn1(t, y, dy, flag):  # ok
        # don't stop
        stop = False
        # only after sucessfull steps
        if not flag:
            stop = True
        else:
            bar(t / tEnd, wait)
        return stop

    global tEnd, wait, mu1, mu2, R10, R20, omega0, plotLimits, LP, leaveTrail, rotatingFrame
    # return returnVals
    (nargin, varargs, keywords, defaults) = inspect.getargspec(integrator)
    if len(nargin) < 4:
        print('crtbpRKN1210 requires at least 4 parameters')
        exit()

    ##########                                                       ##########
    ##########           Initialization and Parameters               ##########
    ##########                                                       ##########

    ####################  Flags Controlling Output  ###########################
    animate = False
    rotatingFrame = False

    if len(nargin) >= 5:
        rotatingFrame = flag[0]  # if true, transform to rotating frame
        # if false, display inertial frame

        animate = flag[1]  # if true, animate motion
        # if false, just display final trajectory

        leaveTrail = flag[
            2]  # if true and animation is on, past positions will
        # not be erased.  If false and animation is on,
        # no 'trail' of past positions will be left

    progressBar = False  # if true, show progress bar

    animateDelay = 0.0  # delay (in seconds) between frames of animation.
    # smaller the value, faster the animation will play

    leaveTrail = flag[2]  # if true and animation is on, past positions will
    # not be erased.  If false and animation is on,
    # no 'trail' of past positions will be left

    trailLength = 700  # if leaveTrail is true, this sets the length of
    # trail

    #########################  System Parameters  ############################

    NP = len(pos0) / 2  # number of test particles.  Assumes 2D

    M1 = masses[0]  # mass 1
    M2 = masses[1]  # mass 2
    M = M1 + M2  # total mass

    G = 1  # Gravitational Constant
    R = 1  # distance between M1 and M2 set to 1

    ################  Orbital properties of two massive bodies  ##############

    mu = G * M
    mu1 = G * M1
    mu2 = G * M2

    R10 = ([-M2 / M], [0])  # initial position of M1
    R20 = ([M1 / M], [0])  # initial position of M2

    P = 2 * math.pi * math.sqrt(R**3 / mu)  # period from Kepler's 3rd law
    omega0 = 2 * math.pi / P  # angular velocity of massive bodies

    plotLimits = 1.5  # limit for plotting
    tEnd = times[len(times) - 1]

    LP = lpoints(M2 / M)  # calculate Lagrange points

    ###########################   Trail Properties  ###########################

    trail = np.ones((trailLength, 2 * NP))
    for j in range(0, len(pos0)):
        trail[:, j] = pos0[j] * np.ones((trailLength))

    ##########                                                       ##########
    ##########                      Integrate                        ##########
    ##########                                                       ##########

    # Use Runge-Kutta 45 integrator to solve the ODE

    # initialize wait variable
    eng = matlab.engine.start_matlab()
    options = eng.odeset('RelTol', 1E-12)
    wait = True

    if animate:
        options = eng.odeset('RelTol', 1e-12, 'AbsTol', 1e-12, 'outputfcn',
                             '@OutputFcn2')

    else:  # initialize waitbar
        if progressBar:
            while (wait):
                w = bar(0, 'integrating...')
                wait = OutputFcn1(0, 0, 0, w)
                options = eng.odeset('RelTol', 1e-12, 'AbsTol', 1e-12,
                                     'outputfcn', '@OutputFcn1')
        else:
            options = eng.odeset('RelTol', 1e-12, 'AbsTol', 1e-12)

        # Use Runge-Kutta-Nystrom integrator to solve the ODE
    tic = time.time()
    # [t,pos,vel] = rkn1210(@derivatives, times, pos0, vel0, options)
    [t, pos, vel, te, ye] = rkn1210(derivatives, times, pos0, vel0, options)
    toc = time.time() - tic
    DT = toc

    if rotatingFrame:
        for j in range(0, len(t) - 1):
            pos[j, :] = rotate(pos[j, :].transpose(), t[j], -1)

        # returnVals = [t, pos, vel, YE]
    return [t, pos, vel]