/*
 *  issue-410.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::issue-410 - Ensure thread safety of volume transmitter

   Synopsis: (issue-410) run -> NEST exits if test fails
   Description:
   This test compares the impact of the volume transmitter
   on a dopamine synapse using different number of threads.   

   FirstVersion: 07/2016
   Author: Till Schumann
 */

% The following test needs the model iaf_cond_alpha, so
% this test should only run if we have GSL
statusdict/have_gsl :: not {statusdict/exitcodes/success :: quit_i} if


(unittest) run
/unittest using

M_ERROR setverbosity

is_threaded_with_openmp not { exit_test_gracefully } if

/sim_weight_in_dopa_syn
{

dup /threads Set
ResetKernel
0 << /local_num_threads threads >> SetStatus

/dc_generator 1 << /amplitude 1500.0 >> Create /stim1 Set
/dc_generator 1 << /amplitude 1000.0 >> Create /stim2 Set


/iaf_neuron 1 << /C_m 100.0 /tau_m 10.0 >> Create /neuron1 Set
/iaf_neuron 1 << /C_m 100.0 /tau_m 10.0 /tau_minus 10.0>> Create /neuron2 Set

0 GetStatus /network_size get /dopa_first Set
/iaf_cond_alpha 100 << /V_reset -70.0 /C_m 80.0 /V_th -60.0>> Create /dopa_last Set
dopa_first dopa_last cvgidcollection /dopa Set

/volume_transmitter 1 Create /vt Set

/stdp_dopamine_synapse /syn1
               << /Wmax 1000.0
                /Wmin 0.0
                /tau_plus 10.0
                /A_minus 0.05
                /A_plus 0.05
                /b 45.45
                /tau_c 1.0
                /tau_n 100.0
                /vt vt >> CopyModel

stim1 neuron1 << /weight 10.0 /delay 1.0 >> Connect 
[stim2] cvgidcollection dopa <</rule /all_to_all >> << /weight 10.0 /delay 1.0 >> Connect
neuron1 neuron2 << /weight 500.0 /delay 1.0 >> /syn1 Connect
dopa [vt] cvgidcollection Connect

2000.0 Simulate

%push return value
<< /source [neuron1] /target [neuron2] >> GetConnections /conns Set
conns size 0 gt {
  0 get GetStatus /weight get
}
{
  0.0
} ifelse
} def

{
  1 sim_weight_in_dopa_syn /t1 Set 
  2 sim_weight_in_dopa_syn /t2 Set
  4 sim_weight_in_dopa_syn /t3 Set
  8 sim_weight_in_dopa_syn /t4 Set
  16 sim_weight_in_dopa_syn /t5 Set
  32 sim_weight_in_dopa_syn /t6 Set
  
  (1 thread weight=) t1 cvs join =
  (2 threads weight=) t2 cvs join =
  (4 threads weight=) t3 cvs join =
  (8 threads weight=) t4 cvs join =
  (16 threads weight=) t5 cvs join =
  (32 threads weight=) t6 cvs join =
  
  t1 6 ToUnitTestPrecision t2 6 ToUnitTestPrecision eq 
  t1 6 ToUnitTestPrecision t3 6 ToUnitTestPrecision eq and
  t1 6 ToUnitTestPrecision t4 6 ToUnitTestPrecision eq and
  t1 6 ToUnitTestPrecision t5 6 ToUnitTestPrecision eq and
  t1 6 ToUnitTestPrecision t6 6 ToUnitTestPrecision eq and 
} assert_or_die

endusing
