Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
TorkelE committed Apr 10, 2024
1 parent 164b5d3 commit 1e97178
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
24 changes: 18 additions & 6 deletions src/reaction_network.jl
Original file line number Diff line number Diff line change
Expand Up @@ -834,11 +834,20 @@ function read_equations_options(options, variables_declared)
vars_extracted = []
add_default_diff = false
for eq in equations
((eq.head != :call) || (eq.args[1] != :~)) && error("Malformed equation: \"$eq\". Equation's left hand and right hand sides should be separated by a \"~\".")
(!(eq.args[2] isa Expr) || eq.args[2].head != :call) && continue
if (eq.args[2].args[1] == :D) && (eq.args[2].args[2] isa Symbol) && (length(eq.args[2].args) == 2)
diff_var = eq.args[2].args[2]
in(diff_var, forbidden_symbols_error) && error("A forbidden symbol ($(diff_var)) was used as an variable in this differential equation: $eq")
if (eq.head != :call) || (eq.args[1] != :~)
error("Malformed equation: \"$eq\". Equation's left hand and right hand sides should be separated by a \"~\".")
end

# Checks if the equation have the format D(X) ~ ... (where X is a symbol). This means that the
# default differential has been used. X is added as a declared variable to the system, and
# we make a note that a differential D = Differential(iv) should be made as well.
lhs = eq.args[2]
# if lhs: is an expression. Is a function call. The function's name is D. Calls a single symbol.
if (lhs isa Expr) && (lhs.head == :call) && (lhs.args[1] == :D) && (lhs.args[2] isa Symbol)
diff_var = lhs.args[2]
if in(diff_var, forbidden_symbols_error)
error("A forbidden symbol ($(diff_var)) was used as an variable in this differential equation: $eq")
end
add_default_diff = true
in(diff_var, variables_declared) || push!(vars_extracted, diff_var)
end
Expand Down Expand Up @@ -868,7 +877,7 @@ function create_differential_expr(options, add_default_diff, used_syms)
if add_default_diff && !any(diff_dec.args[1] == :D for diff_dec in diffexpr.args)
push!(diffexpr.args, :(D = Differential($(DEFAULT_IV_SYM))))
end
println(diffexpr)

return diffexpr
end

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

# Adds the correctly formatted event to the event creation expression.
push!(events_expr.args, arg)
end

return events_expr
end

Expand All @@ -910,6 +921,7 @@ function check_default_noise_scaling!(default_reaction_metadata, options)
end
end


### Functionality for expanding function call to custom and specific functions ###

#Recursively traverses an expression and replaces special function call like "hill(...)" with the actual corresponding expression.
Expand Down
19 changes: 15 additions & 4 deletions test/reactionsystem_structure/hybrid_equation_reaction_systems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ end
# Checks for both differential and algebraic equations.
# Checks for problems, integrators, and solutions yielded by hybrid systems.
# Checks that metadata, types, and default values are carried through correctly.
let
@test_broken let # SDEs are currently broken with structural simplify.
# Creates the model
@parameters a1 [description="Parameter a1"] a2::Rational{Int64} a3=0.3 a4::Rational{Int64}=4//10 [description="Parameter a4"]
@parameters b1 [description="Parameter b1"] b2::Int64 b3 = 3 b4::Int64=4 [description="Parameter b4"]
Expand Down Expand Up @@ -407,7 +407,7 @@ end

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

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


Expand Down Expand Up @@ -677,6 +677,17 @@ let
issetequal(unknowns(rs_2)[2:3], [rs_2.V, rs_2.N])
end

# Checks that variables that can be inferred from differential equations, but are also declared
# manually, have their additional inputs properly registered.
let
rs = @reaction_network begin
@variables V(t)=2.0 [description = "A variable"]
@equations D(V) ~ -1
end
@test getdefault(rs.V) == 2.0
@test getdescription(rs.V) == "A variable"
end

# Checks that equations can be formatted in various ways. Tries e.g. isolating a single number on
# either side of the equality.
# Checks that various weird function can be used within equations.
Expand Down Expand Up @@ -815,7 +826,7 @@ let
end

# Misformatted expression for a differential.
@reaction_network begin
@test_throws Exception @eval @reaction_network begin
@variables D
@differentials d ~ D
end
Expand Down

0 comments on commit 1e97178

Please sign in to comment.