-
-
Notifications
You must be signed in to change notification settings - Fork 224
Fixes for dependent parameters #2668
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
Changes from all commits
5e6556a
441565b
c54903e
45d9925
09bc118
adf3b44
87d1dd5
99f088b
a5c79d8
250e730
5a2705e
55a9dc8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,9 +49,28 @@ using NonlinearSolve | |
@test integ.ps[p2] == 10.0 | ||
end | ||
|
||
@testset "vector parameter deps" begin | ||
@parameters p1[1:2]=[1.0, 2.0] p2[1:2]=[0.0, 0.0] | ||
@variables x(t) = 0 | ||
|
||
@named sys = ODESystem( | ||
[D(x) ~ sum(p1) * t + sum(p2)], | ||
t; | ||
parameter_dependencies = [p2 => 2p1] | ||
) | ||
prob = ODEProblem(complete(sys)) | ||
setp1! = setp(prob, p1) | ||
get_p1 = getp(prob, p1) | ||
get_p2 = getp(prob, p2) | ||
setp1!(prob, [1.5, 2.5]) | ||
|
||
@test get_p1(prob) == [1.5, 2.5] | ||
@test get_p2(prob) == [3.0, 5.0] | ||
end | ||
|
||
@testset "extend" begin | ||
@parameters p1=1.0 p2=1.0 | ||
@variables x(t) | ||
@variables x(t) = 0 | ||
|
||
@mtkbuild sys1 = ODESystem( | ||
[D(x) ~ p1 * t + p2], | ||
|
@@ -65,6 +84,73 @@ end | |
sys = extend(sys2, sys1) | ||
@test isequal(only(parameters(sys)), p1) | ||
@test Set(full_parameters(sys)) == Set([p1, p2]) | ||
prob = ODEProblem(complete(sys)) | ||
get_dep = getu(prob, 2p2) | ||
@test get_dep(prob) == 4 | ||
end | ||
|
||
@testset "getu with parameter deps" begin | ||
@parameters p1=1.0 p2=1.0 | ||
@variables x(t) = 0 | ||
|
||
@named sys = ODESystem( | ||
[D(x) ~ p1 * t + p2], | ||
t; | ||
parameter_dependencies = [p2 => 2p1] | ||
) | ||
prob = ODEProblem(complete(sys)) | ||
get_dep = getu(prob, 2p2) | ||
@test get_dep(prob) == 4 | ||
end | ||
|
||
Comment on lines
+92
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you also add a testcase for hierarchical systems where the subsystems have parameter dependencies? I have a feeling that will fail There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it failed. I added a test in db89f68 and I tried to do the renamespacing, but something is off and the dependent parameter does not seem to update correctly. |
||
@testset "getu with vector parameter deps" begin | ||
@parameters p1[1:2]=[1.0, 2.0] p2[1:2]=[0.0, 0.0] | ||
@variables x(t) = 0 | ||
|
||
@named sys = ODESystem( | ||
[D(x) ~ sum(p1) * t + sum(p2)], | ||
t; | ||
parameter_dependencies = [p2 => 2p1] | ||
) | ||
prob = ODEProblem(complete(sys)) | ||
get_dep = getu(prob, 2p1) | ||
@test get_dep(prob) == [2.0, 4.0] | ||
end | ||
|
||
@testset "composing systems with parameter deps" begin | ||
@parameters p1=1.0 p2=2.0 | ||
@variables x(t) = 0 | ||
|
||
@mtkbuild sys1 = ODESystem( | ||
[D(x) ~ p1 * t + p2], | ||
t | ||
) | ||
@named sys2 = ODESystem( | ||
[D(x) ~ p1 * t - p2], | ||
t; | ||
parameter_dependencies = [p2 => 2p1] | ||
) | ||
sys = complete(ODESystem([], t, systems = [sys1, sys2], name = :sys)) | ||
|
||
prob = ODEProblem(sys) | ||
v1 = sys.sys2.p2 | ||
v2 = 2 * v1 | ||
@test is_parameter(prob, v1) | ||
@test is_observed(prob, v2) | ||
get_v1 = getu(prob, v1) | ||
get_v2 = getu(prob, v2) | ||
@test get_v1(prob) == 2 | ||
@test get_v2(prob) == 4 | ||
|
||
setp1! = setp(prob, sys2.p1) | ||
setp1!(prob, 2.5) | ||
@test prob.ps[sys2.p2] == 5.0 | ||
|
||
new_prob = remake(prob, p = [sys2.p1 => 1.5]) | ||
|
||
@test !isempty(ModelingToolkit.parameter_dependencies(sys2)) | ||
@test new_prob.ps[sys2.p1] == 1.5 | ||
@test new_prob.ps[sys2.p2] == 3.0 | ||
end | ||
|
||
@testset "Clock system" begin | ||
|
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 is not technically necessary, although it will work regardless. If the system is simplified, all parameter dependencies will be available at the top level. If it isn't simplified, we can't simulate it anyway so an index cache is immaterial.