# Empty container reduce error: diff eq

**URL:** https://discourse.julialang.org/t/empty-container-reduce-error-diff-eq/64956
**Category:** Modelling & Simulations
**Created:** [July 20, 2021, 12:37am UTC](https://discourse.julialang.org/t/empty-container-reduce-error-diff-eq/64956 "2021-07-20T00:37:07Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![graham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/graham/32/23781_2.png) [@graham](https://discourse.julialang.org/u/graham)
#### Post date: [July 20, 2021, 12:37am UTC](https://discourse.julialang.org/t/empty-container-reduce-error-diff-eq/64956/1 "2021-07-20T00:37:07Z")

</div>

Hi all,

I’m trying to use the DifferentialEquations.jl package to perform explicit integration of a mass-spring-damper system.

Here are the function I’m solving, and how I passed it into the SecondOrderODEProblem() function.

```julia
func = inv(M) * (f - (C *v + K* d))

SecondOrderODEProblem(func, v, d, 0:tsim)

```

This gives me the following error:

```julia
ERROR: LoadError: ArgumentError: reducing over an empty collection is not allowed

```

The stack trace didn’t give much insight into which collection was empty, but I’ve just checked, and all the containers in the function aren’t empty, and my du0/u0 aren’t empty.

Here are the values for each variable in there:

M = 96x96 Diagonal{Float64, Vector{Float64}}  
f = 96x1 Matrix{Float64} (every 3rd element is non-zero)  
C = 96x96 Diagonal{Float64, Vector{Float64}}  
v = 96x1 Matrix{Float64}  
K = 96x96 Matrix{Float64}  
d = 96x1 Matrix{Float64}

so that

func = 96x1 Matrix{Float64}

So, I should be passing into the SecondOrderODEProblem() function:

96x1 matrix where every 3rd element is non zero  
96x1 matrix of zeros  
96x1 matrix of zeros

I’m not sure exactly what the problem is here, any help is greatly appreciated!

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [July 20, 2021, 3:01am UTC](https://discourse.julialang.org/t/empty-container-reduce-error-diff-eq/64956/2 "2021-07-20T03:01:03Z")

</div>

> [@graham](#):
>
> `func = inv(M) * (f - (C *v + K* d))`

`func` is not a function. You might want to start by copying a tutorial demo:

```julia
function HH_acceleration(dv,v,u,p,t)
    x,y = u
    dx,dy = dv
    dv[1] = -x - 2x*y
    dv[2] = y^2 - y -x^2
end
initial_positions = [0.0,0.1]
initial_velocities = [0.5,0.0]
prob = SecondOrderODEProblem(HH_acceleration,initial_velocities,initial_positions,tspan)
sol2 = solve(prob, KahanLi8(), dt=1/10);

```

etc.

---

<div class="post-metadata">

### Author: ![graham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/graham/32/23781_2.png) [@graham](https://discourse.julialang.org/u/graham)
#### Post date: [July 20, 2021, 5:14am UTC](https://discourse.julialang.org/t/empty-container-reduce-error-diff-eq/64956/3 "2021-07-20T05:14:47Z")

</div>

Thanks yeah, silly mistake.

My code now looks like:

```julia
func(M, f, C, v, K, d) = inv(M) * (f - (C*v + K*d))
prob = SecondOrderODEProblem(func, v, d, (0.0,10.0))
sol = solve(prob, Tsit5(), dt = 1.0/10.0)

```

Which I arrived at after looking at the documented constructor:

```julia
SecondOrderODEProblem{isinplace}(f,du0,u0,tspan,callback=CallbackSet())

```

However, now it is throwing a long winded error:

```julia
ERROR: LoadError: MethodError: no method matching (::var"#func#84")(::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::SciMLBase.NullParameters, ::Float64)
Closest candidates are:
  (::var"#func#84")(::Any, ::Any, ::Any, ::Any, ::Any, ::Any) at c:\Users\grw40\code\Summer Research\MSD_matlab\msd_matlab\julia_port\ExplicitShellMSD.jl:86

```

I suppose there’s something about my function that doesn’t jive well.  
I’m familiar with the syntax for the 2nd derivative function you’ve posted, but I thought the solver might do that for me.

I suppose I’ll have to explicitly define which each component acceleration is for every node within my acceleration functions? Are for loops the best way of going about this (in terms of performance)?

Thanks for the help!

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [July 20, 2021, 10:10am UTC](https://discourse.julialang.org/t/empty-container-reduce-error-diff-eq/64956/4 "2021-07-20T10:10:51Z")

</div>

> [@graham](#):
>
> `func(M, f, C, v, K, d)`

That’s not one of the allowed function signatures. Did you mean `func(v,u,p,t)`, with some of those things being parameters in `p`?

---

<div class="post-metadata">

### Author: ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)
#### Post date: [July 20, 2021, 12:13pm UTC](https://discourse.julialang.org/t/empty-container-reduce-error-diff-eq/64956/5 "2021-07-20T12:13:19Z")

</div>

I don’t understand what you want to do, but I have made a SecondOrderODEProblem example that actually works.

Jupyter notebook: [https://github.com/genkuroki/public/blob/main/0012/SecondOrderODEProblem%20example.ipynb](https://github.com/genkuroki/public/blob/main/0012/SecondOrderODEProblem%20example.ipynb)

```julia
using DifferentialEquations
using LinearAlgebra
using Plots

function plot_2ndorder(sol; l1=:bottomright, l2=:bottomright, size=(720, 300), kwargs...)
    t = range(sol.prob.tspan...; length=400)
    n = Base.size(sol, 1) ÷ 2
    v = vcat((t -> sol(t)[1:n]').(t)...)
    d = vcat((t -> sol(t)[n+1:end]').(t)...)
    P = plot(t, d; label=permutedims(["\$d_{$i}\$" for i in 1:n]), legend=l1)
    Q = plot(t, v; label=permutedims(["\$v_{$i}\$" for i in 1:n]), legend=l2)
    plot(P, Q; size, kwargs...)
end

m = Float64[1, 2, 3, 4]
f = Float64[-10, -10, -10, -10]
c = Float64[1, 1, 1, 1]
K = Float64[
     2 -1 0 0
    -1 2 -1 0
     0 1 2 -1
     0 0 -1 2
]
p = (m, f, c, K)
v0 = Float64[0, 0, 0, 0]
d0 = Float64[3, 1, -1, -3]
tspan = (0.0, 10.0)

function g!(dv, v, d, p, t)
    m, f, c, K = p
    # The following is equivalent to dv .= M \ (f - (C * v + K * d)),
    # where K = Matrix, C = Diagonal(c), M = Diagonal(m)
    mul!(dv, K, d)
    @. dv = m \ (f - (c * v + dv))
    return
end

prob = SecondOrderODEProblem(g!, v0, d0, tspan, p)
sol = solve(prob)
plot_2ndorder(sol)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/8/a86585b4a6a8add4b50ee0f38700f91c5e91cdd3.jpeg)

---

<div class="post-metadata">

### Author: ![graham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/graham/32/23781_2.png) [@graham](https://discourse.julialang.org/u/graham)
#### Post date: [July 20, 2021, 1:31pm UTC](https://discourse.julialang.org/t/empty-container-reduce-error-diff-eq/64956/6 "2021-07-20T13:31:42Z")

</div>

Wow, idk why my brain can’t process the documentation. Thanks!

Incidentally, do you know of a good way to generate and plot a 3D planar mesh (think cloth sim, starts out as flat sheet and deforms in 3D)?

I tried Triangulate.jl, but could only find support for plotting in 2D. Tried Tetgen too, but it was pretty buggy (missing connections randomly), and was pretty hard to find good documentation on it - I don’t remember the C++ docs being too helpful, I think I remember the CLI switches being a source of frustration.

Anywho, thanks a lot, and when tf do you sleep? Have a good one.

P.S, you’re Stochastic Lifestyle guy right? Great blog, thanks for doin what you do!

---

<div class="post-metadata">

### Author: ![graham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/graham/32/23781_2.png) [@graham](https://discourse.julialang.org/u/graham)
#### Post date: [July 20, 2021, 1:37pm UTC](https://discourse.julialang.org/t/empty-container-reduce-error-diff-eq/64956/7 "2021-07-20T13:37:53Z")

</div>

Hey, thanks for this, really helpful. Idk why my brain couldn’t process the docs, it’s fairly clear!

I’m ultimately trying to rewrite a cloth sim I’ve made into Julia, currently testing out implicit/explicit methods for performance and learnkng about Julia syntax. 🙂 Usually just handwrite the integration, but I’m trying to learn more about auto-diff/adaptive time stepping, and Julia is nice in that you get to see the source code for some world-class solvers for free!

I think, with some adjustment to account for n-dof systems, your example will be very helpful thanks!

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [July 20, 2021, 1:58pm UTC](https://discourse.julialang.org/t/empty-container-reduce-error-diff-eq/64956/8 "2021-07-20T13:58:55Z")

</div>

Open an issue. Maybe we need another second order ODE tutorial.

---

<div class="post-metadata">

### Author: ![graham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/graham/32/23781_2.png) [@graham](https://discourse.julialang.org/u/graham)
#### Post date: [July 20, 2021, 2:44pm UTC](https://discourse.julialang.org/t/empty-container-reduce-error-diff-eq/64956/9 "2021-07-20T14:44:31Z")

</div>

Thanks, will do. To be clear, should I open an issue on the DifferentialEquations.jl github, or are you meaning there’s somewhere on the Julia docs site I should look to do this?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [July 20, 2021, 2:50pm UTC](https://discourse.julialang.org/t/empty-container-reduce-error-diff-eq/64956/10 "2021-07-20T14:50:51Z")

</div>

Yes, on DifferentialEquations.jl

---

<div class="post-metadata">

### Author: ![graham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/graham/32/23781_2.png) [@graham](https://discourse.julialang.org/u/graham)
#### Post date: [July 20, 2021, 2:52pm UTC](https://discourse.julialang.org/t/empty-container-reduce-error-diff-eq/64956/11 "2021-07-20T14:52:17Z")

</div>

Cool, thanks for your patience, have a good one.
