ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4AdjointInterpolator.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4AdjointInterpolator.cc
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 #include "G4AdjointCSMatrix.hh"
28 #include "G4AdjointInterpolator.hh"
29 
32 //
34 {
35  return GetInstance();
36 }
37 
39 //
41 {
42  if(!theInstance)
43  {
45  }
46  return theInstance;
47 }
48 
50 //
52 {
53 }
54 
56 //
58 {
59 }
60 
62 //
64 {
65  G4double res = y1+ (x-x1)*(y2-y1)/(x2-x1);
66  //G4cout<<"Linear "<<res<<G4endl;
67  return res;
68 }
69 
71 //
73 {
74  if (y1<=0 || y2<=0 || x1<=0) return LinearInterpolation(x,x1,x2,y1,y2);
75  G4double B=std::log(y2/y1)/std::log(x2/x1);
76  //G4cout<<"x1,x2,y1,y2 "<<x1<<'\t'<<x2<<'\t'<<y1<<'\t'<<y2<<'\t'<<G4endl;
77  G4double A=y1/std::pow(x1,B);
78  G4double res=A*std::pow(x,B);
79  // G4cout<<"Log "<<res<<G4endl;
80  return res;
81 }
82 
84 //
86 {
87  G4double B=(std::log(y2)-std::log(y1));
88  B=B/(x2-x1);
89  G4double A=y1*std::exp(-B*x1);
90  G4double res=A*std::exp(B*x);
91  return res;
92 }
93 
95 //
97 {
98  if (InterPolMethod == "Log" ){
99  return LogarithmicInterpolation(x,x1,x2,y1,y2);
100  }
101  else if (InterPolMethod == "Lin" ){
102  return LinearInterpolation(x,x1,x2,y1,y2);
103  }
104  else if (InterPolMethod == "Exp" ){
105  return ExponentialInterpolation(x,x1,x2,y1,y2);
106  }
107  else {
108  //G4cout<<"The interpolation method that you invoked does not exist!"<<G4endl;
109  return -1111111111.;
110  }
111 }
112 
114 //
115 size_t G4AdjointInterpolator::FindPosition(G4double& x,std::vector<G4double>& x_vec,size_t , size_t ) //only valid if x_vec is monotically increasing
116 {
117  //most rapid nethod could be used probably
118  //It is important to put std::vector<G4double>& such that the vector itself is used and not a copy
119 
120 
121  size_t ndim = x_vec.size();
122  size_t ind1 = 0;
123  size_t ind2 = ndim - 1;
124  /* if (ind_max >= ind_min){
125  ind1=ind_min;
126  ind2=ind_max;
127 
128 
129  }
130  */
131 
132 
133  if (ndim >1) {
134 
135  if (x_vec[0] < x_vec[1] ) { //increasing
136  do {
137  size_t midBin = (ind1 + ind2)/2;
138  if (x < x_vec[midBin])
139  ind2 = midBin;
140  else
141  ind1 = midBin;
142  // Loop checking, 07-Aug-2015, Vladimir Ivanchenko
143  } while (ind2 - ind1 > 1);
144  }
145  else {
146  do {
147  size_t midBin = (ind1 + ind2)/2;
148  if (x < x_vec[midBin])
149  ind1 = midBin;
150  else
151  ind2 = midBin;
152  // Loop checking, 07-Aug-2015, Vladimir Ivanchenko
153  } while (ind2 - ind1 > 1);
154  }
155 
156  }
157 
158  return ind1;
159 }
160 
162 //
163 size_t G4AdjointInterpolator::FindPositionForLogVector(G4double& log_x,std::vector<G4double>& log_x_vec) //only valid if x_vec is monotically increasing
164 {
165  //most rapid nethod could be used probably
166  //It is important to put std::vector<G4double>& such that the vector itself is used and not a copy
167  return FindPosition(log_x, log_x_vec);
168  /*
169  if (log_x_vec.size()>3){
170  size_t ind=0;
171  G4double log_x1=log_x_vec[1];
172  G4double d_log =log_x_vec[2]-log_x1;
173  G4double dind=(log_x-log_x1)/d_log +1.;
174  if (dind <1.) ind=0;
175  else if (dind >= double(log_x_vec.size())-2.) ind =log_x_vec.size()-2;
176  else ind =size_t(dind);
177  return ind;
178 
179  }
180  else return FindPosition(log_x, log_x_vec);
181  */
182 
183 
184 }
185 
187 //
188 G4double G4AdjointInterpolator::Interpolate(G4double& x,std::vector<G4double>& x_vec,std::vector<G4double>& y_vec,G4String InterPolMethod)
189 {
190  size_t i=FindPosition(x,x_vec);
191  //G4cout<<i<<G4endl;
192  //G4cout<<x<<G4endl;
193  //G4cout<<x_vec[i]<<G4endl;
194  return Interpolation( x,x_vec[i],x_vec[i+1],y_vec[i],y_vec[i+1],InterPolMethod);
195 }
196 
198 //
199 G4double G4AdjointInterpolator::InterpolateWithIndexVector(G4double& x,std::vector<G4double>& x_vec,std::vector<G4double>& y_vec,
200  std::vector<size_t>& index_vec,G4double x0, G4double dx) //only linear interpolation possible
201 {
202  size_t ind=0;
203  if (x>x0) ind=int((x-x0)/dx);
204  if (ind >= index_vec.size()-1) ind= index_vec.size()-2;
205  size_t ind1 = index_vec[ind];
206  size_t ind2 = index_vec[ind+1];
207  if (ind1 >ind2) {
208  size_t ind11=ind1;
209  ind1=ind2;
210  ind2=ind11;
211 
212  }
213  ind=FindPosition(x,x_vec,ind1,ind2);
214  return Interpolation( x,x_vec[ind],x_vec[ind+1],y_vec[ind],y_vec[ind+1],"Lin");
215 }
216 
218 //
219 G4double G4AdjointInterpolator::InterpolateForLogVector(G4double& log_x,std::vector<G4double>& log_x_vec,std::vector<G4double>& log_y_vec)
220 {
221  //size_t i=0;
222  size_t i=FindPositionForLogVector(log_x,log_x_vec);
223  /*G4cout<<"In interpolate "<<G4endl;
224  G4cout<<i<<G4endl;
225  G4cout<<log_x<<G4endl;
226  G4cout<<log_x_vec[i]<<G4endl;
227  G4cout<<log_x_vec[i+1]<<G4endl;
228  G4cout<<log_y_vec[i]<<G4endl;
229  G4cout<<log_y_vec[i+1]<<G4endl;*/
230 
231  G4double log_y=LinearInterpolation(log_x,log_x_vec[i],log_x_vec[i+1],log_y_vec[i],log_y_vec[i+1]);
232  return log_y;
233 }