YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
grid_reg2d_rot.c
Go to the documentation of this file.
1// Copyright (c) 2024 The YAC Authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
6#include "grid_reg2d_common.h"
7#include "geometry.h"
8#include "utils_common.h"
9
11 yac_coordinate_pointer coordinates, size_t num_coordinates,
12 double north_pole[3]) {
13
14 // compute angle between original and new north pol
15 struct sin_cos_angle rot_angle =
16 get_vector_angle_2(north_pole, (double[]){0.0, 0.0, 1.0});
17
19 compare_angles(rot_angle, SIN_COS_TOL) > 0,
20 "new north pole is the original north pole "
21 "(new north pole (%.3lf; %.3lf; %.3lf); angle (sin: %e, cos: %e)",
22 north_pole[0], north_pole[1], north_pole[2], rot_angle.sin, rot_angle.cos);
23
25 compare_angles(rot_angle, SIN_COS_M_PI_2) <= 0,
26 "new north pole is on the southern hemisphere "
27 "(new north pole (%.3lf; %.3lf; %.3lf); angle (sin: %e, cos: %e)",
28 north_pole[0], north_pole[1], north_pole[2], rot_angle.sin, rot_angle.cos);
29
30 // compute rotatation axis
31 // (has an angle of 90 degree between original and new north pole)
32 double rot_axis[3];
33 crossproduct_kahan((double[]){0.0, 0.0, 1.0}, north_pole, rot_axis);
34 normalise_vector(rot_axis);
35
36 for (size_t i = 0; i < num_coordinates; ++i) {
37 double temp[3];
38 rotate_vector2(rot_axis, rot_angle, coordinates[i], temp);
39 coordinates[i][0] = temp[0];
40 coordinates[i][1] = temp[1];
41 coordinates[i][2] = temp[2];
42 }
43}
44
46 size_t nbr_vertices[2], int cyclic[2],
47 double *lon_vertices, double *lat_vertices,
48 double north_pol_lon, double north_pol_lat,
49 void (*LLtoXYZ_ptr)(double, double, double[])) {
50
51 YAC_ASSERT(!cyclic[1], "cyclic[1] != 0 not yet supported")
52
53 size_t num_cells_2d[2] =
54 {nbr_vertices[0] - (cyclic[0]?0:1), nbr_vertices[1] - (cyclic[1]?0:1)};
55 size_t num_vertices_2d[2] = {num_cells_2d[0] + (cyclic[0]?0:1), num_cells_2d[1] + 1};
56 size_t num_vertices = num_vertices_2d[0] * num_vertices_2d[1];
57 size_t num_edges =
58 (num_cells_2d[0] + (cyclic[0]?0:1)) * num_cells_2d[1] +
59 num_cells_2d[0] * (num_cells_2d[1] + 1);
60
63 for (size_t i = 0, k = 0; i < num_vertices_2d[1]; ++i)
64 for (size_t j = 0; j < num_vertices_2d[0]; ++j, ++k)
65 LLtoXYZ_ptr(lon_vertices[j], lat_vertices[i], vertex_coordinates[k]);
66
67 // rotate all coordinates according to provided north pole
68 double north_pole[3];
69 LLtoXYZ_ptr(north_pol_lon, north_pol_lat, north_pole);
71
73 for (size_t i = 0; i < num_edges; ++i) edge_type[i] = YAC_GREAT_CIRCLE_EDGE;
74
75 struct yac_basic_grid_data grid =
77 grid.vertex_coordinates = vertex_coordinates;
78 grid.edge_type = edge_type;
79 return grid;
80}
81
83 size_t nbr_vertices[2], int cyclic[2],
84 double *lon_vertices, double *lat_vertices,
85 double north_pol_lon, double north_pol_lat) {
86
87 return
89 nbr_vertices, cyclic, lon_vertices, lat_vertices,
90 north_pol_lon, north_pol_lat, LLtoXYZ);
91}
92
94 size_t nbr_vertices[2], int cyclic[2],
95 double *lon_vertices, double *lat_vertices,
96 double north_pol_lon, double north_pol_lat) {
97
98 return
100 nbr_vertices, cyclic, lon_vertices, lat_vertices,
101 north_pol_lon, north_pol_lat, LLtoXYZ_deg);
102}
#define YAC_ASSERT(exp, msg)
static struct sin_cos_angle get_vector_angle_2(double const a[3], double const b[3])
Definition geometry.h:493
static void crossproduct_kahan(double const a[], double const b[], double cross[])
Definition geometry.h:414
static const struct sin_cos_angle SIN_COS_TOL
Definition geometry.h:37
static const struct sin_cos_angle SIN_COS_M_PI_2
Definition geometry.h:40
static void rotate_vector2(double axis[], struct sin_cos_angle angle, double v_in[], double v_out[])
Definition geometry.h:769
static void LLtoXYZ_deg(double lon, double lat, double p_out[])
Definition geometry.h:278
static int compare_angles(struct sin_cos_angle a, struct sin_cos_angle b)
Definition geometry.h:506
static void normalise_vector(double v[])
Definition geometry.h:754
yac_edge_type
Definition grid_cell.h:12
@ YAC_GREAT_CIRCLE_EDGE
great circle
Definition grid_cell.h:13
struct yac_basic_grid_data yac_generate_basic_grid_data_reg2d_common(size_t nbr_vertices[2], int cyclic[2])
struct yac_basic_grid_data yac_generate_basic_grid_data_reg_2d_rot(size_t nbr_vertices[2], int cyclic[2], double *lon_vertices, double *lat_vertices, double north_pol_lon, double north_pol_lat)
struct yac_basic_grid_data yac_generate_basic_grid_data_reg_2d_rot_deg(size_t nbr_vertices[2], int cyclic[2], double *lon_vertices, double *lat_vertices, double north_pol_lon, double north_pol_lat)
void yac_rotate_coordinates(yac_coordinate_pointer coordinates, size_t num_coordinates, double north_pole[3])
static struct yac_basic_grid_data yac_generate_basic_grid_data_reg_2d_rot_(size_t nbr_vertices[2], int cyclic[2], double *lon_vertices, double *lat_vertices, double north_pol_lon, double north_pol_lat, void(*LLtoXYZ_ptr)(double, double, double[]))
#define xmalloc(size)
Definition ppm_xfuncs.h:66
double sin
Definition geometry.h:33
double cos
Definition geometry.h:33
yac_coordinate_pointer vertex_coordinates
enum yac_edge_type * edge_type
unsigned cyclic[2]
static void LLtoXYZ(double lon, double lat, double p_out[])
Definition toy_scrip.c:587
#define YAC_ASSERT_F(exp, format,...)
Definition yac_assert.h:39
double(* yac_coordinate_pointer)[3]
Definition yac_types.h:21