Skip to content

Commit ccd4acb

Browse files
committed
Avoid original meshes being modified when merging
1 parent 2c86260 commit ccd4acb

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/Mesh/Mesh.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ julia> add_property!(r, :absorbed_PAR, [0.0, 0.0]);
174174
function add_property!(m::Mesh, prop::Symbol, data, nt = ntriangles(m))
175175
# Check if the data is an array and if not convert it to an array with length nt
176176
vecdata = data isa AbstractVector ? data : fill(data, nt)
177-
# Create new property if the one being added does not exist
177+
# Create new property if the one being added does not exist (make sure to copy)
178178
if !haskey(properties(m), prop)
179-
properties(m)[prop] = vecdata
179+
properties(m)[prop] = copy(vecdata)
180180
# Otherwise add to existing property
181181
else
182182
add_property!(properties(m), prop, vecdata)
@@ -207,7 +207,7 @@ julia> m = Mesh([e,r]);
207207
function Mesh(meshes::Vector{<:Mesh})
208208
@assert !isempty(meshes) "At least one mesh must be provided"
209209
@inbounds verts = copy(vertices(meshes[1]))
210-
@inbounds props = properties(meshes[1])
210+
@inbounds props = copy(properties(meshes[1]))
211211
if length(meshes) > 1
212212
@inbounds for i in 2:length(meshes)
213213
append!(verts, vertices(meshes[i]))

test/test_properties.jl

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,29 @@ e = PGP.Ellipse()
1919
t = PGP.Triangle()
2020

2121
PGP.add_property!(r, :prop, D.bar())
22-
eltype(PGP.properties(r)[:prop]) == D.bar
22+
@test eltype(PGP.properties(r)[:prop]) == D.bar
2323
PGP.add!(r, e, prop = D.foo())
24-
eltype(PGP.properties(r)[:prop]) == Union{D.bar, D.foo}
24+
@test eltype(PGP.properties(r)[:prop]) == Union{D.bar, D.foo}
2525
PGP.add!(r, t, prop = D.oomp())
26-
eltype(PGP.properties(r)[:prop]) == Union{D.bar, D.foo, D.oomp}
26+
@test eltype(PGP.properties(r)[:prop]) == Union{D.bar, D.foo, D.oomp}
2727

2828

29-
# Merging properties of two meshes
29+
# Merging meshes
30+
# Make sure property unions are created
3031
PGP.add_property!(e, :prop, D.foo())
3132
PGP.add_property!(t, :prop, D.bar())
3233
m = PGP.Mesh([t, e])
33-
eltype(PGP.properties(m)[:prop]) == Union{D.bar, D.foo}
34-
34+
@test eltype(PGP.properties(m)[:prop]) == Union{D.bar, D.foo}
35+
@test eltype(PGP.properties(e)[:prop]) == D.foo
36+
@test eltype(PGP.properties(t)[:prop]) == D.bar
37+
# Make sure original meshes are not modified
38+
l1 = length(PGP.properties(e)[:prop])
39+
l2 = length(PGP.properties(t)[:prop])
40+
l3 = length(PGP.properties(m)[:prop])
41+
@test l1 + l2 == l3
42+
nv1 = PGP.nvertices(e)
43+
nv2 = PGP.nvertices(t)
44+
nv3 = PGP.nvertices(m)
45+
@test nv1 + nv2 == nv3
3546

3647
end

0 commit comments

Comments
 (0)