From 2921882fd46f130d77782d192e1a5d19e41239e6 Mon Sep 17 00:00:00 2001
From: Anton Reinhard <anton.reinhard@proton.me>
Date: Fri, 24 May 2024 19:20:59 +0200
Subject: [PATCH] EOD

---
 src/code_gen/function.jl                      |   2 +-
 src/code_gen/tape_machine.jl                  |  40 ++----
 src/code_gen/type.jl                          |   6 +-
 src/models/interface.jl                       | 112 +--------------
 .../{ => physics_models}/abc/compute.jl       |   0
 src/models/{ => physics_models}/abc/create.jl |   0
 src/models/{ => physics_models}/abc/parse.jl  |   0
 .../{ => physics_models}/abc/particle.jl      |   0
 src/models/{ => physics_models}/abc/print.jl  |   0
 .../{ => physics_models}/abc/properties.jl    |   0
 src/models/{ => physics_models}/abc/types.jl  |   0
 src/models/physics_models/interface.jl        | 131 ++++++++++++++++++
 .../{ => physics_models}/qed/compute.jl       |   0
 src/models/{ => physics_models}/qed/create.jl |   0
 .../{ => physics_models}/qed/diagrams.jl      |   0
 src/models/{ => physics_models}/qed/parse.jl  |   0
 .../{ => physics_models}/qed/particle.jl      |   0
 src/models/{ => physics_models}/qed/print.jl  |   0
 .../{ => physics_models}/qed/properties.jl    |   0
 src/models/{ => physics_models}/qed/types.jl  |   0
 20 files changed, 154 insertions(+), 137 deletions(-)
 rename src/models/{ => physics_models}/abc/compute.jl (100%)
 rename src/models/{ => physics_models}/abc/create.jl (100%)
 rename src/models/{ => physics_models}/abc/parse.jl (100%)
 rename src/models/{ => physics_models}/abc/particle.jl (100%)
 rename src/models/{ => physics_models}/abc/print.jl (100%)
 rename src/models/{ => physics_models}/abc/properties.jl (100%)
 rename src/models/{ => physics_models}/abc/types.jl (100%)
 create mode 100644 src/models/physics_models/interface.jl
 rename src/models/{ => physics_models}/qed/compute.jl (100%)
 rename src/models/{ => physics_models}/qed/create.jl (100%)
 rename src/models/{ => physics_models}/qed/diagrams.jl (100%)
 rename src/models/{ => physics_models}/qed/parse.jl (100%)
 rename src/models/{ => physics_models}/qed/particle.jl (100%)
 rename src/models/{ => physics_models}/qed/print.jl (100%)
 rename src/models/{ => physics_models}/qed/properties.jl (100%)
 rename src/models/{ => physics_models}/qed/types.jl (100%)

diff --git a/src/code_gen/function.jl b/src/code_gen/function.jl
index 97df44e..588b350 100644
--- a/src/code_gen/function.jl
+++ b/src/code_gen/function.jl
@@ -56,7 +56,7 @@ end
 """
     execute(graph::DAG, process::AbstractProcessDescription, machine::Machine, input::AbstractProcessInput)
 
-Execute the code of the given `graph` on the given input particles.
+Execute the code of the given `graph` on the given input values.
 
 This is essentially shorthand for
 ```julia
diff --git a/src/code_gen/tape_machine.jl b/src/code_gen/tape_machine.jl
index 165b26b..ea42b9b 100644
--- a/src/code_gen/tape_machine.jl
+++ b/src/code_gen/tape_machine.jl
@@ -1,3 +1,4 @@
+# TODO: do this with macros
 function call_fc(fc::FunctionCall{VectorT, 0}, cache::Dict{Symbol, Any}) where {VectorT <: SVector{1}}
     cache[fc.return_symbol] = fc.func(cache[fc.arguments[1]])
     return nothing
@@ -73,38 +74,27 @@ function gen_cache_init_code(machine::Machine)
     return initializeCaches
 end
 
-"""
-    part_from_x(type::Type, index::Int, x::AbstractProcessInput)
-
-Return the [`ParticleValue`](@ref) of the given type of particle with the given `index` from the given process input.
-
-Function is wrapped into a [`FunctionCall`](@ref) in [`gen_input_assignment_code`](@ref).
-"""
-part_from_x(type::Type, index::Int, x::AbstractProcessInput) =
-    ParticleValue{type, ComplexF64}(get_particle(x, type, index), one(ComplexF64))
-
 """
     gen_input_assignment_code(
         inputSymbols::Dict{String, Vector{Symbol}},
-        processDescription::AbstractProcessDescription,
+        instance::AbstractProblemInstance,
         machine::Machine,
-        processInputSymbol::Symbol = :input,
+        problemInputSymbol::Symbol = :input,
     )
 
-Return a `Vector{Expr}` doing the input assignments from the given `processInputSymbol` onto the `inputSymbols`.
+Return a `Vector{Expr}` doing the input assignments from the given `problemInputSymbol` onto the `inputSymbols`.
 """
 function gen_input_assignment_code(
     inputSymbols::Dict{String, Vector{Symbol}},
-    processDescription::AbstractProcessDescription,
+    instance::AbstractProblemInstance,
     machine::Machine,
-    processInputSymbol::Symbol = :input,
+    problemInputSymbol::Symbol = :input,
 )
-    @assert length(inputSymbols) >=
-            sum(values(in_particles(processDescription))) + sum(values(out_particles(processDescription))) "Number of input Symbols is smaller than the number of particles in the process description"
+    @assert length(inputSymbols) >= sum(values(in_particles(instance))) + sum(values(out_particles(instance))) "Number of input Symbols is smaller than the number of particles in the process description"
 
     assignInputs = Vector{FunctionCall}()
     for (name, symbols) in inputSymbols
-        (type, index) = type_index_from_name(model(processDescription), name)
+        (type, index) = type_index_from_name(model(instance), name)
         # make a function for this, since we can't use anonymous functions in the FunctionCall
 
         for symbol in symbols
@@ -114,7 +104,7 @@ function gen_input_assignment_code(
                 FunctionCall(
                     # x is the process input
                     part_from_x,
-                    SVector{1, Symbol}(processInputSymbol),
+                    SVector{1, Symbol}(problemInputSymbol),
                     SVector{2, Any}(type, index),
                     symbol,
                     device,
@@ -127,13 +117,13 @@ function gen_input_assignment_code(
 end
 
 """
-    gen_tape(graph::DAG, process::AbstractProcessDescription, machine::Machine)
+    gen_tape(graph::DAG, instance::AbstractProblemInstance, machine::Machine)
 
 Generate the code for a given graph. The return value is a [`Tape`](@ref).
 
 See also: [`execute`](@ref), [`execute_tape`](@ref)
 """
-function gen_tape(graph::DAG, process::AbstractProcessDescription, machine::Machine)
+function gen_tape(graph::DAG, instance::AbstractProblemInstance, machine::Machine)
     schedule = schedule_dag(GreedyScheduler(), graph, machine)
 
     # get inSymbols
@@ -150,19 +140,19 @@ function gen_tape(graph::DAG, process::AbstractProcessDescription, machine::Mach
     outSym = Symbol(to_var_name(get_exit_node(graph).id))
 
     initCaches = gen_cache_init_code(machine)
-    assignInputs = gen_input_assignment_code(inputSyms, process, machine, :input)
+    assignInputs = gen_input_assignment_code(inputSyms, instance, machine, :input)
 
-    return Tape(initCaches, assignInputs, schedule, inputSyms, outSym, Dict(), process, machine)
+    return Tape(initCaches, assignInputs, schedule, inputSyms, outSym, Dict(), instance, machine)
 end
 
 """
-    execute_tape(tape::Tape, input::AbstractProcessInput)
+    execute_tape(tape::Tape, input::Input) where {Input}
 
 Execute the given tape with the given input.
 
 For implementation reasons, this disregards the set [`CacheStrategy`](@ref) of the devices and always uses a dictionary.
 """
-function execute_tape(tape::Tape, input::AbstractProcessInput)
+function execute_tape(tape::Tape, input)
     cache = Dict{Symbol, Any}()
     cache[:input] = input
     # simply execute all the code snippets here
diff --git a/src/code_gen/type.jl b/src/code_gen/type.jl
index 4160a70..46e9fd1 100644
--- a/src/code_gen/type.jl
+++ b/src/code_gen/type.jl
@@ -1,19 +1,19 @@
 
 """
-    Tape
+    Tape{INPUT}
 
 TODO: update docs
 - `code::Vector{Expr}`: The julia expression containing the code for the whole graph.
 - `inputSymbols::Dict{String, Vector{Symbol}}`: A dictionary of symbols mapping the names of the input nodes of the graph to the symbols their inputs should be provided on.
 - `outputSymbol::Symbol`: The symbol of the final calculated value
 """
-struct Tape
+struct Tape{INPUT}
     initCachesCode::Vector{Expr}
     inputAssignCode::Vector{FunctionCall}
     computeCode::Vector{FunctionCall}
     inputSymbols::Dict{String, Vector{Symbol}}
     outputSymbol::Symbol
     cache::Dict{Symbol, Any}
-    process::AbstractProcessDescription
+    instance::AbstractProblemInstance
     machine::Machine
 end
diff --git a/src/models/interface.jl b/src/models/interface.jl
index 60e6d5e..66f6514 100644
--- a/src/models/interface.jl
+++ b/src/models/interface.jl
@@ -1,120 +1,16 @@
-import QEDbase.mass
-import QEDbase.AbstractParticle
 
 """
-    AbstractPhysicsModel
+    AbstractModel
 
-Base type for a model, e.g. ABC-Model or QED. This is used to dispatch many functions.
-"""
-abstract type AbstractPhysicsModel end
 
 """
-    ParticleValue{ParticleType <: AbstractParticle}
-
-A struct describing a particle during a calculation of a Feynman Diagram, together with the value that's being calculated. `AbstractParticle` is the type from the QEDbase package.
-
-`sizeof(ParticleValue())` = 48 Byte
-"""
-struct ParticleValue{ParticleType <: AbstractParticle, ValueType}
-    p::ParticleType
-    v::ValueType
-end
+abstract type AbstractModel end
 
 """
-    AbstractProcessDescription
+    AbstractProblemInstance
 
 Base type for process descriptions. An object of this type of a corresponding [`AbstractPhysicsModel`](@ref) should uniquely identify a process in that model.
 
 See also: [`parse_process`](@ref)
 """
-abstract type AbstractProcessDescription end
-
-"""
-    AbstractProcessInput
-
-Base type for process inputs. An object of this type contains the input values (e.g. momenta) of the particles in a process.
-
-See also: [`gen_process_input`](@ref)
-"""
-abstract type AbstractProcessInput end
-
-"""
-    interaction_result(t1::Type{T1}, t2::Type{T2}) where {T1 <: AbstractParticle, T2 <: AbstractParticle}
-
-Interface function that must be implemented for every subtype of [`AbstractParticle`](@ref), returning the result particle type when the two given particles interact.
-"""
-function interaction_result end
-
-"""
-    types(::AbstractPhysicsModel)
-
-Interface function that must be implemented for every subtype of [`AbstractPhysicsModel`](@ref), returning a `Vector` of the available particle types in the model.
-"""
-function types end
-
-"""
-    in_particles(::AbstractProcessDescription)
-
-Interface function that must be implemented for every subtype of [`AbstractProcessDescription`](@ref).
-Returns a `<: Dict{Type{AbstractParticle}, Int}` object, representing the number of incoming particles for the process per particle type.
-
-
-    in_particles(::AbstractProcessInput)
-
-Interface function that must be implemented for every subtype of [`AbstractProcessInput`](@ref).
-Returns a `<: Vector{AbstractParticle}` object with the values of all incoming particles for the corresponding `ProcessDescription`.
-"""
-function in_particles end
-
-"""
-    out_particles(::AbstractProcessDescription)
-
-Interface function that must be implemented for every subtype of [`AbstractProcessDescription`](@ref).
-Returns a `<: Dict{Type{AbstractParticle}, Int}` object, representing the number of outgoing particles for the process per particle type.
-
-
-    out_particles(::AbstractProcessInput)
-
-Interface function that must be implemented for every subtype of [`AbstractProcessInput`](@ref).
-Returns a `<: Vector{AbstractParticle}` object with the values of all outgoing particles for the corresponding `ProcessDescription`.
-"""
-function out_particles end
-
-"""
-    get_particle(::AbstractProcessInput, t::Type, n::Int)
-
-Interface function that must be implemented for every subtype of [`AbstractProcessInput`](@ref).
-Returns the `n`th particle of type `t`.
-"""
-function get_particle end
-
-"""
-    parse_process(::AbstractString, ::AbstractPhysicsModel)
-
-Interface function that must be implemented for every subtype of [`AbstractPhysicsModel`](@ref).
-Returns a `ProcessDescription` object.
-"""
-function parse_process end
-
-"""
-    gen_process_input(::AbstractProcessDescription)
-
-Interface function that must be implemented for every specific [`AbstractProcessDescription`](@ref).
-Returns a randomly generated and valid corresponding `ProcessInput`.
-"""
-function gen_process_input end
-
-"""
-    model(::AbstractProcessDescription)
-    model(::AbstarctProcessInput)
-
-Return the model of this process description or input.
-"""
-function model end
-
-"""
-    type_from_name(model::AbstractModel, name::String)
-
-For a name of a particle in the given [`AbstractModel`](@ref), return the particle's [`Type`] and index as a tuple. The input string can be expetced to be of the form \"<name><index>\".
-"""
-function type_index_from_name end
+abstract type AbstractProblemInstance end
diff --git a/src/models/abc/compute.jl b/src/models/physics_models/abc/compute.jl
similarity index 100%
rename from src/models/abc/compute.jl
rename to src/models/physics_models/abc/compute.jl
diff --git a/src/models/abc/create.jl b/src/models/physics_models/abc/create.jl
similarity index 100%
rename from src/models/abc/create.jl
rename to src/models/physics_models/abc/create.jl
diff --git a/src/models/abc/parse.jl b/src/models/physics_models/abc/parse.jl
similarity index 100%
rename from src/models/abc/parse.jl
rename to src/models/physics_models/abc/parse.jl
diff --git a/src/models/abc/particle.jl b/src/models/physics_models/abc/particle.jl
similarity index 100%
rename from src/models/abc/particle.jl
rename to src/models/physics_models/abc/particle.jl
diff --git a/src/models/abc/print.jl b/src/models/physics_models/abc/print.jl
similarity index 100%
rename from src/models/abc/print.jl
rename to src/models/physics_models/abc/print.jl
diff --git a/src/models/abc/properties.jl b/src/models/physics_models/abc/properties.jl
similarity index 100%
rename from src/models/abc/properties.jl
rename to src/models/physics_models/abc/properties.jl
diff --git a/src/models/abc/types.jl b/src/models/physics_models/abc/types.jl
similarity index 100%
rename from src/models/abc/types.jl
rename to src/models/physics_models/abc/types.jl
diff --git a/src/models/physics_models/interface.jl b/src/models/physics_models/interface.jl
new file mode 100644
index 0000000..32478cf
--- /dev/null
+++ b/src/models/physics_models/interface.jl
@@ -0,0 +1,131 @@
+import QEDbase.mass
+import QEDbase.AbstractParticle
+
+"""
+    AbstractPhysicsModel
+
+Base type for a model, e.g. ABC-Model or QED. This is used to dispatch many functions.
+"""
+abstract type AbstractPhysicsModel <: AbstractModel end
+
+"""
+    ParticleValue{ParticleType <: AbstractParticle}
+
+A struct describing a particle during a calculation of a Feynman Diagram, together with the value that's being calculated. `AbstractParticle` is the type from the QEDbase package.
+
+`sizeof(ParticleValue())` = 48 Byte
+"""
+struct ParticleValue{ParticleType <: AbstractParticle, ValueType}
+    p::ParticleType
+    v::ValueType
+end
+
+"""
+    AbstractProcessDescription <: AbstractProblemInstance
+
+Base type for particle scattering process descriptions. An object of this type of a corresponding [`AbstractPhysicsModel`](@ref) should uniquely identify a scattering process in that model.
+
+See also: [`parse_process`](@ref), [`AbstractProblemInstance`](@ref)
+"""
+abstract type AbstractProcessDescription end
+
+
+"""
+    AbstractProcessInput
+
+Base type for process inputs. An object of this type contains the input values (e.g. momenta) of the particles in a process.
+
+See also: [`gen_process_input`](@ref)
+"""
+abstract type AbstractProcessInput end
+
+"""
+    interaction_result(t1::Type{T1}, t2::Type{T2}) where {T1 <: AbstractParticle, T2 <: AbstractParticle}
+
+Interface function that must be implemented for every subtype of [`AbstractParticle`](@ref), returning the result particle type when the two given particles interact.
+"""
+function interaction_result end
+
+"""
+    types(::AbstractPhysicsModel)
+
+Interface function that must be implemented for every subtype of [`AbstractPhysicsModel`](@ref), returning a `Vector` of the available particle types in the model.
+"""
+function types end
+
+"""
+    in_particles(::AbstractProcessDescription)
+
+Interface function that must be implemented for every subtype of [`AbstractProcessDescription`](@ref).
+Returns a `<: Dict{Type{AbstractParticle}, Int}` object, representing the number of incoming particles for the process per particle type.
+
+
+    in_particles(::AbstractProcessInput)
+
+Interface function that must be implemented for every subtype of [`AbstractProcessInput`](@ref).
+Returns a `<: Vector{AbstractParticle}` object with the values of all incoming particles for the corresponding `ProcessDescription`.
+"""
+function in_particles end
+
+"""
+    out_particles(::AbstractProcessDescription)
+
+Interface function that must be implemented for every subtype of [`AbstractProcessDescription`](@ref).
+Returns a `<: Dict{Type{AbstractParticle}, Int}` object, representing the number of outgoing particles for the process per particle type.
+
+
+    out_particles(::AbstractProcessInput)
+
+Interface function that must be implemented for every subtype of [`AbstractProcessInput`](@ref).
+Returns a `<: Vector{AbstractParticle}` object with the values of all outgoing particles for the corresponding `ProcessDescription`.
+"""
+function out_particles end
+
+"""
+    get_particle(::AbstractProcessInput, t::Type, n::Int)
+
+Interface function that must be implemented for every subtype of [`AbstractProcessInput`](@ref).
+Returns the `n`th particle of type `t`.
+"""
+function get_particle end
+
+"""
+    parse_process(::AbstractString, ::AbstractPhysicsModel)
+
+Interface function that must be implemented for every subtype of [`AbstractPhysicsModel`](@ref).
+Returns a `ProcessDescription` object.
+"""
+function parse_process end
+
+"""
+    gen_process_input(::AbstractProcessDescription)
+
+Interface function that must be implemented for every specific [`AbstractProcessDescription`](@ref).
+Returns a randomly generated and valid corresponding `ProcessInput`.
+"""
+function gen_process_input end
+
+"""
+    model(::AbstractProcessDescription)
+    model(::AbstractProcessInput)
+
+Return the model of this process description or input.
+"""
+function model end
+
+"""
+    type_from_name(model::AbstractModel, name::String)
+
+For a name of a particle in the given [`AbstractModel`](@ref), return the particle's [`Type`] and index as a tuple. The input string can be expetced to be of the form \"<name><index>\".
+"""
+function type_index_from_name end
+
+"""
+    part_from_x(type::Type, index::Int, x::AbstractProcessInput)
+
+Return the [`ParticleValue`](@ref) of the given type of particle with the given `index` from the given process input.
+
+Function is wrapped into a [`FunctionCall`](@ref) in [`gen_input_assignment_code`](@ref).
+"""
+part_from_x(type::Type, index::Int, x::AbstractProcessInput) =
+    ParticleValue{type, ComplexF64}(get_particle(x, type, index), one(ComplexF64))
diff --git a/src/models/qed/compute.jl b/src/models/physics_models/qed/compute.jl
similarity index 100%
rename from src/models/qed/compute.jl
rename to src/models/physics_models/qed/compute.jl
diff --git a/src/models/qed/create.jl b/src/models/physics_models/qed/create.jl
similarity index 100%
rename from src/models/qed/create.jl
rename to src/models/physics_models/qed/create.jl
diff --git a/src/models/qed/diagrams.jl b/src/models/physics_models/qed/diagrams.jl
similarity index 100%
rename from src/models/qed/diagrams.jl
rename to src/models/physics_models/qed/diagrams.jl
diff --git a/src/models/qed/parse.jl b/src/models/physics_models/qed/parse.jl
similarity index 100%
rename from src/models/qed/parse.jl
rename to src/models/physics_models/qed/parse.jl
diff --git a/src/models/qed/particle.jl b/src/models/physics_models/qed/particle.jl
similarity index 100%
rename from src/models/qed/particle.jl
rename to src/models/physics_models/qed/particle.jl
diff --git a/src/models/qed/print.jl b/src/models/physics_models/qed/print.jl
similarity index 100%
rename from src/models/qed/print.jl
rename to src/models/physics_models/qed/print.jl
diff --git a/src/models/qed/properties.jl b/src/models/physics_models/qed/properties.jl
similarity index 100%
rename from src/models/qed/properties.jl
rename to src/models/physics_models/qed/properties.jl
diff --git a/src/models/qed/types.jl b/src/models/physics_models/qed/types.jl
similarity index 100%
rename from src/models/qed/types.jl
rename to src/models/physics_models/qed/types.jl