Skip to content

Commit

Permalink
Merge pull request #727 from StochSS/multi-event-test
Browse files Browse the repository at this point in the history
added unit test for hybrid solvers based on multifiring event notebook
  • Loading branch information
BryanRumsey authored Feb 18, 2022
2 parents 7d3648d + 1998505 commit e26e39c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
47 changes: 46 additions & 1 deletion test/example_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def __init__(self, parameter_values=None):

class Dimerization(Model):
def __init__(self, parameter_values=None):
# First call the gillespy2.Model initializer.
# First call the Model initializer.
Model.__init__(self, name="Dimerization")

# Define parameters for the rates of creation and dissociation.
Expand Down Expand Up @@ -525,6 +525,51 @@ def __init__(self, parameter_values=None):
# Timespan
self.timespan(np.arange(0, 20, 0.05))

class MultiFiringEvent(Model):
"""
This is a simple example for mass-action degradation of species S. We will add two events
to demonstrate the usage of events. The first event will assign the value '0' to our species
once time passes 20, and the second event will be triggered once time crosses 30, assigning
a value of "100" to our species and changing the value of our degradation rate parameter
"k1" from .01 to .1, causing the species to decay more quickly.
"""

def __init__(self, parameter_values=None):

# Initialize the model.
Model.__init__(self, name="Example")

# Species
S = Species(name='Sp', initial_value=100, mode='discrete')
# ev_time1 = Species(name='ev_time1', initial_value=20)
# ev_time2 = Species(name='ev_time2', initial_value=30)
self.add_species([S])

# Parameters
k1 = Parameter(name='k1', expression=0.01)
ev_time1 = Parameter(name='ev_time1', expression=20)
ev_time2 = Parameter(name='ev_time2', expression=30)
self.add_parameter([k1, ev_time1, ev_time2])

# Events
et = EventTrigger(expression='t>ev_time1')
ea = EventAssignment(variable=S, expression='0')
ea1 = EventAssignment(variable=ev_time1, expression='ev_time1+15')
e = Event(name='event1', trigger=et, assignments=[ea, ea1])

et2 = EventTrigger(expression='t>ev_time2', persistent=True)
ea2 = EventAssignment(variable=S, expression='100')
ea3 = EventAssignment(variable=k1, expression='.1')
ea4 = EventAssignment(variable=ev_time2, expression='ev_time2+15')
e2 = Event(name='event2', trigger=et2, assignments=[ea2, ea3, ea4])
self.add_event([e, e2])

#Reactions
r = Reaction(name='R', reactants={S:1}, products={}, rate=k1) #Multiple reactions
self.add_reaction([r])

self.timespan(np.linspace(0, 60, 181))

__all__ = ['Trichloroethylene', 'LacOperon', 'Schlogl', 'MichaelisMenten',
'ToggleSwitch', 'Example', 'Tyson2StateOscillator', 'Oregonator',
'VilarOscillator', 'Dimerization', 'RobustModel']
14 changes: 12 additions & 2 deletions test/test_hybrid_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

import unittest
import numpy as np
import gillespy2
from gillespy2.core.gillespyError import *
from example_models import Example, ExampleNoTspan
from example_models import Example, ExampleNoTspan, MultiFiringEvent
from gillespy2 import TauHybridSolver


Expand Down Expand Up @@ -238,6 +237,17 @@ def test_continuous_state_values(self):
self.assertGreater(result["S1"][-1], 0.0,
"Reaction never fired; indicates that continuous species is being truncated")

def test_multi_firing_event(self):
model = MultiFiringEvent()
for solver in self.solvers:
with self.subTest(solver=solver.name):
res = model.run(solver=solver, seed=1)
self.assertNotEqual(res['Sp'][45], 0)
self.assertEqual(res['Sp'][75], 0)
self.assertNotEqual(res['Sp'][96], 0)
self.assertEqual(res['Sp'][120], 0)
self.assertNotEqual(res['Sp'][144], 0)
self.assertEqual(res['Sp'][165], 0)

if __name__ == '__main__':
unittest.main()

0 comments on commit e26e39c

Please sign in to comment.