#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rebound.h"
#include "gravity.h"
#include "tree.h"
#include "boundary.h"
#include "output.h"
#include "integrator.h"
#include "integrator_whfast.h"
#include "integrator_whfast512.h"
#include "integrator_saba.h"
#include "integrator_ias15.h"
#include "integrator_mercurius.h"
#include "integrator_trace.h"
#include "integrator_leapfrog.h"
#include "integrator_sei.h"
#include "integrator_janus.h"
#include "integrator_eos.h"
#include "integrator_bs.h"
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) > (b) ? (b) : (a))
void reb_integrator_step(struct reb_simulation* r){
switch(r->integrator){
case REB_INTEGRATOR_IAS15:
reb_integrator_ias15_step(r);
break;
case REB_INTEGRATOR_LEAPFROG:
reb_integrator_leapfrog_step(r);
break;
case REB_INTEGRATOR_SEI:
reb_integrator_sei_step(r);
break;
case REB_INTEGRATOR_WHFAST:
reb_integrator_whfast_step(r);
break;
case REB_INTEGRATOR_WHFAST512:
reb_integrator_whfast512_step(r);
break;
case REB_INTEGRATOR_SABA:
reb_integrator_saba_step(r);
break;
case REB_INTEGRATOR_MERCURIUS:
reb_integrator_mercurius_step(r);
break;
case REB_INTEGRATOR_JANUS:
reb_integrator_janus_step(r);
break;
case REB_INTEGRATOR_EOS:
reb_integrator_eos_step(r);
break;
case REB_INTEGRATOR_BS:
reb_integrator_bs_step(r);
break;
case REB_INTEGRATOR_TRACE:
reb_integrator_trace_step(r);
break;
case REB_INTEGRATOR_NONE:
r->t += r->dt;
r->dt_last_done = r->dt;
break;
default:
break;
}
if (r->integrator != REB_INTEGRATOR_BS && r->N_odes){
if (r->ode_warnings==0 && (!r->ri_whfast.safe_mode || !r->ri_saba.safe_mode || !r->ri_eos.safe_mode || !r->ri_mercurius.safe_mode)){
reb_simulation_warning(r, "Safe mode should be enabled when custom ODEs are being used.");
r->ode_warnings = 1;
}
double dt = r->dt_last_done;
double t = r->t - r->dt_last_done; double forward = (dt>0.) ? 1. : -1.;
r->ri_bs.first_or_last_step = 1;
while(t*forward < r->t*forward && fabs((r->t - t)/(fabs(r->t)+1e-16))>1e-15){
if (reb_sigint > 1){
r->status = REB_STATUS_SIGINT;
return;
}
if (r->ri_bs.dt_proposed !=0.){
double max_dt = fabs(r->t - t);
dt = fabs(r->ri_bs.dt_proposed);
if (dt > max_dt){ dt = max_dt;
r->ri_bs.first_or_last_step = 1;
}
dt *= forward;
}
int success = reb_integrator_bs_step_odes(r, dt);
if (success){
t += dt;
}
}
}
}
void reb_simulation_synchronize(struct reb_simulation* r){
switch(r->integrator){
case REB_INTEGRATOR_IAS15:
reb_integrator_ias15_synchronize(r);
break;
case REB_INTEGRATOR_LEAPFROG:
reb_integrator_leapfrog_synchronize(r);
break;
case REB_INTEGRATOR_SEI:
reb_integrator_sei_synchronize(r);
break;
case REB_INTEGRATOR_WHFAST:
reb_integrator_whfast_synchronize(r);
break;
case REB_INTEGRATOR_WHFAST512:
reb_integrator_whfast512_synchronize(r);
break;
case REB_INTEGRATOR_SABA:
reb_integrator_saba_synchronize(r);
break;
case REB_INTEGRATOR_MERCURIUS:
reb_integrator_mercurius_synchronize(r);
break;
case REB_INTEGRATOR_JANUS:
reb_integrator_janus_synchronize(r);
break;
case REB_INTEGRATOR_EOS:
reb_integrator_eos_synchronize(r);
break;
case REB_INTEGRATOR_BS:
reb_integrator_bs_synchronize(r);
break;
case REB_INTEGRATOR_TRACE:
reb_integrator_trace_synchronize(r);
break;
default:
break;
}
}
void reb_simulation_reset_integrator(struct reb_simulation* r){
r->integrator = REB_INTEGRATOR_IAS15;
r->gravity = REB_GRAVITY_BASIC; r->gravity_ignore_terms = 0;
reb_integrator_ias15_reset(r);
reb_integrator_mercurius_reset(r);
reb_integrator_sei_reset(r);
reb_integrator_whfast_reset(r);
reb_integrator_whfast512_reset(r);
reb_integrator_saba_reset(r);
reb_integrator_janus_reset(r);
reb_integrator_eos_reset(r);
reb_integrator_bs_reset(r);
reb_integrator_trace_reset(r);
reb_integrator_leapfrog_reset(r);
}
void reb_simulation_update_acceleration(struct reb_simulation* r){
if (r->tree_needs_update || r->gravity==REB_GRAVITY_TREE || r->collision==REB_COLLISION_TREE || r->collision==REB_COLLISION_LINETREE){
reb_boundary_check(r);
reb_simulation_update_tree(r);
}
#ifdef MPI
reb_communication_mpi_distribute_particles(r);
#endif
if (r->tree_root!=NULL && r->gravity==REB_GRAVITY_TREE){
reb_simulation_update_tree_gravity_data(r);
#ifdef MPI
reb_tree_prepare_essential_tree_for_gravity(r);
reb_communication_mpi_distribute_essential_tree_for_gravity(r);
#endif }
reb_simulation_update_acceleration_gravity(r);
if (r->N_var){
reb_simulation_update_acceleration_gravity_var(r);
}
if (r->additional_forces && (r->integrator != REB_INTEGRATOR_MERCURIUS || r->ri_mercurius.mode==0) && (r->integrator != REB_INTEGRATOR_TRACE || r->ri_trace.mode==REB_TRACE_MODE_INTERACTION || r->ri_trace.mode==REB_TRACE_MODE_FULL)){
if (r->integrator==REB_INTEGRATOR_MERCURIUS){
if(r->N>r->ri_mercurius.N_allocated_additional_forces){
r->ri_mercurius.particles_backup_additional_forces = realloc(r->ri_mercurius.particles_backup_additional_forces, r->N*sizeof(struct reb_particle));
r->ri_mercurius.N_allocated_additional_forces = r->N;
}
memcpy(r->ri_mercurius.particles_backup_additional_forces,r->particles,r->N*sizeof(struct reb_particle));
reb_integrator_mercurius_dh_to_inertial(r);
}
if (r->integrator==REB_INTEGRATOR_TRACE){
if(r->N>r->ri_trace.N_allocated_additional_forces){
r->ri_trace.particles_backup_additional_forces = realloc(r->ri_trace.particles_backup_additional_forces, r->N*sizeof(struct reb_particle));
r->ri_trace.N_allocated_additional_forces = r->N;
}
memcpy(r->ri_trace.particles_backup_additional_forces,r->particles,r->N*sizeof(struct reb_particle));
reb_integrator_trace_dh_to_inertial(r);
}
r->additional_forces(r);
if (r->integrator==REB_INTEGRATOR_MERCURIUS){
struct reb_particle* restrict const particles = r->particles;
struct reb_particle* restrict const backup = r->ri_mercurius.particles_backup_additional_forces;
for (unsigned int i=0;i<r->N;i++){
particles[i].x = backup[i].x;
particles[i].y = backup[i].y;
particles[i].z = backup[i].z;
particles[i].vx = backup[i].vx;
particles[i].vy = backup[i].vy;
particles[i].vz = backup[i].vz;
}
}
if (r->integrator==REB_INTEGRATOR_TRACE){
struct reb_particle* restrict const particles = r->particles;
struct reb_particle* restrict const backup = r->ri_trace.particles_backup_additional_forces;
for (unsigned int i=0;i<r->N;i++){
particles[i].x = backup[i].x;
particles[i].y = backup[i].y;
particles[i].z = backup[i].z;
particles[i].vx = backup[i].vx;
particles[i].vy = backup[i].vy;
particles[i].vz = backup[i].vz;
}
}
}
}