// This experiment is used to tune the AMPA conductance values at most locations along each
// section such that a single pulse stimulation at ANY synapse with both an AMPA and an NMDA mechanism
// will give rise to 5mV local depolarization.

//load_proc("nrnmainmenu")
//load_template("ExperimentControl")                                           // load needed templates
load_file("nrngui.hoc")
load_file("../../template/load_templates.hoc")

objref econ                                                                  // initialize template parameters
show_errs=1
debug_lev=1
econ=new ExperimentControl(show_errs,debug_lev)
econ.self_define(econ)

econ.morphology_dir = "../../morphology/n123"                                // set location for morphology files
econ.add_lib_dir("Terrence","../../lib")                                     // set location for library files
econ.generic_dir    = "../../experiment/"                                    // set location for cell-setup file 
econ.data_dir       = "data"                                                 // set directory to store data
sprint(econ.syscmd, "mkdir -p %s", econ.data_dir)
system(econ.syscmd)

econ.xopen_geometry_dependent("cell")                                        // load raw cell morphology
maximum_segment_length=20
econ.xopen_geometry_dependent("cell-analysis")                               // load user-defined semantics on morphology
cell_analysis(econ)

printf("Opening cell setup\n")                                               // load cell-setup to
econ.xopen_generic("cell-setup")                                             // specify all mechanisms,
printf("Opened. Setting up cell\n")                                          // membrane properties etc
cell_setup(econ)

// Set simulation parameters for the experiment

econ.defvar("Simulation Control", "tstop", "100", "Defines when the simulation stops.") 
econ.defvar("Simulation Control", "dt", "0.2", "Timestep") 
econ.defvar("Simulation Control", "steps_per_ms", "10", "How many points are plotted per ms") 
setdt() 

econ.defvar("Experiment Control", "BASELINE", "-70", "")                  // Set resting membrane potential
econ.defvar("Experiment Control", "desired_voltage", "BASELINE+5", "")    // Set desired local depolarization to 5 mV
econ.defvar("Experiment Control","Epsilon", "0.1", "")                    // Set voltage fault tolerance = 0.1 mV

create fakecell                                                           // Create a single pulse stimulus
access fakecell 
objref ic 
ic=new IClamp(0.5)

objref tunings    							 // list to save tuned values             
strdef recordsec               
objref tune_epsp_list, tip_list
tune_epsp_list=new List()

proc tune_sections() {                                                  // tuning proceedure 

    econ.xopen_library("Terrence","basic-graphics")                  
    addgraph_2("v(0.5)",               0,tstop,-72,-60)                 // plot the voltage at the section beeing tuned
       
    create fakecell
    tunings=new File()
    econ.xopen_library("Terrence","tune-epsps")                         // library function to tune epsps
    econ.xopen_library("Terrence","salloc")                             // library function to insert a synapse at the tested location
    econ.xopen_library("Terrence","deduce-ratio")                       // library function to get NMDA/AMPA conductance ratio
    econ.xopen_geometry_dependent("nmda-ampa-ratio")                    // file containing values for NMDA/AMPA ratio

    GMAX = 1.3e-3                                                       // initialize AMPA conductance value
   
//  Start tuning the valious section lists


//  Tune basal dendrites, not needed in the experiments to follow
/*
    tunings.aopen("My_tunings.dat")
    forsec basal_tree_list {
       nseg = 6
        NMDA_AMPA_RATIO=BASAL_NMDA_AMPA_RATIO
        for (x) {
           sprint(recordsec, "%s.v(%f)",secname(),x) 
           graph_label(recordsec,0,tstop,-72,-60)
           tune_epsp_fast(econ,x,Epsilon,tunings)
        }
   }
   tunings.close()

// Tune somatic sections

   tunings.aopen("My_tunings.dat")
   forsec soma_list {
      nseg = 6
      NMDA_AMPA_RATIO=SOMA_NMDA_AMPA_RATIO
      for (x) {
          sprint(recordsec, "%s.v(%f)",secname(),x) 
          graph_label(recordsec,0,tstop,-72,-60)
          tune_epsp_fast(econ,x,Epsilon,tunings)
      }
   }
   tunings.close()

*/
 
// Tune apical-tip sections
//tip_list=new SectionList()
//apical_dendrite[73] tip_list.append()
   tunings.aopen("My_tunings.dat")
   forsec apical_tip_list {
 //    forsec tip_list {
     nseg = 6
       NMDA_AMPA_RATIO=APICAL_NON_TRUNK_NMDA_AMPA_RATIO
       for (x) {
          sprint(recordsec, "%s.v(%f)",secname(),x) 
          graph_label(recordsec,0,tstop,-72,-60)
          tune_epsp_fast(econ,x,Epsilon,tunings)
       }
   }
  tunings.close()

  tunings.aopen("My_tunings.dat")
   forsec apical_tip_list_addendum {
       nseg = 6
       NMDA_AMPA_RATIO=APICAL_NON_TRUNK_NMDA_AMPA_RATIO
       for (x) {
          sprint(recordsec, "%s.v(%f)",secname(),x) 
          graph_label(recordsec,0,tstop,-72,-60)
          tune_epsp_fast(econ,x,Epsilon,tunings)
       }
   }
 tunings.close()

// Tune apical-trunk sections

   tunings.close()
   skip = 0
   tunings.aopen("My_tunings.dat")
   forsec apical_trunk_list {
      nseg = 6
      ifsec apical_tip_list {skip = 1} 
      if (!skip) {
         NMDA_AMPA_RATIO=APICAL_TRUNK_NMDA_AMPA_RATIO
         for (x) tune_epsp_fast(econ,x,Epsilon,tunings)
      }   
      skip = 0
   }
   tunings.close()

/*
// Tune apical-non-trunk sections

   tunings.aopen("My_tunings.dat")
   forsec apical_non_trunk_list {
      nseg = 6
      ifsec apical_tip_list {skip = 1} 
      ifsec apical_tip_list_addendum {skip = 1} 
      if (!skip) {
          NMDA_AMPA_RATIO=APICAL_NON_TRUNK_NMDA_AMPA_RATIO
          for (x) {
            sprint(recordsec, "%s.v(%f)",secname(),x) 
            graph_label(recordsec,0,tstop,-72,-60)
            tune_epsp_fast(econ,x,Epsilon,tunings)
          }
      }
      skip = 0
   }
   tunings.close()

// Tune axon sections, not needed in the following experiments
   tunings.aopen("My_tunings.dat")
   forsec axon_sec_list {
      nseg = 6
      NMDA_AMPA_RATIO=AXON_NMDA_AMPA_RATIO
      for (x) {
         sprint(recordsec, "%s.v(%f)",secname(),x) 
         graph_label(recordsec,0,tstop,-72,-60)
         tune_epsp_fast(econ,x,Epsilon,tunings)
     }
   }
   tunings.close()
*/

} 
 								// end of tuning proceedure
Deadtime_GLU=0.1
tune_sections()     // Execute the tuning proceedure