-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpercol2d.cpp
More file actions
936 lines (857 loc) · 24.6 KB
/
percol2d.cpp
File metadata and controls
936 lines (857 loc) · 24.6 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
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
#include "percol2d.h"
#include "mkl.h"
#include <cassert>
#include <QtWidgets/QMessageBox>
#include <QString>
#include <QFile>
#include <cmath>
#include <QMap>
#include <QVector>
#include <QTextStream>
#define DATAOF(array) array.data()
#define MYMAP QMap
// Find maximum value in a container
template<typename T>
T vmax(T *begin, T *end)
{
T res;
for (res = *begin; begin != end; ++begin)
{
if (res < *begin) res = *begin;
}
return res;
}
Percol2D::Percol2D(void)
{
}
Percol2D::~Percol2D(void)
{
}
// Compute using general matrix
void Percol2D::compute_general()
{
int nv = this->nV(); // number of defined nodes
int nw = this->nW(); // number of undefined nodes
int ni = this->nI(); // number of current links
// Build LHS matrix = S_W^T SIGMA S_W
MYARRAY<double> lhs( nw*nw );
lhs.fill(0.0);
for (int i = 0; i < ni; ++i)
{
MYPAIR<int,int> ends = this->ends(i);
int r = ends.first - nv;
int c = ends.second - nv;
double sigma = this->Sigma[i];
if (r >= 0 && c >= 0)
{
double sir = this->S(i,r+nv);
double sic = this->S(i,c+nv);
#define LHS(r,c) lhs[(r)+nw*(c)]
LHS(r,c) += sir * sigma * sic;
LHS(r,r) += sir * sigma * sir;
LHS(c,r) += sic * sigma * sir;
LHS(c,c) += sic * sigma * sic;
}
else if (r >= 0)
{
double sir = this->S(i,r+nv);
LHS(r,r) += sir * sigma * sir;
}
else if (c >= 0)
{
double sic = this->S(i,c+nv);
LHS(c,c) += sic * sigma * sic;
}
}
// Build temp vector = SIGMA S_V V
MYARRAY<double> t( ni );
t.fill(0.0);
for (int i = 0; i < ni; ++i)
{
for (int v = 0; v < nv; ++v)
{
if (this->S(i,v) == 0) continue;
t[i] += this->S(i,v) * this->V[v] * this->Sigma[i];
}
}
// Build right hand side: rhs = -S(...) SIGMA S_V V
MYARRAY<double> rhs( nw );
rhs.fill(0.0);
for (int w = 0; w < nw; ++w)
{
for (int i = 0; i < ni; ++i)
{
if (S(i,nv + w) == 0) continue;
rhs[ w ] -= S(i,nv + w) * t[i];
}
}
int ONE = 1;
MYARRAY<int> ipiv( nw );
// Before factoring with dgetrf we compute anorm, used later to estimate rcond
#if WANT_1NORM_RCOND
double anorm_1 = 0;
for (int c = 0; c < nw; ++c)
{
double colnorm = 0;
for (int r = 0; r < nw; ++r)
{
colnorm += fabs(LHS(r,c));
}
if (colnorm > anorm_1) anorm_1 = colnorm;
}
#else
double anorm_8 = 0;
for (int r = 0; r < nw; ++r)
{
double rownorm = 0;
for (int c = 0; c < nw; ++c)
{
rownorm += fabs(LHS(r,c));
}
if (rownorm > anorm_8) anorm_8 = rownorm;
}
#endif
int info;
//dgesv(&nw, &ONE, lhs.data(), &nw, ipiv.data(), rhs.data(), &nw, &info );
dgetrf(&nw,&nw,DATAOF(lhs),&nw,DATAOF(ipiv),&info);
if (info > 0)
{
// The system is degenerate, so we'll only solve part of it
QString msg;
msg.sprintf("Factorization failed with info = %i (nw=%i)",info,nw);
switch( QMessageBox::warning( 0, "LU failed",
msg,"Continue", "Abort", NULL, 0, 0) )
{
case 0: // Continue
return;
case 1: // Abort
exit(1);
}
}
MYARRAY<double> wrk( 4*nw );
MYARRAY<int> iwk( nw );
#if WANT_1NORM_RCOND
dgecon("1-Norm",&nw,lhs.data(),&nw,&anorm_1,&this->rcond,wrk.data(),iwk.data(),&info);
#else
dgecon("Infinity-norm",&nw,DATAOF(lhs),&nw,&anorm_8,&this->rcond,DATAOF(wrk),DATAOF(iwk),&info);
#endif
// Let's compute cond, which is the product of diagonal elements
assert( info == 0 );
dgetrs("No transpose",&nw,&ONE,DATAOF(lhs),&nw,DATAOF(ipiv),DATAOF(rhs),&nw,&info);
for (int w = 0; w < nw; ++w)
{
this->W[w] = rhs[w];
}
// Given V and W, compute I
for (int i = 0; i < ni; ++i)
{
MYPAIR<int,int> ends = this->ends(i);
if (ends.first < nv)
this->I[i] = - this->Sigma[i] * this->V[ends.first];
else
this->I[i] = - this->Sigma[i] * this->W[ends.first - nv];
if (ends.second < nv)
this->I[i] += this->Sigma[i] * this->V[ends.second];
else
this->I[i] += this->Sigma[i] * this->W[ends.second - nv];
}
}
// Compute using banded matrix
void Percol2D::computeOld()
//void Percol2D::compute_general()
{
int nv = this->nV(); // number of defined nodes
int nw = this->nW(); // number of undefined nodes
int ni = this->nI(); // number of current links
// Build band-stored LHS matrix = S_W^T SIGMA S_W
typedef MYMAP<MYPAIR<int,int>,double> RC_D_map;
RC_D_map nonz; //here we'll keep only nonzero elements of lhs(r,c)
for (int i = 0; i < ni; ++i)
{
MYPAIR<int,int> ends = this->ends(i);
int r = ends.first - nv;
int c = ends.second - nv;
double sigma = this->Sigma[i];
if (r >= 0 && c >= 0)
{
double sir = this->S(i,r+nv);
double sic = this->S(i,c+nv);
#define NONZ(r,c) nonz[MYPAIR<int,int>(r,c)]
NONZ(r,c) += sir * sigma * sic;
NONZ(r,r) += sir * sigma * sir;
NONZ(c,r) += sic * sigma * sir;
NONZ(c,c) += sic * sigma * sic;
}
else if (r >= 0)
{
double sir = this->S(i,r+nv);
NONZ(r,r) += sir * sigma * sir;
}
else if (c >= 0)
{
double sic = this->S(i,c+nv);
NONZ(c,c) += sic * sigma * sic;
}
}
// Now determine kl and ku - numbers of sub- and super- diagonals.
MYARRAY<int> tkl(nw), tku(nw);
tkl.fill(0);
tku.fill(0);
for (RC_D_map::const_iterator e = nonz.constBegin(); e != nonz.constEnd(); ++e)
{
int r = e.key().first;
int c = e.key().second;
if (r > c) tkl[c] = r - c;
if (r < c) tku[c] = c - r;
}
int kl = vmax(tkl.begin(),tkl.end());
int ku = vmax(tku.begin(),tku.end());
// Now prepare the lhs matrix with banded storage
int lhs_rows = kl + ku + 1 + kl; // last kl is for LU factorization with dgbtrf
MYARRAY<double> lhs( lhs_rows * nw );
lhs.fill(0);
for (RC_D_map::const_iterator e = nonz.constBegin(); e != nonz.constEnd(); ++e)
{
int r = e.key().first;
int c = e.key().second;
//aij is stored in ab(kl+ku+1+i-j,j) for max(1,j-ku) ? i ? min(n,j+kl).
#undef LHS
#define LHS(r,c) lhs[kl + ku + r - c + c*lhs_rows]
LHS(r,c) = e.value();
}
// Build temp vector = SIGMA S_V V
MYARRAY<double> t( ni );
t.fill(0.0);
for (int i = 0; i < ni; ++i)
{
for (int v = 0; v < nv; ++v)
{
if (this->S(i,v) == 0) continue;
t[i] += this->S(i,v) * this->V[v] * this->Sigma[i];
}
}
// Build right hand side: rhs = -S(...) SIGMA S_V V
MYARRAY<double> rhs( nw );
rhs.fill(0.0);
for (int w = 0; w < nw; ++w)
{
for (int i = 0; i < ni; ++i)
{
if (S(i,nv + w) == 0) continue;
rhs[ w ] -= S(i,nv + w) * t[i];
}
}
int ONE = 1;
MYARRAY<int> ipiv( nw );
// Before factoring with dgbtrf we compute anorm, used later to estimate rcond
//#define WANT_1NORM_RCOND 1
#if WANT_1NORM_RCOND
double anorm_1 = 0;
for (int c = 0; c < nw; ++c)
{
double colnorm = 0;
for (int r = 0; r < lhs_rows; ++r)
{
colnorm += fabs(LHS(r,c));
}
if (colnorm > anorm_1) anorm_1 = colnorm;
}
#else
double anorm_8 = 0;
for (int r = 0; r < lhs_rows; ++r)
{
double rownorm = 0;
for (int c = 0; c < nw; ++c)
{
rownorm += fabs(LHS(r,c));
}
if (rownorm > anorm_8) anorm_8 = rownorm;
}
#endif
int info;
MYARRAY<double> lhs_saved = lhs;
dgbtrf(&nw,&nw,&kl,&ku, DATAOF(lhs),&lhs_rows,DATAOF(ipiv),&info);
if (info > 0)
{
// The system is degenerate, so we'll only solve part of it
QString msg;
msg.sprintf("Factorization failed with info = %i (nw=%i)",info,nw);
switch( QMessageBox::warning( 0, "LU failed",
msg,"Continue", "Abort", NULL, 0, 0) )
{
case 0: // Continue
return;
case 1: // Abort
exit(1);
}
}
if (0)
{
QFile f("tmp.txt");
f.open(QIODevice::WriteOnly);
QTextStream ts(&f);
for (int i=0; i < nw; ++i)
ts << i << " " << ipiv[i] << "\n";
f.close();
}
MYARRAY<double> wrk( 3*nw );
MYARRAY<int> iwk( nw );
#if WANT_1NORM_RCOND
dgbcon("1-Norm",&nw,&kl,&ku,lhs.data(),&lhs_rows,ipiv.data(),&anorm_1,&this->rcond,
wrk.data(), iwk.data(), &info);
#else
dgbcon("Inf-Norm",&nw,&kl,&ku,DATAOF(lhs),&lhs_rows,DATAOF(ipiv),&anorm_8,&this->rcond,
DATAOF(wrk), DATAOF(iwk), &info);
#endif
// Let's compute cond, which is the product of diagonal elements
assert( info == 0 );
dgbtrs("No transpose",&nw,&kl,&ku,&ONE,DATAOF(lhs),&lhs_rows,DATAOF(ipiv),DATAOF(rhs),&nw,&info);
for (int w = 0; w < nw; ++w)
{
this->W[w] = rhs[w];
}
// Given V and W, compute I
for (int i = 0; i < ni; ++i)
{
MYPAIR<int,int> ends = this->ends(i);
if (ends.first < nv)
this->I[i] = - this->Sigma[i] * this->V[ends.first];
else
this->I[i] = - this->Sigma[i] * this->W[ends.first - nv];
if (ends.second < nv)
this->I[i] += this->Sigma[i] * this->V[ends.second];
else
this->I[i] += this->Sigma[i] * this->W[ends.second - nv];
}
}
// Compute using banded matrix
void Percol2D::compute()
//void Percol2D::compute_general()
{
int nv = this->nV(); // number of defined nodes
int nw = this->nW(); // number of undefined nodes
int ni = this->nI(); // number of current links
// Build band-stored LHS matrix = S_W^T SIGMA S_W
typedef MYMAP<MYPAIR<int,int>,double> RC_D_map;
RC_D_map nonz; //here we'll keep only nonzero elements of lhs(r,c)
for (int i = 0; i < ni; ++i)
{
MYPAIR<int,int> ends = this->ends(i);
int r = ends.first - nv;
int c = ends.second - nv;
double sigma = this->Sigma[i];
if (r >= 0 && c >= 0)
{
double sir = this->S(i,r+nv);
double sic = this->S(i,c+nv);
#undef NONZ
#define NONZ(r,c) nonz[MYPAIR<int,int>(r,c)]
NONZ(r,c) += sir * sigma * sic;
NONZ(r,r) += sir * sigma * sir;
NONZ(c,r) += sic * sigma * sir;
NONZ(c,c) += sic * sigma * sic;
}
else if (r >= 0)
{
double sir = this->S(i,r+nv);
NONZ(r,r) += sir * sigma * sir;
}
else if (c >= 0)
{
double sic = this->S(i,c+nv);
NONZ(c,c) += sic * sigma * sic;
}
}
// Now determine kl and ku - numbers of sub- and super- diagonals.
MYARRAY<int> tkl(nw), tku(nw);
tkl.fill(0);
tku.fill(0);
for (RC_D_map::const_iterator e = nonz.constBegin(); e != nonz.constEnd(); ++e)
{
int r = e.key().first;
int c = e.key().second;
if (r > c) tkl[c] = r - c;
if (r < c) tku[c] = c - r;
}
int kl = vmax(tkl.begin(),tkl.end());
int ku = vmax(tku.begin(),tku.end());
// Now prepare the lhs matrix with banded storage
int lhs_rows = /*kl +*/ ku + 1 + kl; // last kl is for LU factorization with dgbtrf
MYARRAY<double> lhs( lhs_rows * nw );
lhs.fill(0);
for (RC_D_map::const_iterator e = nonz.constBegin(); e != nonz.constEnd(); ++e)
{
int r = e.key().first;
int c = e.key().second;
//aij is stored in ab(kl+ku+1+i-j,j) for max(1,j-ku) ? i ? min(n,j+kl).
#undef LHS
#define LHS(r,c) lhs[/*kl +*/ ku + r - c + c*lhs_rows]
LHS(r,c) = e.value();
}
// Build temp vector = SIGMA S_V V
MYARRAY<double> t( ni );
t.fill(0.0);
for (int i = 0; i < ni; ++i)
{
for (int v = 0; v < nv; ++v)
{
if (this->S(i,v) == 0) continue;
t[i] += this->S(i,v) * this->V[v] * this->Sigma[i];
}
}
// Build right hand side: rhs = -S(...) SIGMA S_V V
MYARRAY<double> rhs( nw );
rhs.fill(0.0);
for (int w = 0; w < nw; ++w)
{
for (int i = 0; i < ni; ++i)
{
if (S(i,nv + w) == 0) continue;
rhs[ w ] -= S(i,nv + w) * t[i];
}
}
int ONE = 1;
MYARRAY<int> ipiv( nw );
int info;
MYARRAY<double> lhs_saved = lhs;
MYARRAY<double> wrk( 3*nw );
MYARRAY<int> iwk( nw );
MYARRAY<double> dr( nw );
MYARRAY<double> dc( nw );
char equed;
int afb_rows = 1 + kl + ku + kl;
MYARRAY<double> afb( afb_rows*nw );
dgbsvx("Equilibrate","No transpose",&nw,&kl,&ku,&ONE,
DATAOF(lhs_saved),&lhs_rows,
DATAOF(afb), &afb_rows,
DATAOF(ipiv),
&equed, DATAOF(dr), DATAOF(dc),
DATAOF(rhs), &nw,
DATAOF(this->W), &nw,
&this->rcond,
&ferr, &berr, DATAOF(wrk), DATAOF(iwk), &info);
// Given V and W, compute I
for (int i = 0; i < ni; ++i)
{
MYPAIR<int,int> ends = this->ends(i);
if (ends.first < nv)
this->I[i] = - this->Sigma[i] * this->V[ends.first];
else
this->I[i] = - this->Sigma[i] * this->W[ends.first - nv];
if (ends.second < nv)
this->I[i] += this->Sigma[i] * this->V[ends.second];
else
this->I[i] += this->Sigma[i] * this->W[ends.second - nv];
}
//-------voltage difference
double V_1,V_2,V12;
this->difV.fill(0.0);
for (int i = 0; i < ni; ++i)
{
MYPAIR<int,int> ends_i = this->ends(i);
if(ends_i.first < nv)
V_1=this->V[ends_i.first];
else
V_1=this->W[ends_i.first-nv];
if(ends_i.second < nv)
V_2=this->V[ends_i.second];
else
V_2=this->W[ends_i.second-nv];
V12=(V_1-V_2);
this->difV[i]=(V_1-V_2);
}
//----------------------------------
double imax = -1e300;
double q;
for (int i = 0; i < ni; ++i)
{
q = this->I[i];
if (fabs(q) > imax)
{
imax = fabs(q);
}
}
double deltai_max = 0;
double I1=0,I2=0;
for (int i=0; i < ni; ++i)
{
MYPAIR<int,int> ends = this->ends(i);
int from = ends.first;
int to = ends.second;
MYPAIR<double,double> xy0 = this->xy(from);
MYPAIR<double,double> xy1 = this->xy(to);
if(xy0.first==0 && xy1.first==0&&
(xy0.second==0&&xy1.second==1||xy0.second==1&&xy1.second==0)) I1=this->I[i];
if(xy0.second==0 && xy1.second==0&&
(xy0.first==0&&xy1.first==1||xy0.first==1&&xy1.first==0)) I2=this->I[i];
}
conductivity = fabs(I1+I2)/2;
//-------Joule heat
double IdVmax = -1e300;
double ImaxV = conductivity*4.;
{
this->IdifV.fill(0.0);
double q;
for (int i = 0; i < ni; ++i)
{
q=fabs(this->I[i]*this->difV[i]);
// this->IdifV[i]=q;
this->IdifV[i]=q/ImaxV;
if (q > IdVmax&&this->Sigma[i]!=100)
{
IdVmax = q;
}
}
}
//----------------------------------
//--------------Current Fraction----------------
{
int nt=0;
int nS=0;
for (int i = 0; i < ni; ++i)
{
double q = fabs(this->I[i]);
if(this->Sigma[i]!=100){
nS++;
if(q > imax * 1e-3) nt++;
}
}
// capacity = double(nt)/double(nS);
}
//--------------Joule heat Fraction----------------
{
int nt=0;
for (int i = 0; i < ni; ++i)
{
if(this->Sigma[i]!=100){
double q = fabs(this->IdifV[i]);
// if(q > IdVmax * 0.1) nt++;
if(q > IdVmax/ImaxV * 0.5) nt++;
}
}
capacity = double(nt);///double(ni);
}
//--------------------------
for (int w = 0; w < nw; ++w)
{
MYARRAY<int> from = this->from(nv+w);
MYARRAY<int> to = this->to(nv+w);
double total_i = 0;
for (int i = 0; i < from.size(); ++i)
{
total_i += this->I[ from[i] ];
}
for (int i = 0; i < to.size(); ++i)
{
total_i -= this->I[ to[i] ];
}
if (fabs(total_i) > deltai_max) deltai_max = fabs(total_i);
}
// compute color scale, based on deltai_max
deltaI = deltai_max/imax;
}
/************************************************************************
* PercolRect
************************************************************************/
PercolRect::PercolRect(int _rows, int _cols) : rows(_rows), cols(_cols)
{
V.resize(2);
W.resize(rows*cols - 2);
I.resize((rows-1)*cols + rows*(cols-1));
difV.resize( I.size() );
IdifV.resize(I.size());
Sigma.resize(I.size());
V[0] = 1.0;
V[1] = -1.0;
}
PercolRect::~PercolRect()
{
}
struct RectHelper
{
const int rows, cols;
RectHelper(int _rows,int _cols) : rows(_rows), cols(_cols) {}
// compute number v of the node located at r,c
int v(int r, int c)
{
int i = r + rows * c;
if (r == 0 && c == 0) return 0;
if (r == rows-1 && c == cols-1) return 1;
return i + 1;
}
// compute location r,c of node number v
MYPAIR<int,int> rc(int v)
{
if (v == 0) return qMakePair( 0, 0 );
if (v == 1) return qMakePair( rows-1, cols-1 );
v -= 1;
return qMakePair( v % rows, v / rows );
}
// compute number ibu of the bottom-up edge coming from node at r,c
int ibu(int r, int c)
{
return r*cols + c; //!
}
// compute location r,c of the bottom end of the bottom-up edge i
MYPAIR<int,int> rcbu(int i)
{
return qMakePair( i / cols, i % cols );
}
// compute number ilr of the left-right edge coming from node at r,c
int ilr(int r, int c)
{
return (rows-1)*cols + r + rows*c;
}
// compute location r,c of the left end of the left-right edge i
MYPAIR<int,int> rclr(int i)
{
i -= (rows-1)*cols;
return qMakePair( i % rows, i / rows );
}
};
MYPAIR<int,int> PercolRect::ends(int i) const
{
RectHelper h(rows,cols);
if (i < (rows-1)*cols) /* bottom-up edge */
{
MYPAIR<int,int> rc = h.rcbu(i);
int v_from = h.v( rc.first, rc.second );
int v_to = h.v( rc.first + 1, rc.second );
return qMakePair( v_from, v_to );
}
else /* left-to-right edge */
{
MYPAIR<int,int> rc = h.rclr(i);
int v_from = h.v( rc.first, rc.second );
int v_to = h.v( rc.first, rc.second + 1 );
return qMakePair( v_from, v_to );
}
}
MYARRAY<int> PercolRect::from(int v) const
{
RectHelper h(rows,cols);
MYPAIR<int,int> rc = h.rc(v);
int r = rc.first;
int c = rc.second;
int a, b;
MYARRAY<int> res;
if (r < rows-1 && c < cols-1)
{
res.resize(2);
res[0] = a = h.ibu(r,c);
res[1] = b = h.ilr(r,c);
}
else if (r == rows-1 && c < cols-1)
{
res.resize(1);
res[0] = a = h.ilr(r,c);
}
else if (r < rows-1 && c == cols-1)
{
res.resize(1);
res[0] = a = h.ibu(r,c);
}
else
{
res.resize(0);
}
return res;
}
MYARRAY<int> PercolRect::to(int v) const
{
RectHelper h(rows,cols);
MYPAIR<int,int> rc = h.rc(v);
int r = rc.first;
int c = rc.second;
int a, b;
MYARRAY<int> res;
if (r > 0 && c > 0)
{
res.resize(2);
res[0] = a = h.ibu(r-1,c);
res[1] = b = h.ilr(r,c-1);
}
else if (r == 0 && c > 0)
{
res.resize(1);
res[0] = a = h.ilr(r,c-1);
}
else if (r > 0 && c == 0)
{
res.resize(1);
res[0] = a = h.ibu(r-1,c);
}
else
{
res.resize(0);
}
return res;
}
int PercolRect::S(int i, int v) const
{
MYPAIR<int,int> rc = PercolRect::ends(i);
if (v == rc.first) return -1;
if (v == rc.second) return +1;
return 0;
}
MYPAIR<double,double> PercolRect::xy(int v) const
{
RectHelper h(rows,cols);
MYPAIR<int,int> rc = h.rc(v);
return qMakePair(double(rc.second), double(rc.first));
}
int PercolRect::vnode(double x,double y) const
{
RectHelper h(rows,cols);
int r = int(y > 0 ? y+0.5 : y-0.5); //nearest int
int c = int(x > 0 ? x+0.5 : x-0.5); //nearest int
return h.v(r,c);
}
/*QVector<int> PercolRect::index_for_sorted_W() const
{
QVector<int> res(nW());
for (int i=0; i<res.size(); ++i) res[i] = i;
qSort(res.begin(),res.end(),W_lessthen);
return res;
}
*/
double PercolRect::xmax() const { return cols-1; }
double PercolRect::ymax() const { return rows-1; }
double PercolRect::xmin() const { return 0; }
double PercolRect::ymin() const { return 0; }
// returns the index of I with maximum I^2*R
int Percol2D::index_of_Rcr() const
{
double IV_max = -1e300;
int i_max = 0;
for (int i = 0; i < nI(); ++i)
{
double I_i = I[i];
MYPAIR<int,int> ends_i = ends(i);
double V_i = ends_i.first < nV()
? V[ends_i.first]
: W[ends_i.first - nV()];
V_i -= ends_i.second < nV()
? V[ends_i.second]
: W[ends_i.second - nV()];
double IV_i = fabs(I_i * V_i);
if (IV_i > IV_max)
{
IV_max = IV_i;
i_max = i;
}
}
return i_max;
}
#include <QtAlgorithms>
QVector<int> Percol2D::index_for_sorted_W() const
{
QVector<int> res(nW());
for (int i=0; i<res.size(); ++i)
{
res[i] = i;
}
struct MyLessThan
{
const MYARRAY<double>& w;
MyLessThan(const MYARRAY<double>& w_) : w(w_) {}
bool operator()(int a, int b) const { return w[a] < w[b]; }
};
qSort(res.begin(),res.end(),MyLessThan(W));
return res;
}
QVector<int> Percol2D::index_for_sorted_I() const
{
QVector<int> res(nI());
for (int i=0; i<res.size(); ++i)
{
res[i] = i;
}
struct MyGreaterThan
// struct MyLessThan
{
const MYARRAY<double>& w;
MyGreaterThan(const MYARRAY<double>& w_) : w(w_) {}
// MyLessThan(const MYARRAY<double>& w_) : w(w_) {}
bool operator()(int a, int b) const { return fabs(w[a]) >fabs(w[b]); }
// bool operator()(int a, int b) const { return fabs(w[a]) < fabs(w[b]); }
};
qSort(res.begin(),res.end(),MyGreaterThan(I));
// qSort(res.begin(),res.end(),MyLessThan(I));
return res;
}
QVector<int> Percol2D::index_for_sorted_difV() const
{
QVector<int> res(nI());
for (int i=0; i<res.size(); ++i)
{
res[i] = i;
}
/* struct MyLessThan
{
const MYARRAY<double>& w;
MyLessThan(const MYARRAY<double>& w_) : w(w_) {}
bool operator()(int a, int b) const { return fabs(w[a]) < fabs(w[b]); }
};
*/
struct MyGreaterThan
{
const MYARRAY<double>& w;
MyGreaterThan(const MYARRAY<double>& w_) : w(w_) {}
// bool operator()(int a, int b) const { return w[a] > w[b]; }
bool operator()(int a, int b) const { return fabs(w[a]) > fabs(w[b]); }
};
qSort(res.begin(),res.end(),MyGreaterThan(difV));
// qSort(res.begin(),res.end(),MyLessThan(difV));
return res;
}
QVector<int> Percol2D::index_for_sorted_IdifV() const
{
QVector<int> res(nI());
for (int i=0; i<res.size(); ++i)
{
res[i] = i;
}
/* struct MyLessThan
{
const MYARRAY<double>& w;
MyLessThan(const MYARRAY<double>& w_) : w(w_) {}
bool operator()(int a, int b) const { return fabs(w[a]) < fabs(w[b]); }
};
*/
struct MyGreaterThan
{
const MYARRAY<double>& w;
MyGreaterThan(const MYARRAY<double>& w_) : w(w_) {}
// bool operator()(int a, int b) const { return w[a] > w[b]; }
bool operator()(int a, int b) const { return fabs(w[a]) > fabs(w[b]); }
};
qSort(res.begin(),res.end(),MyGreaterThan(IdifV));
// qSort(res.begin(),res.end(),MyLessThan(difV));
return res;
}
QVector<int> Percol2D::index_for_sorted_Sigma() const
{
QVector<int> res(nI());
for (int i=0; i<res.size(); ++i)
{
res[i] = i;
}
/*
struct MyLessThan
{
const MYARRAY<double>& w;
MyLessThan(const MYARRAY<double>& w_) : w(w_) {}
bool operator()(int a, int b) const { return w[a] < w[b]; }
};
*/
struct MyGreaterThan
{
const MYARRAY<double>& w;
MyGreaterThan(const MYARRAY<double>& w_) : w(w_) {}
bool operator()(int a, int b) const { return w[a] > w[b]; }
};
qSort(res.begin(),res.end(),MyGreaterThan(Sigma));
// qSort(res.begin(),res.end(),MyLessThan(Sigma));
return res;
}