def __init__(self, input_dir, working_dir, chemkin): ''' dir: working directory with chem.inp part files ''' input_dir = fix_dir(input_dir) with open(input_dir + "chem_part_1.inp", "r") as f: self.chem_part_1 = f.read() with open(input_dir + "chem_part_3.inp", "r") as f: self.chem_part_3 = f.read() self.working_dir = fix_dir(working_dir) self.chemkin = chemkin
def init_conditions(working_dir='./', T=1300, P=4, MF={ 'POSF10325': .004, 'AR': .996 }, mode='UV', TIME=2e-3, DELT=2e-5): ''' write initial conditions to file ''' working_dir = fix_dir(working_dir) inputs = '' if mode == 'UV': # constant UV inputs += 'CONV\n' elif mode == 'HP': # constant HP inputs += 'CONP\n' inputs += 'RTOL 1.0E-6\n' inputs += 'ATOL 1.0E-12\n' inputs += 'PRES ' + str(P) + '\n' # pressure inputs += 'TEMP ' + str(T) + '\n' # temperature inputs += 'TIME ' + str(TIME) + '\n' # total time of simulation inputs += 'DELT ' + str(DELT) + '\n' # time step # initial mole fractions for s in MF: inputs += 'REAC ' + s.upper() + ' ' + str(MF[s]) + '\n' inputs += 'END\n' with open(working_dir + 'senkin.inp', 'w') as f: f.write(inputs)
def extract_from_outputs(working_dir='./'): ''' extract mole fraction time history from file ''' working_dir = fix_dir(working_dir) with open(working_dir + 'senkin.ign', 'r') as f: lines = f.read() lines = lines.split('Time Integration:')[-1] d = pd.read_csv(StringIO(lines), sep='\s+') # d['t'] = d['t']/1e3 # time unit return d
def idt_cost(dir, x, idt, cond): dir = fix_dir(dir) write_cheminp(dir, x) sim = chemkin_wrapper(dir, **cond, TIME=3 * idt, DELT=max(1E-6, idt / 1e3 / 100), mode="UV") t = sim['t'] OH = sim['OH'] idt_sim = t[OH.idxmax()] # cost = np.abs((idt-idt_sim)/idt) cost = np.abs(np.log(idt_sim / idt)) return cost, idt_sim
def hychem_cost(dir, x, d, cond): dir = fix_dir(dir) # print("hychem_cost", dir) write_cheminp(dir, x) sim = chemkin_wrapper(working_dir=dir, **cond) # HyChem simulated t = d['t'] tp = sim['t'] common_s = list(set(d).intersection(set(sim)).difference(set(["t"]))) fp = sim[common_s] interp_f = interp1d(tp, fp, axis=0, fill_value="extrapolate") f = interp_f(t) # meas squared error cost = np.linalg.norm(f - d[common_s], ord='fro')**2. / (f.shape[0] * f.shape[1]) # cost = np.sum(np.abs(f-d[common_s])) return cost
def write_cheminp(dir, x, bounds=A2_C1_BOUNDS): ''' input params: (d,1) bounds (l,u): (d,2) ''' dir = fix_dir(dir) assert (x.shape[0] == bounds.shape[0]) assert (2 == bounds.shape[1]) outputs = '' with open(dir + "chem_part_1.inp", "r") as f: outputs += f.read() params = x_to_params(x, bounds) # print(params) outputs += build_hychem_rxn(params) # with open(dir+"chem_part_3_no_oxygen.inp", "r") as f: with open(dir + "chem_part_3.inp", "r") as f: outputs += f.read() # print("write_cheminp: "+dir+"chem.inp") with open(dir + "chem.inp", "w") as f: f.write(outputs) return
def chemkin_wrapper(working_dir='./', T=1225, P=1.7, MF={ 'POSF10325': .004, 'AR': .996 }, mode='HP', TIME=2e-3, DELT=1e-6): ''' wrapper for running chemkin ''' working_dir = fix_dir(working_dir) # write initial condition to file init_conditions(working_dir, T, P, MF, mode, TIME, DELT) # change current directory cwd = os.getcwd() # run ./chem os.system('cd ' + working_dir + '; ./chem; cd ' + cwd) # run ./igsenp os.system('cd ' + working_dir + '; ./igsenp; cd ' + cwd) # extract mole fraction time history d = extract_from_outputs(working_dir) return d
def grad_hychem_gmm( dir, params, cond, perturb=1e-3, target_species=['C2H4', 'C3H6', 'C4H81', 'CH4', 'POSF10325', 'H2'], target_times=np.arange(1, 11) / 10 * 2e3): dir = fix_dir(dir) x = params_to_x(params, A2_C1_BOUNDS) N_grad = 15 write_cheminp(dir, x) sim = chemkin_wrapper(dir, **cond) tp = sim['t'] fp = sim[target_species] interp_f = interp1d(tp, fp, axis=0, fill_value="extrapolate") f = interp_f(target_times) grad = np.zeros((N_grad, target_times.shape[0] * len(target_species))) for i in range(N_grad): x1 = np.array(x, copy=True) x1[i] = x[i] + perturb write_cheminp(dir, x1) sim1 = chemkin_wrapper(dir, **cond) tp = sim1['t'] fp = sim1[target_species] interp_f = interp1d(tp, fp, axis=0, fill_value="extrapolate") f1 = interp_f(target_times) # print(f1.shape, f.shape) grad_i = (f1 - f) / perturb # grad_i = (sim1[target_species]-sim[target_species])/perturb grad_i = np.reshape(grad_i, (1, -1), order="F") # print(grad_i.shape) grad[i, :] = grad_i return grad
def __init__(self, working_dir): self.working_dir = fix_dir(working_dir)