% This file is licensed under the Allen Institute Terms of Use, which can be found at:
%http://www.alleninstitute.org/Media/policies/terms_of_use_content.html
%------------------------------------------------------------------------------------

%%%Script to generate data for Supplement Fig. S11 (matched delta and bimodal
%synaptic distributions differing in 0.1% of their synapses)

clear variables;

%Parameters
th=20*10^-3;%V Threshold
lk=1/(20*10^-3);%s^-1 leak constant 1/RC

dt0=0.2*10^-3;%0.2*10^-3;
tf=100*10^-3; %s final time
tvec=0:dt0:tf;

dv0=0.2*10^-3;%V defines max step in potential
v0=(0:dv0:th);%V defines the grid for the membrane potential

scale = 10^3; % to convert from V to mV

flag_poisson=1;             %Poisson input   
flag_cond_syn=0;            % implies current-based synapses
neur_type = 'LIF';

voltage_fraction=1;

%Initial probability density

%delta function probability distribution starting at 0 mV
p0=zeros(size(v0));
p0(v0==0)=1;

[cellpop connect]=init_cellpop_no_dep;

cellpop(1).v0=v0;
cellpop(2).v0=v0;
cellpop(1).p0=p0;
cellpop(2).p0=p0;
cellpop(2).external_fr=zeros(size(tvec));

connect(1).flag_cond_syn=flag_cond_syn;
connect(1).flag_poisson=flag_poisson;
connect(1).voltage_frac=voltage_fraction;

fr = 1000;

qdw_del=[0.001; 1; 1];
qdw_bidel=[0.986*10^-3 0.015;1 1;14/(15-0.986) 1-(14/(15-0.986))];

cellpop(1).external_fr=[fr*ones(size(tvec))];
cellpop(2).external_fr=zeros(size(tvec));

%1) delta-fn distribution of synaptic weights
connect(1).qdw=qdw_del;

[cellpop connect t_vec]=dipde_multi_pop_fig1(cellpop,connect,dt0,tf,neur_type);

t_vec_del = t_vec;
fout_del = cellpop(2).fout;
fout_del_up = cellpop(2).fout_up;
fout_del_low = cellpop(2).fout_low;
prob_del_unif = cellpop(2).prob_unif;

%2) bimodal distribution of synaptic weights
connect(1).qdw=qdw_bidel;

[cellpop connect t_vec]=dipde_multi_pop_fig1(cellpop,connect,dt0,tf,neur_type);

t_vec_bidel = t_vec;
fout_bidel = cellpop(2).fout;
fout_bidel_up = cellpop(2).fout_up;
fout_bidel_low = cellpop(2).fout_low;
prob_bidel_unif = cellpop(2).prob_unif;

save('FigS11_frates.mat','t_vec_del','fout_del','fout_del_up','fout_del_low','t_vec_bidel','fout_bidel','fout_bidel_up','fout_bidel_low');
save('FigS11_distributions.mat','prob_del_unif','prob_bidel_unif');
