Geant4  10.01.p01
G4ParticleHPVector.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 // neutron_hp -- source file
27 // J.P. Wellisch, Nov-1996
28 // A prototype of the low energy neutron transport model.
29 //
30 // 070523 bug fix for G4FPE_DEBUG on by A. Howard ( and T. Koi)
31 // 080808 bug fix in Sample() and GetXsec() by T. Koi
32 //
33 // P. Arce, June-2014 Conversion neutron_hp to particle_hp
34 //
35 #include "G4ParticleHPVector.hh"
36 #include "G4SystemOfUnits.hh"
37 
38  // if the ranges do not match, constant extrapolation is used.
40  {
42  G4int j=0;
43  G4double x;
44  G4double y;
45  G4int running = 0;
46  for(G4int i=0; i<left.GetVectorLength(); i++)
47  {
48  while(j<right.GetVectorLength())
49  {
50  if(right.GetX(j)<left.GetX(i)*1.001)
51  {
52  x = right.GetX(j);
53  y = right.GetY(j)+left.GetY(x);
54  result->SetData(running++, x, y);
55  j++;
56  }
57  //else if(std::abs((right.GetX(j)-left.GetX(i))/(left.GetX(i)+right.GetX(j)))>0.001)
58  else if( left.GetX(i)+right.GetX(j) == 0
59  || std::abs((right.GetX(j)-left.GetX(i))/(left.GetX(i)+right.GetX(j))) > 0.001 )
60  {
61  x = left.GetX(i);
62  y = left.GetY(i)+right.GetY(x);
63  result->SetData(running++, x, y);
64  break;
65  }
66  else
67  {
68  break;
69  }
70  }
71  if(j==right.GetVectorLength())
72  {
73  x = left.GetX(i);
74  y = left.GetY(i)+right.GetY(x);
75  result->SetData(running++, x, y);
76  }
77  }
78  result->ThinOut(0.02);
79  return *result;
80  }
81 
83  {
84  theData = new G4ParticleHPDataPoint[20];
85  nPoints=20;
86  nEntries=0;
87  Verbose=0;
88  theIntegral=0;
89  totalIntegral=-1;
90  isFreed = 0;
91  maxValue = -DBL_MAX;
94  label = -DBL_MAX;
95 
96  }
97 
99  {
100  nPoints=std::max(n, 20);
102  nEntries=0;
103  Verbose=0;
104  theIntegral=0;
105  totalIntegral=-1;
106  isFreed = 0;
107  maxValue = -DBL_MAX;
110  }
111 
113  {
114 // if(Verbose==1)G4cout <<"G4ParticleHPVector::~G4ParticleHPVector"<<G4endl;
115  delete [] theData;
116 // if(Verbose==1)G4cout <<"Vector: delete theData"<<G4endl;
117  delete [] theIntegral;
118 // if(Verbose==1)G4cout <<"Vector: delete theIntegral"<<G4endl;
119  theHash.Clear();
120  isFreed = 1;
121  }
122 
125  {
126  if(&right == this) return *this;
127 
128  G4int i;
129 
131  if(right.theIntegral!=0) theIntegral = new G4double[right.nEntries];
132  for(i=0; i<right.nEntries; i++)
133  {
134  SetPoint(i, right.GetPoint(i)); // copy theData
135  if(right.theIntegral!=0) theIntegral[i] = right.theIntegral[i];
136  }
137  theManager = right.theManager;
138  label = right.label;
139 
140  Verbose = right.Verbose;
143  theHash = right.theHash;
144  return *this;
145  }
146 
147 
149  {
150  if(nEntries == 0) return 0;
151  if(!theHash.Prepared()) Hash();
153  G4int i;
154  for(i=min ; i<nEntries; i++)
155  {
156  //if(theData[i].GetX()>e) break;
157  if(theData[i].GetX() >= e) break;
158  }
159  G4int low = i-1;
160  G4int high = i;
161  if(i==0)
162  {
163  low = 0;
164  high = 1;
165  }
166  else if(i==nEntries)
167  {
168  low = nEntries-2;
169  high = nEntries-1;
170  }
171  G4double y;
172  if(e<theData[nEntries-1].GetX())
173  {
174  // Protect against doubled-up x values
175  //if( (theData[high].GetX()-theData[low].GetX())/theData[high].GetX() < 0.000001)
176  if ( theData[high].GetX() !=0
177  //080808 TKDB
178  //&&( theData[high].GetX()-theData[low].GetX())/theData[high].GetX() < 0.000001)
179  &&( std::abs( (theData[high].GetX()-theData[low].GetX())/theData[high].GetX() ) < 0.000001 ) )
180  {
181  y = theData[low].GetY();
182  }
183  else
184  {
185  y = theInt.Interpolate(theManager.GetScheme(high), e,
186  theData[low].GetX(), theData[high].GetX(),
187  theData[low].GetY(), theData[high].GetY());
188  }
189  }
190  else
191  {
192  y=theData[nEntries-1].GetY();
193  }
194  return y;
195  }
196 
198  {
199  G4cout << nEntries<<G4endl;
200  for(G4int i=0; i<nEntries; i++)
201  {
202  G4cout << theData[i].GetX()<<" ";
203  G4cout << theData[i].GetY()<<" ";
204 // if (i!=1&&i==5*(i/5)) G4cout << G4endl;
205  G4cout << G4endl;
206  }
207  G4cout << G4endl;
208  }
209 
211  {
212  if(i>nEntries) throw G4HadronicException(__FILE__, __LINE__, "Skipped some index numbers in G4ParticleHPVector");
213  if(i==nPoints)
214  {
215  nPoints = static_cast<G4int>(1.2*nPoints);
217  for (G4int j=0; j<nEntries; j++) buff[j] = theData[j];
218  delete [] theData;
219  theData = buff;
220  }
221  if(i==nEntries) nEntries=i+1;
222  }
223 
227  {
228  // interpolate between labels according to aScheme, cut at aValue,
229  // continue in unknown areas by substraction of the last difference.
230 
231  CleanUp();
232  G4int s_tmp = 0, n=0, m_tmp=0;
233  G4ParticleHPVector * tmp;
234  G4int a = s_tmp, p = n, t;
235  while ( a<active->GetVectorLength() )
236  {
237  if(active->GetEnergy(a) <= passive->GetEnergy(p))
238  {
239  G4double xa = active->GetEnergy(a);
240  G4double yy = theInt.Interpolate(aScheme, aValue, active->GetLabel(), passive->GetLabel(),
241  active->GetXsec(a), passive->GetXsec(xa));
242  SetData(m_tmp, xa, yy);
243  theManager.AppendScheme(m_tmp, active->GetScheme(a));
244  m_tmp++;
245  a++;
246  G4double xp = passive->GetEnergy(p);
247  //if( std::abs(std::abs(xp-xa)/xa)<0.0000001&&a<active->GetVectorLength() )
248  if ( xa != 0
249  && std::abs(std::abs(xp-xa)/xa) < 0.0000001
250  && a < active->GetVectorLength() )
251  {
252  p++;
253  tmp = active; t=a;
254  active = passive; a=p;
255  passive = tmp; p=t;
256  }
257  } else {
258  tmp = active; t=a;
259  active = passive; a=p;
260  passive = tmp; p=t;
261  }
262  }
263 
264  G4double deltaX = passive->GetXsec(GetEnergy(m_tmp-1)) - GetXsec(m_tmp-1);
265  while (p!=passive->GetVectorLength()&&passive->GetEnergy(p)<=aValue)
266  {
267  G4double anX;
268  anX = passive->GetXsec(p)-deltaX;
269  if(anX>0)
270  {
271  //if(std::abs(GetEnergy(m-1)-passive->GetEnergy(p))/passive->GetEnergy(p)>0.0000001)
272  if ( passive->GetEnergy(p) == 0
273  || std::abs(GetEnergy(m_tmp-1)-passive->GetEnergy(p))/passive->GetEnergy(p) > 0.0000001 )
274  {
275  SetData(m_tmp, passive->GetEnergy(p), anX);
276  theManager.AppendScheme(m_tmp++, passive->GetScheme(p));
277  }
278  }
279  p++;
280  }
281  // Rebuild the Hash;
282  if(theHash.Prepared())
283  {
284  ReHash();
285  }
286  }
287 
289  {
290  // anything in there?
291  if(GetVectorLength()==0) return;
292  // make the new vector
294  G4double x, x1, x2, y, y1, y2;
295  G4int count = 0, current = 2, start = 1;
296 
297  // First element always goes and is never tested.
298  aBuff[0] = theData[0];
299 
300  // Find the rest
301  while(current < GetVectorLength())
302  {
303  x1=aBuff[count].GetX();
304  y1=aBuff[count].GetY();
305  x2=theData[current].GetX();
306  y2=theData[current].GetY();
307  for(G4int j=start; j<current; j++)
308  {
309  x = theData[j].GetX();
310  if(x1-x2 == 0) y = (y2+y1)/2.;
311  else y = theInt.Lin(x, x1, x2, y1, y2);
312  if (std::abs(y-theData[j].GetY())>precision*y)
313  {
314  aBuff[++count] = theData[current-1]; // for this one, everything was fine
315  start = current; // the next candidate
316  break;
317  }
318  }
319  current++ ;
320  }
321  // The last one also always goes, and is never tested.
322  aBuff[++count] = theData[GetVectorLength()-1];
323  delete [] theData;
324  theData = aBuff;
325  nEntries = count+1;
326 
327  // Rebuild the Hash;
328  if(theHash.Prepared())
329  {
330  ReHash();
331  }
332  }
333 
335  {
336  G4bool result = false;
337  std::vector<G4double>::iterator i;
338  for(i=theBlocked.begin(); i!=theBlocked.end(); i++)
339  {
340  G4double aBlock = *i;
341  if(std::abs(aX-aBlock) < 0.1*MeV)
342  {
343  result = true;
344  theBlocked.erase(i);
345  break;
346  }
347  }
348  return result;
349  }
350 
351  G4double G4ParticleHPVector::Sample() // Samples X according to distribution Y
352  {
353  G4double result;
354  G4int j;
355  for(j=0; j<GetVectorLength(); j++)
356  {
357  if(GetY(j)<0) SetY(j, 0);
358  }
359 
360  if(theBuffered.size() !=0 && G4UniformRand()<0.5)
361  {
362  result = theBuffered[0];
363  theBuffered.erase(theBuffered.begin());
364  if(result < GetX(GetVectorLength()-1) ) return result;
365  }
366  if(GetVectorLength()==1)
367  {
368  result = theData[0].GetX();
369  }
370  else
371  {
372  if(theIntegral==0) { IntegrateAndNormalise(); }
373  do
374  {
375 //080808
376 /*
377  G4double rand;
378  G4double value, test, baseline;
379  baseline = theData[GetVectorLength()-1].GetX()-theData[0].GetX();
380  do
381  {
382  value = baseline*G4UniformRand();
383  value += theData[0].GetX();
384  test = GetY(value)/maxValue;
385  rand = G4UniformRand();
386  }
387  //while(test<rand);
388  while( test < rand && test > 0 );
389  result = value;
390 */
391  G4double rand;
392  G4double value, test;
393  do
394  {
395  rand = G4UniformRand();
396  G4int ibin = -1;
397  for ( G4int i = 0 ; i < GetVectorLength() ; i++ )
398  {
399  if ( rand < theIntegral[i] )
400  {
401  ibin = i;
402  break;
403  }
404  }
405  if ( ibin < 0 ) G4cout << "TKDB 080807 " << rand << G4endl;
406  // result
407  rand = G4UniformRand();
408  G4double x1, x2;
409  if ( ibin == 0 )
410  {
411  x1 = theData[ ibin ].GetX();
412  value = x1;
413  break;
414  }
415  else
416  {
417  x1 = theData[ ibin-1 ].GetX();
418  }
419 
420  x2 = theData[ ibin ].GetX();
421  value = rand * ( x2 - x1 ) + x1;
422  //***********************************************************************
423  /*
424  test = GetY ( value ) / std::max ( GetY( ibin-1 ) , GetY ( ibin ) );
425  */
426  //***********************************************************************
427  //EMendoza - Always linear interpolation:
428  G4double y1=theData[ ibin-1 ].GetY();
429  G4double y2=theData[ ibin ].GetY();
430  G4double mval=(y2-y1)/(x2-x1);
431  G4double bval=y1-mval*x1;
432  test =(mval*value+bval)/std::max ( GetY( ibin-1 ) , GetY ( ibin ) );
433  //***********************************************************************
434  }
435  while ( G4UniformRand() > test );
436  result = value;
437 //080807
438  }
439  while(IsBlocked(result));
440  }
441  return result;
442  }
443 
445  {
447  G4double result;
448  if(GetVectorLength()==1)
449  {
450  result = theData[0].GetX();
451  the15percentBorderCash = result;
452  }
453  else
454  {
455  if(theIntegral==0) { IntegrateAndNormalise(); }
456  G4int i;
457  result = theData[GetVectorLength()-1].GetX();
458  for(i=0;i<GetVectorLength();i++)
459  {
460  if(theIntegral[i]/theIntegral[GetVectorLength()-1]>0.15)
461  {
462  result = theData[std::min(i+1, GetVectorLength()-1)].GetX();
463  the15percentBorderCash = result;
464  break;
465  }
466  }
467  the15percentBorderCash = result;
468  }
469  return result;
470  }
471 
473  {
475  G4double result;
476  if(GetVectorLength()==1)
477  {
478  result = theData[0].GetX();
479  the50percentBorderCash = result;
480  }
481  else
482  {
483  if(theIntegral==0) { IntegrateAndNormalise(); }
484  G4int i;
485  G4double x = 0.5;
486  result = theData[GetVectorLength()-1].GetX();
487  for(i=0;i<GetVectorLength();i++)
488  {
490  {
491  G4int it;
492  it = i;
493  if(it == GetVectorLength()-1)
494  {
495  result = theData[GetVectorLength()-1].GetX();
496  }
497  else
498  {
499  G4double x1, x2, y1, y2;
500  x1 = theIntegral[i-1]/theIntegral[GetVectorLength()-1];
502  y1 = theData[i-1].GetX();
503  y2 = theData[i].GetX();
504  result = theLin.Lin(x, x1, x2, y1, y2);
505  }
506  the50percentBorderCash = result;
507  break;
508  }
509  }
510  the50percentBorderCash = result;
511  }
512  return result;
513  }
G4double GetEnergy(G4int i) const
static const double MeV
Definition: G4SIunits.hh:193
G4int GetVectorLength() const
G4bool Prepared() const
G4double Lin(G4double x, G4double x1, G4double x2, G4double y1, G4double y2)
void SetData(G4int i, G4double x, G4double y)
G4ParticleHPInterpolator theInt
G4double a
Definition: TRTMaterials.hh:39
G4ParticleHPVector & operator=(const G4ParticleHPVector &right)
int G4int
Definition: G4Types.hh:78
G4double GetXsec(G4int i)
G4ParticleHPHash theHash
G4InterpolationManager theManager
void SetY(G4int i, G4double x)
void AppendScheme(G4int aPoint, const G4InterpolationScheme &aScheme)
G4ParticleHPVector & operator+(G4ParticleHPVector &left, G4ParticleHPVector &right)
#define G4UniformRand()
Definition: Randomize.hh:95
G4GLOB_DLL std::ostream G4cout
G4ParticleHPDataPoint * theData
bool G4bool
Definition: G4Types.hh:79
G4InterpolationScheme GetScheme(G4int index) const
void Merge(G4ParticleHPVector *active, G4ParticleHPVector *passive)
G4double GetX(G4int i) const
void SetPoint(G4int i, const G4ParticleHPDataPoint &it)
G4double GetY(G4double x)
const G4int n
G4double Interpolate(G4InterpolationScheme aScheme, G4double x, G4double x1, G4double x2, G4double y1, G4double y2) const
G4bool IsBlocked(G4double aX)
G4InterpolationScheme
T max(const T t1, const T t2)
brief Return the largest of the two arguments
std::vector< G4double > theBlocked
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
#define G4endl
Definition: G4ios.hh:61
G4ParticleHPInterpolator theLin
std::vector< G4double > theBuffered
double G4double
Definition: G4Types.hh:76
G4int GetMinIndex(G4double e) const
G4InterpolationScheme GetScheme(G4int anIndex)
const G4ParticleHPDataPoint & GetPoint(G4int i) const
#define DBL_MAX
Definition: templates.hh:83
void ThinOut(G4double precision)