Skip to content

Create kernels from list of Python functions #1347

@VeckoTheGecko

Description

@VeckoTheGecko

Programmatically combining kernels is useful for those who are interested in running various simulations with different particle behaviour.

Currently kernels can be combined by hardcoding:

# Define your kernel functions
# ...

# Combine kernels
combined_kernel = pset.Kernel(kernel_1) + pset.Kernel(kernel_2) + pset.Kernel(kernel_3)

If you want to create a combined kernel from a list, you can do:

import functools

# Define your kernel functions
# ...

def get_combined_kernel(pset, kernel_lst):
    """
    Returns a combined kernel given a list of kernels.
    """
    kernels = (pset.Kernel(i) for i in kernel_lst)
    return functools.reduce(lambda x, y: x+y, kernels)

kernel_lst = [kernel_1, kernel_2, kernel_3]
combined_kernel = get_combined_kernel(pset, kernel_lst)

Although this second method is much nicer and much more extensible (not requiring things to be hardcoded, and allowing for the programmatic creation of the list by appending the functions as needed), its not accesible for those who are not as familiar with Python (and the functools package). Adding native parcels support for creating kernels from a list of functions would be nice, especially since I imagine this isn't an uncommon usecase.

solution:

combined_kernel = pset.Kernel.from_list(
    [kernel_1, kernel_2, kernel_3]
)

Anecdotally, I used this in my work when investigating beaching strategies of plastic particles.

I'm happy to work on this (just wanting to check if this is a good feature, get feedback, and whether its congruent with the API design).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions