Skip to content

Commit

Permalink
Adding support for 2D advection in from_croco
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvansebille committed Aug 7, 2024
1 parent 0ced1ec commit 9fefe5b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
4 changes: 3 additions & 1 deletion parcels/fieldset.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,9 @@ def from_croco(cls, filenames, variables, dimensions, indices=None, mesh='spheri
kwargs['creation_log'] = 'from_croco'
if kwargs.pop('gridindexingtype', 'croco') != 'croco':
raise ValueError("gridindexingtype must be 'croco' in FieldSet.from_croco(). Use FieldSet.from_c_grid_dataset otherwise")

Check warning on line 592 in parcels/fieldset.py

View check run for this annotation

Codecov / codecov/patch

parcels/fieldset.py#L592

Added line #L592 was not covered by tests
if 'h' not in variables:

dimsU = dimensions['U'] if 'U' in dimensions else dimensions
if ('depth' in dimsU) and ('h' not in variables):
raise ValueError("FieldSet.from_croco() requires a field 'h' for the bathymetry")

Check warning on line 596 in parcels/fieldset.py

View check run for this annotation

Codecov / codecov/patch

parcels/fieldset.py#L596

Added line #L596 was not covered by tests

interp_method = {}
Expand Down
6 changes: 3 additions & 3 deletions parcels/include/index_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ static inline StatusCode search_indices_vertical_z(type_coord z, int zdim, float
*zeta = z / zvals[0];
return SUCCESS;
}
if ((z > zvals[-1]) && (gridindexingtype == CROCO)){
*zi = -2;
*zeta = 0;
if ((z > zvals[zdim-1]) && (gridindexingtype == CROCO)){
*zi = zdim-2;
*zeta = 1;
return SUCCESS;
}
if (z < zvals[0]) {return ERRORTHROUGHSURFACE;}
Expand Down
25 changes: 21 additions & 4 deletions tests/test_advection.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,33 @@ def test_advection_RK45(lon, lat, mode, rk45_tol, npart=10):
def test_advection_3DCROCO(mode, npart=10):
data_path = os.path.join(os.path.dirname(__file__), 'test_data/')
fieldset = FieldSet.from_modulefile(data_path + 'fieldset_CROCO3D.py')
print(fieldset.U.creation_log)
assert fieldset.U.creation_log == 'from_croco'

# X, Z = np.meshgrid([40e3, 80e3, 120e3], [-10 -250, -400, -850, -1400, -1550])
X, Z = np.meshgrid([40e3], [-130])
runtime = 1e4
X, Z = np.meshgrid([40e3, 80e3, 120e3], [-10, -130])
Y = np.ones(X.size) * 100e3
pset = ParticleSet(fieldset=fieldset, pclass=ptype[mode], lon=X, lat=Y, depth=Z)

pset.execute([AdvectionRK4_3D], runtime=1e4, dt=100)
pset.execute([AdvectionRK4_3D], runtime=runtime, dt=100)
assert np.allclose(pset.depth, Z.flatten(), atol=5) # TODO lower this atol
assert np.allclose(pset.lon_nextloop, [x+runtime for x in X.flatten()], atol=1e-3)


@pytest.mark.parametrize('mode', ['scipy', 'jit'])
def test_advection_2DCROCO(mode, npart=10):
data_path = os.path.join(os.path.dirname(__file__), 'test_data/')
fieldset = FieldSet.from_modulefile(data_path + 'fieldset_CROCO2D.py')
assert fieldset.U.creation_log == 'from_croco'

runtime = 1e4
X = np.array([40e3, 80e3, 120e3])
Y = np.ones(X.size) * 100e3
Z = np.zeros(X.size)
pset = ParticleSet(fieldset=fieldset, pclass=ptype[mode], lon=X, lat=Y, depth=Z)

pset.execute([AdvectionRK4], runtime=runtime, dt=100)
assert np.allclose(pset.depth, Z.flatten(), atol=1e-3)
assert np.allclose(pset.lon_nextloop, [x+runtime for x in X], atol=1e-3)


def periodicfields(xdim, ydim, uvel, vvel):
Expand Down
22 changes: 22 additions & 0 deletions tests/test_data/fieldset_CROCO2D.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os

import parcels


def create_fieldset(indices=None):
file = os.path.join(os.path.dirname(__file__), "CROCO_idealized.nc")

variables = {"U": "u", "V": "v"}
dimensions = {
"U": {"lon": "x_rho", "lat": "y_rho", "time": "time"},
"V": {"lon": "x_rho", "lat": "y_rho", "time": "time"},
}
fieldset = parcels.FieldSet.from_croco(
file,
variables,
dimensions,
allow_time_extrapolation=True,
mesh="flat",
)

return fieldset
1 change: 0 additions & 1 deletion tests/test_data/fieldset_CROCO3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def create_fieldset(indices=None):
variables,
dimensions,
allow_time_extrapolation=True,
indices=indices,
mesh="flat",
)

Expand Down

0 comments on commit 9fefe5b

Please sign in to comment.