/*
 *  test_ac_generator.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_ac_generator - test that ac_generator provides correct current

   Synopsis: (test_ac_generator) run

   Description:
   This testscript checks tests that the ac_generator provides the correct
   current by comparing with a neuron driven by a step-current input 
   corresponding to the current expected from the ac_generator.

   FirstVersion: May 2017
   Author: Hans Ekkehard Plesser
 */

M_ERROR setverbosity

(unittest) run
/unittest using

{
  /dc   1000.0 def
  /ac    550.0 def
  /freq  100.0 def
  /phi     0.0 def
  /start   5.0 def
  /stop    6.0 def
  /dt      0.1 def

  /om freq 1000.0 div 2 mul Pi mul def   % in radians per ms
  
  /sc_times [ start stop dt ] { } Table def
  /sc_amps sc_times { om mul phi add sin ac mul dc add } Map def

  ResetKernel
  0 << /resolution dt >> SetStatus
  /n_ac /iaf_psc_alpha Create def
  /n_sc /iaf_psc_alpha Create def
  
  /g_ac /ac_generator 
        << /amplitude ac /offset dc /frequency freq /phase phi
           /start start /stop stop >>
        Create def
  /g_sc /step_current_generator 
        << /amplitude_times sc_times /amplitude_values sc_amps 
           /start start /stop stop >>
        Create def
  
  /vm_ac /voltmeter 
         << /interval 0.1 >> 
         Create def
  /vm_sc /voltmeter 
         << /interval 0.1 >> 
         Create def
         
  /I_ac /multimeter 
         << /record_from [ /I ] /interval 0.1 >> 
         Create def
  /I_sc /multimeter 
         << /record_from [ /I ] /interval 0.1 >> 
         Create def
        
  g_ac n_ac Connect
  vm_ac n_ac Connect
  I_ac g_ac Connect
  
  g_sc n_sc Connect
  vm_sc n_sc Connect
  I_sc g_sc Connect
  
  10 Simulate
  
  vm_ac [/events /V_m] get
  vm_sc [/events /V_m] get
  sub cva { abs } Map Max 1e-10 lt

  I_ac [/events /I] get
  I_sc [/events /I] get
  sub cva { abs } Map Max 1e-10 lt

  and
}
assert_or_die      
        
endusing


