-
Notifications
You must be signed in to change notification settings - Fork 31
Make time evolution solvers compatible with automatic differentiation #311
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
Merged
Merged
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e29feb8
Working sesolve
albertomercurio 5d68661
add `inplace` keywork argument
albertomercurio 1138092
add SciMLStructures and relax params type
albertomercurio 6598886
Working mcsolve (no type-stability)
albertomercurio 75acbd8
Fix type-instabilities for mcsolve
albertomercurio 5579426
Add SciMLStructures.jl methods
albertomercurio 18549c1
Add callbacks helpers
albertomercurio eb05e53
Fix dsf_mcsolve
albertomercurio 8e72246
Remove ProgressBar from ODE parameters
albertomercurio a070ab6
Fix abstol and reltol extraction
albertomercurio b652fa7
Use Base allequal function
albertomercurio dddb60b
Remove expvals from TimeEvolutionParameters
albertomercurio 024b4fd
Make NullParameters as default for params
albertomercurio 5bcd2e1
Remove custom PresetTimeCallback
albertomercurio 26f6f4c
Update description of `inplace` argument
albertomercurio 84b2c64
Working mesolve
albertomercurio 81d627c
Fix dfd_mesolve and dsf_mesolve
albertomercurio 89b072c
Remove TimeEvolutionParameters (type-unstable)
albertomercurio 892d0e9
Fix type instabilities
albertomercurio 0eb46e4
Fix type instabilities on Julia v1.10
albertomercurio a426df6
Format document
albertomercurio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| #= | ||
| This file contains helper functions for callbacks. The affect! function are defined taking advantage of the Julia struct, which allows to store some cache exclusively for the callback. | ||
| =# | ||
|
|
||
| include("sesolve_callback_helpers.jl") | ||
| include("mesolve_callback_helpers.jl") | ||
| include("mcsolve_callback_helpers.jl") | ||
|
|
||
| ## | ||
|
|
||
| # Multiple dispatch depending on the progress_bar and e_ops types | ||
| function _generate_se_me_kwargs(e_ops, progress_bar, tlist, kwargs, method) | ||
| cb = _generate_save_callback(e_ops, tlist, progress_bar, method) | ||
| return _merge_kwargs_with_callback(kwargs, cb) | ||
| end | ||
| _generate_se_me_kwargs(e_ops::Nothing, progress_bar::Val{false}, tlist, kwargs, method) = kwargs | ||
|
|
||
| function _merge_kwargs_with_callback(kwargs, cb) | ||
| kwargs2 = | ||
| haskey(kwargs, :callback) ? merge(kwargs, (callback = CallbackSet(cb, kwargs.callback),)) : | ||
| merge(kwargs, (callback = cb,)) | ||
|
|
||
| return kwargs2 | ||
| end | ||
|
|
||
| function _generate_save_callback(e_ops, tlist, progress_bar, method) | ||
| e_ops_data = e_ops isa Nothing ? nothing : _get_e_ops_data(e_ops, method) | ||
|
|
||
| progr = getVal(progress_bar) ? ProgressBar(length(tlist), enable = getVal(progress_bar)) : nothing | ||
|
|
||
| expvals = e_ops isa Nothing ? nothing : Array{ComplexF64}(undef, length(e_ops), length(tlist)) | ||
|
|
||
| _save_affect! = method(e_ops_data, progr, Ref(1), expvals) | ||
| return PresetTimeCallback(tlist, _save_affect!, save_positions = (false, false)) | ||
| end | ||
|
|
||
| _get_e_ops_data(e_ops, ::Type{SaveFuncSESolve}) = get_data.(e_ops) | ||
| _get_e_ops_data(e_ops, ::Type{SaveFuncMESolve}) = _generate_mesolve_e_op.(e_ops) | ||
|
|
||
| _generate_mesolve_e_op(op) = mat2vec(adjoint(get_data(op))) | ||
|
|
||
| ## | ||
|
|
||
| # When e_ops is Nothing. Common for both mesolve and sesolve | ||
| function _save_func(integrator, progr) | ||
| next!(progr) | ||
| u_modified!(integrator, false) | ||
| return nothing | ||
| end | ||
|
|
||
| # When progr is Nothing. Common for both mesolve and sesolve | ||
| function _save_func(integrator, progr::Nothing) | ||
| u_modified!(integrator, false) | ||
| return nothing | ||
| end | ||
|
|
||
| ## | ||
|
|
||
| # Get the e_ops from a given AbstractODESolution. Valid for `sesolve`, `mesolve` and `ssesolve`. | ||
| function _se_me_sse_get_expvals(sol::AbstractODESolution) | ||
| cb = _se_me_sse_get_save_callback(sol) | ||
| if cb isa Nothing | ||
| return nothing | ||
| else | ||
| return cb.affect!.expvals | ||
| end | ||
| end | ||
|
|
||
| function _se_me_sse_get_save_callback(sol::AbstractODESolution) | ||
| kwargs = NamedTuple(sol.prob.kwargs) # Convert to NamedTuple to support Zygote.jl | ||
| if hasproperty(kwargs, :callback) | ||
| return _se_me_sse_get_save_callback(kwargs.callback) | ||
| else | ||
| return nothing | ||
| end | ||
| end | ||
| _se_me_sse_get_save_callback(integrator::AbstractODEIntegrator) = _se_me_sse_get_save_callback(integrator.opts.callback) | ||
| function _se_me_sse_get_save_callback(cb::CallbackSet) | ||
| cbs_discrete = cb.discrete_callbacks | ||
| if length(cbs_discrete) > 0 | ||
| _cb = cb.discrete_callbacks[1] | ||
| return _se_me_sse_get_save_callback(_cb) | ||
| else | ||
| return nothing | ||
| end | ||
| end | ||
| _se_me_sse_get_save_callback(cb::DiscreteCallback) = | ||
| if (cb.affect! isa SaveFuncSESolve) || (cb.affect! isa SaveFuncMESolve) | ||
| return cb | ||
| else | ||
| return nothing | ||
| end | ||
| _se_me_sse_get_save_callback(cb::ContinuousCallback) = nothing | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.