Skip to content

Commit f2a8120

Browse files
committed
init
1 parent f624106 commit f2a8120

File tree

3 files changed

+69
-26
lines changed

3 files changed

+69
-26
lines changed

ext/CatalystHomotopyContinuationExtension/homotopy_continuation_extension.jl

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ end
4343

4444
# For a given reaction system, parameter values, and initial conditions, find the polynomial that HC solves to find steady states.
4545
function steady_state_polynomial(rs::ReactionSystem, ps, u0)
46-
ns = convert(NonlinearSystem, rs; remove_conserved = true)
46+
ns = convert(NonlinearSystem, rs; remove_conserved = true, expand_functions = true)
4747
pre_varmap = [symmap_to_varmap(rs,u0)..., symmap_to_varmap(rs,ps)...]
4848
conservationlaw_errorcheck(rs, pre_varmap)
4949
p_vals = ModelingToolkit.varmap_to_vars(pre_varmap, parameters(ns); defaults = ModelingToolkit.defaults(ns))
@@ -63,27 +63,6 @@ function conservationlaw_errorcheck(rs, pre_varmap)
6363
error("The system has conservation laws but initial conditions were not provided for some species.")
6464
end
6565

66-
# Unfolds a function (like mm or hill).
67-
function deregister(fs::Vector{T}, expr) where T
68-
for f in fs
69-
expr = deregister(f, expr)
70-
end
71-
return expr
72-
end
73-
# Provided by Shashi Gowda.
74-
deregister(f, expr) = wrap(Rewriters.Postwalk(Rewriters.PassThrough(___deregister(f)))(unwrap(expr)))
75-
function ___deregister(f)
76-
(expr) ->
77-
if istree(expr) && operation(expr) == f
78-
args = arguments(expr)
79-
invoke_with = map(args) do a
80-
t = typeof(a)
81-
issym(a) || istree(a) ? wrap(a) => symtype(a) : a => typeof(a)
82-
end
83-
invoke(f, Tuple{last.(invoke_with)...}, first.(invoke_with)...)
84-
end
85-
end
86-
8766
# Parses and expression and return a version where any exponents that are Float64 (but an int, like 2.0) are turned into Int64s.
8867
make_int_exps(expr) = wrap(Rewriters.Postwalk(Rewriters.PassThrough(___make_int_exps))(unwrap(expr))).val
8968
function ___make_int_exps(expr)
@@ -124,4 +103,27 @@ function filter_negative_f(sols; neg_thres=-1e-20)
124103
(neg_thres < sol[idx] < 0) && (sol[idx] = 0)
125104
end
126105
return filter(sol -> all(>=(0), sol), sols)
127-
end
106+
end
107+
108+
### Archived ###
109+
110+
# # Unfolds a function (like mm or hill).
111+
# function deregister(fs::Vector{T}, expr) where T
112+
# for f in fs
113+
# expr = deregister(f, expr)
114+
# end
115+
# return expr
116+
# end
117+
# # Provided by Shashi Gowda.
118+
# deregister(f, expr) = wrap(Rewriters.Postwalk(Rewriters.PassThrough(___deregister(f)))(unwrap(expr)))
119+
# function ___deregister(f)
120+
# (expr) ->
121+
# if istree(expr) && operation(expr) == f
122+
# args = arguments(expr)
123+
# invoke_with = map(args) do a
124+
# t = typeof(a)
125+
# issym(a) || istree(a) ? wrap(a) => symtype(a) : a => typeof(a)
126+
# end
127+
# invoke(f, Tuple{last.(invoke_with)...}, first.(invoke_with)...)
128+
# end
129+
# end

src/reactionsystem.jl

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,14 +1287,19 @@ Keyword args and default values:
12871287
function Base.convert(::Type{<:ODESystem}, rs::ReactionSystem; name = nameof(rs),
12881288
combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
12891289
include_zero_odes = true, remove_conserved = false, checks = false,
1290-
kwargs...)
1290+
expand_functions = false, kwargs...)
12911291
spatial_convert_err(rs::ReactionSystem, ODESystem)
12921292
fullrs = Catalyst.flatten(rs)
12931293
remove_conserved && conservationlaws(fullrs)
12941294
ists, ispcs = get_indep_sts(fullrs, remove_conserved)
12951295
eqs = assemble_drift(fullrs, ispcs; combinatoric_ratelaws, remove_conserved,
12961296
include_zero_odes)
12971297
eqs, sts, ps, obs, defs = addconstraints!(eqs, fullrs, ists, ispcs; remove_conserved)
1298+
1299+
# Converts expressions like mm(X,v,K) to v*X/(X+K).
1300+
expand_functions && for eq in eqs
1301+
eq.rhs = expand_registered_functions!(eq.rhs)
1302+
end
12981303

12991304
ODESystem(eqs, get_iv(fullrs), sts, ps;
13001305
observed = obs,
@@ -1326,7 +1331,7 @@ Keyword args and default values:
13261331
function Base.convert(::Type{<:NonlinearSystem}, rs::ReactionSystem; name = nameof(rs),
13271332
combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
13281333
include_zero_odes = true, remove_conserved = false, checks = false,
1329-
kwargs...)
1334+
expand_functions = false, kwargs...)
13301335
spatial_convert_err(rs::ReactionSystem, NonlinearSystem)
13311336
fullrs = Catalyst.flatten(rs)
13321337
remove_conserved && conservationlaws(fullrs)
@@ -1335,6 +1340,12 @@ function Base.convert(::Type{<:NonlinearSystem}, rs::ReactionSystem; name = name
13351340
as_odes = false, include_zero_odes)
13361341
error_if_constraint_odes(NonlinearSystem, fullrs)
13371342
eqs, sts, ps, obs, defs = addconstraints!(eqs, fullrs, ists, ispcs; remove_conserved)
1343+
1344+
# Converts expressions like mm(X,v,K) to v*X/(X+K).
1345+
expand_functions && for eq in eqs
1346+
eq.rhs = expand_registered_functions!(eq.rhs)
1347+
end
1348+
13381349
NonlinearSystem(eqs, sts, ps;
13391350
name,
13401351
observed = obs,
@@ -1374,7 +1385,7 @@ function Base.convert(::Type{<:SDESystem}, rs::ReactionSystem;
13741385
noise_scaling = nothing, name = nameof(rs),
13751386
combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
13761387
include_zero_odes = true, checks = false, remove_conserved = false,
1377-
kwargs...)
1388+
expand_functions = false, kwargs...)
13781389
spatial_convert_err(rs::ReactionSystem, SDESystem)
13791390

13801391
flatrs = Catalyst.flatten(rs)
@@ -1400,6 +1411,11 @@ function Base.convert(::Type{<:SDESystem}, rs::ReactionSystem;
14001411
eqs, sts, ps, obs, defs = addconstraints!(eqs, flatrs, ists, ispcs; remove_conserved)
14011412
ps = (noise_scaling === nothing) ? ps : vcat(ps, toparam(noise_scaling))
14021413

1414+
# Converts expressions like mm(X,v,K) to v*X/(X+K).
1415+
expand_functions && for eq in eqs
1416+
eq.rhs = expand_registered_functions!(eq.rhs)
1417+
end
1418+
14031419
if any(isbc, get_states(flatrs))
14041420
@info "Boundary condition species detected. As constraint equations are not currently supported when converting to SDESystems, the resulting system will be undetermined. Consider using constant species instead."
14051421
end

src/registered_functions.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,28 @@ function Symbolics.derivative(::typeof(hillar), args::NTuple{5, Any}, ::Val{5})
107107
(args[4]^args[5]) * log(args[4])) /
108108
(args[1]^args[5] + args[2]^args[5] + args[4]^args[5])^2
109109
end
110+
111+
"""
112+
expand_registered_functions!(expr)
113+
114+
Takes an expression, and expands registered function expressions. E.g. `mm(X,v,K)` is replaced with v*X/(X+K). Currently supported functions: `mm`, `mmr`, `hill`, `hillr`, and `hill`.
115+
"""
116+
function expand_registered_functions!(expr)
117+
istree(expr) || return expr
118+
args = arguments(expr)
119+
if operation(expr) == Catalyst.mm
120+
return args[2]*args[1]/(args[1] + args[3])
121+
elseif operation(expr) == Catalyst.mmr
122+
return args[2]*args[3]/(args[1] + args[3])
123+
elseif operation(expr) == Catalyst.hill
124+
return args[2]*(args[1]^args[4])/((args[1])^args[4] + (args[3])^args[4])
125+
elseif operation(expr) == Catalyst.hillr
126+
return args[2]*(args[3]^args[4])/((args[1])^args[4] + (args[3])^args[4])
127+
elseif operation(expr) == Catalyst.hillar
128+
return args[3]*(args[1]^args[5])/((args[1])^args[5] + (args[2])^args[5] + (args[4])^args[5])
129+
end
130+
for i = 1:length(args)
131+
args[i] = expand_registered_functions!(args[i])
132+
end
133+
return expr
134+
end

0 commit comments

Comments
 (0)