Skip to content

Commit

Permalink
add observables support, and a couple tests for ode_system_from_amr (#67
Browse files Browse the repository at this point in the history
)
  • Loading branch information
JeffBezanson committed Jul 13, 2023
1 parent 6e22c11 commit 46078d3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/SimulationService.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import InteractiveUtils: subtypes
import JobSchedulers
import JSON3
import MathML
import ModelingToolkit: @parameters, substitute, Differential, Num, @variables, ODESystem, ODEProblem
import ModelingToolkit: @parameters, substitute, Differential, Num, @variables, ODESystem, ODEProblem, structural_simplify
import OpenAPI
import Oxygen
import SciMLBase: SciMLBase, DiscreteCallback, solve
Expand Down Expand Up @@ -147,6 +147,11 @@ function ode_system_from_amr(obj::Config)
statenames = [Symbol(s.id) for s in model.states]
statevars = [only(@variables $s) for s in statenames]
statefuncs = [only(@variables $s(t)) for s in statenames]
obsnames = [Symbol(o.id) for o in ode.observables]
obsvars = [only(@variables $o) for o in obsnames]
obsfuncs = [only(@variables $o(t)) for o in obsnames]
allvars = [statevars; obsvars]
allfuncs = [statefuncs; obsfuncs]

# get parameter values and state initial values
paramnames = [Symbol(x.id) for x in ode.parameters]
Expand All @@ -171,10 +176,15 @@ function ode_system_from_amr(obj::Config)
end
end

subst = Dict(statevars .=> statefuncs)
subst = merge!(Dict(allvars .=> allfuncs), Dict(paramvars .=> paramvars))
eqs = [D(statef) ~ substitute(eqs[state], subst) for (state, statef) in (statenames .=> statefuncs)]

ODESystem(eqs, t, statefuncs, paramvars; defaults = [statefuncs .=> initial_vals; sym_defs], name=Symbol(obj.name))
for (o, ofunc) in zip(ode.observables, obsfuncs)
expr = substitute(MathML.parse_str(o.expression_mathml), subst)
push!(eqs, ofunc ~ expr)
end

structural_simplify(ODESystem(eqs, t, allfuncs, paramvars; defaults = [statefuncs .=> initial_vals; sym_defs], name=Symbol(obj.name)))
end

#-----------------------------------------------------------------------------# health: GET /
Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ using HTTP
using JSON3
using Oxygen
using SciMLBase: solve
using ModelingToolkit

using SimulationService

SimulationService.SIMSERVICE_ENABLE_TDS = false

#-----------------------------------------------------------------------------# AMR parsing
@testset "AMR parsing" begin
file = joinpath(@__DIR__, "..", "examples", "BIOMD0000000955_askenet.json")
amr = JSON3.read(read(file), Config)
sys = SimulationService.ode_system_from_amr(amr)
@test string.(states(sys)) == ["Susceptible(t)", "Diagnosed(t)", "Infected(t)", "Ailing(t)", "Recognized(t)", "Healed(t)", "Threatened(t)", "Extinct(t)"]
@test string.(parameters(sys)) == ["beta", "gamma", "delta", "alpha", "epsilon", "zeta", "lambda", "eta", "rho", "theta", "kappa", "mu", "nu", "xi", "tau", "sigma"]
@test map(x->string(x.lhs), observed(sys)) == ["Cases(t)", "Hospitalizations(t)", "Deaths(t)"]
end

#-----------------------------------------------------------------------------# Operations
@testset "Operations" begin
@testset "simulate" begin
Expand Down

0 comments on commit 46078d3

Please sign in to comment.