From 1e97178800524dc71dae750cf9a5ebb7c9cf50f3 Mon Sep 17 00:00:00 2001 From: Torkel Date: Fri, 5 Apr 2024 21:30:26 -0400 Subject: [PATCH] up --- src/reaction_network.jl | 24 ++++++++++++++----- .../hybrid_equation_reaction_systems.jl | 19 +++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/reaction_network.jl b/src/reaction_network.jl index 5e44132e02..f0e814ab59 100644 --- a/src/reaction_network.jl +++ b/src/reaction_network.jl @@ -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 @@ -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 @@ -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 @@ -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. diff --git a/test/reactionsystem_structure/hybrid_equation_reaction_systems.jl b/test/reactionsystem_structure/hybrid_equation_reaction_systems.jl index bf4c53ead9..08979ea767 100644 --- a/test/reactionsystem_structure/hybrid_equation_reaction_systems.jl +++ b/test/reactionsystem_structure/hybrid_equation_reaction_systems.jl @@ -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"] @@ -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) @@ -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 @@ -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. @@ -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