@@ -22,9 +22,10 @@ Notes:
22
22
`combinatoric_ratelaw=false` then the ratelaw is `k*S^2`, i.e. the scaling
23
23
factor is ignored.
24
24
"""
25
- function oderatelaw (rx; combinatoric_ratelaw = true )
25
+ function oderatelaw (rx; combinatoric_ratelaw = true , expand_catalyst_funs = true )
26
26
@unpack rate, substrates, substoich, only_use_rate = rx
27
27
rl = rate
28
+ expand_catalyst_funs && (rl = expand_registered_functions (rl))
28
29
29
30
# if the stoichiometric coefficients are not integers error if asking to scale rates
30
31
! all (s -> s isa Union{Integer, Symbolic}, substoich) &&
46
47
# including non-species variables.
47
48
drop_dynamics (s) = isconstant (s) || isbc (s) || (! isspecies (s))
48
49
49
- function assemble_oderhs (rs, ispcs; combinatoric_ratelaws = true , remove_conserved = false )
50
+ function assemble_oderhs (rs, ispcs; combinatoric_ratelaws = true ,
51
+ remove_conserved = false , expand_catalyst_funs = true )
50
52
nps = get_networkproperties (rs)
51
53
species_to_idx = Dict (x => i for (i, x) in enumerate (ispcs))
52
54
rhsvec = Any[0 for _ in ispcs]
@@ -57,7 +59,8 @@ function assemble_oderhs(rs, ispcs; combinatoric_ratelaws = true, remove_conserv
57
59
end
58
60
59
61
for rx in get_rxs (rs)
60
- rl = oderatelaw (rx; combinatoric_ratelaw = combinatoric_ratelaws)
62
+ rl = oderatelaw (rx; combinatoric_ratelaw = combinatoric_ratelaws,
63
+ expand_catalyst_funs)
61
64
remove_conserved && (rl = substitute (rl, depspec_submap))
62
65
for (spec, stoich) in rx. netstoich
63
66
# dependent species don't get an ODE, so are skipped
@@ -90,8 +93,9 @@ function assemble_oderhs(rs, ispcs; combinatoric_ratelaws = true, remove_conserv
90
93
end
91
94
92
95
function assemble_drift (rs, ispcs; combinatoric_ratelaws = true , as_odes = true ,
93
- include_zero_odes = true , remove_conserved = false )
94
- rhsvec = assemble_oderhs (rs, ispcs; combinatoric_ratelaws, remove_conserved)
96
+ include_zero_odes = true , remove_conserved = false , expand_catalyst_funs = true )
97
+ rhsvec = assemble_oderhs (rs, ispcs; combinatoric_ratelaws, remove_conserved,
98
+ expand_catalyst_funs)
95
99
if as_odes
96
100
D = Differential (get_iv (rs))
97
101
eqs = [Equation (D (x), rhs)
104
108
105
109
# this doesn't work with constraint equations currently
106
110
function assemble_diffusion (rs, sts, ispcs; combinatoric_ratelaws = true ,
107
- remove_conserved = false )
111
+ remove_conserved = false , expand_catalyst_funs = true )
108
112
# as BC species should ultimately get an equation, we include them in the noise matrix
109
113
num_bcsts = count (isbc, get_unknowns (rs))
110
114
@@ -120,7 +124,9 @@ function assemble_diffusion(rs, sts, ispcs; combinatoric_ratelaws = true,
120
124
end
121
125
122
126
for (j, rx) in enumerate (get_rxs (rs))
123
- rlsqrt = sqrt (abs (oderatelaw (rx; combinatoric_ratelaw = combinatoric_ratelaws)))
127
+ rl = oderatelaw (rx; combinatoric_ratelaw = combinatoric_ratelaws,
128
+ expand_catalyst_funs)
129
+ rlsqrt = sqrt (abs (rl))
124
130
hasnoisescaling (rx) && (rlsqrt *= getnoisescaling (rx))
125
131
remove_conserved && (rlsqrt = substitute (rlsqrt, depspec_submap))
126
132
@@ -169,9 +175,12 @@ Notes:
169
175
the ratelaw is `k*S*(S-1)`, i.e. the rate law is not normalized by the scaling
170
176
factor.
171
177
"""
172
- function jumpratelaw (rx; combinatoric_ratelaw = true )
178
+ function jumpratelaw (rx; combinatoric_ratelaw = true , expand_catalyst_funs = true )
173
179
@unpack rate, substrates, substoich, only_use_rate = rx
180
+
174
181
rl = rate
182
+ expand_catalyst_funs && (rl = expand_registered_functions (rl))
183
+
175
184
if ! only_use_rate
176
185
coef = eltype (substoich) <: Number ? one (eltype (substoich)) : 1
177
186
for (i, stoich) in enumerate (substoich)
@@ -313,7 +322,7 @@ function get_depgraph(rs)
313
322
eqeq_dependencies (jdeps, vdeps). fadjlist
314
323
end
315
324
316
- function assemble_jumps (rs; combinatoric_ratelaws = true )
325
+ function assemble_jumps (rs; combinatoric_ratelaws = true , expand_catalyst_funs = true )
317
326
meqs = MassActionJump[]
318
327
ceqs = ConstantRateJump[]
319
328
veqs = VariableRateJump[]
@@ -361,7 +370,8 @@ function assemble_jumps(rs; combinatoric_ratelaws = true)
361
370
if (! isvrj) && ismassaction (rx, rs; rxvars, haveivdep = false , unknownset)
362
371
push! (meqs, makemajump (rx; combinatoric_ratelaw = combinatoric_ratelaws))
363
372
else
364
- rl = jumpratelaw (rx; combinatoric_ratelaw = combinatoric_ratelaws)
373
+ rl = jumpratelaw (rx; combinatoric_ratelaw = combinatoric_ratelaws,
374
+ expand_catalyst_funs)
365
375
affect = Vector {Equation} ()
366
376
for (spec, stoich) in rx. netstoich
367
377
# don't change species that are constant or BCs
@@ -481,12 +491,15 @@ Keyword args and default values:
481
491
- `remove_conserved=false`, if set to `true` will calculate conservation laws of the
482
492
underlying set of reactions (ignoring constraint equations), and then apply them to reduce
483
493
the number of equations.
494
+ - `expand_catalyst_funs = true`, replaces Catalyst defined functions like `hill(A,B,C,D)`
495
+ with their rational function representation when converting to another system type. Set to
496
+ `false`` to disable.
484
497
"""
485
498
function Base. convert (:: Type{<:ODESystem} , rs:: ReactionSystem ; name = nameof (rs),
486
499
combinatoric_ratelaws = get_combinatoric_ratelaws (rs),
487
- include_zero_odes = true , remove_conserved = false ,
488
- checks = false , default_u0 = Dict (), default_p = Dict (),
489
- defaults = _merge (Dict (default_u0), Dict (default_p)),
500
+ include_zero_odes = true , remove_conserved = false , checks = false ,
501
+ default_u0 = Dict (), default_p = Dict (),
502
+ defaults = _merge (Dict (default_u0), Dict (default_p)), expand_catalyst_funs = true ,
490
503
kwargs... )
491
504
# Error checks.
492
505
iscomplete (rs) || error (COMPLETENESS_ERROR)
@@ -496,7 +509,7 @@ function Base.convert(::Type{<:ODESystem}, rs::ReactionSystem; name = nameof(rs)
496
509
remove_conserved && conservationlaws (fullrs)
497
510
ists, ispcs = get_indep_sts (fullrs, remove_conserved)
498
511
eqs = assemble_drift (fullrs, ispcs; combinatoric_ratelaws, remove_conserved,
499
- include_zero_odes)
512
+ include_zero_odes, expand_catalyst_funs )
500
513
eqs, us, ps, obs, defs = addconstraints! (eqs, fullrs, ists, ispcs; remove_conserved)
501
514
502
515
ODESystem (eqs, get_iv (fullrs), us, ps;
@@ -547,13 +560,16 @@ Keyword args and default values:
547
560
conservation laws. See the [FAQ
548
561
entry](https://docs.sciml.ai/Catalyst/stable/faqs/#faq_remake_nonlinprob) for more
549
562
details.
563
+ - `expand_catalyst_funs = true`, replaces Catalyst defined functions like `hill(A,B,C,D)`
564
+ with their rational function representation when converting to another system type. Set to
565
+ `false`` to disable.
550
566
"""
551
567
function Base. convert (:: Type{<:NonlinearSystem} , rs:: ReactionSystem ; name = nameof (rs),
552
568
combinatoric_ratelaws = get_combinatoric_ratelaws (rs),
553
569
remove_conserved = false , conseqs_remake_warn = true , checks = false ,
554
570
default_u0 = Dict (), default_p = Dict (),
555
571
defaults = _merge (Dict (default_u0), Dict (default_p)),
556
- all_differentials_permitted = false , kwargs... )
572
+ all_differentials_permitted = false , expand_catalyst_funs = true , kwargs... )
557
573
# Error checks.
558
574
iscomplete (rs) || error (COMPLETENESS_ERROR)
559
575
spatial_convert_err (rs:: ReactionSystem , NonlinearSystem)
@@ -565,7 +581,7 @@ function Base.convert(::Type{<:NonlinearSystem}, rs::ReactionSystem; name = name
565
581
remove_conserved && conservationlaws (fullrs)
566
582
ists, ispcs = get_indep_sts (fullrs, remove_conserved)
567
583
eqs = assemble_drift (fullrs, ispcs; combinatoric_ratelaws, remove_conserved,
568
- as_odes = false , include_zero_odes = false )
584
+ as_odes = false , include_zero_odes = false , expand_catalyst_funs )
569
585
eqs, us, ps, obs, defs, initeqs = addconstraints! (eqs, fullrs, ists, ispcs;
570
586
remove_conserved, treat_conserved_as_eqs = true )
571
587
@@ -627,11 +643,16 @@ Notes:
627
643
- `remove_conserved=false`, if set to `true` will calculate conservation laws of the
628
644
underlying set of reactions (ignoring constraint equations), and then apply them to reduce
629
645
the number of equations.
646
+ - `expand_catalyst_funs = true`, replaces Catalyst defined functions like `hill(A,B,C,D)`
647
+ with their rational function representation when converting to another system type. Set to
648
+ `false`` to disable.
630
649
"""
631
650
function Base. convert (:: Type{<:SDESystem} , rs:: ReactionSystem ;
632
651
name = nameof (rs), combinatoric_ratelaws = get_combinatoric_ratelaws (rs),
633
652
include_zero_odes = true , checks = false , remove_conserved = false ,
634
- default_u0 = Dict (), default_p = Dict (), defaults = _merge (Dict (default_u0), Dict (default_p)),
653
+ default_u0 = Dict (), default_p = Dict (),
654
+ defaults = _merge (Dict (default_u0), Dict (default_p)),
655
+ expand_catalyst_funs = true ,
635
656
kwargs... )
636
657
# Error checks.
637
658
iscomplete (rs) || error (COMPLETENESS_ERROR)
@@ -642,9 +663,9 @@ function Base.convert(::Type{<:SDESystem}, rs::ReactionSystem;
642
663
remove_conserved && conservationlaws (flatrs)
643
664
ists, ispcs = get_indep_sts (flatrs, remove_conserved)
644
665
eqs = assemble_drift (flatrs, ispcs; combinatoric_ratelaws, include_zero_odes,
645
- remove_conserved)
646
- noiseeqs = assemble_diffusion (flatrs, ists, ispcs;
647
- combinatoric_ratelaws, remove_conserved )
666
+ remove_conserved, expand_catalyst_funs )
667
+ noiseeqs = assemble_diffusion (flatrs, ists, ispcs; combinatoric_ratelaws,
668
+ remove_conserved, expand_catalyst_funs )
648
669
eqs, us, ps, obs, defs = addconstraints! (eqs, flatrs, ists, ispcs; remove_conserved)
649
670
650
671
if any (isbc, get_unknowns (flatrs))
@@ -678,12 +699,14 @@ Notes:
678
699
differential equations.
679
700
- Does not currently support continuous events as these are not supported by
680
701
`ModelingToolkit.JumpSystems`.
702
+ - `expand_catalyst_funs = true`, replaces Catalyst defined functions like `hill(A,B,C,D)`
703
+ with their rational function representation when converting to another system type. Set to
704
+ `false`` to disable.
681
705
"""
682
706
function Base. convert (:: Type{<:JumpSystem} , rs:: ReactionSystem ; name = nameof (rs),
683
707
combinatoric_ratelaws = get_combinatoric_ratelaws (rs),
684
- remove_conserved = nothing , checks = false ,
685
- default_u0 = Dict (), default_p = Dict (),
686
- defaults = _merge (Dict (default_u0), Dict (default_p)),
708
+ remove_conserved = nothing , checks = false , default_u0 = Dict (), default_p = Dict (),
709
+ defaults = _merge (Dict (default_u0), Dict (default_p)), expand_catalyst_funs = true ,
687
710
kwargs... )
688
711
iscomplete (rs) || error (COMPLETENESS_ERROR)
689
712
spatial_convert_err (rs:: ReactionSystem , JumpSystem)
@@ -696,7 +719,7 @@ function Base.convert(::Type{<:JumpSystem}, rs::ReactionSystem; name = nameof(rs
696
719
(length (MT. continuous_events (flatrs)) > 0 ) &&
697
720
(@warn " continuous_events will be dropped as they are not currently supported by JumpSystems." )
698
721
699
- eqs = assemble_jumps (flatrs; combinatoric_ratelaws)
722
+ eqs = assemble_jumps (flatrs; combinatoric_ratelaws, expand_catalyst_funs )
700
723
701
724
# handle BC species
702
725
sts, ispcs = get_indep_sts (flatrs)
@@ -720,9 +743,9 @@ function DiffEqBase.ODEProblem(rs::ReactionSystem, u0, tspan,
720
743
check_length = false , name = nameof (rs),
721
744
combinatoric_ratelaws = get_combinatoric_ratelaws (rs),
722
745
include_zero_odes = true , remove_conserved = false , checks = false ,
723
- structural_simplify = false , kwargs... )
746
+ expand_catalyst_funs = true , structural_simplify = false , kwargs... )
724
747
osys = convert (ODESystem, rs; name, combinatoric_ratelaws, include_zero_odes, checks,
725
- remove_conserved)
748
+ remove_conserved, expand_catalyst_funs )
726
749
727
750
# Handles potential differential algebraic equations (which requires `structural_simplify`).
728
751
if structural_simplify
@@ -764,14 +787,19 @@ Keyword args and default values:
764
787
conservation laws. See the [FAQ
765
788
entry](https://docs.sciml.ai/Catalyst/stable/faqs/#faq_remake_nonlinprob) for more
766
789
details.
790
+ - `expand_catalyst_funs = true`, replaces Catalyst defined functions like `hill(A,B,C,D)`
791
+ with their rational function representation when converting to another system type. Set to
792
+ `false`` to disable.
767
793
"""
768
794
function DiffEqBase. NonlinearProblem (rs:: ReactionSystem , u0,
769
795
p = DiffEqBase. NullParameters (), args... ;
770
796
name = nameof (rs), combinatoric_ratelaws = get_combinatoric_ratelaws (rs),
771
- remove_conserved = false , conseqs_remake_warn = true , checks = false , check_length = false ,
797
+ remove_conserved = false , conseqs_remake_warn = true , checks = false ,
798
+ check_length = false , expand_catalyst_funs = true ,
772
799
structural_simplify = false , all_differentials_permitted = false , kwargs... )
773
800
nlsys = convert (NonlinearSystem, rs; name, combinatoric_ratelaws, checks,
774
- all_differentials_permitted, remove_conserved, conseqs_remake_warn)
801
+ all_differentials_permitted, remove_conserved, conseqs_remake_warn,
802
+ expand_catalyst_funs)
775
803
nlsys = structural_simplify ? MT. structural_simplify (nlsys) : complete (nlsys)
776
804
return NonlinearProblem (nlsys, u0, p, args... ; check_length,
777
805
kwargs... )
781
809
function DiffEqBase. SDEProblem (rs:: ReactionSystem , u0, tspan,
782
810
p = DiffEqBase. NullParameters (), args... ;
783
811
name = nameof (rs), combinatoric_ratelaws = get_combinatoric_ratelaws (rs),
784
- include_zero_odes = true , checks = false , check_length = false , remove_conserved = false ,
785
- structural_simplify = false , kwargs... )
786
- sde_sys = convert (SDESystem, rs; name, combinatoric_ratelaws,
812
+ include_zero_odes = true , checks = false , check_length = false ,
813
+ remove_conserved = false , structural_simplify = false ,
814
+ expand_catalyst_funs = true , kwargs... )
815
+ sde_sys = convert (SDESystem, rs; name, combinatoric_ratelaws, expand_catalyst_funs,
787
816
include_zero_odes, checks, remove_conserved)
788
817
789
818
# Handles potential differential algebraic equations (which requires `structural_simplify`).
@@ -848,8 +877,9 @@ plot(sol, idxs = :A)
848
877
"""
849
878
function JumpInputs (rs:: ReactionSystem , u0, tspan, p = DiffEqBase. NullParameters ();
850
879
name = nameof (rs), combinatoric_ratelaws = get_combinatoric_ratelaws (rs),
851
- checks = false , kwargs... )
852
- jsys = complete (convert (JumpSystem, rs; name, combinatoric_ratelaws, checks))
880
+ expand_catalyst_funs = true , checks = false , kwargs... )
881
+ jsys = complete (convert (JumpSystem, rs; name, combinatoric_ratelaws,
882
+ expand_catalyst_funs, checks))
853
883
if MT. has_variableratejumps (jsys)
854
884
prob = ODEProblem (jsys, u0, tspan, p; kwargs... )
855
885
else
@@ -873,11 +903,11 @@ end
873
903
874
904
# DiscreteProblem from AbstractReactionNetwork
875
905
function DiffEqBase. DiscreteProblem (rs:: ReactionSystem , u0, tspan:: Tuple ,
876
- p = DiffEqBase. NullParameters (), args... ;
877
- name = nameof (rs),
878
- combinatoric_ratelaws = get_combinatoric_ratelaws (rs),
879
- checks = false , kwargs ... )
880
- jsys = convert (JumpSystem, rs; name, combinatoric_ratelaws, checks )
906
+ p = DiffEqBase. NullParameters (), args... ; name = nameof (rs),
907
+ combinatoric_ratelaws = get_combinatoric_ratelaws (rs), checks = false ,
908
+ expand_catalyst_funs = true , kwargs ... )
909
+ jsys = convert (JumpSystem, rs; name, combinatoric_ratelaws, checks,
910
+ expand_catalyst_funs )
881
911
jsys = complete (jsys)
882
912
return DiscreteProblem (jsys, u0, tspan, p, args... ; kwargs... )
883
913
end
886
916
function JumpProcesses. JumpProblem (rs:: ReactionSystem , prob,
887
917
aggregator = JumpProcesses. NullAggregator (); name = nameof (rs),
888
918
combinatoric_ratelaws = get_combinatoric_ratelaws (rs),
889
- checks = false , kwargs... )
890
- jsys = convert (JumpSystem, rs; name, combinatoric_ratelaws, checks)
919
+ expand_catalyst_funs = true , checks = false , kwargs... )
920
+ jsys = convert (JumpSystem, rs; name, combinatoric_ratelaws,
921
+ expand_catalyst_funs, checks)
891
922
jsys = complete (jsys)
892
923
return JumpProblem (jsys, prob, aggregator; kwargs... )
893
924
end
@@ -905,9 +936,9 @@ function DiffEqBase.SteadyStateProblem(rs::ReactionSystem, u0,
905
936
check_length = false , name = nameof (rs),
906
937
combinatoric_ratelaws = get_combinatoric_ratelaws (rs),
907
938
remove_conserved = false , include_zero_odes = true , checks = false ,
908
- structural_simplify = false , kwargs... )
939
+ expand_catalyst_funs = true , structural_simplify = false , kwargs... )
909
940
osys = convert (ODESystem, rs; name, combinatoric_ratelaws, include_zero_odes, checks,
910
- remove_conserved)
941
+ remove_conserved, expand_catalyst_funs )
911
942
912
943
# Handles potential differential algebraic equations (which requires `structural_simplify`).
913
944
if structural_simplify
0 commit comments