Skip to content

Conversation

@erikvansebille
Copy link
Member

This PR implements C-grid interpolation for VectorFields (and Tracer Fields), building on #2122 (which will need to be merged into v4-dev first)

  • Chose the correct base branch (main for v3 changes, v4-dev for v4 changes)
  • Added tests

Using correct mesh_type and conversion

Also had to change the yi-indexing. To explore why!
dxdeta = np.dot(px, dphideta)
dydxsi = np.dot(py, dphidxsi)
dydeta = np.dot(py, dphideta)
dxdxsi = np.dot(dphidxsi, px)
Copy link
Member

Choose a reason for hiding this comment

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

@erikvansebille, following up on our discussion about these four $n \times n$ matrices, and the possible memory usage that may incur for large particle sets, I think the following code can work (and reduce any need to sparse)

dxdxsi_diag = np.einsum('ij,ji->i', dphidxsi, px)
dxdeta_diag = np.einsum('ij,ji->i', dphideta, px)
dydxsi_diag = np.einsum('ij,ji->i', dphidxsi, py)
dydeta_diag = np.einsum('ij,ji->i', dphideta, py)

jac_diag = dxdxsi_diag * dydeta_diag - dxdeta_diag * dydxsi_diag
return jac_diag

Base automatically changed from vectorized-kernel to v4-dev August 19, 2025 18:26
Copy link
Contributor

@VeckoTheGecko VeckoTheGecko left a comment

Choose a reason for hiding this comment

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

Still need to have a proper look at application_kernels/interpolation.py

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have any tests that test C-grid vs A grid interpolation?

Comment on lines +86 to +90
with np.errstate(divide="ignore", invalid="ignore"):
det = np.where(det2 > 0, np.sqrt(det2), eta)

eta = np.where(abs(aa) < 1e-12, -cc / bb, np.where(det2 > 0, (-bb + det) / (2 * aa), eta))

Copy link
Contributor

Choose a reason for hiding this comment

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

Which scenarios would produce the errors in particular are we ignoring here? And is the rest of the code robust to the nans/inf values that ignoring the errors would produce?

Copy link
Member Author

Choose a reason for hiding this comment

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

The way that np.where works is that it always computes both the True and False results (as far as I understand).
That means that it will for example take the np.sqrt(det2) even if det2 < 0, leading to a lot of warnings (have you not seen them in your test results?).
The with np.errstate() filters these warnings out
So yes, the code is robust to NaNs and Infs, because these would not be warnings but errors

Copy link
Contributor

Choose a reason for hiding this comment

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

one option would be to do:

mask = det2 > 0
det[mask] = np.sqrt(det2[mask])

lets just merge for now and leave this 'unresolved' in the PR - minor thing

@github-project-automation github-project-automation bot moved this from Backlog to Ready in Parcels v4 Sep 8, 2025
@github-project-automation github-project-automation bot moved this from Backlog to Ready in Parcels development Sep 8, 2025
@erikvansebille erikvansebille merged commit 1c8d5e5 into v4-dev Sep 9, 2025
8 of 9 checks passed
@erikvansebille erikvansebille deleted the c-grid-interpolation branch September 9, 2025 13:26
@github-project-automation github-project-automation bot moved this from Ready to Done in Parcels v4 Sep 9, 2025
@github-project-automation github-project-automation bot moved this from Ready to Done in Parcels development Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done
Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants