From ce47b7bb6c504b75d117b32a2aed95198f8766d5 Mon Sep 17 00:00:00 2001 From: Datseris Date: Fri, 2 Feb 2024 11:02:38 +0000 Subject: [PATCH 1/6] add integer converting functions --- src/core/dynamicalsystem_interface.jl | 36 +++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/core/dynamicalsystem_interface.jl b/src/core/dynamicalsystem_interface.jl index de254426..757a2a50 100644 --- a/src/core/dynamicalsystem_interface.jl +++ b/src/core/dynamicalsystem_interface.jl @@ -52,7 +52,9 @@ model and all symbolic variables. Accessing a `DynamicalSystem` using symbolic v is possible via the functions [`observe_state`](@ref), [`set_state!`](@ref), [`current_parameter`](@ref) and [`set_parameter!`](@ref). The referenced MTK model corresponding to the dynamical system can be obtained with -`model = referrenced_sciml_model(ds::DynamicalSystem)`. +`model = referrenced_sciml_model(ds::DynamicalSystem)` and the convenience functions +[`integer_state_index`](@ref), [`integer_parameter_index`](@ref) are useful for further +usage of the dynamical system. See also the DynamicalSystems.jl tutorial online for an example. @@ -116,7 +118,8 @@ errormsg(ds) = error("Not yet implemented for dynamical system of type $(nameof( export current_state, initial_state, current_parameters, current_parameter, initial_parameters, isinplace, current_time, initial_time, successful_step, isdeterministic, isdiscretetime, dynamic_rule, - reinit!, set_state!, set_parameter!, set_parameters!, step!, observe_state, referrenced_sciml_model + reinit!, set_state!, set_parameter!, set_parameters!, step!, observe_state, + referrenced_sciml_model, integer_state_index, integer_parameter_index ########################################################################################### # Symbolic support @@ -142,6 +145,35 @@ has_referrenced_model(::Nothing) = false has_referrenced_model(::SymbolicIndexingInterface.SymbolCache{Nothing, Nothing, Nothing}) = false has_referrenced_model(sys) = true +""" + integer_parameter_index(symbol, ds::DynamicalSystem) → i::Int + +Convert the given symbolic variable representing a parameter to its integer +index in the parameter container ([`current_parameters`](@ref)). +Return `nothing` if `ds` doesn't reference a symbolic model +or the model does not have the given `symbol` as parameter. +""" +integer_parameter_index(s, ds::DynamicalSystem) = integer_parameter_index(s, referrenced_sciml_model(ds)) +function integer_parameter_index(symbol, model) + isnothing(model) && return nothing + findfirst(isequal(symbol), parameters(model)) +end + +""" + integer_state_index(symbol, ds::DynamicalSystem) → i::Int + +Convert the given symbolic variable representing a state (dynamic) variable to its integer +index in the state vector ([`current_state`](@ref)). +Return `nothing` if `ds` doesn't reference a symbolic model +or the model does not have the given `symbol` as a state variable. +""" +integer_state_index(s, ds::DynamicalSystem) = integer_state_index(s, referrenced_sciml_model(ds)) +function integer_state_index(symbol, model) + isnothing(model) && return nothing + findfirst(isequal(symbol), states(model)) +end + + ########################################################################################### # API - obtaining information from the system ########################################################################################### From 94ad74d0d91f721fd073f387ff198fba04df8de9 Mon Sep 17 00:00:00 2001 From: Datseris Date: Fri, 2 Feb 2024 11:04:42 +0000 Subject: [PATCH 2/6] add the new names in the docs --- docs/src/index.md | 21 ++++++++++++++++++++- src/core/dynamicalsystem_interface.jl | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index e9fd158c..f4f6f524 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -13,6 +13,7 @@ DynamicalSystemsBase DynamicalSystem ``` +### API - Obtain information ```@docs current_state initial_state @@ -27,9 +28,10 @@ current_time initial_time isinplace(::DynamicalSystem) successful_step -referrenced_sciml_model ``` +### API - Alter status + ```@docs reinit!(::DynamicalSystem, args...; kwargs...) set_state! @@ -37,7 +39,16 @@ set_parameter! set_parameters! ``` +### API - MTK integration + +```@docs +referrenced_sciml_model +integer_state_index +integer_parameter_index +``` + ## Time evolution + ```@docs step!(::DynamicalSystem, args...; kwargs...) trajectory @@ -45,21 +56,25 @@ StateSpaceSet ``` ## `DeterministicIteratedMap` + ```@docs DeterministicIteratedMap ``` ## `CoupledODEs` + ```@docs CoupledODEs ``` ## `StroboscopicMap` + ```@docs StroboscopicMap ``` ## `PoincareMap` + ```@docs PoincareMap current_crossing_time @@ -67,6 +82,7 @@ poincaresos ``` ## `TangentDynamicalSystem` + ```@docs CoreDynamicalSystem TangentDynamicalSystem @@ -76,11 +92,13 @@ orthonormal ``` ## `ProjectedDynamicalSystem` + ```@docs ProjectedDynamicalSystem ``` ## `ParallelDynamicalSystem` + ```@docs ParallelDynamicalSystem initial_states @@ -88,6 +106,7 @@ current_states ``` ## `ArbitrarySteppable` + ```@docs ArbitrarySteppable ``` diff --git a/src/core/dynamicalsystem_interface.jl b/src/core/dynamicalsystem_interface.jl index 757a2a50..7ab5d7be 100644 --- a/src/core/dynamicalsystem_interface.jl +++ b/src/core/dynamicalsystem_interface.jl @@ -122,7 +122,7 @@ export current_state, initial_state, current_parameters, current_parameter, init referrenced_sciml_model, integer_state_index, integer_parameter_index ########################################################################################### -# Symbolic support +# Symbolic support (MTK integration) ########################################################################################### # Simply extend the `referrenced_sciml_prob` and you have symbolic indexing support! import SymbolicIndexingInterface From d1aa3252adae753cd81280a936e4eb8734c23abb Mon Sep 17 00:00:00 2001 From: Datseris Date: Fri, 2 Feb 2024 11:07:27 +0000 Subject: [PATCH 3/6] add tests --- test/mtk_integration.jl | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/test/mtk_integration.jl b/test/mtk_integration.jl index a0440e15..7423d62c 100644 --- a/test/mtk_integration.jl +++ b/test/mtk_integration.jl @@ -59,6 +59,12 @@ set_state!(ds, 1.5, 1) set_state!(ds, -0.5, fol_1.x) @test observe_state(ds, 1) == -0.5 +# integer conversion +@test integer_state_index(fol_1.x, ds) == 1 +@test isnothing(integer_state_index(fol_2.RHS, ds)) +@test integer_parameter_index(fol_1.f, ds) == 1 +@test isnothing(integer_parameter_index(fol_1.x, ds)) + # test that derivative dynamical systems also work as execpted u1 = current_state(ds) pds = ParallelDynamicalSystem(ds, [u1, copy(u1)]) @@ -110,8 +116,10 @@ set_parameter!(ds, 1, 2.0) @test_throws ArgumentError observe_state(ds, fol_1.f) -# Test that remake works also without anything initial +@test isnothing(integer_state_index(fol_2.RHS, ds)) +@test isnothing(integer_parameter_index(fol_1.x, ds)) +# Test that remake works also without anything initial @variables t D = Differential(t) @mtkmodel Roessler begin @@ -138,4 +146,4 @@ end prob = ODEProblem(roessler_model) roessler = CoupledODEs(prob) -@test roessler isa CoupledODEs \ No newline at end of file +@test roessler isa CoupledODEs From 889b12439b5d8c78df6529b6afddbb37e0d12a3c Mon Sep 17 00:00:00 2001 From: Datseris Date: Fri, 2 Feb 2024 11:09:43 +0000 Subject: [PATCH 4/6] add changelog --- CHANGELOG.md | 4 ++++ Project.toml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc4209ec..9d7de789 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# v3.6.0 + +Two new API functions for MTK integration: `integer_state_index` and `integer_parameter_index`. + # v3.5.0 Dynamical systems that have been constructed from `DEProblem`s that themselves diff --git a/Project.toml b/Project.toml index 4ac8f85c..779a8083 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "DynamicalSystemsBase" uuid = "6e36e845-645a-534a-86f2-f5d4aa5a06b4" repo = "https://github.com/JuliaDynamics/DynamicalSystemsBase.jl.git" -version = "3.5.2" +version = "3.6.0" [deps] ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" From f2c51f0c205474fa6c1008b4badc9cadd0c07cb5 Mon Sep 17 00:00:00 2001 From: Datseris Date: Fri, 2 Feb 2024 11:45:24 +0000 Subject: [PATCH 5/6] document that one can alter the obdserved state --- src/core/dynamicalsystem_interface.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/dynamicalsystem_interface.jl b/src/core/dynamicalsystem_interface.jl index 7ab5d7be..7eee8a44 100644 --- a/src/core/dynamicalsystem_interface.jl +++ b/src/core/dynamicalsystem_interface.jl @@ -195,9 +195,9 @@ See also [`initial_state`](@ref), [`observe_state`](@ref). current_state(ds::DynamicalSystem) = ds.u """ - observe_state(ds::DynamicalSystem, i) → x::Real + observe_state(ds::DynamicalSystem, i [,u = current_state(ds)]) → x::Real -Return the current state of `ds` _observed_ at "index" `i`. Possibilities are: +Return the state `u` of `ds` _observed_ at "index" `i`. Possibilities are: - `i::Int` returns the `i`-th dynamic variable. - `i::Function` returns `f(current_state(ds))`, which is asserted to be a real number. From 1cb209c23f5fe09aac29e6974249122706bb31a8 Mon Sep 17 00:00:00 2001 From: Datseris Date: Fri, 2 Feb 2024 11:47:49 +0000 Subject: [PATCH 6/6] typo --- src/core/dynamicalsystem_interface.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/dynamicalsystem_interface.jl b/src/core/dynamicalsystem_interface.jl index 7eee8a44..64e9554d 100644 --- a/src/core/dynamicalsystem_interface.jl +++ b/src/core/dynamicalsystem_interface.jl @@ -143,7 +143,7 @@ referrenced_sciml_model(::Nothing) = nothing has_referrenced_model(prob::SciMLBase.DEProblem) = has_referrenced_model(referrenced_sciml_model(prob)) has_referrenced_model(::Nothing) = false has_referrenced_model(::SymbolicIndexingInterface.SymbolCache{Nothing, Nothing, Nothing}) = false -has_referrenced_model(sys) = true +has_referrenced_model(model) = true """ integer_parameter_index(symbol, ds::DynamicalSystem) → i::Int