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

statusdict/have_music :: not {statusdict/exitcodes/success :: quit_i} if

% spike exporter
% send spikes generated by a group of spike_generators/parrot_neurons
% to a MUSIC port
% author: Moritz Helias
% data: 2008-10-16

21.0 /T Set % total simulation time
0 << /resolution 0.1 >> SetStatus

10 /N Set % number of spike generators

% create a subnet containing N parrot_neurons
% these are the dummy population to send data to the
% music port
/subnet Create /SN Set
SN ChangeSubnet
/parrot_neuron N Create pop
0 ChangeSubnet
SN GetGlobalNodes /parrots Set

% create N spike generators each to feed one of the N parrots
/subnet Create /SNG Set
SNG ChangeSubnet
/spike_generator N Create pop
0 ChangeSubnet
SNG GetGlobalNodes /generators Set

% let the spike generator i
% produce a spike at 1.0 + 2.0*i ms
% connect it to parrot neuron i
generators
{
        /i Set /sg Set
        sg << /spike_times [0.5 i 2.0 mul add] >> SetStatus
        sg parrots i get Connect
}
forallindexed

% create a music_event_out_proxy
% for the port named "spikes_out"
/music_event_out_proxy Create /mo Set
mo
<<
    /port_name (spikes_out)
>>
SetStatus

% connect the parrot neuron i
% to the music_event_out_proxy on receiver port (channel) i
parrots
{
     /i Set
     /src Set
     src GetStatus /local get
     {
       src mo << /receptor_type i >> Connect
     }
     if
}
forallindexed

% print the indexmap
% mapping from local index to
% global MUSIC index for each process
cout (rank=) <- Rank <- ( ) <- pop
mo GetStatus /index_map get ==

% simulate
T Simulate
