/*
 *  test_mat2.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_mat2 - sli script for overall test of mat2_neuron model

    Synopsis: (test_mat2) run -> compares response to current step with reference data 

    Description:
    test_mat2.sli is an overall test of the mat2_neuron model connected
    to some useful devices.

    A DC current is injected into the neuron using a current generator
    device. The membrane potential, the threshold potential as well as the
    spiking activity are recorded by corresponding devices.

    It can be observed how the current charges the membrane, a spike
    is emitted, the threshold potential is updated and evaluated whereas
    the membrane potential is not resetted and further evaluated, too.
    Additionally the neuron becomes refractory after emitting a spike.

    The timing of the various events on the simulation grid is of
    particular interest and crucial for the consistency of the
    simulation scheme.

    Although 0.1 cannot be represented in the IEEE double data type, it
    is safe to simulate with a resolution (computation step size) of 0.1
    ms because by default nest is built with a timebase enabling exact
    representation of 0.1 ms.

    The expected output is documented and briefly commented at the end of
    the script. The textual output of the multimeter documented in this file
    can be regenerated by setting adding /to_screen true to the SetStatus
    call of mm below.

    Other test programs discuss the various aspects of this script in detail,
    see the SeeAlso key below.

    Author:  October 2009, T. Pfeil
    SeeAlso: testsuite::test_iaf, testsuite::test_iaf_i0, testsuite::test_iaf_i0_refractory, testsuite::test_iaf_dc
*/


/unittest (8418) require
/unittest using

0.1 /h Set

ResetKernel

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

% explicitly set threshold omega here to ensure that after 
% change of definition of omega from relative to E_L to absolute
% we still get the original results; see #506.
/mat2_psc_exp << /omega -51.0 >> Create /neuron Set

/dc_generator Create /dc_gen Set
dc_gen << /amplitude 2400. >> SetStatus

/multimeter Create /mm Set
mm <<
     /withtime true
     /interval h
     /record_from [/V_m /V_th] % record non-resetting membrane potential
                               % and evaluated threshold potential
     /time_in_steps true
   >> SetStatus

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


dc_gen neuron 1.0 h Connect
mm neuron     1.0 h Connect
neuron sp_det 1.0 h Connect

8 Simulate


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Expected output of this program:
% 
% The output send to std::cout is a superposition of the output of
% the multimeter and the spike detector. Both, multimeter and spike
% detector are connected to the same neuron. 
%

{
 sp_det [/events [/times]] get cva   	     % array of recorded data
 eq                                          % compare
}

%%%%%%%%%%
% spike times (in steps)

[[11 32 54]] % The threshold potential crosses the membrane potential at time
     	     % step 31. But due to the refractory period of 2 ms the neuron
	     % is not allowed to fire until time step 32.

exch assert_or_die


{                                            % reference data
 dup Transpose First /test_times Set         % times of reference

 mm [/events [/times /V_m /V_th]] get cva    % array of recorded data
  6 ToUnitTestPrecision                      % to precision of reference
  Transpose                                  % all recorded tuples
  {First test_times exch MemberQ } Select    % those with reference
 eq                                          % compare
}

%%%%%%%%%%
% time (in steps)  membrane potential V_m (in mV)  threshold V_th (in mV)

[
[ 1	-70		-51	 ] % <--- dc_gen is switched on
[ 2	-70		-51	 ] % <--- The DC current arrives at neuron,
[ 3	-67.6238	-51	 ] % <-	  but has not affected the potential yet
[ 4	-65.2947	-51	 ] %   |
[ 5	-63.0117	-51	 ] %    - First evaluation of the DC current. The
[ 6	-60.774		-51	 ] %      threshold potential stays constant,
[ 7	-58.5805	-51	 ] %      because it is at its resting level.
[ 8	-56.4305	-51	 ]
[ 9	-54.323		-51	 ]
[ 10	-52.2573	-51	 ]
[ 11	-50.2324	-12	 ] % <--- The membrane potential crossed the
[ 12	-48.2477	-12.3692 ] % <-   threshold potential the first time.
[ 13	-46.3023	-12.7346 ] %   |  The threshold potential is updated and
[ 14	-44.3953	-13.0965 ] %   |  the membrane potential is further evaluated
[ 15	-42.5262	-13.4548 ] %   |  without resetting.
[ 16	-40.694		-13.8095 ] %   |
[ 17	-38.8982	-14.1607 ] %    - The threshold potential decays double
[ 18	-37.1379	-14.5084 ] %      exponential towards its resting level.
[ 19	-35.4124	-14.8527 ]
[ 20	-33.7212	-15.1935 ]
[ 21	-32.0634	-15.531	 ]
]


exch assert_or_die

endusing