File: Reaction.py

package info (click to toggle)
cain 1.10%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 29,848 kB
  • sloc: cpp: 49,612; python: 14,988; xml: 11,654; ansic: 3,644; makefile: 129; sh: 2
file content (40 lines) | stat: -rw-r--r-- 1,484 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""Implements the Reaction class."""

from SpeciesReference import SpeciesReference
from math import *

class Reaction:
    """A reaction is comprised of an identifier, name, reactants, products,
    mass action indicator, and propensity function. 
    Member data:
    - self.reactants: List of SpeciesReference.
    - self.products: List of SpeciesReference."""
    
    def __init__(self, model, reactants, products, propensityExpression):
        self.model = model
        # A list of SpeciesReference.
        self.reactants = reactants
        # A list of SpeciesReference.
        self.products = products
        # The string that defines the propensity function.
        self.propensityExpression = propensityExpression

    def propensity(self):
        return self.propensityFunction(self.model)

    def initialize(self):
        self.count = 0
        # Decorate the expression here so that we don't have to do it when
        # the reaction fires. Note that we can't do this in the constructor
        # because the propensity expression may depend on parameters that would
        # not yet have been defined.
        e = self.model.decorate(self.propensityExpression)
        self.propensityFunction = lambda m: eval(e)

    def fire(self):
        self.count += 1
        for x in self.reactants:
            self.model.species[x.species].amount -= x.stoichiometry
        for x in self.products:
            self.model.species[x.species].amount += x.stoichiometry