0

I am unable to calculate the average values of atomic distances using NumPy with Python. I used get_distances to get the distance values of the atoms in the trajectory file and I want to calculate the average of these distances.

I have tried to solve this on my own by reading other StackOverflow questions with similar problems and the ASE website(https://wiki.fysik.dtu.dk/ase/ase/atoms.html#ase.Atoms.get_all_distances) to figure out how to get the average distance values.

The current error I have now is The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() To my understanding, NumPy is reading the 'mic=True, vector=True' from the line below as an unclear message because it could relate to various ways the elements could be True, however, I do not know how to specify if all of the elements are True or if there is maybe an error earlier in my script.

Here is the code I have so far with the output error. I am wondering how to incorporate a.any() or a.all() into my code or if I should use np.array as I saw that helped others with this issue. Any help would be greatly appreciated since I am new to writing codes in python and I am struggling to understand my errors.

Input:

traj = Trajectory('optimisation.traj')
for atoms in traj:
    new_slab = deepcopy(atoms)
    all_distances=np.array(atoms.get_all_distances(mic=True))
    x,y = np.shape(all_distances)
    listofdistances =[]
    for i in range(x):
        atom_indices=np.flatnonzero(all_distances[i,:]<2.0)
        if len(atom_indices) == 4:
            print(atom_indices)
            ai0 = atom_indices[0]
            ai1 = atom_indices[1]
            ai2 = atom_indices[2]
            ai3 = atom_indices[3]

            if atoms.get_distances(i,ai0, mic=True, vector=True) == 0:
                listofdistances += atoms.get_distances(i, ai1, mic=True, vector=True)
                listofdistances += atoms.get_distances(i, ai2, mic=True, vector=True)
                listofdistances += atoms.get_distances(i, ai3, mic=True, vector=True)
            elif ai1 == i:
                listofdistances += atoms.get_distances(i, ai0, mic=True, vector=True)
                listofdistances += atoms.get_distances(i, ai2, mic=True, vector=True)
                listofdistances += atoms.get_distances(i, ai3, mic=True, vector=True)
            elif ai2 == i:
                listofdistances += atoms.get_distances(i, ai0, mic=True, vector=True)
                listofdistances += atoms.get_distances(i, ai1, mic=True, vector=True)
                listofdistances += atoms.get_distances(i, ai3, mic=True, vector=True)
            elif ai3 == i:
                listofdistances += atoms.get_distances(i, ai0, mic=True, vector=True)
                listofdistances += atoms.get_distances(i, ai2, mic=True, vector=True)
                listofdistances += atoms.get_distances(i, ai3, mic=True, vector=True)
            else:
                print(listofdistances)

np.average(listofdistances)
print(listofdistances)

Output:

[ 0  1 11 23]
Traceback (most recent call last):
  File "plotValues.py", line 109, in <module>
    if atoms.get_distances(i,ai0, mic=True, vector=True) == 0:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
4

0 回答 0