/*
 *  test_node_distribution.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_node_distribution - sli script to test the distribution of nodes.

   Synopsis: (test_node_distribution) run -> compare response with reference data

   Description: test_node_distribution checks the spike interaction of several 
   iaf_neuron model neurons, which are grouped in subnets. The output of this script 
   should be the same with all combinations of threads and processes. The order of 
   the lines may vary, however.

   The neurons are connected in an all-to-all fashion and the spikes of the first
   neuron of each subnet are fed into a spike_detector. The nodes of the second
   subnet are forced to be created on the same virtual process.

   FirstVersion: August 2006
   Author: Jochen Eppler
   SeeAlso: testsuite::test_iaf_1to2
 */

/unittest (6688) require
/unittest using

/NodeDistribution
{
/d Set /h Set

ResetKernel

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

/iaf_neuron << /I_e 1450.0 >> SetDefaults

/subnet Create /sn1 Set
  sn1 ChangeSubnet
  /iaf_neuron 3 Create ;

0 ChangeSubnet
/subnet Create /sn2 Set
  sn2 << /children_on_same_vp true >> SetStatus
  sn2 ChangeSubnet
  /iaf_neuron 3 Create ;

sn1 GetGlobalLeaves
{ sn2 GetGlobalLeaves DivergentConnect }
forall

sn2 GetGlobalLeaves
{ sn1 GetGlobalLeaves DivergentConnect }
forall

0 ChangeSubnet
  /spike_detector Create /sd Set
  sd << /withgid true /withtime true /to_screen false >> SetStatus

sn1 GetGlobalNodes 0 get sd Connect
sn2 GetGlobalNodes 0 get sd Connect

40 Simulate

d Transpose First /test_times Set

sd [/events [/senders /times]] get cva 6 ToUnitTestPrecision Transpose
{First test_times exch MemberQ } Select 

d eq
} def

{

 % h d,  vector of resolutions and compressed reference data   
 InflateUnitTestData

 Transpose {NodeDistribution} MapThread

 true exch {and} Fold   % remove this line to see individual results
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Expected output of this program:
%
% The output sent to cout is a superposition of the output of all processes.
% The order of the printed lines may vary.
%
% h =
[0.1]

% gid     time  
[
[2 3]
[6 3]
[2 8]
[6 8]
[2 13]
[6 13]
[2 18]
[6 18]
[2 23]
[6 23]
[2 28]
[6 28]
[2 33]
[6 33]
[2 38]
[6 38]
]

rolld assert_or_die