% 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 find output firing rates for 1222 random distributions of
%synaptic weights between 0 mV and th = 20 mV. This data is then used to
%find the transient times (T20) to 20% of the steady state firing rate for
%a given distribution of synaptic weights

%NOTE: The .mat file FigS10_randdist.mat consisting of the original
%distributions used for FigS10 has also been made available and can be 
%used to reproduce the figure exactly. 

clear variables;

load FigS10_randdist.mat; %using randdist3.m to generate distributions

%Initializing parameters
lk=1/(20*10^-3);% (s^-1) leak constant 1/RC
th=20*10^-3;

dt0=0.2*10^-3;% (s) time step
tf=200*10^-3; % (s) final time
tvec=0:dt0:tf;% (s) defines grid for time

dv0=0.2*10^-3;
v0=0:dv0:th;

fr=500;
mean_desired = 2*10^-3;

voltage_fraction=1;
flag_poisson=1;
flag_cond_syn=0;

%Initial probability density

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

[cellpop connect]=init_cellpop_no_dep;

cellpop(1).v0=v0;
cellpop(1).p0=p0;
cellpop(1).external_fr=[fr*ones(size(tvec))];

cellpop(2).v0=v0;
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;

output_fr = [];
equil_volt=[];

tic

%Calculate output firing rates and steady state voltage distributions
for jj=1:length(dist(1,1:1222))
    wts_dist=x*0.02;
    qwts_dist=dist(:,jj);
    qdw_dist= [wts_dist;ones(size(wts_dist));qwts_dist'];
    connect(1).qdw=qdw_dist;
    [cellpop connect t_vec]=dipde_multi_pop2(cellpop,connect,dt0,tf);
    output_fr=[output_fr;cellpop(2).fout];
    equil_volt=[equil_volt; cellpop(2).prob_unif(end-1,:)];
end

save('FigS10_randdist_DiPDEoutput.mat','output_fr','equil_volt','v0','t_vec','dt0');

toc
