'''generates module that records change tendencies'''
# function to automatically generate a module that is used to record the tendency
# of components (components specified by the user, their index given by rec_comp_index) 
# to change in response to box model 
# mechanisms - gets called inside ode_gen on each time step

import datetime

def write_dydt_rec(): # define function

	f = open('PyCHAM/dydt_rec.py', mode='w')
	f.write('\'\'\'module for calculating and recording change tendency of components\'\'\'\n')
	f.write('# changes due to gas-phase photochemistry and partitioning are included\n')
	f.write('\n')
	f.write('# File Created at %s\n' %(datetime.datetime.now()))
	f.write('\n')
	f.write('import numpy as np \n')
	f.write('\n')
	# following part is the function (there should be an indent at the start of each line)
	# suggest 1 Tab
	f.write('def dydt_rec(y, rindx, rstoi, reac_coef, pindx, pstoi, nprod, step, dydt_vst, nreac, num_sb, num_speci, pconc, core_diss, Psat, kelv_fac, kimt, kwgt, Cw, act_coeff, seedi):\n')
	f.write('	\n')
	f.write('	# loop through components to record the tendency of change \n')
	f.write('	for compi in dydt_vst.get(\'comp_index\'): \n')
	f.write('		# open relevant dictionary value \n')
	f.write('		dydt_rec = dydt_vst.get(compi) \n')
	f.write('		# keep count on relevant reactions \n')
	f.write('		reac_count = 0 \n')
	f.write('		# loop through relevant reactions \n')
	f.write('		for i in dydt_rec[0, 0:-2]: # final two rows for particle- and wall-partitioning \n')
	f.write('			i = int(i) # ensure reaction index is integer - this necessary because the dydt_rec array is float (the tendency to change records beneath its first row are float) \n')
	f.write('			# estimate gas-phase change tendency for each reaction involving this component \n')
	f.write('			gprate = ((y[rindx[i, 0:nreac[i]]]**rstoi[i, 0:nreac[i]]).prod())*reac_coef[i] \n')
	f.write('			# identify whether this component reacted and/or produced\n')
	f.write('			# and get its stoichiometric index\n')
	f.write('			stoi_indx = (np.where(rindx[i, 0:nreac[i]]==compi))[0]\n')
	f.write('			if len(stoi_indx)>0: \n')
	f.write('				dydt_rec[step+1, reac_count] -= rstoi[i, stoi_indx]*((gprate))\n')
	f.write('			stoi_indx = (np.where(pindx[i, 0:nprod[i]]==compi))[0]\n')
	f.write('			if len(stoi_indx)>0: \n')
	f.write('				dydt_rec[step+1, reac_count] += pstoi[i, stoi_indx]*((gprate))\n')
	f.write('			reac_count += 1 \n')
	f.write('		# now estimate and record tendency to change due to particle- and wall-partitioning  \n')
	f.write('		# particle-partitioning \n')
	f.write('		for ibin in range(num_sb-1): # size bin loop\n')
	f.write('			Csit = y[num_speci*(ibin+1):num_speci*(ibin+2)]\n')
	f.write('			conc_sum = np.zeros((1)) \n')
	f.write('			if any(pconc > 0.): # if seed particles present \n')
	f.write('				conc_sum[0] = ((Csit.sum()-Csit[seedi])+Csit[seedi]*core_diss)\n')
	f.write('			else: \n')
	f.write('				conc_sum[0] = Csit.sum() \n')
	f.write('			# prevent numerical error due to division by zero \n')
	f.write('			ish = conc_sum==0.0 \n')
	f.write('			conc_sum[ish] = 1.0e-40 \n')
	f.write('			# particle surface gas-phase concentration (molecules/cc (air)) \n')
	f.write('			Csit = (Csit[compi]/conc_sum)*Psat[0, compi]*kelv_fac[ibin, 0]*act_coeff[0, compi] \n')
	f.write('			# partitioning rate (molecules/cc.s) \n')
	f.write('			dydt_all = kimt[ibin, compi]*(y[compi]-Csit) \n')
	f.write('			# gas-phase change (molecules/cc/s) \n')
	f.write('			dydt_rec[step+1, reac_count] -= dydt_all \n')
	f.write('		# wall-partitioning \n')
	f.write('		if (kwgt)>1.0e-10: \n')
	f.write('			# concentration at wall (molecules/cc (air)) \n')
	f.write('			Csit = y[num_speci*num_sb:num_speci*(num_sb+1)] \n')
	f.write('			Csit = (Psat[0, :]*(Csit/Cw)*act_coeff[0, compi])\n')
	f.write('			dydt_all = (kwgt)*(y[compi]-Csit[compi]) \n')
	f.write('			# gas-phase change (molecules/cc/s) \n')
	f.write('			dydt_rec[step+1, reac_count+1] -= dydt_all \n')
	f.write('		\n')
	f.write('	return(dydt_vst) \n')
	f.close()
