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" << 36 #include <iomanip> 56 #include <iomanip> >> 57 #include "G4PhysicsVector.hh" 37 58 38 // ------------------------------------------- 59 // -------------------------------------------------------------- >> 60 39 G4PhysicsVector::G4PhysicsVector(G4bool val) 61 G4PhysicsVector::G4PhysicsVector(G4bool val) 40 : useSpline(val) << 62 : type(T_G4PhysicsVector), >> 63 edgeMin(0.), edgeMax(0.), numberOfNodes(0), >> 64 useSpline(val), >> 65 dBin(0.), baseBin(0.), >> 66 verboseLevel(0) 41 {} 67 {} 42 68 43 // ------------------------------------------- << 69 // -------------------------------------------------------------- 44 void G4PhysicsVector::Initialise() << 70 >> 71 G4PhysicsVector::~G4PhysicsVector() >> 72 {} >> 73 >> 74 // -------------------------------------------------------------- >> 75 >> 76 G4PhysicsVector::G4PhysicsVector(const G4PhysicsVector& right) 45 { 77 { 46 if (1 < numberOfNodes) << 78 dBin = right.dBin; 47 { << 79 baseBin = right.baseBin; 48 idxmax = numberOfNodes - 2; << 80 verboseLevel = right.verboseLevel; 49 edgeMin = binVector[0]; << 81 50 edgeMax = binVector[idxmax + 1]; << 82 DeleteData(); >> 83 CopyData(right); >> 84 } >> 85 >> 86 // -------------------------------------------------------------- >> 87 >> 88 G4PhysicsVector& G4PhysicsVector::operator=(const G4PhysicsVector& right) >> 89 { >> 90 if (&right==this) { return *this; } >> 91 dBin = right.dBin; >> 92 baseBin = right.baseBin; >> 93 verboseLevel = right.verboseLevel; >> 94 >> 95 DeleteData(); >> 96 CopyData(right); >> 97 return *this; >> 98 } >> 99 >> 100 // -------------------------------------------------------------- >> 101 >> 102 G4int G4PhysicsVector::operator==(const G4PhysicsVector &right) const >> 103 { >> 104 return (this == &right); >> 105 } >> 106 >> 107 // -------------------------------------------------------------- >> 108 >> 109 G4int G4PhysicsVector::operator!=(const G4PhysicsVector &right) const >> 110 { >> 111 return (this != &right); >> 112 } >> 113 >> 114 // -------------------------------------------------------------- >> 115 >> 116 void G4PhysicsVector::DeleteData() >> 117 { >> 118 useSpline = false; >> 119 secDerivative.clear(); >> 120 } >> 121 >> 122 // -------------------------------------------------------------- >> 123 >> 124 void G4PhysicsVector::CopyData(const G4PhysicsVector& vec) >> 125 { >> 126 type = vec.type; >> 127 edgeMin = vec.edgeMin; >> 128 edgeMax = vec.edgeMax; >> 129 numberOfNodes = vec.numberOfNodes; >> 130 useSpline = vec.useSpline; >> 131 >> 132 size_t i; >> 133 dataVector.resize(numberOfNodes); >> 134 for(i=0; i<numberOfNodes; ++i) { >> 135 dataVector[i] = (vec.dataVector)[i]; >> 136 } >> 137 binVector.resize(numberOfNodes); >> 138 for(i=0; i<numberOfNodes; ++i) { >> 139 binVector[i] = (vec.binVector)[i]; >> 140 } >> 141 if(0 < (vec.secDerivative).size()) { >> 142 secDerivative.resize(numberOfNodes); >> 143 for(i=0; i<numberOfNodes; ++i){ >> 144 secDerivative[i] = (vec.secDerivative)[i]; >> 145 } 51 } 146 } 52 } 147 } 53 148 54 // ------------------------------------------- 149 // -------------------------------------------------------------- >> 150 >> 151 G4double G4PhysicsVector::GetLowEdgeEnergy(size_t binNumber) const >> 152 { >> 153 return binVector[binNumber]; >> 154 } >> 155 >> 156 // -------------------------------------------------------------- >> 157 55 G4bool G4PhysicsVector::Store(std::ofstream& f 158 G4bool G4PhysicsVector::Store(std::ofstream& fOut, G4bool ascii) const 56 { 159 { 57 // Ascii mode 160 // Ascii mode 58 if (ascii) 161 if (ascii) 59 { 162 { 60 fOut << *this; 163 fOut << *this; 61 return true; 164 return true; 62 } << 165 } 63 // Binary Mode 166 // Binary Mode 64 167 65 // binning 168 // binning 66 fOut.write((char*) (&edgeMin), sizeof edgeMi << 169 fOut.write((char*)(&edgeMin), sizeof edgeMin); 67 fOut.write((char*) (&edgeMax), sizeof edgeMa << 170 fOut.write((char*)(&edgeMax), sizeof edgeMax); 68 fOut.write((char*) (&numberOfNodes), sizeof << 171 fOut.write((char*)(&numberOfNodes), sizeof numberOfNodes); 69 172 70 // contents 173 // contents 71 std::size_t size = dataVector.size(); << 174 size_t size = dataVector.size(); 72 fOut.write((char*) (&size), sizeof size); << 175 fOut.write((char*)(&size), sizeof size); 73 176 74 auto value = new G4double[2 * size]; << 177 G4double* value = new G4double[2*size]; 75 for (std::size_t i = 0; i < size; ++i) << 178 for(size_t i = 0; i < size; ++i) 76 { 179 { 77 value[2 * i] = binVector[i]; << 180 value[2*i] = binVector[i]; 78 value[2 * i + 1] = dataVector[i]; << 181 value[2*i+1]= dataVector[i]; 79 } 182 } 80 fOut.write((char*) (value), 2 * size * (size << 183 fOut.write((char*)(value), 2*size*(sizeof (G4double))); 81 delete[] value; << 184 delete [] value; 82 185 83 return true; 186 return true; 84 } 187 } 85 188 86 // ------------------------------------------- 189 // -------------------------------------------------------------- >> 190 87 G4bool G4PhysicsVector::Retrieve(std::ifstream 191 G4bool G4PhysicsVector::Retrieve(std::ifstream& fIn, G4bool ascii) 88 { 192 { 89 // clear properties; 193 // clear properties; 90 dataVector.clear(); 194 dataVector.clear(); 91 binVector.clear(); 195 binVector.clear(); 92 secDerivative.clear(); 196 secDerivative.clear(); 93 197 94 // retrieve in ascii mode 198 // retrieve in ascii mode 95 if (ascii) << 199 if (ascii){ 96 { << 97 // binning 200 // binning 98 fIn >> edgeMin >> edgeMax >> numberOfNodes << 201 fIn >> edgeMin >> edgeMax >> numberOfNodes; 99 if (fIn.fail() || numberOfNodes < 2) << 202 if (fIn.fail()) { return false; } 100 { << 101 return false; << 102 } << 103 // contents 203 // contents 104 G4int siz0 = 0; << 204 G4int siz=0; 105 fIn >> siz0; << 205 fIn >> siz; 106 if (siz0 < 2) { return false; } << 206 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 207 113 binVector.reserve(siz); 208 binVector.reserve(siz); 114 dataVector.reserve(siz); 209 dataVector.reserve(siz); 115 G4double vBin, vData; 210 G4double vBin, vData; 116 211 117 for (std::size_t i = 0; i < siz; ++i) << 212 for(G4int i = 0; i < siz ; i++) 118 { 213 { 119 vBin = 0.; << 214 vBin = 0.; 120 vData = 0.; << 215 vData= 0.; 121 fIn >> vBin >> vData; 216 fIn >> vBin >> vData; 122 if (fIn.fail()) << 217 if (fIn.fail()) { return false; } 123 { << 124 return false; << 125 } << 126 binVector.push_back(vBin); 218 binVector.push_back(vBin); 127 dataVector.push_back(vData); 219 dataVector.push_back(vData); 128 } 220 } 129 Initialise(); << 221 130 return true; << 222 // to remove any inconsistency >> 223 numberOfNodes = siz; >> 224 edgeMin = binVector[0]; >> 225 edgeMax = binVector[numberOfNodes-1]; >> 226 return true ; 131 } 227 } 132 228 133 // retrieve in binary mode 229 // retrieve in binary mode 134 // binning 230 // binning 135 fIn.read((char*) (&edgeMin), sizeof edgeMin) << 231 fIn.read((char*)(&edgeMin), sizeof edgeMin); 136 fIn.read((char*) (&edgeMax), sizeof edgeMax) << 232 fIn.read((char*)(&edgeMax), sizeof edgeMax); 137 fIn.read((char*) (&numberOfNodes), sizeof nu << 233 fIn.read((char*)(&numberOfNodes), sizeof numberOfNodes ); 138 << 234 139 // contents 235 // contents 140 std::size_t size; << 236 size_t size; 141 fIn.read((char*) (&size), sizeof size); << 237 fIn.read((char*)(&size), sizeof size); 142 << 238 143 auto value = new G4double[2 * size]; << 239 G4double* value = new G4double[2*size]; 144 fIn.read((char*) (value), 2 * size * (sizeof << 240 fIn.read((char*)(value), 2*size*(sizeof(G4double)) ); 145 if (static_cast<G4int>(fIn.gcount()) != stat << 241 if (G4int(fIn.gcount()) != G4int(2*size*(sizeof(G4double))) ) 146 { 242 { 147 delete[] value; << 243 delete [] value; 148 return false; 244 return false; 149 } 245 } 150 246 151 binVector.reserve(size); 247 binVector.reserve(size); 152 dataVector.reserve(size); 248 dataVector.reserve(size); 153 for (std::size_t i = 0; i < size; ++i) << 249 for(size_t i = 0; i < size; ++i) 154 { 250 { 155 binVector.push_back(value[2 * i]); << 251 binVector.push_back(value[2*i]); 156 dataVector.push_back(value[2 * i + 1]); << 252 dataVector.push_back(value[2*i+1]); 157 } 253 } 158 delete[] value; << 254 delete [] value; >> 255 >> 256 // to remove any inconsistency >> 257 numberOfNodes = size; >> 258 edgeMin = binVector[0]; >> 259 edgeMax = binVector[numberOfNodes-1]; 159 260 160 Initialise(); << 161 return true; 261 return true; 162 } 262 } 163 263 164 // ------------------------------------------- 264 // -------------------------------------------------------------- >> 265 165 void G4PhysicsVector::DumpValues(G4double unit 266 void G4PhysicsVector::DumpValues(G4double unitE, G4double unitV) const 166 { 267 { 167 for (std::size_t i = 0; i < numberOfNodes; + << 268 for (size_t i = 0; i < numberOfNodes; ++i) 168 { << 269 { 169 G4cout << binVector[i] / unitE << " " << << 270 G4cout << binVector[i]/unitE << " " << dataVector[i]/unitV << G4endl; 170 << G4endl; << 271 } 171 } << 172 } 272 } 173 273 174 // ------------------------------------------- 274 // -------------------------------------------------------------------- 175 std::size_t G4PhysicsVector::FindBin(const G4d << 275 176 std::size << 276 void >> 277 G4PhysicsVector::ScaleVector(G4double factorE, G4double factorV) 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(); >> 286 >> 287 edgeMin = binVector[0]; >> 288 edgeMax = binVector[n-1]; 192 } 289 } 193 290 194 // ------------------------------------------- << 291 // -------------------------------------------------------------- 195 void G4PhysicsVector::ScaleVector(const G4doub << 292 196 const G4doub << 293 void >> 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. 197 { 300 { 198 for (std::size_t i = 0; i < numberOfNodes; + << 301 if(4 > numberOfNodes) // cannot compute derivatives for less than 4 bins 199 { 302 { 200 binVector[i] *= factorE; << 303 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; 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 << 305 375 >> 376 G4double* u = new G4double [n]; >> 377 >> 378 G4double p, sig; >> 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 << 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 429 372 secDerivative[0] = -0.5; << 430 void >> 431 G4PhysicsVector::ComputeSecDerivatives() >> 432 // A simplified method of computation of second derivatives >> 433 { >> 434 if(3 > numberOfNodes) // cannot compute derivatives for less than 4 bins >> 435 { >> 436 useSpline = false; >> 437 return; >> 438 } 373 439 374 // Decomposition loop for tridiagonal algori << 440 if(!SplinePossible()) { return; } 375 // and u[i] are used for temporary storage o << 376 441 377 for (std::size_t i = 1; i < n; ++i) << 442 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 443 399 // The back-substitution loop for the triago << 444 size_t n = numberOfNodes-1; 400 // a linear system of equations. << 401 445 402 for (std::size_t k = n - 1; k > 0; --k) << 446 for(size_t i=1; i<n; ++i) 403 { 447 { 404 secDerivative[k] *= << 448 secDerivative[i] = 405 (secDerivative[k + 1] - u[k] * (binVecto << 449 3.0*((dataVector[i+1]-dataVector[i])/(binVector[i+1]-binVector[i]) - 406 (binVector[k + << 450 (dataVector[i]-dataVector[i-1])/(binVector[i]-binVector[i-1])) >> 451 /(binVector[i+1]-binVector[i-1]); 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]; >> 507 } else { >> 508 lastIdx = FindBin(theEnergy, lastIdx); >> 509 y = Interpolation(lastIdx, theEnergy); 442 } 510 } 443 if (val >= dataVector[numberOfNodes - 1]) << 511 return y; 444 { << 512 } 445 return edgeMax; << 513 446 } << 514 //--------------------------------------------------------------- 447 std::size_t bin = std::lower_bound(dataVecto << 515 448 - dataVector.cbegin() - 1; << 516 G4double G4PhysicsVector::FindLinearEnergy(G4double rand) const 449 if (bin > idxmax) { bin = idxmax; } << 517 { >> 518 if(1 >= numberOfNodes) { return 0.0; } >> 519 G4double y = rand*dataVector[numberOfNodes-1]; >> 520 size_t bin = std::lower_bound(dataVector.begin(), dataVector.end(), y) >> 521 - dataVector.begin() - 1; >> 522 bin = std::min(bin, numberOfNodes-2); 450 G4double res = binVector[bin]; 523 G4double res = binVector[bin]; 451 G4double del = dataVector[bin + 1] - dataVec << 524 G4double del = dataVector[bin+1] - dataVector[bin]; 452 if (del > 0.0) << 525 if(del > 0.0) { 453 { << 526 res += (y - dataVector[bin])*(binVector[bin+1] - res)/del; 454 res += (val - dataVector[bin]) * (binVecto << 455 } 527 } 456 return res; 528 return res; 457 } 529 } 458 530 459 //-------------------------------------------- 531 //--------------------------------------------------------------- 460 void G4PhysicsVector::PrintPutValueError(std:: << 532 461 G4dou << 533 void G4PhysicsVector::PrintPutValueError(size_t index) 462 const << 463 { 534 { 464 G4ExceptionDescription ed; 535 G4ExceptionDescription ed; 465 ed << "Vector type: " << type << " length= " << 536 ed << "Vector type " << type << " length= " << numberOfNodes 466 << "; an attempt to put data at index= " << 537 << " an attempt to put data at index= " << index; 467 << " value= " << val << " in " << text; << 538 G4Exception("G4PhysicsVector::PutValue()","gl0005",FatalException, 468 G4Exception("G4PhysicsVector:", "gl0005", << 539 ed,"Memory overwritten"); 469 FatalException, ed, "Wrong opera << 540 470 } 541 } 471 542 472 //-------------------------------------------- 543 //--------------------------------------------------------------- 473 544