-
Notifications
You must be signed in to change notification settings - Fork 10
Better torus support #965
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
Better torus support #965
Changes from 44 commits
f21b246
5b14541
bc7e2bc
65bdfc3
521968d
4c73db2
afaf759
1d67822
6f98c70
cedf32a
f4173f4
e3eee2a
b36d5fa
21e1219
e0ced8f
0469778
47e95ce
3bf84dd
76678a4
b2e86c5
8bd2207
4535bad
682db97
ee51e8a
65eef62
7da8935
f3b4b03
ac2ed72
d6974b7
2b1d139
86c2190
fd46530
9a640ab
fd03b20
b113457
67988f8
3d44585
d4b5ed9
e030b4d
db33932
f124ade
0ebe72c
47b6940
60ebdf9
ed78a24
0218c56
ded15dc
20c2b12
a5f4c8d
48b5806
188d235
d490191
0dbbe5a
c0420f0
9642322
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,12 +17,14 @@ | |
| from icon4py.model.common.math.helpers import ( | ||
| arc_length_on_edges, | ||
| cross_product_on_edges, | ||
| diff_on_edges_torus, | ||
| distance_on_edges_torus, | ||
| geographical_to_cartesian_on_edges, | ||
| geographical_to_cartesian_on_vertices, | ||
| normalize_cartesian_vector_on_edges, | ||
| zonal_and_meridional_components_on_edges, | ||
| ) | ||
| from icon4py.model.common.utils import data_allocation as alloc | ||
| from icon4py.model.common.utils import data_allocation as data_alloc | ||
|
|
||
|
|
||
| @gtx.field_operator(grid_type=gtx.GridType.UNSTRUCTURED) | ||
|
|
@@ -56,6 +58,51 @@ def cartesian_coordinates_of_edge_tangent( | |
| return normalize_cartesian_vector_on_edges(x, y, z) | ||
|
|
||
|
|
||
| @gtx.field_operator(grid_type=gtx.GridType.UNSTRUCTURED) | ||
| def cartesian_coordinates_of_edge_tangent_torus( | ||
| vertex_x: fa.VertexField[ta.wpfloat], | ||
| vertex_y: fa.VertexField[ta.wpfloat], | ||
| edge_orientation: fa.EdgeField[ta.wpfloat], | ||
| domain_length: ta.wpfloat, | ||
| domain_height: ta.wpfloat, | ||
| ) -> tuple[ | ||
| fa.EdgeField[ta.wpfloat], | ||
| fa.EdgeField[ta.wpfloat], | ||
| fa.EdgeField[ta.wpfloat], | ||
| ]: | ||
| """ | ||
| Compute normalized cartesian vector tangential to an edge on a torus grid. | ||
|
|
||
| That is: computes the delta between the two vertices adjacent to the edge: | ||
| t = d(v1, v2) | ||
|
|
||
| Args: | ||
| vertex_x: x coordinates of vertices | ||
| vertex_y: y coordinates of vertices | ||
| edge_orientation: encoding of the edge orientation: (-1, +1) depending on whether the | ||
| edge is directed from first to second neighbor of vice versa. | ||
| Returns: | ||
| x: x coordinate of normalized tangent vector | ||
| y: y coordinate of normalized tangent vector | ||
| z: z coordinate of normalized tangent vector | ||
| """ | ||
| xdiff, ydiff = diff_on_edges_torus( | ||
| vertex_x(E2V[0]), | ||
| vertex_x(E2V[1]), | ||
| vertex_y(E2V[0]), | ||
| vertex_y(E2V[1]), | ||
| domain_length, | ||
| domain_height, | ||
| ) | ||
| x = edge_orientation * xdiff | ||
| y = edge_orientation * ydiff | ||
| z = 0.0 * x | ||
jcanton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # TODO(msimberg): This should use something like numpy.zeros_like if and | ||
| # when that becomes available in gt4py. | ||
|
|
||
| return normalize_cartesian_vector_on_edges(x, y, z) | ||
|
|
||
|
|
||
| @gtx.field_operator | ||
| def cartesian_coordinates_of_edge_normal( | ||
| edge_lat: fa.EdgeField[ta.wpfloat], | ||
|
|
@@ -93,6 +140,32 @@ def cartesian_coordinates_of_edge_normal( | |
| return normalize_cartesian_vector_on_edges(x, y, z) | ||
|
|
||
|
|
||
| @gtx.field_operator | ||
| def cartesian_coordinates_of_edge_normal_torus( | ||
| edge_tangent_x: fa.EdgeField[ta.wpfloat], | ||
| edge_tangent_y: fa.EdgeField[ta.wpfloat], | ||
| ) -> tuple[ | ||
| fa.EdgeField[ta.wpfloat], | ||
| fa.EdgeField[ta.wpfloat], | ||
| fa.EdgeField[ta.wpfloat], | ||
| ]: | ||
| """ | ||
| Compute the normal to the edge tangent vector on a torus grid. | ||
|
|
||
| Args: | ||
| edge_tangent_x: x coordinate of the tangent | ||
| edge_tangent_y: y coordinate of the tangent | ||
| Returns: | ||
| edge_normal_x: x coordinate of the normal | ||
| edge_normal_y: y coordinate of the normal | ||
| edge_normal_z: y coordinate of the normal | ||
| """ | ||
| z = 0.0 * edge_tangent_x | ||
jcanton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # TODO(msimberg): This should use something like numpy.zeros_like if and | ||
| # when that becomes available in gt4py. | ||
| return normalize_cartesian_vector_on_edges(-edge_tangent_y, edge_tangent_x, z) | ||
|
|
||
|
|
||
| @gtx.field_operator | ||
| def cartesian_coordinates_edge_tangent_and_normal( | ||
| vertex_lat: fa.VertexField[ta.wpfloat], | ||
|
|
@@ -123,6 +196,59 @@ def cartesian_coordinates_edge_tangent_and_normal( | |
| return tangent_x, tangent_y, tangent_z, normal_x, normal_y, normal_z | ||
|
|
||
|
|
||
| @gtx.field_operator | ||
| def cartesian_coordinates_edge_tangent_and_normal_torus( | ||
| vertex_x: fa.VertexField[ta.wpfloat], | ||
| vertex_y: fa.VertexField[ta.wpfloat], | ||
| edge_x: fa.EdgeField[ta.wpfloat], | ||
| edge_y: fa.EdgeField[ta.wpfloat], | ||
| edge_orientation: fa.EdgeField[ta.wpfloat], | ||
| domain_length: ta.wpfloat, | ||
| domain_height: ta.wpfloat, | ||
| ) -> tuple[ | ||
| fa.EdgeField[ta.wpfloat], | ||
| fa.EdgeField[ta.wpfloat], | ||
| fa.EdgeField[ta.wpfloat], | ||
| fa.EdgeField[ta.wpfloat], | ||
| fa.EdgeField[ta.wpfloat], | ||
| fa.EdgeField[ta.wpfloat], | ||
| fa.EdgeField[ta.wpfloat], | ||
| fa.EdgeField[ta.wpfloat], | ||
| fa.EdgeField[ta.wpfloat], | ||
| fa.EdgeField[ta.wpfloat], | ||
| ]: | ||
| """Compute normalized cartesian vectors of edge tangent and edge normal.""" | ||
| tangent_x, tangent_y, tangent_z = cartesian_coordinates_of_edge_tangent_torus( | ||
| vertex_x, | ||
| vertex_y, | ||
| edge_orientation, | ||
| domain_length, | ||
| domain_height, | ||
| ) | ||
| tangent_u = tangent_x | ||
| tangent_v = tangent_y | ||
|
|
||
| normal_x, normal_y, normal_z = cartesian_coordinates_of_edge_normal_torus( | ||
| tangent_x, | ||
| tangent_y, | ||
| ) | ||
| normal_u = normal_x | ||
| normal_v = normal_y | ||
|
|
||
| return ( | ||
| tangent_x, | ||
| tangent_y, | ||
| tangent_z, | ||
| tangent_u, | ||
| tangent_v, | ||
| normal_x, | ||
| normal_y, | ||
| normal_z, | ||
| normal_u, | ||
| normal_v, | ||
| ) | ||
|
Comment on lines
+239
to
+250
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here why we cannot just return the components on
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could but it does not make a difference in terms of memory usage to do it here (and doing it in other places makes it less readable IMO) |
||
|
|
||
|
|
||
| @gtx.program(grid_type=gtx.GridType.UNSTRUCTURED) | ||
| def compute_cartesian_coordinates_of_edge_tangent_and_normal( | ||
| vertex_lat: fa.VertexField[ta.wpfloat], | ||
|
|
@@ -150,6 +276,52 @@ def compute_cartesian_coordinates_of_edge_tangent_and_normal( | |
| ) | ||
|
|
||
|
|
||
| @gtx.program(grid_type=gtx.GridType.UNSTRUCTURED) | ||
| def compute_cartesian_coordinates_of_edge_tangent_and_normal_torus( | ||
| vertex_x: fa.VertexField[ta.wpfloat], | ||
| vertex_y: fa.VertexField[ta.wpfloat], | ||
| edge_x: fa.EdgeField[ta.wpfloat], | ||
| edge_y: fa.EdgeField[ta.wpfloat], | ||
| edge_orientation: fa.EdgeField[ta.wpfloat], | ||
| tangent_x: fa.EdgeField[ta.wpfloat], | ||
| tangent_y: fa.EdgeField[ta.wpfloat], | ||
| tangent_z: fa.EdgeField[ta.wpfloat], | ||
| tangent_u: fa.EdgeField[ta.wpfloat], | ||
| tangent_v: fa.EdgeField[ta.wpfloat], | ||
| normal_x: fa.EdgeField[ta.wpfloat], | ||
| normal_y: fa.EdgeField[ta.wpfloat], | ||
| normal_z: fa.EdgeField[ta.wpfloat], | ||
| normal_u: fa.EdgeField[ta.wpfloat], | ||
| normal_v: fa.EdgeField[ta.wpfloat], | ||
| domain_length: ta.wpfloat, | ||
| domain_height: ta.wpfloat, | ||
| horizontal_start: gtx.int32, | ||
| horizontal_end: gtx.int32, | ||
| ): | ||
| cartesian_coordinates_edge_tangent_and_normal_torus( | ||
| vertex_x, | ||
| vertex_y, | ||
| edge_x, | ||
| edge_y, | ||
| edge_orientation, | ||
| domain_length, | ||
| domain_height, | ||
| out=( | ||
| tangent_x, | ||
| tangent_y, | ||
| tangent_z, | ||
| tangent_u, | ||
| tangent_v, | ||
| normal_x, | ||
| normal_y, | ||
| normal_z, | ||
| normal_u, | ||
| normal_v, | ||
| ), | ||
| domain={dims.EdgeDim: (horizontal_start, horizontal_end)}, | ||
| ) | ||
|
|
||
|
|
||
| @gtx.field_operator(grid_type=gtx.GridType.UNSTRUCTURED) | ||
| def zonal_and_meridional_component_of_edge_field_at_vertex( | ||
| vertex_lat: fa.VertexField[ta.wpfloat], | ||
|
|
@@ -410,6 +582,38 @@ def arc_distance_of_far_edges_in_diamond( | |
| return far_vertex_vertex_length | ||
|
|
||
|
|
||
| @gtx.field_operator | ||
| def distance_of_far_edges_in_diamond_torus( | ||
| vertex_x: fa.VertexField[ta.wpfloat], | ||
| vertex_y: fa.VertexField[ta.wpfloat], | ||
| domain_length: ta.wpfloat, | ||
| domain_height: ta.wpfloat, | ||
| ) -> fa.EdgeField[ta.wpfloat]: | ||
| """ | ||
| Compute the distance between the "far" vertices of an edge on a torus grid. | ||
|
|
||
| See arc_distance_of_far_edges_in_diamond for details. | ||
|
|
||
| Args: | ||
| vertex_x: x coordinate of vertices | ||
| vertex_y: y coordinate of vertices | ||
| domain_length: length of the domain | ||
| domain_height: height of the domain | ||
|
|
||
| Returns: | ||
| distance between the "far" vertices in the diamond. | ||
|
|
||
| """ | ||
| return distance_on_edges_torus( | ||
| vertex_x(E2C2V[2]), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suddenly, I am thinking if is it better to define far indices in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fixed the docstsring, nice catch! |
||
| vertex_x(E2C2V[3]), | ||
| vertex_y(E2C2V[2]), | ||
| vertex_y(E2C2V[3]), | ||
| domain_length, | ||
| domain_height, | ||
| ) | ||
|
|
||
|
|
||
| @gtx.field_operator | ||
| def edge_length( | ||
| vertex_lat: fa.VertexField[ta.wpfloat], | ||
|
|
@@ -502,6 +706,26 @@ def compute_arc_distance_of_far_edges_in_diamond( | |
| ) | ||
|
|
||
|
|
||
| @gtx.program(grid_type=gtx.GridType.UNSTRUCTURED) | ||
| def compute_distance_of_far_edges_in_diamond_torus( | ||
| vertex_x: fa.VertexField[ta.wpfloat], | ||
| vertex_y: fa.VertexField[ta.wpfloat], | ||
| domain_length: ta.wpfloat, | ||
| domain_height: ta.wpfloat, | ||
| far_vertex_distance: fa.EdgeField[ta.wpfloat], | ||
| horizontal_start: gtx.int32, | ||
| horizontal_end: gtx.int32, | ||
| ): | ||
| distance_of_far_edges_in_diamond_torus( | ||
| vertex_x, | ||
| vertex_y, | ||
| domain_length, | ||
| domain_height, | ||
| out=far_vertex_distance, | ||
| domain={dims.EdgeDim: (horizontal_start, horizontal_end)}, | ||
| ) | ||
|
|
||
|
|
||
| @gtx.field_operator | ||
| def edge_area( | ||
| owner_mask: fa.EdgeField[bool], | ||
|
|
@@ -574,11 +798,11 @@ def compute_coriolis_parameter_on_edges( | |
|
|
||
|
|
||
| def compute_primal_cart_normal( | ||
| primal_cart_normal_x: alloc.NDArray, | ||
| primal_cart_normal_y: alloc.NDArray, | ||
| primal_cart_normal_z: alloc.NDArray, | ||
| primal_cart_normal_x: data_alloc.NDArray, | ||
| primal_cart_normal_y: data_alloc.NDArray, | ||
| primal_cart_normal_z: data_alloc.NDArray, | ||
| array_ns: ModuleType = np, | ||
| ) -> alloc.NDArray: | ||
| ) -> data_alloc.NDArray: | ||
| primal_cart_normal = array_ns.transpose( | ||
| array_ns.stack( | ||
| ( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.