setuptime = startsw()
{xopen("hoc/defvar.hoc")}

// load balancing implies several runs.
// 0) load balancing just plain off (but will read mcomplex.dat if exists)
// 1) create a mcomplex.dat file (only one cpu needed)
// 2) create single split balance.dat file (any number of cpus can be used)
//  manually: run the balcomp program to create balance.ncpu files
// 3) run with single split load balancing on (ncpu corresponding to files from balcomp)
// add more for whole cell and multisplit load balancing
// 4) create whole cell load balance file
// 5) run with whole cell load balance file
// 6) create multisplit load balance information file
// 7) run using multisplit load balance

default_var("load_balance_phase", 0)
default_var("one_tenth_ncell", 1)
default_var("use_gap", 1)
default_var("use_ectopic", 1)
default_var("use_inject", 1)
default_var("awake", 0)
default_var("default_delay", 0.5)
default_var("ranseedbase", 1)
default_var("use_traubexact", 0) // will be reset to 0 if load balance
default_var("wholecell_prefix", "cxwhole")
default_var("multisplit_prefix", "cx")
default_var("multisplit_nhost", 256)
default_var("msoptfactor", 0.8)
default_var("nthread", 1)
default_var("savestatetest", 0)


default_var("spike_compress", 5)
default_var("cacheeffic", 1)  // for multisplit it is always 1
default_var("multisend", 0)
default_var("selfevents", 0)

default_var("fakerank", -1)
default_var("fakenhost", -1)

default_var("jordan_trace",0)

default_var("jEFP1x",0)
default_var("jEFP1y",500)
default_var("jEFP1z",0)
default_var("sigmae",0.3*10^-6)



strdef varfn
sprint(varfn, "stndbs.dat")

objref tfn[14]
strdef trace
objref Jtrace[14]
 
 
objref IN_MATRIX
objref input_file2
IN_MATRIX = new Matrix() 		// N = size of IN_VECTOR
input_file2 = new File()
input_file2.ropen("Iintra.dat") 		// Same file.dat as exported from MATLAB
IN_MATRIX.scanf(input_file2,10,800001)
input_file2.close()


if (jordan_trace == 1){
{sprint(trace, "data_stndbs_4.5hz/traces/trace_suppyrRS_%s",varfn)}
tfn[0] = new String(trace)
{sprint(trace, "data_stndbs_4.5hz/traces/trace_suppyrFRB_%s",varfn)}
tfn[1] = new String(trace)
{sprint(trace, "data_stndbs_4.5hz/traces/trace_supaxax_%s",varfn)}
tfn[2] = new String(trace)
{sprint(trace, "data_stndbs_4.5hz/traces/trace_spinstell_%s",varfn)}
tfn[3] = new String(trace)
{sprint(trace, "data_stndbs_4.5hz/traces/trace_tuftIB_%s",varfn)}
tfn[4] = new String(trace)
{sprint(trace, "data_stndbs_4.5hz/traces/trace_tuftRS_%s",varfn)}
tfn[5] = new String(trace)
{sprint(trace, "data_stndbs_4.5hz/traces/trace_nontuftRS_%s",varfn)}
tfn[6] = new String(trace)
{sprint(trace, "data_stndbs_4.5hz/traces/trace_deepaxax_%s",varfn)}
tfn[7] = new String(trace)
{sprint(trace, "data_stndbs_4.5hz/traces/trace_deepbask_%s",varfn)}
tfn[8] = new String(trace)
{sprint(trace, "data_stndbs_4.5hz/traces/trace_supbask_%s",varfn)}
tfn[9] = new String(trace)
{sprint(trace, "data_stndbs_4.5hz/traces/trace_supLTS_%s",varfn)}
tfn[10] = new String(trace)
{sprint(trace, "data_stndbs_4.5hz/traces/trace_deepLTS_%s",varfn)}
tfn[11] = new String(trace)
{sprint(trace, "data_stndbs_4.5hz/traces/trace_TCR_%s",varfn)}
tfn[12] = new String(trace)
{sprint(trace, "data_stndbs_4.5hz/traces/trace_nRT_%s",varfn)}
tfn[13] = new String(trace)
for i = 0, 13 {
	Jtrace[i] = new Vector()
}
}

objref myout
objref tvec
tvec = new Vector()

objref EF[1]
EF[0] = new Vector()

objref EFtvec
EFtvec = new Vector()
double efmm[12]
double efm[1]


gfac_AMPA = 1
gfac_NMDA = 1
gfac_GABAA = 1

use_p2c_net_connections = 0 // not 0, requires p2c emitted  map and compmap files

{localloadfile("manage_setup_stndbs_4.5hz.hoc")}

steps_per_ms = 40
dt = .025
secondorder = 0
default_var("mytstop", 20000)
tstop = mytstop

pc.timeout(0)


//{load_file("balanal.hoc")}
//if (pc.nhost >= 128) {thread_per_piece()}

if (0) { pc.runworker()  pc.done() quit() }

//{finitialize(-65) cvode_local(1) cvode.atol(1e-4)} // the finitialze avoids /0 in BREAKPOINT

prun()

//if (pc.nhost >= 128) {balanalfile("balanal.dat")}

endtime = startsw()

if (pc.id == 0) { print "tstop = ", tstop }
if (pc.id == 0) { print "RunTime: ", runtime }

//{localloadfile("prcellstate.hoc")}
proc pcs() {local spgid
	spgid = thishost_gid($1)
	if (spgid >= 0) prcellstate(load_balance_phase, spgid)
	pc.runworker()  pc.done() quit()
}
//pcs(103)

spike2file()

if (pc.nhost > 5) {cvode_active(1)} // to count equations
{pc.runworker()}

print "Maximum integration interval: ", mindelay()
getstat()
prhist()
print_spike_stat_info()

{pc.done()}

perf2file()
endtime = startsw() - endtime
print "endtime ", endtime

// if (!serial) { quit() }