Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Right hand side derivative #359

Closed
MaAl13 opened this issue Sep 27, 2024 · 4 comments
Closed

Right hand side derivative #359

MaAl13 opened this issue Sep 27, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@MaAl13
Copy link

MaAl13 commented Sep 27, 2024

Hello, i am trying to use your amazing software for my ODE system. However if i end up using it i get the error that a derivative was detected on the right hand side. My original system is not rational, so i followed advice from you for an old Issue here on github: #219 . This resulted in the following code:

using StructuralIdentifiability
using OrderedCollections

# Define the ODE model with variables and parameters 
ode = @ODEmodel begin
    # Define the state derivatives
    a'(t) = p_a * s_0 * nb(t) - d_a * pa1(t),
    b'(t) = p_b * pa2(t) - d_b * pb(t),
    nb'(t) = -nb(t) * n_ba * ((p_b * pa2(t) - d_b * pb(t)) / b(t)) * (1 - nb(t)),
    pa1'(t) = pa1(t) * n_a * ((p_a * s_0 * nb(t) - d_a * pa1(t)) / a(t)) * (1 - pa1(t)),
    pa2'(t) = pa2(t) * n_ab * ((p_a * s_0 * nb(t) - d_a * pa1(t)) / a(t)) * (1 - pa2(t)),
    pb'(t) = pb(t) * n_b * ((p_b * pa2(t) - d_b * pb(t)) / b(t)) * (1 - pb(t)),
    
    # Define the outputs
    y1(t) = a(t),  # First measurable output
    y2(t) = b(t)   # Second measurable output
end

# Specify the known initial conditions with variables 
known_initial_conditions = [a, b]

global_identifiability_results = assess_identifiability(
    ode,
    known_ic = known_initial_conditions
)

# Display the identifiability results
println("\nGlobal Identifiability Assessment Results:")
println("----------------------------------------")
for (key, value) in global_identifiability_results
    println("$key => $value")
end

And here is the error:
ERROR: LoadError: ArgumentError: Derivative are not allowed in the right-hand side
Stacktrace:
[1] (::StructuralIdentifiability.var"#676#677"{Bool, Symbol, Set{Any}, Set{Any}, Set{Symbol}})(x::Expr)
@ StructuralIdentifiability ~/.julia/packages/StructuralIdentifiability/rZ9cm/src/input_macro.jl:7
[2] walk
@ ~/.julia/packages/MacroTools/Cf2ok/src/utils.jl:135 [inlined]
[3] postwalk
@ ~/.julia/packages/MacroTools/Cf2ok/src/utils.jl:145 [inlined]
[4] (::MacroTools.var"#25#26"{StructuralIdentifiability.var"#676#677"{Bool, Symbol, Set{Any}, Set{Any}, Set{Symbol}}})(x::Expr)
@ MacroTools ~/.julia/packages/MacroTools/Cf2ok/src/utils.jl:145
[5] iterate(::Base.Generator{Vector{Any}, MacroTools.var"#25#26"{StructuralIdentifiability.var"#676#677"{Bool, Symbol, Set{Any}, Set{Any}, Set{Symbol}}}})
@ Base ./generator.jl:47
[6] _collect(c::Vector{Any}, itr::Base.Generator{Vector{Any}, MacroTools.var"#25#26"{StructuralIdentifiability.var"#676#677"{Bool, Symbol, Set{Any}, Set{Any}, Set{Symbol}}}}, ::Base.EltypeUnknown, isz::Base.HasShape{1})
@ Base ./array.jl:854
[7] collect_similar(cont::Vector{Any}, itr::Base.Generator{Vector{Any}, MacroTools.var"#25#26"{StructuralIdentifiability.var"#676#677"{Bool, Symbol, Set{Any}, Set{Any}, Set{Symbol}}}})
@ Base ./array.jl:763
[8] map(f::Function, A::Vector{Any})
@ Base ./abstractarray.jl:3285
[9] walk
@ ~/.julia/packages/MacroTools/Cf2ok/src/utils.jl:135 [inlined]
[10] postwalk
@ ~/.julia/packages/MacroTools/Cf2ok/src/utils.jl:145 [inlined]
[11] (::MacroTools.var"#25#26"{StructuralIdentifiability.var"#676#677"{Bool, Symbol, Set{Any}, Set{Any}, Set{Symbol}}})(x::Expr)
@ MacroTools ~/.julia/packages/MacroTools/Cf2ok/src/utils.jl:145
[12] iterate
@ ./generator.jl:47 [inlined]
[13] collect_to!(dest::Vector{LineNumberNode}, itr::Base.Generator{Vector{Any}, MacroTools.var"#25#26"{StructuralIdentifiability.var"#676#677"{Bool, Symbol, Set{Any}, Set{Any}, Set{Symbol}}}}, offs::Int64, st::Int64)
@ Base ./array.jl:892
[14] collect_to_with_first!(dest::Vector{LineNumberNode}, v1::LineNumberNode, itr::Base.Generator{Vector{Any}, MacroTools.var"#25#26"{StructuralIdentifiability.var"#676#677"{Bool, Symbol, Set{Any}, Set{Any}, Set{Symbol}}}}, st::Int64)
@ Base ./array.jl:870
[15] _collect(c::Vector{Any}, itr::Base.Generator{Vector{Any}, MacroTools.var"#25#26"{StructuralIdentifiability.var"#676#677"{Bool, Symbol, Set{Any}, Set{Any}, Set{Symbol}}}}, ::Base.EltypeUnknown, isz::Base.HasShape{1})
@ Base ./array.jl:864
[16] collect_similar(cont::Vector{Any}, itr::Base.Generator{Vector{Any}, MacroTools.var"#25#26"{StructuralIdentifiability.var"#676#677"{Bool, Symbol, Set{Any}, Set{Any}, Set{Symbol}}}})
@ Base ./array.jl:763
[17] map(f::Function, A::Vector{Any})
@ Base ./abstractarray.jl:3285
[18] walk
@ ~/.julia/packages/MacroTools/Cf2ok/src/utils.jl:135 [inlined]
[19] postwalk
@ ~/.julia/packages/MacroTools/Cf2ok/src/utils.jl:145 [inlined]
[20] _extract_aux!(funcs::Set{Any}, all_symb::Set{Any}, eq::Expr; ders_ok::Bool, type::Symbol)
@ StructuralIdentifiability ~/.julia/packages/StructuralIdentifiability/rZ9cm/src/input_macro.jl:3
[21] _extract_aux!
@ ~/.julia/packages/StructuralIdentifiability/rZ9cm/src/input_macro.jl:1 [inlined]
[22] macrohelper_extract_vars(equations::Vector{Expr}, type::Symbol)
@ StructuralIdentifiability ~/.julia/packages/StructuralIdentifiability/rZ9cm/src/input_macro.jl:67
[23] generate_model_code(::Symbol, ::Expr, ::Vararg{Expr})
@ StructuralIdentifiability ~/.julia/packages/StructuralIdentifiability/rZ9cm/src/input_macro.jl:105
[24] var"@ODEmodel"(source::LineNumberNode, module::Module, ex::Vararg{Expr})
@ StructuralIdentifiability ~/.julia/packages/StructuralIdentifiability/rZ9cm/src/input_macro.jl:270
in expression starting at /Users/malmansto/Documents/Timer/Structural_id.jl:49
in expression starting at /Users/malmansto/Documents/Timer/Structural_id.jl:49

@MaAl13 MaAl13 added the bug Something isn't working label Sep 27, 2024
@pogudingleb
Copy link
Collaborator

Hi Marius,

Thanks for reporting! I think that the reason for this behaviour is that @ODEmodel macro has slightly different syntax, it expects parenthesis afterwards, not begin ... end. So if you replace

ode = @ODEmodel begin
    # Define the state derivatives
    a'(t) = p_a * s_0 * nb(t) - d_a * pa1(t),
    b'(t) = p_b * pa2(t) - d_b * pb(t),
    nb'(t) = -nb(t) * n_ba * ((p_b * pa2(t) - d_b * pb(t)) / b(t)) * (1 - nb(t)),
    pa1'(t) = pa1(t) * n_a * ((p_a * s_0 * nb(t) - d_a * pa1(t)) / a(t)) * (1 - pa1(t)),
    pa2'(t) = pa2(t) * n_ab * ((p_a * s_0 * nb(t) - d_a * pa1(t)) / a(t)) * (1 - pa2(t)),
    pb'(t) = pb(t) * n_b * ((p_b * pa2(t) - d_b * pb(t)) / b(t)) * (1 - pb(t)),
    
    # Define the outputs
    y1(t) = a(t),  # First measurable output
    y2(t) = b(t)   # Second measurable output
end

with

ode = @ODEmodel(
    # Define the state derivatives
    a'(t) = p_a * s_0 * nb(t) - d_a * pa1(t),
    b'(t) = p_b * pa2(t) - d_b * pb(t),
    nb'(t) = -nb(t) * n_ba * ((p_b * pa2(t) - d_b * pb(t)) / b(t)) * (1 - nb(t)),
    pa1'(t) = pa1(t) * n_a * ((p_a * s_0 * nb(t) - d_a * pa1(t)) / a(t)) * (1 - pa1(t)),
    pa2'(t) = pa2(t) * n_ab * ((p_a * s_0 * nb(t) - d_a * pa1(t)) / a(t)) * (1 - pa2(t)),
    pb'(t) = pb(t) * n_b * ((p_b * pa2(t) - d_b * pb(t)) / b(t)) * (1 - pb(t)),
    
    # Define the outputs
    y1(t) = a(t),  # First measurable output
    y2(t) = b(t)   # Second measurable output
)

everything will work.

@MaAl13
Copy link
Author

MaAl13 commented Sep 30, 2024

Thank you very much for the solution! It indeed works now :)
I have one more question, is it possible to define known parameters?

@sumiya11
Copy link
Collaborator

Hi! Appending an output for a known parameter should work. For example,

ode = @ODEmodel(
    # Define the state derivatives
    a'(t) = p_a * s_0 * nb(t) - d_a * pa1(t),
    b'(t) = p_b * pa2(t) - d_b * pb(t),
    nb'(t) = -nb(t) * n_ba * ((p_b * pa2(t) - d_b * pb(t)) / b(t)) * (1 - nb(t)),
    pa1'(t) = pa1(t) * n_a * ((p_a * s_0 * nb(t) - d_a * pa1(t)) / a(t)) * (1 - pa1(t)),
    pa2'(t) = pa2(t) * n_ab * ((p_a * s_0 * nb(t) - d_a * pa1(t)) / a(t)) * (1 - pa2(t)),
    pb'(t) = pb(t) * n_b * ((p_b * pa2(t) - d_b * pb(t)) / b(t)) * (1 - pb(t)),
    
    # Define the outputs
    y1(t) = a(t),  # First measurable output
    y2(t) = b(t),   # Second measurable output
    y3(t) = p_a
)

@MaAl13
Copy link
Author

MaAl13 commented Oct 1, 2024

Thank you very much, awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants