Skip to content

Commit

Permalink
feat: builders icosahedral
Browse files Browse the repository at this point in the history
  • Loading branch information
theissenhelen committed Jun 28, 2024
1 parent b28b0ff commit 7fbc5d2
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/anemoi/graphs/nodes/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
class BaseNodeBuilder(ABC):
"""Base class for node builders."""

def __init__(self) -> None:
self.aoi_mask_builder = None

def register_nodes(self, graph: HeteroData, name: str) -> None:
graph[name].x = self.get_coordinates()
graph[name].node_type = type(self).__name__
Expand Down Expand Up @@ -62,3 +65,52 @@ def __init__(self, resolution: str, grid_definition_path: str) -> None:
def get_coordinates(self) -> np.ndarray:
coords = self.reshape_coords(self.grid_definition["latitudes"], self.grid_definition["longitudes"])
return coords


class RefinedIcosahedralNodeBuilder(BaseNodeBuilder):
"""Processor mesh based on a triangular mesh.
It is based on the icosahedral mesh, which is a mesh of triangles that covers the sphere.
Parameters
----------
resolution : list[int] | int
Refinement level of the mesh.
np_dtype : np.dtype, optional
The numpy data type to use, by default np.float32.
"""

def __init__(
self,
resolution: Union[int, list[int]],
np_dtype: np.dtype = np.float32,
) -> None:
self.np_dtype = np_dtype

if isinstance(resolution, int):
self.resolutions = list(range(resolution + 1))
else:
self.resolutions = resolution

super().__init__()

def get_coordinates(self) -> np.ndarray:
self.nx_graph, coords_rad, self.node_ordering = self.create_nodes()
return coords_rad[self.node_ordering]

def create_nodes(self) -> np.ndarray: ...

def register_attributes(self, graph: HeteroData, name: str, config: DotDict) -> HeteroData:
graph[name]["resolutions"] = self.resolutions
graph[name]["nx_graph"] = self.nx_graph
graph[name]["node_ordering"] = self.node_ordering
graph[name]["aoi_mask_builder"] = self.aoi_mask_builder
return super().register_attributes(graph, name, config)


class TriRefinedIcosahedralNodeBuilder(RefinedIcosahedralNodeBuilder):
"""It depends on the trimesh Python library."""

def create_nodes(self) -> np.ndarray:
return create_icosahedral_nodes(resolutions=self.resolutions, aoi_nneighb=self.aoi_mask_builder)

0 comments on commit 7fbc5d2

Please sign in to comment.