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