create dummy //a virtual segment recording electrode is connected actually to this dummy
//and the dummy is connected to the neuron at the desired position
//you can easily change the recording position by "reconnecting" the dummy  

dummy.L = 0.001
dummy.diam = 0.001
dummy.Ra = 1e-9
dummy.cm = 0



load_file("nrngui.hoc")
load_file("bjd1218.hoc")
load_file("CC.ses")
load_file("filter_settings.hoc")


forsec all{//custominit.hoc will set parameters properly
	insert hh_mod
		gl_hh_mod = 0

	insert pas
		g_pas=2e-005
		e_pas=-80
	
	Ra=150
	cm=1

	ena = 70	
	
	
	nseg = int((L/(0.1*lambda_f(5000))+.999)/2)*2 + 1 

	}

load_file("custominit.hoc")


//plots for the model amplifier voltage signal
objref vecpip, vecloc, vgpip, vgloc


vecloc = new Vector()
vecpip = new Vector()
vgloc = new Vector()
vgpip = new Vector()

vecloc.record(&dummy.v(0.5))
vecpip.record(&LinearCircuit[0].V_out_filtered)


objref Greal
Greal=new Graph()
Greal.unmap()
Greal.view(4, 10, -90, 120, 200, 10, 450, 300)
Greal.addvar("recorded voltage", &LinearCircuit[0].V_out_filtered, 2,3)
Greal.addvar("local voltage", &dummy.v(0.5), 5,3)

graphList[0].append(Greal)

Greal.size(1.5,5.5,-90, 65)



objref Gnative
Gnative=new Graph()
Gnative.unmap()
graphList[0].append(Gnative)



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



proc set_CPN(){// capacitance neutralization. The argument is the desired compensation in pF
desired_CPN=$01
if (desired_CPN == 0){
LinearCircuit[0].Acpn_Gain=(1e-9/1000)/LinearCircuit[0].Ccpn
}else{
LinearCircuit[0].Acpn_Gain=((desired_CPN/1000)/LinearCircuit[0].Ccpn)*0.965//see documentation of v7 CC
}

}

proc set_BB(){// bridge balance compensation. The argument is the desired compensation in MOhm.
desired_BB=$01
if (desired_BB == 0){
LinearCircuit[0].A_BB_Gain=1e-9
}else{
LinearCircuit[0].A_BB_Gain=desired_BB
}
}



////////////////////////////////////////////////////////////////////Stim////////////////

objref vecstim
vecstim = new Vector()  //generates a vector that will be playd in the CC amplifier as command waveform
//of the current generator. Signal is in mV, Rf converts it to current (with 500 mOhm Rf, the conversion factor is 0.5 V/nA)   

proc vecstim_gen(){
	vecstim.resize(0)
	for i = 0, (2/0.004)-1{
		vecstim.append(0)
	}
	for i = 0, (3/0.004)-1{
		vecstim.append($1/2)  //conversion factor of 0.5 V/nA 
	}
	for i = 0, (2/0.004)-1{
		vecstim.append(0)
	}
	vecstim.play(&LinearCircuit[0].Vcmd, 0.004)  
}



proc isoneAP(){local scaleup

}


//////////////////////////////////////////////////////////////////////IClamp/////////////////////////////////////////////////////////////////////////////////////////////////

objref icl


proc Iclamp1(){							//built-in CC mechanism to evoke APs after removal of the experimental rig
	forsec all{e_pas=baseline_with_rig}//to eliminate the effect of seal induced depolarization, we 
	v_init=baseline_with_rig			// v_init and e_pas is adjusted in native AP simulations to reproduce the baseline with experimental rig 

	flagicl = 0
	LinearCircuit[0].R_rest = 1e9

	icl = new IClamp(.5)////
	icl.amp = $01
	icl.del = 2
	icl.dur = 3
	run()
	LinearCircuit[0].R_rest = 1e-9
	icl.amp = 0	
}



///////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////preconfigured recording positions/////////////////////////////////////////////////////////


soma distance(0 , .5)


proc largest(){
			Gnative.unmap()
			v_init=-80
			soma connect dummy (1),.5
			access soma
			forsec all{e_pas=-80}	
			vecstim_gen(500)
			run()
			baseline_with_rig=vecloc.mean(0,int(1.95/0.004))
			vgpip=vecpip.c()			
			vgloc=vecloc.c()
			isoneAP()
			Gnative.view(4, 10, -90, 120, 200, 500, 450, 300)
			Gnative.addvar("native voltage", &dummy.v(0.5), 4,3)
			vgloc.plot(Gnative,dt,5,3)
			vgpip.plot(Gnative,dt,2,3)
			graphList[0].append(Gnative)
			Gnative.size(1.5,5.5,-90, 65)
			Iclamp1(.510)
						print "diameter: ", diam(.5), "distance: ", distance(.5)

	}
	
	
proc larger(){
			Gnative.unmap()
			v_init=-80
			axon[458] connect dummy (1),.5
			access axon[458]
			forsec all{e_pas=-80}	
			vecstim_gen(140)
			run()
			baseline_with_rig=vecloc.mean(0,int(1.95/0.004))
			vgpip=vecpip.c()			
			vgloc=vecloc.c()
			isoneAP()
			Gnative.view(4, 10, -90, 120, 200, 500, 450, 300)
			Gnative.addvar("native voltage", &dummy.v(0.5), 4,3)
			vgloc.plot(Gnative,dt,5,3)
			vgpip.plot(Gnative,dt,2,3)
			graphList[0].append(Gnative)
			Gnative.size(1.5,5.5,-90, 65)
			Iclamp1(.131)
						print "diameter: ", diam(.5), "distance: ", distance(.5)

	}

proc small(){
			Gnative.unmap()
			v_init=-80
			axon[441] connect dummy (1),.5
			access axon[441]
			forsec all{e_pas=-80}	
			vecstim_gen(70)
			run()
			baseline_with_rig=vecloc.mean(0,int(1.95/0.004))
			vgpip=vecpip.c()			
			vgloc=vecloc.c()
			isoneAP()
			Gnative.view(4, 10, -90, 120, 200, 500, 450, 300)
			Gnative.addvar("native voltage", &dummy.v(0.5), 4,3)
			vgloc.plot(Gnative,dt,5,3)
			vgpip.plot(Gnative,dt,2,3)
			graphList[0].append(Gnative)
			Gnative.size(1.5,5.5,-90, 65)
			Iclamp1(.0548)
						print "diameter: ", diam(.5), "distance: ", distance(.5)

	}
	
proc smallest(){
			Gnative.unmap()
			v_init=-80
			axon[435] connect dummy (1),.5
			access axon[435]
			forsec all{e_pas=-80}	
			vecstim_gen(71)
			run()
			baseline_with_rig=vecloc.mean(0,int(1.95/0.004))
			vgpip=vecpip.c()			
			vgloc=vecloc.c()
			isoneAP()
			Gnative.view(4, 10, -90, 120, 200, 500, 450, 300)
			Gnative.addvar("native voltage", &dummy.v(0.5), 4,3)
			vgloc.plot(Gnative,dt,5,3)
			vgpip.plot(Gnative,dt,2,3)
			graphList[0].append(Gnative)
			Gnative.size(1.5,5.5,-90, 65)
			Iclamp1(.0545)
						print "diameter: ", diam(.5), "distance: ", distance(.5)
	}

/////////////////////////////////////////////////GUI//////////////////////////


xpanel("simulated experiments ")

				xlabel("simulated real experiments ")
				xlabel("")			
				xlabel("choose one of the recording positionlisted below")
	
				xlabel("First, pipette- and local voltage appears")
				xlabel("on the upper graph. After the first run")
				xlabel("amplifer and pipette will be disconnected from")	
				xlabel("the axon and native axonal voltage appears on the")
				xlabel("lower graph together with the previously simulated")
				xlabel("local and measure waveforms")
				xlabel("")
				xlabel("instrumental parameters are the same in all cases")
				xlabel("R_access=60 MOhm, C_tot=6.74 pF")				
				xlabel("BB compensation=60 MOhm, CPN compensation76.8 pF")
				xlabel("")		
				xlabel("")	
				xlabel("")					
                xbutton("somatic recording","largest()")
                xbutton("large MFB recording","larger()")				
                xbutton("small MF axon recording 1","small()")  
                xbutton("small MF axon recording 2","smallest()")  

		
xpanel(850,200)





set_filter(100,100)
set_BB(60)
set_CPN(6.8)