Reputation: 61
I have an array as follows:
xyz = [[ 0.08778, 0.99666, 0.30648],
[ 0.41688, 2.70076, 1.10135],
[ 1.90494, 0.91685, -0.26984],
[-0.44512, 1.73972, -1.24406],
[-1.68572, 1.01617, 1.01221],
[-0.1977 , -0.77177, -0.35553],
[ 0.61588, 3.6995 , 1.59813],
[ 2.98863, 0.86173, -0.59616],
[-0.78312, 2.20935, -2.22362],
[-2.73208, 1.02071, 1.44702],
[-0.36006, -1.82939, -0.72827]]
I want to add 0.005 to the first element (0.08778), and then save this array to a new file 'output0.xyz'. I then want to add 0.005 to the second element (0.99666) and keep the first element as its original value, and save this array to a new file 'output1.xyz', and so on. So I need to write 33 new files, each with just one of the numbers in xyz changed. Here is my code:
i = 30
number_atoms = 12
at_nums = [26., 6., 6., 6., 6., 6., 8., 8., 8., 8., 8.])
for atom in xyz:
for coordinate in atom:
coordinate += 0.005
with open('output{}.xyz'.format(i), 'w') as f:
f.write('{:.0f}\n\n'.format(number_atoms))
for at_num_i, xyz_disp_i in zip(at_nums, xyz):
f.write('{:.0f}\t{:.8f}\t{:.8f}\t{:.8f}\n'.format(at_num_i, *xyz_disp_i))
Currently this code is just saving the original array into a file called 'output30.xyz'. I've been stumped on this for a while now!
Upvotes: 1
Views: 415
Reputation: 18106
You need copy xyz
each iteration and change the values in the copy in order to keep track of original values:
number_atoms = 12
at_nums = [26., 6., 6., 6., 6., 6., 8., 8., 8., 8., 8.]
xyz = [
[0.08778, 0.99666, 0.30648], [0.41688, 2.70076, 1.10135],
[1.90494, 0.91685, -0.26984], [-0.44512, 1.73972, -1.24406],
[-1.68572, 1.01617, 1.01221], [-0.1977, -0.77177, -0.35553],
[0.61588, 3.6995, 1.59813], [2.98863, 0.86173, -0.59616],
[-0.78312, 2.20935, -2.22362], [-2.73208, 1.02071, 1.44702],
[-0.36006, -1.82939, -0.72827]
]
for i, atom in enumerate(xyz):
for j, item in enumerate(atom):
new_xyz = [row.copy() for row in xyz]
new_xyz[i][j] += .005
with open('/tmp/output{}.xyz'.format(i * 3 + j), 'w') as f:
f.write('{:.0f}\n\n'.format(number_atoms))
for at_num_i, xyz_disp_i in zip(at_nums, new_xyz):
f.write(
'{:.0f}\t{:.8f}\t{:.8f}\t{:.8f}\n'.format(
at_num_i, *xyz_disp_i
)
)
Upvotes: 1