G4AdjointInterpolator.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 // $Id: G4AdjointInterpolator.cc 69844 2013-05-16 09:19:33Z gcosmo $
00027 //
00028 #include "G4AdjointCSMatrix.hh"
00029 #include "G4AdjointInterpolator.hh"
00030 
00031 G4AdjointInterpolator* G4AdjointInterpolator::theInstance = 0;
00033 //
00034 G4AdjointInterpolator* G4AdjointInterpolator::GetAdjointInterpolator()
00035 { if(theInstance == 0) {
00036     static G4AdjointInterpolator interpolator;
00037      theInstance = &interpolator;
00038   }
00039  return theInstance; 
00040 }
00042 //
00043 G4AdjointInterpolator* G4AdjointInterpolator::GetInstance()
00044 { if(theInstance == 0) {
00045     static G4AdjointInterpolator interpolator;
00046      theInstance = &interpolator;
00047   }
00048  return theInstance; 
00049 }
00050 
00052 //
00053 G4AdjointInterpolator::G4AdjointInterpolator()
00054 {;
00055 }
00057 //
00058 G4AdjointInterpolator::~G4AdjointInterpolator()
00059 {;
00060 }
00062 //
00063 G4double G4AdjointInterpolator::LinearInterpolation(G4double& x,G4double& x1,G4double& x2,G4double& y1,G4double& y2)
00064 { G4double res = y1+ (x-x1)*(y2-y1)/(x2-x1);
00065   //G4cout<<"Linear "<<res<<G4endl;
00066   return res;   
00067 }
00069 //
00070 G4double G4AdjointInterpolator::LogarithmicInterpolation(G4double& x,G4double& x1,G4double& x2,G4double& y1,G4double& y2)
00071 { if (y1<=0 || y2<=0 || x1<=0) return LinearInterpolation(x,x1,x2,y1,y2);
00072   G4double B=std::log(y2/y1)/std::log(x2/x1);
00073   //G4cout<<"x1,x2,y1,y2 "<<x1<<'\t'<<x2<<'\t'<<y1<<'\t'<<y2<<'\t'<<G4endl;
00074   G4double A=y1/std::pow(x1,B);
00075   G4double res=A*std::pow(x,B);
00076  // G4cout<<"Log "<<res<<G4endl;
00077   return res;
00078 }
00080 //
00081 G4double G4AdjointInterpolator::ExponentialInterpolation(G4double& x,G4double& x1,G4double& x2,G4double& y1,G4double& y2)
00082 { G4double B=(std::log(y2)-std::log(y1));
00083   B=B/(x2-x1);
00084   G4double A=y1*std::exp(-B*x1);
00085   G4double res=A*std::exp(B*x);
00086   return res;
00087 }
00089 //
00090 G4double G4AdjointInterpolator::Interpolation(G4double& x,G4double& x1,G4double& x2,G4double& y1,G4double& y2,G4String InterPolMethod)
00091 {
00092   if (InterPolMethod == "Log" ){
00093         return LogarithmicInterpolation(x,x1,x2,y1,y2);
00094   }
00095   else if (InterPolMethod == "Lin" ){
00096         return LinearInterpolation(x,x1,x2,y1,y2);
00097   }
00098   else if (InterPolMethod == "Exp" ){
00099         return ExponentialInterpolation(x,x1,x2,y1,y2);
00100   }
00101   else {
00102         //G4cout<<"The interpolation method that you invoked does not exist!"<<G4endl; 
00103         return -1111111111.;
00104   }
00105 }
00107 //
00108 size_t  G4AdjointInterpolator::FindPosition(G4double& x,std::vector<G4double>& x_vec,size_t , size_t ) //only valid if x_vec is monotically increasing
00109 {  //most rapid nethod could be used probably
00110    //It is important to put std::vector<G4double>& such that the vector itself is used and not a copy
00111   
00112   
00113   size_t ndim = x_vec.size();
00114   size_t ind1 = 0;
00115   size_t ind2 = ndim - 1;
00116  /* if (ind_max >= ind_min){
00117         ind1=ind_min;
00118         ind2=ind_max;
00119         
00120   
00121   }
00122   */
00123   
00124   
00125   if (ndim >1) {
00126         
00127         if (x_vec[0] < x_vec[1] ) { //increasing
00128                 do {
00129                         size_t midBin = (ind1 + ind2)/2;
00130                         if (x < x_vec[midBin])
00131                                         ind2 = midBin;
00132                         else 
00133                                         ind1 = midBin;
00134                 } while (ind2 - ind1 > 1);
00135         }
00136         else {
00137                 do {
00138                         size_t midBin = (ind1 + ind2)/2;
00139                         if (x < x_vec[midBin])
00140                                         ind1 = midBin;
00141                         else 
00142                                         ind2 = midBin;
00143                 } while (ind2 - ind1 > 1);
00144         }
00145   
00146   }
00147         
00148   return ind1;
00149 }
00150 
00152 //
00153 size_t  G4AdjointInterpolator::FindPositionForLogVector(G4double& log_x,std::vector<G4double>& log_x_vec) //only valid if x_vec is monotically increasing
00154 {  //most rapid nethod could be used probably
00155    //It is important to put std::vector<G4double>& such that the vector itself is used and not a copy
00156   return FindPosition(log_x, log_x_vec);
00157   /*
00158   if (log_x_vec.size()>3){ 
00159         size_t ind=0;
00160         G4double log_x1=log_x_vec[1];
00161         G4double d_log =log_x_vec[2]-log_x1;
00162         G4double dind=(log_x-log_x1)/d_log +1.;
00163         if (dind <1.) ind=0;
00164         else if (dind >= double(log_x_vec.size())-2.) ind =log_x_vec.size()-2;
00165         else ind =size_t(dind);
00166         return ind;
00167         
00168   }
00169   else  return FindPosition(log_x, log_x_vec);
00170   */
00171   
00172  
00173 }
00175 //
00176 G4double G4AdjointInterpolator::Interpolate(G4double& x,std::vector<G4double>& x_vec,std::vector<G4double>& y_vec,G4String InterPolMethod)
00177 { size_t i=FindPosition(x,x_vec);
00178   //G4cout<<i<<G4endl;
00179   //G4cout<<x<<G4endl;
00180   //G4cout<<x_vec[i]<<G4endl; 
00181   return Interpolation( x,x_vec[i],x_vec[i+1],y_vec[i],y_vec[i+1],InterPolMethod);
00182 }
00183 
00185 //
00186 G4double G4AdjointInterpolator::InterpolateWithIndexVector(G4double& x,std::vector<G4double>& x_vec,std::vector<G4double>& y_vec,
00187                                             std::vector<size_t>& index_vec,G4double x0, G4double dx) //only linear interpolation possible
00188 { size_t ind=0;
00189   if (x>x0) ind=int((x-x0)/dx);
00190   if (ind >= index_vec.size()-1) ind= index_vec.size()-2;
00191   size_t ind1 = index_vec[ind];
00192   size_t ind2 = index_vec[ind+1];
00193   if (ind1 >ind2) {
00194         size_t ind11=ind1;
00195         ind1=ind2;
00196         ind2=ind11;
00197   
00198   }
00199   
00200   ind=FindPosition(x,x_vec,ind1,ind2);
00201   return Interpolation( x,x_vec[ind],x_vec[ind+1],y_vec[ind],y_vec[ind+1],"Lin");
00202   
00203 }                                           
00204                                               
00205 
00207 //
00208 G4double G4AdjointInterpolator::InterpolateForLogVector(G4double& log_x,std::vector<G4double>& log_x_vec,std::vector<G4double>& log_y_vec)
00209 { //size_t i=0;
00210   size_t i=FindPositionForLogVector(log_x,log_x_vec);
00211   /*G4cout<<"In interpolate "<<G4endl;
00212   G4cout<<i<<G4endl;
00213   G4cout<<log_x<<G4endl;
00214   G4cout<<log_x_vec[i]<<G4endl;
00215   G4cout<<log_x_vec[i+1]<<G4endl;
00216   G4cout<<log_y_vec[i]<<G4endl;
00217   G4cout<<log_y_vec[i+1]<<G4endl;*/
00218   
00219   G4double log_y=LinearInterpolation(log_x,log_x_vec[i],log_x_vec[i+1],log_y_vec[i],log_y_vec[i+1]);
00220   return log_y; 
00221  
00222 }  

Generated on Mon May 27 17:47:37 2013 for Geant4 by  doxygen 1.4.7