source: trunk/examples/extended/field/field04/src/F04GlobalField.cc @ 1337

Last change on this file since 1337 was 807, checked in by garnier, 16 years ago

update

File size: 8.1 KB
Line 
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
29#include <time.h>
30
31#include "Randomize.hh"
32#include "G4TransportationManager.hh"
33
34#include "G4ExplicitEuler.hh"
35#include "G4ImplicitEuler.hh"
36#include "G4SimpleRunge.hh"
37#include "G4SimpleHeum.hh"
38#include "G4ClassicalRK4.hh"
39#include "G4CashKarpRKF45.hh"
40
41#include "F04GlobalField.hh"
42
43F04GlobalField* F04GlobalField::object = 0;
44
45F04GlobalField::F04GlobalField() : G4ElectroMagneticField(),
46                             minStep(0.01*mm), deltaChord(3.0*mm),
47                             deltaOneStep(0.01*mm), deltaIntersection(0.1*mm),
48                             epsMin(2.5e-7*mm), epsMax(0.05*mm),
49                             fEquation(0), fFieldManager(0), 
50                             fFieldPropagator(0), fStepper(0), fChordFinder(0)
51//F04GlobalField::F04GlobalField() : G4MagneticField(),
52//                             minStep(0.01*mm), deltaChord(3.0*mm),
53//                             deltaOneStep(0.01*mm), deltaIntersection(0.1*mm),
54//                             epsMin(2.5e-7*mm), epsMax(0.05*mm),
55//                             fEquation(0), fFieldManager(0),
56//                             fFieldPropagator(0), fStepper(0), fChordFinder(0)
57{
58  fFieldMessenger = new F04FieldMessenger(this);
59
60  fields = new FieldList();
61
62  fStepperType = 4 ;       // ClassicalRK4 is default stepper
63
64  //  set object
65
66  object = this;
67
68  updateField();
69}
70
71F04GlobalField::~F04GlobalField()
72{
73  clear();
74
75  delete fFieldMessenger;
76
77  if (fEquation)        delete fEquation;
78  if (fFieldManager)    delete fFieldManager;
79  if (fFieldPropagator) delete fFieldPropagator;
80  if (fStepper)         delete fStepper;
81  if (fChordFinder)     delete fChordFinder;
82}
83
84void F04GlobalField::updateField()
85{
86  first = true;
87
88  nfp = 0;
89  fp = 0;
90
91  clear();
92
93  //  Construct equ. of motion of particles through B fields
94//  fEquation = new G4Mag_EqRhs(this);
95  //  Construct equ. of motion of particles through e.m. fields
96//  fEquation = new G4EqMagElectricField(this);
97  //  Construct equ. of motion of particles including spin through B fields
98//  fEquation = new G4Mag_SpinEqRhs(this);
99  //  Construct equ. of motion of particles including spin through e.m. fields
100  fEquation = new G4EqEMFieldWithSpin(this);
101
102  //  Get transportation, field, and propagator managers
103  G4TransportationManager* fTransportManager =
104         G4TransportationManager::GetTransportationManager();
105
106  fFieldManager = GetGlobalFieldManager();
107
108  fFieldPropagator = fTransportManager->GetPropagatorInField();
109
110  //  Need to SetFieldChangesEnergy to account for a time varying electric
111  //  field (r.f. fields)
112  fFieldManager->SetFieldChangesEnergy(true);
113
114  //  Set the field
115  fFieldManager->SetDetectorField(this);
116
117  //  Choose a stepper for integration of the equation of motion
118  SetStepper();
119
120  //  Create a cord finder providing the (global field, min step length,
121  //  a pointer to the stepper)
122  fChordFinder = new G4ChordFinder((G4MagneticField*)this,minStep,fStepper);
123
124  // Set accuracy parameters
125  fChordFinder->SetDeltaChord( deltaChord );
126
127  fFieldManager->SetAccuraciesWithDeltaOneStep(deltaOneStep);
128
129  fFieldManager->SetDeltaIntersection(deltaIntersection);
130
131  fFieldPropagator->SetMinimumEpsilonStep(epsMin);
132  fFieldPropagator->SetMaximumEpsilonStep(epsMax);
133
134  G4cout << "Accuracy Parameters:" <<
135            " MinStep=" << minStep <<
136            " DeltaChord=" << deltaChord <<
137            " DeltaOneStep=" << deltaOneStep << G4endl;
138  G4cout << "                    " <<
139            " DeltaIntersection=" << deltaIntersection <<
140            " EpsMin=" << epsMin <<
141            " EpsMax=" << epsMax <<  G4endl;
142
143  fFieldManager->SetChordFinder(fChordFinder);
144
145}
146
147F04GlobalField* F04GlobalField::getObject()
148{
149  if (!object) new F04GlobalField();
150  return object;
151}
152
153void F04GlobalField::SetStepper()
154{
155  if(fStepper) delete fStepper;
156
157  switch ( fStepperType )
158  {
159    case 0:
160//      fStepper = new G4ExplicitEuler( fEquation, 8 ); // no spin tracking
161      fStepper = new G4ExplicitEuler( fEquation, 12 ); // with spin tracking
162      G4cout << "G4ExplicitEuler is called" << G4endl;
163      break;
164    case 1:
165//      fStepper = new G4ImplicitEuler( fEquation, 8 ); // no spin tracking
166      fStepper = new G4ImplicitEuler( fEquation, 12 ); // with spin tracking
167      G4cout << "G4ImplicitEuler is called" << G4endl;
168      break;
169    case 2:
170//      fStepper = new G4SimpleRunge( fEquation, 8 ); // no spin tracking
171      fStepper = new G4SimpleRunge( fEquation, 12 ); // with spin tracking
172      G4cout << "G4SimpleRunge is called" << G4endl;
173      break;
174    case 3:
175//      fStepper = new G4SimpleHeum( fEquation, 8 ); // no spin tracking
176      fStepper = new G4SimpleHeum( fEquation, 12 ); // with spin tracking
177      G4cout << "G4SimpleHeum is called" << G4endl;
178      break;
179    case 4:
180//      fStepper = new G4ClassicalRK4( fEquation, 8 ); // no spin tracking
181      fStepper = new G4ClassicalRK4( fEquation, 12 ); // with spin tracking
182      G4cout << "G4ClassicalRK4 (default) is called" << G4endl;
183      break;
184    case 5:
185//      fStepper = new G4CashKarpRKF45( fEquation, 8 ); // no spin tracking
186      fStepper = new G4CashKarpRKF45( fEquation, 12 ); // with spin tracking
187      G4cout << "G4CashKarpRKF45 is called" << G4endl;
188      break;
189    default: fStepper = 0;
190  }
191}
192
193G4FieldManager* F04GlobalField::GetGlobalFieldManager()
194{
195  return G4TransportationManager::GetTransportationManager()
196                                ->GetFieldManager();
197}
198
199void F04GlobalField::GetFieldValue(const G4double* point, G4double* field) const
200{
201  // NOTE: this routine dominates the CPU time for tracking.
202  // Using the simple array fp[] instead of fields[]
203  // directly sped it up
204
205  field[0] = field[1] = field[2] = field[3] = field[4] = field[5] = 0.0;
206
207  // protect against Geant4 bug that calls us with point[] NaN.
208  if(point[0] != point[0]) return;
209
210  // (can't use nfp or fp, as they may change)
211  if (first) ((F04GlobalField*)this)->setupArray();   // (cast away const)
212
213  for (int i=0; i<nfp; ++i) {
214      const F04ElementField* p = fp[i];
215      if (p->isInBoundingBox(point)) {
216         p->addFieldValue(point,field);
217      }
218  }
219
220}
221
222void F04GlobalField::clear()
223{
224  if (fields) {
225     if (fields->size()>0) {
226        FieldList::iterator i;
227        for (i=fields->begin(); i!=fields->end(); ++i) delete *i;
228        fields->clear();
229     }
230  }
231
232  if (fp) delete[] fp;
233
234  first = true;
235
236  nfp = 0;
237  fp = NULL;
238}
239
240void F04GlobalField::setupArray()
241{
242  first = false;
243  nfp = fields->size();
244  fp = new const F04ElementField* [nfp+1]; // add 1 so it's never 0
245  for (int i=0; i<nfp; ++i) fp[i] = (*fields)[i];
246}
Note: See TracBrowser for help on using the repository browser.