// Geometry-induced features of current transfer in neuronal dendrites with tonically activated conductances
// Sergey M. Korogod, Irina B. Kulagina
// Biol. Cybern. 79, 231-240 (1998)

objectvar VRGraph, JRGraph, IRGraph

// define membrane mechanisms
proc SetMembrane() {
  strdef OutLine
  OutLine = "Soma {insert PasSA}"
  execute1(OutLine)
  for i=0, 2 {
    sprint(OutLine, "Dendrite[%d] {insert PasS insert hh1}", i)
    execute1(OutLine)
  }
} // SetMembrane()

func CalcEq() { local Gm, Eq
  Gm = gs_PasS($1) + gl_hh1($1) + gna_hh1($1) + gk_hh1($1)
  Eq = ((gs_PasS($1)/Gm)*es_PasS($1)) + ((gl_hh1($1)/Gm)*el_hh1($1)) + ((gna_hh1($1)/Gm)*ena($1)) + ((gk_hh1($1)/Gm)*ek($1))
  return Eq
} // CalcEq()

// make graphics for Fig.5. E (symmetric)
proc MakeVRGraph() {
  strdef OutLine
  VRGraph = new Graph(0)
  VRGraph.xaxis()
  VRGraph.yaxis()
  RRGraph = new RangeVarPlot("v")
  OutLine = "Dendrite[0] RRGraph.begin(0) Dendrite[1] RRGraph.end(1)"
  execute1(OutLine)
  VRGraph.addobject(RRGraph, 1, 1, 1, 1)
  RRGraph = new RangeVarPlot("v")
  OutLine = "Dendrite[0] RRGraph.begin(0) Dendrite[2] RRGraph.end(1)"
  execute1(OutLine)
  VRGraph.addobject(RRGraph, 1, 1, 1, 1)
  RRGraph = new RangeVarPlot("CalcEq($1)")
  OutLine = "Dendrite[0] RRGraph.begin(0) Dendrite[1] RRGraph.end(1)"
  execute1(OutLine)
  VRGraph.addobject(RRGraph, 3, 1, 1, 3)
  RRGraph = new RangeVarPlot("CalcEq($1)")
  OutLine = "Dendrite[0] RRGraph.begin(0) Dendrite[2] RRGraph.end(1)"
  execute1(OutLine)
  VRGraph.addobject(RRGraph, 3, 1, 1, 3)
  VRGraph.size(0, 800, -56, -50)
  VRGraph.view(0, -56.1, 800, 6.1, 50, 200, 250.0, 215.0)
  VRGraph.label(0.5, 1, "E", 2, 1, 0, 1, 1)
  VRGraph.label(0.05, 1, "mV", 2, 1, 0, 1, 1)
  VRGraph.label(0.9, 0.18, "um", 2, 1, 0, 1, 1)
  VRGraph.label(0.9, 0.60, "Eq", 2, 1, 0, 1, 3)
  VRGraph.label(0.9, 0.45, "E", 2, 1, 0, 1, 1)
  flush_list.append(VRGraph)
  VRGraph.save_name("flush_list.")
  objectvar RRGraph
} // MakeVRGraph()

func CalcJm() { local Gm, Eq, Jm // uA/cm2
  Gm = gs_PasS($1) + gl_hh1($1) + gna_hh1($1) + gk_hh1($1)
  Eq = ((gs_PasS($1)/Gm)*es_PasS($1)) + ((gl_hh1($1)/Gm)*el_hh1($1)) + ((gna_hh1($1)/Gm)*ena($1)) + ((gk_hh1($1)/Gm)*ek($1))
  Jm = (Gm * (v($1) - Eq)) * 1000
  return Jm
} // CalcJm()

// make graphics for Fig.5. F (symmetric)
proc MakeJRGraph() {
  strdef OutLine
  JRGraph = new Graph(0)
  JRGraph.xaxis()
  JRGraph.yaxis()
  RRGraph = new RangeVarPlot("CalcJm($1)")
  OutLine = "Dendrite[0] RRGraph.begin(0) Dendrite[1] RRGraph.end(1)"
  execute1(OutLine)
  JRGraph.addobject(RRGraph, 1, 1, 1, 1)
  RRGraph = new RangeVarPlot("CalcJm($1)")
  OutLine = "Dendrite[0] RRGraph.begin(0) Dendrite[2] RRGraph.end(1)"
  execute1(OutLine)
  JRGraph.addobject(RRGraph, 1, 1, 1, 1)
  JRGraph.size(0, 800, -2, 0)
  JRGraph.view(-20, -2.05, 820, 2.05, 400, 200, 250.0, 215.0)
  JRGraph.label(0.5, 1, "F", 2, 1, 0, 1, 1)
  JRGraph.label(0.05, 1, "uA/cm2", 2, 1, 0, 1, 1)
  JRGraph.label(0.9, 0.18, "um", 2, 1, 0, 1, 1)
  JRGraph.yaxis(3)
  JRGraph.xaxis(0, 800, -2, 4, 0, 0, 1)
  JRGraph.yaxis(-2, 0, 0, 2, 0, 0, 1)
  flush_list.append(JRGraph)
  JRGraph.save_name("flush_list.")
  objectvar RRGraph
} // MakeJRGraph()

func CalcI() { local Gm, Eq, Jm, Im // 0.01*pA/um
  Gm = gs_PasS($1) + gl_hh1($1) + gna_hh1($1) + gk_hh1($1)
  Eq = ((gs_PasS($1)/Gm)*es_PasS($1)) + ((gl_hh1($1)/Gm)*el_hh1($1)) + ((gna_hh1($1)/Gm)*ena($1)) + ((gk_hh1($1)/Gm)*ek($1))
  Jm = (Gm * (v($1) - Eq)) * 1000
  Im = -(PI * diam($1) * Jm)
  return Im
} // CalcI()

// make graphics for Fig.5. G (symmetric)
proc MakeIRGraph() {
  strdef OutLine
  IRGraph = new Graph(0)
  IRGraph.xaxis()
  IRGraph.yaxis()
  RRGraph = new RangeVarPlot("CalcI($1)")
  OutLine = "Dendrite[0] RRGraph.begin(0) Dendrite[1] RRGraph.end(1)"
  execute1(OutLine)
  IRGraph.addobject(RRGraph, 1, 1, 1, 1)
  RRGraph = new RangeVarPlot("CalcI($1)")
  OutLine = "Dendrite[0] RRGraph.begin(0) Dendrite[2] RRGraph.end(1)"
  execute1(OutLine)
  IRGraph.addobject(RRGraph, 1, 1, 1, 1)
  IRGraph.size(0, 800, 0, 30)
  IRGraph.view(0, -0.2, 800, 30.2, 750, 200, 250.0, 215.0)
  IRGraph.label(0.5, 1, "G", 2, 1, 0, 1, 1)
  IRGraph.label(0.05, 1, "0.01 pA/um", 2, 1, 0, 1, 1)
  IRGraph.label(0.9, 0.18, "um", 2, 1, 0, 1, 1)
  flush_list.append(IRGraph)
  IRGraph.save_name("flush_list.")
  objectvar RRGraph
} // MakeIRGraph()

proc Destroy() {
  VRGraph.unmap()
  JRGraph.unmap()
  IRGraph.unmap()
} // Destroy()

proc MainExec() {
  GetModelTopology(6)
  SetMembrane()
  OutLine = "access Soma"
  execute1(OutLine)
  celcius = 6.3
  tstop = 100
  MakeVRGraph()
  MakeJRGraph()
  MakeIRGraph()
} // MainExec()

MainExec()