// Injects hyperpolarising pulses into model cell
// cf. Figure 1(A) Whyment et al. 2011, Figure X Pickering et al. 2013
// Created Linford Briant October 2013

load_file("Cell.hoc")

tstop=8000
steps_per_ms=1
dt=1

celcius=20

proc init() {
finitialize(-55)
/*
if (cvode.active()) {
cvode.re_init()
} else {fcurrent()
}
frecord_init()
*/
}

// Do not want spiking currents
access SPNcells[0].soma
SPNcells[0].soma.gbar_na3=0
SPNcells[0].axon.gbar_na3=0

num=2
objectvar stim[num]
for i=0,num-1 {stim[i]=new IClamp(0.5)}
stim[0].amp=0
stim[0].dur=1000
stim[0].del=4000
stim[1].amp=0
stim[1].dur=tstop
stim[1].del=0

proc stimAMP() {
stim[0].amp=$1
}

// Following sets up all recording vectors, the time vector, and the database matrices:

nstim=201

objectvar IArec[nstim], grec[nstim], vrec[nstim], EKrec[nstim]
for i=0, nstim-1 IArec[i]=new Vector()
for i=0, nstim-1 grec[i]=new Vector()
for i=0, nstim-1 vrec[i]=new Vector()
for i=0, nstim-1 EKrec[i]=new Vector()

stimAMP(0)
grec[0].record(&SPNcells[0].soma.gka_borgka(0.5))
vrec[0].record(&SPNcells[0].soma.v(0.5))
run()

// Sets up vector which is the A-current rather than the A-conductance:

objectvar DataM, DataN, DataK, M, N, K
M=new Matrix(vrec[0].size(),204)
N=new Matrix(vrec[0].size(),204)
K=new Matrix(vrec[0].size(),204)
N.setcol(1,grec[0])
K.setcol(1,vrec[0])

EKrec[0]=vrec[0].sub(ek)
IArec[0]=grec[0].mul(EKrec[0])

M.setcol(1,IArec[0])

objectvar vdt
vdt = new Vector()
vdt.resize(vrec[0].size())
vdt.indgen(dt)

M.setcol(0,vdt)
N.setcol(0,vdt)
K.setcol(0,vdt)

// Now that matrices and protocol have been set up, we can run the main procudure:

for i=1, nstim-1 {

print i

stimAMP(-0.1-i*0.005)

grec[i].record(&SPNcells[0].soma.gka_borgka(0.5))
vrec[i].record(&SPNcells[0].soma.v(0.5))

run()

N.setcol(i+1,grec[i])
K.setcol(i+1,vrec[i])

// Need to set columns for these two matrices before IA else they get overwritten.

EKrec[i]=vrec[i].sub(ek)
IArec[i]=grec[i].mul(EKrec[i])

M.setcol(i+1,IArec[i])

}

DataN=new File()
DataN.wopen("HyperpolarisingCurrents_Gka.dat")
N.fprint(DataN, " %g")
DataN.close("HyperpolarisingCurrents_Gka.dat")

DataK=new File()
DataK.wopen("HyperpolarisingCurrents_V.dat")
K.fprint(DataK, " %g")
DataK.close("HyperpolarisingCurrents_V.dat")

DataM=new File()
DataM.wopen("HyperpolarisingCurrents_IA.dat")
M.fprint(DataM, " %g")
DataM.close("HyperpolarisingCurrents_IA.dat")