YAC 3.13.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_dummy_coupling9_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 <mpi.h>
6
7#include <stdlib.h>
8#include <stdio.h>
9#include <string.h>
10#include <math.h>
11#include <float.h>
12#include "tests.h"
13#include "test_common.h"
14#include "yac.h"
15
21#define YAC_RAD (0.01745329251994329576923690768489) // M_PI / 180
22
23enum {
26 NUM_POINTS = 9
27};
28
29static char const * mask_types[3] =
30 {"even", "odd", "none"};
31
32typedef int (*func_check_src_ptr)(
33 double value, int idx, int collection_idx, int with_field_mask);
34typedef int (*func_check_tgt_ptr)(
35 double value, int idx, int collection_idx, int with_field_mask,
36 func_check_src_ptr utest_check_src);
37
38static int utest_check_src_no_mask(
39 double value, int idx, int collection_idx, int with_field_mask);
40static int utest_check_src_even_mask(
41 double value, int idx, int collection_idx, int with_field_mask);
42static int utest_check_src_odd_mask(
43 double value, int idx, int collection_idx, int with_field_mask);
44static int utest_check_tgt_no_mask(
45 double value, int idx, int collection_idx, int with_field_mask,
46 func_check_src_ptr utest_check_src);
47static int utest_check_tgt_even_mask(
48 double value, int idx, int collection_idx, int with_field_mask,
49 func_check_src_ptr utest_check_src);
50static int utest_check_tgt_odd_mask(
51 double value, int idx, int collection_idx, int with_field_mask,
52 func_check_src_ptr utest_check_src);
53
54func_check_src_ptr utest_check_src[3] = {
55 utest_check_src_even_mask,
56 utest_check_src_odd_mask,
57 utest_check_src_no_mask};
58func_check_tgt_ptr utest_check_tgt[3] = {
59 utest_check_tgt_even_mask,
60 utest_check_tgt_odd_mask,
61 utest_check_tgt_no_mask};
62
63int main(int argc, char** argv) {
64
65 yac_cinit();
67
68 if (argc != 2) {
69 PUT_ERR("ERROR: missing config file directory");
70 MPI_Finalize();
71 return TEST_EXIT_CODE;
72 }
73
74 char * yaml_filename =
75 strcat(
76 strcpy(
77 malloc(strlen(argv[1]) + 32), argv[1]), "coupling_test9.yaml");
79 free(yaml_filename);
80
81 int size, rank;
82 MPI_Comm_rank ( MPI_COMM_WORLD, &rank );
83 MPI_Comm_size ( MPI_COMM_WORLD, &size );
84
85 if (size != 2) {
86 fputs("wrong number of processes (has to be 2)\n", stderr);
87 exit(EXIT_FAILURE);
88 }
89
90 int is_target = rank == 1;
91
92 // define local component
93 int comp_id;
94 char const * src_comp_name = "source_comp";
95 char const * tgt_comp_name = "target_comp";
96 yac_cdef_comp((is_target)?tgt_comp_name:src_comp_name, &comp_id);
97
98 // define grid (both components use an identical grid)
99 int grid_id;
100 char const * src_grid_name = "source_grid";
101 char const * tgt_grid_name = "target_grid";
103 (is_target)?tgt_grid_name:src_grid_name, (int[2]){3,3}, (int[2]){0,0},
104 (double[]){0*YAC_RAD,1*YAC_RAD,2*YAC_RAD},
105 (double[]){0*YAC_RAD,1*YAC_RAD,2*YAC_RAD}, &grid_id);
106
107 // define points at the vertices of the grid
108 int point_id;
110 grid_id, (int[2]){3,3}, YAC_LOCATION_CORNER,
111 (double[]){0*YAC_RAD,1*YAC_RAD,2*YAC_RAD},
112 (double[]){0*YAC_RAD,1*YAC_RAD,2*YAC_RAD}, &point_id);
113
114 // define masks for vertices
115 int default_mask_id, dummy_mask_id;
118 (int[3*3]){1,0,0, 0,0,0, 0,0,0}, &default_mask_id);
119 char const * even_mask_name = "even_mask";
122 (int[3*3]){1,0,1, 0,1,0, 1,0,1}, even_mask_name, &dummy_mask_id);
123 char const * odd_mask_name = "odd_mask";
126 (int[3*3]){0,1,0, 1,0,1, 0,1,0}, odd_mask_name, &dummy_mask_id);
127
128 // define field
129 int field_ids[2][2][3][3];
130 for (int config_from_file = 0; config_from_file < 2; ++config_from_file) {
131 for (int with_field_mask = 0; with_field_mask < 2; ++with_field_mask) {
132 for (int source_mask_type = 0; source_mask_type < 3; ++source_mask_type) {
133 for (int target_mask_type = 0; target_mask_type < 3; ++target_mask_type) {
134 char field_name[128];
135 sprintf(
136 field_name, "src2tgt_%s_%s_field_mask_%s_src_mask_%s_tgt_mask",
137 config_from_file?"yaml":"manual",
138 with_field_mask?"with":"without",
139 mask_types[source_mask_type],
140 mask_types[target_mask_type]);
141 if (with_field_mask) {
143 field_name, comp_id, &point_id, &default_mask_id, NUM_POINTSETS,
145 &field_ids[config_from_file]
146 [with_field_mask]
147 [source_mask_type]
148 [target_mask_type]);
149 } else {
151 field_name, comp_id, &point_id, NUM_POINTSETS,
153 &field_ids[config_from_file]
154 [with_field_mask]
155 [source_mask_type]
156 [target_mask_type]);
157 }
158 }
159 }
160 }
161 }
162
163 // define manual couples
164 int interp_stack_config;
165 yac_cget_interp_stack_config(&interp_stack_config);
167 interp_stack_config, YAC_NNN_AVG, 1, 0.0, 0.0);
168 int ext_couple_config;
169 yac_cget_ext_couple_config(&ext_couple_config);
170 for (int with_field_mask = 0; with_field_mask < 2; ++with_field_mask) {
171 for (int source_mask_type = 0; source_mask_type < 3; ++source_mask_type) {
172 for (int target_mask_type = 0; target_mask_type < 3; ++target_mask_type) {
173 if (source_mask_type != 2)
175 ext_couple_config, 1, source_mask_type?&odd_mask_name:&even_mask_name);
176 else
177 yac_cset_ext_couple_config_src_mask_names(ext_couple_config, 0, NULL);
178 if (target_mask_type != 2)
180 ext_couple_config, target_mask_type?odd_mask_name:even_mask_name);
181 else
182 yac_cset_ext_couple_config_tgt_mask_name(ext_couple_config, NULL);
183
184 size_t num_src_maks_names;
185 char const * const * src_mask_names;
187 ext_couple_config, &num_src_maks_names, &src_mask_names);
188 if ((num_src_maks_names != (source_mask_type != 2)) ||
189 ((source_mask_type == 2) && (src_mask_names != NULL)) ||
190 ((source_mask_type != 2) &&
191 strcmp(src_mask_names[0],
192 source_mask_type?odd_mask_name:even_mask_name)))
193 PUT_ERR("ERROR in yac_cget_ext_couple_config_src_mask_names");
194 char const * tgt_mask_name;
195 yac_cget_ext_couple_config_tgt_mask_name(ext_couple_config, &tgt_mask_name);
196 if (((target_mask_type == 2) && (tgt_mask_name != NULL)) ||
197 ((target_mask_type != 2) &&
198 strcmp(tgt_mask_name, target_mask_type?odd_mask_name:even_mask_name)))
199 PUT_ERR("ERROR in yac_cget_ext_couple_config_tgt_mask_name");
200
201 char field_name[128];
202 sprintf(
203 field_name, "src2tgt_manual_%s_field_mask_%s_src_mask_%s_tgt_mask",
204 with_field_mask?"with":"without",
205 mask_types[source_mask_type],
206 mask_types[target_mask_type]);
208 src_comp_name, src_grid_name, field_name,
209 tgt_comp_name, tgt_grid_name, field_name,
211 interp_stack_config, 0, 0, ext_couple_config);
212 }
213 }
214 }
215 char const * dummy_mask_name = "dummy_src_mask";
217 ext_couple_config, 1, &dummy_mask_name);
218 yac_cfree_ext_couple_config(ext_couple_config);
219 yac_cfree_interp_stack_config(interp_stack_config);
220
221 yac_cenddef ( );
222
223 double send_field[COLLECTION_SIZE][NUM_POINTSETS][NUM_POINTS] =
224 {{{ 0, 1, 2, 3, 4, 5, 6, 7, 8}},
225 {{10,11,12,13,14,15,16,17,18}},
226 {{20,21,22,23,24,25,26,27,28}}};
227
228 // do time steps
229 for (int t = 0; t < 4; ++t) {
230
231 if (!is_target) {
232
233 for (int config_from_file = 0; config_from_file < 2; ++config_from_file) {
234 for (int with_field_mask = 0; with_field_mask < 2; ++with_field_mask) {
235 for (int source_mask_type = 0; source_mask_type < 3; ++source_mask_type) {
236 for (int target_mask_type = 0; target_mask_type < 3; ++target_mask_type) {
237
238 int info, ierror;
239 yac_cput_(
240 field_ids[config_from_file]
241 [with_field_mask]
242 [source_mask_type]
243 [target_mask_type],
244 COLLECTION_SIZE, &send_field[0][0][0], &info, &ierror);
245 yac_cwait(
246 field_ids[config_from_file]
247 [with_field_mask]
248 [source_mask_type]
249 [target_mask_type]);
250 if (info != YAC_ACTION_COUPLING)
251 PUT_ERR("error in yac_cput_: wrong info");
252 if (ierror) PUT_ERR("error in yac_cput_: wrong ierror");
253 }
254 }
255 }
256 }
257 } else {
258
259 for (int config_from_file = 0; config_from_file < 2; ++config_from_file) {
260 for (int with_field_mask = 0; with_field_mask < 2; ++with_field_mask) {
261 for (int source_mask_type = 0; source_mask_type < 3; ++source_mask_type) {
262 for (int target_mask_type = 0; target_mask_type < 3; ++target_mask_type) {
263
264 // initialise recv_field
265 double recv_field[COLLECTION_SIZE][NUM_POINTS];
266 for (int j = 0; j < COLLECTION_SIZE; ++j)
267 for (int k = 0; k < NUM_POINTS; ++k)
268 recv_field[j][k] = -1;
269
270 int info, ierror;
271 if (t & 1) {
272 yac_cget_(
273 field_ids[config_from_file]
274 [with_field_mask]
275 [source_mask_type]
276 [target_mask_type],
277 COLLECTION_SIZE, &recv_field[0][0], &info, &ierror);
278 } else {
280 field_ids[config_from_file]
281 [with_field_mask]
282 [source_mask_type]
283 [target_mask_type],
284 COLLECTION_SIZE, &recv_field[0][0], &info, &ierror);
285 yac_cwait(field_ids[config_from_file]
286 [with_field_mask]
287 [source_mask_type]
288 [target_mask_type]);
289 }
290
291 if (info != YAC_ACTION_COUPLING)
292 PUT_ERR("error in yac_cget_: wrong info");
293 if (ierror) PUT_ERR("error in yac_cget_: wrong ierror");
294
295 for (int j = 0; j < COLLECTION_SIZE; ++j)
296 for (int k = 0; k < NUM_POINTS; ++k)
297 if (utest_check_tgt[target_mask_type](
298 recv_field[j][k], k, j, with_field_mask,
299 utest_check_src[source_mask_type]))
300 PUT_ERR("error in yac_cget_: wrong recv_field");
301 }
302 }
303 }
304 }
305 }
306 }
307
308 yac_cfinalize ();
309
310 return TEST_EXIT_CODE;
311}
312
313static int utest_check_src_even_mask(
314 double value, int idx, int collection_idx, int with_field_mask) {
315
316 UNUSED(idx);
317 UNUSED(collection_idx);
318 UNUSED(with_field_mask);
319 return (int)value & 1;
320}
321
322static int utest_check_src_odd_mask(
323 double value, int idx, int collection_idx, int with_field_mask) {
324
325 UNUSED(idx);
326 UNUSED(collection_idx);
327 UNUSED(with_field_mask);
328 return !((int)value & 1);
329}
330
331static int utest_check_src_no_mask(
332 double value, int idx, int collection_idx, int with_field_mask) {
333
334 return
335 with_field_mask?
336 ((int)value != (10 * collection_idx)):
337 ((int)value != idx + 10 * collection_idx);
338}
339
340static int utest_check_tgt_even_mask(
341 double value, int idx, int collection_idx, int with_field_mask,
342 func_check_src_ptr utest_check_src) {
343
344 return
345 (idx&1)?
346 (value != -1.0):
347 utest_check_src(value, idx, collection_idx, with_field_mask);
348}
349
350static int utest_check_tgt_odd_mask(
351 double value, int idx, int collection_idx, int with_field_mask,
352 func_check_src_ptr utest_check_src) {
353
354 return
355 (idx&1)?
356 utest_check_src(value, idx, collection_idx, with_field_mask):(value != -1.0);
357}
358
359static int utest_check_tgt_no_mask(
360 double value, int idx, int collection_idx, int with_field_mask,
361 func_check_src_ptr utest_check_src) {
362
363 return
364 (with_field_mask && idx)?
365 (value != -1.0):
366 utest_check_src(value, idx, collection_idx, with_field_mask);
367}
#define UNUSED(x)
Definition core.h:72
struct @34::@35 value
@ COLLECTION_SIZE
static char const * mask_types[3]
#define YAC_RAD
int(* func_check_tgt_ptr)(double value, int idx, int collection_idx, int with_field_mask, func_check_src_ptr utest_check_src)
int(* func_check_src_ptr)(double value, int idx, int collection_idx, int with_field_mask)
char * yaml_filename
int point_id
char const src_grid_name[]
char const tgt_grid_name[]
#define TEST_EXIT_CODE
Definition tests.h:14
#define PUT_ERR(string)
Definition tests.h:10
int info
int grid_id
int ierror
int comp_id
void yac_cenddef(void)
Definition yac.c:4400
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:1323
void yac_cget_(int const field_id, int const collection_size, double *recv_field, int *info, int *ierr)
Definition yac.c:2741
void yac_cfree_ext_couple_config(int ext_couple_config_id)
Definition yac.c:1570
void yac_cset_ext_couple_config_tgt_mask_name(int ext_couple_config_id, char const *tgt_mask_name)
Definition yac.c:1743
void yac_cinit(void)
Definition yac.c:510
void yac_cfinalize()
Finalises YAC.
Definition yac.c:740
void yac_cset_ext_couple_config_src_mask_names(int ext_couple_config_id, size_t num_src_mask_names, char const *const *src_mask_names)
Definition yac.c:1718
void yac_cget_ext_couple_config(int *ext_couple_config_id)
Definition yac.c:1549
int const YAC_LOCATION_CORNER
Definition yac.c:35
void yac_cdef_couple_custom(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, int ext_couple_config_id)
Definition yac.c:1919
void yac_cget_ext_couple_config_tgt_mask_name(int ext_couple_config_id, char const **tgt_mask_name)
Definition yac.c:1750
void yac_cget_ext_couple_config_src_mask_names(int ext_couple_config_id, size_t *num_src_mask_names, char const *const **src_mask_names)
Definition yac.c:1726
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:4831
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:1103
int const YAC_TIME_UNIT_SECOND
Definition yac.c:59
void yac_cread_config_yaml(const char *yaml_filename)
Definition yac.c:595
void yac_cdef_calendar(int calendar)
Definition yac.c:769
void yac_cwait(int field_id)
Definition yac.c:3235
void yac_cput_(int const field_id, int const collection_size, double *send_field, int *info, int *ierr)
Definition yac.c:3063
int const YAC_PROLEPTIC_GREGORIAN
Definition yac.c:68
void yac_cget_async_(int const field_id, int const collection_size, double *recv_field, int *info, int *ierr)
Definition yac.c:2750
int const YAC_ACTION_COUPLING
data exchange
Definition yac.c:44
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:1295
void yac_cdef_mask_named(int const grid_id, int const nbr_points, int const located, int const *is_valid, char const *name, int *mask_id)
Definition yac.c:1274
void yac_cfree_interp_stack_config(int interp_stack_config_id)
Definition yac.c:5151
int const YAC_NNN_AVG
Definition yac.c:79
void yac_cadd_interp_stack_config_nnn(int interp_stack_config_id, int type, size_t n, double max_search_distance, double scale)
Definition yac.c:5208
void yac_cget_interp_stack_config(int *interp_stack_config_id)
Definition yac.c:5137
void yac_cdef_comp(char const *comp_name, int *comp_id)
Definition yac.c:1013
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:1396
int const YAC_REDUCTION_TIME_NONE
Definition yac.c:52