11#include "TMatrixTBase.h"
26 vector<particle*>::iterator p_;
32 vector<force*>::iterator ff_;
38 vector<inspector*>::iterator ii_;
41 if((*ii_)->IsInspectorP()){
48 for(i=0; i<(Int_t) e.
particles.size();i++){
57 std::cerr<<
"event::event(event&) : inspector ("<<iis->
GetMethod()
58 <<
") watches a particle that is not in the copied event. It is not copied."<<std::endl;
66 }
else if((*ii_)->IsInspectorE()){
92 vector<particle*>::iterator p_;
98 vector<inspector*>::iterator i_;
110 if(!(rel>=0.) || !(abs>=0.) || (rel==0. && abs==0.)){
111 std::cerr<<
"event::SetTolerance : invalid tolerance (rel "<<rel<<
", abs "<<abs
112 <<
"). Tolerance is not changed."<<std::endl;
120 vector<particle*>::iterator p_;
134 }
else if(
name==
"SEULER"){
136 }
else if(
name==
"VERLET"){
138 }
else if(
name==
"RK4"){
140 }
else if(
name==
"RK45"){
143 std::cerr<<
"event::SetIntegrator : No integrator named / "<<
name
144 <<
" (TAYLOR, SEULER, VERLET, RK4, RK45). Integrator is not changed."<<std::endl;
152 case kTaylor:
return TString(
"TAYLOR");
155 case kRK4:
return TString(
"RK4");
156 case kRK45:
return TString(
"RK45");
186 frc.assign(n, TVector3());
189 vector<force*>::iterator ff_;
197 for(j=i+1; j<n; j++){
200 if(!move1 && !move2)
continue;
203 TVector3 f = (*ff_)->Evaluate(p1, p2);
205 if(move2) frc[j] -= f;
208 frc[j] += (*ff_)->Evaluate(p2, p1);
221 frc[i] += (*ff_)->Evaluate(p1,
particles[j]);
231 for(i=0; i<acc.size(); i++){
234 acc[i] = acc[i] * (1./p->
GetM(kTRUE));
240 return v * (p->
GetM0() / TMath::Sqrt(1. - v.Mag2()));
245 Double_t m0 = p->
GetM0();
246 return u * (1. / TMath::Sqrt(m0*m0 + u.Mag2()));
260void event::SetState(
const std::vector<TVector3> & x,
const std::vector<TVector3> & u){
285 vector<particle*>::iterator pp_;
287 (*pp_)->finishStep(dt);
295 std::vector<TVector3> frc;
308 std::vector<TVector3> acc;
329 for(i=0; cached && i<n; i++){
341 std::vector<TVector3> uh(n);
366 std::vector<TVector3> acc0 =
accCache;
371 p->
applyDX(p->
GetV() * dt + acc0[i] * (0.5*dt*dt));
392 std::vector<TVector3> x0, u0, xs(n), us(n);
393 std::vector<TVector3> k1x, k2x, k3x, k4x;
394 std::vector<TVector3> k1u, k2u, k3u, k4u;
401 xs[i] = x0[i] + k1x[i]*(0.5*dt);
402 us[i] = u0[i] + k1u[i]*(0.5*dt);
408 xs[i] = x0[i] + k2x[i]*(0.5*dt);
409 us[i] = u0[i] + k2u[i]*(0.5*dt);
415 xs[i] = x0[i] + k3x[i]*dt;
416 us[i] = u0[i] + k3u[i]*dt;
422 xs[i] = x0[i] + (k1x[i] + k2x[i]*2. + k3x[i]*2. + k4x[i]) * (dt/6.);
423 us[i] = u0[i] + (k1u[i] + k2u[i]*2. + k3u[i]*2. + k4u[i]) * (dt/6.);
434 static const Double_t a[7][6] = {
435 {0., 0., 0., 0., 0., 0.},
436 {1./5., 0., 0., 0., 0., 0.},
437 {3./40., 9./40., 0., 0., 0., 0.},
438 {44./45., -56./15., 32./9., 0., 0., 0.},
439 {19372./6561., -25360./2187., 64448./6561., -212./729., 0., 0.},
440 {9017./3168., -355./33., 46732./5247., 49./176., -5103./18656., 0.},
441 {35./384., 0., 500./1113., 125./192., -2187./6784., 11./84.}};
443 static const Double_t e[7] = {71./57600., 0., -71./16695., 71./1920.,
444 -17253./339200., 22./525., -1./40.};
446 static const Double_t safe = 0.9, facmin = 0.2, facmax = 5.;
449 const Double_t total = TMath::Abs(dt);
450 const Double_t sgn = (dt<0.) ? -1. : 1.;
451 if(total==0. || n==0){
457 const Double_t hmin = total*1e-12;
462 std::vector<TVector3> x0, u0, xs(n), us(n);
463 std::vector<TVector3> kx[7], ku[7];
470 Bool_t rejected = kFALSE;
478 std::cerr<<
"event::DeriveRK45 : more than "<<
RK45MAXSUBSTEPS<<
" substeps in one call of DeriveDT("
479 <<dt<<
"), substep "<<h<<
". Integration given up (singular force ?)."<<std::endl;
485 Double_t remain = total - done;
487 Bool_t last = kFALSE;
488 if(h*1.01 >= remain){
492 Double_t hs = sgn*htry;
500 if(a[s][j]==0.)
continue;
501 xs[i] += kx[j][i] * (hs*a[s][j]);
502 us[i] += ku[j][i] * (hs*a[s][j]);
517 if(e[j]==0.)
continue;
518 ex += kx[j][i] * (hs*e[j]);
519 eu += ku[j][i] * (hs*e[j]);
522 Double_t um = rel ? 1./p->
GetM0() : 1.;
523 Double_t scx =
rk45Abs +
rk45Rel*TMath::Max(x0[i].Mag(), xs[i].Mag());
524 Double_t scu =
rk45Abs +
rk45Rel*TMath::Max(u0[i].Mag(), us[i].Mag())*um;
525 Double_t rx = ex.Mag()/scx;
526 Double_t ru = eu.Mag()*um/scu;
527 sum += rx*rx + ru*ru;
530 Double_t err = (nterm>0) ? TMath::Sqrt(sum/nterm) : 0.;
531 if(!(err<=1e300)) err = 1e300;
533 if(err<=1. || htry<=hmin){
535 std::cerr<<
"event::DeriveRK45 : substep "<<htry<<
" reached the minimum and the error is "
536 <<err<<
" times the tolerance. Accepted anyway (warned once per event)."<<std::endl;
540 Double_t fac = (err>0.) ? safe*TMath::Power(err, -0.2) : facmax;
541 fac = TMath::Min(facmax, TMath::Max(facmin, fac));
542 if(rejected) fac = TMath::Min(fac, 1.);
543 Double_t hnew = htry*fac;
545 if(last && htry<h && fac>=1.) hnew = TMath::Max(hnew, h);
557 h = htry*TMath::Max(facmin, safe*TMath::Power(err, -0.2));
568 vector<particle*>::iterator p1_;
569 vector<particle*>::iterator p2_;
570 vector<force*>::iterator ff_;
571 vector<particle*>::iterator pp_;
576 TVector3 tempforce = TVector3();
594 (*pp_)->releaseForce(dt);
626 vector<particle*>::iterator p1_;
631 std::cerr<<
"CALL PARTICLE ERROR:: Index over NParticle"<<endl;
646 vector<force*>::iterator f1_;
651 std::cerr<<
"CALL Force ERROR:: Index over NForce"<<endl;
665 vector<inspector*>::iterator p1_;
670 std::cerr<<
"CALL INSPECTOR ERROR:: Index over NInspector"<<endl;
686 vector<particle*>::iterator p_;
689 Long_t result = minima ? std::numeric_limits<Long_t>::max() : std::numeric_limits<Long_t>::min();
691 result = minima ? TMath::Min(result, (*p_)->GetECNT()) : TMath::Max(result, (*p_)->GetECNT());
699 vector<particle*>::iterator p_;
702 Double_t tempKE = 0.;
706 tempKE += (0.5)*(p->
GetM(!SR))*(p->
GetV().Mag2());
712 vector<particle*>::iterator p_;
713 Double_t tempKE = 0.;
718 Double_t v2 = p->
GetV().Mag2();
719 Double_t s = TMath::Sqrt(1. - v2);
720 tempKE += p->
GetM0() * v2 / (s*(1. + s));
727 vector<particle*>::iterator p_;
728 TVector3 result = TVector3();
732 TVector3 v = p->
GetV();
733 result += rel ? v * (p->
GetM0()/TMath::Sqrt(1. - v.Mag2())) : v * p->
GetM0();
740 vector<particle*>::iterator p_;
741 TVector3 result = TVector3();
745 TVector3 v = p->
GetV();
746 TVector3 mom = rel ? v * (p->
GetM0()/TMath::Sqrt(1. - v.Mag2())) : v * p->
GetM0();
747 result += (p->
GetX() - origin).Cross(mom);
757 vector<particle*>::iterator pt_;
758 vector<force*>::iterator ff_;
763 Double_t tempPE = 0.;
806 Double_t tempPE = 0.;
809 tempPE += 0.5*(
GetPE(i));
841 if(iperiod<1) iperiod = 1;
844 vector<inspector*>::iterator i_;
846 if(!(*i_)->IsValid()){
847 std::cerr<<
"event::DeriveInspect : inspector with undefined method / "
848 <<(*i_)->GetMethod()<<
" . Nothing derived."<<std::endl;
861 std::cerr<<
"event::DeriveInspect : stopped by the maximum number of steps ("
878 vector<inspector*>::iterator i_;
881 Int_t nnormal = 0, nabort = 0;
882 Bool_t resultAND = kTRUE;
883 Bool_t resultOR = kFALSE;
910 vector<particle*>::iterator p_;
911 Double_t result = 0.;
913 result = TMath::Max(result, (*p_)->GetTime());
919 Double_t result = std::numeric_limits<Double_t>::max();
930 std::vector<TVector3> acc;
932 Double_t result = 0.;
934 for(i=0; i<acc.size(); i++){
935 result = TMath::Max(result, acc[i].Mag());
943 vector<particle*>::iterator p_;
944 Bool_t resultAll = kTRUE;
945 Bool_t resultAny = kFALSE;
948 if((*p_)->IsInvincible())
continue;
954 if(n==0)
return kTRUE;
955 return all ? resultAll : resultAny;
967 TString result = TString(
"[ Event ");
970 result = result +
"\n";
974 result += TString(
"| Particles ");
975 result += TString(
"{ ");
976 std::vector<particle*>::iterator i;
978 result += (*i)->Print(onlymechanic, kTRUE);
980 result += TString(
" }");
982 result = result +
"\n";
987 result += TString(
"| Inspectors ");
988 result += TString(
"{ ");
992 std::vector<inspector*>::iterator i;
994 result += (*i)->Print(kTRUE, kTRUE, kTRUE, pprint);
996 result += TString(
" }");
999 result += TString(
" ]");
Compile-time limits and default tolerances (macros).
#define RK45MAXSUBSTEPS
Substeps event::kRK45 may try in one DeriveDT() call before it gives up (event::HasIntegratorFailed()...
virtual Bool_t Inside(TVector3 position)
Is the point inside the volume?
One simulated system: particles, pairwise forces, inspectors and the time integration.
void ComputeForce(std::vector< TVector3 > &frc)
Net force on every particle at the present state, in amu c^2/fm (zero for invincibles).
Double_t GetMaxAcceleration()
The largest acceleration (force / rest mass) of the non-invincible particles (amu c^2/fm/amu = c^2/fm...
Double_t rk45Abs
absolute tolerance
TVector3 GetNetAngularMomentum(TVector3 origin=TVector3())
Total angular momentum of the non-invincible particles (amu c fm).
void CalibrateVelocity()
Correct the speed of every non-invincible particle so that the given velocity is the velocity far fro...
Bool_t integFailed
the integrator gave up (see HasIntegratorFailed())
eventVolume * volume
not owned
std::vector< TVector3 > accCacheX
positions for which accCache is valid
Bool_t rk45Warned
the minimum-substep warning was printed
void AddForce(force *force_)
Add a force that acts on every pair of particles of the event.
Bool_t preDerived
whether pEvent is valid
Bool_t IsInsideVolume(Bool_t all=kTRUE)
Whether the non-invincible particles are inside the volume (SetVolume()).
void SetTolerance(Double_t rel, Double_t abs)
Set the tolerances of kRK45.
Bool_t pairSymmetric
Newton's third law in use.
Double_t GetNetKERel()
Relativistic kinetic energy, the sum of over the non-invincible particles (amu c^2).
inspector * getInspector(Int_t index)
Get an inspector by the order of addition (index as in getParticle(); owned by the event).
Double_t GetNetPE()
Potential energy of the whole event (amu c^2).
TString GetStopReasonName()
Name of the last stop reason: "NONE", "INSPECTOR", "ABORT", "MAXSTEPS", "INVALID" or "FAILED".
void DeriveVelocityVerlet(Double_t dt)
Velocity-Verlet step (kick-drift-kick on the momentum in the relativistic mode).
Double_t rk45Rel
relative tolerance
std::vector< inspector * > inspectors
owned
Int_t getNParticle()
Number of particles.
Double_t rk45H
substep suggested for the next call
void preDeriveSetup()
Placeholder for the set-up after preDerive().
force * getForce(Int_t index)
Get a force by the order of addition (index as in getParticle(); the force is not owned).
integrator integ
selected integrator
void AddInspector(inspector *inspector_)
Add an inspector (a terminating condition for DeriveInspect()).
Double_t GetPE(Int_t index, Bool_t OnlyInvincibles=kFALSE)
Potential energy of one particle with the other particles (amu c^2).
inspectMode imode
how the inspectors are combined
TVector3 GetNetMomentum()
Total momentum of the non-invincible particles (amu c).
event * pEvent
pre-derived copy (owned)
void DeriveMAX(Double_t dt=1)
Call DeriveDT() MAXSTEPS times (100,000, RPConfig.hpp), ignoring the inspectors.
Long_t GetDeriveN(const Bool_t minima=kFALSE)
Number of derived steps (particle::GetECNT()) over all particles, invincible ones included.
@ kStopFailed
the integrator gave up (kRK45 on a singular force, see HasIntegratorFailed())
@ kStopInspector
the inspectors were satisfied (normal end)
@ kStopAbort
an abort inspector fired
@ kStopNone
DeriveInspect() was not called yet (or is running)
@ kStopInvalid
an inspector has an undefined method; nothing was derived
@ kStopMaxSteps
the safety cap of steps (SetMaxInspectSteps()) was reached
TString GetIntegratorName()
Name of the integrator in use, as accepted by SetIntegrator(TString).
particle * AddParticle(particle *particle_)
Add a particle to the event.
Bool_t resetPreDerive()
Delete the pre-derived copy.
Double_t GetMinDistance()
The smallest distance between two particles, invincible ones included (fm). The largest double for fe...
virtual Bool_t UseRelativistic()
Whether the relativistic option is really in effect: set, and not kTaylor.
event()
Make an empty event: no particles, forces, inspectors nor volume.
void FinishStep(Double_t dt)
Close one step: every particle counts the step, the moving ones advance their time,...
stopReason lastStop
reason of the last DeriveInspect()
@ kRK45
"RK45", adaptive Dormand-Prince
@ kVelocityVerlet
"VERLET", velocity Verlet
@ kRK4
"RK4", classical Runge-Kutta
@ kSymplecticEuler
"SEULER", symplectic Euler
@ kTaylor
"TAYLOR", original scheme (default)
void GetState(std::vector< TVector3 > &x, std::vector< TVector3 > &u)
Read the positions x and state variables u of all particles.
particle * getParticle(Int_t index)
Get a particle by the order of addition.
void ComputeDerivative(std::vector< TVector3 > &dx, std::vector< TVector3 > &du)
Time derivatives dx/dt = v and du/dt (F/m0, or F when relativistic) at the present state.
virtual ~event()
Delete the particles, the inspectors and the pre-derived copy (not the forces nor the volume).
Int_t nforce
number of forces
std::vector< particle * > particles
the particles, in the order of addition (owned)
Int_t getNForce()
Number of forces.
virtual void DeriveDT(Double_t dt=1)
Advance the event by one time step with the selected integrator.
void SetIntegrator(integrator integ_)
Select the integrator by enumerator (see integrator).
Bool_t Inspect()
Evaluate the terminating condition once, without deriving.
Long_t rk45NReject
rejected substeps
void ComputeAcceleration(std::vector< TVector3 > &acc)
Acceleration F/m0 of every particle at the present state, in c^2/fm (zero for invincibles).
Double_t GetNetKE(const Bool_t SR=kFALSE)
Kinetic energy of the non-invincible particles (amu c^2).
TString Print(Bool_t onlymechanic=kFALSE, Bool_t mute=kFALSE, Bool_t pprint=kFALSE)
Describe the event (particles and inspectors) as text.
Bool_t relativistic
relativistic flag
Long_t rk45NAccept
accepted substeps
void SetState(const std::vector< TVector3 > &x, const std::vector< TVector3 > &u)
Write positions and state variables back to the non-invincible particles.
void DeriveSymplecticEuler(Double_t dt)
Symplectic Euler step, v += a dt then x += v dt (momentum in the relativistic mode).
void DeriveTaylor(Double_t dt)
The original scheme: all forces are evaluated for the present state of every moving particle (every o...
void DeriveRK4(Double_t dt)
Classical 4th order Runge-Kutta step.
TVector3 MomentumOf(particle *p, const TVector3 &v)
The state variable u of the velocity v: v itself, or when relativistic.
std::vector< force * > forces
the forces (not owned)
TVector3 VelocityOf(particle *p, const TVector3 &u)
The velocity v of the state variable u (inverse of MomentumOf()).
void preDerive()
Make a copy of the event and derive it until its inspectors are satisfied (pre-derivation).
Long_t maxInspectSteps
safety cap of DeriveInspect()
Double_t GetTime()
The largest evolved time among the particles (fm/c). Invincible particles do not advance their time,...
void DeriveRK45(Double_t dt)
Dormand-Prince 5(4) with adaptive substeps, advancing exactly dt (see integrator).
Bool_t DeriveInspect(Int_t iperiod=1, Double_t dt=1)
Derive until Inspect() is satisfied.
Int_t getNInspector()
Number of inspectors.
std::vector< TVector3 > accCache
acceleration (or force) at the end of the last step
Int_t nparticle
number of particles
void DeriveDTN(Int_t n=1, Double_t dt=1)
Call DeriveDT() n times.
@ kInspectOR
stop when any of them is satisfied
Base class of the pairwise forces.
virtual TVector3 Evaluate(particle *p1_, particle *p2_)
Force on the first particle by the second.
virtual Double_t Potential(particle *p1_, particle *p2_)
Potential energy of the pair (amu c^2).
Inspector on the whole event.
Inspector on a pair of particles.
particle * GetParticle2()
The second particle.
particle * GetParticle1()
The first particle.
Inspector : one terminating condition of event::DeriveInspect().
TString GetMethod()
The method name as given.
Double_t GetCriterion()
The criterion value (default -1).
virtual Bool_t Inspect()
Whether the condition is satisfied now (the base class : never).
void SetAbort(Bool_t abort_=kTRUE)
Make this inspector an abort inspector.
Bool_t IsAbort()
Whether this is an abort inspector.
A point body with mass, position, velocity, step count, time and an optional recorded path.
TVector3 GetV()
Velocity in c (a copy).
void holdForce(const TVector3 f)
Add a force to the pending force of this particle.
Bool_t IsInvincible()
Whether the particle is fixed (invincible).
void applyDV(const TVector3 dv)
Add dv (c) to the velocity.
void applyV(const TVector3 v_)
Set the velocity (c).
TVector3 calibrateVelocity(event *ev_, Bool_t ignoreInvincible=kFALSE, Bool_t verbose=kFALSE)
Apply calculateCalibrateVelocity() to this particle.
Double_t GetM0()
Rest mass in amu.
void applyDX(const TVector3 dx)
Add dx (fm) to the position.
Double_t GetM(const Bool_t restmass=kTRUE)
Mass in amu.
TVector3 GetX()
Position in fm (a copy).
void applyMechanic(const TVector3 x_, const TVector3 v_)
Set position and velocity (fm, c).