From 18855ab059934e859d72b165674ef2fef92c75cc Mon Sep 17 00:00:00 2001 From: DhairyaLGandhi Date: Wed, 23 Apr 2025 17:28:09 +0530 Subject: [PATCH 1/2] chore: copy MTKparameter fields only if not empty --- src/systems/parameter_buffer.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 4d33aa0a06..6142c95776 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -319,8 +319,8 @@ function Base.copy(p::MTKParameters) initials = copy(p.initials) discrete = Tuple(eltype(buf) <: Real ? copy(buf) : copy.(buf) for buf in p.discrete) constant = Tuple(eltype(buf) <: Real ? copy(buf) : copy.(buf) for buf in p.constant) - nonnumeric = copy.(p.nonnumeric) - caches = copy.(p.caches) + nonnumeric = isempty(p.nonnumeric) ? p.nonnumeric : copy.(p.nonnumeric) + caches = isempty(p.caches) ? p.caches : copy.(p.caches) return MTKParameters( tunable, initials, From e8b1d8ee0e5dedd410f149e155c9654ff4941b3c Mon Sep 17 00:00:00 2001 From: DhairyaLGandhi Date: Wed, 23 Apr 2025 17:46:19 +0530 Subject: [PATCH 2/2] test: add test to check field equality --- test/mtkparameters.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index 22201b1988..55de0768e0 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -18,6 +18,11 @@ ivs = Dict(c => 3a, d => 4, e => [5.0, 6.0, 7.0], ps = MTKParameters(sys, ivs) @test_nowarn copy(ps) +ps_copy = copy(ps) +ps_field_equals = map(fieldnames(typeof(ps))) do f + getfield(ps, f) == getfield(ps_copy, f) +end +@test all(ps_field_equals) # dependent initialization, also using defaults @test getp(sys, a)(ps) == getp(sys, b)(ps) == getp(sys, c)(ps) == 0.0 @test getp(sys, d)(ps) isa Int