forked from teknasd/RFIM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFIMwindows.cpp
776 lines (664 loc) · 17.1 KB
/
RFIMwindows.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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <random>
#include <crtdbg.h>
#include <Windows.h>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#define WIDTH 7
#define DIV 1024
#define iter 1
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
#define INFINITE 100000
#define HIGH INFINITE
/* DEFINE ALL THE PARAMETERS HERE */
#define VER 20 // width of lattice matrix. for ex 6*6 lattice
#define latt_pc 1 // percentage of lattice points where atom exist
#define upspin_pc 0.5 // percentage of upspin in lattice
#define w 1 // omega(w) for bimmodal distribution of Bi in eq 6.1
#define N VER*VER // width of edge matrix. for ex 36*36 edges
#define V N+2 // source + sink
#define tab "\t"
#define d " : "
//delta is multipling factor with Bmat
#define del_beg 0
#define del_end 41
#define del_inc 2
int del = 0;
using namespace cv;
using namespace std;
/* FUNCTION ALERT : TO CREATE BIMODAL DESTRIBUTION FOR LOCAL MAG FIELD*/
int* create_Bmat(int* Bmat)
{
long i = 0, var = 0, count1 = 0, count2 = 0;
uniform_int_distribution<int> distr(0, 1000);
std::random_device rand_dev;
std::mt19937 generator(rand_dev());
cout << "B mtrix ->" << tab;
for (i = 0; i < N; i++)
{
//Bmat[i] = distr(generator);// floor(rand() / 1000);
var = distr(generator,10);
if (var < 5)
{
Bmat[i] = w;
count1++;
}
else
{
Bmat[i] = -w;
count2++;
}
//cout << Bmat[i] << d;
} cout << endl << "counts : " << count1 << d << count2 << endl;
return Bmat;
}
/*
* A DFS based function to find all reachable vertices from s.
*/
int * dfs(int ** F, long s, int *visited)
{
visited[s] = true;
for (long i = 0; i < V; i++)
{
if (F[s][i] && !visited[i])
{
//cout << i<<d;
dfs(F, i, visited);
}
}
return (visited);
}
void push(const int * const * C, int ** F, int *excess, int u, int v) {
//cout << "\nin push\n";
int send = MIN(excess[u], C[u][v] - F[u][v]);
F[u][v] += send;
F[v][u] -= send;
excess[u] -= send;
excess[v] += send;
}
void relabel(const int * const * C, const int * const * F, int *height, int u) {
int v;
int min_height = INFINITE;
for (v = 0; v < V; v++) {
if (C[u][v] - F[u][v] > 0) {
min_height = MIN(min_height, height[v]);
height[u] = min_height + 1;
}
}
};
void discharge(const int * const * C, int ** F, int *excess, int *height, int *seen, int u) {
//cout << "\nflag 2\n";
while (excess[u] > 0) {
if (seen[u] < V) {
int v = seen[u];
if ((C[u][v] - F[u][v] > 0) && (height[u] > height[v])) {
push(C, F, excess, u, v);
}
else
seen[u] += 1;
}
else {
relabel(C, F, height, u);
seen[u] = 0;
}
}
}
void moveToFront(int i, int *A) {
//cout << "\nflag 3\n";
int temp = A[i];
int n;
for (n = i; n > 0; n--) {
A[n] = A[n - 1];
}
A[0] = temp;
}
int pushRelabel(const int * const * C, int ** F, int source, int sink) {
int *excess, *height, *list, *seen, i, p;
//cout << "add in fn " << &C[100][70];
excess = new int[V];
height = new int[V];
seen = new int[V];
list = new int[V - 2];
for (i = 0; i < V; i++) {
excess[i] = 0;
height[i] = 0;
seen[i] = 0;
if (i<V - 2)
list[i] = 0;
}
for (i = 0, p = 0; i < V; i++) {
if ((i != source) && (i != sink)) {
list[p] = i;
p++;
}
}
//cout << "\nflag 1\n";
height[source] = V;
excess[source] = INFINITE;
for (i = 0; i < V; i++)
push(C, F, excess, source, i);
p = 0;
while (p < V - 2) {
int u = list[p];
int old_height = height[u]; //cout << "\nflag 2\n";
discharge(C, F, excess, height, seen, u);
if (height[u] > old_height) {
moveToFront(p, list);
p = 0;
}
else
p += 1;
}
int maxflow = 0;
for (i = 0; i < V; i++)
maxflow += F[source][i];
delete(list);
delete(seen);
delete(height);
delete(excess);
//cout << "\nflag 2\n";
return maxflow;
}
void printMatrix(const int * const * M, int len) {
int i, j;
for (i = 0; i < len; i++) {
for (j = 0; j < len; j++)
cout << "\t" << M[i][j];
cout << "\n";
}
}
void printMatrix( int const* M, int len) {
int i;
for (i = 0; i < len; i++) {
cout << "\t" << M[i];
}cout << "\n";
}
//function does nothing
int gotoindex(int A[V - 1], int index)
{
if (A[index] < 0)
{
//cout << "\nconflict of" << A[index] << "index changed to" << abs(A[index]) << endl;
gotoindex(A, abs(A[index]));
}
else
{
//cout << "\nindex ret: " << index << endl;
return index;
}
}
struct RGB {
uchar blue;
uchar green;
uchar red;
};
int main(void) {
Mat image(VER, VER, CV_8UC3);
//namedWindow("square image", WINDOW_AUTOSIZE); // Create a window for display.
//imshow("square image", image); // Show our image inside it.
time_t time_begin, time_end, time_1, time_2;
long t1, t2, tdiff[iter] = { 0 }, l = 0, sum = 0, i, j, cap = 0;
for (l = 0; l < iter; l++)
{
cout << "\n\n*****ITER: " << l << "****\n\n";
int **flow, *visited, **CapacityMat, **Exmat, *Wmat, *latt, *Bmat, **sqlat0, **sqlat1, *clusstats0, *clusstats1;
int J = 1;
// dynamic memory allocated
latt = new int[N];
Bmat = new int[N];
Exmat = new int *[N];
flow = new int *[V];
sqlat0 = new int *[VER + 1];
sqlat1 = new int *[VER + 1];
CapacityMat = new int *[V];
Wmat = new int[N];
visited = new int[V];
clusstats0 = new int[V / 2];
clusstats1 = new int[V / 2];
//Bmat and latt initiated
for (i = 0; i < N; i++)
{
Bmat[i] = 0;// B: local magnetic fields//fig: 6.5
latt[i] = 1;
}
//cout << "printing latte" << endl;
//printMatrix(latt, N);
cout << "latte created" << endl;
cout << "Bmat creating" << endl;
Bmat = create_Bmat(Bmat);
cout << "Bmat created" << endl;
//cout << "printing Bmat" << endl;
//printMatrix(Bmat, N);
//flow,visited,CapacityMat,Exmat,Wmat,sqlat0,sqlat1,clusstats0,clusstats1 2d array allocation
for (i = 0; i < VER + 1; i++)
{
sqlat0[i] = new int[VER];
sqlat1[i] = new int[VER];
}
for (i = 0; i < V; i++) {
flow[i] = new int[V];
CapacityMat[i] = new int[V];
}
for (i = 0; i < N; i++) {
Exmat[i] = new int[N];
}
//init
cout << "matrixex initiated sloly" << endl;
for (i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
{
if (i < VER + 1 && j < VER + 1)
{
sqlat0[i][j] = 0;
sqlat1[i][j] = 0;
}
if (i < N && j < N)
{
Exmat[i][j] = 0;
}
CapacityMat[i][j] = 0;
flow[i][j] = 0;
}
if (i < V / 2)
{
clusstats0[i] = 0;
clusstats1[i] = 0;
}
if (i < N)
{
Wmat[i] = 0;
}visited[i] = 0;
}
// pg:111 // existence matrix for edges // connection from node i(row) to node j(col)
cout << "Exmat in making" << endl;
for (i = 0; i < N; i++)
{
if (((i + 1) % VER != 0))
{
Exmat[i][i + 1] = 1 * latt[i] * latt[i + 1];
//cout << Exmat[i][i + 1];
}
if (i + VER < N)
{
Exmat[i][i + VER] = 1 * latt[i] * latt[i + VER];
}
//else
}
cout << "Exmat created" << endl;
/*cout << "Exmat printing" << endl;
printMatrix(Exmat, N);*/
/*----------------------------------- eq 6.12 pg 110 -------------------------------------*/
cout << "making CapacityMatrix" << endl;
for (i = 1; i <= N; i++)
{
//cout << "\n";
for (j = 1; j <= N; j++)
{
if (i < j)
{
CapacityMat[i][j] = 4 * J * Exmat[i - 1][j - 1] * 10;
}
else
CapacityMat[i][j] = 0;
//cout << CapacityMat[i][j] << " ";
}
}
cout << "created CapacityMatrix" << endl;
for (del = del_beg; del < del_end; del += del_inc)
{
//open
cout << "\n\ndEL: " << del << endl;
cout << "making Wmat" << endl;
/*----------------------------------- eq 6.13 pg 110 ---------------------------------*/
//cout << "w mat agumenting from i=1, j=1" << endl;
for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
{
cap += CapacityMat[i][j] - CapacityMat[j][i];
//cout << cap<<"\t";
}
Wmat[i - 1] = -2 * Bmat[i - 1] * del* latt[i - 1] - cap / 2;
cap = 0;
//cout <<Wmat[i-1]<< endl;
}
cout << "created Wmat" << endl;
cap = 0;
// ------------------------------- print capacity matrix
//printMatrix(CapacityMat, V);
// --------------------------------print Wmatrix
//printMatrix(Wmat, N);
/*----------------------------------- eq 6.14 pg 110 -----------------------------------*/
for (i = 1; i <= N; i++)
{
if (Wmat[i - 1] > 0)
{
CapacityMat[0][i] = 0;
CapacityMat[i][N + 1] = Wmat[i - 1];
}
else
{
CapacityMat[0][i] = -Wmat[i - 1];
CapacityMat[i][N + 1] = 0;
}
}
/*----------------------------------------------------------------------------------------*/
t1 = time(&time_1); /* get current time;*/
//cout << "time now:" << t1;
/*================================================================================================*/
/* ladies and gentlemen its honour to present you the most important stuff in this awesome code */
cout << "\nMax Flow:" << pushRelabel(CapacityMat, flow, 0, V - 1)<<endl;
/*====================================================================================*/
t2 = time(&time_2); /* get current time;*/
//cout << "\ntime now:" << t2 << "\n\n";
//printMatrix(flow,V);
// making reisdual graph by overwriting the flow mat,flow[i][j] = CapacityMat[i][j] - flow[i][j];
for (i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
flow[i][j] = CapacityMat[i][j] - flow[i][j];
//printf("%d\t", capacities[i][j]-flow[i][j]);
}
//printf("\n");
}
//dfsing over res grph to get accessable and non accessable nodes
visited = dfs(flow, 0, visited);
cout << "visited/non visited nodes:";
//its clustering time
//agumented matrix
for (i = 0; i <= VER; i++)
{
sqlat0[i][0] = HIGH;
sqlat1[i][0] = HIGH;
}
for (j = 0; j <= VER; j++)
{
sqlat0[0][j] = HIGH;
sqlat1[0][j] = HIGH;
}
for (i = 1; i <= VER; i++)
{
for (j = 1; j <= VER; j++)
{
sqlat0[i][j] = visited[(i - 1)*VER + (j - 1) + 1];
sqlat1[i][j] = visited[(i - 1)*VER + (j - 1) + 1];
if (sqlat1[i][j] == 0)sqlat1[i][j] = HIGH;
if (sqlat0[i][j] == 1)sqlat0[i][j] = HIGH;
//cout << visited[(i - 1)*VER + (j - 1) + 1];
}
}
//cout << "\nsqlat 0\n";
//printMatrix(sqlat0, VER);
//cout << "\nsqlat 1\n";
//printMatrix(sqlat1, VER);
//clustering
//finding clusters of 1
int m = 0, n = 0, min = 0, a = 0, b = 0;
for (i = 1; i < VER + 1; i++)
{
for (j = 1; j < VER + 1; j++)
{
if (sqlat1[i][j] == 1)
{
a = sqlat1[i - 1][j];
b = sqlat1[i][j - 1];
//cout << "\n\nbw " << a << "(" << i - 1 << "," << j << ") & " << b << " (" << i << "," << j - 1 << " )\n";
min = MIN(a, b);
if (a == HIGH && b == HIGH)
{//rule 1
//cout << "rule:" << 1 << d;
m++;
clusstats1[m]++;
sqlat1[i][j] = m;
//cout << "ret:" << m << d;
}
else if ((a == HIGH && b < HIGH) || (a < HIGH && b == HIGH))
{//rule 2
//cout << "rule:" << 2 << d;
min = MIN(a, b);
n = gotoindex(clusstats1, min);
clusstats1[n]++;
sqlat1[i][j] = n;
}
else if (a < b)
{//rule 3
//cout << "rule:" << 3 << d;
n = gotoindex(clusstats1, a);
if (a != gotoindex(clusstats1, b))
clusstats1[n] += clusstats1[gotoindex(clusstats1, b)] + 1;
else
clusstats1[n] += 1;
clusstats1[b] = -n;
sqlat1[i][j] = n;
}
else if (a == b)
{//rule 4
//cout << "rule:" << 4 << d;
n = gotoindex(clusstats1, a);
clusstats1[n] += 1;
sqlat1[i][j] = n;
}
else
{//rule 5
//cout << "rule:" << 5 << d;
n = gotoindex(clusstats1, b);
if (b != gotoindex(clusstats1, a))
clusstats1[n] += clusstats1[gotoindex(clusstats1, a)] + 1;
else
clusstats1[n] += 1;
clusstats1[a] = -n;
sqlat1[i][j] = n;
}
}
}
}
//finding clusters of 0
m = 0, n = 0, min = 0, a = 0, b = 0;
for (i = 1; i < VER + 1; i++)
{
for (j = 1; j < VER + 1; j++)
{
if (sqlat0[i][j] == 0)
{
a = sqlat0[i - 1][j];
b = sqlat0[i][j - 1];
//cout << "\n\nbw " << a << "(" << i - 1 << "," << j << ") & " << b << " (" << i << "," << j - 1 << " )\n";
min = MIN(a, b);
if (a == HIGH && b == HIGH)
{//rule 1
//cout << "rule:" << 1 << d;
m++;
clusstats0[m]++;
sqlat0[i][j] = m;
//cout << "ret:" << m << d;
}
else if ((a == HIGH && b < HIGH) || (a < HIGH && b == HIGH))
{//rule 2
//cout << "rule:" << 2 << d;
min = MIN(a, b);
n = gotoindex(clusstats0, min);
clusstats0[n]++;
sqlat0[i][j] = n;
}
else if (a < b)
{//rule 3
//cout << "rule:" << 3 << d;
n = gotoindex(clusstats0, a);
if (a != gotoindex(clusstats0, b))
clusstats0[n] += clusstats0[gotoindex(clusstats0, b)] + 1;
else clusstats0[n] += 1;
clusstats0[b] = -n;
sqlat0[i][j] = n;
}
else if (a == b)
{//rule 4
//cout << "rule:" << 4 << d;
n = gotoindex(clusstats0, a);
clusstats0[n] += 1;
sqlat0[i][j] = n;
}
else
{//rule 5
//cout << "rule:" << 5 << d;
n = gotoindex(clusstats0, b);
if (b != gotoindex(clusstats0, a))
clusstats0[n] += clusstats0[gotoindex(clusstats0, a)] + 1;
else clusstats0[n] += 1;
clusstats0[a] = -n;
sqlat0[i][j] = n;
}
}
}
}
//cout << "clus stats for 0\n";
//printMatrix(clusstats0, N);
cout << "clus stats 1\n";
//printMatrix(clusstats1, N);
cout << "\ncluster\n\n";
//uncomment block below to print cluster on console
//for (i = 0; i <= VER; i++)
//{
// for (j = 0; j <= VER; j++)
// {
// if (sqlat0[i][j] == HIGH)
// //cout << "H ";// << sqlat1[i][j] << " ";
// //cout << gotoindex(clusstats1, sqlat1[i][j]) << " ";
// else
// //cout << sqlat0[i][j] << " ";
// cout << "" << gotoindex(clusstats0, sqlat0[i][j]) << " ";
// }//cout << endl;
//}
//cout << "\ncluster\n\n";
//creating cluster image
for (i = 1; i <= VER; i++)
{
for (j = 1; j <= VER; j++)
{
RGB& rgb = image.ptr<RGB>(i - 1)[j - 1];
if (sqlat1[i][j] == HIGH)
{
//cout << "H ";// << sqlat1[i][j] << " ";
rgb.blue = 0; rgb.green = 0; rgb.red = 255;
} //cout << gotoindex(clusstats1, sqlat1[i][j]) << " ";
else
{ //cout << sqlat0[i][j] << " ";
rgb.blue = 255; rgb.green = 0; rgb.red = 0;
//cout << "" << gotoindex(clusstats1, sqlat1[i][j]) << " ";
}
}//cout << endl;
}
//namedWindow("square image", WINDOW_AUTOSIZE); // Create a window for display.
//imshow("square image", image); // Show our image inside it.
string S = "img/cluster-";
S = S + to_string(VER)+"-"+ to_string(iter) + "-" + to_string(del) + ".bmp";
imwrite(S, image);
cout << "cluster image created";
cout << "\n\n\ntime_req : " << t2 - t1;
tdiff[l] = t2 - t1;
sum += tdiff[l];
//re_init
for (i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
{
if (i < VER + 1 && j < VER + 1)
{
sqlat0[i][j] = 0;
sqlat1[i][j] = 0;
}
flow[i][j] = 0;
}
if (i < V / 2)
{
clusstats0[i] = 0;
clusstats1[i] = 0;
}
if (i < N)
{
Wmat[i] = 0;
}visited[i] = 0;
}
//close
}
// MS mem sts
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);
printf(TEXT("\nThere is %*ld percent of memory in use.\n"),
WIDTH, statex.dwMemoryLoad);
printf(TEXT("There are %*I64d total KB of physical memory.\n"),
WIDTH, statex.ullTotalPhys / DIV);
printf(TEXT("There are %*I64d free KB of physical memory.\n"),
WIDTH, statex.ullAvailPhys / DIV);
/*cout << "matrixex reinitiated sloly" << endl;
for (i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
{
if (i < VER + 1 && j < VER + 1)
{
sqlat0[i][j] = 0;
sqlat1[i][j] = 0;
}
flow[i][j] = 0;
}
if (i < V / 2)
{
clusstats0[i] = 0;
clusstats1[i] = 0;
}
if (i < N)
{
Wmat[i] = 0;
}visited[i] = 0;
}*/
for (i = 0; i < V; i++)
{
delete(flow[i]);
delete(CapacityMat[i]);
}
for (i = 0; i < N; i++)
{
delete(Exmat[i]);
}
delete(CapacityMat);
delete(flow);
delete(visited);
delete(Exmat);
delete(Wmat);
delete(sqlat0);
delete(sqlat1);
delete(clusstats0);
delete(clusstats1);
// MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);
printf(TEXT("\nThere is %*ld percent of memory in use.\n"),
WIDTH, statex.dwMemoryLoad);
printf(TEXT("There are %*I64d total KB of physical memory.\n"),
WIDTH, statex.ullTotalPhys / DIV);
printf(TEXT("There are %*I64d free KB of physical memory.\n"),
WIDTH, statex.ullAvailPhys / DIV);
delete(latt);
delete(Bmat);
}
cout << "\n\n\nAVERAGE TIME for " << VER << " ver : " << sum / iter << endl;
// print time req for each iter
for (l = 0; l < iter; l++)
{
cout << "-" << tdiff[l];
}
system("PAUSE");
return 0;
}