File: rot_tools.py

package info (click to toggle)
python-ase 3.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,192 kB
  • ctags: 8,112
  • sloc: python: 93,375; sh: 99; makefile: 94
file content (48 lines) | stat: -rw-r--r-- 1,617 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Gives the rotation matrix which rotates theta degrees about
# vecU

#  Generates the rotation matrix that rotate theta degrees about the vecU
def rotate_about_vec(vecU, theta):
    import numpy as np
    vecU = np.array(vecU)
    vecU = vecU / (sum(vecU ** 2) ** 0.5)
    ux, uy, uz = vecU
    st = np.sin(theta)
    ct = np.cos(theta)
    mat = np.array([[ux ** 2 + ct * (1 - ux ** 2), 
                     ux * uy * (1 - ct) - uz * st, 
                     uz * ux * (1 - ct) + uy * st],
                    [ux * uy * (1 - ct) + uz * st, 
                     uy ** 2 + ct * (1 - uy ** 2),
                     uy * uz * (1 - ct) - ux * st],
                    [uz * ux * (1 - ct) - uy * st,
                     uy * uz * (1 - ct) + ux * st,
                     uz ** 2 + ct * (1 - uz **2)]])
    return (mat)

# Generates the rotation matrix which rotates aVec into intoVec
def rotate_vec_into_newvec(aVec, intoVec):
    def length(v):
        return((sum(v ** 2)) ** 0.5)

    import numpy as np
    from math import acos
    fac = 1.0
    aVec = np.array(aVec)
    intoVec = np.array(intoVec)
    nor = np.cross(aVec, intoVec)
    if length(nor) == 0:
        nor = np.array([1, 0, 0])
    nor = nor / length(nor)
    theta = acos(np.dot(aVec, intoVec) / (length(aVec) * length(intoVec)))
    if np.dot(aVec, intoVec) < 0:
        theta = theta + np.pi
        fac = -1
    return(fac * rotate_about_vec(nor, theta))

# Applies the rotation matrix to the vector and returns the rotated vector
def rotate_vec (rot_mat, vec):
    import numpy as np
    rot_vec = np.dot(rot_mat, vec)

    return (rot_vec)