0

I am writing a Molecular Dynamics code and for that I have a function that computes forces between particles: conservative, random and dissipative forces. The conservative forces are pairwise forces, which means I have a double loop for to compute them. I wanted to save some time and include the calculation of the random and dissipative forces in one of the loops of the double loop as follows:

fr = np.zeros((npart, dim))
fd = np.zeros((npart, dim))
fc = np.zeros((npart, dim))

for i in range(npart-1):

    for d in range(dim):
        # dissipative and random forces
        fd[i, d] = -gamma * v[i, d]
        fr[i, d] = noise/np.sqrt(dt) * np.random.normal()

    for j in range(i+1, npart):

        # conservative force for particle i
        fc[i, 0] = fc[i, 0] + (dX/r2) * fr
        fc[i, 1] = fc[i, 1] + (dY/r2) * fr
        fc[i, 2] = fc[i, 2] + (dZ/r2) * fr

        # conservative force for particle j (action-reaction)
        fc[j, 0] = fc[j, 0] - (dX/r2) * fr
        fc[j, 1] = fc[j, 1] - (dY/r2) * fr
        fc[j, 2] = fc[j, 2] - (dZ/r2) * fr

Here gamma, noise and dt are constants. I get the following error:

    fr[i, d] = noise/np.sqrt(dt)*np.random.normal()
TypeError: 'numpy.float64' object does not support item assignment

Nevertheless, if I compute the random and dissipative forces in an external, separate loop, the error disappears:

for i in range(npart):
    for d in range(dim):
        fd[i, d] = -gamma * v[i, d]
        fr[i, d] = noise/np.sqrt(dt) * np.random.normal()

What is the difference between both computations? Why there is no error when the computation is done in a separate loop?

6
  • your first for loops have different lengths. could it be the source of the problem? Commented Jul 4, 2019 at 8:37
  • How do you define fr and fc at the beginning? And what of these objects are numpy objects? Commented Jul 4, 2019 at 8:41
  • @micric Sorry, I forgot to add that fr, fc and fd are numpy arrays. I just corrected it now. Only fr, fc and fd, together with v are numpy arrays. Commented Jul 4, 2019 at 9:52
  • @BehzadMehrtash I printed the outcome of the loops to see what was not working, and the first for loop in 'dim' runs correctly once, then it raises the error. That is, it d = 0, d = 1 and d = 2 without problems for i = 0, and then it raises the error. Commented Jul 4, 2019 at 9:55
  • 1
    fc[i, 0] = fc[i, 0] + (dX/r2) * fr in this line there is a size mismatch, isn't there? fr and fc are the same size at the beginning, but now you are summing fr to just one column Commented Jul 4, 2019 at 10:01

1 Answer 1

1

SOLVED: As @micric pointed out, there is a variable inside the second loop called 'fr', which is of type float. I made the mistake of using the same name for an array. Hence Python's complaints.

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.