YetAnotherCoupler 3.5.2
Loading...
Searching...
No Matches
test_dummy_coupling7_c.c

This test checks the frac mask feature.

// Copyright (c) 2024 The YAC Authors
//
// SPDX-License-Identifier: BSD-3-Clause
#include <mpi.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <yaxt.h>
#include "tests.h"
#include "yac.h"
#define YAC_RAD (0.01745329251994329576923690768489) // M_PI / 180
static void run_comp_a(int with_field_mask, int exchange_type);
static void run_comp_b(int with_field_mask, int exchange_type);
int main (void) {
MPI_Init(NULL, NULL);
xt_initialize(MPI_COMM_WORLD);
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
for (int exchange_type = 0; exchange_type < 2; ++exchange_type) {
for (int with_field_mask = 0; with_field_mask < 2; ++with_field_mask) {
switch(rank) {
case(0): {
MPI_Comm yac_comm;
const char* group_name = "yac";
yac_cmpi_handshake(MPI_COMM_WORLD, 1, &group_name, &yac_comm);
MPI_Comm_free(&yac_comm);
break;
}
case(1):
run_comp_a(with_field_mask, exchange_type);
break;
case(2):
run_comp_b(with_field_mask, exchange_type);
break;
default:
PUT_ERR("wrong number of processes (has to be 3)");
break;
}
}
}
xt_finalize();
MPI_Finalize();
return TEST_EXIT_CODE;
}
static void run_comp_a(int with_field_mask, int exchange_type) {
// initialise YAC default instance
yac_cdef_datetime("2000-01-01T00:00:00", "2000-01-01T00:00:12");
// define local component
int comp_id;
yac_cdef_comp("comp_A", &comp_id);
// get communicator that contains both components
MPI_Comm pair_comm;
yac_cget_comps_comm((char const*[]){"comp_A", "comp_B"}, 2, &pair_comm);
// check the pair_comm
{
char * sendbuf = "A";
char recvbuf[2];
MPI_Allgather(sendbuf, 1, MPI_CHAR, recvbuf, 1, MPI_CHAR, pair_comm);
if ((recvbuf[0] != 'A') || (recvbuf[1] != 'B'))
PUT_ERR("ERROR in yac_cget_comps_instance");
}
MPI_Comm_free(&pair_comm);
int nbr_vertices = 4;
int nbr_cells = 2;
int nbr_vertices_per_cell[2] = {3,3};
double x_vertices[4] =
{0.0 * YAC_RAD, -1.0 * YAC_RAD, 1.0 * YAC_RAD, 0.0 * YAC_RAD};
double y_vertices[4] =
{1.0 * YAC_RAD, 0.0 * YAC_RAD, 0.0 * YAC_RAD, -1.0 * YAC_RAD};
int cell_to_vertex[6] = {0,1,2, 1,3,2};
/* define local grid
0
/ \
/ o \
/ \
1-------2 Eq.
\ /
\ o /
\ /
3
*/
int grid_id;
"grid_A", nbr_vertices, nbr_cells, nbr_vertices_per_cell,
x_vertices, y_vertices, cell_to_vertex, &grid_id);
double x_cells[2] = {0.0 * YAC_RAD, 0.0 * YAC_RAD};
double y_cells[2] = {0.5 * YAC_RAD, -0.5 * YAC_RAD};
// center points in cells (needed e.g. for nearest neighbour)
int cell_point_id, vertex_point_id;
grid_id, nbr_cells, YAC_LOCATION_CELL, x_cells, y_cells, &cell_point_id);
// vertex points
grid_id, nbr_vertices, YAC_LOCATION_CORNER, x_vertices, y_vertices,
&vertex_point_id);
// masks
int cell_mask_id, vertex_mask_id;
int cell_mask[2] = {1, 0};
int vertex_mask[4] = {1, 1, 1, 1};
grid_id, nbr_cells, YAC_LOCATION_CELL, cell_mask, &cell_mask_id);
grid_id, nbr_vertices, YAC_LOCATION_CORNER, vertex_mask, &vertex_mask_id);
// define field
int field_id, multi_field_id, dummy_field_id;
double frac_mask_fallback_value = -1.0;
double frac_mask_fallback_value_multi = strtod("inf", NULL);
int collection_size = 1;
if (with_field_mask) {
"A_to_B_src", comp_id, &cell_point_id, &cell_mask_id, 1,
collection_size, "1", YAC_TIME_UNIT_SECOND, &field_id);
} else {
"A_to_B_src", comp_id, &cell_point_id, 1,
collection_size, "1", YAC_TIME_UNIT_SECOND, &field_id);
}
"comp_A", "grid_A", "A_to_B_src", frac_mask_fallback_value);
int multi_field_point_ids[2], multi_field_mask_ids[2];
multi_field_point_ids[0] = cell_point_id;
multi_field_point_ids[1] = vertex_point_id;
multi_field_mask_ids[0] = cell_mask_id;
multi_field_mask_ids[1] = vertex_mask_id;
if (with_field_mask) {
"A_to_B_multi", comp_id, multi_field_point_ids, multi_field_mask_ids,
2, collection_size, "1", YAC_TIME_UNIT_SECOND, &multi_field_id);
} else {
"A_to_B_multi", comp_id, multi_field_point_ids,
2, collection_size, "1", YAC_TIME_UNIT_SECOND, &multi_field_id);
}
"comp_A", "grid_A", "A_to_B_multi", frac_mask_fallback_value_multi);
"dummy_field", comp_id, &cell_point_id, 1,
collection_size, "1", YAC_TIME_UNIT_SECOND, &dummy_field_id);
// generate coupling
int ierror;
"comp_A", "grid_A", "A_to_B_multi") != frac_mask_fallback_value_multi)
PUT_ERR("ERROR in yac_cget_field_frac_mask_fallback_value");
// move data from comp_A to comp_B
{
double send_field_data[2] = {3.0, 4.0};
double * send_field_[1] = {&send_field_data[0]};
double ** send_field[1] = {&send_field_[0]};
double send_frac_mask_data[2] = {1.0, 1.0};
double * send_frac_mask_[1] = {&send_frac_mask_data[0]};
double ** send_frac_mask[1] = {&send_frac_mask_[0]};
int info;
if (exchange_type) {
field_id, collection_size, send_field, send_frac_mask, &info, &ierror);
yac_cget(dummy_field_id, collection_size, NULL, &info, &ierror);
} else {
field_id, dummy_field_id, collection_size,
send_field, send_frac_mask, NULL, &info, &info, &ierror);
}
}
{
double send_cell_field_data[2] = {3.0, 4.0};
double send_vertex_field_data[4] = {3.0, 4.0, 5.0, 6.0};
double * send_field_[2] =
{&send_cell_field_data[0], &send_vertex_field_data[0]};
double ** send_field[1] = {&send_field_[0]};
double send_cell_frac_mask_data[2] = {1.0, 1.0};
double send_vertex_frac_mask_data[4] = {1.0, 1.0, 1.0, 1.0};
double * send_frac_mask_[2] =
{&send_cell_frac_mask_data[0], &send_vertex_frac_mask_data[0]};
double ** send_frac_mask[1] = {&send_frac_mask_[0]};
int info;
multi_field_id, collection_size, send_field, send_frac_mask,
&info, &ierror);
}
// finalise YAC default instance
}
static void run_comp_b(int with_field_mask, int exchange_type) {
double frac_mask_fallback_value = strtod("inf", NULL);
// initialise YAC default instance
default_instance_id, "2000-01-01T00:00:00", "2000-01-01T00:00:12");
// define local component
int comp_id;
// get communicator that contains both components
MPI_Comm pair_comm;
default_instance_id, (char const *[]){"comp_B", "comp_A"}, 2, &pair_comm);
// check the pair_comm
{
char * sendbuf = "B";
char recvbuf[2];
MPI_Allgather(sendbuf, 1, MPI_CHAR, recvbuf, 1, MPI_CHAR, pair_comm);
if ((recvbuf[0] != 'A') || (recvbuf[1] != 'B'))
PUT_ERR("ERROR in yac_cget_comps_comm");
}
MPI_Comm_free(&pair_comm);
int nbr_vertices[2] = {2,3};
int cyclic[2] = {0,0};
double x_vertices[2] = {-0.5 * YAC_RAD, 0.5 * YAC_RAD};
double y_vertices[3] = {-1.0 * YAC_RAD, 0.0 * YAC_RAD, 1.0 * YAC_RAD};
/* define local grid
4-------5
| |
| o |
| |
2-------3
| |
| o |
| |
0-------1
*/
int grid_id_B;
"grid_B", nbr_vertices, cyclic, x_vertices, y_vertices, &grid_id_B);
int nbr_cells[2] = {1,2};
double x_cells[1] = {0.0 * YAC_RAD};
double y_cells[2] = {-0.5 * YAC_RAD, 0.5 * YAC_RAD};
// center points in cells (needed e.g. for nearest neighbour)
int point_id_B;
grid_id_B, nbr_cells, YAC_LOCATION_CELL, x_cells, y_cells, &point_id_B);
// define field
int field_id, multi_field_id, dummy_field_id;
int collection_size = 1;
"A_to_B_tgt", comp_id, &point_id_B, 1, collection_size, "1",
YAC_TIME_UNIT_SECOND, &field_id);
"A_to_B_multi", comp_id, &point_id_B, 1, collection_size, "1",
YAC_TIME_UNIT_SECOND, &multi_field_id);
"dummy_field", comp_id, &point_id_B, 1, collection_size, "1",
YAC_TIME_UNIT_SECOND, &dummy_field_id);
// define interpolation stacks
int interp_stack_nnn, interp_stack_fixed;
"- nnn:\n"
" n: 1", &interp_stack_nnn);
"[{\"fixed\": {\"user_value\": -2}}]", &interp_stack_fixed);
// define couplings
"comp_A", "grid_A", "A_to_B_src",
"comp_B", "grid_B", "A_to_B_tgt",
interp_stack_nnn, 0, 0);
"comp_A", "grid_A", "A_to_B_multi",
"comp_B", "grid_B", "A_to_B_multi",
interp_stack_fixed, 0, 0);
yac_cfree_interp_stack_config(interp_stack_nnn);
yac_cfree_interp_stack_config(interp_stack_fixed);
// generate coupling
int ierror;
default_instance_id, "comp_A", "grid_A", "A_to_B_multi") !=
frac_mask_fallback_value)
PUT_ERR("ERROR in yac_cget_field_frac_mask_fallback_value_instance");
// get data from comp_A
double recv_field_data[2] = {-1.0, -1.0};
double * recv_field[1] = {&recv_field_data[0]};
int info;
yac_cget(multi_field_id, collection_size, recv_field, &info, &ierror);
if ((recv_field_data[0] != -2.0) || (recv_field_data[1] != -2.0))
PUT_ERR("ERROR: wrong results from multi field exchange");
if (exchange_type) {
dummy_field_id, collection_size, NULL, &info, &ierror);
dummy_field_id, field_id, collection_size, NULL, recv_field,
&info, &info, &ierror);
} else {
yac_cget(field_id, collection_size, recv_field, &info, &ierror);
}
{ // run internal instance
MPI_Comm internal_comm;
yac_cget_comp_comm(comp_id, &internal_comm);
// initialise internal YAC instance
int internal_instance_id;
yac_cinit_comm_instance(internal_comm, &internal_instance_id);
internal_instance_id, "2000-01-01T00:00:00", "2000-01-01T00:00:12");
// define internal component
int internal_comp_id;
yac_cdef_comp_instance(internal_instance_id, "comp_B", &internal_comp_id);
int nbr_vertices = 2*3;
double x_vertices[2*3] = {-0.4 * YAC_RAD, 0.4 * YAC_RAD,
-0.4 * YAC_RAD, 0.4 * YAC_RAD,
-0.4 * YAC_RAD, 0.4 * YAC_RAD};
double y_vertices[2*3] = {-0.7 * YAC_RAD, -0.7 * YAC_RAD,
0.0 * YAC_RAD, 0.0 * YAC_RAD,
0.8 * YAC_RAD, 0.8 * YAC_RAD};
/* define local grid
4-------5
| |
| |
| |
2-------3
| |
| |
| |
0-------1
*/
int grid_id_C;
"grid_C", nbr_vertices, x_vertices, y_vertices, &grid_id_C);
// vertices points (needed e.g. for nearest neighbour)
int point_id_C;
grid_id_C, nbr_vertices, YAC_LOCATION_CORNER,
x_vertices, y_vertices, &point_id_C);
// define field
int field_id_src, field_id_tgt;
"B_to_C", internal_comp_id, &point_id_B, 1,
1, "1", YAC_TIME_UNIT_SECOND, &field_id_src);
internal_instance_id, "comp_B", "grid_B", "B_to_C",
frac_mask_fallback_value);
"B_to_C", internal_comp_id, &point_id_C, 1, 1, "1",
YAC_TIME_UNIT_SECOND, &field_id_tgt);
// define interpolation stack
int interp_stack_nnn;
yac_cget_interp_stack_config(&interp_stack_nnn);
interp_stack_nnn, YAC_NNN_AVG, 1, 0.0, 0.0);
// define coupling
int ext_couple_config;
double const scale_factor = 2.0;
double const scale_summand = 0.5;
yac_cget_ext_couple_config(&ext_couple_config);
ext_couple_config, scale_factor);
ext_couple_config, scale_summand);
internal_instance_id,
"comp_B", "grid_B", "B_to_C",
"comp_B", "grid_C", "B_to_C",
interp_stack_nnn, 0, 0, ext_couple_config);
yac_cfree_ext_couple_config(ext_couple_config);
yac_cfree_interp_stack_config(interp_stack_nnn);
// generate internal coupling
int ierror;
yac_cenddef_instance(internal_instance_id);
// move data from grid B to grid C
int send_info, recv_info;
int collection_size = 1;
double * send_field_[1] = {&recv_field_data[0]};
double ** send_field[1] = {&send_field_[0]};
double send_frac_mask_data[2] = {1.0, 0.0};
double * send_frac_mask_[1] = {send_frac_mask_data};
double ** send_frac_mask[1] = {&send_frac_mask_[0]};
double recv_field_data_C[6] = {-1.0, -1.0, -1.0, -1.0, -1.0, -1.0};
double * recv_field[1] = {&recv_field_data_C[0]};
if (exchange_type)
field_id_src, field_id_tgt, collection_size,
send_field, send_frac_mask, recv_field,
&send_info, &recv_info, &ierror);
else
field_id_src, field_id_tgt, collection_size,
send_field_, send_frac_mask_, recv_field,
&send_info, &recv_info, &ierror);
double ref_recv_field_data_C[2][6] =
{{4.0, 4.0, 4.0, 4.0,
frac_mask_fallback_value, frac_mask_fallback_value},
{3.0, 3.0, 3.0, 3.0,
frac_mask_fallback_value, frac_mask_fallback_value}};
for (int i = 0; i < 6; ++i)
if (recv_field_data_C[i] !=
scale_factor * ref_recv_field_data_C[with_field_mask][i] +
scale_summand)
PUT_ERR("data missmatch");
yac_ccleanup_instance(internal_instance_id);
MPI_Comm_free(&internal_comm);
}
// finalise YAC default instance
}
int main(int argc, char **argv)
#define YAC_RAD
Definition geometry.h:30
void yac_cinit_comm_dummy(MPI_Comm comm)
Definition yac.c:429
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:2582
void yac_cenddef(void)
Definition yac.c:3000
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:1259
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:1143
void yac_cdef_datetime_instance(int yac_instance_id, const char *start_datetime, const char *end_datetime)
Definition yac.c:631
void yac_cfree_ext_couple_config(int ext_couple_config_id)
Definition yac.c:1373
void yac_cdef_datetime(const char *start_datetime, const char *end_datetime)
Definition yac.c:640
int const YAC_LOCATION_CELL
Definition yac.c:27
void yac_cset_ext_couple_config_scale_factor(int ext_couple_config_id, double scale_factor)
Definition yac.c:1442
void yac_cmpi_handshake(MPI_Comm comm, size_t n, char const **group_names, MPI_Comm *group_comms)
Definition yac.c:344
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:2827
void yac_cget_interp_stack_config_from_string_json(char const *interp_stack_config, int *interp_stack_config_id)
Definition yac.c:3935
void yac_cinit(void)
Definition yac.c:419
void yac_cfinalize_instance(int yac_instance_id)
Definition yac.c:610
void yac_cfinalize()
Definition yac.c:619
void yac_cenddef_instance(int yac_instance_id)
Definition yac.c:2993
void yac_cget_ext_couple_config(int *ext_couple_config_id)
Definition yac.c:1365
int const YAC_LOCATION_CORNER
Definition yac.c:28
void yac_cset_ext_couple_config_scale_summand(int ext_couple_config_id, double scale_summand)
Definition yac.c:1465
void yac_cget_comp_comm(int comp_id, MPI_Comm *comp_comm)
Definition yac.c:729
void yac_cget(int const field_id, int collection_size, double **recv_field, int *info, int *ierr)
Definition yac.c:2007
void yac_cdef_comp_instance(int yac_instance_id, char const *comp_name, int *comp_id)
Definition yac.c:880
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:3416
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:1585
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:976
void yac_cget_comps_comm(const char **comp_names, int num_comps, MPI_Comm *comps_comm)
Definition yac.c:779
void yac_cinit_comm_instance(MPI_Comm comm, int *yac_instance_id)
Definition yac.c:381
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:1247
void yac_cget_interp_stack_config_from_string_yaml(char const *interp_stack_config, int *interp_stack_config_id)
Definition yac.c:3927
int const YAC_TIME_UNIT_SECOND
Definition yac.c:52
void yac_cdef_grid_cloud(const char *grid_name, int nbr_points, double *x_points, double *y_points, int *grid_id)
Definition yac.c:3538
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:3452
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:2963
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:1050
void yac_cput(int const field_id, int const collection_size, double ***const send_field, int *info, int *ierr)
Definition yac.c:2672
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:2791
void yac_cdef_calendar(int calendar)
Definition yac.c:648
int const YAC_PROLEPTIC_GREGORIAN
Definition yac.c:61
double yac_cget_field_frac_mask_fallback_value(const char *comp_name, const char *grid_name, const char *field_name)
Definition yac.c:3356
void yac_cget_comps_comm_instance(int yac_instance_id, char const **comp_names, int num_comps, MPI_Comm *comps_comm)
Definition yac.c:769
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:3335
int const YAC_TIME_UNIT_ISO_FORMAT
Definition yac.c:58
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:1115
void yac_ccleanup_instance(int yac_instance_id)
Definition yac.c:582
void yac_cfree_interp_stack_config(int interp_stack_config_id)
Definition yac.c:3681
int const YAC_NNN_AVG
Definition yac.c:72
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:3738
void yac_cinit_instance(int *yac_instance_id)
Definition yac.c:407
void yac_cget_interp_stack_config(int *interp_stack_config_id)
Definition yac.c:3667
void yac_cdef_comp(char const *comp_name, int *comp_id)
Definition yac.c:886
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:1216
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:1615
static int default_instance_id
Definition yac.c:123
int const YAC_REDUCTION_TIME_NONE
Definition yac.c:45