getindex(::ComponentVector, ::Symbol) returns a copy, while getproperty returns a view

Aperta
#337 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
julia
Ambito
data

Direzione di ricerca

Inizia riproducendo gli esempi Julia con ComponentArrays v0.15.32, confrontando getindex(u, :a) con getproperty(u, :a). Individua quindi le implementazioni di getindex e getproperty e aggiungi o aggiorna i test che mostrano che l indicizzazione tramite simbolo restituisce una view le cui mutazioni influiscono sul vettore piatto originale.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

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
Lingua principale
Julia
Stelle
378
Fork
43
Merge medio
6h 49m
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di SciML/ComponentArrays.jl

Tutte le issue di SciML/ComponentArrays.jl

Issue simili

Altre issue su Julia

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.