Firstly in this section we will need to import some mathematical functions from the
numpy module to allow us to efficiently work with arrays of numbers. Note especially that
the sqrt and exp functions have the same names as the equivalents in the math module, but
Before we align structural coordinates by optimising rotations, we first need to move
the input structures so that their centres are aligned. Effectively this means calculating the
centre of mass of each structure and moving its coordinates so that it is repositioned with
the centre at the zero points of all the axes (x, y and z). The centre needs to be the zero
point because the rotational transformations we will consider are all rotations about this
centre. The function that moves an array of coordinates naturally takes the array as an
input argument, together with an array of weights. Note that both arguments must be
NumPy array objects, which allows us to perform operations on whole arrays of numbers
at once, rather than using loops; this is quicker to run and arguably easier to read (once
you understand NumPy).
The weights array allows different kinds of atom to have different degrees of influence,
typically derived from the atom’s mass. Inside the function we first multiply the elements
of the coordinates array by the weights array, taking special notice that we use the
transpose of coords array where rows are switched with columns. This transposition is
required because element-by-element multiplication of a one-dimensional array (weights)
with a two-dimensional array (coords) works on a per-row basis. If we switch rows with
columns then we get three rows that correspond to all of the x, y and z values respectively,
and what we require is to multiply all of these rows separately with the weights. Given the
weighted coordinates (wCoords) we then find the summation of the x, y, and z coordinates
independently, i.e. we add up along the rows (specified with sum(axis=1)), the result of
which is an array of three numbers. This array of totals is then divided by the total weight
to get an average that represents the centre of the coordinates (center). Finally, all the
coordinates are moved to the new centre (0, 0, 0) by taking away the old centre position;
another example of a per-row numpy.array operation. The new coordinates and the old
centre position (which represents a translation operation) are passed back from the
function.
def centerCoords(coords, weights):
wCoords = coords.transpose() * weights
xyzTotals = wCoords.sum(axis=1)
center = xyzTotals/sum(weights)
coords -= center
return coords, center
Do'stlaringiz bilan baham: