forked from jwallen/RMG-database
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexportOldDatabase.py
128 lines (93 loc) · 4.38 KB
/
exportOldDatabase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python
# encoding: utf-8
"""
This script exports the database to the old RMG-Java format. The script
requires two command-line arguments: the path to the database to import, and
the path to save the old RMG-Java database to.
"""
import os
import sys
from rmgpy.data.rmg import RMGDatabase
from rmgpy.data.base import Entry
from rmgpy.reaction import Reaction
from rmgpy.kinetics import Arrhenius, ArrheniusEP, KineticsData
###############################################################################
def main():
if len(sys.argv) != 3:
raise Exception('You must pass input and output '
'directories as parameters.')
newPath = sys.argv[1]
oldPath = os.path.join(sys.argv[2], 'RMG_database')
export(newPath, oldPath)
###############################################################################
def export(input, output, database=None):
print 'Loading the new RMG-Py database...'
if not database:
database = RMGDatabase()
database.load(input)
print 'Constructing additional rate rules from kinetics depository...'
for family in database.kinetics.families.values():
generateRules(family, database)
print 'Saving old RMG-Java database...'
database.saveOld(output)
###############################################################################
def generateRules(family, database):
"""
For a given reaction `family` label, generate additional rate rules from
the corresponding depository training set. This function does automatically
what users used to do by hand to construct a rate rule from reaction
kinetics found in the literature, i.e. determine the groups involved,
adjust to a per-site basis, etc.
"""
# Load rules and determine starting index
rules = family.rules
index = max([entry.index for entry in rules.entries.values()] or [0]) + 1
# Load training entries
for depository in family.depositories:
if 'training' in depository.name:
entries = sorted(depository.entries.values(),
key=lambda entry: (entry.index, entry.label))
break
# Generate a rate rule for each training entry
for entry in entries:
# Load entry's reaction, template, and kinetics
reaction, template = database.kinetics.getForwardReactionForFamilyEntry(entry=entry,
family=family.name,
thermoDatabase=database.thermo)
kinetics = reaction.kinetics
# Convert KineticsData to Arrhenius
if isinstance(kinetics, KineticsData):
kinetics = Arrhenius().fitToData(Tdata=kinetics.Tdata.values,
kdata=kinetics.kdata.values,
kunits=kinetics.kdata.units,
T0=1)
# Ignore other kinetics types
if not isinstance(kinetics, Arrhenius):
continue
# Change reference temperature to 1 K if necessary
if kinetics.T0.value != 1:
kinetics = kinetics.changeT0(1)
# Convert kinetics to a per-site basis
kinetics.A.value /= reaction.degeneracy
# Convert to ArrheniusEP
kinetics = ArrheniusEP(A=kinetics.A,
n=kinetics.n,
alpha=0,
E0=kinetics.Ea,
Tmin=kinetics.Tmin,
Tmax=kinetics.Tmax)
# Add new rate rule
rules.entries[index] = Entry(index=index,
label=';'.join([group.label for group in template]),
item=Reaction(reactants=template[:],
products=None),
data=kinetics,
reference=entry.reference,
rank=entry.rank,
shortDesc=entry.shortDesc,
longDesc=entry.longDesc,
history=entry.history)
index += 1
###############################################################################
if __name__ == '__main__':
main()