From 4874728179bd30f9c231c89fb3dcbf3317df3bee Mon Sep 17 00:00:00 2001 From: ZhiyiLi Date: Wed, 6 Dec 2023 18:18:56 +0800 Subject: [PATCH 1/4] add for creating dot file for ComputationalGraph --- src/backend/compiler.jl | 1 + src/backend/to_dot.jl | 258 ++++++++++++++++++++++++++++++++++++++++ src/frontend/GV.jl | 2 + 3 files changed, 261 insertions(+) create mode 100644 src/backend/to_dot.jl diff --git a/src/backend/compiler.jl b/src/backend/compiler.jl index dfd7073f..e3fb57ee 100644 --- a/src/backend/compiler.jl +++ b/src/backend/compiler.jl @@ -10,5 +10,6 @@ RuntimeGeneratedFunctions.init(Compilers) include("static.jl") include("compiler_python.jl") +include("to_dot.jl") end \ No newline at end of file diff --git a/src/backend/to_dot.jl b/src/backend/to_dot.jl new file mode 100644 index 00000000..5a6d8e49 --- /dev/null +++ b/src/backend/to_dot.jl @@ -0,0 +1,258 @@ +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! " + ) +end + +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" + 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 (g, gfactor) in zip(subgraphs, subgraph_factors) + if gfactor!= 1 + factor_str = "factor$(g.id)_$(id)[label=$(gfactor), style=filled, fillcolor=lavender]\n" + subg_str = "g$(g.id)_$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" + node_temp *= factor_str * subg_str + arrow_temp *= "factor$(g.id)_$(id)->g$(g.id)_$(id)[arrowhead=vee,]\ng$(g.id)->g$(g.id)_$(id)[arrowhead=vee,]\n" + arrow_temp *= "g$(g.id)_$(id)->$opr_name[arrowhead=vee,]\n" + else + arrow_temp *= "g$(g.id)->$opr_name[arrowhead=vee,]\n" + end + end + 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} + 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" + 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" + node_temp *= opr_node + if length(subgraphs) == 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" + 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 (g, gfactor) in zip(subgraphs, subgraph_factors) + if gfactor!= 1 + factor_str = "factor$(g.id)_$(id)[label=$(gfactor), style=filled, fillcolor=lavender]\n" + subg_str = "g$(g.id)_$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" + node_temp *= factor_str * subg_str + arrow_temp *= "factor$(g.id)_$(id)->g$(g.id)_$(id)[arrowhead=vee,]\ng$(g.id)->g$(g.id)_$(id)[arrowhead=vee,]\n" + arrow_temp *= "g$(g.id)_$(id)->$opr_name[arrowhead=vee,]\n" + else + arrow_temp *= "g$(g.id)->$opr_name[arrowhead=vee,]\n" + end + end + end + 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} + 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" + 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" + 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" + if subgraph_factors[1] != 1 + 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" + arrow_temp *= "g$(subgraphs[1].id)_$(id)->$opr_name[arrowhead=vee,]\n" + else + arrow_temp *= "g$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n" + end + 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} + 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" + 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 (g, gfactor) in zip(subgraphs, subgraph_factors) + if gfactor!= 1 + factor_str = "factor$(g.id)_$(id)[label=$(gfactor), style=filled, fillcolor=lavender]\n" + subg_str = "g$(g.id)_$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" + node_temp *= factor_str * subg_str + arrow_temp *= "factor$(g.id)_$(id)->g$(g.id)_$(id)[arrowhead=vee,]\ng$(g.id)->g$(g.id)_$(id)[arrowhead=vee,]\n" + arrow_temp *= "g$(g.id)_$(id)->$opr_name[arrowhead=vee,]\n" + else + arrow_temp *= "g$(g.id)->$opr_name[arrowhead=vee,]\n" + end + end + 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} + 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" + 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" + node_temp *= opr_node + if length(subgraphs) == 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" + 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 (g, gfactor) in zip(subgraphs, subgraph_factors) + if gfactor!= 1 + factor_str = "factor$(g.id)_$(id)[label=$(gfactor), style=filled, fillcolor=lavender]\n" + subg_str = "g$(g.id)_$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" + node_temp *= factor_str * subg_str + arrow_temp *= "factor$(g.id)_$(id)->g$(g.id)_$(id)[arrowhead=vee,]\ng$(g.id)->g$(g.id)_$(id)[arrowhead=vee,]\n" + arrow_temp *= "g$(g.id)_$(id)->$opr_name[arrowhead=vee,]\n" + else + arrow_temp *= "g$(g.id)->$opr_name[arrowhead=vee,]\n" + end + end + end + 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} + 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" + 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" + 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" + if subgraph_factors[1] != 1 + 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" + arrow_temp *= "g$(subgraphs[1].id)_$(id)->$opr_name[arrowhead=vee,]\n" + else + arrow_temp *= "g$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n" + end + return node_temp, arrow_temp +end + +""" + function to_dot_str(graphs::AbstractVector{<:AbstractGraph}) + 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"`) +""" +function to_dot_str(graphs::AbstractVector{<:AbstractGraph}, name::String="ComputationalGraph") + head = "digraph ComputationalGraph { \nlabel=\"$name\"\n" + head *= "ReturnNode[shape=box, label = \"Return\", style=filled, fillcolor=darkorange,]\n" + body_node = "" + body_arrow = "" + leafidx = 1 + root = [id(g) for g in graphs] + inds_visitedleaf = Int[] + inds_visitednode = Int[] + rootidx = 1 + for graph in graphs + for g in PostOrderDFS(graph) #leaf first search + g_id = id(g) + isroot = false + if g_id in root + isroot = true + end + if isempty(subgraphs(g)) #leaf + g_id in inds_visitedleaf && continue + if factor(g) == 1 + gnode_str = "g$g_id[label=l$(leafidx), style=filled, fillcolor=paleturquoise]\n" + body_node *= gnode_str + else + factor_str = "factor$(leafidx)_inp[label=$(factor(g)), style=filled, fillcolor=lavender]\n" + leaf_node = "l$(leafidx)[label=l$(leafidx), 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 + body_arrow *= "factor$(leafidx)_inp->g$g_id[arrowhead=vee,]\nl$(leafidx)->g$g_id[arrowhead=vee,]\n" + end + leafidx += 1 + 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 + body_arrow *= temp_arrow + push!(inds_visitednode, g_id) + end + if isroot + body_arrow *= "g$(g_id)->ReturnNode[arrowhead=vee,]\n" + rootidx +=1 + end + end + end + tail = " }\n" + expr = head * body_node * body_arrow * tail + # println(expr) + return expr +end + +function compile_dot(graphs::AbstractVector{<:AbstractGraph}, filename::String; graph_name="ComputationalGraph") + dot_string = to_dot_str(graphs, graph_name) + open(filename, "w") do f + write(f, dot_string) + end +end + diff --git a/src/frontend/GV.jl b/src/frontend/GV.jl index 51033120..08b32c45 100644 --- a/src/frontend/GV.jl +++ b/src/frontend/GV.jl @@ -124,6 +124,7 @@ function diagdictGV(type::Symbol, MaxOrder::Int, has_counterterm::Bool=false; key = (order, GOrder, VerOrder) dict_graphs[key] = (gvec, extT_labels) IR.optimize!(gvec) + IR.optimize!(gvec) end end end @@ -190,6 +191,7 @@ function diagdictGV(type::Symbol, gkeys::Vector{Tuple{Int,Int,Int}}; spinPolarPa labelProd=labelProd, spinPolarPara=spinPolarPara) dict_graphs[key] = (gvec, extT_labels) IR.optimize!(gvec) + IR.optimize!(gvec) # append!(graphvector, gvec) end # IR.optimize!(graphvector) From 537870427d9c041e1a77f3d07144dad3193ad978 Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 14 Dec 2023 22:00:08 +0800 Subject: [PATCH 2/4] fix bugs for same subgraphs in to_dot.jl --- src/backend/to_dot.jl | 44 +++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/backend/to_dot.jl b/src/backend/to_dot.jl index 5a6d8e49..e99b3833 100644 --- a/src/backend/to_dot.jl +++ b/src/backend/to_dot.jl @@ -18,13 +18,13 @@ function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F,subgra end opr_node = opr_name * "[shape=box, label = \"Add\", style=filled, fillcolor=cyan,]\n" node_temp *= opr_node - for (g, gfactor) in zip(subgraphs, subgraph_factors) + for (gix,(g, gfactor)) in enumerate(zip(subgraphs, subgraph_factors)) if gfactor!= 1 - factor_str = "factor$(g.id)_$(id)[label=$(gfactor), style=filled, fillcolor=lavender]\n" - subg_str = "g$(g.id)_$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" + 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)->g$(g.id)_$(id)[arrowhead=vee,]\ng$(g.id)->g$(g.id)_$(id)[arrowhead=vee,]\n" - arrow_temp *= "g$(g.id)_$(id)->$opr_name[arrowhead=vee,]\n" + 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" + arrow_temp *= "g$(g.id)_$(id)_$gix->$opr_name[arrowhead=vee,]\n" else arrow_temp *= "g$(g.id)->$opr_name[arrowhead=vee,]\n" end @@ -55,13 +55,13 @@ function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, factor::F, subg arrow_temp *= "factor$(subgraphs[1].id)_$(id)->$opr_name[arrowhead=vee,]\ng$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n" end else - for (g, gfactor) in zip(subgraphs, subgraph_factors) + for (gix,(g, gfactor)) in enumerate(zip(subgraphs, subgraph_factors)) if gfactor!= 1 - factor_str = "factor$(g.id)_$(id)[label=$(gfactor), style=filled, fillcolor=lavender]\n" - subg_str = "g$(g.id)_$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n" + 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)->g$(g.id)_$(id)[arrowhead=vee,]\ng$(g.id)->g$(g.id)_$(id)[arrowhead=vee,]\n" - arrow_temp *= "g$(g.id)_$(id)->$opr_name[arrowhead=vee,]\n" + 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" + arrow_temp *= "g$(g.id)_$(id)_$gix->$opr_name[arrowhead=vee,]\n" else arrow_temp *= "g$(g.id)->$opr_name[arrowhead=vee,]\n" end @@ -112,13 +112,13 @@ function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F,subgr end opr_node = opr_name * "[shape=box, label = \"Add\", style=filled, fillcolor=cyan,]\n" node_temp *= opr_node - for (g, gfactor) in zip(subgraphs, subgraph_factors) - if gfactor!= 1 - factor_str = "factor$(g.id)_$(id)[label=$(gfactor), style=filled, fillcolor=lavender]\n" - subg_str = "g$(g.id)_$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\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)->g$(g.id)_$(id)[arrowhead=vee,]\ng$(g.id)->g$(g.id)_$(id)[arrowhead=vee,]\n" - arrow_temp *= "g$(g.id)_$(id)->$opr_name[arrowhead=vee,]\n" + 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" + arrow_temp *= "g$(g.id)_$(id)_$gix->$opr_name[arrowhead=vee,]\n" else arrow_temp *= "g$(g.id)->$opr_name[arrowhead=vee,]\n" end @@ -149,13 +149,13 @@ function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, factor::F, subg arrow_temp *= "factor$(subgraphs[1].id)_$(id)->$opr_name[arrowhead=vee,]\ng$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n" end else - for (g, gfactor) in zip(subgraphs, subgraph_factors) - if gfactor!= 1 - factor_str = "factor$(g.id)_$(id)[label=$(gfactor), style=filled, fillcolor=lavender]\n" - subg_str = "g$(g.id)_$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\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)->g$(g.id)_$(id)[arrowhead=vee,]\ng$(g.id)->g$(g.id)_$(id)[arrowhead=vee,]\n" - arrow_temp *= "g$(g.id)_$(id)->$opr_name[arrowhead=vee,]\n" + 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" + arrow_temp *= "g$(g.id)_$(id)_$gix->$opr_name[arrowhead=vee,]\n" else arrow_temp *= "g$(g.id)->$opr_name[arrowhead=vee,]\n" end From 0f6c5074b93a60adcb9286a8155e910bf4e6e4cc Mon Sep 17 00:00:00 2001 From: Lizhiyi Date: Fri, 15 Dec 2023 01:14:40 +0800 Subject: [PATCH 3/4] refactor the leaf name with its type in to_dot.jl --- src/FeynmanDiagram.jl | 9 ++++----- src/backend/compiler.jl | 5 ++++- src/backend/to_dot.jl | 20 ++++++++++++++++++-- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/FeynmanDiagram.jl b/src/FeynmanDiagram.jl index 9e31c965..aed2d068 100644 --- a/src/FeynmanDiagram.jl +++ b/src/FeynmanDiagram.jl @@ -129,11 +129,6 @@ include("TaylorSeries/TaylorSeries.jl") using .Taylor export Taylor - -include("backend/compiler.jl") -using .Compilers -export Compilers - include("diagram_tree/DiagTree.jl") using .DiagTree export DiagTree @@ -182,6 +177,10 @@ using .GV export GV export diagdictGV, diagdict_parquet, leafstates, leafstates_diagtree +include("backend/compiler.jl") +using .Compilers +export Compilers + ##################### precompile ####################### # precompile as the final step of the module definition: diff --git a/src/backend/compiler.jl b/src/backend/compiler.jl index e3fb57ee..b286e808 100644 --- a/src/backend/compiler.jl +++ b/src/backend/compiler.jl @@ -1,7 +1,10 @@ module Compilers using PyCall using ..ComputationalGraphs -import ..ComputationalGraphs: id, name, set_name!, operator, subgraphs, subgraph_factors, factor +import ..ComputationalGraphs: id, name, set_name!, operator, subgraphs, subgraph_factors, factor, FeynmanProperties + +using ..DiagTree +using ..DiagTree: Diagram, PropagatorId, BareGreenId, BareInteractionId using ..AbstractTrees using ..RuntimeGeneratedFunctions diff --git a/src/backend/to_dot.jl b/src/backend/to_dot.jl index e99b3833..72925447 100644 --- a/src/backend/to_dot.jl +++ b/src/backend/to_dot.jl @@ -218,12 +218,13 @@ 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) if factor(g) == 1 - gnode_str = "g$g_id[label=l$(leafidx), style=filled, fillcolor=paleturquoise]\n" + 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" - leaf_node = "l$(leafidx)[label=l$(leafidx), style=filled, fillcolor=paleturquoise]\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 body_arrow *= "factor$(leafidx)_inp->g$g_id[arrowhead=vee,]\nl$(leafidx)->g$g_id[arrowhead=vee,]\n" @@ -256,3 +257,18 @@ function compile_dot(graphs::AbstractVector{<:AbstractGraph}, filename::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" + else + lfname = "L$leafidx" + end + return lfname +end + From c347b7695a763c3723537cae7c743217d89896e7 Mon Sep 17 00:00:00 2001 From: Lizhiyi Date: Fri, 15 Dec 2023 01:39:56 +0800 Subject: [PATCH 4/4] refactor the format to_dot.jl --- src/backend/to_dot.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/backend/to_dot.jl b/src/backend/to_dot.jl index 72925447..b001ada3 100644 --- a/src/backend/to_dot.jl +++ b/src/backend/to_dot.jl @@ -259,15 +259,15 @@ end function getname(properties,leafidx) if properties isa BareGreenId - lfname = "G$leafidx" + lfname = "<G$leafidx>" elseif properties isa BareInteractionId - lfname = "V$leafidx" + lfname = "<V$leafidx>" elseif typeof(properties) == FeynmanProperties && properties.diagtype == ComputationalGraphs.Propagator - lfname = "G$leafidx" + lfname = "<G$leafidx>" elseif typeof(properties) == FeynmanProperties && properties.diagtype == ComputationalGraphs.Interaction - lfname = "V$leafidx" + lfname = "<V$leafidx>" else - lfname = "L$leafidx" + lfname = "$leafidx>" end return lfname end