Skip to content

Commit 17cb7ef

Browse files
rwestssun30
authored andcommitted
Refactor some rate fitting in rule generation.
It was:: if n > 1: # big long block else: return None I inverted the check so we can return early, and outdent a huge block of code.
1 parent 6b72e99 commit 17cb7ef

1 file changed

Lines changed: 73 additions & 66 deletions

File tree

rmgpy/data/kinetics/family.py

Lines changed: 73 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4542,85 +4542,92 @@ def get_objective_function(kinetics1, kinetics2, obj=information_gain, T=1000.0)
45424542

45434543
def _make_rule(rr):
45444544
"""
4545-
function for parallelization of rule and uncertainty calculation
4545+
Function for parallelization of rule and uncertainty calculation
4546+
4547+
Input: rr - tuple of (recipe, rxns, Tref, fmax, label, ranks)
4548+
rxns and ranks are lists of equal length.
4549+
Output: kinetics object, with uncertainty and comment attached.
4550+
If Blowers-Masel fitting is successful it will be ArrheniusBM or ArrheniusChargeTransferBM,
4551+
else Arrhenius, SurfaceChargeTransfer, or ArrheniusChargeTransfer.
4552+
45464553
Errors in Ln(k) at each reaction are treated as samples from a weighted normal distribution
45474554
weights are inverse variance weights based on estimates of the error in Ln(k) for each individual reaction
45484555
"""
45494556
recipe, rxns, Tref, fmax, label, ranks = rr
45504557
for i, rxn in enumerate(rxns):
45514558
rxn.rank = ranks[i]
45524559
rxns = np.array(rxns)
4553-
rs = np.array([r for r in rxns if type(r.kinetics) != KineticsModel])
4560+
rs = np.array([r for r in rxns if type(r.kinetics) != KineticsModel]) # KineticsModel is the base class with no data.
45544561
n = len(rs)
45554562
data_mean = np.mean(np.log([r.kinetics.get_rate_coefficient(Tref) for r in rs]))
4556-
if n > 0:
4557-
if isinstance(rs[0].kinetics, Arrhenius):
4558-
arr = ArrheniusBM
4563+
4564+
if n == 0:
4565+
return None
4566+
4567+
if isinstance(rs[0].kinetics, Arrhenius):
4568+
arr = ArrheniusBM
4569+
else:
4570+
arr = ArrheniusChargeTransferBM
4571+
if n > 1:
4572+
kin = arr().fit_to_reactions(rs, recipe=recipe)
4573+
if n == 1 or kin.E0.value_si < 0.0:
4574+
kin = average_kinetics([r.kinetics for r in rs])
4575+
#kin.comment = "Only one reaction or Arrhenius BM fit bad. Instead averaged from {} reactions.".format(n)
4576+
if n == 1:
4577+
kin.uncertainty = RateUncertainty(mu=0.0, var=(np.log(fmax) / 2.0) ** 2, N=1, Tref=Tref, data_mean=data_mean, correlation=label)
45594578
else:
4560-
arr = ArrheniusChargeTransferBM
4561-
if n > 1:
4562-
kin = arr().fit_to_reactions(rs, recipe=recipe)
4563-
if n == 1 or kin.E0.value_si < 0.0:
4564-
kin = average_kinetics([r.kinetics for r in rs])
4565-
#kin.comment = "Only one reaction or Arrhenius BM fit bad. Instead averaged from {} reactions.".format(n)
4566-
if n == 1:
4567-
kin.uncertainty = RateUncertainty(mu=0.0, var=(np.log(fmax) / 2.0) ** 2, N=1, Tref=Tref, data_mean=data_mean, correlation=label)
4568-
else:
4579+
dlnks = np.array([
4580+
np.log(
4581+
average_kinetics([r.kinetics for r in rs[list(set(range(len(rs))) - {i})]]).get_rate_coefficient(T=Tref) / rxn.get_rate_coefficient(T=Tref)
4582+
) for i, rxn in enumerate(rs)
4583+
]) # 1) fit to set of reactions without the current reaction (k) 2) compute log(kfit/kactual) at Tref
4584+
varis = (np.array([rank_accuracy_map[rxn.rank].value_si for rxn in rs]) / (2.0 * 8.314 * Tref)) ** 2
4585+
# weighted average calculations
4586+
ws = 1.0 / varis
4587+
V1 = ws.sum()
4588+
V2 = (ws ** 2).sum()
4589+
mu = np.dot(ws, dlnks) / V1
4590+
s = np.sqrt(np.dot(ws, (dlnks - mu) ** 2) / (V1 - V2 / V1))
4591+
kin.uncertainty = RateUncertainty(mu=mu, var=s ** 2, N=n, Tref=Tref, data_mean=data_mean, correlation=label)
4592+
else:
4593+
if n == 1:
4594+
kin.uncertainty = RateUncertainty(mu=0.0, var=(np.log(fmax) / 2.0) ** 2, N=1, Tref=Tref, data_mean=data_mean, correlation=label)
4595+
else:
4596+
if isinstance(rs[0].kinetics, Arrhenius):
45694597
dlnks = np.array([
45704598
np.log(
4571-
average_kinetics([r.kinetics for r in rs[list(set(range(len(rs))) - {i})]]).get_rate_coefficient(T=Tref) / rxn.get_rate_coefficient(T=Tref)
4572-
) for i, rxn in enumerate(rs)
4573-
]) # 1) fit to set of reactions without the current reaction (k) 2) compute log(kfit/kactual) at Tref
4574-
varis = (np.array([rank_accuracy_map[rxn.rank].value_si for rxn in rs]) / (2.0 * 8.314 * Tref)) ** 2
4575-
# weighted average calculations
4576-
ws = 1.0 / varis
4577-
V1 = ws.sum()
4578-
V2 = (ws ** 2).sum()
4579-
mu = np.dot(ws, dlnks) / V1
4580-
s = np.sqrt(np.dot(ws, (dlnks - mu) ** 2) / (V1 - V2 / V1))
4581-
kin.uncertainty = RateUncertainty(mu=mu, var=s ** 2, N=n, Tref=Tref, data_mean=data_mean, correlation=label)
4582-
else:
4583-
if n == 1:
4584-
kin.uncertainty = RateUncertainty(mu=0.0, var=(np.log(fmax) / 2.0) ** 2, N=1, Tref=Tref, data_mean=data_mean, correlation=label)
4599+
arr().fit_to_reactions(rs[list(set(range(len(rs))) - {i})], recipe=recipe)
4600+
.to_arrhenius(rxn.get_enthalpy_of_reaction(Tref))
4601+
.get_rate_coefficient(T=Tref) / rxn.get_rate_coefficient(T=Tref)
4602+
) for i, rxn in enumerate(rs)
4603+
]) # 1) fit to set of reactions without the current reaction (k) 2) compute log(kfit/kactual) at Tref
45854604
else:
4586-
if isinstance(rs[0].kinetics, Arrhenius):
4587-
dlnks = np.array([
4588-
np.log(
4589-
arr().fit_to_reactions(rs[list(set(range(len(rs))) - {i})], recipe=recipe)
4590-
.to_arrhenius(rxn.get_enthalpy_of_reaction(Tref))
4591-
.get_rate_coefficient(T=Tref) / rxn.get_rate_coefficient(T=Tref)
4592-
) for i, rxn in enumerate(rs)
4593-
]) # 1) fit to set of reactions without the current reaction (k) 2) compute log(kfit/kactual) at Tref
4594-
else:
4595-
dlnks = np.array([
4596-
np.log(
4597-
arr().fit_to_reactions(rs[list(set(range(len(rs))) - {i})], recipe=recipe)
4598-
.to_arrhenius_charge_transfer(rxn.get_enthalpy_of_reaction(Tref))
4599-
.get_rate_coefficient(T=Tref) / rxn.get_rate_coefficient(T=Tref)
4600-
) for i, rxn in enumerate(rs)
4601-
]) # 1) fit to set of reactions without the current reaction (k) 2) compute log(kfit/kactual) at Tref
4602-
varis = (np.array([rank_accuracy_map[rxn.rank].value_si for rxn in rs]) / (2.0 * 8.314 * Tref)) ** 2
4603-
# weighted average calculations
4604-
ws = 1.0 / varis
4605-
V1 = ws.sum()
4606-
V2 = (ws ** 2).sum()
4607-
mu = np.dot(ws, dlnks) / V1
4608-
s = np.sqrt(np.dot(ws, (dlnks - mu) ** 2) / (V1 - V2 / V1))
4609-
kin.uncertainty = RateUncertainty(mu=mu, var=s ** 2, N=n, Tref=Tref, data_mean=data_mean, correlation=label)
4610-
4611-
#site solute parameters
4612-
site_datas = [get_site_solute_data(rxn) for rxn in rxns]
4613-
site_datas = [sdata for sdata in site_datas if sdata is not None]
4614-
if len(site_datas) > 0:
4615-
site_data = SoluteTSData()
4616-
for sdata in site_datas:
4617-
site_data += sdata
4618-
site_data = site_data * (1.0/len(site_datas))
4619-
kin.solute = site_data
4620-
return kin
4621-
else:
4622-
return None
4623-
4605+
dlnks = np.array([
4606+
np.log(
4607+
arr().fit_to_reactions(rs[list(set(range(len(rs))) - {i})], recipe=recipe)
4608+
.to_arrhenius_charge_transfer(rxn.get_enthalpy_of_reaction(Tref))
4609+
.get_rate_coefficient(T=Tref) / rxn.get_rate_coefficient(T=Tref)
4610+
) for i, rxn in enumerate(rs)
4611+
]) # 1) fit to set of reactions without the current reaction (k) 2) compute log(kfit/kactual) at Tref
4612+
varis = (np.array([rank_accuracy_map[rxn.rank].value_si for rxn in rs]) / (2.0 * 8.314 * Tref)) ** 2
4613+
# weighted average calculations
4614+
ws = 1.0 / varis
4615+
V1 = ws.sum()
4616+
V2 = (ws ** 2).sum()
4617+
mu = np.dot(ws, dlnks) / V1
4618+
s = np.sqrt(np.dot(ws, (dlnks - mu) ** 2) / (V1 - V2 / V1))
4619+
kin.uncertainty = RateUncertainty(mu=mu, var=s ** 2, N=n, Tref=Tref, data_mean=data_mean, correlation=label)
4620+
4621+
#site solute parameters
4622+
site_datas = [get_site_solute_data(rxn) for rxn in rxns]
4623+
site_datas = [sdata for sdata in site_datas if sdata is not None]
4624+
if len(site_datas) > 0:
4625+
site_data = SoluteTSData()
4626+
for sdata in site_datas:
4627+
site_data += sdata
4628+
site_data = site_data * (1.0/len(site_datas))
4629+
kin.solute = site_data
4630+
return kin
46244631

46254632
def _spawn_tree_process(family, template_rxn_map, obj, T, nprocs, depth, min_splitable_entry_num, min_rxns_to_spawn, extension_iter_max, extension_iter_item_cap):
46264633
parent_conn, child_conn = mp.Pipe()

0 commit comments

Comments
 (0)