RooParticle
Particle scattering simulator with ROOT
Loading...
Searching...
No Matches
event.cpp
Go to the documentation of this file.
1#include "iostream"
2#include "list"
3#include "vector"
4#include "stdio.h"
5#include <limits>
6
7#include "RPConfig.hpp"
8
9#include "TROOT.h"
10#include "TString.h"
11#include "TMatrixTBase.h"
12#include "TVector3.h"
13
14#include "force.hpp"
15
16#include "particle.hpp"
17#include "event.hpp"
18#include "inspector.hpp"
19
22
23//Copy Constructor
25 //Copy all particle into new event
26 vector<particle*>::iterator p_;
27 for(p_=e.particles.begin(); p_!=e.particles.end(); p_++){
28 AddParticle(new particle(**p_));
29 }
30
31 //Add all force into new event (not duplicate)
32 vector<force*>::iterator ff_;
33 for(ff_=e.forces.begin(); ff_!=e.forces.end(); ff_++){
34 AddForce(*ff_);
35 }
36
37 //copy inspectors : link all particles in each inspectors
38 vector<inspector*>::iterator ii_;
39
40 for(ii_=e.inspectors.begin(); ii_!=e.inspectors.end(); ii_++){
41 if((*ii_)->IsInspectorP()){
42 inspectorP* iis; //source inspector
43 iis = (inspectorP*) *ii_;
44 Int_t i;
45 // found again for every inspector : an index left from the previous
46 // inspector would silently link this one to the wrong particles.
47 Int_t ip1=-1, ip2=-1;
48 for(i=0; i<(Int_t) e.particles.size();i++){
49 if((iis->GetParticle1())==e.particles[i]){
50 ip1 = i;
51 }
52 if((iis->GetParticle2())==e.particles[i]){
53 ip2 = i;
54 }
55 }
56 if(ip1<0 || ip2<0){
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;
59 continue;
60 }
61 inspectorP* iin = new inspectorP( // Make new inspector (linking particles)
62 getParticle(ip1), getParticle(ip2),
63 iis->GetMethod(), iis->GetCriterion());
64 iin->SetAbort(iis->IsAbort());
65 AddInspector(iin);
66 }else if((*ii_)->IsInspectorE()){
67 // event inspector : the copy watches the new event, not the source one
68 inspectorE* iie = (inspectorE*) *ii_;
69 inspectorE* iin = new inspectorE(this, iie->GetMethod(), iie->GetCriterion());
70 iin->SetAbort(iie->IsAbort());
71 AddInspector(iin);
72 }
73 }
74
75 //copy other properties
76 volume = e.volume;
78 nforce = e.nforce;
79 integ = e.integ;
80 imode = e.imode;
84 rk45Rel = e.rk45Rel;
85 rk45Abs = e.rk45Abs;
86 // rk45H is not copied : the copy finds its own substep from its first call.
87}
88
90 // The event owns its particles, its inspectors and the pre-derived copy.
91 // Forces and the volume are shared with the copies : not deleted here.
92 vector<particle*>::iterator p_;
93 for(p_=particles.begin(); p_!=particles.end(); p_++){
94 delete *p_;
95 }
96 particles.clear();
97
98 vector<inspector*>::iterator i_;
99 for(i_=inspectors.begin(); i_!=inspectors.end(); i_++){
100 delete *i_;
101 }
102 inspectors.clear();
103
104 delete pEvent;
105 pEvent = nullptr;
106}
107
108void event::SetTolerance(Double_t rel, Double_t abs){
109 // A zero (or negative) tolerance can never be met : keep the previous one.
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;
113 return;
114 }
115 rk45Rel = rel;
116 rk45Abs = abs;
117}
118
120 vector<particle*>::iterator p_;
121 particle *p;
122
123 for(p_=particles.begin(); p_!=particles.end(); p_++){
124 p=*p_;
125 if(p->IsInvincible()) continue;
126 p->calibrateVelocity(this);
127 }
128}
129
131 name.ToUpper();
132 if(name=="TAYLOR"){
134 }else if(name=="SEULER"){
136 }else if(name=="VERLET"){
138 }else if(name=="RK4"){
140 }else if(name=="RK45"){
142 }else{
143 std::cerr<<"event::SetIntegrator : No integrator named / "<<name
144 <<" (TAYLOR, SEULER, VERLET, RK4, RK45). Integrator is not changed."<<std::endl;
145 return kFALSE;
146 }
147 return kTRUE;
148}
149
151 switch(integ){
152 case kTaylor: return TString("TAYLOR");
153 case kSymplecticEuler: return TString("SEULER");
154 case kVelocityVerlet: return TString("VERLET");
155 case kRK4: return TString("RK4");
156 case kRK45: return TString("RK45");
157 }
158 return TString("");
159}
160
162 switch(lastStop){
163 case kStopNone: return TString("NONE");
164 case kStopInspector: return TString("INSPECTOR");
165 case kStopAbort: return TString("ABORT");
166 case kStopMaxSteps: return TString("MAXSTEPS");
167 case kStopInvalid: return TString("INVALID");
168 case kStopFailed: return TString("FAILED");
169 }
170 return TString("");
171}
172
173void event::DeriveDT(Double_t dt){
174 switch(integ){
176 case kVelocityVerlet: DeriveVelocityVerlet(dt); break;
177 case kRK4: DeriveRK4(dt); break;
178 case kRK45: DeriveRK45(dt); break;
179 case kTaylor:
180 default: DeriveTaylor(dt); break;
181 }
182}
183
184void event::ComputeForce(std::vector<TVector3> & frc){
185 const size_t n = particles.size();
186 frc.assign(n, TVector3());
187
188 size_t i, j;
189 vector<force*>::iterator ff_;
190 if(pairSymmetric){
191 // Each unordered pair once : the force on p1 by p2 goes to p1, its opposite to p2.
192 // The contributions to one particle are still summed in the order of the
193 // particles, so only the sign of the evaluation differs from the other branch.
194 for(i=0; i<n; i++){
195 particle* p1 = particles[i];
196 Bool_t move1 = !p1->IsInvincible();
197 for(j=i+1; j<n; j++){
198 particle* p2 = particles[j];
199 Bool_t move2 = !p2->IsInvincible();
200 if(!move1 && !move2) continue;
201 for(ff_=forces.begin(); ff_!=forces.end(); ff_++){
202 if(move1){
203 TVector3 f = (*ff_)->Evaluate(p1, p2);
204 frc[i] += f;
205 if(move2) frc[j] -= f;
206 }else{
207 // p1 is fixed : only the force on p2 is needed
208 frc[j] += (*ff_)->Evaluate(p2, p1);
209 }
210 }
211 }
212 }
213 }else{
214 // Every ordered pair : for forces without action-reaction symmetry.
215 for(i=0; i<n; i++){
216 particle* p1 = particles[i];
217 if(p1->IsInvincible()) continue;
218 for(j=0; j<n; j++){
219 if(i==j) continue;
220 for(ff_=forces.begin(); ff_!=forces.end(); ff_++){
221 frc[i] += (*ff_)->Evaluate(p1, particles[j]);
222 }
223 }
224 }
225 }
226}
227
228void event::ComputeAcceleration(std::vector<TVector3> & acc){
229 ComputeForce(acc);
230 size_t i;
231 for(i=0; i<acc.size(); i++){
232 particle* p = particles[i];
233 if(p->IsInvincible()) continue;
234 acc[i] = acc[i] * (1./p->GetM(kTRUE));
235 }
236}
237
238TVector3 event::MomentumOf(particle * p, const TVector3 & v){
239 if(!UseRelativistic()) return v;
240 return v * (p->GetM0() / TMath::Sqrt(1. - v.Mag2()));
241}
242
243TVector3 event::VelocityOf(particle * p, const TVector3 & u){
244 if(!UseRelativistic()) return u;
245 Double_t m0 = p->GetM0();
246 return u * (1. / TMath::Sqrt(m0*m0 + u.Mag2()));
247}
248
249void event::GetState(std::vector<TVector3> & x, std::vector<TVector3> & u){
250 const size_t n = particles.size();
251 x.resize(n);
252 u.resize(n);
253 size_t i;
254 for(i=0; i<n; i++){
255 x[i] = particles[i]->GetX();
256 u[i] = MomentumOf(particles[i], particles[i]->GetV());
257 }
258}
259
260void event::SetState(const std::vector<TVector3> & x, const std::vector<TVector3> & u){
261 size_t i;
262 for(i=0; i<particles.size(); i++){
263 particle* p = particles[i];
264 if(p->IsInvincible()) continue;
265 p->applyMechanic(x[i], VelocityOf(p, u[i]));
266 }
267}
268
269void event::ComputeDerivative(std::vector<TVector3> & dx, std::vector<TVector3> & du){
270 // dx/dt = v ; du/dt = F (relativistic, u = p) or F/m0 (u = v)
271 if(UseRelativistic()){
272 ComputeForce(du);
273 }else{
275 }
276 const size_t n = particles.size();
277 dx.resize(n);
278 size_t i;
279 for(i=0; i<n; i++){
280 dx[i] = particles[i]->GetV();
281 }
282}
283
284void event::FinishStep(Double_t dt){
285 vector<particle*>::iterator pp_;
286 for(pp_=particles.begin(); pp_!=particles.end(); pp_++){
287 (*pp_)->finishStep(dt);
288 }
289}
290
292 size_t i;
293 if(UseRelativistic()){
294 // p(t+dt) = p(t) + F(t) dt ; x(t+dt) = x(t) + v(p(t+dt)) dt
295 std::vector<TVector3> frc;
296 ComputeForce(frc);
297 for(i=0; i<particles.size(); i++){
298 particle* p = particles[i];
299 if(p->IsInvincible()) continue;
300 p->applyV(VelocityOf(p, MomentumOf(p, p->GetV()) + frc[i] * dt));
301 p->applyDX(p->GetV() * dt);
302 }
303 FinishStep(dt);
304 return;
305 }
306
307 // v(t+dt) = v(t) + a(t) dt ; x(t+dt) = x(t) + v(t+dt) dt
308 std::vector<TVector3> acc;
310
311 for(i=0; i<particles.size(); i++){
312 particle* p = particles[i];
313 if(p->IsInvincible()) continue;
314 p->applyDV(acc[i] * dt);
315 p->applyDX(p->GetV() * dt);
316 }
317 FinishStep(dt);
318}
319
321 // x(t+dt) = x(t) + v(t) dt + a(t) dt^2/2
322 // v(t+dt) = v(t) + (a(t) + a(t+dt)) dt/2
323 const size_t n = particles.size();
324 size_t i;
325
326 // a(t) : reuse the one calculated at the end of the previous step,
327 // only if every particle is still where that step left it.
328 Bool_t cached = (accCache.size()==n && accCacheX.size()==n);
329 for(i=0; cached && i<n; i++){
330 if(particles[i]->GetX() != accCacheX[i]) cached = kFALSE;
331 }
332 if(UseRelativistic()){
333 // Kick-drift-kick on the momentum (same scheme as below, as x += v dt + a dt^2/2
334 // is x += v(t+dt/2) dt) :
335 // p(t+dt/2) = p(t) + F(t) dt/2 ; x(t+dt) = x(t) + v(p(t+dt/2)) dt
336 // p(t+dt) = p(t+dt/2) + F(t+dt) dt/2
337 // The cache holds forces here ; SetRelativistic() clears it.
338 if(!cached){
340 }
341 std::vector<TVector3> uh(n);
342 for(i=0; i<n; i++){
343 particle* p = particles[i];
344 if(p->IsInvincible()) continue;
345 uh[i] = MomentumOf(p, p->GetV()) + accCache[i] * (0.5*dt);
346 TVector3 vh = VelocityOf(p, uh[i]);
347 p->applyMechanic(p->GetX() + vh * dt, vh);
348 }
349
351
352 accCacheX.resize(n);
353 for(i=0; i<n; i++){
354 particle* p = particles[i];
355 accCacheX[i] = p->GetX();
356 if(p->IsInvincible()) continue;
357 p->applyV(VelocityOf(p, uh[i] + accCache[i] * (0.5*dt)));
358 }
359 FinishStep(dt);
360 return;
361 }
362
363 if(!cached){
365 }
366 std::vector<TVector3> acc0 = accCache;
367
368 for(i=0; i<n; i++){
369 particle* p = particles[i];
370 if(p->IsInvincible()) continue;
371 p->applyDX(p->GetV() * dt + acc0[i] * (0.5*dt*dt));
372 }
373
375
376 accCacheX.resize(n);
377 for(i=0; i<n; i++){
378 particle* p = particles[i];
379 accCacheX[i] = p->GetX();
380 if(p->IsInvincible()) continue;
381 p->applyDV((acc0[i] + accCache[i]) * (0.5*dt));
382 }
383 FinishStep(dt);
384}
385
386void event::DeriveRK4(Double_t dt){
387 // Classical 4th order Runge-Kutta on (x, v) of all non-invincible particles.
388 // The state is (x, u) : u = v, or u = p in the relativistic mode.
389 const size_t n = particles.size();
390 size_t i;
391
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;
395
396 GetState(x0, u0);
397
398 // stage 1
399 ComputeDerivative(k1x, k1u);
400 for(i=0; i<n; i++){
401 xs[i] = x0[i] + k1x[i]*(0.5*dt);
402 us[i] = u0[i] + k1u[i]*(0.5*dt);
403 }
404 SetState(xs, us);
405 // stage 2
406 ComputeDerivative(k2x, k2u);
407 for(i=0; i<n; i++){
408 xs[i] = x0[i] + k2x[i]*(0.5*dt);
409 us[i] = u0[i] + k2u[i]*(0.5*dt);
410 }
411 SetState(xs, us);
412 // stage 3
413 ComputeDerivative(k3x, k3u);
414 for(i=0; i<n; i++){
415 xs[i] = x0[i] + k3x[i]*dt;
416 us[i] = u0[i] + k3u[i]*dt;
417 }
418 SetState(xs, us);
419 // stage 4
420 ComputeDerivative(k4x, k4u);
421 for(i=0; i<n; i++){
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.);
424 }
425 SetState(xs, us);
426 FinishStep(dt);
427}
428
429void event::DeriveRK45(Double_t dt){
430 // Dormand-Prince 5(4) (Hairer, Norsett, Wanner, Solving ODE I, II.5).
431 // The 5th order solution is kept; the embedded 4th order one gives the error.
432 // The last stage is evaluated at the new state, so it is the first stage of
433 // the next substep (FSAL). Forces do not depend on time, so the nodes c_i are not needed.
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.}};
442 // 5th - 4th order weights
443 static const Double_t e[7] = {71./57600., 0., -71./16695., 71./1920.,
444 -17253./339200., 22./525., -1./40.};
445 // step controller : h_new = h * clamp(safe * err^(-1/5), facmin, facmax)
446 static const Double_t safe = 0.9, facmin = 0.2, facmax = 5.;
447
448 const size_t n = particles.size();
449 const Double_t total = TMath::Abs(dt);
450 const Double_t sgn = (dt<0.) ? -1. : 1.;
451 if(total==0. || n==0){
452 FinishStep(dt);
453 return;
454 }
455 // below this substep the error is accepted as it is (with a warning),
456 // instead of looping for ever on a singular force
457 const Double_t hmin = total*1e-12;
458 const Bool_t rel = UseRelativistic();
459
460 size_t i;
461 Int_t s, j;
462 std::vector<TVector3> x0, u0, xs(n), us(n);
463 std::vector<TVector3> kx[7], ku[7];
464
465 GetState(x0, u0);
466 ComputeDerivative(kx[0], ku[0]);
467
468 Double_t h = (rk45H>0.) ? rk45H : total; // magnitude of the substep
469 Double_t done = 0.;
470 Bool_t rejected = kFALSE; // the present substep was rejected before
471
472 Long_t ntry = 0; // substeps tried in this call
473 while(kTRUE){
474 // A force singularity (e.g. an unsoftened attractive centre) shrinks the
475 // substep without end. Inspectors and the step cap are only looked at
476 // between calls, so the call itself has to give up.
477 if(++ntry > RK45MAXSUBSTEPS){
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;
480 integFailed = kTRUE;
481 SetState(x0, u0); // last accepted state
482 break;
483 }
484 // the end of dt : take the rest exactly (also when only a sliver would be left)
485 Double_t remain = total - done;
486 Double_t htry = h;
487 Bool_t last = kFALSE;
488 if(h*1.01 >= remain){
489 htry = remain;
490 last = kTRUE;
491 }
492 Double_t hs = sgn*htry;
493
494 // stages 2..7 ; after stage 7 the particles are at the 5th order solution
495 for(s=1; s<7; s++){
496 for(i=0; i<n; i++){
497 xs[i] = x0[i];
498 us[i] = u0[i];
499 for(j=0; j<s; j++){
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]);
503 }
504 }
505 SetState(xs, us);
506 ComputeDerivative(kx[s], ku[s]);
507 }
508
509 // RMS of the error over the moving particles, scaled by the tolerance
510 Double_t sum = 0.;
511 Int_t nterm = 0;
512 for(i=0; i<n; i++){
513 particle* p = particles[i];
514 if(p->IsInvincible()) continue;
515 TVector3 ex, eu;
516 for(j=0; j<7; j++){
517 if(e[j]==0.) continue;
518 ex += kx[j][i] * (hs*e[j]);
519 eu += ku[j][i] * (hs*e[j]);
520 }
521 // momentum / m0 : the absolute tolerance is in c in both modes
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;
528 nterm += 2;
529 }
530 Double_t err = (nterm>0) ? TMath::Sqrt(sum/nterm) : 0.;
531 if(!(err<=1e300)) err = 1e300; // NaN or inf : reject
532
533 if(err<=1. || htry<=hmin){
534 if(err>1. && !rk45Warned){
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;
537 rk45Warned = kTRUE;
538 }
539 rk45NAccept++;
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;
544 // a substep cut by the end of dt says nothing against the longer one suggested before
545 if(last && htry<h && fac>=1.) hnew = TMath::Max(hnew, h);
546 h = hnew;
547 rejected = kFALSE;
548
549 x0 = xs;
550 u0 = us;
551 kx[0] = kx[6];
552 ku[0] = ku[6];
553 if(last) break;
554 done += htry;
555 }else{
556 rk45NReject++;
557 h = htry*TMath::Max(facmin, safe*TMath::Power(err, -0.2));
558 rejected = kTRUE;
559 SetState(x0, u0);
560 }
561 }
562 rk45H = h;
563 FinishStep(dt);
564}
565
566void event::DeriveTaylor(Double_t dt){
567 // Int_t pi, pj, fi;
568 vector<particle*>::iterator p1_;
569 vector<particle*>::iterator p2_;
570 vector<force*>::iterator ff_;
571 vector<particle*>::iterator pp_;
572
573 particle *p1, *p2;
574 force *ff;
575
576 TVector3 tempforce = TVector3();
577 for(p1_=particles.begin(); p1_!=particles.end(); p1_++){
578 p1 = *p1_;
579 if(p1->IsInvincible()) continue;
580 for(p2_=particles.begin(); p2_!=particles.end(); p2_++){
581 p2 = *p2_;
582 if(p1==p2){
583 continue;
584 }
585 for(ff_=forces.begin(); ff_!=forces.end(); ff_++){
586 ff = *ff_;
587 tempforce = ff->Evaluate(p1, p2);
588 p1->holdForce(tempforce);
589 }
590 }
591 }
592
593 for(pp_=particles.begin(); pp_!=particles.end(); pp_++){
594 (*pp_)->releaseForce(dt);
595 }
596 return;
597
598 // Int_t i;
599 // for(i=0; i<recorders.size(); i++){
600 // if(recorders[i]->IsConditioned()){
601 // recorders[i]->Save();
602 // }else{
603 // recorders[i]->JudgeSave();
604 // }
605 // }
606 // return;
607}
608
609void event::DeriveDTN(Int_t n, Double_t dt){
610 Int_t i;
611 for(i=0; i<n; i++){
612 DeriveDT(dt);
613 // std::cout<<"DERIVE: "<<i<<endl;
614 }
615}
616
617void event::DeriveMAX(Double_t dt){
618 Int_t i;
619 for(i=0; i<MAXSTEPS; i++){
620 DeriveDT(dt);
621 // std::cout<<"DERIVE: "<<i<<endl;
622 }
623}
624
626 vector<particle*>::iterator p1_;
627 particle* p1;
628
629 Int_t i=0;
630 if(index >= getNParticle()){
631 std::cerr<<"CALL PARTICLE ERROR:: Index over NParticle"<<endl;
632 return 0;
633 }
634 for(p1_=particles.begin(); p1_!=particles.end(); p1_++){
635 // p1 = &p1_;
636 p1 = *p1_;
637 if(i==index){
638 return p1;
639 }
640 i++;
641 }
642 return p1;
643}
644
645force * event::getForce(Int_t index){
646 vector<force*>::iterator f1_;
647 force* f1;
648
649 Int_t i=0;
650 if(index >= getNForce()){
651 std::cerr<<"CALL Force ERROR:: Index over NForce"<<endl;
652 return 0;
653 }
654 for(f1_=forces.begin(); f1_!=forces.end(); f1_++){
655 f1 = *f1_;
656 if(i==index){
657 return f1;
658 }
659 i++;
660 }
661 return f1;
662}
663
665 vector<inspector*>::iterator p1_;
666 inspector* p1;
667
668 Int_t i=0;
669 if(index >= getNInspector()){
670 std::cerr<<"CALL INSPECTOR ERROR:: Index over NInspector"<<endl;
671 return 0;
672 }
673 for(p1_=inspectors.begin(); p1_!=inspectors.end(); p1_++){
674 // p1 = &p1_;
675 p1 = *p1_;
676 if(i==index){
677 return p1;
678 }
679 i++;
680 }
681 return p1;
682}
683
684Long_t event::GetDeriveN(const Bool_t minima){
685
686 vector<particle*>::iterator p_;
687 // particle *p;
688
689 Long_t result = minima ? std::numeric_limits<Long_t>::max() : std::numeric_limits<Long_t>::min();
690 for(p_=particles.begin(); p_!=particles.end(); p_++){
691 result = minima ? TMath::Min(result, (*p_)->GetECNT()) : TMath::Max(result, (*p_)->GetECNT());
692 }
693 return result;
694
695}
696
697Double_t event::GetNetKE(const Bool_t SR){
698
699 vector<particle*>::iterator p_;
700 particle *p;
701
702 Double_t tempKE = 0.;
703 for(p_=particles.begin(); p_!=particles.end(); p_++){
704 p = *p_;
705 if(p->IsInvincible()) continue;
706 tempKE += (0.5)*(p->GetM(!SR))*(p->GetV().Mag2());
707 }
708 return tempKE;
709}
710
712 vector<particle*>::iterator p_;
713 Double_t tempKE = 0.;
714 for(p_=particles.begin(); p_!=particles.end(); p_++){
715 particle* p = *p_;
716 if(p->IsInvincible()) continue;
717 // gamma - 1 = v^2 / (s (1 + s)), s = sqrt(1 - v^2) : no cancellation at low speed
718 Double_t v2 = p->GetV().Mag2();
719 Double_t s = TMath::Sqrt(1. - v2);
720 tempKE += p->GetM0() * v2 / (s*(1. + s));
721 }
722 return tempKE;
723}
724
726 const Bool_t rel = UseRelativistic();
727 vector<particle*>::iterator p_;
728 TVector3 result = TVector3();
729 for(p_=particles.begin(); p_!=particles.end(); p_++){
730 particle* p = *p_;
731 if(p->IsInvincible()) continue;
732 TVector3 v = p->GetV();
733 result += rel ? v * (p->GetM0()/TMath::Sqrt(1. - v.Mag2())) : v * p->GetM0();
734 }
735 return result;
736}
737
738TVector3 event::GetNetAngularMomentum(TVector3 origin){
739 const Bool_t rel = UseRelativistic();
740 vector<particle*>::iterator p_;
741 TVector3 result = TVector3();
742 for(p_=particles.begin(); p_!=particles.end(); p_++){
743 particle* p = *p_;
744 if(p->IsInvincible()) continue;
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);
748 }
749 return result;
750}
751
752Double_t event::GetPE(Int_t index, Bool_t OnlyInvincibles){
753 return GetPE(getParticle(index), OnlyInvincibles);
754}
755
756Double_t event::GetPE(particle* p, Bool_t OnlyInvincibles){
757 vector<particle*>::iterator pt_;
758 vector<force*>::iterator ff_;
759
760 particle *pt;
761 force *ff;
762
763 Double_t tempPE = 0.;
764
765 for(pt_=particles.begin(); pt_!=particles.end(); pt_++){
766 pt = *pt_;
767 if(pt==p){
768 continue;
769 }
770 for(ff_=forces.begin(); ff_!=forces.end(); ff_++){
771 ff = *ff_;
772 if((!(pt->IsInvincible()))&&OnlyInvincibles)continue;
773 tempPE += ff->Potential(p, pt);
774 }
775 }
776 return tempPE;
777}
778
780
781 // vector<particle*>::iterator p1_;
782 // vector<particle*>::iterator p2_;
783 // vector<force*>::iterator ff_;
784
785 // particle *p1, *p2;
786 // force *ff;
787
788 // Double_t tempPE = 0.;
789 // for(p1_=particles.begin(); p1_!=particles.end(); p1_++){
790 // p1 = *p1_;
791 // for(p2_=particles.begin(); p2_!=particles.end(); p2_++){
792 // p2 = *p2_;
793 // if(p1==p2){
794 // continue;
795 // }
796 // for(ff_=forces.begin(); ff_!=forces.end(); ff_++){
797 // ff = *ff_;
798
799 // tempPE += ff->Potential(p1, p2);
800 // }
801 // }
802 // }
803 // return tempPE;
804
805 Int_t i;
806 Double_t tempPE = 0.;
807 for(i=0; i<getNParticle(); i++){
808 // std::cout<<"GETPARTICLEPE "<<i<<std::endl;
809 tempPE += 0.5*(GetPE(i));
810 }
811 return tempPE;
812}
813
814
816 if(preDerived){
817 delete pEvent;
818 pEvent = nullptr;
819 // cout<<"Clean previous pre-derive"<<endl;
820 }
821 pEvent = new event(*this);
822 preDerived = kTRUE;
824 // cout<<"NEW EVENT COMP"<<endl;
825 return;
826}
827
829 if(preDerived){
830 delete pEvent;
831 pEvent = nullptr;
832 preDerived=kFALSE;
833 return kTRUE;
834 }else{
835 return kFALSE;
836 }
837}
838
839Bool_t event::DeriveInspect(Int_t iperiod, Double_t dt){
841 if(iperiod<1) iperiod = 1;
842
843 // An inspector with an undefined method never gets satisfied : refuse to start.
844 vector<inspector*>::iterator i_;
845 for(i_=inspectors.begin(); i_!=inspectors.end(); i_++){
846 if(!(*i_)->IsValid()){
847 std::cerr<<"event::DeriveInspect : inspector with undefined method / "
848 <<(*i_)->GetMethod()<<" . Nothing derived."<<std::endl;
850 return kFALSE;
851 }
852 }
853
854 Long_t i;
855 for(i=0; ; i++){
856 if(i%iperiod==0 && Inspect()){
858 break;
859 }
861 std::cerr<<"event::DeriveInspect : stopped by the maximum number of steps ("
862 <<maxInspectSteps<<"), inspectors are not satisfied."<<std::endl;
864 break;
865 }
866 DeriveDT(dt);
867 if(integFailed){
869 break;
870 }
871 }
872 return lastStop==kStopInspector;
873}
874
876 // 1. Abort inspectors : any of them stops the event, whatever the others say.
877 // 2. The other inspectors are combined by the inspect mode (AND by default).
878 vector<inspector*>::iterator i_;
879 inspector* i;
880
881 Int_t nnormal = 0, nabort = 0;
882 Bool_t resultAND = kTRUE;
883 Bool_t resultOR = kFALSE;
884
885 for(i_=inspectors.begin(); i_!=inspectors.end(); i_++){
886 i = *i_;
887 if(i->IsAbort()){
888 nabort++;
889 if(i->Inspect()){
891 return kTRUE;
892 }
893 continue;
894 }
895 nnormal++;
896 Bool_t r = i->Inspect();
897 resultAND &= r;
898 resultOR |= r;
899 }
900
901 if(nnormal==0){
902 // only abort inspectors : keep going until one of them fires.
903 // no inspector at all : nothing to wait for (as before).
904 return nabort==0;
905 }
906 return imode==kInspectOR ? resultOR : resultAND;
907}
908
909Double_t event::GetTime(){
910 vector<particle*>::iterator p_;
911 Double_t result = 0.;
912 for(p_=particles.begin(); p_!=particles.end(); p_++){
913 result = TMath::Max(result, (*p_)->GetTime());
914 }
915 return result;
916}
917
919 Double_t result = std::numeric_limits<Double_t>::max();
920 size_t i, j;
921 for(i=0; i<particles.size(); i++){
922 for(j=i+1; j<particles.size(); j++){
923 result = TMath::Min(result, (particles[i]->GetX()-particles[j]->GetX()).Mag());
924 }
925 }
926 return result;
927}
928
930 std::vector<TVector3> acc;
932 Double_t result = 0.;
933 size_t i;
934 for(i=0; i<acc.size(); i++){
935 result = TMath::Max(result, acc[i].Mag());
936 }
937 return result;
938}
939
940Bool_t event::IsInsideVolume(Bool_t all){
941 // Without a volume, everything is regarded as inside.
942 if(!volume) return kTRUE;
943 vector<particle*>::iterator p_;
944 Bool_t resultAll = kTRUE;
945 Bool_t resultAny = kFALSE;
946 Int_t n = 0;
947 for(p_=particles.begin(); p_!=particles.end(); p_++){
948 if((*p_)->IsInvincible()) continue;
949 n++;
950 Bool_t in = volume->Inside((*p_)->GetX());
951 resultAll &= in;
952 resultAny |= in;
953 }
954 if(n==0) return kTRUE;
955 return all ? resultAll : resultAny;
956}
957
959 // this->getInspector(0)->GetParticle1()->
960
961
962
963 // return;
964}
965
966TString event::Print(Bool_t onlymechanic, Bool_t mute, Bool_t pprint){
967 TString result = TString("[ Event ");
968
969 if(pprint){
970 result = result + "\n";
971 }
972
973 if(particles.size()>0){
974 result += TString("| Particles ");
975 result += TString("{ ");
976 std::vector<particle*>::iterator i;
977 for(i=particles.begin(); i<particles.end(); i++){
978 result += (*i)->Print(onlymechanic, kTRUE);
979 }
980 result += TString(" }");
981 if(pprint){
982 result = result + "\n";
983 }
984 }
985
986 if(inspectors.size()>0){
987 result += TString("| Inspectors ");
988 result += TString("{ ");
989 if(pprint){
990 result += "\n";
991 }
992 std::vector<inspector*>::iterator i;
993 for(i=inspectors.begin(); i<inspectors.end(); i++){
994 result += (*i)->Print(kTRUE, kTRUE, kTRUE, pprint);
995 }
996 result += TString(" }");
997 }
998
999 result += TString(" ]");
1000
1001 if(!mute){
1002 cout<<result<<endl;
1003 }
1004
1005 return result;
1006}
Compile-time limits and default tolerances (macros).
#define RK45MAXSUBSTEPS
Substeps event::kRK45 may try in one DeriveDT() call before it gives up (event::HasIntegratorFailed()...
Definition RPConfig.hpp:16
#define MAXSTEPS
Definition RPConfig.hpp:8
virtual Bool_t Inside(TVector3 position)
Is the point inside the volume?
One simulated system: particles, pairwise forces, inspectors and the time integration.
Definition event.hpp:93
void ComputeForce(std::vector< TVector3 > &frc)
Net force on every particle at the present state, in amu c^2/fm (zero for invincibles).
Definition event.cpp:184
Double_t GetMaxAcceleration()
The largest acceleration (force / rest mass) of the non-invincible particles (amu c^2/fm/amu = c^2/fm...
Definition event.cpp:929
Double_t rk45Abs
absolute tolerance
Definition event.hpp:591
TVector3 GetNetAngularMomentum(TVector3 origin=TVector3())
Total angular momentum of the non-invincible particles (amu c fm).
Definition event.cpp:738
void CalibrateVelocity()
Correct the speed of every non-invincible particle so that the given velocity is the velocity far fro...
Definition event.cpp:119
Bool_t integFailed
the integrator gave up (see HasIntegratorFailed())
Definition event.hpp:596
eventVolume * volume
not owned
Definition event.hpp:575
std::vector< TVector3 > accCacheX
positions for which accCache is valid
Definition event.hpp:601
Bool_t rk45Warned
the minimum-substep warning was printed
Definition event.hpp:595
void AddForce(force *force_)
Add a force that acts on every pair of particles of the event.
Definition event.hpp:175
Bool_t preDerived
whether pEvent is valid
Definition event.hpp:577
Bool_t IsInsideVolume(Bool_t all=kTRUE)
Whether the non-invincible particles are inside the volume (SetVolume()).
Definition event.cpp:940
void SetTolerance(Double_t rel, Double_t abs)
Set the tolerances of kRK45.
Definition event.cpp:108
Bool_t pairSymmetric
Newton's third law in use.
Definition event.hpp:586
Double_t GetNetKERel()
Relativistic kinetic energy, the sum of over the non-invincible particles (amu c^2).
Definition event.cpp:711
inspector * getInspector(Int_t index)
Get an inspector by the order of addition (index as in getParticle(); owned by the event).
Definition event.cpp:664
Double_t GetNetPE()
Potential energy of the whole event (amu c^2).
Definition event.cpp:779
TString GetStopReasonName()
Name of the last stop reason: "NONE", "INSPECTOR", "ABORT", "MAXSTEPS", "INVALID" or "FAILED".
Definition event.cpp:161
void DeriveVelocityVerlet(Double_t dt)
Velocity-Verlet step (kick-drift-kick on the momentum in the relativistic mode).
Definition event.cpp:320
Double_t rk45Rel
relative tolerance
Definition event.hpp:590
std::vector< inspector * > inspectors
owned
Definition event.hpp:579
Int_t getNParticle()
Number of particles.
Definition event.hpp:365
Double_t rk45H
substep suggested for the next call
Definition event.hpp:592
void preDeriveSetup()
Placeholder for the set-up after preDerive().
Definition event.cpp:958
force * getForce(Int_t index)
Get a force by the order of addition (index as in getParticle(); the force is not owned).
Definition event.cpp:645
integrator integ
selected integrator
Definition event.hpp:581
void AddInspector(inspector *inspector_)
Add an inspector (a terminating condition for DeriveInspect()).
Definition event.hpp:179
Double_t GetPE(Int_t index, Bool_t OnlyInvincibles=kFALSE)
Potential energy of one particle with the other particles (amu c^2).
Definition event.cpp:752
inspectMode imode
how the inspectors are combined
Definition event.hpp:582
TVector3 GetNetMomentum()
Total momentum of the non-invincible particles (amu c).
Definition event.cpp:725
event * pEvent
pre-derived copy (owned)
Definition event.hpp:576
void DeriveMAX(Double_t dt=1)
Call DeriveDT() MAXSTEPS times (100,000, RPConfig.hpp), ignoring the inspectors.
Definition event.cpp:617
Long_t GetDeriveN(const Bool_t minima=kFALSE)
Number of derived steps (particle::GetECNT()) over all particles, invincible ones included.
Definition event.cpp:684
@ kStopFailed
the integrator gave up (kRK45 on a singular force, see HasIntegratorFailed())
Definition event.hpp:137
@ kStopInspector
the inspectors were satisfied (normal end)
Definition event.hpp:133
@ kStopAbort
an abort inspector fired
Definition event.hpp:134
@ kStopNone
DeriveInspect() was not called yet (or is running)
Definition event.hpp:132
@ kStopInvalid
an inspector has an undefined method; nothing was derived
Definition event.hpp:136
@ kStopMaxSteps
the safety cap of steps (SetMaxInspectSteps()) was reached
Definition event.hpp:135
TString GetIntegratorName()
Name of the integrator in use, as accepted by SetIntegrator(TString).
Definition event.cpp:150
particle * AddParticle(particle *particle_)
Add a particle to the event.
Definition event.hpp:168
Bool_t resetPreDerive()
Delete the pre-derived copy.
Definition event.cpp:828
Double_t GetMinDistance()
The smallest distance between two particles, invincible ones included (fm). The largest double for fe...
Definition event.cpp:918
virtual Bool_t UseRelativistic()
Whether the relativistic option is really in effect: set, and not kTaylor.
Definition event.hpp:557
event()
Make an empty event: no particles, forces, inspectors nor volume.
Definition event.cpp:20
void FinishStep(Double_t dt)
Close one step: every particle counts the step, the moving ones advance their time,...
Definition event.cpp:284
stopReason lastStop
reason of the last DeriveInspect()
Definition event.hpp:584
@ kRK45
"RK45", adaptive Dormand-Prince
Definition event.hpp:118
@ kVelocityVerlet
"VERLET", velocity Verlet
Definition event.hpp:116
@ kRK4
"RK4", classical Runge-Kutta
Definition event.hpp:117
@ kSymplecticEuler
"SEULER", symplectic Euler
Definition event.hpp:115
@ kTaylor
"TAYLOR", original scheme (default)
Definition event.hpp:114
void GetState(std::vector< TVector3 > &x, std::vector< TVector3 > &u)
Read the positions x and state variables u of all particles.
Definition event.cpp:249
particle * getParticle(Int_t index)
Get a particle by the order of addition.
Definition event.cpp:625
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.
Definition event.cpp:269
virtual ~event()
Delete the particles, the inspectors and the pre-derived copy (not the forces nor the volume).
Definition event.cpp:89
Int_t nforce
number of forces
Definition event.hpp:499
std::vector< particle * > particles
the particles, in the order of addition (owned)
Definition event.hpp:495
Int_t getNForce()
Number of forces.
Definition event.hpp:368
virtual void DeriveDT(Double_t dt=1)
Advance the event by one time step with the selected integrator.
Definition event.cpp:173
void SetIntegrator(integrator integ_)
Select the integrator by enumerator (see integrator).
Definition event.hpp:189
Bool_t Inspect()
Evaluate the terminating condition once, without deriving.
Definition event.cpp:875
Long_t rk45NReject
rejected substeps
Definition event.hpp:594
void ComputeAcceleration(std::vector< TVector3 > &acc)
Acceleration F/m0 of every particle at the present state, in c^2/fm (zero for invincibles).
Definition event.cpp:228
Double_t GetNetKE(const Bool_t SR=kFALSE)
Kinetic energy of the non-invincible particles (amu c^2).
Definition event.cpp:697
TString Print(Bool_t onlymechanic=kFALSE, Bool_t mute=kFALSE, Bool_t pprint=kFALSE)
Describe the event (particles and inspectors) as text.
Definition event.cpp:966
Bool_t relativistic
relativistic flag
Definition event.hpp:587
Long_t rk45NAccept
accepted substeps
Definition event.hpp:593
void SetState(const std::vector< TVector3 > &x, const std::vector< TVector3 > &u)
Write positions and state variables back to the non-invincible particles.
Definition event.cpp:260
void DeriveSymplecticEuler(Double_t dt)
Symplectic Euler step, v += a dt then x += v dt (momentum in the relativistic mode).
Definition event.cpp:291
void DeriveTaylor(Double_t dt)
The original scheme: all forces are evaluated for the present state of every moving particle (every o...
Definition event.cpp:566
void DeriveRK4(Double_t dt)
Classical 4th order Runge-Kutta step.
Definition event.cpp:386
TVector3 MomentumOf(particle *p, const TVector3 &v)
The state variable u of the velocity v: v itself, or when relativistic.
Definition event.cpp:238
std::vector< force * > forces
the forces (not owned)
Definition event.hpp:496
TVector3 VelocityOf(particle *p, const TVector3 &u)
The velocity v of the state variable u (inverse of MomentumOf()).
Definition event.cpp:243
void preDerive()
Make a copy of the event and derive it until its inspectors are satisfied (pre-derivation).
Definition event.cpp:815
Long_t maxInspectSteps
safety cap of DeriveInspect()
Definition event.hpp:583
Double_t GetTime()
The largest evolved time among the particles (fm/c). Invincible particles do not advance their time,...
Definition event.cpp:909
void DeriveRK45(Double_t dt)
Dormand-Prince 5(4) with adaptive substeps, advancing exactly dt (see integrator).
Definition event.cpp:429
Bool_t DeriveInspect(Int_t iperiod=1, Double_t dt=1)
Derive until Inspect() is satisfied.
Definition event.cpp:839
Int_t getNInspector()
Number of inspectors.
Definition event.hpp:371
std::vector< TVector3 > accCache
acceleration (or force) at the end of the last step
Definition event.hpp:600
Int_t nparticle
number of particles
Definition event.hpp:498
void DeriveDTN(Int_t n=1, Double_t dt=1)
Call DeriveDT() n times.
Definition event.cpp:609
@ kInspectOR
stop when any of them is satisfied
Definition event.hpp:127
Base class of the pairwise forces.
Definition force.hpp:24
virtual TVector3 Evaluate(particle *p1_, particle *p2_)
Force on the first particle by the second.
Definition force.hpp:33
virtual Double_t Potential(particle *p1_, particle *p2_)
Potential energy of the pair (amu c^2).
Definition force.hpp:42
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().
Definition inspector.hpp:28
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).
Definition inspector.hpp:87
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.
Definition particle.hpp:36
TVector3 GetV()
Velocity in c (a copy).
Definition particle.hpp:164
void holdForce(const TVector3 f)
Add a force to the pending force of this particle.
Definition particle.cpp:60
Bool_t IsInvincible()
Whether the particle is fixed (invincible).
Definition particle.hpp:208
void applyDV(const TVector3 dv)
Add dv (c) to the velocity.
Definition particle.hpp:135
void applyV(const TVector3 v_)
Set the velocity (c).
Definition particle.hpp:118
TVector3 calibrateVelocity(event *ev_, Bool_t ignoreInvincible=kFALSE, Bool_t verbose=kFALSE)
Apply calculateCalibrateVelocity() to this particle.
Definition particle.cpp:107
Double_t GetM0()
Rest mass in amu.
Definition particle.hpp:179
void applyDX(const TVector3 dx)
Add dx (fm) to the position.
Definition particle.hpp:132
Double_t GetM(const Bool_t restmass=kTRUE)
Mass in amu.
Definition particle.cpp:138
TVector3 GetX()
Position in fm (a copy).
Definition particle.hpp:161
void applyMechanic(const TVector3 x_, const TVector3 v_)
Set position and velocity (fm, c).
Definition particle.hpp:112
const char * name
Definition inspector.cpp:25