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 // G4SimpleIntegration class implementation << 27 // 26 // 28 // Author: V.Grichine, 26.03.1997 << 27 // $Id: G4SimpleIntegration.cc,v 1.7 2006-06-29 19:00:21 gunter Exp $ 29 // ------------------------------------------- << 28 // GEANT4 tag $Name: not supported by cvs2svn $ >> 29 // >> 30 // Implementation file for simple integration methods >> 31 // 30 32 31 #include "G4SimpleIntegration.hh" << 32 #include "globals.hh" 33 #include "globals.hh" >> 34 #include "G4SimpleIntegration.hh" >> 35 >> 36 >> 37 G4int G4SimpleIntegration::fMaxDepth = 100 ; >> 38 33 39 34 G4SimpleIntegration::G4SimpleIntegration(funct << 40 G4SimpleIntegration::G4SimpleIntegration( function pFunction ) 35 : fFunction(pFunction) << 41 : fFunction(pFunction), 36 {} << 42 fTolerance(.0001) 37 << 43 { 38 G4SimpleIntegration::G4SimpleIntegration(funct << 44 } 39 G4dou << 45 40 : fFunction(pFunction) << 46 G4SimpleIntegration::G4SimpleIntegration( function pFunction, 41 , fTolerance(pTolerance) << 47 G4double pTolerance) 42 {} << 48 : fFunction(pFunction), 43 << 49 fTolerance(pTolerance) 44 // Simple integration methods << 50 { 45 << 51 } 46 G4double G4SimpleIntegration::Trapezoidal(G4do << 52 47 G4in << 53 48 { << 54 G4SimpleIntegration::~G4SimpleIntegration() 49 G4double Step = (xFinal - xInitial) / iterat << 55 { 50 G4double mean = (fFunction(xInitial) + fFunc << 56 } 51 G4double x = xInitial; << 57 52 for(G4int i = 1; i < iterationNumber; ++i) << 58 // Simple integration methods 53 { << 59 54 x += Step; << 60 G4double 55 mean += fFunction(x); << 61 G4SimpleIntegration::Trapezoidal(G4double xInitial, 56 } << 62 G4double xFinal, 57 return mean * Step; << 63 G4int iterationNumber ) 58 } << 64 { 59 << 65 G4double Step = (xFinal - xInitial)/iterationNumber ; 60 G4double G4SimpleIntegration::MidPoint(G4doubl << 66 G4double mean = (fFunction(xInitial) + fFunction(xFinal))*0.5 ; 61 G4int i << 67 G4double x = xInitial ; 62 { << 68 for(G4int i=1;i<iterationNumber;i++) 63 G4double Step = (xFinal - xInitial) / iterat << 69 { 64 G4double x = xInitial + 0.5 * Step; << 70 x += Step ; 65 G4double mean = fFunction(x); << 71 mean += fFunction(x) ; 66 for(G4int i = 1; i < iterationNumber; ++i) << 72 } 67 { << 73 return mean*Step ; 68 x += Step; << 74 } 69 mean += fFunction(x); << 75 70 } << 76 G4double 71 return mean * Step; << 77 G4SimpleIntegration::MidPoint(G4double xInitial, 72 } << 78 G4double xFinal, 73 << 79 G4int iterationNumber ) 74 G4double G4SimpleIntegration::Gauss(G4double x << 80 { 75 G4int iter << 81 G4double Step = (xFinal - xInitial)/iterationNumber ; 76 { << 82 G4double x = xInitial + 0.5*Step; 77 G4double x = 0.; << 83 G4double mean = fFunction(x) ; 78 static const G4double root = 1.0 / std::sqrt << 84 for(G4int i=1;i<iterationNumber;i++) 79 G4double Step = (xFinal - xInit << 85 { 80 G4double delta = Step * root; << 86 x += Step ; 81 G4double mean = 0.0; << 87 mean += fFunction(x) ; 82 for(G4int i = 0; i < iterationNumber; ++i) << 88 } 83 { << 89 return mean*Step ; 84 x = (2 * i + 1) * Step; << 90 } 85 mean += (fFunction(x + delta) + fFunction( << 91 86 } << 92 G4double 87 return mean * Step; << 93 G4SimpleIntegration::Gauss(G4double xInitial, 88 } << 94 G4double xFinal, 89 << 95 G4int iterationNumber ) 90 G4double G4SimpleIntegration::Simpson(G4double << 96 { 91 G4int it << 97 G4double x=0.; 92 { << 98 static G4double root = 1.0/std::sqrt(3.0) ; 93 G4double Step = (xFinal - xInitial) / itera << 99 G4double Step = (xFinal - xInitial)/(2.0*iterationNumber) ; 94 G4double x = xInitial; << 100 G4double delta = Step*root ; 95 G4double xPlus = xInitial + 0.5 * Step; << 101 G4double mean = 0.0 ; 96 G4double mean = (fFunction(xInitial) + fFun << 102 for(G4int i=0;i<iterationNumber;i++) 97 G4double sum = fFunction(xPlus); << 103 { 98 for(G4int i = 1; i < iterationNumber; ++i) << 104 x = (2*i + 1)*Step ; 99 { << 105 mean += (fFunction(x+delta) + fFunction(x-delta)) ; 100 x += Step; << 106 } 101 xPlus += Step; << 107 return mean*Step ; 102 mean += fFunction(x); << 108 } 103 sum += fFunction(xPlus); << 109 104 } << 110 G4double 105 mean += 2.0 * sum; << 111 G4SimpleIntegration::Simpson(G4double xInitial, 106 return mean * Step / 3.0; << 112 G4double xFinal, 107 } << 113 G4int iterationNumber ) 108 << 114 { 109 // Adaptive Gauss integration << 115 G4double Step = (xFinal - xInitial)/iterationNumber ; 110 << 116 G4double x = xInitial ; 111 G4double G4SimpleIntegration::AdaptGaussIntegr << 117 G4double xPlus = xInitial + 0.5*Step ; 112 << 118 G4double mean = (fFunction(xInitial) + fFunction(xFinal))*0.5 ; 113 { << 119 G4double sum = fFunction(xPlus) ; 114 G4int depth = 0; << 120 for(G4int i=1;i<iterationNumber;i++) 115 G4double sum = 0.0; << 121 { 116 AdaptGauss(xInitial, xFinal, sum, depth); << 122 x += Step ; 117 return sum; << 123 xPlus += Step ; 118 } << 124 mean += fFunction(x) ; 119 << 125 sum += fFunction(xPlus) ; 120 G4double G4SimpleIntegration::Gauss(G4double x << 126 } 121 { << 127 mean += 2.0*sum ; 122 static const G4double root = 1.0 / std::sqrt << 128 return mean*Step/3.0 ; 123 << 129 } 124 G4double xMean = (xInitial + xFinal) / 2.0; << 130 125 G4double Step = (xFinal - xInitial) / 2.0; << 131 126 G4double delta = Step * root; << 132 127 G4double sum = (fFunction(xMean + delta) + << 133 // Adaptive Gauss integration 128 << 134 129 return sum * Step; << 135 G4double 130 } << 136 G4SimpleIntegration::AdaptGaussIntegration( G4double xInitial, 131 << 137 G4double xFinal ) 132 void G4SimpleIntegration::AdaptGauss(G4double << 138 { 133 G4double& << 139 G4int depth = 0 ; 134 { << 140 G4double sum = 0.0 ; 135 if(depth > fMaxDepth) << 141 AdaptGauss(xInitial,xFinal,sum,depth) ; 136 { << 142 return sum ; 137 G4Exception("G4SimpleIntegration::AdaptGau << 143 } 138 "Function varies too rapidly ! << 144 139 } << 145 140 G4double xMean = (xInitial + xFinal) / 2 << 146 G4double 141 G4double leftHalf = Gauss(xInitial, xMean); << 147 G4SimpleIntegration::Gauss( G4double xInitial, 142 G4double rightHalf = Gauss(xMean, xFinal); << 148 G4double xFinal ) 143 G4double full = Gauss(xInitial, xFinal) << 149 { 144 if(std::fabs(leftHalf + rightHalf - full) < << 150 static G4double root = 1.0/std::sqrt(3.0) ; 145 { << 151 146 sum += full; << 152 G4double xMean = (xInitial + xFinal)/2.0 ; 147 } << 153 G4double Step = (xFinal - xInitial)/2.0 ; 148 else << 154 G4double delta = Step*root ; 149 { << 155 G4double sum = (fFunction(xMean + delta) + fFunction(xMean - delta)) ; 150 ++depth; << 156 151 AdaptGauss(xInitial, xMean, sum, depth); << 157 return sum*Step ; 152 AdaptGauss(xMean, xFinal, sum, depth); << 158 } 153 } << 159 >> 160 >> 161 void >> 162 G4SimpleIntegration::AdaptGauss( G4double xInitial, >> 163 G4double xFinal, >> 164 G4double& sum, >> 165 G4int& depth ) >> 166 { >> 167 if(depth >fMaxDepth) >> 168 { >> 169 G4Exception("G4SimpleIntegration::AdaptGauss()", "Error", >> 170 FatalException, "Function varies too rapidly !") ; >> 171 } >> 172 G4double xMean = (xInitial + xFinal)/2.0 ; >> 173 G4double leftHalf = Gauss(xInitial,xMean) ; >> 174 G4double rightHalf = Gauss(xMean,xFinal) ; >> 175 G4double full = Gauss(xInitial,xFinal) ; >> 176 if(std::fabs(leftHalf+rightHalf-full) < fTolerance) >> 177 { >> 178 sum += full ; >> 179 } >> 180 else >> 181 { >> 182 depth++ ; >> 183 AdaptGauss(xInitial,xMean,sum,depth) ; >> 184 AdaptGauss(xMean,xFinal,sum,depth) ; >> 185 } 154 } 186 } 155 187