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.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

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