YAC 3.17.0
Yet Another Coupler
Loading...
Searching...
No Matches
perf_toy_icon.c
Go to the documentation of this file.
1// Copyright (c) 2024 The YAC Authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5// #define VERBOSE
6
7#include <mpi.h>
8#include <yaxt.h>
9#include <stdlib.h>
10#include <stdio.h>
11#include <unistd.h>
12#include <math.h>
13#include "yac.h"
14#include "yac_utils.h"
15
16/* ------------------------------------------------- */
17
18/* For simplicity we define the same 8 fields that are in the
19 * coupling configuration */
20
21const char * fieldName[] = {"icon_out", "cube_out"};
22
23// redefine YAC assert macros
24#undef YAC_ASSERT
25#define STR_USAGE "Usage: %s -c configFilename -g gridFilename\n"
26#define YAC_ASSERT(exp, msg) \
27 { \
28 if(!((exp))) { \
29 fprintf(stderr, "ERROR: %s\n" STR_USAGE, msg, argv[0]); \
30 exit(EXIT_FAILURE); \
31 } \
32 }
33
34static void parse_arguments(
35 int argc, char ** argv, char const ** configFilename, char const ** gridFilename);
36static inline void LLtoXYZ(double lon, double lat, double p_out[]);
37static inline void XYZtoLL (double const p_in[], double * lon, double * lat);
38
39int main (int argc, char *argv[]) {
40
41 // Initialisation of MPI
42
43 MPI_Init (NULL, NULL);
44
45 char const * configFilename = "perf_toy.yaml"; // default configuration file
46 char const * gridFilename = "iconR2B06-grid.nc"; // default grid file
47 parse_arguments(argc, argv, &configFilename, &gridFilename);
48
49 xt_initialize(MPI_COMM_WORLD);
50
51 /* The initialisation phase includes the reading of the
52 * coupling configuration */
53#ifdef VERBOSE
54 printf (". main: calling yac_cinit\n");
55#endif
56
57 double tic, toc, time;
58
59 MPI_Barrier(MPI_COMM_WORLD);
60
61 tic=MPI_Wtime();
62
63 // move yaml file to node cache
64 {
65 FILE *f = fopen(configFilename, "rb");
66 fseek(f, 0, SEEK_END);
67 long fsize = ftell(f);
68 fseek(f, 0, SEEK_SET); /* same as rewind(f); */
69
70 char *string = malloc(fsize + 1);
71 size_t dummy = fread(string, 1, fsize, f);
72 (void)(dummy); // UNUSED
73 fclose(f);
74 free(string);
75 }
76 yac_cinit ();
77 yac_cread_config_yaml(configFilename);
78
79 /* The usual component definition, here for two sequential components on the same process */
80
81#ifdef VERBOSE
82 printf (". main: calling yac_cdef_comp\n");
83#endif
84
85 int comp_id;
86 char * comp_name = "ICON";
87
88 yac_cdef_comp ( comp_name, &comp_id );
89#ifdef VERBOSE
90 printf ( ". main: defined %s with local comp ID %i \n", "ICON", comp_id );
91#endif
92
93 MPI_Comm local_comm;
94
95 yac_cget_comp_comm(comp_id, &local_comm);
96
97 int rank, size;
98
99 MPI_Comm_rank(local_comm,&rank);
100 MPI_Comm_size(local_comm,&size);
101
102 MPI_Comm_free(&local_comm);
103
104
105 int cell_point_id;
106 int corner_point_id;
107
108 int field_ids[2];
109 int grid_id;
110
111 /* Grid definition for the first component (ICON) */
112
113 int num_vertices;
114 int num_cells;
115 int * num_vertices_per_cell;
116 int * cell_to_vertex;
117 double * x_vertices;
118 double * y_vertices;
119 double * x_cells;
120 double * y_cells;
121
122 int * cell_mask;
123 int * global_cell_id;
124 int * cell_core_mask;
125 int * global_corner_id;
126 int * corner_core_mask;
127
128 yac_read_part_icon_grid_information(gridFilename, &num_vertices, &num_cells,
129 &num_vertices_per_cell, &cell_to_vertex,
130 &x_vertices, &y_vertices, &x_cells,
131 &y_cells, &global_cell_id,
132 &cell_mask,
133 &cell_core_mask, &global_corner_id,
134 &corner_core_mask, rank, size);
135
136 double * x_points, * y_points;
137
138 x_points = x_vertices;
139 y_points = y_vertices;
140
141 double * x_center, * y_center;
142
143 x_center = x_cells;
144 y_center = y_cells;
145
147 "icon_grid", num_vertices, num_cells, num_vertices_per_cell,
148 x_vertices, y_vertices, cell_to_vertex, &grid_id);
149
154
156 grid_id, num_cells, YAC_LOCATION_CELL, x_center, y_center, &cell_point_id );
158 grid_id, num_vertices, YAC_LOCATION_CORNER, x_points, y_points, &corner_point_id );
159
160 /* Field definition for the first component (ICON-atmosphere) */
161
164 &field_ids[0]);
167 &field_ids[1]);
168
169 toc=MPI_Wtime();
170 time = toc-tic;
171 printf ("ICON: Time for initialisation %f\n", time );
172
173 /* Search. */
174
175#ifdef VERBOSE
176 printf (". main: calling yac_cenddef\n");
177#endif
178
179 MPI_Barrier(MPI_COMM_WORLD);
180
181 tic=MPI_Wtime();
182
183 yac_cenddef ( );
184
185 toc=MPI_Wtime();
186 time = toc-tic;
187 printf ("ICON: Time for search %f\n", time );
188
189 double * conserv_in = malloc(num_cells * sizeof(*conserv_in));
190 double * avg_in = malloc(num_vertices * sizeof(*avg_in));
191
192 for (int i = 0; i < num_cells; ++i)
193 conserv_in[i] = -10;
194 for (int i = 0; i < num_vertices; ++i) avg_in[i] = -10;
195
196 double * cell_out = malloc(num_cells * sizeof(*cell_out));
197 double * corner_out = malloc(num_vertices * sizeof(*corner_out));
198
199 int err, info;
200
201 int cell_to_vertex_offset = 0;
202
203 for (int i = 0; i < num_cells; ++i) {
204
205 double middle_point[3] = {0, 0, 0};
206
207 for (int j = 0; j < num_vertices_per_cell[i]; ++j) {
208
209 double curr_point[3];
210
211 LLtoXYZ(x_vertices[cell_to_vertex[cell_to_vertex_offset + j]],
212 y_vertices[cell_to_vertex[cell_to_vertex_offset + j]],
213 curr_point);
214
215 middle_point[0] += curr_point[0];
216 middle_point[1] += curr_point[1];
217 middle_point[2] += curr_point[2];
218 }
219
220 double scale = 1.0 / sqrt(middle_point[0] * middle_point[0] +
221 middle_point[1] * middle_point[1] +
222 middle_point[2] * middle_point[2]);
223
224 middle_point[0] *= scale;
225 middle_point[1] *= scale;
226 middle_point[2] *= scale;
227
228 double lon, lat;
229
230 XYZtoLL(middle_point, &lon, &lat);
231
232 cell_to_vertex_offset += num_vertices_per_cell[i];
233
234 cell_out[i] = yac_test_func(lon, lat);
235 }
236 for (int i = 0; i < num_vertices; ++i)
237 corner_out[i] = yac_test_func(x_vertices[i], y_vertices[i]);
238
239 MPI_Barrier(MPI_COMM_WORLD);
240
241 tic=MPI_Wtime();
242
243 {
244 double *point_set_data[1];
245 double **collection_data[1] = {point_set_data};
246
247 point_set_data[0] = cell_out;
248 yac_cput(field_ids[0], 1, collection_data, &info, &err);
249 }
250
251 {
252 double *collection_data[1] = {conserv_in};
253
254 yac_cget(field_ids[1], 1, collection_data, &info, &err);
255 }
256
257 toc=MPI_Wtime();
258 time = toc-tic;
259 printf ("ICON: Time for ping-pong %f\n", time );
260
261#ifdef VTK_OUTPUT
262 //----------------------------------------------------------
263 // write field to vtk output file
264 //----------------------------------------------------------
265
266 char vtk_filename[32];
267
268 sprintf(vtk_filename, "perf_toy_icon_%d.vtk", rank);
269
270 double point_data[num_vertices][3];
271 for (int i = 0; i < num_vertices; ++i) {
272 LLtoXYZ(x_vertices[i], y_vertices[i], point_data[i]);
273 }
274
275 YAC_VTK_FILE *vtk_file = yac_vtk_open(vtk_filename, "unstruct_out");
276 yac_vtk_write_point_data(vtk_file, (double *)point_data, num_vertices);
277 yac_vtk_write_cell_data(vtk_file, (unsigned *)cell_to_vertex,
278 (unsigned*)num_vertices_per_cell, num_cells);
280 vtk_file, corner_core_mask, num_vertices, "corner_core_mask");
282 vtk_file, global_corner_id, num_vertices, "global_corner_id");
284 vtk_file, cell_core_mask, num_cells, "cell_core_mask");
286 vtk_file, global_cell_id, num_cells, "global_cell_id");
287
289 vtk_file, conserv_in, num_cells, "conserv_in");
291 vtk_file, cell_out, num_cells, "cell_out");
293 vtk_file, avg_in, num_vertices, "avg_in");
295 vtk_file, corner_out, num_vertices, "corner_out");
296
297 yac_vtk_close(vtk_file);
298
299#endif // VTK_OUTPUT
300
302
303 xt_finalize();
304
305 MPI_Finalize();
306
307 free(num_vertices_per_cell);
308 free(cell_to_vertex);
309 free(x_cells);
310 free(y_cells);
311 free(x_vertices);
312 free(y_vertices);
313 free(cell_mask);
314 free(global_cell_id);
315 free(cell_core_mask);
316 free(global_corner_id);
317 free(corner_core_mask);
318 free(conserv_in);
319 free(cell_out);
320 free(avg_in);
321 free(corner_out);
322
323 return EXIT_SUCCESS;
324}
325
326static void parse_arguments(
327 int argc, char ** argv,
328 char const ** configFilename, char const ** gridFilename) {
329
330 int opt;
331 while ((opt = getopt(argc, argv, "c:g:")) != -1) {
332 YAC_ASSERT((opt == 'c') || (opt == 'g'), "invalid command argument")
333 switch (opt) {
334 default:
335 case 'c':
336 *configFilename = optarg;
337 break;
338 case 'g':
339 *gridFilename = optarg;
340 break;
341 }
342 }
343}
344
345static inline void LLtoXYZ(double lon, double lat, double p_out[]) {
346
347 while (lon < -M_PI) lon += 2.0 * M_PI;
348 while (lon >= M_PI) lon -= 2.0 * M_PI;
349
350 double cos_lat = cos(lat);
351 p_out[0] = cos_lat * cos(lon);
352 p_out[1] = cos_lat * sin(lon);
353 p_out[2] = sin(lat);
354}
355
356static inline void XYZtoLL (double const p_in[], double * lon, double * lat) {
357
358 *lon = atan2(p_in[1] , p_in[0]);
359 *lat = M_PI_2 - acos(p_in[2]);
360}
static void parse_arguments(int argc, char **argv, char const **configFilename, char const **gridFilename)
static void LLtoXYZ(double lon, double lat, double p_out[])
const char * fieldName[]
#define YAC_ASSERT(exp, msg)
static void XYZtoLL(double const p_in[], double *lon, double *lat)
void yac_read_part_icon_grid_information(const char *filename, int *nbr_vertices, int *nbr_cells, int **num_vertices_per_cell, int **cell_to_vertex, double **x_vertices, double **y_vertices, double **x_cells, double **y_cells, int **global_cell_id, int **cell_mask, int **cell_core_mask, int **global_corner_id, int **corner_core_mask, int rank, int size)
int * cell_to_vertex
int * cell_mask
double yac_test_func(double lon, double lat)
size_t num_cells[2]
int info
int * cell_core_mask
int grid_id
int cell_point_id
int comp_id
void yac_vtk_write_cell_scalars_double(YAC_VTK_FILE *vtk_file, double *scalars, unsigned num_cells, char *name)
Definition vtk_output.c:264
void yac_vtk_write_cell_scalars_int(YAC_VTK_FILE *vtk_file, int *scalars, unsigned num_cells, char *name)
Definition vtk_output.c:250
void yac_vtk_write_point_scalars_double(YAC_VTK_FILE *vtk_file, double *scalars, unsigned num_points, char *name)
Definition vtk_output.c:292
void yac_vtk_write_point_data(YAC_VTK_FILE *vtk_file, double *point_data, unsigned num_points)
Definition vtk_output.c:69
void yac_vtk_write_point_scalars_int(YAC_VTK_FILE *vtk_file, int *scalars, unsigned num_points, char *name)
Definition vtk_output.c:278
void yac_vtk_close(YAC_VTK_FILE *vtk_file)
Definition vtk_output.c:403
void yac_vtk_write_cell_data(YAC_VTK_FILE *vtk_file, unsigned *cell_corners, unsigned *num_points_per_cell, unsigned num_cells)
Definition vtk_output.c:88
YAC_VTK_FILE * yac_vtk_open(const char *filename, const char *title)
Definition vtk_output.c:45
void yac_cenddef(void)
Definition yac.c:4912
void yac_cset_global_index(int const *global_index, int location, int grid_id)
Definition yac.c:5651
int const YAC_LOCATION_CELL
Definition yac.c:36
void yac_cinit(void)
Definition yac.c:641
void yac_cfinalize()
Finalises YAC.
Definition yac.c:867
int const YAC_LOCATION_CORNER
Definition yac.c:37
void yac_cget_comp_comm(int comp_id, MPI_Comm *comp_comm)
Definition yac.c:1004
void yac_cget(int const field_id, int collection_size, double **recv_field, int *info, int *ierr)
Definition yac.c:3199
int const YAC_TIME_UNIT_SECOND
Definition yac.c:62
void yac_cdef_grid_unstruct(const char *grid_name, int nbr_vertices, int nbr_cells, int *num_vertices_per_cell, double *x_vertices, double *y_vertices, int *cell_to_vertex, int *grid_id)
Definition yac.c:5466
void yac_cdef_points_unstruct(int const grid_id, int const nbr_points, int const located, double const *x_points, double const *y_points, int *point_id)
Definition yac.c:1523
void yac_cput(int const field_id, int const collection_size, double ***const send_field, int *info, int *ierr)
Definition yac.c:4161
void yac_cread_config_yaml(const char *yaml_filename)
Definition yac.c:722
void yac_cset_core_mask(int const *is_core, int location, int grid_id)
Definition yac.c:5667
void yac_cdef_comp(char const *comp_name, int *comp_id)
Definition yac.c:1223
void yac_cdef_field(char const *name, int const comp_id, int const *point_ids, int const num_pointsets, int collection_size, const char *timestep, int time_unit, int *field_id)
Definition yac.c:1825