Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 96751 2016-05-04 09:39:38Z 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 magnetic field
50  G4MagneticField* aF = static_cast<G4MagneticField*>(fpMagneticField->Clone());
51  G4CachedMagneticField* cloned = new G4CachedMagneticField(aF, fDistanceConst);
52 
53  cloned->fLastLocation = fLastLocation;
54  cloned->fLastValue = 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(rightCMF)
72 {
73  fpMagneticField= rightCMF.fpMagneticField; // NOTE: sharing pointer here!
74  fDistanceConst = rightCMF.fDistanceConst;
75  fLastLocation = rightCMF.fLastLocation;
76  fLastValue = rightCMF.fLastValue;
77  this->ClearCounts();
78 }
79 
81 {
82  if (&p == this) return *this;
84  fpMagneticField= p.fpMagneticField; // NOTE: sharing pointer here!
85  fDistanceConst = p.fDistanceConst;
86  fLastLocation = p.fLastLocation;
87  fLastValue = p.fLastValue;
88  this->ClearCounts();
89  return *this;
90 }
91 
92 void
94  G4double *Bfield ) const
95 {
96  G4ThreeVector newLocation( Point[0], Point[1], Point[2] );
97 
98  // G4cout << "Cache-B-field called at " << newLocation << G4endl;
99 
100  G4double distSq= (newLocation-fLastLocation).mag2();
101  fCountCalls++;
102  if( distSq < fDistanceConst*fDistanceConst ) {
103  Bfield[0] = fLastValue.x();
104  Bfield[1] = fLastValue.y();
105  Bfield[2] = fLastValue.z();
106  }else{
107  // G4CachedMagneticField* thisNonC= const_cast<G4CachedMagneticField*>(this);
108  fpMagneticField->GetFieldValue( Point, Bfield );
109  // G4cout << " Evaluating. " << G4endl;
111  // thisNonC->
112  fLastLocation= G4ThreeVector( Point[0], Point[1], Point[2] );
113  // thisNonC->
114  fLastValue= G4ThreeVector( Bfield[0], Bfield[1], Bfield[2] );
115  }
116 }
virtual void GetFieldValue(const G4double Point[4], G4double *Bfield) const
CLHEP::Hep3Vector G4ThreeVector
double x() const
const char * p
Definition: xmltok.h:285
virtual G4Field * Clone() const
virtual void GetFieldValue(const G4double Point[4], G4double *Bfield) const =0
virtual G4Field * Clone() const
Definition: G4Field.cc:54
double z() const
G4GLOB_DLL std::ostream G4cout
G4CachedMagneticField(G4MagneticField *, G4double distanceConst)
G4MagneticField & operator=(const G4MagneticField &p)
double y() 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