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

simplify_fractions changes ModelingToolkit parameters to variables #451

Open
paulflang opened this issue Jul 12, 2022 · 3 comments
Open

Comments

@paulflang
Copy link

simplify_fractions changes ModelingToolkit parameters to variables in some very rare cases. This creates problems downstream when using structural_simplify (unbalanced reaction system). The stdout from this code block in the SBMLToolkit.get_massaction function coarsely describes the issue:

        println("--- before division ---")
        println(kl)
        println(get_variables(kl))
        for v in get_variables(kl)
            println(SymbolicUtils.metadata(v))
        end
        println(ModelingToolkit.isparameter.(get_variables(kl)))
        rate_const = kl / *((.^(reactants, stoich))...)
        println("--- before simplifying fractions ---")
        println(rate_const)
        println(get_variables(rate_const))
        for v in get_variables(rate_const)
            println(SymbolicUtils.metadata(v))
        end
        println(ModelingToolkit.isparameter.(get_variables(rate_const)))
        rate_const = SymbolicUtils.simplify_fractions(rate_const)
        println("--- after simplifying fractions ---")
        println(rate_const)
        println(get_variables(rate_const))
        for v in get_variables(rate_const)
            println(SymbolicUtils.metadata(v))
        end
        println(ModelingToolkit.isparameter.(get_variables(rate_const)))

prints for example

--- before division ---
(k3*S2(t)) / default_compartment
Any[k3, S2(t), default_compartment]
Base.ImmutableDict{DataType, Any}(ModelingToolkit.MTKParameterCtx => true, Catalyst.ParameterConstantSpecies => false, Symbolics.VariableSource => (:parameters, :k3))
Base.ImmutableDict{DataType, Any}(ModelingToolkit.VariableInput => false, ModelingToolkit.VariableIrreducible => false, Catalyst.VariableBCSpecies => false, Symbolics.VariableSource => (:variables, :S2))
Base.ImmutableDict{DataType, Any}(ModelingToolkit.MTKParameterCtx => true, Catalyst.ParameterConstantSpecies => false, Symbolics.VariableSource => (:parameters, :default_compartment))
Bool[1, 0, 1]
--- before simplifying fractions ---
k3 / default_compartment
Any[k3, default_compartment]
Base.ImmutableDict{DataType, Any}(ModelingToolkit.MTKParameterCtx => true, Catalyst.ParameterConstantSpecies => false, Symbolics.VariableSource => (:parameters, :k3))
Base.ImmutableDict{DataType, Any}(ModelingToolkit.MTKParameterCtx => true, Catalyst.ParameterConstantSpecies => false, Symbolics.VariableSource => (:parameters, :default_compartment))
Bool[1, 1]
--- after simplifying fractions ---
k3 / default_compartment
Any[k3, default_compartment]
Base.ImmutableDict{DataType, Any}(Symbolics.VariableSource => (:variables, :k3))
Base.ImmutableDict{DataType, Any}(Symbolics.VariableSource => (:variables, :default_compartment))
Bool[0, 0]

(Strangely, most reactions work fine, as does the following MWE:

using SymbolicUtils, ModelingToolkit

@parameters a b t
@variables c(t)

kl = a * c / b
f1 = kl / c
println(get_variables(f1))  # Any[a, b]
println(ModelingToolkit.isparameter.(get_variables(f1)))  # Bool[1, 1]

f2 = SymbolicUtils.simplify_fractions(f1)
println(get_variables(f2))  # Any[a, b]
println(ModelingToolkit.isparameter.(get_variables(f2)))  # Bool[1, 1]

)

cc @isaacsas

@isaacsas
Copy link

Looks like another instance of #434

@isaacsas
Copy link

@shashi this one is going to cause (or perhaps is causing) issues for SBMLToolkit, Catalyst and ModelingToolkit. Is there a workaround one can use to avoid it till it gets fixed? Thanks!

@paulflang
Copy link
Author

In my use case, the output of rate_cost = SymbolicUtils.simplify_fractions(rate_const) is expected to be a functions of parameters only. To something like convert_all_vars_to_pars(rate_const) should fix it for me. Is there a function that does sth like that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants