Skip to content

Instantly share code, notes, and snippets.

@exergonic
Created June 20, 2014 13:53
Show Gist options
  • Save exergonic/cea8aef4fe264774aa14 to your computer and use it in GitHub Desktop.
Save exergonic/cea8aef4fe264774aa14 to your computer and use it in GitHub Desktop.
c Copyright (c) 2014 by Billy Wayne McCann
c All rights reserved.
c
c Redistribution and use in source and binary forms, with or without
c modification, are permitted provided that the following conditions
c are met:
c 1. Redistributions of source code must retain the above copyright
c notice, this list of conditions and the following disclaimer.
c 2. Redistributions in binary form must reproduce the above copyright
c notice, this list of conditions and the following disclaimer in the
c documentation and/or other materials provided with the distribution.
c
c THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
c IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
c OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
c IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
c INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
c NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
c DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
c THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
c (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
c THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
c
c-----------------------------------------------------------------------
c-----------------------------------------------------------------------
c
c This subroutine processes a terminal bond by taking the atom that
c the terminal atom (jj) is bonded to (ii) and moving it to the
c origin. The terminal atom (jj) is then aligned with the positive
c (idir = 1) or negative (idir = -1) z axis.
c
c passed variables:
c
c integer n number of points in the cartesian coordinate arrays
c real x, y, z input cartesian coordinate arrays for molecule
c real xo,yo,zo output cartesian coordinate arrays for molecule
c integer ii atom to be set at the origin
c integer jj terminal atom
c integer idir +1 = positive z-axis, -1 = negative z-axis
c
c-----------------------------------------------------------------------
c-----------------------------------------------------------------------
subroutine alignzpass(n, x, y, z, ii, jj, idir, xo, yo, zo)
implicit none
real x(n), y(n), z(n)
real xt(n),yt(n),zt(n)
real xo(n),yo(n),zo(n)
integer i, ii, jj, n, idir
real xi, yi, zi
real init_vector(3), jj_vector(3)
real z_axis(3)
real rotaxis(3)
real rx, ry, rz
real Q1(4), Q2(4), Q2CONJ(4), Q3(4), Q4(4)
real magq,magv,phi,PI
c
c Initialize
c
jj_vector = 0.0
init_vector = 0.0
rotaxis = 0.0
rx = 0.0
ry = 0.0
rz = 0.0
Q1 = 0.0
Q2 = 0.0
Q2CONJ = 0.0
Q3 = 0.0
Q4 = 0.0
magq = 0.0
magv = 0.0
PI = acos(-1.0e0)
c
c idir determines whether to align to the positive or negative z
c axis
c
z_axis = [ 0.0, 0.0, idir*1.0e0 ]
c
c Remember x(ii), as it will be the origin in the output xo,yo,zo:
c
xi = x(ii)
yi = y(ii)
zi = z(ii)
c
c Move ii (bonder atom) to origin, translating all atoms with it.
c
do i=1, n
xt(i) = x(i) - xi
yt(i) = y(i) - yi
zt(i) = z(i) - zi
end do
c
c Find the angle between jj and the z-axis, as well as the axis of
c rotation.
c
jj_vector = [ xt(jj), yt(jj), zt(jj) ]
magv = sqrt(dot_product(jj_vector, jj_vector))
& * sqrt(dot_product(z_axis, z_axis))
phi = acos(dot_product(jj_vector, z_axis)/magv)
rotaxis = [ z_axis(2)*jj_vector(3) - z_axis(3)*jj_vector(2),
& z_axis(3)*jj_vector(1) - z_axis(1)*jj_vector(3),
& z_axis(1)*jj_vector(2) - z_axis(2)*jj_vector(1)
& ]
magv = sqrt(dot_product(rotaxis, rotaxis))
rotaxis = rotaxis / magv !normalize
c
c Assign x,y,z components of rotation axis their own variables
c
rx = rotaxis(1)
ry = rotaxis(2)
rz = rotaxis(3)
c
c Rotate the cartesion coordinates phi degrees about an arbitrary
c axis using a quaternion.
c
c 1. Let Q1 be the initial quaternion containing the initial vector
c (the cartesian coordinates of a specific atom).
c
c 2. The rotation quaternion is :
c
c Q2 = ( cos(phi/2), rx*sin(phi/2),
c ry*sin(phi/2)), rz*sin(phi/2) )
c
c 3. The rotated vector is then the last three components of the
c quaternion multiplication
c
c Q2 x Q1 x Q2*
c
c where Q2* is the complex conjugate of Q2, herein labeled
c Q2CONJ.
c
c To solve this, the quaternion multiplication is broken down into
c two steps:
c
c 1. Q1 x Q2CONJ ==> Q3
c 2. Q2 x Q3 ==> Q4
c
c The new cartesian coordinates are then
c
c Q4(2:4) ==> x, y, z
c
Q2(1) = cos(phi/2.)
Q2(2) = rx*sin(phi/2.)
Q2(3) = ry*sin(phi/2.)
Q2(4) = rz*sin(phi/2.)
Q2CONJ = [ Q2(1), -1.0e0*Q2(2), -1.0e0*Q2(3), -1.0e0*Q2(4) ]
magq = sqrt( Q2(1)**2 + Q2(2)**2 + Q2(3)**2 + Q2(4)**2 )
Q2 = Q2 / magq !normalize
do i=1, n
Q1 = [0.0, xt(i), yt(i), zt(i) ]
Q3(1) = Q1(1)*Q2CONJ(1) - Q1(2)*Q2CONJ(2)
& - Q1(3)*Q2CONJ(3) - Q1(4)*Q2CONJ(4)
Q3(2) = Q1(1)*Q2CONJ(2) + Q1(2)*Q2CONJ(1)
& - Q1(3)*Q2CONJ(4) + Q1(4)*Q2CONJ(3)
Q3(3) = Q1(1)*Q2CONJ(3) + Q1(2)*Q2CONJ(4)
& + Q1(3)*Q2CONJ(1) - Q1(4)*Q2CONJ(2)
Q3(4) = Q1(1)*Q2CONJ(4) - Q1(2)*Q2CONJ(3)
& + Q1(3)*Q2CONJ(2) + Q1(4)*Q2CONJ(1)
Q4(1) = Q2(1)*Q3(1) - Q2(2)*Q3(2) - Q2(3)*Q3(3) - Q2(4)*Q3(4)
Q4(2) = Q2(1)*Q3(2) + Q2(2)*Q3(1) - Q2(3)*Q3(4) + Q2(4)*Q3(3)
Q4(3) = Q2(1)*Q3(3) + Q2(2)*Q3(4) + Q2(3)*Q3(1) - Q2(4)*Q3(2)
Q4(4) = Q2(1)*Q3(4) - Q2(2)*Q3(3) + Q2(3)*Q3(2) + Q2(4)*Q3(1)
xo(i) = Q4(2)
yo(i) = Q4(3)
zo(i) = Q4(4)
end do
end subroutine alignzpass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment