Hi.
I have hit a snag in that I wish to calculate a quantity u[3]
as an ‘assignment’ (not sure that is the correct terminology) within the ODE function and pass back to one of my ODE’s du[3]
My code is below:
# Packages (import Pkg; Pkg.add("name of package")
using DifferentialEquations
using Plots
function perfadap(du,u,p,t)
## Reactions
# Reaction: id = r1
reaction_r1 = p[1]*p[2]*u[3];
# Reaction: id = r2
reaction_r2 = p[1]*p[3]*u[1]*u[2];
# Reaction: id = r3
reaction_r3 = p[1]*p[4]*u[3];
# Reaction: id = r4
reaction_r4 = p[1]*p[5]*u[2];
## Assignment
# assignmentRule: variable = S
u[3] = 1*floor(tspan/p[6]);
## Species
# Species: id = R, name = u[1], affected by kineticLaw
du[1] = (1/(p[1]))*(( 1.0 * reaction_r1) + (-1.0 * reaction_r2));
# Species: id = X = u, name = X = u[2], affected by kineticLaw
du[2] = (1/(p[1]))*(( 1.0 * reaction_r3) + (-1.0 * reaction_r4));
# Species: id = S, name = u[3], involved in a rule
du[3] = u[3];
end
### PARAMETERS (p)
p = [1.0, 2.0, 2.0, 1.0, 1.0, 4.0] # compartment vol, k1, k2, k3, k4, tau
### TIME (tspan)
tspan = (0.0, 20.0) #10.0
### INITIAL CONDITIONS (u0)
u0 = [1.0,0.0,0.0] #du[1] = R, du[2] = X, du[3] = S respectively
### PROB SPECIFICATION (op)
op = ODEProblem(perfadap, u0, tspan, p)
### SYS SOLUTION (sol)
sol = solve(op, Tsit5()) # use Tsit5 ODE solver
### PLOT
plot(sol, title = "PerfAdap.jl") # seriestype = :scatter,
This is the full ‘ERROR’ and ‘Stacktrace’ appended below
ERROR: LoadError: MethodError: no method matching /(::Tuple{Float64, Float64}, ::Float64)
Closest candidates are:
/(::Any, ::ChainRulesCore.AbstractThunk) at C:\Users\bsmall\.julia\packages\ChainRulesCore\1LqRD\src\differentials\thunks.jl:33
/(::StridedArray{P, N} where N, ::Real) where P<:Dates.Period at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\deprecated.jl:44
/(::Union{SparseArrays.SparseVector{Tv, Ti}, SubArray{Tv, 1, var"#s832", Tuple{Base.Slice{Base.OneTo{Int64}}}, false} where var"#s832"<:SparseArrays.AbstractSparseVector{Tv, Ti}, SubArray{Tv, 1, var"#s832", Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, false} where var"#s832"<:SparseArrays.AbstractSparseMatrixCSC{Tv, Ti}} where {Tv, Ti}, ::Number) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\SparseArrays\src\sparsevector.jl:1450
...
Stacktrace:
[1] perfadap(du::Vector{Float64}, u::Vector{Float64}, p::Vector{Float64}, t::Float64)
@ Main z:\Documents\Julia\tutorial\Tyson2003_PerfAdap_V2.jl:23
[2] ODEFunction
@ C:\Users\bsmall\.julia\packages\SciMLBase\UIp7W\src\scimlfunctions.jl:334 [inlined]
[3] initialize!(integrator::OrdinaryDiffEq.ODEIntegrator{Tsit5, true, Vector{Float64}, Nothing, Float64, Vector{Float64}, Float64, Float64, Float64, Float64, Vector{Vector{Float64}}, ODESolution{Float64, 2, Vector{Vector{Float64}}, Nothing, Nothing, Vector{Float64}, Vector{Vector{Vector{Float64}}}, ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true,
typeof(perfadap), LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED),
Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, Tsit5, OrdinaryDiffEq.InterpolationData{ODEFunction{true, typeof(perfadap), LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Vector{Vector{Float64}}, Vector{Float64}, Vector{Vector{Vector{Float64}}}, OrdinaryDiffEq.Tsit5Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, OrdinaryDiffEq.Tsit5ConstantCache{Float64, Float64}}}, DiffEqBase.DEStats}, ODEFunction{true, typeof(perfadap), LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, OrdinaryDiffEq.Tsit5Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, OrdinaryDiffEq.Tsit5ConstantCache{Float64, Float64}}, OrdinaryDiffEq.DEOptions{Float64, Float64, Float64, Float64, PIController{Rational{Int64}}, typeof(DiffEqBase.ODE_DEFAULT_NORM), typeof(LinearAlgebra.opnorm), Nothing, CallbackSet{Tuple{}, Tuple{}}, typeof(DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN), typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE), typeof(DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK), DataStructures.BinaryHeap{Float64, DataStructures.FasterForward}, DataStructures.BinaryHeap{Float64, DataStructures.FasterForward}, Nothing, Nothing, Int64, Tuple{}, Tuple{}, Tuple{}}, Vector{Float64}, Float64, Nothing, OrdinaryDiffEq.DefaultInit}, cache::OrdinaryDiffEq.Tsit5Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, OrdinaryDiffEq.Tsit5ConstantCache{Float64, Float64}})
@ OrdinaryDiffEq C:\Users\bsmall\.julia\packages\OrdinaryDiffEq\8K0Aj\src\perform_step\low_order_rk_perform_step.jl:623
[4] __init(prob::ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, typeof(perfadap), LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, alg::Tsit5, timeseries_init::Tuple{}, ts_init::Tuple{}, ks_init::Tuple{}, recompile::Type{Val{true}}; saveat::Tuple{}, tstops::Tuple{}, d_discontinuities::Tuple{}, save_idxs::Nothing, save_everystep::Bool, save_on::Bool, save_start::Bool, save_end::Nothing, callback::Nothing, dense::Bool, calck::Bool, dt::Float64, dtmin::Nothing, dtmax::Float64, force_dtmin::Bool, adaptive::Bool, gamma::Rational{Int64}, abstol::Nothing, reltol::Nothing, qmin::Rational{Int64}, qmax::Int64, qsteady_min::Int64, qsteady_max::Int64, beta1::Nothing, beta2::Nothing, qoldinit::Rational{Int64}, controller::Nothing, fullnormalize::Bool, failfactor::Int64, maxiters::Int64, internalnorm::typeof(DiffEqBase.ODE_DEFAULT_NORM), internalopnorm::typeof(LinearAlgebra.opnorm), isoutofdomain::typeof(DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN), unstable_check::typeof(DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK), verbose::Bool, timeseries_errors::Bool, dense_errors::Bool, advance_to_tstop::Bool, stop_at_next_tstop::Bool, initialize_save::Bool, progress::Bool, progress_steps::Int64, progress_name::String, progress_message::typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE), userdata::Nothing, allow_extrapolation::Bool, initialize_integrator::Bool, alias_u0::Bool, alias_du0::Bool, initializealg::OrdinaryDiffEq.DefaultInit, kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
@ OrdinaryDiffEq C:\Users\bsmall\.julia\packages\OrdinaryDiffEq\8K0Aj\src\solve.jl:456
[5] __init(prob::ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, typeof(perfadap), LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, alg::Tsit5, timeseries_init::Tuple{}, ts_init::Tuple{}, ks_init::Tuple{}, recompile::Type{Val{true}}) (repeats 5 times)
@ OrdinaryDiffEq C:\Users\bsmall\.julia\packages\OrdinaryDiffEq\8K0Aj\src\solve.jl:67
[6] #__solve#471
@ C:\Users\bsmall\.julia\packages\OrdinaryDiffEq\8K0Aj\src\solve.jl:4 [inlined]
[7] __solve
@ C:\Users\bsmall\.julia\packages\OrdinaryDiffEq\8K0Aj\src\solve.jl:4 [inlined]
[8] #solve_call#42
@ C:\Users\bsmall\.julia\packages\DiffEqBase\OPDgm\src\solve.jl:61 [inlined]
[9] solve_call
@ C:\Users\bsmall\.julia\packages\DiffEqBase\OPDgm\src\solve.jl:48 [inlined]
[10] #solve_up#44
@ C:\Users\bsmall\.julia\packages\DiffEqBase\OPDgm\src\solve.jl:87 [inlined]
[11] solve_up
@ C:\Users\bsmall\.julia\packages\DiffEqBase\OPDgm\src\solve.jl:78 [inlined]
[12] #solve#43
@ C:\Users\bsmall\.julia\packages\DiffEqBase\OPDgm\src\solve.jl:73 [inlined]
[13] solve(prob::ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, typeof(perfadap), LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, args::Tsit5)
@ DiffEqBase C:\Users\bsmall\.julia\packages\DiffEqBase\OPDgm\src\solve.jl:68
[14] top-level scope
@ z:\Documents\Julia\tutorial\Tyson2003_PerfAdap_V2.jl:43
in expression starting at z:\Documents\Julia\tutorial\Tyson2003_PerfAdap_V2.jl:43
Any and all help / pointers appreciated.
Thanks
Ben