Skip to content
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

DRAFT: API changes for area correction #1188

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/asv-benchmarking-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: ASV Benchmarking (PR)

permissions:
issues: write
pull-requests: write

on:
pull_request:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pypi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5.4.0
uses: actions/setup-python@v5.5.0
with:
python-version: '3.x'
- name: Install dependencies
Expand All @@ -34,7 +34,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5.4.0
uses: actions/setup-python@v5.5.0
with:
python-version: '3.x'
- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repos:

- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.11.0
rev: v0.11.2
hooks:
# Run the linter.
- id: ruff
Expand Down
78 changes: 39 additions & 39 deletions docs/user-guide/area_calc.ipynb

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions test/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_face_area_coords():
face_dimension = np.array([3], dtype=INT_DTYPE)

area, _ = ux.grid.area.get_all_face_area_from_coords(
x, y, z, face_nodes, face_dimension, 3, coords_type="cartesian")
x, y, z, face_nodes, face_dimension)
nt.assert_almost_equal(area, constants.TRI_AREA, decimal=5)


Expand All @@ -54,12 +54,12 @@ def test_calculate_face_area():
z = np.array([0.66674712, 0.43462917, 0.66674712])

area, _ = ux.grid.area.calculate_face_area(
x, y, z, "gaussian", 5, "cartesian", latitude_adjusted_area=False)
x, y, z, "gaussian", 5, latitude_adjusted_area=False)

nt.assert_almost_equal(area, constants.TRI_AREA, decimal=5)

area_corrected, _ = ux.grid.area.calculate_face_area(
x, y, z, "gaussian", 5, "cartesian", latitude_adjusted_area=True)
x, y, z, "gaussian", 5, latitude_adjusted_area=True)

nt.assert_almost_equal(area_corrected, constants.CORRECTED_TRI_AREA, decimal=5)

Expand Down
40 changes: 1 addition & 39 deletions uxarray/grid/area.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np

from uxarray.grid.coordinates import _lonlat_rad_to_xyz

from numba import njit
from uxarray.constants import ERROR_TOLERANCE
Expand All @@ -13,7 +12,6 @@ def calculate_face_area(
z,
quadrature_rule="gaussian",
order=4,
coords_type="spherical",
latitude_adjusted_area=False,
):
"""Calculate area of a face on sphere.
Expand All @@ -39,9 +37,6 @@ def calculate_face_area(
- Gaussian Quadrature: 1 to 10
- Triangular: 1, 4, 8, 10 and 12

coords_type : str, optional
coordinate type, default is spherical, can be cartesian also.

latitude_adjusted_area : bool, optional
If True, performs the check if any face consists of an edge that has constant latitude, modifies the area of that face by applying the correction term due to that edge. Default is False.

Expand All @@ -65,24 +60,6 @@ def calculate_face_area(

# num triangles is two less than the total number of nodes
num_triangles = num_nodes - 2

# TODO: Remove/clarify option for spherical coordinates
if coords_type == "spherical":
# Preallocate arrays for Cartesian coordinates
n_points = len(x)
x_cartesian = np.empty(n_points)
y_cartesian = np.empty(n_points)
z_cartesian = np.empty(n_points)

# Convert all points to Cartesian coordinates using an explicit loop
for i in range(n_points):
lon_rad = np.deg2rad(x[i])
lat_rad = np.deg2rad(y[i])
cartesian = _lonlat_rad_to_xyz(lon_rad, lat_rad)
x_cartesian[i], y_cartesian[i], z_cartesian[i] = cartesian

x, y, z = x_cartesian, y_cartesian, z_cartesian

# Using tempestremap GridElements: https://github.com/ClimateGlobalChange/tempestremap/blob/master/src/GridElements.cpp
# loop through all sub-triangles of face
total_correction = 0.0
Expand Down Expand Up @@ -199,10 +176,8 @@ def get_all_face_area_from_coords(
z,
face_nodes,
face_geometry,
dim,
quadrature_rule="triangular",
order=4,
coords_type="spherical",
latitude_adjusted_area=False,
):
"""Given coords, connectivity and other area calculation params, this
Expand All @@ -223,18 +198,12 @@ def get_all_face_area_from_coords(
face_nodes : 2D ndarray, required
node ids of each face

dim : int, required
dimension

quadrature_rule : str, optional
"triangular" or "gaussian". Defaults to triangular

order : int, optional
count or order for Gaussian or spherical resp. Defaults to 4 for spherical.

coords_type : str, optional
coordinate type, default is spherical, can be cartesian also.

latitude_adjusted_area : bool, optional
If True, performs the check if any face consists of an edge that has constant latitude, modifies the area of that face by applying the correction term due to that edge. Default is False.

Expand All @@ -256,13 +225,7 @@ def get_all_face_area_from_coords(
for face_idx, max_nodes in enumerate(face_geometry):
face_x = x[face_nodes[face_idx, 0:max_nodes]]
face_y = y[face_nodes[face_idx, 0:max_nodes]]

# check if z dimension

if dim > 2:
face_z = z[face_nodes[face_idx, 0:max_nodes]]
else:
face_z = face_x * 0.0
face_z = z[face_nodes[face_idx, 0:max_nodes]]

# After getting all the nodes of a face assembled call the cal. face area routine
face_area, face_jacobian = calculate_face_area(
Expand All @@ -271,7 +234,6 @@ def get_all_face_area_from_coords(
face_z,
quadrature_rule,
order,
coords_type,
latitude_adjusted_area,
)
# store current face area
Expand Down
21 changes: 4 additions & 17 deletions uxarray/grid/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -1892,7 +1892,6 @@ def compute_face_areas(
self,
quadrature_rule: Optional[str] = "triangular",
order: Optional[int] = 4,
latlon: Optional[bool] = True,
latitude_adjusted_area: Optional[bool] = False,
):
"""Face areas calculation function for grid class, calculates area of
Expand All @@ -1904,8 +1903,6 @@ def compute_face_areas(
Quadrature rule to use. Defaults to "triangular".
order : int, optional
Order of quadrature rule. Defaults to 4.
latlon : bool, optional
If True, the coordinates are in latlon. Defaults to True.
latitude_adjusted_area : bool, optional
If True, corrects the area of the faces accounting for lines of constant lattitude. Defaults to False.

Expand All @@ -1930,18 +1927,10 @@ def compute_face_areas(
# if self._face_areas is None: # this allows for using the cached result,
# but is not the expected behavior behavior as we are in need to recompute if this function is called with different quadrature_rule or order

if latlon:
x = self.node_lon.values
y = self.node_lat.values
z = np.zeros((self.n_node))
coords_type = "spherical"
else:
x = self.node_x.values
y = self.node_y.values
z = self.node_z.values
coords_type = "cartesian"

dim = 2
self.normalize_cartesian_coordinates()
x = self.node_x.values
y = self.node_y.values
z = self.node_z.values

# Note: x, y, z are np arrays of type float
# Using np.issubdtype to check if the type is float
Expand All @@ -1961,10 +1950,8 @@ def compute_face_areas(
z,
face_nodes,
n_nodes_per_face,
dim,
quadrature_rule,
order,
coords_type,
latitude_adjusted_area,
)

Expand Down
Loading