diff --git a/src/backend/to_dot.jl b/src/backend/to_dot.jl index b001ada3..69e64f2f 100644 --- a/src/backend/to_dot.jl +++ b/src/backend/to_dot.jl @@ -1,26 +1,26 @@ function to_dotstatic(operator::Type, id::Int, factor, subgraphs::AbstractVector{<:AbstractGraph}, subgraph_factors::AbstractVector) error( - "Static representation for computational graph nodes with operator $(operator) not yet implemented! " + "Static representation for computational graph nodes with operator $(operator) not yet implemented! " ) end -function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F,subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W} +function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W} node_temp = "" arrow_temp = "" if factor != 1 opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n" - opr_name = "g$(id)_t" - node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" - arrow_temp*= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" + opr_name = "g$(id)_t" + node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" + arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" node_temp *= opr_fac * node_str else opr_name = "g$id" end opr_node = opr_name * "[shape=box, label = \"Add\", style=filled, fillcolor=cyan,]\n" node_temp *= opr_node - for (gix,(g, gfactor)) in enumerate(zip(subgraphs, subgraph_factors)) - if gfactor!= 1 - factor_str = "factor$(g.id)_$(id)_$gix[label=$(gfactor), style=filled, fillcolor=lavender]\n" + for (gix, (g, gfactor)) in enumerate(zip(subgraphs, subgraph_factors)) + if gfactor != 1 + factor_str = "factor$(g.id)_$(id)_$gix[label=$(gfactor), style=filled, fillcolor=lavender]\n" subg_str = "g$(g.id)_$(id)_$gix[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" node_temp *= factor_str * subg_str arrow_temp *= "factor$(g.id)_$(id)_$gix->g$(g.id)_$(id)_$gix[arrowhead=vee,]\ng$(g.id)->g$(g.id)_$(id)_$gix[arrowhead=vee,]\n" @@ -29,7 +29,7 @@ function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F,subgra arrow_temp *= "g$(g.id)->$opr_name[arrowhead=vee,]\n" end end - return node_temp,arrow_temp + return node_temp, arrow_temp end function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, factor::F, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W} @@ -37,27 +37,27 @@ function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, factor::F, subg arrow_temp = "" if factor != 1 opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n" - opr_name = "g$(id)_t" - node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" - arrow_temp*= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" + opr_name = "g$(id)_t" + node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" + arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" node_temp *= opr_fac * node_str else opr_name = "g$id" end - opr_node = opr_name * "[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" + opr_node = opr_name * "[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" node_temp *= opr_node if length(subgraphs) == 1 - if subgraph_factors[1] ==1 + if subgraph_factors[1] == 1 arrow_temp *= "g$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n" else - factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n" + factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n" node_temp *= factor_str arrow_temp *= "factor$(subgraphs[1].id)_$(id)->$opr_name[arrowhead=vee,]\ng$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n" end else - for (gix,(g, gfactor)) in enumerate(zip(subgraphs, subgraph_factors)) - if gfactor!= 1 - factor_str = "factor$(g.id)_$(id)_$gix[label=$(gfactor), style=filled, fillcolor=lavender]\n" + for (gix, (g, gfactor)) in enumerate(zip(subgraphs, subgraph_factors)) + if gfactor != 1 + factor_str = "factor$(g.id)_$(id)_$gix[label=$(gfactor), style=filled, fillcolor=lavender]\n" subg_str = "g$(g.id)_$(id)_$gix[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" node_temp *= factor_str * subg_str arrow_temp *= "factor$(g.id)_$(id)_$gix->g$(g.id)_$(id)_$gix[arrowhead=vee,]\ng$(g.id)->g$(g.id)_$(id)_$gix[arrowhead=vee,]\n" @@ -67,7 +67,7 @@ function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, factor::F, subg end end end - return node_temp,arrow_temp + return node_temp, arrow_temp end function to_dotstatic(::Type{ComputationalGraphs.Power{N}}, id::Int, factor::F, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {N,F,W} @@ -75,19 +75,19 @@ function to_dotstatic(::Type{ComputationalGraphs.Power{N}}, id::Int, factor::F, arrow_temp = "" if factor != 1 opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n" - opr_name = "g$(id)_t" - node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" - arrow_temp*= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" + opr_name = "g$(id)_t" + node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" + arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" node_temp *= opr_fac * node_str else opr_name = "g$id" end - opr_node = opr_name * "[shape=box, label = \"Pow\", style=filled, fillcolor=darkolivegreen,]\n" + opr_node = opr_name * "[shape=box, label = \"Pow\", style=filled, fillcolor=darkolivegreen,]\n" order_node = "order$(id)[label=$N, style=filled, fillcolor=lavender]\n" node_temp *= opr_node * order_node - arrow_temp*= "order$(id)->$opr_name[arrowhead=vee,]\n" + arrow_temp *= "order$(id)->$opr_name[arrowhead=vee,]\n" if subgraph_factors[1] != 1 - factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n" + factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n" subg_str = "g$(subgraphs[1].id)_$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" node_temp *= factor_str * subg_str arrow_temp *= "factor$(subgraphs[1].id)_$(id)->g$(subgraphs[1].id)_$(id)[arrowhead=vee,]\ng$(subgraphs[1].id)->g$(subgraphs[1].id)_$(id)[arrowhead=vee,]\n" @@ -98,14 +98,14 @@ function to_dotstatic(::Type{ComputationalGraphs.Power{N}}, id::Int, factor::F, return node_temp, arrow_temp end -function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F,subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W} +function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W} node_temp = "" arrow_temp = "" if factor != 1 opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n" - opr_name = "g$(id)_t" - node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" - arrow_temp*= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" + opr_name = "g$(id)_t" + node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" + arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" node_temp *= opr_fac * node_str else opr_name = "g$id" @@ -123,7 +123,7 @@ function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F,subgr arrow_temp *= "g$(g.id)->$opr_name[arrowhead=vee,]\n" end end - return node_temp,arrow_temp + return node_temp, arrow_temp end function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, factor::F, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W} @@ -131,20 +131,20 @@ function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, factor::F, subg arrow_temp = "" if factor != 1 opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n" - opr_name = "g$(id)_t" - node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" - arrow_temp*= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" + opr_name = "g$(id)_t" + node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" + arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" node_temp *= opr_fac * node_str else opr_name = "g$id" end - opr_node = opr_name * "[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" + opr_node = opr_name * "[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" node_temp *= opr_node if length(subgraphs) == 1 - if subgraph_factors[1] ==1 + if subgraph_factors[1] == 1 arrow_temp *= "g$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n" else - factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n" + factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n" node_temp *= factor_str arrow_temp *= "factor$(subgraphs[1].id)_$(id)->$opr_name[arrowhead=vee,]\ng$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n" end @@ -161,7 +161,7 @@ function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, factor::F, subg end end end - return node_temp,arrow_temp + return node_temp, arrow_temp end function to_dotstatic(::Type{ComputationalGraphs.Power{N}}, id::Int, factor::F, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {N,F,W} @@ -169,19 +169,19 @@ function to_dotstatic(::Type{ComputationalGraphs.Power{N}}, id::Int, factor::F, arrow_temp = "" if factor != 1 opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n" - opr_name = "g$(id)_t" - node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" - arrow_temp*= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" + opr_name = "g$(id)_t" + node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" + arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" node_temp *= opr_fac * node_str else opr_name = "g$id" end - opr_node = opr_name * "[shape=box, label = \"Pow\", style=filled, fillcolor=darkolivegreen,]\n" + opr_node = opr_name * "[shape=box, label = \"Pow\", style=filled, fillcolor=darkolivegreen,]\n" order_node = "order$(id)[label=$N, style=filled, fillcolor=lavender]\n" node_temp *= opr_node * order_node - arrow_temp*= "order$(id)->$opr_name[arrowhead=vee,]\n" + arrow_temp *= "order$(id)->$opr_name[arrowhead=vee,]\n" if subgraph_factors[1] != 1 - factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n" + factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n" subg_str = "g$(subgraphs[1].id)_$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" node_temp *= factor_str * subg_str arrow_temp *= "factor$(subgraphs[1].id)_$(id)->g$(subgraphs[1].id)_$(id)[arrowhead=vee,]\ng$(subgraphs[1].id)->g$(subgraphs[1].id)_$(id)[arrowhead=vee,]\n" @@ -193,13 +193,15 @@ function to_dotstatic(::Type{ComputationalGraphs.Power{N}}, id::Int, factor::F, end """ - function to_dot_str(graphs::AbstractVector{<:AbstractGraph}) + function to_dot_str(graphs::AbstractVector{<:AbstractGraph}, name::String="", diagram_id_map=nothing) + Compile a list of graphs into a string for dot language. + # Arguments: - `graphs` vector of computational graphs - - `title` The name of the complied function (defaults to `"ComputationalGraph"`) + - `title` The name of the compiled function (defaults to nothing) """ -function to_dot_str(graphs::AbstractVector{<:AbstractGraph}, name::String="ComputationalGraph") +function to_dot_str(graphs::AbstractVector{<:AbstractGraph}, name::String="", diagram_id_map=nothing) head = "digraph ComputationalGraph { \nlabel=\"$name\"\n" head *= "ReturnNode[shape=box, label = \"Return\", style=filled, fillcolor=darkorange,]\n" body_node = "" @@ -218,12 +220,12 @@ function to_dot_str(graphs::AbstractVector{<:AbstractGraph}, name::String="Compu end if isempty(subgraphs(g)) #leaf g_id in inds_visitedleaf && continue - leafname = getname(g.properties, leafidx) + leafname = get_leafname(g, leafidx, diagram_id_map) if factor(g) == 1 gnode_str = "g$g_id[label=$leafname, style=filled, fillcolor=paleturquoise]\n" body_node *= gnode_str else - factor_str = "factor$(leafidx)_inp[label=$(factor(g)), style=filled, fillcolor=lavender]\n" + factor_str = "factor$(leafidx)_inp[label=$(factor(g)), style=filled, fillcolor=lavender]\n" leaf_node = "l$(leafidx)[label=$leafname, style=filled, fillcolor=paleturquoise]\n" gnode_str = "g$g_id[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" body_node *= factor_str * leaf_node * gnode_str @@ -233,14 +235,14 @@ function to_dot_str(graphs::AbstractVector{<:AbstractGraph}, name::String="Compu push!(inds_visitedleaf, g_id) else g_id in inds_visitednode && continue - temp_node,temp_arrow = to_dotstatic(operator(g), g_id, factor(g), subgraphs(g), subgraph_factors(g)) - body_node *=temp_node + temp_node, temp_arrow = to_dotstatic(operator(g), g_id, factor(g), subgraphs(g), subgraph_factors(g)) + body_node *= temp_node body_arrow *= temp_arrow push!(inds_visitednode, g_id) end if isroot body_arrow *= "g$(g_id)->ReturnNode[arrowhead=vee,]\n" - rootidx +=1 + rootidx += 1 end end end @@ -250,25 +252,42 @@ function to_dot_str(graphs::AbstractVector{<:AbstractGraph}, name::String="Compu return expr end -function compile_dot(graphs::AbstractVector{<:AbstractGraph}, filename::String; graph_name="ComputationalGraph") - dot_string = to_dot_str(graphs, graph_name) +function compile_dot(graphs::AbstractVector{<:AbstractGraph}, filename::String; graph_name="", diagram_id_map=nothing) + dot_string = to_dot_str(graphs, graph_name, diagram_id_map) open(filename, "w") do f write(f, dot_string) end end -function getname(properties,leafidx) - if properties isa BareGreenId - lfname = "<G$leafidx>" - elseif properties isa BareInteractionId - lfname = "<V$leafidx>" - elseif typeof(properties) == FeynmanProperties && properties.diagtype == ComputationalGraphs.Propagator - lfname = "<G$leafidx>" - elseif typeof(properties) == FeynmanProperties && properties.diagtype == ComputationalGraphs.Interaction - lfname = "<V$leafidx>" +function get_leafname(g, leafidx, diagram_id_map=nothing) + println(typeof(g)) + leaftype = Nothing + if g isa FeynmanGraph + leaftype = g.properties.diagtype + elseif g isa Graph + if isnothing(diagram_id_map) == false + leaftype = typeof(diagram_id_map[g.id]) + end + else + error("Unknown graph type: $(typeof(g))") + end + if leaftype in [BareGreenId, ComputationalGraphs.Propagator] + leafname = "<G$leafidx>" + elseif leaftype in [BareInteractionId, ComputationalGraphs.Interaction] + leafname = "<V$leafidx>" + elseif leaftype == PolarId + leafname = "<Π$leafidx>" + elseif leaftype == Ver3Id + leafname = "<Γ(3)$leafidx>" + elseif leaftype == Ver4Id + leafname = "<Γ(4)$leafidx>" else - lfname = "$leafidx>" + leafname = "$leafidx>" end - return lfname + println() + println(g) + println(leaftype) + println(leafname) + println() + return leafname end - diff --git a/src/computational_graph/transform.jl b/src/computational_graph/transform.jl index ee3ba934..fecf791a 100644 --- a/src/computational_graph/transform.jl +++ b/src/computational_graph/transform.jl @@ -183,7 +183,7 @@ end function flatten_chains(g::AbstractGraph) Recursively flattens chains of subgraphs within a given graph `g` by merging certain trivial unary subgraphs into their parent graphs, - This function returns a new graph with flatten chains, dervied from the input graph `g` remaining unchanged. + This function returns a new graph with flatten chains, derived from the input graph `g` remaining unchanged. # Arguments: - `g::AbstractGraph`: graph to be modified