MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
RandSpike.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2003-2007 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 
10 #include "../basecode/header.h"
11 #include "../basecode/global.h"
12 
13 #include "RandSpike.h"
14 
16 // MsgSrc definitions
19 {
20  static SrcFinfo1< double > spikeOut( "spikeOut",
21  "Sends out a trigger for an event.");
22  return &spikeOut;
23 }
24 
26 {
28  // Shared message definitions
30  static DestFinfo process( "process",
31  "Handles process call",
33  static DestFinfo reinit( "reinit",
34  "Handles reinit call",
36 
37  static Finfo* processShared[] =
38  {
39  &process, &reinit
40  };
41 
42  static SharedFinfo proc( "proc",
43  "Shared message to receive Process message from scheduler",
44  processShared, sizeof( processShared ) / sizeof( Finfo* ) );
45 
47  // Dest Finfos.
49 
51  // Value Finfos.
53 
54  static ValueFinfo< RandSpike, double > rate( "rate",
55  "Specifies rate for random spike train. Note that this is"
56  "probabilistic, so the instantaneous rate may differ. "
57  "If the rate is assigned be message and it varies slowly then "
58  "the average firing rate will approach the specified rate",
61  );
62  static ValueFinfo< RandSpike, double > refractT( "refractT",
63  "Refractory Time.",
66  );
67  static ValueFinfo< RandSpike, double > absRefract( "abs_refract",
68  "Absolute refractory time. Synonym for refractT.",
71  );
72  static ValueFinfo< RandSpike, bool > doPeriodic( "doPeriodic",
73  "Flag: when false, do Poisson process with specified mean rate.\n"
74  "When true, fire periodically at specified rate.\n"
75  "Defaults to false. Note that refractory time overrides this: "
76  "Rate cannot exceed 1/refractT.",
79  );
80  static ReadOnlyValueFinfo< RandSpike, bool > hasFired( "hasFired",
81  "True if RandSpike has just fired",
83  );
84 
85  static Finfo* spikeGenFinfos[] =
86  {
87  spikeOut(), // SrcFinfo
88  &proc, // Shared
89  &rate, // Value
90  &refractT, // Value
91  &absRefract, // Value
92  &doPeriodic, // Value
93  &hasFired, // ReadOnlyValue
94  };
95 
96  static string doc[] =
97  {
98  "Name", "RandSpike",
99  "Author", "Upi Bhalla",
100  "Description", "RandSpike object, generates random or regular "
101  "spikes at "
102  "specified mean rate. Based closely on GENESIS randspike. "
103  };
104  static Dinfo< RandSpike > dinfo;
105  static Cinfo spikeGenCinfo(
106  "RandSpike",
108  spikeGenFinfos, sizeof( spikeGenFinfos ) / sizeof( Finfo* ),
109  &dinfo,
110  doc,
111  sizeof(doc)/sizeof(string)
112  );
113 
114  return &spikeGenCinfo;
115 }
116 
118 
120  :
121  rate_( 0.0 ),
122  realRate_( 0.0 ),
123  refractT_(0.0),
124  lastEvent_(0.0),
125  threshold_(0.0),
126  fired_( false ),
127  doPeriodic_( false )
128 {
129  ;
130 }
131 
133 // Here we put the RandSpike class functions.
135 
136 // Value Field access function definitions.
137 void RandSpike::setRate( double rate )
138 {
139  if ( rate < 0.0 )
140  {
141  cout <<"Warning: RandSpike::setRate: Rate must be >= 0. Using 0.\n";
142  rate = 0.0;
143  }
144  rate_ = rate;
145  double prob = 1.0 - rate * refractT_;
146  if ( prob <= 0.0 )
147  {
148  cout << "Warning: RandSpike::setRate: Rate is too high compared to refractory time\n";
149  realRate_ = rate_;
150  }
151  else
152  {
153  realRate_ = rate_ / prob;
154  }
155 }
156 double RandSpike::getRate() const
157 {
158  return rate_;
159 }
160 
161 void RandSpike::setRefractT( double val )
162 {
163  refractT_ = val;
164 }
166 {
167  return refractT_;
168 }
169 
171 {
172  return fired_;
173 }
174 
175 void RandSpike::setDoPeriodic( bool val )
176 {
177  doPeriodic_ = val;
178 }
180 {
181  return doPeriodic_;
182 }
183 
184 
186 // RandSpike::Dest function definitions.
188 
189 void RandSpike::process( const Eref& e, ProcPtr p )
190 {
191  if ( refractT_ > p->currTime - lastEvent_ || rate_ <= 0.0 )
192  return;
193 
194  fired_ = false;
195  if (doPeriodic_)
196  {
197  if ( (p->currTime - lastEvent_) > 1.0/rate_ )
198  {
199  lastEvent_ = p->currTime;
200  spikeOut()->send( e, p->currTime );
201  fired_ = true;
202  }
203  }
204  else
205  {
206  double prob = realRate_ * p->dt;
207  if ( prob >= 1.0 || prob >= moose::mtrand() )
208  {
209  lastEvent_ = p->currTime;
210  spikeOut()->send( e, p->currTime );
211  fired_ = true;
212  }
213  }
214 }
215 
216 // Set it so that first spike is allowed.
217 void RandSpike::reinit( const Eref& e, ProcPtr p )
218 {
219  if ( rate_ <= 0.0 )
220  {
221  lastEvent_ = 0.0;
222  realRate_ = 0.0;
223  }
224  else
225  {
226  double prob = moose::mtrand();
227  double m = 1.0 / rate_;
228  lastEvent_ = m * log( prob );
229  }
230 }
static const Cinfo * spikeGenCinfo
Definition: RandSpike.cpp:117
static const Cinfo * initCinfo()
Definition: RandSpike.cpp:25
double lastEvent_
Definition: RandSpike.h:45
double realRate_
Definition: RandSpike.h:43
double currTime
Definition: ProcInfo.h:19
Definition: Dinfo.h:60
void setRate(double rate)
Definition: RandSpike.cpp:137
bool fired_
Definition: RandSpike.h:47
void process(const Eref &e, ProcPtr p)
Definition: RandSpike.cpp:189
void log(string msg, serverity_level_ type=debug, bool redirectToConsole=true, bool removeTicks=true)
Log to console (and to a log-file)
bool getDoPeriodic() const
Definition: RandSpike.cpp:179
void reinit(const Eref &e, ProcPtr p)
Definition: RandSpike.cpp:217
double dt
Definition: ProcInfo.h:18
double getRate() const
Definition: RandSpike.cpp:156
Definition: Eref.h:26
static SrcFinfo1< double > * spikeOut()
Definition: RandSpike.cpp:18
double rate_
Definition: RandSpike.h:42
double mtrand(void)
Generate a random double between 0 and 1.
Definition: global.cpp:97
static const Cinfo * initCinfo()
Definition: Neutral.cpp:16
void setRefractT(double val)
Definition: RandSpike.cpp:161
double getRefractT() const
Definition: RandSpike.cpp:165
bool getFired() const
Definition: RandSpike.cpp:170
void setDoPeriodic(bool val)
Definition: RandSpike.cpp:175
Definition: Cinfo.h:18
double refractT_
Definition: RandSpike.h:44
bool doPeriodic_
Definition: RandSpike.h:48
Definition: Finfo.h:12