Счупена тръба на Python

Завърших код в 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)

Както можете да видите, не съм пробвал функцията Recursion, просто за да направя нещата по-прости.

Сега файлът за четене е форматиран по следния начин:

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 ram, 64-битов десктоп.


person Blaise Delaney    schedule 12.07.2013    source източник
comment
Актуализирайте spyder.   -  person jorgeca    schedule 12.07.2013
comment
Това пълното проследяване ли е?   -  person tommy.carstensen    schedule 04.11.2014


Отговори (1)


Това изглежда като известен проблем в Spyder 2.1.10, според Проблем 1474 (Проблем 1106).

Корекцията изглежда е налична в Spyder 2.2.

person Anand S Kumar    schedule 24.06.2015