Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
87626d2
Add coordinate promolecular transformation (#15)
Ali-Tehrani May 29, 2020
fbe5355
Add promol cubic class and grid transform (#15)
Ali-Tehrani May 29, 2020
8b1e12b
Add promolecular density evaluation (#15)
Ali-Tehrani May 29, 2020
815096e
Add integration method to promol transform (#15)
Ali-Tehrani May 29, 2020
42efb07
Add docs to promolecular grid transform (#15)
Ali-Tehrani May 29, 2020
aebc519
Fix black complaints
Ali-Tehrani May 29, 2020
8e37b8c
Fix integration method for promol transform (#15)
Ali-Tehrani May 31, 2020
775466f
Add tests to promolecular grid transformation
Ali-Tehrani Jun 12, 2020
a79ee45
Add jacobian and steepest ascent to promol transf
Ali-Tehrani Jun 12, 2020
809f68c
Fix black issue
Ali-Tehrani Jun 12, 2020
50f9a73
Add promolecular density dataclass
Ali-Tehrani Jun 17, 2020
ad01358
Add docs to _root_equation for promol transform
Ali-Tehrani Jun 17, 2020
11b603a
Remove helper function in inverse_coordinate
Ali-Tehrani Jun 17, 2020
239858b
Change stepsize to number of points for input
Ali-Tehrani Jun 17, 2020
eb32897
Add mask to small/nan values for integrate promol
Ali-Tehrani Jun 17, 2020
40a0487
Rename coords to indices
Ali-Tehrani Jun 17, 2020
b8fc2b2
Add dynamic bracketing method to root eqn
Ali-Tehrani Jun 17, 2020
c104ac6
Add cubic grid support for protransform
Ali-Tehrani Jun 20, 2020
a35a07e
Change default [0, 1] to [-1, 1] for Promolecular
Ali-Tehrani Jun 20, 2020
ab7cd82
Add integration test for promolecular transform
Ali-Tehrani Jun 20, 2020
9e73400
Add Hessian and tests promolecular
Ali-Tehrani Jun 26, 2020
8727c0a
Add hessian and interpolation to grid transform
Ali-Tehrani Jul 6, 2020
f21aa78
Add test for hessian and interpolation promol
Ali-Tehrani Jul 6, 2020
a6f0a96
Add documentation to Promolecular
Ali-Tehrani Jul 7, 2020
4d56f35
Update setup.py to use dataclass in 3.6
Ali-Tehrani Aug 5, 2020
187c00e
Update integrand doc in promol
Ali-Tehrani Aug 5, 2020
7d420e4
Add parameter to doc of interpolation
Ali-Tehrani Aug 5, 2020
1f4a0e7
Merge branch 'master' of https://github.com/Ali-Tehrani/grid into tra…
Ali-Tehrani May 12, 2023
fd69714
Rename interpolation
Ali-Tehrani May 15, 2023
012f52b
Group pytest parameterize test in protrans
Ali-Tehrani May 15, 2023
717649d
Generalize Promol For HyperRectangle class
Ali-Tehrani May 15, 2023
9fc1a13
Remove doc in protransform test
Ali-Tehrani May 15, 2023
a5c9f8a
Change np.nan to infinity
Ali-Tehrani May 15, 2023
a6e60ba
Fix interpolate in cubic to not include boundary
Ali-Tehrani May 15, 2023
53fa689
Add grid_pts option to interpolate
Ali-Tehrani May 16, 2023
fa43c9d
Fix interpolate in cubic with log is vectorized
Ali-Tehrani May 16, 2023
0d75416
Add cut-off for promolecular transform
Ali-Tehrani May 16, 2023
3a48432
Add lower and upper bound to promolecular
Ali-Tehrani May 16, 2023
d52ec8b
Update interpolation in promolecular transform
Ali-Tehrani May 16, 2023
adacc37
Add error raise if couldn't solve for inverse
Ali-Tehrani May 16, 2023
791fb4a
Remove requirement for boundary in promoltransf
Ali-Tehrani May 16, 2023
7cf98c5
Update interpolation tests in promol transf
Ali-Tehrani May 16, 2023
a9f2a85
Merge branch 'master' into transformed_cube
Ali-Tehrani May 16, 2023
cc4625e
Remove TODO in promol transform
Ali-Tehrani May 16, 2023
435577e
Merge branch 'transformed_cube' of https://github.com/Ali-Tehrani/gri…
Ali-Tehrani May 16, 2023
cd0dc69
Merge remote-tracking branch 'origin/master' into transformed_cube
Ali-Tehrani Jun 4, 2025
e439e5f
Fix _get_bracket of Transformed Cube & black linter issues
FarnazH Jun 5, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies = [
"pytest>=8.0.0",
"scipy>=1.4",
"importlib_resources",
"dataclasses",
"sympy",
]
dynamic = ["version"]
Expand Down
55 changes: 45 additions & 10 deletions src/grid/cubic.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ def get_points_along_axes(self):
x = self.points[coords_x, 0]
return x, y

def interpolate(self, points, values, use_log=False, nu_x=0, nu_y=0, nu_z=0, method="cubic"):
r"""Interpolate function and its derivatives on cubic grid.
def interpolate(
self, points, values, use_log=False, nu_x=0, nu_y=0, nu_z=0, method="cubic", grid_pts=None
):
r"""Interpolate function value at a given point.

Only implemented in three-dimensions.

Expand Down Expand Up @@ -130,17 +132,33 @@ def interpolate(self, points, values, use_log=False, nu_x=0, nu_y=0, nu_z=0, met
If zero, then the function in z-direction is interpolated.
If greater than zero, then the "nu_z"th-order derivative in the z-direction is
interpolated.
method : str, optional
method: str, optional
The method of interpolation to perform. Supported are "cubic" (most accurate but
computationally expensive), "linear", or "nearest" (least accurate but cheap
computationally). The last two methods use SciPy's RegularGridInterpolator function.
grid_pts: list[OneDGrids], optional
If provided, then uses `grid_pts` rather than the points of the HyperRectangle class
`self.points` to construct interpolation. Useful when doing a promolecular
transformation.

Returns
-------
float :
The interpolation of a function (or of it's derivatives) at a :math:`M` point.

"""
# Needed because CubicProTransform is a subclass of this method and has its own
# interpolate function. Since interpolate references itself, it chooses
# CubicProTransform rather than _HyperRectangleGrid class.
return self._interpolate(
points, values, use_log, nu_x, nu_y, nu_z, method, grid_pts
)

def _interpolate(
self, points, values, use_log=False, nu_x=0, nu_y=0, nu_z=0, method="cubic", grid_pts=None
):
r"""Core of the Interpolate Algorithm."""

if method not in ["cubic", "linear", "nearest"]:
raise ValueError(
f"Argument method should be either cubic, linear, or nearest , got {method}"
Expand All @@ -154,6 +172,10 @@ def interpolate(self, points, values, use_log=False, nu_x=0, nu_y=0, nu_z=0, met
f"Number of function values {values.shape[0]} does not match number of "
f"grid points {np.prod(self.shape)}."
)
if grid_pts is not None and not isinstance(grid_pts, np.ndarray):
raise TypeError(
f"The grid points {type(grid_pts)} should have type None or numpy array."
)

if use_log:
values = np.log(values)
Expand All @@ -165,6 +187,10 @@ def interpolate(self, points, values, use_log=False, nu_x=0, nu_y=0, nu_z=0, met
interpolate = RegularGridInterpolator((x, y, z), values, method=method)
return interpolate(points)

# If grid_pts isn't specified, then use the grid stored as the class attribute.
if grid_pts is None:
grid_pts = self.points

# Interpolate the Z-Axis.
def z_spline(z, x_index, y_index, nu_z=nu_z):
# x_index, y_index is assumed to be in the grid while z is not assumed.
Expand All @@ -173,7 +199,7 @@ def z_spline(z, x_index, y_index, nu_z=nu_z):
small_index = self.coordinates_to_index((x_index, y_index, 1))
large_index = self.coordinates_to_index((x_index, y_index, self.shape[2] - 2))
val = CubicSpline(
self.points[small_index:large_index, 2],
grid_pts[small_index:large_index, 2],
values[small_index:large_index],
)(z, nu_z)
return val
Expand All @@ -183,7 +209,7 @@ def y_splines(y, x_index, z, nu_y=nu_y):
# The `1` and `self.num_puts[1] - 2` is needed because I don't want the boundary.
# Assumes x_index is in the grid while y, z may not be.
val = CubicSpline(
self.points[np.arange(1, self.shape[1] - 2) * self.shape[2], 1],
grid_pts[np.arange(1, self.shape[1] - 2) * self.shape[2], 1],
[z_spline(z, x_index, y_index, nu_z) for y_index in range(1, self.shape[1] - 2)],
)(y, nu_y)
# Trying to vectorize over z-axis and y-axis, this computes the interpolation for every
Expand All @@ -195,7 +221,7 @@ def y_splines(y, x_index, z, nu_y=nu_y):
# Interpolate the point (x, y, z) from a list of interpolated points on x,y-axis.
def x_spline(x, y, z, nu_x):
val = CubicSpline(
self.points[np.arange(1, self.shape[0] - 2) * self.shape[1] * self.shape[2], 0],
grid_pts[np.arange(1, self.shape[0] - 2) * self.shape[1] * self.shape[2], 0],
[y_splines(y, x_index, z, nu_y) for x_index in range(1, self.shape[0] - 2)],
)(x, nu_x)
# Trying to vectorize over x-axis, this computes the interpolation for every
Expand All @@ -207,7 +233,9 @@ def x_spline(x, y, z, nu_x):
if use_log:
# All derivatives require the interpolation of f at (x,y,z)
interpolated = np.exp(
self.interpolate(points, values, use_log=False, nu_x=0, nu_y=0, nu_z=0)
self._interpolate(
points, values, use_log=False, nu_x=0, nu_y=0, nu_z=0, grid_pts=grid_pts
)
)
# Only consider taking the derivative in only one direction
one_var_deriv = sum([nu_x == 0, nu_y == 0, nu_z == 0]) == 2
Expand All @@ -218,21 +246,28 @@ def x_spline(x, y, z, nu_x):
elif one_var_deriv:
# Taking the k-th derivative wrt to only one variable (x, y, z)
# Interpolate d^k ln(f) d"deriv_var" for all k from 1 to "deriv_var"
# Each entry of `derivs` is the interpolation of the derivative eval on points.
if nu_x > 0:
derivs = [
self.interpolate(points, values, use_log=False, nu_x=i, nu_y=0, nu_z=0)
self._interpolate(
points, values, use_log=False, nu_x=i, nu_y=0, nu_z=0, grid_pts=grid_pts
)
for i in range(1, nu_x + 1)
]
deriv_var = nu_x
elif nu_y > 0:
derivs = [
self.interpolate(points, values, use_log=False, nu_x=0, nu_y=i, nu_z=0)
self._interpolate(
points, values, use_log=False, nu_x=0, nu_y=i, nu_z=0, grid_pts=grid_pts
)
for i in range(1, nu_y + 1)
]
deriv_var = nu_y
else:
derivs = [
self.interpolate(points, values, use_log=False, nu_x=0, nu_y=0, nu_z=i)
self._interpolate(
points, values, use_log=False, nu_x=0, nu_y=0, nu_z=i, grid_pts=grid_pts
)
for i in range(1, nu_z + 1)
]
deriv_var = nu_z
Expand Down
Loading
Loading