45 #define SYNC cudaThreadSynchronize()
48 using namespace swarm;
60 std::cerr <<
"# nsystems = " << ens.
nsys() <<
" nbodies/system = " << ens.
nbod() <<
"\n";
63 double time_init = cfg.
optional(
"time_init", 0.0);
66 const bool use_jacobi = cfg.
optional(
"use_jacobi", 0);
69 ifstream jacobi_input( cfg.
optional(
"input_mcmc_keplerian",
string(
"mcmc.out")).c_str() );
73 assert(jacobi_input.good());
76 jacobi_input >>
id >> mass_star;
78 ens[
sysid].time() = time_init;
79 ens[
sysid].set_active();
80 double x=0, y=0, z=0, vx=0, vy=0, vz=0;
81 ens.set_body(
sysid, 0, mass_star, x, y, z, vx, vy, vz);
82 ens[
sysid][0].attribute(0) = 0.005;
83 double mass_enclosed = mass_star;
84 for(
unsigned int bod=1;bod<ens.
nbod();++bod)
87 double mass_planet, a, e, i, O, w, M;
88 jacobi_input >> mass_planet >> a >> e >> i >> w >> O >> M;
95 mass_enclosed += mass_planet;
96 double mass = use_jacobi ? mass_enclosed : mass_star+mass_planet;
97 if(cfg.count(
"verbose")&&(
sysid<10))
98 std::cout <<
"# Drawing sysid= " <<
sysid <<
" bod= " << bod <<
' ' << mass_planet <<
" " << a <<
' ' << e <<
' ' << i*180./M_PI <<
' ' << O*180./M_PI <<
' ' << w*180./M_PI <<
' ' << M*180./M_PI <<
'\n';
100 calc_cartesian_for_ellipse(x,y,z,vx,vy,vz, a, e, i, O, w, M, mass);
105 double bx, by, bz, bvx, bvy, bvz;
107 x += bx; y += by; z += bz;
108 vx += bvx; vy += bvy; vz += bvz;
112 ens.set_body(
sysid, bod, mass_planet, x, y, z, vx, vy, vz);
113 ens[
sysid][bod].attribute(0) = 0.;
115 if(cfg.count(
"verbose")&&(
sysid<10))
117 double x_t = ens.x(
sysid,bod);
118 double y_t = ens.y(
sysid,bod);
119 double z_t = ens.z(
sysid,bod);
120 double vx_t = ens.vx(
sysid,bod);
121 double vy_t = ens.vy(
sysid,bod);
122 double vz_t = ens.vz(
sysid,bod);
124 std::cout <<
" x= " << x <<
"=" << x_t <<
" ";
125 std::cout <<
" y= " << y <<
"=" << y_t <<
" ";
126 std::cout <<
" z= " << z <<
"=" << z_t <<
" ";
127 std::cout <<
"vx= " << vx <<
"=" << vx_t <<
" ";
128 std::cout <<
"vy= " << vy <<
"=" << vy_t <<
" ";
129 std::cout <<
"vz= " << vz <<
"=" << vz_t <<
"\n";
136 for(
unsigned int bod=0;bod<ens.
nbod();++bod)
141 ens[
sysid][bod].attribute(0) = 0.;
156 std::cerr <<
"# nsystems = " << ens.
nsys() <<
" nbodies/system = " << ens.
nbod() <<
"\n";
159 double time_init = cfg.
optional(
"time_init", 0.0);
162 const bool use_jacobi = cfg.
optional(
"use_jacobi", 0);
165 ifstream mcmc_input( cfg.
optional(
"input_mcmc_cartesian",
string(
"mcmc.out")).c_str() );
167 assert(mcmc_input.good());
169 const int lines_to_skip = cfg.
optional(
"lines_to_skip", 0);
170 for(
int i=0;i<lines_to_skip;++i)
172 assert(mcmc_input.good());
174 mcmc_input.getline(junk,1024);
179 assert(mcmc_input.good());
181 std::vector<double> masses(ens.
nbod(),0.0);
182 for(
unsigned int bod=0;bod<ens.
nbod();++bod)
184 mcmc_input >> masses[bod];
188 ens[
sysid].time() = time_init;
189 ens[
sysid].set_active();
190 double x=0, y=0, z=0, vx=0, vy=0, vz=0;
191 ens.set_body(
sysid, 0, masses[0], x, y, z, vx, vy, vz);
192 ens[
sysid][0].attribute(0) = 0.005;
193 double mass_enclosed = masses[0];
194 for(
unsigned int bod=1;bod<ens.
nbod();++bod)
196 mcmc_input >> x >> y >> z >> vx >> vy >> vz;
198 mass_enclosed += masses[bod];
199 double mass = use_jacobi ? mass_enclosed : masses[0]+masses[bod];
200 if(cfg.count(
"verbose")&&(
sysid<10))
201 std::cout <<
"# Drawing sysid= " <<
sysid <<
" bod= " << bod <<
' ' << masses[bod] <<
" " << x <<
' ' << y <<
' ' << z <<
' ' << vx <<
' ' << vy <<
' ' << vz <<
'\n';
205 double bx, by, bz, bvx, bvy, bvz;
207 x += bx; y += by; z += bz;
208 vx += bvx; vy += bvy; vz += bvz;
212 ens.set_body(
sysid, bod, masses[bod], x, y, z, vx, vy, vz);
213 ens[
sysid][bod].attribute(0) = 0.0;
215 if(cfg.count(
"verbose")&&(
sysid<10))
217 double x_t = ens.x(
sysid,bod);
218 double y_t = ens.y(
sysid,bod);
219 double z_t = ens.z(
sysid,bod);
220 double vx_t = ens.vx(
sysid,bod);
221 double vy_t = ens.vy(
sysid,bod);
222 double vz_t = ens.vz(
sysid,bod);
224 std::cout <<
" x= " << x <<
"=" << x_t <<
" ";
225 std::cout <<
" y= " << y <<
"=" << y_t <<
" ";
226 std::cout <<
" z= " << z <<
"=" << z_t <<
" ";
227 std::cout <<
"vx= " << vx <<
"=" << vx_t <<
" ";
228 std::cout <<
"vy= " << vy <<
"=" << vy_t <<
" ";
229 std::cout <<
"vz= " << vz <<
"=" << vz_t <<
"\n";
233 std::vector<double> masses(ens.
nbod(),0.0);
234 double x=0, y=0, z=0, vx=0, vy=0, vz=0;
235 double mass_enclosed = 0.0;
237 ens[
sysid].time() = time_init;
238 ens[
sysid].set_active();
239 for(
unsigned int bod=0;bod<ens.
nbod();++bod)
241 mcmc_input >> masses[bod];
242 mcmc_input >> x >> y >> z >> vx >> vy >> vz;
244 mass_enclosed += masses[bod];
245 double mass = use_jacobi ? mass_enclosed : masses[0]+masses[bod];
246 if(cfg.count(
"verbose")&&(
sysid<10))
247 std::cout <<
"# Drawing sysid= " <<
sysid <<
" bod= " << bod <<
' ' << masses[bod] <<
" " << x <<
' ' << y <<
' ' << z <<
' ' << vx <<
' ' << vy <<
' ' << vz <<
'\n';
251 double bx, by, bz, bvx, bvy, bvz;
253 x += bx; y += by; z += bz;
254 vx += bvx; vy += bvy; vz += bvz;
258 ens.set_body(
sysid, bod, masses[bod], x, y, z, vx, vy, vz);
260 if(cfg.count(
"verbose")&&(
sysid<10))
262 double x_t = ens.x(
sysid,bod);
263 double y_t = ens.y(
sysid,bod);
264 double z_t = ens.z(
sysid,bod);
265 double vx_t = ens.vx(
sysid,bod);
266 double vy_t = ens.vy(
sysid,bod);
267 double vz_t = ens.vz(
sysid,bod);
269 std::cout <<
" x= " << x <<
"=" << x_t <<
" ";
270 std::cout <<
" y= " << y <<
"=" << y_t <<
" ";
271 std::cout <<
" z= " << z <<
"=" << z_t <<
" ";
272 std::cout <<
"vx= " << vx <<
"=" << vx_t <<
" ";
273 std::cout <<
"vy= " << vy <<
"=" << vy_t <<
" ";
274 std::cout <<
"vz= " << vz <<
"=" << vz_t <<
"\n";
281 for(
unsigned int bod=0;bod<ens.
nbod();++bod)
293 void print_system(
const swarm::ensemble& ens,
const int systemid, std::ostream &os = std::cout)
296 JACOBI, BARYCENTRIC, ASTROCENTRIC
297 } COORDINATE_SYSTEM = BARYCENTRIC;
298 const bool use_jacobi = cfg.
optional(
"use_jacobi_output", 0);
299 if(use_jacobi) COORDINATE_SYSTEM = JACOBI;
301 std::streamsize cout_precision_old = os.precision();
303 os <<
"sys_idx= " << systemid <<
" sys_id= " << ens[systemid].id() <<
" time= " << ens.time(systemid) <<
"\n";
304 double star_mass = ens.mass(systemid,0);
305 double mass_effective = star_mass;
306 double bx, by, bz, bvx, bvy, bvz;
307 switch(COORDINATE_SYSTEM)
318 ens.get_body(systemid,0,star_mass,bx,by,bz,bvx,bvy,bvz);
322 for(
unsigned int bod=1;bod<ens.
nbod();++bod)
325 double mass = ens.mass(systemid,bod);
327 switch(COORDINATE_SYSTEM)
331 mass_effective += mass;
335 mass_effective = star_mass + mass;
339 mass_effective = star_mass + mass;
342 double x = ens.x(systemid,bod)-bx;
343 double y = ens.y(systemid,bod)-by;
344 double z = ens.z(systemid,bod)-bz;
345 double vx = ens.vx(systemid,bod)-bvx;
346 double vy = ens.vy(systemid,bod)-bvy;
347 double vz = ens.vz(systemid,bod)-bvz;
349 double a, e, i, O, w, M;
350 calc_keplerian_for_cartesian(a,e,i,O,w,M, x,y,z,vx,vy,vz, mass_effective);
356 os << ens[systemid].id() <<
" " << bod <<
" " << mass <<
" " << a <<
" " << e <<
" " << i <<
" " << O <<
" " << w <<
" " << M <<
"\n";
359 os.precision(cout_precision_old);
364 void print_selected_systems(
swarm::ensemble& ens, std::vector<unsigned int> systemindices, std::ostream &os = std::cout)
366 for(
unsigned int i=0; i<systemindices.size(); ++i)
367 print_system(ens,systemindices[i], os);
370 void print_selected_systems_for_demo(
swarm::ensemble& ens,
unsigned int nprint, std::ostream &os = std::cout)
372 if(nprint>ens.
nsys()) nprint = ens.
nsys();
373 for(
unsigned int systemid = 0; systemid< nprint; ++systemid)
374 print_system(ens,systemid,os);
383 std::vector<unsigned int> stable_system_indices, unstable_system_indices;
384 for(
int i = 0; i < ens.
nsys() ; i++ )
386 if(ens[i].is_disabled())
387 unstable_system_indices.push_back(i);
389 stable_system_indices.push_back(i);
393 if(cfg.count(
"stable_init_output"))
395 ofstream stable_init_output( cfg.
optional(
"stable_init_output",
string(
"stable_init_output.txt")).c_str() );
396 print_selected_systems(ens_init,stable_system_indices, stable_init_output);
399 if(cfg.count(
"stable_final_output"))
401 ofstream stable_final_output( cfg.
optional(
"stable_final_output",
string(
"stable_final_output.txt")).c_str() );
402 print_selected_systems(ens,stable_system_indices, stable_final_output);
405 if(cfg.count(
"unstable_init_output"))
407 ofstream unstable_init_output( cfg.
optional(
"unstable_init_output",
string(
"unstable_init_output.txt")).c_str() );
408 print_selected_systems(ens_init,unstable_system_indices, unstable_init_output);
411 if(cfg.count(
"unstable_final_output"))
413 ofstream unstable_final_output( cfg.
optional(
"unstable_final_output",
string(
"unstable_final_output.txt")).c_str() );
414 print_selected_systems(ens,unstable_system_indices, unstable_final_output);
421 std::vector<std::vector<double> > semimajor_axes(ens.
nsys(),std::vector<double>(ens.
nbod(),0.));
423 for(
int sys_idx = 0; sys_idx < ens.
nsys() ; sys_idx++)
425 int sys_id = ens[sys_idx].id();
427 assert(sys_id<ens.
nsys());
428 double star_mass = ens.mass(sys_idx,0);
429 double mass_effective = star_mass;
430 double bx, by, bz, bvx, bvy, bvz;
432 for(
unsigned int bod=1;bod<ens.
nbod();++bod)
434 double mass = ens.mass(sys_idx,bod);
435 mass_effective += mass;
437 double x = ens.x(sys_idx,bod)-bx;
438 double y = ens.y(sys_idx,bod)-by;
439 double z = ens.z(sys_idx,bod)-bz;
440 double vx = ens.vx(sys_idx,bod)-bvx;
441 double vy = ens.vy(sys_idx,bod)-bvy;
442 double vz = ens.vz(sys_idx,bod)-bvz;
443 double a, e, i, O, w, M;
444 calc_keplerian_for_cartesian(a,e,i,O,w,M, x,y,z,vx,vy,vz, mass_effective);
445 semimajor_axes[sys_id][bod-1] = a;
448 return semimajor_axes;
451 void disable_unstable_systems(
defaultEnsemble& ens,
const std::vector<std::vector<double> >& semimajor_axes_init,
const double deltaa_threshold )
453 for(
int sys_idx = 0; sys_idx < ens.
nsys() ; sys_idx++)
455 if(ens[sys_idx].is_disabled() )
continue;
456 bool disable =
false;
457 int sys_id = ens[sys_idx].id();
458 double star_mass = ens.mass(sys_idx,0);
459 double mass_effective = star_mass;
460 double bx, by, bz, bvx, bvy, bvz;
462 for(
unsigned int bod=1;bod<ens.
nbod();++bod)
466 double mass = ens.mass(sys_idx,bod);
467 mass_effective += mass;
469 double x = ens.x(sys_idx,bod)-bx;
470 double y = ens.y(sys_idx,bod)-by;
471 double z = ens.z(sys_idx,bod)-bz;
472 double vx = ens.vx(sys_idx,bod)-bvx;
473 double vy = ens.vy(sys_idx,bod)-bvy;
474 double vz = ens.vz(sys_idx,bod)-bvz;
475 double a, e, i, O, w, M;
476 calc_keplerian_for_cartesian(a,e,i,O,w,M, x,y,z,vx,vy,vz, mass_effective);
482 if(!((e>=0.)&&(e<1.0))) { disable =
true; }
483 if(!((a>0.)&&(a<10.0))) { disable =
true; }
485 assert(sys_id<semimajor_axes_init.size());
487 assert(bod-1<semimajor_axes_init[sys_id].size());
488 double da = a - semimajor_axes_init[sys_id][bod-1];
489 if(fabs(da) >= deltaa_threshold * semimajor_axes_init[sys_id][bod-1])
494 if(cfg.count(
"verbose"))
495 std::cout <<
"# Disabling idx=" << sys_idx <<
" id=" << sys_id <<
" b=" << bod <<
" ainit= " << semimajor_axes_init[sys_id][bod-1] <<
" a= " << a <<
" e= " << e <<
" i= " << i <<
" Omega= " << O <<
" omega= " << w <<
" M= " << M <<
"\n";
499 if(disable) ens[sys_idx].set_disabled();
508 const double critical_ratio = 0.75;
511 double ratio = double(count_disabled) / double( ens.
nsys() );
513 return ratio > critical_ratio;
535 int nsys = ens.
nsys();
538 if(active_nsys==0)
return ens;
539 int nbod = ens.
nbod();
544 for(
int i = 0, j = 0; (i < nsys) && (j < active_nsys); i++)
546 if( !ens[i].is_disabled() )
548 ens[i].
copyTo( active_ens[j] );
560 for(
int i = 0; i < ens.
nsys() ; i++)
562 if(ens[i].is_inactive())
567 volatile bool integration_loop_not_aborted_yet =
true;
576 fprintf(stderr,
"# Break requested... Finishing the current GPU kernel call and will then save the results before exitting.\n");
577 integration_loop_not_aborted_yet =
false;
585 int main(
int argc,
char* argv[] )
592 if(argc < 3) cout <<
"Usage: montecarlo_mcmc_outputs <integration configuration> <initial conditions configuration>" << endl;
595 string integ_configfile = argv[1];
598 string initc_configfile = argv[2];
608 if( cfg.count(
"input") )
610 else if(cfg.count(
"input_mcmc_keplerian") )
612 else if(cfg.count(
"input_mcmc_cartesian") )
616 std::cerr <<
"# Must specify one of input [for binary snapshot], input_mcmc_keplerian or input_mcmc_cartesian.\n";
621 if(cfg.count(
"initial_snapshot"))
629 double destination_time = cfg.
optional(
"destination_time", 1.0E6);
634 integ->set_ensemble(ens);
635 integ->set_destination_time ( destination_time );
640 int max_kernel_calls_per_integrate = cfg.
optional(
"max_kernel_calls_per_integrate",1);
642 int max_itterations_per_kernel_call = cfg.
optional(
"max_itterations_per_kernel_call",20000);
643 integ->set_max_attempts( max_kernel_calls_per_integrate );
644 integ->set_max_iterations ( max_itterations_per_kernel_call );
656 reactivate_systems(ens);
662 int num_integrate_calls = 0;
671 ++num_integrate_calls;
675 const double deltaa_frac_threshold = cfg.
optional(
"deltaa_frac_threshold", 0.5);
676 disable_unstable_systems( ens, semimajor_axes_init, deltaa_frac_threshold );
680 std::cerr << active_ones <<
" max|dE/E|= " << max_deltaE <<
"\n";
683 if(num_integrate_calls%10==0)
692 if ( needs_shrinking( ens ) )
709 integ->set_ensemble( ens );
737 #if ACCESS_HOST_ARRAY_OF_TRANSIT_TIMES
739 static const int max_num_doubles_per_event = 2;
740 typedef swarm::event_record<max_num_doubles_per_event> event_record_type;
741 typedef std::vector<event_record_type> event_log_one_system_type;
742 typedef std::vector<event_log_one_system_type> event_log_one_code_type;
743 std::vector<event_log_one_code_type> event_log;
746 std::vector<std::vector<std::vector<double> > > transit_times_model(ens_init.
nsys(), std::vector<std::vector<double> >(ens_init.
nbod()));
747 std::vector<std::vector<std::vector<double> > > transit_durations_model(ens_init.
nsys(), std::vector<std::vector<double> >(ens_init.
nbod()));
751 for(
int e=0;e<data[
sysid].size();++e)
753 event_record_type er = data[
sysid][e];
754 int bodid = er.bodid1;
755 double time = er.time;
756 double b = er.data[0];
757 double vproj = er.data[1];
758 transit_times_model[
sysid][bodid].push_back(time);
759 transit_durations_model[
sysid][bodid].push_back(sqrt(1.-b*b)/vproj);
763 std::cout.precision(10);
767 for(
int bodid=0;bodid<transit_times_model[
sysid].size();++bodid)
769 for(
int trid=0;trid<transit_times_model[
sysid][bodid].size();++trid)
771 std::cout <<
"s= " <<
sysid <<
" b= " << bodid <<
" n= " << trid <<
" t= " << transit_times_model[
sysid][bodid][trid] <<
" D= " << transit_durations_model[
sysid][bodid][trid] <<
" \n";