/*
 *  parallel_conn_and_rand.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/>.
 *
 */

/*
   Parallel Connect and Randomize Test Script

   This script creates a neuron population to itself, randomizing
   weight, delay, receptor type and one synaptic parameter. It
   then dumps the complete connectivity to file pcar_NumMPIProcs_Rank.conn,
   one line per connection.
   
   This script runs with a fixed number of virtual processes (default 16). 
   The script should be executed with varying numbers of MPI processes.
   By merging and sorting results across ranks for each MPI process number
   and comparing the resulting files, one can test whether identical
   connections are created for all MPI-thread splits and thus test
   for correct parallelization.

   Hans Ekkehard Plesser
*/

%%% PARAMETER SECTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% define all relevant parameters: changes should be made here
% all data is place in the userdict dictionary

/num_neurons 1000 def   

/connspec << /rule /fixed_indegree /indegree 100 >> def
%/connspec << /rule /fixed_outdegree /outdegree 100 >> def

/num_vps 16 def

/simtime     10.0 def % simulation time [ms]
/dt           0.1 def % simulation step length [ms]

%%% HELPER SECTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

/padded 
{
  dup 10 lt
  {
     cvs (0) exch join
  }
  {
     cvs
  }
  ifelse
} def

%%% CONSTRUCTION SECTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


ResetKernel      % clear all existing network elements
M_WARNING setverbosity


% set resolution and total/local number of threads
0 << 
    /resolution  dt 
    /total_num_virtual_procs num_vps
    /overwrite_files true
>> SetStatus

tic % start timer on construction

(Creating the network.) =  

% setup to allow us to randomize receptor_type
/tau_syns [0.2 1.0 0.1] Range def
/receptor_min 1 def
/receptor_max tau_syns length def
/iaf_psc_alpha_multisynapse << /tau_syn tau_syns >> SetDefaults

/iaf_psc_alpha_multisynapse [ num_neurons ] LayoutNetwork
GetGlobalLeaves /population Set


(Connecting neurons.) =
population population connspec
<< /model /stdp_synapse
   /delay << /distribution /uniform /low 0.5 /high 1.5 >>
   /weight << /distribution /normal /mu 10.0 /sigma 5.0 >>
   /receptor_type << /distribution /uniform_int /low receptor_min /high receptor_max >>  
   /alpha << /distribution /uniform /low 0.1 /high 2.3 >> 
   /tau_plus << /distribution /uniform /low 1.5 /high 5.0 >>
>>     
Connect

(Done connecting.) =

toc /BuildCPUTime Set

% dump connections to file
/fname (pcar_) NumProcesses padded join (_) join Rank padded join (.conn) join def

(Dumping to file ) fname join =
/of fname (w) file def
<< /synapse_model /stdp_synapse >> GetConnections 
{ 
  [[/source /target /weight /delay /alpha /tau_plus]] get 
  { of exch <- (\t) <- ; } forall 
  of endl ;
} forall
of close
(Done dumping) =

%%% SIMULATION SECTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

(Simulating) =
simtime Simulate
(Done simulating) =

(Number of MPI procs: ) =only NumProcesses =
(Number of VPs      : ) =only num_vps =
(Number of Neurons  : ) =only num_neurons =
(Number of Synapses : ) =only 0 GetStatus /num_connections get =
(Building time     : ) =only BuildCPUTime =only ( s\n) =

