forked from nest/nest-simulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrate_transformer_node_impl.h
More file actions
319 lines (270 loc) · 9 KB
/
Copy pathrate_transformer_node_impl.h
File metadata and controls
319 lines (270 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* rate_transformer_node_impl.h
*
* 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/>.
*
*/
#ifndef RATE_TRANSFORMER_NODE_IMPL_H
#define RATE_TRANSFORMER_NODE_IMPL_H
#include "rate_transformer_node.h"
// C++ includes:
#include <cmath> // in case we need isnan() // fabs
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
// Includes from libnestutil:
#include "dict_util.h"
#include "numerics.h"
// Includes from nestkernel:
#include "exceptions.h"
#include "kernel_manager.h"
#include "universal_data_logger_impl.h"
// Includes from sli:
#include "dict.h"
#include "dictutils.h"
#include "doubledatum.h"
#include "integerdatum.h"
namespace nest
{
/* ----------------------------------------------------------------
* Recordables map
* ---------------------------------------------------------------- */
template < class TNonlinearities >
RecordablesMap< rate_transformer_node< TNonlinearities > > rate_transformer_node< TNonlinearities >::recordablesMap_;
/* ----------------------------------------------------------------
* Default constructors defining default parameters and state
* ---------------------------------------------------------------- */
template < class TNonlinearities >
nest::rate_transformer_node< TNonlinearities >::Parameters_::Parameters_()
: linear_summation_( true )
{
}
template < class TNonlinearities >
nest::rate_transformer_node< TNonlinearities >::State_::State_()
: rate_( 0.0 )
{
}
/* ----------------------------------------------------------------
* Parameter and state extractions and manipulation functions
* ---------------------------------------------------------------- */
template < class TNonlinearities >
void
nest::rate_transformer_node< TNonlinearities >::Parameters_::get( DictionaryDatum& d ) const
{
def< bool >( d, names::linear_summation, linear_summation_ );
}
template < class TNonlinearities >
void
nest::rate_transformer_node< TNonlinearities >::Parameters_::set( const DictionaryDatum& d, Node* node )
{
updateValueParam< bool >( d, names::linear_summation, linear_summation_, node );
}
template < class TNonlinearities >
void
nest::rate_transformer_node< TNonlinearities >::State_::get( DictionaryDatum& d ) const
{
def< double >( d, names::rate, rate_ ); // Rate
}
template < class TNonlinearities >
void
nest::rate_transformer_node< TNonlinearities >::State_::set( const DictionaryDatum& d, Node* node )
{
updateValueParam< double >( d, names::rate, rate_, node ); // Rate
}
template < class TNonlinearities >
nest::rate_transformer_node< TNonlinearities >::Buffers_::Buffers_( rate_transformer_node< TNonlinearities >& n )
: logger_( n )
{
}
template < class TNonlinearities >
nest::rate_transformer_node< TNonlinearities >::Buffers_::Buffers_( const Buffers_&,
rate_transformer_node< TNonlinearities >& n )
: logger_( n )
{
}
/* ----------------------------------------------------------------
* Default and copy constructor for node
* ---------------------------------------------------------------- */
template < class TNonlinearities >
nest::rate_transformer_node< TNonlinearities >::rate_transformer_node()
: ArchivingNode()
, S_()
, B_( *this )
{
recordablesMap_.create();
Node::set_node_uses_wfr( kernel().simulation_manager.use_wfr() );
}
template < class TNonlinearities >
nest::rate_transformer_node< TNonlinearities >::rate_transformer_node( const rate_transformer_node& n )
: ArchivingNode( n )
, nonlinearities_( n.nonlinearities_ )
, S_( n.S_ )
, B_( n.B_, *this )
{
Node::set_node_uses_wfr( kernel().simulation_manager.use_wfr() );
}
/* ----------------------------------------------------------------
* Node initialization functions
* ---------------------------------------------------------------- */
template < class TNonlinearities >
void
nest::rate_transformer_node< TNonlinearities >::init_buffers_()
{
B_.delayed_rates_.clear(); // includes resize
// resize buffers
const size_t buffer_size = kernel().connection_manager.get_min_delay();
B_.instant_rates_.resize( buffer_size, 0.0 );
B_.last_y_values.resize( buffer_size, 0.0 );
B_.logger_.reset(); // includes resize
ArchivingNode::clear_history();
}
template < class TNonlinearities >
void
nest::rate_transformer_node< TNonlinearities >::pre_run_hook()
{
B_.logger_.init(); // ensures initialization in case mm connected after Simulate
}
/* ----------------------------------------------------------------
* Update and event handling functions
*/
template < class TNonlinearities >
bool
nest::rate_transformer_node< TNonlinearities >::update_( Time const& origin,
const long from,
const long to,
const bool called_from_wfr_update )
{
const size_t buffer_size = kernel().connection_manager.get_min_delay();
const double wfr_tol = kernel().simulation_manager.get_wfr_tol();
bool wfr_tol_exceeded = false;
// allocate memory to store rates to be sent by rate events
std::vector< double > new_rates( buffer_size, 0.0 );
for ( long lag = from; lag < to; ++lag )
{
// store rate
new_rates[ lag ] = S_.rate_;
// reinitialize output rate
S_.rate_ = 0.0;
double delayed_rates = 0;
if ( called_from_wfr_update )
{
// use get_value_wfr_update to keep values in buffer
delayed_rates = B_.delayed_rates_.get_value_wfr_update( lag );
}
else
{
// use get_value to clear values in buffer after reading
delayed_rates = B_.delayed_rates_.get_value( lag );
}
if ( P_.linear_summation_ )
{
S_.rate_ += nonlinearities_.input( delayed_rates + B_.instant_rates_[ lag ] );
}
else
{
S_.rate_ += delayed_rates + B_.instant_rates_[ lag ];
}
if ( called_from_wfr_update )
{
// check if deviation from last iteration exceeds wfr_tol
wfr_tol_exceeded = wfr_tol_exceeded or fabs( S_.rate_ - B_.last_y_values[ lag ] ) > wfr_tol;
// update last_y_values for next wfr iteration
B_.last_y_values[ lag ] = S_.rate_;
}
else
{
// rate logging
B_.logger_.record_data( origin.get_steps() + lag );
}
}
if ( not called_from_wfr_update )
{
// Send delay-rate-neuron-event. This only happens in the final iteration
// to avoid accumulation in the buffers of the receiving neurons.
DelayedRateConnectionEvent drve;
drve.set_coeffarray( new_rates );
kernel().event_delivery_manager.send_secondary( *this, drve );
// clear last_y_values
std::vector< double >( buffer_size, 0.0 ).swap( B_.last_y_values );
// modifiy new_rates for rate-neuron-event as proxy for next min_delay
for ( long temp = from; temp < to; ++temp )
{
new_rates[ temp ] = S_.rate_;
}
}
// Send rate-neuron-event
InstantaneousRateConnectionEvent rve;
rve.set_coeffarray( new_rates );
kernel().event_delivery_manager.send_secondary( *this, rve );
// Reset variables
std::vector< double >( buffer_size, 0.0 ).swap( B_.instant_rates_ );
return wfr_tol_exceeded;
}
template < class TNonlinearities >
void
nest::rate_transformer_node< TNonlinearities >::handle( InstantaneousRateConnectionEvent& e )
{
const double weight = e.get_weight();
size_t i = 0;
std::vector< unsigned int >::iterator it = e.begin();
// The call to get_coeffvalue( it ) in this loop also advances the iterator it
while ( it != e.end() )
{
if ( P_.linear_summation_ )
{
B_.instant_rates_[ i ] += weight * e.get_coeffvalue( it );
}
else
{
B_.instant_rates_[ i ] += weight * nonlinearities_.input( e.get_coeffvalue( it ) );
}
++i;
}
}
template < class TNonlinearities >
void
nest::rate_transformer_node< TNonlinearities >::handle( DelayedRateConnectionEvent& e )
{
const double weight = e.get_weight();
const long delay = e.get_delay_steps() - kernel().connection_manager.get_min_delay();
size_t i = 0;
std::vector< unsigned int >::iterator it = e.begin();
// The call to get_coeffvalue( it ) in this loop also advances the iterator it
while ( it != e.end() )
{
if ( P_.linear_summation_ )
{
B_.delayed_rates_.add_value( delay + i, weight * e.get_coeffvalue( it ) );
}
else
{
B_.delayed_rates_.add_value( delay + i, weight * nonlinearities_.input( e.get_coeffvalue( it ) ) );
}
++i;
}
}
template < class TNonlinearities >
void
nest::rate_transformer_node< TNonlinearities >::handle( DataLoggingRequest& e )
{
B_.logger_.handle( e );
}
} // namespace
#endif /* #ifndef RATE_TRANSFORMER_NODE_IMPL_H */