begintemplate Mitral

// ls - long secondary dendrites 

public soma, priden, secden, tuftden, hillock, initialseg
public all, secdens, tufts, dendritic, somatic, somden, external_syn, synls, ncls
public dampa, nsecden, ntuft
public position, x, y, z


create soma, priden, secden[2], tuftden[1], hillock, initialseg

forall {Ra = 150}

objref dendritic, somatic, somden, synodor, external_syn, all, secdens, tufts, axonal
objref dampa, synls, ncls

proc init() {


  
	synls = new List()
	synls.remove_all()
	ncls = new List()
	ncls.remove_all()

	nsecden = 2
	ntuft = 1
	subsets()
	topol()
	geometry()
	segments()
        
	memb()
	x = y = z = 0 // only change via position
}

proc createsec() {
	nsecden = $1
	ntuft = $2
  
	synls.remove_all()
	ncls.remove_all()

	create soma, priden, secden[nsecden], tuftden[ntuft], hillock, initialseg
}

proc subsets() {
	all = new SectionList()
	forall all.append()
	
	secdens = new SectionList()
	forsec "secden" secdens.append()

	tufts = new SectionList()
	forsec "tuftden" tufts.append()

	dendritic = new SectionList()
	forsec "priden" dendritic.append()
	forsec secdens dendritic.append()
	forsec tufts dendritic.append()

	somatic = new SectionList()
	soma somatic.append

	axonal = new SectionList()
	hillock axonal.append
	initialseg axonal.append

//	somden = new SectionList()
//	forsec somatic somden.append()
//	forsec dendritic somden.append()
}

proc topol() {local i  localobj s
	if (numarg() > 0) if ($1 == 0) { // do not connect secondary dendrites
	}else{ // connect all secondary dendrites to soma (no branching)
		for i =0, nsecden-1 {
			connect secden[i](0), soma(.5)
		}
	}
	connect priden(0), soma(1)
	for i =0, ntuft-1 {
		connect tuftden[i](0), priden(1)
	}

	connect hillock(0), soma(0)
	connect initialseg(0), hillock(1)
}

proc segments() {local i, dx
	/*dx = 20
	soma.nseg= 1
	priden.nseg = int(priden.L/dx)+1
	forsec tufts nseg = int(L/dx)+1
	forsec secdens nseg = int(L/dx)+1*/
	soma.nseg = 1
	priden {
		if(n3d() > 1) nseg = n3d() - 1
	}
	forsec tufts {
		if(n3d() > 1) nseg = n3d() - 1
	}
	forsec secdens {
		if(n3d() > 1) nseg = n3d() - 1
	}
	initialseg.nseg = 3
	hillock.nseg = 3
}

proc geometry() {local i
	basic_shape()
	soma { L = 25  diam = 20 }
	priden { L = 300  diam = 3 }
//	forsec tufts {rallbranch=20 L=300  diam(0:1)=.4:.4 }
	forsec tufts {L=300  diam(0:1)=.4:.4 }
	forsec secdens { L=1000  diam=2 }
	initialseg{ L=30  diam=1.5}
	hillock { L=5  diam(0:1) = soma.diam(0) : initialseg.diam(0) }
	//define_shape()
}
	
proc basic_shape() {
  soma {pt3dclear() pt3dadd(0, 0, 0, 1) pt3dadd(0, 1, 0, 1)}
  priden {pt3dclear() pt3dadd(0, 0, 0, 1) pt3dadd(0, 1, 0, 1)}
  forsec tufts {pt3dclear() pt3dadd(0, 0, 0, 1) pt3dadd(0, 1, 0, 1)}
  secden[0] {pt3dclear() pt3dadd(0, 0, 0, 1) pt3dadd(1, 0, 0, 1)}
  secden[1] {pt3dclear() pt3dadd(0, 0, 0, 1) pt3dadd(-1, 0, 0, 1)}
  hillock {pt3dclear() pt3dadd(0, 0, 0, 1) pt3dadd(0, -1, 0, 1)}
  initialseg {pt3dclear() pt3dadd(0, 0, 0, 1) pt3dadd(0, -1, 0, 1)}
}

proc memb() { local x

	totarea=0
	forall {
		insert pas
		insert nax  insert kamt insert kdrmt
		Ra=150
		for (x) {totarea=totarea+area(x)}
		cm = 0.7
		g_pas = 1/10000
		e_pas = -65
		ek = -90
		ena = 50
	}
	forsec dendritic {
		gbar_nax = 0.04
		sh_nax=10
	 	gbar_kdrmt = 0.001
		gbar_kamt = 0.004
		}

	forsec axonal {
        g_pas = 1/1000
        gbar_nax = 0.8
		sh_nax = 0
        gbar_kamt = 0.08
        gbar_kdrmt = 0.001
	}       

	forsec somatic {
        gbar_nax = 0.04
		sh_nax = 0
        gbar_kamt = 0.004
        gbar_kdrmt = 0.001
	}       

	for i=0,ntuft-1 tuftden[i] {
//		priden {
		synodor = new Exp2Syn(0.1)
		synodor.e=0
		synodor.tau1 = 20
		synodor.tau2 = 200
		synls.append(synodor)
	}
        soma {
                external_syn = new Exp2Syn(.5)
                external_syn.e=0
                external_syn.tau1 = 5
                external_syn.tau2 = 50  
       }

}

proc position() { local i
  soma for i = 0, n3d()-1 {
    pt3dchange(i, $1-x+x3d(i), $2-y+y3d(i), $3-z+z3d(i), diam3d(i))
  }
  x = $1  y = $2  z = $3
}

endtemplate Mitral