|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +from parcels import ( |
| 5 | + AdvectionRK4, |
| 6 | + Field, |
| 7 | + FieldSet, |
| 8 | + ParticleSet, |
| 9 | + xgcm, |
| 10 | +) |
| 11 | +from parcels._datasets.structured.generic import datasets as datasets_structured |
| 12 | +from parcels.xgrid import XGrid |
| 13 | +from tests.common_kernels import MoveEast, MoveNorth |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def fieldset() -> FieldSet: |
| 18 | + ds = datasets_structured["ds_2d_left"] |
| 19 | + grid = XGrid(xgcm.Grid(ds)) |
| 20 | + U = Field("U", ds["U (A grid)"], grid, mesh_type="flat") |
| 21 | + V = Field("V", ds["V (A grid)"], grid, mesh_type="flat") |
| 22 | + return FieldSet([U, V]) |
| 23 | + |
| 24 | + |
| 25 | +def test_multi_kernel_reuse_varnames(fieldset): |
| 26 | + pset = ParticleSet(fieldset, lon=[0.5], lat=[0.5]) |
| 27 | + |
| 28 | + # Testing for merging of two Kernels with the same variable declared |
| 29 | + def MoveEast1(particle, fieldset, time): # pragma: no cover |
| 30 | + add_lon = 0.2 |
| 31 | + particle_dlon += add_lon # noqa |
| 32 | + |
| 33 | + def MoveEast2(particle, fieldset, time): # pragma: no cover |
| 34 | + particle_dlon += add_lon # noqa |
| 35 | + |
| 36 | + pset.execute([MoveEast1, MoveEast2], runtime=np.timedelta64(2, "s")) |
| 37 | + assert np.allclose(pset.lon, [0.9], atol=1e-5) # should be 0.5 + 0.2 + 0.2 = 0.9 |
| 38 | + |
| 39 | + |
| 40 | +def test_combined_kernel_from_list(fieldset): |
| 41 | + """ |
| 42 | + Test pset.Kernel(List[function]) |
| 43 | +
|
| 44 | + Tests that a Kernel can be created from a list functions, or a list of |
| 45 | + mixed functions and kernel objects. |
| 46 | + """ |
| 47 | + pset = ParticleSet(fieldset, lon=[0.5], lat=[0.5]) |
| 48 | + kernels_single = pset.Kernel([AdvectionRK4]) |
| 49 | + kernels_functions = pset.Kernel([AdvectionRK4, MoveEast, MoveNorth]) |
| 50 | + |
| 51 | + # Check if the kernels were combined correctly |
| 52 | + assert kernels_single.funcname == "AdvectionRK4" |
| 53 | + assert kernels_functions.funcname == "AdvectionRK4MoveEastMoveNorth" |
| 54 | + |
| 55 | + |
| 56 | +def test_combined_kernel_from_list_error_checking(fieldset): |
| 57 | + """ |
| 58 | + Test pset.Kernel(List[function]) |
| 59 | +
|
| 60 | + Tests that various error cases raise appropriate messages. |
| 61 | + """ |
| 62 | + pset = ParticleSet(fieldset, lon=[0.5], lat=[0.5]) |
| 63 | + |
| 64 | + # Test that list has to be non-empty |
| 65 | + with pytest.raises(ValueError): |
| 66 | + pset.Kernel([]) |
| 67 | + |
| 68 | + # Test that list has to be all functions |
| 69 | + with pytest.raises(ValueError): |
| 70 | + pset.Kernel([AdvectionRK4, "something else"]) |
| 71 | + |
| 72 | + # Can't mix kernel objects and functions in list |
| 73 | + with pytest.raises(ValueError): |
| 74 | + kernels_mixed = pset.Kernel([pset.Kernel(AdvectionRK4), MoveEast, MoveNorth]) |
| 75 | + assert kernels_mixed.funcname == "AdvectionRK4MoveEastMoveNorth" |
0 commit comments