Geant4-11
G4ExtDEDXTable.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27// ===========================================================================
28// GEANT4 class source file
29//
30// Class: G4ExtDEDXTable
31//
32// Base class: G4VIonDEDXTable
33//
34// Author: Anton Lechner (Anton.Lechner@cern.ch)
35//
36// First implementation: 29. 02. 2009
37//
38// Modifications:
39// 03.11.2009 A. Lechner: Added new methods BuildPhysicsVector according
40// to interface changes in base class G4VIonDEDXTable.
41// 25.10.2010 V.Ivanchenko fixed bug in usage of iterators reported by the
42// Coverity tool
43// 01.11.2010 V.Ivanchenko fixed remaining bugs reported by Coverity
44//
45//
46// Class description:
47// Utility class for users to add their own electronic stopping powers
48// for ions. This class is dedicated for use with G4IonParametrisedLossModel
49// of the low-energy electromagnetic package.
50//
51// Comments:
52//
53// ===========================================================================
54//
55
56#include "G4ExtDEDXTable.hh"
57#include "G4PhysicsVector.hh"
60#include "G4PhysicsLogVector.hh"
62#include <fstream>
63#include <sstream>
64#include <iomanip>
65
66
67// #########################################################################
68
70
71}
72
73// #########################################################################
74
76
77 ClearTable();
78}
79
80// #########################################################################
81
83
84 return IsApplicable( ionZ, matZ );
85}
86
87
88// #########################################################################
89
91 const G4String& matName) {
92
93 return IsApplicable( ionZ, matName );
94}
95
96// #########################################################################
97
99 G4int atomicNumberIon, // Atomic number of ion
100 G4int atomicNumberElem // Atomic number of elemental material
101 )
102{
103 G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
104
105 G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
106
107 return (iter == dedxMapElements.end()) ? false : true;
108}
109
110// #########################################################################
111
113 G4int atomicNumberIon, // Atomic number of ion
114 const G4String& matIdentifier // Name or chemical formula of material
115 )
116{
117 G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
118
119 G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
120
121 return (iter == dedxMapMaterials.end()) ? false : true;
122}
123
124// #########################################################################
125
127 G4int atomicNumberIon, // Atomic number of ion
128 G4int atomicNumberElem // Atomic number of elemental material
129 )
130{
131 G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
132
133 G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
134
135 return (iter != dedxMapElements.end()) ? iter->second : nullptr;
136}
137
138// #########################################################################
139
141 G4int atomicNumberIon, // Atomic number of ion
142 const G4String& matIdentifier // Name or chemical formula of material
143 )
144{
145 G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
146
147 G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
148
149 return (iter != dedxMapMaterials.end()) ? iter->second : nullptr;
150}
151
152// #########################################################################
153
155 G4double kinEnergyPerNucleon, // Kinetic energy per nucleon
156 G4int atomicNumberIon, // Atomic number of ion
157 G4int atomicNumberElem // Atomic number of elemental material
158 )
159{
160 G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
161
162 G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
163
164 return ( iter != dedxMapElements.end() ) ?
165 (iter->second)->Value( kinEnergyPerNucleon) : 0.0;
166}
167
168// #########################################################################
169
171 G4double kinEnergyPerNucleon, // Kinetic energy per nucleon
172 G4int atomicNumberIon, // Atomic number of ion
173 const G4String& matIdentifier // Name or chemical formula of material
174 )
175{
176 G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
177
178 G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
179
180 return (iter != dedxMapMaterials.end()) ?
181 (iter->second)->Value( kinEnergyPerNucleon) : 0.0;
182}
183
184// #########################################################################
185
187 G4PhysicsVector* physicsVector, // Physics vector
188 G4int atomicNumberIon, // Atomic number of ion
189 const G4String& matIdentifier, // Name of elemental material
190 G4int atomicNumberElem // Atomic number of elemental material
191 ) {
192
193 if(physicsVector == nullptr) {
194 G4Exception ("G4ExtDEDXTable::AddPhysicsVector() for material",
195 "mat037", FatalException,
196 "Pointer to vector is null-pointer.");
197 return false;
198 }
199
200 if(matIdentifier.empty()) {
201 G4Exception ("G4ExtDEDXTable::AddPhysicsVector() for material",
202 "mat038", FatalException, "Invalid name of the material.");
203 return false;
204 }
205
206 if(atomicNumberIon <= 2) {
207 G4Exception ("G4ExtDEDXTable::AddPhysicsVector() for material",
208 "mat039", FatalException, "Illegal atomic number.");
209 return false;
210 }
211
212 if(atomicNumberElem > 0) {
213
214 G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
215
216 if(dedxMapElements.count(key) == 1) {
217 G4Exception ("G4ExtDEDXTable::AddPhysicsVector() for material",
218 "mat037", FatalException,
219 "Vector already exist, remove it before replacing.");
220 return false;
221 }
222
223 dedxMapElements[key] = physicsVector;
224 }
225
226 G4IonDEDXKeyMat mkey = std::make_pair(atomicNumberIon, matIdentifier);
227
228 if(dedxMapMaterials.count(mkey) == 1) {
229 G4Exception ("G4ExtDEDXTable::AddPhysicsVector() for material",
230 "mat037", FatalException,
231 "Vector already exist, remove it before replacing.");
232 return false;
233 }
234
235 dedxMapMaterials[mkey] = physicsVector;
236
237 return true;
238}
239
240// #########################################################################
241
243 G4int atomicNumberIon, // Atomic number of ion
244 const G4String& matIdentifier // Name or chemical formula of material
245 ) {
246
247 G4PhysicsVector* physicsVector = nullptr;
248
249 // Deleting key of physics vector from material map
250 G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
251
252 G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
253
254 if(iter == dedxMapMaterials.end()) {
255 G4Exception ("G4ExtDEDXTable::RemovePhysicsVector() for material",
256 "mat037", FatalException,
257 "Pointer to vector is null-pointer.");
258 return false;
259 }
260
261 physicsVector = (*iter).second;
262 dedxMapMaterials.erase(key);
263
264 // Deleting key of physics vector from elemental material map (if it exists)
265 G4IonDEDXMapElem::iterator it;
266
267 for(it=dedxMapElements.begin(); it!=dedxMapElements.end(); ++it) {
268
269 if( (*it).second == physicsVector ) {
270 dedxMapElements.erase(it);
271 break;
272 }
273 }
274
275 // Deleting physics vector
276 delete physicsVector;
277
278 return true;
279}
280
281// #########################################################################
282
284 const G4String& fileName // File name
285 ) {
286 G4bool success = true;
287
288 std::ofstream ofilestream;
289
290 ofilestream.open( fileName, std::ios::out );
291
292 if( !ofilestream ) {
294 ed << "Cannot open file " << fileName;
295 G4Exception ("G4IonStoppingData::StorePhysicsTable()",
296 "mat030", FatalException, ed);
297 success = false;
298 }
299 else {
300
301 size_t nmbMatTables = dedxMapMaterials.size();
302
303 ofilestream << nmbMatTables << G4endl << G4endl;
304
305 G4IonDEDXMapMat::iterator iterMat = dedxMapMaterials.begin();
306 G4IonDEDXMapMat::iterator iterMat_end = dedxMapMaterials.end();
307
308 for(;iterMat != iterMat_end; iterMat++) {
309 G4IonDEDXKeyMat key = iterMat -> first;
310 G4PhysicsVector* physicsVector = iterMat -> second;
311
312 G4int atomicNumberIon = key.first;
313 G4String matIdentifier = key.second;
314
315 G4int atomicNumberElem = FindAtomicNumberElement(physicsVector);
316
317 if(physicsVector != nullptr) {
318 ofilestream << atomicNumberIon << " " << matIdentifier;
319
320 if(atomicNumberElem > 0) ofilestream << " " << atomicNumberElem;
321
322 ofilestream << " # <Atomic number ion> <Material name> ";
323
324 if(atomicNumberElem > 0) ofilestream << "<Atomic number element>";
325
326 ofilestream << G4endl << physicsVector -> GetType() << G4endl;
327
328 physicsVector -> Store(ofilestream, true);
329
330 ofilestream << G4endl;
331 } else {
332 G4Exception ("G4IonStoppingData::StorePhysicsTable()",
333 "mat030", FatalException,"Cannot store vector.");
334 }
335 }
336 }
337
338 ofilestream.close();
339
340 return success;
341}
342
343// #########################################################################
344
346{
347 std::ifstream ifilestream;
348 ifilestream.open( fileName, std::ios::in|std::ios::binary );
349 if( ! ifilestream ) {
351 ed << "Cannot open file " << fileName;
352 G4Exception ("G4IonStoppingData::RetrievePhysicsTable()",
353 "mat030", FatalException, ed);
354 return false;
355 }
356
357 //std::string::size_type nmbVectors;
358 G4int nmbVectors = 0;
359 ifilestream >> nmbVectors;
360 if( ifilestream.fail() || nmbVectors <= 0) {
361 G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() "
362 << " File content of " << fileName << " ill-formated."
363 << " Nvectors= " << nmbVectors
364 << G4endl;
365 ifilestream.close();
366 return false;
367 }
368
369 for(G4int i = 0; i<nmbVectors; ++i) {
370
371 G4String line = "";
372 // Loop checking, 07-Aug-2015, Vladimir Ivanchenko
373 while( line.empty() ) {
374
375 getline( ifilestream, line );
376 if( ifilestream.fail() ) {
377 G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() "
378 << " File content of " << fileName << " ill-formated."
379 << G4endl;
380 ifilestream.close();
381 return false;
382 }
383
384 std::string::size_type pos = line.find_first_of("#");
385 if(pos != std::string::npos && pos > 0) {
386 line = line.substr(0, pos);
387 }
388 }
389
390 std::istringstream headerstream( line );
391
392 std::string::size_type atomicNumberIon;
393 headerstream >> atomicNumberIon;
394
395 G4String materialName;
396 headerstream >> materialName;
397
398 if( headerstream.fail() || std::string::npos == atomicNumberIon) {
399 G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() "
400 << " File content of " << fileName << " ill-formated "
401 << " (vector header)."
402 << G4endl;
403 ifilestream.close();
404 return false;
405 }
406
407 std::string::size_type atomicNumberMat;
408 headerstream >> atomicNumberMat;
409
410 if( headerstream.eof() || std::string::npos == atomicNumberMat) {
411 atomicNumberMat = 0;
412 }
413
414 G4int vectorType;
415 ifilestream >> vectorType;
416
417 G4PhysicsVector* physicsVector = CreatePhysicsVector(vectorType);
418
419 if(physicsVector == nullptr) {
420 G4cout << "G4ExtDEDXTable::RetrievePhysicsTable "
421 << " illegal physics Vector type " << vectorType
422 << " in " << fileName
423 << G4endl;
424 ifilestream.close();
425 return false;
426 }
427
428 if( !physicsVector -> Retrieve(ifilestream, true) ) {
429 G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() "
430 << " File content of " << fileName << " ill-formated."
431 << G4endl;
432 ifilestream.close();
433 return false;
434 }
435 physicsVector -> FillSecondDerivatives();
436
437 // Retrieved vector is added to material store
438 if( !AddPhysicsVector(physicsVector, (G4int)atomicNumberIon,
439 materialName, (G4int)atomicNumberMat) ) {
440
441 delete physicsVector;
442 ifilestream.close();
443 return false;
444 }
445 }
446
447 ifilestream.close();
448
449 return true;
450}
451
452// #########################################################################
453
455
456 G4PhysicsVector* physicsVector = nullptr;
457
458 switch (vectorType) {
459
461 physicsVector = new G4PhysicsLinearVector(true);
462 break;
463
465 physicsVector = new G4PhysicsLogVector(true);
466 break;
467
469 physicsVector = new G4PhysicsFreeVector(true);
470 break;
471
472 default:
473 break;
474 }
475 return physicsVector;
476}
477
478// #########################################################################
479
481 G4PhysicsVector* physicsVector
482 ) {
483
484 G4int atomicNumber = 0;
485
486 G4IonDEDXMapElem::iterator iter = dedxMapElements.begin();
487 G4IonDEDXMapElem::iterator iter_end = dedxMapElements.end();
488
489 for(;iter != iter_end; ++iter) {
490
491 if( (*iter).second == physicsVector ) {
492
493 G4IonDEDXKeyElem key = (*iter).first;
494 atomicNumber = key.second;
495 }
496 }
497
498 return atomicNumber;
499}
500
501// #########################################################################
502
504
505 G4IonDEDXMapMat::iterator iterMat = dedxMapMaterials.begin();
506 G4IonDEDXMapMat::iterator iterMat_end = dedxMapMaterials.end();
507
508 for(;iterMat != iterMat_end; ++iterMat) {
509
510 G4PhysicsVector* vec = iterMat -> second;
511
512 if(vec != 0) delete vec;
513 }
514
515 dedxMapElements.clear();
516 dedxMapMaterials.clear();
517}
518
519// #########################################################################
520
522
523 G4IonDEDXMapMat::iterator iterMat = dedxMapMaterials.begin();
524 G4IonDEDXMapMat::iterator iterMat_end = dedxMapMaterials.end();
525
526 G4cout << std::setw(15) << std::right
527 << "Atomic nmb ion"
528 << std::setw(25) << std::right
529 << "Material name"
530 << std::setw(25) << std::right
531 << "Atomic nmb material"
532 << G4endl;
533
534 for(;iterMat != iterMat_end; ++iterMat) {
535 G4IonDEDXKeyMat key = iterMat -> first;
536 G4PhysicsVector* physicsVector = iterMat -> second;
537
538 G4int atomicNumberIon = key.first;
539 G4String matIdentifier = key.second;
540
541 G4int atomicNumberElem = FindAtomicNumberElement(physicsVector);
542
543 if(physicsVector != 0) {
544 G4cout << std::setw(15) << std::right
545 << atomicNumberIon
546 << std::setw(25) << std::right
547 << matIdentifier
548 << std::setw(25) << std::right;
549
550 if(atomicNumberElem > 0) G4cout << atomicNumberElem;
551 else G4cout << "N/A";
552
553 G4cout << G4endl;
554 }
555 }
556
557}
558
559// #########################################################################
560
static const G4double pos
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
@ T_G4PhysicsFreeVector
@ T_G4PhysicsLinearVector
@ T_G4PhysicsLogVector
static constexpr double second
Definition: G4SIunits.hh:137
double G4double
Definition: G4Types.hh:83
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
virtual G4PhysicsVector * GetPhysicsVector(G4int atomicNumberIon, G4int atomicNumberElem)
virtual G4bool BuildPhysicsVector(G4int ionZ, const G4String &matName)
virtual G4bool IsApplicable(G4int atomicNumberIon, G4int atomicNumberElem)
G4double GetDEDX(G4double kinEnergyPerNucleon, G4int atomicNumberIon, G4int atomicNumberElem)
G4IonDEDXMapMat dedxMapMaterials
G4bool StorePhysicsTable(const G4String &fileName)
std::pair< G4int, G4String > G4IonDEDXKeyMat
G4bool RemovePhysicsVector(G4int atomicNumberIon, const G4String &matIdentifier)
G4bool AddPhysicsVector(G4PhysicsVector *physicsVector, G4int atomicNumberIon, const G4String &matIdenfier, G4int atomicNumberElem=0)
std::pair< G4int, G4int > G4IonDEDXKeyElem
G4PhysicsVector * CreatePhysicsVector(G4int vectorType)
virtual ~G4ExtDEDXTable()
G4int FindAtomicNumberElement(G4PhysicsVector *physicsVector)
G4bool RetrievePhysicsTable(const G4String &fileName)
G4IonDEDXMapElem dedxMapElements