// this sets the variables used by LGMD_model files, funcs and procs.
// When creating new procedures, use local variables when possible.
// As needed add new global variables to the list below


/* --- Initialize builtin NEURON Settings --- */

secondorder=2			// sets the time integration method
v_init = -65			// initial membrane potential
steps_per_ms = 10		// number of points to plot per ms
dt = 0.02				// time step of simulations (in ms)	(must be compatible with steps_per_ms)
t = 0					// Neuron time (ms)
tstop=500				// time (ms) that simulation stops
realtime = 0			// real world time since simulation start (s)
screen_update_invl= 0.1	// how often to update plots

/* --- End builtin NEURON Settings --- */


/* --- Initialize Global variables for simulations --- */

verbosity = 2	// scalar. verbosity level (0-6). Sets how much to print to command line
if (verbosity > 2) printf("Verbosity level is %g \n", verbosity)
showGUI = 1		// boolean. whether to run "initgui.hoc" to open gui
msplit = 1		// boolean. whether to start multisplit parallel context
nmt = 8			// integer. number of threads to run if split (change based on system)

tstart=0		// the time (ms into simulation) that visual stimulus starts
nSection=0		// number of sections in the model
forall nSection+=1

/* --- End Global simulation variables --- */


// ---- Global strdef (there are no local strdef in hoc)
strdef NRNDIR			// NRNDIR is base directory of model
strdef syndir			// syndir is subdirectory containing synapse files
syndir = "synapse/"
strdef esynfullpath		// filepath of excitatory synapse file
strdef isynfullpath		// filepath of inhibitory synapse file
strdef DATADIR			// DATADIR is the directory where simulation data is saved
DATADIR = "data/"
strdef subdir			// data subdirectory
strdef strtmp, tmpstr	// tmp strings used by several procs & funcs (there are no local strdef in hoc)
strdef datafile			// name of data file being saved
strdef filename
strdef headerstr		// header at top of data file
strdef cwd				// working directory
strdef sect
strdef Hname, KDname
KDname = "KD"			// sets which KD-like channel to use
Hname = "h"				// sets which HCN channel to use
// ------


// ---- Global objref
objref objtmp[1]
objref StrFx			// StringFunction object 
objref sref				// SectionRef
objref MS_				// MechanismStandard
objref AREAS			// vector of section surface areas
objref GLS
objref KDchan,Hchan,Mchan,CaTchan,KAchan		// chanAnalysis objects for logging mean conductance
objref esyn, isyn		// lists of excitatory and inhibitory synapse objects
objref espont, ispont	// Lists of spontaneous synapse objects
esyn = new List()
isyn = new List()
objref syn, synlist		// List of all synapse objects inserted
synlist = new List()
objref synfilelist		// List of synapse filenames to include in batch sims


// I used the '_G_' suffix for many global objects to decrease the chances of name conflicts
objref idc_G_[2]			// Array of step currents (IClamp objects) used for dc holding current
objref simsecs_G_			// SectionList specifying sections from which to record simulation data
objref rvec_G_[1], tvec_G_	// time and data vectors to record during simulation
objref ivec_G_				// current vector to record
objref gvec_G_				// conductance and state vectors to record during simulation
objref vecList				// list of vectors (currently unused)
RecDt = 0.2					// Sample interval (ms) for saving recorded data to file (should be multiple of dt)
Gstep = 5					// How many time steps between measuring conductances
// ------


// SectionList objects to subdivide the LGMD
objref Ctines,Chandle,InhEnds
objref ParentBranch,ChildBranch
objref FieldA, TineBase
objref TineEnds
objref siz
objref KDList, HList, MList, CaTList, KAList
HList = new SectionList()
KDList = new SectionList()
KAList = new SectionList()
MList = new SectionList()
CaTList = new SectionList()

Ctines = new SectionList()		// Field C branches
Chandle = new SectionList()		// neurite connecting Field C to Handle
InhEnds = new SectionList()		// End compartments of Field B and C branches
ParentBranch = new SectionList()	// 
ChildBranch = new SectionList()
//FindBranches( ParentBranch, ChildBranch, FieldA)
TineBase = new SectionList()
// FindFieldBase( TineBase, "MainTrunk", "Tines" )
TineEnds = new SectionList()
// FindEnds(TineEnds,"Tines")

MakeSecList( siz, "soma", "SIZ")
MakeSecList( FieldA, "MainTrunk", "Tines")

// I use the '_G_' suffix for many global objects to decrease the chances of name conflicts
objref idc_G_[2]			// Array of step currents (IClamp objects) used for dc holding currents
objref simsecs_G_			// SectionList specifying sections from which to record simulation data
objref rvec_G_[1], tvec_G_	// time and data vectors to record during simulation
objref ivec_G_				// current vector to record
objref gvec_G_				// conductance and state vectors to record during simulation
objref vecList				// list of vectors (currently unused)
RecDt = 0.2					// Sample interval (ms) for saving recorded data to file (should be integer-multiple of dt)
Gstep = 5					// How many time steps between measuring conductances

simsecs_G_ = new SectionList()
MakeSecList( simsecs_G_,"soma[0]","Handle[30]","MainTrunk[0]","FieldC[15]","Tines[621]")

idc_G_[0] = new IClamp()
// Tines[1] idc_G_[1].loc(0.5)
idc_G_[0].loc(0.5)
idc_G_[0].amp=0
tvec_G_ = new Vector()
tvec_G_.record(idc_G_[0], &t, RecDt)
ivec_G_ = new Vector()
ivec_G_.label("nA")
gvec_G_ = new List()
// ------


/* --- Initialize Global variables for LGMD simulation --- */

loadsyn=2		// state. whether to preload synapse files for visual stimulus. 0=none, 1=set file, 2=set directory 
seclist=5		// state. sets which sections to add to SecList used by simulations
spontsyns=1		// boolean. whether visual stimuli should have spontaneous synaptic inputs

synCa = 3		// state. synapse causes Ca influx (value enumerates different effects of Ca influx)
recCa = 0		// boolean. whether to record cai instead of v
acn = 0			// boolean. whether the moddel has cyclic nucleotide dependent mechs
presyn=1		// boolean. Wether SimBatch procedure preloads synapses before starting sim

// tstart = 350	// time to start simulations (ms). Allows model to reach steady state
e4AP = 0.90		// effectiveness of 4AP application (0 = no effect, 1 = complete block)
eZD = 0.95		// effectiveness of ZD7288 application (0 = no effect, 1 = complete block)
eXE = 0.95		// effectiveness of XE991 application (0 = no effect, 1 = complete block)
tstart=0		// the time (ms into simulation) that stimulus starts
t0=0			// the time of collision for visual stimuli
nSection=0		// number of sections in the model
forall nSection+=1

AREAS = new Vector(nSection)
n=0
forall {
	AREAS.x[n] = area(0.5)
	n+=1
}
SA = AREAS.sum()	// total surface area of cell
/* --- end simulation globals */


/* --- Initialize Global membrane variables --- */

passive=0// state. 0=full active model, 1=no active conductances, 2=passive dendrites, 3=h and pas dendrites, 4=passive siz
gl = 7.0e-6		// leak conductance (S/cm2) (for FieldA, some other sections are set higher)
el = -67		// leak reversal potential (mV)
Cm = 0.60		// membrane capacitance (µF/cm2)
axial = 300		// axial resistivity (Ωcm) (some sections ignore this value)
Axax = 100		// axial resistivity of axon
SIZNa = 0.11	// sodium channel density at siz (S/cm2) (other sections scaled to this value)
SIZKdr = 1.0e-2	// Kdr channel density at siz (S/cm2) (other sections scaled to this value)
SIZM = 1.1e-3	// M channel density at siz (S/cm2) (other sections scaled to this value)
EK = -78		// K+ reversal potential (mV)
ENa = 50		// Na+ reversal potential (mV)
ECa = 100		// initial Ca2+ reversal potential (mV), changes with Ca2+ influx and eflux
Rabeforeg = 2	// 0: set condunctance then axial resistivity gradients, 1: set Ra then g, 2: no Ra gradient

//	in locust saline (mM): [Na] = 144, [K] = 5, [Ca]=5, [Cl]=157