11# Wild Fire Simulation
22
3+ import os
34import sys
5+ import time
46import math
57import random
68import copy
@@ -120,9 +122,18 @@ def colormap(title, array):
120122
121123def 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
246256ims = []
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
277295if visual and rank == 0 :
0 commit comments