Import Geant4 11.4.0 source tree
This commit is contained in:
@@ -23,129 +23,132 @@
|
||||
// * acceptance of all terms of the Geant4 Software license. *
|
||||
// ********************************************************************
|
||||
//
|
||||
// G4TCachedMagneticField
|
||||
|
||||
#ifndef TCACHED_MAGNETIC_FIELD_DEF
|
||||
#define TCACHED_MAGNETIC_FIELD_DEF
|
||||
// Author: Josh Xie (CERN, Google Summer of Code 2014), June 2014
|
||||
// Supervisors: Sandro Wenzel, John Apostolakis (CERN)
|
||||
// --------------------------------------------------------------------
|
||||
#ifndef G4TCACHED_MAGNETIC_FIELD_HH
|
||||
#define G4TCACHED_MAGNETIC_FIELD_HH
|
||||
|
||||
#include "G4Types.hh"
|
||||
#include "G4ThreeVector.hh"
|
||||
#include "G4MagneticField.hh"
|
||||
|
||||
/**
|
||||
* @brief G4TCachedMagneticField is a templated implementation for a specialisation
|
||||
* of G4MagneticField used to cache the Magnetic Field value.
|
||||
*/
|
||||
|
||||
template <class T_Field>
|
||||
class G4TCachedMagneticField : public G4MagneticField
|
||||
{
|
||||
public: // with description
|
||||
G4TCachedMagneticField(T_Field* pTField, G4double distance)
|
||||
: G4MagneticField()
|
||||
, fLastLocation(DBL_MAX, DBL_MAX, DBL_MAX)
|
||||
, fLastValue(DBL_MAX, DBL_MAX, DBL_MAX)
|
||||
, fCountCalls(0)
|
||||
, fCountEvaluations(0)
|
||||
{
|
||||
fpMagneticField = pTField;
|
||||
fDistanceConst = distance;
|
||||
public:
|
||||
|
||||
// G4cout << " Cached-B-Field constructor> Distance = " << distance <<
|
||||
// G4endl;
|
||||
this->ClearCounts();
|
||||
}
|
||||
|
||||
G4TCachedMagneticField(const G4TCachedMagneticField<T_Field>& rightCMF)
|
||||
{
|
||||
fpMagneticField = rightCMF.fpMagneticField;
|
||||
fDistanceConst = rightCMF.fDistanceConst;
|
||||
fLastLocation = rightCMF.fLastLocation;
|
||||
fLastValue = rightCMF.fLastValue;
|
||||
this->ClearCounts();
|
||||
}
|
||||
|
||||
G4TCachedMagneticField* Clone() const
|
||||
{
|
||||
G4cout << "Clone is called" << G4endl;
|
||||
// Cannot use copy constructor: I need to clone the associated magnetif
|
||||
// field
|
||||
T_Field* aF = this->fpMagneticField->T_Field::Clone();
|
||||
G4TCachedMagneticField* cloned =
|
||||
new G4TCachedMagneticField(aF, this->fDistanceConst);
|
||||
cloned->fLastLocation = this->fLastLocation;
|
||||
cloned->fLastValue = this->fLastValue;
|
||||
return cloned;
|
||||
}
|
||||
|
||||
virtual ~G4TCachedMagneticField() { ; }
|
||||
// Constructor and destructor. No actions.
|
||||
|
||||
void ReportStatistics()
|
||||
{
|
||||
G4cout << " Cached field: " << G4endl
|
||||
<< " Number of calls: " << fCountCalls << G4endl
|
||||
<< " Number of evaluations : " << fCountEvaluations << G4endl;
|
||||
}
|
||||
|
||||
virtual void GetFieldValue(const G4double Point[4], G4double* Bfield) const
|
||||
{
|
||||
G4ThreeVector newLocation(Point[0], Point[1], Point[2]);
|
||||
|
||||
G4double distSq = (newLocation - fLastLocation).mag2();
|
||||
fCountCalls++;
|
||||
if(distSq < fDistanceConst * fDistanceConst)
|
||||
G4TCachedMagneticField(T_Field* pTField, G4double distance)
|
||||
: G4MagneticField()
|
||||
, fLastLocation(DBL_MAX, DBL_MAX, DBL_MAX)
|
||||
, fLastValue(DBL_MAX, DBL_MAX, DBL_MAX)
|
||||
, fCountCalls(0)
|
||||
, fCountEvaluations(0)
|
||||
{
|
||||
Bfield[0] = fLastValue.x();
|
||||
Bfield[1] = fLastValue.y();
|
||||
Bfield[2] = fLastValue.z();
|
||||
fpMagneticField = pTField;
|
||||
fDistanceConst = distance;
|
||||
|
||||
this->ClearCounts();
|
||||
}
|
||||
else
|
||||
|
||||
virtual ~G4TCachedMagneticField() = default;
|
||||
|
||||
G4TCachedMagneticField(const G4TCachedMagneticField<T_Field>& rightCMF)
|
||||
{
|
||||
// G4CachedMagneticField* thisNonC=
|
||||
// const_cast<G4CachedMagneticField*>(this);
|
||||
fpMagneticField->T_Field::GetFieldValue(Point, Bfield);
|
||||
// G4cout << " Evaluating. " << G4endl;
|
||||
fCountEvaluations++;
|
||||
// thisNonC->
|
||||
fLastLocation = G4ThreeVector(Point[0], Point[1], Point[2]);
|
||||
// thisNonC->
|
||||
fLastValue = G4ThreeVector(Bfield[0], Bfield[1], Bfield[2]);
|
||||
fpMagneticField = rightCMF.fpMagneticField;
|
||||
fDistanceConst = rightCMF.fDistanceConst;
|
||||
fLastLocation = rightCMF.fLastLocation;
|
||||
fLastValue = rightCMF.fLastValue;
|
||||
this->ClearCounts();
|
||||
}
|
||||
}
|
||||
|
||||
G4double GetConstDistance() const { return fDistanceConst; }
|
||||
void SetConstDistance(G4double dist) { fDistanceConst = dist; }
|
||||
G4TCachedMagneticField& operator=(const G4TCachedMagneticField& right)
|
||||
{
|
||||
if(&right == this) { return *this; }
|
||||
|
||||
G4int GetCountCalls() const { return fCountCalls; }
|
||||
G4int GetCountEvaluations() const { return fCountEvaluations; }
|
||||
void ClearCounts()
|
||||
{
|
||||
fCountCalls = 0;
|
||||
fCountEvaluations = 0;
|
||||
}
|
||||
fpMagneticField = right.fpMagneticField;
|
||||
|
||||
G4TCachedMagneticField& operator=(const G4TCachedMagneticField& right)
|
||||
{
|
||||
if(&right == this)
|
||||
return *this;
|
||||
fDistanceConst= right.fDistanceConst;
|
||||
fLastLocation = right.fLastLocation;
|
||||
fLastValue = right.fLastValue;
|
||||
|
||||
fpMagneticField = right.fpMagneticField;
|
||||
|
||||
fDistanceConst= right.fDistanceConst;
|
||||
fLastLocation = right.fLastLocation;
|
||||
fLastValue = right.fLastValue;
|
||||
|
||||
fCountCalls = 0;
|
||||
fCountEvaluations = 0;
|
||||
fCountCalls = 0;
|
||||
fCountEvaluations = 0;
|
||||
|
||||
return *this;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
T_Field* fpMagneticField;
|
||||
// When the field is evaluated within this distance it will not change
|
||||
G4double fDistanceConst;
|
||||
// Caching state
|
||||
mutable G4ThreeVector fLastLocation;
|
||||
mutable G4ThreeVector fLastValue;
|
||||
G4TCachedMagneticField* Clone() const
|
||||
{
|
||||
G4cout << "Clone is called" << G4endl;
|
||||
// Cannot use copy constructor: I need to clone associated magnetic field
|
||||
T_Field* aF = this->fpMagneticField->T_Field::Clone();
|
||||
G4TCachedMagneticField* cloned = new G4TCachedMagneticField(aF, this->fDistanceConst);
|
||||
cloned->fLastLocation = this->fLastLocation;
|
||||
cloned->fLastValue = this->fLastValue;
|
||||
return cloned;
|
||||
}
|
||||
|
||||
protected:
|
||||
mutable G4int fCountCalls, fCountEvaluations;
|
||||
void ReportStatistics()
|
||||
{
|
||||
G4cout << " Cached field: " << G4endl
|
||||
<< " Number of calls: " << fCountCalls << G4endl
|
||||
<< " Number of evaluations : " << fCountEvaluations << G4endl;
|
||||
}
|
||||
|
||||
virtual void GetFieldValue(const G4double Point[4], G4double* Bfield) const
|
||||
{
|
||||
G4ThreeVector newLocation(Point[0], Point[1], Point[2]);
|
||||
|
||||
G4double distSq = (newLocation - fLastLocation).mag2();
|
||||
fCountCalls++;
|
||||
if(distSq < fDistanceConst * fDistanceConst)
|
||||
{
|
||||
Bfield[0] = fLastValue.x();
|
||||
Bfield[1] = fLastValue.y();
|
||||
Bfield[2] = fLastValue.z();
|
||||
}
|
||||
else
|
||||
{
|
||||
fpMagneticField->T_Field::GetFieldValue(Point, Bfield);
|
||||
fCountEvaluations++;
|
||||
fLastLocation = G4ThreeVector(Point[0], Point[1], Point[2]);
|
||||
fLastValue = G4ThreeVector(Bfield[0], Bfield[1], Bfield[2]);
|
||||
}
|
||||
}
|
||||
|
||||
G4double GetConstDistance() const { return fDistanceConst; }
|
||||
void SetConstDistance(G4double dist) { fDistanceConst = dist; }
|
||||
|
||||
G4int GetCountCalls() const { return fCountCalls; }
|
||||
G4int GetCountEvaluations() const { return fCountEvaluations; }
|
||||
void ClearCounts()
|
||||
{
|
||||
fCountCalls = 0;
|
||||
fCountEvaluations = 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
mutable G4int fCountCalls, fCountEvaluations;
|
||||
|
||||
private:
|
||||
|
||||
T_Field* fpMagneticField;
|
||||
|
||||
/** When the field is evaluated within this distance it will not change. */
|
||||
G4double fDistanceConst;
|
||||
|
||||
/** Caching state. */
|
||||
mutable G4ThreeVector fLastLocation;
|
||||
mutable G4ThreeVector fLastValue;
|
||||
};
|
||||
|
||||
#endif /* TCACHED_MAGNETIC_FIELD_DEF */
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user