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