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