Updating fields with vectors contained inside them returns a copy of the vector rather than a view into the vector. In addition to preventing the original flat vector from being modified, this allocates a new array.
julia> u = ComponentArray(a = [0.0, 0.0], b = [0.0, 0.0])
julia> getindex(u, :a)
2-element Vector{Float64}: #notice it's a plain vector and not a view
0.0
0.0
julia> getproperty(u, :a)
2-element view(::Vector{Float64}, 1:2) with eltype Float64:
0.0
0.0
julia> u.a.= 0.0
julia> u[:a][1] += 1.0
julia> u.a[1]
0.0 (should be 1.0)
julia> u.a.= 0.0
julia> u.a[1] += 1.0
julia> u.a[1]
1.0 (works fine)
Why do we need this?
This is related to issue: #338
I'm going to post an additional issue about this, but if we ever had something like this:
julia> u = ComponentArray(mass_fractions = (methanol = zeros(n_cells), water = zeros(n_cells)))
and we wanted to loop through it, doing something like:
for cell_id in 1:n_cells
for species_name in keys(u.mass_fractions)
u.mass_fractions[species_name][cell_id] += 1.0
end
end
the original array would not be modified.
The for species_name in keys(u.mass_fractions) is a terrible pattern for performance which you can see here:
https://gist.github.com/Jolt8/3b7cf79326f9ffbee6626a0d39e6e428
Julia Version 1.12.0
ComponentArrays v0.15.32
Updating fields with vectors contained inside them returns a copy of the vector rather than a view into the vector. In addition to preventing the original flat vector from being modified, this allocates a new array.
Why do we need this?
This is related to issue: #338
I'm going to post an additional issue about this, but if we ever had something like this:
and we wanted to loop through it, doing something like:
the original array would not be modified.
The for species_name in keys(u.mass_fractions) is a terrible pattern for performance which you can see here:
https://gist.github.com/Jolt8/3b7cf79326f9ffbee6626a0d39e6e428