4.4. Making the Model Switchable Between Algorithms

Fortunately, at least as far as the model has only elementary reactions, switching between these stochastic and deterministic algorithms is just to switch between NRStepper/GillespieProcess pair and ODE45Stepper/MassActionFluxProcess pair of classnames. Both Process classes takes the same property 'k' with the same unit.

Some use of empy macros makes the model generic. In the following example, setting the Python variable TYPE to ODE makes it run in deterministic differential equation mode, and setting TYPE to NR turns it stochastic.

Example 4-3. simple-switchable.em


@{ALGORITHM='ODE'}

@{
if ALGORITHM == 'ODE':
    STEPPER='ODE45Stepper'
    PROCESS='MassActionFluxProcess'
elif ALGORITHM == 'NR':
    STEPPER='NRStepper'
    PROCESS='GillespieProcess'
else:
    raise 'unknown algorithm: %s' % ALGORITHM
}

Stepper @(STEPPER)( STEPPER1 )
{
    # no property
}

System System( / )
{
    StepperID       STEPPER1;

    Variable Variable( SIZE ) { Value 1e-15; }

    Variable Variable( S1 )
    {
        Value   1000;
    }
        
    Variable Variable( S2 )
    {
        Value   0;
    }
        
    Process @(PROCESS)( P1 )
    {
        VariableReferenceList   [ S0 :.:S1 -1 ]
                                [ P0 :.:S2 1 ];
        k       1.0;
    }

    Process @(PROCESS)( P2 )
    {
        VariableReferenceList   [ S0 :.:S2 -1 ]
                                [ P0 :.:S1 1 ];
        k       1.0;
    }
}