Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/graph_system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ function make_connection_matrices(g_flat, nodes_partitioned=make_partitioned_nod
push!(conns, conn)

for (prop, name) ∈ pairs(connection_property_namemap(conn, get_name(nodekl), get_name(nodeij)))
connection_namemap[name] = ConnectionIndex(nc, k, i, l, j, prop)
connection_namemap[name] = ConnectionIndex(nc, k, i, l, j, name, prop)
end

for t ∈ event_times(conn)
Expand Down Expand Up @@ -281,7 +281,7 @@ end
name_kl = names_partitioned[k][l]
name_ij = names_partitioned[i][j]
for (prop, name) ∈ pairs(connection_property_namemap(conn, name_kl, name_ij))
connection_namemap[name] = ConnectionIndex(nc, k, i, l, j, prop)
connection_namemap[name] = ConnectionIndex(nc, k, i, l, j, name, prop)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/subsystems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function ConstructionBase.setproperties(s::SubsystemParams{T}, patch::NamedTuple
set_param_prop(s, patch; allow_typechange=false)
end
function set_param_prop(s::SubsystemParams{T}, key, val; allow_typechange=false) where {T}
set_param_prop(s, NamedTuple{(key,)}(val); allow_typechange)
set_param_prop(s, NamedTuple{(key,)}((val,)); allow_typechange)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit is to allow values to be iterators because previously it was trying to splat them and that was erroring

end
function set_param_prop(s::SubsystemParams{T}, patch; allow_typechange=false) where {T}
props = NamedTuple(s)
Expand Down
36 changes: 29 additions & 7 deletions src/symbolic_indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ struct CompuIndex
prop::Symbol
requires_inputs::Bool
end

struct ConnectionIndex
nc::Int
i_src::Int
i_dst::Int
j_src::Int
j_dst::Int
connection_key::Symbol
prop::Symbol
end

Expand Down Expand Up @@ -164,6 +164,25 @@ function SymbolicIndexingInterface.parameter_values(p::GraphSystemParameters, i:
p.connection_matrices[i]
end

function SymbolicIndexingInterface.set_parameter!(p::GraphSystemParameters, val, idx::ParamIndex)
(; tup_index, v_index, prop) = idx
(; params_partitioned) = p
params = params_partitioned[tup_index][v_index]
params_new = set_param_prop(params, prop, val; allow_typechange=false)
params_partitioned[tup_index][v_index] = params_new
p
end

function SymbolicIndexingInterface.set_parameter!(buffer::GraphSystemParameters, value, conn_index::ConnectionIndex)
(;connection_matrices, connection_namemap) = buffer
(; nc, i_src, i_dst, j_src, j_dst, connection_key, prop) = conn_index
conn_old = connection_matrices[nc][i_src, i_dst][j_src, j_dst]
conn_new = setproperties(conn_old, NamedTuple{(prop,)}(value))
connection_matrices[nc][i_src, i_dst][j_src, j_dst] = conn_new
buffer
end


function SymbolicIndexingInterface.parameter_symbols(g::PartitionedGraphSystem)
collect(Iterators.flatten((keys(g.param_namemap), keys(g.connection_namemap))))
end
Expand Down Expand Up @@ -253,17 +272,20 @@ function set_params!!(buffer::GraphSystemParameters, param_map)
(; param_namemap, connection_namemap) = buffer
for (key, val) ∈ param_map
if haskey(param_namemap, key)
buffer = set_param!!(buffer, key, param_namemap[key], val)
buffer = set_param!!(buffer, param_namemap[key], val)
elseif haskey(connection_namemap, key)
buffer = set_param!!(buffer, key, connection_namemap[key], val)
buffer = set_param!!(buffer, connection_namemap[key], val)
else
error("Key $key does not correspond to a known parameter. ")
end
end
buffer
end

function set_param!!(buffer::GraphSystemParameters, key, (; tup_index, v_index, prop)::ParamIndex, val)

# This is a possibly-out-of-place variant of set_parameter! that is meant to be used by `remake` where
# types are allowed to be widened.
function set_param!!(buffer::GraphSystemParameters, (; tup_index, v_index, prop)::ParamIndex, val)
(; params_partitioned) = buffer
params = params_partitioned[tup_index][v_index]
params_new = set_param_prop(params, prop, val; allow_typechange=true)
Expand All @@ -288,9 +310,9 @@ function re_eltype_params(params_partitioned)
end
end

function set_param!!(buffer::GraphSystemParameters, key, conn_index::ConnectionIndex, value)
function set_param!!(buffer::GraphSystemParameters, conn_index::ConnectionIndex, value)
(;connection_matrices, connection_namemap) = buffer
(; nc, i_src, i_dst, j_src, j_dst, prop) = conn_index
(; nc, i_src, i_dst, j_src, j_dst, connection_key, prop) = conn_index
conn_old = connection_matrices[nc][i_src, i_dst][j_src, j_dst]
conn_new = setproperties(conn_old, NamedTuple{(prop,)}(value))
CR_new = typeof(conn_new)
Expand All @@ -317,7 +339,7 @@ function set_param!!(buffer::GraphSystemParameters, key, conn_index::ConnectionI

# Update the position in the namemap
let conn_index_new = @set conn_index.nc = nc_new
connection_namemap[key] = conn_index_new # This is important so we don't lose track of where the parameter moved to!
connection_namemap[connection_key] = conn_index_new # This is important so we don't lose track of where the parameter moved to!
end

# Delete the old element!
Expand Down
2 changes: 1 addition & 1 deletion test/particle_osc_example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function particle_osc_prob(;x1, x2, m=3.0, mp1=1.0, kc_p1_p2=1, tspan = (0.0, 10
[:particle1₊x => x1, :particle2₊x => x2, :particle2₊v => 0.0, :osc₊x => 0.0
],
tspan,
(:osc₊m => m, :particle1₊m =>mp1, :fac_coulomb_particle1_particle2 => kc_p1_p2)
(:osc₊m => m, :particle1₊m => mp1, :fac_coulomb_particle1_particle2 => kc_p1_p2)
)
end
function solve_particle_osc(;reltol=nothing, saveat=nothing, kwargs...)
Expand Down
33 changes: 32 additions & 1 deletion test/symbolic_indexing.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
include("particle_osc_example.jl")
using SymbolicIndexingInterface

@testset "Symbolic Indexing of Vectors" begin
@testset "Symbolic Indexing of Vectors of observables" begin
sol = solve_particle_osc(x1=1.0, x2=-1.0)

a = getsym(sol, :particle1₊a)(sol)[end]
ω = getsym(sol, :osc₊ω₀)(sol)[end]
@test getsym(sol, [:osc₊ω₀, :particle1₊a])(sol)[end] == [ω, a]
end

@testset "setp and getp" begin
prob = particle_osc_prob(; x1 = 1.0, x2 = 0.0)

# Test setp works
setp(prob, :particle1₊m)(prob, 2.0)
@test getp(prob, :particle1₊m)(prob) == 2

# Test type promotion and conversion
@test_broken begin
(prob, :particle1₊m)(prob, 20)
@test getp(prob, :particle1₊m)(prob) === 20.0
end
Comment on lines +20 to +23
Copy link
Copy Markdown
Collaborator

@MasonProtter MasonProtter Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll want to support stuff like setting an Int where the value is actually a Float64, but it's a little complicated. Lets do that in a followup PR and just get this merged first.

# Test on connections as well
setp(prob, :fac_coulomb_particle1_particle2)(prob, 100)
@test getp(prob, :fac_coulomb_particle1_particle2)(prob) == 100

# Test type promotion and conversion on connections
@test_broken begin
setp(prob, :fac_coulomb_particle1_particle2)(prob, 100.0)
getp(prob, :fac_coulomb_particle1_particle2)(prob) == 100
end

# Error on type-unstable change
@test_throws ErrorException setp(prob, :particle1₊m)(prob, ones(3))

# Remake
prob = remake(prob, p = [:particle1₊m => 2 + 3im, :particle2₊m => 3 + 2im])
setp(prob, :particle1₊m)(prob, 3 + 3.0im)
@test getp(prob, :particle1₊m)(prob) == 3 + 3im
end