diff --git a/src/grid/basegrid.py b/src/grid/basegrid.py index 3579dfea..b1b4dc94 100644 --- a/src/grid/basegrid.py +++ b/src/grid/basegrid.py @@ -249,19 +249,21 @@ def moments( each row/order is, e.g. for Cartesian, [(0, 0, 0), (1, 0, 0) ,...]. """ + # Points of shape (N,), as in OneDGrid, are N points in one dimension + points = self.points if self.points.ndim == 2 else self.points.reshape(-1, 1) if func_vals.ndim > 1: raise ValueError(f"`func_vals` {func_vals.ndim} should have dimension one.") if centers.ndim != 2: raise ValueError(f"`centers` {centers.ndim} should have dimension one or two.") - if self.points.shape[1] != centers.shape[1]: + if points.shape[1] != centers.shape[1]: raise ValueError( - f"The dimension of the grid {self.points.shape[1]} should" + f"The dimension of the grid {points.shape[1]} should" f"match the dimension of the centers {centers.shape[1]}." ) - if len(func_vals) != self.points.shape[0]: + if len(func_vals) != points.shape[0]: raise ValueError( f"The length of function values {len(func_vals)} should match " - f"the number of points in the grid {self.points.shape[0]}." + f"the number of points in the grid {points.shape[0]}." ) if type_mom == "pure-radial" and orders == 0: raise ValueError( @@ -274,7 +276,7 @@ def moments( orders = range(0, orders + 1) if type_mom != "pure-radial" else range(1, orders + 1) else: raise TypeError(f"Orders {type(orders)} should be either integer, list or numpy array.") - dim = self.points.shape[1] + dim = points.shape[1] all_orders = generate_orders_horton_order(orders[0], type_mom, dim) for l_ord in orders[1:]: all_orders = np.vstack((all_orders, generate_orders_horton_order(l_ord, type_mom, dim))) @@ -282,7 +284,7 @@ def moments( integrals = [] for center in centers: # Calculate centered pts: [(X-c), (Y-c), (Z-c)] - centered_pts = self.points - center + centered_pts = points - center if type_mom == "cartesian": # Take the powers to get [(X-c)^mx, (Y-c)^my, (Z-c)^mz] diff --git a/src/grid/tests/test_grid.py b/src/grid/tests/test_grid.py index 6dee4ac7..f9b474fb 100644 --- a/src/grid/tests/test_grid.py +++ b/src/grid/tests/test_grid.py @@ -109,6 +109,14 @@ def test_get_localgrid_empty(self): assert localgrid.weights.shape == (0,) assert_allclose(localgrid.center, center) + def test_moments_empty_localgrid(self): + """Moments over a local grid without points are zero.""" + center = self.grid.points.max(axis=0) + 10.0 + localgrid = self.grid.get_localgrid(center, 0.5) + centers = np.atleast_1d(center).reshape(1, -1) + moments = localgrid.moments(1, centers, np.zeros(0), type_mom="cartesian") + assert_allclose(moments, 0.0) + def test_get_localgrid_radius_inf(self): """Test the creation of the local grid with an infinite radius.""" localgrid = self.grid.get_localgrid(self.grid.points[3], np.inf) @@ -172,6 +180,15 @@ def test_cartesian_moments(self): expected = [np.sum((points - 0.5) ** n * func_vals * self._ref_weights) for n in range(4)] assert_allclose(integrals[:, 0], expected) + def test_cartesian_moments_flat_points(self): + """Test that points of shape (N,), as in OneDGrid, give the same moments as (N, 1).""" + points = self._ref_points[:, 0] + func_vals = np.exp(-(points**2)) + center = np.array([[0.5]]) + expected = self.grid.moments(3, center, func_vals, type_mom="cartesian") + for grid in [Grid(points, self._ref_weights), OneDGrid(points, self._ref_weights)]: + assert_allclose(grid.moments(3, center, func_vals, type_mom="cartesian"), expected) + class TestGrid2D(TestGrid): """Grid testcase class for 2D point arrays."""