def timeofflight(xhat0): """ Computes time of flight for xhat0 on the Poincare section to come back onto the section for a second time """ #Find the point on the computed poincare section, closest to the x0 imin = np.argmin(np.linalg.norm(ps[:,1:5]-xhat0, axis=1)) #Take its time of flight as if imin < np.size(ps,0)-1: Tapproximate = ps[imin+1,0]-ps[imin,0] else: Tapproximate = ps[imin,0]-ps[imin-1,0] print "Tapproximate:" print Tapproximate #Integrate for a little bit longer than the approximated integration time: stoptime = 1.2*Tapproximate numpoints = int(stoptime/0.01) #Integration time array: t = [stoptime * float(i) / (numpoints - 1) for i in range(numpoints)] xhatphi0 = np.append(xhat0, np.array([0], float)) xhatsol = onslicesolver.integrate(xhatphi0, pars, t, abserror=1.0e-14, relerror=1.0e-12) tx = np.append(np.array([t], float).transpose(), xhatsol, axis=1) #Compute Poincare section: psreturn=psectslice.computeps(tx, sectp, nhat, direction, p=pars) print "psreturn:" print psreturn #Take the time nearest to the approximated time. This is due to the #fact that the array ps sometimes includes the initial point and sometimes #does not, hence we are not always sure the position of the first return. itof = np.argmin(np.abs(psreturn[:,0]-Tapproximate)) tof = psreturn[itof,0] return tof
def approximateperiod(xhat0, n): """ Computes time of flight for xhat0 on the Poincare section to come back onto the section for nth time """ #Find the point on the computed poincare section, closest to the x0 imin = np.argmin(np.linalg.norm(ps[:,1:5]-xhat0, axis=1)) #Take its time of flight as if imin < np.size(ps,0)-n: Tapproximate = ps[imin+n,0]-ps[imin,0] else: Tapproximate = ps[imin,0]-ps[imin-n,0] print "Tapproximate:" print Tapproximate #Integrate for a little bit longer than the approximated integration time: stoptime = 1.2*Tapproximate numpoints = int(stoptime/0.01) #Integration time array: t = [stoptime * float(i) / (numpoints - 1) for i in range(numpoints)] xhatphi0 = np.append(xhat0, np.array([0], float)) xhatsol = onslicesolver.integrate(xhatphi0, pars, t, abserror=1.0e-12, relerror=1.0e-10) tx = np.append(np.array([t], float).transpose(), xhatsol, axis=1) #Compute Poincare section: psreturn=psectslice.computeps(tx, sectp, nhat, direction, p=pars) print "psreturn:" print psreturn #Take the time nearest to the approximated time. This is due to the #fact that the array ps sometimes includes the initial point and sometimes #does not, hence we are not always sure the position of the first return. itof = np.argmin(np.abs(psreturn[:,0]-Tapproximate)) tof = psreturn[itof,0] return tof
def rpot(xt): ''' not for use, under development ''' #print "xt" #print xt abserr = 1.0e-14 relerr = 1.0e-12 #stoptime = xt[4] stoptime = xt[3] numpoints = int(stoptime / 0.01 + 1.0) #print "numpoints:" #print numpoints #numpoints = 2 #xphi0=[xt[0], xt[1], xt[2], xt[3], 0] xphi0 = [xt[0], 0, xt[1], xt[2], 0] # Create the time samples for the output of the ODE solver: t = [stoptime * float(i) / (numpoints - 1) for i in range(numpoints)] xsol = onslicesolver.integrate(xphi0, p, t, abserror=abserr, relerror=relerr) xsol3d = np.concatenate((np.array([xsol[:, 0]]).T, xsol[:, 2:4]), axis=1) #diff = np.concatenate((xsol[10:len(xsol),0:4], np.array([t[10:len(xsol)]]).T), axis=1) - xt[0:5] diff = np.concatenate( (xsol3d[10:len(xsol), 0:3], np.array([t[10:len(xsol)]]).T), axis=1) - xt[0:5] #diff = np.linalg.norm(diff, axis=1) #diff = xsol[10:len(xsol),0:4] - xt[0:4] #diff[:,4] = diff[:,4]/xt[4] diff = np.linalg.norm(diff, axis=1) imin = np.argmin(diff) + 10 #print "imin" #print imin #xf = [xsol[imin, 0], #xsol[imin, 1], #xsol[imin, 2], #xsol[imin, 3], #t[imin]] xf = [xsol3d[imin, 0], xsol3d[imin, 1], xsol3d[imin, 2], t[imin]] #print "xf" #print xf return xf - xt
def computeps(xsol, sectp, nhat, direction, p=pars): #Poincare hyperplane equation: def U(x): cond = np.dot(x - sectp, nhat) * direction return cond #Apply hyperplane condition to each element: uxarray = np.dot(xsol[:, 1:5] - sectp, nhat) * direction deltat = xsol[1, 0] - xsol[0, 0] # Read deltat from the data #dummy assignment for ps: ps = np.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], float) #Look for intersections: #for i in range(len(uxarray)-1): #if uxarray[i]<0 and uxarray[i+1]>0: for i in range(len(xsol) - 1): if U(xsol[i, 1:5]) < 0 and U(xsol[i + 1, 1:5]) > 0: xhatphidummy = xsol[i, 1:6] deltatadaptive = deltat / 2 pstime = xsol[i, 0] j = 1 condition = U(xhatphidummy[0:4]) while np.abs(condition) > tolerance: j = j + 1 #Adaptive integration: t = [float(0), deltatadaptive] xhatsol = onslicesolver.integrate(xhatphidummy, p, t, abserr, relerr) xhatphidummy = xhatsol[1, :] pstime = pstime + deltatadaptive condition = U(xhatphidummy[0:4]) if condition > 0: xhatphidummy = xhatsol[0, :] pstime = pstime - deltatadaptive deltatadaptive = deltatadaptive / 2 if j > 100: print 'not converging, exiting...' break pstime = np.array([pstime], float) psdummy = np.concatenate((pstime, xhatphidummy[0:4])) ps = np.append(ps, [psdummy], 0) ps = ps[2:len(ps), :] return ps
def computeps(xsol, sectp, nhat, direction, p=pars): #Poincare hyperplane equation: def U(x): cond=np.dot(x-sectp, nhat) * direction return cond #Apply hyperplane condition to each element: uxarray = np.dot(xsol[:,1:5] - sectp, nhat) * direction deltat = xsol[1,0]-xsol[0,0] # Read deltat from the data #dummy assignment for ps: ps = np.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], float) #Look for intersections: #for i in range(len(uxarray)-1): #if uxarray[i]<0 and uxarray[i+1]>0: for i in range(len(xsol)-1): if U(xsol[i, 1:5])<0 and U(xsol[i+1, 1:5])>0: xhatphidummy = xsol[i, 1:6] deltatadaptive = deltat/2 pstime = xsol[i,0] j=1 condition = U(xhatphidummy[0:4]) while np.abs(condition)>tolerance: j=j+1 #Adaptive integration: t = [float(0), deltatadaptive] xhatsol = onslicesolver.integrate(xhatphidummy, p, t, abserr, relerr) xhatphidummy=xhatsol[1, :] pstime = pstime + deltatadaptive condition = U(xhatphidummy[0:4]) if condition > 0: xhatphidummy=xhatsol[0,:] pstime = pstime - deltatadaptive deltatadaptive = deltatadaptive/2 if j > 100: print 'not converging, exiting...' break pstime = np.array([pstime], float) psdummy = np.concatenate((pstime, xhatphidummy[0:4])) ps = np.append(ps, [psdummy],0) ps = ps[2:len(ps),:] return ps
def computeps(xhatGS, p=pars): deltat = xhatGS[1, 0] - xhatGS[0, 0] # Read deltat from the data #dummy assignment for ps: ps = np.array([[0, 0, 0, 0], [0, 0, 0, 0]], float) #Look for intersections: for i in range(len(xhatGS) - 1): if U(xhatGS[i, 1:4]) < 0 and U(xhatGS[i + 1, 1:4]) > 0: xdummyGS = xhatGS[i, 1:4] deltatadaptive = deltat / 2 pstime = xhatGS[i, 0] j = 1 condition = U(xdummyGS) xhatphidummy = np.concatenate( (twomode.gramschmidt2ssp(xdummyGS), np.array([0], float))) while np.abs(condition) > tolerance: j = j + 1 #Adaptive integration: t = [float(0), deltatadaptive] xhatsol = onslicesolver.integrate(xhatphidummy, p, t, abserr, relerr) xhatphidummy = xhatsol[1, :] pstime = pstime + deltatadaptive xdummyGS = twomode.ssp2gramschmidt(xhatphidummy[0:4]) condition = U(xdummyGS) if condition > 0: xhatphidummy = xhatsol[0, :] pstime = pstime - deltatadaptive deltatadaptive = deltatadaptive / 2 if j > 100: print 'not converging, exiting...' break pstime = np.array([pstime], float) psdummy = np.concatenate((pstime, xdummyGS)) ps = np.append(ps, [psdummy], 0) ps = ps[2:len(ps), :] return ps
def computeps(xhatGS, p=pars): deltat = xhatGS[1,0]-xhatGS[0,0] # Read deltat from the data #dummy assignment for ps: ps = np.array([[0, 0, 0, 0], [0,0,0,0]], float) #Look for intersections: for i in range(len(xhatGS)-1): if U(xhatGS[i, 1:4])<0 and U(xhatGS[i+1, 1:4])>0: xdummyGS = xhatGS[i, 1:4] deltatadaptive = deltat/2 pstime = xhatGS[i,0] j=1 condition = U(xdummyGS) xhatphidummy=np.concatenate((twomode.gramschmidt2ssp(xdummyGS), np.array([0],float))) while np.abs(condition)>tolerance: j=j+1 #Adaptive integration: t = [float(0), deltatadaptive] xhatsol = onslicesolver.integrate(xhatphidummy, p, t, abserr, relerr) xhatphidummy=xhatsol[1, :] pstime = pstime + deltatadaptive xdummyGS = twomode.ssp2gramschmidt(xhatphidummy[0:4]) condition = U(xdummyGS) if condition > 0: xhatphidummy=xhatsol[0,:] pstime = pstime - deltatadaptive deltatadaptive = deltatadaptive/2 if j > 100: print 'not converging, exiting...' break pstime = np.array([pstime], float) psdummy = np.concatenate((pstime, xdummyGS)) ps = np.append(ps, [psdummy],0) ps = ps[2:len(ps),:] return ps
rangea1 = np.linspace(0.45, 0.49, num=1000) #dummy assignment for bif: bif = np.array([[0,0,0,0], [0,0,0,0]], float) for a1 in rangea1: p = [mu1, a1, b1, c1, mu2, a2, b2, c2, e2] stoptime = 1000 numpoints = 50000 # Create the time samples for the output of the ODE solver: t = [stoptime * float(i) / (numpoints - 1) for i in range(numpoints)] #Integrate xhatphi=onslicesolver.integrate(xhatphi0, p, t) #Discard the first 1000 time units and phi: xhat = xhatphi[numpoints/2:numpoints,0:4] #Transform to the Gram-Schmidt basis: xgs = twomode.ssp2gramschmidt(xhat.transpose()) t = np.array([t[numpoints/2:numpoints]],float) xgst = np.concatenate((t,xgs)) xgst = xgst.transpose() ps = psectgs.computeps(xgst, p) adummy = np.ones(len(ps))*a1 adummy = np.array([adummy]) bif = np.append(bif, np.concatenate((adummy.T,ps[:,1:4]), axis=1),0) bif = bif[2:len(bif),:]
print x0 itinerary = itineraries[i1] print itinerary[0] #Period of the ith cycle: T = periods[i-1] stoptime = T numpoints = int(stoptime / 0.01) # Create the time samples for the output of the ODE solver: t = [stoptime * float(i) / (numpoints - 1) for i in range(numpoints)] # Call the ODE solver xhatphi0 = np.append(x0, np.array([0], float)) xhatsol = onslicesolver.integrate(xhatphi0, pars, t, abserror=1.0e-14, relerror=1.0e-12) ax.plot(xhatsol[:,0], xhatsol[:,2], xhatsol[:,3], lw=0.5) ax.set_xlabel('$x$', fontsize=18) ax.set_ylabel('$y$', fontsize=18) ax.set_zlabel('$z$', fontsize=18) ax.view_init(25,35) ax.grid(b='off') fname = 'image/' + itinerary[0] + '.png' savefig(fname, bbox_inches='tight', dpi=100) #plt.show() #raw_input("Press Enter to continue...")
print itinerary[0] #Period of the ith cycle: T = periods[i - 1] stoptime = T numpoints = int(stoptime / 0.01) # Create the time samples for the output of the ODE solver: t = [stoptime * float(i) / (numpoints - 1) for i in range(numpoints)] # Call the ODE solver xhatphi0 = np.append(x0, np.array([0], float)) xhatsol = onslicesolver.integrate(xhatphi0, pars, t, abserror=1.0e-14, relerror=1.0e-12) ax.plot(xhatsol[:, 0], xhatsol[:, 2], xhatsol[:, 3], lw=0.5) ax.set_xlabel('$x$', fontsize=18) ax.set_ylabel('$y$', fontsize=18) ax.set_zlabel('$z$', fontsize=18) ax.view_init(25, 35) ax.grid(b='off') fname = 'image/' + itinerary[0] + '.png' savefig(fname, bbox_inches='tight', dpi=100) #plt.show() #raw_input("Press Enter to continue...")
def FPO(xn, tof): """ Vector input - Vector output function to solve for to get fixed points or n-cycles of the Poincare return map. xn = (n*(d-1))-dimensional array with n-cycle candidates at each row. xn = [x1_1 x1_2 x2_1 x2_2 x3_1 x3_2 ... ] Where in xi_j first index denotes ith element of cycle and the second index denotes jth coordinate of ith element. tof = n - dimensional vector to hold return time for each candidate """ #Reshape xn into a form such that at each row there are coordinates of #one element of the cycle in collumns. Namely, #xn = [[x1_1, x1_2], # [x2_1, x2_2], # [x3_1, x3_2], ...] xn = xn.reshape(np.size(tof), 2) #First compute P(x_i) and write as a matrix (n x d-1): Px = np.zeros(np.shape(xn)) #dummy assignment #print "Px" #print Px for i in range(np.size(xn,0)): #3D initial point to integrate xhat0=ps2D2psxhat(xn[i]) pret = np.array([], float) factor = 1.2 while np.size(pret)==0: #Integrate for a little bit longer than the time of flight: stoptime = factor*tof[i] #print "stoptime=" #print stoptime numpoints = int(stoptime/0.01) #Integration time array: t = [stoptime * float(j) / (numpoints - 1) for j in range(numpoints)] xhatphi0 = np.append(xhat0, np.array([0], float)) xhatsol = onslicesolver.integrate(xhatphi0, pars, t, abserror=1.0e-14, relerror=1.0e-12) tx = np.append(np.array([t], float).transpose(), xhatsol, axis=1) #Compute Poincare section: psreturn=psectslice.computeps(tx, sectp, nhat, direction, p=pars) #Take the time nearest to the approximated time. This is due to the #fact that the array ps sometimes includes the initial point and sometimes #does not, hence we are not always sure the position of the first return. #print "xsol:" #print xsol #print "psreturn:" #print psreturn if np.size(psreturn,0) != 0: itof = np.argmin(np.abs(psreturn[:,0])-tof[i]) pret = psreturn[itof, 0:] else: factor = factor**2 if factor > 50: print "something went wrong for:" print "xhat0 = " print xhat0 print "Time of flight:" print tof[i] break ps2D = psxhat2ps2D(pret)[0:2] #print "ps2D" #print ps2D Px[i,:] = ps2D Pxshifted = np.append(np.array([Px[np.size(Px,0)-1,:]]), Px[0:np.size(Px,0)-1,:], axis=0) fun = xn - Pxshifted #Convert back to a vector function fun = fun.reshape(np.size(fun)) return fun
ax.hold(True) for i in range(nplot): stoptime = Trpo[i] numpoints = int(stoptime / 0.001 + 1.0) t = np.linspace(0, stoptime, numpoints) xphi0 = [xrpo[i, 0], 0, xrpo[i, 1], xrpo[i, 2], 0] #xphi0=[xrpo[i, 0], xrpo[i, 1], xrpo[i, 2], xrpo[i, 3], 0] xsol = onslicesolver.integrate(xphi0, pars, t, abserror=abserr, relerror=relerr) if i % nplot == 0: color = 'b' elif i % nplot == 1: color = 'r' elif i % nplot == 2: color = 'g' elif i % nplot == 3: color = 'k' ax.plot(xsol[:, 0], xsol[:, 2], xsol[:, 3], linewidth=lw, c=color) print xsol[0, :] - xsol[numpoints - 1, :]
ax.w_zaxis.set_pane_color((1, 1, 1, 1.0)) ax.view_init(30,30) ax.hold(True) for i in range(nplot): stoptime = Trpo[i] numpoints = int(stoptime/0.001 + 1.0) t = np.linspace(0,stoptime,numpoints) xphi0=[xrpo[i, 0], 0, xrpo[i, 1], xrpo[i, 2], 0] #xphi0=[xrpo[i, 0], xrpo[i, 1], xrpo[i, 2], xrpo[i, 3], 0] xsol = onslicesolver.integrate(xphi0, pars, t, abserror=abserr, relerror=relerr) if i%nplot == 0: color = 'b' elif i%nplot == 1: color = 'r' elif i%nplot == 2: color = 'g' elif i%nplot == 3: color = 'k' ax.plot(xsol[:,0], xsol[:,2], xsol[:,3], linewidth=lw , c=color) print xsol[0,:] - xsol[numpoints-1,:] savefig('image/rpos.png', bbox_inches='tight', dpi=300)
xphi02 = [0.43998243, 0.0, -1.68577368, 0.06639063, 0]# rpo01 xphi03 = [0.0384074556708, 0.0, -1.90362452394, 0.0668631895808, 0] #attractor u01 = twomode.ssp2invpol(x01) u02 = twomode.ssp2invpol(x02) u03 = twomode.ssp2invpol(x03) t1 = np.linspace(0,100,100000) t2 = np.linspace(0, 2*7.34594139e+00, 10000) t3 = t1 xsol1 = sspsolver.integrate(x01, p, t1) xsol2 = sspsolver.integrate(x02, p, t2) xsol3 = sspsolver.integrate(x03, p, t3) xhatsol1 = onslicesolver.integrate(xphi01, p, t1) xhatsol2 = onslicesolver.integrate(xphi02, p, t2) xhatsol3 = onslicesolver.integrate(xphi03, p, t3) usol1 = invpolsolver.integrate(u01, p, t1) usol2 = invpolsolver.integrate(u02, p, t2) usol3 = invpolsolver.integrate(u03, p, t3) xtildesol1 = twomode.ssp2sspRed2(xsol1) xtildesol2 = twomode.ssp2sspRed2(xsol2) xtildesol3 = twomode.ssp2sspRed2(xsol3) #Plot full state space: fig = plt.figure(figsize=(8,7)) ax = fig.gca(projection='3d')
def rpo(x, Tapproximate): print "x" print x abserr = 1.0e-14 relerr = 1.0e-12 starttime = Tapproximate * 0.9 stoptime = Tapproximate * 1.1 #print "starttime = ", starttime #print "stoptime = ", stoptime #numpoints = int(stoptime/0.0001 + 1.0) numpoints1 = int(starttime / 0.01 + 1.0) numpoints = int(1e4) #print "numpoints:" #print numpoints #numpoints = 2 #xphi0=[x[0], x[1], x[2], x[3], 0] xphi0 = [x[0], 0, x[1], x[2], 0] t1 = [starttime * float(i) / (numpoints1 - 1) for i in range(numpoints1)] xsol1 = onslicesolver.integrate(xphi0, p, t1, abserror=abserr, relerror=relerr) xphi1 = xsol1[len(xsol1) - 1, :] # Create the time samples for the output of the ODE solver: #t = [stoptime * float(i) / (numpoints - 1) for i in range(numpoints)] #t = np.append([0], np.linspace(starttime, stoptime, numpoints)) t = [(stoptime - starttime) * float(i) / (numpoints - 1) for i in range(numpoints)] #xsol = onslicesolver.integrate(xphi0, p, t, abserror=abserr, relerror=relerr) xsol = onslicesolver.integrate(xphi1, p, t, abserror=abserr, relerror=relerr) xsol3d = np.concatenate((np.array([xsol[:, 0]]).T, xsol[:, 2:4]), axis=1) #diff = xsol[int(numpoints/2):len(xsol),0:4] - x #diff = xsol3d[int(numpoints/2):len(xsol),0:3] - x diff = xsol3d[0:len(xsol3d), 0:3] - x diff = np.linalg.norm(diff, axis=1) #imin = np.argmin(diff)+int(numpoints/2) #imin = np.argmin(diff)+1 imin = np.argmin(diff) print "tmin" print t[imin] + starttime #xf = [xsol[imin, 0], #xsol[imin, 1], #xsol[imin, 2], #xsol[imin, 3]] xf = [xsol3d[imin, 0], xsol3d[imin, 1], xsol3d[imin, 2]] print "xf" print xf return xf - x
u02 = twomode.ssp2invpol(x02) u03 = twomode.ssp2invpol(x03) tfreqv = 50 tfrpo = 10*3.641511999233241426e+00 tfrpo = 2* 7.34594127 tfergo = tfreqv treqv = np.linspace(0,tfreqv,5000) trpo = np.linspace(0, tfrpo, 10000) tergo = np.linspace(0, tfergo, 5000) xsolreqv = sspsolver.integrate(x01, p, treqv) xsolrpo = sspsolver.integrate(x02, p, trpo) xsolergo = sspsolver.integrate(x03, p, tergo) xhatsolreqv = onslicesolver.integrate(xphi01, p, treqv) xhatsolrpo = onslicesolver.integrate(xphi02, p, trpo) xhatsolergo = onslicesolver.integrate(xphi03, p, tergo) def inverseFourier(xsol): """ Generates configuration space solution u(x) = F^-1{z[k]} from the Fourier modes """ zsol = np.array([[xsol[i,0] + 1j*xsol[i,1], xsol[i,2] + 1j*xsol[i,3]] for i in range(np.size(xsol, 0))],complex) x = np.arange(-np.pi, np.pi, 0.05) exp1jx = np.exp(1j*x) exp2jx = np.exp(2j*x)