pwtools.crys.coord_trans

pwtools.crys.coord_trans(coords, old=None, new=None, copy=True, axis=-1)[source]

General-purpose n-dimensional coordinate transformation. coords can have arbitrary dimension, i.e. it can contain many vectors to be transformed at once. But old and new must have ndim=2, i.e. only one old and new coord sys for all vectors in coords.

The most general case is that you want to transform an MD trajectory from a variable cell run, you have smth like this:

coords.shape = (nstep,natoms,3)
old.shape/new.shape = (nstep,3,3)

You have a set of old and new coordinate systems at each step. Then, use a loop over all time steps and call this function nstep times. See also coord_trans3d().

Parameters:
  • coords (array (d0, d1, ..., M)) –

    Array of arbitrary rank with coordinates (length M vectors) in old coord sys old. The only shape resiriction is that the last dim must equal the number of coordinates (coords.shape[-1] == M == 3 for normal 3-dim x,y,z).

    1d : trivial, transform that vector (length M)
    2d : The matrix must have shape (N,M), i.e. N vectors to be
    transformed are the rows.
    3d : coords must have shape (…, M)

    If coords has a different shape, use axis to define the M-axis.

  • old (2d arrays (M,M)) – Matrices with the old and new basis vectors as rows. Note that in the usual math literature, columns are used. In that case, use old.T and/or new.T.

  • new (2d arrays (M,M)) – Matrices with the old and new basis vectors as rows. Note that in the usual math literature, columns are used. In that case, use old.T and/or new.T.

  • copy (bool, optional) – True: overwrite coords False: return new array

  • axis (the axis along which the length-M vectors are placed in coords,) – default is -1, i.e. coords.shape = (…,M)

Return type:

array of shape = coords.shape, coordinates in system new

Examples

>>> # Taken from [1]_.
>>> import numpy as np
>>> import math
>>> v_I = np.array([1.0,1.5])
>>> I = np.identity(2)
>>> X = math.sqrt(2)/2.0*np.array([[1,-1],[1,1]]).T
>>> Y = np.array([[1,1],[0,1]]).T
>>> coord_trans(v_I,I,I)
array([ 1. ,  1.5])
>>> v_X = coord_trans(v,I,X)
>>> v_Y = coord_trans(v,I,Y)
>>> v_X
array([ 1.76776695,  0.35355339])
>>> v_Y
array([-0.5,  1.5])
>>> coord_trans(v_Y,Y,I)
array([ 1. ,  1.5])
>>> coord_trans(v_X,X,I)
array([ 1. ,  1.5])
>>> # 3d example
>>> c_old = np.random.rand(30,200,3)
>>> old = np.random.rand(3,3)
>>> new = np.random.rand(3,3)
>>> c_new = coord_trans(c_old, old=old, new=new)
>>> c_old2 = coord_trans(c_new, old=new, new=old)
>>> np.testing.assert_almost_equal(c_old, c_old2)
>>> # If you have an array of shape, say (10,3,100), i.e. the last
>>> # dimension is NOT 3, then use numpy.swapaxes() or axis:
>>> coord_trans(arr, old=..., new=..., axis=1)
>>> coord_trans(arr.swapaxes(1,2), old=..., new=...).swapaxes(1,2)

References

See also

coord_trans3d