Skip to content

Commit

Permalink
Renamed get_data method to get_sparse_matrix_data
Browse files Browse the repository at this point in the history
This expresses the intend of the method a bit better.
  • Loading branch information
david-zwicker committed Jan 29, 2024
1 parent b5015a3 commit 4702268
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 33 deletions.
8 changes: 5 additions & 3 deletions pde/grids/boundaries/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ def get_mathematical_representation(self, field_name: str = "C") -> tuple[str, s
self.high.get_mathematical_representation(field_name),
)

def get_data(self, idx: tuple[int, ...]) -> tuple[float, dict[int, float]]:
def get_sparse_matrix_data(
self, idx: tuple[int, ...]
) -> tuple[float, dict[int, float]]:
"""sets the elements of the sparse representation of this condition
Args:
Expand All @@ -182,10 +184,10 @@ def get_data(self, idx: tuple[int, ...]) -> tuple[float, dict[int, float]]:
axis_coord = idx[self.axis]
if axis_coord == -1:
# the virtual point on the lower side
return self.low.get_data(idx)
return self.low.get_sparse_matrix_data(idx)
elif axis_coord == self.grid.shape[self.axis]:
# the virtual point on the upper side
return self.high.get_data(idx)
return self.high.get_sparse_matrix_data(idx)
else:
# the normal case of an interior point
return 0, {axis_coord: 1}
Expand Down
16 changes: 12 additions & 4 deletions pde/grids/boundaries/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,9 @@ def check_value_rank(self, rank: int) -> None:
def copy(self: TBC, upper: bool | None = None, rank: int | None = None) -> TBC:
raise NotImplementedError

def get_data(self, idx: tuple[int, ...]) -> tuple[float, dict[int, float]]:
def get_sparse_matrix_data(
self, idx: tuple[int, ...]
) -> tuple[float, dict[int, float]]:
raise NotImplementedError

def get_virtual_point(self, arr, idx: tuple[int, ...] | None = None) -> float:
Expand Down Expand Up @@ -1512,7 +1514,9 @@ def to_subgrid(self: ExpressionBC, subgrid: GridBase) -> ExpressionBC:
bc._get_value_cell_index(with_ghost_cells=False)
return bc

def get_data(self, idx: tuple[int, ...]) -> tuple[float, dict[int, float]]:
def get_sparse_matrix_data(
self, idx: tuple[int, ...]
) -> tuple[float, dict[int, float]]:
raise NotImplementedError

def get_virtual_point(self, arr, idx: tuple[int, ...] | None = None) -> float:
Expand Down Expand Up @@ -2055,7 +2059,9 @@ def get_virtual_point_data(self, compiled: bool = False) -> tuple[Any, Any, int]
tuple: the data structure associated with this virtual point
"""

def get_data(self, idx: tuple[int, ...]) -> tuple[float, dict[int, float]]:
def get_sparse_matrix_data(
self, idx: tuple[int, ...]
) -> tuple[float, dict[int, float]]:
"""sets the elements of the sparse representation of this condition
Args:
Expand Down Expand Up @@ -2631,7 +2637,9 @@ def get_virtual_point_data(self) -> tuple[Any, Any, int, Any, int]:
tuple: the data structure associated with this virtual point
"""

def get_data(self, idx: tuple[int, ...]) -> tuple[float, dict[int, float]]:
def get_sparse_matrix_data(
self, idx: tuple[int, ...]
) -> tuple[float, dict[int, float]]:
"""sets the elements of the sparse representation of this condition
Args:
Expand Down
24 changes: 12 additions & 12 deletions pde/grids/operators/cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ def _get_laplace_matrix_1d(bcs: Boundaries) -> tuple[np.ndarray, np.ndarray]:
matrix[i, i] += -2

if i == 0:
const, entries = bcs[0].get_data((-1,))
const, entries = bcs[0].get_sparse_matrix_data((-1,))
vector[i] += const
for k, v in entries.items():
matrix[i, k] += v
else:
matrix[i, i - 1] += 1

if i == dim_x - 1:
const, entries = bcs[0].get_data((dim_x,))
const, entries = bcs[0].get_sparse_matrix_data((dim_x,))
vector[i] += const
for k, v in entries.items():
matrix[i, k] += v
Expand Down Expand Up @@ -113,15 +113,15 @@ def i(x, y):
for y in range(dim_y):
# handle x-direction
if x == 0:
const, entries = bc_x.get_data((-1, y))
const, entries = bc_x.get_sparse_matrix_data((-1, y))
vector[i(x, y)] += const * scale_x
for k, v in entries.items():
matrix[i(x, y), i(k, y)] += v * scale_x
else:
matrix[i(x, y), i(x - 1, y)] += scale_x

if x == dim_x - 1:
const, entries = bc_x.get_data((dim_x, y))
const, entries = bc_x.get_sparse_matrix_data((dim_x, y))
vector[i(x, y)] += const * scale_x
for k, v in entries.items():
matrix[i(x, y), i(k, y)] += v * scale_x
Expand All @@ -130,15 +130,15 @@ def i(x, y):

# handle y-direction
if y == 0:
const, entries = bc_y.get_data((x, -1))
const, entries = bc_y.get_sparse_matrix_data((x, -1))
vector[i(x, y)] += const * scale_y
for k, v in entries.items():
matrix[i(x, y), i(x, k)] += v * scale_y
else:
matrix[i(x, y), i(x, y - 1)] += scale_y

if y == dim_y - 1:
const, entries = bc_y.get_data((x, dim_y))
const, entries = bc_y.get_sparse_matrix_data((x, dim_y))
vector[i(x, y)] += const * scale_y
for k, v in entries.items():
matrix[i(x, y), i(x, k)] += v * scale_y
Expand Down Expand Up @@ -183,15 +183,15 @@ def i(x, y, z):
for z in range(dim_z):
# handle x-direction
if x == 0:
const, entries = bc_x.get_data((-1, y, z))
const, entries = bc_x.get_sparse_matrix_data((-1, y, z))
vector[i(x, y, z)] += const * scale_x
for k, v in entries.items():
matrix[i(x, y, z), i(k, y, z)] += v * scale_x
else:
matrix[i(x, y, z), i(x - 1, y, z)] += scale_x

if x == dim_x - 1:
const, entries = bc_x.get_data((dim_x, y, z))
const, entries = bc_x.get_sparse_matrix_data((dim_x, y, z))
vector[i(x, y, z)] += const * scale_x
for k, v in entries.items():
matrix[i(x, y, z), i(k, y, z)] += v * scale_x
Expand All @@ -200,15 +200,15 @@ def i(x, y, z):

# handle y-direction
if y == 0:
const, entries = bc_y.get_data((x, -1, z))
const, entries = bc_y.get_sparse_matrix_data((x, -1, z))
vector[i(x, y, z)] += const * scale_y
for k, v in entries.items():
matrix[i(x, y, z), i(x, k, z)] += v * scale_y
else:
matrix[i(x, y, z), i(x, y - 1, z)] += scale_y

if y == dim_y - 1:
const, entries = bc_y.get_data((x, dim_y, z))
const, entries = bc_y.get_sparse_matrix_data((x, dim_y, z))
vector[i(x, y, z)] += const * scale_y
for k, v in entries.items():
matrix[i(x, y, z), i(x, k, z)] += v * scale_y
Expand All @@ -217,15 +217,15 @@ def i(x, y, z):

# handle z-direction
if z == 0:
const, entries = bc_z.get_data((x, y, -1))
const, entries = bc_z.get_sparse_matrix_data((x, y, -1))
vector[i(x, y, z)] += const * scale_z
for k, v in entries.items():
matrix[i(x, y, z), i(x, y, k)] += v * scale_z
else:
matrix[i(x, y, z), i(x, y, z - 1)] += scale_z

if z == dim_z - 1:
const, entries = bc_z.get_data((x, y, dim_z))
const, entries = bc_z.get_sparse_matrix_data((x, y, dim_z))
vector[i(x, y, z)] += const * scale_z
for k, v in entries.items():
matrix[i(x, y, z), i(x, y, k)] += v * scale_z
Expand Down
8 changes: 4 additions & 4 deletions pde/grids/operators/cylindrical_sym.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ def i(r, z):
for z in range(dim_z):
# handle r-direction
if r == 0:
const, entries = bc_r.get_data((-1, z))
const, entries = bc_r.get_sparse_matrix_data((-1, z))
vector[i(r, z)] += const * (scale_r - factor_r[0])
for k, v in entries.items():
matrix[i(r, z), i(k, z)] += v * (scale_r - factor_r[0])
else:
matrix[i(r, z), i(r - 1, z)] += scale_r - factor_r[r]

if r == dim_r - 1:
const, entries = bc_r.get_data((dim_r, z))
const, entries = bc_r.get_sparse_matrix_data((dim_r, z))
vector[i(r, z)] += const * (scale_r + factor_r[-1])
for k, v in entries.items():
matrix[i(r, z), i(k, z)] += v * (scale_r + factor_r[-1])
Expand All @@ -85,15 +85,15 @@ def i(r, z):

# handle z-direction
if z == 0:
const, entries = bc_z.get_data((r, -1))
const, entries = bc_z.get_sparse_matrix_data((r, -1))
vector[i(r, z)] += const * scale_z
for k, v in entries.items():
matrix[i(r, z), i(r, k)] += v * scale_z
else:
matrix[i(r, z), i(r, z - 1)] += scale_z

if z == dim_z - 1:
const, entries = bc_z.get_data((r, dim_z))
const, entries = bc_z.get_sparse_matrix_data((r, dim_z))
vector[i(r, z)] += const * scale_z
for k, v in entries.items():
matrix[i(r, z), i(r, k)] += v * scale_z
Expand Down
4 changes: 2 additions & 2 deletions pde/grids/operators/polar_sym.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def _get_laplace_matrix(bcs: Boundaries) -> tuple[np.ndarray, np.ndarray]:
matrix[i, i + 1] = 2 * scale
continue # the special case of the inner boundary is handled
else:
const, entries = bcs[0].get_data((-1,))
const, entries = bcs[0].get_sparse_matrix_data((-1,))
factor = scale - scale_i
vector[i] += const * factor
for k, v in entries.items():
Expand All @@ -298,7 +298,7 @@ def _get_laplace_matrix(bcs: Boundaries) -> tuple[np.ndarray, np.ndarray]:
matrix[i, i - 1] = scale - scale_i

if i == dim_r - 1:
const, entries = bcs[0].get_data((dim_r,))
const, entries = bcs[0].get_sparse_matrix_data((dim_r,))
factor = scale + scale_i
vector[i] += const * factor
for k, v in entries.items():
Expand Down
4 changes: 2 additions & 2 deletions pde/grids/operators/spherical_sym.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ def _get_laplace_matrix(bcs: Boundaries) -> tuple[np.ndarray, np.ndarray]:
if r_min == 0:
matrix[i, i + 1] = factor_l[i]
else:
const, entries = bcs[0].get_data((-1,))
const, entries = bcs[0].get_sparse_matrix_data((-1,))
vector[i] += const * factor_l[i]
for k, v in entries.items():
matrix[i, k] += v * factor_l[i]
Expand All @@ -575,7 +575,7 @@ def _get_laplace_matrix(bcs: Boundaries) -> tuple[np.ndarray, np.ndarray]:
matrix[i, i - 1] = factor_l[i]

if i == dim_r - 1:
const, entries = bcs[0].get_data((dim_r,))
const, entries = bcs[0].get_sparse_matrix_data((dim_r,))
vector[i] += const * factor_h[i]
for k, v in entries.items():
matrix[i, k] += v * factor_h[i]
Expand Down
8 changes: 4 additions & 4 deletions tests/tools/test_numba.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def test_flat_idx():

# testing the numba compiled version
@jit
def get_data(data):
def get_sparse_matrix_data(data):
return flat_idx(data, 1)

assert get_data(2) == 2
assert get_data(np.arange(2)) == 1
assert get_data(np.arange(4).reshape(2, 2)) == 1
assert get_sparse_matrix_data(2) == 2
assert get_sparse_matrix_data(np.arange(2)) == 1
assert get_sparse_matrix_data(np.arange(4).reshape(2, 2)) == 1


def test_counter():
Expand Down
4 changes: 2 additions & 2 deletions tests/trackers/test_trackers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ def test_trackers(rng):
def store_time(state, t):
times.append(t)

def get_data(state):
def get_sparse_matrix_data(state):
return {"integral": state.integral}

devnull = open(os.devnull, "w")
data = trackers.DataTracker(get_data, interrupts=0.1)
data = trackers.DataTracker(get_sparse_matrix_data, interrupts=0.1)
tracker_list = [
trackers.PrintTracker(interrupts=0.1, stream=devnull),
trackers.CallbackTracker(store_time, interrupts=0.1),
Expand Down

0 comments on commit 4702268

Please sign in to comment.