-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrainfall_seq.cpp
More file actions
229 lines (206 loc) · 7.1 KB
/
rainfall_seq.cpp
File metadata and controls
229 lines (206 loc) · 7.1 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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <time.h>
#include <stdio.h>
#include "computeLowestNeighbor.h"
#include "updateTricle.h"
using namespace std;
int main(int argc, char** argv)
{
int parallelThreads, timeSteps, N;
float absorbRate;
string elevationFile;
// read the command arguments
if (argc != 6) {
cout << "Enter arguments format: ";
cout << "./rainfall <P> <M> <A> <N> <elevation_file> ";
exit(1);
}
istringstream ss1(argv[1]);
if (!(ss1 >> parallelThreads)) {
cerr << "Invalid number for parallel threads number: " << argv[1] << "\n";
}
istringstream ss2(argv[2]);
if (!(ss2 >> timeSteps)) {
cerr << "Invalid number for time steps: " << argv[2] << "\n";
} // cout << "read timesteps: " << timeSteps << endl;
istringstream ss3(argv[3]);
if (!(ss3 >> absorbRate)) {
cerr << "Invalid number for absorbtion rate: " << argv[3] << "\n";
} // cout << "read absorbtion rate: " << absorbRate << endl;
istringstream ss4(argv[4]);
if (!(ss4 >> N)) {
cerr << "Invalid number for dimension: " << argv[4] << "\n";
} // cout << "read dimension: " << N << endl;
istringstream ss5(argv[5]);
if (!(ss5 >> elevationFile)) {
cerr << "Invalid number for input file name: " << argv[5] << "\n";
}
// store input file ro elevation matrix
ifstream inFile;
inFile.open(elevationFile + ".txt");
if (!inFile) {
cout << "Unable to open in file\n";
exit(1);
}
int **eleMatPtr = new int *[N];
for (int i = 0 ; i <N; i++) {
eleMatPtr[i] = new int[N];
for (int j = 0; j < N; j++) {
inFile >> eleMatPtr[i][j];
}
}
inFile.close();
/* cout << "Check eleMat: " << endl;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << eleMatPtr[i][j] << " ";
}
cout << endl;
} */
// int **lowsestNeighbor = computeLowestNeighbor(eleMatPtr, N);
int **lowsestNeighbor = new int*[N];
for (int i = 0; i < N; i++) {
lowsestNeighbor[i] = new int[N];
}
// init sucess!
// cout << "start compute lowestMat: " << endl;
computeLowestNeighbor(eleMatPtr, lowsestNeighbor, N);
/* cout << "Check lowsestNeighbor: " << endl;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << lowsestNeighbor[i][j] << " ";
}
cout << endl;
} */
for (int i = 0; i < N; i++) {
delete[] eleMatPtr[i];
}
delete[] eleMatPtr;
// Algorithm for time = timeSteps, During rainfall phase
int i,j;
bool unfinish = true;
double maxVol = 0;
int cntIter = 0;
double **stepVol = new double *[N];
double **tricleMat = new double *[N];
double **absorbed = new double *[N];
for (int i = 0; i < N; i++) {
stepVol[i] = new double[N];
tricleMat[i] = new double[N];
absorbed[i] = new double[N];
}
// for (int t = 0; t < timeSteps; t++) {
// First traversal:
// 1. receive a new raindrop for each point
// 2. if there are raindrops on a point, absorb water into the point
const clock_t begin = clock();
while (unfinish) {
cntIter++;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if (cntIter <= timeSteps) { // When still raining
absorbed[i][j] += absorbRate * 1;
tricleMat[i][j] += (1 - absorbRate) * 1;
} else { // Only Absorb without rain
if (tricleMat[i][j] <= absorbRate * 1) {
absorbed[i][j] += tricleMat[i][j];
tricleMat[i][j] = 0;
} else { // tricleMat[i][j] > absorbRate volumn
absorbed[i][j] += absorbRate * 1;
tricleMat[i][j] -= absorbRate * 1;
}
}
// 3a. calcualte the number of raindrops that will next tricle to the lowest neighbors
if (tricleMat[i][j] > 1) {
stepVol[i][j] = 1;
tricleMat[i][j] -= 1;
} else {
stepVol[i][j] = tricleMat[i][j];
tricleMat[i][j] = 0;
}
}
}
// Second traversal:
// 3b. for each point, use the calculation in 3a
// update the number of raindrops at each lowest neighbor
/*
cout << "checking stepVol mat at " << cntIter << ": " << endl;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
cout << stepVol[i][j] << " ";
}
cout << endl;
}
*/
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
updateTricle(lowsestNeighbor[i][j], stepVol[i][j], tricleMat, i, j);
}
}
if (cntIter > timeSteps) {
maxVol = 0;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
maxVol = max(maxVol, tricleMat[i][j]);
if (maxVol > 0) break;
}
if (maxVol > 0) break;
}
if (maxVol == 0) unfinish = false;
}
/*
cout << "Check tricleMat at time " << cntIter << endl;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
cout << tricleMat[i][j] << " ";
}
cout << endl;
}
std::cout << "Check absorbedMat at time " << cntIter << endl;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
cout << absorbed[i][j] << " ";
}
cout << endl;
}
*/
}
// output
ofstream myfile;
string out_name = to_string(N) + 'x' + "_myout.txt";
myfile.open (out_name);
clock_t end_clock = clock();
cout << "Rainfall simulation took " << cntIter << " time steps to complete." << endl;
cout << "Runtime = " << double(end_clock - begin) / CLOCKS_PER_SEC << " ms"<< endl;
myfile << "Rainfall simulation took " << cntIter << " time steps to complete." << endl;
myfile << "Runtime = " << double(end_clock - begin) / CLOCKS_PER_SEC << " ms"<< endl;
cout << "\n" << "The following grid shows the number of raindrops absorbed at each point: " << endl;
myfile << "\n" << "The following grid shows the number of raindrops absorbed at each point: " << endl;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
cout << absorbed[i][j] << " ";
myfile << absorbed[i][j] << " ";
}
cout << endl;
myfile << endl;
}
myfile.close();
// Free memory
for (int i = 0; i < N; i++) {
delete[] absorbed[i];
delete[] stepVol[i];
delete[] tricleMat[i];
// delete[] eleMatPtr[i];
delete[] lowsestNeighbor[i];
}
delete[] absorbed;
delete[] stepVol;
delete[] tricleMat;
// delete[] eleMatPtr;
delete[] lowsestNeighbor;
return 0;
}