/* In our experiments we understand that voltage dependent change in
the condunctance of NMDAR alone cannot explain the voltage-dependnent change 
in the time constant: we understand that in a triple-exponential model
tau1 is voltage independent but tau2 and tau3 are voltage-dependent
Both tau2 and tau3 rise exponentially to a maximum in responce to voltage

In this experiment we want to find an estimate of these parameters in CA1 pyramidal cells

note: net_send(590, 1) command in the SynExp4NMDA2.mod file help us reset the model 
after 590 ms from the start of the simulation so that we can fit our model 
to both recordings at the same time
*/
load_file("nrngui.hoc")
load_file("params.hoc")

//fully activates cache efficiency
cvode.cache_efficient(1)  

tstop = 1200
dt = .025
celsius = 18	//room temperature in spruston95 experiment
v_init = 40

Tau1 = 1.0232
Tau2_0 = 33.755
A2 = 0.42299
Tau3_0 = 225.5
A3 = 10.007
SynWeight = 0.16464

// ------------------
create soma
access soma

objref sNMDA, stim, nc
stim = new NetStim(.5)
	stim.interval = 600	//ms (mean) time between spikes
	stim.number = 3	//(average) number of spikes
	stim.start 	= 0	//ms (most likely) start time of first spike
	stim.noise 	= 0		//---- range 0 to 1. Fractional randomness.
	//0 deterministic, 1 intervals have negexp distribution.
	
// this is an especial version of our model in which model would reset itself 
// after 590 ms from the start of simulation
sNMDA = new Exp4NMDA2(.5)
nc = new NetCon(stim, sNMDA)

proc init_NMDA() {
	sNMDA.tau1 = Tau1
	sNMDA.a2 = A2
	sNMDA.tau2_0 = Tau2_0
	sNMDA.a3 = A3
	sNMDA.tau3_0 = Tau3_0
	
	nc.weight = SynWeight
	nc.delay = .001
}
objref FinNMDA
FinNMDA = new FInitializeHandler(3,"init_NMDA()")
	
//----------------------------------------------------	
objref vc, vscr, tvec

vscr = new Vector(12)
tvec = new Vector(12)

vscr.x[0] = 40
tvec.x[0] = 0

vscr.x[1] = -80
tvec.x[1] = 510

vscr.x[2] = 40
tvec.x[2] = 647.175

vscr.x[3] = -80
tvec.x[3] = 652.743

vscr.x[4] = 40
tvec.x[4] = 745.74

vscr.x[5] = -80
tvec.x[5] = 751.352

vscr.x[6] = 40
tvec.x[6] = 845.027

vscr.x[7] = -80
tvec.x[7] = 850.629

vscr.x[8] = 40
tvec.x[8] = 944.315

vscr.x[9] = -80
tvec.x[9] = 950.179

vscr.x[10] = 40
tvec.x[10] = 1044.6659

vscr.x[11] = -80
tvec.x[11] = 1049.6795

vc = new VClamp(.5)
	vc.dur[0] = tstop
	vscr.play(&vc.amp[0],tvec)

objref iNMDA, vSoma
iNMDA = new Graph()
iNMDA.size(0,tstop,-1,50)
iNMDA.addvar("sNMDA.i",3,0)
iNMDA.save_name("graphList[0].")
graphList[0].append(iNMDA)

vSoma = new Graph()
vSoma.size(0,tstop,-100,50)
vSoma.addvar("soma.v(.5)",3,0)
vSoma.save_name("graphList[0].")
graphList[0].append(vSoma)

load_file("MRFExp4SynFull.ses")

// objref iNMDA, iFile, time, tFile
// iNMDA = new Vector()
// iNMDA.record(&sNMDA.i)
// iFile = new File("iFile.dat")
// iFile.wopen("iFile.dat")

// time = new Vector()
// time.record(&t)
// tFile = new File("tFile.dat")
// tFile.wopen("tFile.dat")

// init()
// run()

// iNMDA.printf(iFile)
// iFile.close()

// time.printf(tFile)
// tFile.close()