# Changing the size of VectorContinuousCallback

**URL:** <https://discourse.julialang.org/t/changing-the-size-of-vectorcontinuouscallback/119534>\
**Category:** Modelling & Simulations\
**Tags:** diffeq\
**Created:** [September 18, 2024, 4:18am UTC](https://discourse.julialang.org/t/changing-the-size-of-vectorcontinuouscallback/119534 "2024-09-18T04:18:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![evan-wehi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evan-wehi/32/32692_2.png) [@evan-wehi](https://discourse.julialang.org/u/evan-wehi)\
**Post date:** [September 18, 2024, 4:18am UTC](https://discourse.julialang.org/t/changing-the-size-of-vectorcontinuouscallback/119534/1 "2024-09-18T04:18:15Z")

</div>

I’m modelling cell population dynamics. Cells die and divide so the size of system is always changing. I _love_ DifferentialEquations.jl because it is the only library I know of that has an API for this. Most of my state variables can trigger events (i.e. death or division) so it seems that `VectorContinuousCallback` is the way to go. Unfortunately, the constructor for VectorContinuousCallback requires the length of the returned vector which in my case will change frequently. I could specify a very large length and pad the return vector with non-zero values but that seems inelegant and inefficient.

Have I missed something in the API? Can I simple change `VectorContinuousCallback.len` in an `affect!` call? What is the best way to handle 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:** [September 18, 2024, 7:41am UTC](https://discourse.julialang.org/t/changing-the-size-of-vectorcontinuouscallback/119534/2 "2024-09-18T07:41:30Z")

</div>

There isn’t a way to change that right now. It’s worth adding though. Open an issue. As a workaround, you could have a single ContinuousCallback that is the multiplication of conditions and then calculate which one had the crossing.

---

<div class="post-metadata">

**Author:** ![evan-wehi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evan-wehi/32/32692_2.png) [@evan-wehi](https://discourse.julialang.org/u/evan-wehi)\
**Post date:** [September 19, 2024, 12:30am UTC](https://discourse.julialang.org/t/changing-the-size-of-vectorcontinuouscallback/119534/3 "2024-09-19T00:30:39Z")

</div>

> <https://github.com/SciML/DifferentialEquations.jl/issues/1052>
>
> \*\*Is your feature request related to a problem? Please describe.\*\*
> 
> I’m modell…ing cell population dynamics. Cells die and divide so the size of system is always changing. Most of my state variables can trigger events (i.e. death or division) so it seems that \`VectorContinuousCallback\` is the way to go. Unfortunately, the constructor for \`VectorContinuousCallback\` requires the length of the returned vector which in my case will change frequently. I could specify a very large length and pad the return vector with non-zero values but that seems inelegant and inefficient. 
> 
> \*\*Describe the solution you’d like\*\*
> 
> To able to change the length of the returned vector. Changing during a \`affect!\` call is adequate for my use case. 
> 
> \*\*Describe alternatives you’ve considered\*\*
> As suggested on the julialang forum: "a single ContinuousCallback that is the multiplication of conditions and then calculate which one had the crossing." I haven't tried this yet but seems like it should work. 
> 
> \*\*Additional context\*\*

---

<div class="post-metadata">

**Author:** ![evan-wehi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evan-wehi/32/32692_2.png) [@evan-wehi](https://discourse.julialang.org/u/evan-wehi)\
**Post date:** [October 13, 2024, 9:47pm UTC](https://discourse.julialang.org/t/changing-the-size-of-vectorcontinuouscallback/119534/4 "2024-10-13T21:47:06Z")

</div>

This can fail if the solver steps over the sign change from two callbacks. I’m finding it a bit difficult to work with. At the moment, I’m forcing `dtmax` to be very small but that’s not ideal.

---

<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:** [October 14, 2024, 2:11pm UTC](https://discourse.julialang.org/t/changing-the-size-of-vectorcontinuouscallback/119534/5 "2024-10-14T14:11:33Z")

</div>

Yes we don’t handle that case yet

---

<div class="post-metadata">

**Author:** ![evan-wehi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evan-wehi/32/32692_2.png) [@evan-wehi](https://discourse.julialang.org/u/evan-wehi)\
**Post date:** [October 23, 2024, 3:44am UTC](https://discourse.julialang.org/t/changing-the-size-of-vectorcontinuouscallback/119534/6 "2024-10-23T03:44:53Z")

</div>

How difficult is this? Is it simply matter of rejigging data structures during an `affect!` call? If so, I could have a crack it myself. Or does it require an understanding of DE methods, in which case, I’m probably not qualified.

---

<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:** [October 23, 2024, 5:40am UTC](https://discourse.julialang.org/t/changing-the-size-of-vectorcontinuouscallback/119534/7 "2024-10-23T05:40:56Z")

</div>

Yeah it’s actually not so hard. You’d need code just like:

> <https://github.com/SciML/OrdinaryDiffEq.jl/blob/master/lib/OrdinaryDiffEqCore/src/integrators/integrator_interface.jl#L219-L233>

that checks if there are any VectorContinuousCallbacks in integrator.opts.callback and then calls `resize!` on them, and the dispatch for that needs to resize! the internal cache vector.
