Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
69
70 ClearTable();
71}
72
73// #########################################################################
74
76
77 return IsApplicable( ionZ, matZ );
78}
79
80
81// #########################################################################
82
84 const G4String& matName) {
85
86 return IsApplicable( ionZ, matName );
87}
88
89// #########################################################################
90
92 G4int atomicNumberIon, // Atomic number of ion
93 G4int atomicNumberElem // Atomic number of elemental material
94 )
95{
96 G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
97
98 auto iter = dedxMapElements.find(key);
99
100 return iter != dedxMapElements.end();
101}
102
103// #########################################################################
104
106 G4int atomicNumberIon, // Atomic number of ion
107 const G4String& matIdentifier // Name or chemical formula of material
108 )
109{
110 G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
111
112 auto iter = dedxMapMaterials.find(key);
113
114 return iter != dedxMapMaterials.end();
115}
116
117// #########################################################################
118
120 G4int atomicNumberIon, // Atomic number of ion
121 G4int atomicNumberElem // Atomic number of elemental material
122 )
123{
124 G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
125
126 auto iter = dedxMapElements.find(key);
127
128 return (iter != dedxMapElements.end()) ? iter->second : nullptr;
129}
130
131// #########################################################################
132
134 G4int atomicNumberIon, // Atomic number of ion
135 const G4String& matIdentifier // Name or chemical formula of material
136 )
137{
138 G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
139
140 auto iter = dedxMapMaterials.find(key);
141
142 return (iter != dedxMapMaterials.end()) ? iter->second : nullptr;
143}
144
145// #########################################################################
146
148 G4double kinEnergyPerNucleon, // Kinetic energy per nucleon
149 G4int atomicNumberIon, // Atomic number of ion
150 G4int atomicNumberElem // Atomic number of elemental material
151 )
152{
153 G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
154
155 auto iter = dedxMapElements.find(key);
156
157 return ( iter != dedxMapElements.end() ) ?
158 (iter->second)->Value( kinEnergyPerNucleon) : 0.0;
159}
160
161// #########################################################################
162
164 G4double kinEnergyPerNucleon, // Kinetic energy per nucleon
165 G4int atomicNumberIon, // Atomic number of ion
166 const G4String& matIdentifier // Name or chemical formula of material
167 )
168{
169 G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
170
171 auto iter = dedxMapMaterials.find(key);
172
173 return (iter != dedxMapMaterials.end()) ?
174 (iter->second)->Value( kinEnergyPerNucleon) : 0.0;
175}
176
177// #########################################################################
178
180 G4PhysicsVector* physicsVector, // Physics vector
181 G4int atomicNumberIon, // Atomic number of ion
182 const G4String& matIdentifier, // Name of elemental material
183 G4int atomicNumberElem // Atomic number of elemental material
184 ) {
185
186 if(physicsVector == nullptr) {
187 G4Exception ("G4ExtDEDXTable::AddPhysicsVector() for material",
188 "mat037", FatalException,
189 "Pointer to vector is null-pointer.");
190 return false;
191 }
192
193 if(matIdentifier.empty()) {
194 G4Exception ("G4ExtDEDXTable::AddPhysicsVector() for material",
195 "mat038", FatalException, "Invalid name of the material.");
196 return false;
197 }
198
199 if(atomicNumberIon <= 2) {
200 G4Exception ("G4ExtDEDXTable::AddPhysicsVector() for material",
201 "mat039", FatalException, "Illegal atomic number.");
202 return false;
203 }
204
205 if(atomicNumberElem > 0) {
206
207 G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
208
209 if(dedxMapElements.count(key) == 1) {
210 G4Exception ("G4ExtDEDXTable::AddPhysicsVector() for material",
211 "mat037", FatalException,
212 "Vector already exist, remove it before replacing.");
213 return false;
214 }
215
216 dedxMapElements[key] = physicsVector;
217 }
218
219 G4IonDEDXKeyMat mkey = std::make_pair(atomicNumberIon, matIdentifier);
220
221 if(dedxMapMaterials.count(mkey) == 1) {
222 G4Exception ("G4ExtDEDXTable::AddPhysicsVector() for material",
223 "mat037", FatalException,
224 "Vector already exist, remove it before replacing.");
225 return false;
226 }
227
228 dedxMapMaterials[mkey] = physicsVector;
229
230 return true;
231}
232
233// #########################################################################
234
236 G4int atomicNumberIon, // Atomic number of ion
237 const G4String& matIdentifier // Name or chemical formula of material
238 ) {
239
240 G4PhysicsVector* physicsVector = nullptr;
241
242 // Deleting key of physics vector from material map
243 G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
244
245 auto iter = dedxMapMaterials.find(key);
246
247 if(iter == dedxMapMaterials.end()) {
248 G4Exception ("G4ExtDEDXTable::RemovePhysicsVector() for material",
249 "mat037", FatalException,
250 "Pointer to vector is null-pointer.");
251 return false;
252 }
253
254 physicsVector = (*iter).second;
255 dedxMapMaterials.erase(key);
256
257 // Deleting key of physics vector from elemental material map (if it exists)
258 G4IonDEDXMapElem::iterator it;
259
260 for(it=dedxMapElements.begin(); it!=dedxMapElements.end(); ++it) {
261
262 if( (*it).second == physicsVector ) {
263 dedxMapElements.erase(it);
264 break;
265 }
266 }
267
268 // Deleting physics vector
269 delete physicsVector;
270
271 return true;
272}
273
274// #########################################################################
275
277 const G4String& fileName // File name
278 ) {
279 G4bool success = true;
280
281 std::ofstream ofilestream;
282
283 ofilestream.open( fileName, std::ios::out );
284
285 if( !ofilestream ) {
287 ed << "Cannot open file " << fileName;
288 G4Exception ("G4IonStoppingData::StorePhysicsTable()",
289 "mat030", FatalException, ed);
290 success = false;
291 }
292 else {
293
294 size_t nmbMatTables = dedxMapMaterials.size();
295
296 ofilestream << nmbMatTables << G4endl << G4endl;
297
298 auto iterMat = dedxMapMaterials.begin();
299 auto iterMat_end = dedxMapMaterials.end();
300
301 for(;iterMat != iterMat_end; iterMat++) {
302 G4IonDEDXKeyMat key = iterMat -> first;
303 G4PhysicsVector* physicsVector = iterMat -> second;
304
305 G4int atomicNumberIon = key.first;
306 G4String matIdentifier = key.second;
307
308 G4int atomicNumberElem = FindAtomicNumberElement(physicsVector);
309
310 if(physicsVector != nullptr) {
311 ofilestream << atomicNumberIon << " " << matIdentifier;
312
313 if(atomicNumberElem > 0)
314 {
315 ofilestream << " " << atomicNumberElem;
316 }
317
318 ofilestream << " # <Atomic number ion> <Material name> ";
319
320 if(atomicNumberElem > 0)
321 {
322 ofilestream << "<Atomic number element>";
323 }
324
325 ofilestream << G4endl << physicsVector -> GetType() << G4endl;
326
327 physicsVector -> Store(ofilestream, true);
328
329 ofilestream << G4endl;
330 } else {
331 G4Exception ("G4IonStoppingData::StorePhysicsTable()",
332 "mat030", FatalException,"Cannot store vector.");
333 }
334 }
335 }
336
337 ofilestream.close();
338
339 return success;
340}
341
342// #########################################################################
343
345{
346 std::ifstream ifilestream;
347 ifilestream.open( fileName, std::ios::in|std::ios::binary );
348 if( ! ifilestream ) {
350 ed << "Cannot open file " << fileName;
351 G4Exception ("G4IonStoppingData::RetrievePhysicsTable()",
352 "mat030", FatalException, ed);
353 return false;
354 }
355
356 //std::string::size_type nmbVectors;
357 G4int nmbVectors = 0;
358 ifilestream >> nmbVectors;
359 if( ifilestream.fail() || nmbVectors <= 0) {
360 G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() "
361 << " File content of " << fileName << " ill-formated."
362 << " Nvectors= " << nmbVectors
363 << G4endl;
364 ifilestream.close();
365 return false;
366 }
367
368 for(G4int i = 0; i<nmbVectors; ++i) {
369
370 G4String line = "";
371 // Loop checking, 07-Aug-2015, Vladimir Ivanchenko
372 while( line.empty() ) {
373
374 getline( ifilestream, line );
375 if( ifilestream.fail() ) {
376 G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() "
377 << " File content of " << fileName << " ill-formated."
378 << G4endl;
379 ifilestream.close();
380 return false;
381 }
382
383 std::string::size_type pos = line.find_first_of("#");
384 if(pos != std::string::npos && pos > 0) {
385 line = line.substr(0, pos);
386 }
387 }
388
389 std::istringstream headerstream( line );
390
391 std::string::size_type atomicNumberIon;
392 headerstream >> atomicNumberIon;
393
394 G4String materialName;
395 headerstream >> materialName;
396
397 if( headerstream.fail() || std::string::npos == atomicNumberIon) {
398 G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() "
399 << " File content of " << fileName << " ill-formated "
400 << " (vector header)."
401 << G4endl;
402 ifilestream.close();
403 return false;
404 }
405
406 std::string::size_type atomicNumberMat;
407 headerstream >> atomicNumberMat;
408
409 if( headerstream.eof() || std::string::npos == atomicNumberMat) {
410 atomicNumberMat = 0;
411 }
412
413 G4int vectorType;
414 ifilestream >> vectorType;
415
416 G4PhysicsVector* physicsVector = CreatePhysicsVector(vectorType);
417
418 if(physicsVector == nullptr) {
419 G4cout << "G4ExtDEDXTable::RetrievePhysicsTable "
420 << " illegal physics Vector type " << vectorType
421 << " in " << fileName
422 << G4endl;
423 ifilestream.close();
424 return false;
425 }
426
427 if( !physicsVector -> Retrieve(ifilestream, true) ) {
428 G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() "
429 << " File content of " << fileName << " ill-formated."
430 << G4endl;
431 ifilestream.close();
432 return false;
433 }
434 physicsVector -> FillSecondDerivatives();
435
436 // Retrieved vector is added to material store
437 if( !AddPhysicsVector(physicsVector, (G4int)atomicNumberIon,
438 materialName, (G4int)atomicNumberMat) ) {
439
440 delete physicsVector;
441 ifilestream.close();
442 return false;
443 }
444 }
445
446 ifilestream.close();
447
448 return true;
449}
450
451// #########################################################################
452
453G4PhysicsVector* G4ExtDEDXTable::CreatePhysicsVector(G4int vectorType) {
454
455 G4PhysicsVector* physicsVector = nullptr;
456
457 switch (vectorType) {
458
460 physicsVector = new G4PhysicsLinearVector(true);
461 break;
462
464 physicsVector = new G4PhysicsLogVector(true);
465 break;
466
468 physicsVector = new G4PhysicsFreeVector(true);
469 break;
470
471 default:
472 break;
473 }
474 return physicsVector;
475}
476
477// #########################################################################
478
479G4int G4ExtDEDXTable::FindAtomicNumberElement(
480 G4PhysicsVector* physicsVector
481 ) {
482
483 G4int atomicNumber = 0;
484
485 auto iter = dedxMapElements.begin();
486 auto iter_end = dedxMapElements.end();
487
488 for(;iter != iter_end; ++iter) {
489
490 if( (*iter).second == physicsVector ) {
491
492 G4IonDEDXKeyElem key = (*iter).first;
493 atomicNumber = key.second;
494 }
495 }
496
497 return atomicNumber;
498}
499
500// #########################################################################
501
503 auto iterMat = dedxMapMaterials.begin();
504 auto iterMat_end = dedxMapMaterials.end();
505
506 for(;iterMat != iterMat_end; ++iterMat) {
507
508 G4PhysicsVector* vec = iterMat -> second;
509
510 delete vec;
511 }
512
513 dedxMapElements.clear();
514 dedxMapMaterials.clear();
515}
516
517// #########################################################################
518
520 auto iterMat = dedxMapMaterials.begin();
521 auto iterMat_end = dedxMapMaterials.end();
522
523 G4cout << std::setw(15) << std::right
524 << "Atomic nmb ion"
525 << std::setw(25) << std::right
526 << "Material name"
527 << std::setw(25) << std::right
528 << "Atomic nmb material"
529 << G4endl;
530
531 for(;iterMat != iterMat_end; ++iterMat) {
532 G4IonDEDXKeyMat key = iterMat -> first;
533 G4PhysicsVector* physicsVector = iterMat -> second;
534
535 G4int atomicNumberIon = key.first;
536 G4String matIdentifier = key.second;
537
538 G4int atomicNumberElem = FindAtomicNumberElement(physicsVector);
539
540 if(physicsVector != nullptr)
541 {
542 G4cout << std::setw(15) << std::right << atomicNumberIon
543 << std::setw(25) << std::right << matIdentifier << std::setw(25)
544 << std::right;
545
546 if(atomicNumberElem > 0)
547 {
548 G4cout << atomicNumberElem;
549 }
550 else
551 {
552 G4cout << "N/A";
553 }
554
555 G4cout << G4endl;
556 }
557 }
558
559}
560
561// #########################################################################
562
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:59
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
@ T_G4PhysicsFreeVector
@ T_G4PhysicsLinearVector
@ T_G4PhysicsLogVector
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
~G4ExtDEDXTable() override
G4double GetDEDX(G4double kinEnergyPerNucleon, G4int atomicNumberIon, G4int atomicNumberElem)
G4bool StorePhysicsTable(const G4String &fileName)
G4PhysicsVector * GetPhysicsVector(G4int atomicNumberIon, G4int atomicNumberElem) override
G4bool RemovePhysicsVector(G4int atomicNumberIon, const G4String &matIdentifier)
G4bool IsApplicable(G4int atomicNumberIon, G4int atomicNumberElem) override
G4bool AddPhysicsVector(G4PhysicsVector *physicsVector, G4int atomicNumberIon, const G4String &matIdenfier, G4int atomicNumberElem=0)
G4bool BuildPhysicsVector(G4int ionZ, const G4String &matName) override
G4bool RetrievePhysicsTable(const G4String &fileName)