OpenDrift / opendrift

Open source framework for ocean trajectory modelling
https://opendrift.github.io
GNU General Public License v2.0
248 stars 121 forks source link

seed_within_polygon problem #979

Closed lyingTree closed 2 years ago

lyingTree commented 2 years ago

when I run the code below, some errors occur. Are there any limitations to seed in the polygon?

from datetime import datetime
from datetime import timedelta

from opendrift.models.leeway import Leeway

o = Leeway()
o.set_config('environment:fallback:x_wind', 1)
o.set_config('environment:fallback:y_wind', 1)
o.set_config('environment:fallback:x_sea_water_velocity', 1)
o.set_config('environment:fallback:y_sea_water_velocity', 1)
o.seed_within_polygon(
    lons=[119.92115, 119.534667, 119.798764, 120.160555, 119.92115],
    lats=[39.737191, 39.619035, 39.442667, 39.595876, 39.737191],
    time=[datetime(2022, 9, 19, 3, 13, 4), datetime(2022, 9, 19, 3, 13, 4)],
    number=3
)
o.run(time_step=timedelta(seconds=60), duration=timedelta(hours=24))

image

when I modified the code in basemodel.py, it can run normally. Is it correct?

# before modification
if len(lonpoints) < number:
    # If number of positions is smaller than requested,
    # we duplicate the first ones
    missing = number - len(lonpoints)
    lonpoints = np.append(lonpoints, lonpoints[0:missing])
    latpoints = np.append(latpoints, latpoints[0:missing])
# ---------------------------------------------------------------------
# after modification
while True:
    if len(lonpoints) < number:
        # If number of positions is smaller than requested,
        # we duplicate the first ones
        missing = number - len(lonpoints)
        lonpoints = np.append(lonpoints, lonpoints[0:missing])
        latpoints = np.append(latpoints, latpoints[0:missing])
    else:
        break
knutfrode commented 2 years ago

Yes, this might happen with a small number of elements (here 3) within polygons.

But your fix seems to be a good one, so I now added this in https://github.com/OpenDrift/opendrift/pull/980