0

The code is for generating bead positions of a 3d polymer. It is getting into an infinite loop. I could not figure out the mistake.

import numpy as np
import math as m

atoms = 249
pos = [[]]
sigma = 1.0
pos[0] = [0, 0, 0]

for p in range(atoms):
    md = 0.0
    c = 0
    n = p

    while md < sigma:
        rnd_vector = (2 * (np.random.rand(3))) - [1, 1, 1]
        rnd_vector = np.round(rnd_vector, 6)

        d = m.sqrt(sum(map(lambda x: x * x, rnd_vector)))

        rnd_vector = [x / d for x in rnd_vector]

        next_pos = [pos[p][x] + rnd_vector[x] for x in range(3)]

        c += 1

        for i in range(n):
            temp = [rnd_vector[x] - pos[i][x] for x in range(3)]
            td = m.sqrt(sum(map(lambda x: x * x, temp)))

            if i == 0:
                md = td

            if td < md:
                md = td

    pos.append(next_pos)
0

1 Answer 1

3

Difficult to tell without knowing what exactly the program is supposed to do, however, the reason for the infinite loop is that in the very first iteration, when n=p=0, the inner-most loop is never executed and thus md is never updated.

If you change the inner loop to for i in range(n+1):, then the program eventually terminates, though I can not tell if that's still doing what it is supposed to do.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.