Skip to content

[BUG FIX] Field.igrid set by reconstruct_fields() - #3018

Merged
wyatt-fluidnumerics merged 4 commits into
mainfrom
pic-check-caching-failures
Oct 29, 2026
Merged

wyatt-fluidnumerics merged 4 commits into
mainfrom
pic-check-caching-failures

Conversation

@wyatt-fluidnumerics

@wyatt-fluidnumerics wyatt-fluidnumerics commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

Currently, Field.igrid is always set to -1 by the field constructor. This causes issues indexing into the correct grid during PIC checks for fieldsets that have more than one underlying grid, since _ei is set using igrid:

if particles is None:
_ei = None
else:
_ei = particles.ei[:, self.igrid]

The result is cache misses as described here.

This PR resolves this problem by making igrid a property of a field that is set during FieldSet construction by reconstruct_fields().

Checklist

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
    • Describe how you used it (e.g., by pasting your prompt): Once I had sketched out the desired changes I guided an llm (opus 5.5) through the implementation.

@erikvansebille erikvansebille left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me - just two small comments below

Comment thread src/parcels/_core/field.py Outdated
Comment on lines +111 to +116
try:
return self._igrid
except AttributeError as e:
raise AttributeError(
f"{type(self).__name__} doesn't have an igrid defined for it. Use `.igrid = ...`"
) from e

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not overly cautious? Since users don't set en get igrid themselves, can't we assume it's accessed properly? Plus, python will throw an error anyways?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is a fair assumption, removed in 980ddf4

Comment thread src/parcels/_core/field.py Outdated
Comment on lines +120 to +123
if value < 0:
raise ValueError(
f"igrid of Field {self.name!r} must be a non-negative index into FieldSet.gridset, got {value}. "
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, overly cautious? Is there a case where a user would put in a negative number?

@wyatt-fluidnumerics wyatt-fluidnumerics Oct 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, plus the new test should catch any cases where this occurs and causes problems. Removed in 980ddf4

@erikvansebille erikvansebille left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, happy to be merged!

@wyatt-fluidnumerics
wyatt-fluidnumerics merged commit 08c4a6c into main Oct 29, 2026
23 checks passed
@wyatt-fluidnumerics
wyatt-fluidnumerics deleted the pic-check-caching-failures branch October 29, 2026 19:22
@github-project-automation github-project-automation Bot moved this from Backlog to Done in Parcels development Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Grid search (ei/igrid) not caching between successive grid search calls

2 participants