# Nested vector variables in ODE problem definitions

**URL:** <https://discourse.julialang.org/t/nested-vector-variables-in-ode-problem-definitions/33417>\
**Category:** Modelling & Simulations\
**Created:** [January 15, 2020, 8:59pm UTC](https://discourse.julialang.org/t/nested-vector-variables-in-ode-problem-definitions/33417 "2020-01-15T20:59:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [January 15, 2020, 8:59pm UTC](https://discourse.julialang.org/t/nested-vector-variables-in-ode-problem-definitions/33417/1 "2020-01-15T20:59:15Z")

</div>

I am trying to define an ODE in which the state variables are nested in arrays. Here is a toy example:

```julia
using DifferentialEquations

function f!(du, u, p, t)
    du[1] = u[1] - u[2][1]
    du[2] .= -5 * u[2]
    return nothing
end

u₀ = [1.0, [2.0, 3.0]]
tspan = (0.0, 10.0)

prob = ODEProblem(f!, u₀, tspan)

sol = solve(prob, Tsit5())

```

I assume this type of thing is supported since the `solve` function makes a call to the RecursiveArrayTools function `recursive_unitless_bottom_eltype` to get the base element type of the nested array. But here it throws a stack overflow error because the methods of `recursive_unitless_bottom_eltype` look like:

```julia
recursive_unitless_bottom_eltype(a) = recursive_unitless_bottom_eltype(typeof(a))
recursive_unitless_bottom_eltype(a::Type{T}) where T = recursive_unitless_bottom_eltype(eltype(a))
recursive_unitless_bottom_eltype(a::Type{T}) where {T<:AbstractArray} = recursive_unitless_bottom_eltype(eltype(a))
recursive_unitless_bottom_eltype(a::Type{T}) where {T<:Number} = eltype(a) == Number ? Float64 : typeof(one(eltype(a)))

```

Since the `typeof(u₀)` is `Array{Any,1}`, the second pass goes into the second method. Since `eltype(Array{Any,1})` is `Any` and `eltype(Any)` is `Any`, we recurse indefinitely.

Is my use case supported or am I doing something that just shouldn’t be done?

---

<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:** [January 15, 2020, 9:58pm UTC](https://discourse.julialang.org/t/nested-vector-variables-in-ode-problem-definitions/33417/2 "2020-01-15T21:58:17Z")

</div>

This is one thing I haven’t been able to solve without a type because you cannot declare broadcast as recursive. `u₀ = VectorOfArray([[1.0], [2.0, 3.0]])` works though, and so does `ArrayPartition`.

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [January 16, 2020, 2:01am UTC](https://discourse.julialang.org/t/nested-vector-variables-in-ode-problem-definitions/33417/3 "2020-01-16T02:01:14Z")

</div>

Oh, cool, I’ll have to give that a try. Thanks!

---

<div class="post-metadata">

**Author:** ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)\
**Post date:** [August 17, 2020, 9:11pm UTC](https://discourse.julialang.org/t/nested-vector-variables-in-ode-problem-definitions/33417/4 "2020-08-17T21:11:46Z")

</div>

This is the same problem I am having on the Slack discussion.

````julia
julia> recursive_unitless_bottom_eltype([xyzVec(),xyzVec(),xVec(1.0)])
ERROR: StackOverflowError:
Stacktrace:
 [1] recursive_unitless_bottom_eltype(::Type{Any}) at C:\Users\bakerar\.julia\packages\RecursiveArrayTools\DBKJE\src\utils.jl:86 (repeats 65195 times)
 [2] recursive_unitless_bottom_eltype(::Type{Array{xyzVec,1}}) at C:\Users\bakerar\.julia\packages\RecursiveArrayTools\DBKJE\src\utils.jl:87
 [3] recursive_unitless_bottom_eltype(::Array{xyzVec,1}) at C:\Users\bakerar\.julia\packages\RecursiveArrayTools\DBKJE\src\utils.jl:85
 [4] top-level scope at none:0```

My composite type is simple:
struct xyzVec <:AbstractVec
    x::Float64
    y::Float64
    z::Float64
end

````

I have lots of math for vectors defined…  
and the thing that is causing the problem is an ill defined eltype function which I can’t find a good example of.

```julia
Base.iterate(v::AbstractVec, state=1) = state > length(v) ?
                                        nothing :
                                        (v[state], state+1) #Return the positional number

Base.getindex(v::AbstractVec, i1::Int64) = toTuple(v)[i1]

Base.eltype(::xyzVec) = Float64
Base.eltype(::xyVec) = Float64
Base.eltype(::xyVec) = Float64
Base.eltype(::AbstractVec) = Float64

```

I think I need my syntax corrected on the eltype and it will all work out, but so far no luck.

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [August 19, 2020, 11:35pm UTC](https://discourse.julialang.org/t/nested-vector-variables-in-ode-problem-definitions/33417/5 "2020-08-19T23:35:48Z")

</div>

[ComponentArrays.jl](https://github.com/jonniedie/ComponentArrays.jl) is basically what came out of this topic. Check it out and see if it fits your needs.

---

<div class="post-metadata">

**Author:** ![levasco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/levasco/32/17539_2.png) [@levasco](https://discourse.julialang.org/u/levasco)\
**Post date:** [April 22, 2021, 8:22am UTC](https://discourse.julialang.org/t/nested-vector-variables-in-ode-problem-definitions/33417/6 "2021-04-22T08:22:39Z")

</div>

ComponentArrays does work for my case, but `VectorOfArray` integrators are not `resize!()`able or `deleteat!()`able within callbacks (no methods). Any way to have a nested structure that allows for easily changing dimensionality?

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [April 22, 2021, 1:05pm UTC](https://discourse.julialang.org/t/nested-vector-variables-in-ode-problem-definitions/33417/7 "2021-04-22T13:05:32Z")

</div>

@levasco Yep, [MultiScaleArrays.jl](https://github.com/SciML/MultiScaleArrays.jl) was created specifically for this purpose (assuming the resizing and deleting you’re doing is of whole branches of the tree)
