Geant4  10.00.p02
G4LatticeReader.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 //
28 //
29 // NOTE: This reader class for logical lattices should be moved to
30 // materials/ after the 10.0 release (and this comment removed).
31 // $Id: G4LatticeReader.cc 76885 2013-11-18 12:55:15Z gcosmo $
32 //
33 // 20131106 M.Kelsey -- Add const to getenv() to avoid compiler warning.
34 // 20131112 Throw exception if input file fails.
35 // 20131115 Check file input arguments for maps for validity before use;
36 // move ctor, dtor here; check stream pointer before closing.
37 
38 #include "G4LatticeReader.hh"
39 #include "G4ExceptionSeverity.hh"
40 #include "G4LatticeLogical.hh"
41 #include "G4SystemOfUnits.hh"
42 #include <fstream>
43 #include <limits>
44 #include <stdlib.h>
45 
46 
47 // Default path to lattice files, for use with filenames below
48 
50  getenv("G4LATTICEDATA") ? (const char*)getenv("G4LATTICEDATA") : "./CrystalMaps";
51 
52 
53 // Constructor and destructor
54 
56  : verboseLevel(vb), psLatfile(0), pLattice(0), fMapPath(""),
57  fToken(""), fValue(0.), fMap(""), fsPol(""), fPol(-1), fNX(0), fNY(0) {;}
58 
60  delete psLatfile; psLatfile = 0;
61 }
62 
63 
64 // Main drivers to read configuration from file or stream
65 
67  if (verboseLevel) G4cout << "G4LatticeReader " << filename << G4endl;
68 
69  if (!OpenFile(filename)) {
71  msg << "Unable to open " << filename;
72  G4Exception("G4LatticeReader::MakeLattice", "Lattice001",
73  FatalException, msg);
74  return 0;
75  }
76 
77  pLattice = new G4LatticeLogical; // Create lattice to be filled
78 
79  G4bool goodLattice = true;
80  while (!psLatfile->eof()) {
81  goodLattice &= ProcessToken();
82  }
83  CloseFile();
84 
85  if (!goodLattice) {
87  msg << "Error reading lattice from " << filename;
88  G4Exception("G4LatticeReader::MakeLattice", "Lattice002",
89  FatalException, msg);
90  delete pLattice;
91  pLattice = 0;
92  }
93 
94  return pLattice; // Lattice complete; return pointer with ownership
95 }
96 
97 
98 // Open local file or file found under data path
99 
101  if (verboseLevel)
102  G4cout << "G4LatticeReader::OpenFile " << filename << G4endl;
103 
104  G4String filepath = filename;
105  psLatfile = new std::ifstream(filepath);
106  if (!psLatfile->good()) { // Local file not found
107  filepath = fDataDir + "/" + filename;
108  psLatfile->open(filepath); // Try data directory
109  if (!psLatfile->good()) {
110  CloseFile();
111  return false;
112  }
113  if (verboseLevel>1) G4cout << " Found file " << filepath << G4endl;
114  }
115 
116  // Extract path from filename to use in finding .ssv map files
117  size_t lastdir = filepath.last('/');
118  if (lastdir == std::string::npos) fMapPath = "."; // No path at all
119  else fMapPath = filepath(0,lastdir);
120 
121  return true;
122 }
123 
124 // Close and delete input stream
125 
127  if (psLatfile) psLatfile->close();
128  delete psLatfile;
129  psLatfile = 0;
130 }
131 
132 
133 // Read next token from file, use it to store next data into lattice
134 
136  fToken = "";
137  *psLatfile >> fToken;
138  if (fToken.empty() || psLatfile->eof()) return true; // End of file reached
139 
140  if (verboseLevel>1) G4cout << " ProcessToken " << fToken << G4endl;
141 
142  fToken.toLower();
143  if (fToken.contains('#')) return SkipComments(); // Ignore rest of line
144  if (fToken == "vdir") return ProcessNMap(); // Direction vector map
145  if (fToken == "vg") return ProcessMap(); // Velocity magnitudes
146  if (fToken == "dyn") return ProcessConstants(); // Dynamical parameters
147  return ProcessValue(fToken); // Single numeric value
148 }
149 
150 // Eat remainder of line, assuming a '#' token was found
151 
154  return true; // Never fails
155 }
156 
157 // Read double value from file, store based on name string
158 
160  *psLatfile >> fValue;
161  if (verboseLevel>1) G4cout << " ProcessValue " << fValue << G4endl;
162 
163  G4bool good = true;
164  /***** NOTE: Individual Set functions not included in Release 10.0
165  if (name == "beta") pLattice->SetBeta(fValue);
166  else if (name == "gamma") pLattice->SetGamma(fValue);
167  else if (name == "lambda") pLattice->SetLambda(fValue);
168  else if (name == "mu") pLattice->SetMu(fValue);
169  else *****/
170  if (name == "scat") pLattice->SetScatteringConstant(fValue*s*s*s);
171  else if (name == "b") pLattice->SetScatteringConstant(fValue*s*s*s);
172  else if (name == "decay") pLattice->SetAnhDecConstant(fValue*s*s*s*s);
173  else if (name == "a") pLattice->SetAnhDecConstant(fValue*s*s*s*s);
174  else if (name == "ldos") pLattice->SetLDOS(fValue);
175  else if (name == "stdos") pLattice->SetSTDOS(fValue);
176  else if (name == "ftdos") pLattice->SetFTDOS(fValue);
177  else {
178  G4cerr << "G4LatticeReader: Unrecognized token " << name << G4endl;
179  good = false;
180  }
181 
182  return good;
183 }
184 
186  G4double beta=0., gamma=0., lambda=0., mu=0.;
187  *psLatfile >> beta >> gamma >> lambda >> mu;
188  if (verboseLevel>1)
189  G4cout << " ProcessConstants " << beta << " " << gamma
190  << " " << lambda << " " << mu << G4endl;
191 
192  pLattice->SetDynamicalConstants(beta, gamma, lambda, mu);
193  return psLatfile->good();
194 }
195 
196 // Read map filename, polarization, and binning dimensions
197 
199  *psLatfile >> fMap >> fsPol >> fNX >> fNY;
200  if (verboseLevel>1)
201  G4cout << " ReadMapInfo " << fMap << " " << fsPol
202  << " " << fNX << " " << fNY << G4endl;
203 
204  if (fNX < 0 || fNX >= G4LatticeLogical::MAXRES) {
205  G4cerr << "G4LatticeReader: Invalid map theta dimension " << fNX << G4endl;
206  return false;
207  }
208 
209  if (fNY < 0 || fNY >= G4LatticeLogical::MAXRES) {
210  G4cerr << "G4LatticeReader: Invalid map phi dimension " << fNY << G4endl;
211  return false;
212  }
213 
214  // Prepend path to data files to map filename
215  fMap = fMapPath + "/" + fMap;
216 
217  // Convert string code (L,ST,LT) to polarization index
218  fsPol.toLower();
219  fPol = ( (fsPol=="l") ? 0 : // Longitudinal
220  (fsPol=="st") ? 1 : // Slow-transverse
221  (fsPol=="ft") ? 2 : // Fast-transverse
222  -1 ); // Invalid code
223 
224  if (fPol<0 || fPol>2) {
225  G4cerr << "G4LatticeReader: Invalid polarization code " << fsPol << G4endl;
226  return false;
227  }
228 
229  return true;
230 }
231 
233  if (!ReadMapInfo()) { // Get specific parameters for map to load
234  G4cerr << "G4LatticeReader: Unable to process mapfile directive." << G4endl;
235  return false;
236  }
237 
238  return pLattice->LoadMap(fNX, fNY, fPol, fMap);
239 }
240 
242  if (!ReadMapInfo()) { // Get specific parameters for map to load
243  G4cerr << "G4LatticeReader: Unable to process mapfile directive." << G4endl;
244  return false;
245  }
246 
247  return pLattice->Load_NMap(fNX, fNY, fPol, fMap);
248 }
void SetFTDOS(G4double FTDOS)
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4bool LoadMap(G4int, G4int, G4int, G4String)
G4String name
Definition: TRTMaterials.hh:40
int G4int
Definition: G4Types.hh:78
static const double s
Definition: G4SIunits.hh:150
G4bool ProcessConstants()
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
void SetAnhDecConstant(G4double a)
void SetScatteringConstant(G4double b)
void toLower()
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
void SetLDOS(G4double LDOS)
T max(const T t1, const T t2)
brief Return the largest of the two arguments
G4int last(char) const
void SetSTDOS(G4double STDOS)
G4bool OpenFile(const G4String &filepath)
Definition of the G4LatticeLogical class.
G4LatticeLogical * pLattice
#define G4endl
Definition: G4ios.hh:61
Definition of the G4LatticeReader class.
G4LatticeLogical * MakeLattice(const G4String &filepath)
G4bool Load_NMap(G4int, G4int, G4int, G4String)
double G4double
Definition: G4Types.hh:76
void SetDynamicalConstants(G4double Beta, G4double Gamma, G4double Lambda, G4double Mu)
static const G4String fDataDir
G4LatticeReader(G4int vb=0)
G4GLOB_DLL std::ostream G4cerr
G4bool ProcessValue(const G4String &name)
std::ifstream * psLatfile