Skip to content

Commit

Permalink
Add balanceSpecies feature
Browse files Browse the repository at this point in the history
If a balance species is set mole fractions are generated thus:  the ranged mole fractions are varied within their ranges according to the algorithm while the others remain constant, then the balance species is adjusted so that all of the mole fractions sum to one.  This causes all mole fractions except the balanceSpecies to remain in their ranges
  • Loading branch information
mjohnson541 committed Apr 29, 2018
1 parent 0f36c3d commit 97798b3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
15 changes: 14 additions & 1 deletion rmgpy/rmg/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def simpleReactor(temperature,
nSimsTerm=6,
terminationConversion=None,
terminationTime=None,
balanceSpecies=None,
sensitivity=None,
sensitivityThreshold=1e-3,
sensitivityTemperature=None,
Expand Down Expand Up @@ -209,7 +210,19 @@ def simpleReactor(temperature,

system = SimpleReactor(T, P, initialMoleFractions, nSimsTerm, termination, sensitiveSpecies, sensitivityThreshold,sensConditions)
rmg.reactionSystems.append(system)


rmg.balanceSpecies = balanceSpecies
if balanceSpecies: #check that the balanceSpecies can't be taken to zero
total = 0.0
for key,item in initialMoleFractions.iteritems():
if key == balanceSpecies:
assert not isinstance(item,list), 'balanceSpecies must not have a defined range'
xbspcs = item
if isinstance(item,list):
total += item[1]-item[0]

if total > xbspcs:
raise ValueError('The sum of the differences in the ranged mole fractions is greater than the mole fraction of the balance species, this would require the balanceSpecies mole fraction to be negative in some cases which is not allowed, either reduce the maximum mole fractions or dont use balanceSpecies')

# Reaction systems
def liquidReactor(temperature,
Expand Down
29 changes: 20 additions & 9 deletions rmgpy/rmg/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def clear(self):

self.modelSettingsList = []
self.simulatorSettingsList = []
self.balanceSpecies = None

self.filterReactions=False
self.unimolecularReact = None
Expand Down Expand Up @@ -560,7 +561,7 @@ def execute(self, **kwargs):
if self.filterReactions:
# Run the reaction system to update threshold and react flags
for index, reactionSystem in enumerate(self.reactionSystems):
self.rmg_memories.append(RMG_Memory(reactionSystem))
self.rmg_memories.append(RMG_Memory(reactionSystem,self.balanceSpecies))
self.rmg_memories[index].generate_cond()
log_conditions(self.rmg_memories,index)
reactionSystem.initializeModel(
Expand All @@ -579,7 +580,7 @@ def execute(self, **kwargs):
rxnSysBimolecularThreshold=reactionSystem.bimolecularThreshold)
else:
for index, reactionSystem in enumerate(self.reactionSystems):
self.rmg_memories.append(RMG_Memory(reactionSystem))
self.rmg_memories.append(RMG_Memory(reactionSystem,self.balanceSpecies))
self.rmg_memories[index].generate_cond()
log_conditions(self.rmg_memories,index)

Expand Down Expand Up @@ -1560,15 +1561,19 @@ class RMG_Memory:
class for remembering RMG simulations
and determining what simulation to run next
"""
def __init__(self,reactionSystem):
def __init__(self,reactionSystem,bspc):
self.Ranges = dict()

if hasattr(reactionSystem,'Trange') and type(reactionSystem.Trange) == list:
Trange = reactionSystem.Trange
self.Ranges['T'] = [T.value_si for T in Trange]
if hasattr(reactionSystem,'Prange') and type(reactionSystem.Prange) == list:
Prange = reactionSystem.Prange
self.Ranges['P'] = [np.log(P.value_si) for P in Prange]
if hasattr(reactionSystem,'initialMoleFractions'):
if bspc:
self.initialMoleFractions = deepcopy(reactionSystem.initialMoleFractions)
self.balanceSpecies = [x for x in self.initialMoleFractions.keys() if x.label == bspc][0] #find the balance species
for key,value in reactionSystem.initialMoleFractions.iteritems():
assert key != 'T' and key != 'P', 'naming a species T or P is forbidden'
if type(value) == list:
Expand All @@ -1583,7 +1588,7 @@ def __init__(self,reactionSystem):
if isinstance(term, TerminationTime):
self.tmax = term.time.value_si

self.normalize = hasattr(reactionSystem,'initialMoleFractions')
self.reactionSystem = reactionSystem
self.conditionList = []
self.scaledConditionList = []
self.ts = []
Expand Down Expand Up @@ -1642,12 +1647,18 @@ def obj(y):
if 'P' in newCond.keys():
newCond['P'] = np.exp(newCond['P'])

if self.normalize:
if hasattr(self,'initialMoleFractions'):
for key in self.initialMoleFractions.keys():
if not isinstance(self.initialMoleFractions[key],list):
newCond[key] = self.initialMoleFractions[key]
total = sum([val for key,val in newCond.iteritems() if key != 'T' and key != 'P'])
for key,val in newCond.iteritems():
if key != 'T' and key != 'P':
newCond[key] = val/total

if self.balanceSpecies is None:
for key,val in newCond.iteritems():
if key != 'T' and key != 'P':
newCond[key] = val/total
else:
newCond[self.balanceSpecies] = self.initialMoleFractions[self.balanceSpecies] + 1.0 - total

self.conditionList.append(newCond)
self.scaledConditionList.append(scaledNewCond)
return
Expand Down

0 comments on commit 97798b3

Please sign in to comment.