ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/DirectionalAtom.cpp
(Generate patch)

Comparing trunk/OOPSE/libmdtools/DirectionalAtom.cpp (file contents):
Revision 878 by gezelter, Fri Dec 12 15:42:13 2003 UTC vs.
Revision 1136 by tim, Tue Apr 27 16:26:44 2004 UTC

# Line 1 | Line 1
1   #include <math.h>
2  
3   #include "Atom.hpp"
4 + #include "DirectionalAtom.hpp"
5   #include "simError.h"
6 + #include "MatVec3.h"
7  
6
7
8   void DirectionalAtom::zeroForces() {
9    if( hasCoords ){
10 <    frc[offsetX] = 0.0;
11 <    frc[offsetY] = 0.0;
12 <    frc[offsetZ] = 0.0;
10 >
11 >    Atom::zeroForces();
12      
13      trq[offsetX] = 0.0;
14      trq[offsetY] = 0.0;
# Line 36 | Line 35 | void DirectionalAtom::setCoords(void){
35                       &trq,
36                       &Amat,
37                       &mu,  
38 <                     &ul );
38 >                     &ul,
39 >                 &rc,
40 >                     &massRatio);
41    }
42    else{
43      sprintf( painCave.errMsg,
# Line 48 | Line 49 | void DirectionalAtom::setCoords(void){
49  
50    hasCoords = true;
51  
51  *mu = myMu;
52
52   }
53  
55 double DirectionalAtom::getMu( void ) {
56
57  if( hasCoords ){
58    return *mu;
59  }
60  else{
61    return myMu;
62  }
63 }
64
65 void DirectionalAtom::setMu( double the_mu ) {
66
67  if( hasCoords ){
68    *mu = the_mu;
69    myMu = the_mu;
70  }
71  else{
72    myMu = the_mu;
73  }
74 }
75
54   void DirectionalAtom::setA( double the_A[3][3] ){
55  
56    if( hasCoords ){
# Line 92 | Line 70 | void DirectionalAtom::setI( double the_I[3][3] ){
70    }
71   }
72  
73 < void DirectionalAtom::setI( double the_I[3][3] ){
73 > void DirectionalAtom::setI( double the_I[3][3] ){  
74    
75    Ixx = the_I[0][0]; Ixy = the_I[0][1]; Ixz = the_I[0][2];
76    Iyx = the_I[1][0]; Iyy = the_I[1][1]; Iyz = the_I[1][2];
# Line 182 | Line 160 | void DirectionalAtom::getU( double the_u[3] ){
160  
161   void DirectionalAtom::getU( double the_u[3] ){
162    
163 <  the_u[0] = sux;
164 <  the_u[1] = suy;
165 <  the_u[2] = suz;
166 <
163 >  the_u[0] = sU[2][0];
164 >  the_u[1] = sU[2][1];
165 >  the_u[2] = sU[2][2];
166 >  
167    this->body2Lab( the_u );
168   }
169  
# Line 246 | Line 224 | void DirectionalAtom::getQ( double q[4] ){
224      simError();
225    }
226   }
227 +
228 + void DirectionalAtom::setUnitFrameFromEuler(double phi,
229 +                                            double theta,
230 +                                            double psi) {
231 +
232 +  double myA[3][3];
233 +  double uFrame[3][3];
234 +  double len;
235 +  int i, j;
236 +  
237 +  myA[0][0] = (cos(phi) * cos(psi)) - (sin(phi) * cos(theta) * sin(psi));
238 +  myA[0][1] = (sin(phi) * cos(psi)) + (cos(phi) * cos(theta) * sin(psi));
239 +  myA[0][2] = sin(theta) * sin(psi);
240 +  
241 +  myA[1][0] = -(cos(phi) * sin(psi)) - (sin(phi) * cos(theta) * cos(psi));
242 +  myA[1][1] = -(sin(phi) * sin(psi)) + (cos(phi) * cos(theta) * cos(psi));
243 +  myA[1][2] = sin(theta) * cos(psi);
244 +  
245 +  myA[2][0] = sin(phi) * sin(theta);
246 +  myA[2][1] = -cos(phi) * sin(theta);
247 +  myA[2][2] = cos(theta);
248 +  
249 +  // Make the unit Frame:
250 +
251 +  for (i=0; i < 3; i++)
252 +    for (j=0; j < 3; j++)
253 +      uFrame[i][j] = 0.0;
254  
255 +  for (i=0; i < 3; i++)
256 +    uFrame[i][i] = 1.0;
257  
258 +  // rotate by the given rotation matrix:
259 +
260 +  matMul3(myA, uFrame, sU);
261 +
262 +  // renormalize column vectors:
263 +
264 +  for (i=0; i < 3; i++) {
265 +    len = 0.0;
266 +    for (j = 0; j < 3; j++) {
267 +      len += sU[i][j]*sU[i][j];
268 +    }
269 +    len = sqrt(len);
270 +    for (j = 0; j < 3; j++) {
271 +      sU[i][j] /= len;    
272 +    }
273 +  }
274 +  
275 +  // sU now contains the coordinates of the 'special' frame;
276 +    
277 + }
278 +
279   void DirectionalAtom::setEuler( double phi, double theta, double psi ){
280    
281    if( hasCoords ){
# Line 300 | Line 328 | void DirectionalAtom::body2Lab( double r[3] ){
328  
329   }
330  
331 + void DirectionalAtom::rotateBy( double by_A[3][3]) {
332 +
333 +  // Check this
334 +  
335 +  double r00, r01, r02, r10, r11, r12, r20, r21, r22;
336 +
337 +  if( hasCoords ){
338 +
339 +    r00 = by_A[0][0]*Amat[Axx] + by_A[0][1]*Amat[Ayx] + by_A[0][2]*Amat[Azx];
340 +    r01 = by_A[0][0]*Amat[Axy] + by_A[0][1]*Amat[Ayy] + by_A[0][2]*Amat[Azy];
341 +    r02 = by_A[0][0]*Amat[Axz] + by_A[0][1]*Amat[Ayz] + by_A[0][2]*Amat[Azz];
342 +    
343 +    r10 = by_A[1][0]*Amat[Axx] + by_A[1][1]*Amat[Ayx] + by_A[1][2]*Amat[Azx];
344 +    r11 = by_A[1][0]*Amat[Axy] + by_A[1][1]*Amat[Ayy] + by_A[1][2]*Amat[Azy];
345 +    r12 = by_A[1][0]*Amat[Axz] + by_A[1][1]*Amat[Ayz] + by_A[1][2]*Amat[Azz];
346 +    
347 +    r20 = by_A[2][0]*Amat[Axx] + by_A[2][1]*Amat[Ayx] + by_A[2][2]*Amat[Azx];
348 +    r21 = by_A[2][0]*Amat[Axy] + by_A[2][1]*Amat[Ayy] + by_A[2][2]*Amat[Azy];
349 +    r22 = by_A[2][0]*Amat[Axz] + by_A[2][1]*Amat[Ayz] + by_A[2][2]*Amat[Azz];
350 +    
351 +    Amat[Axx] = r00; Amat[Axy] = r01; Amat[Axz] = r02;
352 +    Amat[Ayx] = r10; Amat[Ayy] = r11; Amat[Ayz] = r12;
353 +    Amat[Azx] = r20; Amat[Azy] = r21; Amat[Azz] = r22;
354 +
355 +  }
356 +  else{
357 +    
358 +    sprintf( painCave.errMsg,
359 +             "Attempt to rotate frame for atom %d before coords set.\n",
360 +             index );
361 +    painCave.isFatal = 1;
362 +    simError();
363 +  }
364 +
365 + }
366 +
367 +
368   void DirectionalAtom::body2Lab( double r[3] ){
369  
370    double rb[3]; // the body frame vector
# Line 326 | Line 391 | void DirectionalAtom::updateU( void ){
391   void DirectionalAtom::updateU( void ){
392  
393    if( hasCoords ){
394 <    ul[offsetX] = (Amat[Axx] * sux) + (Amat[Ayx] * suy) + (Amat[Azx] * suz);
395 <    ul[offsetY] = (Amat[Axy] * sux) + (Amat[Ayy] * suy) + (Amat[Azy] * suz);
396 <    ul[offsetZ] = (Amat[Axz] * sux) + (Amat[Ayz] * suy) + (Amat[Azz] * suz);
394 >    ul[offsetX] = (Amat[Axx] * sU[2][0]) +
395 >      (Amat[Ayx] * sU[2][1]) + (Amat[Azx] * sU[2][2]);
396 >    ul[offsetY] = (Amat[Axy] * sU[2][0]) +
397 >      (Amat[Ayy] * sU[2][1]) + (Amat[Azy] * sU[2][2]);
398 >    ul[offsetZ] = (Amat[Axz] * sU[2][0]) +
399 >      (Amat[Ayz] * sU[2][1]) + (Amat[Azz] * sU[2][2]);
400    }
401    else{
402      
# Line 429 | Line 497 | void DirectionalAtom::getGrad( double grad[6] ) {
497    ephi[0] = 0.0;
498    ephi[1] = 0.0;
499    ephi[2] = 1.0;
500 <  etheta[0] = -sphi;
501 <  etheta[1] = cphi;
500 >
501 >  etheta[0] = cphi;
502 >  etheta[1] = sphi;
503    etheta[2] = 0.0;
435  epsi[0] = ctheta * cphi;
436  epsi[1] = ctheta * sphi;
437  epsi[2] = -stheta;
504    
505 +  epsi[0] = stheta * cphi;
506 +  epsi[1] = stheta * sphi;
507 +  epsi[2] = ctheta;
508 +  
509    for (int j = 0 ; j<3; j++)
510      grad[j] = frc[j];
511  
512 +  grad[3] = 0;
513 +  grad[4] = 0;
514 +  grad[5] = 0;
515 +
516    for (int j = 0; j < 3; j++ ) {
517      
518      grad[3] += trq[j]*ephi[j];
# Line 449 | Line 523 | void DirectionalAtom::getGrad( double grad[6] ) {
523  
524   }
525  
526 <
526 > /**
527 >  * getEulerAngles computes a set of Euler angle values consistent
528 >  *  with an input rotation matrix.  They are returned in the following
529 >  * order:
530 >  *  myEuler[0] = phi;
531 >  *  myEuler[1] = theta;
532 >  *  myEuler[2] = psi;
533 > */
534   void DirectionalAtom::getEulerAngles(double myEuler[3]) {
535  
536 <  // getEulerAngles computes a set of Euler angle values consistent
537 <  // with an input rotation matrix.  They are returned in the following
538 <  // order:
539 <  //  myEuler[0] = phi;
540 <  //  myEuler[1] = theta;
460 <  //  myEuler[2] = psi;
536 >  // We use so-called "x-convention", which is the most common definition.
537 >  // In this convention, the rotation given by Euler angles (phi, theta, psi), where the first
538 >  // rotation is by an angle phi about the z-axis, the second is by an angle  
539 >  // theta (0 <= theta <= 180)about the x-axis, and thethird is by an angle psi about the
540 >  //z-axis (again).
541    
542 +  
543    double phi,theta,psi,eps;
544    double pi;
545    double cphi,ctheta,cpsi;
# Line 469 | Line 550 | void DirectionalAtom::getEulerAngles(double myEuler[3]
550    // set the tolerance for Euler angles and rotation elements
551    
552    eps = 1.0e-8;
472    
473  // get a trial value of theta from a single rotation element
474  
475  theta = asin(min(1.0,max(-1.0,-Amat[Axz])));
476  ctheta = cos(theta);
477  stheta = -Amat[Axz];
478  
479  // set the phi/psi difference when theta is either 90 or -90
480  
481  if (fabs(ctheta) <= eps) {
482    phi = 0.0;
483    if (fabs(Amat[Azx]) < eps) {
484      psi = asin(min(1.0,max(-1.0,-Amat[Ayx]/Amat[Axz])));
485    } else {
486      if (fabs(Amat[Ayx]) < eps) {
487        psi = acos(min(1.0,max(-1.0,-Amat[Azx]/Amat[Axz])));
488      } else {
489        psi = atan(Amat[Ayx]/Amat[Azx]);
490      }    
491    }
492  }
553  
554 <  // set the phi and psi values for all other theta values
554 >  theta = acos(min(1.0,max(-1.0,Amat[Azz])));
555 >  ctheta = Amat[Azz];
556 >  stheta = sqrt(1.0 - ctheta * ctheta);
557 >
558 >  // when sin(theta) is close to 0, we need to consider singularity
559 >  // In this case, we can assign an arbitary value to phi (or psi), and then determine
560 >  // the psi (or phi) or vice-versa. We'll assume that phi always gets the rotation, and psi is 0
561 >  // in cases of singularity.  
562 >  // we use atan2 instead of atan, since atan2 will give us -Pi to Pi.
563 >  // Since 0 <= theta <= 180, sin(theta) will be always non-negative. Therefore, it never
564 >  // change the sign of both of the parameters passed to atan2.
565    
566 <  else {
567 <    if (fabs(Amat[Axx]) < eps) {
568 <      phi = asin(min(1.0,max(-1.0,Amat[Axy]/ctheta)));
499 <    } else {
500 <      if (fabs(Amat[Axy]) < eps) {
501 <        phi = acos(min(1.0,max(-1.0,Amat[Axx]/ctheta)));
502 <      } else {
503 <        phi = atan(Amat[Axy]/Amat[Axx]);
504 <      }
505 <    }
506 <    if (fabs(Amat[Azz]) < eps) {
507 <      psi = asin(min(1.0,max(-1.0,Amat[Ayz]/ctheta)));
508 <    } else {
509 <      if (fabs(Amat[Ayz]) < eps) {
510 <        psi = acos(min(1.0,max(-1.0,Amat[Azz]/ctheta)));
511 <      }
512 <      psi = atan(Amat[Ayz]/Amat[Azz]);
513 <    }
566 >  if (fabs(stheta) <= eps){
567 >    psi = 0.0;
568 >    phi = atan2(-Amat[Ayx], Amat[Axx]);  
569    }
570 <
571 <  // find sine and cosine of the trial phi and psi values
572 <
573 <  cphi = cos(phi);
519 <  sphi = sin(phi);
520 <  cpsi = cos(psi);
521 <  spsi = sin(psi);
522 <
523 <  // reconstruct the diagonal of the rotation matrix
524 <
525 <  b[0] = ctheta * cphi;
526 <  b[1] = spsi*stheta*sphi + cpsi*cphi;
527 <  b[2] = ctheta * cpsi;
528 <
529 <  // compare the correct matrix diagonal to rebuilt diagonal
530 <
531 <  for (int i = 0; i < 3; i++) {
532 <    flip[i] = 0;
533 <    if (fabs(Amat[3*i + i] - b[i]) > eps)  flip[i] = 1;
570 >  // we only have one unique solution
571 >  else{    
572 >      phi = atan2(Amat[Azx], -Amat[Azy]);
573 >      psi = atan2(Amat[Axz], Amat[Ayz]);
574    }
575  
576 <  // alter Euler angles to get correct rotation matrix values
577 <  
578 <  if (flip[0] && flip[1]) phi = phi - copysign(M_PI,phi);
539 <  if (flip[0] && flip[2]) theta = -theta + copysign(M_PI, theta);
540 <  if (flip[1] && flip[2]) psi = psi - copysign(M_PI, psi);
576 >  //wrap phi and psi, make sure they are in the range from 0 to 2*Pi
577 >  //if (phi < 0)
578 >  //  phi += M_PI;
579  
580 +  //if (psi < 0)
581 +  //  psi += M_PI;
582 +
583    myEuler[0] = phi;
584    myEuler[1] = theta;
585    myEuler[2] = psi;
586 <
586 >  
587    return;
588   }
589  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines