-
-
Notifications
You must be signed in to change notification settings - Fork 104
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
Feat: adjoints through observable functions #689
Merged
Merged
Changes from 22 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
92ad6a8
feat: adjoints through observable functions
DhairyaLGandhi 22dc7ec
Update ext/SciMLBaseZygoteExt.jl
DhairyaLGandhi 0c2b69d
feat: allow observables in collections
DhairyaLGandhi c61e08c
chore: handle no observables in collection
DhairyaLGandhi 8600a8d
fix: typo
DhairyaLGandhi a69d087
Merge branch 'master' into dg/obsfn
DhairyaLGandhi 785b052
test: add test for observable functions
DhairyaLGandhi adee4f0
test: add AD testset
DhairyaLGandhi 2197a30
Update test/downstream/observables_autodiff.jl
ChrisRackauckas 9172014
test: add a simple DAE example; disable till sensitivities are turned on
DhairyaLGandhi 95cf416
test: add missing imports
DhairyaLGandhi 4ce8257
chore: format
DhairyaLGandhi 839bd63
chore: rm unwanted adjoint
DhairyaLGandhi 2474a8d
test: check failures with SciMLSensitivity + SII
DhairyaLGandhi f68cb05
ci(SciMLSensitivity): checkout SII branch
DhairyaLGandhi 9ab29d9
ci(SciMLSensitivity): use correct path
DhairyaLGandhi a417cdd
ci: revert changes
DhairyaLGandhi ff9bb2c
chore: revert literal_getproperty adjoint
DhairyaLGandhi 032b927
chore: try to avoid returning object
DhairyaLGandhi 44bfc91
build: add MSL to test deps
DhairyaLGandhi de2d6cd
chore: don't return structural tangent
DhairyaLGandhi c63dfbf
chore: fix imports
DhairyaLGandhi 940ea78
test: add MSL to downstream env
DhairyaLGandhi 8e48f1c
test: rm MSL from test env
DhairyaLGandhi d061ce4
chore: format
DhairyaLGandhi f817b52
Update CI.yml
ChrisRackauckas 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 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 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 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,102 @@ | ||
using ModelingToolkit, OrdinaryDiffEq | ||
using Zygote | ||
using ModelingToolkit: t_nounits as t, D_nounits as D | ||
import SymbolicIndexingInterface as SII | ||
import SciMLStructures as SS | ||
using ModelingToolkitStandardLibrary | ||
import ModelingToolkitStandardLibrary as MSL | ||
|
||
@parameters σ ρ β | ||
@variables x(t) y(t) z(t) w(t) | ||
|
||
eqs = [D(D(x)) ~ σ * (y - x), | ||
D(y) ~ x * (ρ - z) - y, | ||
D(z) ~ x * y - β * z, | ||
w ~ x + y + z + 2 * β] | ||
|
||
@mtkbuild sys = ODESystem(eqs, t) | ||
|
||
u0 = [D(x) => 2.0, | ||
x => 1.0, | ||
y => 0.0, | ||
z => 0.0] | ||
|
||
p = [σ => 28.0, | ||
ρ => 10.0, | ||
β => 8 / 3] | ||
|
||
tspan = (0.0, 100.0) | ||
prob = ODEProblem(sys, u0, tspan, p, jac = true) | ||
sol = solve(prob, Tsit5()) | ||
|
||
@testset "AutoDiff Observable Functions" begin | ||
gs, = gradient(sol) do sol | ||
sum(sol[sys.w]) | ||
end | ||
du_ = [0.0, 1.0, 1.0, 1.0] | ||
du = [du_ for _ in sol.u] | ||
@test du == gs | ||
|
||
# Observable in a vector | ||
gs, = gradient(sol) do sol | ||
sum(sum.(sol[[sys.w, sys.x]])) | ||
end | ||
du_ = [0.0, 1.0, 1.0, 2.0] | ||
du = [du_ for _ in sol.u] | ||
@test du == gs | ||
end | ||
|
||
# DAE | ||
|
||
function create_model(; C₁ = 3e-5, C₂ = 1e-6) | ||
@variables t | ||
@named resistor1 = MSL.Electrical.Resistor(R = 5.0) | ||
@named resistor2 = MSL.Electrical.Resistor(R = 2.0) | ||
@named capacitor1 = MSL.Electrical.Capacitor(C = C₁) | ||
@named capacitor2 = MSL.Electrical.Capacitor(C = C₂) | ||
@named source = MSL.Electrical.Voltage() | ||
@named input_signal = MSL.Blocks.Sine(frequency = 100.0) | ||
@named ground = MSL.Electrical.Ground() | ||
@named ampermeter = MSL.Electrical.CurrentSensor() | ||
|
||
eqs = [connect(input_signal.output, source.V) | ||
connect(source.p, capacitor1.n, capacitor2.n) | ||
connect(source.n, resistor1.p, resistor2.p, ground.g) | ||
connect(resistor1.n, capacitor1.p, ampermeter.n) | ||
connect(resistor2.n, capacitor2.p, ampermeter.p)] | ||
|
||
@named circuit_model = ODESystem(eqs, t, | ||
systems = [ | ||
resistor1, resistor2, capacitor1, capacitor2, | ||
source, input_signal, ground, ampermeter | ||
]) | ||
end | ||
|
||
@testset "DAE Observable function AD" begin | ||
model = create_model() | ||
sys = structural_simplify(model) | ||
|
||
prob = ODEProblem(sys, [], (0.0, 1.0)) | ||
sol = solve(prob, Rodas4()) | ||
|
||
gs, = gradient(sol) do sol | ||
sum(sol[sys.ampermeter.i]) | ||
end | ||
du_ = [0.2, 1.0] | ||
du = [du_ for _ in sol.u] | ||
@test gs == du | ||
end | ||
|
||
# @testset "Adjoints with DAE" begin | ||
# gs_mtkp, gs_p_new = gradient(mtkparams, p_new) do p, new_tunables | ||
# new_p = SciMLStructures.replace(SciMLStructures.Tunable(), p, new_tunables) | ||
# new_prob = remake(prob, p = new_p) | ||
# sol = solve(new_prob, Rodas4()) | ||
# @show size(sol) | ||
# # mean(abs.(sol[sys.ampermeter.i] .- gt)) | ||
# sum(sol[sys.ampermeter.i]) | ||
# end | ||
# | ||
# @test isnothing(gs_mtkp) | ||
# @test length(gs_p_new) == length(p_new) | ||
# end |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be gated int Downstream
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added bounds to
test/downstream/Project.toml
in 940ea78, should I remove anything from the regular test environment or do i need to declare these in both places?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove it from the regular
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
d061ce4 does that