YAC 3.18.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_dummy_coupling7_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#include <stdlib.h>
7#include <stdio.h>
8#include <math.h>
9#include <yaxt.h>
10#include "tests.h"
11#include "yac.h"
12
18#define YAC_RAD (0.01745329251994329576923690768489) // M_PI / 180
19
20static void utest_run_comp_a(int with_field_mask, int exchange_type);
21static void utest_run_comp_b(int with_field_mask, int exchange_type);
22
23int main (void) {
24
25 MPI_Init(NULL, NULL);
26 xt_initialize(MPI_COMM_WORLD);
27
28 int rank;
29 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
30
31 for (int exchange_type = 0; exchange_type < 2; ++exchange_type) {
32 for (int with_field_mask = 0; with_field_mask < 2; ++with_field_mask) {
33 switch(rank) {
34 case(0): {
35 MPI_Comm yac_comm;
36 const char* group_name = yac_cget_mpi_handshake_group_name();
37 yac_cmpi_handshake(MPI_COMM_WORLD, 1, &group_name, &yac_comm);
38 yac_cinit_comm_dummy(yac_comm);
39 MPI_Comm_free(&yac_comm);
40 break;
41 }
42 case(1):
43 utest_run_comp_a(with_field_mask, exchange_type);
44 break;
45 case(2):
46 utest_run_comp_b(with_field_mask, exchange_type);
47 break;
48 default:
49 PUT_ERR("wrong number of processes (has to be 3)");
50 break;
51 }
52 }
53 }
54
55 xt_finalize();
56 MPI_Finalize();
57 return TEST_EXIT_CODE;
58}
59
60static void utest_run_comp_a(int with_field_mask, int exchange_type) {
61
62 // initialise YAC default instance
63 yac_cinit();
65 yac_cdef_datetime("2000-01-01T00:00:00", "2000-01-01T00:00:12");
66
67 // define local component
68 int comp_id;
69 yac_cdef_comp("comp_A", &comp_id);
70
71 // get communicator that contains both components
72 MPI_Comm pair_comm;
73 yac_cget_comps_comm((char const*[]){"comp_A", "comp_B"}, 2, &pair_comm);
74
75 // check the pair_comm
76 {
77 char * sendbuf = "A";
78 char recvbuf[2];
79 MPI_Allgather(sendbuf, 1, MPI_CHAR, recvbuf, 1, MPI_CHAR, pair_comm);
80
81 if ((recvbuf[0] != 'A') || (recvbuf[1] != 'B'))
82 PUT_ERR("ERROR in yac_cget_comps_instance");
83 }
84 MPI_Comm_free(&pair_comm);
85
86 int nbr_vertices = 4;
87 int nbr_cells = 2;
88 int nbr_vertices_per_cell[2] = {3,3};
89 double x_vertices[4] =
90 {0.0 * YAC_RAD, -1.0 * YAC_RAD, 1.0 * YAC_RAD, 0.0 * YAC_RAD};
91 double y_vertices[4] =
92 {1.0 * YAC_RAD, 0.0 * YAC_RAD, 0.0 * YAC_RAD, -1.0 * YAC_RAD};
93 int cell_to_vertex[6] = {0,1,2, 1,3,2};
94
95 /* define local grid
96
97 0
98 / \
99 / o \
100 / \
101 1-------2 Eq.
102 \ /
103 \ o /
104 \ /
105 3
106
107 */
108 int grid_id;
110 "grid_A", nbr_vertices, nbr_cells, nbr_vertices_per_cell,
111 x_vertices, y_vertices, cell_to_vertex, &grid_id);
112
113 double x_cells[2] = {0.0 * YAC_RAD, 0.0 * YAC_RAD};
114 double y_cells[2] = {0.5 * YAC_RAD, -0.5 * YAC_RAD};
115
116 // center points in cells (needed e.g. for nearest neighbour)
119 grid_id, nbr_cells, YAC_LOCATION_CELL, x_cells, y_cells, &cell_point_id);
120 // vertex points
122 grid_id, nbr_vertices, YAC_LOCATION_CORNER, x_vertices, y_vertices,
123 &vertex_point_id);
124
125 // masks
126 int cell_mask_id, vertex_mask_id;
127 int cell_mask[2] = {1, 0};
128 int vertex_mask[4] = {1, 1, 1, 1};
130 grid_id, nbr_cells, YAC_LOCATION_CELL, cell_mask, &cell_mask_id);
132 grid_id, nbr_vertices, YAC_LOCATION_CORNER, vertex_mask, &vertex_mask_id);
133
134 // define field
135 int field_id, multi_field_id, dummy_field_id;
136 double frac_mask_fallback_value = -1.0;
137 double frac_mask_fallback_value_multi = strtod("inf", NULL);
138 int collection_size = 1;
139 if (with_field_mask) {
141 "A_to_B_src", comp_id, &cell_point_id, &cell_mask_id, 1,
143 } else {
145 "A_to_B_src", comp_id, &cell_point_id, 1,
147 }
149 "comp_A", "grid_A", "A_to_B_src", frac_mask_fallback_value);
150 int multi_field_point_ids[2], multi_field_mask_ids[2];
151 multi_field_point_ids[0] = cell_point_id;
152 multi_field_point_ids[1] = vertex_point_id;
153 multi_field_mask_ids[0] = cell_mask_id;
154 multi_field_mask_ids[1] = vertex_mask_id;
155 if (with_field_mask) {
157 "A_to_B_multi", comp_id, multi_field_point_ids, multi_field_mask_ids,
158 2, collection_size, "1", YAC_TIME_UNIT_SECOND, &multi_field_id);
159 } else {
161 "A_to_B_multi", comp_id, multi_field_point_ids,
162 2, collection_size, "1", YAC_TIME_UNIT_SECOND, &multi_field_id);
163 }
165 "comp_A", "grid_A", "A_to_B_multi", frac_mask_fallback_value_multi);
167 "dummy_field", comp_id, &cell_point_id, 1,
168 collection_size, "1", YAC_TIME_UNIT_SECOND, &dummy_field_id);
169
170 // generate coupling
171 int ierror;
172 yac_cenddef();
173
175 "comp_A", "grid_A", "A_to_B_multi") != frac_mask_fallback_value_multi)
176 PUT_ERR("ERROR in yac_cget_field_frac_mask_fallback_value");
177
178 // move data from comp_A to comp_B
179 {
180 double send_field_data[2] = {3.0, 4.0};
181 double * send_field_[1] = {&send_field_data[0]};
182 double ** send_field[1] = {&send_field_[0]};
183 double send_frac_mask_data[2] = {1.0, 1.0};
184 double * send_frac_mask_[1] = {&send_frac_mask_data[0]};
185 double ** send_frac_mask[1] = {&send_frac_mask_[0]};
186 int info;
187 if (exchange_type) {
189 field_id, collection_size, send_field, send_frac_mask, &info, &ierror);
190 yac_cget(dummy_field_id, collection_size, NULL, &info, &ierror);
191 } else {
193 field_id, dummy_field_id, collection_size,
194 send_field, send_frac_mask, NULL, &info, &info, &ierror);
195 }
196 }
197 {
198 double send_cell_field_data[2] = {3.0, 4.0};
199 double send_vertex_field_data[4] = {3.0, 4.0, 5.0, 6.0};
200 double * send_field_[2] =
201 {&send_cell_field_data[0], &send_vertex_field_data[0]};
202 double ** send_field[1] = {&send_field_[0]};
203 double send_cell_frac_mask_data[2] = {1.0, 1.0};
204 double send_vertex_frac_mask_data[4] = {1.0, 1.0, 1.0, 1.0};
205 double * send_frac_mask_[2] =
206 {&send_cell_frac_mask_data[0], &send_vertex_frac_mask_data[0]};
207 double ** send_frac_mask[1] = {&send_frac_mask_[0]};
208 int info;
210 multi_field_id, collection_size, send_field, send_frac_mask,
211 &info, &ierror);
212 }
213
214 // finalise YAC default instance
216}
217
218static void utest_run_comp_b(int with_field_mask, int exchange_type) {
219
220 double frac_mask_fallback_value = strtod("inf", NULL);
221
222 // initialise YAC default instance
227 default_instance_id, "2000-01-01T00:00:00", "2000-01-01T00:00:12");
228
229 // define local component
230 int comp_id;
232
233 // get communicator that contains both components
234 MPI_Comm pair_comm;
236 default_instance_id, (char const *[]){"comp_B", "comp_A"}, 2, &pair_comm);
237
238 // check the pair_comm
239 {
240 char * sendbuf = "B";
241 char recvbuf[2];
242 MPI_Allgather(sendbuf, 1, MPI_CHAR, recvbuf, 1, MPI_CHAR, pair_comm);
243 if ((recvbuf[0] != 'A') || (recvbuf[1] != 'B'))
244 PUT_ERR("ERROR in yac_cget_comps_comm");
245 }
246 MPI_Comm_free(&pair_comm);
247
248 int nbr_vertices[2] = {2,3};
249 int cyclic[2] = {0,0};
250 double x_vertices[2] = {-0.5 * YAC_RAD, 0.5 * YAC_RAD};
251 double y_vertices[3] = {-1.0 * YAC_RAD, 0.0 * YAC_RAD, 1.0 * YAC_RAD};
252
253 /* define local grid
254
255 4-------5
256 | |
257 | o |
258 | |
259 2-------3
260 | |
261 | o |
262 | |
263 0-------1
264
265 */
266 int grid_id_B;
268 "grid_B", nbr_vertices, cyclic, x_vertices, y_vertices, &grid_id_B);
269
270 int nbr_cells[2] = {1,2};
271 double x_cells[1] = {0.0 * YAC_RAD};
272 double y_cells[2] = {-0.5 * YAC_RAD, 0.5 * YAC_RAD};
273
274 // center points in cells (needed e.g. for nearest neighbour)
275 int point_id_B;
277 grid_id_B, nbr_cells, YAC_LOCATION_CELL, x_cells, y_cells, &point_id_B);
278
279 // define field
280 int field_id, multi_field_id, dummy_field_id;
281 int collection_size = 1;
283 "A_to_B_tgt", comp_id, &point_id_B, 1, collection_size, "1",
286 "A_to_B_multi", comp_id, &point_id_B, 1, collection_size, "1",
287 YAC_TIME_UNIT_SECOND, &multi_field_id);
289 "dummy_field", comp_id, &point_id_B, 1, collection_size, "1",
290 YAC_TIME_UNIT_SECOND, &dummy_field_id);
291
292 // define interpolation stacks
293 int interp_stack_nnn, interp_stack_fixed;
295 "- nnn:\n"
296 " n: 1", &interp_stack_nnn);
298 "[{\"fixed\": {\"user_value\": -2}}]", &interp_stack_fixed);
299
300 // define couplings
303 "comp_A", "grid_A", "A_to_B_src",
304 "comp_B", "grid_B", "A_to_B_tgt",
306 interp_stack_nnn, 0, 0);
309 "comp_A", "grid_A", "A_to_B_multi",
310 "comp_B", "grid_B", "A_to_B_multi",
312 interp_stack_fixed, 0, 0);
313 yac_cfree_interp_stack_config(interp_stack_nnn);
314 yac_cfree_interp_stack_config(interp_stack_fixed);
315
316 // generate coupling
317 int ierror;
319
321 default_instance_id, "comp_A", "grid_A", "A_to_B_multi") !=
322 frac_mask_fallback_value)
323 PUT_ERR("ERROR in yac_cget_field_frac_mask_fallback_value_instance");
324
325 // get data from comp_A
326 double recv_field_data[2] = {-1.0, -1.0};
327 double * recv_field[1] = {&recv_field_data[0]};
328 int info;
329 yac_cget(multi_field_id, collection_size, recv_field, &info, &ierror);
330 if ((recv_field_data[0] != -2.0) || (recv_field_data[1] != -2.0))
331 PUT_ERR("ERROR: wrong results from multi field exchange");
332 if (exchange_type) {
333 yac_cput(
334 dummy_field_id, collection_size, NULL, &info, &ierror);
336 dummy_field_id, field_id, collection_size, NULL, recv_field,
337 &info, &info, &ierror);
338 } else {
339 yac_cget(field_id, collection_size, recv_field, &info, &ierror);
340 }
341
342 { // run internal instance
343
344 MPI_Comm internal_comm;
345 yac_cget_comp_comm(comp_id, &internal_comm);
346
347 // initialise internal YAC instance
348 int internal_instance_id;
349 yac_cinit_comm_instance(internal_comm, &internal_instance_id);
351 internal_instance_id, "2000-01-01T00:00:00", "2000-01-01T00:00:12");
352
353 // define internal component
354 int internal_comp_id;
355 yac_cdef_comp_instance(internal_instance_id, "comp_B", &internal_comp_id);
356
357 int nbr_vertices = 2*3;
358 double x_vertices[2*3] = {-0.4 * YAC_RAD, 0.4 * YAC_RAD,
359 -0.4 * YAC_RAD, 0.4 * YAC_RAD,
360 -0.4 * YAC_RAD, 0.4 * YAC_RAD};
361 double y_vertices[2*3] = {-0.7 * YAC_RAD, -0.7 * YAC_RAD,
362 0.0 * YAC_RAD, 0.0 * YAC_RAD,
363 0.8 * YAC_RAD, 0.8 * YAC_RAD};
364
365 /* define local grid
366
367 4-------5
368 | |
369 | |
370 | |
371 2-------3
372 | |
373 | |
374 | |
375 0-------1
376
377 */
378 int grid_id_C;
380 "grid_C", nbr_vertices, x_vertices, y_vertices, &grid_id_C);
381
382 // vertices points (needed e.g. for nearest neighbour)
383 int point_id_C;
385 grid_id_C, nbr_vertices, YAC_LOCATION_CORNER,
386 x_vertices, y_vertices, &point_id_C);
387
388 // define field
389 int field_id_src, field_id_tgt;
391 "B_to_C", internal_comp_id, &point_id_B, 1,
392 1, "1", YAC_TIME_UNIT_SECOND, &field_id_src);
394 internal_instance_id, "comp_B", "grid_B", "B_to_C",
395 frac_mask_fallback_value);
397 "B_to_C", internal_comp_id, &point_id_C, 1, 1, "1",
398 YAC_TIME_UNIT_SECOND, &field_id_tgt);
399
400 // define interpolation stack
401 int interp_stack_nnn;
402 yac_cget_interp_stack_config(&interp_stack_nnn);
404 interp_stack_nnn, YAC_NNN_AVG, 1, 0.0, 0.0);
405
406 // define coupling
407 int ext_couple_config;
408 double const scale_factor = 2.0;
409 double const scale_summand = 0.5;
410 yac_cget_ext_couple_config(&ext_couple_config);
412 ext_couple_config, scale_factor);
414 ext_couple_config, scale_summand);
416 internal_instance_id,
417 "comp_B", "grid_B", "B_to_C",
418 "comp_B", "grid_C", "B_to_C",
420 interp_stack_nnn, 0, 0, ext_couple_config);
421 yac_cfree_ext_couple_config(ext_couple_config);
422 yac_cfree_interp_stack_config(interp_stack_nnn);
423
424 // generate internal coupling
425 int ierror;
426 yac_cenddef_instance(internal_instance_id);
427
428 // move data from grid B to grid C
429 int send_info, recv_info;
430 int collection_size = 1;
431 double * send_field_[1] = {&recv_field_data[0]};
432 double ** send_field[1] = {&send_field_[0]};
433 double send_frac_mask_data[2] = {1.0, 0.0};
434 double * send_frac_mask_[1] = {send_frac_mask_data};
435 double ** send_frac_mask[1] = {&send_frac_mask_[0]};
436 double recv_field_data_C[6] = {-1.0, -1.0, -1.0, -1.0, -1.0, -1.0};
437 double * recv_field[1] = {&recv_field_data_C[0]};
438
439 if (exchange_type)
441 field_id_src, field_id_tgt, collection_size,
442 send_field, send_frac_mask, recv_field,
443 &send_info, &recv_info, &ierror);
444 else
446 field_id_src, field_id_tgt, collection_size,
447 send_field_, send_frac_mask_, recv_field,
448 &send_info, &recv_info, &ierror);
449
450 double ref_recv_field_data_C[2][6] =
451 {{4.0, 4.0, 4.0, 4.0,
452 frac_mask_fallback_value, frac_mask_fallback_value},
453 {3.0, 3.0, 3.0, 3.0,
454 frac_mask_fallback_value, frac_mask_fallback_value}};
455 for (int i = 0; i < 6; ++i)
456 if (recv_field_data_C[i] !=
457 scale_factor * ref_recv_field_data_C[with_field_mask][i] +
458 scale_summand)
459 PUT_ERR("data missmatch");
460
461 yac_ccleanup_instance(internal_instance_id);
462
463 MPI_Comm_free(&internal_comm);
464 }
465
466 // finalise YAC default instance
468}
#define YAC_RAD
int nbr_vertices_per_cell[NBR_CELLS]
int * cell_to_vertex
int * cell_mask
int 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 * field_id
int comp_id
void yac_cinit_comm_dummy(MPI_Comm comm)
Definition yac.c:778
void yac_cput_frac(int const field_id, int const collection_size, double ***const send_field, double ***const send_frac_mask, int *info, int *ierr)
Definition yac.c:4209
void yac_cenddef(void)
Definition yac.c:5091
void yac_cenable_field_frac_mask(const char *comp_name, const char *grid_name, const char *field_name, double frac_mask_fallback_value)
Definition yac.c:2028
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_instance(int yac_instance_id, const char *start_datetime, const char *end_datetime)
Definition yac.c:1046
void yac_cfree_ext_couple_config(int ext_couple_config_id)
Definition yac.c:2159
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_cset_ext_couple_config_scale_factor(int ext_couple_config_id, double scale_factor)
Definition yac.c:2255
void yac_cmpi_handshake(MPI_Comm comm, size_t n, char const **group_names, MPI_Comm *group_comms)
Definition yac.c:669
void yac_cexchange_frac(int const send_field_id, int const recv_field_id, int const collection_size, double ***const send_field, double ***const send_frac_mask, double **recv_field, int *send_info, int *recv_info, int *ierr)
Definition yac.c:4645
void yac_cget_interp_stack_config_from_string_json(char const *interp_stack_config, int *interp_stack_config_id)
Definition yac.c:6725
void yac_cinit(void)
Definition yac.c:768
void yac_cfinalize_instance(int yac_instance_id)
Finalises YAC.
Definition yac.c:1018
void yac_cfinalize()
Finalises YAC.
Definition yac.c:1030
void yac_cenddef_instance(int yac_instance_id)
Definition yac.c:5065
void yac_cget_ext_couple_config(int *ext_couple_config_id)
Definition yac.c:2138
const char * yac_cget_mpi_handshake_group_name(void)
Retrieve the MPI handshake group name used by YAC.
Definition yac.c:1114
int const YAC_LOCATION_CORNER
Definition yac.c:38
void yac_cset_ext_couple_config_scale_summand(int ext_couple_config_id, double scale_summand)
Definition yac.c:2278
void yac_cget_comp_comm(int comp_id, MPI_Comm *comp_comm)
Definition yac.c:1159
void yac_cget(int const field_id, int collection_size, double **recv_field, int *info, int *ierr)
Definition yac.c:3364
void yac_cdef_comp_instance(int yac_instance_id, char const *comp_name, int *comp_id)
Definition yac.c:1377
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_couple_custom_instance(int yac_instance_id, 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:2494
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_cget_comps_comm(const char **comp_names, int num_comps, MPI_Comm *comps_comm)
Definition yac.c:1209
void yac_cinit_comm_instance(MPI_Comm comm, int *yac_instance_id)
Definition yac.c:722
void yac_cenable_field_frac_mask_instance(int yac_instance_id, const char *comp_name, const char *grid_name, const char *field_name, double frac_mask_fallback_value)
Definition yac.c:2016
void yac_cget_interp_stack_config_from_string_yaml(char const *interp_stack_config, int *interp_stack_config_id)
Definition yac.c:6717
int const YAC_TIME_UNIT_SECOND
Definition yac.c:63
void yac_cdef_grid_cloud(const char *grid_name, int nbr_points, double *x_points, double *y_points, int *grid_id)
Definition yac.c:5730
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:5644
void yac_cexchange(int const send_field_id, int const recv_field_id, int const collection_size, double ***const send_field, double **recv_field, int *send_info, int *recv_info, int *ierr)
Definition yac.c:4957
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:1683
void yac_cput(int const field_id, int const collection_size, double ***const send_field, int *info, int *ierr)
Definition yac.c:4332
void yac_cexchange_frac_ptr_(int const send_field_id, int const recv_field_id, int const collection_size, double **send_field, double **send_frac_mask, double **recv_field, int *send_info, int *recv_info, int *ierr)
Definition yac.c:4562
void yac_cdef_calendar(int calendar)
Definition yac.c:1063
int const YAC_PROLEPTIC_GREGORIAN
Definition yac.c:72
double yac_cget_field_frac_mask_fallback_value(const char *comp_name, const char *grid_name, const char *field_name)
Definition yac.c:5547
void yac_cget_comps_comm_instance(int yac_instance_id, char const **comp_names, int num_comps, MPI_Comm *comps_comm)
Definition yac.c:1199
double yac_cget_field_frac_mask_fallback_value_instance(int yac_instance_id, const char *comp_name, const char *grid_name, const char *field_name)
Definition yac.c:5515
int const YAC_TIME_UNIT_ISO_FORMAT
Definition yac.c:69
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_ccleanup_instance(int yac_instance_id)
Clean-up a YAC instance (see Restarting YAC)
Definition yac.c:989
void yac_cfree_interp_stack_config(int interp_stack_config_id)
Definition yac.c:5938
int const YAC_NNN_AVG
Definition yac.c:83
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:5996
void yac_cinit_instance(int *yac_instance_id)
Definition yac.c:756
void yac_cget_interp_stack_config(int *interp_stack_config_id)
Definition yac.c:5922
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_instance(int yac_instance_id, 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:2524
static int default_instance_id
Definition yac.c:171
int const YAC_REDUCTION_TIME_NONE
Definition yac.c:56