54#include "G4mplEquationSetup.hh"
56#include "G4mplEqMagElectricField.hh"
58#include "G4MagneticField.hh"
59#include "G4UniformMagField.hh"
60#include "G4FieldManager.hh"
61#include "G4TransportationManager.hh"
62#include "G4Mag_UsualEqRhs.hh"
63#include "G4MagIntegratorStepper.hh"
64#include "G4ChordFinder.hh"
65#include "G4MagIntegratorDriver.hh"
66#include "G4AtlasRK4.hh"
67#include "G4ClassicalRK4.hh"
74#include "G4SystemOfUnits.hh"
75#include "G4UniformMagField.hh"
78G4mplEquationSetup::ESThreadMap_t G4mplEquationSetup::m_ESThreadMap;
83G4mplEquationSetup::G4mplEquationSetup():
88 G4cout <<
"!!! G4mplEquationSetup constructor" << G4endl;
93G4mplEquationSetup* G4mplEquationSetup::GetInstance()
110G4mplEquationSetup::~G4mplEquationSetup()
113 G4cout <<
"!!! G4mplEquationSetup destructor" << G4endl;
118#ifdef G4MULTITHREADED
119 G4mplEquationSetup* G4mplEquationSetup::getES()
122 const auto tid = std::this_thread::get_id();
123 auto esPair = m_ESThreadMap.find(tid);
124 if(esPair == m_ESThreadMap.end())
126 else return esPair->second;
131 G4mplEquationSetup* G4mplEquationSetup::setES()
133 G4mplEquationSetup*
instance =
new G4mplEquationSetup;
134 const auto tid = std::this_thread::get_id();
135 auto inserted = m_ESThreadMap.insert( std::make_pair(tid,
instance)).first;
136 return (G4mplEquationSetup*) inserted->second;
155G4mplEquationSetup::InitialiseForField(G4FieldManager* fieldManager )
157 if (fieldManager ==
nullptr) {
165 fOriginalChordFinder = fieldManager->GetChordFinder();
166 fCurrentFieldManager = fieldManager;
170 const G4MagneticField* magField =
dynamic_cast<const G4MagneticField*
>(fieldManager->GetDetectorField());
171 G4MagneticField* magFieldNC
ATLAS_THREAD_SAFE =
const_cast<G4MagneticField*
>(magField);
173 CreateStepperToChordFinder(magFieldNC);
179G4mplEquationSetup::CreateStepperToChordFinder(G4MagneticField* magFieldNC)
181 if(magFieldNC ==
nullptr)
183 static G4ThreadLocal G4UniformMagField nullField( G4ThreeVector(0.0, 0.0, 0.0));
184 magFieldNC= &nullField;
187 fMonopoleEquation = std::make_unique<G4mplEqMagElectricField>(magFieldNC);
189 fMonopoleStepper = std::make_unique< G4ClassicalRK4>( fMonopoleEquation.get(), 8 );
191 if ( !fMonopoleStepper )
193 G4ExceptionDescription ermsg;
194 ermsg <<
"The creation of a stepper for Monopoles failed - trying to use G4ClassicalRK4";
195 G4Exception(
"G4mplEquationSetup::InitialiseForField",
196 "FailureToCreateObject", FatalException, ermsg);
199 auto integrDriver =
new G4MagInt_Driver( fMinStep, fMonopoleStepper.get(),
201 fMonopoleStepper->GetNumberOfVariables() );
202 fMonopoleChordFinder = std::make_unique<G4ChordFinder>( integrDriver );
209void G4mplEquationSetup::SwitchStepperAndChordFinder(G4bool useMonopoleEq,
210 G4FieldManager* fieldManager)
215 const G4MagneticField* magField =
dynamic_cast<const G4MagneticField*
>(fieldManager->GetDetectorField());
216 G4MagneticField* magFieldNC
ATLAS_THREAD_SAFE =
const_cast<G4MagneticField*
>(magField);
225 CheckAndUpdateField( magFieldNC );
228 fieldManager->SetChordFinder( fMonopoleChordFinder.get() );
230 fieldManager->SetChordFinder( fOriginalChordFinder );
237void G4mplEquationSetup::ResetIntegration(G4FieldManager* fieldManager)
242 if( fieldManager != fCurrentFieldManager){
243 G4ExceptionDescription ermsg;
244 ermsg <<
"This method expects to be called with the chordfinder it knows"
245 <<
" yet its is called with " << fieldManager
246 <<
" and expected " << fCurrentFieldManager << G4endl;
247 G4Exception(
"G4mplEquationSetup::ResetIntegration",
248 "InvalidState", FatalException, ermsg);
251 if( fCurrentFieldManager ) {
252 fCurrentFieldManager->SetChordFinder(fOriginalChordFinder);
255 G4cout <<
" G4mplEquationSetup: Reset Chord-Finder to original one (for pure electric charge): "
256 << fOriginalChordFinder << G4endl;
258 fOriginalChordFinder =
nullptr;
259 fCurrentFieldManager =
nullptr;
266void G4mplEquationSetup::CheckAndUpdateField( G4MagneticField* magFieldNC )
269 if( magFieldNC != fMonopoleEquation->GetFieldObj() )
273 CreateStepperToChordFinder(magFieldNC);
#define ATLAS_THREAD_SAFE