Skip to content

Commit 76fde71

Browse files
rwestJacksonBurns
authored andcommitted
Simple refactor in Reaction.to_cantera()
By putting the "if not self.kinetics check first" and raising the exception early, instead of having that at the end in the else block, the rest of the code doesn't need to be inside the "if" block.
1 parent 0a49e16 commit 76fde71

File tree

1 file changed

+89
-84
lines changed

1 file changed

+89
-84
lines changed

rmgpy/reaction.py

Lines changed: 89 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -319,100 +319,105 @@ def to_cantera(self, species_list=None, use_chemkin_identifier=False):
319319
if self.specific_collider: # add a specific collider if exists
320320
ct_collider[self.specific_collider.to_chemkin() if use_chemkin_identifier else self.specific_collider.label] = 1
321321

322-
if self.kinetics:
323-
# Create the Cantera reaction object,
324-
# with the correct type of kinetics object
325-
# but don't actually set its kinetics (we do that at the end)
326-
if isinstance(self.kinetics, Arrhenius):
327-
# Create an Elementary Reaction
328-
if isinstance(self.kinetics, SurfaceArrhenius): # SurfaceArrhenius inherits from Arrhenius
329-
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.InterfaceArrheniusRate())
330-
else:
331-
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.ArrheniusRate())
332-
elif isinstance(self.kinetics, MultiArrhenius):
333-
# Return a list of elementary reactions which are duplicates
334-
ct_reaction = [ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.ArrheniusRate())
335-
for arr in self.kinetics.arrhenius]
322+
if not self.kinetics:
323+
raise Exception('Cantera reaction cannot be created because there was no kinetics.')
336324

337-
elif isinstance(self.kinetics, PDepArrhenius):
338-
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.PlogRate())
325+
# Create the Cantera reaction object,
326+
# with the correct type of kinetics object
327+
# but don't actually set its kinetics (we do that at the end)
328+
if isinstance(self.kinetics, Arrhenius):
329+
# Create an Elementary Reaction
330+
if isinstance(self.kinetics, SurfaceArrhenius): # SurfaceArrhenius inherits from Arrhenius
331+
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.InterfaceArrheniusRate())
332+
else:
333+
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.ArrheniusRate())
334+
elif isinstance(self.kinetics, MultiArrhenius):
335+
# Return a list of elementary reactions which are duplicates
336+
ct_reaction = [ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.ArrheniusRate())
337+
for arr in self.kinetics.arrhenius]
339338

340-
elif isinstance(self.kinetics, MultiPDepArrhenius):
341-
ct_reaction = [ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.PlogRate())
342-
for arr in self.kinetics.arrhenius]
339+
elif isinstance(self.kinetics, PDepArrhenius):
340+
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.PlogRate())
343341

344-
elif isinstance(self.kinetics, Chebyshev):
345-
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.ChebyshevRate())
342+
elif isinstance(self.kinetics, MultiPDepArrhenius):
343+
ct_reaction = [ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.PlogRate())
344+
for arr in self.kinetics.arrhenius]
346345

347-
elif isinstance(self.kinetics, ThirdBody):
348-
if ct_collider is not None:
349-
ct_reaction = ct.ThreeBodyReaction(reactants=ct_reactants, products=ct_products, third_body=ct_collider)
350-
else:
351-
ct_reaction = ct.ThreeBodyReaction(reactants=ct_reactants, products=ct_products)
352-
353-
elif isinstance(self.kinetics, Troe):
354-
if ct_collider is not None:
355-
ct_reaction = ct.FalloffReaction(
356-
reactants=ct_reactants,
357-
products=ct_products,
358-
tbody=ct_collider,
359-
rate=ct.TroeRate()
360-
)
361-
else:
362-
ct_reaction = ct.FalloffReaction(
363-
reactants=ct_reactants,
364-
products=ct_products,
365-
rate=ct.TroeRate()
366-
)
367-
368-
elif isinstance(self.kinetics, Lindemann):
369-
if ct_collider is not None:
370-
ct_reaction = ct.FalloffReaction(
371-
reactants=ct_reactants,
372-
products=ct_products,
373-
tbody=ct_collider,
374-
rate=ct.LindemannRate()
375-
)
376-
else:
377-
ct_reaction = ct.FalloffReaction(
378-
reactants=ct_reactants,
379-
products=ct_products,
380-
rate=ct.LindemannRate()
381-
)
382-
383-
elif isinstance(self.kinetics, SurfaceArrhenius):
384-
ct_reaction = ct.InterfaceReaction(reactants=ct_reactants,
385-
products=ct_products,
386-
rate=ct.InterfaceArrheniusRate())
387-
388-
elif isinstance(self.kinetics, StickingCoefficient):
389-
ct_reaction = ct.Reaction(reactants=ct_reactants,
390-
products=ct_products,
391-
rate=ct.StickingArrheniusRate())
346+
elif isinstance(self.kinetics, Chebyshev):
347+
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.ChebyshevRate())
392348

349+
elif isinstance(self.kinetics, ThirdBody):
350+
if ct_collider is not None:
351+
ct_reaction = ct.ThreeBodyReaction(reactants=ct_reactants, products=ct_products, third_body=ct_collider)
393352
else:
394-
raise NotImplementedError(f"Unable to set cantera kinetics for {self.kinetics}")
395-
396-
# Set reversibility, duplicate, and ID attributes
397-
if isinstance(ct_reaction, list):
398-
for rxn in ct_reaction:
399-
rxn.reversible = self.reversible
400-
# Set the duplicate flag to true since this reaction comes from multiarrhenius or multipdeparrhenius
401-
rxn.duplicate = True
402-
# Set the ID flag to the original rmg index
403-
rxn.ID = str(self.index)
353+
ct_reaction = ct.ThreeBodyReaction(reactants=ct_reactants, products=ct_products)
354+
355+
elif isinstance(self.kinetics, Troe):
356+
if ct_collider is not None:
357+
ct_reaction = ct.FalloffReaction(
358+
reactants=ct_reactants,
359+
products=ct_products,
360+
tbody=ct_collider,
361+
rate=ct.TroeRate()
362+
)
404363
else:
405-
ct_reaction.reversible = self.reversible
406-
ct_reaction.duplicate = self.duplicate
407-
ct_reaction.ID = str(self.index)
408-
409-
# Now we set the kinetics.
410-
self.kinetics.set_cantera_kinetics(ct_reaction, species_list)
364+
ct_reaction = ct.FalloffReaction(
365+
reactants=ct_reactants,
366+
products=ct_products,
367+
rate=ct.TroeRate()
368+
)
369+
370+
elif isinstance(self.kinetics, Lindemann):
371+
if ct_collider is not None:
372+
ct_reaction = ct.FalloffReaction(
373+
reactants=ct_reactants,
374+
products=ct_products,
375+
tbody=ct_collider,
376+
rate=ct.LindemannRate()
377+
)
378+
else:
379+
ct_reaction = ct.FalloffReaction(
380+
reactants=ct_reactants,
381+
products=ct_products,
382+
rate=ct.LindemannRate()
383+
)
384+
385+
elif isinstance(self.kinetics, SurfaceArrhenius):
386+
ct_reaction = ct.InterfaceReaction(
387+
reactants=ct_reactants,
388+
products=ct_products,
389+
rate=ct.InterfaceArrheniusRate()
390+
)
411391

412-
return ct_reaction
392+
elif isinstance(self.kinetics, StickingCoefficient):
393+
ct_reaction = ct.Reaction(
394+
reactants=ct_reactants,
395+
products=ct_products,
396+
rate=ct.StickingArrheniusRate()
397+
)
413398

414399
else:
415-
raise Exception('Cantera reaction cannot be created because there was no kinetics.')
400+
raise NotImplementedError(f"Unable to set cantera kinetics for {self.kinetics}")
401+
402+
# Set reversibility, duplicate, and ID attributes
403+
if isinstance(ct_reaction, list):
404+
for rxn in ct_reaction:
405+
rxn.reversible = self.reversible
406+
# Set the duplicate flag to true since this reaction comes from multiarrhenius or multipdeparrhenius
407+
rxn.duplicate = True
408+
# Set the ID flag to the original rmg index
409+
rxn.ID = str(self.index)
410+
else:
411+
ct_reaction.reversible = self.reversible
412+
ct_reaction.duplicate = self.duplicate
413+
ct_reaction.ID = str(self.index)
414+
415+
# Now we set the kinetics.
416+
self.kinetics.set_cantera_kinetics(ct_reaction, species_list)
417+
418+
return ct_reaction
419+
420+
416421

417422
def get_url(self):
418423
"""

0 commit comments

Comments
 (0)