def test_perturbed_inert(self): c = cs.CellSize() pert = 3 pert_frac = 0.01 inert = 'AR' test = c(base_mechanism='Mevel2017.cti', initial_temp=300, initial_press=101325, fuel='H2', oxidizer='O2', equivalence=1, diluent='AR', diluent_mol_frac=0.02, cj_speed=2834.9809153464994, perturbed_reaction=pert, perturbation_fraction=pert_frac, inert=inert) gas = ct.Solution(self.mechanism) rxns = [ rxn for rxn in gas.reactions() if inert not in rxn.reactants or inert not in rxn.products ] checks = [c.base_gas.n_reactions == len(rxns)] checks += [ rxn0 == rxn1 for rxn0, rxn1 in zip(rxns, c.base_gas.reactions()) ]
def test_build_gas_no_dilution(self): c = cs.CellSize() undiluted = {'H2': 2 / 3, 'O2': 1 / 3} # should not dilute with diluent=None c.mechanism = 'Mevel2017.cti' c.initial_temp = self.init_temp c.initial_press = self.init_press c.fuel = 'H2' c.oxidizer = 'O2' c.inert = None c.equivalence = 1 c.diluent = None c.diluent_mol_frac = 0.5 c.perturbed_reaction = -1 test = c._build_gas_object().mole_fraction_dict() check_none = [ np.isclose(undiluted[key], value) for key, value in test.items() ] # should not dilute with diluent_mol_frac=0 c.diluent = 'AR' c.diluent_mol_frac = 0 test = c._build_gas_object().mole_fraction_dict() check_zero = [ np.isclose(undiluted[key], value) for key, value in test.items() ] assert all([check_none, check_zero])
def test_build_gas_with_dilution(self): c = cs.CellSize() c.mechanism = 'Mevel2017.cti' c.initial_temp = self.init_temp c.initial_press = self.init_press c.fuel = 'H2' c.oxidizer = 'O2' c.inert = None c.equivalence = 1 c.diluent = 'AR' c.diluent_mol_frac = 0.1 c.perturbed_reaction = -1 test = c._build_gas_object().mole_fraction_dict() check = [ np.isclose(test['H2'] / test['O2'], 2), np.isclose(test['AR'], 0.1) ] assert all(check)
def test_cell_sizes(self): c = cs.CellSize() test = c(base_mechanism=self.mechanism, initial_temp=self.init_temp, initial_press=self.init_press, fuel='H2', oxidizer='O2:1, N2:3.76', equivalence=1, diluent='None', diluent_mol_frac=0, cj_speed=self.cj_speed) assert (all( [[ abs(cell - test[correlation]) / cell < relTol for correlation, cell in self.original_cell_sizes.items() ], [ abs(cell - test[correlation]) < absTol for correlation, cell in self.original_cell_sizes.items() ]]))
def test_induction_lengths(self): c = cs.CellSize() c(base_mechanism=self.mechanism, initial_temp=self.init_temp, initial_press=self.init_press, fuel='H2', oxidizer='O2:1, N2:3.76', equivalence=1, diluent='None', diluent_mol_frac=0, cj_speed=self.cj_speed) assert (all([[ abs(length - c.induction_length[correlation]) / length < relTol for correlation, length in self.original_induction_lengths.items() ], [ abs(length - c.induction_length[correlation]) < absTol for correlation, length in self.original_induction_lengths.items() ]]))
def test_perturbed_diluted(self): c = cs.CellSize() pert = 3 pert_frac = 0.01 c(base_mechanism='Mevel2017.cti', initial_temp=300, initial_press=101325, fuel='H2', oxidizer='O2', equivalence=1, diluent='AR', diluent_mol_frac=0.02, cj_speed=2834.9809153464994, perturbed_reaction=pert, perturbation_fraction=pert_frac) n_rxns = c.base_gas.n_reactions correct_multipliers = np.ones(n_rxns) correct_multipliers[pert] = 1 + pert_frac multipliers = [c.base_gas.multiplier(i) for i in range(n_rxns)] assert np.allclose(multipliers, correct_multipliers)
def test_perturbed(self): c = cs.CellSize() pert = 3 pert_frac = 0.01 c(base_mechanism=self.mechanism, initial_temp=self.init_temp, initial_press=self.init_press, fuel='H2', oxidizer='O2:1, N2:3.76', equivalence=1, diluent=None, diluent_mol_frac=0, cj_speed=self.cj_speed, perturbed_reaction=pert, perturbation_fraction=pert_frac) n_rxns = c.base_gas.n_reactions correct_multipliers = np.ones(n_rxns) correct_multipliers[pert] = 1 + pert_frac multipliers = [c.base_gas.multiplier(i) for i in range(n_rxns)] assert np.allclose(multipliers, correct_multipliers)
def perform_study(mech, init_temp, init_press, equivalence, fuel, oxidizer, diluent, diluent_mol_frac, inert, perturbation_fraction, perturbed_reaction_no, db_lock): CellSize = cell_size.CellSize() gas = cell_size.solution_with_inerts(mech, inert) gas.TP = init_temp, init_press gas.set_equivalence_ratio(equivalence, fuel, oxidizer) db_name = 'sensitivity.sqlite' table_name = 'data' current_table = db.Table(database=db_name, table_name=table_name) with db_lock: # check for stored cj speed stored_cj = check_stored_base_cj_speed( table=current_table, mechanism=mech, initial_temp=init_temp, initial_press=init_press, fuel=fuel, oxidizer=oxidizer, equivalence=equivalence, diluent=diluent, diluent_mol_frac=diluent_mol_frac, inert=inert) if not stored_cj: # calculate and store cj speed cj_speed = sd.postshock.CJspeed(P1=init_press, T1=init_temp, q=gas.mole_fraction_dict(), mech=mech) rxn_table_id = current_table.store_test_row( mechanism=mech, initial_temp=init_temp, initial_press=init_press, fuel=fuel, oxidizer=oxidizer, equivalence=equivalence, diluent=diluent, diluent_mol_frac=diluent_mol_frac, inert=inert, cj_speed=cj_speed, ind_len_west=0, ind_len_gav=0, ind_len_ng=0, cell_size_west=0, cell_size_gav=0, cell_size_ng=0, overwrite_existing=False) else: # stored cj speed exists current_data = current_table.fetch_test_rows( mechanism=mech, initial_temp=init_temp, initial_press=init_press, fuel=fuel, oxidizer=oxidizer, equivalence=equivalence, diluent=diluent, diluent_mol_frac=diluent_mol_frac, inert=inert) [rxn_table_id] = current_data['rxn_table_id'] [cj_speed] = current_data['cj_speed'] del current_data del stored_cj # check for stored base reaction data stored_rxns = current_table.check_for_stored_base_data(rxn_table_id) if not stored_rxns: current_table.store_base_rxn_table(rxn_table_id=rxn_table_id, gas=gas) del stored_rxns stored_base_calcs = check_stored_base_calcs( table=current_table, mechanism=mech, initial_temp=init_temp, initial_press=init_press, fuel=fuel, oxidizer=oxidizer, equivalence=equivalence, diluent=diluent, diluent_mol_frac=diluent_mol_frac, inert=inert) if not stored_base_calcs: # calculate base cell size base_cell_calcs = CellSize( base_mechanism=mech, cj_speed=cj_speed, initial_temp=init_temp, initial_press=init_press, fuel=fuel, oxidizer=oxidizer, equivalence=equivalence, diluent=diluent, diluent_mol_frac=diluent_mol_frac, inert=inert, ) base_ind_len = CellSize.induction_length current_table.store_test_row( mechanism=mech, initial_temp=init_temp, initial_press=init_press, fuel=fuel, oxidizer=oxidizer, equivalence=equivalence, diluent=diluent, diluent_mol_frac=diluent_mol_frac, inert=inert, cj_speed=cj_speed, ind_len_west=base_ind_len['Westbrook'], ind_len_gav=base_ind_len['Gavrikov'], ind_len_ng=base_ind_len['Ng'], cell_size_west=base_cell_calcs['Westbrook'], cell_size_gav=base_cell_calcs['Gavrikov'], cell_size_ng=base_cell_calcs['Ng'], overwrite_existing=True) else: # look up base calcs from db current_data = current_table.fetch_test_rows( mechanism=mech, initial_temp=init_temp, initial_press=init_press, fuel=fuel, oxidizer=oxidizer, equivalence=equivalence, diluent=diluent, diluent_mol_frac=diluent_mol_frac, inert=inert) base_ind_len = { 'Westbrook': current_data['ind_len_west'][0], 'Gavrikov': current_data['ind_len_gav'][0], 'Ng': current_data['ind_len_ng'][0], } base_cell_calcs = { 'Westbrook': current_data['cell_size_west'][0], 'Gavrikov': current_data['cell_size_gav'][0], 'Ng': current_data['cell_size_ng'][0], } del current_data del stored_base_calcs # calculate perturbed cell size pert_cell_calcs = CellSize(base_mechanism=mech, cj_speed=cj_speed, initial_temp=init_temp, initial_press=init_press, fuel=fuel, oxidizer=oxidizer, equivalence=equivalence, diluent=diluent, diluent_mol_frac=diluent_mol_frac, inert=inert, perturbed_reaction=perturbed_reaction_no) pert_ind_len = CellSize.induction_length # calculate sensitivities sens_ind_len_west = (pert_ind_len['Westbrook'] - base_ind_len['Westbrook']) / \ (base_ind_len['Westbrook'] * perturbation_fraction) sens_cell_size_west = (pert_cell_calcs['Westbrook'] - base_cell_calcs['Westbrook']) / \ (base_cell_calcs['Westbrook'] * perturbation_fraction) sens_ind_len_gav = (pert_ind_len['Gavrikov'] - base_ind_len['Gavrikov']) / \ (base_ind_len['Gavrikov'] * perturbation_fraction) sens_cell_size_gav = (pert_cell_calcs['Gavrikov'] - base_cell_calcs['Gavrikov']) / \ (base_cell_calcs['Gavrikov'] * perturbation_fraction) sens_ind_len_ng = (pert_ind_len['Ng'] - base_ind_len['Ng']) / \ (base_ind_len['Ng'] * perturbation_fraction) sens_cell_size_ng = (pert_cell_calcs['Ng'] - base_cell_calcs['Ng']) / \ (base_cell_calcs['Ng'] * perturbation_fraction) with db_lock: current_table.store_perturbed_row( rxn_table_id=rxn_table_id, rxn_no=perturbed_reaction_no, rxn=CellSize.base_gas.reaction_equation(perturbed_reaction_no), k_i=CellSize.base_gas. forward_rate_constants[perturbed_reaction_no], ind_len_west=pert_ind_len['Westbrook'], ind_len_gav=pert_ind_len['Gavrikov'], ind_len_ng=pert_ind_len['Ng'], cell_size_west=pert_cell_calcs['Westbrook'], cell_size_gav=pert_cell_calcs['Gavrikov'], cell_size_ng=pert_cell_calcs['Ng'], sens_ind_len_west=sens_ind_len_west, sens_ind_len_gav=sens_ind_len_gav, sens_ind_len_ng=sens_ind_len_ng, sens_cell_size_west=sens_cell_size_west, sens_cell_size_gav=sens_cell_size_gav, sens_cell_size_ng=sens_cell_size_ng, overwrite_existing=True)