Dear all

I am trying to create a movie file of a monte carlo simulation at each time 
step from the plot of matplotlib in python2.7.
The movie creation using ffmpeg is giving following error


Traceback (most recent call last):

  File "code1.py", line 99, in <module>

    print simulate(side, time, density)

  File "code1.py", line 94, in simulate

    t+=1

  File "/Users/smaheshwari/anaconda2/lib/python2.7/contextlib.py", line 35, in 
__exit__

    self.gen.throw(type, value, traceback)

  File 
"/Users/smaheshwari/anaconda2/lib/python2.7/site-packages/matplotlib/animation.py",
 line 256, in saving

    self.finish()

  File 
"/Users/smaheshwari/anaconda2/lib/python2.7/site-packages/matplotlib/animation.py",
 line 276, in finish

    self.cleanup()

  File 
"/Users/smaheshwari/anaconda2/lib/python2.7/site-packages/matplotlib/animation.py",
 line 311, in cleanup

    out, err = self._proc.communicate()

  File 
"/Users/smaheshwari/anaconda2/lib/python2.7/site-packages/subprocess32.py", 
line 927, in communicate

    stdout, stderr = self._communicate(input, endtime, timeout)

  File 
"/Users/smaheshwari/anaconda2/lib/python2.7/site-packages/subprocess32.py", 
line 1713, in _communicate

    orig_timeout)

  File 
"/Users/smaheshwari/anaconda2/lib/python2.7/site-packages/subprocess32.py", 
line 1769, in _communicate_with_poll

    register_and_append(self.stdout, select_POLLIN_POLLPRI)

  File 
"/Users/smaheshwari/anaconda2/lib/python2.7/site-packages/subprocess32.py", 
line 1748, in register_and_append

    poller.register(file_obj.fileno(), eventmask)

ValueError: I/O operation on closed file

Please tell me what is the work around to this
I am using FFMpeg on MacOSX and this is the latest version


ffmpeg version N-78810-g4e05a12 Copyright (c) 2000-2016 the FFmpeg developers

built with Apple LLVM version 6.1.0 (clang-602.0.51) (based on LLVM 3.6.0svn)

configuration: --disable-yasm

libavutil      55. 19.100 / 55. 19.100

libavcodec     57. 27.101 / 57. 27.101

libavformat    57. 26.100 / 57. 26.100

libavdevice    57.  0.101 / 57.  0.101

libavfilter     6. 37.100 /  6. 37.100

libswscale      4.  0.100 /  4.  0.100

libswresample   2.  0.101 /  2.  0.101


My code is attached


sudeep

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
import os

#create a 2D lattice with -1 and 1 spins
def lattice(side, density):
	array = np.zeros((side,side))
	halfside = np.int(side/2.)
	
	SU = np.zeros((side, halfside))
	SD = np.zeros((side, side-halfside))

	places = int(SU.shape[0]*SU.shape[1]*density)
	placessur = np.random.randint(0,SU.shape[0],places)
	placessuc = np.random.randint(0,SU.shape[1],places)
	SU[placessur, placessuc] = -1
	SU[:,:] = -1

	places = int(SD.shape[0]*SD.shape[1]*density)
	placessur = np.random.randint(0,SD.shape[0],places)
	placessuc = np.random.randint(0,SD.shape[1],places)
	SD[placessur, placessuc] = 1
	SD[:,:] = 1

	array = np.column_stack((SU,SD))
	array = array.astype(int)
	return array

#compute total energy
def totenergy(syst, side):
	Ene = 0
	for i in range(len(syst[:,0])):
		for j in range(len(syst[:,1])):
			WF = syst[(i+1)%side,j] + syst[i,(j+1)%side] + syst[(i-1)%side,j] + syst[i,(j-1)%side]
			Ene += -WF*syst[i,j] # Each neighbor gives energy 1.0
	return Ene/2. # Each par counted twice

#compute magnetization
def magnet(syst, side):
	return (1./(side**2))*(np.sum(syst))

#conditions
kbT, density, side, time = 2.0, 0.9, 4, 5

#writer of animation
FFMpegWriter = animation.writers['ffmpeg']
metadata = dict(title='MovieTest', artist='Matplotlib', comment='Movie support!')
writer = FFMpegWriter(metadata=metadata)
	
#figure for animation
fig = plt.figure()

#start simulation
def simulate(side, time, density):
	syst = lattice(side,density)
	energy = totenergy(syst, side)
	magnetize = []
	t = 0
	
	with writer.saving(fig, 'simulation.avi', time):
		while t<time:
			#pick a point randomly
			r = np.random.random_integers(1,side,2)
			posr, posc = r[0]-1, r[1]-1
	
			#neighbors
			NRP, NRM = (posr+1)%side, (posr-1)%side
			NCP, NCM = (posc+1)%side, (posc-1)%side
			NRP, NRM = syst[NRP, posc], syst[NRM, posc]
			NCP, NCM = syst[posr, NCP], syst[posr, NCM]
	
			#change spin
			syst[posr,posc] = (-1)*syst[posr,posc]
		
			#calculate energy now
			chenergy = energy - 2*(syst[posr,posc]*(NRP + NRM + NCP + NCM))
		
			#determine if it should be accepted
			if chenergy - energy < 0:
				energy = chenergy
			else:
				eps = np.random.rand()
				if eps < np.exp(-float((chenergy-energy)/kbT)):
					energy = chenergy
				else:
					syst[posr,posc] = (-1)*syst[posr,posc]
	
			#plot what happened		
			plt.matshow(syst, cmap=plt.cm.bwr)
			writer.grab_frame()
			plt.clf()
			plt.pause(0.05)
			t+=1
	return syst



print simulate(side, time, density)


_______________________________________________
ffmpeg-user mailing list
ffmpeg-user@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
ffmpeg-user-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to