RooParticle
Particle scattering simulator with ROOT
Loading...
Searching...
No Matches
asymptoteN.cpp
Go to the documentation of this file.
1#include "asymptoteN.hpp"
2
3#include "iostream"
4#include "map"
5#include "vector"
6#include <chrono>
7#include <cmath>
8#include <typeinfo>
9
10#include "TMath.h"
11
12#include "event.hpp"
13#include "particle.hpp"
14#include "force.hpp"
15#include "asymptote.hpp"
16
17namespace {
18
19typedef asymptoteN::pstate pstate;
20
21// relative round-off floor of every error estimate
22const Double_t kRoundOff = 4e-15;
23// a non-1/r force below this fraction of the pair potential and force, on a
24// receding pair, is taken as finished (short-range forces decay faster than 1/r^2)
25const Double_t kShortRange = 1e-17;
26
28// quadrature
30struct glRule { std::vector<Double_t> x, w; };
31
32// Gauss-Legendre nodes and weights on [-1, 1] (Newton on P_n), cached
33const glRule & GaussLegendre(Int_t n){
34 static std::map<Int_t, glRule> cache;
35 std::map<Int_t, glRule>::iterator it = cache.find(n);
36 if(it!=cache.end()) return it->second;
37 glRule g;
38 g.x.resize(n);
39 g.w.resize(n);
40 for(Int_t i=0; i<n; i++){
41 Double_t z = std::cos(TMath::Pi()*(i+0.75)/(n+0.5));
42 Double_t pp = 1.;
43 for(Int_t iter=0; iter<100; iter++){
44 Double_t p1 = 1., p2 = 0.;
45 for(Int_t j=1; j<=n; j++){
46 Double_t p3 = p2;
47 p2 = p1;
48 p1 = ((2.*j-1.)*z*p2 - (j-1.)*p3)/j;
49 }
50 pp = n*(z*p1 - p2)/(z*z - 1.);
51 Double_t z1 = z;
52 z = z1 - p1/pp;
53 if(std::fabs(z-z1) < 1e-15) break;
54 }
55 // derivative at the final node
56 Double_t p1 = 1., p2 = 0.;
57 for(Int_t j=1; j<=n; j++){
58 Double_t p3 = p2;
59 p2 = p1;
60 p1 = ((2.*j-1.)*z*p2 - (j-1.)*p3)/j;
61 }
62 pp = n*(z*p1 - p2)/(z*z - 1.);
63 g.x[i] = z;
64 g.w[i] = 2./((1.-z*z)*pp*pp);
65 }
66 return cache[n] = g;
67}
68
69// panels on [0, 1]
70const Double_t kBrPhi[] = {0., 0.01, 0.03, 0.06, 0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.};
71const Double_t kBrLow[] = {0., 1e-4, 1e-3, 3e-3, 0.01, 0.03, 0.1, 0.2, 0.35, 0.5, 0.65, 0.8, 0.9, 1.};
72const Double_t kBrHigh[] = {0., 0.1, 0.2, 0.35, 0.5, 0.65, 0.8, 0.9, 0.97, 0.99, 0.997, 0.999, 0.9999, 1.};
73const Int_t kNBrPhi = sizeof(kBrPhi)/sizeof(kBrPhi[0]);
74const Int_t kNBrLow = sizeof(kBrLow)/sizeof(kBrLow[0]);
75const Int_t kNBrHigh = sizeof(kBrHigh)/sizeof(kBrHigh[0]);
76
77template<class T, class F>
78T Composite(const F & f, const Double_t* br, Int_t nbr, Int_t n){
79 const glRule & g = GaussLegendre(n);
80 T s = T();
81 for(Int_t k=0; k+1<nbr; k++){
82 const Double_t a = br[k], b = br[k+1];
83 const Double_t hm = 0.5*(b-a), c = 0.5*(a+b);
84 for(Int_t i=0; i<n; i++){
85 s += f(c + hm*g.x[i]) * (hm*g.w[i]);
86 }
87 }
88 return s;
89}
90
91// lower comparison order of the quadrature error estimate
92Int_t LowerOrder(Int_t n){ Int_t m = (2*n)/3; return (m<4) ? 4 : m; }
93
94Double_t Norm(Double_t x){ return std::fabs(x); }
95Double_t Norm(const TVector3 & v){ return v.Mag(); }
96
97template<class T, class F>
98T Panel(const F & f, Double_t a, Double_t b, const glRule & g){
99 const Double_t hm = 0.5*(b-a), c = 0.5*(a+b);
100 T s = T();
101 for(size_t i=0; i<g.x.size(); i++) s += f(c + hm*g.x[i]) * (hm*g.w[i]);
102 return s;
103}
104
105// Adaptive composite Gauss-Legendre on [0, 1] : each panel is accepted when
106// the n- and LowerOrder(n)-point rules agree within tol * (panel width),
107// otherwise it is bisected. tol < 0 : relative, |tol| times the size of a
108// first non-adaptive pass. The accepted differences are added to err (a
109// pessimistic estimate : it is the error of the lower rule).
110template<class T, class F>
111T Adaptive(const F & f, const Double_t* br, Int_t nbr, Int_t n, Double_t tol, Double_t & err){
112 const glRule & gh = GaussLegendre(n);
113 const glRule & gl = GaussLegendre(LowerOrder(n));
114 if(tol < 0.){
115 T s0 = T();
116 for(Int_t k=0; k+1<nbr; k++) s0 += Panel<T>(f, br[k], br[k+1], gh);
117 tol = -tol*Norm(s0);
118 if(!(tol>0.)) tol = 1e-300;
119 }
120 struct seg { Double_t a, b; Int_t depth; };
121 std::vector<seg> stack;
122 for(Int_t k=nbr-2; k>=0; k--) stack.push_back({br[k], br[k+1], 0});
123 T s = T();
124 Int_t npanel = 0;
125 while(!stack.empty()){
126 seg p = stack.back();
127 stack.pop_back();
128 const T ih = Panel<T>(f, p.a, p.b, gh);
129 const T il = Panel<T>(f, p.a, p.b, gl);
130 const Double_t d = Norm(ih - il);
131 npanel++;
132 // accepted : within tolerance, or at the round-off level of the panel
133 // (a 1e-14 kink of the integrand must not exhaust the budget), or too deep
134 if(d <= tol*(p.b - p.a) || d <= 1e-12*Norm(ih) || p.depth >= 30 || npanel > 20000){
135 s += ih;
136 err += d;
137 }else{
138 const Double_t m = 0.5*(p.a + p.b);
139 stack.push_back({m, p.b, p.depth+1});
140 stack.push_back({p.a, m, p.depth+1});
141 }
142 }
143 return s;
144}
145
147// pair interaction through stand-in particles (mass, charge, position)
149class pairPot{
150public:
151 pairPot(const std::vector<force*> & f, Double_t ma_, Double_t qa_, Double_t mb_, Double_t qb_)
152 : forces(f), ma(ma_), qa(qa_), mb(mb_), qb(qb_) {}
153
154 // potential at separation r (central force assumed)
155 Double_t U(Double_t r) const {
156 EMparticle a(ma, qa, TVector3(0., 0., r)), b(mb, qb, TVector3());
157 Double_t s = 0.;
158 for(size_t k=0; k<forces.size(); k++) s += forces[k]->Potential(&a, &b);
159 return s;
160 }
161 // radial force -dU/dr at separation r
162 Double_t Fr(Double_t r) const {
163 EMparticle a(ma, qa, TVector3(0., 0., r)), b(mb, qb, TVector3());
164 Double_t s = 0.;
165 for(size_t k=0; k<forces.size(); k++) s += forces[k]->Evaluate(&a, &b).Z();
166 return s;
167 }
168 // force on a by b, a at d relative to b
169 TVector3 F(const TVector3 & d) const {
170 EMparticle a(ma, qa, d), b(mb, qb, TVector3());
171 TVector3 s;
172 for(size_t k=0; k<forces.size(); k++) s += forces[k]->Evaluate(&a, &b);
173 return s;
174 }
175 // force on b by a, a at d relative to b
176 TVector3 Fon2(const TVector3 & d) const {
177 EMparticle a(ma, qa, d), b(mb, qb, TVector3());
178 TVector3 s;
179 for(size_t k=0; k<forces.size(); k++) s += forces[k]->Evaluate(&b, &a);
180 return s;
181 }
182 // U(r0) - U(r0 + d) = integral of F_r, without cancellation for small d
183 Double_t DeltaU(Double_t r0, Double_t d) const {
184 if(std::fabs(d) < 1e-2*r0){
185 static const Double_t gx = std::sqrt(0.6);
186 const Double_t c = r0 + 0.5*d, hm = 0.5*d;
187 return hm*((5./9.)*Fr(c - hm*gx) + (8./9.)*Fr(c) + (5./9.)*Fr(c + hm*gx));
188 }
189 return U(r0) - U(r0 + d);
190 }
191private:
192 const std::vector<force*> & forces;
193 Double_t ma, qa, mb, qb;
194};
195
196// Split the forces into the pure 1/r part (unsoftened coulombForce and
197// universalForce : U = kappa / r) and the others. Returns kTRUE if there are
198// no others.
199Bool_t SplitForces(const std::vector<force*> & forces, Double_t ma, Double_t qa,
200 Double_t mb, Double_t qb, Double_t & kappa, std::vector<force*> & others){
201 kappa = 0.;
202 others.clear();
203 for(size_t k=0; k<forces.size(); k++){
204 force* f = forces[k];
205 if(typeid(*f) == typeid(coulombForce) && static_cast<coulombForce*>(f)->GetSoftening()==0.){
206 kappa += static_cast<coulombForce*>(f)->GetK()*qa*qb;
207 }else if(typeid(*f) == typeid(universalForce) && static_cast<universalForce*>(f)->GetSoftening()==0.){
208 kappa -= static_cast<universalForce*>(f)->GetG()*ma*mb;
209 }else{
210 others.push_back(f);
211 }
212 }
213 return others.empty();
214}
215Bool_t PureKappa(const std::vector<force*> & forces, Double_t ma, Double_t qa,
216 Double_t mb, Double_t qb, Double_t & kappa){
217 std::vector<force*> others;
218 return SplitForces(forces, ma, qa, mb, qb, kappa, others);
219}
220
221Double_t PotentialAt(const std::vector<force*> & forces, const pstate & pa, const pstate & pb){
222 EMparticle a(pa.m, pa.q, pa.x), b(pb.m, pb.q, pb.x);
223 Double_t s = 0.;
224 for(size_t k=0; k<forces.size(); k++) s += forces[k]->Potential(&a, &b);
225 return s;
226}
227
229// first order : straight-line impulse on a (r = xa - xb, w = va - vb)
231TVector3 BornInvR(const TVector3 & r, const TVector3 & w, Double_t kappa, UInt_t & fl){
232 if(kappa==0.) return TVector3();
233 const Double_t W = w.Mag(), R = r.Mag();
234 if(!(W>0.)){ fl |= asymptoteN::kMarginal; return TVector3(); }
235 const TVector3 wh = w*(1./W);
236 const Double_t s = r.Dot(wh);
237 const TVector3 bv = r - wh*s;
238 const Double_t b2 = bv.Mag2();
239 Double_t c;
240 if(s >= 0.){
241 c = 1./(R*(R+s)); // = (1 - s/R)/b^2, stable
242 }else{
243 if(b2 <= 1e-20*R*R){ fl |= asymptoteN::kSingular; return TVector3(); }
244 c = (1. - s/R)/b2;
245 }
246 return (bv*c + wh*(1./R))*(kappa/W);
247}
248
249TVector3 BornNumeric(const pairPot & pp, const TVector3 & r, const TVector3 & w, Int_t n,
250 Bool_t onB, UInt_t & fl, Double_t & qerr){
251 const Double_t W = w.Mag(), R = r.Mag();
252 if(!(W>0.)){ fl |= asymptoteN::kMarginal; return TVector3(); }
253 Double_t e = 0.;
254 const TVector3 wh = w*(1./W);
255 const Double_t s = r.Dot(wh);
256 const TVector3 bv = r - wh*s;
257 const Double_t b = bv.Mag();
258 auto F = [&](Double_t u)->TVector3{
259 TVector3 d = bv + wh*u;
260 return onB ? pp.Fon2(d) : pp.F(d);
261 };
262 TVector3 I;
263 if(s >= 0.){
264 const Double_t L = R;
265 auto f = [&](Double_t y)->TVector3{ Double_t om = 1.-y; return F(s + L*y/om)*(L/(om*om)); };
266 I = Adaptive<TVector3>(f, kBrLow, kNBrLow, n, -1e-13, e);
267 }else{
268 auto f1 = [&](Double_t z)->TVector3{ return F(s*(1.-z))*(-s); };
269 I = Adaptive<TVector3>(f1, kBrHigh, kNBrHigh, n, -1e-13, e);
270 const Double_t L2 = TMath::Max(b, 1e-3*R);
271 auto f2 = [&](Double_t y)->TVector3{ Double_t om = 1.-y; return F(L2*y/om)*(L2/(om*om)); };
272 I += Adaptive<TVector3>(f2, kBrLow, kNBrLow, n, -1e-13, e);
273 }
274 qerr = e/W;
275 return I*(1./W);
276}
277
279// exact isolated two-body problem for a central potential
281// g(r0 + d) - g(r0), g(r) = 2 (E - U(r)) / mu - h^2 / r^2 (radial velocity squared)
282Double_t GDiff(const pairPot & pp, Double_t mu, Double_t h, Double_t r0, Double_t d){
283 const Double_t r = r0 + d;
284 return (2./mu)*pp.DeltaU(r0, d) + h*h*d*(2.*r0 + d)/(r0*r0*r*r);
285}
286
287// first root inward from R of g(r) = wr2 + GDiff(R, r - R) ; -1 if none
288Double_t Turning(const pairPot & pp, Double_t mu, Double_t h, Double_t R, Double_t wr2){
289 auto g = [&](Double_t r)->Double_t{ return wr2 + GDiff(pp, mu, h, R, r - R); };
290 Double_t hi = R, lo = -1., r = R;
291 for(Int_t k=0; k<600; k++){
292 r *= 0.95;
293 if(g(r) < 0.){ lo = r; break; }
294 hi = r;
295 }
296 if(lo < 0.) return -1.;
297 for(Int_t it=0; it<200; it++){
298 const Double_t mid = 0.5*(lo + hi);
299 if(!(mid>lo && mid<hi)) break;
300 if(g(mid) < 0.) lo = mid; else hi = mid;
301 }
302 return hi;
303}
304
305struct pairSol {
306 TVector3 wout;
307 Double_t rho = 0.; // distance for the strength eta (closest approach ahead, or now)
308 Double_t qerr = 0.; // quadrature error of |wout| (c)
309 UInt_t flags = 0;
310 Bool_t ok = kFALSE;
311};
312
313pairSol SolveCentral(const pairPot & pp, const TVector3 & r, const TVector3 & w, Double_t mu, Int_t nq){
314 pairSol s;
315 const Double_t R = r.Mag(), w2 = w.Mag2(), wm = std::sqrt(w2);
316 s.rho = R;
317 if(!(R>0.) || !(mu>0.)){ s.flags |= asymptoteN::kInvalid; return s; }
318 const TVector3 rhat = r*(1./R);
319 const Double_t wr = w.Dot(rhat);
320 const TVector3 hv = r.Cross(w);
321 const Double_t h = hv.Mag();
322 const Double_t U0 = pp.U(R);
323 const Double_t E = 0.5*mu*w2 + U0;
324 if(!(E > 1e-12*(0.5*mu*w2 + std::fabs(U0)))){ s.flags |= asymptoteN::kMarginal; return s; }
325 const Double_t vinf = std::sqrt(2.*E/mu);
326
327 if(h <= 1e-12*R*wm){
328 // radial motion
329 if(wr >= 0.){ s.wout = rhat*vinf; s.ok = kTRUE; return s; }
330 const Double_t rt = Turning(pp, mu, 0., R, wr*wr);
331 if(rt > 0.){ s.wout = rhat*vinf; s.rho = rt; }
332 else { s.wout = rhat*(-vinf); s.rho = 0.; s.flags |= asymptoteN::kSingular; }
333 s.ok = kTRUE;
334 return s;
335 }
336
337 const TVector3 qhat = hv.Cross(rhat)*(1./h); // direction of the transverse motion
338 const Bool_t direct = (wr >= 0.3*wm);
339 Double_t rmin = R;
340 if(!direct){
341 rmin = Turning(pp, mu, h, R, wr*wr);
342 if(!(rmin>0.)){ s.flags |= asymptoteN::kSingular; return s; }
343 }
344 // angle swept from r0 to infinity ; r = r0 / (1 - y^2)
345 Double_t qe = 0.;
346 const Double_t kTolPhi = 1e-14; // rad
347 auto phiInf = [&](Double_t r0, Double_t gbase, Int_t n)->Double_t{
348 auto f = [&](Double_t y)->Double_t{
349 const Double_t om = 1. - y*y;
350 Double_t gg;
351 if(om <= 0.) gg = gbase + (2./mu)*pp.U(r0) + h*h/(r0*r0);
352 else gg = gbase + GDiff(pp, mu, h, r0, r0*y*y/om);
353 if(!(gg > 0.)) return 0.;
354 return 2.*y*(h/r0)/std::sqrt(gg);
355 };
356 return Adaptive<Double_t>(f, kBrPhi, kNBrPhi, n, kTolPhi, qe);
357 };
358 // angle swept from rmin to R ; r = rmin + (R - rmin) z^2
359 auto phiInner = [&](Int_t n)->Double_t{
360 const Double_t D = R - rmin;
361 if(!(D>0.)) return 0.;
362 auto f = [&](Double_t z)->Double_t{
363 const Double_t d = D*z*z, rr = rmin + d;
364 const Double_t gg = GDiff(pp, mu, h, rmin, d);
365 if(!(gg > 0.)) return 0.;
366 return 2.*z*D*h/(rr*rr*std::sqrt(gg));
367 };
368 return Adaptive<Double_t>(f, kBrLow, kNBrLow, n, kTolPhi, qe);
369 };
370 auto total = [&](Int_t n)->Double_t{
371 if(direct) return phiInf(R, wr*wr, n);
372 const Double_t p1 = phiInf(rmin, 0., n);
373 if(wr >= 0.) return p1 - phiInner(n); // just after the closest approach
374 if(wr <= -0.3*wm) return 2.*p1 - phiInf(R, wr*wr, n); // approaching, far
375 return p1 + phiInner(n); // approaching, near the turning point
376 };
377 const Double_t dphi = total(nq);
378 // the estimate is the difference of the two rules (that of the lower one) :
379 // pessimistic for the result of the higher one
380 s.qerr = vinf*qe;
381 s.wout = (rhat*std::cos(dphi) + qhat*std::sin(dphi))*vinf;
382 s.rho = (wr >= 0.) ? R : rmin;
383 s.ok = kTRUE;
384 return s;
385}
386
388// bodies (single particles or bound clusters)
390struct bodyW {
391 std::vector<Int_t> mem;
392 Bool_t fixed = kFALSE;
393 Double_t mdyn = 0.; // moving mass
394 Double_t mprobe = 0.; // all rest mass (probe for gravity)
395 Double_t q = 0.;
396 TVector3 x, v, p; // CM position, velocity, momentum (relativistic : sum gamma m v)
397 Double_t eint = 0., iso = 0.;
398 TVector3 dvPair, dvBorn, dpBorn;
399 Double_t cross = 0., qerrPair = 0., qerrBorn = 0., merr = 0., second = 0.;
400 UInt_t flags = 0;
401};
402
403void SetBody(bodyW & b, const std::vector<pstate> & ps, Bool_t rel){
404 b.fixed = kFALSE;
405 b.mdyn = b.mprobe = b.q = 0.;
406 b.x = b.v = b.p = TVector3();
407 Int_t ifix = -1;
408 for(size_t k=0; k<b.mem.size(); k++){
409 const pstate & s = ps[b.mem[k]];
410 b.mprobe += s.m;
411 b.q += s.q;
412 if(s.fixed){ b.fixed = kTRUE; if(ifix<0) ifix = b.mem[k]; continue; }
413 b.mdyn += s.m;
414 b.x += s.x*s.m;
415 b.v += s.v*s.m;
416 b.p += rel ? s.v*(s.m/std::sqrt(1. - s.v.Mag2())) : s.v*s.m;
417 }
418 if(b.fixed){
419 b.x = ps[ifix].x;
420 b.v = b.p = TVector3();
421 }else if(b.mdyn > 0.){
422 b.x *= 1./b.mdyn;
423 b.v *= 1./b.mdyn;
424 }
425}
426
427Double_t PairMu(const bodyW & a, const bodyW & b){
428 if(a.fixed) return b.mdyn;
429 if(b.fixed) return a.mdyn;
430 return a.mdyn*b.mdyn/(a.mdyn + b.mdyn);
431}
432
433Double_t BodyU(const bodyW & a, const bodyW & b, const std::vector<std::vector<Double_t> > & U,
434 const std::vector<pstate> & ps){
435 Double_t s = 0.;
436 for(size_t i=0; i<a.mem.size(); i++){
437 for(size_t j=0; j<b.mem.size(); j++){
438 if(ps[a.mem[i]].fixed && ps[b.mem[j]].fixed) continue;
439 s += U[a.mem[i]][b.mem[j]];
440 }
441 }
442 return s;
443}
444
445void BuildBodies(const std::vector<pstate> & ps, const std::vector<std::vector<Double_t> > & U,
446 Bool_t rel, std::vector<bodyW> & bodies){
447 const Int_t n = ps.size();
448 bodies.clear();
449 for(Int_t i=0; i<n; i++){
450 bodyW b;
451 b.mem.push_back(i);
452 SetBody(b, ps, rel);
453 bodies.push_back(b);
454 }
455 // merge the most bound pair of bodies while one has a negative pair energy
456 while(kTRUE){
457 Int_t ba = -1, bb = -1;
458 Double_t emin = 0.;
459 for(size_t a=0; a<bodies.size(); a++){
460 for(size_t b=a+1; b<bodies.size(); b++){
461 if(bodies[a].fixed && bodies[b].fixed) continue;
462 const Double_t u = BodyU(bodies[a], bodies[b], U, ps);
463 const Double_t mu = PairMu(bodies[a], bodies[b]);
464 const Double_t ke = 0.5*mu*(bodies[a].v - bodies[b].v).Mag2();
465 const Double_t e = ke + u;
466 if(e < -1e-12*(ke + std::fabs(u)) && e < emin){ emin = e; ba = a; bb = b; }
467 }
468 }
469 if(ba < 0) break;
470 bodies[ba].mem.insert(bodies[ba].mem.end(), bodies[bb].mem.begin(), bodies[bb].mem.end());
471 SetBody(bodies[ba], ps, rel);
472 bodies.erase(bodies.begin() + bb);
473 }
474}
475
476struct pairW {
477 Int_t a = 0, b = 0;
478 TVector3 r, w;
479 Double_t mu = 0., E = 0., eta = 0.;
480 Bool_t interacting = kFALSE, approaching = kFALSE, havePair = kFALSE, pure = kFALSE;
481 Double_t kappa = 0.;
482 TVector3 dwPair; // change of the relative velocity still to come (pairwise or first order)
483 TVector3 Ia, Ib; // first-order impulses on a and b
484 Double_t qerrPair = 0., qerrBorn = 0.;
485 UInt_t flags = 0;
486};
487
488void ReadState(event* ev, std::vector<pstate> & ps, Bool_t flip){
489 const Int_t n = ev->getNParticle();
490 ps.resize(n);
491 for(Int_t i=0; i<n; i++){
492 particle* p = ev->getParticle(i);
493 ps[i].m = p->GetM0();
494 ps[i].q = p->GetQ();
495 ps[i].x = p->GetX();
496 ps[i].fixed = p->IsInvincible();
497 ps[i].v = ps[i].fixed ? TVector3() : (flip ? -p->GetV() : p->GetV());
498 }
499}
500
501// light copy for the integrating methods : stand-in particles (no path
502// record), same forces, RK45, same pair symmetry and relativistic dynamics
503event* LightCopy(event* ev, Bool_t flip, Double_t rel, Double_t abs){
504 event* c = new event();
505 for(Int_t k=0; k<ev->getNForce(); k++) c->AddForce(ev->getForce(k));
506 for(Int_t i=0; i<ev->getNParticle(); i++){
507 particle* p = ev->getParticle(i);
508 TVector3 v = p->GetV();
509 if(flip && !p->IsInvincible()) v = -v;
510 c->AddParticle(new EMparticle(p->GetM0(), p->GetQ(), p->GetX(), v, p->IsInvincible(), kFALSE));
511 }
512 c->SetPairSymmetric(ev->IsPairSymmetric());
513 c->SetRelativistic(ev->IsRelativistic() && ev->GetIntegrator()!=event::kTaylor);
514 c->SetIntegrator(event::kRK45);
515 c->SetTolerance(rel, abs);
516 return c;
517}
518
519// time for the closest interacting pair to double its distance
520Double_t TimeScale(const std::vector<pstate> & ps, const std::vector<std::vector<Double_t> > & U){
521 Double_t t = 0.;
522 for(size_t i=0; i<ps.size(); i++){
523 for(size_t j=i+1; j<ps.size(); j++){
524 if(ps[i].fixed && ps[j].fixed) continue;
525 if(U[i][j]==0.) continue;
526 const Double_t R = (ps[i].x - ps[j].x).Mag();
527 const Double_t W = (ps[i].v - ps[j].v).Mag();
528 if(!(W>0.)) continue;
529 const Double_t tij = R/W;
530 if(t==0. || tij < t) t = tij;
531 }
532 }
533 return t;
534}
535
536} // namespace
537
539// asymptoteN
541void asymptoteN::Reset(Int_t n){
542 vNow.assign(n, TVector3());
543 vBody.assign(n, TVector3());
544 vInf.assign(n, TVector3());
545 err.assign(n, 0.);
546 mass.assign(n, 0.);
547 flags.assign(n, 0);
548 cluster.assign(n, -1);
549 fixed.assign(n, kFALSE);
550 clMembers.clear();
551 clEnergy.clear();
552 clIso.clear();
553 clVInf.clear();
554 allFlags = 0;
555 separating = kTRUE;
556 maxDefl = maxErr = vScale = maxEta = etaApp = 0.;
557 eNow = eInf = 0.;
558 runTime = tInt = 0.;
559 nBlocks = 0;
560 nSub = 0;
561}
562
563void asymptoteN::Analyze(event* ev, const std::vector<pstate> & ps, method m, Bool_t clusterOnly){
564 const Int_t n = ps.size();
565 std::vector<force*> forces;
566 for(Int_t k=0; k<ev->getNForce(); k++) forces.push_back(ev->getForce(k));
567 const Bool_t sym = ev->IsPairSymmetric();
568 const Bool_t rel = ev->IsRelativistic() && ev->GetIntegrator()!=event::kTaylor;
569
570 // reset the per-analysis outputs
571 for(Int_t i=0; i<n; i++){ vInf[i] = TVector3(); vBody[i] = TVector3(); err[i] = 0.; flags[i] = 0; cluster[i] = -1; }
572 clMembers.clear(); clEnergy.clear(); clIso.clear(); clVInf.clear();
573 allFlags = 0; separating = kTRUE; maxEta = etaApp = 0.;
574
575 Int_t nmoving = 0;
576 for(Int_t i=0; i<n; i++){
577 mass[i] = ps[i].m;
578 fixed[i] = ps[i].fixed;
579 if(!ps[i].fixed){
580 nmoving++;
581 if(!(ps[i].m > 0.)) allFlags |= kInvalid;
582 }
583 }
584 if(nmoving==0) allFlags |= kInvalid;
585 if(allFlags & kInvalid) return;
586
587 // pair potentials at the present positions
588 std::vector<std::vector<Double_t> > U(n, std::vector<Double_t>(n, 0.));
589 for(Int_t i=0; i<n; i++){
590 for(Int_t j=i+1; j<n; j++){
591 if(ps[i].fixed && ps[j].fixed) continue;
592 U[i][j] = U[j][i] = PotentialAt(forces, ps[i], ps[j]);
593 }
594 }
595 eNow = 0.;
596 for(Int_t i=0; i<n; i++){
597 if(!ps[i].fixed) eNow += 0.5*ps[i].m*ps[i].v.Mag2();
598 for(Int_t j=i+1; j<n; j++) eNow += U[i][j];
599 }
600
601 // bodies and clusters
602 std::vector<bodyW> bodies;
603 BuildBodies(ps, U, rel, bodies);
604 const Int_t nb = bodies.size();
605 for(Int_t a=0; a<nb; a++){
606 bodyW & B = bodies[a];
607 if(B.mem.size() < 2) continue;
608 // internal energy in the CM frame
609 Double_t e = 0.;
610 for(size_t i=0; i<B.mem.size(); i++){
611 const pstate & s = ps[B.mem[i]];
612 if(!s.fixed) e += 0.5*s.m*(s.v - B.v).Mag2();
613 for(size_t j=i+1; j<B.mem.size(); j++) e += U[B.mem[i]][B.mem[j]];
614 }
615 B.eint = e;
616 // isolation : tidal / multipole coupling to the other bodies
617 Double_t tid = 0.;
618 for(Int_t c=0; c<nb; c++){
619 if(c==a) continue;
620 const bodyW & C = bodies[c];
621 if(B.fixed && C.fixed) continue;
622 pairPot pp(forces, B.mprobe, B.q, C.mprobe, C.q);
623 const Double_t umono = pp.U((B.x - C.x).Mag());
624 tid += std::fabs(BodyU(B, C, U, ps) - umono);
625 }
626 B.iso = (e!=0.) ? tid/std::fabs(e) : 1e300;
627 B.flags |= kInCluster;
628 if(B.fixed) B.flags |= kBoundToFixed;
629 if(B.iso > isoThr) B.flags |= kNotIsolated;
630 }
631
632 if(clusterOnly){
633 for(Int_t a=0; a<nb; a++){
634 const bodyW & B = bodies[a];
635 Int_t cid = -1;
636 if(B.mem.size() > 1){
637 cid = clMembers.size();
638 clMembers.push_back(B.mem);
639 clEnergy.push_back(B.eint);
640 clIso.push_back(B.iso);
641 clVInf.push_back(B.v);
642 }
643 for(size_t k=0; k<B.mem.size(); k++){
644 const Int_t i = B.mem[k];
645 vInf[i] = ps[i].fixed ? TVector3() : B.v;
646 vBody[i] = vInf[i];
647 flags[i] = B.flags;
648 cluster[i] = cid;
649 }
650 allFlags |= B.flags;
651 }
652 return;
653 }
654
655 const Bool_t wantPair = (m!=kBorn) || estimate;
656
657 // pairs of bodies
658 std::vector<pairW> pairs;
659 for(Int_t a=0; a<nb; a++){
660 for(Int_t b=a+1; b<nb; b++){
661 bodyW & A = bodies[a];
662 bodyW & B = bodies[b];
663 if(A.fixed && B.fixed) continue;
664 pairW P;
665 P.a = a; P.b = b;
666 P.r = A.x - B.x;
667 P.w = A.v - B.v;
668 P.mu = PairMu(A, B);
669 const Double_t R = P.r.Mag();
670 if(!(R>0.) || !(P.mu>0.)){ allFlags |= kInvalid; continue; }
671 pairPot pp(forces, A.mprobe, A.q, B.mprobe, B.q);
672 const Double_t U0 = pp.U(R);
673 const TVector3 F0 = pp.F(P.r);
674 // a neutral cluster has no monopole, but its members still interact
675 P.interacting = (U0!=0. || F0.Mag2()!=0.
676 || ((A.mem.size()>1 || B.mem.size()>1) && BodyU(A, B, U, ps)!=0.));
677 if(!P.interacting){ pairs.push_back(P); continue; }
678 const Bool_t central = F0.Cross(P.r).Mag() <= 1e-10*F0.Mag()*R;
679 P.E = 0.5*P.mu*P.w.Mag2() + U0;
680 P.approaching = P.r.Dot(P.w) < 0.;
681 if(analytic){
682 std::vector<force*> others;
683 P.pure = SplitForces(forces, A.mprobe, A.q, B.mprobe, B.q, P.kappa, others);
684 if(!P.pure && !P.approaching){
685 // A receding pair outside the range of its other (short-range)
686 // forces : they are finished, the 1/r part is solved in closed form.
687 pairPot po(others, A.mprobe, A.q, B.mprobe, B.q);
688 const Double_t uo = po.U(R);
689 const TVector3 fo = po.F(P.r);
690 if(std::fabs(uo) <= kShortRange*std::fabs(U0) && fo.Mag() <= kShortRange*F0.Mag()) P.pure = kTRUE;
691 }
692 }
693
694 // first order
695 if(P.pure){
696 P.Ia = BornInvR(P.r, P.w, P.kappa, P.flags);
697 P.Ib = -P.Ia;
698 }else{
699 UInt_t fdummy = 0;
700 Double_t qb = 0.;
701 P.Ia = BornNumeric(pp, P.r, P.w, nquad, kFALSE, P.flags, P.qerrBorn);
702 P.Ib = sym ? -P.Ia : BornNumeric(pp, P.r, P.w, nquad, kTRUE, fdummy, qb);
703 P.qerrBorn += qb;
704 }
705
706 // exact pair solution
707 Double_t rho = R;
708 const Bool_t canPair = sym && central;
709 if(!canPair) P.flags |= kNoPairwise;
710 if(wantPair && canPair){
711 Bool_t generic = !P.pure;
712 if(P.pure){
713 asymptote two(P.r, P.w, P.mu, P.kappa);
714 if(two.HasAsymptote()){
715 P.dwPair = two.GetVOutRel() - P.w;
716 P.havePair = kTRUE;
717 rho = two.IsOutgoing() ? R : two.GetClosestApproach();
718 }else if(two.GetStatus()==asymptote::kRadialAttractive && !P.approaching){
719 generic = kTRUE; // radial, attractive, receding : asymptote gives none, the orbit solver does
721 P.flags |= kSingular;
722 }else{
723 P.flags |= kMarginal;
724 }
725 }
726 if(generic){
727 pairSol s = SolveCentral(pp, P.r, P.w, P.mu, nquad);
728 P.flags |= s.flags;
729 if(s.ok){
730 P.dwPair = s.wout - P.w;
731 P.qerrPair = s.qerr;
732 P.havePair = kTRUE;
733 rho = s.rho;
734 }
735 }
736 }
737 if(!P.havePair){
738 // first order in place of the pair solution
739 TVector3 dva = A.fixed ? TVector3() : P.Ia*(1./A.mdyn);
740 TVector3 dvb = B.fixed ? TVector3() : P.Ib*(1./B.mdyn);
741 P.dwPair = dva - dvb;
742 }
743 // strength
744 if(P.E > 1e-12*(0.5*P.mu*P.w.Mag2() + std::fabs(U0))){
745 Double_t num;
746 if(rho > 0.) num = TMath::Max(std::fabs(pp.U(rho)), rho*std::fabs(pp.Fr(rho)));
747 else num = 1e300;
748 P.eta = num/(2.*P.E);
749 }else{
750 P.eta = 1e300;
751 P.flags |= kMarginal;
752 }
753 if(P.approaching){
754 P.flags |= kApproaching;
755 if(P.eta > 1e-14) separating = kFALSE;
756 etaApp = TMath::Max(etaApp, P.eta);
757 }
758 maxEta = TMath::Max(maxEta, P.eta);
759 pairs.push_back(P);
760 }
761 }
762
763 // sums over the pairs
764 for(size_t k=0; k<pairs.size(); k++){
765 const pairW & P = pairs[k];
766 if(!P.interacting) continue;
767 bodyW & A = bodies[P.a];
768 bodyW & B = bodies[P.b];
769 if(!A.fixed){
770 A.dvPair += P.dwPair*(P.mu/A.mdyn);
771 A.dvBorn += P.Ia*(1./A.mdyn);
772 A.dpBorn += P.Ia;
773 A.flags |= P.flags;
774 A.qerrPair += P.qerrPair*(P.mu/A.mdyn);
775 A.qerrBorn += P.qerrBorn/A.mdyn;
776 }
777 if(!B.fixed){
778 B.dvPair -= P.dwPair*(P.mu/B.mdyn);
779 B.dvBorn += P.Ib*(1./B.mdyn);
780 B.dpBorn += P.Ib;
781 B.flags |= P.flags;
782 B.qerrPair += P.qerrPair*(P.mu/B.mdyn);
783 B.qerrBorn += P.qerrBorn/B.mdyn;
784 }
785 }
786
787 // error terms
788 for(size_t k=0; k<pairs.size(); k++){
789 const pairW & P = pairs[k];
790 if(!P.interacting) continue;
791 bodyW & A = bodies[P.a];
792 bodyW & B = bodies[P.b];
793 const TVector3 dVa = A.fixed ? TVector3() : A.dvPair;
794 const TVector3 dVb = B.fixed ? TVector3() : B.dvPair;
795 // cross term : the other bodies change the relative velocity of this pair
796 const Double_t ext = ((dVa - dVb) - P.dwPair).Mag();
797 const Double_t term = (P.eta < 1e299) ? crossC*P.eta*ext : 1e300;
798 // second order of a pair without its exact solution
799 const Double_t sec = P.havePair ? 0. : ((P.eta < 1e299) ? P.eta*P.dwPair.Mag() : 1e300);
800 const Bool_t strong = (crossC*P.eta > 0.5) && (ext > 1e-12*P.w.Mag());
801 if(!A.fixed){ A.cross += term*(P.mu/A.mdyn); A.second += sec*(P.mu/A.mdyn); if(strong) A.flags |= kStrong; }
802 if(!B.fixed){ B.cross += term*(P.mu/B.mdyn); B.second += sec*(P.mu/B.mdyn); if(strong) B.flags |= kStrong; }
803 // multipole error of clusters : members (moving rigidly) vs point composite
804 if(A.mem.size()>1 || B.mem.size()>1){
805 TVector3 isum;
806 for(size_t i=0; i<A.mem.size(); i++){
807 for(size_t j=0; j<B.mem.size(); j++){
808 const pstate & si = ps[A.mem[i]];
809 const pstate & sj = ps[B.mem[j]];
810 if(si.fixed && sj.fixed) continue;
811 pairPot pm(forces, si.m, si.q, sj.m, sj.q);
812 UInt_t fd = 0;
813 Double_t kap = 0.;
814 if(analytic && PureKappa(forces, si.m, si.q, sj.m, sj.q, kap)) isum += BornInvR(si.x - sj.x, P.w, kap, fd);
815 else{ Double_t qd = 0.; isum += BornNumeric(pm, si.x - sj.x, P.w, nquad, kFALSE, fd, qd); }
816 }
817 }
818 const Double_t dI = (isum - P.Ia).Mag();
819 if(!A.fixed) A.merr += dI/A.mdyn;
820 if(!B.fixed) B.merr += dI/B.mdyn;
821 }
822 }
823
824 // results
825 const Bool_t useBorn = (m==kBorn) || rel;
826 // velocity scale of the event for the round-off floor (relative velocities
827 // of a slow body are differences of the larger velocities)
828 Double_t vglob = 0.;
829 for(Int_t a=0; a<nb; a++){
830 if(bodies[a].fixed) continue;
831 vglob = TMath::Max(vglob, bodies[a].v.Mag());
832 vglob = TMath::Max(vglob, (bodies[a].v + bodies[a].dvPair).Mag());
833 }
834 for(Int_t a=0; a<nb; a++){
835 bodyW & B = bodies[a];
836 if(rel) B.flags |= kRelativistic;
837 TVector3 vinf;
838 Double_t e = 0.;
839 if(!B.fixed){
840 if(useBorn){
841 if(rel){
842 const TVector3 p = B.p + B.dpBorn;
843 vinf = p*(1./std::sqrt(B.mdyn*B.mdyn + p.Mag2()));
844 }else{
845 vinf = B.v + B.dvBorn;
846 }
847 e = B.cross + B.second + B.qerrBorn + B.merr;
848 if(wantPair) e += (B.dvBorn - B.dvPair).Mag();
849 }else{
850 vinf = B.v + B.dvPair;
851 e = B.cross + B.second + B.qerrPair + B.merr;
852 }
853 }
854 // round-off floor : a few ulp of the velocities involved
855 if(!B.fixed) e += kRoundOff*vglob;
856 Int_t cid = -1;
857 if(B.mem.size() > 1){
858 cid = clMembers.size();
859 clMembers.push_back(B.mem);
860 clEnergy.push_back(B.eint);
861 clIso.push_back(B.iso);
862 clVInf.push_back(vinf);
863 }
864 for(size_t k=0; k<B.mem.size(); k++){
865 const Int_t i = B.mem[k];
866 vInf[i] = ps[i].fixed ? TVector3() : vinf;
867 vBody[i] = ps[i].fixed ? TVector3() : B.v;
868 err[i] = ps[i].fixed ? 0. : e;
869 flags[i] = B.flags;
870 cluster[i] = cid;
871 }
872 allFlags |= B.flags;
873 }
874}
875
877 const Bool_t flip = (dir==kIncoming);
878 event* c = LightCopy(ev, flip, rkRel, rkAbs);
879 std::vector<pstate> ps;
880 ReadState(c, ps, kFALSE);
881 const Int_t n = ps.size();
882
883 std::vector<force*> forces;
884 for(Int_t k=0; k<ev->getNForce(); k++) forces.push_back(ev->getForce(k));
885 std::vector<std::vector<Double_t> > U(n, std::vector<Double_t>(n, 0.));
886 for(Int_t i=0; i<n; i++){
887 for(Int_t j=i+1; j<n; j++){
888 if(ps[i].fixed && ps[j].fixed) continue;
889 U[i][j] = U[j][i] = PotentialAt(forces, ps[i], ps[j]);
890 }
891 }
892 const Double_t T0 = TimeScale(ps, U);
893
894 auto intErr = [&](Double_t scale)->Double_t{
895 const Long_t ns = c->GetRK45NSubsteps() + c->GetRK45NRejected();
896 return (rkRel*scale + rkAbs)*std::sqrt((Double_t) ns);
897 };
898 auto scaleOf = [&](const std::vector<TVector3> & v)->Double_t{
899 Double_t s = 0.;
900 for(Int_t i=0; i<n; i++) if(!fixed[i]) s = TMath::Max(s, v[i].Mag());
901 return s;
902 };
903
904 if(m==kAuto){
905 for(Int_t k=0; ; k++){
906 Analyze(c, ps, kPairwise);
907 if(allFlags & kInvalid) break;
908 const Double_t sc = scaleOf(vInf);
909 Double_t emax = 0.;
910 for(Int_t i=0; i<n; i++) emax = TMath::Max(emax, err[i]);
911 const Bool_t ok = (emax + intErr(sc) <= autoTol*sc)
912 && !(allFlags & (kStrong | kSingular | kMarginal))
913 && !(etaApp > 1e-4);
914 if(ok) break;
915 if(k>=maxBlocks || !(T0>0.)){ allFlags |= kNotConverged; break; }
916 const Double_t dt = T0*TMath::Power(2., k);
917 c->DeriveDT(dt);
918 tInt += dt;
919 nBlocks++;
920 ReadState(c, ps, kFALSE);
921 }
922 const Double_t ie = intErr(scaleOf(vInf));
923 for(Int_t i=0; i<n; i++){
924 if(fixed[i]) continue;
925 err[i] += ie;
927 }
928 }else{
929 // brute force with Richardson extrapolation in 1/t
930 std::vector<TVector3> vPrev, rPrev;
931 std::vector<Int_t> sigPrev;
932 Double_t tPrev = 0.;
933 Bool_t haveV = kFALSE, haveR = kFALSE, done = kFALSE;
934 if(!(T0>0.)){
935 Analyze(c, ps, kBrute, kTRUE);
936 done = kTRUE;
937 }
938 for(Int_t k=0; !done; k++){
939 const Double_t dt = T0*TMath::Power(2., k);
940 c->DeriveDT(dt);
941 tInt += dt;
942 nBlocks++;
943 ReadState(c, ps, kFALSE);
944 Analyze(c, ps, kBrute, kTRUE);
945 if(allFlags & kInvalid) break;
946 const Double_t t = tInt + T0;
947 std::vector<TVector3> vk = vInf;
948 std::vector<Int_t> sig = cluster;
949 if(haveV && sig!=sigPrev){ haveV = haveR = kFALSE; } // cluster structure changed : restart
950 if(haveV){
951 std::vector<TVector3> rk(n);
952 for(Int_t i=0; i<n; i++) rk[i] = (vk[i]*t - vPrev[i]*tPrev)*(1./(t - tPrev));
953 if(haveR){
954 const Double_t sc = scaleOf(rk);
955 Double_t dmax = 0.;
956 std::vector<Double_t> d(n, 0.);
957 for(Int_t i=0; i<n; i++){ if(fixed[i]) continue; d[i] = (rk[i] - rPrev[i]).Mag(); dmax = TMath::Max(dmax, d[i]); }
958 const Bool_t conv = (k>=3) && dmax <= bruteTol*sc;
959 if(conv || k>=maxBlocks){
960 const Double_t ie = intErr(sc);
961 for(Int_t i=0; i<n; i++){
962 vInf[i] = fixed[i] ? TVector3() : rk[i];
963 err[i] = fixed[i] ? 0. : d[i] + ie;
964 if(!conv && !fixed[i]) flags[i] |= kNotConverged;
965 }
966 for(size_t cc=0; cc<clMembers.size(); cc++) clVInf[cc] = vInf[clMembers[cc][0]];
967 if(!conv) allFlags |= kNotConverged;
968 done = kTRUE;
969 }
970 }
971 rPrev = rk;
972 haveR = kTRUE;
973 }
974 vPrev = vk;
975 sigPrev = sig;
976 tPrev = t;
977 haveV = kTRUE;
978 }
979 }
981 delete c;
982}
983
985 const Int_t n = vInf.size();
986 if(dir==kIncoming){
987 for(Int_t i=0; i<n; i++){ vInf[i] = -vInf[i]; vBody[i] = -vBody[i]; }
988 for(size_t c=0; c<clVInf.size(); c++) clVInf[c] = -clVInf[c];
989 }
990 for(Int_t i=0; i<n; i++){
991 particle* p = ev->getParticle(i);
992 vNow[i] = p->IsInvincible() ? TVector3() : p->GetV();
993 }
994 maxDefl = maxErr = vScale = 0.;
995 eInf = 0.;
996 for(Int_t i=0; i<n; i++){
997 if(fixed[i]) continue;
998 maxDefl = TMath::Max(maxDefl, GetDeflection(i));
999 maxErr = TMath::Max(maxErr, err[i]);
1000 vScale = TMath::Max(vScale, vInf[i].Mag());
1001 eInf += 0.5*mass[i]*vInf[i].Mag2();
1002 }
1003 for(size_t c=0; c<clEnergy.size(); c++) eInf += clEnergy[c];
1004}
1005
1007 auto t0 = std::chrono::steady_clock::now();
1008 meth = m;
1009 dir = d;
1010 if(ev==nullptr || ev->getNParticle()==0){
1011 Reset(0);
1013 return kFALSE;
1014 }
1015 const Int_t n = ev->getNParticle();
1016 Reset(n);
1017 std::vector<pstate> ps;
1018 ReadState(ev, ps, d==kIncoming);
1019 if(m==kBorn || m==kPairwise){
1020 Analyze(ev, ps, m);
1021 }else{
1022 // velocities of the bodies and the energy of the given state
1023 Analyze(ev, ps, kPairwise, kTRUE);
1024 std::vector<TVector3> vb = vBody;
1025 const Double_t e0 = eNow;
1026 if(!(allFlags & kInvalid)) RunIntegrating(ev, m);
1027 vBody = vb;
1028 eNow = e0;
1029 }
1030 Finish(ev);
1031 runTime = std::chrono::duration<Double_t>(std::chrono::steady_clock::now() - t0).count();
1032 return IsValid();
1033}
1034
1035Double_t asymptoteN::GetDeflection(Int_t i) const{
1036 const TVector3 & a = vBody.at(i);
1037 const TVector3 & b = vInf.at(i);
1038 if(!(a.Mag2()>0.) || !(b.Mag2()>0.)) return 0.;
1039 return asymptote::Angle(a, b);
1040}
1041
1042Double_t asymptoteN::GetAngularError(Int_t i) const{
1043 const Double_t v = vInf.at(i).Mag();
1044 return (v>0.) ? err.at(i)/v : 0.;
1045}
1046
1048 TVector3 p;
1049 for(size_t i=0; i<vInf.size(); i++) if(!fixed[i]) p += vInf[i]*mass[i];
1050 return p;
1051}
1052
1053TString asymptoteN::GetFlagNames(UInt_t f) const{
1054 static const char* names[] = {"APPROACHING", "STRONG", "CLUSTER", "NOT_ISOLATED", "BOUND_TO_FIXED",
1055 "RELATIVISTIC", "NO_PAIRWISE", "MARGINAL", "SINGULAR", "NOT_CONVERGED", "INVALID"};
1056 TString s;
1057 for(Int_t k=0; k<11; k++){
1058 if(f & (1u<<k)){
1059 if(s.Length()>0) s += "|";
1060 s += names[k];
1061 }
1062 }
1063 if(s.Length()==0) s = "OK";
1064 return s;
1065}
1066
1067TString asymptoteN::Print(Bool_t mute) const{
1068 static const char* mnames[] = {"BORN", "PAIRWISE", "AUTO", "BRUTE"};
1069 TString out;
1070 out += Form("asymptoteN : %s, %s, flags %s\n", mnames[meth], dir==kOutgoing ? "outgoing" : "incoming",
1071 GetFlagNames(allFlags).Data());
1072 out += Form(" max deflection %.3e rad, max error %.3e c (relative %.3e), max eta %.3e, separating %s\n",
1073 maxDefl, maxErr, GetMaxRelError(), maxEta, separating ? "yes" : "no");
1074 out += Form(" energy now %.12e, at infinity %.12e ; run time %.3e s, integrated %.3e fm/c, %ld substeps\n",
1075 eNow, eInf, runTime, tInt, nSub);
1076 for(size_t i=0; i<vInf.size(); i++){
1077 out += Form(" %2d %s v_inf (% .12e, % .12e, % .12e) err %.2e defl %.3e cl %d %s\n", (Int_t) i,
1078 fixed[i] ? "fixed " : "moving", vInf[i].X(), vInf[i].Y(), vInf[i].Z(), err[i],
1079 GetDeflection(i), cluster[i], GetFlagNames(flags[i]).Data());
1080 }
1081 if(!mute) std::cout<<out;
1082 return out;
1083}
1084
1085Double_t asymptoteN::RemainingDeflection(event* ev, Bool_t* sep){
1086 asymptoteN a;
1087 a.SetErrorEstimate(kFALSE);
1088 a.Compute(ev, kBorn);
1089 if(sep) *sep = a.IsSeparating();
1090 return a.GetMaxDeflection();
1091}
1092
1093Double_t asymptoteN::RemainingChange(event* ev, Bool_t* sep){
1094 asymptoteN a;
1095 a.SetErrorEstimate(kFALSE);
1096 a.Compute(ev, kBorn);
1097 if(sep) *sep = a.IsSeparating();
1098 Double_t r = 0.;
1099 for(Int_t i=0; i<a.GetNParticle(); i++){
1100 if(a.IsFixed(i)) continue;
1101 const Double_t v = a.GetVInf(i).Mag();
1102 if(v>0.) r = TMath::Max(r, a.GetResidual(i).Mag()/v);
1103 }
1104 return r;
1105}
Analytic two-body 1/r asymptote calculator (asymptote), used to validate the integrators.
A particle with a charge, for electromagnetic forces (coulombForce).
Definition particle.hpp:249
std::vector< Double_t > mass
Double_t isoThr
Double_t crossC
direction dir
std::vector< TVector3 > vNow
std::vector< TVector3 > vInf
Double_t rkRel
std::vector< TVector3 > clVInf
Bool_t IsSeparating() const
Double_t bruteTol
Bool_t estimate
std::vector< Bool_t > fixed
Double_t etaApp
Double_t vScale
std::vector< Double_t > clEnergy
std::vector< Double_t > err
Double_t eInf
std::vector< TVector3 > vBody
Int_t maxBlocks
std::vector< std::vector< Int_t > > clMembers
Double_t GetMaxRelError() const
Bool_t IsValid() const
static Double_t RemainingChange(event *ev, Bool_t *separating=nullptr)
UInt_t allFlags
Double_t runTime
void RunIntegrating(event *ev, method m)
Double_t maxEta
static Double_t RemainingDeflection(event *ev, Bool_t *separating=nullptr)
TVector3 GetVInf(Int_t i) const
TString Print(Bool_t mute=kFALSE) const
Bool_t separating
std::vector< Double_t > clIso
std::vector< UInt_t > flags
Double_t rkAbs
void Reset(Int_t n)
void Finish(event *ev)
void Analyze(event *ev, const std::vector< pstate > &ps, method m, Bool_t clusterOnly=kFALSE)
Double_t tInt
Double_t autoTol
Double_t eNow
Bool_t analytic
Double_t GetDeflection(Int_t i) const
std::vector< Int_t > cluster
Double_t maxErr
TVector3 GetResidual(Int_t i) const
Int_t GetNParticle() const
TString GetFlagNames(UInt_t f) const
Double_t maxDefl
TVector3 GetMomentumInf() const
Double_t GetAngularError(Int_t i) const
Double_t GetMaxDeflection() const
Bool_t IsFixed(Int_t i) const
Bool_t Compute(event *ev, method m=kPairwise, direction d=kOutgoing)
void SetErrorEstimate(Bool_t on=kTRUE)
Analytic two-body 1/r asymptote calculator.
status GetStatus() const
Bool_t IsOutgoing() const
the given state is past closest approach ( ); no check of the status
Double_t GetClosestApproach() const
distance of closest approach (fm)
@ kRadialAttractive
, (whatever the sign of E): falls into the singularity, no asymptote ( )
Bool_t HasAsymptote() const
true for kUnbound, kHeadOn, kFree
TVector3 GetVOutRel() const
outgoing relative velocity at
static Double_t Angle(const TVector3 &a, const TVector3 &b)
Angle between two vectors, , accurate at 0 and .
Electric (Coulomb) force between the charges of two particles.
Definition force.hpp:66
One simulated system: particles, pairwise forces, inspectors and the time integration.
Definition event.hpp:93
Int_t getNParticle()
Number of particles.
Definition event.hpp:365
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
Long_t GetRK45NSubsteps()
Number of accepted kRK45 substeps so far.
Definition event.hpp:226
Long_t GetRK45NRejected()
Number of rejected kRK45 substeps so far.
Definition event.hpp:229
@ kRK45
"RK45", adaptive Dormand-Prince
Definition event.hpp:118
@ kTaylor
"TAYLOR", original scheme (default)
Definition event.hpp:114
particle * getParticle(Int_t index)
Get a particle by the order of addition.
Definition event.cpp:625
integrator GetIntegrator()
The integrator in use (default kTaylor).
Definition event.hpp:198
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
Bool_t IsPairSymmetric()
Whether the pair symmetry is in use (default kTRUE).
Definition event.hpp:245
Bool_t IsRelativistic()
Whether the relativistic flag is set (the flag only; kTaylor ignores it).
Definition event.hpp:265
Base class of the pairwise forces.
Definition force.hpp:24
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
Bool_t IsInvincible()
Whether the particle is fixed (invincible).
Definition particle.hpp:208
Double_t GetM0()
Rest mass in amu.
Definition particle.hpp:179
Double_t GetQ()
Charge in e. 0 for a plain particle, set by EMparticle.
Definition particle.hpp:199
TVector3 GetX()
Position in fm (a copy).
Definition particle.hpp:161
Newtonian gravity between the rest masses of two particles. Always attractive.
Definition force.hpp:104
Double_t GetSoftening()
The softening length eps in fm.
Definition force.hpp:120