Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer << 3 // * DISCLAIMER * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th << 5 // * The following disclaimer summarizes all the specific disclaimers * 6 // * the Geant4 Collaboration. It is provided << 6 // * of contributors to this software. The specific disclaimers,which * 7 // * conditions of the Geant4 Software License << 7 // * govern, are listed with their locations in: * 8 // * LICENSE and available at http://cern.ch/ << 8 // * http://cern.ch/geant4/license * 9 // * include a list of copyright holders. << 10 // * 9 // * * 11 // * Neither the authors of this software syst 10 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 11 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 12 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 13 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file << 14 // * use. * 16 // * for the full disclaimer and the limitatio << 17 // * 15 // * * 18 // * This code implementation is the result << 16 // * This code implementation is the intellectual property of the * 19 // * technical work of the GEANT4 collaboratio << 17 // * GEANT4 collaboration. * 20 // * By using, copying, modifying or distri << 18 // * By copying, distributing or modifying the Program (or any work * 21 // * any work based on the software) you ag << 19 // * based on the Program) you indicate your acceptance of this * 22 // * use in resulting scientific publicati << 20 // * statement, and all its terms. * 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* 21 // ******************************************************************** 25 // 22 // 26 // G4PhysicsVector class implementation << 27 // 23 // 28 // Authors: << 24 // $Id: G4PhysicsVector.cc,v 1.16 2005/03/15 19:11:35 gcosmo Exp $ 29 // - 02 Dec. 1995, G.Cosmo: Structure created << 25 // GEANT4 tag $Name: geant4-07-01 $ 30 // - 03 Mar. 1996, K.Amako: Implemented the 1s << 26 // 31 // Revisions: << 27 // 32 // - 11 Nov. 2000, H.Kurashige: Use STL vector << 28 // -------------------------------------------------------------- 33 // ------------------------------------------- << 29 // GEANT 4 class implementation file >> 30 // >> 31 // G4PhysicsVector.cc >> 32 // >> 33 // History: >> 34 // 02 Dec. 1995, G.Cosmo : Structure created based on object model >> 35 // 03 Mar. 1996, K.Amako : Implemented the 1st version >> 36 // 01 Jul. 1996, K.Amako : Hidden bin from the user introduced >> 37 // 12 Nov. 1998, K.Amako : A bug in GetVectorLength() fixed >> 38 // 11 Nov. 2000, H.Kurashige : use STL vector for dataVector and binVector >> 39 // 18 Jan. 2001, H.Kurashige : removed ptrNextTable >> 40 // 09 Mar. 2001, H.Kurashige : added G4PhysicsVector type >> 41 // -------------------------------------------------------------- 34 42 35 #include "G4PhysicsVector.hh" 43 #include "G4PhysicsVector.hh" 36 #include <iomanip> 44 #include <iomanip> 37 45 38 // ------------------------------------------- << 46 G4PhysicsVector::G4PhysicsVector() 39 G4PhysicsVector::G4PhysicsVector(G4bool val) << 47 : type(T_G4PhysicsVector), 40 : useSpline(val) << 48 edgeMin(0.), edgeMax(0.), numberOfBin(0), 41 {} << 49 lastEnergy(0.), lastValue(0.), lastBin(0) >> 50 { >> 51 } 42 52 43 // ------------------------------------------- << 53 G4PhysicsVector::~G4PhysicsVector() 44 void G4PhysicsVector::Initialise() << 45 { 54 { 46 if (1 < numberOfNodes) << 55 dataVector.clear(); 47 { << 56 binVector.clear(); 48 idxmax = numberOfNodes - 2; << 57 type = T_G4PhysicsVector; 49 edgeMin = binVector[0]; << 50 edgeMax = binVector[idxmax + 1]; << 51 } << 52 } 58 } 53 59 54 // ------------------------------------------- << 60 G4PhysicsVector::G4PhysicsVector(const G4PhysicsVector& right) 55 G4bool G4PhysicsVector::Store(std::ofstream& f << 61 { >> 62 *this=right; >> 63 } >> 64 >> 65 G4PhysicsVector& G4PhysicsVector::operator=(const G4PhysicsVector& right) >> 66 { >> 67 if (&right==this) { return *this; } >> 68 if (type != right.type) { return *this; } >> 69 >> 70 type = right.type; >> 71 edgeMin = right.edgeMin; >> 72 edgeMax = right.edgeMax; >> 73 numberOfBin = right.numberOfBin; >> 74 lastEnergy = right.lastEnergy; >> 75 lastValue = right.lastValue; >> 76 lastBin = right.lastBin; >> 77 dataVector = right.dataVector; >> 78 binVector = right.binVector; >> 79 comment = right.comment; >> 80 return *this; >> 81 } >> 82 >> 83 G4int G4PhysicsVector::operator==(const G4PhysicsVector &right) const >> 84 { >> 85 return (this == &right); >> 86 } >> 87 >> 88 G4int G4PhysicsVector::operator!=(const G4PhysicsVector &right) const >> 89 { >> 90 return (this != &right); >> 91 } >> 92 >> 93 G4double G4PhysicsVector::GetLowEdgeEnergy(size_t binNumber) const >> 94 { >> 95 return binVector[binNumber]; >> 96 } >> 97 >> 98 G4bool G4PhysicsVector::Store(std::ofstream& fOut, G4bool ascii) 56 { 99 { 57 // Ascii mode 100 // Ascii mode 58 if (ascii) 101 if (ascii) 59 { 102 { 60 fOut << *this; 103 fOut << *this; 61 return true; 104 return true; 62 } << 105 } 63 // Binary Mode 106 // Binary Mode 64 107 65 // binning 108 // binning 66 fOut.write((char*) (&edgeMin), sizeof edgeMi << 109 fOut.write((char*)(&edgeMin), sizeof edgeMin); 67 fOut.write((char*) (&edgeMax), sizeof edgeMa << 110 fOut.write((char*)(&edgeMax), sizeof edgeMax); 68 fOut.write((char*) (&numberOfNodes), sizeof << 111 fOut.write((char*)(&numberOfBin), sizeof numberOfBin); 69 112 70 // contents 113 // contents 71 std::size_t size = dataVector.size(); << 114 size_t size = dataVector.size(); 72 fOut.write((char*) (&size), sizeof size); << 115 fOut.write((char*)(&size), sizeof size); 73 116 74 auto value = new G4double[2 * size]; << 117 G4double* value = new G4double[2*size]; 75 for (std::size_t i = 0; i < size; ++i) << 118 for(size_t i = 0; i < size; i++) 76 { 119 { 77 value[2 * i] = binVector[i]; << 120 value[2*i] = binVector[i]; 78 value[2 * i + 1] = dataVector[i]; << 121 value[2*i+1]= dataVector[i]; 79 } 122 } 80 fOut.write((char*) (value), 2 * size * (size << 123 fOut.write((char*)(value), 2*size*(sizeof (G4double))); 81 delete[] value; << 124 delete [] value; 82 125 83 return true; 126 return true; 84 } 127 } 85 128 86 // ------------------------------------------- << 87 G4bool G4PhysicsVector::Retrieve(std::ifstream 129 G4bool G4PhysicsVector::Retrieve(std::ifstream& fIn, G4bool ascii) 88 { 130 { 89 // clear properties; 131 // clear properties; >> 132 lastEnergy=0.; >> 133 lastValue =0.; >> 134 lastBin =0; 90 dataVector.clear(); 135 dataVector.clear(); 91 binVector.clear(); 136 binVector.clear(); 92 secDerivative.clear(); << 137 comment = ""; 93 138 94 // retrieve in ascii mode 139 // retrieve in ascii mode 95 if (ascii) 140 if (ascii) 96 { 141 { 97 // binning 142 // binning 98 fIn >> edgeMin >> edgeMax >> numberOfNodes << 143 fIn >> edgeMin >> edgeMax >> numberOfBin; 99 if (fIn.fail() || numberOfNodes < 2) << 144 if (fIn.fail()) { return false; } 100 { << 101 return false; << 102 } << 103 // contents 145 // contents 104 G4int siz0 = 0; << 146 size_t size=0; 105 fIn >> siz0; << 147 fIn >> size; 106 if (siz0 < 2) { return false; } << 148 if (fIn.fail()) { return false; } 107 auto siz = static_cast<std::size_t>(siz0); << 149 108 if (fIn.fail() || siz != numberOfNodes) << 150 binVector.reserve(size); 109 { << 151 dataVector.reserve(size); 110 return false; << 152 for(size_t i = 0; i < size ; i++) 111 } << 112 << 113 binVector.reserve(siz); << 114 dataVector.reserve(siz); << 115 G4double vBin, vData; << 116 << 117 for (std::size_t i = 0; i < siz; ++i) << 118 { 153 { 119 vBin = 0.; << 154 G4double vBin=0., vData=0.; 120 vData = 0.; << 121 fIn >> vBin >> vData; 155 fIn >> vBin >> vData; 122 if (fIn.fail()) << 156 if (fIn.fail()) { return false; } 123 { << 124 return false; << 125 } << 126 binVector.push_back(vBin); 157 binVector.push_back(vBin); 127 dataVector.push_back(vData); 158 dataVector.push_back(vData); 128 } 159 } 129 Initialise(); << 160 return true ; 130 return true; << 131 } 161 } 132 162 133 // retrieve in binary mode 163 // retrieve in binary mode 134 // binning 164 // binning 135 fIn.read((char*) (&edgeMin), sizeof edgeMin) << 165 fIn.read((char*)(&edgeMin), sizeof edgeMin); 136 fIn.read((char*) (&edgeMax), sizeof edgeMax) << 166 fIn.read((char*)(&edgeMax), sizeof edgeMax); 137 fIn.read((char*) (&numberOfNodes), sizeof nu << 167 fIn.read((char*)(&numberOfBin), sizeof numberOfBin ); 138 << 168 139 // contents 169 // contents 140 std::size_t size; << 170 size_t size; 141 fIn.read((char*) (&size), sizeof size); << 171 fIn.read((char*)(&size), sizeof size); 142 << 172 143 auto value = new G4double[2 * size]; << 173 G4double* value = new G4double[2*size]; 144 fIn.read((char*) (value), 2 * size * (sizeof << 174 fIn.read((char*)(value), 2*size*(sizeof(G4double)) ); 145 if (static_cast<G4int>(fIn.gcount()) != stat << 175 if (G4int(fIn.gcount()) != G4int(2*size*(sizeof(G4double))) ){ 146 { << 176 delete [] value; 147 delete[] value; << 148 return false; 177 return false; 149 } 178 } 150 179 151 binVector.reserve(size); 180 binVector.reserve(size); 152 dataVector.reserve(size); 181 dataVector.reserve(size); 153 for (std::size_t i = 0; i < size; ++i) << 182 for(size_t i = 0; i < size; i++) { 154 { << 183 binVector.push_back(value[2*i]); 155 binVector.push_back(value[2 * i]); << 184 dataVector.push_back(value[2*i+1]); 156 dataVector.push_back(value[2 * i + 1]); << 157 } 185 } 158 delete[] value; << 186 delete [] value; 159 << 160 Initialise(); << 161 return true; 187 return true; 162 } 188 } 163 << 189 164 // ------------------------------------------- << 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 << 174 // ------------------------------------------- << 175 std::size_t G4PhysicsVector::FindBin(const G4d << 176 std::size << 177 { << 178 if (idx + 1 < numberOfNodes && << 179 energy >= binVector[idx] && energy <= bi << 180 { << 181 return idx; << 182 } << 183 if (energy <= binVector[1]) << 184 { << 185 return 0; << 186 } << 187 if (energy >= binVector[idxmax]) << 188 { << 189 return idxmax; << 190 } << 191 return GetBin(energy); << 192 } << 193 << 194 // ------------------------------------------- << 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 << 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; << 225 } << 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 << 247 // spline is possible << 248 Initialise(); << 249 secDerivative.resize(numberOfNodes); << 250 << 251 if (1 < verboseLevel) << 252 { << 253 G4cout << "### G4PhysicsVector:: FillSecon << 254 << numberOfNodes << G4endl; << 255 DumpValues(); << 256 } << 257 << 258 switch(stype) << 259 { << 260 case G4SplineType::Base: << 261 ComputeSecDerivative1(); << 262 break; << 263 << 264 case G4SplineType::FixedEdges: << 265 ComputeSecDerivative2(dir1, dir2); << 266 break; << 267 << 268 default: << 269 ComputeSecDerivative0(); << 270 } << 271 } << 272 << 273 // ------------------------------------------- << 274 void G4PhysicsVector::ComputeSecDerivative0() << 275 // A simplified method of computation of seco << 276 { << 277 std::size_t n = numberOfNodes - 1; << 278 << 279 for (std::size_t i = 1; i < n; ++i) << 280 { << 281 secDerivative[i] = 3.0 * << 282 ((dataVector[i + 1] - dataVector[i]) / ( << 283 (dataVector[i] - dataVector[i - 1]) / << 284 (binVector[i] - binVector[i - 1])) / << 285 (binVector[i + 1] - binVector[i - 1]); << 286 } << 287 secDerivative[n] = secDerivative[n - 1]; << 288 secDerivative[0] = secDerivative[1]; << 289 } << 290 << 291 // ------------------------------------------- << 292 void G4PhysicsVector::ComputeSecDerivative1() << 293 // Computation of second derivatives using "No << 294 // B.I. Kvasov "Methods of shape-preserving sp << 295 // World Scientific, 2000 << 296 { << 297 std::size_t n = numberOfNodes - 1; << 298 auto u = new G4double[n]; << 299 G4double p, sig; << 300 << 301 u[1] = ((dataVector[2] - dataVector[1]) / (b << 302 (dataVector[1] - dataVector[0]) / (b << 303 u[1] = 6.0 * u[1] * (binVector[2] - binVecto << 304 ((binVector[2] - binVector[0]) * (bin << 305 << 306 // Decomposition loop for tridiagonal algori << 307 // and u[i] are used for temporary storage o << 308 << 309 secDerivative[1] = (2.0 * binVector[1] - bin << 310 (2.0 * binVector[2] - bin << 311 << 312 for(std::size_t i = 2; i < n - 1; ++i) << 313 { << 314 sig = << 315 (binVector[i] - binVector[i - 1]) / (bin << 316 p = sig * secDerivative[i - << 317 secDerivative[i] = (sig - 1.0) / p; << 318 u[i] = << 319 (dataVector[i + 1] - dataVector[i]) / (b << 320 (dataVector[i] - dataVector[i - 1]) / (b << 321 u[i] = << 322 (6.0 * u[i] / (binVector[i + 1] - binVec << 323 } << 324 << 325 sig = << 326 (binVector[n - 1] - binVector[n - 2]) / (b << 327 p = sig * secDerivative[n - 3] + 2.0; << 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 << 335 p = (1.0 + sig) + (2.0 * sig - 1.0) * secDer << 336 secDerivative[n - 1] = u[n - 1] / p; << 337 << 338 // The back-substitution loop for the triago << 339 // a linear system of equations. << 340 << 341 for (std::size_t k = n - 2; k > 1; --k) << 342 { << 343 secDerivative[k] *= << 344 (secDerivative[k + 1] - u[k] * (binVecto << 345 (binVector[k + << 346 } << 347 secDerivative[n] = << 348 (secDerivative[n - 1] - (1.0 - sig) * secD << 349 sig = 1.0 - ((binVector[2] - binVector[1]) / << 350 secDerivative[1] *= (secDerivative[2] - u[1] << 351 secDerivative[0] = (secDerivative[1] - sig * << 352 << 353 delete[] u; << 354 } << 355 << 356 // ------------------------------------------- << 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 << 374 // Decomposition loop for tridiagonal algori << 375 // and u[i] are used for temporary storage o << 376 << 377 for (std::size_t i = 1; i < n; ++i) << 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 << 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 } << 408 secDerivative[0] = 0.5 * (u[0] - secDerivati << 409 << 410 delete[] u; << 411 } << 412 << 413 // ------------------------------------------- << 414 std::ostream& operator<<(std::ostream& out, co 190 std::ostream& operator<<(std::ostream& out, const G4PhysicsVector& pv) 415 { 191 { 416 // binning 192 // binning 417 G4long prec = out.precision(); << 193 out << std::setprecision(12) << pv.edgeMin; 418 out << std::setprecision(12) << pv.edgeMin < << 194 out <<" " << pv.edgeMax <<" " << pv.numberOfBin << G4endl; 419 << pv.numberOfNodes << G4endl; << 420 195 421 // contents 196 // contents 422 out << pv.dataVector.size() << G4endl; << 197 out << pv.dataVector.size() << G4endl; 423 for (std::size_t i = 0; i < pv.dataVector.si << 198 for(size_t i = 0; i < pv.dataVector.size(); i++) 424 { 199 { 425 out << pv.binVector[i] << " " << pv.dataV << 200 out << std::setprecision(12) << pv.binVector[i] << " " >> 201 << pv.dataVector[i] << G4endl; 426 } 202 } 427 out.precision(prec); << 428 << 429 return out; 203 return out; 430 } 204 } 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 205