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