Add notebook abc model showcase, add some pretty print functions

This commit is contained in:
Anton Reinhard 2023-09-28 19:42:19 +02:00
parent 7dd9fedf2e
commit bef017130b
10 changed files with 457 additions and 15 deletions

1
.gitignore vendored
View File

@ -27,3 +27,4 @@ Manifest.toml
# vscode workspace directory
.vscode
.julia
**/.ipynb_checkpoints/

View File

@ -10,6 +10,12 @@ Pages = ["models/interface.jl"]
Order = [:type, :constant, :function]
```
```@autodocs
Modules = [MetagraphOptimization]
Pages = ["models/print.jl"]
Order = [:function]
```
## ABC-Model
### Types
@ -54,6 +60,13 @@ Pages = ["models/abc/compute.jl"]
Order = [:function]
```
### Print
```@autodocs
Modules = [MetagraphOptimization]
Pages = ["models/abc/print.jl"]
Order = [:function]
```
## QED-Model
*To be added*

View File

@ -1,3 +1,7 @@
# Manual
This will become a manual.
## Jupyter Notebooks
In the `notebooks` directory are notebooks containing some examples of the usage of this repository.
- `abc_model_showcase`: A simple showcase of the intended usage of the ABC Model implementation.

View File

@ -0,0 +1,314 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 32,
"id": "20768e45-df62-4638-ba33-b0ccf239f1aa",
"metadata": {},
"outputs": [],
"source": [
"using Revise\n",
"using MetagraphOptimization\n",
"using BenchmarkTools"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "9df482a4-ca44-44c5-9ea7-7a2977d529be",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ABCModel()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Create a model identifier\n",
"model = ABCModel()"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "30b16872-07f7-4d47-8ff8-8c3a849c9d4e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ABC Process: 'AB->ABBB'"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Create a process in our model\n",
"process_str = \"AB->ABBB\"\n",
"process = parse_process(process_str, model)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "043bd9e2-f89a-4362-885a-8c89d4cdd76f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Graph:\n",
" Nodes: \n",
" Edges: 385\n",
" Total Compute Effort: 1075.0\n",
" Total Data Transfer: 10944.0\n",
" Total Compute Intensity: 0.09822733918128655\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total: 280, ComputeTaskS2: 24, DataTask: 143, \n",
" ComputeTaskU: 6, ComputeTaskSum: 1, ComputeTaskS1: 36, \n",
" ComputeTaskV: 64, ComputeTaskP: 6"
]
}
],
"source": [
"# Read the graph (of the same process) from a file\n",
"graph = parse_dag(\"../input/$process_str.txt\", model)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "02f01ad3-fd10-48d5-a0e0-c03dc83c80a4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Input for ABC Process: 'AB->ABBB':\n",
" Input particles:\n",
" A: [3.1643943055419363, 0.0, 0.0, 3.002231057221651]\n",
" B: [3.1643943055419363, 0.0, 0.0, -3.002231057221651]\n",
" Output Particles:\n",
" A: [-2.256745792111763, -1.8187612014283614, 0.7623781267848754, -0.45142978877450474]\n",
" B: [-1.2010630585601612, 0.48677829051626326, -0.3095511190586244, -0.33132683442411753]\n",
" B: [-1.851230645629639, 1.2170428203792298, -0.6136921618583376, 0.7544823438327369]\n",
" B: [-1.0197491147823097, 0.11494009053286842, 0.16086515413208705, 0.02827427936588541]\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Generate some random input data for our process\n",
"input_data = gen_process_input(process)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "083fb1be-ce2a-47f9-afb9-60a6fdfaed0b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"compute_ca11d460_5e25_11ee_14ab_5ddab1542812 (generic function with 1 method)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Get the function computing the result of the process from a ProcessInput\n",
"AB_AB3_compute = get_compute_function(graph, process)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "a40c9500-8f79-4f04-b3c5-59b72a6b7ba9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-2.133138322948753e-12"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Actually compute a result using the generated function and the input data\n",
"result = AB_AB3_compute(input_data)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "80c70010",
"metadata": {},
"outputs": [],
"source": [
"include(\"../examples/profiling_utilities.jl\")\n",
"\n",
"# We can also mute the graph by applying some operations to it\n",
"reduce_all!(graph)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "5b192b44",
"metadata": {},
"outputs": [],
"source": [
"# The result should be the same as before (we can use execute to save having to generate the function ourselves)\n",
"@assert result ≈ execute(graph, process, input_data)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "9b2f4a3f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1000-element Vector{Float64}:\n",
" -4.414620594828263e-10\n",
" -6.52806527801196e-13\n",
" -1.697445561359304e-14\n",
" -3.0968989470125384e-10\n",
" -4.502627629291429e-15\n",
" -1.5151443298524234e-12\n",
" -4.6735707896483893e-11\n",
" -4.39957245807686e-12\n",
" -4.9303667114098105e-12\n",
" -7.978610716067286e-12\n",
" ⋮\n",
" -3.274950681363404e-15\n",
" 3.720176763468205e-14\n",
" -1.6979650967920612e-16\n",
" -1.2110610922665538e-11\n",
" -3.488772363060999e-13\n",
" -7.743825512592736e-12\n",
" -1.1887405783378775e-11\n",
" -1.7657864496526309e-13\n",
" -8.099730579187646e-12"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Now we can generate a function and use it on lots of inputs\n",
"inputs = [gen_process_input(process) for _ in 1:1000]\n",
"AB_AB3_reduced_compute = get_compute_function(graph, process)\n",
"\n",
"results = AB_AB3_reduced_compute.(inputs)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "d43e4ff0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: 1338 samples with 1 evaluation.\n",
" Range \u001b[90m(\u001b[39m\u001b[36m\u001b[1mmin\u001b[22m\u001b[39m … \u001b[35mmax\u001b[39m\u001b[90m): \u001b[39m\u001b[36m\u001b[1m2.718 ms\u001b[22m\u001b[39m … \u001b[35m7.994 ms\u001b[39m \u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmin … max\u001b[90m): \u001b[39m0.00% … 32.43%\n",
" Time \u001b[90m(\u001b[39m\u001b[34m\u001b[1mmedian\u001b[22m\u001b[39m\u001b[90m): \u001b[39m\u001b[34m\u001b[1m3.007 ms \u001b[22m\u001b[39m\u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmedian\u001b[90m): \u001b[39m0.00%\n",
" Time \u001b[90m(\u001b[39m\u001b[32m\u001b[1mmean\u001b[22m\u001b[39m ± \u001b[32mσ\u001b[39m\u001b[90m): \u001b[39m\u001b[32m\u001b[1m3.736 ms\u001b[22m\u001b[39m ± \u001b[32m1.156 ms\u001b[39m \u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmean ± σ\u001b[90m): \u001b[39m5.92% ± 11.07%\n",
"\n",
" \u001b[39m█\u001b[39m█\u001b[39m▃\u001b[39m▂\u001b[34m▂\u001b[39m\u001b[39m▁\u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[32m \u001b[39m\u001b[39m▁\u001b[39m \u001b[39m▁\u001b[39m▂\u001b[39m▂\u001b[39m▁\u001b[39m▂\u001b[39m▁\u001b[39m▁\u001b[39m▁\u001b[39m \u001b[39m \u001b[39m▁\u001b[39m \u001b[39m▁\u001b[39m▁\u001b[39m▁\u001b[39m▃\u001b[39m▃\u001b[39m▄\u001b[39m▂\u001b[39m▂\u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \n",
" \u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[34m█\u001b[39m\u001b[39m█\u001b[39m█\u001b[39m▇\u001b[39m█\u001b[39m▇\u001b[39m▇\u001b[39m▆\u001b[39m█\u001b[32m█\u001b[39m\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m▇\u001b[39m▆\u001b[39m▅\u001b[39m▁\u001b[39m▅\u001b[39m▄\u001b[39m▅\u001b[39m▄\u001b[39m▄\u001b[39m▁\u001b[39m▅\u001b[39m▄\u001b[39m▅\u001b[39m▅\u001b[39m▅\u001b[39m▄\u001b[39m▆\u001b[39m▁\u001b[39m▄\u001b[39m▆\u001b[39m▇\u001b[39m \u001b[39m█\n",
" 2.72 ms\u001b[90m \u001b[39m\u001b[90mHistogram: \u001b[39m\u001b[90m\u001b[1mlog(\u001b[22m\u001b[39m\u001b[90mfrequency\u001b[39m\u001b[90m\u001b[1m)\u001b[22m\u001b[39m\u001b[90m by time\u001b[39m 7.16 ms \u001b[0m\u001b[1m<\u001b[22m\n",
"\n",
" Memory estimate\u001b[90m: \u001b[39m\u001b[33m6.17 MiB\u001b[39m, allocs estimate\u001b[90m: \u001b[39m\u001b[33m143004\u001b[39m."
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"@benchmark results = AB_AB3_compute.($inputs)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "e18d9546",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: 1739 samples with 1 evaluation.\n",
" Range \u001b[90m(\u001b[39m\u001b[36m\u001b[1mmin\u001b[22m\u001b[39m … \u001b[35mmax\u001b[39m\u001b[90m): \u001b[39m\u001b[36m\u001b[1m2.178 ms\u001b[22m\u001b[39m … \u001b[35m 6.643 ms\u001b[39m \u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmin … max\u001b[90m): \u001b[39m0.00% … 37.05%\n",
" Time \u001b[90m(\u001b[39m\u001b[34m\u001b[1mmedian\u001b[22m\u001b[39m\u001b[90m): \u001b[39m\u001b[34m\u001b[1m2.291 ms \u001b[22m\u001b[39m\u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmedian\u001b[90m): \u001b[39m0.00%\n",
" Time \u001b[90m(\u001b[39m\u001b[32m\u001b[1mmean\u001b[22m\u001b[39m ± \u001b[32mσ\u001b[39m\u001b[90m): \u001b[39m\u001b[32m\u001b[1m2.873 ms\u001b[22m\u001b[39m ± \u001b[32m902.626 μs\u001b[39m \u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmean ± σ\u001b[90m): \u001b[39m6.58% ± 12.21%\n",
"\n",
" \u001b[39m█\u001b[39m▇\u001b[34m▄\u001b[39m\u001b[39m▂\u001b[39m▂\u001b[39m▂\u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[32m \u001b[39m\u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m▁\u001b[39m▁\u001b[39m▁\u001b[39m▁\u001b[39m▂\u001b[39m▂\u001b[39m▁\u001b[39m▁\u001b[39m▁\u001b[39m▂\u001b[39m▂\u001b[39m▂\u001b[39m▂\u001b[39m▁\u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \n",
" \u001b[39m█\u001b[39m█\u001b[34m█\u001b[39m\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m▇\u001b[39m▇\u001b[32m█\u001b[39m\u001b[39m█\u001b[39m▇\u001b[39m▇\u001b[39m▅\u001b[39m█\u001b[39m▇\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m█\u001b[39m▇\u001b[39m▅\u001b[39m▆\u001b[39m▆\u001b[39m▁\u001b[39m▁\u001b[39m▄\u001b[39m▅\u001b[39m▄\u001b[39m▁\u001b[39m▄\u001b[39m▁\u001b[39m▄\u001b[39m▄\u001b[39m▆\u001b[39m▅\u001b[39m▁\u001b[39m▄\u001b[39m▄\u001b[39m▄\u001b[39m▄\u001b[39m▅\u001b[39m▄\u001b[39m▆\u001b[39m \u001b[39m█\n",
" 2.18 ms\u001b[90m \u001b[39m\u001b[90mHistogram: \u001b[39m\u001b[90m\u001b[1mlog(\u001b[22m\u001b[39m\u001b[90mfrequency\u001b[39m\u001b[90m\u001b[1m)\u001b[22m\u001b[39m\u001b[90m by time\u001b[39m 5.97 ms \u001b[0m\u001b[1m<\u001b[22m\n",
"\n",
" Memory estimate\u001b[90m: \u001b[39m\u001b[33m5.26 MiB\u001b[39m, allocs estimate\u001b[90m: \u001b[39m\u001b[33m123004\u001b[39m."
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"@benchmark results = AB_AB3_reduced_compute.($inputs)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "8bc5dece",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.9.3",
"language": "julia",
"name": "julia-1.9"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.9.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@ -117,6 +117,7 @@ include("task/print.jl")
include("task/properties.jl")
include("models/interface.jl")
include("models/print.jl")
include("models/abc/types.jl")
include("models/abc/particle.jl")
@ -124,6 +125,7 @@ include("models/abc/compute.jl")
include("models/abc/create.jl")
include("models/abc/properties.jl")
include("models/abc/parse.jl")
include("models/abc/print.jl")
include("code_gen/main.jl")

View File

@ -6,7 +6,7 @@ using ForwardDiff
ComputeTaskSum() = ComputeTaskSum(0)
"""
gen_process_input(in::Vector{ParticleType}, out::Vector{ParticleType})
gen_process_input(processDescription::ABCProcessDescription)
Return a ProcessInput of randomly generated [`ABCParticle`](@ref)s from a [`ABCProcessDescription`](@ref). The process description can be created manually or parsed from a string using [`parse_process`](@ref).

View File

@ -228,8 +228,14 @@ function parse_process(str::AbstractString, model::ABCModel)
end
for t in types(model)
inParticles[t] = count(x -> x == String(t)[1], inStr)
outParticles[t] = count(x -> x == String(t)[1], outStr)
inCount = count(x -> x == String(t)[1], inStr)
outCount = count(x -> x == String(t)[1], outStr)
if inCount != 0
inParticles[t] = inCount
end
if outCount != 0
outParticles[t] = outCount
end
end
if length(inStr) != sum(values(inParticles))

View File

@ -1,19 +1,18 @@
using QEDbase
abstract type ABCParticle <: AbstractParticle end
"""
ABCModel <: AbstractPhysicsModel
Singleton definition for identification of the ABC-Model.
"""
struct ABCModel <: AbstractPhysicsModel end
struct ABCProcessDescription <: AbstractProcessDescription
inParticles::Dict{Type, Int}
outParticles::Dict{Type, Int}
end
"""
ABCParticle
struct ABCProcessInput <: AbstractProcessInput
process::ABCProcessDescription
inParticles::Vector{ABCParticle}
outParticles::Vector{ABCParticle}
end
Base type for all particles in the [`ABCModel`](@ref).
"""
abstract type ABCParticle <: AbstractParticle end
"""
ParticleA <: ABCParticle
@ -24,18 +23,53 @@ struct ParticleA <: ABCParticle
momentum::SFourMomentum
end
"""
ParticleB <: ABCParticle
A 'B' particle in the ABC Model.
"""
struct ParticleB <: ABCParticle
momentum::SFourMomentum
end
"""
ParticleC <: ABCParticle
A 'C' particle in the ABC Model.
"""
struct ParticleC <: ABCParticle
momentum::SFourMomentum
end
"""
ABCProcessDescription <: AbstractProcessDescription
A description of a process in the ABC-Model. Contains the input and output particles.
See also: [`in_particles`](@ref), [`out_particles`](@ref), [`parse_process`](@ref)
"""
struct ABCProcessDescription <: AbstractProcessDescription
inParticles::Dict{Type, Int}
outParticles::Dict{Type, Int}
end
"""
ABCProcessInput <: AbstractProcessInput
Input for a ABC Process. Contains the [`ABCProcessDescription`](@ref) of the process it is an input for, and the values of the in and out particles.
See also: [`gen_process_input`](@ref)
"""
struct ABCProcessInput <: AbstractProcessInput
process::ABCProcessDescription
inParticles::Vector{ABCParticle}
outParticles::Vector{ABCParticle}
end
"""
PARTICLE_MASSES
A constant dictionary containing the masses of the different [`ParticleType`](@ref)s.
A constant dictionary containing the masses of the different [`ABCParticle`](@ref)s.
"""
const PARTICLE_MASSES = Dict{Type, Float64}(ParticleA => 1.0, ParticleB => 1.0, ParticleC => 0.0)

58
src/models/abc/print.jl Normal file
View File

@ -0,0 +1,58 @@
"""
show(io::IO, process::ABCProcessDescription)
Pretty print an [`ABCProcessDescription`](@ref) (no newlines).
```jldoctest
julia> using MetagraphOptimization
julia> print(parse_process("AB->ABBB", ABCModel()))
ABC Process: 'AB->ABBB'
```
"""
function show(io::IO, process::ABCProcessDescription)
# types() gives the types in order (ABC) instead of random like keys() would
print(io, "ABC Process: \'")
for type in types(ABCModel())
for _ in 1:get(process.inParticles, type, 0)
print(io, String(type))
end
end
print(io, "->")
for type in types(ABCModel())
for _ in 1:get(process.outParticles, type, 0)
print(io, String(type))
end
end
print(io, "'")
return nothing
end
"""
show(io::IO, processInput::ABCProcessInput)
Pretty print an [`ABCProcessInput`](@ref) (with newlines).
"""
function show(io::IO, processInput::ABCProcessInput)
println(io, "Input for $(processInput.process):")
println(io, " Input particles:")
for particle in processInput.inParticles
println(io, " $particle")
end
println(io, " Output Particles:")
for particle in processInput.outParticles
println(io, " $particle")
end
return nothing
end
"""
show(io::IO, particle::T) where {T <: ABCParticle}
Pretty print an [`ABCParticle`](@ref) (no newlines).
"""
function show(io::IO, particle::T) where {T <: ABCParticle}
print(io, "$(String(typeof(particle))): $(particle.momentum)")
return nothing
end

10
src/models/print.jl Normal file
View File

@ -0,0 +1,10 @@
"""
show(io::IO, particleValue::ParticleValue)
Pretty print a [`ParticleValue`](@ref), no newlines.
"""
function show(io::IO, particleValue::ParticleValue)
print(io, "($(particleValue.p), value: $(particleValue.v))")
return nothing
end