-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary.cpp
174 lines (152 loc) · 4.01 KB
/
binary.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
/**
* IT 515R - Scientific Computing - Phase 4 Binary Test Input
* @author Tanner Satchwell [email protected]
**/
#include <iostream>
#include <cstdint>
#include <cmath>
using std::cout;
using std::cin;
using std::endl;
void initializeGrid(uint32_t r, uint32_t c, float ** grid);
bool isStable(uint32_t r, uint32_t c, float e, float ** grid);
void recalcGrid(uint32_t r, uint32_t c, float ** grid, float ** tmp);
void printGrid(uint32_t r, uint32_t c, float ** grid);
void deleteGrid(uint32_t r, float ** grid);
int main() {
// declare vars
uint32_t itr = 0;
uint32_t max = 0;
float epsilon = 0.1;
uint32_t rows = 1024;
uint32_t cols = 1024;
float ** grid1;
float ** grid2;
// Allocate memory
grid1 = new float * [rows];
grid2 = new float * [rows];
for (uint32_t i = 0; i < rows; ++i) {
grid1[i] = new float [cols];
grid2[i] = new float [cols];
}
// initialize grid
initializeGrid(rows, cols, grid1);
// check for stability, and recalc as needed
while (!isStable(rows, cols, epsilon, grid1) && itr < max) {
recalcGrid(rows, cols, grid1, grid2);
itr ++;
}
// human readable test output
// cout << "Iterator: " << itr << endl;
// cout << "Epsilon: " << epsilon << endl;
// cout << "Rows: " << rows << endl;
// cout << "Columns: " << cols << endl;
// output binary
cout.write(reinterpret_cast<char const *>(&itr), sizeof(uint32_t));
cout.write(reinterpret_cast<char const *>(&epsilon), sizeof(float));
cout.write(reinterpret_cast<char const *>(&rows), sizeof(uint32_t));
cout.write(reinterpret_cast<char const *>(&cols), sizeof(uint32_t));
printGrid(rows, cols, grid1);
// De-allocate memory
deleteGrid(rows, grid1);
deleteGrid(rows, grid2);
return 0;
}
/**
* Initialize grid
* @param r
* The number of rows in the 2D grid
* @param c
* The number of columns in the 2D grid
* @param grid
* Dereferenced 2D grid to be initialized
**/
void initializeGrid(uint32_t r, uint32_t c, float ** grid) {
for (uint32_t i = 0; i < r; i++) {
for (uint32_t j = 0; j < c; j++) {
if (i == 0 || j == 0 || (i == r-1) || (j == c-1)) {
grid[i][j] = 0;
} else {
grid[i][j] = 50;
}
}
}
}
/**
* Check to see if the 2D grid is stable.
* @param r
* The number of rows in the 2D grid
* @param c
* The number of columns in the 2D grid
* @param e
* The epsilon value
* @param grid
* Dereferenced 2D grid to be evaluated if it is stable
* @return
* True if stable, false if unstable
**/
bool isStable(uint32_t r, uint32_t c, float e, float ** grid) {
float tmp_val;
for (uint32_t i = 1; i < r-1; i++) {
for (uint32_t j = 1; j < c-1; j++) {
tmp_val = (grid[i-1][j] + grid[i+1][j] + grid[i][j-1] + grid[i][j+1]);
tmp_val = fabs((tmp_val/4) - grid[i][j]) ;
if (tmp_val > e) {
return false;
}
}
}
return true;
}
/**
* A single iteration through the 2D grid to recalculated possible stable values.
* @param r
* The number of rows in the 2D grid
* @param c
* The number of columns in the 2D grid
* @param grid
* Dereferenced 2D grid to be recalculated
* @param tmp
* Dereferenced 2D grid to temporarily hold the recalculated values
**/
void recalcGrid(uint32_t r, uint32_t c, float ** grid, float ** tmp) {
for (uint32_t i = 1; i < r-1; i++) {
for (uint32_t j = 1; j < c-1; j++) {
tmp[i][j] = ((grid[i-1][j] + grid[i+1][j] + grid[i][j-1] + grid[i][j+1])/4);
}
}
for (uint32_t i = 1; i < r-1; i++) {
for (uint32_t j = 1; j < c-1; j++) {
grid[i][j] = tmp[i][j];
}
}
}
/**
* Print grid
* @param r
* The number of rows in the 2D grid
* @param c
* The number of columns in the 2D grid
* @param grid
* Dereferenced 2D grid
**/
void printGrid(uint32_t r, uint32_t c, float ** grid) {
for (uint32_t i = 0; i < r; i++) {
for (uint32_t j = 0; j < c; j++) {
cout.write(reinterpret_cast<char const *>(&grid[i][j]), sizeof(float));
}
}
}
/**
* De-allocate memory
* @param r
* The number of rows in the 2D grid
* @param grid
* Dereferenced 2D grid
**/
void deleteGrid(uint32_t r, float ** grid) {
for (uint32_t i = 0; i < r; ++i) {
delete [] grid[i];
}
delete [] grid;
}