-
Notifications
You must be signed in to change notification settings - Fork 1
/
CanteraChemEdgeAnalysis.py
189 lines (173 loc) · 6 KB
/
CanteraChemEdgeAnalysis.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from string import *
from ChemEdge import *
from mechinfo import *
class Group:
def copy(self):
return Group(self.mEltCnts)
def __init__(self,eltCnts_in):
self.mEltCnts = {}
for elt in eltCnts_in.keys():
self.mEltCnts[elt] = eltCnts_in[elt]
def __sub__(self,rhs):
val = self.copy()
for elt in rhs.mEltCnts.keys():
if (val.mEltCnts.has_key(elt)):
res = val.mEltCnts[elt] - rhs.mEltCnts[elt]
if (res != 0):
val.mEltCnts[elt] = res
else:
del val.mEltCnts[elt]
else:
val.mEltCnts[elt] = - rhs.mEltCnts[elt]
return val
def __mul__(self,rhs):
val = self.copy()
for elt in val.mEltCnts.keys():
val.mEltCnts[elt] = rhs * val.mEltCnts[elt]
return val
def __rmul__(self,lhs):
val = self.copy()
for elt in val.mEltCnts.keys():
val.mEltCnts[elt] = lhs * val.mEltCnts[elt]
return val
def __str__(self):
val = 'Group < '
for elt in self.mEltCnts.keys():
val = val + elt + ':' + str(self.mEltCnts[elt]) + ' '
val = val + '>'
return val
def __getitem__(self,elt):
if self.contains(elt):
return self.mEltCnts[elt]
return 0
def sameSign(self):
keys = self.mEltCnts.keys()
if (len(keys) > 0):
if (self.mEltCnts[keys[0]] < 0):
for elt in keys:
if (self.mEltCnts[elt] > 0):
return 0
else:
for elt in keys:
if (self.mEltCnts[elt] < 0):
return 0
return 1
def contains(self,elt):
return self.mEltCnts.has_key(elt)
def awt(self):
awt = 0
for elt in self.mEltCnts.keys():
awtElt = elementAtomicWt()[indexElt(elt)]
awt = awt + abs(self.mEltCnts[elt])*awtElt
return awt
def size(self):
return len(self.mEltCnts)
def getEdges(trElt):
edges = []
groups = {}
for sp in speciesNames():
eltCnts = {}
for elt in elementNames():
m = numberOfElementXinSpeciesY(elt,sp)
if (m>0):
eltCnts[elt] = m
groups[sp] = Group(eltCnts)
for r in range(numReactions()):
Redges = []
coeffs = specCoeffsInReaction(r)
net = {}
for (sp,co) in coeffs:
if (net.has_key(sp)):
net[sp] = net[sp] + co
if (net[sp]==0):
del net[sp]
else:
net[sp] = co
reac = []
prod = []
for sp in net.keys():
if (net[sp] < 0):
if (numberOfElementXinSpeciesY(trElt,sp)>0):
reac.append([sp,-net[sp]])
else:
if (numberOfElementXinSpeciesY(trElt,sp)>0):
prod.append([sp,net[sp]])
LR = len(reac)
LP = len(prod)
# no tr-containing species in reac
if (LR == 0 | LP == 0):
continue
# exactly one tr-containing species either side
if ((LR <= 1) | (LP <= 1)):
for spr in reac:
[spcr,cor] = spr
for spp in prod:
[spcp,cop] = spp
w = min(cor*groups[spcr][trElt],cop*groups[spcp][trElt])
Redges.append(Edge(spcr,spcp,[[r,w]]))
else:
# two tr-containing species each side
if (LR == 2 & LP == 2):
[rs0,rc0] = reac[0]
[rs1,rc1] = reac[1]
[ps0,pc0] = prod[0]
[ps1,pc1] = prod[1]
b0 = pc0*groups[ps0] - rc0*groups[rs0]
b1 = pc1*groups[ps1] - rc0*groups[rs0]
pick = 0
if (b0.sameSign() & b1.sameSign()):
if (b1.size() < b0.size()):
pick = 1
if (b1.size() == b0.size()):
if (b0.awt() > b1.awt()):
pick = 1
else:
if (b1.sameSign()):
pick = 1
nR0 = rc0*groups[rs0][trElt]
nR1 = rc1*groups[rs1][trElt]
nP0 = pc0*groups[ps0][trElt]
nP1 = pc1*groups[ps1][trElt]
if (pick == 0):
Redges.append(Edge(rs0,ps0,[[r,min(nR0,nP0)]]))
if (nP0<nR0):
Redges.append(Edge(rs0,ps1,[[r,nR0-nP0]]))
if (nR0<nP0):
Redges.append(Edge(rs1,ps0,[[r,nP0-nR0]]))
Redges.append(Edge(rs1,ps1,[[r,min(nR1,nP1)]]))
else:
Redges.append(Edge(rs0,ps1,[[r,min(nR0,nP1)]]))
if (nP1<nR0):
Redges.append(Edge(rs0,ps0,[[r,nR0-nP1]]))
if (nR0<nP1):
Redges.append(Edge(rs1,ps1,[[r,nP1-nR0]]))
Redges.append(Edge(rs1,ps0,[[r,min(nR1,nP0)]]))
else:
print 'Cannot decompose rxn: ',r,': ',reactionString(r)
# line = '(%3d) %-25s: ' % (r+1,reactionString(r))
# for edge in Redges:
# [rxn,c] = edge.rateWeightList[0]
# line = line + '%5s==>%-5s (%d) ' % (edge.sp1,edge.sp2,c)
# print line
for edge in Redges:
edges.append(edge)
# Uniqueify edges
edgesNew = []
while (len(edges)!=0):
edgesNew.append(edges[0])
del edges[0]
N = len(edges)
i = 0
while (i < N):
e = edgesNew[len(edgesNew)-1]
e1 = edges[i]
sgn = e.equivSign(e1)
if (sgn!=0):
e.combine(e1,sgn)
del edges[i]
N = len(edges)
else:
i = i+1
return edgesNew
if __name__ == '__main__':
print 'Need to make a test for these'