Skip to content

Commit e3e187d

Browse files
KEHANGconnie
authored andcommitted
Changed toleranceKeepInEdge, which was initially a flux, into a ratio of edge rate against characteristic rate!
A unitless tolerance makes more sense than flux.
1 parent 69e655e commit e3e187d

3 files changed

Lines changed: 35 additions & 15 deletions

File tree

rmgpy/rmg/model.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ def addSpeciesToEdge(self, spec):
10431043
"""
10441044
self.edge.species.append(spec)
10451045

1046-
def prune(self, reactionSystems, fluxToleranceKeepInEdge, maximumEdgeSpecies):
1046+
def prune(self, reactionSystems, toleranceKeepInEdge, maximumEdgeSpecies):
10471047
"""
10481048
Remove species from the model edge based on the simulation results from
10491049
the list of `reactionSystems`.
@@ -1062,55 +1062,57 @@ def prune(self, reactionSystems, fluxToleranceKeepInEdge, maximumEdgeSpecies):
10621062

10631063
# Get the maximum species rates (and network leak rates)
10641064
# across all reaction systems
1065-
maxEdgeSpeciesRates = numpy.zeros((numEdgeSpecies), numpy.float64)
1065+
maxEdgeSpeciesRateRatios = numpy.zeros((numEdgeSpecies), numpy.float64)
10661066
for reactionSystem in reactionSystems:
10671067
for i in range(numEdgeSpecies):
1068-
rate = reactionSystem.maxEdgeSpeciesRates[i]
1069-
if maxEdgeSpeciesRates[i] < rate:
1070-
maxEdgeSpeciesRates[i] = rate
1068+
rateRatio = reactionSystem.maxEdgeSpeciesRateRatios[i]
1069+
if maxEdgeSpeciesRateRatios[i] < rateRatio:
1070+
maxEdgeSpeciesRateRatios[i] = rateRatio
10711071

10721072
for i in range(len(self.networkList)):
10731073
network = self.networkList[i]
1074-
rate = reactionSystem.maxNetworkLeakRates[i]
1074+
rateRatio = reactionSystem.maxNetworkLeakRateRatios[i]
10751075
# Add the fraction of the network leak rate contributed by
10761076
# each unexplored species to that species' rate
10771077
# This is to ensure we have an overestimate of that species flux
10781078
ratios = network.getLeakBranchingRatios(reactionSystem.T.value_si,reactionSystem.P.value_si)
10791079
for spec, frac in ratios.iteritems():
10801080
index = self.edge.species.index(spec)
1081-
maxEdgeSpeciesRates[index] += frac * rate
1081+
maxEdgeSpeciesRateRatios[index] += frac * rateRatio
10821082
# Mark any species that is explored in any partial network as ineligible for pruning
10831083
for spec in network.explored:
10841084
if spec not in ineligibleSpecies:
10851085
ineligibleSpecies.append(spec)
10861086

10871087
# Sort the edge species rates by index
1088-
indices = numpy.argsort(maxEdgeSpeciesRates)
1089-
1088+
indices = numpy.argsort(maxEdgeSpeciesRateRatios)
10901089
# Determine which species to prune
10911090
speciesToPrune = []
10921091
pruneDueToRateCounter = 0
10931092
for index in indices:
10941093
# Remove the species with rates below the pruning tolerance from the model edge
1095-
if maxEdgeSpeciesRates[index] < fluxToleranceKeepInEdge and self.edge.species[index] not in ineligibleSpecies:
1094+
if maxEdgeSpeciesRateRatios[index] < toleranceKeepInEdge and self.edge.species[index] not in ineligibleSpecies:
10961095
speciesToPrune.append((index, self.edge.species[index]))
10971096
pruneDueToRateCounter += 1
10981097
# Keep removing species with the lowest rates until we are below the maximum edge species size
10991098
elif numEdgeSpecies - len(speciesToPrune) > maximumEdgeSpecies and self.edge.species[index] not in ineligibleSpecies:
1099+
logging.info('Pruning species {0} to make numEdgeSpecies smaller than maximumEdgeSpecies'.format(self.edge.species[index]))
11001100
speciesToPrune.append((index, self.edge.species[index]))
11011101
else:
1102-
break
1102+
continue
11031103

11041104
# Actually do the pruning
11051105
if pruneDueToRateCounter > 0:
1106-
logging.info('Pruning {0:d} species whose rates did not exceed the minimum threshold of {1:g}'.format(pruneDueToRateCounter, fluxToleranceKeepInEdge))
1106+
logging.info('Pruning {0:d} species whose rate ratios against characteristic rate did not exceed the minimum threshold of {1:g}'.format(pruneDueToRateCounter, toleranceKeepInEdge))
11071107
for index, spec in speciesToPrune[0:pruneDueToRateCounter]:
1108-
logging.debug(' {0:<56} {1:10.4e}'.format(spec, maxEdgeSpeciesRates[index]))
1108+
logging.info('Pruning species {0:<56}'.format(spec))
1109+
logging.debug(' {0:<56} {1:10.4e}'.format(spec, maxEdgeSpeciesRateRatios[index]))
11091110
self.removeSpeciesFromEdge(spec)
11101111
if len(speciesToPrune) - pruneDueToRateCounter > 0:
11111112
logging.info('Pruning {0:d} species to obtain an edge size of {1:d} species'.format(len(speciesToPrune) - pruneDueToRateCounter, maximumEdgeSpecies))
11121113
for index, spec in speciesToPrune[pruneDueToRateCounter:]:
1113-
logging.debug(' {0:<56} {1:10.4e}'.format(spec, maxEdgeSpeciesRates[index]))
1114+
logging.info('Pruning species {0:<56}'.format(spec))
1115+
logging.debug(' {0:<56} {1:10.4e}'.format(spec, maxEdgeSpeciesRateRatios[index]))
11141116
self.removeSpeciesFromEdge(spec)
11151117

11161118
# Delete any networks that became empty as a result of pruning

rmgpy/solver/base.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ cdef class ReactionSystem(DASSL):
4242
cdef public numpy.ndarray maxCoreSpeciesRates
4343
cdef public numpy.ndarray maxEdgeSpeciesRates
4444
cdef public numpy.ndarray maxNetworkLeakRates
45+
cdef public numpy.ndarray maxEdgeSpeciesRateRatios
46+
cdef public numpy.ndarray maxNetworkLeakRateRatios
4547
cdef public numpy.ndarray sensitivityCoefficients
4648

4749
cdef public list termination

rmgpy/solver/base.pyx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ cdef class ReactionSystem(DASSL):
6464
self.maxCoreSpeciesRates = None
6565
self.maxEdgeSpeciesRates = None
6666
self.maxNetworkLeakRates = None
67+
self.maxEdgeSpeciesRateRatios = None
68+
self.maxNetworkLeakRateRatios = None
6769
self.sensitivityCoefficients = None
6870
self.termination = termination or []
6971

@@ -93,6 +95,8 @@ cdef class ReactionSystem(DASSL):
9395
self.maxCoreSpeciesRates = numpy.zeros((numCoreSpecies), numpy.float64)
9496
self.maxEdgeSpeciesRates = numpy.zeros((numEdgeSpecies), numpy.float64)
9597
self.maxNetworkLeakRates = numpy.zeros((numPdepNetworks), numpy.float64)
98+
self.maxEdgeSpeciesRateRatios = numpy.zeros((numEdgeSpecies), numpy.float64)
99+
self.maxNetworkLeakRateRatios = numpy.zeros((numPdepNetworks), numpy.float64)
96100
self.sensitivityCoefficients = numpy.zeros((numCoreSpecies, numCoreReactions), numpy.float64)
97101

98102

@@ -126,7 +130,7 @@ cdef class ReactionSystem(DASSL):
126130
cdef double stepTime, charRate, maxSpeciesRate, maxNetworkRate, prevTime, volume
127131
cdef numpy.ndarray[numpy.float64_t, ndim=1] y0, dydk #: Vector containing the number of moles of each species
128132
cdef numpy.ndarray[numpy.float64_t, ndim=1] coreSpeciesRates, edgeSpeciesRates, networkLeakRates
129-
cdef numpy.ndarray[numpy.float64_t, ndim=1] maxCoreSpeciesRates, maxEdgeSpeciesRates, maxNetworkLeakRates
133+
cdef numpy.ndarray[numpy.float64_t, ndim=1] maxCoreSpeciesRates, maxEdgeSpeciesRates, maxNetworkLeakRates,maxEdgeSpeciesRateRatios, maxNetworkLeakRateRatios
130134
cdef bint terminated
131135
cdef object maxSpecies, maxNetwork
132136
cdef int iteration, i
@@ -158,6 +162,8 @@ cdef class ReactionSystem(DASSL):
158162
maxCoreSpeciesRates = self.maxCoreSpeciesRates
159163
maxEdgeSpeciesRates = self.maxEdgeSpeciesRates
160164
maxNetworkLeakRates = self.maxNetworkLeakRates
165+
maxEdgeSpeciesRateRatios = self.maxEdgeSpeciesRateRatios
166+
maxNetworkLeakRateRatios = self.maxNetworkLeakRateRatios
161167

162168
# Copy the initial conditions to use in evaluating conversions
163169
y0 = self.y.copy()
@@ -220,6 +226,8 @@ cdef class ReactionSystem(DASSL):
220226
coreSpeciesRates = numpy.abs(self.coreSpeciesRates)
221227
edgeSpeciesRates = numpy.abs(self.edgeSpeciesRates)
222228
networkLeakRates = numpy.abs(self.networkLeakRates)
229+
edgeSpeciesRateRatios = numpy.abs(self.edgeSpeciesRates/charRate)
230+
networkLeakRateRatios = numpy.abs(self.networkLeakRates/charRate)
223231

224232
# Update the maximum species rate and maximum network leak rate arrays
225233
for index in range(numCoreSpecies):
@@ -231,6 +239,12 @@ cdef class ReactionSystem(DASSL):
231239
for index in range(numPdepNetworks):
232240
if maxNetworkLeakRates[index] < networkLeakRates[index]:
233241
maxNetworkLeakRates[index] = networkLeakRates[index]
242+
for index in range(numEdgeSpecies):
243+
if maxEdgeSpeciesRateRatios[index] < edgeSpeciesRateRatios[index]:
244+
maxEdgeSpeciesRateRatios[index] = edgeSpeciesRateRatios[index]
245+
for index in range(numPdepNetworks):
246+
if maxNetworkLeakRateRatios[index] < networkLeakRateRatios[index]:
247+
maxNetworkLeakRateRatios[index] = networkLeakRateRatios[index]
234248

235249
# Get the edge species with the highest flux
236250
if numEdgeSpecies > 0:
@@ -313,6 +327,8 @@ cdef class ReactionSystem(DASSL):
313327
self.maxCoreSpeciesRates = maxCoreSpeciesRates
314328
self.maxEdgeSpeciesRates = maxEdgeSpeciesRates
315329
self.maxNetworkLeakRates = maxNetworkLeakRates
330+
self.maxEdgeSpeciesRateRatios = maxEdgeSpeciesRateRatios
331+
self.maxNetworkLeakRateRatios = maxNetworkLeakRateRatios
316332

317333
# Return the invalid object (if the simulation was invalid) or None
318334
# (if the simulation was valid)

0 commit comments

Comments
 (0)