Rust println! returning two copies of each line

I have a small toy compartmental model that iteratively solves a system of ode's using a rk4 algorithm from a ode_solvers crate. For some reason when I get printout from each iteration, only for monitoring/testing purposes I get two copies of each line from "println!"

Have I coded something incorrectly or could it possible be a bug in the ode solver? Thx. J.

The code to create the ode solver object is this:

mpl ode_solvers::System<f64, State> for TwoPool {
    fn system(&self, _: Time, y: &State, dy: &mut State) {

and the line further down that outputs the results is this:

	// print the outputs of the model at each integration iteration 
	println!("PoolSizes   A={:.3}, B={:.3}, Tot={:.3}", y[0], y[1], y[2]);

and this is a section of the output I get:

PoolSizes   A=22.231, B=14.926, Tot=37.157
PoolSizes   A=22.231, B=14.926, Tot=37.157
PoolSizes   A=22.288, B=14.956, Tot=37.243
PoolSizes   A=22.288, B=14.956, Tot=37.243
PoolSizes   A=22.344, B=14.986, Tot=37.330
PoolSizes   A=22.344, B=14.986, Tot=37.330
PoolSizes   A=22.401, B=15.015, Tot=37.416
PoolSizes   A=22.401, B=15.015, Tot=37.416
PoolSizes   A=22.457, B=15.045, Tot=37.502
PoolSizes   A=22.457, B=15.045, Tot=37.502
PoolSizes   A=22.513, B=15.075, Tot=37.588

Any thoughts why I'm getting doubles? Thx. J

I don't know ODE so I don't know if getting the same data twice is a bug or just how it works but I am quite sure println! would never print the same line twice if invoked once.

1 Like

Rk4 evaluates the system four times on each integration step, that’s probably what you’re seeing.

I wondered the same but would expect to get four copies? I may have to ask the writer of the ode crate. the Rk4 might be variable ... up to 4 evaluations per iteration if required? It could be finding a suitable solution after the second evaluation in each case? I will try to find out and report back.

No, see the source. If you print out all the parameters (x, y, dy), they should be different on each call as far as I can see.