YAC 3.12.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 "ERROR(yac_rotate_coordinates): "
21 "new north pole is the original north pole "
22 "(new north pole (%.3lf; %.3lf; %.3lf); angle (sin: %e, cos: %e)",
23 north_pole[0], north_pole[1], north_pole[2], rot_angle.sin, rot_angle.cos);
24
26 compare_angles(rot_angle, SIN_COS_M_PI_2) <= 0,
27 "ERROR(yac_rotate_coordinates): "
28 "new north pole is on the southern hemisphere "
29 "(new north pole (%.3lf; %.3lf; %.3lf); angle (sin: %e, cos: %e)",
30 north_pole[0], north_pole[1], north_pole[2], rot_angle.sin, rot_angle.cos);
31
32 // compute rotatation axis
33 // (has an angle of 90 degree between original and new north pole)
34 double rot_axis[3];
35 crossproduct_kahan((double[]){0.0, 0.0, 1.0}, north_pole, rot_axis);
36 normalise_vector(rot_axis);
37
38 for (size_t i = 0; i < num_coordinates; ++i) {
39 double temp[3];
40 rotate_vector2(rot_axis, rot_angle, coordinates[i], temp);
41 coordinates[i][0] = temp[0];
42 coordinates[i][1] = temp[1];
43 coordinates[i][2] = temp[2];
44 }
45}
46
48 size_t nbr_vertices[2], int cyclic[2],
49 double *lon_vertices, double *lat_vertices,
50 double north_pol_lon, double north_pol_lat,
51 void (*LLtoXYZ_ptr)(double, double, double[])) {
52
54 !cyclic[1],
55 "ERROR(yac_generate_basic_grid_data_reg_2d_rot): "
56 "cyclic[1] != 0 not yet supported")
57
58 size_t num_cells_2d[2] =
59 {nbr_vertices[0] - (cyclic[0]?0:1), nbr_vertices[1] - (cyclic[1]?0:1)};
60 size_t num_vertices_2d[2] = {num_cells_2d[0] + (cyclic[0]?0:1), num_cells_2d[1] + 1};
61 size_t num_vertices = num_vertices_2d[0] * num_vertices_2d[1];
62 size_t num_edges =
63 (num_cells_2d[0] + (cyclic[0]?0:1)) * num_cells_2d[1] +
64 num_cells_2d[0] * (num_cells_2d[1] + 1);
65
68 for (size_t i = 0, k = 0; i < num_vertices_2d[1]; ++i)
69 for (size_t j = 0; j < num_vertices_2d[0]; ++j, ++k)
70 LLtoXYZ_ptr(lon_vertices[j], lat_vertices[i], vertex_coordinates[k]);
71
72 // rotate all coordinates according to provided north pole
73 double north_pole[3];
74 LLtoXYZ_ptr(north_pol_lon, north_pol_lat, north_pole);
76
78 for (size_t i = 0; i < num_edges; ++i) edge_type[i] = YAC_GREAT_CIRCLE_EDGE;
79
80 struct yac_basic_grid_data grid =
82 grid.vertex_coordinates = vertex_coordinates;
83 grid.edge_type = edge_type;
84 return grid;
85}
86
88 size_t nbr_vertices[2], int cyclic[2],
89 double *lon_vertices, double *lat_vertices,
90 double north_pol_lon, double north_pol_lat) {
91
92 return
94 nbr_vertices, cyclic, lon_vertices, lat_vertices,
95 north_pol_lon, north_pol_lat, LLtoXYZ);
96}
97
99 size_t nbr_vertices[2], int cyclic[2],
100 double *lon_vertices, double *lat_vertices,
101 double north_pol_lon, double north_pol_lat) {
102
103 return
105 nbr_vertices, cyclic, lon_vertices, lat_vertices,
106 north_pol_lon, north_pol_lat, LLtoXYZ_deg);
107}
#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:361
static void crossproduct_kahan(double const a[], double const b[], double cross[])
Definition geometry.h:297
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:650
static void LLtoXYZ_deg(double lon, double lat, double p_out[])
Definition geometry.h:269
static int compare_angles(struct sin_cos_angle a, struct sin_cos_angle b)
Definition geometry.h:374
static void normalise_vector(double v[])
Definition geometry.h:635
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:19
double(* yac_coordinate_pointer)[3]
Definition yac_types.h:19