#Thalamic model

param g_LK=0.0
param g_h=0.025

#Time constants for excitatory and inhibitory neurons in ms
!tau=20

#Conductances 
!g_L=1

!g_T_t=3
!g_T_r=2.3

!g_AMPA=1
!g_GABA=1

#Reversal potential of excitatory and inhibitory synapses in mV
!E_AMPA=0
!E_GABA=-70

# Reversal potential of other currents
!E_L=-70
!E_K=-100 
!E_Ca=120
!E_h=-40

#Maximum firing rate in ms^-1
!Q_max=400E-3

#Sigmoid threshold in mV
!theta=-58.5

#Slope for sigmoid in mV
!sigma=6

#Scaling parameter for sigmoidal mapping(dimensionless)
!C=(pi/sqrt(3))

#PSC rise time in ms^-1
!gamma_t=70E-3
!gamma_r=100E-3

#Connectivities
!N_tr=3
!N_rt=5
!N_rr=25

#Calcium dynamic
!alpha_Ca=-51.8E-6
!tau_Ca=10
!Ca_0=2.4E-4

#Parameters of h-current
!k1= 2.5E7
!k2= 4E-4
!k3= 1E-1
!k4= 1E-3
!n_P= 4
!g_inc= 2

#Functions
#Firing rates
Qt(Vt)=Q_max/(1+exp(-C*(Vt-theta)/sigma))
Qr(Vr)=Q_max/(1+exp(-C*(Vr-theta)/sigma))

#Activation functions
m_inf_T_t(Vt)=1/(1+exp(-(Vt+59)/6.2))
m_inf_T_r(Vr)=1/(1+exp(-(Vr+52)/7.4))
h_inf_T_t(Vt)=1/(1+exp((Vt+81)/4))
h_inf_T_r(Vr)=1/(1+exp((Vr+80)/5))

tau_h_T_t(Vt)=(30.8+(211.4+exp((Vt+115.2)/5))/(1+exp((Vt+86)/3.2)))/3.7371928
tau_h_T_r(Vr)=(85+1/(exp((Vr+48)/4)+exp(-(Vr+407)/50)))/3.7371928

m_inf_h(Vt)=1/(1+exp((Vt+75)/5.5))
tau_m_h(Vt)=(20+1000/(exp((Vt+71.5)/14.2)+exp(-(Vt+89)/11.6)))

#Synaptic currents
I_tr(Vr,s_er)=s_er*(Vr-E_AMPA)
I_rt(Vt,s_rt)=s_rt*(Vt-E_GABA)
I_rr(Vr,s_rr)=s_r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,total=3000,dt=0.1,meth=rungekutta,njmp=10,bound=200
@ YLO=-80,YHI=0,XLO=0,XHI=3000
@ nmax=80000,npr=0,ntst=100,epsl=1e-9,epss=1e-7,epsu=1e-7,parmin=0,parmax=0.1,
@ autoxmin=0,autoxmax=0.1,autoymin=-100,autoymax=0, ds=1e-3,dsmin=1e-5,dsmax=0.005
done