// distribute_currents.hoc

plateau_500=0 // switch determines if to assign IA mx cond. values at 
// greater than 500 um from the soma to a value which is equal to that at 500 um
// in the below function

proc assign_migliore_distribution() { // this new version scales kab, kad throughout the cell
  print "Assigning Migliore distribution of currents with new I_A slope scale ",scale_ka
  forsec "axon" {   
                insert nax gbar_nax=gna * AXONM
                insert kdr gkdrbar_kdr=gkdr
//              insert kap gkabar_kap = KMULTP // J. Neurosci., September 8, 2004 • 24(36):7903–7915
//                                                Rhodes et al.  seems to indicate no IA in axons
}

  forsec "soma" {  
		insert ds // measure max v, [Ca2+]internal, and times these occur
		insert hd ghdbar_hd=ghd	vhalfl_hd=-73
                insert na3 gbar_na3=gna
                insert kdr gkdrbar_kdr=gkdr
                insert kap gkabar_kap = KMULTP
//		print "adding additional currents KM and Na,p to soma"
//		insert KM
//		gMbar_KM=0.002
//		insert nap
//		gbar_nap=0.001
  }
  // scale_ka=1 // factor for scaling ka default set in fig2A_c91662.hoc
  forsec "basal" {
		insert hd ghdbar_hd=ghd vhalfl_hd=-73
                insert na3 gbar_na3=gna
                insert kdr gkdrbar_kdr=gkdr
		insert kap gkabar_kap=0
		insert kad gkabar_kad=0
 		for (x) if (x>0 && x<1) { xdist = distance(x)
                  ghdbar_hd(x) = ghd*(1+3*xdist/100)
                  if (xdist > 100){
                    if ((xdist > 500)&&(plateau_500)) {
		      vhalfl_hd=-81
                      gkabar_kad(x) = KMULT*(1+scale_ka*500/100)
                    } else { // 100<x<500
		      vhalfl_hd=-81
                      gkabar_kad(x) = KMULT*(1+scale_ka*xdist/100)
                    }
                  } else { // x<100
		      vhalfl_hd=-73
                      gkabar_kap(x) = KMULTP*(1+scale_ka*xdist/100)
                  }
                }
  }                
  // forsec "apical_dendrite" {
  forsec "apic" { // includes primary dend., obliques, and tuft
	insert ds
	insert hd ghdbar_hd=ghd
        insert na3 gbar_na3=gna
        insert kdr gkdrbar_kdr=gkdr
	insert kap gkabar_kap=0
	insert kad gkabar_kad=0

	for (x) if (x>0 && x<1) { xdist = distance(x)
                  ghdbar_hd(x) = ghd*(1+3*xdist/100)
                  if (xdist > 100){
                    if ((xdist > 500)&&(plateau_500)) {
		      vhalfl_hd=-81
                      gkabar_kad(x) = KMULT*(1+scale_ka*500/100)
                    } else { // 100<x<500
		      vhalfl_hd=-81
                      gkabar_kad(x) = KMULT*(1+scale_ka*xdist/100)
                    }
                  } else { // x<100
		      vhalfl_hd=-73
                      gkabar_kap(x) = KMULTP*(1+scale_ka*xdist/100)
                  }
        }
  }
}
proc assign_migliore_distribution_old() { // this first method used the absolute value at the tip of the 
// distal dendrite to assign (a possibly scaled) distribution of kad
  print "Assigning Migliore distribution of currents with I_A scale ",scale_ka
  forsec "axon" {   
                insert nax gbar_nax=gna * AXONM
                insert kdr gkdrbar_kdr=gkdr
                insert kap gkabar_kap = KMULTP
  }

  forsec "soma" {  
		insert ds // measure max voltages, max [Ca2+]internal, and times they occur
		insert hd ghdbar_hd=ghd	vhalfl_hd=-73
                insert na3 gbar_na3=gna
                insert kdr gkdrbar_kdr=gkdr
                insert kap gkabar_kap = KMULTP
  }
  // scale_ka=1 // factor for scaling ka default set in fig2A_c91662.hoc
  forsec "basal" {
		insert hd ghdbar_hd=ghd vhalfl_hd=-73
                insert na3 gbar_na3=gna
                insert kdr gkdrbar_kdr=gkdr
		insert kap gkabar_kap=0
		insert kad gkabar_kad=0
		for (x) if (x>0 && x<1) { xdist = distance(x)
                	ghdbar_hd(x) = ghd*(1+3*xdist/100)
                		if (xdist > 100){
					vhalfl_hd=-81
                        		gkabar_kad(x) = KMULT*(1+scale_ka*xdist/100)
                			} else {
					vhalfl_hd=-73
                        		gkabar_kap(x) = KMULTP*(1+xdist/100)
               				}
		}
  }
                
  // forsec "apical_dendrite" {
  forsec "apic" {
		insert ds // measure max voltages, max [Ca2+]internal, and their times
		insert hd ghdbar_hd=ghd
                insert na3 gbar_na3=gna
                insert kdr gkdrbar_kdr=gkdr
		insert kap gkabar_kap=0
		insert kad gkabar_kad=0

		for (x) if (x>0 && x<1) { xdist = distance(x)
                	ghdbar_hd(x) = ghd*(1+3*xdist/100)
                		if (xdist > 100){
					vhalfl_hd=-81
                        		gkabar_kad(x) = KMULT*(1+scale_ka*xdist/100)
                			} else {
					vhalfl_hd=-73
                        		gkabar_kap(x) = KMULTP*(1+xdist/100)
               				}
		}
  }
}

assign_migliore_distribution()
load_file("add_ca.hoc")  // add L,N,T-type Ca2+ chan, pump, and Ca2+ sens. K+ current
init_ca() // initialize calcium channels max conductances
// for c91662 note that the apic subsection contains the analagous pieces of
// both apical_dendrite and user5 in the default cell from modeldb and that
// they are set with identical intrinsic currents and parameters
//forsec "user5" {
//	insert ds
//		insert hd ghdbar_hd=ghd
//              insert na3 gbar_na3=gna
//              insert kdr gkdrbar_kdr=gkdr
//		insert kap gkabar_kap=0
//		insert kad gkabar_kad=0

//		for (x) if (x>0 && x<1) { xdist = distance(x)
//                	ghdbar_hd(x) = ghd*(1+3*xdist/100)
//                		if (xdist > 100){
//					vhalfl_hd=-81
//                        		gkabar_kad(x) = KMULT*(1+xdist/100)
//                			} else {
//					vhalfl_hd=-73
//                        		gkabar_kap(x) = KMULTP*(1+xdist/100)
//               				}
//		}
//}