/*
 *  test_stdp_poiss.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_stdp_sg - sli script to test stdp_synapse model 
implementing spike time dependent plasticity syanpses 
according to Guetig et al. (2003), Journal of Neuroscience.
This script performs repeated spike pairing of two neurons connected by a stdp_synapse.
The synapse strength is recorded after 60 paired spikes in a file.



Synopsis: (test_stdp) run -> compare cout with comments in file

author:  Moritz Helias, Wiebke Potjans, Abigail Morrison, Markus Diesmann
date:	 Sep 2007
*/


ResetKernel   % clear all existing network elements



modeldict begin  % open model and user dictionaries
synapsedict begin
userdict begin

 0.1 /h Set	     % simulation timestep [ms]

 [0]
    <<
        /resolution h
        /total_num_virtual_procs  1
	/overwrite_files true
      >>
      SetStatus


/delay 1.0 def	     % dendritic delay [ms]



10000              /K Set
0.8                /f_ex Set
f_ex K mul         /K_ex Set
1.0 f_ex sub K mul /K_in Set

10.0               /nu_ex  Set
10.0               /nu_in  Set

45.0               /w_ex  Set     % in pA, background PSC amplitude
3.83               /g     Set     % 
w_ex neg g mul     /w_in  Set

      

/poisson_generator Create /pg_ex Set
pg_ex <<
     /rate K_ex nu_ex mul
   >> SetStatus

/poisson_generator Create /pg_in Set
pg_in <<
     /rate K_in nu_in mul
   >> SetStatus



/spike_detector Create /sd Set
sd << /withtime true /withgid true  /to_file true >> SetStatus


/voltmeter Create /vm Set
vm << /withtime true /withgid true  /to_file true >> SetStatus

      
            
/iaf_psc_alpha Create /neuron1 Set
/iaf_psc_alpha Create /neuron2 Set



neuron1 << /tau_syn_ex 0.3 /tau_syn_in 0.3 /tau_minus 20.0 >> SetStatus
neuron2 << /tau_syn_ex 0.3 /tau_syn_in 0.3 /tau_minus 20.0  >> SetStatus

pg_ex neuron1 w_ex delay Connect
pg_ex neuron2 w_ex delay Connect 

pg_in neuron1 w_in delay Connect 
pg_in neuron2 w_in delay Connect 


neuron1 sd Connect
neuron2 sd Connect

%vm neuron1 Connect
vm neuron2 Connect


/stdp_synapse <<
  /delay delay
  /weight 35.
  /Wmax 70.
  /alpha 0.95
  /lambda 0.025
  /mu_plus 0.05
  /mu_minus 0.05
  /tau_plus 20.0
>>
SetDefaults

neuron1 neuron2 /stdp_synapse Connect



neuron2 GetStatus /local get
{
 (weight.gdf) (w) file /f_weight Set
} if




40000.0 /T Set

100.0 /dt Set
%76.0 /dt Set
50.0 /dt Set
%10000.0 /dt Set



0 dt T
{
 /t Set

 neuron2 GetStatus /local get
 {
     % we are on the machine where neuron2 resides
     << /source neuron1 /synapse_model /stdp_synapse >> FindConnections
     0 get GetStatus /weight get
     /w Set  %we know that there is only a single synapse

  w ==
  
  f_weight t <- (\t) <- w <- endl pop
  
 } if

 dt Simulate  
} for

neuron2 GetStatus /local get   
{
 cout  (expected  weight at T=40000 ms: 33.8466 pA) <- endl pop 
 cout  (weight at last event:           ) <- w <- ( pA) <- endl pop
} if


end
end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%
% Expected output of this program:
% 
% The output send to std::cout is a superposition of the output of  
% the voltmeter and the spike detector. Both, voltmeter and spike 
% detector are connected to the same neuron. 
%
%
% The expected output of the program for a simulation time of
% 40000.0ms is 33.8466pA.
%
% The result is independent of the length of the interval dt in which
% Simulate is called as long as the last pre-synaptic spike included 
% in the simulation remains the same.
%