YAC 3.13.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_interp_method_parallel.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 <stdlib.h>
6#include <unistd.h>
7#include <mpi.h>
8#include <yaxt.h>
9#include <string.h>
10
11#include "tests.h"
12#include "test_common.h"
13#include "geometry.h"
14#include "read_icon_grid.h"
27#include "weight_file_common.h"
28#include "yac_mpi.h"
29
35static void utest_compute_weights_callback(
36 double const tgt_coords[3], int src_cell_id, size_t src_cell_idx,
37 int const ** global_results_points, double ** result_weights,
38 size_t * result_count, void * user_data);
39
40int main(int argc, char** argv) {
41
42 MPI_Init(NULL, NULL);
43
44 xt_initialize(MPI_COMM_WORLD);
45
46 int comm_rank, comm_size;
47 MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank);
48 MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
49
50 set_even_io_rank_list(MPI_COMM_WORLD);
51
52 if (argc != 2) {
53 PUT_ERR("ERROR: missing grid file directory");
54 xt_finalize();
55 MPI_Finalize();
56 return TEST_EXIT_CODE;
57 }
58
59 char * filenames[2];
60 char * grid_filenames[] ={"icon_grid_0030_R02B03_G.nc", "icon_grid_0043_R02B04_G.nc"};
61 for (int i = 0; i < 2; ++i)
62 filenames[i] =
63 strcat(
64 strcpy(
65 malloc(strlen(argv[1]) + strlen(grid_filenames[i]) + 2), argv[1]),
66 grid_filenames[i]);
67
69
70 for (int i = 0; i < 2; ++i)
71 grid_data[i] =
73 filenames[i], MPI_COMM_WORLD);
74
75 struct yac_basic_grid * grids[2] =
76 {yac_basic_grid_new(filenames[0], grid_data[0]),
77 yac_basic_grid_new(filenames[1], grid_data[1])};
78
79 struct yac_dist_grid_pair * grid_pair =
80 yac_dist_grid_pair_new(grids[0], grids[1], MPI_COMM_WORLD);
81
82 struct yac_interp_field src_fields[] =
83 {{.location = YAC_LOC_CELL, .coordinates_idx = SIZE_MAX, .masks_idx = SIZE_MAX}};
84 size_t num_src_fields = sizeof(src_fields) / sizeof(src_fields[0]);
86 {.location = YAC_LOC_CELL, .coordinates_idx = SIZE_MAX, .masks_idx = SIZE_MAX};
87
88 struct yac_interp_grid * interp_grid =
89 yac_interp_grid_new(grid_pair, filenames[0], filenames[1],
91
92 struct interp_method * method_stack[] =
93 {yac_interp_method_fixed_new(-1.0), NULL};
94
96 yac_interp_method_do_search(method_stack, interp_grid);
97
98 struct yac_interpolation * interpolation_src =
100 weights, YAC_MAPPING_ON_SRC, 1,
101 YAC_FRAC_MASK_NO_VALUE, 1.0, 0.0, NULL, 1, 1);
102 struct yac_interpolation * interpolation_tgt =
104 weights, YAC_MAPPING_ON_TGT, 1,
105 YAC_FRAC_MASK_NO_VALUE, 1.0, 0.0, NULL, 1, 1);
106
107 yac_interpolation_delete(interpolation_src);
108 yac_interpolation_delete(interpolation_tgt);
109
111
112 yac_interp_method_delete(method_stack);
113
114 yac_interp_grid_delete(interp_grid);
115 yac_dist_grid_pair_delete(grid_pair);
116
117 for (int i = 0; i < 2; ++i) {
119 free(filenames[i]);
120 }
121
122 { // test interpolation_complete argument
123
124 char const * weight_file_empty =
125 "test_interp_method_parallel_weights_empty.nc";
126 char const * weight_file_fixed =
127 "test_interp_method_parallel_weights_fixed.nc";
128
129 char const * src_grid_name = "src_grid";
130 char const * tgt_grid_name = "tgt_grid";
131
132 // create weight file (one empty and one with a fixed value)
133 if (comm_rank == 0) {
134
135 { // empty file
136 int * tgt_indices = NULL;
137 int * src_indices = NULL;
138 double * weights = NULL;
139 size_t num_links = 0;
140 enum yac_location src_locations[] = {YAC_LOC_CELL};
141 enum yac_location tgt_location = YAC_LOC_CELL;
142 enum {
143 NUM_SRC_FIELDS = sizeof(src_locations) / sizeof(src_locations[0])};
144 int num_links_per_field[NUM_SRC_FIELDS] = {num_links};
145 int * tgt_id_fixed = NULL;
146 size_t num_fixed_tgt = 0;
147 double * fixed_values = NULL;
148 int * num_tgt_per_fixed_value = NULL;
149 size_t num_fixed_values = 0;
150
152 weight_file_empty, src_indices, tgt_indices, weights, num_links,
153 src_locations, NUM_SRC_FIELDS, num_links_per_field, tgt_id_fixed,
154 num_fixed_tgt, fixed_values, num_tgt_per_fixed_value,
155 num_fixed_values, tgt_location, src_grid_name, tgt_grid_name);
156 }
157
158 { // file with fixed value
159 int * tgt_indices = NULL;
160 int * src_indices = NULL;
161 double * weights = NULL;
162 size_t num_links = 0;
163 enum yac_location src_locations[] = {YAC_LOC_CELL};
164 enum yac_location tgt_location = YAC_LOC_CELL;
165 enum {
166 NUM_SRC_FIELDS = sizeof(src_locations) / sizeof(src_locations[0])};
167 int num_links_per_field[NUM_SRC_FIELDS] = {num_links};
168 int tgt_id_fixed[] = {0, 1, 2, 3};
169 size_t num_fixed_tgt = 4;
170 double fixed_values[] = {999.0};
171 int num_tgt_per_fixed_value[] = {4};
172 size_t num_fixed_values = 1;
173
175 weight_file_fixed, src_indices, tgt_indices, weights, num_links,
176 src_locations, NUM_SRC_FIELDS, num_links_per_field, tgt_id_fixed,
177 num_fixed_tgt, fixed_values, num_tgt_per_fixed_value,
178 num_fixed_values, tgt_location, src_grid_name, tgt_grid_name);
179 }
180 }
181 double coordinates_x[] = {0.0, 1.0, 2.0};
182 double coordinates_y[] = {0.0, 1.0, 2.0};
183 size_t num_vertices[2] = {3,3};
184 int cyclic[2] = {0,0};
185
186 struct yac_basic_grid * src_grid =
189 struct yac_basic_grid * tgt_grid =
192
193 double cell_coords[4][3];
194 double cell_coords_x[] = {0.5, 1.5};
195 double cell_coords_y[] = {0.5, 1.5};
196 for (int i = 0, k = 0; i < 2; ++i)
197 for (int j = 0; j < 2; ++j, ++k)
198 LLtoXYZ_deg(cell_coords_x[j], cell_coords_y[i], cell_coords[k]);
201
202 struct yac_dist_grid_pair * grid_pair =
203 yac_dist_grid_pair_new(src_grid, tgt_grid, MPI_COMM_WORLD);
204
205 struct yac_interp_field src_fields[] =
206 {{.location = YAC_LOC_CELL, .coordinates_idx = 0, .masks_idx = SIZE_MAX}};
207 size_t num_src_fields = sizeof(src_fields) / sizeof(src_fields[0]);
209 {.location = YAC_LOC_CELL, .coordinates_idx = 0, .masks_idx = SIZE_MAX};
210
211 struct yac_interp_grid * interp_grid =
214
215 //----------------------------------------
216 // test generation of interpolation method
217 //----------------------------------------
218
219 struct interp_method * method_stack[] = {
220 yac_interp_method_file_new( // this method stops the stack
221 weight_file_empty, YAC_INTERP_FILE_MISSING_ERROR,
227 yac_interp_method_callback_new(utest_compute_weights_callback, NULL),
228 yac_interp_method_check_new( NULL, NULL, NULL, NULL),
237 weight_file_fixed, YAC_INTERP_FILE_MISSING_ERROR,
246 (struct yac_nnn_config) {
247 .type =
250 .max_search_distance = YAC_INTERP_NNN_MAX_SEARCH_DISTANCE_DEFAULT,
254 NULL};
255
256 //-----------------
257 // generate weights
258 //-----------------
259
260 struct yac_interp_weights * weights =
261 yac_interp_method_do_search(method_stack, interp_grid);
262
263 if (yac_interp_weights_get_interp_count(weights) != 0)
264 PUT_ERR(
265 "error in handling of interpolation_complete argument by do_search");
266
267 //---------------
268 // cleanup
269 //---------------
270
271 yac_interp_method_delete(method_stack);
273 yac_interp_grid_delete(interp_grid);
274 yac_dist_grid_pair_delete(grid_pair);
275 yac_basic_grid_delete(tgt_grid);
276 yac_basic_grid_delete(src_grid);
277
278 if (comm_rank == 0) {
279 unlink(weight_file_empty);
280 unlink(weight_file_fixed);
281 }
282 }
283
284 xt_finalize();
285
286 MPI_Finalize();
287
288 return TEST_EXIT_CODE;
289}
290
291static void utest_compute_weights_callback(
292 double const tgt_coords[3], int src_cell_id, size_t src_cell_idx,
293 int const ** global_results_points, double ** result_weights,
294 size_t * result_count, void * user_data) {
295
296 UNUSED(tgt_coords);
297 UNUSED(src_cell_id);
298 UNUSED(src_cell_idx);
299 UNUSED(global_results_points);
300 UNUSED(result_weights);
301 UNUSED(result_count);
303
304 PUT_ERR("ERROR: in handling interpolation_complete in do_search_callback");
305}
struct yac_basic_grid * yac_basic_grid_new(char const *name, struct yac_basic_grid_data grid_data)
Definition basic_grid.c:53
struct yac_basic_grid * yac_basic_grid_reg_2d_deg_new(char const *name, size_t nbr_vertices[2], int cyclic[2], double *lon_vertices, double *lat_vertices)
Definition basic_grid.c:338
size_t yac_basic_grid_add_coordinates(struct yac_basic_grid *grid, enum yac_location location, yac_coordinate_pointer coordinates, size_t count)
Definition basic_grid.c:235
void yac_basic_grid_delete(struct yac_basic_grid *grid)
Definition basic_grid.c:73
#define UNUSED(x)
Definition core.h:72
void yac_dist_grid_pair_delete(struct yac_dist_grid_pair *grid_pair)
Definition dist_grid.c:2315
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:2063
static void LLtoXYZ_deg(double lon, double lat, double p_out[])
Definition geometry.h:269
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:30
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_avg_new(enum yac_interp_avg_weight_type weight_type, int partial_coverage)
#define YAC_INTERP_AVG_PARTIAL_COVERAGE_DEFAULT
yac_interp_avg_weight_type
#define YAC_INTERP_AVG_WEIGHT_TYPE_DEFAULT
void * user_data
struct interp_method * yac_interp_method_callback_new(yac_func_compute_weights compute_weights_callback, void *user_data)
struct interp_method * yac_interp_method_check_new(func_constructor constructor_callback, void *constructor_user_data, func_do_search do_search_callback, void *do_search_user_data)
struct interp_method * yac_interp_method_conserv_new(int order, int enforced_conserv, int partial_coverage, enum yac_interp_method_conserv_normalisation normalisation)
#define YAC_INTERP_CONSERV_NORMALISATION_DEFAULT
#define YAC_INTERP_CONSERV_ENFORCED_CONSERV_DEFAULT
#define YAC_INTERP_CONSERV_PARTIAL_COVERAGE_DEFAULT
#define YAC_INTERP_CONSERV_ORDER_DEFAULT
yac_interp_method_conserv_normalisation
struct interp_method * yac_interp_method_creep_new(int creep_distance)
#define YAC_INTERP_CREEP_DISTANCE_DEFAULT
struct interp_method * yac_interp_method_file_new(char const *weight_file_name, enum yac_interp_file_on_missing_file on_missing_file, enum yac_interp_file_on_success on_success)
@ YAC_INTERP_FILE_MISSING_ERROR
abort on missing file
@ YAC_INTERP_FILE_SUCCESS_CONT
@ YAC_INTERP_FILE_SUCCESS_STOP
struct interp_method * yac_interp_method_fixed_new(double value)
#define YAC_INTERP_FIXED_VALUE_DEFAULT
struct interp_method * yac_interp_method_hcsbb_new()
struct interp_method * yac_interp_method_ncc_new(enum yac_interp_ncc_weight_type weight_type, int partial_coverage)
#define YAC_INTERP_NCC_PARTIAL_COVERAGE_DEFAULT
#define YAC_INTERP_NCC_WEIGHT_TYPE_DEFAULT
yac_interp_ncc_weight_type
struct interp_method * yac_interp_method_nnn_new(struct yac_nnn_config config)
#define YAC_INTERP_NNN_WEIGHTED_DEFAULT
yac_interp_nnn_weight_type
#define YAC_INTERP_NNN_MAX_SEARCH_DISTANCE_DEFAULT
#define YAC_INTERP_NNN_GAUSS_SCALE_DEFAULT
#define YAC_INTERP_NNN_N_DEFAULT
struct interp_method * yac_interp_method_spmap_new(struct yac_interp_spmap_config const *default_config, struct yac_spmap_overwrite_config const *const *overwrite_configs)
#define YAC_INTERP_SPMAP_OVERWRITE_DEFAULT
#define YAC_INTERP_SPMAP_DEFAULT_CONFIG
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, int is_source, int is_target)
void yac_interp_weights_delete(struct yac_interp_weights *weights)
size_t yac_interp_weights_get_interp_count(struct yac_interp_weights *weights)
@ YAC_MAPPING_ON_TGT
weights will be applied at target processes
@ YAC_MAPPING_ON_SRC
weights will be applied at source processes
void yac_interpolation_delete(struct yac_interpolation *interp)
Free an interpolation object and release all resources.
double const YAC_FRAC_MASK_NO_VALUE
yac_location
Definition location.h:12
@ YAC_LOC_CELL
Definition location.h:14
struct yac_basic_grid_data yac_read_icon_basic_grid_data_parallel(const char *filename, MPI_Comm comm)
enum yac_location location
Definition basic_grid.h:16
struct yac_interp_field tgt_field
Definition interp_grid.c:25
size_t num_src_fields
Definition interp_grid.c:26
struct yac_dist_grid_pair * grid_pair
Definition interp_grid.c:24
struct yac_interp_field src_fields[]
Definition interp_grid.c:27
void set_even_io_rank_list(MPI_Comm comm)
double * data
char const src_grid_name[]
char const tgt_grid_name[]
double coordinates_x[]
double coordinates_y[]
double cell_coords[36][3]
unsigned cyclic[2]
#define TEST_EXIT_CODE
Definition tests.h:14
#define PUT_ERR(string)
Definition tests.h:10
void write_weight_file(char const *file_name, int const *src_id, int const *tgt_id, double const *weights, unsigned num_links, enum yac_location const *src_locations, unsigned num_src_fields, int const *num_links_per_src_field, int *tgt_id_fixed, unsigned num_fixed_tgt, double *fixed_values, int *num_tgt_per_fixed_value, unsigned num_fixed_values, enum yac_location tgt_location, char const *src_grid_name, char const *tgt_grid_name)
struct yac_basic_grid ** grids
Definition yac.c:155