Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer << 3 // * DISCLAIMER * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th << 5 // * The following disclaimer summarizes all the specific disclaimers * 6 // * the Geant4 Collaboration. It is provided << 6 // * of contributors to this software. The specific disclaimers,which * 7 // * conditions of the Geant4 Software License << 7 // * govern, are listed with their locations in: * 8 // * LICENSE and available at http://cern.ch/ << 8 // * http://cern.ch/geant4/license * 9 // * include a list of copyright holders. << 10 // * 9 // * * 11 // * Neither the authors of this software syst 10 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 11 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 12 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 13 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file << 14 // * use. * 16 // * for the full disclaimer and the limitatio << 17 // * 15 // * * 18 // * This code implementation is the result << 16 // * This code implementation is the intellectual property of the * 19 // * technical work of the GEANT4 collaboratio << 17 // * GEANT4 collaboration. * 20 // * By using, copying, modifying or distri << 18 // * By copying, distributing or modifying the Program (or any work * 21 // * any work based on the software) you ag << 19 // * based on the Program) you indicate your acceptance of this * 22 // * use in resulting scientific publicati << 20 // * statement, and all its terms. * 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* 21 // ******************************************************************** 25 // 22 // 26 // G4GaussLegendreQ class implementation << 27 // 23 // 28 // Author: V.Grichine, 13.05.1997 << 24 // $Id: G4GaussLegendreQ.cc,v 1.3 2001/07/11 10:00:41 gunter Exp $ 29 // ------------------------------------------- << 25 // GEANT4 tag $Name: geant4-05-02-patch-01 $ 30 << 26 // 31 #include "G4GaussLegendreQ.hh" 27 #include "G4GaussLegendreQ.hh" 32 #include "G4PhysicalConstants.hh" << 33 28 34 G4GaussLegendreQ::G4GaussLegendreQ(function pF << 29 35 : G4VGaussianQuadrature(pFunction) << 30 G4GaussLegendreQ::G4GaussLegendreQ( function pFunction ) 36 {} << 31 : G4VGaussianQuadrature(pFunction) 37 << 32 { 38 // ------------------------------------------- << 33 ; 39 // << 34 } 40 // Constructor for GaussLegendre quadrature me << 35 41 // the accuracy required, i.e the number of po << 36 42 // will be evaluated during integration. The c << 37 43 // abscissas and weights that are used in Gaus << 38 // ---------------------------------------------------------------------------- >> 39 // >> 40 // Constructor for GaussLegendre quadrature method. The value nLegendre set the >> 41 // accuracy required, i.e the number of points where the function pFunction will >> 42 // be evaluated during integration. The constructor creates the arrays for >> 43 // abscissas and weights that used in Gauss-Legendre quadrature method. 44 // The values a and b are the limits of integr 44 // The values a and b are the limits of integration of the pFunction. 45 // nLegendre MUST BE EVEN !!! 45 // nLegendre MUST BE EVEN !!! 46 46 47 G4GaussLegendreQ::G4GaussLegendreQ(function pF << 47 G4GaussLegendreQ::G4GaussLegendreQ( function pFunction, 48 : G4VGaussianQuadrature(pFunction) << 48 G4int nLegendre ) >> 49 : G4VGaussianQuadrature(pFunction) 49 { 50 { 50 const G4double tolerance = 1.6e-10; << 51 const G4double tolerance = 1.6e-10 ; 51 G4int k = nLegendre; << 52 G4int i, j, k = nLegendre ; 52 fNumber = (nLegendre + 1) / << 53 fNumber = (nLegendre + 1)/2 ; 53 if(2 * fNumber != k) << 54 if(2*fNumber != k) 54 { << 55 { 55 G4Exception("G4GaussLegendreQ::G4GaussLege << 56 G4Exception("Invalid nLegendre in G4GaussLegendreQ::G4GaussLegendreQ") ; 56 FatalException, "Invalid nLege << 57 } 57 } << 58 G4double newton, newton1, temp1, temp2, temp3, temp ; 58 G4double newton0 = 0.0, newton1 = 0.0, temp1 << 59 59 temp = 0.0; << 60 fAbscissa = new G4double[fNumber] ; 60 << 61 fWeight = new G4double[fNumber] ; 61 fAbscissa = new G4double[fNumber]; << 62 62 fWeight = new G4double[fNumber]; << 63 for(i=1;i<=fNumber;i++) // Loop over the desired roots 63 << 64 { 64 for(G4int i = 1; i <= fNumber; ++i) // Loop << 65 newton = cos(pi*(i - 0.25)/(k + 0.5)) ; // Initial root approximation 65 { << 66 do 66 newton0 = std::cos(pi * (i - 0.25) / (k + << 67 { // loop of Newton's method 67 do << 68 temp1 = 1.0 ; 68 { << 69 temp2 = 0.0 ; 69 temp1 = 1.0; << 70 for(j=1;j<=k;j++) 70 temp2 = 0.0; << 71 { 71 for(G4int j = 1; j <= k; ++j) << 72 temp3 = temp2 ; 72 { << 73 temp2 = temp1 ; 73 temp3 = temp2; << 74 temp1 = ((2.0*j - 1.0)*newton*temp2 - (j - 1.0)*temp3)/j ; 74 temp2 = temp1; << 75 } 75 temp1 = ((2.0 * j - 1.0) * newton0 * t << 76 temp = k*(newton*temp1 - temp2)/(newton*newton - 1.0) ; >> 77 newton1 = newton ; >> 78 newton = newton1 - temp1/temp ; // Newton's method 76 } 79 } 77 temp = k * (newton0 * temp1 - temp2) << 80 while(fabs(newton - newton1) > tolerance) ; 78 newton1 = newton0; << 81 79 newton0 = newton1 - temp1 / temp; // Ne << 82 fAbscissa[fNumber-i] = newton ; 80 } while(std::fabs(newton0 - newton1) > tol << 83 fWeight[fNumber-i] = 2.0/((1.0 - newton*newton)*temp*temp) ; 81 << 84 } 82 fAbscissa[fNumber - i] = newton0; << 83 fWeight[fNumber - i] = 2.0 / ((1.0 - new << 84 } << 85 } 85 } 86 86 87 // ------------------------------------------- << 87 >> 88 // ------------------------------------------------------------------------------- 88 // 89 // 89 // Returns the integral of the function to be << 90 // Returns the integral of the function to be pointed by fFunction between a and b, 90 // and b, by 2*fNumber point Gauss-Legendre in << 91 // by 2*fNumber point Gauss-Legendre integration: the function is evaluated exactly 91 // evaluated exactly 2*fNumber times at interi << 92 // 2*fNumber Times at interior points in the range of integration. Since the weights 92 // integration. Since the weights and abscissa << 93 // and abscissas are, in this case, symmetric around the midpoint of the range of 93 // around the midpoint of the range of integra << 94 // integration, there are actually only fNumber distinct values of each. 94 // fNumber distinct values of each. << 95 95 96 G4double G4GaussLegendreQ::Integral(G4double a << 96 G4double >> 97 G4GaussLegendreQ::Integral(G4double a, G4double b) const 97 { 98 { 98 G4double xMean = 0.5 * (a + b), xDiff = 0.5 << 99 G4int i ; 99 dx = 0.0; << 100 G4double xDiff, xMean, dx, integral ; 100 for(G4int i = 0; i < fNumber; ++i) << 101 101 { << 102 xMean = 0.5*(a + b) ; 102 dx = xDiff * fAbscissa[i]; << 103 xDiff = 0.5*(b - a) ; 103 integral += fWeight[i] * (fFunction(xMean << 104 integral = 0.0 ; 104 } << 105 for(i=0;i<fNumber;i++) 105 return integral *= xDiff; << 106 { >> 107 dx = xDiff*fAbscissa[i] ; >> 108 integral += fWeight[i]*(fFunction(xMean + dx) + fFunction(xMean - dx)) ; >> 109 } >> 110 return integral *= xDiff ; 106 } 111 } 107 112 108 // ------------------------------------------- << 113 // ------------------------------------------------------------------------------- 109 // 114 // 110 // Returns the integral of the function to be << 115 // Returns the integral of the function to be pointed by fFunction between a and b, 111 // and b, by ten point Gauss-Legendre integrat << 116 // by ten point Gauss-Legendre integration: the function is evaluated exactly 112 // exactly ten times at interior points in the << 117 // ten Times at interior points in the range of integration. Since the weights 113 // weights and abscissas are, in this case, sy << 118 // and abscissas are, in this case, symmetric around the midpoint of the range of 114 // the range of integration, there are actuall << 119 // integration, there are actually only five distinct values of each 115 // each. << 116 120 117 G4double G4GaussLegendreQ::QuickIntegral(G4dou << 121 G4double >> 122 G4GaussLegendreQ::QuickIntegral(G4double a, G4double b) const 118 { 123 { 119 // From Abramowitz M., Stegan I.A. 1964 , Ha << 124 G4int i ; 120 << 125 G4double xDiff, xMean, dx, integral ; 121 static const G4double abscissa[] = { 0.14887 << 126 122 0.67940 << 127 // From Abramowitz M., Stegan I.A. 1964 , Handbook of Math... , p. 916 123 0.97390 << 128 124 << 129 static G4double abscissa[] = { 0.148874338981631, 0.433395394129247, 125 static const G4double weight[] = { 0.2955242 << 130 0.679409568299024, 0.865063366688985, 126 0.2190863 << 131 0.973906528517172 } ; 127 0.0666713 << 132 128 G4double xMean = 0.5 * (a + b), xDiff = 0.5 << 133 static G4double weight[] = { 0.295524224714753, 0.269266719309996, 129 dx = 0.0; << 134 0.219086362515982, 0.149451349150581, 130 for(G4int i = 0; i < 5; ++i) << 135 0.066671344308688 } ; 131 { << 136 xMean = 0.5*(a + b) ; 132 dx = xDiff * abscissa[i]; << 137 xDiff = 0.5*(b - a) ; 133 integral += weight[i] * (fFunction(xMean + << 138 integral = 0.0 ; 134 } << 139 for(i=0;i<5;i++) 135 return integral *= xDiff; << 140 { >> 141 dx = xDiff*abscissa[i] ; >> 142 integral += weight[i]*(fFunction(xMean + dx) + fFunction(xMean - dx)) ; >> 143 } >> 144 return integral *= xDiff ; 136 } 145 } 137 146 >> 147 138 // ------------------------------------------- 148 // ------------------------------------------------------------------------- 139 // 149 // 140 // Returns the integral of the function to be << 150 // Returns the integral of the function to be pointed by fFunction between a and b, 141 // and b, by 96 point Gauss-Legendre integrati << 151 // by 96 point Gauss-Legendre integration: the function is evaluated exactly 142 // exactly ten times at interior points in the << 152 // ten Times at interior points in the range of integration. Since the weights 143 // weights and abscissas are, in this case, sy << 153 // and abscissas are, in this case, symmetric around the midpoint of the range of 144 // the range of integration, there are actuall << 154 // integration, there are actually only five distinct values of each 145 // each. << 146 155 147 G4double G4GaussLegendreQ::AccurateIntegral(G4 << 156 G4double >> 157 G4GaussLegendreQ::AccurateIntegral(G4double a, G4double b) const 148 { 158 { 149 // From Abramowitz M., Stegan I.A. 1964 , Ha << 159 G4int i ; 150 << 160 G4double xDiff, xMean, dx, integral ; 151 static const G4double abscissa[] = { << 161 152 0.016276744849602969579, 0.048812985136049 << 162 // From Abramowitz M., Stegan I.A. 1964 , Handbook of Math... , p. 919 153 0.081297495464425558994, 0.113695850110665 << 163 154 0.145973714654896941989, 0.178096882367618 << 164 static 155 << 165 G4double abscissa[] = { 156 0.210031310460567203603, 0.241743156163840 << 166 0.016276744849602969579, 0.048812985136049731112, 157 0.273198812591049141487, 0.304364944354496 << 167 0.081297495464425558994, 0.113695850110665920911, 158 0.335208522892625422616, 0.365696861472313 << 168 0.145973714654896941989, 0.178096882367618602759, // 6 159 << 169 160 0.395797649828908603285, 0.425478988407300 << 170 0.210031310460567203603, 0.241743156163840012328, 161 0.454709422167743008636, 0.483457973920596 << 171 0.273198812591049141487, 0.304364944354496353024, 162 0.511694177154667673586, 0.539388108324357 << 172 0.335208522892625422616, 0.365696861472313635031, // 12 163 << 173 164 0.566510418561397168404, 0.593032364777572 << 174 0.395797649828908603285, 0.425478988407300545365, 165 0.618925840125468570386, 0.644163403784967 << 175 0.454709422167743008636, 0.483457973920596359768, 166 0.668718310043916153953, 0.692564536642171 << 176 0.511694177154667673586, 0.539388108324357436227, // 18 167 << 177 168 0.715676812348967626225, 0.738030643744400 << 178 0.566510418561397168404, 0.593032364777572080684, 169 0.759602341176647498703, 0.780369043867433 << 179 0.618925840125468570386, 0.644163403784967106798, 170 0.800308744139140817229, 0.819400310737931 << 180 0.668718310043916153953, 0.692564536642171561344, // 24 171 << 181 172 0.837623511228187121494, 0.854959033434601 << 182 0.715676812348967626225, 0.738030643744400132851, 173 0.871388505909296502874, 0.886894517402420 << 183 0.759602341176647498703, 0.780369043867433217604, 174 0.901460635315852341319, 0.915071423120898 << 184 0.800308744139140817229, 0.819400310737931675539, // 30 175 << 185 176 0.927712456722308690965, 0.939370339752755 << 186 0.837623511228187121494, 0.854959033434601455463, 177 0.950032717784437635756, 0.959688291448742 << 187 0.871388505909296502874, 0.886894517402420416057, 178 0.968326828463264212174, 0.975939174585136 << 188 0.901460635315852341319, 0.915071423120898074206, // 36 179 << 189 180 0.982517263563014677447, 0.988054126329623 << 190 0.927712456722308690965, 0.939370339752755216932, 181 0.992543900323762624572, 0.995981842987209 << 191 0.950032717784437635756, 0.959688291448742539300, 182 0.998364375863181677724, 0.999689503883230 << 192 0.968326828463264212174, 0.975939174585136466453, // 42 183 }; << 193 184 << 194 0.982517263563014677447, 0.988054126329623799481, 185 static const G4double weight[] = { << 195 0.992543900323762624572, 0.995981842987209290650, 186 0.032550614492363166242, 0.032516118713868 << 196 0.998364375863181677724, 0.999689503883230766828 // 48 187 0.032447163714064269364, 0.032343822568575 << 197 } ; 188 0.032206204794030250669, 0.032034456231992 << 198 189 << 199 static 190 0.031828758894411006535, 0.031589330770727 << 200 G4double weight[] = { 191 0.031316425596862355813, 0.031010332586313 << 201 0.032550614492363166242, 0.032516118713868835987, 192 0.030671376123669149014, 0.030299915420827 << 202 0.032447163714064269364, 0.032343822568575928429, 193 << 203 0.032206204794030250669, 0.032034456231992663218, // 6 194 0.029896344136328385984, 0.029461089958167 << 204 195 0.028994614150555236543, 0.028497411065085 << 205 0.031828758894411006535, 0.031589330770727168558, 196 0.027970007616848334440, 0.027412962726029 << 206 0.031316425596862355813, 0.031010332586313837423, 197 << 207 0.030671376123669149014, 0.030299915420827593794, // 12 198 0.026826866725591762198, 0.026212340735672 << 208 199 0.025570036005349361499, 0.024900633222483 << 209 0.029896344136328385984, 0.029461089958167905970, 200 0.024204841792364691282, 0.023483399085926 << 210 0.028994614150555236543, 0.028497411065085385646, 201 << 211 0.027970007616848334440, 0.027412962726029242823, // 18 202 0.022737069658329374001, 0.021966644438744 << 212 203 0.021172939892191298988, 0.020356797154333 << 213 0.026826866725591762198, 0.026212340735672413913, 204 0.019519081140145022410, 0.018660679627411 << 214 0.025570036005349361499, 0.024900633222483610288, 205 << 215 0.024204841792364691282, 0.023483399085926219842, // 24 206 0.017782502316045260838, 0.016885479864245 << 216 207 0.015970562902562291381, 0.015038721026994 << 217 0.022737069658329374001, 0.021966644438744349195, 208 0.014090941772314860916, 0.013128229566961 << 218 0.021172939892191298988, 0.020356797154333324595, 209 << 219 0.019519081140145022410, 0.018660679627411467385, // 30 210 0.012151604671088319635, 0.011162102099838 << 220 211 0.010160770535008415758, 0.009148671230783 << 221 0.017782502316045260838, 0.016885479864245172450, 212 0.008126876925698759217, 0.007096470791153 << 222 0.015970562902562291381, 0.015038721026994938006, 213 << 223 0.014090941772314860916, 0.013128229566961572637, // 36 214 0.006058545504235961683, 0.005014202742927 << 224 215 0.003964554338444686674, 0.002910731817934 << 225 0.012151604671088319635, 0.011162102099838498591, 216 0.001853960788946921732, 0.000796792065552 << 226 0.010160770535008415758, 0.009148671230783386633, 217 }; << 227 0.008126876925698759217, 0.007096470791153865269, // 42 218 G4double xMean = 0.5 * (a + b), xDiff = 0.5 << 228 219 dx = 0.0; << 229 0.006058545504235961683, 0.005014202742927517693, 220 for(G4int i = 0; i < 48; ++i) << 230 0.003964554338444686674, 0.002910731817934946408, 221 { << 231 0.001853960788946921732, 0.000796792065552012429 // 48 222 dx = xDiff * abscissa[i]; << 232 } ; 223 integral += weight[i] * (fFunction(xMean + << 233 xMean = 0.5*(a + b) ; 224 } << 234 xDiff = 0.5*(b - a) ; 225 return integral *= xDiff; << 235 integral = 0.0 ; >> 236 for(i=0;i<48;i++) >> 237 { >> 238 dx = xDiff*abscissa[i] ; >> 239 integral += weight[i]*(fFunction(xMean + dx) + fFunction(xMean - dx)) ; >> 240 } >> 241 return integral *= xDiff ; 226 } 242 } >> 243 227 244