From 837be9807ec93d6ddbf9cf8b21d0ae8afcfbfae7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 13 Dec 2022 14:05:43 +0530 Subject: [PATCH 1/7] Add default methods interface functions - All interface functions have sensible defaults - All interface functions by default compare `Symbol` - Update documentation - Add `SymbolCache` docstring --- docs/src/api.md | 2 ++ src/interface.jl | 47 +++++++++++++++++++++++++++++++++++----------- src/symbolcache.jl | 13 +++++-------- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/docs/src/api.md b/docs/src/api.md index 06eaa07c..c29ad76c 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -1,5 +1,7 @@ # Interface Functions +Default methods cast all symbols to `Symbol` before comparing. + ```@docs independent_variables is_indep_sym diff --git a/src/interface.jl b/src/interface.jl index 8b2de0b7..6d9186e4 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -1,58 +1,83 @@ """ $(TYPEDSIGNATURES) -Get the set of independent variables for the given system. +Get an iterable over the independent variables for the given system. Default to an empty +vector. """ function independent_variables end +independent_variables(::Any) = [] """ $(TYPEDSIGNATURES) -Check if the given sym is an independent variable in the given system. Defaults -to `false` if not implemented for the given system/container type. +Check if the given sym is an independent variable in the given system. Default to checking +if the given `sym` exists in the iterable returned by `independent_variables`. """ function is_indep_sym end +function is_indep_sym(store, sym) + any(isequal(Symbol(sym)), Symbol(x) for x in independent_variables(store)) +end + """ $(TYPEDSIGNATURES) -Get the set of states for the given system. +Get an iterable over the states for the given system. Default to an empty vector. """ function states end +states(::Any) = [] + """ $(TYPEDSIGNATURES) -Find the index of the given sym in the given system. +Find the index of the given sym in the given system. Default to the index of the first +symbol in the iterable returned by `states` which matches the given `sym`. Return +`nothing` if the given `sym` does not match. """ function state_sym_to_index end +function state_sym_to_index(store, sym) + findfirst(isequal(Symbol(sym)), Symbol(x) for x in states(store)) +end + """ $(TYPEDSIGNATURES) -Check if the given sym is a state variable in the given system. Defaults -to `false` if not implemented for the given system/container type. +Check if the given sym is a state variable in the given system. Default to checking if +the value returned by `state_sym_to_index` is not `nothing`. """ function is_state_sym end +is_state_sym(store, sym) = !isnothing(state_sym_to_index(store, sym)) + """ $(TYPEDSIGNATURES) -Get the set of parameters variables for the given system. +Get an iterable over the parameters variables for the given system. Default to an empty +vector. """ function parameters end +parameters(::Any) = [] + """ $(TYPEDSIGNATURES) -Find the index of the given sym in the given system. +Find the index of the given sym in the given system. Default to the index of the first +symbol in the iterable retruned by `parameters` which matches the given `sym`. Return +`nothing` if the given `sym` does not match. """ function param_sym_to_index end +param_sym_to_index(store, sym) = findfirst(isequal(Symbol(sym)), Symbol.(parameters(store))) + """ $(TYPEDSIGNATURES) -Check if the given sym is a parameter variable in the given system. Defaults -to `false` if not implemented for the given system/container type. +Check if the given sym is a parameter variable in the given system. Default +to checking if the value returned by `param_sym_to_index` is not `nothing`. """ function is_param_sym end + +is_param_sym(store, sym) = !isnothing(param_sym_to_index(store, sym)) diff --git a/src/symbolcache.jl b/src/symbolcache.jl index 0c104b4b..f9583fff 100644 --- a/src/symbolcache.jl +++ b/src/symbolcache.jl @@ -1,3 +1,8 @@ +""" + SymbolCache(syms, indepsym, paramsyms) + +A container that simply stores a vector of all syms, indepsym and paramsyms. +""" struct SymbolCache{S, T, U} syms::S indepsym::T @@ -6,21 +11,13 @@ end independent_variables(sc::SymbolCache) = sc.indepsym independent_variables(::SymbolCache{S, Nothing}) where {S} = [] -is_indep_sum(::Any, _) = false -is_indep_sym(sc::SymbolCache, sym) = any(isequal(sym), sc.indepsym) is_indep_sym(::SymbolCache{S, Nothing}, _) where {S} = false states(sc::SymbolCache) = sc.syms states(::SymbolCache{Nothing}) = [] -state_sym_to_index(sc::SymbolCache, sym) = findfirst(isequal(sym), sc.syms) state_sym_to_index(::SymbolCache{Nothing}, _) = nothing -is_state_sym(::Any, _) = false -is_state_sym(sc::SymbolCache, sym) = !isnothing(state_sym_to_index(sc, sym)) parameters(sc::SymbolCache) = sc.paramsyms parameters(::SymbolCache{S, T, Nothing}) where {S, T} = [] -param_sym_to_index(sc::SymbolCache, sym) = findfirst(isequal(sym), sc.paramsyms) param_sym_to_index(::SymbolCache{S, T, Nothing}, _) where {S, T} = nothing -is_param_sym(::Any, _) = false -is_param_sym(sc::SymbolCache, sym) = !isnothing(param_sym_to_index(sc, sym)) function Base.copy(VA::SymbolCache) typeof(VA)((VA.syms === nothing) ? nothing : copy(VA.syms), From ebb062030da97ed999c9b7e1603e778e56e8953e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 13 Dec 2022 14:12:21 +0530 Subject: [PATCH 2/7] Add tests for default interface methods --- test/default_function_test.jl | 10 ++++++++++ test/runtests.jl | 1 + 2 files changed, 11 insertions(+) create mode 100644 test/default_function_test.jl diff --git a/test/default_function_test.jl b/test/default_function_test.jl new file mode 100644 index 00000000..732bdd54 --- /dev/null +++ b/test/default_function_test.jl @@ -0,0 +1,10 @@ +using SymbolicIndexingInterface, Test + +@test independent_variables(nothing) == [] +@test states(nothing) == [] +@test parameters(nothing) == [] +@test !is_indep_sym(nothing, :a) +@test !is_state_sym(nothing, :a) +@test !is_param_sym(nothing, :a) +@test isnothing(state_sym_to_index(nothing, :a)) +@test isnothing(param_sym_to_index(nothing, :a)) diff --git a/test/runtests.jl b/test/runtests.jl index 42843c76..a1461476 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,3 +2,4 @@ using SymbolicIndexingInterface using Test @time begin @time @testset begin include("symbolcache.jl") end end +@time begin @time @testset begin include("default_function_test.jl") end end \ No newline at end of file From c37680103de1715901a524db2c4e012c0317b679 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 13 Dec 2022 15:01:34 +0530 Subject: [PATCH 3/7] Fix docs compat --- docs/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Project.toml b/docs/Project.toml index d2106b21..f2af07c7 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -4,4 +4,4 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" [compat] Documenter = "0.27" -SymbolicIndexingInterface = "0.1" +SymbolicIndexingInterface = "0.2" From e35d44d00a5823b733e5ea5db7e8ac853d35d511 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 13 Dec 2022 15:08:45 +0530 Subject: [PATCH 4/7] Downstream SciMLBase and RecursiveArrayTools tests --- .github/workflows/Downstream.yml | 54 ++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 .github/workflows/Downstream.yml diff --git a/.github/workflows/Downstream.yml b/.github/workflows/Downstream.yml new file mode 100644 index 00000000..cb765bf4 --- /dev/null +++ b/.github/workflows/Downstream.yml @@ -0,0 +1,54 @@ +name: IntegrationTest +on: + push: + branches: [master] + tags: [v*] + pull_request: + +jobs: + test: + name: ${{ matrix.package.repo }}/${{ matrix.package.group }}/${{ matrix.julia-version }} + runs-on: ${{ matrix.os }} + env: + GROUP: ${{ matrix.package.group }} + strategy: + fail-fast: false + matrix: + julia-version: [1,1.6] + os: [ubuntu-latest] + package: + - {user: SciML, repo: SciMLBase.jl, group: Downstream} + - {user: SciML, repo: RecursiveArrayTools.jl, group: All} + steps: + - uses: actions/checkout@v2 + - uses: julia-actions/setup-julia@v1 + with: + version: ${{ matrix.julia-version }} + arch: x64 + - uses: julia-actions/julia-buildpkg@latest + - name: Clone Downstream + uses: actions/checkout@v2 + with: + repository: ${{ matrix.package.user }}/${{ matrix.package.repo }} + path: downstream + - name: Load this and run the downstream tests + shell: julia --color=yes --project=downstream {0} + run: | + using Pkg + try + # force it to use this PR's version of the package + Pkg.develop(PackageSpec(path=".")) # resolver may fail with main deps + Pkg.update() + Pkg.test(coverage=true) # resolver may fail with test time deps + catch err + err isa Pkg.Resolve.ResolverError || rethrow() + # If we can't resolve that means this is incompatible by SemVer and this is fine + # It means we marked this as a breaking change, so we don't need to worry about + # Mistakenly introducing a breaking change, as we have intentionally made one + @info "Not compatible with this release. No problem." exception=err + exit(0) # Exit immediately, as a success + end + - uses: julia-actions/julia-processcoverage@v1 + - uses: codecov/codecov-action@v1 + with: + file: lcov.info From 03f2fcec88d248d8e69ba145cb90412a18b13e9f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 13 Dec 2022 15:09:33 +0530 Subject: [PATCH 5/7] Don't test downstream SciMLBase yet --- .github/workflows/Downstream.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/Downstream.yml b/.github/workflows/Downstream.yml index cb765bf4..19538286 100644 --- a/.github/workflows/Downstream.yml +++ b/.github/workflows/Downstream.yml @@ -17,7 +17,6 @@ jobs: julia-version: [1,1.6] os: [ubuntu-latest] package: - - {user: SciML, repo: SciMLBase.jl, group: Downstream} - {user: SciML, repo: RecursiveArrayTools.jl, group: All} steps: - uses: actions/checkout@v2 From 26b28a8df102cd0d816cb9cf0f48e0b66acef7a5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 13 Dec 2022 15:13:58 +0530 Subject: [PATCH 6/7] Don't use generators --- src/interface.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index 6d9186e4..f594f974 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -16,7 +16,7 @@ if the given `sym` exists in the iterable returned by `independent_variables`. function is_indep_sym end function is_indep_sym(store, sym) - any(isequal(Symbol(sym)), Symbol(x) for x in independent_variables(store)) + any(isequal(Symbol(sym)), Symbol.(independent_variables(store))) end """ @@ -38,7 +38,7 @@ symbol in the iterable returned by `states` which matches the given `sym`. Retur function state_sym_to_index end function state_sym_to_index(store, sym) - findfirst(isequal(Symbol(sym)), Symbol(x) for x in states(store)) + findfirst(isequal(Symbol(sym)), Symbol.(states(store))) end """ From 21344daa96fb602a79f396c02111797aa278cc9d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 13 Dec 2022 15:14:42 +0530 Subject: [PATCH 7/7] Formatting --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index a1461476..50a538ad 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,4 +2,4 @@ using SymbolicIndexingInterface using Test @time begin @time @testset begin include("symbolcache.jl") end end -@time begin @time @testset begin include("default_function_test.jl") end end \ No newline at end of file +@time begin @time @testset begin include("default_function_test.jl") end end