diff --git a/spatialpy/core/reaction.py b/spatialpy/core/reaction.py index e8591195..7d5f5dff 100644 --- a/spatialpy/core/reaction.py +++ b/spatialpy/core/reaction.py @@ -100,18 +100,20 @@ def __init__(self, name=None, reactants=None, products=None, propensity_function if reactants is not None: for r in reactants: rtype = type(r).__name__ - if rtype == 'Species': - self.reactants[r.name] = reactants[r] + name = r.name if rtype == 'Species' else r + if name in self.reactants: + self.reactants[name] += reactants[r] else: - self.reactants[r] = reactants[r] - + self.reactants[name] = reactants[r] + if products is not None: for p in products: - rtype = type(p).__name__ - if rtype == 'Species': - self.products[p.name] = products[p] + ptype = type(p).__name__ + name = p.name if ptype == 'Species' else p + if name in self.products: + self.products[name] += products[p] else: - self.products[p] = products[p] + self.products[name] = products[p] if self.marate is not None: rtype = type(self.marate).__name__ @@ -343,7 +345,10 @@ def add_product(self, species, stoichiometry): except TypeError as err: raise ReactionError(f"Failed to validate product. Reason given: {err}") from err - self.products[name] = stoichiometry + if name in self.products: + self.products[name] += stoichiometry + else: + self.products[name] = stoichiometry def add_reactant(self, species, stoichiometry): """ @@ -362,7 +367,10 @@ def add_reactant(self, species, stoichiometry): except TypeError as err: raise ReactionError(f"Failed to validate reactant. Reason given: {err}") from err - self.reactants[name] = stoichiometry + if name in self.reactants: + self.reactants[name] += stoichiometry + else: + self.reactants[name] = stoichiometry if self.massaction and self.type == "mass-action": self._create_mass_action()