From b42b1e1b5b53008f827a0d1b76cbe073887fac77 Mon Sep 17 00:00:00 2001 From: mtsch Date: Tue, 17 Dec 2024 20:02:21 +1300 Subject: [PATCH 1/5] Fix normalization in single_particle_density --- src/strategies_and_params/poststepstrategy.jl | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/strategies_and_params/poststepstrategy.jl b/src/strategies_and_params/poststepstrategy.jl index 49dfb282b..962770214 100644 --- a/src/strategies_and_params/poststepstrategy.jl +++ b/src/strategies_and_params/poststepstrategy.jl @@ -267,20 +267,16 @@ julia> single_particle_density(v; component=1) * [`SingleParticleDensity`](@ref) """ -function single_particle_density(dvec; component=0) +function single_particle_density(dvec::AbstractDVec; component=0) K = keytype(dvec) V = float(valtype(dvec)) M = num_modes(K) - N = num_particles(K) - result = mapreduce( - +, pairs(dvec); - init=MultiScalar(ntuple(_ -> zero(V), Val(M))) - ) do (k, v) + result = sum(pairs(dvec); init=MultiScalar(ntuple(_ -> zero(V), Val(M)))) do (k, v) MultiScalar(v^2 .* single_particle_density(k; component)) end - return result.tuple ./ sum(result.tuple) .* N + return result.tuple ./ norm(dvec) end function single_particle_density(add::SingleComponentFockAddress; component=0) From 3f6ecbf4ba7e45eb44d5cd88e542fdfe462c4547 Mon Sep 17 00:00:00 2001 From: mtsch Date: Tue, 17 Dec 2024 20:05:13 +1300 Subject: [PATCH 2/5] fix tests --- test/lomc.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/lomc.jl b/test/lomc.jl index 0c4aeca14..2f1bb7037 100644 --- a/test/lomc.jl +++ b/test/lomc.jl @@ -516,7 +516,9 @@ Random.seed!(1234) @test single_particle_density(address) == (1, 3, 3) @test single_particle_density(address; component=1) == (1, 2, 3) @test single_particle_density(address; component=2) == (0, 1, 0) - @test single_particle_density(DVec(address => 1); component=2) == (0, 7, 0) + @test single_particle_density(DVec(address => 1); component=0) == (1, 3, 3) + @test single_particle_density(DVec(address => 2); component=1) == (2, 4, 6) + @test single_particle_density(DVec(address => 3); component=2) == (0, 3, 0) end end end From 64f8fcdb5936ebe8eddb25fff79f6f1b4726ee92 Mon Sep 17 00:00:00 2001 From: mtsch Date: Tue, 17 Dec 2024 20:49:27 +1300 Subject: [PATCH 3/5] fix the fix --- src/strategies_and_params/poststepstrategy.jl | 2 +- test/lomc.jl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/strategies_and_params/poststepstrategy.jl b/src/strategies_and_params/poststepstrategy.jl index 962770214..527472d88 100644 --- a/src/strategies_and_params/poststepstrategy.jl +++ b/src/strategies_and_params/poststepstrategy.jl @@ -276,7 +276,7 @@ function single_particle_density(dvec::AbstractDVec; component=0) MultiScalar(v^2 .* single_particle_density(k; component)) end - return result.tuple ./ norm(dvec) + return result.tuple ./ sum(abs2, dvec) end function single_particle_density(add::SingleComponentFockAddress; component=0) diff --git a/test/lomc.jl b/test/lomc.jl index 2f1bb7037..2b82db05e 100644 --- a/test/lomc.jl +++ b/test/lomc.jl @@ -517,8 +517,8 @@ Random.seed!(1234) @test single_particle_density(address; component=1) == (1, 2, 3) @test single_particle_density(address; component=2) == (0, 1, 0) @test single_particle_density(DVec(address => 1); component=0) == (1, 3, 3) - @test single_particle_density(DVec(address => 2); component=1) == (2, 4, 6) - @test single_particle_density(DVec(address => 3); component=2) == (0, 3, 0) + @test single_particle_density(DVec(address => 2); component=1) == (1, 2, 3) + @test single_particle_density(DVec(address => 3); component=2) == (0, 1, 0) end end end From 50be36a77e3b633985791b97c5b84f0f888dc8e5 Mon Sep 17 00:00:00 2001 From: mtsch Date: Tue, 17 Dec 2024 21:50:19 +1300 Subject: [PATCH 4/5] fix doctests --- src/strategies_and_params/poststepstrategy.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/strategies_and_params/poststepstrategy.jl b/src/strategies_and_params/poststepstrategy.jl index 527472d88..6d0f54d5e 100644 --- a/src/strategies_and_params/poststepstrategy.jl +++ b/src/strategies_and_params/poststepstrategy.jl @@ -260,14 +260,14 @@ julia> single_particle_density(v) (0.2, 1.0, 1.6, 1.0, 0.2) julia> single_particle_density(v; component=1) -(0.0, 1.6, 1.6, 0.4, 0.4) +(0.0, 0.8, 0.8, 0.2, 0.2) ``` # See also * [`SingleParticleDensity`](@ref) """ -function single_particle_density(dvec::AbstractDVec; component=0) +function single_particle_density(dvec; component=0) K = keytype(dvec) V = float(valtype(dvec)) M = num_modes(K) From 99c70f96253c7499f34e0ba84ef4cf0ab0e6c7b8 Mon Sep 17 00:00:00 2001 From: mtsch Date: Fri, 20 Dec 2024 21:31:47 +1100 Subject: [PATCH 5/5] Update src/strategies_and_params/poststepstrategy.jl Co-authored-by: Joachim Brand --- src/strategies_and_params/poststepstrategy.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/strategies_and_params/poststepstrategy.jl b/src/strategies_and_params/poststepstrategy.jl index 6d0f54d5e..33c702daa 100644 --- a/src/strategies_and_params/poststepstrategy.jl +++ b/src/strategies_and_params/poststepstrategy.jl @@ -273,7 +273,7 @@ function single_particle_density(dvec; component=0) M = num_modes(K) result = sum(pairs(dvec); init=MultiScalar(ntuple(_ -> zero(V), Val(M)))) do (k, v) - MultiScalar(v^2 .* single_particle_density(k; component)) + MultiScalar(abs2(v) .* single_particle_density(k; component)) end return result.tuple ./ sum(abs2, dvec)