YAC 3.18.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_dummy_coupling10_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 <stdio.h>
7#include <mpi.h>
8
9#include <yac.h>
10#include "tests.h"
11#include "geometry.h"
12
19int main(void) {
20
21 yac_cinit();
23 yac_cdef_datetime("1990-01-01T00:00:00", "2000-01-01T00:00:00");
24
25 int comm_rank, comm_size;
26 MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank);
27 MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
28
29 enum {NUM_PROCS = 4};
30 if (comm_size != NUM_PROCS) {
31 if (comm_rank == 0) PUT_ERR("This test must be run with 4 processes");
33 return TEST_EXIT_CODE;
34 }
35
36 // Define component (setup only has a single component)
37 char const * comp_name = "main_component";
38 int comp_id;
39 yac_cdef_comp(comp_name, &comp_id);
40
41 // Global grid dimensions
42 enum {
43 GLOBAL_NUM_CELLS_X = 16,
44 GLOBAL_NUM_CELLS_Y = 16,
45 };
46
47 // Allocate and fill global coordinates for grid
48 double vertices_x[GLOBAL_NUM_CELLS_X+1];
49 double vertices_y[GLOBAL_NUM_CELLS_Y+1];
50 double cells_x[GLOBAL_NUM_CELLS_X];
51 double cells_y[GLOBAL_NUM_CELLS_Y];
52 for (size_t i = 0; i <= GLOBAL_NUM_CELLS_X; ++i)
53 vertices_x[i] =
54 (-5.0 + (10.0 * (double)i) / (double)GLOBAL_NUM_CELLS_X) * YAC_RAD;
55 for (size_t j = 0; j <= GLOBAL_NUM_CELLS_Y; ++j)
56 vertices_y[j] =
57 (-5.0 + (10.0 * (double)j) / (double)GLOBAL_NUM_CELLS_Y) * YAC_RAD;
58 for (size_t i = 0; i < GLOBAL_NUM_CELLS_X; ++i)
59 cells_x[i] = 0.5 * (vertices_x[i] + vertices_x[i+1]);
60 for (size_t j = 0; j < GLOBAL_NUM_CELLS_Y; ++j)
61 cells_y[j] = 0.5 * (vertices_y[j] + vertices_y[j+1]);
62
63 // Define local grid regular grid (each process uses one fourth of grid rows)
64 int local_start_x = 0;
65 int local_start_y = (comm_rank * GLOBAL_NUM_CELLS_Y) / comm_size;
66 int local_count_x = GLOBAL_NUM_CELLS_X;
67 int local_count_y =
68 ((comm_rank + 1) * GLOBAL_NUM_CELLS_Y) / comm_size - local_start_y;
69 char const * grid_name = "main_grid";
70 int nbr_cells[2] = {local_count_x, local_count_y};
71 int nbr_vertices[2] = {local_count_x + 1, local_count_y + 1};
72 int cyclic[2] = {0,0};
73 int grid_id;
75 grid_name, nbr_vertices, cyclic,
76 vertices_x + local_start_x, vertices_y + local_start_y, &grid_id);
77
78 // Define cell center coordinates
79 int cell_point_id;
81 grid_id, nbr_cells, YAC_LOCATION_CELL,
82 cells_x + local_start_x, cells_y + local_start_y, &cell_point_id);
83
84 // Generate global coast and ocean mask
85 enum {COAST_COLUMN = 8}; // column 8 is the coast
86 int coast_mask[GLOBAL_NUM_CELLS_Y][GLOBAL_NUM_CELLS_X];
87 int ocean_mask[GLOBAL_NUM_CELLS_Y][GLOBAL_NUM_CELLS_X];
88 for (size_t j = 0; j < GLOBAL_NUM_CELLS_Y; ++j) {
89 for (size_t i = 0; i < GLOBAL_NUM_CELLS_X; ++i) {
90
91 // column 8 is the coast
92 coast_mask[j][i] = (i == COAST_COLUMN);
93
94 // everything right of coast is ocean
95 ocean_mask[j][i] = (i > COAST_COLUMN);
96 }
97 }
98 int coast_mask_id, ocean_mask_id;
100 grid_id, local_count_x * local_count_y, YAC_LOCATION_CELL,
101 &coast_mask[local_start_y][0], &coast_mask_id);
103 grid_id, local_count_x * local_count_y, YAC_LOCATION_CELL,
104 &ocean_mask[local_start_y][0], &ocean_mask_id);
105
106 // Define fields
107 enum {COLLECTION_SIZE = 1, NUM_POINTSETS = 1};
108 char const * timestep = "1";
109 int temp_field_id, sst_field_id, runoff_coast_field_id, runoff_ocean_field_id;
110 // temp: no mask
113 COLLECTION_SIZE, timestep, YAC_TIME_UNIT_HOUR, &temp_field_id);
114 // sst: ocean mask
116 "sst", comp_id, &cell_point_id, &ocean_mask_id, NUM_POINTSETS,
117 COLLECTION_SIZE, timestep, YAC_TIME_UNIT_HOUR, &sst_field_id);
118 // runoff: coast mask
120 "runoff", comp_id, &cell_point_id, &coast_mask_id, NUM_POINTSETS,
121 COLLECTION_SIZE, timestep, YAC_TIME_UNIT_HOUR, &runoff_coast_field_id);
122 // runoff: ocean mask
124 "river_runoff", comp_id, &cell_point_id, &ocean_mask_id, NUM_POINTSETS,
125 COLLECTION_SIZE, timestep, YAC_TIME_UNIT_HOUR, &runoff_ocean_field_id);
126
127
128 // Define coupling
129 // temp -> sst using 1NN
130 enum {SRC_LAG = 0, TGT_LAG = 0};
131 int interp_stack_1nn;
133 "[{\"nnn\": {\"n\": 1}}]", &interp_stack_1nn);
135 comp_name, grid_name, "temp",
136 comp_name, grid_name, "sst",
138 interp_stack_1nn, SRC_LAG, TGT_LAG);
139 yac_cfree_interp_stack_config(interp_stack_1nn);
140 // runoff (coast) -> runoff (ocean) using SPMAP
141 int interp_stack_spmap;
143 "[\"source_to_target_map\", {\"fixed\": {\"user_value\": 999}}]",
144 &interp_stack_spmap);
146 comp_name, grid_name, "runoff",
147 comp_name, grid_name, "river_runoff",
149 interp_stack_spmap, SRC_LAG, TGT_LAG);
150 yac_cfree_interp_stack_config(interp_stack_spmap);
151
152 // End of definition
153 yac_cenddef();
154
155
156 // Set up dummy data for fields
157 double global_temp_data[GLOBAL_NUM_CELLS_Y][GLOBAL_NUM_CELLS_X];
158 double global_runoff_data[GLOBAL_NUM_CELLS_Y][GLOBAL_NUM_CELLS_X];
159
160 // Initialize temp: simple pattern, e.g., temp = 100.0 + 10.0 * j + i
161 for (size_t j = 0; j < GLOBAL_NUM_CELLS_Y; ++j) {
162 for (size_t i = 0; i < GLOBAL_NUM_CELLS_X; ++i) {
163 global_temp_data[j][i] = 100.0 + 10.0 * (double)j + (double)i;
164 }
165 }
166
167 // Initialize runoff: set coast column to 42.0, rest to -1.0
168 for (size_t j = 0; j < GLOBAL_NUM_CELLS_Y; ++j) {
169 for (size_t i = 0; i < GLOBAL_NUM_CELLS_X; ++i) {
170 if (i == COAST_COLUMN) {
171 global_runoff_data[j][i] = (double)j;
172 } else {
173 global_runoff_data[j][i] = -1.0;
174 }
175 }
176 }
177
178 // Output buffers for received fields
179 double sst_data[local_count_y][local_count_x];
180 double river_runoff_data[local_count_y][local_count_x];
181 for (int j = 0; j < local_count_y; ++j) {
182 for (int i = 0; i < local_count_x; ++i) {
183 sst_data[j][i] = -2.0;
184 river_runoff_data[j][i] = -2.0;
185 }
186 }
187
188 // Do single exchange of fields using yac_cexchange (pairwise)
189 int info, ierror = 0;
191 temp_field_id, sst_field_id, COLLECTION_SIZE,
192 &global_temp_data[local_start_y][0], &sst_data[0][0],
193 &info, &info, &ierror);
195 runoff_coast_field_id, runoff_ocean_field_id, COLLECTION_SIZE,
196 &global_runoff_data[local_start_y][0], &river_runoff_data[0][0],
197 &info, &info, &ierror);
198
199 // Reference for sst:
200 // for ocean points, should be nearest temp (column > COAST_COLUMN)
201 double ref_global_sst_data[GLOBAL_NUM_CELLS_Y][GLOBAL_NUM_CELLS_X];
202 for (size_t j = 0; j < GLOBAL_NUM_CELLS_Y; ++j) {
203 for (size_t i = 0; i < GLOBAL_NUM_CELLS_X; ++i) {
204 if (i > COAST_COLUMN) {
205 // 1NN: nearest is itself (since grid is regular)
206 ref_global_sst_data[j][i] = global_temp_data[j][i];
207 } else {
208 ref_global_sst_data[j][i] = -2.0; // not ocean
209 }
210 }
211 }
212
213 // Reference for river_runoff:
214 // ocean points adjacent to coast should receive its value,
215 // other ocean points the fixed value
216 double ref_global_river_runoff_data[GLOBAL_NUM_CELLS_Y][GLOBAL_NUM_CELLS_X];
217 for (size_t j = 0; j < GLOBAL_NUM_CELLS_Y; ++j) {
218 for (size_t i = 0; i < GLOBAL_NUM_CELLS_X; ++i) {
219 if (i == COAST_COLUMN + 1) {
220 // SPMAP: ocean receives from coast column in same row
221 ref_global_river_runoff_data[j][i] = global_runoff_data[j][COAST_COLUMN];
222 } else if (i > COAST_COLUMN + 1) {
223 ref_global_river_runoff_data[j][i] = 999.0; // fixed value
224 } else {
225 ref_global_river_runoff_data[j][i] = -2.0; // not ocean
226 }
227 }
228 }
229
230 // Check received sst and river_runoff
231 for (int j = local_start_y; j < local_count_y; ++j) {
232 for (int i = local_start_x; i < local_count_x; ++i) {
233 if (sst_data[j][i] !=
234 ref_global_sst_data[local_start_y + j][local_start_x + i]) {
235 PUT_ERR("Error in sst data");
236 }
237 if (river_runoff_data[j][i] !=
238 ref_global_river_runoff_data[local_start_y + j][local_start_x + i]) {
239 PUT_ERR("Error in sst data");
240 }
241 }
242 }
243
244 // Finalize YAC
245 yac_cfinalize ();
246
247 return TEST_EXIT_CODE;
248}
#define YAC_RAD
@ COLLECTION_SIZE
unsigned cyclic[2]
#define TEST_EXIT_CODE
Definition tests.h:15
#define PUT_ERR(string)
Definition tests.h:10
int info
int grid_id
int cell_point_id
int ierror
int comp_id
void yac_cenddef(void)
Definition yac.c:5091
void yac_cdef_field_mask(char const *name, int const comp_id, int const *point_ids, int const *mask_ids, int const num_pointsets, int collection_size, const char *timestep, int time_unit, int *field_id)
Definition yac.c:1901
void yac_cdef_datetime(const char *start_datetime, const char *end_datetime)
Definition yac.c:1055
int const YAC_LOCATION_CELL
Definition yac.c:37
void yac_cget_interp_stack_config_from_string_json(char const *interp_stack_config, int *interp_stack_config_id)
Definition yac.c:6725
int const YAC_TIME_UNIT_HOUR
Definition yac.c:65
void yac_cinit(void)
Definition yac.c:768
void yac_cfinalize()
Finalises YAC.
Definition yac.c:1030
void yac_cexchange_(int const send_field_id, int const recv_field_id, int const collection_size, double *send_field, double *recv_field, int *send_info, int *recv_info, int *ierr)
Definition yac.c:4347
void yac_cdef_grid_reg2d(const char *grid_name, int nbr_vertices[2], int cyclic[2], double *x_vertices, double *y_vertices, int *grid_id)
Definition yac.c:5608
void yac_cdef_points_reg2d(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:1583
void yac_cdef_calendar(int calendar)
Definition yac.c:1063
int const YAC_PROLEPTIC_GREGORIAN
Definition yac.c:72
void yac_cdef_mask(int const grid_id, int const nbr_points, int const located, int const *is_valid, int *mask_id)
Definition yac.c:1870
void yac_cfree_interp_stack_config(int interp_stack_config_id)
Definition yac.c:5938
void yac_cdef_comp(char const *comp_name, int *comp_id)
Definition yac.c:1383
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:1985
void yac_cdef_couple(char const *src_comp_name, char const *src_grid_name, char const *src_field_name, char const *tgt_comp_name, char const *tgt_grid_name, char const *tgt_field_name, char const *coupling_timestep, int time_unit, int time_reduction, int interp_stack_config_id, int src_lag, int tgt_lag)
Definition yac.c:2541
int const YAC_REDUCTION_TIME_NONE
Definition yac.c:56