Repository navigation
Expand file tree
/
Copy pathGrid.java
More file actions
196 lines (166 loc) · 6.08 KB
/
Copy pathGrid.java
File metadata and controls
196 lines (166 loc) · 6.08 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
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
public class Grid {
private WeatherCell[][] cells;
private int rows;
private int cols;
public Grid(int rows, int cols) {
this.rows = rows;
this.cols = cols;
cells = new WeatherCell[rows][cols];
}
public void initializeCells() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cells[i][j] = new WeatherCell();
}
}
}
// Method to get a specific cell
public WeatherCell getCell(int row, int col) {
if (isValidPosition(row, col)) {
return cells[row][col];
}
return null;
}
// Method to update a specific cell
public void updateCell(int row, int col, WeatherCell newState) {
if (isValidPosition(row, col)) {
cells[row][col] = newState;
}
}
// Getter methods
public int getRows() {
return rows;
}
public int getCols() {
return cols;
}
public void setCells(WeatherCell[][] newCells) {
cells = newCells;
}
// Serial update method
public void update() {
WeatherCell[][] newCells = new WeatherCell[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
WeatherCell currentCell = getCell(i, j);
WeatherCell[] neighbors = getNeighbors(i, j);
newCells[i][j] = currentCell.calculateNewState(neighbors);
}
}
cells = newCells;
}
// Parallel update method using multiple threads
public void update(int numThreads) throws InterruptedException {
final WeatherCell[][] newCells = new WeatherCell[rows][cols];
final WeatherCell[][] oldCells = this.cells; // Reference to the original grid
Thread[] threads = new Thread[numThreads];
int rowsPerThread = rows / numThreads;
int remainder = rows % numThreads;
int startRow = 0;
for (int i = 0; i < numThreads; i++) {
int threadRows = rowsPerThread + (i < remainder ? 1 : 0);
int endRow = startRow + threadRows;
final int threadStartRow = startRow;
final int threadEndRow = endRow;
threads[i] = new Thread(() -> {
for (int row = threadStartRow; row < threadEndRow; row++) {
for (int col = 0; col < cols; col++) {
WeatherCell currentCell = oldCells[row][col];
WeatherCell[] neighbors = getNeighbors(row, col, oldCells);
newCells[row][col] = currentCell.calculateNewState(neighbors);
}
}
});
threads[i].start();
startRow = endRow;
}
// Wait for all threads to finish
for (Thread thread : threads) {
thread.join();
}
// Update the grid cells
this.cells = newCells;
}
// Neighbor retrieval for serial update
public WeatherCell[] getNeighbors(int row, int col) {
WeatherCell[] neighbors = new WeatherCell[4]; // Up, Down, Left, Right
if (isValidPosition(row - 1, col)) {
neighbors[0] = cells[row - 1][col]; // Up
}
if (isValidPosition(row + 1, col)) {
neighbors[1] = cells[row + 1][col]; // Down
}
if (isValidPosition(row, col - 1)) {
neighbors[2] = cells[row][col - 1]; // Left
}
if (isValidPosition(row, col + 1)) {
neighbors[3] = cells[row][col + 1]; // Right
}
return neighbors;
}
// Neighbor retrieval for parallel update
public WeatherCell[] getNeighbors(int row, int col, WeatherCell[][] cellsArray) {
WeatherCell[] neighbors = new WeatherCell[4]; // Up, Down, Left, Right
if (isValidPosition(row - 1, col)) {
neighbors[0] = cellsArray[row - 1][col]; // Up
}
if (isValidPosition(row + 1, col)) {
neighbors[1] = cellsArray[row + 1][col]; // Down
}
if (isValidPosition(row, col - 1)) {
neighbors[2] = cellsArray[row][col - 1]; // Left
}
if (isValidPosition(row, col + 1)) {
neighbors[3] = cellsArray[row][col + 1]; // Right
}
return neighbors;
}
public boolean isValidPosition(int row, int col) {
return (row >= 0 && row < rows && col >= 0 && col < cols);
}
// Display the grid (optional)
public void displayGrid() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print((cells[i][j].temperature - 273.15) + " "); // Convert Kelvin to Celsius
}
System.out.println();
}
}
// Save the grid state to a CSV file
public void saveGridToCSV(String filename, int step) {
try (FileWriter fileWriter = new FileWriter(filename, true)) {
fileWriter.write("Step " + step + "\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
WeatherCell cell = cells[i][j];
fileWriter.write(cell.temperature + "," + cell.pressure + "," + cell.humidity);
if (j < cols - 1) {
fileWriter.write(",");
}
}
fileWriter.write("\n");
}
} catch (IOException e) {
System.err.println("Unable to open file " + filename);
}
}
// Methods for boundary exchange (not directly applicable in Java multithreading)
public WeatherCell[] getRow(int rowIndex) {
if (rowIndex >= 0 && rowIndex < rows) {
return cells[rowIndex];
} else {
// Return an empty array or handle ghost rows if necessary
return new WeatherCell[cols];
}
}
public void setRow(int rowIndex, WeatherCell[] rowData) {
if (rowIndex >= 0 && rowIndex < rows) {
cells[rowIndex] = rowData;
}
// Handle ghost rows if necessary
}
}