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

Example of wiring diagram from fortran code #212

Open
jpfairbanks opened this issue Aug 28, 2019 · 0 comments
Open

Example of wiring diagram from fortran code #212

jpfairbanks opened this issue Aug 28, 2019 · 0 comments

Comments

@jpfairbanks
Copy link
Owner

Hi @pauldhein here is that code we were talking about today.

using Catlab
using Catlab.WiringDiagrams
using Catlab.Doctrines
using Test
import Catlab.Doctrines: , id
(a::WiringDiagram, b::WiringDiagram) = otimes(a,b)
import Base: 
(a::WiringDiagram, b::WiringDiagram) = compose(b, a)
(a,b) = b  a

import Catlab.Graphics: to_graphviz
import Catlab.Graphics.Graphviz: run_graphviz

wd(s::Symbol, a,b) = WiringDiagram(Hom(s, a, b))
id(g::Catlab.Doctrines.FreeSymmetricMonoidalCategory.Ob{:generator}) = id(Ports([g]))

function writesvg(f::Union{IO,AbstractString}, d::WiringDiagram)
    write(f, to_graphviz(d, labels=true)
          |>g->run_graphviz(g, format="svg")
          )
end

#       subroutine sir(S, I, R, beta, gamma, dt)
#         implicit none
#         double precision S, I, R, beta, gamma, dt
#         double precision infected, recovered

#         infected = (-(beta*S*I) / (S + I + R)) * dt
#         recovered = (gamma*I) * dt

#         S = S - infected
#         I = I + infected - recovered
#         R = R + recovered
#       end subroutine sir

# C      program main
# C      double precision, parameter :: S0 = 500, I0 = 10, R0 = 0
# C      double precision, parameter :: beta = 0.5, gamma = 0.3, t = 1
# C
# C      call sir(S0, I0, R0, beta, gamma, t)
# C      end program main

double = Ob(FreeSymmetricMonoidalCategory, :F64)

code = wd(:sir, doubledoubledoubledoubledoubledouble, doubledoubledouble)

quote
    infection(β, S, I, R, dt, infected) = begin
        infected = (-(beta*S*I) / (S + I + R)) * dt
    end

    recovery(γ, I, dt) = begin
        recovered = (gamma*I) * dt
    end

    update(S, I, R, infected, recovered) = begin
        S = S - infected
        I = I + infected - recovered
        R = R + recovered
    end

    # updateS(S, infected) = S - infected
    # updateI(I, infected, recovered) = I+infected-recovered
    # updateR(R, recovered) = R+ recovered
end

d2 = doubledouble
d3 = d2  double
d4 = d3  double
d5 = d4  double
d6 = d5  double

idd = id(double)

copies = wd(:copies, d3, d6)
copy2 = wd(:copy2, double, d2)
inf = wd(:infection, d5, double)
rec = wd(:recovery, d4, double)
update = wd(:update, d5, d3)
m1 = ((copies  idd  idd)  (idd  idd  idd  inf))
code2 = (m1  rec)  update
swap = braid(Ports([double]), Ports([double]))
# code2 = (idd ⊗ (copy2⊗idd ⊚ (idd⊗swap)) ⊗ idd ⊗ idd) ⊚ m1
sirsiri = (idd  (copy2idd  (iddswap)))  ( copies  idd)


writesvg("sir_simple_f.svg", code)
writesvg("sir_simple_f_refactored.svg", code2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant