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

A test for the parallel radial basis function interpolation method.

// Copyright (c) 2024 The YAC Authors
//
// SPDX-License-Identifier: BSD-3-Clause
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "tests.h"
#include "interp_method.h"
#include "dist_grid_utils.h"
#include "yac_mpi.h"
#include "geometry.h"
#include <mpi.h>
#include <yaxt.h>
#include <netcdf.h>
char const src_grid_name[] = "src_grid";
char const tgt_grid_name[] = "tgt_grid";
static void target_main(MPI_Comm target_comm);
static void source_main(MPI_Comm source_comm);
int main (void) {
MPI_Init(NULL, NULL);
xt_initialize(MPI_COMM_WORLD);
int comm_rank, comm_size;
MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
MPI_Barrier(MPI_COMM_WORLD);
if (comm_size != 2) {
PUT_ERR("ERROR: wrong number of processes");
xt_finalize();
MPI_Finalize();
return TEST_EXIT_CODE;
}
int tgt_flag = comm_rank < 1;
MPI_Comm split_comm;
MPI_Comm_split(MPI_COMM_WORLD, tgt_flag, 0, &split_comm);
if (tgt_flag) target_main(split_comm);
else source_main(split_comm);
MPI_Comm_free(&split_comm);
xt_finalize();
MPI_Finalize();
return TEST_EXIT_CODE;
}
static void source_main(MPI_Comm source_comm) {
// 1 and 5 nearest neighbours per target point with fixed value fallback
// corner and cell ids for a 7 x 7 grid (x == target point position)
// 56-----57-----58-----59-----60-----61-----62-----63
// | x| x| x| x| x| x| x|
// | 42 | 43 | 44 | 45 | 46 | 47 | 48 |
// | | | | | | | |
// 48-----49-----50-----51-----52-----53-----54-----55
// | x| x| x| x| x| x| x|
// | 35 | 36 | 37 | 38 | 39 | 40 | 41 |
// | | | | | | | |
// 40-----41-----42-----43-----44-----45-----46-----47
// | x| x| x| x| x| x| x|
// | 28 | 29 | 30 | 31 | 32 | 33 | 34 |
// | | | | | | | |
// 32-----33-----34-----35-----36-----37-----38-----39
// | x| x| x| x| x| x| x|
// | 21 | 22 | 23 | 24 | 25 | 26 | 27 |
// | | | | | | | |
// 24-----25-----26-----27-----28-----29-----30-----31
// | x| x| x| x| x| x| x|
// | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
// | | | | | | | |
// 16-----17-----18-----19-----20-----21-----22-----23
// | x| x| x| x| x| x| x|
// | 07 | 08 | 09 | 10 | 11 | 12 | 13 |
// | | | | | | | |
// 08-----09-----10-----11-----12-----13-----14-----15
// | x| x| x| x| x| x| x|
// | 00 | 01 | 02 | 03 | 04 | 05 | 06 |
// | | | | | | | |
// 00-----01-----02-----03-----04-----05-----06-----07
//
// the grid is distributed among the processes as follows:
// (index == process)
//
// 3---3---3---3---3---3---3---3
// | 3 | 3 | 3 | 3 | 3 | 3 | 3 |
// 3---3---3---3---3---3---3---3
// | 3 | 3 | 3 | 3 | 3 | 3 | 3 |
// 3---3---3---1---2---2---3---3
// | 1 | 1 | 1 | 2 | 2 | 2 | 2 |
// 1---1---1---2---2---2---2---2
// | 1 | 1 | 1 | 2 | 2 | 2 | 2 |
// 1---1---1---1---2---2---2---2
// | 1 | 1 | 1 | 2 | 2 | 2 | 2 |
// 1---1---1---0---0---0---2---2
// | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
// 0---0---0---0---0---0---0---0
// | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
// 0---0---0---0---0---0---0---0
//
// the source mask looks as follows (# == masked point)
//
// +---+---+---+---+---+---+---#
// | | | | | | | |
// +---+---+---+---+---+---#---+
// | | | | | | | |
// +---+---+---+---+---#---+---+
// | | | | | | | |
// +---+---+---+---#---+---+---+
// | | | | | | | |
// +---+---+---#---+---+---+---+
// | | | | | | | |
// +---+---#---+---+---+---+---+
// | | | | | | | |
// +---#---+---+---+---+---+---+
// | | | | | | | |
// #---+---+---+---+---+---+---+
int my_source_rank;
MPI_Comm_rank(source_comm, &my_source_rank);
double coordinates_x[] = {-2.0, -1.0, 0.0, 1.0, 2.0};
double coordinates_y[] = {-2.0, -1.0, 0.0, 1.0, 2.0};
size_t const num_cells[2] = {4,4};
size_t local_start[2] = {0,0};
size_t local_count[2] = {4,4};
int with_halo = 0;
for (size_t i = 0; i <= num_cells[0]; ++i) coordinates_x[i] *= YAC_RAD;
for (size_t i = 0; i <= num_cells[1]; ++i) coordinates_y[i] *= YAC_RAD;
struct yac_basic_grid_data grid_data =
yac_generate_basic_grid_data_reg2d(
coordinates_x, coordinates_y, num_cells,
local_start, local_count, with_halo);
struct yac_basic_grid * grids[2] =
{yac_basic_grid_new(src_grid_name, grid_data),
yac_basic_grid_empty_new(tgt_grid_name)};
struct yac_dist_grid_pair * grid_pair =
yac_dist_grid_pair_new(grids[0], grids[1], MPI_COMM_WORLD);
struct yac_interp_field src_fields[] =
{{.location = YAC_LOC_CORNER, .coordinates_idx = SIZE_MAX, .masks_idx = SIZE_MAX}};
size_t num_src_fields = sizeof(src_fields) / sizeof(src_fields[0]);
struct yac_interp_field tgt_field =
{.location = YAC_LOC_CELL, .coordinates_idx = 0, .masks_idx = SIZE_MAX};
struct yac_interp_grid * interp_grid =
struct interp_method * method_stack[2] =
(struct yac_nnn_config){
.type = YAC_INTERP_NNN_RBF, .n = 9,
.data.rbf_scale = YAC_INTERP_RBF_SCALE_DEFAULT}),
NULL};
struct yac_interp_weights * weights =
yac_interp_method_do_search(method_stack, interp_grid);
yac_interp_method_delete(method_stack);
enum yac_interp_weights_reorder_type reorder_type[2] =
for (size_t i = 0; i < 2; ++i) {
for (size_t collection_size = 1; collection_size < 4;
struct yac_interpolation * interpolation =
weights, reorder_type[i], collection_size,
YAC_FRAC_MASK_NO_VALUE, 1.0, 0.0, NULL);
// check generated interpolation
{
double *** src_data = xmalloc(collection_size * sizeof(*src_data));
double ref_src_data[25] = {0,1,2,3,4,
1,2,3,4,5,
2,3,4,5,6,
3,4,5,6,7,
4,5,6,7,8};
for (size_t collection_idx = 0; collection_idx < collection_size;
++collection_idx) {
// only one field
src_data[collection_idx] = xmalloc(1 * sizeof(**src_data));
src_data[collection_idx][0] =
xmalloc(grid_data.num_vertices * sizeof(***src_data));
for (size_t i = 0; i < grid_data.num_vertices; ++i)
src_data[collection_idx][0][i] =
ref_src_data[i] + (double)(collection_idx * 9);
}
yac_interpolation_execute(interpolation, src_data, NULL);
for (size_t collection_idx = 0; collection_idx < collection_size;
++collection_idx) {
free(src_data[collection_idx][0]);
free(src_data[collection_idx]);
}
free(src_data);
}
yac_interpolation_delete(interpolation);
}
}
yac_interp_grid_delete(interp_grid);
}
static void target_main(MPI_Comm target_comm) {
UNUSED(target_comm);
double coordinates_x[] = {-1.5,-0.5,0.5,1.5};
double coordinates_y[] = {-1.5,-0.5,0.5,1.5};
double cell_coordinates_x[] = {-1.0,0.0,1.0};
double cell_coordinates_y[] = {-1.0,0.0,1.0};
yac_coordinate_pointer cell_coordinates = xmalloc(9 * sizeof(*cell_coordinates));
size_t const num_cells[2] = {3,3};
size_t local_start[2] = {0,0};
size_t local_count[2] = {3,3};
int with_halo = 0;
for (size_t i = 0; i <= num_cells[0]; ++i) coordinates_x[i] *= YAC_RAD;
for (size_t i = 0; i <= num_cells[1]; ++i) coordinates_y[i] *= YAC_RAD;
for (size_t i = 0, k = 0; i < num_cells[1]; ++i)
for (size_t j = 0; j < num_cells[0]; ++j, ++k)
cell_coordinates_x[j], cell_coordinates_y[i], cell_coordinates[k]);
struct yac_basic_grid_data grid_data =
yac_generate_basic_grid_data_reg2d(
coordinates_x, coordinates_y, num_cells,
local_start, local_count, with_halo);
struct yac_basic_grid * grids[2] =
{yac_basic_grid_new(tgt_grid_name, grid_data),
yac_basic_grid_empty_new(src_grid_name)};
grids[0], YAC_LOC_CELL, cell_coordinates);
struct yac_dist_grid_pair * grid_pair =
yac_dist_grid_pair_new(grids[0], grids[1], MPI_COMM_WORLD);
struct yac_interp_field src_fields[] =
{{.location = YAC_LOC_CORNER, .coordinates_idx = SIZE_MAX, .masks_idx = SIZE_MAX}};
size_t num_src_fields = sizeof(src_fields) / sizeof(src_fields[0]);
struct yac_interp_field tgt_field =
{.location = YAC_LOC_CELL, .coordinates_idx = 0, .masks_idx = SIZE_MAX};
struct yac_interp_grid * interp_grid =
struct interp_method * method_stack[2] =
(struct yac_nnn_config){
.type = YAC_INTERP_NNN_RBF, .n = 9,
.data.rbf_scale = YAC_INTERP_RBF_SCALE_DEFAULT}),
NULL};
struct yac_interp_weights * weights =
yac_interp_method_do_search(method_stack, interp_grid);
yac_interp_method_delete(method_stack);
enum yac_interp_weights_reorder_type reorder_type[2] =
for (size_t i = 0; i < 2; ++i) {
for (size_t collection_size = 1; collection_size < 4;
struct yac_interpolation * interpolation =
weights, reorder_type[i], collection_size,
YAC_FRAC_MASK_NO_VALUE, 1.0, 0.0, NULL);
// check generated interpolation
{
double ref_tgt_field[9] = {2,3,4, 3,4,5, 4,5,6};
double ** tgt_data = xmalloc(collection_size * sizeof(*tgt_data));
for (size_t collection_idx = 0; collection_idx < collection_size;
++collection_idx) {
tgt_data[collection_idx] =
xmalloc(grid_data.num_cells * sizeof(**tgt_data));
for (size_t j = 0; j < 9; ++j) tgt_data[collection_idx][j] = -1.0;
}
yac_interpolation_execute(interpolation, NULL, tgt_data);
for (size_t collection_idx = 0; collection_idx < collection_size;
++collection_idx)
for (size_t j = 0, offset = collection_idx * 9;
j < grid_data.num_cells; ++j)
if (fabs((ref_tgt_field[j] + (double)offset) -
tgt_data[collection_idx][j]) > 1e-3)
PUT_ERR("wrong interpolation result");
for (size_t collection_idx = 0; collection_idx < collection_size;
++collection_idx)
free(tgt_data[collection_idx]);
free(tgt_data);
}
yac_interpolation_delete(interpolation);
}
}
yac_interp_grid_delete(interp_grid);
}
struct yac_basic_grid * yac_basic_grid_new(char const *name, struct yac_basic_grid_data grid_data)
Definition basic_grid.c:50
size_t yac_basic_grid_add_coordinates_nocpy(struct yac_basic_grid *grid, enum yac_location location, yac_coordinate_pointer coordinates)
Definition basic_grid.c:208
struct yac_basic_grid * yac_basic_grid_empty_new(char const *name)
Definition basic_grid.c:63
void yac_basic_grid_delete(struct yac_basic_grid *grid)
Definition basic_grid.c:70
#define UNUSED(x)
Definition core.h:73
int main(int argc, char **argv)
void yac_dist_grid_pair_delete(struct yac_dist_grid_pair *grid_pair)
Definition dist_grid.c:2608
struct yac_dist_grid_pair * yac_dist_grid_pair_new(struct yac_basic_grid *grid_a, struct yac_basic_grid *grid_b, MPI_Comm comm)
Definition dist_grid.c:2359
#define YAC_RAD
Definition geometry.h:30
static void LLtoXYZ_deg(double lon, double lat, double p_out[])
Definition geometry.h:304
void yac_interp_grid_delete(struct yac_interp_grid *interp_grid)
struct yac_interp_grid * yac_interp_grid_new(struct yac_dist_grid_pair *grid_pair, char const *src_grid_name, char const *tgt_grid_name, size_t num_src_fields, struct yac_interp_field const *src_fields, struct yac_interp_field const tgt_field)
Definition interp_grid.c:31
void yac_interp_method_delete(struct interp_method **method)
struct yac_interp_weights * yac_interp_method_do_search(struct interp_method **method, struct yac_interp_grid *interp_grid)
struct interp_method * yac_interp_method_nnn_new(struct yac_nnn_config config)
@ YAC_INTERP_NNN_RBF
radial basis functions
#define YAC_INTERP_RBF_MAX_SEARCH_DISTANCE_DEFAULT
#define YAC_INTERP_RBF_SCALE_DEFAULT
void yac_interp_weights_delete(struct yac_interp_weights *weights)
struct yac_interpolation * yac_interp_weights_get_interpolation(struct yac_interp_weights *weights, enum yac_interp_weights_reorder_type reorder, size_t collection_size, double frac_mask_fallback_value, double scaling_factor, double scaling_summand, char const *yaxt_exchanger_name)
yac_interp_weights_reorder_type
@ YAC_MAPPING_ON_TGT
weights will be applied at target processes
@ YAC_MAPPING_ON_SRC
weights will be appied at source processes
void yac_interpolation_execute(struct yac_interpolation *interp, double ***src_fields, double **tgt_field)
void yac_interpolation_delete(struct yac_interpolation *interp)
double const YAC_FRAC_MASK_NO_VALUE
@ YAC_LOC_CORNER
Definition location.h:15
@ YAC_LOC_CELL
Definition location.h:14
#define xmalloc(size)
Definition ppm_xfuncs.h:66
enum yac_location location
Definition basic_grid.h:18
struct yac_interp_field tgt_field
Definition interp_grid.c:26
size_t num_src_fields
Definition interp_grid.c:27
struct yac_dist_grid_pair * grid_pair
Definition interp_grid.c:25
char * src_grid_name
Definition interp_grid.c:23
struct yac_interp_field src_fields[]
Definition interp_grid.c:28
char * tgt_grid_name
Definition interp_grid.c:24
struct yac_basic_grid ** grids
Definition yac.c:130
double(* yac_coordinate_pointer)[3]
Definition yac_types.h:19