-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverge.cc
444 lines (412 loc) · 12.6 KB
/
converge.cc
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
/*********************************************************************************
** FILENAME : converge.c
**
** DESCRIPTION : This file defines the functions necessary to implement the convergence of
** the dissipation model
** Revision History:
** DATE NAME REASON
** ------------------------------------------------------------------------------
** Feb 12 2016 Sangeeta Kumari Parallel Computing Project
********************************************************************************/
/*********************************************************************************
** HEADER FILES
********************************************************************************/
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <cstdlib>
#include <string>
#include <ctime>
#include <chrono>
#include <vector>
#include <sstream>
using namespace std;
using namespace std::chrono;
#define MAX_ITERATIONS 1000000
/*********************************************************************************
** GLOBAL VARIABLES
********************************************************************************/
struct box
{
int boxId;
int xLeft, xRight;
int yLeft, yBottom;
int height;
int width;
int topN;
int bottomN;
int leftN;
int rightN;
vector <int> topNeighbors;
vector <int> bottomNeighbors;
vector <int> leftNeighbors;
vector <int> rightNeighbors;
int perimeter;
double temp;
};
/*********************************************************************************
** FUNCTION DECLARATIONS
********************************************************************************/
void parseFile(vector<box>& matrix);
void printMatrix(vector<box>& matrix);
void printBox(box block);
void convergence(vector<box>& matrix, double epsilon, double affect_rate);
double temperatureDiff(int id, vector<box>& matrix);
double tempOverPerimeter(box block, vector<box>& matrix);
int contactDist(int id1, int id2, vector<box>& matrix);
void calPerimeter(vector<box>& matrix);
/*********************************************************************************
** FUNCTION NAME : main()
**
** DESCRIPTION : It calls the appropriate functions for the implementation
**
** RETURNS : 0 if successfully executed else exits with value 0
********************************************************************************/
int main(int argc, char *argv[])
{
system_clock::time_point t1 = system_clock::now();
int noBoxes,noRows,noCols;
string line;
int id;
vector<box> matrix;
double affect_rate, epsilon;
if(argc < 3)
{
cout<<"\nExecute like ./a.out <AFFECT RATE> <EPSILON> < test_file\n";
exit(0);
}
affect_rate = atof(argv[1]);
epsilon = atof(argv[2]);
clock_t begin = clock();
time_t start = time(NULL);
system_clock::time_point start1 = system_clock::now();
parseFile(matrix);
calPerimeter(matrix);
//printMatrix(matrix);
convergence(matrix, epsilon, affect_rate);
clock_t end = clock();
time_t finish = time(NULL);
system_clock::time_point end1 = system_clock::now();
double elapsedSecs = double(end - begin)/CLOCKS_PER_SEC;
double elapsedTime = double(finish - start);
cout<<"\nelapsed convergence loop time (clock): "<<elapsedSecs;
cout<<"\nelapsed convergence loop time (time): "<<elapsedTime;
cout <<"\nelapsed convergence loop time (chrono): "<<std::chrono::duration_cast<std::chrono::milliseconds>(end1 - start1).count();
cout<<endl<<"***********************************************************"<<endl;
return 0;
}
/*********************************************************************************
** FUNCTION NAME : parseFile()
**
** DESCRIPTION : parses the input file and saves the value in the struct
**
** RETURNS : NIL
********************************************************************************/
void parseFile(vector<box>& matrix)
{
int noBoxes, noRows, noCols;
int id;
string line = "";
if (true)
{
cin>>noBoxes;
cin>>noRows;
cin>>noCols;
for (int i=0; i < noBoxes; i++)
{
matrix.push_back(box());
cin>>matrix[i].boxId;
cin>>matrix[i].xLeft;
cin>>matrix[i].yLeft;
cin>>matrix[i].height;
cin>>matrix[i].width;
matrix[i].xRight = matrix[i].xLeft + matrix[i].width;
matrix[i].yBottom = matrix[i].yLeft + matrix[i].height;
cin>>matrix[i].topN;
/* Information of the top neighbors*/
for (int j=0; j<matrix[i].topN; j++)
{
cin>>id;
matrix[i].topNeighbors.push_back(id);
}
/* Information of the bottom neighbors*/
cin>>matrix[i].bottomN;
for (int j=0; j<matrix[i].bottomN; j++)
{
cin>>id;
matrix[i].bottomNeighbors.push_back(id);
}
/* Information of the left neighbors*/
cin>>matrix[i].leftN;
for (int j=0; j<matrix[i].leftN; j++)
{
cin>>id;
matrix[i].leftNeighbors.push_back(id);
}
/* Information of the right neighbors*/
cin>>matrix[i].rightN;
for (int j=0; j<matrix[i].rightN; j++)
{
cin >> id;
matrix[i].rightNeighbors.push_back(id);
}
cin>>matrix[i].temp;
}
}
}
/*********************************************************************************
** FUNCTION NAME : printMatrix()
**
** DESCRIPTION : pretty prints the populated boxes (struct members)
**
** RETURNS : NIL
********************************************************************************/
void printMatrix(vector<box>& matrix)
{
for (int i=0; i<matrix.size(); i++)
{
printBox(matrix[i]);
}
}
/*********************************************************************************
** FUNCTION NAME : printBox()
**
** DESCRIPTION : pretty prints each box
**
** RETURNS : NIL
********************************************************************************/
void printBox(box block)
{
cout<<"*************************************************"<<endl;
cout<<"Box Id : "<<block.boxId<<endl;
cout<<"xLeft : "<<block.xLeft<<" yLeft: "<<block.yLeft<<" Height: "<<block.height<<" Width: "<<block.width<<endl;
cout<<"xRight : "<<block.xRight<<" yBottom: "<<block.yBottom<<endl;
cout<<"topN: "<<block.topN<<" <";
for (int i=0; i<block.topN; i++)
{
cout<<block.topNeighbors[i]<<",";
}
cout<<">";
cout<<endl;
cout<<"bottomN: "<<block.bottomN<<" <";
for (int i=0; i<block.bottomN; i++)
{
cout<<block.bottomNeighbors[i]<<",";
}
cout<<">";
cout<<endl;
cout<<"leftN: "<<block.leftN<<" <";
for (int i=0; i<block.leftN; i++)
{
cout<<block.leftNeighbors[i]<<",";
}
cout<<">";
cout<<endl;
cout<<"rightN: "<<block.rightN<<" <";
for (int i=0; i<block.rightN; i++)
{
cout<<block.rightNeighbors[i]<<",";
}
cout<<">";
cout<<endl;
cout<<"Temperature: "<<block.temp<<endl;
cout<<"Perimeter: "<<block.perimeter<<endl;
cout<<"****************************************************"<<endl;
}
/*********************************************************************************
** FUNCTION NAME : convergence()
**
** DESCRIPTION : helps the data to converge using the condition (max-min) < (max*epsilon)
**
** RETURNS : NIL
********************************************************************************/
void convergence(vector<box>& matrix , double epsilon, double affect_rate)
{
vector<double> temporary;
bool stop = false;
int j=0;
double min,max;
for(int i=0; i<matrix.size(); i++)
{
temporary.push_back(0);
}
//cout<<"AFFECT_RATE:"<<affect_rate<<" EPSILON:"<<epsilon<<endl;
while(!stop)
{
for(int i=0; i<matrix.size(); i++)
{
/* calls temperatureDiff to update the current temp of the box taking into account the temp of the neighbors */
double diff = temperatureDiff(i, matrix);
temporary[i] = matrix[i].temp - diff*affect_rate;
}
min = temporary[0];
max = temporary[0];
for(int i=0; i<matrix.size(); i++)
{
matrix[i].temp = temporary[i];
if(temporary[i] < min)
{
min = temporary[i];
}
if(temporary[i] > max)
{
max = temporary[i];
}
}
j++;
/* the iteration stops when the program converges */
if((max-min) < (max*epsilon))
{
stop = true;
}
}
cout<<endl<<"***********************************************************"<<endl;
cout<<"Dissipation converged in: "<<j<<" iterations,"<<endl<<" with max DSV = "<< max<<" and min DSV = "<<min<<endl;
cout<<"\t affect rate = "<<affect_rate<<" epsilon= "<<epsilon<<endl;
}
/*********************************************************************************
** FUNCTION NAME : temperatureDiff()
**
** DESCRIPTION : calculates the temp difference by:
** (temp of current box - weighted average adjacent temperature)
**
** RETURNS : NIL
********************************************************************************/
double temperatureDiff(int id, vector<box>& matrix)
{
double diff;
/* calls tempOverPerimeter for getting the weighted avergae temp of a box */
double tempN = tempOverPerimeter(matrix[id],matrix);
diff = matrix[id].temp - (tempN/matrix[id].perimeter);
return diff;
}
/*********************************************************************************
** FUNCTION NAME : tempOverPerimeter()
**
** DESCRIPTION : finds the weighted average of a box
**
** RETURNS : temperature
********************************************************************************/
double tempOverPerimeter(box block, vector<box>& matrix)
{
double temperature = 0.0;
int id;
/*calculates the total contact distance of all the top neighbors */
for (int i=0; i<block.topN; i++)
{
id = block.topNeighbors[i];
temperature += contactDist(block.boxId,id,matrix) * matrix[id].temp;
}
/*calculates the total contact distance of all the bottom neighbors */
for (int i=0; i<block.bottomN; i++)
{
id = block.bottomNeighbors[i];
temperature += contactDist(block.boxId,id,matrix) * matrix[id].temp;
}
/*calculates the total contact distance of all the left neighbors */
for (int i=0; i<block.leftN; i++)
{
id = block.leftNeighbors[i];
temperature += contactDist(block.boxId,id,matrix) * matrix[id].temp;
}
/*calculates the total contact distance of all the right neighbors */
for (int i=0; i<block.rightN; i++)
{
id = block.rightNeighbors[i];
temperature += contactDist(block.boxId,id,matrix) * matrix[id].temp;
}
return temperature;
}
/*********************************************************************************
** FUNCTION NAME : contactDist()
**
** DESCRIPTION : finds the overall contact distance of a box with its neighbors
**
** RETURNS : dist
********************************************************************************/
int contactDist(int id1, int id2, vector<box>& matrix)
{
int dist = 0;
if((matrix[id1].xLeft <= matrix[id2].xLeft) && (matrix[id2].xLeft < matrix[id1].xRight))
{
if(matrix[id2].xRight < matrix[id1].xRight)
{
dist = matrix[id2].width;
}
else
{
dist = matrix[id1].xLeft + matrix[id1].width - matrix[id2].xLeft;
}
}
else if((matrix[id2].xLeft <= matrix[id1].xLeft ) && (matrix[id1].xLeft < matrix[id2].xRight))
{
if(matrix[id1].xRight < matrix[id2].xRight)
{
dist = matrix[id1].width;
}
else
{
dist = matrix[id2].xRight - matrix[id1].xLeft;
}
}
else if((matrix[id1].yLeft <= matrix[id2].yLeft) && (matrix[id2].yLeft < matrix[id1].yBottom))
{
if(matrix[id2].yBottom < matrix[id1].yBottom)
{
dist = matrix[id2].height;
}
else
{
dist = matrix[id1].yBottom - matrix[id2].yLeft;
}
}
else if((matrix[id2].yLeft <= matrix[id1].yLeft) && (matrix[id1].yLeft < matrix[id2].yBottom))
{
if(matrix[id1].yBottom < matrix[id2].yBottom)
{
dist = matrix[id1].height;
}
else
{
dist = matrix[id2].yBottom - matrix[id1].yLeft;
}
}
return dist;
}
/*********************************************************************************
** FUNCTION NAME : calPerimeter()
**
** DESCRIPTION : calculates the perimeter of the box by using the aggregate
** contact distance with the neighbors
**
** RETURNS : NIL
********************************************************************************/
void calPerimeter(vector<box>& matrix)
{
int id;
for(int i=0; i<matrix.size(); i++)
{
int perimeter = 0;
for (int j=0; j<matrix[i].topN; j++)
{
id = matrix[i].topNeighbors[j];
perimeter += contactDist(matrix[i].boxId, id, matrix);
}
for (int j=0; j<matrix[i].bottomN; j++)
{
id = matrix[i].bottomNeighbors[j];
perimeter += contactDist(matrix[i].boxId, id, matrix);
}
for (int j=0; j<matrix[i].leftN; j++)
{
id = matrix[i].leftNeighbors[j];
perimeter += contactDist(matrix[i].boxId, id, matrix);
}
for (int j=0; j<matrix[i].rightN; j++)
{
id = matrix[i].rightNeighbors[j];
perimeter += contactDist(matrix[i].boxId, id, matrix);
}
matrix[i].perimeter = perimeter;
}
}