1 /**
2  * @file grid.h
3  *
4  * @copyright Copyright  (C)  2013 Moritz Hanke <hanke@dkrz.de>
5  *                                 Rene Redler <rene.redler@mpimet.mpg.de>
6  *
7  * @version 1.0
8  * @author Moritz Hanke <hanke@dkrz.de>
9  *         Rene Redler <rene.redler@mpimet.mpg.de>
10  */
11 /*
12  * Keywords:
13  * Maintainer: Moritz Hanke <hanke@dkrz.de>
14  *             Rene Redler <rene.redler@mpimet.mpg.de>
15  * URL: https://dkrz-sw.gitlab-pages.dkrz.de/yac/
16  *
17  * This file is part of YAC.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are  permitted provided that the following conditions are
21  * met:
22  *
23  * Redistributions of source code must retain the above copyright notice,
24  * this list of conditions and the following disclaimer.
25  *
26  * Redistributions in binary form must reproduce the above copyright
27  * notice, this list of conditions and the following disclaimer in the
28  * documentation and/or other materials provided with the distribution.
29  *
30  * Neither the name of the DKRZ GmbH nor the names of its contributors
31  * may be used to endorse or promote products derived from this software
32  * without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
35  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
37  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
38  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
41  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  */
46 
47 #ifndef GRID_H
48 #define GRID_H
49 
50 #include "utils.h"
51 #include "grid_cell.h"
52 
53 #define YAC_MAX_LOC_STR_LEN 10
54 
55 enum yac_location {
56 
57    CELL =   0,
58    CORNER = 1,
59    EDGE =   2,
60    LOC_UNDEFINED = 3,
61    LOC_INVALID = 4,
62 };
63 
64 /** \example test_grid.c
65  */
66 
67 #ifndef  M_PI
68 #define  M_PI        3.14159265358979323846264338327950288  /* pi */
69 #endif
70 
71 #ifndef  M_PI_2
72 #define  M_PI_2      1.57079632679489661923132169163975144  /* pi/2 */
73 #endif
74 
75 #define YAC_EARTH_RADIUS (6371.2290)
76 #define YAC_EARTH_RADIUS2 ((6371.2290 * 6371.2290) * 0.5)
77 #define YAC_RAD (0.01745329251994329576923690768489) // M_PI / 180
78 
79 typedef size_t (* size_t_2_pointer)[2];
80 
81 struct yac_field_data {
82   int ** masks;
83   size_t masks_count;
84   coordinate_pointer * coordinates;
85   size_t coordinates_count;
86 };
87 
88 struct yac_field_data_set {
89   struct yac_field_data cell, vertex, edge;
90 };
91 
92 struct interp_field {
93   enum yac_location location;
94   size_t coordinates_idx;
95   size_t masks_idx;
96 };
97 
98 struct basic_grid_data {
99   coordinate_pointer vertex_coordinates;
100   yac_int * cell_ids;
101   yac_int * vertex_ids;
102   yac_int * edge_ids;
103   size_t num_cells; // number of local cells (owned by local process)
104   size_t num_vertices; // number of local vertices (owned by local process)
105   size_t num_edges; // number of local edges (owned by local process)
106   int * core_cell_mask;
107   int * core_vertex_mask;
108   int * core_edge_mask;
109   int * num_vertices_per_cell;
110   int * num_cells_per_vertex;
111   size_t * cell_to_vertex;
112   size_t * cell_to_vertex_offsets;
113   size_t * cell_to_edge;
114   size_t * cell_to_edge_offsets;
115   size_t * vertex_to_cell;
116   size_t * vertex_to_cell_offsets;
117   size_t_2_pointer edge_to_vertex;
118   enum yac_edge_type * edge_type;
119   size_t num_total_cells; // number of locally stored cells
120   size_t num_total_vertices; // number of locally stored vertices
121   size_t num_total_edges; // number of locally stored edges
122 };
123 
124 struct yac_basic_grid;
125 
126 struct basic_grid_data yac_generate_basic_grid_data_reg_2d(
127   size_t nbr_vertices[2], int cyclic[2],
128   double *lon_vertices, double *lat_vertices);
129 
130 struct basic_grid_data yac_generate_basic_grid_data_reg_2d_deg(
131   size_t nbr_vertices[2], int cyclic[2],
132   double *lon_vertices, double *lat_vertices);
133 
134 struct basic_grid_data yac_generate_basic_grid_data_curve_2d(
135   size_t nbr_vertices[2], int cyclic[2],
136   double *lon_vertices, double *lat_vertices);
137 
138 struct basic_grid_data yac_generate_basic_grid_data_curve_2d_deg(
139   size_t nbr_vertices[2], int cyclic[2],
140   double *lon_vertices, double *lat_vertices);
141 
142 struct basic_grid_data yac_generate_basic_grid_data_unstruct(
143   size_t nbr_vertices, size_t nbr_cells, int *num_vertices_per_cell,
144   double *x_vertices, double *y_vertices, int *cell_to_vertex);
145 
146 struct basic_grid_data yac_generate_basic_grid_data_unstruct_deg(
147   size_t nbr_vertices, size_t nbr_cells, int *num_vertices_per_cell,
148   double *x_vertices, double *y_vertices, int *cell_to_vertex);
149 
150 struct yac_basic_grid * yac_basic_grid_new(
151   char const * name, struct basic_grid_data grid_data);
152 coordinate_pointer yac_basic_grid_get_field_coordinates(
153   struct yac_basic_grid * grid, struct interp_field field);
154 int const * yac_basic_grid_get_field_mask(
155   struct yac_basic_grid * grid, struct interp_field field);
156 char const * yac_basic_grid_get_name(struct yac_basic_grid * grid);
157 struct basic_grid_data * yac_basic_grid_get_data(struct yac_basic_grid * grid);
158 struct yac_field_data * yac_basic_grid_get_field_data(
159   struct yac_basic_grid * grid, enum yac_location location);
160 size_t yac_basic_grid_get_data_size(
161   struct yac_basic_grid * grid, enum yac_location location);
162 size_t yac_basic_grid_add_coordinates(
163   struct yac_basic_grid * grid, enum yac_location location,
164   coordinate_pointer coordinates, size_t count);
165 size_t yac_basic_grid_add_coordinates_nocpy(
166   struct yac_basic_grid * grid, enum yac_location location,
167   coordinate_pointer coordinates);
168 size_t yac_basic_grid_add_mask(
169   struct yac_basic_grid * grid, enum yac_location location,
170   int const * mask, size_t count);
171 size_t yac_basic_grid_add_mask_nocpy(
172   struct yac_basic_grid * grid, enum yac_location location,
173   int const * mask);
174 void yac_basic_grid_delete(struct yac_basic_grid * grid);
175 
176 void yac_basic_grid_data_free(struct basic_grid_data grid);
177 
178 enum yac_location yac_str2loc(char const * location);
179 char const * yac_loc2str(enum yac_location location);
180 enum yac_location yac_get_location(int const location);
181 
182 struct yac_field_data yac_field_data_init();
183 size_t yac_field_data_add_mask_nocpy(
184   struct yac_field_data * field_data, int const * mask);
185 size_t yac_field_data_add_coordinates_nocpy(
186   struct yac_field_data * field_data, coordinate_pointer coordinates);
187 
188 struct yac_field_data_set yac_field_data_set_init();
189 size_t yac_field_data_set_add_mask(
190   struct yac_field_data_set * field_data_set,
191   enum yac_location location, int const * mask, size_t count);
192 size_t yac_field_data_set_add_coordinates(
193   struct yac_field_data_set * field_data_set,
194   enum yac_location location, coordinate_pointer coordinates,
195   size_t count);
196 size_t yac_field_data_set_add_mask_nocpy(
197   struct yac_field_data_set * field_data_set,
198   enum yac_location location, int const * mask);
199 size_t yac_field_data_set_add_coordinates_nocpy(
200   struct yac_field_data_set * field_data_set,
201   enum yac_location location, coordinate_pointer coordinates);
202 void yac_field_data_set_free(struct yac_field_data_set field_data_set);
203 
204 #endif // GRID_H
205 
206