forked from teknasd/RFIM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFIM_2D_stl_woClustering.cpp
614 lines (520 loc) · 14.4 KB
/
RFIM_2D_stl_woClustering.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
// RFIM for 2D without OPENCV
// making use of stl vectors instead of new int
// without clustering implementation
// for raw visited node output
//
// author: Teknas
//#define _HAS_ITERATOR_DEBUGGING 0
#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 100 // 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 40
#define del_inc 2
int del = 0;
//using namespace cv;
using namespace std;
void printMatrix(vector <vector <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(vector<int> const & M, int len) {
int i;
for (i = 0; i < len; i++) {
cout << "\t" << M[i];
}cout << "\n";
}
/* FUNCTION ALERT : TO CREATE BIMODAL DESTRIBUTION FOR LOCAL MAG FIELD*/
vector <int> create_Bmat(vector <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.
*/
void dfs(vector <vector <int> >& F, long s, vector <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);
}
}
}
void push(vector <vector <int> >const& C1, vector <vector <int> >& F1, vector <int>& excess1, int u, int v) {
//cout << "\nin push\n";
int send = MIN(excess1[u], C1[u][v] - F1[u][v]);
F1[u][v] += send;
F1[v][u] -= send;
excess1[u] -= send;
excess1[v] += send;
}
void relabel(vector <vector <int> >const & C, vector <vector <int> >& F, vector <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) {
void discharge(vector <vector <int> >const & C, vector <vector <int> >& F, vector <int> & excess, vector <int> & height, vector <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, vector <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) {
void pushRelabel(vector <vector <int> >const& C, vector <vector <int> >& F, int source, int sink) {
//int *excess, *height, *list, *seen, i, p;
vector <int> excess(V, 0), height(V, 0), list(V - 2, 0), seen(V, 0);
int 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;
/*cout << "excess" << endl;
printMatrix(excess, V);*/
for (i = 0; i < V; i++)
push(C, F, excess, source, i);
/*cout << "excess" << endl;
printMatrix(excess, V);*/
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];
cout << "maxflow: " << maxflow << endl;
list.erase(list.begin(), list.end());
seen.erase(seen.begin(), seen.end());
height.erase(height.begin(), height.end());
excess.erase(excess.begin(), excess.end());
//cout << "\nflag 2\n";
}
//function does nothing
int gotoindex(vector <int>& A, 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;
// vector form
// 2D vectors
vector < vector <int> > flow(V, vector<int>(V, 0))
, CapacityMat(V, vector<int>(V, 0))
, Exmat(N, vector<int>(N, 0))
, sqlat0(VER + 1, vector<int>(VER + 1, 0))
, sqlat1(VER + 1, vector<int>(VER + 1, 0));
// 1D vectors
vector <int> visited(V, 0), Wmat(N, 0), latt(N, 1), Bmat(N, 0), clusstats0(V / 2, 0), clusstats1(V / 2, 0);
int J = 1;
// dynamic memory allocated
// 2nd edit
/*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;
// Bmat.push_back(0);
// latt.push_back(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
// not req for vect
//for (i = 0; i < VER + 1; i++)
//{
// sqlat0[i].reserve(VER);// [i] = new int[VER];
// sqlat1[i].reserve(VER);// [i] = new int[VER];
//}
//for (i = 0; i < V; i++) {
// flow[i].reserve(V);
// CapacityMat[i].reserve(V);
//}
//for (i = 0; i < N; i++) {
// Exmat[i].reserve(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;
//printMatrix(CapacityMat, V);
/*================================================================================================*/
/* ladies and gentlemen its honour to present you the most important stuff in this awesome code */
pushRelabel(CapacityMat, flow, 0, V - 1);
//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);
cout << endl;
// 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];
//cout<< CapacityMat[i][j]-flow[i][j];
}
//cout<<endl;
}
//printMatrix(flow, V);
//dfsing over res grph to get accessable and non accessable nodes
dfs(flow, 0, visited);
cout << "visited/non visited nodes:";
printMatrix(visited, V);
//its clustering time
//deleting everyting from here onwards
cout << "lets assume cluster image is created";
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;
}*/
flow.erase(flow.begin(), flow.end());
CapacityMat.erase(CapacityMat.begin(), CapacityMat.end());
Exmat.erase(Exmat.begin(), Exmat.end());
visited.erase(visited.begin(), visited.end());
Wmat.erase(Wmat.begin(), Wmat.end());
sqlat0.erase(sqlat0.begin(), sqlat0.end());
sqlat1.erase(sqlat1.begin(), sqlat1.end());
clusstats0.erase(clusstats0.begin(), clusstats0.end());
clusstats1.erase(clusstats1.begin(), clusstats1.end());
/*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;
}