source: trunk/source/processes/hadronic/models/cascade/cascade/include/G4CascadeInterpolator.icc @ 1316

Last change on this file since 1316 was 1316, checked in by garnier, 14 years ago

update geant4-09-04-beta-cand-01 interfaces-V09-03-09 vis-V09-03-08

File size: 4.3 KB
Line 
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// $Id: G4CascadeInterpolator.icc,v 1.5 2010/06/08 06:29:34 stesting Exp $
26// GEANT4 tag $Name: geant4-09-04-beta-cand-01 $
27//
28// Author:  Michael Kelsey <kelsey@slac.stanford.edu>
29//
30// Simple linear interpolation class, more lightweight than
31// G4PhysicsVector.  Templated on number of X-axis (usually energy)
32// bins, constructor takes a C-array of bin edges as input, and an
33// optional flag whether to truncate or extrapolate (the default) values
34// beyond the bin boundaries.
35//
36// The interpolation action returns a simple double: the integer part
37// is the bin index, and the fractional part is, obviously, the
38// fractional part.
39//
40// 20100517  M. Kelsey -- Bug fix in interpolate:  If bin position is _exactly_
41//              at upper edge (== last + 0.0), just return bin value.
42// 20100520  M. Kelsey -- Second bug fix:  Loop in bin search should start at
43//              i=1, not i=0 (since i-1 is the key).
44
45#include "G4CascadeInterpolator.hh"
46
47
48// Find bin position (index and fraction) using input argument and array
49
50template <int NBINS>
51G4double G4CascadeInterpolator<NBINS>::getBin(const G4double x) const {
52  if (x == lastX) return lastVal;       // Avoid unnecessary work
53
54  G4double xindex, xdiff, xbin;
55
56  lastX = x;
57  if (x < xBins[0]) {                   // Handle boundaries first
58    xindex = 0.;
59    xbin = xBins[1]-xBins[0];
60    xdiff = doExtrapolation ? x-xBins[0] : 0.;          // Less than zero
61  } else if (x >= xBins[last]) {
62    xindex = last;
63    xbin = xBins[last]-xBins[last-1];
64    xdiff = doExtrapolation ? x-xBins[last] : 0.;
65  } else {                              // Assume nBins small; linear search
66    int i;
67    for (i=1; i<nBins && x>xBins[i]; i++) {;}   // Stops when x within bin i-1
68    xindex = i-1;
69    xbin = xBins[i] - xBins[i-1];
70    xdiff = x - xBins[i-1];
71  }
72
73#ifdef G4CASCADE_DEBUG_SAMPLER
74  G4cout << " G4CascadeInterpolator<" << NBINS << ">: x=" << x
75         << " index=" << xindex << " fraction=" << xdiff/xbin << G4endl;
76#endif
77
78  return (lastVal = xindex + xdiff/xbin);       // Save return value for later
79}
80
81
82// Apply interpolation of input argument to user array
83
84template <int NBINS>
85G4double G4CascadeInterpolator<NBINS>::
86interpolate(const G4double x, const G4double (&yb)[nBins]) const {
87  getBin(x);
88  return interpolate(yb);
89}
90
91// Apply last found interpolation to user array
92
93template <int NBINS>
94G4double G4CascadeInterpolator<NBINS>::
95interpolate(const G4double (&yb)[nBins]) const {
96  // Treat boundary extrapolations specially, otherwise just truncate
97  G4int i = (lastVal<0) ? 0 : (lastVal>last) ? last-1 : G4int(lastVal);
98  G4double frac = lastVal - G4double(i);        // May be <0 or >1 if extrapolating
99
100  // Special case:  if exactly on upper bin edge, just return value
101  return (i==last) ? yb[last] : (yb[i] + frac*(yb[i+1]-yb[i]));
102}
Note: See TracBrowser for help on using the repository browser.