diff options
author | Ian Jauslin <jauslin@ias.edu> | 2019-05-22 22:59:03 -0400 |
---|---|---|
committer | Ian Jauslin <jauslin@ias.edu> | 2019-05-22 22:59:03 -0400 |
commit | df7449e4a29ec8d3182cf7b2aebcb86f7ac596c2 (patch) | |
tree | 182c5c85f57264b38c60a02ec268e5c00d937b34 /figs/animation/animate.py |
Diffstat (limited to 'figs/animation/animate.py')
-rw-r--r-- | figs/animation/animate.py | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/figs/animation/animate.py b/figs/animation/animate.py new file mode 100644 index 0000000..aaa3a80 --- /dev/null +++ b/figs/animation/animate.py @@ -0,0 +1,127 @@ +from matplotlib import pyplot as pl +from matplotlib import animation +import sys + +# read data +# time dependent data +frames=[] +# asymptotic data (located in the first block) +asym=[] +infile=open(sys.argv[1],'r') +row=[] +for line in infile: + # read first block + if len(asym)==0: + if line=='\n': + asym=row + row=[] + else: + dat=[] + for n in line.split(): + dat.append(float(n)) + row.append(dat) + # read other blocks + else: + if line=='\n': + frames.append(row) + row=[] + else: + dat=[] + for n in line.split(): + dat.append(float(n)) + row.append(dat) +infile.close() + + +# set up plot +fig = pl.figure() +#pl.subplot(211) +axr=fig.gca() +asym_rho, = axr.plot([],[],linewidth=3.5,color='#00FF00') +rho, = axr.plot([],[],color='red') + +#pl.subplot(212) +#axJ=fig.gca() +#asym_J, = axJ.plot([],[],linewidth=3.5,color='#00FF00') +#J, = axJ.plot([],[],color='red') + +# plot ranges +xmax=0 +maxyr=0 +maxyJ=0 +for frame in frames: + for i in range(len(frame)): + if frame[i][1]>xmax: + xmax=frame[i][1] + if frame[i][2]>maxyr: + maxyr=frame[i][2] + if frame[i][3]>maxyJ: + maxyJ=frame[i][3] +for i in range(len(asym)): + if asym[i][0]>xmax: + xmax=asym[i][0] + if asym[i][1]>maxyr: + maxyr=asym[i][1] + if asym[i][2]>maxyJ: + maxyJ=asym[i][2] +xmin=0 +minyr=0 +minyJ=0 +for frame in frames: + for i in range(len(frame)): + if frame[i][1]<xmin: + xmin=frame[i][1] + if frame[i][2]<minyr: + minyr=frame[i][2] + if frame[i][3]<minyJ: + minyJ=frame[i][3] +for i in range(len(asym)): + if asym[i][0]<xmin: + xmin=asym[i][0] + if asym[i][1]<minyr: + minyr=asym[i][1] + if asym[i][2]<minyJ: + minyJ=asym[i][2] + + +# plot asymptotes +asym_rho_datax=[] +asym_rho_datay=[] +for i in range(len(asym)): + asym_rho_datax.append(asym[i][0]) + asym_rho_datay.append(asym[i][1]) +asym_rho.set_data(asym_rho_datax,asym_rho_datay) +#asym_J_datax=[] +#asym_J_datay=[] +#for i in range(len(asym)): +# asym_J_datax.append(asym[i][0]) +# asym_J_datay.append(asym[i][2]) +#asym_J.set_data(asym_J_datax,asym_J_datay) + +# animate +def init_plot(): + axr.set_ylim(minyr,maxyr) + axr.set_xlim(xmin,xmax) + #axJ.set_ylim(minyJ,maxyJ) + #axJ.set_xlim(xmin,xmax) + + axr.vlines(0,minyr,maxyr,linestyles="dotted") + #axJ.vlines(0,minyJ,maxyJ,linestyles="dotted") +def update(frame): + axr.set_title("t=% .3f fs" % (frame[0][0])) + xdata=[] + ydata=[] + for i in range(len(frame)): + xdata.append(frame[i][1]) + ydata.append(frame[i][2]) + rho.set_data(xdata,ydata) + + #xdata=[] + #ydata=[] + #for i in range(len(frame)): + # xdata.append(frame[i][1]) + # ydata.append(frame[i][3]) + #J.set_data(xdata,ydata) +anim = animation.FuncAnimation(fig, update, frames=frames, blit=False, interval=100, repeat=True, init_func=init_plot) +#anim.save('schrodinger_barrier.mp4', fps=15, extra_args=['-vcodec', 'libx264']) +pl.show() |