Geant4  10.01
G4CachedMagneticField.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 //
27 // $Id: G4CachedMagneticField.cc 68055 2013-03-13 14:43:28Z gcosmo $
28 //
29 // --------------------------------------------------------------------
30 
31 #include "G4CachedMagneticField.hh"
32 
34  G4double distance)
35  : G4MagneticField(),
36  fLastLocation(DBL_MAX,DBL_MAX,DBL_MAX),
37  fLastValue(DBL_MAX,DBL_MAX,DBL_MAX),
38  fCountCalls(0), fCountEvaluations(0)
39 {
40  fpMagneticField= pMagField;
41  fDistanceConst= distance;
42 
43  // G4cout << " Cached-B-Field constructor> Distance = " << distance << G4endl;
44  this->ClearCounts();
45 }
46 
48 {
49  //Cannot use copy constructor: I need to clone the associated magnetif field
50  G4MagneticField* aF = static_cast<G4MagneticField*>(this->fpMagneticField->Clone());
52  this->fDistanceConst );
53  cloned->fLastLocation = this->fLastLocation;
54  cloned->fLastValue = this->fLastValue;
55  return cloned;
56 }
57 
59 {
60 }
61 
62 void
64 {
65  G4cout << " Cached field: " << G4endl
66  << " Number of calls: " << fCountCalls << G4endl
67  << " Number of evaluations : " << fCountEvaluations << G4endl;
68 }
69 
71  : G4MagneticField()
72 {
74  fDistanceConst = rightCMF.fDistanceConst;
75  fLastLocation = rightCMF.fLastLocation;
76  fLastValue = rightCMF.fLastValue;
77  this->ClearCounts();
78 }
79 
81 {
82  if (&p == this) return *this; *this = p; return *this;
83 }
84 
85 void
87  G4double *Bfield ) const
88 {
89  G4ThreeVector newLocation( Point[0], Point[1], Point[2] );
90 
91  // G4cout << "Cache-B-field called at " << newLocation << G4endl;
92 
93  G4double distSq= (newLocation-fLastLocation).mag2();
94  fCountCalls++;
95  if( distSq < fDistanceConst*fDistanceConst ) {
96  Bfield[0] = fLastValue.x();
97  Bfield[1] = fLastValue.y();
98  Bfield[2] = fLastValue.z();
99  }else{
100  // G4CachedMagneticField* thisNonC= const_cast<G4CachedMagneticField*>(this);
101  fpMagneticField->GetFieldValue( Point, Bfield );
102  // G4cout << " Evaluating. " << G4endl;
104  // thisNonC->
105  fLastLocation= G4ThreeVector( Point[0], Point[1], Point[2] );
106  // thisNonC->
107  fLastValue= G4ThreeVector( Bfield[0], Bfield[1], Bfield[2] );
108  }
109 }
G4MagneticField * fpMagneticField
virtual void GetFieldValue(const G4double Point[4], G4double *Bfield) const
CLHEP::Hep3Vector G4ThreeVector
virtual void GetFieldValue(const G4double Point[4], G4double *Bfield) const =0
virtual G4Field * Clone() const
Definition: G4Field.cc:54
G4GLOB_DLL std::ostream G4cout
G4CachedMagneticField(G4MagneticField *, G4double distanceConst)
virtual G4CachedMagneticField * Clone() const
#define G4endl
Definition: G4ios.hh:61
G4CachedMagneticField & operator=(const G4CachedMagneticField &p)
double G4double
Definition: G4Types.hh:76
#define DBL_MAX
Definition: templates.hh:83