-
Notifications
You must be signed in to change notification settings - Fork 168
Move interpolators to directory #2469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+113
−83
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d056ed5
Move interpolators to directory
erikvansebille c5f59a0
Separating interpolators into x and ux files
erikvansebille 53dea7e
Merge branch 'v4-dev' into interpolators_as_directory
erikvansebille 3ee6dab
Making interpolator files private
erikvansebille 15eda3a
Making kernel files private
erikvansebille File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| from ._uxinterpolators import ( | ||
| Ux_Velocity, | ||
| UxPiecewiseConstantFace, | ||
| UxPiecewiseLinearNode, | ||
| ) | ||
| from ._xinterpolators import ( | ||
| CGrid_Tracer, | ||
| CGrid_Velocity, | ||
| XConstantField, | ||
| XFreeslip, | ||
| XLinear, | ||
| XLinear_Velocity, | ||
| XLinearInvdistLandTracer, | ||
| XNearest, | ||
| XPartialslip, | ||
| ZeroInterpolator, | ||
| ZeroInterpolator_Vector, | ||
| ) | ||
|
|
||
| __all__ = [ # noqa: RUF022 | ||
| # xinterpolators | ||
| "CGrid_Tracer", | ||
| "CGrid_Velocity", | ||
| "XConstantField", | ||
| "XFreeslip", | ||
| "XLinear", | ||
| "XLinearInvdistLandTracer", | ||
| "XLinear_Velocity", | ||
| "XNearest", | ||
| "XPartialslip", | ||
| "ZeroInterpolator", | ||
| "ZeroInterpolator_Vector", | ||
| # uxinterpolators | ||
| "UxPiecewiseConstantFace", | ||
| "UxPiecewiseLinearNode", | ||
| "Ux_Velocity", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| """Collection of pre-built interpolation kernels for unstructured grids.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| import numpy as np | ||
|
|
||
| if TYPE_CHECKING: | ||
| from parcels._core.field import Field, VectorField | ||
| from parcels._core.uxgrid import _UXGRID_AXES | ||
|
|
||
|
|
||
| def UxPiecewiseConstantFace( | ||
| particle_positions: dict[str, float | np.ndarray], | ||
| grid_positions: dict[_UXGRID_AXES, dict[str, int | float | np.ndarray]], | ||
| field: Field, | ||
| ): | ||
| """ | ||
| Piecewise constant interpolation kernel for face registered data. | ||
| This interpolation method is appropriate for fields that are | ||
| face registered, such as u,v in FESOM. | ||
| """ | ||
| return field.data.values[ | ||
| grid_positions["T"]["index"], grid_positions["Z"]["index"], grid_positions["FACE"]["index"] | ||
| ] | ||
|
|
||
|
|
||
| def UxPiecewiseLinearNode( | ||
| particle_positions: dict[str, float | np.ndarray], | ||
| grid_positions: dict[_UXGRID_AXES, dict[str, int | float | np.ndarray]], | ||
| field: Field, | ||
| ): | ||
| """ | ||
| Piecewise linear interpolation kernel for node registered data located at vertical interface levels. | ||
| This interpolation method is appropriate for fields that are node registered such as the vertical | ||
| velocity W in FESOM2. Effectively, it applies barycentric interpolation in the lateral direction | ||
| and piecewise linear interpolation in the vertical direction. | ||
| """ | ||
| ti = grid_positions["T"]["index"] | ||
| zi, fi = grid_positions["Z"]["index"], grid_positions["FACE"]["index"] | ||
| z = particle_positions["z"] | ||
| bcoords = grid_positions["FACE"]["bcoord"] | ||
| node_ids = field.grid.uxgrid.face_node_connectivity[fi, :].values | ||
| # The zi refers to the vertical layer index. The field in this routine are assumed to be defined at the vertical interface levels. | ||
| # For interface zi, the interface indices are [zi, zi+1], so we need to use the values at zi and zi+1. | ||
| # First, do barycentric interpolation in the lateral direction for each interface level | ||
| fzk = np.sum(field.data.values[ti[:, None], zi[:, None], node_ids] * bcoords, axis=-1) | ||
| fzkp1 = np.sum(field.data.values[ti[:, None], zi[:, None] + 1, node_ids] * bcoords, axis=-1) | ||
|
|
||
| # Then, do piecewise linear interpolation in the vertical direction | ||
| zk = field.grid.z.values[zi] | ||
| zkp1 = field.grid.z.values[zi + 1] | ||
| return (fzk * (zkp1 - z) + fzkp1 * (z - zk)) / (zkp1 - zk) # Linear interpolation in the vertical direction | ||
|
|
||
|
|
||
| def Ux_Velocity( | ||
| particle_positions: dict[str, float | np.ndarray], | ||
| grid_positions: dict[_UXGRID_AXES, dict[str, int | float | np.ndarray]], | ||
| vectorfield: VectorField, | ||
| ): | ||
| """Interpolation kernel for Vectorfields of velocity on a UxGrid.""" | ||
| u = vectorfield.U._interp_method(particle_positions, grid_positions, vectorfield.U) | ||
| v = vectorfield.V._interp_method(particle_positions, grid_positions, vectorfield.V) | ||
| if vectorfield.grid._mesh == "spherical": | ||
| u /= 1852 * 60 * np.cos(np.deg2rad(particle_positions["lat"])) | ||
| v /= 1852 * 60 | ||
|
|
||
| if "3D" in vectorfield.vector_type: | ||
| w = vectorfield.W._interp_method(particle_positions, grid_positions, vectorfield.W) | ||
| else: | ||
| w = 0.0 | ||
| return u, v, w |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we prefix
xinterpolators.pywith a_(denoting its not public API for users). Same forux...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once this is done, happy to merge!