/*
 *  test_sli_neuron.sli
 *
 *  This file is part of NEST.
 *
 *  Copyright (C) 2004 The NEST Initiative
 *
 *  NEST is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  NEST is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with NEST.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

 /* BeginDocumentation
    Name: testsuite::test_sli_neuron - test of sli_neuron

    Synopsis: (test_sli_neuron) run -> completes successfully 

    Description:


    Author: Marc-Oliver Gewaltig
    FirstVersion: January 2009
    SeeAlso: testsuite::test_iaf, sli_neuron 
*/


/unittest (6666) require
/unittest using

0.1 /h Set

ResetKernel

0 << 
      /local_num_threads 2 
      /resolution h
  >> SetStatus


/sli_neuron_state
<<
  /tau_m 10.0 
  /C_m   250.0
  /E_L   -70.0
  /I_e   0.0
  /tau_m  10.0
  /tau_minus    20.0
  /tau_syn       2.0
  /t_ref         2.0
  /t_spike      -1.0
  /V_m          -70.0
  /V_reset        0.0
  /V0		-70.0
  /V_th         -55.0
  /theta         15.0
  /currents       0.0

  /y0 0.0
  /y1 0.0
  /y2 0.0
  /y3 0.0
  /r  0

/update_template
{
   r 0 eq
   {
	update_y3 /y3 Set
   } 
   {
	r 1 sub /r Set
   } ifelse

   update_y2 /y2 Set
   y1 P11 mul /y1 Set
   y1 PSCInitialValue ex_spikes mul add /y1 Set

   y3 theta geq                           % voltage larger threshold?
   dup /spike Set                         % the return value
   {
      t_ref_steps /r Set
      V_reset   /y3 Set                    % Reset the potential
   } if
   y3 V0 add /V_m Set
   currents /y0 Set

}

/calibrate
{  
  GetResolution /h Set

  (V_th - V0) ExecMath /theta Set 
  << >> /consts Set
  consts begin
    (exp(-h/tau_syn)) ExecMath /P11 Set
    (exp(-h/tau_m))   ExecMath /P33 Set
    (P11 * h)         ExecMath /P21 Set
    ((1.0 - P33)*tau_m/C_m)   ExecMath /P30 Set
    (1/ C_m * ((P11-P33)/(-1/tau_syn + 1/tau_m)- h*P11)/(-1/tau_m+1/tau_syn)) ExecMath /P31 Set
    (1/C_m*(P33-P11)/(-1/tau_m + 1/tau_syn)) ExecMath /P32 Set
    (exp(1.0)/tau_syn) ExecMath /PSCInitialValue Set
     t_ref h div 0.5 add floor cvi /t_ref_steps Set
    (P30*(y0 + I_e) + P31 * y1 + P32 * y2 + P33 *y3) CompileMath consts Inline /update_y3 Set
    (P21* y1 + P11 * y2) CompileMath consts Inline /update_y2 Set
  end

  /update_template load consts Inline
  /update Set
}

>> def % sli_neuron_state

/sli_neuron sli_neuron_state SetDefaults
 
/sli_neuron Create /neuron Set


/voltmeter Create /vm Set
vm << /withtime true  /time_in_steps true /interval h >> SetStatus

/spike_detector Create /sd Set
sd << /withtime true /withgid true /time_in_steps true >> SetStatus

/dc_generator Create /dc Set
dc << /amplitude 1000.0 
   >> SetStatus

dc neuron 1.0 h Connect
vm neuron 1.0 h Connect
neuron sd 1.0 h Connect

3 Simulate

vm [/events [/times /V_m]] get cva Transpose ==