source: trunk/source/global/management/src/G4PhysicsVector.cc @ 916

Last change on this file since 916 was 850, checked in by garnier, 16 years ago

geant4.8.2 beta

File size: 8.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//
26//
27// $Id: G4PhysicsVector.cc,v 1.22 2008/09/05 18:04:45 vnivanch Exp $
28// GEANT4 tag $Name: HEAD $
29//
30//
31// --------------------------------------------------------------
32//      GEANT 4 class implementation file
33//
34//  G4PhysicsVector.cc
35//
36//  History:
37//    02 Dec. 1995, G.Cosmo : Structure created based on object model
38//    03 Mar. 1996, K.Amako : Implemented the 1st version
39//    01 Jul. 1996, K.Amako : Hidden bin from the user introduced
40//    12 Nov. 1998, K.Amako : A bug in GetVectorLength() fixed
41//    11 Nov. 2000, H.Kurashige : use STL vector for dataVector and binVector
42//    18 Jan. 2001, H.Kurashige : removed ptrNextTable
43//    09 Mar. 2001, H.Kurashige : added G4PhysicsVector type
44//    05 Sep. 2008, V.Ivanchenko : added protections for zero-length vector
45// --------------------------------------------------------------
46
47#include "G4PhysicsVector.hh"
48#include <iomanip>
49
50// --------------------------------------------------------------
51
52G4PhysicsVector::G4PhysicsVector(G4bool spline)
53 : type(T_G4PhysicsVector),
54   edgeMin(DBL_MAX), edgeMax(0.), numberOfBin(0),
55   lastEnergy(0.), lastValue(0.), lastBin(0), 
56   secDerivative(0), useSpline(spline)
57{
58  binVector.push_back(edgeMin);
59  dataVector.push_back(0.0);
60}
61
62// --------------------------------------------------------------
63
64G4PhysicsVector::~G4PhysicsVector() 
65{
66  delete [] secDerivative;
67}
68
69// --------------------------------------------------------------
70
71G4PhysicsVector::G4PhysicsVector(const G4PhysicsVector& right)
72{
73  *this=right;
74}
75
76// --------------------------------------------------------------
77
78G4PhysicsVector& G4PhysicsVector::operator=(const G4PhysicsVector& right)
79{
80  if (&right==this)  { return *this; }
81  if (type != right.type)  { return *this; }
82
83  type = right.type;
84  edgeMin = right.edgeMin;
85  edgeMax = right.edgeMax;
86  numberOfBin = right.numberOfBin;
87  lastEnergy = right.lastEnergy;
88  lastValue = right.lastValue;
89  lastBin = right.lastBin;
90  dataVector = right.dataVector;
91  binVector = right.binVector;
92  secDerivative = right.secDerivative;
93  useSpline = right.useSpline;
94  comment = right.comment;
95  return *this;
96}
97
98// --------------------------------------------------------------
99
100G4int G4PhysicsVector::operator==(const G4PhysicsVector &right) const
101{
102  return (this == &right);
103}
104
105// --------------------------------------------------------------
106
107G4int G4PhysicsVector::operator!=(const G4PhysicsVector &right) const
108{
109  return (this != &right);
110}
111
112// --------------------------------------------------------------
113
114G4double G4PhysicsVector::GetLowEdgeEnergy(size_t binNumber) const
115{
116  return binVector[binNumber];
117}
118
119// --------------------------------------------------------------
120
121G4bool G4PhysicsVector::Store(std::ofstream& fOut, G4bool ascii)
122{
123  // Ascii mode
124  if (ascii)
125  {
126    fOut << *this;
127    return true;
128  } 
129  // Binary Mode
130
131  // binning
132  fOut.write((char*)(&edgeMin), sizeof edgeMin);
133  fOut.write((char*)(&edgeMax), sizeof edgeMax);
134  fOut.write((char*)(&numberOfBin), sizeof numberOfBin);
135
136  // contents
137  size_t size = dataVector.size(); 
138  fOut.write((char*)(&size), sizeof size);
139
140  G4double* value = new G4double[2*size];
141  for(size_t i = 0; i < size; i++)
142  {
143    value[2*i]  =  binVector[i];
144    value[2*i+1]=  dataVector[i];
145  }
146  fOut.write((char*)(value), 2*size*(sizeof (G4double)));
147  delete [] value;
148
149  return true;
150}
151
152// --------------------------------------------------------------
153
154G4bool G4PhysicsVector::Retrieve(std::ifstream& fIn, G4bool ascii)
155{
156  // clear properties;
157  lastEnergy=0.;
158  lastValue =0.;
159  lastBin   =0;
160  dataVector.clear();
161  binVector.clear();
162  comment = "";
163
164  // retrieve in ascii mode
165  if (ascii)
166  {
167    // binning
168    fIn >> edgeMin >> edgeMax >> numberOfBin; 
169    if (fIn.fail())  { return false; }
170    // contents
171    size_t size=0;
172    fIn >> size;
173    if (fIn.fail())  { return false; }
174
175    binVector.reserve(size);
176    dataVector.reserve(size);
177    G4double vBin, vData;
178
179    for(size_t i = 0; i < size ; i++)
180    {
181      vBin = 0.;
182      vData= 0.;
183      fIn >> vBin >> vData;
184      if (fIn.fail())  { return false; }
185      binVector.push_back(vBin);
186      dataVector.push_back(vData);
187    }
188    return true ;
189  }
190
191  // retrieve in binary mode
192  // binning
193  fIn.read((char*)(&edgeMin), sizeof edgeMin);
194  fIn.read((char*)(&edgeMax), sizeof edgeMax);
195  fIn.read((char*)(&numberOfBin), sizeof numberOfBin ); 
196 
197  // contents
198  size_t size;
199  fIn.read((char*)(&size), sizeof size); 
200 
201  G4double* value = new G4double[2*size];
202  fIn.read((char*)(value),  2*size*(sizeof(G4double)) );
203  if (G4int(fIn.gcount()) != G4int(2*size*(sizeof(G4double))) )
204  {
205    delete [] value;
206    return false;
207  }
208
209  binVector.reserve(size);
210  dataVector.reserve(size);
211  for(size_t i = 0; i < size; i++)
212  {
213    binVector.push_back(value[2*i]);
214    dataVector.push_back(value[2*i+1]);
215  }
216  delete [] value;
217  return true;
218}
219
220// --------------------------------------------------------------
221
222void G4PhysicsVector::FillSecondDerivatives()
223{ 
224  secDerivative = new G4double [numberOfBin];
225
226  secDerivative[0] = 0.0 ;
227
228  // cannot compute derivatives for less than 3 points
229  if(3 > numberOfBin) {
230    secDerivative[numberOfBin-1] = 0.0 ;
231    return;
232  }
233
234  G4double* u = new G4double [numberOfBin];
235  u[0] = 0.0 ;
236   
237  // Decomposition loop for tridiagonal algorithm. secDerivative[i]
238  // and u[i] are used for temporary storage of the decomposed factors.
239   
240  for(size_t i=1; i<numberOfBin-1; i++)
241  {
242    G4double sig = (binVector[i]-binVector[i-1])
243                 / (binVector[i+1]-binVector[i-1]) ;
244    G4double p = sig*secDerivative[i-1] + 2.0 ;
245    secDerivative[i] = (sig - 1.0)/p ;
246    u[i] = (dataVector[i+1]-dataVector[i])/(binVector[i+1]-binVector[i])
247         - (dataVector[i]-dataVector[i-1])/(binVector[i]-binVector[i-1]) ;
248    u[i] =(6.0*u[i]/(binVector[i+1]-binVector[i-1]) - sig*u[i-1])/p ;
249  }
250
251  G4double qn = 0.0 ;
252  G4double un = 0.0 ;
253
254  secDerivative[numberOfBin-1] = (un - qn*u[numberOfBin-2])
255                               / (qn*secDerivative[numberOfBin-2] + 1.0) ;
256   
257  // The back-substitution loop for the triagonal algorithm of solving
258  // a linear system of equations.
259   
260  for(G4int k=numberOfBin-2; k>=0; k--)
261  {
262    secDerivative[k] = secDerivative[k]*secDerivative[k+1] + u[k];
263  }
264  delete [] u;
265}
266   
267// --------------------------------------------------------------
268
269std::ostream& operator<<(std::ostream& out, const G4PhysicsVector& pv)
270{
271  // binning
272  out << std::setprecision(12) << pv.edgeMin;
273  out <<" " << pv.edgeMax <<" "  << pv.numberOfBin << G4endl; 
274
275  // contents
276  out << pv.dataVector.size() << G4endl; 
277  for(size_t i = 0; i < pv.dataVector.size(); i++)
278  {
279    out << std::setprecision(12) << pv.binVector[i] << "  "
280        << pv.dataVector[i] << G4endl;
281  }
282  return out;
283}
Note: See TracBrowser for help on using the repository browser.