Skip to content

Commit

Permalink
Merge pull request #715 from OceanParcels/kernel_delete_cfiles_argument
Browse files Browse the repository at this point in the history
delete_cfiles argument in Kernel initialisation
  • Loading branch information
erikvansebille authored Jan 29, 2020
2 parents 4066958 + 41d62dc commit 0fa7bfd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
6 changes: 4 additions & 2 deletions parcels/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Kernel(object):
:arg fieldset: FieldSet object providing the field information
:arg ptype: PType object for the kernel particle
:param delete_cfiles: Boolean whether to delete the C-files after compilation in JIT mode (default is True)
Note: A Kernel is either created from a compiled <function ...> object
or the necessary information (funcname, funccode, funcvars) is provided.
Expand All @@ -69,10 +70,11 @@ class Kernel(object):
"""

def __init__(self, fieldset, ptype, pyfunc=None, funcname=None,
funccode=None, py_ast=None, funcvars=None, c_include=""):
funccode=None, py_ast=None, funcvars=None, c_include="", delete_cfiles=True):
self.fieldset = fieldset
self.ptype = ptype
self._lib = None
self.delete_cfiles = delete_cfiles

# Derive meta information from pyfunc, if not given
self.funcname = funcname or pyfunc.__name__
Expand Down Expand Up @@ -171,7 +173,7 @@ def __del__(self):
_ctypes.FreeLibrary(self._lib._handle) if platform == 'win32' else _ctypes.dlclose(self._lib._handle)
del self._lib
self._lib = None
if path.isfile(self.lib_file):
if path.isfile(self.lib_file) and self.delete_cfiles:
[remove(s) for s in [self.src_file, self.lib_file, self.log_file]]

@property
Expand Down
9 changes: 6 additions & 3 deletions parcels/particleset.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,10 +603,13 @@ def density(self, field=None, particle_val=None, relative=False, area_scale=Fals

return density

def Kernel(self, pyfunc, c_include=""):
def Kernel(self, pyfunc, c_include="", delete_cfiles=True):
"""Wrapper method to convert a `pyfunc` into a :class:`parcels.kernel.Kernel` object
based on `fieldset` and `ptype` of the ParticleSet"""
return Kernel(self.fieldset, self.ptype, pyfunc=pyfunc, c_include=c_include)
based on `fieldset` and `ptype` of the ParticleSet
:param delete_cfiles: Boolean whether to delete the C-files after compilation in JIT mode (default is True)
"""
return Kernel(self.fieldset, self.ptype, pyfunc=pyfunc, c_include=c_include,
delete_cfiles=delete_cfiles)

def ParticleFile(self, *args, **kwargs):
"""Wrapper method to initialise a :class:`parcels.particlefile.ParticleFile`
Expand Down
13 changes: 13 additions & 0 deletions tests/test_kernel_execution.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from os import path
from parcels import (
FieldSet, ParticleSet, ScipyParticle, JITParticle, ErrorCode, KernelError,
OutOfBoundsError
Expand Down Expand Up @@ -262,3 +263,15 @@ def simpleKernel(particle, fieldset, time):

pset = ParticleSet(fieldset, pclass=ptype[mode], lon=[0.], lat=[0.])
pset.execute(pset.Kernel(simpleKernel), endtime=3., dt=1.)


@pytest.mark.parametrize('delete_cfiles', [True, False])
def test_execution_keep_cfiles(fieldset, delete_cfiles):
pset = ParticleSet(fieldset, pclass=JITParticle, lon=[0.], lat=[0.])
pset.execute(pset.Kernel(DoNothing, delete_cfiles=delete_cfiles), endtime=1., dt=1.)
cfile = pset.kernel.src_file
del pset.kernel
if delete_cfiles:
assert not path.exists(cfile)
else:
assert path.exists(cfile)

0 comments on commit 0fa7bfd

Please sign in to comment.