Here is the code for the function that writes diagnostics to the standard error stream. Note the declarations of arguments: all arrays are specified to be const, which is appropriate since their values should only be reported, without changing them. The argument einit is passed by reference, since it will hold the initial value of the total energy of the system, information that should be passed back to the calling function. The other arguments are all passed by value.
The only calculation performed in this function is that of the kinetic energy. The potential energy is determined in the function get_acc_jerk_pot_coll(). The init_flag is set to true when write_diagnostics() is evoked for the first time, at t = 0. In that case, we want to pass the value of the initial total energy back to the calling function evolve(), which can use that information to compare it with later measured values of the total energy, in order to determine the absolute and relative amounts of energy drifts, which are a good measure of numerical accuracy.
Note that we could have defined the initial energy einit as a
static variable inside write_diagnostics(). For our present
purpose that would be fine, but this type of programming may easily
create a future limitation. If some day we would like to compare two
different -body systems, each of which evolves, we would get into a
conflict if both of them would try to access the same static variable.
Therefore, for the same reason we don't use global variables in the
first place, we prefer to pass einit as a function variable.