Skip to content
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

Analytical Advection in jit #1612

Draft
wants to merge 60 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
91fb4e0
First attempt to create header-file for analytical advection in JIT
erikvansebille Jul 16, 2024
90e1190
First semi-complete C-version of analytical-advection (not working)
erikvansebille Jul 18, 2024
81d7c1d
Fixing customed_Cfunc_header_test to use particle_dlon
erikvansebille Jul 18, 2024
314493a
Fixing small but important bug in advection computation
erikvansebille Jul 19, 2024
760fe58
Using functions for computing ds and rs
erikvansebille Jul 19, 2024
f48d052
Adding support for dt<0
erikvansebille Jul 22, 2024
6e96fe3
Adding 3D support to analyticaladvection
erikvansebille Jul 22, 2024
ebd11cb
renaming xgrid_loc, ygrid_loc to px, py
erikvansebille Jul 22, 2024
2a67fd1
Automatic loading of library when calling AdvectionAnalytical in JIT
erikvansebille Jul 22, 2024
beb1a0a
Updating function names for future support of 2D
erikvansebille Jul 22, 2024
eed3422
Implementing separate 2D kernel for JIT AnalyticalAdvection
erikvansebille Jul 22, 2024
1fb9de2
Support for 2D JIT AnalyticalAdvection in kernel.py
erikvansebille Jul 22, 2024
bfab79c
Merge branch 'master' into analyticaladvection_in_jit
erikvansebille Jul 22, 2024
cd325b8
FIxing timevarying flow in 2D mode
erikvansebille Jul 23, 2024
5c03327
Setting include_dirs to inlcude application_kernels
erikvansebille Jul 23, 2024
1f56ec9
Fixing 2D analyticaladvection to work with and without time
erikvansebille Jul 23, 2024
acba3fd
Adding particle.ti as function input
erikvansebille Jul 23, 2024
47f50a0
Fixing xi sampling code, and making unit test more subtle
erikvansebille Jul 25, 2024
d2c27f3
Fixing calling analyticaladvection with general particle.xi
erikvansebille Jul 25, 2024
c769886
Fixing small bug in jit analyticaladvection
erikvansebille Jul 25, 2024
2bc9af3
Fixing AnalyticalAdvection Kernels to also work well for negative speeds
erikvansebille Jul 26, 2024
74b0d83
Combining AnalyticalAdvection in 2D and 3D JIT into one C function
erikvansebille Jul 26, 2024
0edacc5
Using particle_ddepth in analyticaladvection
erikvansebille Jul 22, 2024
ff57384
FIxing Scipy AnalyticalAdvection to add to dlon,dlat,ddepth instead o…
erikvansebille Jul 26, 2024
d4c9a6a
Updating AnalyticalAdvection tutorial
erikvansebille Jul 26, 2024
96e6176
Fixing include statement in codegenerator
erikvansebille Jul 26, 2024
ae9f681
Support for Curvilinear Grids in AnalyticalAdvection
erikvansebille Jul 26, 2024
b2406fe
Merge branch 'master' into analyticaladvection_in_jit
erikvansebille Jul 26, 2024
116416b
Updating to change GridCode -> GridType
erikvansebille Jul 26, 2024
1fcf875
Generalising gridindexingtype for AnalyticalAdvection in JIT
erikvansebille Jul 26, 2024
2863265
Also writing first time of simulation in AnalyticalAdvection
erikvansebille Jul 29, 2024
c297795
Merge branch 'master' into analyticaladvection_in_jit
erikvansebille Jul 29, 2024
3913277
Moving c1,c2,c3,c4 calculation outside flow3D if-loop
erikvansebille Jul 29, 2024
46b9675
Changing gcode -> gtype in advectionanalytical.h
erikvansebille Jul 29, 2024
abbf683
setting counter inside for-loop for reduced clutter
erikvansebille Jul 31, 2024
6203413
Fixing bug in backward AnalyticalAdvection where wrong cell was used
erikvansebille Aug 2, 2024
e24041f
Fixing grid search bug, and increasing tolerance for grid search
erikvansebille Aug 12, 2024
c28a4df
Finding correct cell for analyticaladvection in a loop
erikvansebille Aug 12, 2024
8f34f69
Adding support for time-evolving fields in cell search
erikvansebille Aug 13, 2024
2f4cae3
Merge branch 'master' into analyticaladvection_in_jit
erikvansebille Aug 13, 2024
317172c
Update tutorial_advection to new parcels import
erikvansebille Aug 13, 2024
6bbbe70
Merge branch 'master' into analyticaladvection_in_jit
VeckoTheGecko Aug 16, 2024
0a98f39
Add noqa flags
VeckoTheGecko Aug 16, 2024
99d94b2
Using NEMO cell edges (e1v, e2u etc) in calculation of fluxes
erikvansebille Aug 16, 2024
405cc6d
Merge branch 'master' into analyticaladvection_in_jit
erikvansebille Aug 16, 2024
c5fe559
Merge branch 'analyticaladvection_in_jit' of https://github.com/Ocean…
erikvansebille Aug 16, 2024
c025d94
Fixing analyticaladvection to expect grid sizes in Nemo format
erikvansebille Aug 26, 2024
2ea2c73
Updating test_advection to use nemo gridsizes for AA
erikvansebille Aug 26, 2024
af46e0e
Merge branch 'master' into analyticaladvection_in_jit
erikvansebille Aug 26, 2024
a1750d5
Adding comment to also update Scipy AA code
erikvansebille Aug 26, 2024
f5c0e95
Fixing outofbounds test for flow3D == 0
erikvansebille Aug 26, 2024
4505636
Updating analyticaladvection tutorial to use Nemo cell edge convention
erikvansebille Aug 26, 2024
f557e3c
Adding mesh_mask_edges file for AA in nemo_curvilinear
erikvansebille Aug 27, 2024
71eb3f7
Updating mesh_mask_edges filename
erikvansebille Aug 27, 2024
fd7383f
Adding cell edges to AnalyticalAdvection in example_peninsula
erikvansebille Aug 27, 2024
f0974d4
Merge branch 'master' into analyticaladvection_in_jit
erikvansebille Aug 30, 2024
c98161e
Changing time interpolation scheme to use intermediate timesteps better
erikvansebille Aug 30, 2024
adce434
Merge branch 'analyticaladvection_in_jit' of https://github.com/Ocean…
erikvansebille Aug 30, 2024
4578997
Renaming direction to time_direction throughout
erikvansebille Aug 30, 2024
88aa327
Adding support for MITGCM grids in analyticaladvection.h
erikvansebille Sep 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/examples/example_nemo_curvilinear.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@ def run_nemo_curvilinear(mode, outfile, advtype="RK4"):
fieldset = parcels.FieldSet.from_nemo(
filenames, variables, dimensions, chunksize=chunksize
)
if advtype == "AA":
mesh_mask = f"{data_folder}/mesh_mask_edges.nc4"
fieldset.add_field(
parcels.Field.from_netcdf(
mesh_mask, "e2u", dimensions, interp_method="nearest"
)
)
fieldset.add_field(
parcels.Field.from_netcdf(
mesh_mask, "e1v", dimensions, interp_method="nearest"
)
)
fieldset.add_field(
parcels.Field.from_netcdf(
mesh_mask, "e1t", dimensions, interp_method="nearest"
)
)
fieldset.add_field(
parcels.Field.from_netcdf(
mesh_mask, "e2t", dimensions, interp_method="nearest"
)
)
fieldset.add_field(
parcels.Field(
"e3t", np.array(1), lon=0, lat=0, depth=0, interp_method="nearest"
)
)

assert fieldset.U.chunksize == chunksize

# Now run particles as normal
Expand Down
37 changes: 32 additions & 5 deletions docs/examples/example_peninsula.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,40 @@ def test_peninsula_fieldset(mode, mesh, tmpdir):
assert (err_smpl <= 1.0e-3).all()


@pytest.mark.parametrize(
"mode", ["scipy"]
) # Analytical Advection only implemented in Scipy mode
@pytest.mark.parametrize("mesh", ["flat", "spherical"])
def test_peninsula_fieldset_AnalyticalAdvection(mode, mesh, tmpdir):
@pytest.mark.parametrize("mode", ["jit", "scipy"])
def test_peninsula_fieldset_AnalyticalAdvection(mode, tmpdir):
"""Execute peninsula test using Analytical Advection on C grid."""
fieldset = peninsula_fieldset(101, 51, "flat", grid_type="C")
lon = fieldset.U.lon
lat = fieldset.U.lat
dx = lon[1] - lon[0]
dy = lat[1] - lat[0]
fieldset.add_field(
parcels.Field(
"e2u", dy * np.ones((len(lat), len(lon))), lon, lat, interp_method="nearest"
)
)
fieldset.add_field(
parcels.Field(
"e1v", dx * np.ones((len(lat), len(lon))), lon, lat, interp_method="nearest"
)
)
fieldset.add_field(
parcels.Field(
"e1t", dx * np.ones((len(lat), len(lon))), lon, lat, interp_method="nearest"
)
)
fieldset.add_field(
parcels.Field(
"e2t", dy * np.ones((len(lat), len(lon))), lon, lat, interp_method="nearest"
)
)
fieldset.add_field(
parcels.Field(
"e3t", np.array(1), lon=0, lat=0, depth=0, interp_method="nearest"
)
)

outfile = tmpdir.join("PeninsulaAA")
pset = peninsula_example(
fieldset, outfile, npart=10, mode=mode, method=parcels.AdvectionAnalytical
Expand Down
54,301 changes: 27,520 additions & 26,781 deletions docs/examples/tutorial_analyticaladvection.ipynb

Large diffs are not rendered by default.

103 changes: 98 additions & 5 deletions parcels/application_kernels/advection.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def AdvectionRK45(particle, fieldset, time):


def AdvectionAnalytical(particle, fieldset, time):
"""Advection of particles using 'analytical advection' integration.
"""Advection of particles using 'analytical advection' integration. This code only works in SciPy mode.
The JIT version of this code is hardcoded in C in advectionanalytical.h, and is called by the JIT version of this kernel.

Based on Ariane/TRACMASS algorithm, as detailed in e.g. Doos et al (https://doi.org/10.5194/gmd-10-1733-2017).
Note that the time-dependent scheme is currently implemented with 'intermediate timesteps'
Expand All @@ -136,27 +137,53 @@ def AdvectionAnalytical(particle, fieldset, time):
particle.lon, particle.lat, particle.depth, particle=particle
)
if withW:
updatexi = 0
updateyi = 0
if abs(xsi - 1) < tol:
if fieldset.U.data[0, zi + 1, yi + 1, xi + 1] > 0:
xi += 1
if fieldset.U.data[0, zi + 1, yi + 1, xi + 1] > 0: # TODO update scipy code here to match JIT kernel
updatexi = 1
xsi = 0
elif abs(xsi) < tol:
if fieldset.U.data[0, zi, yi, xi] < 0:
updatexi = -1
xsi = 1
if abs(eta - 1) < tol:
if fieldset.V.data[0, zi + 1, yi + 1, xi + 1] > 0:
yi += 1
updateyi = 1
eta = 0
elif abs(eta) < tol:
if fieldset.V.data[0, zi, yi, xi] < 0:
yi -= 1
eta = 1
if abs(zeta - 1) < tol:
if fieldset.W.data[0, zi + 1, yi + 1, xi + 1] > 0:
zi += 1
zeta = 0
if abs(zeta) < tol:
if fieldset.W.data[0, zi, yi, xi] < 0:
zi -= 1
zeta = 1
xi += updatexi
yi += updateyi
else:
updatexi = 0
if abs(xsi - 1) < tol:
if fieldset.U.data[0, yi + 1, xi + 1] > 0:
xi += 1
updatexi = 1
xsi = 0
elif abs(xsi) < tol:
if fieldset.U.data[0, yi, xi] < 0:
updatexi = -1
xsi = 1
if abs(eta - 1) < tol:
if fieldset.V.data[0, yi + 1, xi + 1] > 0:
yi += 1
eta = 0
elif abs(eta) < tol:
if fieldset.V.data[0, yi, xi] < 0:
yi -= 1
eta = 1
xi += updatexi

particle.xi[:] = xi
particle.yi[:] = yi
Expand Down Expand Up @@ -284,3 +311,69 @@ def compute_rs(r, B, delta, s_min):
particle.dt = max(direction * s_min * (dxdy * dz), 1e-7)
else:
particle.dt = min(direction * s_min * (dxdy * dz), -1e-7)


def AdvectionAnalytical_2D_JIT(particle, fieldset, time):
"""JIT version of 2D AdvectionAnalytical kernel.

The code itself is in advectionanalytical.h and is automatically included
when using AdvectionAnalytical in JIT mode.
"""
flow3D = 0
calcAdvectionAnalytical_JIT( # noqa
"parcels_customed_Cfunc_pointer_args",
fieldset.U,
fieldset.V,
fieldset.V, # Note repeating V field here; W is not used in 2D
flow3D,
fieldset.e2u,
fieldset.e1v,
fieldset.e1t,
fieldset.e2t,
fieldset.e3t,
particle.xi,
particle.yi,
particle.zi,
particle.ti,
particle.lon,
particle.lat,
particle.depth,
time,
particle.dt,
particle_dlon, # noqa
particle_dlat, # noqa
particle_ddepth, # noqa
)


def AdvectionAnalytical_3D_JIT(particle, fieldset, time):
"""JIT version of 3D AdvectionAnalytical kernel.

The code itself is in advectionanalytical.h and is automatically included
when using AdvectionAnalytical in JIT mode.
"""
flow3D = 1
calcAdvectionAnalytical_JIT( # noqa
"parcels_customed_Cfunc_pointer_args",
fieldset.U,
fieldset.V,
fieldset.W,
flow3D,
fieldset.e2u,
fieldset.e1v,
fieldset.e1t,
fieldset.e2t,
fieldset.e3t,
particle.xi,
particle.yi,
particle.zi,
particle.ti,
particle.lon,
particle.lat,
particle.depth,
time,
particle.dt,
particle_dlon, # noqa
particle_dlat, # noqa
particle_ddepth, # noqa
)
Loading