def test_falloff(self): r = ct.FalloffReaction('OH:2', 'H2O2:1') r.high_rate = ct.Arrhenius(7.4e10, -0.37, 0.0) r.low_rate = ct.Arrhenius(2.3e12, -0.9, -1700*1000*4.184) r.falloff = ct.TroeFalloff((0.7346, 94, 1756, 5182)) r.efficiencies = {'AR':0.7, 'H2':2.0, 'H2O':6.0} gas2 = ct.Solution(thermo='IdealGas', kinetics='GasKinetics', species=self.species, reactions=[r]) gas2.TPX = self.gas.TPX self.assertNear(gas2.forward_rate_constants[0], self.gas.forward_rate_constants[20]) self.assertNear(gas2.net_rates_of_progress[0], self.gas.net_rates_of_progress[20])
def _build_cantera_solution(cls, gdata): p_ref = gdata['ref_pressure'] species_list = list() for s in gdata['species']: spec = gdata['species'][s] ctspec = ct.Species( s, ','.join( [f'{k}:{spec["atom_map"][k]}' for k in spec['atom_map']])) if spec['cp'][0] == 'constant': Tmin, Tmax, T0, h0, s0, cp = spec['cp'][1:] ctspec.thermo = ct.ConstantCp(Tmin, Tmax, p_ref, list([T0, h0, s0, cp])) elif spec['cp'][0] == 'NASA7': Tmin, Tmid, Tmax, low_coeffs, high_coeffs = spec['cp'][1:] coeffs = [Tmid] + high_coeffs + low_coeffs ctspec.thermo = ct.NasaPoly2(Tmin, Tmax, p_ref, coeffs) species_list.append(ctspec) reaction_list = list() for rxn in gdata['reactions']: type, reactants_stoich, products_stoich, reversible = rxn[:4] fwd_pre_exp_value, fwd_temp_exponent, fwd_act_energy = rxn[4:7] fwd_rate = ct.Arrhenius(fwd_pre_exp_value, fwd_temp_exponent, fwd_act_energy) if type == 'simple' or type == 'simple-special': ctrxn = ct.ElementaryReaction(reactants_stoich, products_stoich) ctrxn.rate = fwd_rate elif type == 'three-body' or type == 'three-body-special': ctrxn = ct.ThreeBodyReaction(reactants_stoich, products_stoich) ctrxn.rate = fwd_rate ctrxn.efficiencies = rxn[7] ctrxn.default_efficiency = rxn[8] elif type == 'Lindemann' or type == 'Lindemann-special': ctrxn = ct.FalloffReaction(reactants_stoich, products_stoich) ctrxn.efficiencies = rxn[7] ctrxn.default_efficiency = rxn[8] flf_pre_exp_value, flf_temp_exponent, flf_act_energy = rxn[ 9:12] ctrxn.high_rate = fwd_rate ctrxn.low_rate = ct.Arrhenius(flf_pre_exp_value, flf_temp_exponent, flf_act_energy) ctrxn.falloff = ct.Falloff() elif type == 'Troe' or type == 'Troe-special': ctrxn = ct.FalloffReaction(reactants_stoich, products_stoich) ctrxn.efficiencies = rxn[7] ctrxn.default_efficiency = rxn[8] flf_pre_exp_value, flf_temp_exponent, flf_act_energy = rxn[ 9:12] troe_params = rxn[12] ctrxn.high_rate = fwd_rate ctrxn.low_rate = ct.Arrhenius(flf_pre_exp_value, flf_temp_exponent, flf_act_energy) if len(troe_params) == 4 and abs(troe_params[3]) < 1e-300: ctrxn.falloff = ct.TroeFalloff(troe_params[:3]) else: ctrxn.falloff = ct.TroeFalloff(troe_params) if 'special' in type: ctrxn.orders = rxn[-1] ctrxn.reversible = reversible reaction_list.append(ctrxn) return ct.Solution(thermo='IdealGas', kinetics='GasKinetics', species=species_list, reactions=reaction_list)
def modify_reactions(self, coeffs, rxnNums=[]): # Only works for Arrhenius equations currently if not rxnNums: # if rxnNums does not exist, modify all rxnNums = range(len(coeffs)) else: if isinstance(rxnNums, (float, int)): # if single reaction given, run that one rxnNums = [rxnNums] rxns_to_SRI = {} for rxnNum in rxnNums: rxn = self.gas.reaction(rxnNum) rxnChanged = False if type(rxn) in [ct.ElementaryReaction, ct.ThreeBodyReaction]: for coefName in ['activation_energy', 'pre_exponential_factor', 'temperature_exponent']: if coeffs[rxnNum][0][coefName] != eval(f'rxn.rate.{coefName}'): rxnChanged = True if rxnChanged: # Update reaction rate A = coeffs[rxnNum][0]['pre_exponential_factor'] b = coeffs[rxnNum][0]['temperature_exponent'] Ea = coeffs[rxnNum][0]['activation_energy'] rxn.rate = ct.Arrhenius(A, b, Ea) #elif type(rxn) is ct.PlogReaction: # # Search for altered reactions, if found convert to SRI # for n, rate in enumerate(rxn.rates): # if rxnNum in rxns_to_SRI: # break # for coefName in ['activation_energy', 'pre_exponential_factor', 'temperature_exponent']: # if coeffs[rxnNum][n][coefName] != eval(f'rate[1].{coefName}'): # rxns_to_SRI[rxnNum] = 'SRI' # break # continue elif type(rxn) is ct.FalloffReaction: for key in ['low_rate', 'high_rate', 'falloff_parameters']: if 'rate' in key: for coefName in ['activation_energy', 'pre_exponential_factor', 'temperature_exponent']: if coeffs[rxnNum][key][coefName] != eval(f'rxn.{key}.{coefName}'): rxnChanged = True A = coeffs[rxnNum][key]['pre_exponential_factor'] b = coeffs[rxnNum][key]['temperature_exponent'] Ea = coeffs[rxnNum][key]['activation_energy'] setattr(rxn, key, ct.Arrhenius(A, b, Ea)) break else: length_different = len(coeffs[rxnNum][key]) != len(rxn.falloff.parameters) if length_different or (coeffs[rxnNum][key] != rxn.falloff.parameters).any(): rxnChanged = True if coeffs[rxnNum]['falloff_type'] == 'Troe': rxn.falloff = ct.TroeFalloff(coeffs[rxnNum][key]) else: # could also be SRI. For optimization this would need to be cast as Troe rxn.falloff = ct.SriFalloff(coeffs[rxnNum][key]) elif type(rxn) is ct.ChebyshevReaction: pass else: continue if rxnChanged: self.gas.modify_reaction(rxnNum, rxn)
def set_mechanism(self, mech_dict, species_dict={}, bnds=[]): def get_Arrhenius_parameters(entry): A = entry['pre_exponential_factor'] b = entry['temperature_exponent'] Ea = entry['activation_energy'] return A, b, Ea if len(species_dict) == 0: species = self.gas.species() else: species = [] for n in range(len(species_dict)): s_dict = species_dict[n] s = ct.Species(name=s_dict['name'], composition=s_dict['composition'], charge=s_dict['charge'], size=s_dict['size']) thermo = s_dict['type'](s_dict['T_low'], s_dict['T_high'], s_dict['P_ref'], s_dict['coeffs']) s.thermo = thermo species.append(s) # Set kinetics data rxns = [] for rxnIdx in range(len(mech_dict)): if 'ElementaryReaction' == mech_dict[rxnIdx]['rxnType']: rxn = ct.ElementaryReaction(mech_dict[rxnIdx]['reactants'], mech_dict[rxnIdx]['products']) rxn.allow_negative_pre_exponential_factor = True A, b, Ea = get_Arrhenius_parameters( mech_dict[rxnIdx]['rxnCoeffs'][0]) rxn.rate = ct.Arrhenius(A, b, Ea) elif 'ThreeBodyReaction' == mech_dict[rxnIdx]['rxnType']: rxn = ct.ThreeBodyReaction(mech_dict[rxnIdx]['reactants'], mech_dict[rxnIdx]['products']) A, b, Ea = get_Arrhenius_parameters( mech_dict[rxnIdx]['rxnCoeffs'][0]) rxn.rate = ct.Arrhenius(A, b, Ea) rxn.efficiencies = mech_dict[rxnIdx]['rxnCoeffs'][0][ 'efficiencies'] elif 'PlogReaction' == mech_dict[rxnIdx]['rxnType']: rxn = ct.PlogReaction(mech_dict[rxnIdx]['reactants'], mech_dict[rxnIdx]['products']) rates = [] for plog in mech_dict[rxnIdx]['rxnCoeffs']: pressure = plog['Pressure'] A, b, Ea = get_Arrhenius_parameters(plog) rates.append((pressure, ct.Arrhenius(A, b, Ea))) rxn.rates = rates elif 'FalloffReaction' == mech_dict[rxnIdx]['rxnType']: rxn = ct.FalloffReaction(mech_dict[rxnIdx]['reactants'], mech_dict[rxnIdx]['products']) # high pressure limit A, b, Ea = get_Arrhenius_parameters( mech_dict[rxnIdx]['rxnCoeffs']['high_rate']) rxn.high_rate = ct.Arrhenius(A, b, Ea) # low pressure limit A, b, Ea = get_Arrhenius_parameters( mech_dict[rxnIdx]['rxnCoeffs']['low_rate']) rxn.low_rate = ct.Arrhenius(A, b, Ea) # falloff parameters if mech_dict[rxnIdx]['rxnCoeffs']['falloff_type'] == 'Troe': falloff_param = mech_dict[rxnIdx]['rxnCoeffs'][ 'falloff_parameters'] if falloff_param[-1] == 0.0: falloff_param = falloff_param[0:-1] rxn.falloff = ct.TroeFalloff(falloff_param) else: rxn.falloff = ct.SriFalloff( mech_dict[rxnIdx]['rxnCoeffs']['falloff_parameters']) rxn.efficiencies = mech_dict[rxnIdx]['rxnCoeffs'][ 'efficiencies'] elif 'ChebyshevReaction' == mech_dict[rxnIdx]['rxnType']: rxn = ct.ChebyshevReaction(mech_dict[rxnIdx]['reactants'], mech_dict[rxnIdx]['products']) rxn.set_parameters(Tmin=mech_dict['Tmin'], Tmax=mech_dict['Tmax'], Pmin=mech_dict['Pmin'], Pmax=mech_dict['Pmax'], coeffs=mech_dict['coeffs']) rxn.duplicate = mech_dict[rxnIdx]['duplicate'] rxn.reversible = mech_dict[rxnIdx]['reversible'] rxn.allow_negative_orders = True rxn.allow_nonreactant_orders = True rxns.append(rxn) self.gas = ct.Solution(thermo='IdealGas', kinetics='GasKinetics', species=species, reactions=rxns) self.set_rate_expression_coeffs(bnds) # set copy of coeffs self.set_thermo_expression_coeffs() # set copy of thermo coeffs