/*
 *  test_noise_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_noise_generator - minimal test of noise_generator

Synopsis: (test_noise_generator) run -> dies if assertion fails

Description:
Tests parameter setting and statistical correctness for one application.

Author: Plesser
FirstVersion: July 2008
SeeAlso: noise_generator
*/

(unittest) run
/unittest using

% First test: parameter setting on model and instance
{
    ResetKernel
    /tdict << /mean 10.5 /std 0.23 /dt 0.5 >> def

    /ng1 /noise_generator Create def
    ng1 tdict SetStatus

    /noise_generator tdict SetDefaults
    /ng2 /noise_generator Create def

    ng1 [tdict keys] get
    ng2 [tdict keys] get
    eq
} assert_or_die
clear
ResetKernel

% Second test: error if uncommensurable delta_tau
{
  ResetKernel
  0 << /resolution 0.1 >> SetStatus
  /noise_generator << /dt 0.25 >> SetDefaults
} fail_or_die  

% Third test: error if uncommensurable change of resolution
{
  ResetKernel
  0 << /resolution 0.1 >> SetStatus
  /noise_generator << /dt 0.1 >> SetDefaults
  0 << /resolution 1.0 >> SetStatus
  /noise_generator Create % cannot create now with dt==0.1
} fail_or_die  

% Fourth test: recording at two different dt
/vb verbosity def
M_ERROR setverbosity  % suppress warning from time reset
{
  % run for resolution dt=0.1 project to iaf_neuron
  % create 100 repetitions of 1000ms simulations
  % collect membrane potential at end 
  ResetKernel
  0 << /resolution 0.1 >> SetStatus
  /ng  /noise_generator Create def
  /nrn /iaf_neuron Create def
  ng nrn Connect

  ng  << /mean 0.0 /std 1.0 /dt 0.1 >> SetStatus

  % no spiking, all parameters 1, 0 leak potential
  nrn << /V_th 1e10 /C_m 1.0 /tau_m 1.0 /E_L 0.0 >> SetStatus


  % now simulate 
  /Nsims 100 def
  [Nsims] {
    pop
    ResetNetwork 
    0 <</time 0.0>> SetStatus 
    1000 Simulate 
    nrn [/V_m] get
  } Table 
  dup length Nsims eq assert

  % compute mean and std dev of potential
  dup Plus Nsims cvd div /meanV Set
  dup mul Plus Nsims cvd div meanV dup mul sub sqrt /stdV Set

  % expected values
  /emeanV 0.0 def
  /estdV ng [/dt] get nrn [/tau_m] get div neg exp 
         dup 1 exch sub exch 1 add div sqrt 
         ng [/std] get mul def
  /estdstdV estdV 2 Nsims cvd mul sqrt div def  % std dev of std dev

  % require mean within 3 std dev, std dev within three std dev of std dev
  meanV emeanV sub abs estdV 3 mul lt
  stdV estdV sub abs estdstdV 3 mul lt
  and
} assert_or_die
vb setverbosity  % restore default verbosity

endusing
