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