我已经完成了暑期实习的 Python 代码。
这是代码:
from numpy import loadtxt, abs, mean, float64
def Sum_radii(particle1, particle2): #Sum of radii - to be compared with distance
summ = particle1.radius+particle2.radius
return summ
def PBCdist(particle1, particle2): #PBC conditions to compute distance for any two particles
dx = abs(particle1.x-particle2.x)
dy = abs(particle1.y-particle2.y)
#dz = abs(particle1.z-particle2.z)
if dx > 0.5:
dx = 1.0-dx
else:
dx = dx
if dy > 0.5:
dy = 1.0-dy
else:
dy = dy
#if dz > 0.5:
# dz = 1.0-dz
#else:
# dz = dz
DX = dx**2
DY = dy**2
#DZ = dz**2
dist = DX+DY
distance = dist**0.5
return distance
def Final_step(my_particles):
for particle1 in my_particles:
if len(particle1.neighbours) <=2 :
for k in range(len(particle1.neighbours)):
n = particle1.neighbours[k]
my_particles[n].neighbours.remove(particle1.n)
for particle1 in my_particles:
if len(particle1.neighbours) <=2 :
my_particles.remove(particle1)
return my_particles
def Recursion(my_particles):
l1 = len(my_particles)
my_particles = Final_step(my_particles)
l2 = len(my_particles)
if (l1!=l2):
Recursion(my_particles)
else:
return my_particles
f = open("contacts.txt", "w")
for i in range(len(my_particles)):
list = []
list.append(my_particles[i].n,my_particles[i].neighbours)
print list
print >>f, list
f.close()
def mean_contacts(my_particles):
for k in range(len(my_particles)):
contact_number.append(len(my_particles[k].neighbours))
print ("%.20f" % mean(contact_number))
#Read data and define the class Particle
class Particle():
def __init__(self, (x,y), n, radius, neighbours):
self.n = n
self.x = x
self.y = y
#self.z = z
self.radius = radius
self.neighbours = neighbours
number = loadtxt("Final.dat", usecols=(0,), unpack=True, dtype = int)
c1,c2,r = loadtxt("Final.dat", usecols=(1,2,4), unpack=True, dtype=float64)
number_of_particles = len(number)
my_particles = []
overlap = []
contact_number = []
for i in range(number_of_particles):
n = number[i]
x = c1[i]
y = c2[i]
#z = c3[i]
radius = r[i]
neighbours = []
particle = Particle((x,y), n, radius, neighbours)
my_particles.append(particle)
for particle1 in my_particles:
for particle2 in my_particles:
distance = PBCdist(particle1, particle2)
sum_of_radii = Sum_radii(particle1, particle2)
if (distance < sum_of_radii) and (distance>0):
olap = sum_of_radii - distance
overlap.append(olap)
particle1.neighbours.append(particle2.n)
#Recursion(my_particles)
Final_step(my_particles)
mean_contacts(my_particles)
如您所见,我没有尝试过递归函数,只是为了让事情更简单。
现在,读取的文件格式如下:
0 0.70138224747245225821 0.28586219648439409324 0 0.0037570717610070714435
1 0.94878397047547669008 0.17267104541971631249 0 0.0038326670080525947204
2 0.59078448810638095612 0.29243415714920478754 0 0.0037315418643608781225
3 0.38696755396911874936 0.15180438637928708734 0 0.004051606114197996676 2
4 0.71585843878867627676 0.47742962311059283786 0 0.0043035198430089825067
对于 16383 行数据。当我尝试在 4 分钟后运行代码时,我收到以下错误消息:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/monitor.py", line 575, in run
already_pickled=True)
File "/usr/lib/python2.7/dist-packages/spyderlib/utils/bsdsocket.py", line 24, in write_packet
sock.send(struct.pack("l", len(sent_data)) + sent_data)
error: [Errno 32] Broken pipe
我用一个 128 行的数据文件进行了尝试,并且在 1 秒内一切正常。
我想知道该消息首先意味着什么,以及如果可能的话,如何修复它。
我在 Ubuntu12.04、4 GB 内存、64 位桌面上运行。