Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Python-Programs-for-Nonlinear-Dynamics/Heiles.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
123 lines (104 sloc)
3.1 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Created on Wed Apr 18 06:03:32 2018 | |
@author: David Nolte | |
Introduction to Modern Dynamics, 2nd edition (Oxford University Press, 2019) | |
Hamiltonian models | |
""" | |
import numpy as np | |
import matplotlib as mpl | |
from mpl_toolkits.mplot3d import Axes3D | |
from scipy import integrate | |
from matplotlib import pyplot as plt | |
from matplotlib import cm | |
import time | |
import os | |
plt.close('all') | |
print('Heiles.py') | |
# model_case 1 = Heiles | |
# model_case 2 = Crescent | |
model_case = int(input('Enter the Model Case (1-2)')) | |
if model_case == 1: | |
E = 1 # Heiles: 1, 0.3411 Crescent: 0.05, 1 | |
epsE = 0.3411 # 3411 | |
def flow_deriv(x_y_z_w,tspan): | |
x, y, z, w = x_y_z_w | |
a = z | |
b = w | |
c = -x - epsE*(2*x*y) | |
d = -y - epsE*(x**2 - y**2) | |
return[a,b,c,d] | |
else: | |
E = 1 # Heiles: 1, 0.3411 Crescent: 0.05, 1 | |
epsE = 0.2 # 0.2 | |
def flow_deriv(x_y_z_w,tspan): | |
x, y, z, w = x_y_z_w | |
a = z | |
b = w | |
c = -(epsE*(y-2*x**2)*(-4*x) + x) | |
d = -(y-epsE*2*x**2) | |
return[a,b,c,d] | |
prms = np.sqrt(E) | |
pmax = np.sqrt(2*E) | |
# Potential Function | |
if model_case == 1: | |
V = np.zeros(shape=(100,100)) | |
for xloop in range(100): | |
x = -4 + 8*xloop/100 | |
for yloop in range(100): | |
y = -4 + 8*yloop/100 | |
V[yloop,xloop] = 0.5*x**2 + 0.5*y**2 + epsE*(x**2*y - 0.33333*y**3) | |
else: | |
V = np.zeros(shape=(100,100)) | |
for xloop in range(100): | |
x = -4 + 8*xloop/100 | |
for yloop in range(100): | |
y = -4 + 8*yloop/100 | |
V[yloop,xloop] = 0.5*x**2 + 0.5*y**2 + epsE*(2*x**4 - 2*x**2*y) | |
mxV = np.int(np.max(V)) | |
fig = plt.figure(1) | |
contr = plt.contourf(V,mxV, cmap=cm.coolwarm, vmin = 0, vmax = 30) | |
fig.colorbar(contr, shrink=0.5, aspect=5) | |
fig = plt.show() | |
repnum = 250 | |
mulnum = 64/repnum | |
np.random.seed(1) | |
for reploop in range(repnum): | |
px1 = 2*(np.random.random((1))-0.499)*pmax | |
py1 = np.sign(np.random.random((1))-0.499)*np.real(np.sqrt(2*(E-px1**2/2))) | |
xp1 = 0 | |
yp1 = 0 | |
x_y_z_w0 = [xp1, yp1, px1, py1] | |
tspan = np.linspace(1,1000,10000) | |
x_t = integrate.odeint(flow_deriv, x_y_z_w0, tspan) | |
siztmp = np.shape(x_t) | |
siz = siztmp[0] | |
if reploop % 50 == 0: | |
plt.figure(2) | |
lines = plt.plot(x_t[:,0],x_t[:,1]) | |
plt.setp(lines, linewidth=0.5) | |
plt.pause(0.05) | |
plt.show() | |
y1 = x_t[:,0] | |
y2 = x_t[:,1] | |
y3 = x_t[:,2] | |
y4 = x_t[:,3] | |
py = np.zeros(shape=(2*repnum,)) | |
yvar = np.zeros(shape=(2*repnum,)) | |
cnt = -1 | |
last = y1[1] | |
for loop in range(2,siz): | |
if (last < 0)and(y1[loop] > 0): # Test for x going positive | |
cnt = cnt+1 | |
del1 = -y1[loop-1]/(y1[loop] - y1[loop-1]) # Interpolate to the Poincare plane | |
py[cnt] = y4[loop-1] + del1*(y4[loop]-y4[loop-1]) | |
yvar[cnt] = y2[loop-1] + del1*(y2[loop]-y2[loop-1]) | |
last = y1[loop] | |
else: | |
last = y1[loop] | |
plt.figure(3) | |
lines = plt.plot(yvar,py,'o',ms=1) | |
plt.pause(0.05) | |
plt.show() | |
plt.savefig('Heiles') |