Skip to content

Commit aa714a5

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 aa714a5

File tree

1 file changed

+84
-83
lines changed

1 file changed

+84
-83
lines changed

rmgpy/reaction.py

Lines changed: 84 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -309,100 +309,101 @@ 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)
342+
else:
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+
)
383353
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)
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+
)
394368
else:
395-
ct_reaction.reversible = self.reversible
396-
ct_reaction.duplicate = self.duplicate
397-
ct_reaction.ID = str(self.index)
369+
ct_reaction = ct.FalloffReaction(
370+
reactants=ct_reactants,
371+
products=ct_products,
372+
rate=ct.LindemannRate()
373+
)
398374

399-
# Now we set the kinetics.
400-
self.kinetics.set_cantera_kinetics(ct_reaction, species_list)
375+
elif isinstance(self.kinetics, SurfaceArrhenius):
376+
ct_reaction = ct.InterfaceReaction(reactants=ct_reactants,
377+
products=ct_products,
378+
rate=ct.InterfaceArrheniusRate())
401379

402-
return ct_reaction
380+
elif isinstance(self.kinetics, StickingCoefficient):
381+
ct_reaction = ct.Reaction(reactants=ct_reactants,
382+
products=ct_products,
383+
rate=ct.StickingArrheniusRate())
403384

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

407408
def get_url(self):
408409
"""

0 commit comments

Comments
 (0)