YAC 3.18.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_dummy_coupling6_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 <string.h>
11#include "tests.h"
12#include "yac.h"
13
18#define YAC_RAD (0.01745329251994329576923690768489) // M_PI / 180
19
20static void utest_run_comp_a(char const * config_dir);
21static void utest_run_comp_b(char const * config_dir);
22
23int main(int argc, char** argv) {
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 if (argc != 2) {
32 PUT_ERR("ERROR: missing config file directory");
33 xt_finalize();
34 MPI_Finalize();
35 return TEST_EXIT_CODE;
36 }
37
38 switch(rank) {
39 case(0): {
40 MPI_Comm yac_comm;
41 const char* group_name = yac_cget_mpi_handshake_group_name();
42 yac_cmpi_handshake(MPI_COMM_WORLD, 1, &group_name, &yac_comm);
43 yac_cinit_comm_dummy(yac_comm);
44 MPI_Comm_free(&yac_comm);
45 break;
46 }
47 case(1):
48 utest_run_comp_a(argv[1]);
49 break;
50 case(2):
51 utest_run_comp_b(argv[1]);
52 break;
53 default:
54 PUT_ERR("wrong number of processes (has to be 3)");
55 break;
56 }
57
58 xt_finalize();
59 MPI_Finalize();
60 return TEST_EXIT_CODE;
61}
62
63static void utest_run_comp_a(char const * config_dir) {
64
65 // initialise YAC default instance
66 yac_cinit();
68
69 char * json_filename =
70 strcat(
71 strcpy(
72 malloc(strlen(config_dir) + 32), config_dir), "coupling_test6.json");
73 yac_cread_config_json(json_filename);
74 free(json_filename);
75
76 // define local component
77 int comp_id;
78 yac_cdef_comp("comp_A", &comp_id);
79
80 // get communicator that contains both components
81 MPI_Comm pair_comm;
82 yac_cget_comps_comm((char const*[]){"comp_A", "comp_B"}, 2, &pair_comm);
83
84 // check the pair_comm
85 {
86 char * sendbuf = "A";
87 char recvbuf[2];
88 MPI_Allgather(sendbuf, 1, MPI_CHAR, recvbuf, 1, MPI_CHAR, pair_comm);
89
90 if ((recvbuf[0] != 'A') || (recvbuf[1] != 'B'))
91 PUT_ERR("ERROR in yac_cget_comps_instance");
92 }
93 MPI_Comm_free(&pair_comm);
94
95 int nbr_vertices = 4;
96 int nbr_cells = 2;
97 int nbr_vertices_per_cell[2] = {3,3};
98 double x_vertices[4] =
99 {0.0 * YAC_RAD, -1.0 * YAC_RAD, 1.0 * YAC_RAD, 0.0 * YAC_RAD};
100 double y_vertices[4] =
101 {1.0 * YAC_RAD, 0.0 * YAC_RAD, 0.0 * YAC_RAD, -1.0 * YAC_RAD};
102 int cell_to_vertex[6] = {0,1,2, 1,3,2};
103
104 /* define local grid
105
106 0
107 / \
108 / o \
109 / \
110 1-------2 Eq.
111 \ /
112 \ o /
113 \ /
114 3
115
116 */
117 int grid_id;
119 "grid_A", nbr_vertices, nbr_cells, nbr_vertices_per_cell,
120 x_vertices, y_vertices, cell_to_vertex, &grid_id);
121
122 double x_cells[2] = {0.0 * YAC_RAD, 0.0 * YAC_RAD};
123 double y_cells[2] = {0.5 * YAC_RAD, -0.5 * YAC_RAD};
124
125 // center points in cells (needed e.g. for nearest neighbour)
128 grid_id, nbr_cells, YAC_LOCATION_CELL, x_cells, y_cells, &cell_point_id);
129 // vertex points
131 grid_id, nbr_vertices, YAC_LOCATION_CORNER, x_vertices, y_vertices,
132 &vertex_point_id);
133
134 // masks
135 int cell_mask_id, vertex_mask_id;
136 int cell_mask[2] = {1, 1};
137 int vertex_mask[4] = {1, 1, 1, 1};
139 grid_id, nbr_cells, YAC_LOCATION_CELL, cell_mask, &cell_mask_id);
141 grid_id, nbr_vertices, YAC_LOCATION_CORNER, vertex_mask, &vertex_mask_id);
142
143 // define field
144 int field_id, multi_field_id;
145 int collection_size = 1;
147 "A_to_B_src", comp_id, &cell_point_id, 1, collection_size, "1",
149 int multi_field_point_ids[2], multi_field_mask_ids[2];
150 multi_field_point_ids[0] = cell_point_id;
151 multi_field_point_ids[1] = vertex_point_id;
152 multi_field_mask_ids[0] = cell_mask_id;
153 multi_field_mask_ids[1] = vertex_mask_id;
155 "A_to_B_multi", comp_id, multi_field_point_ids, multi_field_mask_ids,
156 2, collection_size, "1", YAC_TIME_UNIT_SECOND, &multi_field_id);
157
158 // generate coupling
159 int ierror;
160 yac_cenddef();
161
162 { // test yac_cget_field_source
163 const char* src_comp_name;
164 const char* src_grid_name;
165 const char* src_field_name;
167 "comp_B", "grid_B", "A_to_B_tgt",
168 &src_comp_name, &src_grid_name, &src_field_name);
169
170 if (strcmp(src_comp_name, "comp_A") ||
171 strcmp(src_grid_name, "grid_A") ||
172 strcmp(src_field_name, "A_to_B_src"))
173 PUT_ERR("ERROR in yac_cget_field_source");
174 }
175
176 // move data from comp_A to comp_B
177 {
178 double send_field_data[2] = {3.0, 4.0};
179 double * send_field_[1] = {&send_field_data[0]};
180 double ** send_field[1] = {&send_field_[0]};
181 int info;
182 yac_cput(field_id, collection_size, send_field, &info, &ierror);
183 }
184 {
185 double send_cell_field_data[2] = {3.0, 4.0};
186 double send_vertex_field_data[4] = {3.0, 4.0, 5.0, 6.0};
187 double * send_field_[2] =
188 {&send_cell_field_data[0], &send_vertex_field_data[0]};
189 double ** send_field[1] = {&send_field_[0]};
190 int info;
191 yac_cput(multi_field_id, collection_size, send_field, &info, &ierror);
192 }
193
194 // finalise YAC default instance
196}
197
198static void utest_run_comp_b(char const * config_dir) {
199
200 // initialise YAC default instance
204
205 char * json_filename =
206 strcat(
207 strcpy(
208 malloc(strlen(config_dir) + 32), config_dir), "coupling_test6.json");
210 free(json_filename);
211
212 // define local component
213 int comp_id;
215
216 // get communicator that contains both components
217 MPI_Comm pair_comm;
219 default_instance_id, (char const *[]){"comp_B", "comp_A"}, 2, &pair_comm);
220
221 // check the pair_comm
222 {
223 char * sendbuf = "B";
224 char recvbuf[2];
225 MPI_Allgather(sendbuf, 1, MPI_CHAR, recvbuf, 1, MPI_CHAR, pair_comm);
226 if ((recvbuf[0] != 'A') || (recvbuf[1] != 'B'))
227 PUT_ERR("ERROR in yac_cget_comps_comm");
228 }
229 MPI_Comm_free(&pair_comm);
230
231 int nbr_vertices[2] = {2,3};
232 int cyclic[2] = {0,0};
233 double x_vertices[2] = {-0.5 * YAC_RAD, 0.5 * YAC_RAD};
234 double y_vertices[3] = {-1.0 * YAC_RAD, 0.0 * YAC_RAD, 1.0 * YAC_RAD};
235
236 /* define local grid
237
238 4-------5
239 | |
240 | o |
241 | |
242 2-------3
243 | |
244 | o |
245 | |
246 0-------1
247
248 */
249 int grid_id_B;
251 "grid_B", nbr_vertices, cyclic, x_vertices, y_vertices, &grid_id_B);
252
253 int nbr_cells[2] = {1,2};
254 double x_cells[1] = {0.0 * YAC_RAD};
255 double y_cells[2] = {-0.5 * YAC_RAD, 0.5 * YAC_RAD};
256
257 // center points in cells (needed e.g. for nearest neighbour)
258 int point_id_B;
260 grid_id_B, nbr_cells, YAC_LOCATION_CELL, x_cells, y_cells, &point_id_B);
261
262 // define field
263 int field_id, multi_field_id;
264 int collection_size = 1;
266 "A_to_B_tgt", comp_id, &point_id_B, 1, collection_size, "1",
269 "A_to_B_multi", comp_id, &point_id_B, 1, collection_size, "1",
270 YAC_TIME_UNIT_SECOND, &multi_field_id);
271
272 // generate coupling
273 int ierror;
275
276 { // test yac_cget_field_source_instance
277 const char* src_comp_name;
278 const char* src_grid_name;
279 const char* src_field_name;
281 default_instance_id, "comp_B", "grid_B", "A_to_B_tgt",
282 &src_comp_name, &src_grid_name, &src_field_name);
283
284 if (strcmp(src_comp_name, "comp_A") ||
285 strcmp(src_grid_name, "grid_A") ||
286 strcmp(src_field_name, "A_to_B_src"))
287 PUT_ERR("ERROR in yac_cget_field_source");
288 }
289
290 // get data from comp_A
291 double recv_field_data[2] = {-1.0, -1.0};
292 double * recv_field[1] = {&recv_field_data[0]};
293 int info;
294 yac_cget(multi_field_id, collection_size, recv_field, &info, &ierror);
295 if ((recv_field_data[0] != -2.0) || (recv_field_data[1] != -2.0))
296 PUT_ERR("ERROR: wrong results from multi field exchange");
297 yac_cget(field_id, collection_size, recv_field, &info, &ierror);
298
299 { // run internal instance
300
301 MPI_Comm internal_comm;
302 yac_cget_comp_comm(comp_id, &internal_comm);
303
304 // initialise internal YAC instance
305 int internal_instance_id;
306 yac_cinit_comm_instance(internal_comm, &internal_instance_id);
307
308 char * yaml_filename =
309 strcat(
310 strcpy(
311 malloc(strlen(config_dir) + 32), config_dir),
312 "coupling_test6_local.yaml");
313 yac_cread_config_yaml_instance(internal_instance_id, yaml_filename);
314 free(yaml_filename);
315
316 // define internal component
317 int internal_comp_id;
318 yac_cdef_comp_instance(internal_instance_id, "comp_B", &internal_comp_id);
319
320 int nbr_vertices[2] = {2,3};
321 int cyclic[2] = {0,0};
322 double x_vertices[2*3] = {-0.4 * YAC_RAD, 0.4 * YAC_RAD,
323 -0.4 * YAC_RAD, 0.4 * YAC_RAD,
324 -0.4 * YAC_RAD, 0.4 * YAC_RAD};
325 double y_vertices[2*3] = {-0.7 * YAC_RAD, -0.7 * YAC_RAD,
326 0.0 * YAC_RAD, 0.0 * YAC_RAD,
327 0.8 * YAC_RAD, 0.8 * YAC_RAD};
328
329 /* define local grid
330
331 4-------5
332 | |
333 | |
334 | |
335 2-------3
336 | |
337 | |
338 | |
339 0-------1
340
341 */
342 int grid_id_C;
344 "grid_C", nbr_vertices, cyclic, x_vertices, y_vertices, &grid_id_C);
345
346 // vertices points (needed e.g. for nearest neighbour)
347 int point_id_C;
349 grid_id_C, nbr_vertices, YAC_LOCATION_CORNER,
350 x_vertices, y_vertices, &point_id_C);
351
352 // define field
353 int field_id_src, field_id_tgt;
355 "B_to_C", internal_comp_id, &point_id_B, 1, 1, "1",
356 YAC_TIME_UNIT_SECOND, &field_id_src);
358 "B_to_C", internal_comp_id, &point_id_C, 1, 1, "1",
359 YAC_TIME_UNIT_SECOND, &field_id_tgt);
360
361 // generate internal coupling
362 int ierror;
363 yac_cenddef_instance(internal_instance_id);
364
365 // move data from grid B to grid C
366 int send_info, recv_info;
367 int collection_size = 1;
368 double * send_field_[1] = {&recv_field_data[0]};
369 double ** send_field[1] = {&send_field_[0]};
370 double recv_field_data_C[6] = {-1.0, -1.0, -1.0, -1.0, -1.0, -1.0};
371 double * recv_field[1] = {&recv_field_data_C[0]};
373 field_id_src, field_id_tgt, collection_size, send_field, recv_field,
374 &send_info, &recv_info, &ierror);
375
376 double ref_recv_field_data_C[6] = {4.0, 4.0, 4.0, 4.0, 3.0, 3.0};
377 for (int i = 0; i < 6; ++i)
378 if (recv_field_data_C[i] != ref_recv_field_data_C[i])
379 PUT_ERR("data missmatch");
380
381 yac_ccleanup_instance(internal_instance_id);
382
383 MPI_Comm_free(&internal_comm);
384 }
385
386 // finalise YAC default instance
388}
#define YAC_RAD
int nbr_vertices_per_cell[NBR_CELLS]
int * cell_to_vertex
int * cell_mask
int collection_size
char * yaml_filename
char const src_grid_name[]
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_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
int const YAC_LOCATION_CELL
Definition yac.c:37
void yac_cdef_grid_curve2d(const char *grid_name, int nbr_vertices[2], int cyclic[2], double *x_vertices, double *y_vertices, int *grid_id)
Definition yac.c:5626
void yac_cread_config_json(const char *yaml_filename)
Definition yac.c:863
void yac_cmpi_handshake(MPI_Comm comm, size_t n, char const **group_names, MPI_Comm *group_comms)
Definition yac.c:669
void yac_cget_field_source(const char *tgt_comp_name, const char *tgt_grid_name, const char *tgt_field_name, const char **src_comp_name, const char **src_grid_name, const char **src_field_name)
Definition yac.c:5595
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
const char * yac_cget_mpi_handshake_group_name(void)
Retrieve the MPI handshake group name used by YAC.
Definition yac.c:1114
void yac_cread_config_yaml_instance(int yac_instance_id, const char *yaml_filename)
Definition yac.c:840
int const YAC_LOCATION_CORNER
Definition yac.c:38
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_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_cread_config_json_instance(int yac_instance_id, const char *yaml_filename)
Definition yac.c:854
int const YAC_TIME_UNIT_SECOND
Definition yac.c:63
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_cdef_calendar(int calendar)
Definition yac.c:1063
int const YAC_PROLEPTIC_GREGORIAN
Definition yac.c:72
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
void yac_cget_field_source_instance(int yac_instance_id, const char *tgt_comp_name, const char *tgt_grid_name, const char *tgt_field_name, const char **src_comp_name, const char **src_grid_name, const char **src_field_name)
Definition yac.c:5581
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_cdef_points_curve2d(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:1638
void yac_ccleanup_instance(int yac_instance_id)
Clean-up a YAC instance (see Restarting YAC)
Definition yac.c:989
void yac_cinit_instance(int *yac_instance_id)
Definition yac.c:756
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
static int default_instance_id
Definition yac.c:171