Geant4_10
F04GlobalField.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 // $Id: F04GlobalField.cc 77884 2013-11-29 08:41:11Z gcosmo $
27 //
30 //
31 
32 #include <time.h>
33 
34 #include "Randomize.hh"
36 
37 #include "G4ExplicitEuler.hh"
38 #include "G4ImplicitEuler.hh"
39 #include "G4SimpleRunge.hh"
40 #include "G4SimpleHeum.hh"
41 #include "G4ClassicalRK4.hh"
42 #include "G4CashKarpRKF45.hh"
43 #include "G4SystemOfUnits.hh"
44 
45 #include "F04GlobalField.hh"
46 #include "F04SimpleSolenoid.hh"
47 #include "F04FocusSolenoid.hh"
48 
49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
50 
51 G4ThreadLocal F04GlobalField* F04GlobalField::fObject = 0;
52 
53 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
54 
55 F04GlobalField::F04GlobalField(F04DetectorConstruction* det)
57  fMinStep(0.01*mm), fDeltaChord(3.0*mm),
58  fDeltaOneStep(0.01*mm), fDeltaIntersection(0.1*mm),
59  fEpsMin(2.5e-7*mm), fEpsMax(0.05*mm),
60  fEquation(0), fFieldManager(0),
61  fFieldPropagator(0), fStepper(0), fChordFinder(0),
62  fDetectorConstruction(det)
63 //F04GlobalField::F04GlobalField(F04DetectorConstruction* det)
64 // : G4MagneticField(),
65 // fMinStep(0.01*mm), fDeltaChord(3.0*mm),
66 // fDeltaOneStep(0.01*mm), fDeltaIntersection(0.1*mm),
67 // fEpsMin(2.5e-7*mm), fEpsMax(0.05*mm),
68 // fEquation(0), fFieldManager(0),
69 // fFieldPropagator(0), fStepper(0), fChordFinder(0),
70 // fDetectorConstruction(det)
71 {
72  fFieldMessenger = new F04FieldMessenger(this,det);
73 
74  fFields = new FieldList();
75 
76  fStepperType = 4 ; // ClassicalRK4 is default stepper
77 
78  // set object
79 
80  fObject = this;
81 
82  ConstructField();
83 }
84 
85 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
86 
87 F04GlobalField::~F04GlobalField()
88 {
89  Clear();
90 
91  delete fFieldMessenger;
92 
93  if (fEquation) delete fEquation;
94  if (fFieldManager) delete fFieldManager;
95  if (fFieldPropagator) delete fFieldPropagator;
96  if (fStepper) delete fStepper;
97  if (fChordFinder) delete fChordFinder;
98 }
99 
100 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
101 
103 {
104  fFirst = true;
105 
106  fNfp = 0;
107  fFp = 0;
108 
109  // Construct equ. of motion of particles through B fields
110 // fEquation = new G4Mag_EqRhs(this);
111  // Construct equ. of motion of particles through e.m. fields
112 // fEquation = new G4EqMagElectricField(this);
113  // Construct equ. of motion of particles including spin through B fields
114 // fEquation = new G4Mag_SpinEqRhs(this);
115  // Construct equ. of motion of particles including spin through e.m. fields
116  fEquation = new G4EqEMFieldWithSpin(this);
117 
118  // Get transportation, field, and propagator managers
119  G4TransportationManager* transportManager =
121 
122  fFieldManager = GetGlobalFieldManager();
123 
124  fFieldPropagator = transportManager->GetPropagatorInField();
125 
126  // Need to SetFieldChangesEnergy to account for a time varying electric
127  // field (r.f. fields)
128  fFieldManager->SetFieldChangesEnergy(true);
129 
130  // Set the field
131  fFieldManager->SetDetectorField(this);
132 
133  // Choose a stepper for integration of the equation of motion
134  SetStepper();
135 
136  // Create a cord finder providing the (global field, min step length,
137  // a pointer to the stepper)
138  fChordFinder = new G4ChordFinder((G4MagneticField*)this,fMinStep,fStepper);
139 
140  // Set accuracy parameters
141  fChordFinder->SetDeltaChord( fDeltaChord );
142 
143  fFieldManager->SetAccuraciesWithDeltaOneStep(fDeltaOneStep);
144 
145  fFieldManager->SetDeltaIntersection(fDeltaIntersection);
146 
147  fFieldPropagator->SetMinimumEpsilonStep(fEpsMin);
148  fFieldPropagator->SetMaximumEpsilonStep(fEpsMax);
149 
150  G4cout << "Accuracy Parameters:" <<
151  " MinStep=" << fMinStep <<
152  " DeltaChord=" << fDeltaChord <<
153  " DeltaOneStep=" << fDeltaOneStep << G4endl;
154  G4cout << " " <<
155  " DeltaIntersection=" << fDeltaIntersection <<
156  " EpsMin=" << fEpsMin <<
157  " EpsMax=" << fEpsMax << G4endl;
158 
159  fFieldManager->SetChordFinder(fChordFinder);
160 
161 }
162 
163 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
164 
166 {
167  fFirst = true;
168 
169  fNfp = 0;
170  fFp = 0;
171 
172  Clear();
173 
174  G4double l = 0.0;
175  G4double B1 = fDetectorConstruction->GetCaptureMgntB1();
176  G4double B2 = fDetectorConstruction->GetCaptureMgntB2();
177 
178  G4LogicalVolume* logicCaptureMgnt = fDetectorConstruction->GetCaptureMgnt();
179  G4ThreeVector captureMgntCenter =
180  fDetectorConstruction->GetCaptureMgntCenter();
181 
182  F04FocusSolenoid* focusSolenoid =
183  new F04FocusSolenoid(B1, B2, l, logicCaptureMgnt,captureMgntCenter);
184  focusSolenoid -> SetHalf(true);
185 
186  G4double B = fDetectorConstruction->GetTransferMgntB();
187 
188  G4LogicalVolume* logicTransferMgnt =
189  fDetectorConstruction->GetTransferMgnt();
190  G4ThreeVector transferMgntCenter =
191  fDetectorConstruction->GetTransferMgntCenter();
192 
193  F04SimpleSolenoid* simpleSolenoid =
194  new F04SimpleSolenoid(B, l, logicTransferMgnt,transferMgntCenter);
195 
196  simpleSolenoid->SetColor("1,0,1");
197  simpleSolenoid->SetColor("0,1,1");
198  simpleSolenoid->SetMaxStep(1.5*mm);
199  simpleSolenoid->SetMaxStep(2.5*mm);
200 
201  if (fEquation) delete fEquation;
202 
203  // Construct equ. of motion of particles through B fields
204 // fEquation = new G4Mag_EqRhs(this);
205  // Construct equ. of motion of particles through e.m. fields
206 // fEquation = new G4EqMagElectricField(this);
207  // Construct equ. of motion of particles including spin through B fields
208 // fEquation = new G4Mag_SpinEqRhs(this);
209  // Construct equ. of motion of particles including spin through e.m. fields
210  fEquation = new G4EqEMFieldWithSpin(this);
211 
212  // Get transportation, field, and propagator managers
213  G4TransportationManager* transportManager =
215 
216  fFieldManager = GetGlobalFieldManager();
217 
218  fFieldPropagator = transportManager->GetPropagatorInField();
219 
220  // Need to SetFieldChangesEnergy to account for a time varying electric
221  // field (r.f. fields)
222  fFieldManager->SetFieldChangesEnergy(true);
223 
224  // Choose a stepper for integration of the equation of motion
225  SetStepper();
226 
227  if (fChordFinder) delete fChordFinder;
228 
229  // Create a cord finder providing the (global field, min step length,
230  // a pointer to the stepper)
231  fChordFinder = new G4ChordFinder((G4MagneticField*)this,fMinStep,fStepper);
232 
233  // Set accuracy parameters
234  fChordFinder->SetDeltaChord( fDeltaChord );
235 
236  fFieldManager->SetAccuraciesWithDeltaOneStep(fDeltaOneStep);
237 
238  fFieldManager->SetDeltaIntersection(fDeltaIntersection);
239 
240  fFieldPropagator->SetMinimumEpsilonStep(fEpsMin);
241  fFieldPropagator->SetMaximumEpsilonStep(fEpsMax);
242 
243  G4cout << "Accuracy Parameters:" <<
244  " MinStep=" << fMinStep <<
245  " DeltaChord=" << fDeltaChord <<
246  " DeltaOneStep=" << fDeltaOneStep << G4endl;
247  G4cout << " " <<
248  " DeltaIntersection=" << fDeltaIntersection <<
249  " EpsMin=" << fEpsMin <<
250  " EpsMax=" << fEpsMax << G4endl;
251 
252  fFieldManager->SetChordFinder(fChordFinder);
253 
254  G4VPhysicalVolume* currentWorld = fDetectorConstruction->GetWorld();
255  if (fFields) {
256  if (fFields->size()>0) {
257  FieldList::iterator i;
258  for (i=fFields->begin(); i!=fFields->end(); ++i){
259  (*i)->Construct(currentWorld);
260  }
261  }
262  }
263 }
264 
265 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
266 
268 {
269  if (!fObject) new F04GlobalField(det);
270  return fObject;
271 }
272 
273 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
274 
276 {
277  if (fObject) return fObject;
278  return NULL;
279 }
280 
281 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
282 
284 {
285  if(fStepper) delete fStepper;
286 
287  switch ( fStepperType )
288  {
289  case 0:
290 // fStepper = new G4ExplicitEuler( fEquation, 8 ); // no spin tracking
291  fStepper = new G4ExplicitEuler( fEquation, 12 ); // with spin tracking
292  G4cout << "G4ExplicitEuler is called" << G4endl;
293  break;
294  case 1:
295 // fStepper = new G4ImplicitEuler( fEquation, 8 ); // no spin tracking
296  fStepper = new G4ImplicitEuler( fEquation, 12 ); // with spin tracking
297  G4cout << "G4ImplicitEuler is called" << G4endl;
298  break;
299  case 2:
300 // fStepper = new G4SimpleRunge( fEquation, 8 ); // no spin tracking
301  fStepper = new G4SimpleRunge( fEquation, 12 ); // with spin tracking
302  G4cout << "G4SimpleRunge is called" << G4endl;
303  break;
304  case 3:
305 // fStepper = new G4SimpleHeum( fEquation, 8 ); // no spin tracking
306  fStepper = new G4SimpleHeum( fEquation, 12 ); // with spin tracking
307  G4cout << "G4SimpleHeum is called" << G4endl;
308  break;
309  case 4:
310 // fStepper = new G4ClassicalRK4( fEquation, 8 ); // no spin tracking
311  fStepper = new G4ClassicalRK4( fEquation, 12 ); // with spin tracking
312  G4cout << "G4ClassicalRK4 (default) is called" << G4endl;
313  break;
314  case 5:
315 // fStepper = new G4CashKarpRKF45( fEquation, 8 ); // no spin tracking
316  fStepper = new G4CashKarpRKF45( fEquation, 12 ); // with spin tracking
317  G4cout << "G4CashKarpRKF45 is called" << G4endl;
318  break;
319  default: fStepper = 0;
320  }
321 }
322 
323 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
324 
326 {
328  ->GetFieldManager();
329 }
330 
331 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
332 
333 void F04GlobalField::GetFieldValue(const G4double* point, G4double* field) const
334 {
335  // NOTE: this routine dominates the CPU time for tracking.
336  // Using the simple array fFp[] instead of fields[]
337  // directly sped it up
338 
339  field[0] = field[1] = field[2] = field[3] = field[4] = field[5] = 0.0;
340 
341  // protect against Geant4 bug that calls us with point[] NaN.
342  if(point[0] != point[0]) return;
343 
344  // (can't use fNfp or fFp, as they may change)
345  if (fFirst) ((F04GlobalField*)this)->SetupArray(); // (cast away const)
346 
347  for (int i=0; i<fNfp; ++i) {
348  const F04ElementField* p = fFp[i];
349  if (p->IsInBoundingBox(point)) {
350  p->AddFieldValue(point,field);
351  }
352  }
353 
354 }
355 
356 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
357 
359 {
360  if (fFields) {
361  if (fFields->size()>0) {
362  FieldList::iterator i;
363  for (i=fFields->begin(); i!=fFields->end(); ++i) delete *i;
364  fFields->clear();
365  }
366  }
367 
368  if (fFp) delete[] fFp;
369 
370  fFirst = true;
371 
372  fNfp = 0;
373  fFp = NULL;
374 }
375 
376 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
377 
378 void F04GlobalField::SetupArray()
379 {
380  fFirst = false;
381  fNfp = fFields->size();
382  fFp = new const F04ElementField* [fNfp+1]; // add 1 so it's never 0
383  for (int i=0; i<fNfp; ++i) fFp[i] = (*fFields)[i];
384 }
void SetMaxStep(G4double stp)
SetMaxStep(G4double) sets the max. step size.
G4LogicalVolume * GetTransferMgnt()
G4bool SetDetectorField(G4Field *detectorField)
Definition of the F04GlobalField class.
const char * p
Definition: xmltok.h:285
void UpdateField()
updates all field tracking objects and Clear()
virtual void AddFieldValue(const G4double point[4], G4double field[6]) const =0
virtual void GetFieldValue(const G4double *point, G4double *field) const
#define G4ThreadLocal
Definition: tls.hh:52
void SetChordFinder(G4ChordFinder *aChordFinder)
void SetColor(G4String c)
SetColor(G4String) sets the color.
void SetAccuraciesWithDeltaOneStep(G4double valDeltaOneStep)
static F04GlobalField * GetObject()
G4GLOB_DLL std::ostream G4cout
std::vector< F04ElementField * > FieldList
G4FieldManager * GetGlobalFieldManager()
Get the global field manager.
Definition of the F04FocusSolenoid class.
static G4TransportationManager * GetTransportationManager()
G4FieldManager * GetFieldManager() const
G4LogicalVolume * GetCaptureMgnt()
bool IsInBoundingBox(const G4double point[4]) const
void ConstructField()
constructs all field tracking objects
void SetDeltaIntersection(G4double valueDintersection)
void SetMinimumEpsilonStep(G4double newEpsMin)
void SetFieldChangesEnergy(G4bool value)
Definition of the F04SimpleSolenoid class.
G4VPhysicalVolume * GetWorld()
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
void SetMaximumEpsilonStep(G4double newEpsMax)
G4PropagatorInField * GetPropagatorInField() const
void SetStepper()
Set the Stepper.
void SetDeltaChord(G4double newval)