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 599 by mmeineke, Mon Jul 14 21:48:43 2003 UTC vs.
Revision 788 by mmeineke, Thu Sep 25 21:17:16 2003 UTC

# Line 1 | Line 1
1   #include <cmath>
2  
3   #include "Atom.hpp"
4 + #include "simError.h"
5  
6 + void DirectionalAtom::zeroForces() {
7 +  if( hasCoords ){
8 +    frc[offsetX] = 0.0;
9 +    frc[offsetY] = 0.0;
10 +    frc[offsetZ] = 0.0;
11 +    
12 +    trq[offsetX] = 0.0;
13 +    trq[offsetY] = 0.0;
14 +    trq[offsetZ] = 0.0;
15 +  }
16 +  else{
17 +    
18 +    sprintf( painCave.errMsg,
19 +             "Attempt to zero frc and trq for atom %d before coords set.\n",
20 +             index );
21 +    painCave.isFatal = 1;
22 +    simError();
23 +  }
24 + }
25  
26 + void DirectionalAtom::setCoords(void){
27  
28 +  if( myConfig->isAllocated() ){
29 +
30 +    myConfig->getAtomPointers( index,
31 +                     &pos,
32 +                     &vel,
33 +                     &frc,
34 +                     &trq,
35 +                     &Amat,
36 +                     &mu,  
37 +                     &ul );
38 +  }
39 +  else{
40 +    sprintf( painCave.errMsg,
41 +             "Attempted to set Atom %d  coordinates with an unallocated "
42 +             "SimState object.\n", index );
43 +    painCave.isFatal = 1;
44 +    simError();
45 +  }
46 +
47 +  hasCoords = true;
48 +
49 +  *mu = myMu;
50 +
51 + }
52 +
53 + double DirectionalAtom::getMu( void ) {
54 +
55 +  if( hasCoords ){
56 +    return *mu;
57 +  }
58 +  else{
59 +    return myMu;
60 +  }
61 + }
62 +
63 + void DirectionalAtom::setMu( double the_mu ) {
64 +
65 +  if( hasCoords ){
66 +    *mu = the_mu;
67 +    myMu = the_mu;
68 +  }
69 +  else{
70 +    myMu = the_mu;
71 +  }
72 + }
73 +
74   void DirectionalAtom::setA( double the_A[3][3] ){
8  
9  Amat[Axx] = the_A[0][0]; Amat[Axy] = the_A[0][1]; Amat[Axz] = the_A[0][2];
10  Amat[Ayx] = the_A[1][0]; Amat[Ayy] = the_A[1][1]; Amat[Ayz] = the_A[1][2];
11  Amat[Azx] = the_A[2][0]; Amat[Azy] = the_A[2][1]; Amat[Azz] = the_A[2][2];
75  
76 <  this->updateU();  
76 >  if( hasCoords ){
77 >    Amat[Axx] = the_A[0][0]; Amat[Axy] = the_A[0][1]; Amat[Axz] = the_A[0][2];
78 >    Amat[Ayx] = the_A[1][0]; Amat[Ayy] = the_A[1][1]; Amat[Ayz] = the_A[1][2];
79 >    Amat[Azx] = the_A[2][0]; Amat[Azy] = the_A[2][1]; Amat[Azz] = the_A[2][2];
80 >    
81 >    this->updateU();  
82 >  }
83 >  else{
84 >    
85 >    sprintf( painCave.errMsg,
86 >             "Attempt to set Amat for atom %d before coords set.\n",
87 >             index );
88 >    painCave.isFatal = 1;
89 >    simError();
90 >  }
91   }
92  
93   void DirectionalAtom::setI( double the_I[3][3] ){
# Line 24 | Line 101 | void DirectionalAtom::setQ( double the_q[4] ){
101  
102    double q0Sqr, q1Sqr, q2Sqr, q3Sqr;
103  
104 <  q0Sqr = the_q[0] * the_q[0];
105 <  q1Sqr = the_q[1] * the_q[1];
106 <  q2Sqr = the_q[2] * the_q[2];
107 <  q3Sqr = the_q[3] * the_q[3];
108 <  
104 >  if( hasCoords ){
105 >    q0Sqr = the_q[0] * the_q[0];
106 >    q1Sqr = the_q[1] * the_q[1];
107 >    q2Sqr = the_q[2] * the_q[2];
108 >    q3Sqr = the_q[3] * the_q[3];
109 >    
110 >    
111 >    Amat[Axx] = q0Sqr + q1Sqr - q2Sqr - q3Sqr;
112 >    Amat[Axy] = 2.0 * ( the_q[1] * the_q[2] + the_q[0] * the_q[3] );
113 >    Amat[Axz] = 2.0 * ( the_q[1] * the_q[3] - the_q[0] * the_q[2] );
114 >    
115 >    Amat[Ayx] = 2.0 * ( the_q[1] * the_q[2] - the_q[0] * the_q[3] );
116 >    Amat[Ayy] = q0Sqr - q1Sqr + q2Sqr - q3Sqr;
117 >    Amat[Ayz] = 2.0 * ( the_q[2] * the_q[3] + the_q[0] * the_q[1] );
118 >    
119 >    Amat[Azx] = 2.0 * ( the_q[1] * the_q[3] + the_q[0] * the_q[2] );
120 >    Amat[Azy] = 2.0 * ( the_q[2] * the_q[3] - the_q[0] * the_q[1] );
121 >    Amat[Azz] = q0Sqr - q1Sqr -q2Sqr +q3Sqr;
122 >    
123 >    this->updateU();
124 >  }
125 >  else{
126 >    
127 >    sprintf( painCave.errMsg,
128 >             "Attempt to set Q for atom %d before coords set.\n",
129 >             index );
130 >    painCave.isFatal = 1;
131 >    simError();
132 >  }
133  
33  Amat[Axx] = q0Sqr + q1Sqr - q2Sqr - q3Sqr;
34  Amat[Axy] = 2.0 * ( the_q[1] * the_q[2] + the_q[0] * the_q[3] );
35  Amat[Axz] = 2.0 * ( the_q[1] * the_q[3] - the_q[0] * the_q[2] );
36  
37  Amat[Ayx] = 2.0 * ( the_q[1] * the_q[2] - the_q[0] * the_q[3] );
38  Amat[Ayy] = q0Sqr - q1Sqr + q2Sqr - q3Sqr;
39  Amat[Ayz] = 2.0 * ( the_q[2] * the_q[3] + the_q[0] * the_q[1] );
40
41  Amat[Azx] = 2.0 * ( the_q[1] * the_q[3] + the_q[0] * the_q[2] );
42  Amat[Azy] = 2.0 * ( the_q[2] * the_q[3] - the_q[0] * the_q[1] );
43  Amat[Azz] = q0Sqr - q1Sqr -q2Sqr +q3Sqr;
44
45  this->updateU();
134   }
135  
136   void DirectionalAtom::getA( double the_A[3][3] ){
137    
138 <  the_A[0][0] = Amat[Axx];
139 <  the_A[0][1] = Amat[Axy];
140 <  the_A[0][2] = Amat[Axz];
138 >  if( hasCoords ){
139 >    the_A[0][0] = Amat[Axx];
140 >    the_A[0][1] = Amat[Axy];
141 >    the_A[0][2] = Amat[Axz];
142 >    
143 >    the_A[1][0] = Amat[Ayx];
144 >    the_A[1][1] = Amat[Ayy];
145 >    the_A[1][2] = Amat[Ayz];
146 >    
147 >    the_A[2][0] = Amat[Azx];
148 >    the_A[2][1] = Amat[Azy];
149 >    the_A[2][2] = Amat[Azz];
150 >  }
151 >  else{
152 >    
153 >    sprintf( painCave.errMsg,
154 >             "Attempt to get Amat for atom %d before coords set.\n",
155 >             index );
156 >    painCave.isFatal = 1;
157 >    simError();
158 >  }
159  
54  the_A[1][0] = Amat[Ayx];
55  the_A[1][1] = Amat[Ayy];
56  the_A[1][2] = Amat[Ayz];
57
58  the_A[2][0] = Amat[Azx];
59  the_A[2][1] = Amat[Azy];
60  the_A[2][2] = Amat[Azz];
160   }
161  
162   void DirectionalAtom::printAmatIndex( void ){
163  
164 <  std::cerr << "Atom[" << index << "] index =>\n"
165 <            << "[ " << Axx << ", " << Axy << ", " << Axz << " ]\n"
166 <            << "[ " << Ayx << ", " << Ayy << ", " << Ayz << " ]\n"
167 <            << "[ " << Azx << ", " << Azy << ", " << Azz << " ]\n";
164 >  if( hasCoords ){
165 >    std::cerr << "Atom[" << index << "] index =>\n"
166 >              << "[ " << Axx << ", " << Axy << ", " << Axz << " ]\n"
167 >              << "[ " << Ayx << ", " << Ayy << ", " << Ayz << " ]\n"
168 >              << "[ " << Azx << ", " << Azy << ", " << Azz << " ]\n";
169 >  }
170 >  else{
171 >    
172 >    sprintf( painCave.errMsg,
173 >             "Attempt to print Amat indices for atom %d before coords set.\n",
174 >             index );
175 >    painCave.isFatal = 1;
176 >    simError();
177 >  }
178   }
179  
180  
# Line 83 | Line 192 | void DirectionalAtom::getQ( double q[4] ){
192    double t, s;
193    double ad1, ad2, ad3;
194  
195 <  t = Amat[Axx] + Amat[Ayy] + Amat[Azz] + 1.0;
87 <  if( t > 0.0 ){
195 >  if( hasCoords ){
196      
197 <    s = 0.5 / sqrt( t );
198 <    q[0] = 0.25 / s;
91 <    q[1] = (Amat[Ayz] - Amat[Azy]) * s;
92 <    q[2] = (Amat[Azx] - Amat[Axz]) * s;
93 <    q[3] = (Amat[Axy] - Amat[Ayx]) * s;
94 <  }
95 <  else{
96 <    
97 <    ad1 = fabs( Amat[Axx] );
98 <    ad2 = fabs( Amat[Ayy] );
99 <    ad3 = fabs( Amat[Azz] );
100 <
101 <    if( ad1 >= ad2 && ad1 >= ad3 ){
197 >    t = Amat[Axx] + Amat[Ayy] + Amat[Azz] + 1.0;
198 >    if( t > 0.0 ){
199        
200 <      s = 2.0 * sqrt( 1.0 + Amat[Axx] - Amat[Ayy] - Amat[Azz] );
201 <      q[0] = (Amat[Ayz] + Amat[Azy]) / s;
202 <      q[1] = 0.5 / s;
203 <      q[2] = (Amat[Axy] + Amat[Ayx]) / s;
204 <      q[3] = (Amat[Axz] + Amat[Azx]) / s;
200 >      s = 0.5 / sqrt( t );
201 >      q[0] = 0.25 / s;
202 >      q[1] = (Amat[Ayz] - Amat[Azy]) * s;
203 >      q[2] = (Amat[Azx] - Amat[Axz]) * s;
204 >      q[3] = (Amat[Axy] - Amat[Ayx]) * s;
205      }
109    else if( ad2 >= ad1 && ad2 >= ad3 ){
110
111      s = sqrt( 1.0 + Amat[Ayy] - Amat[Axx] - Amat[Azz] ) * 2.0;
112      q[0] = (Amat[Axz] + Amat[Azx]) / s;
113      q[1] = (Amat[Axy] + Amat[Ayx]) / s;
114      q[2] = 0.5 / s;
115      q[3] = (Amat[Ayz] + Amat[Azy]) / s;
116    }
206      else{
207        
208 <      s = sqrt( 1.0 + Amat[Azz] - Amat[Axx] - Amat[Ayy] ) * 2.0;
209 <      q[0] = (Amat[Axy] + Amat[Ayx]) / s;
210 <      q[1] = (Amat[Axz] + Amat[Azx]) / s;
211 <      q[2] = (Amat[Ayz] + Amat[Azy]) / s;
212 <      q[3] = 0.5 / s;
208 >      ad1 = fabs( Amat[Axx] );
209 >      ad2 = fabs( Amat[Ayy] );
210 >      ad3 = fabs( Amat[Azz] );
211 >      
212 >      if( ad1 >= ad2 && ad1 >= ad3 ){
213 >        
214 >        s = 2.0 * sqrt( 1.0 + Amat[Axx] - Amat[Ayy] - Amat[Azz] );
215 >        q[0] = (Amat[Ayz] + Amat[Azy]) / s;
216 >        q[1] = 0.5 / s;
217 >        q[2] = (Amat[Axy] + Amat[Ayx]) / s;
218 >        q[3] = (Amat[Axz] + Amat[Azx]) / s;
219 >      }
220 >      else if( ad2 >= ad1 && ad2 >= ad3 ){
221 >        
222 >        s = sqrt( 1.0 + Amat[Ayy] - Amat[Axx] - Amat[Azz] ) * 2.0;
223 >        q[0] = (Amat[Axz] + Amat[Azx]) / s;
224 >        q[1] = (Amat[Axy] + Amat[Ayx]) / s;
225 >        q[2] = 0.5 / s;
226 >        q[3] = (Amat[Ayz] + Amat[Azy]) / s;
227 >      }
228 >      else{
229 >        
230 >        s = sqrt( 1.0 + Amat[Azz] - Amat[Axx] - Amat[Ayy] ) * 2.0;
231 >        q[0] = (Amat[Axy] + Amat[Ayx]) / s;
232 >        q[1] = (Amat[Axz] + Amat[Azx]) / s;
233 >        q[2] = (Amat[Ayz] + Amat[Azy]) / s;
234 >        q[3] = 0.5 / s;
235 >      }
236      }
237    }
238 +  else{
239 +    
240 +    sprintf( painCave.errMsg,
241 +             "Attempt to get Q for atom %d before coords set.\n",
242 +             index );
243 +    painCave.isFatal = 1;
244 +    simError();
245 +  }
246   }
247  
248  
249   void DirectionalAtom::setEuler( double phi, double theta, double psi ){
250    
251 <  Amat[Axx] = (cos(phi) * cos(psi)) - (sin(phi) * cos(theta) * sin(psi));
252 <  Amat[Axy] = (sin(phi) * cos(psi)) + (cos(phi) * cos(theta) * sin(psi));
253 <  Amat[Axz] = sin(theta) * sin(psi);
254 <  
255 <  Amat[Ayx] = -(cos(phi) * sin(psi)) - (sin(phi) * cos(theta) * cos(psi));
256 <  Amat[Ayy] = -(sin(phi) * sin(psi)) + (cos(phi) * cos(theta) * cos(psi));
257 <  Amat[Ayz] = sin(theta) * cos(psi);
258 <
259 <  Amat[Azx] = sin(phi) * sin(theta);
260 <  Amat[Azy] = -cos(phi) * sin(theta);
261 <  Amat[Azz] = cos(theta);
262 <
263 <  this->updateU();
251 >  if( hasCoords ){
252 >    Amat[Axx] = (cos(phi) * cos(psi)) - (sin(phi) * cos(theta) * sin(psi));
253 >    Amat[Axy] = (sin(phi) * cos(psi)) + (cos(phi) * cos(theta) * sin(psi));
254 >    Amat[Axz] = sin(theta) * sin(psi);
255 >    
256 >    Amat[Ayx] = -(cos(phi) * sin(psi)) - (sin(phi) * cos(theta) * cos(psi));
257 >    Amat[Ayy] = -(sin(phi) * sin(psi)) + (cos(phi) * cos(theta) * cos(psi));
258 >    Amat[Ayz] = sin(theta) * cos(psi);
259 >    
260 >    Amat[Azx] = sin(phi) * sin(theta);
261 >    Amat[Azy] = -cos(phi) * sin(theta);
262 >    Amat[Azz] = cos(theta);
263 >    
264 >    this->updateU();
265 >  }
266 >  else{
267 >    
268 >    sprintf( painCave.errMsg,
269 >             "Attempt to set Euler angles for atom %d before coords set.\n",
270 >             index );
271 >    painCave.isFatal = 1;
272 >    simError();
273 >  }
274   }
275  
276  
# Line 148 | Line 278 | void DirectionalAtom::lab2Body( double r[3] ){
278  
279    double rl[3]; // the lab frame vector
280    
281 <  rl[0] = r[0];
282 <  rl[1] = r[1];
283 <  rl[2] = r[2];
281 >  if( hasCoords ){
282 >    rl[0] = r[0];
283 >    rl[1] = r[1];
284 >    rl[2] = r[2];
285 >    
286 >    r[0] = (Amat[Axx] * rl[0]) + (Amat[Axy] * rl[1]) + (Amat[Axz] * rl[2]);
287 >    r[1] = (Amat[Ayx] * rl[0]) + (Amat[Ayy] * rl[1]) + (Amat[Ayz] * rl[2]);
288 >    r[2] = (Amat[Azx] * rl[0]) + (Amat[Azy] * rl[1]) + (Amat[Azz] * rl[2]);
289 >  }
290 >  else{
291 >    
292 >    sprintf( painCave.errMsg,
293 >             "Attempt to convert lab2body for atom %d before coords set.\n",
294 >             index );
295 >    painCave.isFatal = 1;
296 >    simError();
297 >  }
298  
155  r[0] = (Amat[Axx] * rl[0]) + (Amat[Axy] * rl[1]) + (Amat[Axz] * rl[2]);
156  r[1] = (Amat[Ayx] * rl[0]) + (Amat[Ayy] * rl[1]) + (Amat[Ayz] * rl[2]);
157  r[2] = (Amat[Azx] * rl[0]) + (Amat[Azy] * rl[1]) + (Amat[Azz] * rl[2]);
299   }
300  
301   void DirectionalAtom::body2Lab( double r[3] ){
302  
303    double rb[3]; // the body frame vector
304    
305 <  rb[0] = r[0];
306 <  rb[1] = r[1];
307 <  rb[2] = r[2];
308 <
309 <  r[0] = (Amat[Axx] * rb[0]) + (Amat[Ayx] * rb[1]) + (Amat[Azx] * rb[2]);
310 <  r[1] = (Amat[Axy] * rb[0]) + (Amat[Ayy] * rb[1]) + (Amat[Azy] * rb[2]);
311 <  r[2] = (Amat[Axz] * rb[0]) + (Amat[Ayz] * rb[1]) + (Amat[Azz] * rb[2]);
305 >  if( hasCoords ){
306 >    rb[0] = r[0];
307 >    rb[1] = r[1];
308 >    rb[2] = r[2];
309 >    
310 >    r[0] = (Amat[Axx] * rb[0]) + (Amat[Ayx] * rb[1]) + (Amat[Azx] * rb[2]);
311 >    r[1] = (Amat[Axy] * rb[0]) + (Amat[Ayy] * rb[1]) + (Amat[Azy] * rb[2]);
312 >    r[2] = (Amat[Axz] * rb[0]) + (Amat[Ayz] * rb[1]) + (Amat[Azz] * rb[2]);
313 >  }
314 >  else{
315 >    
316 >    sprintf( painCave.errMsg,
317 >             "Attempt to convert body2lab for atom %d before coords set.\n",
318 >             index );
319 >    painCave.isFatal = 1;
320 >    simError();
321 >  }
322   }
323  
324   void DirectionalAtom::updateU( void ){
325  
326 <  ul[offsetX] = (Amat[Axx] * sux) + (Amat[Ayx] * suy) + (Amat[Azx] * suz);
327 <  ul[offsetY] = (Amat[Axy] * sux) + (Amat[Ayy] * suy) + (Amat[Azy] * suz);
328 <  ul[offsetZ] = (Amat[Axz] * sux) + (Amat[Ayz] * suy) + (Amat[Azz] * suz);
326 >  if( hasCoords ){
327 >    ul[offsetX] = (Amat[Axx] * sux) + (Amat[Ayx] * suy) + (Amat[Azx] * suz);
328 >    ul[offsetY] = (Amat[Axy] * sux) + (Amat[Ayy] * suy) + (Amat[Azy] * suz);
329 >    ul[offsetZ] = (Amat[Axz] * sux) + (Amat[Ayz] * suy) + (Amat[Azz] * suz);
330 >  }
331 >  else{
332 >    
333 >    sprintf( painCave.errMsg,
334 >             "Attempt to updateU for atom %d before coords set.\n",
335 >             index );
336 >    painCave.isFatal = 1;
337 >    simError();
338 >  }
339   }
340  
341   void DirectionalAtom::getJ( double theJ[3] ){
# Line 193 | Line 354 | void DirectionalAtom::getTrq( double theT[3] ){
354  
355   void DirectionalAtom::getTrq( double theT[3] ){
356    
357 <  theT[0] = trq[offsetX];
358 <  theT[1] = trq[offsetY];
359 <  theT[2] = trq[offsetZ];
357 >  if( hasCoords ){
358 >    theT[0] = trq[offsetX];
359 >    theT[1] = trq[offsetY];
360 >    theT[2] = trq[offsetZ];
361 >  }
362 >  else{
363 >    
364 >    sprintf( painCave.errMsg,
365 >             "Attempt to get Trq for atom %d before coords set.\n",
366 >             index );
367 >    painCave.isFatal = 1;
368 >    simError();
369 >  }
370   }
371  
372   void DirectionalAtom::addTrq( double theT[3] ){
373    
374 <  trq[offsetX] += theT[0];
375 <  trq[offsetY] += theT[1];
376 <  trq[offsetZ] += theT[2];
374 >  if( hasCoords ){
375 >    trq[offsetX] += theT[0];
376 >    trq[offsetY] += theT[1];
377 >    trq[offsetZ] += theT[2];
378 >  }
379 >  else{
380 >    
381 >    sprintf( painCave.errMsg,
382 >             "Attempt to add Trq for atom %d before coords set.\n",
383 >             index );
384 >    painCave.isFatal = 1;
385 >    simError();
386 >  }
387   }
388  
389  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines