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; |
35 |
|
&trq, |
36 |
|
&Amat, |
37 |
|
&mu, |
38 |
< |
&ul ); |
38 |
> |
&ul, |
39 |
> |
&quat); |
40 |
|
} |
41 |
|
else{ |
42 |
|
sprintf( painCave.errMsg, |
48 |
|
|
49 |
|
hasCoords = true; |
50 |
|
|
51 |
– |
*mu = myMu; |
52 |
– |
|
51 |
|
} |
52 |
|
|
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 |
– |
|
53 |
|
void DirectionalAtom::setA( double the_A[3][3] ){ |
54 |
|
|
55 |
|
if( hasCoords ){ |
69 |
|
} |
70 |
|
} |
71 |
|
|
72 |
< |
void DirectionalAtom::setI( double the_I[3][3] ){ |
72 |
> |
void DirectionalAtom::setI( double the_I[3][3] ){ |
73 |
|
|
74 |
|
Ixx = the_I[0][0]; Ixy = the_I[0][1]; Ixz = the_I[0][2]; |
75 |
|
Iyx = the_I[1][0]; Iyy = the_I[1][1]; Iyz = the_I[1][2]; |
159 |
|
|
160 |
|
void DirectionalAtom::getU( double the_u[3] ){ |
161 |
|
|
162 |
< |
the_u[0] = sux; |
163 |
< |
the_u[1] = suy; |
164 |
< |
the_u[2] = suz; |
165 |
< |
|
162 |
> |
the_u[0] = sU[2][0]; |
163 |
> |
the_u[1] = sU[2][1]; |
164 |
> |
the_u[2] = sU[2][2]; |
165 |
> |
|
166 |
|
this->body2Lab( the_u ); |
167 |
|
} |
168 |
|
|
224 |
|
} |
225 |
|
} |
226 |
|
|
227 |
+ |
void DirectionalAtom::setUnitFrameFromEuler(double phi, |
228 |
+ |
double theta, |
229 |
+ |
double psi) { |
230 |
+ |
|
231 |
+ |
double myA[3][3]; |
232 |
+ |
double uFrame[3][3]; |
233 |
+ |
double len; |
234 |
+ |
int i, j; |
235 |
+ |
|
236 |
+ |
myA[0][0] = (cos(phi) * cos(psi)) - (sin(phi) * cos(theta) * sin(psi)); |
237 |
+ |
myA[0][1] = (sin(phi) * cos(psi)) + (cos(phi) * cos(theta) * sin(psi)); |
238 |
+ |
myA[0][2] = sin(theta) * sin(psi); |
239 |
+ |
|
240 |
+ |
myA[1][0] = -(cos(phi) * sin(psi)) - (sin(phi) * cos(theta) * cos(psi)); |
241 |
+ |
myA[1][1] = -(sin(phi) * sin(psi)) + (cos(phi) * cos(theta) * cos(psi)); |
242 |
+ |
myA[1][2] = sin(theta) * cos(psi); |
243 |
+ |
|
244 |
+ |
myA[2][0] = sin(phi) * sin(theta); |
245 |
+ |
myA[2][1] = -cos(phi) * sin(theta); |
246 |
+ |
myA[2][2] = cos(theta); |
247 |
+ |
|
248 |
+ |
// Make the unit Frame: |
249 |
+ |
|
250 |
+ |
for (i=0; i < 3; i++) |
251 |
+ |
for (j=0; j < 3; j++) |
252 |
+ |
uFrame[i][j] = 0.0; |
253 |
+ |
|
254 |
+ |
for (i=0; i < 3; i++) |
255 |
+ |
uFrame[i][i] = 1.0; |
256 |
+ |
|
257 |
+ |
// rotate by the given rotation matrix: |
258 |
+ |
|
259 |
+ |
matMul3(myA, uFrame, sU); |
260 |
+ |
|
261 |
+ |
// renormalize column vectors: |
262 |
+ |
|
263 |
+ |
for (i=0; i < 3; i++) { |
264 |
+ |
len = 0.0; |
265 |
+ |
for (j = 0; j < 3; j++) { |
266 |
+ |
len += sU[i][j]*sU[i][j]; |
267 |
+ |
} |
268 |
+ |
len = sqrt(len); |
269 |
+ |
for (j = 0; j < 3; j++) { |
270 |
+ |
sU[i][j] /= len; |
271 |
+ |
} |
272 |
+ |
} |
273 |
+ |
|
274 |
+ |
// sU now contains the coordinates of the 'special' frame; |
275 |
+ |
|
276 |
+ |
} |
277 |
|
|
278 |
|
void DirectionalAtom::setEuler( double phi, double theta, double psi ){ |
279 |
|
|
327 |
|
|
328 |
|
} |
329 |
|
|
330 |
+ |
void DirectionalAtom::rotateBy( double by_A[3][3]) { |
331 |
+ |
|
332 |
+ |
// Check this |
333 |
+ |
|
334 |
+ |
double r00, r01, r02, r10, r11, r12, r20, r21, r22; |
335 |
+ |
|
336 |
+ |
if( hasCoords ){ |
337 |
+ |
|
338 |
+ |
r00 = by_A[0][0]*Amat[Axx] + by_A[0][1]*Amat[Ayx] + by_A[0][2]*Amat[Azx]; |
339 |
+ |
r01 = by_A[0][0]*Amat[Axy] + by_A[0][1]*Amat[Ayy] + by_A[0][2]*Amat[Azy]; |
340 |
+ |
r02 = by_A[0][0]*Amat[Axz] + by_A[0][1]*Amat[Ayz] + by_A[0][2]*Amat[Azz]; |
341 |
+ |
|
342 |
+ |
r10 = by_A[1][0]*Amat[Axx] + by_A[1][1]*Amat[Ayx] + by_A[1][2]*Amat[Azx]; |
343 |
+ |
r11 = by_A[1][0]*Amat[Axy] + by_A[1][1]*Amat[Ayy] + by_A[1][2]*Amat[Azy]; |
344 |
+ |
r12 = by_A[1][0]*Amat[Axz] + by_A[1][1]*Amat[Ayz] + by_A[1][2]*Amat[Azz]; |
345 |
+ |
|
346 |
+ |
r20 = by_A[2][0]*Amat[Axx] + by_A[2][1]*Amat[Ayx] + by_A[2][2]*Amat[Azx]; |
347 |
+ |
r21 = by_A[2][0]*Amat[Axy] + by_A[2][1]*Amat[Ayy] + by_A[2][2]*Amat[Azy]; |
348 |
+ |
r22 = by_A[2][0]*Amat[Axz] + by_A[2][1]*Amat[Ayz] + by_A[2][2]*Amat[Azz]; |
349 |
+ |
|
350 |
+ |
Amat[Axx] = r00; Amat[Axy] = r01; Amat[Axz] = r02; |
351 |
+ |
Amat[Ayx] = r10; Amat[Ayy] = r11; Amat[Ayz] = r12; |
352 |
+ |
Amat[Azx] = r20; Amat[Azy] = r21; Amat[Azz] = r22; |
353 |
+ |
|
354 |
+ |
} |
355 |
+ |
else{ |
356 |
+ |
|
357 |
+ |
sprintf( painCave.errMsg, |
358 |
+ |
"Attempt to rotate frame for atom %d before coords set.\n", |
359 |
+ |
index ); |
360 |
+ |
painCave.isFatal = 1; |
361 |
+ |
simError(); |
362 |
+ |
} |
363 |
+ |
|
364 |
+ |
} |
365 |
+ |
|
366 |
+ |
|
367 |
|
void DirectionalAtom::body2Lab( double r[3] ){ |
368 |
|
|
369 |
|
double rb[3]; // the body frame vector |
390 |
|
void DirectionalAtom::updateU( void ){ |
391 |
|
|
392 |
|
if( hasCoords ){ |
393 |
< |
ul[offsetX] = (Amat[Axx] * sux) + (Amat[Ayx] * suy) + (Amat[Azx] * suz); |
394 |
< |
ul[offsetY] = (Amat[Axy] * sux) + (Amat[Ayy] * suy) + (Amat[Azy] * suz); |
395 |
< |
ul[offsetZ] = (Amat[Axz] * sux) + (Amat[Ayz] * suy) + (Amat[Azz] * suz); |
393 |
> |
ul[offsetX] = (Amat[Axx] * sU[2][0]) + |
394 |
> |
(Amat[Ayx] * sU[2][1]) + (Amat[Azx] * sU[2][2]); |
395 |
> |
ul[offsetY] = (Amat[Axy] * sU[2][0]) + |
396 |
> |
(Amat[Ayy] * sU[2][1]) + (Amat[Azy] * sU[2][2]); |
397 |
> |
ul[offsetZ] = (Amat[Axz] * sU[2][0]) + |
398 |
> |
(Amat[Ayz] * sU[2][1]) + (Amat[Azz] * sU[2][2]); |
399 |
|
} |
400 |
|
else{ |
401 |
|
|
438 |
|
} |
439 |
|
} |
440 |
|
|
441 |
+ |
void DirectionalAtom::setTrq( double theT[3] ){ |
442 |
+ |
|
443 |
+ |
if( hasCoords ){ |
444 |
+ |
trq[offsetX] = theT[0]; |
445 |
+ |
trq[offsetY] = theT[1]; |
446 |
+ |
trq[offsetZ] = theT[2]; |
447 |
+ |
} |
448 |
+ |
else{ |
449 |
+ |
|
450 |
+ |
sprintf( painCave.errMsg, |
451 |
+ |
"Attempt to add Trq for atom %d before coords set.\n", |
452 |
+ |
index ); |
453 |
+ |
painCave.isFatal = 1; |
454 |
+ |
simError(); |
455 |
+ |
} |
456 |
+ |
} |
457 |
+ |
|
458 |
|
void DirectionalAtom::addTrq( double theT[3] ){ |
459 |
|
|
460 |
|
if( hasCoords ){ |
513 |
|
ephi[0] = 0.0; |
514 |
|
ephi[1] = 0.0; |
515 |
|
ephi[2] = 1.0; |
516 |
< |
etheta[0] = -sphi; |
517 |
< |
etheta[1] = cphi; |
516 |
> |
|
517 |
> |
etheta[0] = cphi; |
518 |
> |
etheta[1] = sphi; |
519 |
|
etheta[2] = 0.0; |
435 |
– |
epsi[0] = ctheta * cphi; |
436 |
– |
epsi[1] = ctheta * sphi; |
437 |
– |
epsi[2] = -stheta; |
520 |
|
|
521 |
+ |
epsi[0] = stheta * cphi; |
522 |
+ |
epsi[1] = stheta * sphi; |
523 |
+ |
epsi[2] = ctheta; |
524 |
+ |
|
525 |
|
for (int j = 0 ; j<3; j++) |
526 |
|
grad[j] = frc[j]; |
527 |
|
|
528 |
+ |
grad[3] = 0; |
529 |
+ |
grad[4] = 0; |
530 |
+ |
grad[5] = 0; |
531 |
+ |
|
532 |
|
for (int j = 0; j < 3; j++ ) { |
533 |
|
|
534 |
|
grad[3] += trq[j]*ephi[j]; |
539 |
|
|
540 |
|
} |
541 |
|
|
542 |
< |
|
542 |
> |
/** |
543 |
> |
* getEulerAngles computes a set of Euler angle values consistent |
544 |
> |
* with an input rotation matrix. They are returned in the following |
545 |
> |
* order: |
546 |
> |
* myEuler[0] = phi; |
547 |
> |
* myEuler[1] = theta; |
548 |
> |
* myEuler[2] = psi; |
549 |
> |
*/ |
550 |
|
void DirectionalAtom::getEulerAngles(double myEuler[3]) { |
551 |
|
|
552 |
< |
// getEulerAngles computes a set of Euler angle values consistent |
553 |
< |
// with an input rotation matrix. They are returned in the following |
554 |
< |
// order: |
555 |
< |
// myEuler[0] = phi; |
556 |
< |
// myEuler[1] = theta; |
460 |
< |
// myEuler[2] = psi; |
552 |
> |
// We use so-called "x-convention", which is the most common definition. |
553 |
> |
// In this convention, the rotation given by Euler angles (phi, theta, psi), where the first |
554 |
> |
// rotation is by an angle phi about the z-axis, the second is by an angle |
555 |
> |
// theta (0 <= theta <= 180)about the x-axis, and thethird is by an angle psi about the |
556 |
> |
//z-axis (again). |
557 |
|
|
558 |
+ |
|
559 |
|
double phi,theta,psi,eps; |
560 |
< |
double pi; |
464 |
< |
double cphi,ctheta,cpsi; |
465 |
< |
double sphi,stheta,spsi; |
466 |
< |
double b[3]; |
467 |
< |
int flip[3]; |
560 |
> |
double ctheta,stheta; |
561 |
|
|
562 |
|
// set the tolerance for Euler angles and rotation elements |
563 |
|
|
564 |
|
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 |
– |
} |
565 |
|
|
566 |
< |
// set the phi and psi values for all other theta values |
566 |
> |
theta = acos(min(1.0,max(-1.0,Amat[Azz]))); |
567 |
> |
ctheta = Amat[Azz]; |
568 |
> |
stheta = sqrt(1.0 - ctheta * ctheta); |
569 |
> |
|
570 |
> |
// when sin(theta) is close to 0, we need to consider singularity |
571 |
> |
// In this case, we can assign an arbitary value to phi (or psi), and then determine |
572 |
> |
// the psi (or phi) or vice-versa. We'll assume that phi always gets the rotation, and psi is 0 |
573 |
> |
// in cases of singularity. |
574 |
> |
// we use atan2 instead of atan, since atan2 will give us -Pi to Pi. |
575 |
> |
// Since 0 <= theta <= 180, sin(theta) will be always non-negative. Therefore, it never |
576 |
> |
// change the sign of both of the parameters passed to atan2. |
577 |
|
|
578 |
< |
else { |
579 |
< |
if (fabs(Amat[Axx]) < eps) { |
580 |
< |
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 |
< |
} |
578 |
> |
if (fabs(stheta) <= eps){ |
579 |
> |
psi = 0.0; |
580 |
> |
phi = atan2(-Amat[Ayx], Amat[Axx]); |
581 |
|
} |
582 |
+ |
// we only have one unique solution |
583 |
+ |
else{ |
584 |
+ |
phi = atan2(Amat[Azx], -Amat[Azy]); |
585 |
+ |
psi = atan2(Amat[Axz], Amat[Ayz]); |
586 |
+ |
} |
587 |
|
|
588 |
< |
// find sine and cosine of the trial phi and psi values |
588 |
> |
//wrap phi and psi, make sure they are in the range from 0 to 2*Pi |
589 |
> |
//if (phi < 0) |
590 |
> |
// phi += M_PI; |
591 |
|
|
592 |
< |
cphi = cos(phi); |
593 |
< |
sphi = sin(phi); |
520 |
< |
cpsi = cos(psi); |
521 |
< |
spsi = sin(psi); |
592 |
> |
//if (psi < 0) |
593 |
> |
// psi += M_PI; |
594 |
|
|
595 |
< |
// reconstruct the diagonal of the rotation matrix |
595 |
> |
myEuler[0] = phi; |
596 |
> |
myEuler[1] = theta; |
597 |
> |
myEuler[2] = psi; |
598 |
> |
|
599 |
> |
return; |
600 |
> |
} |
601 |
|
|
602 |
< |
b[0] = ctheta * cphi; |
603 |
< |
b[1] = spsi*stheta*sphi + cpsi*cphi; |
604 |
< |
b[2] = ctheta * cpsi; |
605 |
< |
|
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; |
602 |
> |
double DirectionalAtom::getZangle( ){ |
603 |
> |
|
604 |
> |
if( hasCoords ){ |
605 |
> |
return zAngle; |
606 |
|
} |
607 |
+ |
else{ |
608 |
+ |
|
609 |
+ |
sprintf( painCave.errMsg, |
610 |
+ |
"Attempt to get zAngle for atom %d before coords set.\n", |
611 |
+ |
index ); |
612 |
+ |
painCave.isFatal = 1; |
613 |
+ |
simError(); |
614 |
+ |
return 0; |
615 |
+ |
} |
616 |
+ |
} |
617 |
|
|
618 |
< |
// alter Euler angles to get correct rotation matrix values |
618 |
> |
void DirectionalAtom::setZangle( double zAng ){ |
619 |
|
|
620 |
< |
if (flip[0] && flip[1]) phi = phi - copysign(M_PI,phi); |
621 |
< |
if (flip[0] && flip[2]) theta = -theta + copysign(M_PI, theta); |
622 |
< |
if (flip[1] && flip[2]) psi = psi - copysign(M_PI, psi); |
620 |
> |
if( hasCoords ){ |
621 |
> |
zAngle = zAng; |
622 |
> |
} |
623 |
> |
else{ |
624 |
> |
|
625 |
> |
sprintf( painCave.errMsg, |
626 |
> |
"Attempt to set zAngle for atom %d before coords set.\n", |
627 |
> |
index ); |
628 |
> |
painCave.isFatal = 1; |
629 |
> |
simError(); |
630 |
> |
} |
631 |
> |
} |
632 |
|
|
633 |
< |
myEuler[0] = phi; |
634 |
< |
myEuler[1] = theta; |
635 |
< |
myEuler[2] = psi; |
636 |
< |
|
637 |
< |
return; |
633 |
> |
void DirectionalAtom::addZangle( double zAng ){ |
634 |
> |
|
635 |
> |
if( hasCoords ){ |
636 |
> |
zAngle += zAng; |
637 |
> |
} |
638 |
> |
else{ |
639 |
> |
|
640 |
> |
sprintf( painCave.errMsg, |
641 |
> |
"Attempt to add zAngle to atom %d before coords set.\n", |
642 |
> |
index ); |
643 |
> |
painCave.isFatal = 1; |
644 |
> |
simError(); |
645 |
> |
} |
646 |
|
} |
647 |
|
|
648 |
|
double DirectionalAtom::max(double x, double y) { |