YAC 3.13.2
Yet Another Coupler
Loading...
Searching...
No Matches
toy_multi_unstruct.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 <stdlib.h>
9#include <stdio.h>
10#include <unistd.h>
11#include "yac.h"
12#include "yac_utils.h"
13
14#include "toy_multi_common.h"
15
16// redefine YAC assert macros
17#undef YAC_ASSERT
18#define STR_USAGE "Usage: %s -g gridFilename\n"
19#define YAC_ASSERT(exp, msg) \
20 { \
21 if(!((exp))) { \
22 fprintf(stderr, "ERROR: %s\n" STR_USAGE, msg, argv[0]); \
23 exit(EXIT_FAILURE); \
24 } \
25 }
26
27static void parse_arguments(
28 int argc, char ** argv, char const ** gridFilename);
29
30int main (int argc, char *argv[]) {
31
32 double tic;
33
34 // Initialisation of MPI
35
36 MPI_Init (0, NULL);
37
38 MPI_Barrier(MPI_COMM_WORLD);
39 tic=MPI_Wtime();
40
41 char const * gridFilename = "iconR2B06-grid.nc"; // default grid file
42 parse_arguments(argc, argv, &gridFilename);
43 yac_cinit ();
45 yac_cdef_datetime("2008-03-09T16:05:07", "2008-03-10T16:05:07");
46
47 int comp_id;
48 char * comp_name = "ICON";
49
50 yac_cdef_comp(comp_name, &comp_id);
51
52 MPI_Comm comp_comm;
53 yac_cget_comp_comm(comp_id, &comp_comm);
54
55 int rank, comp_rank, comp_size;
56 MPI_Comm_rank(MPI_COMM_WORLD,&rank);
57 MPI_Comm_rank(comp_comm, &comp_rank);
58 MPI_Comm_size(comp_comm, &comp_size);
59 MPI_Comm_free(&comp_comm);
60
61 int nbr_vertices;
62 int nbr_cells;
63 int * num_vertices_per_cell;
64 int * cell_to_vertex;
65 double * x_vertices;
66 double * y_vertices;
67 double * x_cells;
68 double * y_cells;
69
70 int * cell_mask;
71 int * global_cell_id;
72 int * cell_core_mask;
73 int * global_corner_id;
74 int * corner_core_mask;
75
76 yac_read_part_icon_grid_information(gridFilename, &nbr_vertices, &nbr_cells,
77 &num_vertices_per_cell, &cell_to_vertex,
78 &x_vertices, &y_vertices, &x_cells,
79 &y_cells, &global_cell_id,
80 &cell_mask,
81 &cell_core_mask, &global_corner_id,
82 &corner_core_mask, comp_rank, comp_size);
83
84 int grid_id;
85 char grid_name[256];
86 sprintf(grid_name, "%s_grid", comp_name);
87
88 yac_cdef_grid_unstruct(grid_name, nbr_vertices, nbr_cells, num_vertices_per_cell,
89 x_vertices, y_vertices, cell_to_vertex, &grid_id);
90
95
96 int cell_point_id;
97 int corner_point_id;
98
100 grid_id, nbr_cells, YAC_LOCATION_CELL, x_cells, y_cells, &cell_point_id );
102 grid_id, nbr_vertices, YAC_LOCATION_CORNER, x_vertices, y_vertices,
103 &corner_point_id );
104
105 // inner land mask
106 // for (int i = 0; i < nbr_cells; ++i) cell_mask[i] = cell_mask[i] == 2;
107 // inner sea mask
108 // for (int i = 0; i < nbr_cells; ++i) cell_mask[i] = cell_mask[i] == -2;
109
110 // yac_cset_mask ( cell_mask, cell_point_id );
111
112 double * cell_point_data =
113 malloc(nbr_cells * sizeof(*cell_point_data));
114 double * corner_point_data =
115 malloc(nbr_vertices * sizeof(*corner_point_data));
116
117 int cell_to_vertex_offset = 0;
118
119 for (int i = 0; i < nbr_cells; ++i) {
120
121 double middle_point[3] = {0, 0, 0};
122
123 for (int j = 0; j < num_vertices_per_cell[i]; ++j) {
124
125 double curr_point[3];
126
127 LLtoXYZ(x_vertices[cell_to_vertex[cell_to_vertex_offset + j]],
128 y_vertices[cell_to_vertex[cell_to_vertex_offset + j]],
129 curr_point);
130
131 middle_point[0] += curr_point[0];
132 middle_point[1] += curr_point[1];
133 middle_point[2] += curr_point[2];
134 }
135
136 double scale = 1.0 / sqrt(middle_point[0] * middle_point[0] +
137 middle_point[1] * middle_point[1] +
138 middle_point[2] * middle_point[2]);
139
140 middle_point[0] *= scale;
141 middle_point[1] *= scale;
142 middle_point[2] *= scale;
143
144 double lon, lat;
145
146 XYZtoLL(middle_point, &lon, &lat);
147
148 cell_to_vertex_offset += num_vertices_per_cell[i];
149
150 cell_point_data[i] = yac_test_func(lon, lat);
151 }
152
153 for (int i = 0; i < nbr_vertices; ++i)
154 corner_point_data[i] = yac_test_func(x_vertices[i], y_vertices[i]);
155
156 MPI_Barrier(MPI_COMM_WORLD);
157 if (rank == 0)
158 printf(
159 "toy_multi_common(%s): Time for initialisation %f\n",
160 comp_name, MPI_Wtime()-tic);
161
162 // initialize VTK file
163 char vtk_filename[32];
164 sprintf(vtk_filename, "%s_out_%d.vtk", comp_name, comp_rank);
165
166 yac_coordinate_pointer point_data =
167 malloc(nbr_vertices * sizeof(*point_data));
168 for (int i = 0; i < nbr_vertices; ++i)
169 LLtoXYZ(x_vertices[i], y_vertices[i], point_data[i]);
170
171 YAC_VTK_FILE *vtk_file = yac_vtk_open(vtk_filename, "unstruct_out");
173 vtk_file, (double *)point_data, nbr_vertices);
175 vtk_file, (unsigned *)cell_to_vertex,
176 (unsigned*)num_vertices_per_cell, nbr_cells);
178 vtk_file, corner_core_mask, nbr_vertices, "corner_core_mask");
180 vtk_file, global_corner_id, nbr_vertices, "global_corner_id");
182 vtk_file, cell_core_mask, nbr_cells, "cell_core_mask");
184 vtk_file, global_cell_id, nbr_cells, "global_cell_id");
185
186 int ret =
188 comp_name, comp_id, grid_id, cell_point_id, corner_point_id,
189 cell_point_data, corner_point_data, vtk_file);
190
192 &global_cell_id,
194 &num_vertices_per_cell,
195 &global_corner_id,
196 &corner_core_mask,
198 &x_cells,
199 &y_cells,
200 &x_vertices,
201 &y_vertices);
202
203 free(point_data);
204 free(corner_point_data);
205 free(cell_point_data);
206
207 return ret;
208}
209
210static void parse_arguments(
211 int argc, char ** argv, char const ** gridFilename) {
212
213 int opt;
214 while ((opt = getopt(argc, argv, "g:")) != -1) {
215 YAC_ASSERT((opt == 'c') || (opt == 'g'), "invalid command argument")
216 switch (opt) {
217 default:
218 case 'g':
219 *gridFilename = optarg;
220 break;
221 }
222 }
223}
static void XYZtoLL(double const p_in[], double *lon, double *lat)
void yac_delete_icon_grid_data(int **cell_mask, int **global_cell_id, int **global_cell_id_rank, int **num_vertices_per_cell, int **global_corner_id, int **global_corner_id_rank, int **cell_to_vertex, double **x_cells, double **y_cells, double **x_vertices, double **y_vertices)
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)
int * cell_core_mask
int grid_id
int cell_point_id
int comp_id
int run_toy_multi_common(char const *comp_name, int comp_id, int grid_id, int cell_point_id, int corner_point_id, double *cell_point_data, double *corner_point_data, YAC_VTK_FILE *vtk_file)
static void parse_arguments(int argc, char **argv, char const **gridFilename)
#define YAC_ASSERT(exp, msg)
static void LLtoXYZ(double lon, double lat, double p_out[])
Definition toy_scrip.c:587
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_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_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_cset_global_index(int const *global_index, int location, int grid_id)
Definition yac.c:5049
void yac_cdef_datetime(const char *start_datetime, const char *end_datetime)
Definition yac.c:761
int const YAC_LOCATION_CELL
Definition yac.c:34
void yac_cinit(void)
Definition yac.c:514
int const YAC_LOCATION_CORNER
Definition yac.c:35
void yac_cget_comp_comm(int comp_id, MPI_Comm *comp_comm)
Definition yac.c:856
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:4867
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:1181
void yac_cdef_calendar(int calendar)
Definition yac.c:769
void yac_cset_core_mask(int const *is_core, int location, int grid_id)
Definition yac.c:5065
int const YAC_PROLEPTIC_GREGORIAN
Definition yac.c:68
void yac_cdef_comp(char const *comp_name, int *comp_id)
Definition yac.c:1013
double(* yac_coordinate_pointer)[3]
Definition yac_types.h:21