|
13 | 13 |
|
14 | 14 | from anemoi.graphs.generate.transforms import latlon_rad_to_cartesian
|
15 | 15 |
|
16 |
| -NORTH_POLE = torch.tensor([[0, 0, 1]], dtype=torch.float32) # North pole in 3D coordinates |
| 16 | +NORTH_POLE = [0, 0, 1] # North pole in 3D coordinates |
17 | 17 |
|
18 | 18 |
|
19 | 19 | def direction_vec(points: torch.Tensor, reference: torch.Tensor, epsilon: float = 10e-11) -> torch.Tensor:
|
@@ -78,20 +78,19 @@ def compute_directions(source_coords: torch.Tensor, target_coords: torch.Tensor)
|
78 | 78 | torch.Tensor of shape (N, 2)
|
79 | 79 | The direction of the edge.
|
80 | 80 | """
|
| 81 | + north_pole = torch.tensor([NORTH_POLE], dtype=source_coords.dtype).to(device=source_coords.device) |
81 | 82 | source_coords_xyz = latlon_rad_to_cartesian(source_coords, 1.0)
|
82 | 83 | target_coords_xyz = latlon_rad_to_cartesian(target_coords, 1.0)
|
83 | 84 |
|
84 | 85 | # Compute the unit direction vector & the angle theta between target coords and the north pole.
|
85 |
| - v_unit = direction_vec(target_coords_xyz, NORTH_POLE.to(source_coords.device)) |
86 |
| - theta = torch.acos( |
87 |
| - torch.clamp(torch.sum(target_coords_xyz * NORTH_POLE.to(source_coords.device), dim=1), -1.0, 1.0) |
88 |
| - ) # Clamp for numerical stability |
| 86 | + v_unit = direction_vec(target_coords_xyz, north_pole) |
| 87 | + theta = torch.acos(torch.clamp(torch.sum(target_coords_xyz * north_pole, dim=1), -1.0, 1.0)) # Clamp for numerical stability |
89 | 88 |
|
90 | 89 | # Rotate source coords by angle theta around v_unit axis.
|
91 | 90 | rotated_source_coords_xyz = rotate_vectors(source_coords_xyz, v_unit, theta)
|
92 | 91 |
|
93 | 92 | # Compute the direction from the rotated vector to the north pole.
|
94 |
| - direction = direction_vec(rotated_source_coords_xyz, NORTH_POLE.to(source_coords.device)) |
| 93 | + direction = direction_vec(rotated_source_coords_xyz, north_pole) |
95 | 94 | normed_direction = direction / torch.norm(direction, dim=1).unsqueeze(-1)
|
96 | 95 |
|
97 | 96 | # All 3rd components should be 0s
|
|
0 commit comments