Geant4  10.01.p01
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();
150  if ( !theHash.Prepared() ) {
151  if ( G4Threading::IsWorkerThread() ) {
152  ;
153  } else {
154  Hash();
155  }
156  }
157 
159  G4int i;
160  for(i=min ; i<nEntries; i++)
161  {
162  //if(theData[i].GetX()>e) break;
163  if(theData[i].GetX() >= e) break;
164  }
165  G4int low = i-1;
166  G4int high = i;
167  if(i==0)
168  {
169  low = 0;
170  high = 1;
171  }
172  else if(i==nEntries)
173  {
174  low = nEntries-2;
175  high = nEntries-1;
176  }
177  G4double y;
178  if(e<theData[nEntries-1].GetX())
179  {
180  // Protect against doubled-up x values
181  //if( (theData[high].GetX()-theData[low].GetX())/theData[high].GetX() < 0.000001)
182  if ( theData[high].GetX() !=0
183  //080808 TKDB
184  //&&( theData[high].GetX()-theData[low].GetX())/theData[high].GetX() < 0.000001)
185  &&( std::abs( (theData[high].GetX()-theData[low].GetX())/theData[high].GetX() ) < 0.000001 ) )
186  {
187  y = theData[low].GetY();
188  }
189  else
190  {
191  y = theInt.Interpolate(theManager.GetScheme(high), e,
192  theData[low].GetX(), theData[high].GetX(),
193  theData[low].GetY(), theData[high].GetY());
194  }
195  }
196  else
197  {
198  y=theData[nEntries-1].GetY();
199  }
200  return y;
201  }
202 
204  {
205  G4cout << nEntries<<G4endl;
206  for(G4int i=0; i<nEntries; i++)
207  {
208  G4cout << theData[i].GetX()<<" ";
209  G4cout << theData[i].GetY()<<" ";
210 // if (i!=1&&i==5*(i/5)) G4cout << G4endl;
211  G4cout << G4endl;
212  }
213  G4cout << G4endl;
214  }
215 
217  {
218  if(i>nEntries) throw G4HadronicException(__FILE__, __LINE__, "Skipped some index numbers in G4NeutronHPVector");
219  if(i==nPoints)
220  {
221  nPoints = static_cast<G4int>(1.2*nPoints);
223  for (G4int j=0; j<nEntries; j++) buff[j] = theData[j];
224  delete [] theData;
225  theData = buff;
226  }
227  if(i==nEntries) nEntries=i+1;
228  }
229 
233  {
234  // interpolate between labels according to aScheme, cut at aValue,
235  // continue in unknown areas by substraction of the last difference.
236 
237  CleanUp();
238  G4int s_tmp = 0, n=0, m_tmp=0;
239  G4NeutronHPVector * tmp;
240  G4int a = s_tmp, p = n, t;
241  while ( a<active->GetVectorLength() )
242  {
243  if(active->GetEnergy(a) <= passive->GetEnergy(p))
244  {
245  G4double xa = active->GetEnergy(a);
246  G4double yy = theInt.Interpolate(aScheme, aValue, active->GetLabel(), passive->GetLabel(),
247  active->GetXsec(a), passive->GetXsec(xa));
248  SetData(m_tmp, xa, yy);
249  theManager.AppendScheme(m_tmp, active->GetScheme(a));
250  m_tmp++;
251  a++;
252  G4double xp = passive->GetEnergy(p);
253  //if( std::abs(std::abs(xp-xa)/xa)<0.0000001&&a<active->GetVectorLength() )
254  if ( xa != 0
255  && std::abs(std::abs(xp-xa)/xa) < 0.0000001
256  && a < active->GetVectorLength() )
257  {
258  p++;
259  tmp = active; t=a;
260  active = passive; a=p;
261  passive = tmp; p=t;
262  }
263  } else {
264  tmp = active; t=a;
265  active = passive; a=p;
266  passive = tmp; p=t;
267  }
268  }
269 
270  G4double deltaX = passive->GetXsec(GetEnergy(m_tmp-1)) - GetXsec(m_tmp-1);
271  while (p!=passive->GetVectorLength()&&passive->GetEnergy(p)<=aValue)
272  {
273  G4double anX;
274  anX = passive->GetXsec(p)-deltaX;
275  if(anX>0)
276  {
277  //if(std::abs(GetEnergy(m-1)-passive->GetEnergy(p))/passive->GetEnergy(p)>0.0000001)
278  if ( passive->GetEnergy(p) == 0
279  || std::abs(GetEnergy(m_tmp-1)-passive->GetEnergy(p))/passive->GetEnergy(p) > 0.0000001 )
280  {
281  SetData(m_tmp, passive->GetEnergy(p), anX);
282  theManager.AppendScheme(m_tmp++, passive->GetScheme(p));
283  }
284  }
285  p++;
286  }
287  // Rebuild the Hash;
288  if(theHash.Prepared())
289  {
290  ReHash();
291  }
292  }
293 
295  {
296  // anything in there?
297  if(GetVectorLength()==0) return;
298  // make the new vector
300  G4double x, x1, x2, y, y1, y2;
301  G4int count = 0, current = 2, start = 1;
302 
303  // First element always goes and is never tested.
304  aBuff[0] = theData[0];
305 
306  // Find the rest
307  while(current < GetVectorLength())
308  {
309  x1=aBuff[count].GetX();
310  y1=aBuff[count].GetY();
311  x2=theData[current].GetX();
312  y2=theData[current].GetY();
313  for(G4int j=start; j<current; j++)
314  {
315  x = theData[j].GetX();
316  if(x1-x2 == 0) y = (y2+y1)/2.;
317  else y = theInt.Lin(x, x1, x2, y1, y2);
318  if (std::abs(y-theData[j].GetY())>precision*y)
319  {
320  aBuff[++count] = theData[current-1]; // for this one, everything was fine
321  start = current; // the next candidate
322  break;
323  }
324  }
325  current++ ;
326  }
327  // The last one also always goes, and is never tested.
328  aBuff[++count] = theData[GetVectorLength()-1];
329  delete [] theData;
330  theData = aBuff;
331  nEntries = count+1;
332 
333  // Rebuild the Hash;
334  if(theHash.Prepared())
335  {
336  ReHash();
337  }
338  }
339 
341  {
342  G4bool result = false;
343  std::vector<G4double>::iterator i;
344  for(i=theBlocked.begin(); i!=theBlocked.end(); i++)
345  {
346  G4double aBlock = *i;
347  if(std::abs(aX-aBlock) < 0.1*MeV)
348  {
349  result = true;
350  theBlocked.erase(i);
351  break;
352  }
353  }
354  return result;
355  }
356 
357  G4double G4NeutronHPVector::Sample() // Samples X according to distribution Y
358  {
359  G4double result;
360  G4int j;
361  for(j=0; j<GetVectorLength(); j++)
362  {
363  if(GetY(j)<0) SetY(j, 0);
364  }
365 
366  if(theBuffered.size() !=0 && G4UniformRand()<0.5)
367  {
368  result = theBuffered[0];
369  theBuffered.erase(theBuffered.begin());
370  if(result < GetX(GetVectorLength()-1) ) return result;
371  }
372  if(GetVectorLength()==1)
373  {
374  result = theData[0].GetX();
375  }
376  else
377  {
378  if(theIntegral==0) { IntegrateAndNormalise(); }
379  do
380  {
381 //080808
382 /*
383  G4double rand;
384  G4double value, test, baseline;
385  baseline = theData[GetVectorLength()-1].GetX()-theData[0].GetX();
386  do
387  {
388  value = baseline*G4UniformRand();
389  value += theData[0].GetX();
390  test = GetY(value)/maxValue;
391  rand = G4UniformRand();
392  }
393  //while(test<rand);
394  while( test < rand && test > 0 );
395  result = value;
396 */
397  G4double rand;
398  G4double value, test;
399  do
400  {
401  rand = G4UniformRand();
402  G4int ibin = -1;
403  for ( G4int i = 0 ; i < GetVectorLength() ; i++ )
404  {
405  if ( rand < theIntegral[i] )
406  {
407  ibin = i;
408  break;
409  }
410  }
411  if ( ibin < 0 ) G4cout << "TKDB 080807 " << rand << G4endl;
412  // result
413  rand = G4UniformRand();
414  G4double x1, x2;
415  if ( ibin == 0 )
416  {
417  x1 = theData[ ibin ].GetX();
418  value = x1;
419  break;
420  }
421  else
422  {
423  x1 = theData[ ibin-1 ].GetX();
424  }
425 
426  x2 = theData[ ibin ].GetX();
427  value = rand * ( x2 - x1 ) + x1;
428  //***********************************************************************
429  /*
430  test = GetY ( value ) / std::max ( GetY( ibin-1 ) , GetY ( ibin ) );
431  */
432  //***********************************************************************
433  //EMendoza - Always linear interpolation:
434  G4double y1=theData[ ibin-1 ].GetY();
435  G4double y2=theData[ ibin ].GetY();
436  G4double mval=(y2-y1)/(x2-x1);
437  G4double bval=y1-mval*x1;
438  test =(mval*value+bval)/std::max ( GetY( ibin-1 ) , GetY ( ibin ) );
439  //***********************************************************************
440  }
441  while ( G4UniformRand() > test );
442  result = value;
443 //080807
444  }
445  while(IsBlocked(result));
446  }
447  return result;
448  }
449 
451  {
453  G4double result;
454  if(GetVectorLength()==1)
455  {
456  result = theData[0].GetX();
457  the15percentBorderCash = result;
458  }
459  else
460  {
461  if(theIntegral==0) { IntegrateAndNormalise(); }
462  G4int i;
463  result = theData[GetVectorLength()-1].GetX();
464  for(i=0;i<GetVectorLength();i++)
465  {
466  if(theIntegral[i]/theIntegral[GetVectorLength()-1]>0.15)
467  {
468  result = theData[std::min(i+1, GetVectorLength()-1)].GetX();
469  the15percentBorderCash = result;
470  break;
471  }
472  }
473  the15percentBorderCash = result;
474  }
475  return result;
476  }
477 
479  {
481  G4double result;
482  if(GetVectorLength()==1)
483  {
484  result = theData[0].GetX();
485  the50percentBorderCash = result;
486  }
487  else
488  {
489  if(theIntegral==0) { IntegrateAndNormalise(); }
490  G4int i;
491  G4double x = 0.5;
492  result = theData[GetVectorLength()-1].GetX();
493  for(i=0;i<GetVectorLength();i++)
494  {
496  {
497  G4int it;
498  it = i;
499  if(it == GetVectorLength()-1)
500  {
501  result = theData[GetVectorLength()-1].GetX();
502  }
503  else
504  {
505  G4double x1, x2, y1, y2;
506  x1 = theIntegral[i-1]/theIntegral[GetVectorLength()-1];
508  y1 = theData[i-1].GetX();
509  y2 = theData[i].GetX();
510  result = theLin.Lin(x, x1, x2, y1, y2);
511  }
512  the50percentBorderCash = result;
513  break;
514  }
515  }
516  the50percentBorderCash = result;
517  }
518  return result;
519  }
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:95
G4GLOB_DLL std::ostream G4cout
G4double Get15percentBorder()
bool G4bool
Definition: G4Types.hh:79
G4InterpolationScheme GetScheme(G4int index) const
const G4int n
G4InterpolationManager theManager
G4bool IsWorkerThread()
Definition: G4Threading.cc:128
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