# mehalter/Petri.jl

A Petri net modeling framework for the Julia programming language
Julia
mehalter Compat (#14)
```* Updated compatibility and version bump

* Fix compatibility entries

* rollback```
2
Latest commit 01d9f72 Nov 12, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information. doc Nov 5, 2019 examples Oct 4, 2019 src Nov 1, 2019 test Oct 14, 2019 .gitignore .travis.yml Aug 20, 2019 LICENSE Nov 5, 2019 Project.toml README.md Nov 5, 2019 REQUIRE Aug 20, 2019 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. ```# 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)``` 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. ```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)``` 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. ```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)``` ## 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.