/*
 *  test_hh_phaseplane.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_hh_phaseplane - sli script for hh_psc_alpha model

Synopsis: (test_hh_phaseplane) run -> 'phaseplane.txt' & 'AP.txt'


Description:

  test_hh_phaseplane makes a numerical phase-plane analysis of the Hodgkin-Huxley neuron
  (iaf_psc_alpha). Dynamics is investigated in the V-n space (see remark below). A constant
  DC can be specified  and its influence on the nullclines can be studied.
  Results are written into two files. 'phaseplane.txt' contains the phase-plane data as a
  vector field and 'AP.txt' the trace of a single action potential as one possible
  numerical solution in the vector field. The data files are used in the matlab-script hh_phaseplane.m

  REMARK
  To make the two-dimensional analysis possible, the (four-dimensional)
  Hodgkin-Huxley formalism needs to be artificially reduced to two dimensions,
  in this case by 'clamping' the two other variables, m an h, to
  constant values (m_eq and h_eq).
  
  Author: Schrader

SeeAlso: iaf_psc_exp, iaf_neuron, testsuite::test_iaf_i0, testsuite::test_iaf_i0_refractory, testsuite::test_iaf_dc
*/


% set externally applied current amplitude in pA
/amplitude 100. def
/dt        0.1 def    % simulation step length [ms]

% open file
(phaseplane.txt) ofstream
{
  /f Set
}
{
  /test_hh_phaseplane /CouldNotOpenFile raiseerror
} ifelse

ResetKernel      % clear all existing network elements

M_ERROR setverbosity

[0]
<<
  /resolution  dt
>> SetStatus
/hh_psc_alpha Create /neuron Set

% numerically obtain equilibrium state
1000 Simulate

neuron GetStatus /Act_m get /m_eq Set
neuron GetStatus /Act_h get /h_eq Set
neuron << /I_e amplitude >> SetStatus % apply external current

% scan state space
(scanning phase space) =
-100. 2. 40. {
  /V Set
  0.1 0.01 0.8 {
    /n Set
    % set V_m and n 
    neuron << /V_m V /Inact_n n /Act_m m_eq /Act_h h_eq >> SetStatus
    % write state into file
    f neuron GetStatus /V_m get print ( ) print neuron GetStatus /Inact_n get print ( ) print ;
    % simulate a short while
    dt Simulate
    % write new state next to old state
    f neuron GetStatus /V_m get V sub print ( ) print  neuron GetStatus /Inact_n get n sub print endl ;
  } for
} for

% flose first datafile
f close

% open file
(AP.txt) ofstream
{
  /f Set
}
{
  /test_hh_phaseplane /CouldNotOpenFile raiseerror
} ifelse

% set state for AP generation
neuron << /V_m -34. /Inact_n 0.2  /Act_m m_eq /Act_h h_eq >> SetStatus

(AP-trajectory) =
1 1 1000 {
  % write state into file and simulate a bit
  f neuron GetStatus /V_m get print ( ) print  neuron GetStatus /Inact_n get print endl ;
  neuron << /Act_m m_eq /Act_h h_eq >> SetStatus
  dt Simulate 
} for

% close second datafile
f close