Skip to content

Commit 46af9a2

Browse files
committed
updates
1 parent 063ed25 commit 46af9a2

File tree

11 files changed

+267
-176
lines changed

11 files changed

+267
-176
lines changed

ext/CatalystHomotopyContinuationExtension/homotopy_continuation_extension.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function filter_negative_f(sols; neg_thres=-1e-20)
107107
return filter(sol -> all(>=(0), sol), sols)
108108
end
109109

110-
# Sometimes (when polynomials are created from hybrid CRN/DAEs), the steady state polynomial have the wrong type.
110+
# Sometimes (when polynomials are created from coupled CRN/DAEs), the steady state polynomial have the wrong type.
111111
# This converts it to the correct type, which homotopy continuation can handle.
112112
const WRONG_POLY_TYPE = Vector{DynamicPolynomials.Polynomial{DynamicPolynomials.Commutative{DynamicPolynomials.CreationOrder}, DynamicPolynomials.Graded{DynamicPolynomials.LexOrder}}}
113113
const CORRECT_POLY_TYPE = Vector{DynamicPolynomials.Polynomial{DynamicPolynomials.Commutative{DynamicPolynomials.CreationOrder}, DynamicPolynomials.Graded{DynamicPolynomials.LexOrder}, Float64}}

src/Catalyst.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ using ModelingToolkit: Symbolic, value, istree, get_unknowns, get_ps, get_iv, ge
3030

3131
import ModelingToolkit: get_variables, namespace_expr, namespace_equation, get_variables!,
3232
modified_unknowns!, validate, namespace_variables,
33-
namespace_parameters, rename, renamespace, getname, flatten
33+
namespace_parameters, rename, renamespace, getname, flatten,
34+
is_alg_equation, is_diff_equation
3435

3536
# internal but needed ModelingToolkit functions
3637
import ModelingToolkit: check_variables,

src/reaction_network.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
401401
parameters = vcat(parameters_declared, parameters_extracted)
402402

403403
# Create differential expression.
404-
diffexpr = create_differential_expr(options, add_default_diff, [species; parameters; variables])
404+
diffexpr = create_differential_expr(options, add_default_diff, [species; parameters; variables], tiv)
405405

406406
# Checks for input errors.
407407
(sum(length.([reaction_lines, option_lines])) != length(ex.args)) &&
@@ -414,7 +414,7 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
414414

415415
# Creates expressions corresponding to actual code from the internal DSL representation.
416416
sexprs = get_sexpr(species_extracted, options; iv_symbols = ivs)
417-
vexprs = get_sexpr(vars_extracted, options, :variables)
417+
vexprs = get_sexpr(vars_extracted, options, :variables; iv_symbols = ivs)
418418
pexprs = get_pexpr(parameters_extracted, options)
419419
ps, pssym = scalarize_macro(!isempty(parameters), pexprs, "ps")
420420
vars, varssym = scalarize_macro(!isempty(variables), vexprs, "vars")
@@ -852,12 +852,13 @@ function read_equations_options(options, variables_declared)
852852
in(diff_var, variables_declared) || push!(vars_extracted, diff_var)
853853
end
854854
end
855-
855+
856856
return vars_extracted, add_default_diff, equations
857857
end
858858

859-
# Creates an expression declaring differentials.
860-
function create_differential_expr(options, add_default_diff, used_syms)
859+
# Creates an expression declaring differentials. Here, `tiv` is the time independent variables,
860+
# which is used by the default differential (if it is used).
861+
function create_differential_expr(options, add_default_diff, used_syms, tiv)
861862
# Creates the differential expression.
862863
# If differentials was provided as options, this is used as the initial expression.
863864
# If the default differential (D(...)) was used in equations, this is added to the expression.
@@ -875,7 +876,7 @@ function create_differential_expr(options, add_default_diff, used_syms)
875876
# If the default differential D has been used, but not pre-declared using the @differenitals
876877
# options, add this declaration to the list of declared differentials.
877878
if add_default_diff && !any(diff_dec.args[1] == :D for diff_dec in diffexpr.args)
878-
push!(diffexpr.args, :(D = Differential($(DEFAULT_IV_SYM))))
879+
push!(diffexpr.args, :(D = Differential($(tiv))))
879880
end
880881

881882
return diffexpr

src/reactionsystem.jl

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ end
312312
### Reaction Acessor Functions ###
313313

314314
# Overwrites functions in ModelingToolkit to give the correct input.
315-
is_diff_equation(rx::Reaction) = false
316-
is_alg_equation(rx::Reaction) = false
315+
ModelingToolkit.is_diff_equation(rx::Reaction) = false
316+
ModelingToolkit.is_alg_equation(rx::Reaction) = false
317317

318318
################################## Reaction Complexes ####################################
319319

@@ -725,6 +725,11 @@ function findvars!(ps, us, exprtosearch, ivs, vars)
725725
end
726726
empty!(vars)
727727
end
728+
# Special dispatch for equations, applied `findvars!` to left-hand and right-hand sides.
729+
function findvars!(ps, us, eq_to_search::Equation, ivs, vars)
730+
findvars!(ps, us, eq_to_search.lhs, ivs, vars)
731+
findvars!(ps, us, eq_to_search.lhs, ivs, vars)
732+
end
728733

729734
# Called internally (whether DSL-based or programmtic model creation is used).
730735
# Creates a sorted reactions + equations vector, also ensuring reaction is first in this vector.
@@ -813,22 +818,14 @@ function find_event_vars!(ps, us, event, ivs, vars)
813818
# If not, it is a vector of conditions and we must check each.
814819
if conds isa Vector
815820
for cond in conds
816-
# For continious events the conditions are equations (with lhs and rhs).
817-
# For discrete events, they are single expressions.
818-
if cond isa Equation
819-
findvars!(ps, us, cond.lhs, ivs, vars)
820-
findvars!(ps, us, cond.rhs, ivs, vars)
821-
else
822-
findvars!(ps, us, cond, ivs, vars)
823-
end
821+
findvars!(ps, us, cond, ivs, vars)
824822
end
825823
else
826824
findvars!(ps, us, conds, ivs, vars)
827825
end
828826
# The affects is always a vector of equations. Here, we handle the lhs and rhs separately.
829827
for affect in affects
830-
findvars!(ps, us, affect.lhs, ivs, vars)
831-
findvars!(ps, us, affect.rhs, ivs, vars)
828+
findvars!(ps, us, cond, ivs, vars)
832829
end
833830
end
834831
"""
@@ -1616,6 +1613,9 @@ function Base.convert(::Type{<:NonlinearSystem}, rs::ReactionSystem; name = name
16161613
as_odes = false, include_zero_odes)
16171614
eqs, us, ps, obs, defs = addconstraints!(eqs, fullrs, ists, ispcs; remove_conserved)
16181615

1616+
# Throws a warning if there are differential equations in non-standard format.
1617+
# Next, sets all differential terms to `0`.
1618+
nonlinear_convert_differentials_check(eqs, nonspecies(rs), get_iv(rs))
16191619
eqs = [remove_diffs(eq.lhs) ~ remove_diffs(eq.rhs) for eq in eqs]
16201620

16211621

@@ -1637,7 +1637,32 @@ function remove_diffs(expr)
16371637
end
16381638
diff_2_zero(expr) = (Symbolics.is_derivative(expr) ? 0.0 : expr)
16391639

1640-
1640+
# Ideally, when `ReactionSystem`s are converted to `NonlinearSystem`s, any coupled ODEs should be
1641+
# on the form D(X) ~ ..., where lhs is the time derivative w.r.t. a single variable, and the rhs
1642+
# does not contain any differentials. If this is not teh case, we throw a warning to let the user
1643+
# know that they should be careful.
1644+
function nonlinear_convert_differentials_check(eqs, vars, iv)
1645+
for eq in eqs
1646+
# For each equation (that is not a reaction), checks in order:
1647+
# If there is a differential on the right hand side.
1648+
# If the lhs is not on the form D(...).
1649+
# If the lhs upper level function is not a differential w.r.t. time.
1650+
# If the contenct of the differential is not a variable (and nothing more).
1651+
# If either of this is a case, throws the warning.
1652+
(eq isa Reaction) && continue
1653+
if Symbolics._occursin(Symbolics.is_derivative, eq.rhs) ||
1654+
!Symbolics.istree(eq.lhs) ||
1655+
!isequal(Symbolics.operation(eq.lhs), Differential(iv)) ||
1656+
(length(arguments(eq.lhs)) != 1) ||
1657+
!any(isequal(arguments(eq.lhs)[1]), vars)
1658+
@warn "You are attempting to convert a `ReactionSystem` coupled with differential equations. However, some of these differentials are not of the form `D(x) ~ ...` where:
1659+
(1) The left-hand side is a differential of a single variable with respect to the time independent variable, and
1660+
(2) The right-hand side does not contain any differentials.
1661+
This is permitted (and all differential will be set to `0`), however, it is recommended to proceed with caution to ensure that the produced nonlinear equation is the desired one."
1662+
return
1663+
end
1664+
end
1665+
end
16411666

16421667
"""
16431668
```julia
@@ -1820,7 +1845,7 @@ function DiffEqBase.DiscreteProblem(rs::ReactionSystem, u0, tspan::Tuple,
18201845
return DiscreteProblem(jsys, u0map, tspan, pmap, args...; kwargs...)
18211846
end
18221847

1823-
# JumpProblem from AbstractReactionNetworkg
1848+
# JumpProblem from AbstractReactionNetwork
18241849
function JumpProcesses.JumpProblem(rs::ReactionSystem, prob, aggregator, args...;
18251850
name = nameof(rs),
18261851
combinatoric_ratelaws = get_combinatoric_ratelaws(rs),

test/dsl/dsl_options.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ let
687687
end
688688

689689

690-
### Hybrid CRN/Equations Models ###
690+
### Coupled CRN/Equations Models ###
691691

692692
# Checks creation of basic network.
693693
# Check indexing of output solution.

test/extensions/bifurcation_kit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ end
180180

181181
### Other Tests ###
182182

183-
# Checks that bifurcation diagrams can be computed for hybrid CRN/DAE systems.
183+
# Checks that bifurcation diagrams can be computed for coupled CRN/DAE systems.
184184
let
185185
# Prepares the model (production/degradation of X, with equations for volume and X concentration).
186186
rs = @reaction_network begin

test/extensions/homotopy_continuation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ end
109109

110110
### Other Tests ###
111111

112-
# Tests that homotopy continuation can be applied to hybrid DAE/CRN systems.
112+
# Tests that homotopy continuation can be applied to coupled DAE/CRN systems.
113113
let
114114
# Prepares the model (production/degradation of X, with equations for volume and X concentration).
115115
rs = @reaction_network begin

test/extensions/structural_identifiability.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ end
312312

313313
### Other Tests ###
314314

315-
# Checks that identifiability can be assessed for hybrid CRN/DAE systems.
315+
# Checks that identifiability can be assessed for coupled CRN/DAE systems.
316316
let
317317
rs = @reaction_network begin
318318
@parameters k c1 c2

test/programmatic_model_creation/component_based_model_creation.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,6 @@ let
363363
rn2 = extend(osys, rn)
364364
rn2 = complete(rn2)
365365
rnodes = complete(convert(ODESystem, rn2))
366-
# @test_throws ErrorException convert(NonlinearSystem, rn2)
367366

368367
# Ensure right number of equations are generated.
369368
@variables G(t)

0 commit comments

Comments
 (0)