-
Notifications
You must be signed in to change notification settings - Fork 0
/
HinesMatrix.cpp
756 lines (638 loc) · 19.6 KB
/
HinesMatrix.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/**********************************************************************
** 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 "HinesMatrix.h"
#include <sstream>
#include <iomanip>
#include <stdexcept>
HinesMatrix::HinesMatrix()
:
nCompt_( 0 ),
dt_( 0.0 ),
stage_( -1 )
{
;
}
void HinesMatrix::setup( const vector< TreeNodeStruct >& tree, double dt )
{
clear();
nCompt_ = tree.size();
#if SANITY_CHECK
stringstream ss;
if(nCompt_ <= 0)
{
ss << "Horror, horror! Trying to create a matrix with size " << nCompt_
<< endl;
dump(ss.str(), "ERROR");
throw range_error("Expected greater than 0.");
}
#endif /* ----- not STRICT_CHECK ----- */
dt_ = dt;
tree_ = &tree;
for ( unsigned int i = 0; i < nCompt_; i++ )
Ga_.push_back( 2.0 / tree[ i ].Ra );
makeJunctions();
makeMatrix();
makeOperands();
}
void HinesMatrix::clear()
{
nCompt_ = 0;
dt_ = 0.0;
junction_.clear();
HS_.clear();
HJ_.clear();
HJCopy_.clear();
VMid_.clear();
operand_.clear();
backOperand_.clear();
stage_ = 0;
tree_ = 0;
Ga_.clear();
coupled_.clear();
operandBase_.clear();
groupNumber_.clear();
}
bool groupCompare(
const vector< unsigned int >& A,
const vector< unsigned int >& B )
{
if ( A.empty() || B.empty() )
return 0;
return A[ 0 ] < B[ 0 ];
}
// Stage 3
void HinesMatrix::makeJunctions()
{
// 3.1
for ( unsigned int i = 0; i < nCompt_; ++i )
{
const vector< unsigned int >& c = ( *tree_ )[ i ].children;
if ( c.size() == 0 )
continue;
if ( c.size() == 1 )
{
int diff = ( int )( c[ 0 ] ) - i;
if ( diff == 1 || diff == -1 )
continue;
}
// "coupled" contains a list of all children..
coupled_.push_back( c );
// ..and the parent compartment itself.
coupled_.back().push_back( i );
}
// 3.2
vector< vector< unsigned int > >::iterator group;
for ( group = coupled_.begin(); group != coupled_.end(); ++group )
sort( group->begin(), group->end() );
sort( coupled_.begin(), coupled_.end(), groupCompare );
// 3.3
unsigned int index;
unsigned int rank;
for ( group = coupled_.begin(); group != coupled_.end(); ++group )
// Loop uptil penultimate compartment in group
for ( unsigned int c = 0; c < group->size() - 1; ++c )
{
index = ( *group )[ c ];
rank = group->size() - c - 1;
junction_.push_back( JunctionStruct( index, rank ) );
groupNumber_[ index ] = group - coupled_.begin();
}
sort( junction_.begin(), junction_.end() );
}
// Stage 4
void HinesMatrix::makeMatrix()
{
const vector< TreeNodeStruct >& node = *tree_;
// Setting up HS
HS_.resize( 4 * nCompt_, 0.0 );
for ( unsigned int i = 0; i < nCompt_; ++i )
HS_[ 4 * i + 2 ] =
node[ i ].Cm / ( dt_ / 2.0 ) +
1.0 / node[ i ].Rm;
double gi, gj, gij;
vector< JunctionStruct >::iterator junction = junction_.begin();
for ( unsigned int i = 0; i < nCompt_ - 1; ++i )
{
if ( !junction_.empty() &&
junction < junction_.end() &&
i == junction->index )
{
++junction;
continue;
}
gi = Ga_[ i ];
gj = Ga_[ i + 1 ];
gij = gi * gj / ( gi + gj );
HS_[ 4 * i + 1 ] = -gij;
HS_[ 4 * i + 2 ] += gij;
HS_[ 4 * i + 6 ] += gij;
}
vector< vector< unsigned int > >::iterator group;
vector< unsigned int >::iterator i;
for ( group = coupled_.begin(); group != coupled_.end(); ++group )
{
double gsum = 0.0;
for ( i = group->begin(); i != group->end(); ++i )
gsum += Ga_[ *i ];
for ( i = group->begin(); i != group->end(); ++i )
{
gi = Ga_[ *i ];
HS_[ 4 * *i + 2 ] += gi * ( 1.0 - gi / gsum );
}
}
// Setting up HJ
vector< unsigned int >::iterator j;
unsigned int size = 0;
unsigned int rank;
for ( group = coupled_.begin(); group != coupled_.end(); ++group )
{
rank = group->size() - 1;
size += rank * ( rank + 1 );
}
HJ_.reserve( size );
for ( group = coupled_.begin(); group != coupled_.end(); ++group )
{
double gsum = 0.0;
for ( i = group->begin(); i != group->end(); ++i )
gsum += Ga_[ *i ];
for ( i = group->begin(); i != group->end() - 1; ++i )
{
int base = HJ_.size();
for ( j = i + 1; j != group->end(); ++j )
{
gij = Ga_[ *i ] * Ga_[ *j ] / gsum;
HJ_.push_back( -gij );
HJ_.push_back( -gij );
}
//~ operandBase_[ *i ] = &HJ_[ base ];
operandBase_[ *i ] = HJ_.begin() + base;
}
}
// Copy diagonal elements into their final locations
for ( unsigned int i = 0; i < nCompt_; ++i )
HS_[ 4 * i ] = HS_[ 4 * i + 2 ];
// Create copy of HJ
HJCopy_.assign( HJ_.begin(), HJ_.end() );
}
// Stage 5
void HinesMatrix::makeOperands()
{
unsigned int index;
unsigned int rank;
unsigned int farIndex;
vdIterator base;
vector< JunctionStruct >::iterator junction;
// Allocate space in VMid. Needed, since we will store pointers to its
// elements below.
VMid_.resize( nCompt_ );
// Operands for forward-elimination
for ( junction = junction_.begin(); junction != junction_.end(); ++junction )
{
index = junction->index;
rank = junction->rank;
base = operandBase_[ index ];
// This is the list of compartments connected at a junction.
const vector< unsigned int >& group =
coupled_[ groupNumber_[ index ] ];
if ( rank == 1 )
{
operand_.push_back( base );
// Select last member.
farIndex = group[ group.size() - 1 ];
operand_.push_back( HS_.begin() + 4 * farIndex );
operand_.push_back( VMid_.begin() + farIndex );
}
else if ( rank == 2 )
{
operand_.push_back( base );
// Select 2nd last member.
farIndex = group[ group.size() - 2 ];
operand_.push_back( HS_.begin() + 4 * farIndex );
operand_.push_back( VMid_.begin() + farIndex );
// Select last member.
farIndex = group[ group.size() - 1 ];
operand_.push_back( HS_.begin() + 4 * farIndex );
operand_.push_back( VMid_.begin() + farIndex );
}
else
{
// Operations on diagonal elements and elements from B (as in Ax = B).
int start = group.size() - rank;
for ( unsigned int j = 0; j < rank; ++j )
{
farIndex = group[ start + j ];
// Diagonal elements
operand_.push_back( HS_.begin() + 4 * farIndex );
operand_.push_back( base + 2 * j );
operand_.push_back( base + 2 * j + 1 );
// Elements from B
operand_.push_back( HS_.begin() + 4 * farIndex + 3 );
operand_.push_back( HS_.begin() + 4 * index + 3 );
operand_.push_back( base + 2 * j + 1 );
}
// Operations on off-diagonal elements.
vdIterator left;
vdIterator above;
vdIterator target;
// Upper triangle elements
left = base + 1;
target = base + 2 * rank;
for ( unsigned int i = 1; i < rank; ++i )
{
above = base + 2 * i;
for ( unsigned int j = 0; j < rank - i; ++j )
{
operand_.push_back( target );
operand_.push_back( above );
operand_.push_back( left );
above += 2;
target += 2;
}
left += 2;
}
// Lower triangle elements
target = base + 2 * rank + 1;
above = base;
for ( unsigned int i = 1; i < rank; ++i )
{
left = base + 2 * i + 1;
for ( unsigned int j = 0; j < rank - i; ++j )
{
operand_.push_back( target );
operand_.push_back( above );
operand_.push_back( left );
/*
* This check required because the MS VC++ compiler is
* paranoid about iterators going out of bounds, even if
* they are never used after that.
*/
if ( i == rank - 1 && j == rank - i - 1 )
continue;
target += 2;
left += 2;
}
above += 2;
}
}
}
// Operands for backward substitution
for ( junction = junction_.begin(); junction != junction_.end(); ++junction )
{
if ( junction->rank < 3 )
continue;
index = junction->index;
rank = junction->rank;
base = operandBase_[ index ];
// This is the list of compartments connected at a junction.
const vector< unsigned int >& group =
coupled_[ groupNumber_[ index ] ];
unsigned int start = group.size() - rank;
for ( unsigned int j = 0; j < rank; ++j )
{
farIndex = group[ start + j ];
backOperand_.push_back( base + 2 * j );
backOperand_.push_back( VMid_.begin() + farIndex );
}
}
}
///////////////////////////////////////////////////////////////////////////
// Public interface to matrix
///////////////////////////////////////////////////////////////////////////
unsigned int HinesMatrix::getSize() const
{
return nCompt_;
}
double HinesMatrix::getA( unsigned int row, unsigned int col ) const
{
/*
* If forward elimination is done, or backward substitution is done, and
* if (row, col) is in the lower triangle, then return 0.
*/
if ( ( stage_ == 1 || stage_ == 2 ) && row > col )
return 0.0;
if ( row >= nCompt_ || col >= nCompt_ )
return 0.0;
if ( row == col )
return HS_[ 4 * row ];
unsigned int smaller = row < col ? row : col;
unsigned int bigger = row > col ? row : col;
if ( groupNumber_.find( smaller ) == groupNumber_.end() )
{
if ( bigger - smaller == 1 )
return HS_[ 4 * smaller + 1 ];
else
return 0.0;
}
else
{
// We could use: groupNumber = groupNumber_[ smaller ], but this is a
// const function
unsigned int groupNumber = groupNumber_.find( smaller )->second;
const vector< unsigned int >& group = coupled_[ groupNumber ];
unsigned int location, size;
unsigned int smallRank, bigRank;
if ( find( group.begin(), group.end(), bigger ) != group.end() )
{
location = 0;
for ( int i = 0; i < static_cast< int >( groupNumber ); ++i )
{
size = coupled_[ i ].size();
location += size * ( size - 1 );
}
size = group.size();
smallRank = group.end() - find( group.begin(), group.end(), smaller ) - 1;
bigRank = group.end() - find( group.begin(), group.end(), bigger ) - 1;
location += size * ( size - 1 ) - smallRank * ( smallRank + 1 );
location += 2 * ( smallRank - bigRank - 1 );
if ( row == smaller )
return HJ_[ location ];
else
return HJ_[ location + 1 ];
}
else
{
return 0.0;
}
}
}
double HinesMatrix::getB( unsigned int row ) const
{
return HS_[ 4 * row + 3 ];
}
double HinesMatrix::getVMid( unsigned int row ) const
{
return VMid_[ row ];
}
///////////////////////////////////////////////////////////////////////////
// Inserting into a stream
///////////////////////////////////////////////////////////////////////////
ostream& operator <<( ostream& s, const HinesMatrix& m )
{
unsigned int size = m.getSize();
s << "\nA:\n";
for ( unsigned int i = 0; i < size; i++ )
{
for ( unsigned int j = 0; j < size; j++ )
s << setw( 12 ) << setprecision( 5 ) << m.getA( i, j );
s << "\n";
}
s << "\n" << "V:\n";
for ( unsigned int i = 0; i < size; i++ )
s << m.getVMid( i ) << "\n";
s << "\n" << "B:\n";
for ( unsigned int i = 0; i < size; i++ )
s << m.getB( i ) << "\n";
return s;
}
///////////////////////////////////////////////////////////////////////////
#ifdef DO_UNIT_TESTS
#include "TestHSolve.h"
void testHinesMatrix()
{
vector< int* > childArray;
vector< unsigned int > childArraySize;
/**
* We test if the Hines' matrix is correctly setup for the following cell:
*
* Soma---> 15 - 14 - 13 - 12
* | |
* | L 11 - 10
* |
* L 16 - 17 - 18 - 19
* |
* L 9 - 8 - 7 - 6 - 5
* | |
* | L 4 - 3
* |
* L 2 - 1 - 0
*
* The numbers are the hines indices of compartments. Compartment X is the
* child of compartment Y if X is one level further away from the soma (#15)
* than Y. So #17 is the parent of #'s 2, 9 and 18.
*/
int childArray_1[ ] =
{
/* c0 */ -1,
/* c1 */ -1, 0,
/* c2 */ -1, 1,
/* c3 */ -1,
/* c4 */ -1, 3,
/* c5 */ -1,
/* c6 */ -1, 5,
/* c7 */ -1, 4, 6,
/* c8 */ -1, 7,
/* c9 */ -1, 8,
/* c10 */ -1,
/* c11 */ -1, 10,
/* c12 */ -1,
/* c13 */ -1, 12,
/* c14 */ -1, 11, 13,
/* c15 */ -1, 14, 16,
/* c16 */ -1, 17,
/* c17 */ -1, 2, 9, 18,
/* c18 */ -1, 19,
/* c19 */ -1,
};
childArray.push_back( childArray_1 );
childArraySize.push_back( sizeof( childArray_1 ) / sizeof( int ) );
/**
* Cell 2:
*
* 3
* |
* Soma---> 2
* / \
* / \
* 1 0
*
*/
int childArray_2[ ] =
{
/* c0 */ -1,
/* c1 */ -1,
/* c2 */ -1, 0, 1, 3,
/* c3 */ -1,
};
childArray.push_back( childArray_2 );
childArraySize.push_back( sizeof( childArray_2 ) / sizeof( int ) );
/**
* Cell 3:
*
* 3
* |
* 2
* / \
* / \
* 1 0 <--- Soma
*
*/
int childArray_3[ ] =
{
/* c0 */ -1, 2,
/* c1 */ -1,
/* c2 */ -1, 1, 3,
/* c3 */ -1,
};
childArray.push_back( childArray_3 );
childArraySize.push_back( sizeof( childArray_3 ) / sizeof( int ) );
/**
* Cell 4:
*
* 3 <--- Soma
* |
* 2
* / \
* / \
* 1 0
*
*/
int childArray_4[ ] =
{
/* c0 */ -1,
/* c1 */ -1,
/* c2 */ -1, 0, 1,
/* c3 */ -1, 2,
};
childArray.push_back( childArray_4 );
childArraySize.push_back( sizeof( childArray_4 ) / sizeof( int ) );
/**
* Cell 5:
*
* 1 <--- Soma
* |
* 2
* / \
* 4 0
* / \
* 3 5
*
*/
int childArray_5[ ] =
{
/* c0 */ -1,
/* c1 */ -1, 2,
/* c2 */ -1, 0, 4,
/* c3 */ -1,
/* c4 */ -1, 3, 5,
/* c5 */ -1,
};
childArray.push_back( childArray_5 );
childArraySize.push_back( sizeof( childArray_5 ) / sizeof( int ) );
/**
* Cell 6:
*
* 3 <--- Soma
* L 4
* L 6
* L 5
* L 2
* L 1
* L 0
*
*/
int childArray_6[ ] =
{
/* c0 */ -1,
/* c1 */ -1,
/* c2 */ -1,
/* c3 */ -1, 4,
/* c4 */ -1, 0, 1, 2, 5, 6,
/* c5 */ -1,
/* c6 */ -1,
};
childArray.push_back( childArray_6 );
childArraySize.push_back( sizeof( childArray_6 ) / sizeof( int ) );
/**
* Cell 7: Single compartment
*/
int childArray_7[ ] =
{
/* c0 */ -1,
};
childArray.push_back( childArray_7 );
childArraySize.push_back( sizeof( childArray_7 ) / sizeof( int ) );
/**
* Cell 8: 3 compartments; soma is in the middle.
*/
int childArray_8[ ] =
{
/* c0 */ -1,
/* c1 */ -1, 0, 2,
/* c2 */ -1,
};
childArray.push_back( childArray_8 );
childArraySize.push_back( sizeof( childArray_8 ) / sizeof( int ) );
/**
* Cell 9: 3 compartments; first compartment is soma.
*/
int childArray_9[ ] =
{
/* c0 */ -1, 1,
/* c1 */ -1, 2,
/* c2 */ -1,
};
childArray.push_back( childArray_9 );
childArraySize.push_back( sizeof( childArray_9 ) / sizeof( int ) );
////////////////////////////////////////////////////////////////////////////
// Run tests
////////////////////////////////////////////////////////////////////////////
HinesMatrix H;
vector< TreeNodeStruct > tree;
double dt = 1.0;
/*
* This is the full reference matrix which will be compared to its sparse
* implementation.
*/
vector< vector< double > > matrix;
double epsilon = 1e-17;
unsigned int i;
unsigned int j;
unsigned int nCompt;
int* array;
unsigned int arraySize;
for ( unsigned int cell = 0; cell < childArray.size(); cell++ )
{
array = childArray[ cell ];
arraySize = childArraySize[ cell ];
nCompt = count( array, array + arraySize, -1 );
// Prepare cell
tree.clear();
tree.resize( nCompt );
for ( i = 0; i < nCompt; ++i )
{
tree[ i ].Ra = 15.0 + 3.0 * i;
tree[ i ].Rm = 45.0 + 15.0 * i;
tree[ i ].Cm = 500.0 + 200.0 * i * i;
}
int count = -1;
for ( unsigned int a = 0; a < arraySize; a++ )
if ( array[ a ] == -1 )
count++;
else
tree[ count ].children.push_back( array[ a ] );
// Prepare local matrix
makeFullMatrix( tree, dt, matrix );
// Prepare sparse matrix
H.setup( tree, dt );
// Compare matrices
for ( i = 0; i < nCompt; ++i )
for ( j = 0; j < nCompt; ++j )
{
ostringstream error;
error << "Testing Hines' Matrix: Cell# "
<< cell + 1 << ", entry (" << i << ", " << j << ")";
ASSERT(
fabs( matrix[ i ][ j ] - H.getA( i, j ) ) < epsilon,
error.str()
);
}
}
}
#endif // DO_UNIT_TESTS