/*
 *  test_dcgen_versus_I_e.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_dcgen_versus_I_e - sli script to test if setting I_e and applying a DC generator yields the same results.

   Synopsis: (test_dcgen_versus_I_e) run

   Description:
   This testscript checks consistency between currents coming from dc_generator and internal variable I_e;
   The approach is to simulate both situations for some time and compare membrane potentials at the end;
   Any model which supports current events and setting I_e is checked, other models are skipped.

   SeeAlso: testsuite::test_iaf_i0

   FirstVersion: January 2011
   Author: Sven Schrader
 */

M_ERROR setverbosity

/unittest (8831) require
/unittest using

{

% set amplitude in pA for both DC generator and I_e
/amp 123.456 def
/res [] def  % array to collect bool results

modeldict keys{

    /model Set

    % check if both membrane potential can be measured....
    /record_VM model GetDefaults /recordables known def
    record_VM {	model GetDefaults /recordables get { /V_m eq {true} if } forall count 1 eq {; /record_VM true def} {/record_VM false def} ifelse } if

    % .... and I_e can be set
    model GetDefaults /I_e known record_VM and
    {
    
	ResetKernel

	% special case for Poisson Neuron
	model /pp_psc_delta eq { model << /c_2 0.0 >> SetDefaults} if 
	
	model =only (\t: ) =only

	%executive
	
	model Create /n1 Set
	model Create /n2 Set

	%take into account synaptic delay
	/dc_generator <</start 99. >> Create /dc Set

	% todo: make sure that this procedure is 'stopped' if and only if current events are not supported!
	{dc n1 Connect

	dc << /amplitude amp >> SetStatus

	100 Simulate
	n2 << /I_e amp >> SetStatus

	300 Simulate

	% compare final membrane potentials
	n1 GetStatus /V_m get n2 GetStatus /V_m get eq dup {(pass) =}{ (failed: unequal voltages) =  executive } ifelse res exch append /res Set

	} stopped {  clear (DC not allowed) = errordict /newerror false put  } if % skip models that don't support current event
    }
    if   % model GetDefaults /I_e known record_VM and
} forall % modeldict keys

% combine results, one bool left on stack
res First res Rest { and } Fold


} assert_or_die
endusing