#This runfile is for running the simulation CO_O_RuO2_111 on titan.
#It will test the corners of the parameter grid in a serial fashion and print the 
#TOFs to a .csv file.

from kmos.run import KMC_Model
from kmc_settings import *
import timeit
model = KMC_Model()

atoms = model.get_atoms(geometry=False)

TOF_header_string = model.get_tof_header()
TOF_header_array = TOF_header_string.split()

N_steps = 10000000000
sps = 1000000000
snapshots = N_steps/sps

#The desired parameter grid is as follows.  We will only test the corners and a
#Single central point of the grid.
#T_gridlims = [400, 2500]
#CO_gridlims = [1.e-6, 1.e1]
#O2_gridlims = [1.e-6, 1.e1]
#We will call the central point:
#T = 1500
#CO = 1.e-4
#O2 = 1.e-4

#This is the new parameter grid that we will test:
T_grid = [400, 2500]
CO_grid = [1.e-6, 1.e1]
O2_grid = [1.e-6, 1.e1]

#Loop across T_grid
for i in range(len(T_grid)):
#Loop across CO_grid
	for j in range(len(CO_grid)):
#Loop across O2 grid
		for k in range(len(O2_grid)):
#Set the parameters for the current location in the parameter grid
			model.parameters.T = T_grid[i]
			model.parameters.p_COgas = CO_grid[j]
			model.parameters.p_O2gas = O2_grid[k]
#File name will be TOF_data_TEMP_COPRESSURE_O2PRESSURE.csv
			file_name = 'TOF_data_%s_%s_%s.csv' %(T_grid[i], CO_grid[j], O2_grid[k])
			TOF_data = open(file_name,'w')
			TOF_data.write('file_name,')
			TOF_data.write('Step,')
			TOF_data.write('Time (s),')
			TOF_data.write('Temp (K),')
			TOF_data.write('CO Pressure,')
			TOF_data.write('O2 Pressure,')
#This will be the column names for the TOF data from the TOF_header array
			for z in range(len(TOF_header_array)):
				if z == len(TOF_header_array) - 1:
					TOF_data.write('%s\n' %(TOF_header_array[z]))
				else:
					TOF_data.write('%s,' %(TOF_header_array[z]))
			for step in range(0,snapshots):
				if step == 0:
					start = timeit.default_timer()
				TOF_data.write('%s,' %(file_name))
				TOF_data.write('%s,' %(step))
				TOF_data.write('%s,' %(atoms.kmc_time))
				TOF_data.write('%s,' %(T_grid[i]))
				TOF_data.write('%s,' %(CO_grid[j]))				
				TOF_data.write('%s,' %(O2_grid[k]))
				for z in range(len(TOF_header_array)):
					if z == len(TOF_header_array) - 1:
						TOF_data.write('%s\n' %(atoms.tof_data[z]))
					else:
						TOF_data.write('%s,' %(atoms.tof_data[z]))
#If we have reached the desired number of steps, move on to the next parameter.  Deallocate, reacllocate, continue
				if step * sps == N_steps - sps:
					config = 'configuration_%s_%s_%s' %(T_grid[i], CO_grid[j], O2_grid[k])
					model.dump_config(config)
					stop = timeit.default_timer()		
					run_time = stop - start
					simulation_log_name = 'simulation_log_%s_%s_%s.txt' %(T_grid[i], CO_grid[j], O2_grid[k])
					simulation_log = open(simulation_log_name, 'w')
					simulation_log.write('simulation run time = %s' %(run_time))
					model.deallocate()
					from kmos.run import KMC_Model
					from kmc_settings import *
					model = KMC_Model()
#Otherwise, do more steps
				else:
					model.do_steps(sps)
					atoms = model.get_atoms(geometry=False)


















			
					
