Skip to content

Commit 8a719d4

Browse files
committed
up
1 parent 1ca47a2 commit 8a719d4

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

src/Catalyst.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ import ModelingToolkit: check_variables,
3838
check_parameters, _iszero, _merge, check_units,
3939
get_unit, check_equations, iscomplete
4040

41-
# Event-related MTK stuff.
42-
import ModelingToolkit: PresetTimeCallback
43-
4441
import Base: (==), hash, size, getindex, setindex, isless, Sort.defalg, length, show
4542
import MacroTools, Graphs
4643
import Graphs: DiGraph, SimpleGraph, SimpleDiGraph, vertices, edges, add_vertices!, nv, ne

src/reaction_network.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ function read_equations_options(options, variables_declared)
831831
# Loops through all equations, checks for lhs of the form `D(X) ~ ...`.
832832
# When this is the case, the variable X and differential D are extracted (for automatic declaration).
833833
# Also performs simple error checks.
834-
vars_extracted = []
834+
vars_extracted = Vector{Symbol}()
835835
add_default_diff = false
836836
for eq in equations
837837
if (eq.head != :call) || (eq.args[1] != :~)

src/reactionsystem.jl

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,7 +1596,7 @@ function Base.convert(::Type{<:NonlinearSystem}, rs::ReactionSystem; name = name
15961596
combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
15971597
include_zero_odes = true, remove_conserved = false, checks = false,
15981598
default_u0 = Dict(), default_p = Dict(), defaults = _merge(Dict(default_u0), Dict(default_p)),
1599-
kwargs...)
1599+
all_differentials_permitted = false, kwargs...)
16001600
iscomplete(rs) || error(COMPLETENESS_ERROR)
16011601
spatial_convert_err(rs::ReactionSystem, NonlinearSystem)
16021602
fullrs = Catalyst.flatten(rs)
@@ -1608,7 +1608,7 @@ function Base.convert(::Type{<:NonlinearSystem}, rs::ReactionSystem; name = name
16081608

16091609
# Throws a warning if there are differential equations in non-standard format.
16101610
# Next, sets all differential terms to `0`.
1611-
nonlinear_convert_differentials_check(rs)
1611+
all_differentials_permitted || nonlinear_convert_differentials_check(rs)
16121612
eqs = [remove_diffs(eq.lhs) ~ remove_diffs(eq.rhs) for eq in eqs]
16131613

16141614

@@ -1635,24 +1635,26 @@ diff_2_zero(expr) = (Symbolics.is_derivative(expr) ? 0.0 : expr)
16351635
# does not contain any differentials. If this is not teh case, we throw a warning to let the user
16361636
# know that they should be careful.
16371637
function nonlinear_convert_differentials_check(rs::ReactionSystem)
1638-
for eq in equations(rs)
1639-
# For each equation (that is not a reaction), checks in order:
1638+
for eq in filter(is_diff_equation, equations(rs))
1639+
# For each differential equation, checks in order:
16401640
# If there is a differential on the right hand side.
16411641
# If the lhs is not on the form D(...).
16421642
# If the lhs upper level function is not a differential w.r.t. time.
16431643
# If the contenct of the differential is not a variable (and nothing more).
16441644
# If either of this is a case, throws the warning.
1645-
(eq isa Reaction) && continue
16461645
if Symbolics._occursin(Symbolics.is_derivative, eq.rhs) ||
16471646
!Symbolics.istree(eq.lhs) ||
16481647
!isequal(Symbolics.operation(eq.lhs), Differential(get_iv(rs))) ||
16491648
(length(arguments(eq.lhs)) != 1) ||
16501649
!any(isequal(arguments(eq.lhs)[1]), nonspecies(rs))
1651-
@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:
1650+
error("You are attempting to convert a `ReactionSystem` coupled with differential equations to a `NonlinearSystem`. However, some of these differentials are not of the form `D(x) ~ ...` where:
16521651
(1) The left-hand side is a differential of a single variable with respect to the time independent variable, and
16531652
(2) The right-hand side does not contain any differentials.
1654-
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."
1655-
return
1653+
This is generally not permitted.
1654+
1655+
If you still would like to perform this conversions, please use the `all_differentials_permitted = true` option. In this case, all differential will be set to `0`.
1656+
However, it is recommended to proceed with caution to ensure that the produced nonlinear equation makes sense for you intended application."
1657+
)
16561658
end
16571659
end
16581660
end
@@ -1790,13 +1792,14 @@ function DiffEqBase.NonlinearProblem(rs::ReactionSystem, u0,
17901792
name = nameof(rs), include_zero_odes = true,
17911793
combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
17921794
remove_conserved = false, checks = false,
1793-
check_length = false, kwargs...)
1795+
check_length = false, all_differentials_permitted = false, kwargs...)
17941796
u0map = symmap_to_varmap(rs, u0)
17951797
pmap = symmap_to_varmap(rs, p)
17961798
nlsys = convert(NonlinearSystem, rs; name, combinatoric_ratelaws, include_zero_odes,
1797-
checks, remove_conserved)
1799+
checks, all_differentials_permitted, remove_conserved)
17981800
nlsys = complete(nlsys)
1799-
return NonlinearProblem(nlsys, u0map, pmap, args...; check_length, kwargs...)
1801+
return NonlinearProblem(nlsys, u0map, pmap, args...; check_length,
1802+
kwargs...)
18001803
end
18011804

18021805
# SDEProblem from AbstractReactionNetwork

test/reactionsystem_structure/coupled_equation_reaction_systems.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -437,48 +437,49 @@ end
437437

438438
### Coupled NonlinearSystems Tests ###
439439

440-
# Checks that systems with weird differential equations yield appropriate warnings.
440+
# Checks that systems with weird differential equations yield errors.
441441
let
442-
# This oen is normal, and should not yield a warning.
442+
# This one is normal, and should not yield an error.
443443
begin
444444
rs = @reaction_network begin
445445
@equations D(V) ~ 1.0 - V
446446
end
447447
@test_nowarn convert(NonlinearSystem, rs)
448448
end
449449

450-
# Higher-order differential on the lhs, should yield a warning.
450+
# Higher-order differential on the lhs, should yield an error.
451451
begin
452452
rs = @reaction_network begin
453453
@differentials D = Differential(t)
454454
@variables V(t)
455455
@equations D(D(V)) ~ 1.0 - V
456456
(p,d), 0 <--> X
457457
end
458-
@test_logs (:warn, r".*") convert(NonlinearSystem, rs)
458+
@test_throws Exception convert(NonlinearSystem, rs)
459459
end
460460

461-
# Differential on the rhs, should yield a warning.
461+
# Differential on the rhs, should yield an error.
462462
begin
463463
rs = @reaction_network begin
464464
@variables U(t)
465465
@equations D(V) ~ 1.0 - V + D(U)
466466
(p,d), 0 <--> X
467467
end
468-
@test_logs (:warn, r".*") convert(NonlinearSystem, rs)
468+
@test_throws Exception convert(NonlinearSystem, rs)
469469
end
470470

471-
# Non-differential term on the lhs, should yield a warning.
471+
# Non-differential term on the lhs, should yield an error.
472472
begin
473473
rs = @reaction_network begin
474474
@differentials D = Differential(t)
475475
@variables V(t)
476476
@equations D(V) + V ~ 1.0 - V
477477
(p,d), 0 <--> X
478478
end
479-
@test_logs (:warn, r".*") convert(NonlinearSystem, rs)
479+
@test_throws Exception convert(NonlinearSystem, rs)
480480
end
481481
end
482+
482483
### Unusual Differentials Tests ###
483484

484485
# Tests that coupled CRN/DAEs with higher order differentials can be created.
@@ -522,7 +523,7 @@ let
522523
# Here `B => 0.1` has to be provided as well (and it shouldn't for the 2nd order ODE), hence the
523524
# separate `u0` declaration.
524525
u0 = [X => 1.0, A => 2.0, D(A) => 1.0, B => 0.1]
525-
nlprob = NonlinearProblem(coupled_rs, u0, ps; structural_simplify = true)
526+
nlprob = NonlinearProblem(coupled_rs, u0, ps; structural_simplify = true, all_differentials_permitted = true)
526527
nlsol = solve(nlprob)
527528
@test nlsol[X][end] 2.0
528529
@test nlsol[A][end] 0.0

0 commit comments

Comments
 (0)