YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
interp_method_utils.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#ifdef HAVE_CONFIG_H
6// Get the definition of the 'restrict' keyword.
7#include "config.h"
8#endif
9
10#include <math.h>
11
13#include "geometry.h"
14#include "yac_assert.h"
16
17#define GAUSS_WEIGHT_SUM_TOL (1e-9)
18
20 double tgt_coord[3], yac_const_coordinate_pointer src_coords, size_t const n,
21 double * weights, double const dummy) {
22
23 UNUSED(tgt_coord);
24 UNUSED(src_coords);
25 UNUSED(dummy);
26
27 double weight = 1.0 / (double)n;
28 for (size_t i = 0; i < n; ++i) weights[i] = weight;
29}
30
32 double tgt_coord[3], yac_const_coordinate_pointer src_coords, size_t const n,
33 double * weights, double const dummy) {
34
35 UNUSED(dummy);
36
37 for (size_t i = 0; i < n; ++i) {
38
39 double distance = get_vector_angle(tgt_coord, (double*)(src_coords[i]));
40
41 // if the target and source point are nearly identical
42 if (distance < yac_angle_tol) {
43 for (size_t j = 0; j < n; ++j) weights[j] = 0.0;
44 weights[i] = 1.0;
45 return;
46 }
47
48 weights[i] = 1.0 / distance;
49 }
50
51 // compute scaling factor for the weights
52 double inv_distance_sum = 0.0;
53 for (size_t i = 0; i < n; ++i) inv_distance_sum += weights[i];
54 double scale = 1.0 / inv_distance_sum;
55
56 for (size_t i = 0; i < n; ++i) weights[i] *= scale;
57}
58
60 double tgt_coord[3], yac_const_coordinate_pointer src_coords, size_t const n,
61 double * weights, double const gauss_scale) {
62
63 // return early if there is only a single source point
64 if (n == 1) {
65 weights[0] = 1.0;
66 return;
67 }
68
69 for (size_t i = 0; i < n; ++i) {
70
71 double distance = get_vector_angle(tgt_coord, src_coords[i]);
72
73 weights[i] = distance * distance;
74 }
75
76 // a) compute sum of source point distances
77 double src_distances_sum = 0.0;
78 double src_distances_count = 0.5 * (double)(n * n - n);
79 for (size_t i = 0; i < n-1; ++i)
80 for (size_t j = i + 1; j < n; ++j)
81 src_distances_sum += get_vector_angle(src_coords[j], src_coords[i]);
82
84 (n == 1) || (src_distances_sum > yac_angle_tol),
85 "all %zu source points for target point (tgt_coord: %.3lf; %.3lf; %.3lf) "
86 "are nearly identical (src_coords[0]: %.3lf; %.3lf; %.3lf)",
87 n, tgt_coord[0], tgt_coord[1], tgt_coord[2],
88 src_coords[0][0], src_coords[0][1], src_coords[0][2]);
89
90 // b) C = -1 / (c * d_mean^2)
91 double src_distances_mean = src_distances_sum / src_distances_count;
92 double scale = -1.0 / (gauss_scale * src_distances_mean * src_distances_mean);
93
94 // c) calculate weights
95 // w_i = e^(-d_i^2/(c*s^2))
96 // w_i = e^(C * d_i^2)
97 double weights_sum = 0.0;
98 for (size_t i = 0; i < n; ++i) {
99 weights[i] = exp(scale * weights[i]);
100 weights_sum += weights[i];
101 }
102
103 // If the sum of the weights is very low, which can happen in case
104 // the target point is very far away from the group of source points.
105 if (fabs(weights_sum) < GAUSS_WEIGHT_SUM_TOL) {
106
107 // Due to limited accuracy the exact contribution of each source
108 // point cannot be computed. Therefore, the normalisation would
109 // generate NaN's. Hence we fall back to inverse distance weighted
110 // average for this target point.
112 tgt_coord, src_coords, n, weights, gauss_scale);
113 return;
114 }
115
116 // d) scale weights such that SUM(w_i) == 1
117 for (size_t i = 0; i < n; ++i) weights[i] /= weights_sum;
118}
119
120static void inverse(double * A, size_t n) {
121
122// LAPACKE_dsytrf_work and LAPACKE_dsytri might not be available (see yac_lapack_interface.h).
123#ifdef YAC_LAPACK_NO_DSYTR
124 lapack_int ipiv[n+1];
125 double work[n*n];
126
127 for (size_t i = 0; i < n+1; ++i) ipiv[i] = 0;
128 for (size_t i = 0; i < n*n; ++i) work[i] = 0;
129
131 !LAPACKE_dgetrf(
132 LAPACK_COL_MAJOR, (lapack_int) n, (lapack_int) n,
133 A, (lapack_int) n, ipiv), "internal ERROR: dgetrf")
134
136 !LAPACKE_dgetri_work(
137 LAPACK_COL_MAJOR, (lapack_int) n, A, (lapack_int) n,
138 ipiv, work, (lapack_int) (n * n)), "internal ERROR: dgetri")
139#else
140 lapack_int ipiv[n];
141 double work[n];
142
144 !LAPACKE_dsytrf_work(
145 LAPACK_COL_MAJOR, 'L', (lapack_int) n , A,
146 (lapack_int) n, ipiv, work, (lapack_int) n), "internal ERROR: dsytrf")
147
149 !LAPACKE_dsytri_work(
150 LAPACK_COL_MAJOR, 'L', (lapack_int) n , A,
151 (lapack_int) n, ipiv, work), "internal ERROR: dsytri")
152
153 for (size_t i = 0; i < n; ++i)
154 for (size_t j = i + 1; j < n; ++j)
155 A[j*n+i] = A[i*n+j];
156#endif
157}
158
160 double tgt_coord[3], yac_const_coordinate_pointer src_coords, size_t const n,
161 double * weights, double const rbf_scale) {
162
163 double A[n][n];
164 double a[n];
165
166 double sum_d = 0.0, scale_d = 1.0;
167
168 // compute distance matrix for all found source points
169 for (size_t i = 0; i < n; ++i) A[i][i] = 0.0;
170 for (size_t i = 0; i < n - 1; ++i) {
171 for (size_t j = i + 1; j < n; ++j) {
172 double d = get_vector_angle(src_coords[i], src_coords[j]);
173 A[i][j] = d;
174 A[j][i] = d;
175 sum_d += d;
176 }
177 }
178
179 // compute and apply scale factor for distance matrix
180 if (sum_d > 0.0)
181 scale_d = ((double)((n - 1) * n)) / (2.0 * sum_d);
182 scale_d /= rbf_scale;
183
184 double sq_scale_d = scale_d * scale_d;
185
186 // compute matrix A[n][n]
187 // with A = rbf(A)
188 // rbf(a) => Radial basis function
189 for (size_t i = 0; i < n; ++i) {
190 for (size_t j = 0; j < n; ++j) {
191 double d = A[i][j];
192 // gauß rbf kernel
193 A[i][j] = exp(-1.0 * d * d * sq_scale_d);
194 }
195 }
196
197 // compute inverse of A
198 inverse(&A[0][0], n);
199
200 // compute a[NUM_NEIGH]
201 // with a_i = rbf(d(x_i, y))
202 // x => vector containing the coordinates of the
203 // n nearest neighbours of the current
204 // target point
205 // y => coordinates of target point
206 // d(a, b) => great circle distance between point a and b
207 // rbf(a) => Radial basis function
208 for (size_t i = 0; i < n; ++i) {
209 double d = get_vector_angle(tgt_coord, src_coords[i]);
210 // gauß rbf kernel
211 a[i] = exp(-1.0 * d * d * sq_scale_d);
212 }
213
214 // compute weights
215 // with w_i = SUM(A_inv[i][j]*a[j]) // j = 0..n-1
216 for (size_t i = 0; i < n; ++i) {
217 weights[i] = 0.0;
218 for (size_t j = 0; j < n; ++j) weights[i] += A[i][j] * a[j];
219 }
220}
#define YAC_ASSERT(exp, msg)
#define UNUSED(x)
Definition core.h:72
#define yac_angle_tol
Definition geometry.h:26
static double get_vector_angle(double const a[3], double const b[3])
Definition geometry.h:472
void yac_interp_compute_weights_gauss(double tgt_coord[3], yac_const_coordinate_pointer src_coords, size_t const n, double *weights, double const gauss_scale)
#define GAUSS_WEIGHT_SUM_TOL
void yac_interp_compute_weights_avg(double tgt_coord[3], yac_const_coordinate_pointer src_coords, size_t const n, double *weights, double const dummy)
void yac_interp_compute_weights_dist(double tgt_coord[3], yac_const_coordinate_pointer src_coords, size_t const n, double *weights, double const dummy)
void yac_interp_compute_weights_rbf(double tgt_coord[3], yac_const_coordinate_pointer src_coords, size_t const n, double *weights, double const rbf_scale)
static void inverse(double *A, size_t n)
#define YAC_ASSERT_F(exp, format,...)
Definition yac_assert.h:39
double const (* yac_const_coordinate_pointer)[3]
Definition yac_types.h:22