# Tasia Raymer
# 250 Project

# This program will plot the bifurcation diagram for different r=r1=r2 for a fixed d.
# The plot will be of r vs (x1+x2) and (x1-x2) 

#dynamic equations:
from CpledLogClass import *

import pylab as pl
from RandomArray import *
from numpy import arange

#set values of r=r1=r2 to sweep over and d:
rmin=3.0
rmax=4.0
dvec=[0.0,.05,.1,.15,.25,.35,.5]

#we will use initial conditions from a uniform [0,1] distribution:
seed()
uniform(0,1)
x1_0=uniform(0,1,(3,))
x2_0=uniform(0,1,(3,))

# x1_0=uniform(0,1)
# x2_0=uniform(0,1)

nTrans=200
#the number of itertations show for each value of r:
nIterations=1000
#this determines how many values of r will be shown:
nSteps=400.0
#the value for incrementing between rmin adn rmax:
rInc=(rmax-rmin)/nSteps


#sweep over d values:
for i in range(len(dvec)):
	d=dvec[i]
	f=pl.figure(i,(18,7))
	f.text(.5,.95,'d=%g'%dvec[i])
	count=0
	for j in range(len(x1_0)):
	# for j in range(1):
		count=count+1
		print count
	#sweep over parameter values:
		for r in arange(rmin,rmax,rInc):
			r1=r
			r2=r
		
			x1=x1_0[j]
			x2=x2_0[j]
			# x1=x1_0
			# x2=x2_0
		#set dynamics equations from the Couple Logistic Class:
			F=CpledLogEqs(r1,r2,d)
		
			for n in range(nTrans):
				x1temp=F.x1dot(x1,x2)
				x2=F.x2dot(x1,x2)
				x1=x1temp
		#set up lists to store values of the next iterations:
			rvals=[]
			x1vals=[]  
			x2vals=[]
			sum=[]
			diff=[]
			
		
			for n in range(nIterations):
				x1temp=F.x1dot(x1,x2)
				x2=F.x2dot(x1,x2)
				x1=x1temp
				x1vals.append(x1)
				x2vals.append(x2)
				sum.append(x1+x2)
				diff.append(x1-x2)
				rvals.append(r)
			pl.subplot(2,3,count)
			# pl.subplot(1,2,1)
			pl.title('(%.5f,%.5f)'%(x1_0[j],x2_0[j]))
			# pl.title('(%.5f,%.5f)'%(x1_0,x2_0))
			pl.xlabel('r')
			pl.ylabel('x1+x2')
			pl.plot(rvals,sum,'k,')
		
			pl.subplot(2,3,count+3)
			# pl.subplot(1,2,2)
			pl.title('(%.5f,%.5f)'%(x1_0[j],x2_0[j]))
			# pl.title('(%.5f,%.5f)'%(x1_0,x2_0))
			pl.xlabel('r')
			pl.ylabel('x1-x2')
			pl.plot(rvals,diff,'k,')
	
pl.show()
