# Add sum term to ODE in DifferentialEquations

Hello,
I wrote some ODEs in Julia with DifferentialEquations. Now I have to translate the following into Julia:

The ODEs I have written are:

``````function ode!(du, u, p, t)
μ, ϕ, ω, β, τ = p
#= with:
a = μ
b = ϕ
l,s = τ
r = β
du[1] = susceptible
du[2] = infected
du[3] = phages
=#
du[1] = (μ * u[1]) -(ϕ * u[1] * u[3]) - (ω * u[1])
du[2] = (ϕ * u[1] * u[3]) - (ω * u[2]) # - SUM
du[3] = - (ϕ * u[1] * u[3]) - (ω * u[3]) # + (β * SUM)
end
``````

But the problem is how to insert the SUM term. In a delayed ODE set I wrote:

``````function dynDelay!(du, u, h, p, t)
μ, κ, ϕ, ω, β, ρ, τ = p
#=
du[1] = susceptible
du[2] = infected
du[3] = phages
=#
history = h(p, t - τ)
delay_lysis =  ϕ * history[1] * history[3] * exp(-1 * ω * τ)
du[1] = (μ * u[1]) * (1 - (u[1]+u[2])/κ) - (ϕ * u[1] * u[3]) - (ω * u[1])
du[2] = (ϕ * u[1] * u[3]) - delay_lysis - (ω * u[2])
du[3] = (β * delay_lysis) - (ρ * ϕ * u[1] * u[3]) - (ω * u[3])
end
``````

In this case, there is also an additional term (delay_lysisi), which comes from history, to add to the ODEs. Would the sum be a similar thing?
Thank you

Yes, you’d just do `sum(...)`

I converted the formulae in:

``````function dynVirul!(du, u, p, t)
μ, ϕ, ω, Ω, β, τ = p
#=
du[1]   = susceptible
du[2]   = infected
du[3]   = phages
τ       = latency time, l
μ       = growth rate, a
ω       = outflow (loss rate bacteria), p
Ω       = outflow (loss rate phage), s = p
β       = burst size, r
=#
sumTerm = sum(u[2] * (ϕ * u[1] * u[3] - ω * ϕ * u[1] * u[3]))
du[1] = (μ * u[1]) - (ϕ * u[1] * u[3]) - (ω * u[1])
du[2] = (ϕ * u[1] * u[3]) - (ω * u[2]) - sumTerm
du[3] = (β * sumTerm) - (ϕ * u[1] * u[3]) - (Ω * u[3])
end
``````

but when I run it I get an error:

``````mu    = 0.03       # maximum growth rate susceptible strain
phi   = 2e-8       # adsorption rate
omega = 0.05       # outflow
beta  = 150        # burst size
tau   = 26         # latency time
tmax  = 4000.0     # time span 0-tmax
s0    = 1.3e5      # initial susceptible population
i0    = 0.0        # initial infected population
v0    = 5.3e3       # initial phage population
omega_bact =  omega_phage  = omega

tspan = (0.0, tmax)
u0 = [s0, i0, v0]
parms = [mu, phi, omega_bact, omega_phage, beta, tau]
prob = DDEProblem(dynVirul!, u0, tspan, parms)

ERROR: MethodError: objects of type Array{Float64,1} are not callable
Use square brackets [] for indexing an Array.
Stacktrace:
[1] DDEProblem{false,tType,lType,lType2,isinplace,P,F,H,K} where K where H where F where P where isinplace where lType2 where lType where tType(::DDEFunction{false,typeof(dynVirul!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing}, ::Array{Float64,1}, ::Tuple{Float64,Float64}, ::Array{Float64,1}; order_discontinuity_t0::Int64, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /home/gigiux/.julia/packages/DiffEqBase/KnYSY/src/problems/dde_problems.jl:32
[2] DDEProblem{false,tType,lType,lType2,isinplace,P,F,H,K} where K where H where F where P where isinplace where lType2 where lType where tType(::DDEFunction{false,typeof(dynVirul!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing}, ::Array{Float64,1}, ::Tuple{Float64,Float64}, ::Array{Float64,1}) at /home/gigiux/.julia/packages/DiffEqBase/KnYSY/src/problems/dde_problems.jl:32
[3] DDEProblem(::DDEFunction{false,typeof(dynVirul!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing}, ::Array{Float64,1}, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /home/gigiux/.julia/packages/DiffEqBase/KnYSY/src/problems/dde_problems.jl:44
[4] DDEProblem(::DDEFunction{false,typeof(dynVirul!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing}, ::Array{Float64,1}, ::Vararg{Any,N} where N) at /home/gigiux/.julia/packages/DiffEqBase/KnYSY/src/problems/dde_problems.jl:44
[5] DDEProblem(::Function, ::Array{Float64,1}, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /home/gigiux/.julia/packages/DiffEqBase/KnYSY/src/problems/dde_problems.jl:41
[6] DDEProblem(::Function, ::Array{Float64,1}, ::Vararg{Any,N} where N) at /home/gigiux/.julia/packages/DiffEqBase/KnYSY/src/problems/dde_problems.jl:41
[7] top-level scope at none:0
``````

What did I get wrong?
Thanks

That’s not the syntax for DDEProblem.

See the tutorial: https://diffeq.sciml.ai/stable/tutorials/dde_example/

A DDEProblem requires a history function for the pre-initial condition in order to be well-defined.

Quite right! it worked with `ODEProblem`. Thank you