Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added labelled petri/reaction net compatibility #126

Merged
merged 4 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ makedocs(
"examples/Ross-Macdonald.md"
],
"Threshold Linear Networks" => "TLN.md",
"AlgebraicPetri Integration" => "AlgebraicPetri.md",
"Library Reference" => "api.md"
]
)
Expand Down
27 changes: 27 additions & 0 deletions docs/src/AlgebraicPetri.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# AlgebraicPetri.jl Integration

You can construct a [`ContinuousResourceSharer`](@ref) from an Open Petri Net for any kind of network supported by AlgebraicPetri.jl including:

1. OpenPetriNet
2. OpenLabelledPetriNet
3. OpenLabelledReactionNet

````@example AlgPetri
using AlgebraicPetri
using AlgebraicDynamics
Brusselator = LabelledPetriNet([:A, :B, :D, :E, :X, :Y],
:t1 => (:A => (:X, :A)),
:t2 => ((:X, :X, :Y) => (:X, :X, :X)),
:t3 => ((:B, :X) => (:Y, :D, :B)),
:t4 => (:X => :E)
)
````

You just call the constructor for ContinuousResourceSharer on an Open Petri Net.
The constructor knows where to use labels or integers for the state variables.
It also knows to get parameters from the ReactionNet and enclose them into the dynamics.

````@example AlgPetri
open_bruss = Open([:A, :D], Brusselator, [:A, :B])
rs = ContinuousResourceSharer{Float64}(open_bruss)
````
39 changes: 29 additions & 10 deletions ext/AlgebraicDynamicsAlgebraicPetriExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,36 @@ module AlgebraicDynamicsAlgebraicPetriExt
using AlgebraicPetri
using AlgebraicDynamics
using Catlab
using LabelledArrays
import AlgebraicDynamics.UWDDynam: ContinuousResourceSharer

function ContinuousResourceSharer{T}(pn::Union{OpenPetriNet, OpenLabelledPetriNet}) where T
nstates = nparts(apex(pn), :S)
portmap = vcat(map(legs(pn)) do f
f[:S](parts(dom(f), :S))
end...)
nports = length(portmap)
vf(u, p, t) = vectorfield(apex(pn))(zeros(nstates), u, p, t)

ContinuousResourceSharer{T}(nports, nstates, vf, portmap)
end
export OpenNet

""" OpenNet=Union{OpenPetriNet,OpenLabelledPetriNet,OpenLabelledReactionNet}

A type alias for any kind of open network from AlgebraicPetri.
"""
const OpenNet = Union{OpenPetriNet,OpenLabelledPetriNet,OpenLabelledReactionNet}

# vecfields need to be initialized differently depending on type of net
function dynamics(pn::OpenPetriNet,T::Type,ns::Int64)
vf(u, p, t) = vectorfield(apex(pn))(zeros(T,ns), u, p, t)
end

function dynamics(pn::OpenLabelledPetriNet,T::Type,ns::Int64)
vf(u, p, t) = vectorfield(apex(pn))(LVector(NamedTuple{tuple(snames(apex(pn))...)}(zeros(T,ns))), u, p, t)
end

function dynamics(pn::OpenLabelledReactionNet,T::Type,ns::Int64)
vf(u, p, t) = vectorfield(apex(pn))(LVector(NamedTuple{tuple(snames(apex(pn))...)}(zeros(T,ns))), u, rates(apex(pn)), t)
end

function ContinuousResourceSharer{T}(pn::OpenNet) where T
nstates = nparts(apex(pn), :S)
portmap = vcat(map(legs(pn)) do f
f[:S](parts(dom(f), :S))
end...)
nports = length(portmap)
ContinuousResourceSharer{T}(nports, nstates, dynamics(pn,T,nstates), portmap)
end
end
66 changes: 44 additions & 22 deletions test/ext/AlgebraicPetri.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,48 @@ using AlgebraicDynamics
using AlgebraicPetri
using Catlab
using Test
using AlgebraicDynamics.UWDDynam
using LabelledArrays

@testset "AlgebraicPetri" begin
birth_petri = Open(PetriNet(1, 1=>(1,1)));
rs = ContinuousResourceSharer{Float64}(birth_petri)
@test nstates(rs) == 1
@test nports(rs) == 1
@test portmap(rs) == [1]
u = [1.0]; p = [2.0];
@test eval_dynamics(rs, u, p, 0) == [2.0]

Brusselator= LabelledPetriNet([:A, :B, :D, :E, :X, :Y],
:t1 => (:A => (:X, :A)),
:t2 => ((:X, :X, :Y) => (:X, :X, :X)),
:t3 => ((:B, :X) => (:Y, :D, :B)),
:t4 => (:X => :E)
)

open_bruss = Open([:A, :D], Brusselator, [:A, :B])
rs = ContinuousResourceSharer{Float64}(open_bruss)
@test nstates(rs) == 6
@test nports(rs) == 4
@test portmap(rs) == [1,3, 1,2]
end

@testset "AlgebraicPetri" begin
birth_petri = Open(PetriNet(1, 1 => (1, 1)))
rs = ContinuousResourceSharer{Float64}(birth_petri)
@test nstates(rs) == 1
@test nports(rs) == 1
@test portmap(rs) == [1]
u = [1.0]
p = [2.0]
@test eval_dynamics(rs, u, p, 0) == [2.0]

labelled_birth_petri = Open(LabelledPetriNet([:foo], (:bar, (:foo => (:foo, :foo)))))
rs = ContinuousResourceSharer{Float64}(labelled_birth_petri)
@test nstates(rs) == 1
@test nports(rs) == 1
@test portmap(rs) == [1]
u = LVector(foo=1.0)
p = LVector(bar=2.0)
@test eval_dynamics(rs, u, p, 0) == [2.0]

labelled_birth_react = Open(LabelledReactionNet{Float64}{Float64}([:foo => 1.0], ((:bar => 2.0), (:foo => (:foo, :foo)))))
rs = ContinuousResourceSharer{Float64}(labelled_birth_react)
@test nstates(rs) == 1
@test nports(rs) == 1
@test portmap(rs) == [1]
u = LVector(NamedTuple(zip(snames(apex(labelled_birth_react)), Vector(subpart(apex(labelled_birth_react), :concentration)))))
p = LVector(bar=2.0)
@test eval_dynamics(rs, u, p, 0) == [2.0]

Brusselator = LabelledPetriNet([:A, :B, :D, :E, :X, :Y],
:t1 => (:A => (:X, :A)),
:t2 => ((:X, :X, :Y) => (:X, :X, :X)),
:t3 => ((:B, :X) => (:Y, :D, :B)),
:t4 => (:X => :E)
)

open_bruss = Open([:A, :D], Brusselator, [:A, :B])
rs = ContinuousResourceSharer{Float64}(open_bruss)
@test nstates(rs) == 6
@test nports(rs) == 4
@test portmap(rs) == [1, 3, 1, 2]
end
Loading