source: trunk/source/processes/electromagnetic/adjoint/src/G4AdjointInterpolator.cc@ 1036

Last change on this file since 1036 was 966, checked in by garnier, 17 years ago

fichier ajoutes

File size: 7.5 KB
RevLine 
[966]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#include "G4AdjointCSMatrix.hh"
27#include "G4AdjointInterpolator.hh"
28
29G4AdjointInterpolator* G4AdjointInterpolator::theInstance = 0;
30///////////////////////////////////////////////////////
31//
32G4AdjointInterpolator* G4AdjointInterpolator::GetAdjointInterpolator()
33{ if(theInstance == 0) {
34 static G4AdjointInterpolator interpolator;
35 theInstance = &interpolator;
36 }
37 return theInstance;
38}
39///////////////////////////////////////////////////////
40//
41G4AdjointInterpolator* G4AdjointInterpolator::GetInstance()
42{ if(theInstance == 0) {
43 static G4AdjointInterpolator interpolator;
44 theInstance = &interpolator;
45 }
46 return theInstance;
47}
48
49///////////////////////////////////////////////////////
50//
51G4AdjointInterpolator::G4AdjointInterpolator()
52{;
53}
54///////////////////////////////////////////////////////
55//
56G4AdjointInterpolator::~G4AdjointInterpolator()
57{;
58}
59///////////////////////////////////////////////////////
60//
61G4double G4AdjointInterpolator::LinearInterpolation(G4double& x,G4double& x1,G4double& x2,G4double& y1,G4double& y2)
62{ G4double res = y1+ (x-x1)*(y2-y1)/(x2-x1);
63 //G4cout<<"Linear "<<res<<std::endl;
64 return res;
65}
66///////////////////////////////////////////////////////
67//
68G4double G4AdjointInterpolator::LogarithmicInterpolation(G4double& x,G4double& x1,G4double& x2,G4double& y1,G4double& y2)
69{ if (y1<=0 || y2<=0 || x1<=0) return LinearInterpolation(x,x1,x2,y1,y2);
70 G4double B=std::log(y2/y1)/std::log(x2/x1);
71 //G4cout<<"x1,x2,y1,y2 "<<x1<<'\t'<<x2<<'\t'<<y1<<'\t'<<y2<<'\t'<<std::endl;
72 G4double A=y1/std::pow(x1,B);
73 G4double res=A*std::pow(x,B);
74 // G4cout<<"Log "<<res<<std::endl;
75 return res;
76}
77///////////////////////////////////////////////////////
78//
79G4double G4AdjointInterpolator::ExponentialInterpolation(G4double& x,G4double& x1,G4double& x2,G4double& y1,G4double& y2)
80{ G4double B=(std::log(y2)-std::log(y1));
81 B=B/(x2-x1);
82 G4double A=y1*std::exp(-B*x1);
83 G4double res=A*std::exp(B*x);
84 return res;
85}
86///////////////////////////////////////////////////////
87//
88G4double G4AdjointInterpolator::Interpolation(G4double& x,G4double& x1,G4double& x2,G4double& y1,G4double& y2,G4String InterPolMethod)
89{
90 if (InterPolMethod == "Log" ){
91 return LogarithmicInterpolation(x,x1,x2,y1,y2);
92 }
93 else if (InterPolMethod == "Lin" ){
94 return LinearInterpolation(x,x1,x2,y1,y2);
95 }
96 else if (InterPolMethod == "Exp" ){
97 return ExponentialInterpolation(x,x1,x2,y1,y2);
98 }
99 else {
100 //G4cout<<"The interpolation method that you invoked does not exist!"<<std::endl;
101 return -1111111111.;
102 }
103}
104///////////////////////////////////////////////////////
105//
106size_t G4AdjointInterpolator::FindPosition(G4double& x,std::vector<double>& x_vec,size_t , size_t ) //only valid if x_vec is monotically increasing
107{ //most rapid nethod could be used probably
108 //It is important to put std::vector<double>& such that the vector itself is used and not a copy
109
110
111 size_t ndim = x_vec.size();
112 size_t ind1 = 0;
113 size_t ind2 = ndim - 1;
114 /* if (ind_max >= ind_min){
115 ind1=ind_min;
116 ind2=ind_max;
117
118
119 }
120 */
121
122
123 if (ndim >1) {
124
125 if (x_vec[0] < x_vec[1] ) { //increasing
126 do {
127 size_t midBin = (ind1 + ind2)/2;
128 if (x < x_vec[midBin])
129 ind2 = midBin;
130 else
131 ind1 = midBin;
132 } while (ind2 - ind1 > 1);
133 }
134 else {
135 do {
136 size_t midBin = (ind1 + ind2)/2;
137 if (x < x_vec[midBin])
138 ind1 = midBin;
139 else
140 ind2 = midBin;
141 } while (ind2 - ind1 > 1);
142 }
143
144 }
145
146 return ind1;
147}
148
149///////////////////////////////////////////////////////
150//
151size_t G4AdjointInterpolator::FindPositionForLogVector(G4double& log_x,std::vector<double>& log_x_vec) //only valid if x_vec is monotically increasing
152{ //most rapid nethod could be used probably
153 //It is important to put std::vector<double>& such that the vector itself is used and not a copy
154
155 if (log_x_vec.size()>3){
156 size_t ind=0;
157 G4double log_x1=log_x_vec[1];
158 G4double d_log =log_x_vec[2]-log_x1;
159 G4double dind=(log_x-log_x1)/d_log +1.;
160 if (dind <1.) ind=0;
161 else if (dind >= double(log_x_vec.size())-2.) ind =log_x_vec.size()-2;
162 else ind =size_t(dind);
163 return ind;
164
165 }
166 else return FindPosition(log_x, log_x_vec);
167
168
169}
170///////////////////////////////////////////////////////
171//
172G4double G4AdjointInterpolator::Interpolate(G4double& x,std::vector<double>& x_vec,std::vector<double>& y_vec,G4String InterPolMethod)
173{ size_t i=FindPosition(x,x_vec);
174 //G4cout<<i<<std::endl;
175 //G4cout<<x<<std::endl;
176 //G4cout<<x_vec[i]<<std::endl;
177 return Interpolation( x,x_vec[i],x_vec[i+1],y_vec[i],y_vec[i+1],InterPolMethod);
178}
179
180///////////////////////////////////////////////////////
181//
182G4double G4AdjointInterpolator::InterpolateWithIndexVector(G4double& x,std::vector<double>& x_vec,std::vector<double>& y_vec,
183 std::vector<size_t>& index_vec,G4double x0, G4double dx) //only linear interpolation possible
184{ size_t ind=0;
185 if (x>x0) ind=int((x-x0)/dx);
186 if (ind >= index_vec.size()-1) ind= index_vec.size()-2;
187 size_t ind1 = index_vec[ind];
188 size_t ind2 = index_vec[ind+1];
189 if (ind1 >ind2) {
190 size_t ind11=ind1;
191 ind1=ind2;
192 ind2=ind11;
193
194 }
195
196 ind=FindPosition(x,x_vec,ind1,ind2);
197 return Interpolation( x,x_vec[ind],x_vec[ind+1],y_vec[ind],y_vec[ind+1],"Lin");
198
199}
200
201
202///////////////////////////////////////////////////////
203//
204G4double G4AdjointInterpolator::InterpolateForLogVector(G4double& log_x,std::vector<double>& log_x_vec,std::vector<double>& log_y_vec)
205{ //size_t i=0;
206 size_t i=FindPositionForLogVector(log_x,log_x_vec);
207 /*G4cout<<"In interpolate "<<std::endl;
208 G4cout<<i<<std::endl;
209 G4cout<<log_x<<std::endl;
210 G4cout<<log_x_vec[i]<<std::endl;
211 G4cout<<log_x_vec[i+1]<<std::endl;
212 G4cout<<log_y_vec[i]<<std::endl;
213 G4cout<<log_y_vec[i+1]<<std::endl;*/
214
215 G4double log_y=LinearInterpolation(log_x,log_x_vec[i],log_x_vec[i+1],log_y_vec[i],log_y_vec[i+1]);
216 return log_y;
217
218}
Note: See TracBrowser for help on using the repository browser.