//cp simplestim.hoc  loadcell.hoc: Tuomo: removed the running part
back=0

//////// load needed templates////////////
//if(!back){ load_file("nrngui.hoc")               }
if(!back){ load_file("template/ObliquePath.hoc") }
if(!back){ load_file("template/BasalPath.hoc"  ) }

objref econ,cvode
cvode = new CVode(1)
x=cvode.active(1)

restart=0
v_init=-70

strdef morphology_location, morpho_path, ObliqueTrunkSection, BasalTrunkSection
objref vRP, vAPEX

proc xopen_morphology(){
	sprint(morpho_path,"%s/%s",morphology_location,$s1)
	xopen(morpho_path)
}

// Carmen
morphology_location = "pc2b"
ObliqueTrunkSection = "trunk[17]"
BasalTrunkSection   = "trunk[7]"

xopen_morphology("cell.hoc")                          //reads morpholofy from the file
xopen_morphology("cell-analysis-simple.hoc") //reads simplified version of CA 


			// --------------- Creating lists-----------------

xopen("lib/TP-lib.hoc")
Tip_sections(apical_non_trunk_list,apical_trunk_list,"Apical")		
objref apical_tip_list
apical_tip_list=TP_list							// Apical Tip list

print "apical_tip_list"
apical_tip_list.printnames()
print  "END apical_tip_list"

objref tmp_pl[num_tips],pl[num_tips],opl[num_tips],degree_apical_tip,peri_trunk_list
objref bl[num_tips],obl[num_tips],degree_basal_tip
xopen("lib/Oblique-lib.hoc")
oblique_sections(apical_tip_list,apical_trunk_list,num_tips)		// apical dendrite path lists and degree of tips

xopen("lib/vector-distance.hoc")

//-----------------------------------------------------------------------------------------------------
printf("Setting up cell\n")                                         // load cell-setup to
xopen("cell-setup.hoc")                                             // specify all mechanisms, membrane properties etc





//Copied from ../almogmod/model.hoc (originally from the Hay model)
// $s1 section
func getLongestBranch(){local maxL,d localobj distallist,sref

  maxL = 0
  d = 0
  distallist = new SectionList()
  forsec $s1 {
    sref = new SectionRef()
    if (sref.nchild==0) distallist.append()
  }
  forsec distallist{
    d = distance(1)
    if(maxL<d) maxL = d
  }
  // for the soma case
  if (maxL == 0) {
    $s1 {
      maxL = L
    }
  }
  return maxL
}

//Copied from ../almogmod/model.hoc (originally from the Hay model)
// $s1 section
// $2 distance x in micrometers
// return list of [1,2] vectors  - of the appropriate section and the location in each vector
obfunc locateSites() {local maxL,site,d0,d1,siteX,i localobj vv,ll
  ll = new List()

  maxL = getLongestBranch($s1)
  site = $2
  i = 0
  forsec $s1 {
  if (distance(0) < distance(1)) {
      d0 = distance(0)
      d1 = distance(1)
    } else {
      d1 = distance(0)
      d0 = distance(1)
    }

    if (site <= d1 && site >= d0) {
      siteX = (site-d0)/(d1-d0)
      secNum = i
      vv = new Vector()
      ll.append(vv.append(secNum,siteX))
    }
    i = i+1
  }
  return ll
}