Skip to content

Looping over nested fields #338

@Jolt8

Description

@Jolt8

This is related to issue: #337

Let's say we have the ComponentArray:

n_cells = 100

u = ComponenArray(mass_fractions = (methanol = zeros(n_cells), water = zeros(n_cells))

And we wanted to loop over mass fractions to apply species_diffusion, advection, a chemical reaction, etc.

for cell_id in 1:n_cells
    for species_name in u.mass_fractions
        u.mass_fractions[species_name][cell_id] += 1.0
    end
end

Well right now, this pattern is terrible for performance and also doesn't work with Polyester's @Batch.

I've also tried:

for cell_id in 1:n_cells
    map(keys(u.mass_fractions) do species_name
        u.mass_fractions[species_name][cell_id] += 1.0
    end
end

but this also performs terribly and also performs terribly with polyester.

Pure NamedTuples perform really well when not using @Batch. For example:

u = (mass_fractions = (methanol = zeros(n_cells), water = zeros(n_cells))

for cell_id in 1:n_cells
    map(keys(u.mass_fractions) do species_name
        u.mass_fractions[species_name][cell_id] += 1.0
    end
end

doesn't allocate at all and performs amazingly, but if you add @Batch to the loop, it allocates and performance goes down the drain. However, NamedTuples aren't ideal as non-vector quantities stored inside a single field cannot be mutated.

If you want to see the performance differences see here:
https://gist.github.com/Jolt8/3b7cf79326f9ffbee6626a0d39e6e428

If there was a dedicated method for looping over nested fields in a ComponentArray that also worked with Polyester, that would be absolutely amazing!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions