ode-solvers icon indicating copy to clipboard operation
ode-solvers copied to clipboard

Why does the `system` method take an unmutable reference to `self`?

Open thdhondt opened this issue 3 years ago • 3 comments

Hi @srenevey ,

First of all, thanks a lot for the work you have put into this crate! It has been really helpful for getting started with numerical simulation using Rust.

I was wondering if there was a specific reason why the system method in the System trait took an unmutable reference to self:

/// Trait needed to be implemented by the user.
pub trait System<V> {
    fn system(&self, x: f64, y: &V, dy: &mut V);
}

The system I am currently simulating has a small controller built-in. It is a simple state machine that transitions based on the values inside the State vector. Unfortunately, I am unable to store this data inside my object implementing the System trait, as I can't modify it via the &self reference.

I am currently a beginner in Rust, so I'm mostly interested in understanding if this was an explicit design decision or just a use case that was not yet covered :).

Thanks in advance for your assistance.

Thomas

thdhondt avatar Jan 04 '23 13:01 thdhondt

Hi Thomas,

Thanks for reaching out, it's always great to hear from people using the crate!

As for your question, there's no specific reason why an immutable reference is used as opposed to a mutable one. I didn't encounter your use case when I was developing the crate for my projects and hence didn't cover it. What you can try is fork the crate and modify the signature of the method to accept a mutable reference.

Best,

srenevey avatar Jan 10 '23 02:01 srenevey

This is what I did (https://github.com/thdhondt/ode-solvers/tree/Solout-for-other-solvers-and-allocation-reduction) and it works perfectly.

I did start from another fork, however, as I also needed the solout method for the Rk4 solver.

thdhondt avatar Jan 10 '23 11:01 thdhondt

Hello @thdhondt, thanks for your solution. It helps me for my system. It has a couple of equations those dont not figured as y' = f(x) form. But it changes over time. It's be wonderful if this fork will be merged as soon as possible.

MaygurovMV avatar Jan 13 '23 14:01 MaygurovMV