@@ -1615,6 +1615,7 @@ function Base.convert(::Type{<:NonlinearSystem}, rs::ReactionSystem; name = name
1615
1615
eqs = assemble_drift (fullrs, ispcs; combinatoric_ratelaws, remove_conserved,
1616
1616
as_odes = false , include_zero_odes)
1617
1617
eqs, sts, ps, obs, defs = addconstraints! (eqs, fullrs, ists, ispcs; remove_conserved)
1618
+ eqs = [remove_diffs (eq. lhs) ~ remove_diffs (eq. rhs) for eq in eqs]
1618
1619
1619
1620
NonlinearSystem (eqs, sts, ps;
1620
1621
name,
@@ -1624,6 +1625,15 @@ function Base.convert(::Type{<:NonlinearSystem}, rs::ReactionSystem; name = name
1624
1625
kwargs... )
1625
1626
end
1626
1627
1628
+ # Finds and differentials in an expression, and sets these to 0.
1629
+ function remove_diffs (expr)
1630
+ (expr isa Number) && (return expr)
1631
+ return Symbolics. replace (expr, diff_2_zero)
1632
+ end
1633
+ diff_2_zero (expr) = (Symbolics. is_derivative (expr) ? 0.0 : expr)
1634
+
1635
+
1636
+
1627
1637
"""
1628
1638
```julia
1629
1639
Base.convert(::Type{<:SDESystem},rs::ReactionSystem)
@@ -1652,7 +1662,7 @@ function Base.convert(::Type{<:SDESystem}, rs::ReactionSystem;
1652
1662
spatial_convert_err (rs:: ReactionSystem , SDESystem)
1653
1663
1654
1664
flatrs = Catalyst. flatten (rs)
1655
- error_if_constraints (SDESystem, flatrs)
1665
+ # error_if_constraints(SDESystem, flatrs)
1656
1666
1657
1667
remove_conserved && conservationlaws (flatrs)
1658
1668
ists, ispcs = get_indep_sts (flatrs, remove_conserved)
@@ -1740,7 +1750,7 @@ function DiffEqBase.ODEProblem(rs::ReactionSystem, u0, tspan,
1740
1750
osys = convert (ODESystem, rs; name, combinatoric_ratelaws, include_zero_odes, checks,
1741
1751
remove_conserved)
1742
1752
1743
- # Handles potential Differential algebraic equations.
1753
+ # Handles potential differential algebraic equations (which requires `structural_simplify`) .
1744
1754
if structural_simplify
1745
1755
(osys = MT. structural_simplify (osys))
1746
1756
elseif has_alg_equations (rs)
@@ -1772,13 +1782,22 @@ function DiffEqBase.SDEProblem(rs::ReactionSystem, u0, tspan,
1772
1782
p = DiffEqBase. NullParameters (), args... ;
1773
1783
name = nameof (rs), combinatoric_ratelaws = get_combinatoric_ratelaws (rs),
1774
1784
include_zero_odes = true , checks = false , check_length = false ,
1775
- remove_conserved = false , kwargs... )
1785
+ remove_conserved = false , structural_simplify = false , kwargs... )
1776
1786
1777
1787
u0map = symmap_to_varmap (rs, u0)
1778
1788
pmap = symmap_to_varmap (rs, p)
1779
1789
sde_sys = convert (SDESystem, rs; name, combinatoric_ratelaws,
1780
1790
include_zero_odes, checks, remove_conserved)
1781
- sde_sys = complete (sde_sys)
1791
+
1792
+ # Handles potential differential algebraic equations (which requires `structural_simplify`).
1793
+ if structural_simplify
1794
+ (sde_sys = MT. structural_simplify (sde_sys))
1795
+ elseif has_alg_equations (rs)
1796
+ error (" The input ReactionSystem has algebraic equations. This requires setting `structural_simplify=true` within `ODEProblem` call." )
1797
+ else
1798
+ sde_sys = complete (sde_sys)
1799
+ end
1800
+
1782
1801
p_matrix = zeros (length (get_unknowns (sde_sys)), numreactions (rs))
1783
1802
return SDEProblem (sde_sys, u0map, tspan, pmap, args... ; check_length,
1784
1803
noise_rate_prototype = p_matrix, kwargs... )
@@ -1813,12 +1832,21 @@ function DiffEqBase.SteadyStateProblem(rs::ReactionSystem, u0,
1813
1832
check_length = false , name = nameof (rs),
1814
1833
combinatoric_ratelaws = get_combinatoric_ratelaws (rs),
1815
1834
remove_conserved = false , include_zero_odes = true ,
1816
- checks = false , kwargs... )
1835
+ checks = false , structural_simplify = false , kwargs... )
1817
1836
u0map = symmap_to_varmap (rs, u0)
1818
1837
pmap = symmap_to_varmap (rs, p)
1819
1838
osys = convert (ODESystem, rs; name, combinatoric_ratelaws, include_zero_odes, checks,
1820
1839
remove_conserved)
1821
- osys = complete (osys)
1840
+
1841
+ # Handles potential differential algebraic equations (which requires `structural_simplify`).
1842
+ if structural_simplify
1843
+ (osys = MT. structural_simplify (osys))
1844
+ elseif has_alg_equations (rs)
1845
+ error (" The input ReactionSystem has algebraic equations. This requires setting `structural_simplify=true` within `ODEProblem` call." )
1846
+ else
1847
+ osys = complete (osys)
1848
+ end
1849
+
1822
1850
return SteadyStateProblem (osys, u0map, pmap, args... ; check_length, kwargs... )
1823
1851
end
1824
1852
0 commit comments