Extended support for customisable kernels #1227
-
I am wanting to run multiple simulations with different kernels resulting in different beaching probabilities for particles. Rather than specifying the kernels manually, I would like to specify them programmatically. I initially thought of doing a closure which returned the beaching kernel, however the resulting function is not compiling to C properly. I feel like my usecase isn't too niche; is there a solution where I could programmatically specify kernels like this? My code is the following: def get_beaching_kernel(proba_beach):
"""
A closure to return the beaching kernel
"""
def kernel(particle, fieldset, time):
"""
Beaches the particle if it is within one gridcell of the land.
"""
proba_dt = 1 - math.exp(- particle.dt / proba_beach) # Converting to probability to apply for each timestep
if fieldset.land[particle] > 0: # ie. if particle is in beaching region
particle.in_beaching_region = 1
if ParcelsRandom.random() < proba_dt:
particle.beach = 1
else:
particle.in_beaching_region = 0
return kernel
pset.execute(AdvectionRK4 + pset.Kernel(get_beaching_kernel(proba_beach=0.5)),
runtime=timedelta(days=24), # runtime controls the interval of the plots
dt=timedelta(minutes=5),
) which gives this error
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The reason for this error is that you can't assign the Instead, you should use See also https://oceanparcels.org/gh-pages/html/#parcels.fieldset.FieldSet.add_constant |
Beta Was this translation helpful? Give feedback.
The reason for this error is that you can't assign the
proba_beach
variable like this; it does not work for the JIT-compiler.Instead, you should use
FieldSet.add_constant('proba_beach', value)
and then usefieldset.proba_beach
within the Kernel.See also https://oceanparcels.org/gh-pages/html/#parcels.fieldset.FieldSet.add_constant