46
46
# including non-species variables.
47
47
drop_dynamics (s) = isconstant (s) || isbc (s) || (! isspecies (s))
48
48
49
- function assemble_oderhs (rs, ispcs; combinatoric_ratelaws = true , remove_conserved = false )
49
+ function assemble_oderhs (rs, ispcs; combinatoric_ratelaws = true , remove_conserved = false ,
50
+ physical_scales = nothing )
50
51
nps = get_networkproperties (rs)
51
52
species_to_idx = Dict (x => i for (i, x) in enumerate (ispcs))
52
53
rhsvec = Any[0 for _ in ispcs]
@@ -56,7 +57,11 @@ function assemble_oderhs(rs, ispcs; combinatoric_ratelaws = true, remove_conserv
56
57
Dict ()
57
58
end
58
59
59
- for rx in get_rxs (rs)
60
+ for (rxidx,rx) in enumerate (get_rxs (rs))
61
+ # check this reaction should be treated as an ODE
62
+ ! ((physical_scales === nothing ) ||
63
+ (physical_scales[rxidx] == PhysicalScale. ODE)) && continue
64
+
60
65
rl = oderatelaw (rx; combinatoric_ratelaw = combinatoric_ratelaws)
61
66
remove_conserved && (rl = substitute (rl, depspec_submap))
62
67
for (spec, stoich) in rx. netstoich
@@ -90,8 +95,10 @@ function assemble_oderhs(rs, ispcs; combinatoric_ratelaws = true, remove_conserv
90
95
end
91
96
92
97
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)
98
+ include_zero_odes = true , remove_conserved = false , physical_scales = nothing )
99
+
100
+ rhsvec = assemble_oderhs (rs, ispcs; combinatoric_ratelaws, remove_conserved,
101
+ physical_scales)
95
102
if as_odes
96
103
D = Differential (get_iv (rs))
97
104
eqs = [Equation (D (x), rhs)
@@ -371,6 +378,9 @@ function assemble_jumps(rs; combinatoric_ratelaws = true, physical_scales = noth
371
378
372
379
rxvars = []
373
380
for (i, rx) in enumerate (rxs)
381
+ # only process reactions that should give jumps
382
+ (physcales[i] in jump_scales) || continue
383
+
374
384
empty! (rxvars)
375
385
(rx. rate isa Symbolic) && get_variables! (rxvars, rx. rate)
376
386
@@ -384,7 +394,7 @@ function assemble_jumps(rs; combinatoric_ratelaws = true, physical_scales = noth
384
394
# don't change species that are constant or BCs
385
395
(! drop_dynamics (spec)) && push! (affect, spec ~ spec + stoich)
386
396
end
387
- if isvrj
397
+ if isvrj
388
398
push! (veqs, VariableRateJump (rl, affect))
389
399
else
390
400
push! (ceqs, ConstantRateJump (rl, affect))
@@ -661,7 +671,7 @@ function Base.convert(::Type{<:SDESystem}, rs::ReactionSystem;
661
671
SDESystem (eqs, noiseeqs, get_iv (flatrs), us, ps;
662
672
observed = obs,
663
673
name,
664
- defaults = defs,
674
+ defaults = _merge (defaults, defs) ,
665
675
checks,
666
676
continuous_events = MT. get_continuous_events (flatrs),
667
677
discrete_events = MT. get_discrete_events (flatrs),
@@ -721,38 +731,51 @@ function Base.convert(::Type{<:JumpSystem}, rs::ReactionSystem; name = nameof(rs
721
731
combinatoric_ratelaws = get_combinatoric_ratelaws (rs),
722
732
remove_conserved = nothing , checks = false ,
723
733
default_u0 = Dict (), default_p = Dict (),
724
- defaults = _merge (Dict (default_u0), Dict (default_p)), physical_scales = nothing ,
734
+ defaults = _merge (Dict (default_u0), Dict (default_p)), include_zero_odes = true ,
735
+ physical_scales = nothing ,
725
736
kwargs... )
726
737
iscomplete (rs) || error (COMPLETENESS_ERROR)
727
738
spatial_convert_err (rs:: ReactionSystem , JumpSystem)
728
739
(remove_conserved != = nothing ) &&
729
740
throw (ArgumentError (" Catalyst does not support removing conserved species when converting to JumpSystems." ))
730
741
731
742
flatrs = Catalyst. flatten (rs)
732
- error_if_constraints (JumpSystem, flatrs)
733
743
734
- physical_scales = merge_physical_scales (reactions (rs ), physical_scales,
744
+ physical_scales = merge_physical_scales (reactions (flatrs ), physical_scales,
735
745
PhysicalScale. Jump)
736
746
admissible_scales = (PhysicalScale. ODE, PhysicalScale. Jump,
737
747
PhysicalScale. VariableRateJump)
738
748
unique_scales = unique (physical_scales)
739
749
(unique_scales ⊆ admissible_scales) ||
740
750
error (" Physical scales must currently be one of $admissible_scales for hybrid systems." )
741
- hasodes = (PhysicalScale. ODE in unique_scales) || has_nonreactions (flatrs)
742
751
752
+ # basic jump states and equations
743
753
eqs = assemble_jumps (flatrs; combinatoric_ratelaws, physical_scales)
754
+ ists, ispcs = get_indep_sts (flatrs)
755
+
756
+ # handle coupled ODEs and BC species
757
+ if (PhysicalScale. ODE in unique_scales) || has_nonreactions (flatrs)
758
+ odeeqs = assemble_drift (flatrs, ispcs; combinatoric_ratelaws,
759
+ remove_conserved = false , include_zero_odes, physical_scales)
760
+ append! (eqs, odeeqs)
761
+ eqs, us, ps, obs, defs = addconstraints! (eqs, flatrs, ists, ispcs;
762
+ remove_conserved = false )
763
+ else
764
+ any (isbc, get_unknowns (flatrs)) &&
765
+ (ists = vcat (ists, filter (isbc, get_unknowns (flatrs))))
766
+ us = ists
767
+ ps = get_ps (flatrs)
768
+ obs = MT. observed (flatrs)
769
+ defs = MT. defaults (flatrs)
770
+ end
744
771
745
- # handle BC species
746
- sts, ispcs = get_indep_sts (flatrs)
747
- any (isbc, get_unknowns (flatrs)) && (sts = vcat (sts, filter (isbc, get_unknowns (flatrs))))
748
- ps = get_ps (flatrs)
749
-
750
- JumpSystem (eqs, get_iv (flatrs), sts, ps;
751
- observed = MT. observed (flatrs),
772
+ JumpSystem (eqs, get_iv (flatrs), us, ps;
773
+ observed = obs,
752
774
name,
753
- defaults = _merge (defaults, MT . defaults (flatrs) ),
775
+ defaults = _merge (defaults, defs ),
754
776
checks,
755
777
discrete_events = MT. discrete_events (flatrs),
778
+ continuous_events = MT. continuous_events (flatrs),
756
779
kwargs... )
757
780
end
758
781
0 commit comments