Skip to content

Commit a5f6cf8

Browse files
committed
update
1 parent e172c47 commit a5f6cf8

File tree

5 files changed

+23
-66
lines changed

5 files changed

+23
-66
lines changed

src/networkapi.jl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -585,21 +585,16 @@ function symmap_to_varmap(sys, symmap::Tuple)
585585
end
586586
end
587587

588-
function symmap_to_varmap(sys, symmap::AbstractArray{Pair{T, V}}) where {T, V}
588+
function symmap_to_varmap(sys, symmap::AbstractArray{Pair{Symbol, T}}) where {T}
589589
[_symbol_to_var(sys, sym) => val for (sym, val) in symmap]
590590
end
591591

592-
function symmap_to_varmap(sys, symmap::Dict{T, V}) where {T, V}
592+
function symmap_to_varmap(sys, symmap::Dict{Symbol, T}) where {T}
593593
Dict(_symbol_to_var(sys, sym) => val for (sym, val) in symmap)
594594
end
595595

596-
# don't permute any other types and let varmap_to_vars handle erroring.
597-
# If all keys are `Num`, conversion not needed.
596+
# don't permute any other types and let varmap_to_vars handle erroring
598597
symmap_to_varmap(sys, symmap) = symmap
599-
symmap_to_varmap(sys, symmap::AbstractArray{Pair{SymbolicUtils.BasicSymbolic{Real}, T}}) where {T} = symmap
600-
symmap_to_varmap(sys, symmap::AbstractArray{Pair{Num, T}}) where {T} = symmap
601-
symmap_to_varmap(sys, symmap::Dict{SymbolicUtils.BasicSymbolic{Real}, T}) where {T} = symmap
602-
symmap_to_varmap(sys, symmap::Dict{Num, T}) where {T} = symmap
603598
#error("symmap_to_varmap requires a Dict, AbstractArray or Tuple to map Symbols to values.")
604599

605600
######################## reaction complexes and reaction rates ###############################

src/reaction_network.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
370370
variables = extract_syms(options, :variables)
371371

372372
# Handles a (potential) noise scaling parameter.
373-
#nosie_scaling_p_args = handle_noise_scaling_ps!(parameters_declared, options)
373+
#noise_scaling_p_args = handle_noise_scaling_ps!(parameters_declared, options)
374374

375375
# handle independent variables
376376
if haskey(options, :ivs)
@@ -417,6 +417,9 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
417417
push!(rxexprs.args, get_rxexprs(reaction))
418418
end
419419

420+
# Adds potentital noise scaling parameters.
421+
#append!(ps.args, noise_scaling_p_args)
422+
420423
# Returns the rephrased expression.
421424
quote
422425
$ps
@@ -563,7 +566,7 @@ function get_pexpr(parameters_extracted, options)
563566
append!(pexprs.args, get_noise_scaling_pexpr(options))
564567
pexprs
565568
end
566-
# Extracts any decalred nosie scaling parameters.
569+
# Extracts any decalred noise scaling parameters.
567570
function get_noise_scaling_pexpr(options)
568571
haskey(options, :noise_scaling_parameters) || return []
569572
ns_expr = options[:noise_scaling_parameters]

src/reactionsystem.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,7 @@ function Base.convert(::Type{<:SDESystem}, rs::ReactionSystem;
15061506
default_u0 = Dict(), default_p = Dict(), defaults = _merge(Dict(default_u0), Dict(default_p)),
15071507
kwargs...)
15081508
spatial_convert_err(rs::ReactionSystem, SDESystem)
1509+
isnothing(noise_scaling) && (noise_scaling = get_noise_scaling(rs)) # Required before final deprication of previous noise scaling functionality.
15091510

15101511
flatrs = Catalyst.flatten(rs)
15111512
error_if_constraints(SDESystem, flatrs)
@@ -1641,11 +1642,15 @@ end
16411642
# SDEProblem from AbstractReactionNetwork
16421643
function DiffEqBase.SDEProblem(rs::ReactionSystem, u0, tspan,
16431644
p = DiffEqBase.NullParameters(), args...;
1644-
noise_scaling = get_noise_scaling(rs), name = nameof(rs),
1645+
noise_scaling = nothing, name = nameof(rs),
16451646
combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
16461647
include_zero_odes = true, checks = false,
16471648
check_length = false,
16481649
remove_conserved = false, kwargs...)
1650+
if !isnothing(noise_scaling)
1651+
any(isnoisescalingparameter.(parameters(rs))) && error("You have declared some paraemters as noise scaling parameters, and also given a \"noise_scaling\" argument to SDEProblem. Please remove the \"noise_scaling\", as this way of scaling CLE noise is being depricated.")
1652+
@warn "Passing noise scaling input into SDEProblem will be depricated. New standard is to declare one (or several) paraemter as noise scaling parameters when the ReactionSystem is created. Please read https://docs.sciml.ai/Catalyst/stable/catalyst_applications/advanced_simulations/#Scaling-the-noise-magnitude-in-the-chemical-Langevin-equations."
1653+
end
16491654
u0map = symmap_to_varmap(rs, u0)
16501655
pmap = symmap_to_varmap(rs, p)
16511656
sde_sys = convert(SDESystem, rs; noise_scaling, name, combinatoric_ratelaws,

test/model_simulation/simulate_SDEs.jl

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -121,29 +121,6 @@ end
121121

122122
### Noise Scaling ###
123123

124-
# Tests where a single noise scaling parameter is given directly to the SDEProblem.
125-
let
126-
noise_scaling_network = @reaction_network begin (k1, k2), X1 X2 end
127-
for repeat in 1:5
128-
p = 1.0 .+ rand(rng, 2)
129-
u0 = 10000 * (1.0 .+ rand(rng, 2))
130-
sol001 = solve(SDEProblem(noise_scaling_network, u0, (0.0, 1000.0), vcat(p, 0.01),
131-
noise_scaling = (@variables η1)[1]), ImplicitEM())
132-
sol01 = solve(SDEProblem(noise_scaling_network, u0, (0.0, 1000.0), vcat(p, 0.1),
133-
noise_scaling = (@variables η1)[1]), ImplicitEM())
134-
sol1 = solve(SDEProblem(noise_scaling_network, u0, (0.0, 1000.0), vcat(p, 1.0),
135-
noise_scaling = (@variables η2)[1]), ImplicitEM())
136-
sol10 = solve(SDEProblem(noise_scaling_network, u0, (0.0, 1000.0), vcat(p, 10.0),
137-
noise_scaling = (@variables η3)[1]), ImplicitEM())
138-
@test 2 * std(first.(sol001.u)[100:end]) < std(first.(sol01.u)[100:end])
139-
@test 2 * std(last.(sol001.u)[100:end]) < std(last.(sol01.u)[100:end])
140-
@test 2 * std(first.(sol01.u)[100:end]) < std(first.(sol1.u)[100:end])
141-
@test 2 * std(last.(sol01.u)[100:end]) < std(last.(sol1.u)[100:end])
142-
@test 2 * std(first.(sol1.u)[100:end]) < std(first.(sol10.u)[100:end])
143-
@test 2 * std(last.(sol1.u)[100:end]) < std(last.(sol10.u)[100:end])
144-
end
145-
end
146-
147124
# Tests with multiple noise scaling parameters directly in the macro.
148125
let
149126
noise_scaling_network_1 = @reaction_network begin
@@ -160,10 +137,10 @@ let
160137
@noise_scaling_parameters η[1:2]
161138
(k1, k2), X1 X2
162139
end
163-
@unpack η = noise_scaling_network_2
164-
sol_2_1 = solve(SDEProblem(noise_scaling_network_2, u0, (0.0, 1000.0), [:k1 => 2.0, :k2 => 0.66, η[1] => 2.0, η[2] => 2.0]), ImplicitEM(); saveat=1.0)
165-
sol_2_2 = solve(SDEProblem(noise_scaling_network_2, u0, (0.0, 1000.0), [:k1 => 2.0, :k2 => 0.66, η[1] => 2.0, η[2] => 0.2]), ImplicitEM(); saveat=1.0)
166-
sol_2_3 = solve(SDEProblem(noise_scaling_network_2, u0, (0.0, 1000.0), [:k1 => 2.0, :k2 => 0.66, η[1] => 0.2, η[2] => 0.2]), ImplicitEM(); saveat=1.0)
140+
@unpack k1, k2, η = noise_scaling_network_2
141+
sol_2_1 = solve(SDEProblem(noise_scaling_network_2, u0, (0.0, 1000.0), [k1 => 2.0, k2 => 0.66, η[1] => 2.0, η[2] => 2.0]), ImplicitEM(); saveat=1.0)
142+
sol_2_2 = solve(SDEProblem(noise_scaling_network_2, u0, (0.0, 1000.0), [k1 => 2.0, k2 => 0.66, η[1] => 2.0, η[2] => 0.2]), ImplicitEM(); saveat=1.0)
143+
sol_2_3 = solve(SDEProblem(noise_scaling_network_2, u0, (0.0, 1000.0), [k1 => 2.0, k2 => 0.66, η[1] => 0.2, η[2] => 0.2]), ImplicitEM(); saveat=1.0)
167144
@test var(sol_2_1[1,:]) > var(sol_2_2[1,:]) > var(sol_2_3[1,:])
168145
end
169146

@@ -175,8 +152,7 @@ let
175152
end
176153
u0 = [:X1 => 1100.0, :X2 => 3900.0]
177154
p = [:k1 => 2.0, :k2 => 0.5, =>0.0]
178-
ss = solve(SDEProblem(noise_scaling_network, u0, (0.0, 1000.0), p), ImplicitEM())[end]
179-
@test ss [1000.0, 4000.0]
155+
@test SDEProblem(noise_scaling_network, u0, (0.0, 1000.0), p)[] == 0.0
180156
end
181157

182158
# Complicated test with many combinations of options.
@@ -187,18 +163,16 @@ let
187163
(p, d), 0 X1
188164
(k1, k2), X1 X2
189165
end
190-
@unpack X1, η4, p = noise_scaling_network
191-
u0 = [X1 => 500.0, :X2 => 500.0]
192-
p = [p => 20.0, :d => 0.1, :η1 => 0.0, :η3 => 0.0, η4 => 0.0, :k1 => 2.0, :k2 => 2.0, :par1 => 1000.0, :par2 => 1000.0]
166+
u0 = [:X1 => 500.0, :X2 => 500.0]
167+
p = [:p => 20.0, :d => 0.1, :η1 => 0.0, :η3 => 0.0, :η4 => 0.0, :k1 => 2.0, :k2 => 2.0, :par1 => 1000.0, :par2 => 1000.0]
193168

194169
@test getdescription(parameters(noise_scaling_network)[2]) == "Parameter par1"
195170
@test getdescription(parameters(noise_scaling_network)[8]) == "Parameter η2"
196171

197-
ss = solve(SDEProblem(noise_scaling_network, u0, (0.0, 1000.0), p), ImplicitEM())[end]
198-
@test ss [200.0, 200.0]
172+
sprob = SDEProblem(noise_scaling_network, u0, (0.0, 1000.0), p)
173+
@test sprob[:η1] == sprob[:η2] == sprob[:η3] == sprob[:η4] == 0.0
199174
end
200175

201-
# Tests that nosie scaling wor
202176

203177
### Checks Simulations Don't Error ###
204178

test/model_simulation/u0_n_parameter_inputs.jl

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,4 @@ let
7171
@test discrete_probs[1].u0 == discrete_probs[i].u0
7272
end
7373
end
74-
end
75-
76-
# Tests uding mix of symbols and symbolics in input.
77-
let
78-
test_network = @reaction_network begin
79-
(p1, d1), 0 X1
80-
(p2, d2), 0 X2
81-
end
82-
@unpack p1, d1, p2, d2, X1, X2 = test_network
83-
u0_1 = [X1 => 0.7, X2 => 3.6]
84-
u0_2 = [:X1 => 0.7, X2 => 3.6]
85-
u0_3 = [:X1 => 0.7, :X2 => 3.6]
86-
p_1 = [p1 => 1.2, d1 => 4.0, p2 => 2.5, d2 =>0.1]
87-
p_2 = [:p1 => 1.2, d1 => 4.0, :p2 => 2.5, d2 =>0.1]
88-
p_3 = [:p1 => 1.2, :d1 => 4.0, :p2 => 2.5, :d2 =>0.1]
89-
90-
ss_base = solve(ODEProblem(test_network, u0_1, (0.0, 10.0), p_1), Tsit5())[end]
91-
for u0 in [u0_1, u0_2, u0_3], p in [p_1, p_2, p_3]
92-
@test ss_base == solve(ODEProblem(test_network, u0, (0.0, 10.0), p), Tsit5())[end]
93-
end
9474
end

0 commit comments

Comments
 (0)