|
| 1 | +import numpy as np |
| 2 | + |
| 3 | + |
| 4 | +class RBFInterpolator: |
| 5 | + |
| 6 | + def __init__(self, z, x, y, hx, hy): |
| 7 | + """ |
| 8 | + Initializes the RBF Interpolator. |
| 9 | +
|
| 10 | + :param z: 2D array of values at the grid points. |
| 11 | + :param x: x-coordinate of the point to interpolate. |
| 12 | + :param y: y-coordinate of the point to interpolate. |
| 13 | + :param hx: Grid spacing in the x-direction. |
| 14 | + :param hy: Grid spacing in the y-direction. |
| 15 | + """ |
| 16 | + |
| 17 | + self._z = z |
| 18 | + self._x = x |
| 19 | + self._y = y |
| 20 | + self._hx = hx |
| 21 | + self._hy = hy |
| 22 | + self._nx, self._ny = z.shape |
| 23 | + |
| 24 | + def _get_coordinates(self): |
| 25 | + """ |
| 26 | + Determines the x and y coordinates of the bottom-left corner of the grid cell |
| 27 | +
|
| 28 | + :return: A tuple containing the coordinates and its corresponding indices |
| 29 | + """ |
| 30 | + |
| 31 | + # gets the grid steps to x |
| 32 | + i_minus_star = int(np.floor(self._x / self._hx)) |
| 33 | + if i_minus_star > self._nx - 1: |
| 34 | + raise Exception("x is out of bounds") |
| 35 | + |
| 36 | + # final i index for interpolation |
| 37 | + i_minus = i_minus_star if i_minus_star < self._nx - 1 else self._nx - 1 |
| 38 | + |
| 39 | + # gets the grid steps to y |
| 40 | + j_minus_star = int(np.floor(self._y / self._hy)) |
| 41 | + if j_minus_star > self._ny - 1: |
| 42 | + raise Exception("y is out of bounds") |
| 43 | + |
| 44 | + # final j index for interpolation |
| 45 | + j_minus = j_minus_star if j_minus_star < self._ny - 1 else self._ny - 1 |
| 46 | + |
| 47 | + # computes the coordinates at the computed indices |
| 48 | + x_minus = i_minus * self._hx |
| 49 | + y_minus = j_minus * self._hy |
| 50 | + |
| 51 | + return x_minus, y_minus, i_minus, j_minus |
| 52 | + |
| 53 | + def _euclidean_distances(self, x_minus, y_minus): |
| 54 | + """ |
| 55 | + Calculates Euclidean distances between (x,y) and the surrounding grid points in the unit cell |
| 56 | +
|
| 57 | + :param x_minus: x-coordinate of the bottom-left corner of the grid |
| 58 | + :param y_minus: y-coordinate of the bottom-left corner of the grid |
| 59 | + :return: returns tuple with the Euclidean distances to the surrounding grid points: |
| 60 | + [bottom left, top left, bottom right, top right] |
| 61 | + """ |
| 62 | + |
| 63 | + bottom_left = np.sqrt((x_minus - self._x) ** 2 + (y_minus - self._y) ** 2) |
| 64 | + top_left = np.sqrt((x_minus - self._x) ** 2 + (y_minus + self._hy - self._y) ** 2) |
| 65 | + bottom_right = np.sqrt((x_minus + self._hx - self._x) ** 2 + (y_minus - self._y) ** 2) |
| 66 | + top_right = np.sqrt((x_minus + self._hx - self._x) ** 2 + (y_minus + self._hy - self._y) ** 2) |
| 67 | + |
| 68 | + return bottom_left, top_left, bottom_right, top_right |
| 69 | + |
| 70 | + @staticmethod |
| 71 | + def _rbf(d, gamma): |
| 72 | + """ |
| 73 | + Computes the Radial Basis Function (RBF) for a given distance and gamma |
| 74 | +
|
| 75 | + :param d: the Euclidean distance to a grid point |
| 76 | + :param gamma: gamma parameter |
| 77 | + :return: the RBF value for the distance d |
| 78 | + """ |
| 79 | + return np.exp(-gamma * d ** 2) |
| 80 | + |
| 81 | + def rbf_interpolate(self): |
| 82 | + """ |
| 83 | + Performs the Radial Basis function (RBF) interpolation for the point (x,y) |
| 84 | +
|
| 85 | + :return: the interpolated value at (x,y) |
| 86 | + """ |
| 87 | + |
| 88 | + x_minus, y_minus, i_minus, j_minus = self._get_coordinates() |
| 89 | + |
| 90 | + distances = self._euclidean_distances(x_minus, y_minus) |
| 91 | + |
| 92 | + h_diag_squared = self._hx ** 2 + self._hy ** 2 |
| 93 | + gamma = -np.log(0.005) / h_diag_squared |
| 94 | + |
| 95 | + rbf_weights = [self._rbf(d, gamma) for d in distances] |
| 96 | + |
| 97 | + sum_rbf = np.sum(rbf_weights) |
| 98 | + interpolated = rbf_weights[0] * self._z[i_minus, j_minus] |
| 99 | + interpolated += rbf_weights[1] * self._z[i_minus, j_minus + 1] |
| 100 | + interpolated += rbf_weights[2] * self._z[i_minus + 1, j_minus] |
| 101 | + interpolated += rbf_weights[3] * self._z[i_minus + 1, j_minus + 1] |
| 102 | + interpolated /= sum_rbf |
| 103 | + |
| 104 | + return interpolated |
0 commit comments