[819] | 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 | // |
---|
| 28 | // Author: Luciano Pandola (Luciano.Pandola@cern.ch) |
---|
| 29 | // |
---|
| 30 | // History: |
---|
| 31 | // ----------- |
---|
| 32 | // 30 Nov 2002 LP Created |
---|
| 33 | // 14 Feb 2003 MGP Corrected compilation errors from SUN |
---|
| 34 | // |
---|
| 35 | // ------------------------------------------------------------------- |
---|
| 36 | |
---|
| 37 | template <class T,class F> |
---|
| 38 | G4double G4PenelopeIntegrator<T,F>::Calculate(T& typeT, F f, |
---|
| 39 | G4double LowPoint,G4double HighPoint, |
---|
| 40 | G4double MaxError) |
---|
| 41 | { |
---|
| 42 | const G4int npoints=10; |
---|
| 43 | const G4int ncallsmax=20000; |
---|
| 44 | const G4int nst=256; |
---|
| 45 | static G4double Abscissas[10] = {7.652651133497334e-02,2.2778585114164508e-01,3.7370608871541956e-01, |
---|
| 46 | 5.1086700195082710e-01,6.3605368072651503e-01,7.4633190646015079e-01, |
---|
| 47 | 8.3911697182221882e-01,9.1223442825132591e-01,9.6397192727791379e-01, |
---|
| 48 | 9.9312859918509492e-01}; |
---|
| 49 | static G4double Weights[10] = {1.5275338713072585e-01,1.4917298647260375e-01,1.4209610931838205e-01, |
---|
| 50 | 1.3168863844917663e-01,1.1819453196151842e-01,1.0193011981724044e-01, |
---|
| 51 | 8.3276741576704749e-02,6.2672048334109064e-02,4.0601429800386941e-02, |
---|
| 52 | 1.7614007139152118e-02}; |
---|
| 53 | |
---|
| 54 | //Error control |
---|
| 55 | G4double Ctol = std::min(std::max(MaxError,1e-13),1e-02); |
---|
| 56 | G4double Ptol = 0.01*Ctol; |
---|
| 57 | G4double Err=1e35; |
---|
| 58 | //Gauss integration from LowPoint to HighPoint |
---|
| 59 | G4double h,sumga,a,b,c,d; |
---|
| 60 | h=HighPoint-LowPoint; |
---|
| 61 | sumga=0.0; |
---|
| 62 | a=0.5*(HighPoint-LowPoint); |
---|
| 63 | b=0.5*(HighPoint+LowPoint); |
---|
| 64 | c=a*Abscissas[0]; |
---|
| 65 | d=Weights[0]*((typeT.*f)(b+c)+(typeT.*f)(b-c)); |
---|
| 66 | G4int i; |
---|
| 67 | for (i=2;i<=npoints;i++){ |
---|
| 68 | c=a*Abscissas[i-1]; |
---|
| 69 | d=d+Weights[i-1]*((typeT.*f)(b+c)+(typeT.*f)(b-c)); |
---|
| 70 | } |
---|
| 71 | G4int icall = 2*npoints; |
---|
| 72 | G4int LH=1; |
---|
| 73 | G4double S[nst],x[nst],sn[nst],xrn[nst]; |
---|
| 74 | S[0]=d*a; |
---|
| 75 | x[0]=LowPoint; |
---|
| 76 | |
---|
| 77 | //Adaptive bipartition scheme |
---|
| 78 | do{ |
---|
| 79 | G4double h0=h; |
---|
| 80 | h=0.5*h; //bipartition |
---|
| 81 | G4double sumr=0; |
---|
| 82 | G4int LHN=0; |
---|
| 83 | G4double si,xa,xb,xc; |
---|
| 84 | for (i=1;i<=LH;i++){ |
---|
| 85 | si=S[i-1]; |
---|
| 86 | xa=x[i-1]; |
---|
| 87 | xb=xa+h; |
---|
| 88 | xc=xa+h0; |
---|
| 89 | a=0.5*(xb-xa); |
---|
| 90 | b=0.5*(xb+xa); |
---|
| 91 | c=a*Abscissas[0]; |
---|
| 92 | d=Weights[0]*((typeT.*f)(b+c)+(typeT.*f)(b-c)); |
---|
| 93 | G4int j; |
---|
| 94 | for (j=1;j<npoints;j++){ |
---|
| 95 | c=a*Abscissas[j]; |
---|
| 96 | d=d+Weights[j]*((typeT.*f)(b+c)+(typeT.*f)(b-c)); |
---|
| 97 | } |
---|
| 98 | G4double s1,s2,s12; |
---|
| 99 | s1=d*a; |
---|
| 100 | a=0.5*(xc-xb); |
---|
| 101 | b=0.5*(xc+xb); |
---|
| 102 | c=a*Abscissas[0]; |
---|
| 103 | d=Weights[0]*((typeT.*f)(b+c)+(typeT.*f)(b-c)); |
---|
| 104 | for (j=1;j<npoints;j++){ |
---|
| 105 | c=a*Abscissas[j]; |
---|
| 106 | d=d+Weights[j]*((typeT.*f)(b+c)+(typeT.*f)(b-c)); |
---|
| 107 | } |
---|
| 108 | s2=d*a; |
---|
| 109 | icall=icall+4*npoints; |
---|
| 110 | s12=s1+s2; |
---|
| 111 | if (std::abs(s12-si)<std::max(Ptol*std::abs(s12),1e-35)){ |
---|
| 112 | sumga=sumga+s12; |
---|
| 113 | } |
---|
| 114 | else |
---|
| 115 | { |
---|
| 116 | sumr=sumr+s12; |
---|
| 117 | LHN=LHN+2; |
---|
| 118 | sn[LHN-1]=s2; |
---|
| 119 | xrn[LHN-1]=xb; |
---|
| 120 | sn[LHN-2]=s1; |
---|
| 121 | xrn[LHN-2]=xa; |
---|
| 122 | } |
---|
| 123 | if (icall>ncallsmax || LHN>nst) |
---|
| 124 | { |
---|
| 125 | G4cout << "G4PenelopeIntegrator: " << G4endl; |
---|
| 126 | G4cout << "LowPoint: " << LowPoint << ", High Point: " << HighPoint << G4endl; |
---|
| 127 | G4cout << "Tolerance: " << MaxError << G4endl; |
---|
| 128 | G4cout << "Calls: " << icall << ", Integral: " << sumga << ", Error: " << Err << G4cout; |
---|
| 129 | G4cout << "Number of open subintervals: " << LHN << G4endl; |
---|
| 130 | G4cout << "WARNING: the required accuracy has not been attained" << G4endl; |
---|
| 131 | return sumga; |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | Err=std::abs(sumr)/std::max(std::abs(sumr+sumga),1e-35); |
---|
| 135 | if (Err < Ctol || LHN == 0) return sumga; //end of cycle |
---|
| 136 | LH=LHN; |
---|
| 137 | for (G4int i=0;i<LH;i++){ |
---|
| 138 | S[i]=sn[i]; |
---|
| 139 | x[i]=xrn[i]; |
---|
| 140 | } |
---|
| 141 | }while(Ctol < 1.0); //infinite loop |
---|
| 142 | return 0; //It should never get here |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | template <class T,class F> |
---|
| 146 | G4double G4PenelopeIntegrator<T,F>::Calculate(T* ptrT, F f, |
---|
| 147 | G4double LowPoint,G4double HighPoint, |
---|
| 148 | G4double MaxError) |
---|
| 149 | { |
---|
| 150 | return Calculate(*ptrT,f,LowPoint,HighPoint,MaxError); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | |
---|