Skip to content

Conversation

@erikvansebille
Copy link
Member

This PR implements #2275 by renaming the pyfunc argument to kernels.

It also simplifies the pset.execute() by assuming that the input is a list of functions, rather than Kernel objects. This means that pset.Kernel is not needed anymore, and we can get rid of a few helper functions, decluttering the code

Comment on lines 185 to 193
def __add__(self, kernel):
if isinstance(kernel, types.FunctionType):
kernel = type(self)(self.fieldset, self.ptype, pyfuncs=[kernel])
kernel = type(self)([kernel], self.fieldset, self.ptype)
return self.merge(kernel)

def __radd__(self, kernel):
if isinstance(kernel, types.FunctionType):
kernel = type(self)(self.fieldset, self.ptype, pyfuncs=[kernel])
kernel = type(self)([kernel], self.fieldset, self.ptype)
return kernel.merge(self)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this PR - but what functionality does the merging of kernels have in version 4 of Parcels? Are there any times users (or us internally) would want to merge kernels?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b1e2e2f

Comment on lines +69 to +70
if isinstance(f, Kernel):
f = f._kernels # unwrap
Copy link
Contributor

@VeckoTheGecko VeckoTheGecko Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? Not sure why this was added - slipped by me during the last review

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is an addition after your reviews. We need this for one specific unit test (below) where a set.execute() is called in a for-loop
https://github.com/erikvansebille/Parcels/blob/32bcf937cb3776d3ac177d992ccf698de435954a/tests/test_particleset_execute.py#L253-L256

The point is that there is a difference between a function and a Kernel, because the latter has the positionupdatekernel added.
https://github.com/erikvansebille/Parcels/blob/32bcf937cb3776d3ac177d992ccf698de435954a/src/parcels/_core/kernel.py#L283-L287

If a user would provide the function in the for-loop above, then the code above is triggered on each iteration, while if they provide the kernel object it's only triggered the first time

This difference between calling pest.execute with a function or a Kernel object may cause confusion to users, I now realise. But the code at the end of the kernel-loop is essential to make the updating of variables correct; this has been a lot of headaches in #2333, so I'm reluctant to change this again.

Something to think about when we're back from holidays....

Comment on lines +396 to +401
if isinstance(kernels, Kernel):
self._kernel = kernels
else:
if not isinstance(kernels, list):
kernels = [kernels]
self._kernel = Kernel(kernels, self.fieldset, self._ptype)
Copy link
Contributor

@VeckoTheGecko VeckoTheGecko Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kernels input arg can't be Kernel object anymore. Also inverting the isinstance check would be more reliable

Suggested change
if isinstance(kernels, Kernel):
self._kernel = kernels
else:
if not isinstance(kernels, list):
kernels = [kernels]
self._kernel = Kernel(kernels, self.fieldset, self._ptype)
if isinstance(kernels, types.FunctionType):
kernels = [kernels]
self._kernel = Kernel(kernels, self.fieldset, self._ptype)

suggestion requires import types at the top of the file

Copy link
Member Author

@erikvansebille erikvansebille Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is related to the comment above #2490 (comment); there is a use-case for still accepting Kernels as input. unless we find another way to figure out whether the position_update_kernel has to be added or not

@github-project-automation github-project-automation bot moved this from Backlog to Ready in Parcels development Jan 22, 2026
@VeckoTheGecko
Copy link
Contributor

I think we can merge after those two comments above are addressed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

2 participants