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$ 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" 56 #include "G4PhysicsVector.hh" 36 #include <iomanip> 57 #include <iomanip> 37 58 >> 59 G4Allocator<G4PhysicsVector> aPVAllocator; >> 60 38 // ------------------------------------------- 61 // -------------------------------------------------------------- 39 G4PhysicsVector::G4PhysicsVector(G4bool val) << 40 : useSpline(val) << 41 {} << 42 62 43 // ------------------------------------------- << 63 G4PhysicsVector::G4PhysicsVector(G4bool spline) 44 void G4PhysicsVector::Initialise() << 64 : type(T_G4PhysicsVector), >> 65 edgeMin(0.), edgeMax(0.), numberOfNodes(0), >> 66 useSpline(spline), >> 67 dBin(0.), baseBin(0.), >> 68 verboseLevel(0) 45 { 69 { 46 if (1 < numberOfNodes) << 70 cache = new G4PhysicsVectorCache(); 47 { << 71 } 48 idxmax = numberOfNodes - 2; << 72 49 edgeMin = binVector[0]; << 73 // -------------------------------------------------------------- 50 edgeMax = binVector[idxmax + 1]; << 74 >> 75 G4PhysicsVector::~G4PhysicsVector() >> 76 { >> 77 delete cache; cache =0; >> 78 } >> 79 >> 80 // -------------------------------------------------------------- >> 81 >> 82 G4PhysicsVector::G4PhysicsVector(const G4PhysicsVector& right) >> 83 { >> 84 cache = new G4PhysicsVectorCache(); >> 85 dBin = right.dBin; >> 86 baseBin = right.baseBin; >> 87 verboseLevel = right.verboseLevel; >> 88 >> 89 DeleteData(); >> 90 CopyData(right); >> 91 } >> 92 >> 93 // -------------------------------------------------------------- >> 94 >> 95 G4PhysicsVector& G4PhysicsVector::operator=(const G4PhysicsVector& right) >> 96 { >> 97 if (&right==this) { return *this; } >> 98 dBin = right.dBin; >> 99 baseBin = right.baseBin; >> 100 verboseLevel = right.verboseLevel; >> 101 >> 102 DeleteData(); >> 103 CopyData(right); >> 104 return *this; >> 105 } >> 106 >> 107 // -------------------------------------------------------------- >> 108 >> 109 G4int G4PhysicsVector::operator==(const G4PhysicsVector &right) const >> 110 { >> 111 return (this == &right); >> 112 } >> 113 >> 114 // -------------------------------------------------------------- >> 115 >> 116 G4int G4PhysicsVector::operator!=(const G4PhysicsVector &right) const >> 117 { >> 118 return (this != &right); >> 119 } >> 120 >> 121 // -------------------------------------------------------------- >> 122 >> 123 void G4PhysicsVector::DeleteData() >> 124 { >> 125 secDerivative.clear(); >> 126 } >> 127 >> 128 // -------------------------------------------------------------- >> 129 >> 130 void G4PhysicsVector::CopyData(const G4PhysicsVector& vec) >> 131 { >> 132 type = vec.type; >> 133 edgeMin = vec.edgeMin; >> 134 edgeMax = vec.edgeMax; >> 135 numberOfNodes = vec.numberOfNodes; >> 136 cache->lastEnergy = vec.GetLastEnergy(); >> 137 cache->lastValue = vec.GetLastValue(); >> 138 cache->lastBin = vec.GetLastBin(); >> 139 useSpline = vec.useSpline; >> 140 >> 141 size_t i; >> 142 dataVector.clear(); >> 143 for(i=0; i<(vec.dataVector).size(); i++){ >> 144 dataVector.push_back( (vec.dataVector)[i] ); >> 145 } >> 146 binVector.clear(); >> 147 for(i=0; i<(vec.binVector).size(); i++){ >> 148 binVector.push_back( (vec.binVector)[i] ); >> 149 } >> 150 secDerivative.clear(); >> 151 for(i=0; i<(vec.secDerivative).size(); i++){ >> 152 secDerivative.push_back( (vec.secDerivative)[i] ); 51 } 153 } 52 } 154 } 53 155 54 // ------------------------------------------- 156 // -------------------------------------------------------------- 55 G4bool G4PhysicsVector::Store(std::ofstream& f << 157 >> 158 G4double G4PhysicsVector::GetLowEdgeEnergy(size_t binNumber) const >> 159 { >> 160 return binVector[binNumber]; >> 161 } >> 162 >> 163 // -------------------------------------------------------------- >> 164 >> 165 G4bool G4PhysicsVector::Store(std::ofstream& fOut, G4bool ascii) 56 { 166 { 57 // Ascii mode 167 // Ascii mode 58 if (ascii) 168 if (ascii) 59 { 169 { 60 fOut << *this; 170 fOut << *this; 61 return true; 171 return true; 62 } << 172 } 63 // Binary Mode 173 // Binary Mode 64 174 65 // binning 175 // binning 66 fOut.write((char*) (&edgeMin), sizeof edgeMi << 176 fOut.write((char*)(&edgeMin), sizeof edgeMin); 67 fOut.write((char*) (&edgeMax), sizeof edgeMa << 177 fOut.write((char*)(&edgeMax), sizeof edgeMax); 68 fOut.write((char*) (&numberOfNodes), sizeof << 178 fOut.write((char*)(&numberOfNodes), sizeof numberOfNodes); 69 179 70 // contents 180 // contents 71 std::size_t size = dataVector.size(); << 181 size_t size = dataVector.size(); 72 fOut.write((char*) (&size), sizeof size); << 182 fOut.write((char*)(&size), sizeof size); 73 183 74 auto value = new G4double[2 * size]; << 184 G4double* value = new G4double[2*size]; 75 for (std::size_t i = 0; i < size; ++i) << 185 for(size_t i = 0; i < size; ++i) 76 { 186 { 77 value[2 * i] = binVector[i]; << 187 value[2*i] = binVector[i]; 78 value[2 * i + 1] = dataVector[i]; << 188 value[2*i+1]= dataVector[i]; 79 } 189 } 80 fOut.write((char*) (value), 2 * size * (size << 190 fOut.write((char*)(value), 2*size*(sizeof (G4double))); 81 delete[] value; << 191 delete [] value; 82 192 83 return true; 193 return true; 84 } 194 } 85 195 86 // ------------------------------------------- 196 // -------------------------------------------------------------- >> 197 87 G4bool G4PhysicsVector::Retrieve(std::ifstream 198 G4bool G4PhysicsVector::Retrieve(std::ifstream& fIn, G4bool ascii) 88 { 199 { 89 // clear properties; 200 // clear properties; >> 201 cache->lastEnergy=-DBL_MAX; >> 202 cache->lastValue =0.; >> 203 cache->lastBin =0; 90 dataVector.clear(); 204 dataVector.clear(); 91 binVector.clear(); 205 binVector.clear(); 92 secDerivative.clear(); 206 secDerivative.clear(); 93 207 94 // retrieve in ascii mode 208 // retrieve in ascii mode 95 if (ascii) << 209 if (ascii){ 96 { << 97 // binning 210 // binning 98 fIn >> edgeMin >> edgeMax >> numberOfNodes << 211 fIn >> edgeMin >> edgeMax >> numberOfNodes; 99 if (fIn.fail() || numberOfNodes < 2) << 212 if (fIn.fail()) { return false; } 100 { << 101 return false; << 102 } << 103 // contents 213 // contents 104 G4int siz0 = 0; << 214 G4int siz=0; 105 fIn >> siz0; << 215 fIn >> siz; 106 if (siz0 < 2) { return false; } << 216 if (fIn.fail()) { return false; } 107 auto siz = static_cast<std::size_t>(siz0); << 217 if (siz<=0) 108 if (fIn.fail() || siz != numberOfNodes) << 109 { 218 { >> 219 #ifdef G4VERBOSE >> 220 G4cerr << "G4PhysicsVector::Retrieve():"; >> 221 G4cerr << " Invalid vector size: " << siz << G4endl; >> 222 #endif 110 return false; 223 return false; 111 } 224 } 112 225 113 binVector.reserve(siz); 226 binVector.reserve(siz); 114 dataVector.reserve(siz); 227 dataVector.reserve(siz); 115 G4double vBin, vData; 228 G4double vBin, vData; 116 229 117 for (std::size_t i = 0; i < siz; ++i) << 230 for(G4int i = 0; i < siz ; i++) 118 { 231 { 119 vBin = 0.; << 232 vBin = 0.; 120 vData = 0.; << 233 vData= 0.; 121 fIn >> vBin >> vData; 234 fIn >> vBin >> vData; 122 if (fIn.fail()) << 235 if (fIn.fail()) { return false; } 123 { << 124 return false; << 125 } << 126 binVector.push_back(vBin); 236 binVector.push_back(vBin); 127 dataVector.push_back(vData); 237 dataVector.push_back(vData); 128 } 238 } 129 Initialise(); << 239 130 return true; << 240 // to remove any inconsistency >> 241 numberOfNodes = siz; >> 242 edgeMin = binVector[0]; >> 243 edgeMax = binVector[numberOfNodes-1]; >> 244 return true ; 131 } 245 } 132 246 133 // retrieve in binary mode 247 // retrieve in binary mode 134 // binning 248 // binning 135 fIn.read((char*) (&edgeMin), sizeof edgeMin) << 249 fIn.read((char*)(&edgeMin), sizeof edgeMin); 136 fIn.read((char*) (&edgeMax), sizeof edgeMax) << 250 fIn.read((char*)(&edgeMax), sizeof edgeMax); 137 fIn.read((char*) (&numberOfNodes), sizeof nu << 251 fIn.read((char*)(&numberOfNodes), sizeof numberOfNodes ); 138 << 252 139 // contents 253 // contents 140 std::size_t size; << 254 size_t size; 141 fIn.read((char*) (&size), sizeof size); << 255 fIn.read((char*)(&size), sizeof size); 142 << 256 143 auto value = new G4double[2 * size]; << 257 G4double* value = new G4double[2*size]; 144 fIn.read((char*) (value), 2 * size * (sizeof << 258 fIn.read((char*)(value), 2*size*(sizeof(G4double)) ); 145 if (static_cast<G4int>(fIn.gcount()) != stat << 259 if (G4int(fIn.gcount()) != G4int(2*size*(sizeof(G4double))) ) 146 { 260 { 147 delete[] value; << 261 delete [] value; 148 return false; 262 return false; 149 } 263 } 150 264 151 binVector.reserve(size); 265 binVector.reserve(size); 152 dataVector.reserve(size); 266 dataVector.reserve(size); 153 for (std::size_t i = 0; i < size; ++i) << 267 for(size_t i = 0; i < size; ++i) 154 { 268 { 155 binVector.push_back(value[2 * i]); << 269 binVector.push_back(value[2*i]); 156 dataVector.push_back(value[2 * i + 1]); << 270 dataVector.push_back(value[2*i+1]); 157 } 271 } 158 delete[] value; << 272 delete [] value; >> 273 >> 274 // to remove any inconsistency >> 275 numberOfNodes = size; >> 276 edgeMin = binVector[0]; >> 277 edgeMax = binVector[numberOfNodes-1]; 159 278 160 Initialise(); << 161 return true; 279 return true; 162 } 280 } 163 281 164 // ------------------------------------------- 282 // -------------------------------------------------------------- 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 283 174 // ------------------------------------------- << 284 void 175 std::size_t G4PhysicsVector::FindBin(const G4d << 285 G4PhysicsVector::ScaleVector(G4double factorE, G4double factorV) 176 std::size << 177 { 286 { 178 if (idx + 1 < numberOfNodes && << 287 size_t n = dataVector.size(); 179 energy >= binVector[idx] && energy <= bi << 288 size_t i; 180 { << 289 if(n > 0) { 181 return idx; << 290 for(i=0; i<n; ++i) { 182 } << 291 binVector[i] *= factorE; 183 if (energy <= binVector[1]) << 292 dataVector[i] *= factorV; 184 { << 293 } 185 return 0; << 186 } 294 } 187 if (energy >= binVector[idxmax]) << 295 // n = secDerivative.size(); 188 { << 296 // if(n > 0) { for(i=0; i<n; ++i) { secDerivative[i] *= factorV; } } 189 return idxmax; << 297 secDerivative.clear(); 190 } << 191 return GetBin(energy); << 192 } << 193 298 194 // ------------------------------------------- << 299 edgeMin *= factorE; 195 void G4PhysicsVector::ScaleVector(const G4doub << 300 edgeMax *= factorE; 196 const G4doub << 301 cache->lastEnergy = factorE*(cache->lastEnergy); 197 { << 302 cache->lastValue = factorV*(cache->lastValue); 198 for (std::size_t i = 0; i < numberOfNodes; + << 199 { << 200 binVector[i] *= factorE; << 201 dataVector[i] *= factorV; << 202 } << 203 Initialise(); << 204 } 303 } 205 304 206 // ------------------------------------------- << 305 // -------------------------------------------------------------- 207 void G4PhysicsVector::FillSecondDerivatives(co << 306 208 const G4double dir1, << 307 void 209 const G4double dir2) << 308 G4PhysicsVector::ComputeSecondDerivatives(G4double firstPointDerivative, >> 309 G4double endPointDerivative) >> 310 // A standard method of computation of second derivatives >> 311 // First derivatives at the first and the last point should be provided >> 312 // See for example W.H. Press et al. "Numerical recipes in C" >> 313 // Cambridge University Press, 1997. 210 { 314 { 211 if (!useSpline) { return; } << 315 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 { 316 { 216 if (0 < verboseLevel) << 317 ComputeSecDerivatives(); 217 { << 218 G4cout << "### G4PhysicsVector: spline c << 219 << numberOfNodes << " points - spline d << 220 << G4endl; << 221 DumpValues(); << 222 } << 223 useSpline = false; << 224 return; 318 return; 225 } 319 } 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 320 247 // spline is possible << 321 if(!SplinePossible()) { return; } 248 Initialise(); << 249 secDerivative.resize(numberOfNodes); << 250 322 251 if (1 < verboseLevel) << 323 G4int n = numberOfNodes-1; 252 { << 253 G4cout << "### G4PhysicsVector:: FillSecon << 254 << numberOfNodes << G4endl; << 255 DumpValues(); << 256 } << 257 324 258 switch(stype) << 325 G4double* u = new G4double [n]; 259 { << 326 260 case G4SplineType::Base: << 327 G4double p, sig, un; 261 ComputeSecDerivative1(); << 328 262 break; << 329 u[0] = (6.0/(binVector[1]-binVector[0])) >> 330 * ((dataVector[1]-dataVector[0])/(binVector[1]-binVector[0]) >> 331 - firstPointDerivative); >> 332 >> 333 secDerivative[0] = - 0.5; >> 334 >> 335 // Decomposition loop for tridiagonal algorithm. secDerivative[i] >> 336 // and u[i] are used for temporary storage of the decomposed factors. 263 337 264 case G4SplineType::FixedEdges: << 338 for(G4int i=1; i<n; ++i) 265 ComputeSecDerivative2(dir1, dir2); << 339 { 266 break; << 340 sig = (binVector[i]-binVector[i-1]) / (binVector[i+1]-binVector[i-1]); >> 341 p = sig*(secDerivative[i-1]) + 2.0; >> 342 secDerivative[i] = (sig - 1.0)/p; >> 343 u[i] = (dataVector[i+1]-dataVector[i])/(binVector[i+1]-binVector[i]) >> 344 - (dataVector[i]-dataVector[i-1])/(binVector[i]-binVector[i-1]); >> 345 u[i] = 6.0*u[i]/(binVector[i+1]-binVector[i-1]) - sig*u[i-1]/p; >> 346 } >> 347 >> 348 sig = (binVector[n-1]-binVector[n-2]) / (binVector[n]-binVector[n-2]); >> 349 p = sig*secDerivative[n-2] + 2.0; >> 350 un = (6.0/(binVector[n]-binVector[n-1])) >> 351 *(endPointDerivative - >> 352 (dataVector[n]-dataVector[n-1])/(binVector[n]-binVector[n-1])) - u[n-1]/p; >> 353 secDerivative[n] = un/(secDerivative[n-1] + 2.0); 267 354 268 default: << 355 // The back-substitution loop for the triagonal algorithm of solving 269 ComputeSecDerivative0(); << 356 // a linear system of equations. >> 357 >> 358 for(G4int k=n-1; k>0; --k) >> 359 { >> 360 secDerivative[k] *= >> 361 (secDerivative[k+1] - >> 362 u[k]*(binVector[k+1]-binVector[k-1])/(binVector[k+1]-binVector[k])); 270 } 363 } >> 364 secDerivative[0] = 0.5*(u[0] - secDerivative[1]); >> 365 >> 366 delete [] u; 271 } 367 } 272 368 273 // ------------------------------------------- 369 // -------------------------------------------------------------- 274 void G4PhysicsVector::ComputeSecDerivative0() << 275 // A simplified method of computation of seco << 276 { << 277 std::size_t n = numberOfNodes - 1; << 278 370 279 for (std::size_t i = 1; i < n; ++i) << 371 void G4PhysicsVector::FillSecondDerivatives() >> 372 // Computation of second derivatives using "Not-a-knot" endpoint conditions >> 373 // B.I. Kvasov "Methods of shape-preserving spline approximation" >> 374 // World Scientific, 2000 >> 375 { >> 376 if(5 > numberOfNodes) // cannot compute derivatives for less than 4 points 280 { 377 { 281 secDerivative[i] = 3.0 * << 378 ComputeSecDerivatives(); 282 ((dataVector[i + 1] - dataVector[i]) / ( << 379 return; 283 (dataVector[i] - dataVector[i - 1]) / << 284 (binVector[i] - binVector[i - 1])) / << 285 (binVector[i + 1] - binVector[i - 1]); << 286 } 380 } 287 secDerivative[n] = secDerivative[n - 1]; << 288 secDerivative[0] = secDerivative[1]; << 289 } << 290 381 291 // ------------------------------------------- << 382 if(!SplinePossible()) { return; } 292 void G4PhysicsVector::ComputeSecDerivative1() << 383 293 // Computation of second derivatives using "No << 384 G4int n = numberOfNodes-1; 294 // B.I. Kvasov "Methods of shape-preserving sp << 295 // World Scientific, 2000 << 296 { << 297 std::size_t n = numberOfNodes - 1; << 298 auto u = new G4double[n]; << 299 G4double p, sig; << 300 385 301 u[1] = ((dataVector[2] - dataVector[1]) / (b << 386 G4double* u = new G4double [n]; 302 (dataVector[1] - dataVector[0]) / (b << 387 303 u[1] = 6.0 * u[1] * (binVector[2] - binVecto << 388 G4double p, sig; 304 ((binVector[2] - binVector[0]) * (bin << 305 389 >> 390 u[1] = ((dataVector[2]-dataVector[1])/(binVector[2]-binVector[1]) - >> 391 (dataVector[1]-dataVector[0])/(binVector[1]-binVector[0])); >> 392 u[1] = 6.0*u[1]*(binVector[2]-binVector[1]) >> 393 / ((binVector[2]-binVector[0])*(binVector[2]-binVector[0])); >> 394 306 // Decomposition loop for tridiagonal algori 395 // Decomposition loop for tridiagonal algorithm. secDerivative[i] 307 // and u[i] are used for temporary storage o 396 // and u[i] are used for temporary storage of the decomposed factors. 308 397 309 secDerivative[1] = (2.0 * binVector[1] - bin << 398 secDerivative[1] = (2.0*binVector[1]-binVector[0]-binVector[2]) 310 (2.0 * binVector[2] - bin << 399 / (2.0*binVector[2]-binVector[0]-binVector[1]); 311 400 312 for(std::size_t i = 2; i < n - 1; ++i) << 401 for(G4int i=2; i<n-1; ++i) 313 { 402 { 314 sig = << 403 sig = (binVector[i]-binVector[i-1]) / (binVector[i+1]-binVector[i-1]); 315 (binVector[i] - binVector[i - 1]) / (bin << 404 p = sig*secDerivative[i-1] + 2.0; 316 p = sig * secDerivative[i - << 405 secDerivative[i] = (sig - 1.0)/p; 317 secDerivative[i] = (sig - 1.0) / p; << 406 u[i] = (dataVector[i+1]-dataVector[i])/(binVector[i+1]-binVector[i]) 318 u[i] = << 407 - (dataVector[i]-dataVector[i-1])/(binVector[i]-binVector[i-1]); 319 (dataVector[i + 1] - dataVector[i]) / (b << 408 u[i] = (6.0*u[i]/(binVector[i+1]-binVector[i-1])) - sig*u[i-1]/p; 320 (dataVector[i] - dataVector[i - 1]) / (b << 409 } 321 u[i] = << 410 322 (6.0 * u[i] / (binVector[i + 1] - binVec << 411 sig = (binVector[n-1]-binVector[n-2]) / (binVector[n]-binVector[n-2]); 323 } << 412 p = sig*secDerivative[n-3] + 2.0; 324 << 413 u[n-1] = (dataVector[n]-dataVector[n-1])/(binVector[n]-binVector[n-1]) 325 sig = << 414 - (dataVector[n-1]-dataVector[n-2])/(binVector[n-1]-binVector[n-2]); 326 (binVector[n - 1] - binVector[n - 2]) / (b << 415 u[n-1] = 6.0*sig*u[n-1]/(binVector[n]-binVector[n-2]) 327 p = sig * secDerivative[n - 3] + 2.0; << 416 - (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 417 335 p = (1.0 + sig) + (2.0 * sig - 1.0) * secDer << 418 p = (1.0+sig) + (2.0*sig-1.0)*secDerivative[n-2]; 336 secDerivative[n - 1] = u[n - 1] / p; << 419 secDerivative[n-1] = u[n-1]/p; 337 420 338 // The back-substitution loop for the triago 421 // The back-substitution loop for the triagonal algorithm of solving 339 // a linear system of equations. 422 // a linear system of equations. 340 << 423 341 for (std::size_t k = n - 2; k > 1; --k) << 424 for(G4int k=n-2; k>1; --k) 342 { 425 { 343 secDerivative[k] *= << 426 secDerivative[k] *= 344 (secDerivative[k + 1] - u[k] * (binVecto << 427 (secDerivative[k+1] - 345 (binVector[k + << 428 u[k]*(binVector[k+1]-binVector[k-1])/(binVector[k+1]-binVector[k])); 346 } << 429 } 347 secDerivative[n] = << 430 secDerivative[n] = (secDerivative[n-1] - (1.0-sig)*secDerivative[n-2])/sig; 348 (secDerivative[n - 1] - (1.0 - sig) * secD << 431 sig = 1.0 - ((binVector[2]-binVector[1])/(binVector[2]-binVector[0])); 349 sig = 1.0 - ((binVector[2] - binVector[1]) / << 432 secDerivative[1] *= (secDerivative[2] - u[1]/(1.0-sig)); 350 secDerivative[1] *= (secDerivative[2] - u[1] << 433 secDerivative[0] = (secDerivative[1] - sig*secDerivative[2])/(1.0-sig); 351 secDerivative[0] = (secDerivative[1] - sig * << 352 434 353 delete[] u; << 435 delete [] u; 354 } 436 } 355 437 356 // ------------------------------------------- 438 // -------------------------------------------------------------- 357 void G4PhysicsVector::ComputeSecDerivative2(G4 << 358 G4 << 359 // A standard method of computation of second << 360 // First derivatives at the first and the last << 361 // See for example W.H. Press et al. "Numerica << 362 // Cambridge University Press, 1997. << 363 { << 364 std::size_t n = numberOfNodes - 1; << 365 auto u = new G4double[n]; << 366 G4double p, sig, un; << 367 << 368 u[0] = (6.0 / (binVector[1] - binVector[0])) << 369 ((dataVector[1] - dataVector[0]) / (b << 370 firstPointDerivative); << 371 439 372 secDerivative[0] = -0.5; << 440 void 373 << 441 G4PhysicsVector::ComputeSecDerivatives() 374 // Decomposition loop for tridiagonal algori << 442 // A simplified method of computation of second derivatives 375 // and u[i] are used for temporary storage o << 443 { >> 444 if(!SplinePossible()) { return; } 376 445 377 for (std::size_t i = 1; i < n; ++i) << 446 if(3 > numberOfNodes) // cannot compute derivatives for less than 4 bins 378 { 447 { 379 sig = << 448 useSpline = false; 380 (binVector[i] - binVector[i - 1]) / (bin << 449 return; 381 p = sig * (secDerivative[i << 450 } 382 secDerivative[i] = (sig - 1.0) / p; << 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 451 399 // The back-substitution loop for the triago << 452 size_t n = numberOfNodes-1; 400 // a linear system of equations. << 401 453 402 for (std::size_t k = n - 1; k > 0; --k) << 454 for(size_t i=1; i<n; ++i) 403 { 455 { 404 secDerivative[k] *= << 456 secDerivative[i] = 405 (secDerivative[k + 1] - u[k] * (binVecto << 457 3.0*((dataVector[i+1]-dataVector[i])/(binVector[i+1]-binVector[i]) - 406 (binVector[k + << 458 (dataVector[i]-dataVector[i-1])/(binVector[i]-binVector[i-1])) >> 459 /(binVector[i+1]-binVector[i-1]); 407 } 460 } 408 secDerivative[0] = 0.5 * (u[0] - secDerivati << 461 secDerivative[n] = secDerivative[n-1]; 409 << 462 secDerivative[0] = secDerivative[1]; 410 delete[] u; << 411 } 463 } 412 464 413 // ------------------------------------------- 465 // -------------------------------------------------------------- >> 466 >> 467 G4bool G4PhysicsVector::SplinePossible() >> 468 // Initialise second derivative array. If neighbor energy coincide >> 469 // or not ordered than spline cannot be applied >> 470 { >> 471 secDerivative.clear(); >> 472 if(!useSpline) { return useSpline; } >> 473 secDerivative.reserve(numberOfNodes); >> 474 for(size_t j=0; j<numberOfNodes; ++j) >> 475 { >> 476 secDerivative.push_back(0.0); >> 477 if(j > 0) >> 478 { >> 479 if(binVector[j]-binVector[j-1] <= 0.) { useSpline = false; } >> 480 } >> 481 } >> 482 return useSpline; >> 483 } >> 484 >> 485 // -------------------------------------------------------------- >> 486 414 std::ostream& operator<<(std::ostream& out, co 487 std::ostream& operator<<(std::ostream& out, const G4PhysicsVector& pv) 415 { 488 { 416 // binning 489 // binning 417 G4long prec = out.precision(); << 490 out << std::setprecision(12) << pv.edgeMin << " " 418 out << std::setprecision(12) << pv.edgeMin < << 491 << pv.edgeMax << " " << pv.numberOfNodes << G4endl; 419 << pv.numberOfNodes << G4endl; << 420 492 421 // contents 493 // contents 422 out << pv.dataVector.size() << G4endl; << 494 out << pv.dataVector.size() << G4endl; 423 for (std::size_t i = 0; i < pv.dataVector.si << 495 for(size_t i = 0; i < pv.dataVector.size(); i++) 424 { 496 { 425 out << pv.binVector[i] << " " << pv.dataV 497 out << pv.binVector[i] << " " << pv.dataVector[i] << G4endl; 426 } 498 } 427 out.precision(prec); << 499 out << std::setprecision(6); 428 500 429 return out; 501 return out; 430 } 502 } 431 503 432 //-------------------------------------------- 504 //--------------------------------------------------------------- 433 G4double G4PhysicsVector::GetEnergy(const G4do << 505 >> 506 void G4PhysicsVector::ComputeValue(G4double theEnergy) 434 { 507 { 435 if (0 == numberOfNodes) << 508 // Use cache for speed up - check if the value 'theEnergy' lies 436 { << 509 // between the last energy and low edge of of the 437 return 0.0; << 510 // bin of last call, then the last bin location is used. 438 } << 511 439 if (1 == numberOfNodes || val <= dataVector[ << 512 if( theEnergy < cache->lastEnergy 440 { << 513 && theEnergy >= binVector[cache->lastBin]) { 441 return edgeMin; << 514 cache->lastEnergy = theEnergy; 442 } << 515 Interpolation(cache->lastBin); 443 if (val >= dataVector[numberOfNodes - 1]) << 516 444 { << 517 } else if( theEnergy <= edgeMin ) { 445 return edgeMax; << 518 cache->lastBin = 0; 446 } << 519 cache->lastEnergy = edgeMin; 447 std::size_t bin = std::lower_bound(dataVecto << 520 cache->lastValue = dataVector[0]; 448 - dataVector.cbegin() - 1; << 521 449 if (bin > idxmax) { bin = idxmax; } << 522 } else if( theEnergy >= edgeMax ) { 450 G4double res = binVector[bin]; << 523 cache->lastBin = numberOfNodes-1; 451 G4double del = dataVector[bin + 1] - dataVec << 524 cache->lastEnergy = edgeMax; 452 if (del > 0.0) << 525 cache->lastValue = dataVector[cache->lastBin]; 453 { << 526 454 res += (val - dataVector[bin]) * (binVecto << 527 } else { >> 528 cache->lastBin = FindBinLocation(theEnergy); >> 529 cache->lastEnergy = theEnergy; >> 530 Interpolation(cache->lastBin); 455 } 531 } 456 return res; << 457 } 532 } 458 533 459 //-------------------------------------------- << 460 void G4PhysicsVector::PrintPutValueError(std:: << 461 G4dou << 462 const << 463 { << 464 G4ExceptionDescription ed; << 465 ed << "Vector type: " << type << " length= " << 466 << "; an attempt to put data at index= " << 467 << " value= " << val << " in " << text; << 468 G4Exception("G4PhysicsVector:", "gl0005", << 469 FatalException, ed, "Wrong opera << 470 } << 471 << 472 //-------------------------------------------- << 473 534