source: trunk/source/global/HEPNumerics/test/testG4AnalyticalPolSolver.cc@ 1340

Last change on this file since 1340 was 1315, checked in by garnier, 15 years ago

update geant4-09-04-beta-cand-01 interfaces-V09-03-09 vis-V09-03-08

File size: 7.5 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27// $Id: testG4AnalyticalPolSolver.cc,v 1.7 2006/06/29 19:00:33 gunter Exp $
28// GEANT4 tag $Name: geant4-09-04-beta-cand-01 $
29//
30// Test program for G4AnalyticalPolSolver class.
31//
32
33#include "G4ios.hh"
34#include "globals.hh"
35#include "Randomize.hh"
36#include "G4AnalyticalPolSolver.hh"
37#include "geomdefs.hh"
38
39// #include "ApproxEqual.hh"
40
41const G4double kApproxEqualTolerance = 1E-6;
42// const G4double kApproxEqualTolerance = 1E-2;
43
44// Return true if the double check is approximately equal to target
45//
46// Process:
47//
48// Return true is check if less than kApproxEqualTolerance from target
49
50G4bool ApproxEqual(const G4double check,const G4double target)
51{
52 G4bool result;
53 G4double mean, delta;
54 mean = 0.5*std::fabs(check + target);
55 delta = std::fabs(check - target);
56
57 if(mean > 1.) delta /= mean;
58
59 if(delta<kApproxEqualTolerance) result = true;
60 else result = false;
61 return result;
62}
63
64
65int main()
66{
67 G4int i, k, n, iRoot, iMax = 10000;
68 G4int iCheck = iMax/10;
69 G4double p[5], r[3][5];
70 G4double a, b, c, d, tmp, range = 10*mm;
71 G4AnalyticalPolSolver solver;
72 enum Eroot {k2, k3, k4};
73 Eroot useCase = k4;
74
75 G4cout.precision(20);
76
77
78 a = 14.511252641677856;
79 b = 14.7648024559021;
80 c = 14.82865571975708;
81 d = 14.437621831893921;
82
83
84 p[0] = 1.;
85 p[1] = -a - b - c - d;
86 p[2] = (a+b)*(c+d) + a*b + c*d;
87 p[3] = -(a+b)*c*d - a*b*(c+d);
88 p[4] = a*b*c*d;
89
90 // iRoot = solver.BiquadRoots(p,r);
91 iRoot = solver.QuarticRoots(p,r);
92
93 for( k = 1; k <= 4; k++ )
94 {
95 tmp = r[1][k];
96
97 if ( ApproxEqual(tmp,a) || ApproxEqual(tmp,b) ||
98 ApproxEqual(tmp,c) || ApproxEqual(tmp,d) ) continue;
99 else
100 {
101 G4cout<<"k = "<<k<<G4endl;
102 G4cout<<"a = "<<a<<G4endl;
103 G4cout<<"b = "<<b<<G4endl;
104 G4cout<<"c = "<<c<<G4endl;
105 G4cout<<"d = "<<d<<G4endl;
106 G4cout<<"root = "<< r[1][k] << " " << r[2][k] <<" i" << G4endl<<G4endl;
107 }
108 }
109
110
111 for ( n = 2; n <= 4; n++ )
112 {
113 // Various test cases
114
115 if( n == 4 ) // roots: 1,2,3,4
116 {
117 p[0] = 1.;
118 p[1] = -10.;
119 p[2] = 35.;
120 p[3] = -50.;
121 p[4] = 24.;
122
123 p[0] = 1.;
124 p[1] = 0.;
125 p[2] = 4.;
126 p[3] = 0.;
127 p[4] = 4.; // roots: +-i sqrt(2)
128 }
129 if( n == 3 ) // roots: 1,2,3
130 {
131 p[0] = 1.;
132 p[1] = -6.;
133 p[2] = 11.;
134 p[3] = -6.;
135 }
136 if(n==2) // roots : 1 +- i
137 {
138 p[0] = 1.;
139 p[1] = -2.;
140 p[2] = 2.;
141 }
142
143 if( n == 2 )
144 {
145 // G4cout<<"Test QuadRoots(p,r):"<<G4endl;
146 i = solver.QuadRoots(p,r);
147 }
148 else if( n == 3 )
149 {
150 // G4cout<<"Test CUBICROOTS(p,r):"<<G4endl;
151 i = solver.CubicRoots(p,r);
152 }
153 else if( n == 4 )
154 {
155 // G4cout<<"Test BIQUADROOTS(p,r):"<<G4endl;
156 i = solver.BiquadRoots(p,r);
157 }
158
159 for( k = 1; k <= n; k++ )
160 {
161 // G4cout << r[1][k] << " " << r[2][k] <<" i" << G4endl;
162 }
163 }
164
165 G4cout << G4endl << G4endl;
166
167 // Random test of quadratic, cubic, and biquadratic equations
168
169 switch (useCase)
170 {
171
172 case k4:
173 G4cout<<"Testing biquadratic:"<<G4endl<<G4endl;
174
175 for( i = 0; i < iMax; i++ )
176 {
177 if(i%iCheck == 0) G4cout<<"i = "<<i<<G4endl<<G4endl;
178
179 a = -range + 2*range*G4UniformRand();
180 b = -range + 2*range*G4UniformRand();
181 c = -range + 2*range*G4UniformRand();
182 d = -range + 2*range*G4UniformRand();
183 p[0] = 1.;
184 p[1] = -a - b - c - d;
185 // p[2] = a*b + a*c + a*d + b*c + b*d + c*d;
186 p[2] = (a+b)*(c+d) + a*b + c*d;
187 // p[3] = -a*b*c - b*c*d - a*b*d - a*c*d;
188 p[3] = -(a+b)*c*d - a*b*(c+d);
189 p[4] = a*b*c*d;
190
191 // iRoot = solver.BiquadRoots(p,r);
192 iRoot = solver.QuarticRoots(p,r);
193 for( k = 1; k <= 4; k++ )
194 {
195 tmp = r[1][k];
196
197 if ( ApproxEqual(tmp,a) || ApproxEqual(tmp,b) ||
198 ApproxEqual(tmp,c) || ApproxEqual(tmp,d) ) continue;
199 else
200 {
201 G4cout<<"i = "<<i<<"; k = "<<k<<G4endl;
202 G4cout<<"a = "<<a<<G4endl;
203 G4cout<<"b = "<<b<<G4endl;
204 G4cout<<"c = "<<c<<G4endl;
205 G4cout<<"d = "<<d<<G4endl;
206 G4cout<<"root = "<< r[1][k] << " " << r[2][k] <<" i"
207 << G4endl << G4endl;
208 }
209 }
210 }
211 break;
212
213 case k3:
214
215 G4cout<<"Testing cubic:"<<G4endl<<G4endl;
216
217 for( i = 0; i < iMax; i++ )
218 {
219 if(i%iCheck == 0) G4cout<<"i = "<<i<<G4endl;
220
221 a = -range + 2*range*G4UniformRand();
222 b = -range + 2*range*G4UniformRand();
223 c = -range + 2*range*G4UniformRand();
224
225 p[0] = 1.;
226 p[1] = -a - b - c;
227 p[2] = (a+b)*c + a*b;
228 p[3] = -a*b*c;
229
230 iRoot = solver.CubicRoots(p,r);
231
232 for( k = 1; k <= 3; k++ )
233 {
234 tmp = r[1][k];
235
236 if ( ApproxEqual(tmp,a) || ApproxEqual(tmp,b) ||
237 ApproxEqual(tmp,c) ) continue;
238 else
239 {
240 G4cout<<"i = "<<i<<G4endl;
241 G4cout<<"k = "<<k<<G4endl;
242 G4cout<<"a = "<<a<<G4endl;
243 G4cout<<"b = "<<b<<G4endl;
244 G4cout<<"c = "<<c<<G4endl;
245 G4cout <<"root = "<< r[1][k] << " " << r[2][k] <<" i" << G4endl;
246 }
247 }
248 }
249 break;
250 case k2:
251
252 G4cout<<"Testing quadratic:"<<G4endl<<G4endl;
253
254 for( i = 0; i < iMax; i++ )
255 {
256 if(i%iCheck == 0) G4cout<<"i = "<<i<<G4endl;
257
258 a = -range + 2*range*G4UniformRand();
259 b = -range + 2*range*G4UniformRand();
260
261 p[0] = 1.;
262 p[1] = -a - b ;
263 p[2] = a*b;
264
265 iRoot = solver.QuadRoots(p,r);
266
267 for( k = 1; k <= 2; k++ )
268 {
269 tmp = r[1][k];
270
271 if ( ApproxEqual(tmp,a) || ApproxEqual(tmp,b) ) continue;
272 else
273 {
274 G4cout<<"i = "<<i<<G4endl;
275 G4cout<<"k = "<<k<<G4endl;
276 G4cout<<"a = "<<a<<G4endl;
277 G4cout<<"b = "<<b<<G4endl;
278 G4cout <<"root = "<< r[1][k] << " " << r[2][k] <<" i" << G4endl;
279 }
280 }
281 }
282 break;
283
284 default:
285 break;
286 }
287 return 0;
288}
Note: See TracBrowser for help on using the repository browser.