diff --git a/Examples/example_Initialise_JIT_particles_with_initial_values.py b/Examples/example_Initialise_JIT_particles_with_initial_values.py new file mode 100644 index 0000000..be31022 --- /dev/null +++ b/Examples/example_Initialise_JIT_particles_with_initial_values.py @@ -0,0 +1,28 @@ +### This example shows how to create a particle set in JIT mode while still sampling initial conditions of a variable eg. Temperature +### This works even with mulitple releases during a simulation +### This is an example of a solution to the github issue here: https://github.com/OceanParcels/parcels/issues/861 +### This code assumes you have already made the fieldset. (This is not a stand-only piece of code) + +# First create a particle class which samples values, in this case it is sampling temperature. +# Within this class create the variable you wish to sample (temp) and initialise it with a value of 0. +# Also create a variable (sampled) to identify if it is the particles day of release. + +class SampleParticle(JITParticle): # Define a new particle class + sampled = Variable('sampled', dtype = np.float32, initial = 0, to_write=False) # variable to identify if it is just released + temp = Variable('temp', dtype=np.float32, initial=0) # initialise temperature + +# Create the kernel to sample temperature +def SampleTemp(particle, fieldset, time): + particle.temp = fieldset.temp[time, particle.depth, particle.lat, particle.lon] + +# Create the kernel to get the initial temperature value for each particle. +# Note at this stage it is calculating the initial values but not recording them in the particle set file +# This would be useful for calcuating distance travelled etc but not if you want a record of day 0 conditions. +def SampleInitial(particle, fieldset, time): + if particle.sampled == 0: + particle.temp = fieldset.temp[time, particle.depth, particle.lat, particle.lon] + particle.sampled = 1 + +# Create your kernel list to run, +# Put the SampleInitial Kernel before the advection kernel to get the initial value of temp before the 1st advection step occurs. +kernels = SampleInitial + pset.Kernel(AdvectionRK4) + SampleTemp