Skip to content

Draft: ImplicitDiscreteSolve #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 32 commits into from
Closed

Conversation

@vyudu vyudu marked this pull request as draft February 20, 2025 01:38
@vyudu
Copy link
Member Author

vyudu commented Feb 20, 2025

@ChrisRackauckas This is barebones but working. Any important features I'm missing here? Also should this actually go in OrdinaryDiffEq?

@ChrisRackauckas
Copy link
Member

Can this to use SimpleNonlinearSolve.jl's Newton and make this be SimpleImplicitDiscreteSolve.jl? I think the complete version will want to use the Jacobian reuse aspect and such, so for now having a static version would be a good way to handle this.

@vyudu
Copy link
Member Author

vyudu commented Feb 26, 2025

Did that refactor, just needs more tests now I think

@vyudu
Copy link
Member Author

vyudu commented Mar 13, 2025

@ChrisRackauckas I think this is basically ready, initialization for problems without initializationproblems needs work but I think it's fine for the MTK purposes. How do I register it?

@vyudu vyudu marked this pull request as ready for review March 13, 2025 01:44
@vyudu
Copy link
Member Author

vyudu commented Mar 14, 2025

Fixed these

@ChrisRackauckas
Copy link
Member

Okay so this implementation finds itself in a somewhat odd spot. It's not a "Simple" alg because it's not statically compile-able, which is a property that all others have. It would need to avoid the OrdinaryDiffEqCore part. But it's not a fully complete version either? But it's a rough start towards it. I guess seeing it at this stage, how about moving this to OrdinaryDiffEq and making this be ImplicitDiscreteSolve.jl, and we can use this as a base to do the required optimizations to make the full version. We can do the SimpleImplicitDiscreteSolve.jl as a somewhat separate version for static support.

@vyudu
Copy link
Member Author

vyudu commented Mar 14, 2025

I see, didn't realize I couldn't use OrdinaryDiffEqCore, will move. I'll keep this open too to keep iterating it toward a SimpleImplicitDiscreteSolve.

@ChrisRackauckas
Copy link
Member

See for example https://github.com/SciML/SimpleDiffEq.jl/blob/master/src/rk4/looprk4.jl which is the most basic RK4 implementation. For IDS, it would just be a loop over nonlinear solve calls. With SimpleNonlinearSolve being similar, the whole thing would statically compile since no mutable structs would be in the middle.

@vyudu
Copy link
Member Author

vyudu commented Mar 24, 2025

@ChrisRackauckas tried to refactor this to a simple solve

for i in 2:length(ts)
uprev = u
t = ts[i]
nlf = isinplace(f) ? (out, u, p) -> f(out, u, uprev, p, t) : (u, p) -> f(u, uprev, p, t)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the compiler always smart enough to optimize this closure? It probably is these days. I would've just made a callable type, but I think this got better. I guess we can just patch this if we find a case where it does allocate.

@ChrisRackauckas
Copy link
Member

This folder will need a license file in order to register, but it should be good to go now.

@ChrisRackauckas
Copy link
Member

Alright I think this is now done, fingers crossed.

But, if ImplicitDiscreteSolve is in OrdinaryDiffEq.jl, it may make sense to move this there before registering. But let's just make sure tests pass and moving code around is then simple.

Comment on lines 52 to 61
if save_everystep && save_start
us = Vector{typeof(u0)}(undef, length(ts))
us[1] = u0
elseif save_everystep
us = Vector{typeof(u0)}(undef, length(ts) - 1)
elseif save_start
us = Vector{typeof(u0)}(undef, 2)
us[1] = u0
else
us = Vector{typeof(u0)}(undef, 1) # for interface compatibility
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to make this possibly be an SArray to skip the allocation when possible. This is done for example in https://github.com/SciML/SimpleDiffEq.jl/blob/master/src/rk4/gpurk4.jl#L40

@ChrisRackauckas
Copy link
Member

Alright it looks like it's passing. Let's move this version over and register.

@vyudu
Copy link
Member Author

vyudu commented Apr 21, 2025

Oops I just added this staticarrays thing

Comment on lines +53 to +56
l = save_everystep ? length(ts) - 1 : 1
save_start && (l = l + 1)
u0type = typeof(u0)
us = u0type <: StaticArray ? MVector{l, u0type}(undef) : Vector{u0type}(undef, l)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't infer though

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm how would I do this then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a switch in the alg that is type based? We can follow up with that though. Let's get a first version and then get that non-allocating.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I'll open it in OrdinaryDiffEq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants