Skip to content

Commit bcef90d

Browse files
waltsimsclaude
andcommitted
Simplify: deduplicate warning, fix lambda pattern, use direct attr access
- Extract duplicate C++ alpha_mode warning into shared warn_cpp_alpha_mode_unsupported() in validation.py - Use outer-ternary lambda pattern consistently in Stokes branch - Replace getattr() with direct attribute access on kWaveMedium dataclass Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 2f985fa commit bcef90d

4 files changed

Lines changed: 21 additions & 22 deletions

File tree

kwave/kWaveSimulation_helper/save_to_disk_func.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import logging
22
import os
3-
import warnings
43

54
import numpy as np
65
from scipy.io import savemat
@@ -230,15 +229,9 @@ def grab_medium_props(integer_variables, float_variables, medium, is_elastic_cod
230229
integer_variables.absorbing_flag = 0
231230

232231
if medium.absorbing:
233-
alpha_mode = getattr(medium, "alpha_mode", None)
234-
if alpha_mode in ("no_absorption", "no_dispersion"):
235-
warnings.warn(
236-
f"medium.alpha_mode='{alpha_mode}' is not supported by the C++ backend "
237-
f"and will be silently ignored. The C++ binary always computes both "
238-
f"absorption and dispersion when absorbing_flag > 0. Use backend='python' "
239-
f"to honor alpha_mode.",
240-
stacklevel=4,
241-
)
232+
from kwave.solvers.validation import warn_cpp_alpha_mode_unsupported
233+
234+
warn_cpp_alpha_mode_unsupported(medium.alpha_mode, stacklevel=4)
242235
if is_elastic_code: # pragma: no cover
243236
# add to the variable list
244237
float_variables["chi"] = None

kwave/solvers/cpp_simulation.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,9 @@ def __init__(self, kgrid, medium, source, sensor, *, pml_size, pml_alpha, use_sg
3737
self.use_sg = use_sg
3838
self.ndim = kgrid.dim
3939

40-
alpha_mode = getattr(medium, "alpha_mode", None)
41-
if alpha_mode in ("no_absorption", "no_dispersion"):
42-
warnings.warn(
43-
f"medium.alpha_mode='{alpha_mode}' is not supported by the C++ backend "
44-
f"and will be silently ignored. The C++ binary always computes both "
45-
f"absorption and dispersion when absorbing_flag > 0. Use backend='python' "
46-
f"to honor alpha_mode.",
47-
stacklevel=3,
48-
)
40+
from kwave.solvers.validation import warn_cpp_alpha_mode_unsupported
41+
42+
warn_cpp_alpha_mode_unsupported(medium.alpha_mode, stacklevel=3)
4943

5044
def prepare(self, data_path=None):
5145
"""Write HDF5 input file. Returns (input_file, output_file)."""

kwave/solvers/kspace_solver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def _setup_physics_operators(self):
411411

412412
# Absorption/dispersion
413413
alpha_coeff_raw = getattr(self.medium, "alpha_coeff", 0)
414-
alpha_mode = getattr(self.medium, "alpha_mode", None)
414+
alpha_mode = self.medium.alpha_mode
415415
if not _is_enabled(alpha_coeff_raw):
416416
self._absorption = lambda div_u: 0
417417
self._dispersion = lambda rho: 0
@@ -424,7 +424,7 @@ def _setup_physics_operators(self):
424424
no_dispersion = alpha_mode == "no_dispersion"
425425

426426
if abs(alpha_power - 2.0) < 1e-10: # Stokes
427-
self._absorption = lambda div_u: 0 if no_absorption else -2 * alpha_np * self.c0 * self.rho0 * div_u
427+
self._absorption = (lambda div_u: 0) if no_absorption else (lambda div_u: -2 * alpha_np * self.c0 * self.rho0 * div_u)
428428
self._dispersion = lambda rho: 0
429429
else: # Power-law with fractional Laplacian
430430
tau = -2 * alpha_np * self.c0 ** (alpha_power - 1)

kwave/solvers/validation.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def validate_medium(medium, kgrid):
4747
power = float(np.asarray(medium.alpha_power).flat[0])
4848
if power < 0 or power > 3:
4949
warnings.warn(f"medium.alpha_power={power} is outside typical range [0, 3].", stacklevel=3)
50-
alpha_mode = getattr(medium, "alpha_mode", None)
50+
alpha_mode = medium.alpha_mode
5151
if abs(power - 1.0) < 0.05 and alpha_mode != "no_dispersion":
5252
raise ValueError(
5353
f"medium.alpha_power={power} is too close to 1.0. The dispersion term "
@@ -106,6 +106,18 @@ def validate_source(source, kgrid):
106106
raise ValueError("All velocity source components must either be single (1D) or many (2D) time series.")
107107

108108

109+
def warn_cpp_alpha_mode_unsupported(alpha_mode, stacklevel=3):
110+
"""Warn that the C++ backend cannot honor medium.alpha_mode."""
111+
if alpha_mode in ("no_absorption", "no_dispersion"):
112+
warnings.warn(
113+
f"medium.alpha_mode='{alpha_mode}' is not supported by the C++ backend "
114+
f"and will be silently ignored. The C++ binary always computes both "
115+
f"absorption and dispersion when absorbing_flag > 0. Use backend='python' "
116+
f"to honor alpha_mode.",
117+
stacklevel=stacklevel,
118+
)
119+
120+
109121
def validate_sensor(sensor, kgrid):
110122
if sensor is None or not hasattr(sensor, "mask") or sensor.mask is None:
111123
return

0 commit comments

Comments
 (0)