-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create example_Initialise_JIT_particles_with_initial_values.py #1
Open
HaydenSchilling
wants to merge
3
commits into
OceanParcels:master
Choose a base branch
from
HaydenSchilling:JIT-particle-initialisation-example
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
Examples/example_Initialise_JIT_particles_with_initial_values.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this actually mean that the temperature at time=0 is also written to the ParticleFile?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was suppose to... but looking closer it appears it does not to.
I originally checked it looking at distance calulations in another script. The calculations worked with this method (using a prev_lat variable --- Did not work without this kernel) but looking closer it is calculating these initial values but not recording them in the particle file for any variable. This might need some more work. Sorry.