是否可以使用回调来访问 Julia 的微分方程集成问题中的单个轨迹?
Is it possible to use callbacks to access a single trajectory in Julia's DifferentialEquations Ensemble Problems?
我是 Julia 的新手,正在尝试使用 Julia 包 DifferentialEquations 同时求解同一组耦合 ODE 的多个条件。我的系统是一个实验模型,在其中一种情况下,我在过程中途增加了一个因变量的数量。
我希望能够调整这个单一轨迹的条件,但是到目前为止我只能一次调整所有轨迹。是否可以使用回调访问单个回调?如果没有,有没有更好的方法来做到这一点?
这是一个使用洛伦兹方程的简化示例:
#Differential Equations setup
function lorentz!(du,u,p,t)
a,r,b=p
du[1]= a*(u[2]-u[1])
du[2]=u[1]*(r-u[3])-u[2]
du[3]=u[1]*u[2]-b*u[3];
end
#function to cycle through inital conditions
function prob_func(prob,i,repeat)
remake(prob; u0 = u0_arr[i]);
end
#inputs
t_span=[(0.0,100.0),(0.0,100.0)];
u01=[0.0;1.0;0.0];
u02=[0.0;1.0;0.0];
u0_arr = [u01,u02];
p=[10.,28.,8/3];
#initialising the Ensemble Problem
prob = ODEProblem(lorentz!,u0_arr[1],t_span[1],p);
CombinedProblem = EnsembleProblem(prob,
prob_func = prob_func, #-> (prob),#repeat is a count for how many times the trajectories had been repeated
safetycopy = true # determines whether a safetly deepcopy is called on the prob before the prob_func (sounds best to leave as true for user-given prob_func)
);
#introducing callback
function condition(u,t,repeat)
return 50 .-t
end
function affect!(repeat)
repeat.u[1]=repeat.u[1] +50
end
callback = DifferentialEquations.ContinuousCallback(condition, affect!)
#solving
sim=solve(CombinedProblem,Rosenbrock23(),EnsembleSerial(),trajectories=2,callback=callback);
# Plotting for ease of understanding example
plot(sim[1].t,sim[1][1,:])
plot!(sim[2].t,sim[2][1,:])
我想制作这样的东西:
Example_desired_outcome
但是这段代码产生:
Example_current_outcome
感谢您的帮助!
您可以使该回调依赖于参数,并使参数在问题之间有所不同。例如:
function f(du,u,p,t)
if p == 0
du[1] = 2u[1]
else
du[1] = -2u[1]
end
du[2] = -u[2]
end
condition(t,u,integrator) = u[2] - 0.5
affect!(integrator) = integrator.prob.p = 1
有关详细信息,请查看有关此主题的常见问题解答:https://diffeq.sciml.ai/stable/basics/faq/#Switching-ODE-functions-in-the-middle-of-integration
我是 Julia 的新手,正在尝试使用 Julia 包 DifferentialEquations 同时求解同一组耦合 ODE 的多个条件。我的系统是一个实验模型,在其中一种情况下,我在过程中途增加了一个因变量的数量。
我希望能够调整这个单一轨迹的条件,但是到目前为止我只能一次调整所有轨迹。是否可以使用回调访问单个回调?如果没有,有没有更好的方法来做到这一点?
这是一个使用洛伦兹方程的简化示例:
#Differential Equations setup
function lorentz!(du,u,p,t)
a,r,b=p
du[1]= a*(u[2]-u[1])
du[2]=u[1]*(r-u[3])-u[2]
du[3]=u[1]*u[2]-b*u[3];
end
#function to cycle through inital conditions
function prob_func(prob,i,repeat)
remake(prob; u0 = u0_arr[i]);
end
#inputs
t_span=[(0.0,100.0),(0.0,100.0)];
u01=[0.0;1.0;0.0];
u02=[0.0;1.0;0.0];
u0_arr = [u01,u02];
p=[10.,28.,8/3];
#initialising the Ensemble Problem
prob = ODEProblem(lorentz!,u0_arr[1],t_span[1],p);
CombinedProblem = EnsembleProblem(prob,
prob_func = prob_func, #-> (prob),#repeat is a count for how many times the trajectories had been repeated
safetycopy = true # determines whether a safetly deepcopy is called on the prob before the prob_func (sounds best to leave as true for user-given prob_func)
);
#introducing callback
function condition(u,t,repeat)
return 50 .-t
end
function affect!(repeat)
repeat.u[1]=repeat.u[1] +50
end
callback = DifferentialEquations.ContinuousCallback(condition, affect!)
#solving
sim=solve(CombinedProblem,Rosenbrock23(),EnsembleSerial(),trajectories=2,callback=callback);
# Plotting for ease of understanding example
plot(sim[1].t,sim[1][1,:])
plot!(sim[2].t,sim[2][1,:])
我想制作这样的东西:
Example_desired_outcome
但是这段代码产生:
Example_current_outcome
感谢您的帮助!
您可以使该回调依赖于参数,并使参数在问题之间有所不同。例如:
function f(du,u,p,t)
if p == 0
du[1] = 2u[1]
else
du[1] = -2u[1]
end
du[2] = -u[2]
end
condition(t,u,integrator) = u[2] - 0.5
affect!(integrator) = integrator.prob.p = 1
有关详细信息,请查看有关此主题的常见问题解答:https://diffeq.sciml.ai/stable/basics/faq/#Switching-ODE-functions-in-the-middle-of-integration