/*
 *  test_iaf_dc_aligned.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_dc_aligned - sli script for test of iaf_neuron resolution independence 

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


Description:

 test_iaf_dc_aligned.sli is a modified version of test_iaf_dc. Here,
 we discuss and remove one of the problems of test_iaf_dc leading to
 simulation results not independent of the computation step size
 (resolution). The remaining flaw is corrected in the follow up script
 test_iaf_dc_delay.sli .

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

 The intention of this script is that the current starts to affect the
 neuron (is present from) at time t=3ms. By default, the delay of the
 connection between DC generator and neuron assumes the minimal value
 of 1 computation step h.  Therefore, the time stamp of the emitted
 current event needs to be 3ms - h. according to the definitions of
 the timing scheme, the DC generator consequently needs to emit the
 appropriate event in the step 3ms - 2h -> 3ms - h.

 The property /start of a device specifies in ms the earliest time at
 which the device emits an event. This enables resolution independent
 specification of the emission time because at any computation step
 size h the device can be activated in the step start - h ->
 start. The only requirement being that start - h is a time larger or
 equal to the current time.  Otherwise the device cannot fulfill the
 request. The additional property /origin enables a shift of the
 device's time axis, see the documentation of devices for details. If
 resolution independence is desired origin should be specified in ms,
 often 0. is a useful value.  The above considerations result in the
 following settings for the DC generator device: 

    /origin 0.             % in ms
    /start arrival h sub   % in ms

 It should be pointed out that not specifying the delay of a
 connection in the .sli script, like it is done here

    dc_gen neuron Connect ;

 results in a simulation script that is not independent of the
 computation step size. This is because at the time of creation of the
 connection the minimal delay (1 computation step) is
 assumed. Therefore we have to compensate for this dependency in the
 start time of the DC generator as described above.  The alternative
 approach would be to specify a delay between the DC generator and the
 Neuron larger or equal to the largest computation time step to be
 used. This approach is investigated in

   test_iaf_dc_aligned.sli  


Remarks:

 In earlier versions /start was defined to specify the time step t at
 which the device becomes activated, which is the transition t -> t+h.
 On the basis of this definition, the correct code was:

   /start  arrival 2 h mul sub     % in ms

 During the development of this script a conversion problem of device
 start time expressed in ms was observed:

  dc_gen << /start  1.8  >> SetStatus  
    dc_gen GetStatus /start get ==   --> 1.800000e+00
  dc_gen << /start  2.8  >> SetStatus  
    dc_gen GetStatus /start get ==   --> 2.799000e+00

 For the purpose of this script, the problem was temporarily fixed by
 adding an epsilon 0.0001

   /start arrival 2 h mul sub 0.0001 add  % in ms

 The problem was fixed by Gewaltig 3.3.2004 and the code of this test
 script changed accordingly, Diesmann .


Author:  February 2004, Diesmann, Gewaltig, Plesser
SeeAlso: testsuite::test_iaf_dc, testsuite::test_iaf_dc_aligned_delay, testsuite::test_iaf_dc_aligned_automatic
*/


/unittest (6688) require
/unittest using


3.0 /arrival Set    % in ms, desired arrival time


/AlignedInjection  
{
 << >> begin    
 /d Set /h Set   
  
 ResetKernel

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

  /iaf_neuron Create /neuron Set

  /dc_generator Create /dc_gen Set
  dc_gen <<  
           /amplitude 1000.            % in pA
           /origin 0.                  % in ms
           /start  arrival h sub       % in ms
          >> SetStatus %   ^
                       %   |
                       %    -- SLI code explicitly depends on 
                       %       computation step size h.

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


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


  dc_gen neuron 1.0 h Connect % code depends on computation step size
                              % because the default delay is 1 h.

  vm neuron Connect

  neuron sp_det Connect


  6 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 {AlignedInjection} 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
[
% ...
[ 25            5           -70]
[ 26     12                 -70]
[ 27                        -70]
[ 28     14                 -70]
[ 29                        -70]
[ 30     15     6     3     -70] %  <---- Current starts to affect
[ 31                        -69.602]  %   neuron (visible in state variable
[ 32     16                 -69.2079] %   y0). This is the desired onset of
[ 33                        -68.8178] %    t= 3.0 ms.
[ 34     17                 -68.4316]
[ 35            7           -68.0492]
[ 36     18                 -67.6706]
[ 37                        -67.2958]
[ 38     19                 -66.9247]
[ 39                        -66.5572]
[ 40     20     8     4     -66.1935]
[ 41                        -65.8334]
[ 42     21                 -65.4768]
% ...
]
%


rolld 
assert_or_die