Exemplo n.º 1
0
def RK45(masses, pos0, vel0i, times, flag):

    # ---------  Events - Poincare Section  --------- #

    def events(tin, y, dy):
        # Locate the time when y1 passes through zero in an
        # increasing direction

        value = y(2)  # Detect when particle passes y1 = 0
        isterminal = 0  # Don't stop the integration
        direction = -1  # Positive direction only
        return (value, isterminal, direction)

    (nargin, varargs, keywords, defaults) = inspect.getargspec(RK45)

    if nargin < 4:
        print('crtbpRK45 requires at least 4 parameters')
        exit()

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

###################  Flags Controlling Output  ###################

    if nargin >= 5:
        Poincare = flag[0]  # if true, create Poincare section
    else:
        Poincare = False

###################  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

    # calculate velocity components in rotating frame
    vel0 = []
    vm = math.sqrt(vel0i[0]**2 + vel0i[1]**2)
    vxu = vel0i[0] / vm
    vyu = vel0i[1] / vm
    vr = omega0 * math.sqrt(pos0[0]**2 + pos0[1]**2)
    vnew = vm - vr
    vel0[0] = vxu * vnew
    vel0[1] = vyu * vnew

    # --------                                                       -------- #
    # --------                      Integrate                        -------- #
    # --------                                                       -------- #

    # Use Runge-Kutta 45 integrator to solve the ODE

    if Poincare:
        options = i.ode(out1).set_integrator('vode', method='bdf', order=15)

        # options = .odeset('RelTol', 1e-12, 'AbsTol', 1e-12, 'Events',@events,'outputfcn',@OutputFcn1)
    else:
        options = i.ode(out1).set_integrator('vode', method='bdf', order=15)

        # options = sp.integrate.odeset('RelTol', 1e-12, 'AbsTol', 1e-12,'outputfcn',@OutputFcn1)

# initialize waitbar
    wait = True
    while (wait):
        w = bar(0, 'integrating...')
        wait = out1(0, 0, 0, w)

# Use Runge-Kutta-Nystrom integrator to solve the ODE
    tic = time.time()
    eng = matlab.engine.start_matlab('-nojvm')
    [t, q, TE, YE] = eng.ode45(derivatives, times,
                               np.array([pos0, vel0]).transpose(), options)
    toc = time.time() - tic
    DT = toc

    pos = q[:, 0:1]
    vel = q[:, 2:3]
    return (t, pos, vel, YE)
Exemplo n.º 2
0
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]
Exemplo n.º 3
0
def RK45(masses, pos0, vel0i, times, flag):

    # ---------  Events - Poincare Section  --------- #

    def events(tin,y,dy):
        # Locate the time when y1 passes through zero in an
        # increasing direction

        value = y(2)     # Detect when particle passes y1 = 0
        isterminal = 0   # Don't stop the integration
        direction = -1   # Positive direction only
        return (value, isterminal, direction)


    (nargin, varargs, keywords, defaults) = inspect.getargspec(RK45)

    if nargin < 4:
        print('crtbpRK45 requires at least 4 parameters')
        exit()



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

###################  Flags Controlling Output  ###################

    if nargin >= 5:
        Poincare = flag[0]      # if true, create Poincare section
    else:
        Poincare = False

###################  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


# calculate velocity components in rotating frame
    vel0 = []
    vm = math.sqrt(vel0i[0]**2 + vel0i[1]**2)
    vxu = vel0i[0]/vm
    vyu = vel0i[1]/vm
    vr = omega0 * math.sqrt(pos0[0]**2+pos0[1]**2)
    vnew = vm-vr
    vel0[0] = vxu * vnew
    vel0[1] = vyu * vnew





# --------                                                       -------- #
# --------                      Integrate                        -------- #
# --------                                                       -------- #


# Use Runge-Kutta 45 integrator to solve the ODE


    if Poincare:
        options = i.ode(out1).set_integrator('vode', method='bdf', order=15)

        # options = .odeset('RelTol', 1e-12, 'AbsTol', 1e-12, 'Events',@events,'outputfcn',@OutputFcn1)
    else:
        options = i.ode(out1).set_integrator('vode', method='bdf', order=15)

        # options = sp.integrate.odeset('RelTol', 1e-12, 'AbsTol', 1e-12,'outputfcn',@OutputFcn1)


# initialize waitbar
    wait = True
    while (wait):
                w = bar(0, 'integrating...')
                wait = out1(0, 0, 0, w)


# Use Runge-Kutta-Nystrom integrator to solve the ODE
    tic = time.time()
    [t, q, TE, YE] = i.odeint(derivatives, pos0, times[0],
                           #ode45(@derivatives, times, [pos0 vel0]', options)
    toc = time.time() - tic
    DT = toc

    pos = q[:, 0:1]
    vel = q[:, 2:3]
    return (t, pos, vel, YE)
Exemplo n.º 4
0
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]