forked from nest/nest-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathac_generator.cpp
More file actions
262 lines (219 loc) · 6.56 KB
/
Copy pathac_generator.cpp
File metadata and controls
262 lines (219 loc) · 6.56 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
/*
* ac_generator.cpp
*
* 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/>.
*
*/
#include "ac_generator.h"
// C++ includes:
#include <cmath>
// Includes from libnestutil:
#include "dict_util.h"
#include "numerics.h"
// Includes from nestkernel:
#include "event_delivery_manager_impl.h"
#include "kernel_manager.h"
#include "universal_data_logger_impl.h"
// Includes from sli:
#include "dict.h"
#include "dictutils.h"
#include "doubledatum.h"
namespace nest
{
RecordablesMap< ac_generator > ac_generator::recordablesMap_;
template <>
void
RecordablesMap< ac_generator >::create()
{
insert_( Name( names::I ), &ac_generator::get_I_ );
}
}
/* ----------------------------------------------------------------
* Default constructors defining default parameters and state
* ---------------------------------------------------------------- */
nest::ac_generator::Parameters_::Parameters_()
: amp_( 0.0 ) // pA
, offset_( 0.0 ) // pA
, freq_( 0.0 ) // Hz
, phi_deg_( 0.0 ) // degree
{
}
nest::ac_generator::Parameters_::Parameters_( const Parameters_& p )
: amp_( p.amp_ )
, offset_( p.offset_ )
, freq_( p.freq_ )
, phi_deg_( p.phi_deg_ )
{
}
nest::ac_generator::Parameters_&
nest::ac_generator::Parameters_::operator=( const Parameters_& p )
{
if ( this == &p )
{
return *this;
}
amp_ = p.amp_;
offset_ = p.offset_;
freq_ = p.freq_;
phi_deg_ = p.phi_deg_;
return *this;
}
nest::ac_generator::State_::State_()
: y_0_( 0.0 )
, y_1_( 0.0 ) // pA
, I_( 0.0 ) // pA
{
}
nest::ac_generator::Buffers_::Buffers_( ac_generator& n )
: logger_( n )
{
}
nest::ac_generator::Buffers_::Buffers_( const Buffers_&, ac_generator& n )
: logger_( n )
{
}
/* ----------------------------------------------------------------
* Parameter extraction and manipulation functions
* ---------------------------------------------------------------- */
void
nest::ac_generator::Parameters_::get( DictionaryDatum& d ) const
{
( *d )[ names::amplitude ] = amp_;
( *d )[ names::offset ] = offset_;
( *d )[ names::phase ] = phi_deg_;
( *d )[ names::frequency ] = freq_;
}
void
nest::ac_generator::State_::get( DictionaryDatum& d ) const
{
( *d )[ names::y_0 ] = y_0_;
( *d )[ names::y_1 ] = y_1_;
}
void
nest::ac_generator::Parameters_::set( const DictionaryDatum& d, Node* node )
{
updateValueParam< double >( d, names::amplitude, amp_, node );
updateValueParam< double >( d, names::offset, offset_, node );
updateValueParam< double >( d, names::frequency, freq_, node );
updateValueParam< double >( d, names::phase, phi_deg_, node );
}
/* ----------------------------------------------------------------
* Default and copy constructor for node
* ---------------------------------------------------------------- */
nest::ac_generator::ac_generator()
: StimulationDevice()
, P_()
, S_()
, B_( *this )
{
recordablesMap_.create();
}
nest::ac_generator::ac_generator( const ac_generator& n )
: StimulationDevice( n )
, P_( n.P_ )
, S_( n.S_ )
, B_( n.B_, *this )
{
}
/* ----------------------------------------------------------------
* Node initialization functions
* ---------------------------------------------------------------- */
void
nest::ac_generator::init_state_()
{
StimulationDevice::init_state();
}
void
nest::ac_generator::init_buffers_()
{
StimulationDevice::init_buffers();
B_.logger_.reset();
}
void
nest::ac_generator::pre_run_hook()
{
B_.logger_.init();
StimulationDevice::pre_run_hook();
const double h = Time::get_resolution().get_ms();
const double t = kernel().simulation_manager.get_time().get_ms();
// scale Hz to ms
const double omega = 2.0 * numerics::pi * P_.freq_ / 1000.0;
const double phi_rad = P_.phi_deg_ * 2.0 * numerics::pi / 360.0;
// initial state
S_.y_0_ = P_.amp_ * std::cos( omega * t + phi_rad );
S_.y_1_ = P_.amp_ * std::sin( omega * t + phi_rad );
// matrix elements
V_.A_00_ = std::cos( omega * h );
V_.A_01_ = -std::sin( omega * h );
V_.A_10_ = std::sin( omega * h );
V_.A_11_ = std::cos( omega * h );
}
void
nest::ac_generator::update( Time const& origin, const long from, const long to )
{
assert( to >= 0 and ( delay ) from < kernel().connection_manager.get_min_delay() );
assert( from < to );
long start = origin.get_steps();
CurrentEvent ce;
for ( long lag = from; lag < to; ++lag )
{
// We need to iterate the oscillator throughout all steps, even when the
// device is not active, since inactivity only windows the oscillator.
const double y_0 = S_.y_0_;
S_.y_0_ = V_.A_00_ * y_0 + V_.A_01_ * S_.y_1_;
S_.y_1_ = V_.A_10_ * y_0 + V_.A_11_ * S_.y_1_;
S_.I_ = 0.0;
if ( StimulationDevice::is_active( Time::step( start + lag ) ) )
{
S_.I_ = S_.y_1_ + P_.offset_;
ce.set_current( S_.I_ );
kernel().event_delivery_manager.send( *this, ce, lag );
}
B_.logger_.record_data( origin.get_steps() + lag );
}
}
void
nest::ac_generator::handle( DataLoggingRequest& e )
{
B_.logger_.handle( e );
}
/* ----------------------------------------------------------------
* Other functions
* ---------------------------------------------------------------- */
void
nest::ac_generator::set_data_from_stimulation_backend( std::vector< double >& input_param )
{
Parameters_ ptmp = P_; // temporary copy in case of errors
// For the input backend
if ( not input_param.empty() )
{
if ( input_param.size() != 4 )
{
throw BadParameterValue(
"The size of the data for the ac_generator needs to be 4 [amplitude, offset, frequency, phase]." );
}
DictionaryDatum d = DictionaryDatum( new Dictionary );
( *d )[ names::amplitude ] = DoubleDatum( input_param[ 0 ] );
( *d )[ names::offset ] = DoubleDatum( input_param[ 1 ] );
( *d )[ names::frequency ] = DoubleDatum( input_param[ 2 ] );
( *d )[ names::phase ] = DoubleDatum( input_param[ 3 ] );
ptmp.set( d, this );
}
// if we get here, temporary contains consistent set of properties
P_ = ptmp;
}