Skip to content

Commit 1e97178

Browse files
committed
up
1 parent 164b5d3 commit 1e97178

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/reaction_network.jl

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -834,11 +834,20 @@ function read_equations_options(options, variables_declared)
834834
vars_extracted = []
835835
add_default_diff = false
836836
for eq in equations
837-
((eq.head != :call) || (eq.args[1] != :~)) && error("Malformed equation: \"$eq\". Equation's left hand and right hand sides should be separated by a \"~\".")
838-
(!(eq.args[2] isa Expr) || eq.args[2].head != :call) && continue
839-
if (eq.args[2].args[1] == :D) && (eq.args[2].args[2] isa Symbol) && (length(eq.args[2].args) == 2)
840-
diff_var = eq.args[2].args[2]
841-
in(diff_var, forbidden_symbols_error) && error("A forbidden symbol ($(diff_var)) was used as an variable in this differential equation: $eq")
837+
if (eq.head != :call) || (eq.args[1] != :~)
838+
error("Malformed equation: \"$eq\". Equation's left hand and right hand sides should be separated by a \"~\".")
839+
end
840+
841+
# Checks if the equation have the format D(X) ~ ... (where X is a symbol). This means that the
842+
# default differential has been used. X is added as a declared variable to the system, and
843+
# we make a note that a differential D = Differential(iv) should be made as well.
844+
lhs = eq.args[2]
845+
# if lhs: is an expression. Is a function call. The function's name is D. Calls a single symbol.
846+
if (lhs isa Expr) && (lhs.head == :call) && (lhs.args[1] == :D) && (lhs.args[2] isa Symbol)
847+
diff_var = lhs.args[2]
848+
if in(diff_var, forbidden_symbols_error)
849+
error("A forbidden symbol ($(diff_var)) was used as an variable in this differential equation: $eq")
850+
end
842851
add_default_diff = true
843852
in(diff_var, variables_declared) || push!(vars_extracted, diff_var)
844853
end
@@ -868,7 +877,7 @@ function create_differential_expr(options, add_default_diff, used_syms)
868877
if add_default_diff && !any(diff_dec.args[1] == :D for diff_dec in diffexpr.args)
869878
push!(diffexpr.args, :(D = Differential($(DEFAULT_IV_SYM))))
870879
end
871-
println(diffexpr)
880+
872881
return diffexpr
873882
end
874883

@@ -894,8 +903,10 @@ function read_events_option(options, event_type::Symbol)
894903
error("The affect part of all events (the righ-hand side) must be a vector. This is not the case for: $(arg).")
895904
end
896905

906+
# Adds the correctly formatted event to the event creation expression.
897907
push!(events_expr.args, arg)
898908
end
909+
899910
return events_expr
900911
end
901912

@@ -910,6 +921,7 @@ function check_default_noise_scaling!(default_reaction_metadata, options)
910921
end
911922
end
912923

924+
913925
### Functionality for expanding function call to custom and specific functions ###
914926

915927
#Recursively traverses an expression and replaces special function call like "hill(...)" with the actual corresponding expression.

test/reactionsystem_structure/hybrid_equation_reaction_systems.jl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ end
246246
# Checks for both differential and algebraic equations.
247247
# Checks for problems, integrators, and solutions yielded by hybrid systems.
248248
# Checks that metadata, types, and default values are carried through correctly.
249-
let
249+
@test_broken let # SDEs are currently broken with structural simplify.
250250
# Creates the model
251251
@parameters a1 [description="Parameter a1"] a2::Rational{Int64} a3=0.3 a4::Rational{Int64}=4//10 [description="Parameter a4"]
252252
@parameters b1 [description="Parameter b1"] b2::Int64 b3 = 3 b4::Int64=4 [description="Parameter b4"]
@@ -407,7 +407,7 @@ end
407407

408408
# Checks that a hybrid SDE + algebraic equations works.
409409
# Checks that structural_simplify is required to simulate hybrid SDE + algebraic equations.
410-
let
410+
@test_broken let # SDEs are currently broken with structural simplify.
411411
# Creates hybrid reactions system.
412412
@parameters p d k1 k2
413413
@species X(t)
@@ -535,7 +535,7 @@ let
535535

536536
# Checks that the simulations are identical.
537537
# Some internal details will be different, however, the solutions should be identical.
538-
osol_messy[[:S, :I, :R, :M, :H]] osol_ordered[[:S, :I, :R, :M, :H]]
538+
@test osol_messy[[:S, :I, :R, :M, :H]] osol_ordered[[:S, :I, :R, :M, :H]]
539539
end
540540

541541

@@ -677,6 +677,17 @@ let
677677
issetequal(unknowns(rs_2)[2:3], [rs_2.V, rs_2.N])
678678
end
679679

680+
# Checks that variables that can be inferred from differential equations, but are also declared
681+
# manually, have their additional inputs properly registered.
682+
let
683+
rs = @reaction_network begin
684+
@variables V(t)=2.0 [description = "A variable"]
685+
@equations D(V) ~ -1
686+
end
687+
@test getdefault(rs.V) == 2.0
688+
@test getdescription(rs.V) == "A variable"
689+
end
690+
680691
# Checks that equations can be formatted in various ways. Tries e.g. isolating a single number on
681692
# either side of the equality.
682693
# Checks that various weird function can be used within equations.
@@ -815,7 +826,7 @@ let
815826
end
816827

817828
# Misformatted expression for a differential.
818-
@reaction_network begin
829+
@test_throws Exception @eval @reaction_network begin
819830
@variables D
820831
@differentials d ~ D
821832
end

0 commit comments

Comments
 (0)