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

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


Description:

 test_iaf_dc_aligned_delay.sli is a modified version of
 test_iaf_dc_aligned. The output generated by the former is exactly
 identical to the output of the latter. However, the  dependence of the
 simulation script on the computation step size is fully removed by
 replacing the default value of the delay between DC generator and
 neuron by a value specified in ms and larger than the largest
 computation step size to be used in the simulation. Note that the
 specification of the delay is now independent of the computation step
 size.

 In addition, variables are introduced to clarify the meaning of 
 of various time measures and a few relationships are tested.

 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 (dc_visible). With a delay of
 1ms (dc_delay), the time stamp of the emitted current event (dc_on)
 needs to be dc_visible - dc_delay. Thus, according to the definitions
 of the timing scheme, the emission time of the DC generator (start)
 needs to be dc_visible - dc_delay.

Remarks:

  In earlier versions, the file

     test_iaf_dc_aligned_automatic.sli

  contained a further improved version of this script. Here, /start
  specified the activation time t of the device not the time stamp t+h
  of the first event that can be emitted. An additional property /on
  was required to make the SLI code independent of the resolution. The
  device carried out the transformation start = on - h (see also
  test_iaf_dc_aligned.sli).  Now the code in
  test_iaf_dc_aligned_automatic.sli is identical to the code in this
  file.


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


/unittest (6688) require
/unittest using


1.0 /max_h Set       % in ms, maximal computation time step

1.0 /dc_delay Set    % in ms, delay of the connection from 
                     % DC generator to neuron

3.0 /dc_visible Set  % in ms, desired onset of the DC current
                     % in the neuron

dc_visible dc_delay sub /dc_on Set
                     % the onset time of the DC generator 
                     % required to make the current visible at
                     % the neuron at the desired time

dc_delay /max_delay Set
                     % the maximal delay tolereated by the 
                     % simulation kernel (in ms) needs to be
                     % larger or equal to the maximal delay
                     % used in this script



dc_delay max_h lt
{
 cout (Delay between DC generator and neuron is too small) <-- endl
      (for the range of step sizes to be tested) <-- endl ;
} if

dc_delay max_delay gt
{
 cout (the maximal delay tolerated by the simulation kernel) <-- endl
      (is too small for the specified delays) <-- endl ;
} if



/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  dc_on             % in ms
          >> SetStatus %  ^
                       %  |
                       %   -- SLI code does not depend 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 /sd Set 
  sd << /withtime true /to_memory true /time_in_steps true >> SetStatus

  dc_gen neuron 1.0 dc_delay Connect
                     %  ^
  vm neuron Connect  %  | 
                     %   --- no dependence on the computation 
  neuron sd Connect  %       step size h

  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