# EnsembleProblem change tstops for each trajectory

**URL:** <https://discourse.julialang.org/t/ensembleproblem-change-tstops-for-each-trajectory/47066>\
**Category:** New to Julia\
**Tags:** question, ode\
**Created:** [September 22, 2020, 1:03pm UTC](https://discourse.julialang.org/t/ensembleproblem-change-tstops-for-each-trajectory/47066 "2020-09-22T13:03:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sberit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sberit/32/18106_2.png) [@sberit](https://discourse.julialang.org/u/sberit)\
**Post date:** [September 22, 2020, 1:03pm UTC](https://discourse.julialang.org/t/ensembleproblem-change-tstops-for-each-trajectory/47066/1 "2020-09-22T13:03:50Z")

</div>

Hey,

I’am trying to use the EnsembleProblem to run an ODE with a big parameter set. Following the problem in the documentation ([Event Handling and Callback Functions · DifferentialEquations.jl](https://diffeq.sciml.ai/stable/features/callback_functions/) example 1) I want to follow the patients drug concentration over time, except from single doses at specific time points I am giving doses over a period of time. I implemented for this two PresetTimeCallbacks, one for activating the dosage and one fore deactivating the dosage. My problem now is that I want to have different treatment schedules for each trajectory, i.e. for each trajectory the treatment starts and ends at different time points. I don’t manage to update the treatment schedules for each trajectory, it always stays the same. I tried to set up a small example:

using DifferentialEquations, DiffEqCallbacks,Plots  
function f(du,u,p,t)  
du[1] = -u[1]+p  
end  
u0 = [10.0]

function affect\_treatment\_monoA!(integrator)  
integrator.p=a  
end  
function affect\_notreat!(integrator)  
integrator.p=0.0  
end

p=0.0

dosage=[10.0, 5.0]  
rndsched=Array{Any}(undef,2)  
rndsched[1]=[[1.0 ,8.0], [3.0 ,9.0]] #treatment start and end of first trajectory  
rndsched[2]=[[5.0] ,[7.0] ]  
#treatment start and end of second trajectory  
treatStart=rndsched[1][1]  
treatEnd=rndsched[1][2]  
treatMonoSchedA=PresetTimeCallback(treatStart,affect\_treatment\_monoA!)  
NOtreatSched=PresetTimeCallback(treatEnd,affect\_notreat!)

function prob\_func\_Mono1(prob,i,repeat)  
global treatStart=rndsched[i][1]  
global treatEnd=rndsched[i][2]  
global a=dosage[i]  
remake(prob,u0=u0,p=p,callback=CallbackSet(treatMonoSchedA,NOtreatSched))  
end

prob = ODEProblem(f,u0,(0.0,10.0),0.0,callback=CallbackSet(treatMonoSchedA,NOtreatSched))  
emble\_prob\_mono1=EnsembleProblem(prob,prob\_func=prob\_func\_Mono1)  
solMonoSched=solve(emble\_prob\_mono1,EnsembleThreads(),trajectories=2)  
plot(solMonoSched[1],label=“trajectory1”)  
plot(solMonoSched[2], label=“trajectory2”)

(Also the update of the dosage parameter a does not seem to work in my small example)  
For any hints for what I am doing wrong I would be grateful.

---

<div class="post-metadata">

**Author:** ![sberit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sberit/32/18106_2.png) [@sberit](https://discourse.julialang.org/u/sberit)\
**Post date:** [September 23, 2020, 9:30pm UTC](https://discourse.julialang.org/t/ensembleproblem-change-tstops-for-each-trajectory/47066/2 "2020-09-23T21:30:27Z")

</div>

Thanks i figured it out! The the whole PresetTimeCallback should be redefined within the prob\_func like:

treatMonoSchedA=PresetTimeCallback(rndsched[1][1],affect\_treatment\_monoA!)  
NOtreatSched=PresetTimeCallback(rndsched[1][2],affect\_notreat!)

function prob\_func\_Mono1(prob,i,repeat)  
treatMonoSchedA=PresetTimeCallback(rndsched[i][1],affect\_treatment\_monoA!)  
NOtreatSched=PresetTimeCallback(rndsched[i][2],affect\_notreat!)  
global a=dosage[i]  
remake(prob,u0=u0,p=p,callback=CallbackSet(treatMonoSchedA,NOtreatSched))  
end  
(I only still need to figure out how to update the dosage for each trajectory)
