Skip to content

Commit

Permalink
Simple refactor in Reaction.to_cantera()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rwest committed Dec 3, 2024
1 parent a3a67a1 commit ffa0ca1
Showing 1 changed file with 89 additions and 84 deletions.
173 changes: 89 additions & 84 deletions rmgpy/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,100 +309,105 @@ def to_cantera(self, species_list=None, use_chemkin_identifier=False):
if self.specific_collider: # add a specific collider if exists
ct_collider[self.specific_collider.to_chemkin() if use_chemkin_identifier else self.specific_collider.label] = 1

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

elif isinstance(self.kinetics, PDepArrhenius):
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.PlogRate())
# Create the Cantera reaction object,
# with the correct type of kinetics object
# but don't actually set its kinetics (we do that at the end)
if isinstance(self.kinetics, Arrhenius):
# Create an Elementary Reaction
if isinstance(self.kinetics, SurfaceArrhenius): # SurfaceArrhenius inherits from Arrhenius
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.InterfaceArrheniusRate())
else:
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.ArrheniusRate())
elif isinstance(self.kinetics, MultiArrhenius):
# Return a list of elementary reactions which are duplicates
ct_reaction = [ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.ArrheniusRate())
for arr in self.kinetics.arrhenius]

elif isinstance(self.kinetics, MultiPDepArrhenius):
ct_reaction = [ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.PlogRate())
for arr in self.kinetics.arrhenius]
elif isinstance(self.kinetics, PDepArrhenius):
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.PlogRate())

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

elif isinstance(self.kinetics, ThirdBody):
if ct_collider is not None:
ct_reaction = ct.ThreeBodyReaction(reactants=ct_reactants, products=ct_products, third_body=ct_collider)
else:
ct_reaction = ct.ThreeBodyReaction(reactants=ct_reactants, products=ct_products)

elif isinstance(self.kinetics, Troe):
if ct_collider is not None:
ct_reaction = ct.FalloffReaction(
reactants=ct_reactants,
products=ct_products,
tbody=ct_collider,
rate=ct.TroeRate()
)
else:
ct_reaction = ct.FalloffReaction(
reactants=ct_reactants,
products=ct_products,
rate=ct.TroeRate()
)

elif isinstance(self.kinetics, Lindemann):
if ct_collider is not None:
ct_reaction = ct.FalloffReaction(
reactants=ct_reactants,
products=ct_products,
tbody=ct_collider,
rate=ct.LindemannRate()
)
else:
ct_reaction = ct.FalloffReaction(
reactants=ct_reactants,
products=ct_products,
rate=ct.LindemannRate()
)

elif isinstance(self.kinetics, SurfaceArrhenius):
ct_reaction = ct.InterfaceReaction(reactants=ct_reactants,
products=ct_products,
rate=ct.InterfaceArrheniusRate())

elif isinstance(self.kinetics, StickingCoefficient):
ct_reaction = ct.Reaction(reactants=ct_reactants,
products=ct_products,
rate=ct.StickingArrheniusRate())
elif isinstance(self.kinetics, Chebyshev):
ct_reaction = ct.Reaction(reactants=ct_reactants, products=ct_products, rate=ct.ChebyshevRate())

elif isinstance(self.kinetics, ThirdBody):
if ct_collider is not None:
ct_reaction = ct.ThreeBodyReaction(reactants=ct_reactants, products=ct_products, third_body=ct_collider)
else:
raise NotImplementedError(f"Unable to set cantera kinetics for {self.kinetics}")

# Set reversibility, duplicate, and ID attributes
if isinstance(ct_reaction, list):
for rxn in ct_reaction:
rxn.reversible = self.reversible
# Set the duplicate flag to true since this reaction comes from multiarrhenius or multipdeparrhenius
rxn.duplicate = True
# Set the ID flag to the original rmg index
rxn.ID = str(self.index)
ct_reaction = ct.ThreeBodyReaction(reactants=ct_reactants, products=ct_products)

elif isinstance(self.kinetics, Troe):
if ct_collider is not None:
ct_reaction = ct.FalloffReaction(
reactants=ct_reactants,
products=ct_products,
tbody=ct_collider,
rate=ct.TroeRate()
)
else:
ct_reaction.reversible = self.reversible
ct_reaction.duplicate = self.duplicate
ct_reaction.ID = str(self.index)

# Now we set the kinetics.
self.kinetics.set_cantera_kinetics(ct_reaction, species_list)
ct_reaction = ct.FalloffReaction(
reactants=ct_reactants,
products=ct_products,
rate=ct.TroeRate()
)

elif isinstance(self.kinetics, Lindemann):
if ct_collider is not None:
ct_reaction = ct.FalloffReaction(
reactants=ct_reactants,
products=ct_products,
tbody=ct_collider,
rate=ct.LindemannRate()
)
else:
ct_reaction = ct.FalloffReaction(
reactants=ct_reactants,
products=ct_products,
rate=ct.LindemannRate()
)

elif isinstance(self.kinetics, SurfaceArrhenius):
ct_reaction = ct.InterfaceReaction(
reactants=ct_reactants,
products=ct_products,
rate=ct.InterfaceArrheniusRate()
)

return ct_reaction
elif isinstance(self.kinetics, StickingCoefficient):
ct_reaction = ct.Reaction(
reactants=ct_reactants,
products=ct_products,
rate=ct.StickingArrheniusRate()
)

else:
raise Exception('Cantera reaction cannot be created because there was no kinetics.')
raise NotImplementedError(f"Unable to set cantera kinetics for {self.kinetics}")

# Set reversibility, duplicate, and ID attributes
if isinstance(ct_reaction, list):
for rxn in ct_reaction:
rxn.reversible = self.reversible
# Set the duplicate flag to true since this reaction comes from multiarrhenius or multipdeparrhenius
rxn.duplicate = True
# Set the ID flag to the original rmg index
rxn.ID = str(self.index)
else:
ct_reaction.reversible = self.reversible
ct_reaction.duplicate = self.duplicate
ct_reaction.ID = str(self.index)

# Now we set the kinetics.
self.kinetics.set_cantera_kinetics(ct_reaction, species_list)

return ct_reaction



def get_url(self):
"""
Expand Down

0 comments on commit ffa0ca1

Please sign in to comment.