File: eulerTest.cc

package info (click to toggle)
clhep 2.1.4.1%2Bdfsg-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,012 kB
  • sloc: cpp: 50,094; sh: 6,694; makefile: 2,694; perl: 28
file content (209 lines) | stat: -rwxr-xr-x 7,208 bytes parent folder | download | duplicates (5)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// eulerTest.cc

// Test extreme cases for Euler angles --
// We perturb the matrix slightly before taking Euler angles.
// A test will have failed if the Rotation resulting from taking euler angles 
// and forming a rotation from them, is ever significantly different than the
// origninal rotation.


#include "CLHEP/Vector/Rotation.h"
#include "CLHEP/Vector/EulerAngles.h"
#include "CLHEP/Units/PhysicalConstants.h"

#include <iostream>
#include <math.h>

using std::cout;
using namespace CLHEP;

const int Nperturbs = 24;

void perturb ( int i, const HepRotation & r, HepRotation & rp, double & del );
bool compareR ( const HepRotation & r1, const HepRotation & r2, double tol );

bool test (double phi, double theta, double psi) {
  HepRotation r (phi, theta, psi);
  HepRotation rp;
  HepRotation rpe;
  HepEulerAngles e;
  bool retval = true;
  double del;
  cout << "\n\n -------------------------------- \n\n";
  for ( int i = 0; i < Nperturbs; ++i ) {
    perturb ( i, r, rp, del );
    e = rp.eulerAngles();
    cout << "(" << phi <<", " << theta << ", " << psi << ") --> "<< e << "\n";
    cout << "       e.phi() = "  << e.phi() 
         << "    rp.phi() = " << rp.phi()  
	 << " difference is " << (e.phi() - rp.phi())/del << " * del\n";    
    if ( std::fabs (e.phi() - rp.phi()) > 200*del ) {
      cout << "phi discrepancy in "  << rp << "\n";
      cout << "       e.phi() = "  << e.phi() 
           << "    rp.phi() = " << rp.phi()  
	   << " difference is " << e.phi() - rp.phi() << "\n";
      if (del < 1.0e-4) cout << "??????????\n";
      retval = false;
    }
    if ( std::fabs (e.theta() - rp.theta()) > 200*del ) {
      cout << "theta discrepancy in "  << rp << "\n";
      cout << "       e.theta() = "  << e.theta() 
           << "    rp.theta() = " << rp.theta()
	   << " difference is " << e.theta() - rp.theta() << "\n";
      if (del < 1.0e-4) cout << "??????????\n";
      retval = false;
    }
    cout << "       e.psi() = "  << e.psi() 
         << "    rp.psi() = " << rp.psi() 
	 << " difference is " << (e.psi() - rp.psi())/del << " * del\n";
     if ( std::fabs (e.psi() - rp.psi()) > 200*del ) {
      cout << "psi discrepancy in "  << rp << "\n";
      cout << "       e.psi() = "  << e.psi() 
           << "    rp.psi() = " << rp.psi() 
	   << " difference is " << e.psi() - rp.psi() << "\n";
      if (del < 1.0e-4) cout << "??????????\n";
      retval = false;
    }
    rpe.set(e);
    retval &= compareR (rpe, rp, del);
  }
  return retval;
}

int main () {

  bool res = true;
  double PI = CLHEP::pi;

  // Some cases not in the potentially unstable region:
  res &= test (  .05,	       PI/5,   .1	);    
  res &= test (  .4,	       PI/7,   -.35	);    
  res &= test (  PI/3,         PI/6,   -PI/3	);    
  res &= test (  PI/5,         PI/2,   -PI/2.5  );    
  res &= test ( -PI/5,         PI/2,   PI/3	);    
  res &= test ( -4*PI/5,       PI/2,   PI/2.5	);    
  res &= test (  4*PI/5,       PI/2,   2*PI/3	);    
  res &= test ( -3*PI/4,       PI/2,   -PI/3	);    
  res &= test (  5*PI/6,       PI/2,   -4*PI/5	);    
  res &= test (  5*PI/6,       PI/2,   -PI/3	);    

  // Specialized cases
  res &= test (  .05,	       0,      .1       );    
  res &= test (  .2,	       0,      1.1      );    
  res &= test (  -.4,	       0,      .4       );    
  res &= test (  -2.4,	       0,     2.0       );    
  res &= test (  -2.4,	       0,     -2.0      );    
  res &= test (  -2.2,	       0,     1.8       );    
  res &= test (  -2.2,	       0,     -1.8      );    
  res &= test (  .05,	       PI,     .1       );    
  res &= test (  .2,	       PI,     1.1      );    
  res &= test (  -.4,	       PI,     .4       );    
  res &= test (  -2.4,	       PI,     2.0      );    
  res &= test (  -2.4,	       PI,     -2.0     );    
  res &= test (  -2.2,	       PI,     1.8      );    
  res &= test (  -2.2,	       PI,     -1.8     );    
  
  // Cases near zero
  res &= test (  .1,	.0000000004,	.5	 );    
  res &= test (  -1.2,  .0000000004,	.5	 );    
  res &= test (  .7,	.0000000004,	-.6	 );    
  res &= test (  1.5,	.0000000004,	-1.1	 );    
  res &= test (  1.4,	.0000000004,	-1.5	 );    
  res &= test (  -.1,	.0000000000028, .5	 );    
  res &= test (  -1.2,  .0000000000028, -.5	 );    
  res &= test (  .7,	.0000000000028, -.6	 );    
  res &= test (  -1.5,  .0000000000028, -1.1	 );    
  res &= test (  1.4,	.0000000000028, 1.5	 );    

  // Cases near PI
  double nearPI = PI - .00000002; 
  res &= test (  .1,	nearPI, 	 .5    );    
  res &= test (  -1.2,  nearPI, 	 .5    );    
  res &= test (  .7,	nearPI, 	-.6    );    
  res &= test (  1.5,	nearPI, 	-1.1   );    
  res &= test (  1.4,	nearPI, 	-1.5   );    
  res &= test (  2.4,	nearPI, 	-1.6   );    
  res &= test (  2.3,	nearPI, 	 1.9   );    
  res &= test ( -2.8,	nearPI, 	  .6   );    
  res &= test (  -.4,	nearPI, 	-3.1   );    
  nearPI = PI - .000000000009;   
  res &= test (   .1,	 nearPI,	 -.5	);    
  res &= test (   1.2,   nearPI,	 .5	);    
  res &= test (   .7,	 nearPI,	 -.6	);    
  res &= test (   1.5,   nearPI,	 1.1	);    
  res &= test (   -1.4,  nearPI,	 -1.5	);    
  res &= test (  2.1,	nearPI, 	-1.2    );    
  res &= test (  2.9,	nearPI, 	  .9    );    
  res &= test ( -2.8,	nearPI, 	 1.6    );    
  res &= test (  -.4,	nearPI, 	-3.0    );    
  
  if (!res) return -1;
  return 0;
}

bool compareR ( const HepRotation & r1, const HepRotation & r2, double tol ) {
  HepRep3x3 m1 = r1.rep3x3();
  HepRep3x3 m2 = r2.rep3x3();
  double flaw = 0;
  flaw = max (flaw, (m1.xx_ - m2.xx_));
  flaw = max (flaw, (m1.xy_ - m2.xy_));
  flaw = max (flaw, (m1.xz_ - m2.xz_));
  flaw = max (flaw, (m1.yx_ - m2.yx_));
  flaw = max (flaw, (m1.yy_ - m2.yy_));
  flaw = max (flaw, (m1.yz_ - m2.yz_));
  flaw = max (flaw, (m1.zx_ - m2.zx_));
  flaw = max (flaw, (m1.zy_ - m2.zy_));
  flaw = max (flaw, (m1.zz_ - m2.zz_));
  if (flaw > 20*std::sqrt(tol)) {
    cout << "???????? comparison flaw at level of " <<  flaw  << "\n"
         << r1 << r2;
  }
  cout << "flaw size is " << flaw << " (" << flaw/std::sqrt(tol) << ")\n\n";
  return (flaw <= tol);
}

void perturb ( int i, const HepRotation & r, HepRotation & rp, double & del ) {
  
  HepRep3x3 p0 (  1,  3, -2,
        	 -1, -2,  4, 
	          2, -1, -1  );

  HepRep3x3 p1 (  1, -1, -2,
                  1,  3, -1, 
	          2, -1, -3  );

  HepRep3x3 p2 (  5,  1, -5,
        	 -3, -2,  3, 
		 -1,  4, -1  );

  HepRep3x3 p3 ( -2, -2, 1,
        	 -1, -2, -4, 
	          4,  2, -2  );

  HepRep3x3 p[4];
  p[0] = p0;
  p[1] = p1;
  p[2] = p2;
  p[3] = p3;

  int cycle = i/4;
  double q;
  switch (cycle){
    case 0: q = 1.0e-14; break;
    case 1: q = 1.0e-12; break;
    case 2: q = 1.0e-10; break;
    case 3: q = 1.0e-8;  break;
    case 4: q = 1.0e-6;  break;
    case 5: q = 1.0e-4;  break;    
  }
  HepRep3x3 d = p[i%4];
  HepRep3x3 m = r.rep3x3();
  if ((m.zz_ + q*d.zz_) < -1) {
    d.zz_ = -d.zz_;
  }
  cout << "i = " << i << " q is " << q << "\n";
  rp.set (HepRep3x3 ( m.xx_ + q*d.xx_ , m.xy_ + q*d.xy_ , m.xz_ + q*d.xz_ ,
                      m.yx_ + q*d.yx_ , m.yy_ + q*d.yy_ , m.yz_ + q*d.yz_ ,
		      m.zx_ + q*d.zx_ , m.zy_ + q*d.zy_ , m.zz_ + q*d.zz_ ) );
  del = q;
}