From 459fe02cda2a7b19aca9e4e62fd6823da443dfaf Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Wed, 23 Sep 2026 12:07:08 -0400 Subject: [PATCH 1/2] Clamp parameter_estim_ode! particle positions to the box elementwise `ode_update_particle_states!` used `max(position, lb)` / `min(position, ub)` on SVectors, which compare lexicographically and return one whole vector, so particle coordinates could leave `[lb, ub]` (and in-box coordinates could be overwritten by the bound). Use broadcasting, matching `update_particle_states!` in kernels.jl. Co-Authored-By: Chris Rackauckas Co-Authored-By: Claude Opus 5.5 (1M context) Agent-Harness: Claude Code 2.1.280 Agent-Model: claude-opus-5-5[1m] Agent-Session: https://claude.ai/code/session_01Vrsu4PESdABpNaxYiTBVfd Claude-Session: https://claude.ai/code/session_01Vrsu4PESdABpNaxYiTBVfd --- src/ode_pso.jl | 4 ++-- test/ode_pso.jl | 18 ++++++++++++++++++ test/runtests.jl | 1 + 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 test/ode_pso.jl diff --git a/src/ode_pso.jl b/src/ode_pso.jl index 202fa57..9ed9162 100644 --- a/src/ode_pso.jl +++ b/src/ode_pso.jl @@ -15,8 +15,8 @@ @set! particle.velocity = updated_velocity @set! particle.position = particle.position + particle.velocity - update_pos = max(particle.position, lb) - update_pos = min(update_pos, ub) + update_pos = max.(particle.position, lb) + update_pos = min.(update_pos, ub) @set! particle.position = update_pos @inbounds gpu_particles[i] = particle diff --git a/test/ode_pso.jl b/test/ode_pso.jl new file mode 100644 index 0000000..5c5f311 --- /dev/null +++ b/test/ode_pso.jl @@ -0,0 +1,18 @@ +using ParallelParticleSwarms, KernelAbstractions, StaticArrays, Test + +@testset "parameter_estim_ode! particle update clamps each coordinate to the box" begin + lb = SVector(-10.0f0, -10.0f0) + ub = SVector(10.0f0, 10.0f0) + positions = [SVector(5.0f0, -20.0f0), SVector(-20.0f0, 5.0f0), SVector(20.0f0, -20.0f0)] + backend = CPU() + update! = ParallelParticleSwarms.ode_update_particle_states!(backend) + for x in positions + single = [ParallelParticleSwarms.SPSOParticle(x, zero(x), Inf32, x, Inf32)] + update!( + single, lb, ub, ParallelParticleSwarms.SPSOGBest(x, Inf32), 0.0f0; + ndrange = 1 + ) + KernelAbstractions.synchronize(backend) + @test single[1].position == clamp.(x, lb, ub) + end +end diff --git a/test/runtests.jl b/test/runtests.jl index 6a93248..a474394 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -12,6 +12,7 @@ run_tests(; @safetestset "Reinitialization tests" include("./reinit.jl") @safetestset "SimpleLBFGS hybrid polish" include("./lbfgs.jl") @safetestset "Algorithm interface tests" include("./interface.jl") + @safetestset "ODE parameter estimation particle update" include("./ode_pso.jl") #TODO: Current throws warning for redefinition with the use of @testset multiple times. Migrate to TestItemRunners.jl return @testset for BACKEND in unique(("CPU", CI_GROUP)) From 15bb81b86034ed9f91be61bd074261341e08fd5c Mon Sep 17 00:00:00 2001 From: Chris Rackauckas Date: Sat, 17 Oct 2026 19:59:31 +0000 Subject: [PATCH 2/2] Exercise ODE particle bounds regression on each test backend Allocate particle storage on the selected backend and copy synchronized results to the host for coordinatewise assertions. Run the regression in the CPU/CUDA backend loop. Co-Authored-By: Chris Rackauckas Co-Authored-By: Codex Agent-Harness: Codex CLI 0.162.1 Agent-Model: gpt-6.1-sol Agent-Session: local session 01a14b6e-4c2a-77d2-8d02-6dcd6c6e46d4 (no session URL) --- test/ode_pso.jl | 9 ++++++--- test/runtests.jl | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/test/ode_pso.jl b/test/ode_pso.jl index 5c5f311..3669d9f 100644 --- a/test/ode_pso.jl +++ b/test/ode_pso.jl @@ -1,18 +1,21 @@ using ParallelParticleSwarms, KernelAbstractions, StaticArrays, Test +include("./utils.jl") + @testset "parameter_estim_ode! particle update clamps each coordinate to the box" begin lb = SVector(-10.0f0, -10.0f0) ub = SVector(10.0f0, 10.0f0) positions = [SVector(5.0f0, -20.0f0), SVector(-20.0f0, 5.0f0), SVector(20.0f0, -20.0f0)] - backend = CPU() update! = ParallelParticleSwarms.ode_update_particle_states!(backend) for x in positions - single = [ParallelParticleSwarms.SPSOParticle(x, zero(x), Inf32, x, Inf32)] + particle = ParallelParticleSwarms.SPSOParticle(x, zero(x), Inf32, x, Inf32) + single = KernelAbstractions.allocate(backend, typeof(particle), 1) + copyto!(single, [particle]) update!( single, lb, ub, ParallelParticleSwarms.SPSOGBest(x, Inf32), 0.0f0; ndrange = 1 ) KernelAbstractions.synchronize(backend) - @test single[1].position == clamp.(x, lb, ub) + @test Array(single)[1].position == clamp.(x, lb, ub) end end diff --git a/test/runtests.jl b/test/runtests.jl index a474394..22ecd29 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -12,11 +12,11 @@ run_tests(; @safetestset "Reinitialization tests" include("./reinit.jl") @safetestset "SimpleLBFGS hybrid polish" include("./lbfgs.jl") @safetestset "Algorithm interface tests" include("./interface.jl") - @safetestset "ODE parameter estimation particle update" include("./ode_pso.jl") #TODO: Current throws warning for redefinition with the use of @testset multiple times. Migrate to TestItemRunners.jl return @testset for BACKEND in unique(("CPU", CI_GROUP)) global GROUP = BACKEND + @testset "$(BACKEND) ODE parameter estimation particle update" include("./ode_pso.jl") @testset "$(BACKEND) optimizers tests" include("./gpu.jl") GC.gc(true) @testset "$(BACKEND) optimizers with constraints tests" include("./constraints.jl")