Skip to content
A Petri net modeling framework for the Julia programming language
Julia
Branch: master
Clone or download
mehalter Compat (#14)
* Updated compatibility and version bump

* Changed license

* added staticarray

* Fix compatibility entries

* rollback
2
Latest commit 01d9f72 Nov 12, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
doc Logo (#12) Nov 5, 2019
examples Avoiding Eval using GeneralizedGenerated.jl (#2) Oct 4, 2019
src Remove openmodel (#8) Nov 1, 2019
test add equality test (#5) Oct 14, 2019
.gitignore
.travis.yml initial code push with basic petri net functionaity Aug 20, 2019
LICENSE Updated compatibility and version bump (#10) Nov 5, 2019
Project.toml
README.md Logo (#12) Nov 5, 2019
REQUIRE initial code push with basic petri net functionaity Aug 20, 2019

README.md

Petri.jl

A Petri net modeling framework for the Julia programming language.

This package uses the ModelingToolkit framework for building embedded DSLs for mathematical computing. We represent Petri nets with ModelingToolkit.Operation expressions and then generate code for simulating these networks.

Examples

We need to include our dependencies. Modeling Toolkit and Petri are required to build the models. LabelledArrays and OrdinaryDiffEq are required for simulating the network with and ordinary differential equation.

using ModelingToolkit
using Petri
import Petri: fluxes, odefunc
using LabelledArrays
using OrdinaryDiffEq
import OrdinaryDiffEq: solve
using Plots

N(x) = sum(x)
@variables S,E,I,R, β,γ,μ

The SIR model represents the epidemiological dynamics of an infectious disease that causes immunity in its victims. There are three states: Suceptible ,Infected, Recovered. These states interact through two transitions. Infection has the form S+I -> 2I where a susceptible person meets an infected person and results in two infected people. The second transition is recovery I -> R where an infected person recovers spontaneously.

The SIR model system shown as a Petri net with ODE formulas

# define the structure of the model
sir = Petri.Model([S,I,R],[(S+I, 2I), (I,R)])

# define the initial conditions
u0 = @LArray [100.0, 1, 0] (:S, :I, :R)

# define the parameters of the model, each rate corresponds to a transition
p = @LArray [0.35, 0.05] (:μ, :β)

# generate an expression for the right hand side of the ODE
fex = odefunc(sir, :sir)

# evaluate the expression to create a runnable function
f = eval(fex)

# this is regular OrdinaryDiffEq problem setup
prob = ODEProblem(f,u0,(0.0,365.0),p)
sol = DifferentialEquations.solve(prob,Tsit5())

# visualize the solution
plt = plot(sol, labels=LabelledArrays.symnames(typeof(sol[end]))|> collect)

A solution to the SIR model system

Petri Nets are a simple language for describing reaction networks, you can make increasingly complex diseases. For example the SEIR model has an Exposed phase where people have the disease, but are not infectious yet.

The SEIR model system shown as a Petri net

seir = Petri.Model([S,E,I,R],[(S+I, E+I), (E,I), (I,R)])
u0 = @LArray [100.0, 1, 0, 0] (:S, :E, :I, :R)
p = @LArray [0.35, 0.05, 0.05] (:μ, :β, :γ)
fex = odefunc(seir, :seir)
f = eval(fex)
prob = ODEProblem(f,u0,(0.0,365.0),p)
sol = DifferentialEquations.solve(prob,Tsit5())
plt = plot(sol, labels=LabelledArrays.symnames(typeof(sol[end]))|> collect)

A solution to the SEIR model system

The previous models have transitory behavior, the infection spreads and then terminates as you end up with no infected people in the population. The following SEIRS model has a non-trivial steady state, because recovered people lose their immunity and become susceptible again.

The SEIRS model system shown as a Petri net

seirs = Petri.Model([S,E,I,R],[(S+I, E+I), (E,I), (I,R), (R,S)])
u0 = @LArray [100.0, 1, 0, 0] (:S, :E, :I, :R)
p = @LArray [0.35, 0.05, 0.07, 0.3] (:μ, :β, :γ, :η)
fex = odefunc(seirs, :seirs)
f = eval(fex)
prob = ODEProblem(f,u0,(0.0,365.0),p)
sol = DifferentialEquations.solve(prob,Tsit5())
plt = plot(sol, labels=LabelledArrays.symnames(typeof(sol[end])) |> collect)

A solution to the SEIRS model system

Goals

Petri makes it easy to build complex reaction networks using a simple DSL. This is related to theDiffeqBiological Reaction DSL, but takes a different implementation approach. Instead of building our framework around symbolic algebra and standard chemical notion, we are working off the Applied Category Theory approach to reaction networks [Baez Pollard, 2017].

There are operations that are easy to do on the Petri.Model like "add a transition from R to S" that require simultaneously changing multiple parts of the algebraic formulation. Applied Category Theory gives a sound theoretical framework for manipulating Petri Nets as a model of chemical reactions. Petri is a Julia package primarily intended to investigate how we can operationalize this theory into practical scientific software.

See SemanticModels.ModelTools for tools that work with Petri net models and manipulating them with higher level APIs based on ACT.

Visualization

You can draw Petri.Model objects with Graphviz using a bipartite graph representation. See the Petri.Graph(m::Petri.Model) function.

Open Petri Nets

An open Petri Net can be used to model a system that interacts with the outside world. For example a chemical reaction that has inflows and outflows of concentration for certain species, can be represented as an OpenModel where the inflows occur on the dom and the outflows on the codom of the OpenModel. This allows you to represent interacting systems from an algebraic perspective. These systems an be combined with composition and combination operators to make complex models out of simple building blocks.

You can’t perform that action at this time.