From 3d1356f31a39986a56565356fdcad569857f311b Mon Sep 17 00:00:00 2001 From: Josh Allen Date: Sat, 11 Feb 2012 16:27:05 -0500 Subject: [PATCH] Added Reaction class to pyrate.reaction module. The Reaction class can be used to represent a chemical reaction and its associated properties, e.g. kinetics model. --- pyrate/reaction.pxd | 14 ++++++++++++++ pyrate/reaction.pyx | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/pyrate/reaction.pxd b/pyrate/reaction.pxd index ce526f7..e3349e4 100644 --- a/pyrate/reaction.pxd +++ b/pyrate/reaction.pxd @@ -26,8 +26,22 @@ # ################################################################################ +from pyrate.kinetics.model cimport KineticsModel + +################################################################################ + cdef class TransitionState: cdef public str label cdef public object statmech cdef double _frequency + +################################################################################ + +cdef class Reaction: + + cdef public list reactants + cdef public list products + cdef public bint reversible + cdef public TransitionState transitionState + cdef public KineticsModel kinetics diff --git a/pyrate/reaction.pyx b/pyrate/reaction.pyx index 56771a5..88cdfe8 100644 --- a/pyrate/reaction.pyx +++ b/pyrate/reaction.pyx @@ -82,3 +82,48 @@ cdef class TransitionState: self._frequency = 0.0 else: self._frequency = float(units.convertFrequency(value, pq.wavenumber)) + +################################################################################ + +cdef class Reaction: + """ + A chemical reaction. The attributes are: + + ======================= ==================================================== + Attribute Description + ======================= ==================================================== + `reactants` The list of reactant species + `products` The list of product species + `kinetics` The kinetics model to use for the reaction + `reversible` ``True`` if the reaction is reversible, ``False`` if not + `transitionState` Information about the transition state + ======================= ==================================================== + + """ + + def __init__(self, reactants=None, products=None, kinetics=None, reversible=True, transitionState=None): + self.reactants = reactants or [] + self.products = products or [] + self.kinetics = kinetics + self.reversible = reversible + self.transitionState = transitionState + + def __str__(self): + """ + Return a string representation of the reaction, in the form 'A + B <=> C + D'. + """ + arrow = ' <=> ' + if not self.reversible: arrow = ' -> ' + return arrow.join([' + '.join([str(s) for s in self.reactants]), ' + '.join([str(s) for s in self.products])]) + + def __repr__(self): + """ + Return a string representation of the reaction. + """ + return 'Reaction(reactants={0!r}, products={1!r}, kinetics={2!r}, reversible={3!r}, transitionState={4!r})'.format(self.reactants, self.products, self.kinetics, self.reversible, self.transitionState) + + def __reduce__(self): + """ + A helper function used when pickling an object. + """ + return (Reaction, (self.reactants, self.products, self.kinetics, self.reversible, self.transitionState))