File: eulerparameters.cpp

package info (click to toggle)
lammps 20220106.git7586adbb6a%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 348,064 kB
  • sloc: cpp: 831,421; python: 24,896; xml: 14,949; f90: 10,845; ansic: 7,967; sh: 4,226; perl: 4,064; fortran: 2,424; makefile: 1,501; objc: 238; lisp: 163; csh: 16; awk: 14; tcl: 6
file content (163 lines) | stat: -rw-r--r-- 6,612 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 *_________________________________________________________________________*
 *      POEMS: PARALLELIZABLE OPEN SOURCE EFFICIENT MULTIBODY SOFTWARE     *
 *      DESCRIPTION: SEE READ-ME                                           *
 *      FILE NAME: eulerparameters.cpp                                     *
 *      AUTHORS: See Author List                                           *
 *      GRANTS: See Grants List                                            *
 *      COPYRIGHT: (C) 2005 by Authors as listed in Author's List          *
 *      LICENSE: Please see License Agreement                              *
 *      DOWNLOAD: Free at www.rpi.edu/~anderk5                             *
 *      ADMINISTRATOR: Prof. Kurt Anderson                                 *
 *                     Computational Dynamics Lab                          *
 *                     Rensselaer Polytechnic Institute                    *
 *                     110 8th St. Troy NY 12180                           *
 *      CONTACT:        anderk5@rpi.edu                                    *
 *_________________________________________________________________________*/

#include "eulerparameters.h"
#include <cmath>

using namespace std;

void EP_Derivatives(ColMatrix& q, ColMatrix& u, ColMatrix& qdot){
  EP_Normalize(q);
  int num=u.GetNumRows();
  if (3<num){
    for (int i=4; i<=num; i++){
      qdot.elements[i]=u.elements[i-1];
    }
  }

  qdot.elements[0] = 0.5 *(q.elements[3]*u.elements[0] - q.elements[2]*u.elements[1] + q.elements[1]*u.elements[2]);
  qdot.elements[1] = 0.5 *(q.elements[2]*u.elements[0] + q.elements[3]*u.elements[1] - q.elements[0]*u.elements[2]);
  qdot.elements[2] = 0.5 *(-q.elements[1]*u.elements[0] + q.elements[0]*u.elements[1] + q.elements[3]*u.elements[2]);
  qdot.elements[3] = -0.5 *(q.elements[0]*u.elements[0] + q.elements[1]*u.elements[1] + q.elements[2]*u.elements[2]);

  }

void EP_Transformation(ColMatrix& q, Mat3x3& C){
  EP_Normalize(q);

  double q11 = q.elements[0]*q.elements[0];
  double q22 = q.elements[1]*q.elements[1];
  double q33 = q.elements[2]*q.elements[2];
  double q44 = q.elements[3]*q.elements[3];

  double q12 = q.elements[0]*q.elements[1];
  double q13 = q.elements[0]*q.elements[2];
  double q14 = q.elements[0]*q.elements[3];
  double q23 = q.elements[1]*q.elements[2];
  double q24 = q.elements[1]*q.elements[3];
  double q34 = q.elements[2]*q.elements[3];

  C.elements[0][0] = q11 - q22 - q33 + q44;
  C.elements[1][1] = -q11 + q22 - q33 + q44;
  C.elements[2][2] = -q11 - q22 + q33 + q44;

  C.elements[0][1] = 2*(q12 - q34);
  C.elements[1][0] = 2*(q12 + q34);

  C.elements[0][2] = 2*(q13 + q24);
  C.elements[2][0] = 2*(q13 - q24);

  C.elements[1][2] = 2*(q23 - q14);
  C.elements[2][1] = 2*(q23 + q14);
}

void EP_FromTransformation(ColMatrix& q, Mat3x3& C){
  double b[4];

  // condition indicators
  b[0] = C.elements[0][0] - C.elements[1][1] - C.elements[2][2] + 1;
  b[1] = -C.elements[0][0] + C.elements[1][1] - C.elements[2][2] + 1;
  b[2] = -C.elements[0][0] - C.elements[1][1] + C.elements[2][2] + 1;
  b[3] = C.elements[0][0] + C.elements[1][1] + C.elements[2][2] + 1;

  int max = 0;
  for(int i=1;i<4;i++){
    if( b[i] > b[max] ) max = i;
  }

  if( max == 3 ){
    q.elements[3] = 0.5 * sqrt( b[3] );
    q.elements[0] = ( C.elements[2][1] - C.elements[1][2] ) / ( 4 * q.elements[3] );
    q.elements[1] = ( C.elements[0][2] - C.elements[2][0] ) / ( 4 * q.elements[3] );
    q.elements[2] = ( C.elements[1][0] - C.elements[0][1] ) / ( 4 * q.elements[3] );
    return;
  }

  if( max == 0 ){
    q.elements[0] = 0.5 * sqrt( b[0] );
    q.elements[1] = ( C.elements[0][1] + C.elements[1][0] ) / ( 4 * q.elements[0] );
    q.elements[2] = ( C.elements[0][2] + C.elements[2][0] ) / ( 4 * q.elements[0] );
    q.elements[3] = ( C.elements[2][1] - C.elements[1][2] ) / ( 4 * q.elements[0] );
    return;
  }

  if( max == 1 ){
    q.elements[1] = 0.5 * sqrt( b[1] );
    q.elements[0] = ( C.elements[0][1] + C.elements[1][0] ) / ( 4 * q.elements[1] );
    q.elements[2] = ( C.elements[1][2] + C.elements[2][1] ) / ( 4 * q.elements[1] );
    q.elements[3] = ( C.elements[0][2] - C.elements[2][0] ) / ( 4 * q.elements[1] );
    return;
  }

  if( max == 2 ){
    q.elements[2] = 0.5 * sqrt( b[2] );
    q.elements[0] = ( C.elements[0][2] + C.elements[2][0] ) / ( 4 * q.elements[2] );
    q.elements[1] = ( C.elements[1][2] + C.elements[2][1] ) / ( 4 * q.elements[2] );
    q.elements[3] = ( C.elements[1][0] - C.elements[0][1] ) / ( 4 * q.elements[2] );
    return;
  }
  EP_Normalize(q);
}

void EP_Normalize(ColMatrix& q){
  double one = 1.0/sqrt(q.elements[0]*q.elements[0] + q.elements[1]*q.elements[1] + q.elements[2]*q.elements[2] + q.elements[3]*q.elements[3]);
  q.elements[0] = one*q.elements[0];
  q.elements[1] = one*q.elements[1];
  q.elements[2] = one*q.elements[2];
  q.elements[3] = one*q.elements[3];
}




void EPdotdot_udot(ColMatrix& Audot, ColMatrix& Aqdot, ColMatrix& Aq, ColMatrix& Aqddot){
  int num=Audot.GetNumRows();
  if (3<num){
    for (int i=4; i<=num; i++){
      Aqddot.elements[i]=Audot.elements[i-1];
    }
  }

  double AA;
  AA=Aqdot.elements[0]*Aqdot.elements[0]+Aqdot.elements[1]*Aqdot.elements[1]+Aqdot.elements[2]*Aqdot.elements[2]+Aqdot.elements[3]*Aqdot.elements[3];

  Aqddot.elements[0] = 0.5 *(Aq.elements[3]*Audot.elements[0] - Aq.elements[2]*Audot.elements[1] + Aq.elements[1]*Audot.elements[2]-2*Aq.elements[0]*AA);

  Aqddot.elements[1] = 0.5 *(Aq.elements[2]*Audot.elements[0] + Aq.elements[3]*Audot.elements[1] - Aq.elements[0]*Audot.elements[2]-2*Aq.elements[1]*AA);

  Aqddot.elements[2] = 0.5 *(-Aq.elements[1]*Audot.elements[0] + Aq.elements[0]*Audot.elements[1] + Aq.elements[3]*Audot.elements[2]-2*Aq.elements[2]*AA);

  Aqddot.elements[3] = -0.5 *(Aq.elements[0]*Audot.elements[0] + Aq.elements[1]*Audot.elements[1] + Aq.elements[2]*Audot.elements[2]+2*Aq.elements[3]*AA);

}


void qdot_to_u(ColMatrix& q, ColMatrix& u, ColMatrix& qdot){
  EP_Normalize(q);
  int num=qdot.GetNumRows();
        if (4<num){
          for (int i=5; i<=num; i++){
            u.elements[i-2]=qdot.elements[i-1];
          }
        }
        u.elements[0]=2*(q.elements[3]*qdot.elements[0]+q.elements[2]*qdot.elements[1]-q.elements[1]*qdot.elements[2]-q.elements[0]*qdot.elements[3]);
        u.elements[1]=2*(-q.elements[2]*qdot.elements[0]+q.elements[3]*qdot.elements[1]+q.elements[0]*qdot.elements[2]-q.elements[1]*qdot.elements[3]);
        u.elements[2]=2*(q.elements[1]*qdot.elements[0]-q.elements[0]*qdot.elements[1]+q.elements[3]*qdot.elements[2]-q.elements[2]*qdot.elements[3]);

}