From 358c30da900c1244a212ff89926b1145e3646821 Mon Sep 17 00:00:00 2001 From: Orjan Ameye Date: Thu, 3 Jul 2025 09:32:59 +0200 Subject: [PATCH 1/2] feat: make HarmonicEquation contain source system --- Project.toml | 2 +- src/HarmonicEquation.jl | 27 +++++++++++++++++---------- test/HarmonicEquation.jl | 8 +++++--- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/Project.toml b/Project.toml index f57b78b..f584da8 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "QuestBase" uuid = "7e80f742-43d6-403d-a9ea-981410111d43" authors = ["Orjan Ameye ", "Jan Kosata ", "Javier del Pino "] -version = "0.3.3" +version = "0.4.0" [deps] DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" diff --git a/src/HarmonicEquation.jl b/src/HarmonicEquation.jl index cb8348a..9323ce0 100644 --- a/src/HarmonicEquation.jl +++ b/src/HarmonicEquation.jl @@ -1,22 +1,25 @@ +# TODO replace link with HarmonicBalance link """ $(TYPEDEF) -Holds a set of algebraic equations governing the harmonics of a `DifferentialEquation`. +Holds a set of algebraic equations governing the harmonics of a +[`DifferentialEquation`](@ref). HarmonicEquation can also be constructed from a +[`QuantumCumulants.MeanfieldEquations`](https://qojulia.github.io/QuantumCumulants.jl/stable/api/#QuantumCumulants.MeanfieldEquations)`. # Fields $(TYPEDFIELDS) """ -mutable struct HarmonicEquation +mutable struct HarmonicEquation{T} """A set of equations governing the harmonics.""" equations::Vector{Equation} """A set of variables describing the harmonics.""" variables::Vector{HarmonicVariable} """The parameters of the equation set.""" parameters::Vector{Num} - "The natural equation (before the harmonic ansatz was used)." - natural_equation::DifferentialEquation "The Jacobian of the natural equation." jacobian::Matrix{Num} + "The system where the HarmonicEquation is derived from." + source_equations::T # use a self-referential constructor with _parameters function HarmonicEquation( @@ -25,12 +28,12 @@ mutable struct HarmonicEquation nat_eq::DifferentialEquation, ) return ( - x=new( + x=new{DifferentialEquation}( equations, variables, Num[], - nat_eq, dummy_symbolic_Jacobian(length(variables)), + nat_eq, ); x.parameters=_parameters(x); x @@ -42,12 +45,12 @@ mutable struct HarmonicEquation parameters::Vector{Num}, natural_equation::DifferentialEquation, ) - return new( + return new{DifferentialEquation}( equations, variables, parameters, - natural_equation, dummy_symbolic_Jacobian(length(variables)), + natural_equation, ) end function HarmonicEquation( @@ -55,11 +58,15 @@ mutable struct HarmonicEquation variables::Vector{HarmonicVariable}, parameters::Vector{Num}, jacobian::Matrix{Num}, - ) - return new(equations, variables, parameters, DifferentialEquation(), jacobian) + source_equations::T, + ) where T + return new{T}(equations, variables, parameters, jacobian, source_equations) end end +source(eom::HarmonicEquation) = eom.source_equations +source_type(eom::HarmonicEquation{T}) where {T} = T + "Get the parameters (not time nor variables) of a HarmonicEquation" function _parameters(eom::HarmonicEquation) all_symbols = flatten([ diff --git a/test/HarmonicEquation.jl b/test/HarmonicEquation.jl index 9bff61f..4343cad 100644 --- a/test/HarmonicEquation.jl +++ b/test/HarmonicEquation.jl @@ -18,7 +18,8 @@ using QuestBase: dummy_symbolic_Jacobian, @eqtest, get_all_terms, - get_independent + get_independent, + source # Setup common test variables @variables t, T @@ -36,19 +37,20 @@ hv2 = HarmonicVariable(v, "test", "v", Num(1.0), y) # Test constructor @testset "Construction" begin + heq1 = HarmonicEquation([eq1, eq2], [hv1, hv2], nat_eq) heq2 = HarmonicEquation([eq1, eq2], [hv1, hv2], Num[], nat_eq) for heq in [heq1, heq2] heq = heq1 @test heq.equations == [eq1, eq2] @test heq.variables == [hv1, hv2] - @test heq.natural_equation == nat_eq + @test source(heq) == nat_eq @test heq.parameters == Num[] @test heq.jacobian isa Matrix{Num} end heq3 = HarmonicEquation([eq1, eq2], [hv1, hv2], Num[], Num[1 1; 1 1]) - @test isempty(heq3.natural_equation.harmonics) + @test isempty(source(heq3).harmonics) end @testset "Parameter handling" begin From e5075a583762b8f698833a0171555a7bdc8ea1b3 Mon Sep 17 00:00:00 2001 From: Orjan Ameye Date: Thu, 3 Jul 2025 09:54:12 +0200 Subject: [PATCH 2/2] fix: correct type parameter syntax in HarmonicEquation constructor --- src/HarmonicEquation.jl | 2 +- test/HarmonicEquation.jl | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/HarmonicEquation.jl b/src/HarmonicEquation.jl index 9323ce0..2c51ebb 100644 --- a/src/HarmonicEquation.jl +++ b/src/HarmonicEquation.jl @@ -59,7 +59,7 @@ mutable struct HarmonicEquation{T} parameters::Vector{Num}, jacobian::Matrix{Num}, source_equations::T, - ) where T + ) where {T} return new{T}(equations, variables, parameters, jacobian, source_equations) end end diff --git a/test/HarmonicEquation.jl b/test/HarmonicEquation.jl index 4343cad..d498088 100644 --- a/test/HarmonicEquation.jl +++ b/test/HarmonicEquation.jl @@ -37,7 +37,6 @@ hv2 = HarmonicVariable(v, "test", "v", Num(1.0), y) # Test constructor @testset "Construction" begin - heq1 = HarmonicEquation([eq1, eq2], [hv1, hv2], nat_eq) heq2 = HarmonicEquation([eq1, eq2], [hv1, hv2], Num[], nat_eq) for heq in [heq1, heq2] @@ -49,7 +48,9 @@ hv2 = HarmonicVariable(v, "test", "v", Num(1.0), y) @test heq.jacobian isa Matrix{Num} end - heq3 = HarmonicEquation([eq1, eq2], [hv1, hv2], Num[], Num[1 1; 1 1]) + heq3 = HarmonicEquation( + [eq1, eq2], [hv1, hv2], Num[], Num[1 1; 1 1], DifferentialEquation() + ) @test isempty(source(heq3).harmonics) end