% 1D time dependent activator-inhibitor system on periodic domain

L = 4;		% length of periodic domain
N = 256;	% space sample points
LifeT = 1;	% life time of the sea shell
nT = 256;	% time sample points

% system parameters
D_a = 0.005;	% activator diffusion coefficient
r_a = 0.004;	% activator removal rate
b_a = 0.05;	% basic activator production
D_b = 0.4;	% inhibitor diffusion coefficient
r_b = 0.006;	% inhibitor removal rate
b_b = 0.0;	% basic inhibitor production
D_c = 0.003;	% second inhibitor diffusion coefficient
r_c = 0.0002;	% second inhibitor removal rate

% help variables
h = L/N;	% grid size
dt = LifeT/nT;	% time step

% FIXME: Meinhardt hack
nT = 144;
KP = 400;
N = 40;
dt = 1.0;
h = 1.0;

% activator production rate
s = r_a * (0.99 + 0.02 * rand(1,N));

% Build Laplace Operator (Finite Differences)
e = ones(N,1) / h^2;
Lap = spdiags([e -2*e e], -1:1, N, N);
Lap(N,1) = e(1);
Lap(1,N) = e(1);

% set initial values
a = zeros(nT, N);
b = zeros(nT, N);
c = zeros(nT, N);
a(1,:) = 1.0;
b(1,:) = 1.0;
c(1,:) = 0.0;

% solve problem (Fixed Step Euler Forward)
for n=1:nT-1
  oa = a(n,:);
  ob = b(n,:);
  oc = c(n,:);
  for k=1:KP
    
    na = oa + dt * (s.*((oa.^2 + b_a)./(ob + oc)) - r_a.*oa + D_a*oa*Lap);
    nb = ob + dt * (s.*oa.^2 - r_b*ob + D_b*ob*Lap + b_b);
    nc = oc + dt * (r_c*oa - r_c*oc + D_c*oc*Lap);
    oa = na;
    ob = nb;
    oc = nc;
  end
  a(n+1,:) = na;
  b(n+1,:) = nb;
  c(n+1,:) = nc;
end

subplot(1,3,1), imagesc(a);
subplot(1,3,2), imagesc(b);
subplot(1,3,3), imagesc(c);

function y = makecolor(x)
% makecolor: return a vector containing 64 steps of the actual color percentage
    if (x == 0)
        y = zeros(64,1);
    else
        y = [0:x/63:x]';
    end
end

colormap([makecolor(0.1), makecolor(0.7), makecolor(1)])


