Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // G4PhysicsVector class implementation << 27 // 26 // 28 // Authors: << 27 // $Id: G4PhysicsVector.cc 93409 2015-10-21 13:26:27Z gcosmo $ 29 // - 02 Dec. 1995, G.Cosmo: Structure created << 28 // 30 // - 03 Mar. 1996, K.Amako: Implemented the 1s << 29 // 31 // Revisions: << 30 // -------------------------------------------------------------- 32 // - 11 Nov. 2000, H.Kurashige: Use STL vector << 31 // GEANT 4 class implementation file 33 // ------------------------------------------- << 32 // >> 33 // G4PhysicsVector.cc >> 34 // >> 35 // History: >> 36 // 02 Dec. 1995, G.Cosmo : Structure created based on object model >> 37 // 03 Mar. 1996, K.Amako : Implemented the 1st version >> 38 // 01 Jul. 1996, K.Amako : Hidden bin from the user introduced >> 39 // 12 Nov. 1998, K.Amako : A bug in GetVectorLength() fixed >> 40 // 11 Nov. 2000, H.Kurashige : use STL vector for dataVector and binVector >> 41 // 18 Jan. 2001, H.Kurashige : removed ptrNextTable >> 42 // 09 Mar. 2001, H.Kurashige : added G4PhysicsVector type >> 43 // 05 Sep. 2008, V.Ivanchenko : added protections for zero-length vector >> 44 // 11 May 2009, A.Bagulya : added new implementation of methods >> 45 // ComputeSecondDerivatives - first derivatives at edge points >> 46 // should be provided by a user >> 47 // FillSecondDerivatives - default computation base on "not-a-knot" >> 48 // algorithm >> 49 // 19 Jun. 2009, V.Ivanchenko : removed hidden bin >> 50 // 17 Nov. 2009, H.Kurashige : use pointer for DataVector >> 51 // 04 May 2010 H.Kurashige : use G4PhyscisVectorCache >> 52 // 28 May 2010 H.Kurashige : Stop using pointers to G4PVDataVector >> 53 // 16 Aug. 2011 H.Kurashige : Add dBin, baseBin and verboseLevel >> 54 // -------------------------------------------------------------- 34 55 35 #include "G4PhysicsVector.hh" << 36 #include <iomanip> 56 #include <iomanip> >> 57 #include "G4PhysicsVector.hh" 37 58 38 // ------------------------------------------- 59 // -------------------------------------------------------------- 39 G4PhysicsVector::G4PhysicsVector(G4bool val) << 40 : useSpline(val) << 41 {} << 42 60 43 // ------------------------------------------- << 61 G4PhysicsVector::G4PhysicsVector(G4bool) 44 void G4PhysicsVector::Initialise() << 62 : type(T_G4PhysicsVector), >> 63 edgeMin(0.), edgeMax(0.), numberOfNodes(0), >> 64 useSpline(false), >> 65 dBin(0.), baseBin(0.), >> 66 verboseLevel(0) 45 { 67 { 46 if (1 < numberOfNodes) << 68 } 47 { << 69 48 idxmax = numberOfNodes - 2; << 70 // -------------------------------------------------------------- 49 edgeMin = binVector[0]; << 71 50 edgeMax = binVector[idxmax + 1]; << 72 G4PhysicsVector::~G4PhysicsVector() >> 73 { >> 74 } >> 75 >> 76 // -------------------------------------------------------------- >> 77 >> 78 G4PhysicsVector::G4PhysicsVector(const G4PhysicsVector& right) >> 79 { >> 80 dBin = right.dBin; >> 81 baseBin = right.baseBin; >> 82 verboseLevel = right.verboseLevel; >> 83 >> 84 DeleteData(); >> 85 CopyData(right); >> 86 } >> 87 >> 88 // -------------------------------------------------------------- >> 89 >> 90 G4PhysicsVector& G4PhysicsVector::operator=(const G4PhysicsVector& right) >> 91 { >> 92 if (&right==this) { return *this; } >> 93 dBin = right.dBin; >> 94 baseBin = right.baseBin; >> 95 verboseLevel = right.verboseLevel; >> 96 >> 97 DeleteData(); >> 98 CopyData(right); >> 99 return *this; >> 100 } >> 101 >> 102 // -------------------------------------------------------------- >> 103 >> 104 G4int G4PhysicsVector::operator==(const G4PhysicsVector &right) const >> 105 { >> 106 return (this == &right); >> 107 } >> 108 >> 109 // -------------------------------------------------------------- >> 110 >> 111 G4int G4PhysicsVector::operator!=(const G4PhysicsVector &right) const >> 112 { >> 113 return (this != &right); >> 114 } >> 115 >> 116 // -------------------------------------------------------------- >> 117 >> 118 void G4PhysicsVector::DeleteData() >> 119 { >> 120 useSpline = false; >> 121 secDerivative.clear(); >> 122 } >> 123 >> 124 // -------------------------------------------------------------- >> 125 >> 126 void G4PhysicsVector::CopyData(const G4PhysicsVector& vec) >> 127 { >> 128 type = vec.type; >> 129 edgeMin = vec.edgeMin; >> 130 edgeMax = vec.edgeMax; >> 131 numberOfNodes = vec.numberOfNodes; >> 132 useSpline = vec.useSpline; >> 133 >> 134 size_t i; >> 135 dataVector.resize(numberOfNodes); >> 136 for(i=0; i<numberOfNodes; ++i) { >> 137 dataVector[i] = (vec.dataVector)[i]; >> 138 } >> 139 binVector.resize(numberOfNodes); >> 140 for(i=0; i<numberOfNodes; ++i) { >> 141 binVector[i] = (vec.binVector)[i]; >> 142 } >> 143 if(0 < (vec.secDerivative).size()) { >> 144 secDerivative.resize(numberOfNodes); >> 145 for(i=0; i<numberOfNodes; ++i){ >> 146 secDerivative[i] = (vec.secDerivative)[i]; >> 147 } 51 } 148 } 52 } 149 } 53 150 54 // ------------------------------------------- 151 // -------------------------------------------------------------- 55 G4bool G4PhysicsVector::Store(std::ofstream& f << 152 >> 153 G4double G4PhysicsVector::GetLowEdgeEnergy(size_t binNumber) const >> 154 { >> 155 return binVector[binNumber]; >> 156 } >> 157 >> 158 // -------------------------------------------------------------- >> 159 >> 160 G4bool G4PhysicsVector::Store(std::ofstream& fOut, G4bool ascii) 56 { 161 { 57 // Ascii mode 162 // Ascii mode 58 if (ascii) 163 if (ascii) 59 { 164 { 60 fOut << *this; 165 fOut << *this; 61 return true; 166 return true; 62 } << 167 } 63 // Binary Mode 168 // Binary Mode 64 169 65 // binning 170 // binning 66 fOut.write((char*) (&edgeMin), sizeof edgeMi << 171 fOut.write((char*)(&edgeMin), sizeof edgeMin); 67 fOut.write((char*) (&edgeMax), sizeof edgeMa << 172 fOut.write((char*)(&edgeMax), sizeof edgeMax); 68 fOut.write((char*) (&numberOfNodes), sizeof << 173 fOut.write((char*)(&numberOfNodes), sizeof numberOfNodes); 69 174 70 // contents 175 // contents 71 std::size_t size = dataVector.size(); << 176 size_t size = dataVector.size(); 72 fOut.write((char*) (&size), sizeof size); << 177 fOut.write((char*)(&size), sizeof size); 73 178 74 auto value = new G4double[2 * size]; << 179 G4double* value = new G4double[2*size]; 75 for (std::size_t i = 0; i < size; ++i) << 180 for(size_t i = 0; i < size; ++i) 76 { 181 { 77 value[2 * i] = binVector[i]; << 182 value[2*i] = binVector[i]; 78 value[2 * i + 1] = dataVector[i]; << 183 value[2*i+1]= dataVector[i]; 79 } 184 } 80 fOut.write((char*) (value), 2 * size * (size << 185 fOut.write((char*)(value), 2*size*(sizeof (G4double))); 81 delete[] value; << 186 delete [] value; 82 187 83 return true; 188 return true; 84 } 189 } 85 190 86 // ------------------------------------------- 191 // -------------------------------------------------------------- >> 192 87 G4bool G4PhysicsVector::Retrieve(std::ifstream 193 G4bool G4PhysicsVector::Retrieve(std::ifstream& fIn, G4bool ascii) 88 { 194 { 89 // clear properties; 195 // clear properties; 90 dataVector.clear(); 196 dataVector.clear(); 91 binVector.clear(); 197 binVector.clear(); 92 secDerivative.clear(); 198 secDerivative.clear(); 93 199 94 // retrieve in ascii mode 200 // retrieve in ascii mode 95 if (ascii) << 201 if (ascii){ 96 { << 97 // binning 202 // binning 98 fIn >> edgeMin >> edgeMax >> numberOfNodes << 203 fIn >> edgeMin >> edgeMax >> numberOfNodes; 99 if (fIn.fail() || numberOfNodes < 2) << 204 if (fIn.fail()) { return false; } 100 { << 101 return false; << 102 } << 103 // contents 205 // contents 104 G4int siz0 = 0; << 206 G4int siz=0; 105 fIn >> siz0; << 207 fIn >> siz; 106 if (siz0 < 2) { return false; } << 208 if (fIn.fail()) { return false; } 107 auto siz = static_cast<std::size_t>(siz0); << 209 if (siz<=0) 108 if (fIn.fail() || siz != numberOfNodes) << 109 { 210 { >> 211 #ifdef G4VERBOSE >> 212 G4cerr << "G4PhysicsVector::Retrieve():"; >> 213 G4cerr << " Invalid vector size: " << siz << G4endl; >> 214 #endif 110 return false; 215 return false; 111 } 216 } 112 217 113 binVector.reserve(siz); 218 binVector.reserve(siz); 114 dataVector.reserve(siz); 219 dataVector.reserve(siz); 115 G4double vBin, vData; 220 G4double vBin, vData; 116 221 117 for (std::size_t i = 0; i < siz; ++i) << 222 for(G4int i = 0; i < siz ; i++) 118 { 223 { 119 vBin = 0.; << 224 vBin = 0.; 120 vData = 0.; << 225 vData= 0.; 121 fIn >> vBin >> vData; 226 fIn >> vBin >> vData; 122 if (fIn.fail()) << 227 if (fIn.fail()) { return false; } 123 { << 124 return false; << 125 } << 126 binVector.push_back(vBin); 228 binVector.push_back(vBin); 127 dataVector.push_back(vData); 229 dataVector.push_back(vData); 128 } 230 } 129 Initialise(); << 231 130 return true; << 232 // to remove any inconsistency >> 233 numberOfNodes = siz; >> 234 edgeMin = binVector[0]; >> 235 edgeMax = binVector[numberOfNodes-1]; >> 236 return true ; 131 } 237 } 132 238 133 // retrieve in binary mode 239 // retrieve in binary mode 134 // binning 240 // binning 135 fIn.read((char*) (&edgeMin), sizeof edgeMin) << 241 fIn.read((char*)(&edgeMin), sizeof edgeMin); 136 fIn.read((char*) (&edgeMax), sizeof edgeMax) << 242 fIn.read((char*)(&edgeMax), sizeof edgeMax); 137 fIn.read((char*) (&numberOfNodes), sizeof nu << 243 fIn.read((char*)(&numberOfNodes), sizeof numberOfNodes ); 138 << 244 139 // contents 245 // contents 140 std::size_t size; << 246 size_t size; 141 fIn.read((char*) (&size), sizeof size); << 247 fIn.read((char*)(&size), sizeof size); 142 << 248 143 auto value = new G4double[2 * size]; << 249 G4double* value = new G4double[2*size]; 144 fIn.read((char*) (value), 2 * size * (sizeof << 250 fIn.read((char*)(value), 2*size*(sizeof(G4double)) ); 145 if (static_cast<G4int>(fIn.gcount()) != stat << 251 if (G4int(fIn.gcount()) != G4int(2*size*(sizeof(G4double))) ) 146 { 252 { 147 delete[] value; << 253 delete [] value; 148 return false; 254 return false; 149 } 255 } 150 256 151 binVector.reserve(size); 257 binVector.reserve(size); 152 dataVector.reserve(size); 258 dataVector.reserve(size); 153 for (std::size_t i = 0; i < size; ++i) << 259 for(size_t i = 0; i < size; ++i) 154 { 260 { 155 binVector.push_back(value[2 * i]); << 261 binVector.push_back(value[2*i]); 156 dataVector.push_back(value[2 * i + 1]); << 262 dataVector.push_back(value[2*i+1]); 157 } 263 } 158 delete[] value; << 264 delete [] value; >> 265 >> 266 // to remove any inconsistency >> 267 numberOfNodes = size; >> 268 edgeMin = binVector[0]; >> 269 edgeMax = binVector[numberOfNodes-1]; 159 270 160 Initialise(); << 161 return true; 271 return true; 162 } 272 } 163 273 164 // ------------------------------------------- 274 // -------------------------------------------------------------- 165 void G4PhysicsVector::DumpValues(G4double unit << 166 { << 167 for (std::size_t i = 0; i < numberOfNodes; + << 168 { << 169 G4cout << binVector[i] / unitE << " " << << 170 << G4endl; << 171 } << 172 } << 173 275 174 // ------------------------------------------- << 276 void 175 std::size_t G4PhysicsVector::FindBin(const G4d << 277 G4PhysicsVector::ScaleVector(G4double factorE, G4double factorV) 176 std::size << 177 { 278 { 178 if (idx + 1 < numberOfNodes && << 279 size_t n = dataVector.size(); 179 energy >= binVector[idx] && energy <= bi << 280 size_t i; 180 { << 281 for(i=0; i<n; ++i) { 181 return idx; << 282 binVector[i] *= factorE; 182 } << 283 dataVector[i] *= factorV; 183 if (energy <= binVector[1]) << 184 { << 185 return 0; << 186 } << 187 if (energy >= binVector[idxmax]) << 188 { << 189 return idxmax; << 190 } 284 } 191 return GetBin(energy); << 285 secDerivative.clear(); 192 } << 193 286 194 // ------------------------------------------- << 287 edgeMin *= factorE; 195 void G4PhysicsVector::ScaleVector(const G4doub << 288 edgeMax *= factorE; 196 const G4doub << 197 { << 198 for (std::size_t i = 0; i < numberOfNodes; + << 199 { << 200 binVector[i] *= factorE; << 201 dataVector[i] *= factorV; << 202 } << 203 Initialise(); << 204 } 289 } 205 290 206 // ------------------------------------------- << 291 // -------------------------------------------------------------- 207 void G4PhysicsVector::FillSecondDerivatives(co << 292 208 const G4double dir1, << 293 void 209 const G4double dir2) << 294 G4PhysicsVector::ComputeSecondDerivatives(G4double firstPointDerivative, >> 295 G4double endPointDerivative) >> 296 // A standard method of computation of second derivatives >> 297 // First derivatives at the first and the last point should be provided >> 298 // See for example W.H. Press et al. "Numerical recipes in C" >> 299 // Cambridge University Press, 1997. 210 { 300 { 211 if (!useSpline) { return; } << 301 if(4 > numberOfNodes) // cannot compute derivatives for less than 4 bins 212 // cannot compute derivatives for less than << 213 const std::size_t nmin = (stype == G4SplineT << 214 if (nmin > numberOfNodes) << 215 { 302 { 216 if (0 < verboseLevel) << 303 ComputeSecDerivatives(); 217 { << 218 G4cout << "### G4PhysicsVector: spline c << 219 << numberOfNodes << " points - spline d << 220 << G4endl; << 221 DumpValues(); << 222 } << 223 useSpline = false; << 224 return; 304 return; 225 } 305 } 226 // check energies of free vector << 227 if (type == T_G4PhysicsFreeVector) << 228 { << 229 for (std::size_t i=0; i<=idxmax; ++i) << 230 { << 231 if (binVector[i + 1] <= binVector[i]) << 232 { << 233 if (0 < verboseLevel) << 234 { << 235 G4cout << "### G4PhysicsVector: spline can << 236 << " E[" << i << "]=" << binVector[i] << 237 << " >= E[" << i+1 << "]=" << binVector[i << 238 << G4endl; << 239 DumpValues(); << 240 } << 241 useSpline = false; << 242 return; << 243 } << 244 } << 245 } << 246 306 247 // spline is possible << 307 if(!SplinePossible()) { return; } 248 Initialise(); << 249 secDerivative.resize(numberOfNodes); << 250 308 251 if (1 < verboseLevel) << 309 useSpline = true; 252 { << 253 G4cout << "### G4PhysicsVector:: FillSecon << 254 << numberOfNodes << G4endl; << 255 DumpValues(); << 256 } << 257 310 258 switch(stype) << 311 G4int n = numberOfNodes-1; 259 { << 260 case G4SplineType::Base: << 261 ComputeSecDerivative1(); << 262 break; << 263 312 264 case G4SplineType::FixedEdges: << 313 G4double* u = new G4double [n]; 265 ComputeSecDerivative2(dir1, dir2); << 314 266 break; << 315 G4double p, sig, un; 267 316 268 default: << 317 u[0] = (6.0/(binVector[1]-binVector[0])) 269 ComputeSecDerivative0(); << 318 * ((dataVector[1]-dataVector[0])/(binVector[1]-binVector[0]) 270 } << 319 - firstPointDerivative); 271 } << 320 >> 321 secDerivative[0] = - 0.5; 272 322 273 // ------------------------------------------- << 323 // Decomposition loop for tridiagonal algorithm. secDerivative[i] 274 void G4PhysicsVector::ComputeSecDerivative0() << 324 // and u[i] are used for temporary storage of the decomposed factors. 275 // A simplified method of computation of seco << 276 { << 277 std::size_t n = numberOfNodes - 1; << 278 325 279 for (std::size_t i = 1; i < n; ++i) << 326 for(G4int i=1; i<n; ++i) 280 { 327 { 281 secDerivative[i] = 3.0 * << 328 sig = (binVector[i]-binVector[i-1]) / (binVector[i+1]-binVector[i-1]); 282 ((dataVector[i + 1] - dataVector[i]) / ( << 329 p = sig*(secDerivative[i-1]) + 2.0; 283 (dataVector[i] - dataVector[i - 1]) / << 330 secDerivative[i] = (sig - 1.0)/p; 284 (binVector[i] - binVector[i - 1])) / << 331 u[i] = (dataVector[i+1]-dataVector[i])/(binVector[i+1]-binVector[i]) 285 (binVector[i + 1] - binVector[i - 1]); << 332 - (dataVector[i]-dataVector[i-1])/(binVector[i]-binVector[i-1]); >> 333 u[i] = 6.0*u[i]/(binVector[i+1]-binVector[i-1]) - sig*u[i-1]/p; >> 334 } >> 335 >> 336 sig = (binVector[n-1]-binVector[n-2]) / (binVector[n]-binVector[n-2]); >> 337 p = sig*secDerivative[n-2] + 2.0; >> 338 un = (6.0/(binVector[n]-binVector[n-1])) >> 339 *(endPointDerivative - >> 340 (dataVector[n]-dataVector[n-1])/(binVector[n]-binVector[n-1])) - u[n-1]/p; >> 341 secDerivative[n] = un/(secDerivative[n-1] + 2.0); >> 342 >> 343 // The back-substitution loop for the triagonal algorithm of solving >> 344 // a linear system of equations. >> 345 >> 346 for(G4int k=n-1; k>0; --k) >> 347 { >> 348 secDerivative[k] *= >> 349 (secDerivative[k+1] - >> 350 u[k]*(binVector[k+1]-binVector[k-1])/(binVector[k+1]-binVector[k])); 286 } 351 } 287 secDerivative[n] = secDerivative[n - 1]; << 352 secDerivative[0] = 0.5*(u[0] - secDerivative[1]); 288 secDerivative[0] = secDerivative[1]; << 353 >> 354 delete [] u; 289 } 355 } 290 356 291 // ------------------------------------------- 357 // -------------------------------------------------------------- 292 void G4PhysicsVector::ComputeSecDerivative1() << 358 293 // Computation of second derivatives using "No << 359 void G4PhysicsVector::FillSecondDerivatives() 294 // B.I. Kvasov "Methods of shape-preserving sp << 360 // Computation of second derivatives using "Not-a-knot" endpoint conditions 295 // World Scientific, 2000 << 361 // B.I. Kvasov "Methods of shape-preserving spline approximation" >> 362 // World Scientific, 2000 296 { 363 { 297 std::size_t n = numberOfNodes - 1; << 364 if(5 > numberOfNodes) // cannot compute derivatives for less than 4 points 298 auto u = new G4double[n]; << 365 { 299 G4double p, sig; << 366 ComputeSecDerivatives(); >> 367 return; >> 368 } >> 369 >> 370 if(!SplinePossible()) { return; } 300 371 301 u[1] = ((dataVector[2] - dataVector[1]) / (b << 372 useSpline = true; 302 (dataVector[1] - dataVector[0]) / (b << 373 303 u[1] = 6.0 * u[1] * (binVector[2] - binVecto << 374 G4int n = numberOfNodes-1; 304 ((binVector[2] - binVector[0]) * (bin << 375 >> 376 G4double* u = new G4double [n]; >> 377 >> 378 G4double p, sig; 305 379 >> 380 u[1] = ((dataVector[2]-dataVector[1])/(binVector[2]-binVector[1]) - >> 381 (dataVector[1]-dataVector[0])/(binVector[1]-binVector[0])); >> 382 u[1] = 6.0*u[1]*(binVector[2]-binVector[1]) >> 383 / ((binVector[2]-binVector[0])*(binVector[2]-binVector[0])); >> 384 306 // Decomposition loop for tridiagonal algori 385 // Decomposition loop for tridiagonal algorithm. secDerivative[i] 307 // and u[i] are used for temporary storage o 386 // and u[i] are used for temporary storage of the decomposed factors. 308 387 309 secDerivative[1] = (2.0 * binVector[1] - bin << 388 secDerivative[1] = (2.0*binVector[1]-binVector[0]-binVector[2]) 310 (2.0 * binVector[2] - bin << 389 / (2.0*binVector[2]-binVector[0]-binVector[1]); 311 390 312 for(std::size_t i = 2; i < n - 1; ++i) << 391 for(G4int i=2; i<n-1; ++i) 313 { 392 { 314 sig = << 393 sig = (binVector[i]-binVector[i-1]) / (binVector[i+1]-binVector[i-1]); 315 (binVector[i] - binVector[i - 1]) / (bin << 394 p = sig*secDerivative[i-1] + 2.0; 316 p = sig * secDerivative[i - << 395 secDerivative[i] = (sig - 1.0)/p; 317 secDerivative[i] = (sig - 1.0) / p; << 396 u[i] = (dataVector[i+1]-dataVector[i])/(binVector[i+1]-binVector[i]) 318 u[i] = << 397 - (dataVector[i]-dataVector[i-1])/(binVector[i]-binVector[i-1]); 319 (dataVector[i + 1] - dataVector[i]) / (b << 398 u[i] = (6.0*u[i]/(binVector[i+1]-binVector[i-1])) - sig*u[i-1]/p; 320 (dataVector[i] - dataVector[i - 1]) / (b << 399 } 321 u[i] = << 400 322 (6.0 * u[i] / (binVector[i + 1] - binVec << 401 sig = (binVector[n-1]-binVector[n-2]) / (binVector[n]-binVector[n-2]); 323 } << 402 p = sig*secDerivative[n-3] + 2.0; 324 << 403 u[n-1] = (dataVector[n]-dataVector[n-1])/(binVector[n]-binVector[n-1]) 325 sig = << 404 - (dataVector[n-1]-dataVector[n-2])/(binVector[n-1]-binVector[n-2]); 326 (binVector[n - 1] - binVector[n - 2]) / (b << 405 u[n-1] = 6.0*sig*u[n-1]/(binVector[n]-binVector[n-2]) 327 p = sig * secDerivative[n - 3] + 2.0; << 406 - (2.0*sig - 1.0)*u[n-2]/p; 328 u[n - 1] = << 329 (dataVector[n] - dataVector[n - 1]) / (bin << 330 (dataVector[n - 1] - dataVector[n - 2]) / << 331 (binVector[n - 1] - binVector[n - 2]); << 332 u[n - 1] = 6.0 * sig * u[n - 1] / (binVector << 333 (2.0 * sig - 1.0) * u[n - 2] / p; << 334 407 335 p = (1.0 + sig) + (2.0 * sig - 1.0) * secDer << 408 p = (1.0+sig) + (2.0*sig-1.0)*secDerivative[n-2]; 336 secDerivative[n - 1] = u[n - 1] / p; << 409 secDerivative[n-1] = u[n-1]/p; 337 410 338 // The back-substitution loop for the triago 411 // The back-substitution loop for the triagonal algorithm of solving 339 // a linear system of equations. 412 // a linear system of equations. 340 << 413 341 for (std::size_t k = n - 2; k > 1; --k) << 414 for(G4int k=n-2; k>1; --k) 342 { 415 { 343 secDerivative[k] *= << 416 secDerivative[k] *= 344 (secDerivative[k + 1] - u[k] * (binVecto << 417 (secDerivative[k+1] - 345 (binVector[k + << 418 u[k]*(binVector[k+1]-binVector[k-1])/(binVector[k+1]-binVector[k])); 346 } << 419 } 347 secDerivative[n] = << 420 secDerivative[n] = (secDerivative[n-1] - (1.0-sig)*secDerivative[n-2])/sig; 348 (secDerivative[n - 1] - (1.0 - sig) * secD << 421 sig = 1.0 - ((binVector[2]-binVector[1])/(binVector[2]-binVector[0])); 349 sig = 1.0 - ((binVector[2] - binVector[1]) / << 422 secDerivative[1] *= (secDerivative[2] - u[1]/(1.0-sig)); 350 secDerivative[1] *= (secDerivative[2] - u[1] << 423 secDerivative[0] = (secDerivative[1] - sig*secDerivative[2])/(1.0-sig); 351 secDerivative[0] = (secDerivative[1] - sig * << 352 424 353 delete[] u; << 425 delete [] u; 354 } 426 } 355 427 356 // ------------------------------------------- 428 // -------------------------------------------------------------- 357 void G4PhysicsVector::ComputeSecDerivative2(G4 << 429 358 G4 << 430 void 359 // A standard method of computation of second << 431 G4PhysicsVector::ComputeSecDerivatives() 360 // First derivatives at the first and the last << 432 // A simplified method of computation of second derivatives 361 // See for example W.H. Press et al. "Numerica << 362 // Cambridge University Press, 1997. << 363 { 433 { 364 std::size_t n = numberOfNodes - 1; << 434 if(3 > numberOfNodes) // cannot compute derivatives for less than 4 bins 365 auto u = new G4double[n]; << 435 { 366 G4double p, sig, un; << 436 useSpline = false; >> 437 return; >> 438 } 367 439 368 u[0] = (6.0 / (binVector[1] - binVector[0])) << 440 if(!SplinePossible()) { return; } 369 ((dataVector[1] - dataVector[0]) / (b << 370 firstPointDerivative); << 371 441 372 secDerivative[0] = -0.5; << 442 useSpline = true; 373 443 374 // Decomposition loop for tridiagonal algori << 444 size_t n = numberOfNodes-1; 375 // and u[i] are used for temporary storage o << 376 445 377 for (std::size_t i = 1; i < n; ++i) << 446 for(size_t i=1; i<n; ++i) 378 { 447 { 379 sig = << 448 secDerivative[i] = 380 (binVector[i] - binVector[i - 1]) / (bin << 449 3.0*((dataVector[i+1]-dataVector[i])/(binVector[i+1]-binVector[i]) - 381 p = sig * (secDerivative[i << 450 (dataVector[i]-dataVector[i-1])/(binVector[i]-binVector[i-1])) 382 secDerivative[i] = (sig - 1.0) / p; << 451 /(binVector[i+1]-binVector[i-1]); 383 u[i] = << 384 (dataVector[i + 1] - dataVector[i]) / (b << 385 (dataVector[i] - dataVector[i - 1]) / (b << 386 u[i] = << 387 6.0 * u[i] / (binVector[i + 1] - binVect << 388 } << 389 << 390 sig = << 391 (binVector[n - 1] - binVector[n - 2]) / (b << 392 p = sig * secDerivative[n - 2] + 2.0; << 393 un = (6.0 / (binVector[n] - binVector[n - 1] << 394 (endPointDerivative - (dataVector[n] << 395 (binVector[n] << 396 u[n - 1] / p; << 397 secDerivative[n] = un / (secDerivative[n - 1 << 398 << 399 // The back-substitution loop for the triago << 400 // a linear system of equations. << 401 << 402 for (std::size_t k = n - 1; k > 0; --k) << 403 { << 404 secDerivative[k] *= << 405 (secDerivative[k + 1] - u[k] * (binVecto << 406 (binVector[k + << 407 } 452 } 408 secDerivative[0] = 0.5 * (u[0] - secDerivati << 453 secDerivative[n] = secDerivative[n-1]; 409 << 454 secDerivative[0] = secDerivative[1]; 410 delete[] u; << 411 } 455 } 412 456 413 // ------------------------------------------- 457 // -------------------------------------------------------------- >> 458 >> 459 G4bool G4PhysicsVector::SplinePossible() >> 460 // Initialise second derivative array. If neighbor energy coincide >> 461 // or not ordered than spline cannot be applied >> 462 { >> 463 G4bool result = true; >> 464 for(size_t j=1; j<numberOfNodes; ++j) >> 465 { >> 466 if(binVector[j] <= binVector[j-1]) { >> 467 result = false; >> 468 useSpline = false; >> 469 secDerivative.clear(); >> 470 break; >> 471 } >> 472 } >> 473 secDerivative.resize(numberOfNodes,0.0); >> 474 return result; >> 475 } >> 476 >> 477 // -------------------------------------------------------------- >> 478 414 std::ostream& operator<<(std::ostream& out, co 479 std::ostream& operator<<(std::ostream& out, const G4PhysicsVector& pv) 415 { 480 { 416 // binning 481 // binning 417 G4long prec = out.precision(); << 482 out << std::setprecision(12) << pv.edgeMin << " " 418 out << std::setprecision(12) << pv.edgeMin < << 483 << pv.edgeMax << " " << pv.numberOfNodes << G4endl; 419 << pv.numberOfNodes << G4endl; << 420 484 421 // contents 485 // contents 422 out << pv.dataVector.size() << G4endl; << 486 out << pv.dataVector.size() << G4endl; 423 for (std::size_t i = 0; i < pv.dataVector.si << 487 for(size_t i = 0; i < pv.dataVector.size(); i++) 424 { 488 { 425 out << pv.binVector[i] << " " << pv.dataV 489 out << pv.binVector[i] << " " << pv.dataVector[i] << G4endl; 426 } 490 } 427 out.precision(prec); << 491 out << std::setprecision(6); 428 492 429 return out; 493 return out; 430 } 494 } 431 495 432 //-------------------------------------------- 496 //--------------------------------------------------------------- 433 G4double G4PhysicsVector::GetEnergy(const G4do << 497 >> 498 G4double G4PhysicsVector::Value(G4double theEnergy, size_t& lastIdx) const 434 { 499 { 435 if (0 == numberOfNodes) << 500 G4double y; 436 { << 501 if(theEnergy <= edgeMin) { 437 return 0.0; << 502 lastIdx = 0; 438 } << 503 y = dataVector[0]; 439 if (1 == numberOfNodes || val <= dataVector[ << 504 } else if(theEnergy >= edgeMax) { 440 { << 505 lastIdx = numberOfNodes-1; 441 return edgeMin; << 506 y = dataVector[lastIdx]; 442 } << 507 } else { 443 if (val >= dataVector[numberOfNodes - 1]) << 508 lastIdx = FindBin(theEnergy, lastIdx); 444 { << 509 y = Interpolation(lastIdx, theEnergy); 445 return edgeMax; << 446 } 510 } 447 std::size_t bin = std::lower_bound(dataVecto << 511 return y; 448 - dataVector.cbegin() - 1; << 449 if (bin > idxmax) { bin = idxmax; } << 450 G4double res = binVector[bin]; << 451 G4double del = dataVector[bin + 1] - dataVec << 452 if (del > 0.0) << 453 { << 454 res += (val - dataVector[bin]) * (binVecto << 455 } << 456 return res; << 457 } 512 } 458 513 459 //-------------------------------------------- 514 //--------------------------------------------------------------- 460 void G4PhysicsVector::PrintPutValueError(std:: << 515 461 G4dou << 516 G4double G4PhysicsVector::FindLinearEnergy(G4double rand) const 462 const << 517 { 463 { << 518 if(1 >= numberOfNodes) { return 0.0; } 464 G4ExceptionDescription ed; << 519 G4double y = rand*dataVector[numberOfNodes-1]; 465 ed << "Vector type: " << type << " length= " << 520 size_t bin = std::lower_bound(dataVector.begin(), dataVector.end(), y) 466 << "; an attempt to put data at index= " << 521 - dataVector.begin() - 1; 467 << " value= " << val << " in " << text; << 522 bin = std::min(bin, numberOfNodes-2); 468 G4Exception("G4PhysicsVector:", "gl0005", << 523 G4double res = binVector[bin]; 469 FatalException, ed, "Wrong opera << 524 G4double del = dataVector[bin+1] - dataVector[bin]; >> 525 if(del > 0.0) { >> 526 res += (y - dataVector[bin])*(binVector[bin+1] - res)/del; >> 527 } >> 528 return res; 470 } 529 } 471 530 472 //-------------------------------------------- 531 //--------------------------------------------------------------- 473 532