M1 = 1 # mass 1 M2 = 0.001 # mass 2 M = M1 + M2 # total mass P = 2*math.pi * math.sqrt(1 / M) # period from Kepler's 3rd law omega = 2*math.pi/P # angular velocity of massive bodies times = [0, nPeriods*P] # set integration limits R = 1 # separation between masses must be 1 r1 = -R*M2/M # x coordinate of M1 r2 = R*M1/M # x coordinate of M2 LP = lpoints(M2/M) # get positions of Lagrange points ########## Set initial conditions for 4:7 resonance P2 = P*4/7 # 4:7 resonance e = 0.1 # eccentricity a = R * (P2/P)**(2/3) # calculate semimajor axis from period x0 = a*(1-e) # initial position y0 = 0 vx0 = 0 # initial velocity vy0 = math.sqrt(M1*(1+e)/x0) # - x0*omega; ########## Set initial conditions 0.025 to right of L4 point # lp = 4 # x0 = LP(lp,1) + 0.024
nPeriods = 100 # number of orbital periods to run simulation M1 = 1 # mass 1 M2 = 0.001 # mass 2 M = M1 + M2 # total mass P = 2 * math.pi * math.sqrt(1 / M) # period from Kepler's 3rd law omega = 2 * math.pi / P # angular velocity of massive bodies times = [0, nPeriods * P] # set integration limits R = 1 # separation between masses must be 1 r1 = -R * M2 / M # x coordinate of M1 r2 = R * M1 / M # x coordinate of M2 LP = lpoints(M2 / M) # get positions of Lagrange points ########## Set initial conditions to match Ceres *** No Chaos P2 = P * 4.60 / 11.86 # ratio of Mars's period to Jupiter's e = 0.079 # eccentricity a = R * (P2 / P)**(2 / 3) # calculate semimajor axis from period x0 = a * (1 - e) # initial position at perihelion y0 = 0 vx0 = 0 # initial velocity at perihelion vy0 = math.sqrt(M1 * (1 + e) / x0) ########## Set initial conditions for 2:3 resonance *** evidence of chaos # P2 = P*2/3 # 2:3 resonance # e = 0.56 # eccentricity # a = R * (P2/P)**(2/3) # calculate semimajor axis from period # x0 = a*(1+e) # initial position
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 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]
# lagrangePoints.m - returns 5x3 array containing (x,y,z) coordinates # of the Lagrange points for given values of m1, m2 # # Matlab-Monkey.com 10/10/2013 # -------------- Parameters and Initialization ----------------- # M1 = 1 # mass 1 M2 = 0.1 # mass 2 M = M1 + M2 # total mass # 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 # find Lagrange points and the pseudo-potential LP = lpoints(M2 / (M1 + M2)) LP1_level = potential(mu1, mu2, LP[0, 0], LP[0, 1]) LP2_level = potential(mu1, mu2, LP[1, 0], LP[1, 1]) LP3_level = potential(mu1, mu2, LP[2, 0], LP[2, 1]) # ------------------- Plotting ----------------------- # # plot zero-velocity curves that run through L1, L2, L3 plt.contour(X, Y, U, [LP1_level, LP2_level, LP3_level]) plt.hold(True) # plot m1 (yellow dot) and m2 (green dot) plt.plot(-M2 / (M1 + M2), 0, "ko", "MarkerSize", 7, "MarkerFaceColor", "y") plt.plot(M1 / (M1 + M2), 0, "ko", "MarkerSize", 5, "MarkerFaceColor", "g")
(X, Y) = np.meshgrid(np.linspace(-2, 2, 100)) U = potential(mu1, mu2, X, Y) U0 = potential(mu1, mu2, 0, 1) m = 2 #map = scipy.ones(m, 3) * 8 level = [-2 - 1.96 - 1.9 - 1.7 - 1.68 - 1.64] for j in range(0, 5): plt.subplot(2, 3, j) # contourf(X,Y,U,[-5:.5:-1]) # added to level[j] in original code [0, -0.0001] plt.contourf(X, Y, U, level[j]) plt.hold(True) plt.plot(-M2 / (M1 + M2), 0, 'ko', 'MarkerSize', 7, 'MarkerFaceColor', 'y') plt.plot(M1 / (M1 + M2), 0, 'ko', 'MarkerSize', 5, 'MarkerFaceColor', 'g') plt.axis([min(X), max(X), min(Y), max(Y)]) plt.title(print('C_J = %.2f', -2 * level[j])) plt.axis('equal') plt.axis('square') plt.hold(True) LP = lpoints(M2 / (M1 + M2)) plt.plot(LP[:, 1], LP[:, 1], 'k+') # set(gcf, 'PaperPosition', [0,-1,8,4]) print('-dpng', 'zeroVelocity.png', '-r100')