Skip to content

Commit

Permalink
More cracefull interrupt handling.
Browse files Browse the repository at this point in the history
Stop integration after the next timestep on the first time CTRL-C is pressed.
If CTRL-C pressed twice, then exit long loops (gravity, collisions)
immediately. This might make it hard to continue an integration.
  • Loading branch information
hannorein committed Dec 22, 2024
1 parent eaa3d4a commit c653292
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/collision.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void reb_collision_search(struct reb_simulation* const r){
// Loop over all particles
for (int i=0;i<N;i++){
#ifndef OPENMP
if (reb_sigint) return;
if (reb_sigint > 1) return;
#endif // OPENMP
int ip = i;
if (mercurius_map){
Expand Down Expand Up @@ -176,7 +176,7 @@ void reb_collision_search(struct reb_simulation* const r){
// Loop over all particles
for (int i=0;i<N;i++){
#ifndef OPENMP
if (reb_sigint) return;
if (reb_sigint > 1) return;
#endif // OPENMP
struct reb_particle p1 = particles[i];
struct reb_vec6d gborig = reb_boundary_get_ghostbox(r, gbx,gby,gbz);
Expand Down Expand Up @@ -260,7 +260,7 @@ void reb_collision_search(struct reb_simulation* const r){
#pragma omp parallel for schedule(guided)
for (int i=0;i<N;i++){
#ifndef OPENMP
if (reb_sigint) return;
if (reb_sigint > 1) return;
#endif // OPENMP
struct reb_particle p1 = particles[i];
struct reb_collision collision_nearest;
Expand Down Expand Up @@ -319,7 +319,7 @@ void reb_collision_search(struct reb_simulation* const r){
#pragma omp parallel for schedule(guided)
for (int i=0;i<N;i++){
#ifndef OPENMP
if (reb_sigint) return;
if (reb_sigint > 1) return;
#endif // OPENMP
struct reb_particle p1 = particles[i];
struct reb_collision collision_nearest;
Expand Down
22 changes: 11 additions & 11 deletions src/gravity.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void reb_calculate_acceleration(struct reb_simulation* r){
// All active particle pairs
#ifndef OPENMP // OPENMP off, do O(1/2*N^2)
for (int i=starti; i<_N_active; i++){
if (reb_sigint) return;
if (reb_sigint > 1) return;
for (int j=startj; j<i; j++){
const double dx = (gb.x+particles[i].x) - particles[j].x;
const double dy = (gb.y+particles[i].y) - particles[j].y;
Expand Down Expand Up @@ -200,7 +200,7 @@ void reb_calculate_acceleration(struct reb_simulation* r){
#ifndef OPENMP // OPENMP off
const int startitestp = MAX(_N_active, starti);
for (int i=startitestp; i<_N_real; i++){
if (reb_sigint) return;
if (reb_sigint > 1) return;
for (int j=startj; j<_N_active; j++){
const double dx = (gb.x+particles[i].x) - particles[j].x;
const double dy = (gb.y+particles[i].y) - particles[j].y;
Expand Down Expand Up @@ -371,7 +371,7 @@ void reb_calculate_acceleration(struct reb_simulation* r){
}
#else // OPENMP
for (int i=0; i<_N_active; i++){
if (reb_sigint) return;
if (reb_sigint > 1) return;
for (int j=i+1; j<_N_active; j++){
if (_gravity_ignore_terms==1 && ((j==1 && i==0) || (i==1 && j==0))) continue;
if (_gravity_ignore_terms==2 && ((j==0 || i==0))) continue;
Expand Down Expand Up @@ -428,7 +428,7 @@ void reb_calculate_acceleration(struct reb_simulation* r){

// Testparticles
for (int i=_N_active; i<_N_real; i++){
if (reb_sigint) return;
if (reb_sigint > 1) return;
for (int j=0; j<_N_active; j++){
if (_gravity_ignore_terms==1 && ((j==1 && i==0) || (i==1 && j==0))) continue;
if (_gravity_ignore_terms==2 && ((j==0 || i==0))) continue;
Expand Down Expand Up @@ -502,7 +502,7 @@ void reb_calculate_acceleration(struct reb_simulation* r){
#pragma omp parallel for schedule(guided)
for (int i=0; i<N; i++){
#ifndef OPENMP
if (reb_sigint) return;
if (reb_sigint > 1) return;
#endif // OPENMP
struct reb_vec6d gb = reb_boundary_get_ghostbox(r, gbx,gby,gbz);
// Precalculated shifted position
Expand Down Expand Up @@ -530,7 +530,7 @@ void reb_calculate_acceleration(struct reb_simulation* r){
particles[i].az = 0;
}
for (int i=2; i<_N_active; i++){
if (reb_sigint) return;
if (reb_sigint > 1) return;
for (int j=1; j<i; j++){
const double dx = particles[i].x - particles[j].x;
const double dy = particles[i].y - particles[j].y;
Expand All @@ -551,7 +551,7 @@ void reb_calculate_acceleration(struct reb_simulation* r){
}
const int startitestp = MAX(_N_active,2);
for (int i=startitestp; i<_N_real; i++){
if (reb_sigint) return;
if (reb_sigint > 1) return;
for (int j=1; j<_N_active; j++){
const double dx = particles[i].x - particles[j].x;
const double dy = particles[i].y - particles[j].y;
Expand Down Expand Up @@ -764,7 +764,7 @@ void reb_calculate_acceleration(struct reb_simulation* r){
particles[i].az = 0;
}
for (int i=2; i<_N_active; i++){
if (reb_sigint) return;
if (reb_sigint > 1) return;
for (int j=1; j<i; j++){
if (r->ri_trace.current_Ks[j*N+i]) continue;
const double dx = particles[i].x - particles[j].x;
Expand All @@ -784,7 +784,7 @@ void reb_calculate_acceleration(struct reb_simulation* r){
}
const int startitestp = MAX(_N_active,2);
for (int i=startitestp; i<_N_real; i++){
if (reb_sigint) return;
if (reb_sigint > 1) return;
for (int j=1; j<_N_active; j++){
if (r->ri_trace.current_Ks[j*N+i]) continue;
const double dx = particles[i].x - particles[j].x;
Expand Down Expand Up @@ -1351,7 +1351,7 @@ void reb_calculate_and_apply_jerk(struct reb_simulation* r, const double v){
#pragma omp parallel for
for (int i=starti; i<_N_active; i++){
#ifndef OPENMP
if (reb_sigint) return;
if (reb_sigint > 1) return;
#endif // OPENMP
for (int j=startj; j<i; j++){
const double dx = particles[i].x - particles[j].x;
Expand Down Expand Up @@ -1382,7 +1382,7 @@ void reb_calculate_and_apply_jerk(struct reb_simulation* r, const double v){
#pragma omp parallel for
for (int i=_N_active; i<_N_real; i++){
#ifndef OPENMP
if (reb_sigint) return;
if (reb_sigint > 1) return;
#endif // OPENMP
for (int j=startj; j<i; j++){
const double dx = particles[i].x - particles[j].x;
Expand Down
2 changes: 1 addition & 1 deletion src/integrator.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void reb_integrator_part2(struct reb_simulation* r){
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){
if (reb_sigint > 1){
r->status = REB_STATUS_SIGINT;
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ int reb_simulation_output_screenshot(struct reb_simulation* r, const char* filen
// Wait until screenshot arrives
while (!r->server_data->screenshot && r->status <0){
usleep(100);
if (reb_sigint== 1){
if (reb_sigint > 1){
r->status = REB_STATUS_SIGINT;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/rebound.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ int reb_check_exit(struct reb_simulation* const r, const double tmax, double* la
#else
usleep(1000);
#endif
if (reb_sigint== 1){ // cancel while paused
if (reb_sigint){ // cancel while paused
r->status = REB_STATUS_SIGINT;
}
}
Expand Down Expand Up @@ -786,7 +786,7 @@ volatile sig_atomic_t reb_sigint;
void reb_sigint_handler(int signum) {
// Handles graceful shutdown for interrupts
if (signum == SIGINT){
reb_sigint = 1;
reb_sigint += 1;
}
}

Expand Down Expand Up @@ -860,7 +860,7 @@ static void* reb_simulation_integrate_raw(void* args){
if (r->simulationarchive_filename){ reb_simulationarchive_heartbeat(r);}
reb_simulation_step(r);
reb_run_heartbeat(r);
if (reb_sigint== 1){
if (reb_sigint){
r->status = REB_STATUS_SIGINT;
}
#ifdef OPENGL
Expand Down

0 comments on commit c653292

Please sign in to comment.