Skip to content

Repeated outputs using reb_simulation_output_check #864

@gitlinffff

Description

@gitlinffff

Environment
Which version of REBOUND are you using and on what operating system?

  • REBOUND Version: v4.4.10
  • API interface: C
  • Operating System (including version): Linux

Describe the bug
I think the code's output time check function has some bugs.

int reb_simulation_output_check_phase(struct reb_simulation* r, double interval,double phase){
    double shift = r->t+interval*phase;
    if (floor(shift/interval)!=floor((shift-r->dt)/interval)){
        return 1;
    }
    // Output at beginning 
    if (r->t==0){
        return 1;
    }
    return 0;
}

int reb_simulation_output_check(struct reb_simulation* r, double interval){
    return reb_simulation_output_check_phase(r, interval,0);
}

688050     769383.090315   339.803299      339.803299      89.0490
688050     769722.893614   302.195923      302.195923      89.0883
688050     770025.089537   477.721574      477.721574      89.1233
688050     770502.811111   2804.047915     2804.047915     89.1786
688050     770502.811111   1402.023957     1402.023957     89.1786
688050     770502.811111   937.413798      937.413798      89.1786
688050     770502.811111   515.119938      515.119938      89.1786
688050     770502.811111   420.778642      420.778642      89.1786
688050     770502.811111   271.242145      271.242145      89.1786
688050     770502.811111   182.565171      182.565171      89.1786
688050     770685.376282   182.565171      182.565171      89.1997
688050     770867.941453   693.381529      693.381529      89.2208

Here the 2nd column is r->t, 3rd column is r->dt. Sometimes it is keeping rejecting the new state and reducing the dt until it finds an acceptable dt to update the state. But during this process, it produces outputs multiple times but only the last one is valid.

My usage of the output check function is within heartbeat function:

void heartbeat(struct reb_simulation* r){
//----------------track minimum dt--------------------
  if (r->dt < dt_minimum){
    dt_minimum = r->dt;
  }

//----------------output dt history-------------------
  if(reb_simulation_output_check(r, 60.0)){
    int N_tot = r->N;

    // Open file in append mode
    char filename[20] = "dt_history.csv";
    FILE* f_dt = fopen(filename, "a");
    if (f_dt == NULL) {
      char error_msg[50];
      sprintf(error_msg, "Could not open file: %s", filename);
      reb_simulation_error(r, error_msg);
      return;
    }

    // If file is empty, print header
    static int header_written = 0;
    if (!header_written){
      fprintf(f_dt, "N_tot, t, dt, dt_minimum, t/tmax%%\n");
      header_written = 1;
    }

    // Write values
    fprintf(f_dt, "%-10d %-15.6f %-15.6f %-15.6f %-8.4f\n", N_tot, r->t, r->dt, dt_minimum, r->t/tmax*100.0);

    fclose(f_dt);

    // reset dt_minimum
    dt_minimum = 1.e15;
  }

To Reproduce
See above

Additional context
None
Name/Affiliation
Maverick Li
University of Michigan

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions