[833] | 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 | // $Id: G4SimpleIntegration.cc,v 1.7 2006/06/29 19:00:21 gunter Exp $ |
---|
| 28 | // GEANT4 tag $Name: $ |
---|
| 29 | // |
---|
| 30 | // Implementation file for simple integration methods |
---|
| 31 | // |
---|
| 32 | |
---|
| 33 | #include "globals.hh" |
---|
| 34 | #include "G4SimpleIntegration.hh" |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | G4int G4SimpleIntegration::fMaxDepth = 100 ; |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | G4SimpleIntegration::G4SimpleIntegration( function pFunction ) |
---|
| 41 | : fFunction(pFunction), |
---|
| 42 | fTolerance(.0001) |
---|
| 43 | { |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | G4SimpleIntegration::G4SimpleIntegration( function pFunction, |
---|
| 47 | G4double pTolerance) |
---|
| 48 | : fFunction(pFunction), |
---|
| 49 | fTolerance(pTolerance) |
---|
| 50 | { |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | G4SimpleIntegration::~G4SimpleIntegration() |
---|
| 55 | { |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | // Simple integration methods |
---|
| 59 | |
---|
| 60 | G4double |
---|
| 61 | G4SimpleIntegration::Trapezoidal(G4double xInitial, |
---|
| 62 | G4double xFinal, |
---|
| 63 | G4int iterationNumber ) |
---|
| 64 | { |
---|
| 65 | G4double Step = (xFinal - xInitial)/iterationNumber ; |
---|
| 66 | G4double mean = (fFunction(xInitial) + fFunction(xFinal))*0.5 ; |
---|
| 67 | G4double x = xInitial ; |
---|
| 68 | for(G4int i=1;i<iterationNumber;i++) |
---|
| 69 | { |
---|
| 70 | x += Step ; |
---|
| 71 | mean += fFunction(x) ; |
---|
| 72 | } |
---|
| 73 | return mean*Step ; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | G4double |
---|
| 77 | G4SimpleIntegration::MidPoint(G4double xInitial, |
---|
| 78 | G4double xFinal, |
---|
| 79 | G4int iterationNumber ) |
---|
| 80 | { |
---|
| 81 | G4double Step = (xFinal - xInitial)/iterationNumber ; |
---|
| 82 | G4double x = xInitial + 0.5*Step; |
---|
| 83 | G4double mean = fFunction(x) ; |
---|
| 84 | for(G4int i=1;i<iterationNumber;i++) |
---|
| 85 | { |
---|
| 86 | x += Step ; |
---|
| 87 | mean += fFunction(x) ; |
---|
| 88 | } |
---|
| 89 | return mean*Step ; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | G4double |
---|
| 93 | G4SimpleIntegration::Gauss(G4double xInitial, |
---|
| 94 | G4double xFinal, |
---|
| 95 | G4int iterationNumber ) |
---|
| 96 | { |
---|
| 97 | G4double x=0.; |
---|
| 98 | static G4double root = 1.0/std::sqrt(3.0) ; |
---|
| 99 | G4double Step = (xFinal - xInitial)/(2.0*iterationNumber) ; |
---|
| 100 | G4double delta = Step*root ; |
---|
| 101 | G4double mean = 0.0 ; |
---|
| 102 | for(G4int i=0;i<iterationNumber;i++) |
---|
| 103 | { |
---|
| 104 | x = (2*i + 1)*Step ; |
---|
| 105 | mean += (fFunction(x+delta) + fFunction(x-delta)) ; |
---|
| 106 | } |
---|
| 107 | return mean*Step ; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | G4double |
---|
| 111 | G4SimpleIntegration::Simpson(G4double xInitial, |
---|
| 112 | G4double xFinal, |
---|
| 113 | G4int iterationNumber ) |
---|
| 114 | { |
---|
| 115 | G4double Step = (xFinal - xInitial)/iterationNumber ; |
---|
| 116 | G4double x = xInitial ; |
---|
| 117 | G4double xPlus = xInitial + 0.5*Step ; |
---|
| 118 | G4double mean = (fFunction(xInitial) + fFunction(xFinal))*0.5 ; |
---|
| 119 | G4double sum = fFunction(xPlus) ; |
---|
| 120 | for(G4int i=1;i<iterationNumber;i++) |
---|
| 121 | { |
---|
| 122 | x += Step ; |
---|
| 123 | xPlus += Step ; |
---|
| 124 | mean += fFunction(x) ; |
---|
| 125 | sum += fFunction(xPlus) ; |
---|
| 126 | } |
---|
| 127 | mean += 2.0*sum ; |
---|
| 128 | return mean*Step/3.0 ; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | // Adaptive Gauss integration |
---|
| 134 | |
---|
| 135 | G4double |
---|
| 136 | G4SimpleIntegration::AdaptGaussIntegration( G4double xInitial, |
---|
| 137 | G4double xFinal ) |
---|
| 138 | { |
---|
| 139 | G4int depth = 0 ; |
---|
| 140 | G4double sum = 0.0 ; |
---|
| 141 | AdaptGauss(xInitial,xFinal,sum,depth) ; |
---|
| 142 | return sum ; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | G4double |
---|
| 147 | G4SimpleIntegration::Gauss( G4double xInitial, |
---|
| 148 | G4double xFinal ) |
---|
| 149 | { |
---|
| 150 | static G4double root = 1.0/std::sqrt(3.0) ; |
---|
| 151 | |
---|
| 152 | G4double xMean = (xInitial + xFinal)/2.0 ; |
---|
| 153 | G4double Step = (xFinal - xInitial)/2.0 ; |
---|
| 154 | G4double delta = Step*root ; |
---|
| 155 | G4double sum = (fFunction(xMean + delta) + fFunction(xMean - delta)) ; |
---|
| 156 | |
---|
| 157 | return sum*Step ; |
---|
| 158 | } |
---|
| 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 | } |
---|
| 186 | } |
---|