Skip to content

Commit dc4ba52

Browse files
author
Wyatt Sieminski
committed
added a warning whenever added fields use different meshes
1 parent 1b1862b commit dc4ba52

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

src/parcels/_core/fieldset.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import functools
44
import sys
5+
import warnings
56
from collections.abc import Iterable
67
from typing import IO, TYPE_CHECKING
78

@@ -21,6 +22,7 @@
2122
from parcels._core.utils.string import _assert_str_and_python_varname
2223
from parcels._core.utils.time import get_datetime_type_calendar
2324
from parcels._core.utils.time import is_compatible as datetime_is_compatible
25+
from parcels._core.warnings import FieldSetWarning
2426
from parcels._python import NOTSET, NotSetType
2527
from parcels._reprs import fieldset_describe
2628
from parcels.interpolators import (
@@ -73,6 +75,7 @@ def __init__(self, models: list[ModelData]):
7375
self._fields: dict[str, Field | VectorField] | None = None
7476
self.reconstruct_fields()
7577
self.context: dict[str, float] = {}
78+
_warn_if_fields_use_different_meshes(self.fields.values())
7679

7780
def __setattr__(self, name, value):
7881
"""Set field attribute by name. If context exists and name in context, raise error to prevent overwriting context variable."""
@@ -151,6 +154,7 @@ def add_field(self, field: Field, name: str | None = None):
151154
raise ValueError(f"FieldSet already has a Field with name '{name}'")
152155

153156
self.fields[name] = field
157+
_warn_if_fields_use_different_meshes(self.fields.values())
154158

155159
def to_windowed_arrays(self, *, max_levels: int | None = None):
156160
"""Wrap dask-backed field data in rolling time-window caches.
@@ -215,6 +219,7 @@ def add_constant_field(self, name: str, value, mesh: ptyping.Mesh = "spherical")
215219
self.reconstruct_fields()
216220
field = getattr(self, name)
217221
field.interp_method = XConstantField()
222+
_warn_if_fields_use_different_meshes(self.fields.values())
218223

219224
def add_context(self, name, value):
220225
"""Add context variable to the FieldSet.
@@ -362,6 +367,28 @@ def assert_compatible_fieldsets(left: FieldSet, right: FieldSet) -> None:
362367
)
363368

364369

370+
def _warn_if_fields_use_different_meshes(fields: Iterable[Field | VectorField]):
371+
"""Warn if multiple fields use different meshes on the underlying grids.
372+
373+
Parameters
374+
----------
375+
fields : Iterable[Field | VectorField]
376+
The fields to check for conflicting meshes.
377+
378+
Warns
379+
------
380+
FieldSetWarning
381+
If the fields have different meshes on the underlying grids.
382+
"""
383+
meshes = {field.grid._mesh for field in fields}
384+
if len(meshes) > 1:
385+
warnings.warn(
386+
f"FieldSet has multiple different meshes: {meshes}. This may lead to unexpected behavior during execution.",
387+
category=FieldSetWarning,
388+
stacklevel=2,
389+
)
390+
391+
365392
class CalendarError(Exception): # TODO: Move to a parcels errors module
366393
"""Exception raised when the calendar of a field is not compatible with the rest of the Fields. The user should ensure that they only add fields to a FieldSet that have compatible CFtime calendars."""
367394

0 commit comments

Comments
 (0)