Skip to content

Commit 3fb9d9f

Browse files
authored
Improved visualization in pure text terminal
1 parent 7fd0cf9 commit 3fb9d9f

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

Parallel_Wild_Fire.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Wild Fire Simulation
22

3+
import os
34
import sys
5+
import time
46
import math
57
import random
68
import copy
@@ -120,9 +122,18 @@ def colormap(title, array):
120122

121123
def print_forest(title,forest):
122124
print (title)
123-
for i in range(n_row):
125+
for i in range(len(forest)):
124126
for j in range(n_col):
125-
sys.stdout.write(str(forest[i][j]))
127+
if not isinstance(title, int):
128+
sys.stdout.write(str(forest[i][j]))
129+
elif forest[i][j] == 1:
130+
sys.stdout.write("\033[1;34;40m 1\033[0m") # blue for not burnable
131+
elif forest[i][j] == 2:
132+
sys.stdout.write("\033[1;32;40m 2\033[0m") # green for burnable
133+
elif forest[i][j] == 3:
134+
sys.stdout.write("\033[1;31;40m 3\033[0m") # red for burning
135+
else:
136+
sys.stdout.write("\033[1;30;40m 4\033[0m") # black for burned down
126137
sys.stdout.write("\n")
127138
print ("--------------------")
128139

@@ -232,19 +243,19 @@ def update_forest(old_forest):
232243
colormap("Density Map", density_matrix)
233244
colormap("Altitude Map", altitude_matrix)
234245
else:
235-
print("Vegetation Map")
246+
236247
print_forest("Vegetation Map", vegetation_matrix)
237-
print("Density Map")
238248
print_forest("Density Map", density_matrix)
239-
print("Altitude Map")
240-
print_forest("Density Map", altitude_matrix)
249+
print_forest("Altitude Map", altitude_matrix)
241250
print("This is the wind matrix:")
242251
for row in wind_matrix:
243252
print(row)
253+
time.sleep(1)
244254

245255

246256
ims = []
247-
for i in tqdm(range(generation)):
257+
# for i in tqdm(range(generation)):
258+
for i in range(generation): # tqdm will damage the visualization in Terminal
248259
sub_forest = copy.deepcopy(update_forest(sub_forest))
249260
# [parallel] message passing function
250261
if rank == 0:
@@ -260,6 +271,7 @@ def update_forest(old_forest):
260271
temp_grid = comm.gather(np_temp_grid, root=0)
261272

262273
# [parallel] only worker 0 do the visualize
274+
263275
if rank == 0:
264276
new_forest = np.vstack(temp_grid)
265277

@@ -269,9 +281,15 @@ def update_forest(old_forest):
269281
ims.append([im])
270282
# colormap(i,new_forest)
271283
else:
272-
# print_forest(i, new_forest)
284+
# os.system("clear")
285+
time.sleep(1)
273286
print("-----------Generation:", i, "---------------")
274-
print(new_forest)
287+
list_forest = new_forest.tolist()
288+
# print (list_forest)
289+
print_forest(i,list_forest)
290+
# for components_of_forest in list_forest:
291+
# print_forest(i, components_of_forest)
292+
# print(new_forest)
275293

276294

277295
if visual and rank == 0:

0 commit comments

Comments
 (0)