Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4SimpleIntegration.cc
Go to the documentation of this file.
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$
28 //
29 // Implementation file for simple integration methods
30 //
31 
32 #include "globals.hh"
33 #include "G4SimpleIntegration.hh"
34 
35 
36 G4int G4SimpleIntegration::fMaxDepth = 100 ;
37 
38 
40  : fFunction(pFunction),
41  fTolerance(.0001)
42 {
43 }
44 
46  G4double pTolerance)
47  : fFunction(pFunction),
48  fTolerance(pTolerance)
49 {
50 }
51 
52 
54 {
55 }
56 
57  // Simple integration methods
58 
61  G4double xFinal,
62  G4int iterationNumber )
63 {
64  G4double Step = (xFinal - xInitial)/iterationNumber ;
65  G4double mean = (fFunction(xInitial) + fFunction(xFinal))*0.5 ;
66  G4double x = xInitial ;
67  for(G4int i=1;i<iterationNumber;i++)
68  {
69  x += Step ;
70  mean += fFunction(x) ;
71  }
72  return mean*Step ;
73 }
74 
75 G4double
77  G4double xFinal,
78  G4int iterationNumber )
79 {
80  G4double Step = (xFinal - xInitial)/iterationNumber ;
81  G4double x = xInitial + 0.5*Step;
82  G4double mean = fFunction(x) ;
83  for(G4int i=1;i<iterationNumber;i++)
84  {
85  x += Step ;
86  mean += fFunction(x) ;
87  }
88  return mean*Step ;
89 }
90 
91 G4double
93  G4double xFinal,
94  G4int iterationNumber )
95 {
96  G4double x=0.;
97  static G4double root = 1.0/std::sqrt(3.0) ;
98  G4double Step = (xFinal - xInitial)/(2.0*iterationNumber) ;
99  G4double delta = Step*root ;
100  G4double mean = 0.0 ;
101  for(G4int i=0;i<iterationNumber;i++)
102  {
103  x = (2*i + 1)*Step ;
104  mean += (fFunction(x+delta) + fFunction(x-delta)) ;
105  }
106  return mean*Step ;
107 }
108 
109 G4double
111  G4double xFinal,
112  G4int iterationNumber )
113 {
114  G4double Step = (xFinal - xInitial)/iterationNumber ;
115  G4double x = xInitial ;
116  G4double xPlus = xInitial + 0.5*Step ;
117  G4double mean = (fFunction(xInitial) + fFunction(xFinal))*0.5 ;
118  G4double sum = fFunction(xPlus) ;
119  for(G4int i=1;i<iterationNumber;i++)
120  {
121  x += Step ;
122  xPlus += Step ;
123  mean += fFunction(x) ;
124  sum += fFunction(xPlus) ;
125  }
126  mean += 2.0*sum ;
127  return mean*Step/3.0 ;
128 }
129 
130 
131 
132  // Adaptive Gauss integration
133 
134 G4double
136  G4double xFinal )
137 {
138  G4int depth = 0 ;
139  G4double sum = 0.0 ;
140  AdaptGauss(xInitial,xFinal,sum,depth) ;
141  return sum ;
142 }
143 
144 
145 G4double
147  G4double xFinal )
148 {
149  static G4double root = 1.0/std::sqrt(3.0) ;
150 
151  G4double xMean = (xInitial + xFinal)/2.0 ;
152  G4double Step = (xFinal - xInitial)/2.0 ;
153  G4double delta = Step*root ;
154  G4double sum = (fFunction(xMean + delta) + fFunction(xMean - delta)) ;
155 
156  return sum*Step ;
157 }
158 
159 
160 void
162  G4double xFinal,
163  G4double& sum,
164  G4int& depth )
165 {
166  if(depth >fMaxDepth)
167  {
168  G4Exception("G4SimpleIntegration::AdaptGauss()", "Error",
169  FatalException, "Function varies too rapidly !") ;
170  }
171  G4double xMean = (xInitial + xFinal)/2.0 ;
172  G4double leftHalf = Gauss(xInitial,xMean) ;
173  G4double rightHalf = Gauss(xMean,xFinal) ;
174  G4double full = Gauss(xInitial,xFinal) ;
175  if(std::fabs(leftHalf+rightHalf-full) < fTolerance)
176  {
177  sum += full ;
178  }
179  else
180  {
181  depth++ ;
182  AdaptGauss(xInitial,xMean,sum,depth) ;
183  AdaptGauss(xMean,xFinal,sum,depth) ;
184  }
185 }