srenevey / ode-solvers

Numerical methods to solve ordinary differential equations in Rust.
BSD 3-Clause "New" or "Revised" License
64 stars 26 forks source link

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

Open thdhondt opened 1 year ago

thdhondt commented 1 year ago

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

srenevey commented 1 year ago

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,

thdhondt commented 1 year ago

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.

MaygurovMV commented 1 year ago

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.