YAC 3.13.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_read_scrip_parallel_c.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#include <stdlib.h>
6#include <string.h>
7#include <unistd.h>
8#include <mpi.h>
9
10#include "tests.h"
11#include "test_common.h"
12#include "grid_file_common.h"
13#include "read_scrip_grid.h"
14#include "grid2vtk.h"
15#include "io_utils.h"
16
22// #define HAVE_OASIS_FILES
23// #define WRITE_VTK_GRID_FILE
24#define DUMMY_GRID_NAME ("dummy_grid")
25
26int main(void) {
27
28 MPI_Init(NULL, NULL);
29
30 int comm_size, comm_rank;
31
32 MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
33 MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank);
34
35 if ((comm_size != 4) && (comm_rank == 0)) {
36 fputs("wrong number of processes (has to be four)\n", stderr);
37 exit(EXIT_FAILURE);
38 }
39
40#ifdef HAVE_OASIS_FILES
41 char * grid_filename = "../examples/OASIS-grid/grids.nc";
42 char * mask_filename = "../examples/OASIS-grid/masks_no_atm.nc";
43#else
44 char * grid_filename = "test_read_scrip_parallel_c_grids.nc";
45 char * mask_filename = "test_read_scrip_parallel_c_masks.nc";
46 if (comm_rank == 0)
48 DUMMY_GRID_NAME, grid_filename, mask_filename, 1,
49 360, 180, (double[]){0.0,360.0}, (double[]){-90.0, 90.0});
50 MPI_Barrier(MPI_COMM_WORLD);
51#endif
52
53 int valid_mask_value = 0;
54
55 if (!yac_file_exists(grid_filename)) return EXIT_FAILURE;
56 if (!yac_file_exists(mask_filename)) return EXIT_FAILURE;
57
58 { // read unstructured grid
59#ifdef HAVE_OASIS_FILES
60 // char * gridname = "bggd";
61 // char * gridname = "icoh";
62 // char * gridname = "icos";
63 // char * gridname = "nogt";
64 char * gridname = "sse7";
65 // char * gridname = "torc";
66 // char * gridname = "ssea";
67#else
68 char * gridname = DUMMY_GRID_NAME;
69#endif
70
71 size_t * duplicated_cell_idx = NULL;
72 yac_int * orig_cell_global_ids = NULL;
73 size_t nbr_duplicated_cells = 0;
74
75 size_t cell_coord_idx;
76
77 set_even_io_rank_list(MPI_COMM_WORLD);
78
79 for (int reader_idx = 0; reader_idx < 2; ++reader_idx) {
80
81 struct yac_basic_grid * scrip_grid;
82
83 if (reader_idx == 0)
84 scrip_grid =
86 grid_filename, mask_filename, MPI_COMM_WORLD,
87 gridname, valid_mask_value, gridname,
88 0, &cell_coord_idx, &duplicated_cell_idx,
89 &orig_cell_global_ids, &nbr_duplicated_cells);
90 else {
91 int point_location;
92 scrip_grid =
94 grid_filename, mask_filename, MPI_COMM_WORLD,
95 gridname, valid_mask_value, gridname,
96 0, &cell_coord_idx, &duplicated_cell_idx,
97 &orig_cell_global_ids, &nbr_duplicated_cells, &point_location);
98 if (point_location != YAC_LOC_CELL)
99 PUT_ERR("error in yac_read_scrip_generic_basic_grid_parallel")
100 }
101
102 free(duplicated_cell_idx);
103 free(orig_cell_global_ids);
104
105#ifdef WRITE_VTK_GRID_FILE
106 char vtk_gridname[64];
107 snprintf(vtk_gridname, sizeof(vtk_gridname), "%s_%d", gridname, comm_rank);
108 yac_write_basic_grid_to_file(scrip_grid, vtk_gridname);
109#endif // WRITE_VTK_GRID_FILE
110
111 yac_basic_grid_delete(scrip_grid);
112 }
114 }
115
116 { // read lon/lat grid
117#ifdef HAVE_OASIS_FILES
118 // char * gridname = "bggd";
119 // char * gridname = "icoh";
120 // char * gridname = "icos";
121 // char * gridname = "nogt";
122 // char * gridname = "sse7";
123 char * gridname = "torc";
124 // char * gridname = "ssea";
125#else
126 char * gridname = DUMMY_GRID_NAME;
127#endif
128
129 set_even_io_rank_list(MPI_COMM_WORLD);
130 struct yac_basic_grid * scrip_grid =
132 grid_filename, mask_filename, MPI_COMM_WORLD,
133 gridname, valid_mask_value, gridname,
134 1, NULL, NULL, NULL, NULL);
135
136#ifdef WRITE_VTK_GRID_FILE
137 char vtk_gridname[64];
138 snprintf(
139 vtk_gridname, sizeof(vtk_gridname), "%s_ll_%d", gridname, comm_rank);
140 yac_write_basic_grid_to_file(scrip_grid, vtk_gridname);
141#endif // WRITE_VTK_GRID_FILE
142
143 yac_basic_grid_delete(scrip_grid);
145 }
146
147 { // read cloud grid (containing only cell centers)
148
149 enum {NUM_LAT = 181, NUM_LON = 380};
150
151 // create grid files
152 char * grid_filename = "test_read_scrip_cloud_parallel_c_grids.nc";
153 char * mask_filename = "test_read_scrip_cloud_parallel_c_masks.nc";
154 if (comm_rank == 0)
156 DUMMY_GRID_NAME, grid_filename, mask_filename, 0,
157 NUM_LON, NUM_LAT, (double[]){0.0, 380.0}, (double[]){-90.0, 90.0});
158 MPI_Barrier(MPI_COMM_WORLD);
159
160 setenv("YAC_IO_RANK_LIST", "0,1,2,3", 1);
161 setenv("YAC_IO_MAX_NUM_RANKS_PER_NODE", "4", 1);
162
163 for (int reader_idx = 0; reader_idx < 2; ++reader_idx) {
164
165 size_t vertex_coord_idx;
166 size_t * duplicated_vertex_idx;
167 yac_int * orig_vertex_global_ids;
168 size_t nbr_duplicated_vertices;
169 struct yac_basic_grid * scrip_cloud_grid;
170
171 if (reader_idx == 0)
172 scrip_cloud_grid =
174 grid_filename, mask_filename, MPI_COMM_WORLD, DUMMY_GRID_NAME, 0,
175 DUMMY_GRID_NAME, &vertex_coord_idx, &duplicated_vertex_idx,
176 &orig_vertex_global_ids, &nbr_duplicated_vertices);
177 else {
178 int point_location;
179 scrip_cloud_grid =
181 grid_filename, mask_filename, MPI_COMM_WORLD, DUMMY_GRID_NAME, 0,
182 DUMMY_GRID_NAME, 0, &vertex_coord_idx, &duplicated_vertex_idx,
183 &orig_vertex_global_ids, &nbr_duplicated_vertices, &point_location);
184 if (point_location != YAC_LOC_CORNER)
185 PUT_ERR("error in yac_read_scrip_generic_basic_grid_parallel");
186 }
187
188#ifdef WRITE_VTK_GRID_FILE
189 char vtk_gridname[64];
190 snprintf(
191 vtk_gridname, sizeof(vtk_gridname), "%s_%d", "scrip_cloud", comm_rank);
192 yac_write_basic_grid_to_file(scrip_cloud_grid, vtk_gridname);
193#endif // WRITE_VTK_GRID_FILE
194
195 if (strcmp(yac_basic_grid_get_name(scrip_cloud_grid), DUMMY_GRID_NAME))
196 PUT_ERR("wrong grid name");
197
198 struct yac_basic_grid_data * scrip_cloud_grid_data =
199 yac_basic_grid_get_data(scrip_cloud_grid);
200
201 if (scrip_cloud_grid_data->num_cells != 0)
202 PUT_ERR("wrong number of cells");
203
204 int local_num_vertices = (int)scrip_cloud_grid_data->num_vertices;
205 int local_num_unique_vertices =
206 local_num_vertices - (int)nbr_duplicated_vertices;
207 int global_num_vertices, global_num_unique_vertices;
208 MPI_Allreduce(
209 &local_num_vertices, &global_num_vertices, 1,
210 MPI_INT, MPI_SUM, MPI_COMM_WORLD);
211 MPI_Allreduce(
212 &local_num_unique_vertices, &global_num_unique_vertices, 1,
213 MPI_INT, MPI_SUM, MPI_COMM_WORLD);
214 if (global_num_vertices != NUM_LAT * NUM_LON)
215 PUT_ERR("wrong global number of vertices");
216 if (global_num_unique_vertices != 181 * 360)
217 PUT_ERR("wrong global number of unique vertices");
218
219 if (scrip_cloud_grid_data->num_edges != 0)
220 PUT_ERR("wrong number of edges");
221
222 free(duplicated_vertex_idx);
223 free(orig_vertex_global_ids);
224 yac_basic_grid_delete(scrip_cloud_grid);
225 }
226
228
229 // delete grid files
230 MPI_Barrier(MPI_COMM_WORLD);
231 if (comm_rank == 0) {
232 unlink(grid_filename);
233 unlink(mask_filename);
234 }
235 }
236
237#ifndef HAVE_OASIS_FILES
238 MPI_Barrier(MPI_COMM_WORLD);
239 if (comm_rank == 0) {
240 unlink(grid_filename);
241 unlink(mask_filename);
242 }
243#endif
244
245 MPI_Finalize();
246
247 return TEST_EXIT_CODE;
248}
249
struct yac_basic_grid_data * yac_basic_grid_get_data(struct yac_basic_grid *grid)
Definition basic_grid.c:140
char const * yac_basic_grid_get_name(struct yac_basic_grid *grid)
Definition basic_grid.c:131
void yac_basic_grid_delete(struct yac_basic_grid *grid)
Definition basic_grid.c:73
void yac_write_basic_grid_to_file(struct yac_basic_grid *grid, char const *name)
Definition grid2vtk.c:283
void write_dummy_scrip_grid_file(char *grid_name, char *grid_filename, char *mask_filename, int with_corners, size_t num_lon, size_t num_lat, double lon_range[2], double lat_range[2])
int yac_file_exists(const char *filename)
Definition utils_core.c:12
@ YAC_LOC_CORNER
Definition location.h:15
@ YAC_LOC_CELL
Definition location.h:14
struct yac_basic_grid * yac_read_scrip_basic_grid_parallel(char const *grid_filename, char const *mask_filename, MPI_Comm comm, char const *grid_name, int valid_mask_value, char const *name, int use_ll_edges, size_t *cell_coord_idx, size_t **duplicated_cell_idx, yac_int **orig_cell_global_ids, size_t *nbr_duplicated_cells)
struct yac_basic_grid * yac_read_scrip_cloud_basic_grid_parallel(char const *grid_filename, char const *mask_filename, MPI_Comm comm, char const *grid_name, int valid_mask_value, char const *name, size_t *vertex_coord_idx, size_t **duplicated_vertex_idx, yac_int **orig_vertex_global_ids, size_t *nbr_duplicated_vertices)
struct yac_basic_grid * yac_read_scrip_generic_basic_grid_parallel(char const *grid_filename, char const *mask_filename, MPI_Comm comm, char const *grid_name, int valid_mask_value, char const *name, int use_ll_edges, size_t *point_coord_idx, size_t **duplicated_point_idx, yac_int **orig_point_global_ids, size_t *nbr_duplicated_points, int *point_location)
void clear_yac_io_env()
void set_even_io_rank_list(MPI_Comm comm)
#define DUMMY_GRID_NAME
#define TEST_EXIT_CODE
Definition tests.h:14
#define PUT_ERR(string)
Definition tests.h:10
YAC_INT yac_int
Definition yac_types.h:15