Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PositiveDomain callback failing on example #44

Open
ChrisRackauckas opened this issue May 23, 2018 · 0 comments
Open

PositiveDomain callback failing on example #44

ChrisRackauckas opened this issue May 23, 2018 · 0 comments

Comments

@ChrisRackauckas
Copy link
Member

ChrisRackauckas commented May 23, 2018

using DifferentialEquations
using Plots

function Scaled_Null(f,y,p,t)
    p1 = 1;
    n, g, p2, p3, p4, p5, p6, h1, h2, z = p;

    f[1] = p1-y[1]-y[1]*sum(y[3:2:end].*g);
    for i = 1:n
        f[2*i] = p2[i]-(p5[i]+p6[i]*z[i])*y[2*i];
        f[2*i+1] = z[i]*y[2*i]+p3[i]*y[2*i+1]*y[1]/(1+y[1])-p4[i]*y[2*i+1];
    end
end

function condition(u,t,integrator)
    t in integrator.p[8] || t in integrator.p[9]
 end

function affect!(integrator)
    k = findall(x->x==integrator.t,h1);
    for i = 1:length(k)
        integrator.p[10][k[i]] = rand();
    end
    k = findall(x->x==integrator.t,h2);
    for i = 1:length(k)
        integrator.p[10][k[i]] = 0.0;
    end
end

tspan = (1,3e+3); antigen_time = 1e+3;
a = 1e+2;
n = 4;
h1 = rand(1.0:antigen_time,n);
h2 = h1 .+ rand(1.0:antigen_time,n);
g = rand(n);
p2 = rand(1.0:10.0,n);
p3 = @. 0.01+(1-0.01)*$(rand(n));
p4 = @. 0.001+(0.1-0.001)*$(rand(n));
p5 = @. 0.001+(0.1-0.001)*$(rand(n));
p6 = @. 0.01+(1-0.01)*$(rand(n));
z = zeros(n);
p = [n, g, p2, p3, p4, p5, p6, h1, h2, z];

y0 = [1.0; vec(hcat(rand(1.0:a,n),rand(n))')];
c = unique(rand(1:n,rand(1:n)));
for i=1:length(c)
    y0[c[i]+1]=0.0;
end

antigen=DiscreteCallback(condition,affect!);
cb=CallbackSet(antigen,PositiveDomain());
dis=sort(unique([i for i in vcat(h1,h2) if i<=tspan[end]]));
prob=ODEProblem(Scaled_Null,y0,tspan,p);
sol=solve(prob,Rosenbrock23(),tstops=dis,callback=antigen)
sol=solve(prob,Rosenbrock23(),tstops=dis,callback=cb)
sol=solve(prob,Rosenbrock23(),tstops=dis,isoutofdomain=(y,p,t)->any(x->x<0,y),callback=antigen)

plot(sol)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant