Geant4  10.02.p02
G4LatticeManager.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 // $Id: G4LatticeManager.cc 84196 2014-10-10 14:31:48Z gcosmo $
30 //
31 // 20131113 Delete lattices in (new) registry, not in lookup maps
32 // 20141008 Change to global singleton; must be shared across worker threads
33 
34 #include "G4LatticeManager.hh"
35 #include "G4AutoLock.hh"
36 #include "G4LatticeLogical.hh"
37 #include "G4LatticePhysical.hh"
38 #include "G4LatticeReader.hh"
39 #include "G4LogicalVolume.hh"
40 #include "G4Material.hh"
41 #include "G4VPhysicalVolume.hh"
42 #include "G4SystemOfUnits.hh"
43 #include "G4Threading.hh"
44 #include <fstream>
45 
46 G4LatticeManager* G4LatticeManager::fLM = 0; // Global (shared) singleton
47 
48 namespace {
49  G4Mutex latMutex = G4MUTEX_INITIALIZER; // For thread protection
50 }
51 
52 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
53 
55  Clear();
56 }
57 
59  Reset(); // Deletes all lattices
60 }
61 
62 // Delete all registered lattices and clear entries from lookup tables
63 
65  for (LatticeLogReg::iterator lm=fLLattices.begin();
66  lm != fLLattices.end(); ++lm) {
67  delete (*lm);
68  }
69 
70  for (LatticePhyReg::iterator pm=fPLattices.begin();
71  pm != fPLattices.end(); ++pm) {
72  delete (*pm);
73  }
74 
75  Clear();
76 }
77 
78 // Remove entries without deletion (for begin-job and end-job initializing)
79 
81  fPLatticeList.clear();
82  fPLattices.clear();
83 
84  fLLatticeList.clear();
85  fLLattices.clear();
86 }
87 
88 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
89 
91  // if no lattice manager exists, create one.
92  G4AutoLock latLock(&latMutex); // Protect before changing pointer
93  if (!fLM) fLM = new G4LatticeManager();
94  latLock.unlock();
95 
96  return fLM;
97 }
98 
99 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
100 
101 // Associate logical lattice with material
102 
104  G4LatticeLogical* Lat) {
105  if (!Mat || !Lat) return false; // Don't register null pointers
106 
107  G4AutoLock latLock(&latMutex); // Protect before changing registry
108  fLLattices.insert(Lat); // Take ownership in registry
109  fLLatticeList[Mat] = Lat;
110  latLock.unlock();
111 
112  if (verboseLevel) {
113  G4cout << "G4LatticeManager::RegisterLattice: "
114  << " Total number of logical lattices: " << fLLatticeList.size()
115  << " (" << fLLattices.size() << " unique)" << G4endl;
116  }
117 
118  return true;
119 }
120 
121 // Construct logical lattice for material from config file
122 
124  const G4String& latDir) {
125  if (verboseLevel) {
126  G4cout << "G4LatticeManager::LoadLattice material " << Mat->GetName()
127  << " " << latDir << G4endl;
128  }
129 
130  G4LatticeReader latReader(verboseLevel);
131  G4LatticeLogical* newLat = latReader.MakeLattice(latDir+"/config.txt");
132  if (verboseLevel>1) G4cout << " Created newLat " << newLat << G4endl;
133 
134  if (newLat) RegisterLattice(Mat, newLat);
135  else {
136  G4cerr << "ERROR creating " << latDir << " lattice for material "
137  << Mat->GetName() << G4endl;
138  }
139 
140  return newLat;
141 }
142 
143 // Combine loading and registration (Material extracted from volume)
144 
146  const G4String& latDir) {
147  if (verboseLevel) {
148  G4cout << "G4LatticeManager::LoadLattice volume " << Vol->GetName()
149  << " " << latDir << G4endl;
150  }
151 
152  G4Material* theMat = Vol->GetLogicalVolume()->GetMaterial();
153 
154  // Create and register the logical lattice, then the physical lattice
155  G4LatticeLogical* lLattice = LoadLattice(theMat, latDir);
156  if (!lLattice) return 0;
157 
158  G4LatticePhysical* pLattice =
159  new G4LatticePhysical(lLattice, Vol->GetFrameRotation());
160  if (pLattice) RegisterLattice(Vol, pLattice);
161 
162  if (verboseLevel>1) G4cout << " Created pLattice " << pLattice << G4endl;
163 
164  return pLattice;
165 }
166 
167 
168 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
169 
170 // Associate physical (oriented) lattice with physical volume
171 
173  G4LatticePhysical* Lat) {
174  if (!Vol || !Lat) return false; // Don't register null pointers
175 
176  G4AutoLock latLock(&latMutex); // Protect before changing registry
177 
178  // SPECIAL: Register first lattice with a null volume to act as default
179  if (fPLatticeList.empty()) fPLatticeList[0] = Lat;
180 
181  fPLattices.insert(Lat);
182  fPLatticeList[Vol] = Lat;
183 
184  latLock.unlock();
185 
186  if (verboseLevel) {
187  G4cout << "G4LatticeManager::RegisterLattice: "
188  << " Total number of physical lattices: " << fPLatticeList.size()-1
189  << " (" << fPLattices.size() << " unique)" << G4endl;
190  }
191 
192  return true;
193 }
194 
196  G4LatticeLogical* LLat) {
197  if (!Vol || !LLat) return false; // Don't register null pointers
198 
199  // Make sure logical lattice is registered for material
201 
202  // Create and register new physical lattice to go with volume
203  return RegisterLattice(Vol, new G4LatticePhysical(LLat, Vol->GetFrameRotation()));
204 }
205 
206 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
207 
208 // Returns a pointer to the LatticeLogical associated with material
209 
211  LatticeMatMap::const_iterator latFind = fLLatticeList.find(Mat);
212  if (latFind != fLLatticeList.end()) {
213  if (verboseLevel)
214  G4cout << "G4LatticeManager::GetLattice found " << latFind->second
215  << " for " << (Mat?Mat->GetName():"NULL") << "." << G4endl;
216  return latFind->second;
217  }
218 
219  if (verboseLevel)
220  G4cerr << "G4LatticeManager:: Found no matching lattices for "
221  << (Mat?Mat->GetName():"NULL") << "." << G4endl;
222 
223  return 0; // No lattice associated with volume
224 }
225 
226 // Returns a pointer to the LatticePhysical associated with volume
227 // NOTE: Passing Vol==0 will return the default lattice
228 
230  LatticeVolMap::const_iterator latFind = fPLatticeList.find(Vol);
231  if (latFind != fPLatticeList.end()) {
232  if (verboseLevel)
233  G4cout << "G4LatticeManager::GetLattice found " << latFind->second
234  << " for " << (Vol?Vol->GetName():"default") << "." << G4endl;
235  return latFind->second;
236  }
237 
238  if (verboseLevel)
239  G4cerr << "G4LatticeManager::GetLattice found no matching lattices for "
240  << (Vol?Vol->GetName():"default") << "." << G4endl;
241 
242  return 0; // No lattice associated with volume
243 }
244 
245 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
246 
247 // Return true if volume Vol has a physical lattice
248 
250  return (fPLatticeList.find(Vol) != fPLatticeList.end());
251 }
252 
253 // Return true if material Mat has a logical lattice
254 
256  return (fLLatticeList.find(Mat) != fLLatticeList.end());
257 }
258 
259 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
260 
261 //Given the phonon wave vector k, phonon physical volume Vol
262 //and polarizationState(0=LON, 1=FT, 2=ST),
263 //returns phonon velocity in m/s
264 
266  G4int polarizationState,
267  const G4ThreeVector & k) const {
268  G4LatticePhysical* theLattice = GetLattice(Vol);
269  if (verboseLevel)
270  G4cout << "G4LatticeManager::MapKtoV using lattice " << theLattice
271  << G4endl;
272 
273  // If no lattice available, use generic "speed of sound"
274  return theLattice ? theLattice->MapKtoV(polarizationState, k) : 300.*m/s;
275 }
276 
277 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
278 
279 // Given the phonon wave vector k, phonon physical volume Vol
280 // and polarizationState(0=LON, 1=FT, 2=ST),
281 // returns phonon propagation direction as dimensionless unit vector
282 
284  G4int polarizationState,
285  const G4ThreeVector & k) const {
286  G4LatticePhysical* theLattice = GetLattice(Vol);
287  if (verboseLevel)
288  G4cout << "G4LatticeManager::MapKtoVDir using lattice " << theLattice
289  << G4endl;
290 
291  // If no lattice available, propagate along input wavevector
292  return theLattice ? theLattice->MapKtoVDir(polarizationState, k) : k.unit();
293 }
static G4LatticeManager * GetLatticeManager()
static G4LatticeManager * fLM
CLHEP::Hep3Vector G4ThreeVector
G4ThreeVector MapKtoVDir(G4VPhysicalVolume *, G4int, const G4ThreeVector &) const
G4Material * GetMaterial() const
const G4String & GetName() const
Definition: G4Material.hh:178
G4double MapKtoV(G4int, G4ThreeVector) const
G4double MapKtoV(G4VPhysicalVolume *, G4int, const G4ThreeVector &) const
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
static const double s
Definition: G4SIunits.hh:168
const G4RotationMatrix * GetFrameRotation() const
G4GLOB_DLL std::ostream G4cout
const G4String & GetName() const
bool G4bool
Definition: G4Types.hh:79
G4LatticeLogical * LoadLattice(G4Material *, const G4String &latDir)
G4ThreeVector MapKtoVDir(G4int, G4ThreeVector) const
G4int G4Mutex
Definition: G4Threading.hh:173
LatticeVolMap fPLatticeList
G4LogicalVolume * GetLogicalVolume() const
Definition of the G4LatticePhysical class.
Definition of the G4LatticeLogical class.
#define G4endl
Definition: G4ios.hh:61
virtual ~G4LatticeManager()
Definition of the G4LatticeReader class.
static const double m
Definition: G4SIunits.hh:128
G4bool HasLattice(G4Material *) const
G4LatticeLogical * MakeLattice(const G4String &filepath)
double G4double
Definition: G4Types.hh:76
G4bool RegisterLattice(G4VPhysicalVolume *, G4LatticePhysical *)
LatticePhyReg fPLattices
LatticeMatMap fLLatticeList
G4LatticeLogical * GetLattice(G4Material *) const
G4GLOB_DLL std::ostream G4cerr
LatticeLogReg fLLattices