-
Notifications
You must be signed in to change notification settings - Fork 0
/
HSolveActive.cpp
403 lines (343 loc) · 12 KB
/
HSolveActive.cpp
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/**********************************************************************
** This program is part of 'MOOSE', the
** Messaging Object Oriented Simulation Environment.
** copyright (C) 2003-2007 Upinder S. Bhalla, Niraj Dudani and NCBS
** It is made available under the terms of the
** GNU Lesser General Public License version 2.1
** See the file COPYING.LIB for the full notice.
**********************************************************************/
#include "header.h"
#include <queue>
#include "HSolveStruct.h"
#include "HinesMatrix.h"
#include "HSolvePassive.h"
#include "RateLookup.h"
#include "HSolveActive.h"
#include "HSolve.h"
#include "../biophysics/CompartmentBase.h"
#include "../biophysics/Compartment.h"
#include "../biophysics/CaConcBase.h"
#include "ZombieCaConc.h"
using namespace moose;
//~ #include "ZombieCompartment.h"
//~ #include "ZombieCaConc.h"
#include "PN2S/HelperFunctions.h"
extern ostream& operator <<( ostream& s, const HinesMatrix& m );
const int HSolveActive::INSTANT_X = 1;
const int HSolveActive::INSTANT_Y = 2;
const int HSolveActive::INSTANT_Z = 4;
HSolveActive::HSolveActive()
{
caAdvance_ = 1;
// Default lookup table size
//~ vDiv_ = 3000; // for voltage
//~ caDiv_ = 3000; // for calcium
}
//////////////////////////////////////////////////////////////////////
// Solving differential equations
//////////////////////////////////////////////////////////////////////
void HSolveActive::step( ProcPtr info )
{
if ( nCompt_ <= 0 )
return;
if ( !current_.size() )
{
current_.resize( channel_.size() );
}
// _printVector(VMid_);
// _printVector(HS_);
// cout << "hm0:"<< endl << flush;
// for (int i = 0; i < nCompt_; ++i) {
// for (int j = 0; j < nCompt_; ++j) {
// cout << getA(i,j) << "\t";
// }
// cout << endl << flush;
// }
//
// _printVector(V_, "V");
advanceChannels( info->dt );
calculateChannelCurrents();
updateMatrix();
// cout << HS_.size() << endl<<flush;
// cout << "hm1:"<< endl << flush;
// for (int i = 0; i < nCompt_; ++i) {
// for (int j = 0; j < nCompt_; ++j) {
// cout << std::setprecision (std::numeric_limits< double >::digits10)<<getA(i,j) << "\t";
// }
// cout << endl << flush;
// }
// cout << "b:"<< endl << flush;
// for (int i = 0; i < nCompt_; ++i) {
// cout << std::setprecision (std::numeric_limits< double >::digits10)<<getB(i) << "\t";
// }
// cout << endl << flush;
// _printVector(HS_);
HSolvePassive::forwardEliminate();
// _printVector(HS_);
HSolvePassive::backwardSubstitute();
// _printVector(HS_);
// _printVector(VMid_);
// _printVector(V_);
advanceCalcium();
advanceSynChans( info );
sendValues( info );
sendSpikes( info );
externalCurrent_.assign( externalCurrent_.size(), 0.0 );
}
void HSolveActive::calculateChannelCurrents()
{
vector< ChannelStruct >::iterator ichan;
vector< CurrentStruct >::iterator icurrent = current_.begin();
if ( state_.size() != 0 )
{
double* istate = &state_[ 0 ];
for ( ichan = channel_.begin(); ichan != channel_.end(); ++ichan )
{
ichan->process( istate, *icurrent );
++icurrent;
}
}
}
void HSolveActive::updateMatrix()
{
/*
* Copy contents of HJCopy_ into HJ_. Cannot do a vector assign() because
* iterators to HJ_ get invalidated in MS VC++
*/
if ( HJ_.size() != 0 )
memcpy( &HJ_[ 0 ], &HJCopy_[ 0 ], sizeof( double ) * HJ_.size() );
double GkSum, GkEkSum;
vector< CurrentStruct >::iterator icurrent = current_.begin();
vector< currentVecIter >::iterator iboundary = currentBoundary_.begin();
vector< double >::iterator ihs = HS_.begin();
vector< double >::iterator iv = V_.begin();
vector< CompartmentStruct >::iterator ic;
for ( ic = compartment_.begin(); ic != compartment_.end(); ++ic )
{
GkSum = 0.0;
GkEkSum = 0.0;
for ( ; icurrent < *iboundary; ++icurrent )
{
GkSum += icurrent->Gk;
GkEkSum += icurrent->Gk * icurrent->Ek;
// cout << "\t( "<< icurrent->Gk<< ", "<< icurrent->Ek <<" )"<<endl<<flush ;
}
// cout <<" Const: "<< std::setprecision (std::numeric_limits< double >::digits10) <<*(2+ihs) << "\t"<< GkSum << "\t"<< GkEkSum << endl<<flush;
*ihs = *( 2 + ihs ) + GkSum;
*( 3 + ihs ) = *iv * ic->CmByDt + ic->EmByRm + GkEkSum;
// cout << *ihs << " "<< *(2+ihs)<< " "<< *(3+ihs) <<endl<<flush ;
++iboundary, ihs += 4, ++iv;
}
// cout <<endl<<flush ;
map< unsigned int, InjectStruct >::iterator inject;
for ( inject = inject_.begin(); inject != inject_.end(); ++inject )
{
unsigned int ic = inject->first;
InjectStruct& value = inject->second;
assert(!(value.injectVarying));
// cout <<value.injectVarying << " "<< value.injectBasal<< endl <<flush;
HS_[ 4 * ic + 3 ] += value.injectVarying + value.injectBasal;
value.injectVarying = 0.0;
}
// Synapses are being handled as external channels.
//~ double Gk, Ek;
//~ vector< SynChanStruct >::iterator isyn;
//~ for ( isyn = synchan_.begin(); isyn != synchan_.end(); ++isyn ) {
//~ get< double >( isyn->elm_, synGkFinfo, Gk );
//~ get< double >( isyn->elm_, synEkFinfo, Ek );
//~
//~ unsigned int ic = isyn->compt_;
//~ HS_[ 4 * ic ] += Gk;
//~ HS_[ 4 * ic + 3 ] += Gk * Ek;
//~ }
ihs = HS_.begin();
vector< double >::iterator iec;
for ( iec = externalCurrent_.begin(); iec != externalCurrent_.end(); iec += 2 )
{
// if(*( iec ) > 0 || *( iec + 1 ) > 0)
// cout <<"SPIKEEEEEEEEEEEEE"<<*( iec )<< " " << *( iec + 1 ) << endl <<flush;
*ihs += *iec;
*( 3 + ihs ) += *( iec + 1 );
ihs += 4;
}
stage_ = 0; // Update done.
}
void HSolveActive::advanceCalcium()
{
vector< double* >::iterator icatarget = caTarget_.begin();
vector< double >::iterator ivmid = VMid_.begin();
vector< CurrentStruct >::iterator icurrent = current_.begin();
vector< currentVecIter >::iterator iboundary = currentBoundary_.begin();
caActivation_.assign( caActivation_.size(), 0.0 );
/*
* caAdvance_: This flag determines how current flowing into a calcium pool
* is computed. A value of 0 means that the membrane potential at the
* beginning of the time-step is used for the calculation. This is how
* GENESIS does its computations. A value of 1 means the membrane potential
* at the middle of the time-step is used. This is the correct way of
* integration, and is the default way.
*/
if ( caAdvance_ == 1 )
{
for ( ; iboundary != currentBoundary_.end(); ++iboundary )
{
for ( ; icurrent < *iboundary; ++icurrent )
{
if ( *icatarget )
**icatarget += icurrent->Gk * ( icurrent->Ek - *ivmid );
++icatarget;
}
++ivmid;
}
}
else if ( caAdvance_ == 0 )
{
vector< double >::iterator iv = V_.begin();
double v0;
for ( ; iboundary != currentBoundary_.end(); ++iboundary )
{
for ( ; icurrent < *iboundary; ++icurrent )
{
if ( *icatarget )
{
v0 = ( 2 * *ivmid - *iv );
**icatarget += icurrent->Gk * ( icurrent->Ek - v0 );
}
++icatarget;
}
++ivmid, ++iv;
}
}
vector< CaConcStruct >::iterator icaconc;
vector< double >::iterator icaactivation = caActivation_.begin();
vector< double >::iterator ica = ca_.begin();
for ( icaconc = caConc_.begin(); icaconc != caConc_.end(); ++icaconc )
{
*ica = icaconc->process( *icaactivation );
++ica, ++icaactivation;
}
}
void HSolveActive::advanceChannels( double dt )
{
vector< double >::iterator iv;
vector< double >::iterator istate = state_.begin();
vector< int >::iterator ichannelcount = channelCount_.begin();
vector< ChannelStruct >::iterator ichan = channel_.begin();
vector< ChannelStruct >::iterator chanBoundary;
vector< unsigned int >::iterator icacount = caCount_.begin();
vector< double >::iterator ica = ca_.begin();
vector< double >::iterator caBoundary;
vector< LookupColumn >::iterator icolumn = column_.begin();
vector< LookupRow >::iterator icarowcompt;
vector< LookupRow* >::iterator icarow = caRow_.begin();
LookupRow vRow;
double C1, C2;
for ( iv = V_.begin(); iv != V_.end(); ++iv )
{
vTable_.row( *iv, vRow );
icarowcompt = caRowCompt_.begin();
caBoundary = ica + *icacount;
for ( ; ica < caBoundary; ++ica )
{
caTable_.row( *ica, *icarowcompt );
++icarowcompt;
}
/*
* Optimize by moving "if ( instant )" outside the loop, because it is
* rarely used. May also be able to avoid "if ( power )".
*
* Or not: excellent branch predictors these days.
*
* Will be nice to test these optimizations.
*/
chanBoundary = ichan + *ichannelcount;
for ( ; ichan < chanBoundary; ++ichan )
{
if ( ichan->Xpower_ > 0.0 )
{
vTable_.lookup( *icolumn, vRow, C1, C2 );
//~ *istate = *istate * C1 + C2;
//~ *istate = ( C1 + ( 2 - C2 ) * *istate ) / C2;
if ( ichan->instant_ & INSTANT_X )
*istate = C1 / C2;
else
{
double temp = 1.0 + dt / 2.0 * C2;
*istate = ( *istate * ( 2.0 - temp ) + dt * C1 ) / temp;
}
++icolumn, ++istate;
}
if ( ichan->Ypower_ > 0.0 )
{
vTable_.lookup( *icolumn, vRow, C1, C2 );
//~ *istate = *istate * C1 + C2;
//~ *istate = ( C1 + ( 2 - C2 ) * *istate ) / C2;
if ( ichan->instant_ & INSTANT_Y )
*istate = C1 / C2;
else
{
double temp = 1.0 + dt / 2.0 * C2;
*istate = ( *istate * ( 2.0 - temp ) + dt * C1 ) / temp;
}
++icolumn, ++istate;
}
if ( ichan->Zpower_ > 0.0 )
{
LookupRow* caRow = *icarow;
if ( caRow )
{
caTable_.lookup( *icolumn, *caRow, C1, C2 );
}
else
{
vTable_.lookup( *icolumn, vRow, C1, C2 );
}
//~ *istate = *istate * C1 + C2;
//~ *istate = ( C1 + ( 2 - C2 ) * *istate ) / C2;
if ( ichan->instant_ & INSTANT_Z )
*istate = C1 / C2;
else
{
double temp = 1.0 + dt / 2.0 * C2;
*istate = ( *istate * ( 2.0 - temp ) + dt * C1 ) / temp;
}
++icolumn, ++istate, ++icarow;
}
}
++ichannelcount, ++icacount;
}
}
/**
* SynChans are currently not under solver's control
*/
void HSolveActive::advanceSynChans( ProcPtr info )
{
return;
}
void HSolveActive::sendSpikes( ProcPtr info )
{
vector< SpikeGenStruct >::iterator ispike;
for ( ispike = spikegen_.begin(); ispike != spikegen_.end(); ++ispike )
ispike->send( info );
}
/**
* This function dispatches state values via any source messages on biophysical
* objects which have been taken over.
*
*/
void HSolveActive::sendValues( ProcPtr info )
{
vector< unsigned int >::iterator i;
for ( i = outVm_.begin(); i != outVm_.end(); ++i )
moose::Compartment::VmOut()->send(
//~ ZombieCompartment::VmOut()->send(
compartmentId_[ *i ].eref(),
V_[ *i ]
);
for ( i = outCa_.begin(); i != outCa_.end(); ++i )
//~ CaConc::concOut()->send(
CaConcBase::concOut()->send(
caConcId_[ *i ].eref(),
ca_[ *i ]
);
}