Skip to content

Commit d343b64

Browse files
Merge pull request #2284 from OceanParcels/bugfix/2260-vectorize-pwlinear-node
Bugfix/2260 vectorize pwlinear node
2 parents 65ea408 + f9099e6 commit d343b64

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

parcels/interpolators.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -653,12 +653,12 @@ def UXPiecewiseLinearNode(
653653
"""
654654
k, fi = position["Z"][0], position["FACE"][0]
655655
bcoords = position["FACE"][1]
656-
node_ids = field.grid.uxgrid.face_node_connectivity[fi, :]
656+
node_ids = field.grid.uxgrid.face_node_connectivity[fi, :].values
657657
# The zi refers to the vertical layer index. The field in this routine are assumed to be defined at the vertical interface levels.
658658
# For interface zi, the interface indices are [zi, zi+1], so we need to use the values at zi and zi+1.
659659
# First, do barycentric interpolation in the lateral direction for each interface level
660-
fzk = np.sum(field.data.values[ti, k, node_ids] * bcoords, axis=-1)
661-
fzkp1 = np.sum(field.data.values[ti, k + 1, node_ids] * bcoords, axis=-1)
660+
fzk = np.sum(field.data.values[ti[:, None], k[:, None], node_ids] * bcoords, axis=-1)
661+
fzkp1 = np.sum(field.data.values[ti[:, None], k[:, None] + 1, node_ids] * bcoords, axis=-1)
662662

663663
# Then, do piecewise linear interpolation in the vertical direction
664664
zk = field.grid.z.values[k]

tests/test_particleset_execute.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
from parcels._datasets.structured.generated import simple_UV_dataset
2323
from parcels._datasets.structured.generic import datasets as datasets_structured
2424
from parcels._datasets.unstructured.generic import datasets as datasets_unstructured
25-
from parcels.interpolators import UXPiecewiseConstantFace
26-
from parcels.kernels import AdvectionEE, AdvectionRK4
25+
from parcels.interpolators import UXPiecewiseConstantFace, UXPiecewiseLinearNode
26+
from parcels.kernels import AdvectionEE, AdvectionRK4, AdvectionRK4_3D
2727
from tests import utils
2828
from tests.common_kernels import DoNothing
2929

@@ -479,6 +479,50 @@ def test_uxstommelgyre_pset_execute():
479479
assert utils.round_and_hash_float_array([p.lat for p in pset]) == 1142124776
480480

481481

482+
def test_uxstommelgyre_multiparticle_pset_execute():
483+
ds = datasets_unstructured["stommel_gyre_delaunay"]
484+
grid = UxGrid(grid=ds.uxgrid, z=ds.coords["nz"], mesh="spherical")
485+
U = Field(
486+
name="U",
487+
data=ds.U,
488+
grid=grid,
489+
interp_method=UXPiecewiseConstantFace,
490+
)
491+
V = Field(
492+
name="V",
493+
data=ds.V,
494+
grid=grid,
495+
interp_method=UXPiecewiseConstantFace,
496+
)
497+
W = Field(
498+
name="W",
499+
data=ds.W,
500+
grid=grid,
501+
interp_method=UXPiecewiseLinearNode,
502+
)
503+
P = Field(
504+
name="P",
505+
data=ds.p,
506+
grid=grid,
507+
interp_method=UXPiecewiseConstantFace,
508+
)
509+
UVW = VectorField(name="UVW", U=U, V=V, W=W)
510+
fieldset = FieldSet([UVW, UVW.U, UVW.V, UVW.W, P])
511+
pset = ParticleSet(
512+
fieldset,
513+
lon=[30.0, 32.0],
514+
lat=[5.0, 5.0],
515+
depth=[50.0, 50.0],
516+
time=[np.timedelta64(0, "s")],
517+
pclass=Particle,
518+
)
519+
pset.execute(
520+
runtime=np.timedelta64(10, "m"),
521+
dt=np.timedelta64(60, "s"),
522+
pyfunc=AdvectionRK4_3D,
523+
)
524+
525+
482526
@pytest.mark.xfail(reason="Output file not implemented yet")
483527
def test_uxstommelgyre_pset_execute_output():
484528
ds = datasets_unstructured["stommel_gyre_delaunay"]

0 commit comments

Comments
 (0)