% # iAMPA_IN_PYso_JB12:
%
% Normalized synaptic AMPAergic excitatory current, with synaptic
% depression and minis, FROM the pyramidal axo-soma TO interneuron
% dendrite IN<-PYso connection used in the DynaSim implementation of
% (Benita et al., 2012)
%
% - Dependencies:
%     - netconNearestNeighbors.m
%
% - References:
%     - Benita, J. M., Guillamon, A., Deco, G., & Sanchez-Vives, M.
%     V. (2012). Synaptic depression and slow oscillatory activity
%     in a biophysical network model of the cerebral cortex.
%     Frontiers in Computational Neuroscience, 6.
%     https://doi.org/10.3389/fncom.2012.00064
%
% - Tags: synapse, connection, excitation, ampa
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parameters
% For DynaSim, we need to convert synaptic maximal conductance from
% absolute to area-relative terms, while also converting the units to
% mS/cm^2 to keep pace with the rest of the equations:
%
% 2.25 nS (gAMPA EI) / 0.02 mm^2 (interneuron area) * ...
% 1 mS / 1000000 nS * 100 mm^2 / 1 cm^2 = ...
% 0.01125 mS/cm^2
%
gAMPAMult = 1 % for experimenting with gAMPA
gAMPA = gAMPAMult * 0.01125 % mS/cm^2
EAMPA = 0 % mV

alpha = 3.48
tauS = 2 % ms
deprFactor = 0.9
tauRes = 400 % ms

sAMPAIC = 0.0
sAMPANoiseIC = 0.1
resIC = 0.8
resNoiseIC = 0.1

% Connectivity
% Connective radius, aka how many target cells each source cell connects
% to, from the source's perspective.
radius = 10

% Remove autapses to the dendrite corresponding to this soma
removeRecurrentBool = 1
% We also need to normalize the conductance in mS/cm^2 by the number of
% connections each target cell is receiving on average, so that the TOTAL
% sum of their conductive inputs adds to our overall maximal conductance
% above.
normalizingFactor = min(((2*radius + (1-removeRecurrentBool)) / (N_post/N_pre)), N_pre)

% Note that what is passed is 2x the radius
netcon = netconNearestNeighbors(2*radius, N_pre, N_post, removeRecurrentBool)


% Functions
IAMPA_IN_PYso_JB12(X,sAMPA,rec) = -gAMPA/normalizingFactor.*((res.*sAMPA)*netcon).*(X-EAMPA)

monitor IAMPA_IN_PYso_JB12

% This is the synaptic activity state variable
sAMPA' = alpha./(1 + exp(-(X_pre - 20)./2)) - sAMPA./tauS
sAMPA(0) = sAMPAIC+sAMPANoiseIC.*rand(1,N_pre)

% This is the 'resources' state variable
res' = (1 - res)./tauRes + max((t-tspike_pre)<=(2*dt)).*(-(1 - res)./tauRes + (deprFactor.*res - res)./dt)
res(0) = resIC-resNoiseIC.*rand(1,N_pre)

% Linker
@current += IAMPA_IN_PYso_JB12(X_post,sAMPA,res)