Skip to content

Can I use a random number generator inside a kernel?  #1008

@hkhirsh

Description

@hkhirsh

I just started using Python and OceanParcels and this is my first time posting a question in Github. I am trying to incorporate kernels to unbeach particles that end up on land - adapted from @delandmeterp Philippe Delandmeter's code here: https://github.com/OceanParcels/Parcelsv2.0PaperNorthSeaScripts/blob/master/northsea_mp_kernels.py

If a particle is tagged as "beached" then I want to "unbeach" it by "bumping" the coordinates aka adding a random number to the latitude and longitude. I tried this:

particle.lon += random.uniform(-0.05, 0.05)
particle.lat += random.uniform(-0.05, 0.05) 

But for some reason the random random number generator does not work inside a kernel (see full kernel below). I also tried np.random.uniform. It works fine if I run random.uniform(-0.05, 0.05) outside of the kernel.

I am importing random like this:
from parcels import rng as random

Philippe Delandmeter uses random.uniform in his BrownianMotion2D kernel so I'm not sure why it doesn't work inside a kernel for me.

I get an error that says:

NotImplementedError: Cannot convert 'random' used in kernel to C-code

Thank you in advance for any advice! I really appreciate it!!

Here is the unbeaching kernel I am trying to use:


def UnBeaching(particle, fieldset, time):
    if particle.beached == 4:     #4 undicates that a particle has been beached
        print("RUNNING UNBEACHING FOR:")
        lat = particle.lat
        lon = particle.lon
        print(lat)
        print(lon)

        particle.lon += np.random.uniform(-0.05, 0.05)
        particle.lat += np.random.uniform(-0.05, 0.05) 

        print("New LATLON:")
        lat = particle.lat
        lon = particle.lon
        print(lat)
        print(lon)

        particle.beached = 0
        particle.unbeachCount += 1

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