For Loops — MobsPy 2.2 documentation

# For Loops

Here we discuss the proper way of using MobsPy to perform queries inside for loops.

We start by giving an example of what not to do. Here we wish to define a model that cycles through different colors.

    [8]:

    from mobspy import *

    Color = BaseSpecies()

    color = ['red', 'yellow', 'blue']
    for c1, c2 in zip(color, color[1:] + [color[0]]):
        Color.c1 >> Color.c2 [1]

    S = Simulation(Color)
    print(S.compile())

    Species
    Color.c1,0
    Color.c2,0

    Mappings
    Color :
    Color.c1
    Color.c2

    Parameters
    volume,1

    Reactions
    reaction_0,{'re': [(1, 'Color.c1')], 'pr': [(1, 'Color.c2')], 'kin': 'Color.c1 * 1'}
    reaction_1,{'re': [(1, 'Color.c1')], 'pr': [(1, 'Color.c2')], 'kin': 'Color.c1 * 1'}
    reaction_2,{'re': [(1, 'Color.c1')], 'pr': [(1, 'Color.c2')], 'kin': 'Color.c1 * 1'}

    Compiling model
    WARNING: Automatic data-saving setup failed. Please save manually
    WARNING: The following reaction:
    {'re': [(1, 'Color_dot_c1')], 'pr': [(1, 'Color_dot_c2')], 'kin': 'Color_dot_c1 * 1'}
    Is doubled. Was that intentional?

    WARNING: The following reaction:
    {'re': [(1, 'Color_dot_c1')], 'pr': [(1, 'Color_dot_c2')], 'kin': 'Color_dot_c1 * 1'}
    Is doubled. Was that intentional?

    WARNING: The following reaction:
    {'re': [(1, 'Color_dot_c1')], 'pr': [(1, 'Color_dot_c2')], 'kin': 'Color_dot_c1 * 1'}
    Is doubled. Was that intentional?

    WARNING: The following reaction:
    {'re': [(1, 'Color_dot_c1')], 'pr': [(1, 'Color_dot_c2')], 'kin': 'Color_dot_c1 * 1'}
    Is doubled. Was that intentional?

    WARNING: The following reaction:
    {'re': [(1, 'Color_dot_c1')], 'pr': [(1, 'Color_dot_c2')], 'kin': 'Color_dot_c1 * 1'}
    Is doubled. Was that intentional?

    WARNING: The following reaction:
    {'re': [(1, 'Color_dot_c1')], 'pr': [(1, 'Color_dot_c2')], 'kin': 'Color_dot_c1 * 1'}
    Is doubled. Was that intentional?

Since Python resolves Color.c1 by adding the characteristic c1 to Color, this loop adds the same reaction three times to the Color meta-species. Furthermore, the characteristics added are wrong. We wish to add the actual colors, not c1 and c2, which have been used for looping over the colors list.

To perform a query over the value of a stored variable and not it’s name, one can use the .c() method. The corrected version of the model follows:

    [9]:

    Color = BaseSpecies()

    color = ['red', 'yellow', 'blue']
    for c1, c2 in zip(color, color[1:] + [color[0]]):
        Color.c(c1) >> Color.c(c2) [1]

    S = Simulation(Color)
    print(S.compile())

    Species
    Color.blue,0
    Color.red,0
    Color.yellow,0

    Mappings
    Color :
    Color.blue
    Color.red
    Color.yellow

    Parameters
    volume,1

    Reactions
    reaction_0,{'re': [(1, 'Color.blue')], 'pr': [(1, 'Color.red')], 'kin': 'Color.blue * 1'}
    reaction_1,{'re': [(1, 'Color.red')], 'pr': [(1, 'Color.yellow')], 'kin': 'Color.red * 1'}
    reaction_2,{'re': [(1, 'Color.yellow')], 'pr': [(1, 'Color.blue')], 'kin': 'Color.yellow * 1'}

    Compiling model
    WARNING: Automatic data-saving setup failed. Please save manually

    [ ]: