34 struct reb_simulation *rsim;
35 struct reb_collision coll;
37 int value=0, pi, pj, pk=0;
38 struct reb_particle*
const particles = rsim->particles;
42 printf (
"\n Merger detected. Merging planets %d and %d\n", particles[pi].hash, particles[pj].hash);
43 masterprint (
"Previous no. of planets: %d\n", rsim->N_active-1);
44 fprintf (
mergers,
"%.12g\t%d\t%d\n", rsim->t, pi, pj);
45 fprintf (
mergers,
"%d\t%f\t%.8g\n", particles[pi].hash, particles[pi].m, particles[pi].r);
46 fprintf (
mergers,
"%#.18g\t%#.18g\t%#.18g\n", particles[pi].x, particles[pi].y, particles[pi].z);
47 fprintf (
mergers,
"%#.18g\t%#.18g\t%#.18g\n", particles[pi].vx, particles[pi].vy, particles[pi].vz);
48 fprintf (
mergers,
"%d\t%f\t%.8g\n", particles[pj].hash, particles[pj].m, particles[pj].r);
49 fprintf (
mergers,
"%#.18g\t%#.18g\t%#.18g\n", particles[pj].x, particles[pj].y, particles[pj].z);
50 fprintf (
mergers,
"%#.18g\t%#.18g\t%#.18g\n", particles[pj].vx, particles[pj].vy, particles[pj].vz);
52 value = reb_collision_resolve_merge (rsim, coll);
63 fprintf (
mergers,
"%d\t%f\t%.8g\n", particles[pk].hash, particles[pk].m, particles[pk].r);
64 fprintf (
mergers,
"%#.18g\t%#.18g\t%#.18g\n", particles[pk].x, particles[pk].y, particles[pk].z);
65 fprintf (
mergers,
"%#.18g\t%#.18g\t%#.18g\n", particles[pk].vx, particles[pk].vy, particles[pk].vz);
66 masterprint (
"No. of planets after the merger: %d\n", rsim->N_active-1);
76 struct reb_simulation *rsim;
82 boolean const keepsorted=
YES;
83 struct reb_particle*
const particles = rsim->particles;
85 Nact = rsim->N_active;
86 for (i=1; i<Nact; i++) {
90 if (r2 < RMIN*RMIN || r2 >
RMAX*
RMAX) {
91 masterprint (
"\nPlanet %d discarded. Reason: it is not in the (RMIN, RMAX) domain.\n", particles[i].hash);
92 masterprint (
"Previous no. of planets: %d\n", rsim->N_active-1);
95 fprintf (
discard,
"%d\t%f\t%#.8g\n", particles[i].hash, particles[i].m, particles[i].r);
96 fprintf (
discard,
"%#.18g\t%#.18g\t%#.18g\n", particles[i].x, particles[i].y, particles[i].z);
97 fprintf (
discard,
"%#.18g\t%#.18g\t%#.18g\n", particles[i].vx, particles[i].vy, particles[i].vz);
99 isremoved = reb_remove (rsim, i, keepsorted);
100 if (isremoved ==
NO) {
101 masterprint (
"\nWarning! Failed to remove planet with id %d and loop index %d\n", particles[i].hash, i);
103 masterprint (
"No. of planets after the discard: %d\n", rsim->N_active-1);
112 struct reb_simulation *rsim;
115 rsim->integrator = REB_INTEGRATOR_IAS15;
119 rsim->collision = REB_COLLISION_DIRECT;
122 rsim->collision = REB_COLLISION_NONE;
132 char s[512], nm[512], filename[256], *s1;
134 float mass,
a, e, inc, Omega, omega, f;
135 real rho, radius, rad;
137 struct reb_simulation *rsim = reb_create_simulation ();
140 rsim->N_active = npl + 1;
142 struct reb_particle primary = {0};
145 reb_add (rsim, primary);
146 input = fopen (plfile,
"r");
148 fprintf (stderr,
"Error : can't find '%s'.\n", filename);
153 while (fgets(s, 510, input) != NULL) {
154 sscanf(s,
"%s ", nm);
158 sscanf(s1 + strspn(s1,
"\t :=>_"),
"%f %f %f %f %f %f %f", \
159 &mass, &a, &e, &inc, &Omega, &omega, &f);
161 struct reb_particle p = reb_tools_orbit_to_particle_err (
G, primary, \
164 masterprint(
"Error: Planet no. %d cannot be initialized \
165 using the input parameters. Terminating now...\n", i);
169 radius = pow(3.0*(
real)mass/4.0/
PI/rho , 1.0/3.0);
172 sys->mass[i-1] = p.m;
185 sprintf (filename,
"%snbody.orbits.dat",
OUTPUTDIR);
187 sprintf (filename,
"%snbody.discard.dat",
OUTPUTDIR);
190 sprintf (filename,
"%snbody.mergers.dat",
OUTPUTDIR);
199 struct reb_simulation *rsim;
204 struct reb_particle com;
205 struct reb_particle*
const particles = rsim->particles;
206 for (i=1;i<rsim->N_active;i++) {
207 particles[i].m = sys->
mass[i-1];
208 particles[i].x = sys->x[i-1];
209 particles[i].y = sys->y[i-1];
210 particles[i].z = sys->z[i-1];
211 particles[i].vx = sys->vx[i-1];
212 particles[i].vy = sys->vy[i-1];
213 particles[i].vz = sys->vz[i-1];
215 com = reb_get_com (rsim);
216 for (i=0;i<rsim->N;i++) {
217 particles[i].x -= com.x;
218 particles[i].y -= com.y;
219 particles[i].z -= com.z;
220 particles[i].vx -= com.vx;
221 particles[i].vy -= com.vy;
222 particles[i].vz -= com.vz;
227 if (rsim->integrator==REB_INTEGRATOR_IAS15) reb_integrator_ias15_clear (rsim);
228 reb_integrate (rsim, ttarget);
229 for (i=1;i<rsim->N;i++) {
230 particles[i].x -= particles[0].x;
231 particles[i].y -= particles[0].y;
232 particles[i].z -= particles[0].z;
233 particles[i].vx -= particles[0].vx;
234 particles[i].vy -= particles[0].vy;
235 particles[i].vz -= particles[0].vz;
237 particles[0].x = 0.0;
238 particles[0].y = 0.0;
239 particles[0].z = 0.0;
240 particles[0].vx = 0.0;
241 particles[0].vy = 0.0;
242 particles[0].vz = 0.0;
252 struct reb_simulation *rsim;
255 boolean lessplanets =
NO;
256 struct reb_particle*
const particles = rsim->particles;
257 if (sys->nb > rsim->N_active-1) {
259 sys->nb = rsim->N_active-1;
261 if (sys->nb < rsim->N_active-1) {
262 printf (
"Error! There are more planets in REBOUND than in FARGO! Terminating now...\n");
265 for (i=1;i<rsim->N_active;i++) {
266 sys->x[i-1] = particles[i].x;
267 sys->y[i-1] = particles[i].y;
268 sys->z[i-1] = particles[i].z;
269 sys->vx[i-1] = particles[i].vx;
270 sys->vy[i-1] = particles[i].vy;
271 sys->vz[i-1] = particles[i].vz;
273 sys->mass[i-1] = particles[i].m;
282 struct reb_simulation *rsim;
285 real dt, dtnew, r2, rv;
286 struct reb_particle*
const particles = rsim->particles;
287 Nact = rsim->N_active;
289 for (i=1; i<Nact; i++) {
290 for (j=i+1; j<Nact; j++) {
291 r2 = (particles[i].x - particles[j].x)*(particles[i].x - particles[j].x) + \
292 (particles[i].y - particles[j].y)*(particles[i].y - particles[j].y) + \
293 (particles[i].z - particles[j].z)*(particles[i].z - particles[j].z);
294 rv = (particles[i].x - particles[j].x)*(particles[i].vx - particles[j].vx) + \
295 (particles[i].y - particles[j].y)*(particles[i].vy - particles[j].vy) + \
296 (particles[i].z - particles[j].z)*(particles[i].vz - particles[j].vz);
310 struct reb_simulation *rsim;
313 struct reb_orbit orbit;
314 struct reb_particle*
const particles = rsim->particles;
316 Nact = rsim->N_active;
317 for (i=1; i<Nact; i++) {
318 orbit = reb_tools_particle_to_orbit (
G, particles[i], particles[0]);
319 fprintf (
plout,
"%d\t%.12g\t%.12g\t%.12g\t%.12g\t%.12g\t%.12g\t%.12g\t%.12g\t%.12g\t%.12g\t%.12g\t%.12g\n", \
320 particles[i].hash,
PhysicalTime, orbit.a, orbit.e, orbit.inc, \
321 orbit.Omega, orbit.omega, orbit.f, particles[i].m, particles[i].r,
322 particles[i].x, particles[i].y, particles[i].z);
330 struct reb_simulation *rsim;
334 printf (
"Writing the N-body simulation file...\n");
335 printf (
"\tNumber of planets: %d\n", rsim->N_active-1);
336 sprintf (filename,
"%snbody.simulation%d.dat",
OUTPUTDIR, nout);
337 reb_output_binary (rsim, filename);
338 printf (
"\t\t...done\n");
349 struct reb_particle *particles;
351 if (
CPU_Master) printf (
"\n\033[1mRestarting N-body part...\033[0m\n");
353 sprintf (filename,
"%snbody.simulation%d.dat",
OUTPUTDIR, nrestart);
354 struct reb_simulation *rsim = reb_create_simulation_from_binary(filename);
356 if (
CPU_Master) printf (
"\033[1mNo problem!\033[0m Function pointers have been reset via the SetupIntegratorParams() function.\n");
357 particles = rsim->particles;
359 sys->nb = rsim->N_active - 1;
360 for (i=1; i<rsim->N_active; i++) {
361 sys->mass[i-1] = particles[i].m;
362 sys->x[i-1] = particles[i].x;
363 sys->y[i-1] = particles[i].y;
364 sys->z[i-1] = particles[i].z;
365 sys->vx[i-1] = particles[i].vx;
366 sys->vy[i-1] = particles[i].vy;
367 sys->vz[i-1] = particles[i].vz;
371 sprintf (filename,
"%snbody.orbits.dat",
OUTPUTDIR);
373 sprintf (filename,
"%snbody.discard.dat",
OUTPUTDIR);
376 sprintf (filename,
"%snbody.mergers.dat",
OUTPUTDIR);
380 printf (
"%d active planets at restart.\n", rsim->N_active - 1);
381 printf (
"\n\033[1m...done\n\n\033[0m");
double real
Definition of the type 'real' used throughout the code.
int ResolveCollisions(struct reb_simulation *rsim, struct reb_collision coll)
If the REBOUND collision search is successful, this function merges the bodies, outputs information a...
struct reb_simulation * RestartReboundSimulation(PlanetarySystem *sys, int nrestart)
Part of the restart process.
FILE * fopenp(char *string, char *mode)
void OutputNbodySimulation(int nout, struct reb_simulation *rsim)
Stores the entire Rebound simulation in a binary file.
void SynchronizeFargoRebound(PlanetarySystem *sys, struct reb_simulation *rsim)
Synchronises the planetary system between the REBOUND integration and the FARGO simulation.
real * mass
Masses of the planets.
Contains all the information about a planetary system at a given instant in time. ...
struct reb_simulation * SetupReboundSimulation(PlanetarySystem *sys, char *plfile)
Initialises a rebound simulation coupled with FARGO.
void MinStepForRebound(struct reb_simulation *rsim)
A simple time step restriction in order not to miss a collision.
void OutputElements(struct reb_simulation *rsim)
Calculates and outputs the orbital elements.
void AdvanceSystemRebound(PlanetarySystem *sys, struct reb_simulation *rsim, real dt)
Performs an integration step of the N-body problem.
void SetupIntegratorParams(struct reb_simulation *rsim)
Fills the rebound simulation structure with parameters inherited from FARGO.
void DiscardParticlesDist(struct reb_simulation *rsim, real dt)
A simple discard routine which looks for planets that were scattered/migrated away from the FARGO gri...
Contains all the include directives requested by the code.
void masterprint(const char *template,...)