YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
interp_method_config.c
Go to the documentation of this file.
1// Copyright (c) 2026 The YAC Authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#include <limits.h>
6#include <stdlib.h>
7#include <string.h>
8
9#include "utils_common.h"
10#include "parameter/param.h"
12#include "yac_mpi_internal.h"
13
16#include "interp_method_avg.h"
17#include "interp_method_check.h"
19#include "interp_method_creep.h"
20#include "interp_method_ncc.h"
21#include "interp_method_nnn.h"
22#include "interp_method_dnn.h"
23#include "interp_method_fixed.h"
24#include "interp_method_file.h"
25#include "interp_method_hcsbb.h"
26#include "interp_method_spmap.h"
28#include "parameter/param.h"
29#include "yac_assert.h"
30#include "utils_common.h"
31
32typedef struct yac_interp_method_config *(*interp_method_default_ctor)(void);
33
49
51 interp_method_name_ctor_map,
69 DEF_NAME_TYPE_PAIR(source_to_target_map, YAC_SOURCE_TO_TARGET_MAP),
71
72#define YAC_ASSERT_VTABLE_FUNC(CONFIG, FUNC) \
73 YAC_ASSERT_FUNC( \
74 (CONFIG)->vtable, "yac_interp_method_config_" #FUNC, "vtable is NULL"); \
75 YAC_ASSERT_FUNC( \
76 (CONFIG)->vtable->FUNC, "yac_interp_method_config_" #FUNC, \
77 #FUNC " function pointer is NULL")
78
80 if (config) {
81 if (config->root_param_cache != NULL) {
84 }
87 }
88}
89
91 const struct yac_interp_method_config *config) {
92 struct yac_interp_method_config * copy = NULL;
93 if (config) {
95 copy = config->vtable->copy(config);
96 }
97 return copy;
98}
99
101 void const *a, void const *b) {
102 UNUSED(a);
103 UNUSED(b);
104 return 0;
105}
106
108 const struct yac_interp_method_config *a,
109 const struct yac_interp_method_config *b) {
112 int (*compare)(void const *, void const *) =
114 if (a) {
115 YAC_ASSERT_VTABLE_FUNC(a, get_type);
116 YAC_ASSERT_VTABLE_FUNC(a, compare);
117 a_type = a->vtable->get_type();
118 compare = a->vtable->compare;
119 }
120 if (b) {
121 YAC_ASSERT_VTABLE_FUNC(b, get_type);
122 b_type = b->vtable->get_type();
123 }
124 if (a_type != b_type) {
125 return ((a_type > b_type) - (a_type < b_type));
126 }
127 return compare(a, b);
128}
129
131 const struct yac_interp_method_config *config, MPI_Comm comm) {
132
133 int type_pack_size = 0;
134 yac_mpi_call(MPI_Pack_size(1, MPI_INT, comm, &type_pack_size), comm);
135 size_t pack_size = (size_t)type_pack_size;
136
137 if (config != NULL) {
138
140 YAC_ASSERT(root_param != NULL, "failed to get method parameter tree");
141
142 pack_size += yac_param_get_pack_size(root_param, comm, __func__);
143 }
144
145 return pack_size;
146}
147
149 const struct yac_interp_method_config *config,
150 void *buffer, int buffer_size, int *position, MPI_Comm comm) {
151
152 int type = YAC_UNDEFINED;
153 if (config != NULL) {
155 type = (int)config->vtable->get_type();
156 }
158 MPI_Pack(&type, 1, MPI_INT, buffer, buffer_size, position, comm), comm);
159
160 if (config == NULL) return;
161
163 YAC_ASSERT(root_param != NULL, "failed to get method parameter tree");
164
165 yac_param_pack(root_param, buffer, buffer_size, position, comm, __func__);
166}
167
169 void *buffer, int buffer_size, int *position, MPI_Comm comm) {
170
171 int type = YAC_UNDEFINED;
173 MPI_Unpack(buffer, buffer_size, position, &type, 1, MPI_INT, comm), comm);
174 if (type == YAC_UNDEFINED) return NULL;
175
176 char const * method_name =
178 interp_method_name_ctor_map, interp_method_name_ctor_map_size, type);
179 YAC_ASSERT_F(method_name != NULL, "invalid method type %d", type);
180
183
185 YAC_ASSERT_F(root_param != NULL, "failed to get method parameter tree");
186
187 yac_param_unpack(root_param, buffer, buffer_size, position, comm, __func__);
188
189 return config;
190}
191
202
203struct yac_param;
205 struct yac_interp_method_config const *config) {
206 struct yac_param *param = NULL;
207 if (config) {
208 if (config->root_param_cache == NULL) {
209 YAC_ASSERT_VTABLE_FUNC(config, get_param);
210 ((struct yac_interp_method_config *)config)->root_param_cache =
212 }
213 param = config->root_param_cache;
214 }
215 return param;
216}
217
219 char const *method_name) {
220
221 YAC_ASSERT(method_name != NULL, "method_name is NULL");
222
223 int type_int =
225 interp_method_name_ctor_map, interp_method_name_ctor_map_size,
226 method_name);
228 type_int != INT_MAX, "unknown interpolation method name \"%s\"",
229 method_name);
230
233 type != YAC_UNDEFINED, "method name \"%s\" maps to YAC_UNDEFINED",
234 method_name);
235
237 type < YAC_LAST, "method name \"%s\" maps to invalid type %d",
238 method_name, type);
239
242 ctor != NULL, "method name \"%s\" maps to NULL constructor", method_name);
243
244 struct yac_interp_method_config * config = ctor();
245
246 return config;
247}
248
250 const struct yac_interp_method_config *config) {
251 struct interp_method * method = NULL;
252 if (config) {
254 method = config->vtable->generate(config);
255 }
256 return method;
257}
258
259//------------------------------------------------------------------------------
260// User-oriented parameter access convenience functions
261//------------------------------------------------------------------------------
262
263// Maximum length of a single path component (incl. NUL terminator).
264#define YAC_PARAM_PATH_COMPONENT_MAX 128
265
270static int parse_index(char const * str, size_t * idx) {
271 if (!str || !*str) return 0;
272 for (char const * p = str; *p; ++p)
273 if (*p < '0' || *p > '9') return 0;
274 unsigned long v = strtoul(str, NULL, 10);
275 *idx = (size_t)v;
276 return 1;
277}
278
290 struct yac_param * root, char const * path, char const * routine) {
291
292 struct yac_param * current = root;
293
294 // Walk the dot-separated path component by component without modifying the
295 // original string (strtok would modify it).
296 char const * seg_start = path;
297 while (1) {
298 char const * dot = strchr(seg_start, '.');
299 size_t seg_len = dot ? (size_t)(dot - seg_start) : strlen(seg_start);
300
302 seg_len > 0 && seg_len < YAC_PARAM_PATH_COMPONENT_MAX, routine,
303 "invalid path component length in path \"%s\"", path);
304
305 char component[YAC_PARAM_PATH_COMPONENT_MAX];
306 memcpy(component, seg_start, seg_len);
307 component[seg_len] = '\0';
308
309 struct yac_param * sub = NULL;
310 size_t idx;
311 if (parse_index(component, &idx)) {
312 int ierr = yac_param_get_sub_by_index(current, idx, &sub);
314 ierr == 0, routine,
315 "failed to navigate to index %zu in path \"%s\": %s",
316 idx, path, yac_param_get_last_error());
317 } else {
318 int ierr = yac_param_get_sub_by_name(current, component, &sub);
320 ierr == 0, routine,
321 "failed to navigate to \"%s\" in path \"%s\": %s",
322 component, path, yac_param_get_last_error());
323 }
324 current = sub;
325
326 if (!dot) break;
327 seg_start = dot + 1;
328 }
329 return current;
330}
331
334 char const *path, char const *value) {
335
336 YAC_ASSERT(config != NULL, "config is NULL");
337 YAC_ASSERT(path != NULL, "path is NULL");
338 YAC_ASSERT(value != NULL, "value is NULL");
339
341 YAC_ASSERT(root != NULL, "failed to get parameter tree");
342
343 struct yac_param * target =
344 navigate_param_path(root, path, "yac_interp_method_config_set_param_str");
345 int ierr = yac_param_set_scalar(target, value);
347 ierr == 0, "failed to set \"%s\" to \"%s\": %s",
349}
350
353 char const *path, int value) {
354
355 YAC_ASSERT(config != NULL, "config is NULL");
356 YAC_ASSERT(path != NULL, "path is NULL");
357
359 YAC_ASSERT(root != NULL, "failed to get parameter tree");
360
361 struct yac_param * target =
362 navigate_param_path(root, path, "yac_interp_method_config_set_param_int");
363 int ierr = yac_param_set_scalar_int(target, value);
365 ierr == 0, "failed to set \"%s\" to %d: %s",
367}
368
371 char const *path, double value) {
372
373 YAC_ASSERT(config != NULL, "config is NULL");
374 YAC_ASSERT(path != NULL, "path is NULL");
375
377 YAC_ASSERT(root != NULL, "failed to get parameter tree");
378
379 struct yac_param * target =
380 navigate_param_path(root, path, "yac_interp_method_config_set_param_dble");
381 int ierr = yac_param_set_scalar_dble(target, value);
383 ierr == 0, "failed to set \"%s\": %s", path, yac_param_get_last_error());
384}
385
387 struct yac_interp_method_config const *config,
388 char const *path) {
389
390 YAC_ASSERT(config != NULL, "config is NULL");
391 YAC_ASSERT(path != NULL, "path is NULL");
392
394 YAC_ASSERT(root != NULL, "failed to get parameter tree");
395
396 struct yac_param * target =
397 navigate_param_path(root, path, "yac_interp_method_config_get_param_int");
398 int value = INT_MAX;
399 int ierr = yac_param_get_scalar_int(target, &value);
401 ierr == 0, "failed to get \"%s\" as int: %s",
403
404 return value;
405}
406
408 struct yac_interp_method_config const *config,
409 char const *path) {
410
411 YAC_ASSERT(config != NULL, "config is NULL");
412 YAC_ASSERT(path != NULL, "path is NULL");
413
415 YAC_ASSERT(root != NULL, "failed to get parameter tree");
416
417 struct yac_param * target =
418 navigate_param_path(root, path, "yac_interp_method_config_get_param_dble");
419 double value = 0.0;
420 int ierr = yac_param_get_scalar_dble(target, &value);
422 ierr == 0, "failed to get \"%s\" as double: %s",
424
425 return value;
426}
427
429 struct yac_interp_method_config const *config,
430 char const *path) {
431
432 YAC_ASSERT(config != NULL, "config is NULL");
433 YAC_ASSERT(path != NULL, "path is NULL");
434
436 YAC_ASSERT(root != NULL, "failed to get parameter tree");
437
438 struct yac_param * target =
439 navigate_param_path(root, path, "yac_interp_method_config_get_param_str");
440
441 // Use a buffer large enough for any reasonable string parameter value.
442 char buf[4096];
443 int ierr = yac_param_get_scalar(target, buf, sizeof(buf));
445 ierr == 0, "failed to get \"%s\" as string: %s",
447
448
449 return xstrdup(buf);
450}
451
453 struct yac_interp_method_config const *config,
454 char const *path,
455 char *buf,
456 int buflen) {
457
458 YAC_ASSERT(config != NULL, "config is NULL");
459 YAC_ASSERT(path != NULL, "path is NULL");
460 YAC_ASSERT(buf != NULL, "buf is NULL");
461 YAC_ASSERT(buflen > 0, "buflen must be positive");
462
464 strncpy(buf, str, (size_t)buflen);
465 buf[buflen - 1] = '\0';
466 free(str);
467}
468
470 struct yac_interp_method_config const *config,
471 char const *path) {
472
473 YAC_ASSERT(config != NULL, "config is NULL");
474 YAC_ASSERT(path != NULL, "path is NULL");
475
477 YAC_ASSERT(root != NULL, "failed to get parameter tree");
478
479 struct yac_param * target =
480 navigate_param_path(root, path, "yac_interp_method_config_get_list_size");
481 size_t count = 0;
482 int ierr = yac_param_get_sub_count(target, &count);
484 ierr == 0, "failed to get list size at \"%s\": %s",
486
487 return count;
488}
489
492 char const *path) {
493
494 YAC_ASSERT(config != NULL, "config is NULL");
495 YAC_ASSERT(path != NULL, "path is NULL");
496
498 YAC_ASSERT(root != NULL, "failed to get parameter tree");
499
500 struct yac_param * list_node =
501 navigate_param_path(root, path, "yac_interp_method_config_append_list_element");
502 struct yac_param * new_elem = NULL;
503 int ierr = yac_param_append(list_node, &new_elem);
505 ierr == 0, "failed to append element to list at \"%s\": %s",
507
508 size_t count = 0;
509 ierr = yac_param_get_sub_count(list_node, &count);
511 ierr == 0, "failed to get list size after append at \"%s\": %s",
513
514 return count - 1;
515}
#define YAC_ASSERT(exp, msg)
#define UNUSED(x)
Definition core.h:72
int yac_name_type_pair_get_type(struct yac_name_type_pair const *pairs, size_t count, char const *name)
Definition utils_core.c:23
#define DEF_NAME_TYPE_PAIR(NAME, TYPE)
char const * yac_name_type_pair_get_name(struct yac_name_type_pair const *pairs, size_t count, int type)
Definition utils_core.c:14
#define DEF_NAME_TYPE_PAIRS(NAME,...)
yac_interpolation_list
@ YAC_N_NEAREST_NEIGHBOR
N-nearest-neighbor interpolation.
@ YAC_CREEP
Creep-fill interpolation.
@ YAC_USER_FILE
User-supplied weights from file.
@ YAC_LAST
Sentinel value for the end of the list.
@ YAC_USER_CALLBACK
@ YAC_SOURCE_TO_TARGET_MAP
Source-to-target mapping (inverse N-nearest-neighbor)
@ YAC_CONSERVATIVE
Conservative remapping (area/flux conserving)
@ YAC_FIXED_VALUE
Assigns a fixed value to all targets.
@ YAC_UNDEFINED
Undefined interpolation method.
@ YAC_CHECK
Diagnostic check method (for testing)
@ YAC_BERNSTEIN_BEZIER
Bernstein-Bezier interpolation.
@ YAC_DISTANCE_NEAREST_NEIGHBOR
Distance-nearest-neighbor interpolation.
@ YAC_NEAREST_CORNER_CELLS
Nearest-corner-cell interpolation.
@ YAC_RADIAL_BASIS_FUNCTION
Radial basis function interpolation.
@ YAC_AVERAGE
Simple averaging (or linear) interpolation.
struct yac_interp_method_config * yac_interp_method_config_default_avg_new(void)
Creates an average interpolation method configuration with default parameters.
yac_func_compute_weights callback
struct yac_interp_method_config * yac_interp_method_config_default_callback_new(void)
Creates a callback interpolation method configuration with default parameters.
enum callback_type type
struct @23::@24 value
struct yac_interp_method_config * yac_interp_method_config_default_check_new(void)
Creates a check interpolation method configuration with default parameters.
int yac_interp_method_config_compare(const struct yac_interp_method_config *a, const struct yac_interp_method_config *b)
#define YAC_PARAM_PATH_COMPONENT_MAX
void yac_interp_method_config_set_param_int(struct yac_interp_method_config *config, char const *path, int value)
struct yac_interp_method_config *(* interp_method_default_ctor)(void)
void yac_interp_method_config_get_param_str_f2c(struct yac_interp_method_config const *config, char const *path, char *buf, int buflen)
static int yac_interp_method_config_undefined_compare(void const *a, void const *b)
struct yac_param * yac_interp_method_config_get_param(struct yac_interp_method_config const *config)
static struct yac_param * navigate_param_path(struct yac_param *root, char const *path, char const *routine)
enum yac_interpolation_list yac_interp_method_config_get_type(struct yac_interp_method_config const *config)
double yac_interp_method_config_get_param_dble(struct yac_interp_method_config const *config, char const *path)
size_t yac_interp_method_config_append_list_element(struct yac_interp_method_config *config, char const *path)
int yac_interp_method_config_get_param_int(struct yac_interp_method_config const *config, char const *path)
void yac_interp_method_config_pack(const struct yac_interp_method_config *config, void *buffer, int buffer_size, int *position, MPI_Comm comm)
size_t yac_interp_method_config_get_pack_size(const struct yac_interp_method_config *config, MPI_Comm comm)
struct yac_interp_method_config * yac_interp_method_config_copy(const struct yac_interp_method_config *config)
static interp_method_default_ctor const interp_method_default_ctors[]
static int parse_index(char const *str, size_t *idx)
#define YAC_ASSERT_VTABLE_FUNC(CONFIG, FUNC)
void yac_interp_method_config_set_param_dble(struct yac_interp_method_config *config, char const *path, double value)
char * yac_interp_method_config_get_param_str(struct yac_interp_method_config const *config, char const *path)
struct yac_interp_method_config * yac_interp_method_config_unpack(void *buffer, int buffer_size, int *position, MPI_Comm comm)
struct yac_interp_method_config * yac_interp_method_config_default_from_name_new(char const *method_name)
struct interp_method * yac_interp_method_config_generate(const struct yac_interp_method_config *config)
void yac_interp_method_config_set_param_str(struct yac_interp_method_config *config, char const *path, char const *value)
size_t yac_interp_method_config_get_list_size(struct yac_interp_method_config const *config, char const *path)
Defines the interface for interpolation method configuration "base class" in YAC.
void yac_interp_method_config_delete(struct yac_interp_method_config *config)
struct yac_interp_method_config * yac_interp_method_config_default_conserv_new(void)
Creates a conservative interpolation method configuration with default parameters.
struct yac_interp_method_config * yac_interp_method_config_default_creep_new(void)
Creates a creep-fill interpolation method configuration with default parameters.
struct yac_interp_method_config * yac_interp_method_config_default_dnn_new(void)
Creates a distance-nearest-neighbour (DNN) interpolation method configuration with default parameters...
struct yac_interp_method_config * yac_interp_method_config_default_file_new(void)
Creates a file-based interpolation method configuration with default parameters.
struct yac_interp_method_config * yac_interp_method_config_default_fixed_new(void)
Creates a fixed-value interpolation method configuration with default parameters.
struct yac_interp_method_config * yac_interp_method_config_default_hcsbb_new(void)
Creates an HCSBB interpolation method configuration with default parameters.
struct yac_interp_method_config * yac_interp_method_config_default_ncc_new(void)
Creates a nearest-corner-cells interpolation method configuration with default parameters.
struct yac_interp_method_config * yac_interp_method_config_default_nnn_new(void)
Creates a nearest-neighbour interpolation method configuration with default parameters.
struct yac_interp_method_config * yac_interp_method_config_default_rbf_new(void)
Creates a radial-basis-function interpolation method configuration with default parameters.
struct yac_interp_method_config * yac_interp_method_config_default_spmap_new(void)
Creates a source-point-mapping interpolation method configuration with default parameters.
int yac_param_get_scalar_dble(const struct yac_param *param, double *value)
Get the value of a scalar parameter as a double.
Definition param.c:174
void yac_param_delete(struct yac_param *param)
Definition param.c:304
int yac_param_get_sub_by_index(struct yac_param *param, size_t idx, struct yac_param **sub_param)
Get a handle to a subparameter by index.
Definition param.c:89
int yac_param_get_scalar(const struct yac_param *param, char *buf, size_t buflen)
Get the value of a scalar parameter as a string.
Definition param.c:123
int yac_param_set_scalar(struct yac_param *param, const char *value_str)
Set the value of a scalar parameter from a string.
Definition param.c:113
int yac_param_append(struct yac_param *param, struct yac_param **sub_param)
Append a new default subparameter to a list parameter and return a handle to it.
Definition param.c:102
int yac_param_set_scalar_int(struct yac_param *param, int value)
Set the value of a scalar parameter as an int.
Definition param.c:147
int yac_param_get_sub_count(const struct yac_param *param, size_t *count)
Get the number of subparameters.
Definition param.c:61
int yac_param_set_scalar_dble(struct yac_param *param, double value)
Set the value of a scalar parameter as a double.
Definition param.c:156
const char * yac_param_get_last_error(void)
Retrieve the last error message for the calling thread.
Definition param.c:298
int yac_param_get_scalar_int(const struct yac_param *param, int *value)
Get the value of a scalar parameter as an int.
Definition param.c:165
int yac_param_get_sub_by_name(struct yac_param *param, const char *name, struct yac_param **sub_param)
Get a handle to a subparameter by name.
Definition param.c:75
Public interface for YAC parameters.
void yac_param_pack(struct yac_param *param, void *buffer, int buffer_size, int *position, MPI_Comm comm, char const *routine)
Public function to pack a parameter record into a buffer.
size_t yac_param_get_pack_size(struct yac_param *param, MPI_Comm comm, char const *routine)
Public function to get the size in bytes required to pack a parameter record.
void yac_param_unpack(struct yac_param *param, void const *buffer, int buffer_size, int *position, MPI_Comm comm, char const *routine)
Unpack a parameter from an MPI buffer and apply its values to the given parameter.
#define xstrdup(s)
Definition ppm_xfuncs.h:84
int(* compare)(void const *a, void const *b)
struct yac_interp_method_config *(* copy)(struct yac_interp_method_config const *config)
struct yac_param *(* get_param)(struct yac_interp_method_config const *config)
enum yac_interpolation_list(* get_type)(void)
void(* delete)(struct yac_interp_method_config *config)
struct interp_method *(* generate)(struct yac_interp_method_config const *config)
struct yac_interp_method_config_vtable const * vtable
static struct yac_interp_method_config * config
double * buffer
double(* p)(double lon, double lat)
Definition toy_scrip.c:119
#define YAC_ASSERT_F(exp, format,...)
Definition yac_assert.h:39
#define YAC_ASSERT_F_FUNC(exp, func, format,...)
Definition yac_assert.h:42
#define yac_mpi_call(call, comm)