YAC 3.21.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 rotated grid for reg2d_rot point definitions
108 int grid_rot_id;
109 char const * src_grid_rot_name = "source_grid_rot";
110 char const * tgt_grid_rot_name = "target_grid_rot";
112 (is_target)?tgt_grid_rot_name:src_grid_rot_name, (int[2]){3,3}, (int[2]){0,0},
113 (double[]){0*YAC_RAD,1*YAC_RAD,2*YAC_RAD},
114 (double[]){0*YAC_RAD,1*YAC_RAD,2*YAC_RAD},
115 45.0 * YAC_RAD, 45.0 * YAC_RAD, &grid_rot_id);
116
117 // define unnamed points at the vertices of the grid
118 int point_id;
120 grid_id, (int[2]){3,3}, YAC_LOCATION_CORNER,
121 (double[]){0*YAC_RAD,1*YAC_RAD,2*YAC_RAD},
122 (double[]){0*YAC_RAD,1*YAC_RAD,2*YAC_RAD}, &point_id);
123
124 // define named points (all point-definition variants)
125 int point_id_named_reg2d, point_id_named_curve2d;
126 int point_id_named_unstruct, point_id_named_reg2d_rot;
128 grid_id, (int[2]){3,3}, YAC_LOCATION_CORNER,
129 (double[]){0*YAC_RAD,1*YAC_RAD,2*YAC_RAD},
130 (double[]){0*YAC_RAD,1*YAC_RAD,2*YAC_RAD},
131 "pointset_reg2d", &point_id_named_reg2d);
132 if (yac_cget_points_size(point_id_named_reg2d) != NUM_POINTS)
133 PUT_ERR("ERROR in yac_cdef_points_reg2d_named");
134
136 grid_id, (int[2]){3,3}, YAC_LOCATION_CORNER,
137 (double[]){0*YAC_RAD,1*YAC_RAD,2*YAC_RAD,
138 0*YAC_RAD,1*YAC_RAD,2*YAC_RAD,
139 0*YAC_RAD,1*YAC_RAD,2*YAC_RAD},
140 (double[]){0*YAC_RAD,0*YAC_RAD,0*YAC_RAD,
141 1*YAC_RAD,1*YAC_RAD,1*YAC_RAD,
142 2*YAC_RAD,2*YAC_RAD,2*YAC_RAD},
143 "pointset_curve2d", &point_id_named_curve2d);
144 if (yac_cget_points_size(point_id_named_curve2d) != NUM_POINTS)
145 PUT_ERR("ERROR in yac_cdef_points_curve2d_named");
146
149 (double[]){0*YAC_RAD,1*YAC_RAD,2*YAC_RAD,
150 0*YAC_RAD,1*YAC_RAD,2*YAC_RAD,
151 0*YAC_RAD,1*YAC_RAD,2*YAC_RAD},
152 (double[]){0*YAC_RAD,0*YAC_RAD,0*YAC_RAD,
153 1*YAC_RAD,1*YAC_RAD,1*YAC_RAD,
154 2*YAC_RAD,2*YAC_RAD,2*YAC_RAD},
155 "pointset_unstruct", &point_id_named_unstruct);
156 if (yac_cget_points_size(point_id_named_unstruct) != NUM_POINTS)
157 PUT_ERR("ERROR in yac_cdef_points_unstruct_named");
158
160 grid_rot_id, (int[2]){3,3}, YAC_LOCATION_CORNER,
161 (double[]){0*YAC_RAD,1*YAC_RAD,2*YAC_RAD},
162 (double[]){0*YAC_RAD,1*YAC_RAD,2*YAC_RAD},
163 45.0 * YAC_RAD, 45.0 * YAC_RAD,
164 "pointset_reg2d_rot", &point_id_named_reg2d_rot);
165 if (yac_cget_points_size(point_id_named_reg2d_rot) != NUM_POINTS)
166 PUT_ERR("ERROR in yac_cdef_points_reg2d_rot_named");
167
168 // define masks for vertices
169 int default_mask_id, dummy_mask_id;
172 (int[3*3]){1,0,0, 0,0,0, 0,0,0}, &default_mask_id);
173 char const * even_mask_name = "even_mask";
176 (int[3*3]){1,0,1, 0,1,0, 1,0,1}, even_mask_name, &dummy_mask_id);
177 char const * odd_mask_name = "odd_mask";
180 (int[3*3]){0,1,0, 1,0,1, 0,1,0}, odd_mask_name, &dummy_mask_id);
181
182 // define field
183 int field_ids[2][2][3][3];
184 for (int config_from_file = 0; config_from_file < 2; ++config_from_file) {
185 for (int with_field_mask = 0; with_field_mask < 2; ++with_field_mask) {
186 for (int source_mask_type = 0; source_mask_type < 3; ++source_mask_type) {
187 for (int target_mask_type = 0; target_mask_type < 3; ++target_mask_type) {
188 char field_name[128];
189 sprintf(
190 field_name, "src2tgt_%s_%s_field_mask_%s_src_mask_%s_tgt_mask",
191 config_from_file?"yaml":"manual",
192 with_field_mask?"with":"without",
193 mask_types[source_mask_type],
194 mask_types[target_mask_type]);
195 if (with_field_mask) {
197 field_name, comp_id, &point_id_named_reg2d,
198 &default_mask_id, NUM_POINTSETS,
200 &field_ids[config_from_file]
201 [with_field_mask]
202 [source_mask_type]
203 [target_mask_type]);
204 } else {
206 field_name, comp_id, &point_id_named_reg2d, NUM_POINTSETS,
208 &field_ids[config_from_file]
209 [with_field_mask]
210 [source_mask_type]
211 [target_mask_type]);
212 }
213 }
214 }
215 }
216 }
217
218 // define manual couples
219 int interp_stack_config;
220 yac_cget_interp_stack_config(&interp_stack_config);
222 interp_stack_config, YAC_NNN_AVG, 1, 0.0, 0.0);
223 int ext_couple_config;
224 yac_cget_ext_couple_config(&ext_couple_config);
225 for (int with_field_mask = 0; with_field_mask < 2; ++with_field_mask) {
226 for (int source_mask_type = 0; source_mask_type < 3; ++source_mask_type) {
227 for (int target_mask_type = 0; target_mask_type < 3; ++target_mask_type) {
228 if (source_mask_type != 2)
230 ext_couple_config, 1, source_mask_type?&odd_mask_name:&even_mask_name);
231 else
232 yac_cset_ext_couple_config_src_mask_names(ext_couple_config, 0, NULL);
233 if (target_mask_type != 2)
235 ext_couple_config, target_mask_type?odd_mask_name:even_mask_name);
236 else
237 yac_cset_ext_couple_config_tgt_mask_name(ext_couple_config, NULL);
238
239 size_t num_src_maks_names;
240 char const * const * src_mask_names;
242 ext_couple_config, &num_src_maks_names, &src_mask_names);
243 if ((num_src_maks_names != (source_mask_type != 2)) ||
244 ((source_mask_type == 2) && (src_mask_names != NULL)) ||
245 ((source_mask_type != 2) &&
246 strcmp(src_mask_names[0],
247 source_mask_type?odd_mask_name:even_mask_name)))
248 PUT_ERR("ERROR in yac_cget_ext_couple_config_src_mask_names");
249 char const * tgt_mask_name;
250 yac_cget_ext_couple_config_tgt_mask_name(ext_couple_config, &tgt_mask_name);
251 if (((target_mask_type == 2) && (tgt_mask_name != NULL)) ||
252 ((target_mask_type != 2) &&
253 strcmp(tgt_mask_name, target_mask_type?odd_mask_name:even_mask_name)))
254 PUT_ERR("ERROR in yac_cget_ext_couple_config_tgt_mask_name");
255
256 char field_name[128];
257 sprintf(
258 field_name, "src2tgt_manual_%s_field_mask_%s_src_mask_%s_tgt_mask",
259 with_field_mask?"with":"without",
260 mask_types[source_mask_type],
261 mask_types[target_mask_type]);
263 src_comp_name, src_grid_name, field_name,
264 tgt_comp_name, tgt_grid_name, field_name,
266 interp_stack_config, 0, 0, ext_couple_config);
267 }
268 }
269 }
270 char const * dummy_mask_name = "dummy_src_mask";
272 ext_couple_config, 1, &dummy_mask_name);
273 yac_cfree_ext_couple_config(ext_couple_config);
274 yac_cfree_interp_stack_config(interp_stack_config);
275
276 yac_cenddef ( );
277
278 double send_field[COLLECTION_SIZE][NUM_POINTSETS][NUM_POINTS] =
279 {{{ 0, 1, 2, 3, 4, 5, 6, 7, 8}},
280 {{10,11,12,13,14,15,16,17,18}},
281 {{20,21,22,23,24,25,26,27,28}}};
282
283 // do time steps
284 for (int t = 0; t < 4; ++t) {
285
286 if (!is_target) {
287
288 for (int config_from_file = 0; config_from_file < 2; ++config_from_file) {
289 for (int with_field_mask = 0; with_field_mask < 2; ++with_field_mask) {
290 for (int source_mask_type = 0; source_mask_type < 3; ++source_mask_type) {
291 for (int target_mask_type = 0; target_mask_type < 3; ++target_mask_type) {
292
293 int info, ierror;
294 yac_cput_(
295 field_ids[config_from_file]
296 [with_field_mask]
297 [source_mask_type]
298 [target_mask_type],
299 COLLECTION_SIZE, &send_field[0][0][0], &info, &ierror);
300 yac_cwait(
301 field_ids[config_from_file]
302 [with_field_mask]
303 [source_mask_type]
304 [target_mask_type]);
306 PUT_ERR("error in yac_cput_: wrong info");
307 if (ierror) PUT_ERR("error in yac_cput_: wrong ierror");
308 }
309 }
310 }
311 }
312 } else {
313
314 for (int config_from_file = 0; config_from_file < 2; ++config_from_file) {
315 for (int with_field_mask = 0; with_field_mask < 2; ++with_field_mask) {
316 for (int source_mask_type = 0; source_mask_type < 3; ++source_mask_type) {
317 for (int target_mask_type = 0; target_mask_type < 3; ++target_mask_type) {
318
319 // initialise recv_field
320 double recv_field[COLLECTION_SIZE][NUM_POINTS];
321 for (int j = 0; j < COLLECTION_SIZE; ++j)
322 for (int k = 0; k < NUM_POINTS; ++k)
323 recv_field[j][k] = -1;
324
325 int info, ierror;
326 if (t & 1) {
327 yac_cget_(
328 field_ids[config_from_file]
329 [with_field_mask]
330 [source_mask_type]
331 [target_mask_type],
332 COLLECTION_SIZE, &recv_field[0][0], &info, &ierror);
333 } else {
335 field_ids[config_from_file]
336 [with_field_mask]
337 [source_mask_type]
338 [target_mask_type],
339 COLLECTION_SIZE, &recv_field[0][0], &info, &ierror);
340 yac_cwait(field_ids[config_from_file]
341 [with_field_mask]
342 [source_mask_type]
343 [target_mask_type]);
344 }
345
347 PUT_ERR("error in yac_cget_: wrong info");
348 if (ierror) PUT_ERR("error in yac_cget_: wrong ierror");
349
350 for (int j = 0; j < COLLECTION_SIZE; ++j)
351 for (int k = 0; k < NUM_POINTS; ++k)
352 if (utest_check_tgt[target_mask_type](
353 recv_field[j][k], k, j, with_field_mask,
354 utest_check_src[source_mask_type]))
355 PUT_ERR("error in yac_cget_: wrong recv_field");
356 }
357 }
358 }
359 }
360 }
361 }
362
363 yac_cfinalize ();
364
365 return TEST_EXIT_CODE;
366}
367
368static int utest_check_src_even_mask(
369 double value, int idx, int collection_idx, int with_field_mask) {
370
371 UNUSED(idx);
372 UNUSED(collection_idx);
373 UNUSED(with_field_mask);
374 return (int)value & 1;
375}
376
377static int utest_check_src_odd_mask(
378 double value, int idx, int collection_idx, int with_field_mask) {
379
380 UNUSED(idx);
381 UNUSED(collection_idx);
382 UNUSED(with_field_mask);
383 return !((int)value & 1);
384}
385
386static int utest_check_src_no_mask(
387 double value, int idx, int collection_idx, int with_field_mask) {
388
389 return
390 with_field_mask?
391 ((int)value != (10 * collection_idx)):
392 ((int)value != idx + 10 * collection_idx);
393}
394
395static int utest_check_tgt_even_mask(
396 double value, int idx, int collection_idx, int with_field_mask,
397 func_check_src_ptr utest_check_src) {
398
399 return
400 (idx&1)?
401 (value != -1.0):
402 utest_check_src(value, idx, collection_idx, with_field_mask);
403}
404
405static int utest_check_tgt_odd_mask(
406 double value, int idx, int collection_idx, int with_field_mask,
407 func_check_src_ptr utest_check_src) {
408
409 return
410 (idx&1)?
411 utest_check_src(value, idx, collection_idx, with_field_mask):(value != -1.0);
412}
413
414static int utest_check_tgt_no_mask(
415 double value, int idx, int collection_idx, int with_field_mask,
416 func_check_src_ptr utest_check_src) {
417
418 return
419 (with_field_mask && idx)?
420 (value != -1.0):
421 utest_check_src(value, idx, collection_idx, with_field_mask);
422}
#define UNUSED(x)
Definition core.h:72
struct @23::@24 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:15
#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:5006
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:1854
void yac_cget_(int const field_id, int const collection_size, double *recv_field, int *info, int *ierr)
Definition yac.c:3280
void yac_cfree_ext_couple_config(int ext_couple_config_id)
Definition yac.c:2100
void yac_cset_ext_couple_config_tgt_mask_name(int ext_couple_config_id, char const *tgt_mask_name)
Definition yac.c:2270
void yac_cdef_points_curve2d_named(int const grid_id, int const *nbr_points, int const located, double const *x_points, double const *y_points, char const *name, int *point_id)
Definition yac.c:1553
void yac_cdef_points_reg2d_rot_named(int const grid_id, int const *nbr_points, int const located, double const *x_points, double const *y_points, double x_north_pole, double y_north_pole, char const *name, int *point_id)
Definition yac.c:1652
void yac_cdef_grid_reg2d_rot(const char *grid_name, int nbr_vertices[2], int cyclic[2], double *x_vertices, double *y_vertices, double x_north_pole, double y_north_pole, int *grid_id)
Definition yac.c:5645
void yac_cinit(void)
Definition yac.c:737
void yac_cfinalize()
Finalises YAC.
Definition yac.c:1007
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:2245
void yac_cdef_points_unstruct_named(int const grid_id, int const nbr_points, int const located, double const *x_points, double const *y_points, char const *name, int *point_id)
Definition yac.c:1607
void yac_cget_ext_couple_config(int *ext_couple_config_id)
Definition yac.c:2079
void yac_cdef_points_reg2d_named(int const grid_id, int const *nbr_points, int const located, double const *x_points, double const *y_points, char const *name, int *point_id)
Definition yac.c:1499
int const YAC_LOCATION_CORNER
Definition yac.c:39
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:2443
void yac_cget_ext_couple_config_tgt_mask_name(int ext_couple_config_id, char const **tgt_mask_name)
Definition yac.c:2277
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:2253
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:5511
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:1540
int const YAC_TIME_UNIT_SECOND
Definition yac.c:64
void yac_cread_config_yaml(const char *yaml_filename)
Definition yac.c:817
void yac_cdef_calendar(int calendar)
Definition yac.c:1036
void yac_cwait(int field_id)
Definition yac.c:3773
void yac_cput_(int const field_id, int const collection_size, double *send_field, int *info, int *ierr)
Definition yac.c:3601
int const YAC_PROLEPTIC_GREGORIAN
Definition yac.c:73
void yac_cget_async_(int const field_id, int const collection_size, double *recv_field, int *info, int *ierr)
Definition yac.c:3289
int const YAC_ACTION_COUPLING
data exchange
Definition yac.c:49
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:1824
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:1801
void yac_cfree_interp_stack_config(int interp_stack_config_id)
Definition yac.c:5834
int const YAC_NNN_AVG
Definition yac.c:84
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:5890
void yac_cget_interp_stack_config(int *interp_stack_config_id)
Definition yac.c:5818
void yac_cdef_comp(char const *comp_name, int *comp_id)
Definition yac.c:1345
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:1929
size_t yac_cget_points_size(int points_id)
Definition yac.c:5805
int const YAC_REDUCTION_TIME_NONE
Definition yac.c:57