Closed VeckoTheGecko closed 2 years ago
Parcels uses its own ParcelsRandom
library, see e.g. the Diffusion Kernels at
https://github.com/OceanParcels/parcels/blob/dd3ba5b3f99b106b275feb87389a63b4b03f4216/parcels/application_kernels/advectiondiffusion.py#L29-L30
The full list of supported random functions is
ParcelsRandom.seed()
ParcelsRandom.random()
ParcelsRandom.uniform(low, high)
, ParcelsRandom.randint(low, high)
ParcelsRandom.normalvariate(loc, scale)
ParcelsRandom.expovariate(lamb)
ParcelsRandom.vonmisesvariate(mu, kappa)
The code is at https://github.com/OceanParcels/parcels/blob/752efb1f57c06fa9e9cc6e1e4ab871c35ffa3137/parcels/rng.py#L137-L195
This is really weird. Replacing
from parcels.rng import random
...
rand = random()
with
import parcels.rng as ParcelsRandom
...
rand = ParcelsRandom.random()
fixes all the issues. From my understanding, there shouldn't be anything different between the two pieces of code (as random()
is imported from parcels.rng
). Is there something I'm missing @erikvansebille ? Is this error reproducible? Could this be due to how it compiles to C?
I posted in the Python Discord #help-donut
channel about this and I think they're equally stumped 😂
Yes indeed, it's intentional that you have to use the ParcelsRandom
methods; this has to do with the code converted to C, which can't shadow the built-in random
function. Long technical story...
A warning indeed is a good idea, just like we issue a warning when someone uses numpy
in a kernel.
https://github.com/OceanParcels/parcels/blob/34eb1cfeb7fc92963ce32f75788c3cdef2466b10/parcels/compilation/codegenerator.py#L318-L321
Parcels version: 2.3.1 OS: Windows 10
I've been trying to specify random behaviour within custom parcels kernels, however including any call to
parcels.rng.random
orrandom.random
causes errors both when usingJITParticle
andScipyParticle
for the particle set. From prior parcels code I have seen having kernels with random behaviour does seem to work in an older version, however I'm not sure what I would need to do to get this to work in this version of parcels.The following is a trimmed example of my code, with the file
random_not_working.nc
available here (for 7 days after posting).Where I get the following error messages for
JITParticle
and
ScipyParticle