/*
 *  test_iaf_psp.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_iaf_psp - sli script for test of iaf_neuron spike input 

Synopsis: (test_iaf_psp) run -> compare response with reference data

Description:

 test_iaf_psp.sli checks the voltage response of the iaf_neuron
 model neuron to a single incoming spike. The voltage excursion is
 called post-synaptic potential (PSP). In the iaf_neuron model neuron
 the post-synaptic current is described by an alpha-function 
 (see [1] and references therein). The resulting PSP has a finite 
 rise-time, with voltage and current beeing zero in the initial 
 condition (see [1]).

 The dynamics is tested by connecting a device that emits spikes
 at individually configurable times (see test_spike_generator) to 
 a model neuron. 

 The weight of the connection specifies the peak value (amplitude)
 of the post-synaptic current (PSC) in pA.

 The subthreshold dynamics of the iaf_neuron is integrated exactly.
 Therefore, it is suitable to check whether the simulation kernel 
 produces results independent of the computation step size
 (resolution).

 In order to obtain identical results for different computation
 step sizes h, the SLI script needs to be independent of h.
 This is achieved by specifying all time parameters in milliseconds
 (ms). In particular the time of spike emission and the synaptic
 delay need to be integer multiples of the computation step sizes 
 to be tested. test_iaf_dc_aligned_delay demonstrates the strategy
 for the case of DC current input.

 The expected output is documented and briefly commented at the end of
 the script.

References:
  [1] Rotter S & Diesmann M (1999) Exact simulation of time-invariant linear
      systems with applications to neuronal modeling. Biologial Cybernetics
      81:381-402.

Author:  July 2004, Diesmann
SeeAlso: testsuite::test_iaf, testsuite::test_iaf_dc_aligned_delay, testsuite::test_spike_generator
*/


(unittest) run
/unittest using

1.0 /delay Set   % in ms 


/AlignedImpact
{
 << >> begin    
 /d Set /h Set   


ResetKernel


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


/spike_generator Create /sg Set


sg <<
     /precise_times false
     /origin 0.0            % in ms
     /spike_times [ 2.0 ]   % in ms
     /start 1.0             % in ms 
     /stop 3.0              % in ms
   >> SetStatus

/iaf_neuron Create /neuron Set


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


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


sg neuron 100.0  delay Connect

vm neuron Connect

neuron sd Connect

7.0 Simulate

 d Transpose First /test_times Set          % times of reference

 vm [/events [/times /V_m]] get cva  % array of recorded voltages
  6 ToUnitTestPrecision                     % to precision of reference
  Transpose                                 % all recorded tuples
  {First test_times exch MemberQ } Select   % those with reference 
  d eq                                      % compare
  
  
end
} def


{
 % h d,  vector of resolutions and compressed reference data   
 InflateUnitTestData 

 Transpose {AlignedImpact} MapThread

 true exch {and} Fold   % remove this line to see individual results
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%
% Expected output of this program:
% 
% The output send to std::cout is a superposition of the output of  
% the voltmeter and the spike detector. Both, voltmeter and spike 
% detector are connected to the same neuron. 
%
%
% h=   (in ms)
[ 0.1   0.2    0.5   1.0 ]
%
% time                    voltage
%
[
%  0     0     0     0     -70
[  1                       -70]
[  2     1                 -70]
[  3                       -70]
[  4     2                 -70]
[  5           1           -70]
[  6     3                 -70]
[  7                       -70]
[  8     4                 -70]
[  9                       -70]
[ 10     5     2     1     -70]
[ 11                       -70]
[ 12     6                 -70]
[ 13                       -70]
[ 14     7                 -70]
[ 15           3           -70]
[ 16     8                 -70]
[ 17                       -70]
[ 18     9                 -70]
[ 19                       -70]
[ 20    10     4     2     -70]%       <-- Spike generator emits spike
[ 21                       -70]%           with time stamp t=2.0 ms.
[ 22    11                 -70]
[ 23                       -70]
[ 24    12                 -70]%       <--  Synaptic delay of 1.0 ms.
[ 25           5           -70]
[ 26    13                 -70]
[ 27                       -70]
[ 28    14                 -70]
[ 29                       -70]
[ 30    15     6     3     -70]% <---------  Spike arrives at neuron and
[ 31                       -69.9974]% <-     changes the state vector of the  
[ 32    16                 -69.9899]%   |    neuron, not visible in voltage 
[ 33                       -69.9781]%   |    because voltage of PSP initial 
[ 34    17                 -69.9624]%   |    condition is 0.
[ 35           7           -69.9434]%   |
[ 36    18                 -69.9213]%    --  Arbitrarily close to the time
[ 37                       -69.8967]%        of impact (t=3.0 ms) the effect
[ 38    19                 -69.8699]%        of the spike (PSP) is visible in
[ 39                       -69.8411]%        the voltage trace.
[ 40    20     8    4      -69.8108]%
[ 41                       -69.779 ]
[ 42    21                 -69.7463]
[ 43                       -69.7126]
[ 44    22                 -69.6783]
[ 45           9           -69.6435]
[ 46    23                 -69.6084]
[ 47                       -69.5732]
[ 48    24                 -69.538 ]
[ 49                       -69.5029]
[ 50    25    10    5      -69.4681]% <---  The voltage trace is independent
[ 51                       -69.4336]%       of the computation step size h.
[ 52    26                 -69.3995]%       Larger step sizes only have fewer
[ 53                       -69.366 ]%       sample points.
[ 54    27                 -69.333 ]
[ 55          11           -69.3008]
[ 56    28                 -69.2692]
[ 57                       -69.2383]
% ...
]
%

rolld exec assert_or_die

