Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:58:57

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // G4Pow
0027 //
0028 // Class description:
0029 //
0030 // Utility singleton class for the fast computation of log and pow
0031 // functions. Integer argument should be in the interval 0-512, no
0032 // check is performed inside these methods for performance reasons.
0033 // For factorial integer argument should be in the interval 0-170
0034 // Computations with double arguments are fast for the interval
0035 // 0.002-511.5 for all functions except exponent, which is computed
0036 // for the interval 0-84.4, standard library is used in the opposite case
0037 
0038 // Author: Vladimir Ivanchenko, 23.05.2009
0039 // --------------------------------------------------------------------
0040 #ifndef G4Pow_hh
0041 #define G4Pow_hh 1
0042 
0043 #include "G4DataVector.hh"
0044 #include "G4Exp.hh"
0045 #include "G4Log.hh"
0046 #include "globals.hh"
0047 
0048 class G4Pow
0049 {
0050  public:
0051   static G4Pow* GetInstance();
0052   ~G4Pow() = default;
0053 
0054   // Fast computation of Z^1/3
0055   //
0056   inline G4double Z13(G4int Z) const;
0057   G4double A13(G4double A) const;
0058 
0059   // Fast computation of Z^2/3
0060   //
0061   inline G4double Z23(G4int Z) const;
0062   inline G4double A23(G4double A) const;
0063 
0064   // Fast computation of log(Z)
0065   //
0066   inline G4double logZ(G4int Z) const;
0067   inline G4double logA(G4double A) const;
0068   inline G4double logX(G4double x) const;
0069 
0070   // Fast computation of log10(Z)
0071   //
0072   inline G4double log10Z(G4int Z) const;
0073   inline G4double log10A(G4double A) const;
0074 
0075   // Fast computation of exp(X)
0076   //
0077   inline G4double expA(G4double A) const;
0078 
0079   // Fast computation of pow(Z,X)
0080   //
0081   inline G4double powZ(G4int Z, G4double y) const;
0082   inline G4double powA(G4double A, G4double y) const;
0083   G4double powN(G4double x, G4int n) const;
0084 
0085   // Fast factorial
0086   //
0087   inline G4double factorial(G4int Z) const;
0088   inline G4double logfactorial(G4int Z) const;
0089 
0090  private:
0091   G4Pow();
0092 
0093   G4double A13Low(const G4double, const G4bool) const;
0094   G4double A13High(const G4double, const G4bool) const;
0095 
0096   inline G4double logBase(G4double x) const;
0097 
0098   static G4Pow* fpInstance;
0099 
0100   const G4double onethird = 1.0 / 3.0;
0101   const G4int max2        = 5;
0102 
0103   G4double maxA;
0104   G4double maxLowA;
0105   G4double maxA2;
0106   G4double maxAexp;
0107 
0108   G4DataVector ener;
0109   G4DataVector logen;
0110   G4DataVector pz13;
0111   G4DataVector lowa13;
0112   G4DataVector lz;
0113   G4DataVector lz2;
0114   G4DataVector fexp;
0115   G4DataVector fact;
0116   G4DataVector logfact;
0117 };
0118 
0119 // -----------------------------
0120 // Inline methods implementation
0121 // -----------------------------
0122 
0123 inline G4double G4Pow::Z13(G4int Z) const { return pz13[Z]; }
0124 
0125 inline G4double G4Pow::Z23(G4int Z) const
0126 {
0127   G4double x = Z13(Z);
0128   return x * x;
0129 }
0130 
0131 inline G4double G4Pow::A23(G4double A) const
0132 {
0133   G4double x = A13(A);
0134   return x * x;
0135 }
0136 
0137 inline G4double G4Pow::logZ(G4int Z) const { return lz[Z]; }
0138 
0139 inline G4double G4Pow::logBase(G4double a) const
0140 {
0141   G4double res;
0142   if(a <= maxA2)
0143   {
0144     G4int i = G4int(max2 * (a - 1) + 0.5);
0145     if(i > max2)
0146     {
0147       i = max2;
0148     }
0149     G4double x = a / (G4double(i) / max2 + 1) - 1;
0150     res        = lz2[i] + x * (1.0 - (0.5 - onethird * x) * x);
0151   }
0152   else if(a <= maxA)
0153   {
0154     G4int i    = G4int(a + 0.5);
0155     G4double x = a / G4double(i) - 1;
0156     res        = lz[i] + x * (1.0 - (0.5 - onethird * x) * x);
0157   }
0158   else
0159   {
0160     res = G4Log(a);
0161   }
0162   return res;
0163 }
0164 
0165 inline G4double G4Pow::logA(G4double A) const
0166 {
0167   return (1.0 <= A ? logBase(A) : -logBase(1. / A));
0168 }
0169 
0170 inline G4double G4Pow::logX(G4double x) const
0171 {
0172   G4double res = 0.0;
0173   G4double a   = (1.0 <= x) ? x : 1.0 / x;
0174 
0175   if(a <= maxA)
0176   {
0177     res = logBase(a);
0178   }
0179   else if(a <= ener[2])
0180   {
0181     res = logen[1] + logBase(a / ener[1]);
0182   }
0183   else if(a <= ener[3])
0184   {
0185     res = logen[2] + logBase(a / ener[2]);
0186   }
0187   else
0188   {
0189     res = G4Log(a);
0190   }
0191 
0192   if(1.0 > x)
0193   {
0194     res = -res;
0195   }
0196   return res;
0197 }
0198 
0199 inline G4double G4Pow::log10Z(G4int Z) const { return lz[Z] / lz[10]; }
0200 
0201 inline G4double G4Pow::log10A(G4double A) const { return logX(A) / lz[10]; }
0202 
0203 inline G4double G4Pow::expA(G4double A) const
0204 {
0205   G4double res;
0206   G4double a = (0.0 <= A) ? A : -A;
0207 
0208   if(a <= maxAexp)
0209   {
0210     G4int i    = G4int(2 * a + 0.5);
0211     G4double x = a - i * 0.5;
0212     res        = fexp[i] * (1.0 + x * (1.0 + 0.5 * (1.0 + onethird * x) * x));
0213   }
0214   else
0215   {
0216     res = G4Exp(a);
0217   }
0218   if(0.0 > A)
0219   {
0220     res = 1.0 / res;
0221   }
0222   return res;
0223 }
0224 
0225 inline G4double G4Pow::powZ(G4int Z, G4double y) const
0226 {
0227   return expA(y * lz[Z]);
0228 }
0229 
0230 inline G4double G4Pow::powA(G4double A, G4double y) const
0231 {
0232   return (0.0 == A ? 0.0 : expA(y * logX(A)));
0233 }
0234 
0235 inline G4double G4Pow::factorial(G4int Z) const { return fact[Z]; }
0236 
0237 inline G4double G4Pow::logfactorial(G4int Z) const { return logfact[Z]; }
0238 
0239 #endif