Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4TCachedMagneticField.hh
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// G4TCachedMagneticField
27
28// Author: Josh Xie (CERN, Google Summer of Code 2014), June 2014
29// Supervisors: Sandro Wenzel, John Apostolakis (CERN)
30// --------------------------------------------------------------------
31#ifndef G4TCACHED_MAGNETIC_FIELD_HH
32#define G4TCACHED_MAGNETIC_FIELD_HH
33
34#include "G4Types.hh"
35#include "G4ThreeVector.hh"
36#include "G4MagneticField.hh"
37
38/**
39 * @brief G4TCachedMagneticField is a templated implementation for a specialisation
40 * of G4MagneticField used to cache the Magnetic Field value.
41 */
42
43template <class T_Field>
45{
46 public:
47
48 G4TCachedMagneticField(T_Field* pTField, G4double distance)
50 , fLastLocation(DBL_MAX, DBL_MAX, DBL_MAX)
51 , fLastValue(DBL_MAX, DBL_MAX, DBL_MAX)
52 , fCountCalls(0)
54 {
55 fpMagneticField = pTField;
56 fDistanceConst = distance;
57
58 this->ClearCounts();
59 }
60
61 virtual ~G4TCachedMagneticField() = default;
62
64 {
65 fpMagneticField = rightCMF.fpMagneticField;
66 fDistanceConst = rightCMF.fDistanceConst;
67 fLastLocation = rightCMF.fLastLocation;
68 fLastValue = rightCMF.fLastValue;
69 this->ClearCounts();
70 }
71
73 {
74 if(&right == this) { return *this; }
75
76 fpMagneticField = right.fpMagneticField;
77
78 fDistanceConst= right.fDistanceConst;
79 fLastLocation = right.fLastLocation;
80 fLastValue = right.fLastValue;
81
82 fCountCalls = 0;
84
85 return *this;
86 }
87
89 {
90 G4cout << "Clone is called" << G4endl;
91 // Cannot use copy constructor: I need to clone associated magnetic field
92 T_Field* aF = this->fpMagneticField->T_Field::Clone();
93 G4TCachedMagneticField* cloned = new G4TCachedMagneticField(aF, this->fDistanceConst);
94 cloned->fLastLocation = this->fLastLocation;
95 cloned->fLastValue = this->fLastValue;
96 return cloned;
97 }
98
100 {
101 G4cout << " Cached field: " << G4endl
102 << " Number of calls: " << fCountCalls << G4endl
103 << " Number of evaluations : " << fCountEvaluations << G4endl;
104 }
105
106 virtual void GetFieldValue(const G4double Point[4], G4double* Bfield) const
107 {
108 G4ThreeVector newLocation(Point[0], Point[1], Point[2]);
109
110 G4double distSq = (newLocation - fLastLocation).mag2();
111 fCountCalls++;
112 if(distSq < fDistanceConst * fDistanceConst)
113 {
114 Bfield[0] = fLastValue.x();
115 Bfield[1] = fLastValue.y();
116 Bfield[2] = fLastValue.z();
117 }
118 else
119 {
120 fpMagneticField->T_Field::GetFieldValue(Point, Bfield);
122 fLastLocation = G4ThreeVector(Point[0], Point[1], Point[2]);
123 fLastValue = G4ThreeVector(Bfield[0], Bfield[1], Bfield[2]);
124 }
125 }
126
127 G4double GetConstDistance() const { return fDistanceConst; }
128 void SetConstDistance(G4double dist) { fDistanceConst = dist; }
129
130 G4int GetCountCalls() const { return fCountCalls; }
133 {
134 fCountCalls = 0;
136 }
137
138 protected:
139
141
142 private:
143
144 T_Field* fpMagneticField;
145
146 /** When the field is evaluated within this distance it will not change. */
147 G4double fDistanceConst;
148
149 /** Caching state. */
150 mutable G4ThreeVector fLastLocation;
151 mutable G4ThreeVector fLastValue;
152};
153
154#endif
CLHEP::Hep3Vector G4ThreeVector
double G4double
Definition G4Types.hh:83
int G4int
Definition G4Types.hh:85
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
G4TCachedMagneticField(T_Field *pTField, G4double distance)
G4TCachedMagneticField(const G4TCachedMagneticField< T_Field > &rightCMF)
G4TCachedMagneticField * Clone() const
G4TCachedMagneticField & operator=(const G4TCachedMagneticField &right)
void SetConstDistance(G4double dist)
virtual void GetFieldValue(const G4double Point[4], G4double *Bfield) const
virtual ~G4TCachedMagneticField()=default
#define DBL_MAX
Definition templates.hh:62