Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // G4Pow << 26 // $Id: G4Pow.hh 83383 2014-08-21 14:20:37Z gcosmo $ >> 27 // >> 28 // >> 29 // ------------------------------------------------------------------- >> 30 // >> 31 // Class G4Pow 27 // 32 // 28 // Class description: 33 // Class description: 29 // 34 // 30 // Utility singleton class for the fast comput 35 // Utility singleton class for the fast computation of log and pow 31 // functions. Integer argument should be in th << 36 // functions. Integer argument should in the interval 0-512, no 32 // check is performed inside these methods for 37 // check is performed inside these methods for performance reasons. 33 // For factorial integer argument should be in 38 // For factorial integer argument should be in the interval 0-170 34 // Computations with double arguments are fast 39 // Computations with double arguments are fast for the interval 35 // 0.002-511.5 for all functions except expone << 40 // 0.002-511.5 for all functions except exponent, which is computed 36 // for the interval 0-84.4, standard library i 41 // for the interval 0-84.4, standard library is used in the opposite case 37 42 38 // Author: Vladimir Ivanchenko, 23.05.2009 << 43 // Author: Vladimir Ivanchenko 39 // ------------------------------------------- << 44 // 40 #ifndef G4Pow_hh << 45 // Creation date: 23.05.2009 41 #define G4Pow_hh 1 << 46 // ------------------------------------------------------------------- >> 47 >> 48 #ifndef G4Pow_h >> 49 #define G4Pow_h 1 42 50 43 #include "G4DataVector.hh" << 44 #include "G4Exp.hh" << 45 #include "G4Log.hh" << 46 #include "globals.hh" 51 #include "globals.hh" >> 52 #include "G4Log.hh" >> 53 #include "G4Exp.hh" >> 54 #include "G4DataVector.hh" 47 55 48 class G4Pow 56 class G4Pow 49 { 57 { 50 public: << 58 51 static G4Pow* GetInstance(); << 59 public: 52 ~G4Pow() = default; << 60 53 << 61 static G4Pow* GetInstance(); 54 // Fast computation of Z^1/3 << 62 ~G4Pow(); 55 // << 63 56 inline G4double Z13(G4int Z) const; << 64 // Fast computation of Z^1/3 57 G4double A13(G4double A) const; << 65 // 58 << 66 inline G4double Z13(G4int Z) const; 59 // Fast computation of Z^2/3 << 67 inline G4double A13(G4double A) const; 60 // << 68 61 inline G4double Z23(G4int Z) const; << 69 // Fast computation of Z^2/3 62 inline G4double A23(G4double A) const; << 70 // 63 << 71 inline G4double Z23(G4int Z) const; 64 // Fast computation of log(Z) << 72 inline G4double A23(G4double A) const; 65 // << 73 66 inline G4double logZ(G4int Z) const; << 74 // Fast computation of log(Z) 67 inline G4double logA(G4double A) const; << 75 // 68 inline G4double logX(G4double x) const; << 76 inline G4double logZ(G4int Z) const; 69 << 77 inline G4double logA(G4double A) const; 70 // Fast computation of log10(Z) << 78 inline G4double logX(G4double x) const; 71 // << 79 72 inline G4double log10Z(G4int Z) const; << 80 // Fast computation of log10(Z) 73 inline G4double log10A(G4double A) const; << 81 // 74 << 82 inline G4double log10Z(G4int Z) const; 75 // Fast computation of exp(X) << 83 inline G4double log10A(G4double A) const; 76 // << 84 77 inline G4double expA(G4double A) const; << 85 // Fast computation of exp(X) 78 << 86 // 79 // Fast computation of pow(Z,X) << 87 inline G4double expA(G4double A) const; 80 // << 88 81 inline G4double powZ(G4int Z, G4double y) co << 89 // Fast computation of pow(Z,X) 82 inline G4double powA(G4double A, G4double y) << 90 // 83 G4double powN(G4double x, G4int n) const; << 91 inline G4double powZ(G4int Z, G4double y) const; 84 << 92 inline G4double powA(G4double A, G4double y) const; 85 // Fast factorial << 93 G4double powN(G4double x, G4int n) const; 86 // << 94 87 inline G4double factorial(G4int Z) const; << 95 // Fast factorial 88 inline G4double logfactorial(G4int Z) const; << 96 // 89 << 97 inline G4double factorial(G4int Z) const; 90 private: << 98 inline G4double logfactorial(G4int Z) const; 91 G4Pow(); << 99 92 << 100 private: 93 G4double A13Low(const G4double, const G4bool << 101 94 G4double A13High(const G4double, const G4boo << 102 G4Pow(); 95 << 103 96 inline G4double logBase(G4double x) const; << 104 inline G4double logBase(G4double x) const; 97 << 105 98 static G4Pow* fpInstance; << 106 static G4Pow* fpInstance; 99 << 107 100 const G4double onethird = 1.0 / 3.0; << 108 const G4double onethird; 101 const G4int max2 = 5; << 109 const G4int max2; 102 << 110 103 G4double maxA; << 111 G4double maxA; 104 G4double maxLowA; << 112 G4double maxA2; 105 G4double maxA2; << 113 G4double maxAexp; 106 G4double maxAexp; << 114 107 << 115 G4DataVector ener; 108 G4DataVector ener; << 116 G4DataVector logen; 109 G4DataVector logen; << 117 G4DataVector pz13; 110 G4DataVector pz13; << 118 G4DataVector lz; 111 G4DataVector lowa13; << 119 G4DataVector lz2; 112 G4DataVector lz; << 120 G4DataVector fexp; 113 G4DataVector lz2; << 121 G4DataVector fact; 114 G4DataVector fexp; << 122 G4DataVector logfact; 115 G4DataVector fact; << 116 G4DataVector logfact; << 117 }; 123 }; 118 124 119 // ----------------------------- << 125 // ------------------------------------------------------------------- 120 // Inline methods implementation << 121 // ----------------------------- << 122 126 123 inline G4double G4Pow::Z13(G4int Z) const { re << 127 inline G4double G4Pow::Z13(G4int Z) const >> 128 { >> 129 return pz13[Z]; >> 130 } >> 131 >> 132 inline G4double G4Pow::A13(G4double A) const >> 133 { >> 134 G4double res; >> 135 G4double a = A; >> 136 if(1.0 > A) { a = 1.0/A; } >> 137 if(a <= maxA) >> 138 { >> 139 G4int i = G4int(a + 0.5); >> 140 G4double x = (a/G4double(i) - 1.0)*onethird; >> 141 res = pz13[i]*(1.0 + x - x*x*(1.0 - 1.66666666*x)); >> 142 if(1.0 > A) { res = 1.0/res; } >> 143 } >> 144 else >> 145 { >> 146 res = std::pow(A, onethird); >> 147 } >> 148 return res; >> 149 } 124 150 125 inline G4double G4Pow::Z23(G4int Z) const 151 inline G4double G4Pow::Z23(G4int Z) const 126 { 152 { 127 G4double x = Z13(Z); 153 G4double x = Z13(Z); 128 return x * x; << 154 return x*x; 129 } 155 } 130 156 131 inline G4double G4Pow::A23(G4double A) const 157 inline G4double G4Pow::A23(G4double A) const 132 { 158 { 133 G4double x = A13(A); 159 G4double x = A13(A); 134 return x * x; << 160 return x*x; 135 } 161 } 136 162 137 inline G4double G4Pow::logZ(G4int Z) const { r << 163 inline G4double G4Pow::logZ(G4int Z) const >> 164 { >> 165 return lz[Z]; >> 166 } 138 167 139 inline G4double G4Pow::logBase(G4double a) con 168 inline G4double G4Pow::logBase(G4double a) const 140 { 169 { 141 G4double res; 170 G4double res; 142 if(a <= maxA2) << 171 if(a <= maxA2) 143 { 172 { 144 G4int i = G4int(max2 * (a - 1) + 0.5); << 173 G4int i = G4int(max2*(a - 1) + 0.5); 145 if(i > max2) << 174 if(i > max2) { i = max2; } 146 { << 175 G4double x = a/(G4double(i)/max2 + 1) - 1; 147 i = max2; << 176 res = lz2[i] + x*(1.0 - (0.5 - onethird*x)*x); 148 } << 149 G4double x = a / (G4double(i) / max2 + 1) << 150 res = lz2[i] + x * (1.0 - (0.5 - on << 151 } 177 } 152 else if(a <= maxA) 178 else if(a <= maxA) 153 { 179 { 154 G4int i = G4int(a + 0.5); << 180 G4int i = G4int(a + 0.5); 155 G4double x = a / G4double(i) - 1; << 181 G4double x = a/G4double(i) - 1; 156 res = lz[i] + x * (1.0 - (0.5 - one << 182 res = lz[i] + x*(1.0 - (0.5 - onethird*x)*x); 157 } 183 } 158 else 184 else 159 { 185 { 160 res = G4Log(a); 186 res = G4Log(a); 161 } 187 } 162 return res; 188 return res; 163 } 189 } 164 190 165 inline G4double G4Pow::logA(G4double A) const 191 inline G4double G4Pow::logA(G4double A) const 166 { 192 { 167 return (1.0 <= A ? logBase(A) : -logBase(1. << 193 G4double res; >> 194 if(1.0 <= A) { res = logBase(A); } >> 195 else { res = -logBase(1./A); } >> 196 return res; 168 } 197 } 169 198 170 inline G4double G4Pow::logX(G4double x) const 199 inline G4double G4Pow::logX(G4double x) const 171 { 200 { 172 G4double res = 0.0; 201 G4double res = 0.0; 173 G4double a = (1.0 <= x) ? x : 1.0 / x; << 202 G4double a = x; >> 203 if(1.0 > x) { a = 1.0/x; } 174 204 175 if(a <= maxA) << 205 if(a <= maxA) 176 { 206 { 177 res = logBase(a); 207 res = logBase(a); 178 } 208 } 179 else if(a <= ener[2]) 209 else if(a <= ener[2]) 180 { 210 { 181 res = logen[1] + logBase(a / ener[1]); << 211 res = logen[1] + logBase(a/ener[1]); 182 } 212 } 183 else if(a <= ener[3]) 213 else if(a <= ener[3]) 184 { 214 { 185 res = logen[2] + logBase(a / ener[2]); << 215 res = logen[2] + logBase(a/ener[2]); 186 } 216 } 187 else 217 else 188 { 218 { 189 res = G4Log(a); 219 res = G4Log(a); 190 } 220 } 191 221 192 if(1.0 > x) << 222 if(1.0 > x) { res = -res; } 193 { << 194 res = -res; << 195 } << 196 return res; 223 return res; 197 } 224 } 198 225 199 inline G4double G4Pow::log10Z(G4int Z) const { << 226 inline G4double G4Pow::log10Z(G4int Z) const >> 227 { >> 228 return lz[Z]/lz[10]; >> 229 } 200 230 201 inline G4double G4Pow::log10A(G4double A) cons << 231 inline G4double G4Pow::log10A(G4double A) const >> 232 { >> 233 return logX(A)/lz[10]; >> 234 } 202 235 203 inline G4double G4Pow::expA(G4double A) const 236 inline G4double G4Pow::expA(G4double A) const 204 { 237 { 205 G4double res; 238 G4double res; 206 G4double a = (0.0 <= A) ? A : -A; << 239 G4double a = A; >> 240 if(0.0 > A) { a = -A; } 207 241 208 if(a <= maxAexp) 242 if(a <= maxAexp) 209 { 243 { 210 G4int i = G4int(2 * a + 0.5); << 244 G4int i = G4int(2*a + 0.5); 211 G4double x = a - i * 0.5; << 245 G4double x = a - i*0.5; 212 res = fexp[i] * (1.0 + x * (1.0 + 0 << 246 res = fexp[i]*(1.0 + x*(1.0 + 0.5*(1.0 + onethird*x)*x)); 213 } 247 } 214 else 248 else 215 { 249 { 216 res = G4Exp(a); 250 res = G4Exp(a); 217 } 251 } 218 if(0.0 > A) << 252 if(0.0 > A) { res = 1.0/res; } 219 { << 220 res = 1.0 / res; << 221 } << 222 return res; 253 return res; 223 } 254 } 224 255 225 inline G4double G4Pow::powZ(G4int Z, G4double 256 inline G4double G4Pow::powZ(G4int Z, G4double y) const 226 { 257 { 227 return expA(y * lz[Z]); << 258 return expA(y*lz[Z]); 228 } 259 } 229 260 230 inline G4double G4Pow::powA(G4double A, G4doub 261 inline G4double G4Pow::powA(G4double A, G4double y) const 231 { 262 { 232 return (0.0 == A ? 0.0 : expA(y * logX(A))); << 263 return expA(y*logX(A)); 233 } 264 } 234 265 235 inline G4double G4Pow::factorial(G4int Z) cons << 266 inline G4double G4Pow::factorial(G4int Z) const >> 267 { >> 268 return fact[Z]; >> 269 } >> 270 >> 271 inline G4double G4Pow::logfactorial(G4int Z) const >> 272 { >> 273 return logfact[Z]; >> 274 } 236 275 237 inline G4double G4Pow::logfactorial(G4int Z) c << 276 // ------------------------------------------------------------------- 238 277 239 #endif 278 #endif 240 279