Skip to content

Commit ffa0ca1

Browse files
committed
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 a3a67a1 commit ffa0ca1

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
@@ -309,100 +309,105 @@ def to_cantera(self, species_list=None, use_chemkin_identifier=False):
309309
if self.specific_collider: # add a specific collider if exists
310310
ct_collider[self.specific_collider.to_chemkin() if use_chemkin_identifier else self.specific_collider.label] = 1
311311

312-
if self.kinetics:
313-
# Create the Cantera reaction object,
314-
# with the correct type of kinetics object
315-
# but don't actually set its kinetics (we do that at the end)
316-
if isinstance(self.kinetics, Arrhenius):
317-
# Create an Elementary Reaction
318-
if isinstance(self.kinetics, SurfaceArrhenius): # SurfaceArrhenius inherits from Arrhenius
319-
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.InterfaceArrheniusRate())
320-
else:
321-
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.ArrheniusRate())
322-
elif isinstance(self.kinetics, MultiArrhenius):
323-
# Return a list of elementary reactions which are duplicates
324-
ct_reaction = [ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.ArrheniusRate())
325-
for arr in self.kinetics.arrhenius]
312+
if not self.kinetics:
313+
raise Exception('Cantera reaction cannot be created because there was no kinetics.')
326314

327-
elif isinstance(self.kinetics, PDepArrhenius):
328-
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.PlogRate())
315+
# Create the Cantera reaction object,
316+
# with the correct type of kinetics object
317+
# but don't actually set its kinetics (we do that at the end)
318+
if isinstance(self.kinetics, Arrhenius):
319+
# Create an Elementary Reaction
320+
if isinstance(self.kinetics, SurfaceArrhenius): # SurfaceArrhenius inherits from Arrhenius
321+
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.InterfaceArrheniusRate())
322+
else:
323+
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.ArrheniusRate())
324+
elif isinstance(self.kinetics, MultiArrhenius):
325+
# Return a list of elementary reactions which are duplicates
326+
ct_reaction = [ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.ArrheniusRate())
327+
for arr in self.kinetics.arrhenius]
329328

330-
elif isinstance(self.kinetics, MultiPDepArrhenius):
331-
ct_reaction = [ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.PlogRate())
332-
for arr in self.kinetics.arrhenius]
329+
elif isinstance(self.kinetics, PDepArrhenius):
330+
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.PlogRate())
333331

334-
elif isinstance(self.kinetics, Chebyshev):
335-
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.ChebyshevRate())
332+
elif isinstance(self.kinetics, MultiPDepArrhenius):
333+
ct_reaction = [ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.PlogRate())
334+
for arr in self.kinetics.arrhenius]
336335

337-
elif isinstance(self.kinetics, ThirdBody):
338-
if ct_collider is not None:
339-
ct_reaction = ct.ThreeBodyReaction(reactants=ct_reactants, products=ct_products, third_body=ct_collider)
340-
else:
341-
ct_reaction = ct.ThreeBodyReaction(reactants=ct_reactants, products=ct_products)
342-
343-
elif isinstance(self.kinetics, Troe):
344-
if ct_collider is not None:
345-
ct_reaction = ct.FalloffReaction(
346-
reactants=ct_reactants,
347-
products=ct_products,
348-
tbody=ct_collider,
349-
rate=ct.TroeRate()
350-
)
351-
else:
352-
ct_reaction = ct.FalloffReaction(
353-
reactants=ct_reactants,
354-
products=ct_products,
355-
rate=ct.TroeRate()
356-
)
357-
358-
elif isinstance(self.kinetics, Lindemann):
359-
if ct_collider is not None:
360-
ct_reaction = ct.FalloffReaction(
361-
reactants=ct_reactants,
362-
products=ct_products,
363-
tbody=ct_collider,
364-
rate=ct.LindemannRate()
365-
)
366-
else:
367-
ct_reaction = ct.FalloffReaction(
368-
reactants=ct_reactants,
369-
products=ct_products,
370-
rate=ct.LindemannRate()
371-
)
372-
373-
elif isinstance(self.kinetics, SurfaceArrhenius):
374-
ct_reaction = ct.InterfaceReaction(reactants=ct_reactants,
375-
products=ct_products,
376-
rate=ct.InterfaceArrheniusRate())
377-
378-
elif isinstance(self.kinetics, StickingCoefficient):
379-
ct_reaction = ct.Reaction(reactants=ct_reactants,
380-
products=ct_products,
381-
rate=ct.StickingArrheniusRate())
336+
elif isinstance(self.kinetics, Chebyshev):
337+
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.ChebyshevRate())
382338

339+
elif isinstance(self.kinetics, ThirdBody):
340+
if ct_collider is not None:
341+
ct_reaction = ct.ThreeBodyReaction(reactants=ct_reactants, products=ct_products, third_body=ct_collider)
383342
else:
384-
raise NotImplementedError(f"Unable to set cantera kinetics for {self.kinetics}")
385-
386-
# Set reversibility, duplicate, and ID attributes
387-
if isinstance(ct_reaction, list):
388-
for rxn in ct_reaction:
389-
rxn.reversible = self.reversible
390-
# Set the duplicate flag to true since this reaction comes from multiarrhenius or multipdeparrhenius
391-
rxn.duplicate = True
392-
# Set the ID flag to the original rmg index
393-
rxn.ID = str(self.index)
343+
ct_reaction = ct.ThreeBodyReaction(reactants=ct_reactants, products=ct_products)
344+
345+
elif isinstance(self.kinetics, Troe):
346+
if ct_collider is not None:
347+
ct_reaction = ct.FalloffReaction(
348+
reactants=ct_reactants,
349+
products=ct_products,
350+
tbody=ct_collider,
351+
rate=ct.TroeRate()
352+
)
394353
else:
395-
ct_reaction.reversible = self.reversible
396-
ct_reaction.duplicate = self.duplicate
397-
ct_reaction.ID = str(self.index)
398-
399-
# Now we set the kinetics.
400-
self.kinetics.set_cantera_kinetics(ct_reaction, species_list)
354+
ct_reaction = ct.FalloffReaction(
355+
reactants=ct_reactants,
356+
products=ct_products,
357+
rate=ct.TroeRate()
358+
)
359+
360+
elif isinstance(self.kinetics, Lindemann):
361+
if ct_collider is not None:
362+
ct_reaction = ct.FalloffReaction(
363+
reactants=ct_reactants,
364+
products=ct_products,
365+
tbody=ct_collider,
366+
rate=ct.LindemannRate()
367+
)
368+
else:
369+
ct_reaction = ct.FalloffReaction(
370+
reactants=ct_reactants,
371+
products=ct_products,
372+
rate=ct.LindemannRate()
373+
)
374+
375+
elif isinstance(self.kinetics, SurfaceArrhenius):
376+
ct_reaction = ct.InterfaceReaction(
377+
reactants=ct_reactants,
378+
products=ct_products,
379+
rate=ct.InterfaceArrheniusRate()
380+
)
401381

402-
return ct_reaction
382+
elif isinstance(self.kinetics, StickingCoefficient):
383+
ct_reaction = ct.Reaction(
384+
reactants=ct_reactants,
385+
products=ct_products,
386+
rate=ct.StickingArrheniusRate()
387+
)
403388

404389
else:
405-
raise Exception('Cantera reaction cannot be created because there was no kinetics.')
390+
raise NotImplementedError(f"Unable to set cantera kinetics for {self.kinetics}")
391+
392+
# Set reversibility, duplicate, and ID attributes
393+
if isinstance(ct_reaction, list):
394+
for rxn in ct_reaction:
395+
rxn.reversible = self.reversible
396+
# Set the duplicate flag to true since this reaction comes from multiarrhenius or multipdeparrhenius
397+
rxn.duplicate = True
398+
# Set the ID flag to the original rmg index
399+
rxn.ID = str(self.index)
400+
else:
401+
ct_reaction.reversible = self.reversible
402+
ct_reaction.duplicate = self.duplicate
403+
ct_reaction.ID = str(self.index)
404+
405+
# Now we set the kinetics.
406+
self.kinetics.set_cantera_kinetics(ct_reaction, species_list)
407+
408+
return ct_reaction
409+
410+
406411

407412
def get_url(self):
408413
"""

0 commit comments

Comments
 (0)