source: trunk/source/processes/hadronic/models/neutron_hp/include/G4NeutronHPVector.hh @ 846

Last change on this file since 846 was 819, checked in by garnier, 16 years ago

import all except CVS

File size: 13.9 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// 070606 fix with Valgrind by T. Koi
27// 080409 Fix div0 error with G4FPE by T. Koi
28//
29#ifndef G4NeutronHPVector_h
30#define G4NeutronHPVector_h 1
31
32#include "G4NeutronHPDataPoint.hh"
33#include "G4PhysicsVector.hh"
34#include "G4NeutronHPInterpolator.hh"
35#include "Randomize.hh"
36#include "G4ios.hh"
37#include <fstream>
38#include "G4InterpolationManager.hh"
39#include "G4NeutronHPInterpolator.hh"
40#include "G4NeutronHPHash.hh"
41#include <cmath>
42#include <vector>
43
44class G4NeutronHPVector
45{
46  friend G4NeutronHPVector & operator + (G4NeutronHPVector & left, 
47                                         G4NeutronHPVector & right);
48 
49  public:
50 
51  G4NeutronHPVector();
52
53  G4NeutronHPVector(G4int n);
54 
55  ~G4NeutronHPVector();
56 
57  G4NeutronHPVector & operator = (const G4NeutronHPVector & right);
58 
59  inline void SetVerbose(G4int ff)
60  {
61    Verbose = ff;
62  }
63 
64  inline void Times(G4double factor)
65  {
66    G4int i;
67    for(i=0; i<nEntries; i++)
68    {
69      theData[i].SetY(theData[i].GetY()*factor);
70    }
71    if(theIntegral!=0)
72    {
73      theIntegral[i] *= factor;
74    }
75  }
76 
77  inline void SetPoint(G4int i, const G4NeutronHPDataPoint & it)
78  {
79    G4double x = it.GetX();
80    G4double y = it.GetY();
81    SetData(i, x, y);
82  }
83   
84  inline void SetData(G4int i, G4double x, G4double y) 
85  { 
86//    G4cout <<"G4NeutronHPVector::SetData called"<<nPoints<<" "<<nEntries<<G4endl;
87    Check(i);
88    if(y>maxValue) maxValue=y;
89    theData[i].SetData(x, y);
90  }
91  inline void SetX(G4int i, G4double e)
92  {
93    Check(i);
94    theData[i].SetX(e);
95  }
96  inline void SetEnergy(G4int i, G4double e)
97  {
98    Check(i);
99    theData[i].SetX(e);
100  }
101  inline void SetY(G4int i, G4double x)
102  {
103    Check(i);
104    if(x>maxValue) maxValue=x;
105    theData[i].SetY(x);
106  }
107  inline void SetXsec(G4int i, G4double x)
108  {
109    Check(i);
110    if(x>maxValue) maxValue=x;
111    theData[i].SetY(x);
112  }
113  inline G4double GetEnergy(G4int i) const { return theData[i].GetX(); }
114  inline G4double GetXsec(G4int i) { return theData[i].GetY(); }
115  inline G4double GetX(G4int i) const 
116  { 
117    if (i<0) i=0;
118    if(i>=GetVectorLength()) i=GetVectorLength()-1;
119    return theData[i].GetX();
120  }
121  inline const G4NeutronHPDataPoint & GetPoint(G4int i) const { return theData[i]; }
122 
123  void Hash() 
124  {
125    G4int i;
126    G4double x, y;
127    for(i=0 ; i<nEntries; i++)
128    {
129      if(0 == (i+1)%10)
130      {
131        x = GetX(i);
132        y = GetY(i);
133        theHash.SetData(i, x, y);
134      }
135    }
136  }
137 
138  void ReHash()
139  {
140    theHash.Clear();
141    Hash();
142  }
143 
144  G4double GetXsec(G4double e);
145  G4double GetXsec(G4double e, G4int min)
146  {
147    G4int i;
148    for(i=min ; i<nEntries; i++)
149    {
150      if(theData[i].GetX()>e) break;
151    }
152    G4int low = i-1;
153    G4int high = i;
154    if(i==0)
155    {
156      low = 0;
157      high = 1;
158    }
159    else if(i==nEntries)
160    {
161      low = nEntries-2;
162      high = nEntries-1;
163    }
164    G4double y;
165    if(e<theData[nEntries-1].GetX()) 
166    {
167      // Protect against doubled-up x values
168      if( (theData[high].GetX()-theData[low].GetX())/theData[high].GetX() < 0.000001)
169      {
170        y = theData[low].GetY();
171      }
172      else
173      {
174        y = theInt.Interpolate(theManager.GetScheme(high), e, 
175                               theData[low].GetX(), theData[high].GetX(),
176                               theData[low].GetY(), theData[high].GetY());
177      }
178    }
179    else
180    {
181      y=theData[nEntries-1].GetY();
182    }
183    return y;
184  }
185 
186  inline G4double GetY(G4double x)  {return GetXsec(x);}
187  inline G4int GetVectorLength() const {return nEntries;}
188
189  inline G4double GetY(G4int i)
190  { 
191    if (i<0) i=0;
192    if(i>=GetVectorLength()) i=GetVectorLength()-1;
193    return theData[i].GetY(); 
194  }
195
196  inline G4double GetY(G4int i) const
197  {
198    if (i<0) i=0;
199    if(i>=GetVectorLength()) i=GetVectorLength()-1;
200    return theData[i].GetY(); 
201  }
202  void Dump();
203 
204  inline void InitInterpolation(std::ifstream & aDataFile)
205  {
206    theManager.Init(aDataFile);
207  }
208 
209  void Init(std::ifstream & aDataFile, G4int total, G4double ux=1., G4double uy=1.)
210  {
211    G4double x,y;
212    for (G4int i=0;i<total;i++)
213    {
214      aDataFile >> x >> y;
215      x*=ux;
216      y*=uy;
217      SetData(i,x,y);
218      if(0 == nEntries%10)
219      {
220        theHash.SetData(nEntries-1, x, y);
221      }
222    }
223  }
224 
225  void Init(std::ifstream & aDataFile,G4double ux=1., G4double uy=1.)
226  {
227    G4int total;
228    aDataFile >> total;
229    if(theData!=0) delete [] theData;
230    theData = new G4NeutronHPDataPoint[total]; 
231    nPoints=total;
232    nEntries=0;   
233    theManager.Init(aDataFile);
234    Init(aDataFile, total, ux, uy);
235  }
236 
237  void ThinOut(G4double precision);
238 
239  inline void SetLabel(G4double aLabel)
240  {
241    label = aLabel;
242  }
243 
244  inline G4double GetLabel()
245  {
246    return label;
247  }
248 
249  inline void CleanUp()
250  {
251    nEntries=0;   
252    theManager.CleanUp();
253    maxValue = -DBL_MAX;
254    theHash.Clear();
255  }
256
257  // merges the vectors active and passive into *this
258  inline void Merge(G4NeutronHPVector * active, G4NeutronHPVector * passive)
259  {
260    CleanUp();
261    G4int s = 0, n=0, m=0;
262    G4NeutronHPVector * tmp;
263    G4int a = s, p = n, t;
264    while (a<active->GetVectorLength()&&p<passive->GetVectorLength())
265    {
266      if(active->GetEnergy(a) <= passive->GetEnergy(p))
267      {
268        G4double xa = active->GetEnergy(a);
269        G4double yy = active->GetXsec(a);
270        SetData(m, xa, yy);
271        theManager.AppendScheme(m, active->GetScheme(a));
272        m++;
273        a++;
274        G4double xp = passive->GetEnergy(p);
275
276//080409 TKDB
277        //if( std::abs(std::abs(xp-xa)/xa)<0.001 ) p++;
278        if ( !( xa == 0 ) && std::abs(std::abs(xp-xa)/xa)<0.001 ) p++;
279      } else {
280        tmp = active; 
281        t=a;
282        active = passive; 
283        a=p;
284        passive = tmp; 
285        p=t;
286      }
287    }
288    while (a!=active->GetVectorLength())
289    {
290      SetData(m, active->GetEnergy(a), active->GetXsec(a));
291      theManager.AppendScheme(m++, active->GetScheme(a));
292      a++;
293    }
294    while (p!=passive->GetVectorLength())
295    {
296      if(std::abs(GetEnergy(m-1)-passive->GetEnergy(p))/passive->GetEnergy(p)>0.001)
297      //if(std::abs(GetEnergy(m)-passive->GetEnergy(p))/passive->GetEnergy(p)>0.001)
298      {
299        SetData(m, passive->GetEnergy(p), passive->GetXsec(p));
300        theManager.AppendScheme(m++, active->GetScheme(p));
301      }
302      p++;
303    }
304  }   
305 
306  void Merge(G4InterpolationScheme aScheme, G4double aValue, 
307             G4NeutronHPVector * active, G4NeutronHPVector * passive);
308 
309  G4double SampleLin() // Samples X according to distribution Y, linear int
310  {
311    G4double result;
312    if(theIntegral==0) IntegrateAndNormalise();
313    if(GetVectorLength()==1)
314    {
315      result = theData[0].GetX();
316    }
317    else
318    {
319      G4int i;
320      G4double rand = G4UniformRand();
321     
322      // this was replaced
323//      for(i=1;i<GetVectorLength();i++)
324//      {
325//      if(rand<theIntegral[i]/theIntegral[GetVectorLength()-1]) break;
326//      }
327
328// by this (begin)
329      for(i=GetVectorLength()-1; i>=0 ;i--)
330      {
331        if(rand>theIntegral[i]/theIntegral[GetVectorLength()-1]) break;
332      }
333      if(i!=GetVectorLength()-1) i++;
334// until this (end)
335     
336      G4double x1, x2, y1, y2;
337      y1 = theData[i-1].GetX();
338      x1 = theIntegral[i-1];
339      y2 = theData[i].GetX();
340      x2 = theIntegral[i];
341      if(std::abs((y2-y1)/y2)<0.0000001) // not really necessary, since the case is excluded by construction
342      {
343        y1 = theData[i-2].GetX();
344        x1 = theIntegral[i-2];
345      }
346      result = theLin.Lin(rand, x1, x2, y1, y2);
347    }
348    return result;
349  }
350 
351  G4double Sample(); // Samples X according to distribution Y
352 
353  G4double * Debug()
354  {
355    return theIntegral;
356  }
357
358  inline void IntegrateAndNormalise()
359  {
360    G4int i;
361    if(theIntegral!=0) return;
362    theIntegral = new G4double[nEntries];
363    if(nEntries == 1)
364    {
365      theIntegral[0] = 1;
366      return;
367    }
368    theIntegral[0] = 0;
369    G4double sum = 0;
370    G4double x1 = 0;
371    G4double x0 = 0;
372    for(i=1;i<GetVectorLength();i++)
373    {
374      x1 = theData[i].GetX();
375      x0 = theData[i-1].GetX();
376      if (std::abs(x1-x0) > std::abs(x1*0.0000001) )
377      {
378        sum+= 0.5*(theData[i].GetY()+theData[i-1].GetY())*
379                  (x1-x0);
380      }
381      theIntegral[i] = sum;
382    }
383    G4double total = theIntegral[GetVectorLength()-1];
384    for(i=1;i<GetVectorLength();i++)
385    {
386      theIntegral[i]/=total;
387    }
388  }
389
390  inline void Integrate() 
391  {
392    G4int i;
393    if(nEntries == 1)
394    {
395      totalIntegral = 0;
396      return;
397    }
398    G4double sum = 0;
399    for(i=1;i<GetVectorLength();i++)
400    {
401      if(std::abs((theData[i].GetX()-theData[i-1].GetX())/theData[i].GetX())>0.0000001)
402      {
403        G4double x1 = theData[i-1].GetX();
404        G4double x2 = theData[i].GetX();
405        G4double y1 = theData[i-1].GetY();
406        G4double y2 = theData[i].GetY();
407        G4InterpolationScheme aScheme = theManager.GetScheme(i);
408        if(aScheme==LINLIN||aScheme==CLINLIN||aScheme==ULINLIN)
409        {
410          sum+= 0.5*(y2+y1)*(x2-x1);
411        }
412        else if(aScheme==LINLOG||aScheme==CLINLOG||aScheme==ULINLOG)
413        {
414          G4double a = y1;
415          G4double b = (y2-y1)/(std::log(x2)-std::log(x1));
416          sum+= (a-b)*(x2-x1) + b*(x2*std::log(x2)-x1*std::log(x1));
417        }
418        else if(aScheme==LOGLIN||aScheme==CLOGLIN||aScheme==ULOGLIN)
419        {
420          G4double a = std::log(y1);
421          G4double b = (std::log(y2)-std::log(y1))/(x2-x1);
422          sum += (std::exp(a)/b)*(std::exp(b*x2)-std::exp(b*x1));
423        }
424        else if(aScheme==HISTO||aScheme==CHISTO||aScheme==UHISTO)
425        {
426          sum+= y1*(x2-x1);
427        }
428        else if(aScheme==LOGLOG||aScheme==CLOGLOG||aScheme==ULOGLOG)
429        {
430          G4double a = std::log(y1);
431          G4double b = (std::log(y2)-std::log(y1))/(std::log(x2)-std::log(x1));
432          sum += (std::exp(a)/(b+1))*(std::pow(x2,b+1)-std::pow(x1,b+1));
433        }
434        else
435        {
436          throw G4HadronicException(__FILE__, __LINE__, "Unknown interpolation scheme in G4NeutronHPVector::Integrate");
437        }
438         
439      }
440    }
441    totalIntegral = sum;
442  }
443 
444  inline G4double GetIntegral() // linear interpolation; use with care
445  { 
446    if(totalIntegral<-0.5) Integrate();
447    return totalIntegral; 
448  }
449 
450  inline void SetInterpolationManager(const G4InterpolationManager & aManager)
451  {
452    theManager = aManager;
453  }
454 
455  inline const G4InterpolationManager & GetInterpolationManager() const
456  {
457    return theManager;
458  }
459 
460  inline void SetInterpolationManager(G4InterpolationManager & aMan)
461  {
462    theManager = aMan;
463  }
464 
465  inline void SetScheme(G4int aPoint, const G4InterpolationScheme & aScheme)
466  {
467    theManager.AppendScheme(aPoint, aScheme);
468  }
469 
470  inline G4InterpolationScheme GetScheme(G4int anIndex)
471  {
472    return theManager.GetScheme(anIndex);
473  }
474 
475  G4double GetMeanX()
476  {
477    G4double result;
478    G4double running = 0;
479    G4double weighted = 0;
480    for(G4int i=1; i<nEntries; i++)
481    {
482      running += theInt.GetBinIntegral(theManager.GetScheme(i-1),
483                           theData[i-1].GetX(), theData[i].GetX(),
484                           theData[i-1].GetY(), theData[i].GetY());
485      weighted += theInt.GetWeightedBinIntegral(theManager.GetScheme(i-1),
486                           theData[i-1].GetX(), theData[i].GetX(),
487                           theData[i-1].GetY(), theData[i].GetY());
488    } 
489    result = weighted / running; 
490    return result;
491  }
492 
493  void Block(G4double aX)
494  {
495    theBlocked.push_back(aX);
496  }
497 
498  void Buffer(G4double aX)
499  {
500    theBuffered.push_back(aX);
501  }
502 
503  std::vector<G4double> GetBlocked() {return theBlocked;}
504  std::vector<G4double> GetBuffered() {return theBuffered;}
505 
506  void SetBlocked(const std::vector<G4double> &aBlocked) {theBlocked = aBlocked;}
507  void SetBuffered(const std::vector<G4double> &aBuffer) {theBuffered = aBuffer;}
508
509  G4double Get15percentBorder();
510  G4double Get50percentBorder();
511 
512  private:
513 
514  void Check(G4int i);
515 
516  G4bool IsBlocked(G4double aX);
517 
518  private:
519 
520  G4NeutronHPInterpolator theLin;
521 
522  private:
523 
524  G4double totalIntegral;
525 
526  G4NeutronHPDataPoint * theData; // the data
527  G4InterpolationManager theManager; // knows how to interpolate the data.
528  G4double * theIntegral;
529  G4int nEntries;
530  G4int nPoints;
531  G4double label;
532 
533  G4NeutronHPInterpolator theInt;
534  G4int Verbose;
535  // debug only
536  G4int isFreed;
537 
538  G4NeutronHPHash theHash;
539  G4double maxValue;
540 
541  std::vector<G4double> theBlocked;
542  std::vector<G4double> theBuffered;
543  G4double the15percentBorderCash;
544  G4double the50percentBorderCash;
545
546};
547
548#endif
Note: See TracBrowser for help on using the repository browser.