Skip to content

Commit d4138b3

Browse files
committed
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 ca18e08 commit d4138b3

File tree

1 file changed

+73
-66
lines changed

1 file changed

+73
-66
lines changed

rmgpy/data/kinetics/family.py

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

47504750
def _make_rule(rr):
47514751
"""
4752-
function for parallelization of rule and uncertainty calculation
4752+
Function for parallelization of rule and uncertainty calculation
4753+
4754+
Input: rr - tuple of (recipe, rxns, Tref, fmax, label, ranks)
4755+
rxns and ranks are lists of equal length.
4756+
Output: kinetics object, with uncertainty and comment attached.
4757+
If Blowers-Masel fitting is successful it will be ArrheniusBM or ArrheniusChargeTransferBM,
4758+
else Arrhenius, SurfaceChargeTransfer, or ArrheniusChargeTransfer.
4759+
47534760
Errors in Ln(k) at each reaction are treated as samples from a weighted normal distribution
47544761
weights are inverse variance weights based on estimates of the error in Ln(k) for each individual reaction
47554762
"""
47564763
recipe, rxns, Tref, fmax, label, ranks = rr
47574764
for i, rxn in enumerate(rxns):
47584765
rxn.rank = ranks[i]
47594766
rxns = np.array(rxns)
4760-
rs = np.array([r for r in rxns if type(r.kinetics) != KineticsModel])
4767+
rs = np.array([r for r in rxns if type(r.kinetics) != KineticsModel]) # KineticsModel is the base class with no data.
47614768
n = len(rs)
47624769
data_mean = np.mean(np.log([r.kinetics.get_rate_coefficient(Tref) for r in rs]))
4763-
if n > 0:
4764-
if isinstance(rs[0].kinetics, Arrhenius):
4765-
arr = ArrheniusBM
4770+
4771+
if n == 0:
4772+
return None
4773+
4774+
if isinstance(rs[0].kinetics, Arrhenius):
4775+
arr = ArrheniusBM
4776+
else:
4777+
arr = ArrheniusChargeTransferBM
4778+
if n > 1:
4779+
kin = arr().fit_to_reactions(rs, recipe=recipe)
4780+
if n == 1 or kin.E0.value_si < 0.0:
4781+
kin = average_kinetics([r.kinetics for r in rs])
4782+
#kin.comment = "Only one reaction or Arrhenius BM fit bad. Instead averaged from {} reactions.".format(n)
4783+
if n == 1:
4784+
kin.uncertainty = RateUncertainty(mu=0.0, var=(np.log(fmax) / 2.0) ** 2, N=1, Tref=Tref, data_mean=data_mean, correlation=label)
47664785
else:
4767-
arr = ArrheniusChargeTransferBM
4768-
if n > 1:
4769-
kin = arr().fit_to_reactions(rs, recipe=recipe)
4770-
if n == 1 or kin.E0.value_si < 0.0:
4771-
kin = average_kinetics([r.kinetics for r in rs])
4772-
#kin.comment = "Only one reaction or Arrhenius BM fit bad. Instead averaged from {} reactions.".format(n)
4773-
if n == 1:
4774-
kin.uncertainty = RateUncertainty(mu=0.0, var=(np.log(fmax) / 2.0) ** 2, N=1, Tref=Tref, data_mean=data_mean, correlation=label)
4775-
else:
4786+
dlnks = np.array([
4787+
np.log(
4788+
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)
4789+
) for i, rxn in enumerate(rs)
4790+
]) # 1) fit to set of reactions without the current reaction (k) 2) compute log(kfit/kactual) at Tref
4791+
varis = (np.array([rank_accuracy_map[rxn.rank].value_si for rxn in rs]) / (2.0 * 8.314 * Tref)) ** 2
4792+
# weighted average calculations
4793+
ws = 1.0 / varis
4794+
V1 = ws.sum()
4795+
V2 = (ws ** 2).sum()
4796+
mu = np.dot(ws, dlnks) / V1
4797+
s = np.sqrt(np.dot(ws, (dlnks - mu) ** 2) / (V1 - V2 / V1))
4798+
kin.uncertainty = RateUncertainty(mu=mu, var=s ** 2, N=n, Tref=Tref, data_mean=data_mean, correlation=label)
4799+
else:
4800+
if n == 1:
4801+
kin.uncertainty = RateUncertainty(mu=0.0, var=(np.log(fmax) / 2.0) ** 2, N=1, Tref=Tref, data_mean=data_mean, correlation=label)
4802+
else:
4803+
if isinstance(rs[0].kinetics, Arrhenius):
47764804
dlnks = np.array([
47774805
np.log(
4778-
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)
4779-
) for i, rxn in enumerate(rs)
4780-
]) # 1) fit to set of reactions without the current reaction (k) 2) compute log(kfit/kactual) at Tref
4781-
varis = (np.array([rank_accuracy_map[rxn.rank].value_si for rxn in rs]) / (2.0 * 8.314 * Tref)) ** 2
4782-
# weighted average calculations
4783-
ws = 1.0 / varis
4784-
V1 = ws.sum()
4785-
V2 = (ws ** 2).sum()
4786-
mu = np.dot(ws, dlnks) / V1
4787-
s = np.sqrt(np.dot(ws, (dlnks - mu) ** 2) / (V1 - V2 / V1))
4788-
kin.uncertainty = RateUncertainty(mu=mu, var=s ** 2, N=n, Tref=Tref, data_mean=data_mean, correlation=label)
4789-
else:
4790-
if n == 1:
4791-
kin.uncertainty = RateUncertainty(mu=0.0, var=(np.log(fmax) / 2.0) ** 2, N=1, Tref=Tref, data_mean=data_mean, correlation=label)
4806+
arr().fit_to_reactions(rs[list(set(range(len(rs))) - {i})], recipe=recipe)
4807+
.to_arrhenius(rxn.get_enthalpy_of_reaction(Tref))
4808+
.get_rate_coefficient(T=Tref) / rxn.get_rate_coefficient(T=Tref)
4809+
) for i, rxn in enumerate(rs)
4810+
]) # 1) fit to set of reactions without the current reaction (k) 2) compute log(kfit/kactual) at Tref
47924811
else:
4793-
if isinstance(rs[0].kinetics, Arrhenius):
4794-
dlnks = np.array([
4795-
np.log(
4796-
arr().fit_to_reactions(rs[list(set(range(len(rs))) - {i})], recipe=recipe)
4797-
.to_arrhenius(rxn.get_enthalpy_of_reaction(Tref))
4798-
.get_rate_coefficient(T=Tref) / rxn.get_rate_coefficient(T=Tref)
4799-
) for i, rxn in enumerate(rs)
4800-
]) # 1) fit to set of reactions without the current reaction (k) 2) compute log(kfit/kactual) at Tref
4801-
else:
4802-
dlnks = np.array([
4803-
np.log(
4804-
arr().fit_to_reactions(rs[list(set(range(len(rs))) - {i})], recipe=recipe)
4805-
.to_arrhenius_charge_transfer(rxn.get_enthalpy_of_reaction(Tref))
4806-
.get_rate_coefficient(T=Tref) / rxn.get_rate_coefficient(T=Tref)
4807-
) for i, rxn in enumerate(rs)
4808-
]) # 1) fit to set of reactions without the current reaction (k) 2) compute log(kfit/kactual) at Tref
4809-
varis = (np.array([rank_accuracy_map[rxn.rank].value_si for rxn in rs]) / (2.0 * 8.314 * Tref)) ** 2
4810-
# weighted average calculations
4811-
ws = 1.0 / varis
4812-
V1 = ws.sum()
4813-
V2 = (ws ** 2).sum()
4814-
mu = np.dot(ws, dlnks) / V1
4815-
s = np.sqrt(np.dot(ws, (dlnks - mu) ** 2) / (V1 - V2 / V1))
4816-
kin.uncertainty = RateUncertainty(mu=mu, var=s ** 2, N=n, Tref=Tref, data_mean=data_mean, correlation=label)
4817-
4818-
#site solute parameters
4819-
site_datas = [get_site_solute_data(rxn) for rxn in rxns]
4820-
site_datas = [sdata for sdata in site_datas if sdata is not None]
4821-
if len(site_datas) > 0:
4822-
site_data = SoluteTSData()
4823-
for sdata in site_datas:
4824-
site_data += sdata
4825-
site_data = site_data * (1.0/len(site_datas))
4826-
kin.solute = site_data
4827-
return kin
4828-
else:
4829-
return None
4830-
4812+
dlnks = np.array([
4813+
np.log(
4814+
arr().fit_to_reactions(rs[list(set(range(len(rs))) - {i})], recipe=recipe)
4815+
.to_arrhenius_charge_transfer(rxn.get_enthalpy_of_reaction(Tref))
4816+
.get_rate_coefficient(T=Tref) / rxn.get_rate_coefficient(T=Tref)
4817+
) for i, rxn in enumerate(rs)
4818+
]) # 1) fit to set of reactions without the current reaction (k) 2) compute log(kfit/kactual) at Tref
4819+
varis = (np.array([rank_accuracy_map[rxn.rank].value_si for rxn in rs]) / (2.0 * 8.314 * Tref)) ** 2
4820+
# weighted average calculations
4821+
ws = 1.0 / varis
4822+
V1 = ws.sum()
4823+
V2 = (ws ** 2).sum()
4824+
mu = np.dot(ws, dlnks) / V1
4825+
s = np.sqrt(np.dot(ws, (dlnks - mu) ** 2) / (V1 - V2 / V1))
4826+
kin.uncertainty = RateUncertainty(mu=mu, var=s ** 2, N=n, Tref=Tref, data_mean=data_mean, correlation=label)
4827+
4828+
#site solute parameters
4829+
site_datas = [get_site_solute_data(rxn) for rxn in rxns]
4830+
site_datas = [sdata for sdata in site_datas if sdata is not None]
4831+
if len(site_datas) > 0:
4832+
site_data = SoluteTSData()
4833+
for sdata in site_datas:
4834+
site_data += sdata
4835+
site_data = site_data * (1.0/len(site_datas))
4836+
kin.solute = site_data
4837+
return kin
48314838

48324839
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):
48334840
parent_conn, child_conn = mp.Pipe()

0 commit comments

Comments
 (0)