% # imGluRKLeak_TRN_PYso:
%
% Synaptic mGluR1-mediated leakage/resistance decrease used in thalamic cells
% in (Crunelli et al., 2011).
%
% Note that this is replacing the KLeak current, but not the regular Leak
% current.
%
% - References:
%     - Crunelli V, Errington AC, Hughes SW, Toth TI. The thalamic
%     low-threshold Ca2+ potential: a key determinant of the local and global
%     dynamics of the slow (<1 Hz) sleep oscillation in thalamocortical
%     networks. Philosophical Transactions of the Royal Society A:
%     Mathematical, Physical and Engineering Sciences. 2011;369: 3820–3839.
%     doi:10.1098/rsta.2011.0126
%
% Tags: synapse, connection, leak
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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:
%
% 5.0 nS / 1.43e-4 cm^2 (thalamic reticular cell area) * ...
% 1 mS / 1000000 nS = ...
% 0.034 mS/cm^2
%
gmGluRKLeak = 0.034 % mS/cm^2
EmGluRKLeak = -100  % mV
alpha = 0.11    % ms^-1 rate of rise
beta =  9.21e-4 % ms^-1 rate of decay

smGluRKLeakIC = 0.1
smGluRKLeakNoiseIC= 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 = 0
% 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
imGluRKLeak_TRN_PYso(X,smGluRKLeak) = -gmGluRKLeak/normalizingFactor.*(smGluRKLeak*netcon).*(X-EmGluRKLeak)

monitor imGluRKLeak_TRN_PYso

% ODEs and ICs
smGluRKLeak' = alpha.*(0.5.*(1 + tanh(X_pre./4))).*(1-smGluRKLeak) - beta.*smGluRKLeak;
smGluRKLeak(0) = smGluRKLeakIC+smGluRKLeakNoiseIC.*rand(1,N_pre);

% Linker
@current += imGluRKLeak_TRN_PYso(X_post,smGluRKLeak)