// Simulate Purkinje cell (Masoli et al.,2015) activity during calcium bursts
// (c) Xu (Shawn) Zhang, UConn
// xu.3.zhang@uconn.edu

load_file("nrngui.hoc")
load_file("Purkinje.hoc")

cvode.active(0)

objref f_trialname
f_trialname = new File()
f_trialname.ropen("trialname_caspike.txt")
strdef f_soma
f_trialname.scanstr(f_soma)
f_trialname.close()

objref f_tDCSparams
f_tDCSparams = new File()
f_tDCSparams.ropen("tDCSparams_caspike.txt")
ampparam = f_tDCSparams.scanvar()
f_tDCSparams.close()

// Inducing calcium spikes
stim0del = 200
stim0dur = 2500
stim0amp = 2

objref stim
soma stim = new IClamp(0.5)
stim.del = stim0del
stim.dur = stim0dur
stim.amp = stim0amp

// Record soma
objref rec_v_soma
rec_v_soma = new Vector()
rec_v_soma.record(&soma.v(0.5))

dt = 0.025
celsius = 37
tstop = 2700
v_init = -50

forall {
  insert extracellular
  insert xtrau
}

load_file("interpxyzu.hoc")	// only interpolates sections that have xtrau
load_file("calcrxcu.hoc")	// computes transfer r between segments and recording electrodes
load_file("calcd.hoc")	// computes scale factor used to calculate extracellular potential
			            //   produced by a uniform electrical field
load_file("setpointersu.hoc")	// automatically calls grindaway() in interpxyzu.hoc
load_file("zapstimu_DC.hoc")		// extracellular stimulus

setstim(0, 1e6, ampparam) // Positive: hyperpolarizing soma
changefield(0, 90, 100)

access soma
// load_file("vm.ses")

finitialize(v_init)
run()

// Save soma
objref sav_v_soma
sav_v_soma = new File()
sav_v_soma.wopen(f_soma)
rec_v_soma.printf(sav_v_soma)
sav_v_soma.close()

quit()