So I have a script that reads multiple values from a file, and creates lists with these values.
I have a list of fractional coordinates of atoms (where each element is a list containing x, y, z coords) and their corresponding charges in another list. I also have three values that are scalars and correspond to the dimensions I am working in.
Here is a snippet of how the lists look:
Coords = [[0.982309, 0.927798, 0.458125], [0.017691, 0.072202, 0.958125], [0.482309, 0.572202, 0.458125], [0.517691, 0.427798, 0.958125], [0.878457, 0.311996, 0.227878], [0.121543, 0.688004, 0.727878], [0.378457, 0.188004, 0.227878], [0.621543, 0.811996, 0.727878], [0.586004, 0.178088, 0.37778], [0.413997, 0.821912, 0.87778], [0.086003, 0.321912, 0.37778], ......]
Charges = [0.18, 0.18, 0.18, 0.18, 0.17, 0.17, 0.17, 0.17, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.39, 0.39, 0.39, 0.39, 0.4, 0.4, 0.4, 0.4, 0.17, 0.17, 0.17....]
Then I have the three dimension values that I will call a, b, c.
I need to do an inner (dot) product of the fractional coordinates by the dimensions, for each atom. Of this sub-list, I need to multiply the first element by a, the second element by b and the third by c. I also need to multiply each of these components by the corresponding charge. This will give me the dipole moments, that I need to finally calculate.
Detailed example:
So I want to take each element in the list. We'll start with element 0.
So coords[0] = [0.982309, 0.927798, 0.458125], and charge[0] = 0.18
What I want to do, is take the first element of coords[0], multiply that by a. Then the second by b, and the third by c. Then, I want to multiply each of these elements by charges[0], 0.18, and sum the three together. That gives me the dipole moment due to one atom. I want to repeat this for every atom, so coords[1] with charges[1], coords[2] with charges[2] and so forth. Then, I want to sum all of these together, and divide by a * b * c
to give the polarization.
I'm still new to using Python, and so I am quite unsure on even where to start with this! I hope I have explained well enough, but if not I can clarify where needed.
Side note:
I also then need to change some of these fractional coordinates and repeat the calculation. I am fairly certain I know how to do this, here's how it would look.
for x in displacement
fracCoord[0]=fracCoord[0]+x
So I would change the relevant values in the fractional coordinates list by constant amount before repeating the calculations.
答案 0 :(得分:0)
You could use the numpy
package, it is designed for such (and more complex) numerical computations based on matrices and arrays.
Then, you have:
import numpy as np
# Assume you have N atoms.
# N x 3 matrix where each row is the coordinates of an atom
CoordsMat = np.array(Coords)
# N x 1 vector where each value is charge of atom
ChargesVec = np.array(Charges)
# 3 x 1 vector, the weights for each coordinate
dims = np.array([a, b, c])
# N x 1 vector, computes dot product for each atom's coordinates with the weights
positionVecs = np.matmul(CoordsMat, dims)
# N x 1 vector, scales each dot product by the charges vector
dipoleMoments = np.multiply(positionVecs, ChargesVec)