// 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, CRGraph

// define membrane mechanisms
proc SetMembrane() {
  strdef OutLine
  OutLine = "Soma {insert PasSA}"
  execute1(OutLine)
  OutLine = "Axon {insert PasSA}"
  execute1(OutLine)
  OutLine = "Dendrite {insert PasS insert hh1}"
  execute1(OutLine)
} // SetMembrane()

func CalcEq() { local Gm, Eq
  if (issection("Dendrite")) {
    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))
  } else {
    Eq = erev_PasSA($1)
  }
  return Eq
} // CalcEq()

// make graphics for Fig.1. B
proc MakeVRGraph() {
  strdef OutLine
  VRGraph = new Graph(0)
  VRGraph.xaxis()
  VRGraph.yaxis()
  RRGraph = new RangeVarPlot("v")
  OutLine = "Axon RRGraph.begin(0) Dendrite RRGraph.end(1)"
  execute1(OutLine)
  VRGraph.addobject(RRGraph, 1, 1, 1, 1)
  RRGraph = new RangeVarPlot("CalcEq($1)")
  OutLine = "Axon RRGraph.begin(0) Dendrite RRGraph.end(1)"
  execute1(OutLine)
  VRGraph.addobject(RRGraph, 3, 1, 1, 3)
  VRGraph.size(-200, 800, -70, -30)
  VRGraph.view(-200, -72, 1000, 42, 268, 200, 300.0, 215.0)
  VRGraph.label(0.5, 1, "B", 2, 1, 0, 1, 1)
  VRGraph.label(0.05, 1, "mV", 2, 1, 0, 1, 1)
  VRGraph.label(0.9, 0.21, "um", 2, 1, 0, 1, 1)
  VRGraph.label(0.9, 0.55, "Eq", 2, 1, 0, 1, 3)
  VRGraph.label(0.9, 0.49, "E", 2, 1, 0, 1, 1)
  VRGraph.yaxis(3)
  VRGraph.xaxis(-200, 800, -70, 10, 0, 0, 1)
  VRGraph.yaxis(-70, -30, -200, 4, 0, 0, 1)
  flush_list.append(VRGraph)
  VRGraph.save_name("flush_list.")
  objectvar RRGraph
} // MakeVRGraph()

func CalcGs() { // mS/cm2
  return gs_PasS($1) * 1000
} // CalcGs()

func CalcGl() { // mS/cm2
  return gl_hh1($1) * 1000
} // CalcGl()

func CalcGNa() { // mS/cm2
  return gna_hh1($1) * 1000
} // CalcGNa()

func CalcGK() { // mS/cm2
  return gk_hh1($1) * 1000
} // CalcGK()

func CalcGhh() { // mS/cm2
  return (gl_hh1($1) + gna_hh1($1) + gk_hh1($1)) * 1000
} // CalcGhh()

func CalcGm() { // mS/cm2
  return ((gs_PasS($1) + gl_hh1($1) + gna_hh1($1) + gk_hh1($1)) * 1000)
} // CalcGm()

// make graphics for Fig.1. D
proc MakeCRGraph() {
  strdef OutLine
  CRGraph = new Graph(0)
  CRGraph.xaxis()
  CRGraph.yaxis()
  RRGraph = new RangeVarPlot("CalcGs($1)")
  OutLine = "Dendrite RRGraph.begin(0) Dendrite RRGraph.end(1)"
  execute1(OutLine)
  CRGraph.addobject(RRGraph, 2, 1, 1, 2)
  RRGraph = new RangeVarPlot("CalcGl($1)")
  OutLine = "Dendrite RRGraph.begin(0) Dendrite RRGraph.end(1)"
  execute1(OutLine)
  CRGraph.addobject(RRGraph, 1, 1, 1, 1)
  RRGraph = new RangeVarPlot("CalcGNa($1)")
  OutLine = "Dendrite RRGraph.begin(0) Dendrite RRGraph.end(1)"
  execute1(OutLine)
  CRGraph.addobject(RRGraph, 4, 1, 1, 4)
  RRGraph = new RangeVarPlot("CalcGK($1)")
  OutLine = "Dendrite RRGraph.begin(0) Dendrite RRGraph.end(1)"
  execute1(OutLine)
  CRGraph.addobject(RRGraph, 7, 1, 1, 7)
  RRGraph = new RangeVarPlot("CalcGhh($1)")
  OutLine = "Dendrite RRGraph.begin(0) Dendrite RRGraph.end(1)"
  execute1(OutLine)
  CRGraph.addobject(RRGraph, 5, 1, 1, 5)
  RRGraph = new RangeVarPlot("CalcGm($1)")
  OutLine = "Dendrite RRGraph.begin(0) Dendrite RRGraph.end(1)"
  execute1(OutLine)
  CRGraph.addobject(RRGraph, 3, 1, 1, 3)
  CRGraph.size(-200, 800, 0, 0.3)
  CRGraph.view(-220, -0.01, 1020, 0.31, 683, 200, 300.0, 215.0)
  CRGraph.label(0.5, 1, "D", 2, 1, 0, 1, 1)
  CRGraph.label(0.05, 1, "mS/cm2", 2, 1, 0, 1, 1)
  CRGraph.label(0.9, 0.2, "um", 2, 1, 0, 1, 1)
  CRGraph.label(0.9, 0.95, "Gm", 2, 1, 0, 1, 3)
  CRGraph.label(0.9, 0.85, "Ghh", 2, 1, 0, 1, 5)
  CRGraph.label(0.9, 0.75, "GK", 2, 1, 0, 1, 7)
  CRGraph.label(0.9, 0.65, "Gs", 2, 1, 0, 1, 2)
  CRGraph.label(0.9, 0.55, "Gl", 2, 1, 0, 1, 1)
  CRGraph.label(0.9, 0.45, "GNa", 2, 1, 0, 1, 4)
  CRGraph.yaxis(3)
  CRGraph.xaxis(-200, 800, 0, 10, 0, 0, 1)
  CRGraph.yaxis(0, 0.3, -200, 3, 0, 0, 1)
  flush_list.append(CRGraph)
  CRGraph.save_name("flush_list.")
  objectvar RRGraph
} // MakeCRGraph()

proc Destroy() {
  VRGraph.unmap()
  CRGraph.unmap()
} // Destroy()

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

MainExec()