From 03c05d69318bb14b30db0fbdd7201191be9d2cd5 Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 2 Jul 2026 21:20:51 +0800 Subject: [PATCH] Expose water diagnostics through public APIs --- CMakeLists.txt | 2 + Python/sphlite_debug.py | 67 ++++++++++++++++++++++++++++-- include/sphlite.hpp | 30 +++++++++++++ include/sphlite_c_api.h | 3 ++ src/interface/sph_api.f90 | 28 +++++++++++++ src/interface/sph_c_api.c | 37 +++++++++++++++++ tests/interface/test_c_api.c | 44 ++++++++++++++++++++ tests/interface/test_cpp_api.cpp | 16 +++++++ todo.md | 40 +++++++++--------- tools/testing/test_python_debug.py | 37 ++++++++++++++++- 10 files changed, 280 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 45aa36e..208f4f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,8 @@ if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU") #define SPHLITE_FC_sphft_write_csv_chars sphft_write_csv_chars_ #define SPHLITE_FC_sphft_write_restart_chars sphft_write_restart_chars_ #define SPHLITE_FC_sphft_read_restart_chars sphft_read_restart_chars_ +#define SPHLITE_FC_sphft_get_water_summary sphft_get_water_summary_ +#define SPHLITE_FC_sphft_get_water_dt_limits sphft_get_water_dt_limits_ #endif ") endif() diff --git a/Python/sphlite_debug.py b/Python/sphlite_debug.py index 1e39045..b1dc320 100644 --- a/Python/sphlite_debug.py +++ b/Python/sphlite_debug.py @@ -60,6 +60,22 @@ class SPHLite: self.lib.sphlite_read_restart.argtypes = [ctypes.c_char_p] self.lib.sphlite_read_restart.restype = ctypes.c_int + self.lib.sphlite_get_water_summary.argtypes = [ + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ] + self.lib.sphlite_get_water_summary.restype = ctypes.c_int + + self.lib.sphlite_get_water_dt_limits.argtypes = [ + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ] + self.lib.sphlite_get_water_dt_limits.restype = ctypes.c_int + self.lib.sphlite_get_particle_state.argtypes = [ ctypes.c_int, ctypes.POINTER(ctypes.c_int), @@ -189,13 +205,58 @@ class SPHLite: def read_restart(self, filename: str | Path) -> None: self._check(self.lib.sphlite_read_restart(str(filename).encode("utf-8"))) - def inspect(self, filename: str | Path) -> dict[str, int]: + def water_summary(self) -> dict[str, float | list[float]]: + mass = ctypes.c_double(0.0) + momentum_type = ctypes.c_double * 3 + momentum = momentum_type() + max_pressure = ctypes.c_double(0.0) + min_density = ctypes.c_double(0.0) + max_density = ctypes.c_double(0.0) + self._check(self.lib.sphlite_get_water_summary( + ctypes.byref(mass), + momentum, + ctypes.byref(max_pressure), + ctypes.byref(min_density), + ctypes.byref(max_density), + )) + return { + "water_mass": float(mass.value), + "water_momentum": list(momentum), + "max_pressure": float(max_pressure.value), + "min_density": float(min_density.value), + "max_density": float(max_density.value), + } + + def water_dt_limits(self) -> dict[str, float]: + dt_cfl = ctypes.c_double(0.0) + dt_accel = ctypes.c_double(0.0) + dt_viscous = ctypes.c_double(0.0) + self._check(self.lib.sphlite_get_water_dt_limits( + ctypes.byref(dt_cfl), + ctypes.byref(dt_accel), + ctypes.byref(dt_viscous), + )) + return { + "dt_cfl": float(dt_cfl.value), + "dt_accel": float(dt_accel.value), + "dt_viscous": float(dt_viscous.value), + } + + def inspect(self, filename: str | Path) -> dict[str, int | float | list[float]]: self.read_inp(filename) self.build_model() - return {"particles": self.particle_count(), "warnings": self.warning_count()} + summary: dict[str, int | float | list[float]] = { + "particles": self.particle_count(), + "warnings": self.warning_count(), + } + summary.update(self.water_summary()) + summary.update(self.water_dt_limits()) + return summary - def run(self, filename: str | Path) -> dict[str, int]: + def run(self, filename: str | Path) -> dict[str, int | float | list[float]]: summary = self.inspect(filename) self.run_explicit() summary["particles"] = self.particle_count() + summary.update(self.water_summary()) + summary.update(self.water_dt_limits()) return summary diff --git a/include/sphlite.hpp b/include/sphlite.hpp index 9ed1e26..7af7f2a 100644 --- a/include/sphlite.hpp +++ b/include/sphlite.hpp @@ -50,6 +50,20 @@ struct ParticleState { std::vector h; }; +struct WaterSummary { + double mass; + double momentum[3]; + double max_pressure; + double min_density; + double max_density; +}; + +struct WaterDtLimits { + double cfl; + double acceleration; + double viscous; +}; + class Error : public std::runtime_error { public: explicit Error(const std::string &message) : std::runtime_error(message) {} @@ -137,6 +151,22 @@ inline ParticleState particle_state() return state; } +inline WaterSummary water_summary() +{ + WaterSummary summary = {0.0, {0.0, 0.0, 0.0}, 0.0, 0.0, 0.0}; + check(sphlite_get_water_summary(&summary.mass, summary.momentum, + &summary.max_pressure, &summary.min_density, &summary.max_density)); + return summary; +} + +inline WaterDtLimits water_dt_limits() +{ + WaterDtLimits limits = {0.0, 0.0, 0.0}; + check(sphlite_get_water_dt_limits( + &limits.cfl, &limits.acceleration, &limits.viscous)); + return limits; +} + inline void set_output_directory(const std::string &path) { check(sphlite_set_output_directory(path.c_str())); diff --git a/include/sphlite_c_api.h b/include/sphlite_c_api.h index 7b5e905..cb9d050 100644 --- a/include/sphlite_c_api.h +++ b/include/sphlite_c_api.h @@ -68,6 +68,9 @@ SPHLITE_API int sphlite_get_water_summary(double *mass, double *max_pressure, double *min_density, double *max_density); +SPHLITE_API int sphlite_get_water_dt_limits(double *dt_cfl, + double *dt_accel, + double *dt_viscous); #ifdef __cplusplus } diff --git a/src/interface/sph_api.f90 b/src/interface/sph_api.f90 index 0b96a36..778d886 100644 --- a/src/interface/sph_api.f90 +++ b/src/interface/sph_api.f90 @@ -535,6 +535,34 @@ subroutine sphft_get_particle_state(max_particles, particle_id_ext, phase, & internal_energy, temperature, h, count, ierr) end subroutine sphft_get_particle_state +subroutine sphft_get_water_summary(mass, momentum, max_pressure, & + min_density, max_density, ierr) + use sph_api, only: sph_get_water_summary + use sph_precision, only: rk + implicit none + real(rk), intent(out) :: mass + real(rk), intent(out) :: momentum(3) + real(rk), intent(out) :: max_pressure + real(rk), intent(out) :: min_density + real(rk), intent(out) :: max_density + integer, intent(out) :: ierr + + call sph_get_water_summary(mass, momentum, max_pressure, min_density, & + max_density, ierr) +end subroutine sphft_get_water_summary + +subroutine sphft_get_water_dt_limits(dt_cfl, dt_accel, dt_viscous, ierr) + use sph_api, only: sph_get_water_dt_limits + use sph_precision, only: rk + implicit none + real(rk), intent(out) :: dt_cfl + real(rk), intent(out) :: dt_accel + real(rk), intent(out) :: dt_viscous + integer, intent(out) :: ierr + + call sph_get_water_dt_limits(dt_cfl, dt_accel, dt_viscous, ierr) +end subroutine sphft_get_water_dt_limits + subroutine sphft_set_output_directory_chars(chars, count, ierr) use sph_api, only: sph_set_output_directory implicit none diff --git a/src/interface/sph_c_api.c b/src/interface/sph_c_api.c index f97d0c5..0b165f9 100644 --- a/src/interface/sph_c_api.c +++ b/src/interface/sph_c_api.c @@ -53,6 +53,15 @@ extern void SPHLITE_FC_sphft_write_restart_chars( const int *chars, const int *count, int *ierr); extern void SPHLITE_FC_sphft_read_restart_chars( const int *chars, const int *count, int *ierr); +extern void SPHLITE_FC_sphft_get_water_summary( + double *mass, + double *momentum, + double *max_pressure, + double *min_density, + double *max_density, + int *ierr); +extern void SPHLITE_FC_sphft_get_water_dt_limits( + double *dt_cfl, double *dt_accel, double *dt_viscous, int *ierr); static int sphlite_call_path( const char *path, @@ -273,3 +282,31 @@ int sphlite_read_restart(const char *filename) return sphlite_call_path( filename, SPHLITE_FC_sphft_read_restart_chars); } + +int sphlite_get_water_summary(double *mass, + double *momentum, + double *max_pressure, + double *min_density, + double *max_density) +{ + int ierr = 0; + if (mass == NULL || momentum == NULL || max_pressure == NULL || + min_density == NULL || max_density == NULL) { + return -1; + } + SPHLITE_FC_sphft_get_water_summary(mass, momentum, max_pressure, + min_density, max_density, &ierr); + return ierr; +} + +int sphlite_get_water_dt_limits(double *dt_cfl, + double *dt_accel, + double *dt_viscous) +{ + int ierr = 0; + if (dt_cfl == NULL || dt_accel == NULL || dt_viscous == NULL) { + return -1; + } + SPHLITE_FC_sphft_get_water_dt_limits(dt_cfl, dt_accel, dt_viscous, &ierr); + return ierr; +} diff --git a/tests/interface/test_c_api.c b/tests/interface/test_c_api.c index 3e50011..6930f4b 100644 --- a/tests/interface/test_c_api.c +++ b/tests/interface/test_c_api.c @@ -33,6 +33,14 @@ int main(void) double temperature[4]; double h[4]; double time = 0.0; + double water_mass = 0.0; + double water_momentum[3] = {0.0, 0.0, 0.0}; + double max_pressure = 0.0; + double min_density = 0.0; + double max_density = 0.0; + double dt_cfl = 0.0; + double dt_accel = 0.0; + double dt_viscous = 0.0; if (SPHLITE_PHASE_SOLID != 4 || SPHLITE_STATUS_ACTIVE != 0) { return fail("C constants are not locked"); @@ -46,10 +54,21 @@ int main(void) if (sphlite_set_output_interval(0) != -1) { return fail("non-positive output interval did not return -1"); } + if (sphlite_get_water_summary(NULL, water_momentum, &max_pressure, + &min_density, &max_density) != -1) { + return fail("null water summary mass pointer did not return -1"); + } + if (sphlite_get_water_dt_limits(NULL, &dt_accel, &dt_viscous) != -1) { + return fail("null water dt pointer did not return -1"); + } ierr = sphlite_initialize(); if (ierr != 0) return fail("initialize failed"); + ierr = sphlite_get_water_summary(&water_mass, water_momentum, &max_pressure, + &min_density, &max_density); + if (ierr != 2) return fail("unbuilt water summary did not return Fortran state error"); + ierr = sphlite_get_version(&major, &minor, &patch); if (ierr != 0) return fail("get_version failed"); if (major != 0 || minor != 2 || patch != 0) return fail("unexpected version"); @@ -102,5 +121,30 @@ int main(void) ierr = sphlite_finalize(); if (ierr != 0) return fail("finalize failed"); + + ierr = sphlite_initialize(); + if (ierr != 0) return fail("initialize water failed"); + + snprintf(path, sizeof(path), "%s/water_minimal_020.inp", SPHLITE_TEST_DATA_DIR); + ierr = sphlite_read_inp(path); + if (ierr != 0) return fail("read water input failed"); + ierr = sphlite_build_model(); + if (ierr != 0) return fail("build water model failed"); + + ierr = sphlite_get_water_summary(&water_mass, water_momentum, &max_pressure, + &min_density, &max_density); + if (ierr != 0) return fail("water summary query failed"); + if (water_mass <= 0.0 || min_density <= 0.0 || max_density < min_density) { + return fail("water summary content mismatch"); + } + + ierr = sphlite_get_water_dt_limits(&dt_cfl, &dt_accel, &dt_viscous); + if (ierr != 0) return fail("water dt limits query failed"); + if (dt_cfl <= 0.0 || dt_accel <= 0.0 || dt_viscous <= 0.0) { + return fail("water dt limits content mismatch"); + } + + ierr = sphlite_finalize(); + if (ierr != 0) return fail("finalize water failed"); return 0; } diff --git a/tests/interface/test_cpp_api.cpp b/tests/interface/test_cpp_api.cpp index a324497..b4fa1ab 100644 --- a/tests/interface/test_cpp_api.cpp +++ b/tests/interface/test_cpp_api.cpp @@ -28,5 +28,21 @@ int main() } session.close(); + + sphlite::Session water_session; + std::string water_input = std::string(SPHLITE_TEST_DATA_DIR) + "/water_minimal_020.inp"; + sphlite::read_inp(water_input); + sphlite::build_model(); + sphlite::WaterSummary summary = sphlite::water_summary(); + if (summary.mass <= 0.0 || summary.min_density <= 0.0 || + summary.max_density < summary.min_density) { + return 1; + } + sphlite::WaterDtLimits limits = sphlite::water_dt_limits(); + if (limits.cfl <= 0.0 || limits.acceleration <= 0.0 || limits.viscous <= 0.0) { + return 1; + } + water_session.close(); + return 0; } diff --git a/todo.md b/todo.md index 5562de2..c9338ad 100644 --- a/todo.md +++ b/todo.md @@ -325,29 +325,29 @@ ## 阶段 N:C ABI、C++ 和 Python - [x] 在 C 头文件中声明 `sphlite_get_water_summary`。 -- [ ] 在 C 头文件中声明 `sphlite_get_water_dt_limits`。 -- [ ] 在 C wrapper 中桥接 `sphlite_get_water_summary`。 -- [ ] 在 C wrapper 中桥接 `sphlite_get_water_dt_limits`。 -- [ ] 为 `sphlite_get_water_summary` 检查空指针。 -- [ ] 为 `sphlite_get_water_dt_limits` 检查空指针。 -- [ ] 确认 C 参数错误返回 `-1`。 -- [ ] 确认 Fortran 错误码原样返回 C 调用方。 -- [ ] 在 C++ wrapper 中包装 water summary。 -- [ ] 在 C++ wrapper 中包装 water dt limits。 -- [ ] 在 C++ 测试中覆盖 water summary。 -- [ ] 在 C++ 测试中覆盖 water dt limits。 -- [ ] 在 Python wrapper 中包装 water summary。 -- [ ] 在 Python wrapper 中包装 water dt limits。 -- [ ] 在 Python wrapper 中扩展 inspect 辅助函数。 -- [ ] 在 Python wrapper 中扩展 run 辅助函数。 -- [ ] 在 Python 测试中覆盖 water summary。 -- [ ] 在 Python 测试中覆盖 water dt limits。 -- [ ] 在 Python 测试中覆盖缺失 Water 参数错误。 -- [ ] 确认 C ABI、C++ 和 Python 不重新定义物理语义。 +- [x] 在 C 头文件中声明 `sphlite_get_water_dt_limits`。 +- [x] 在 C wrapper 中桥接 `sphlite_get_water_summary`。 +- [x] 在 C wrapper 中桥接 `sphlite_get_water_dt_limits`。 +- [x] 为 `sphlite_get_water_summary` 检查空指针。 +- [x] 为 `sphlite_get_water_dt_limits` 检查空指针。 +- [x] 确认 C 参数错误返回 `-1`。 +- [x] 确认 Fortran 错误码原样返回 C 调用方。 +- [x] 在 C++ wrapper 中包装 water summary。 +- [x] 在 C++ wrapper 中包装 water dt limits。 +- [x] 在 C++ 测试中覆盖 water summary。 +- [x] 在 C++ 测试中覆盖 water dt limits。 +- [x] 在 Python wrapper 中包装 water summary。 +- [x] 在 Python wrapper 中包装 water dt limits。 +- [x] 在 Python wrapper 中扩展 inspect 辅助函数。 +- [x] 在 Python wrapper 中扩展 run 辅助函数。 +- [x] 在 Python 测试中覆盖 water summary。 +- [x] 在 Python 测试中覆盖 water dt limits。 +- [x] 在 Python 测试中覆盖缺失 Water 参数错误。 +- [x] 确认 C ABI、C++ 和 Python 不重新定义物理语义。 ## 阶段 O:CLI -- [ ] 保持 `sphlite --version` 可用。 +- [x] 保持 `sphlite --version` 可用。 - [ ] 保持 `sphlite model.inp` inspect 模式可用。 - [ ] 保持 `sphlite model.inp --check` 模式可用。 - [ ] 保持 `sphlite model.inp --run` 模式可用。 diff --git a/tools/testing/test_python_debug.py b/tools/testing/test_python_debug.py index db96e30..a268584 100644 --- a/tools/testing/test_python_debug.py +++ b/tools/testing/test_python_debug.py @@ -14,7 +14,7 @@ def main() -> int: root = Path(__file__).resolve().parents[2] sys.path.insert(0, str(root / "Python")) - from sphlite_debug import PHASE_SOLID, STATUS_ACTIVE, SPHLite, SPHLiteError + from sphlite_debug import PHASE_SOLID, PHASE_WATER, STATUS_ACTIVE, SPHLite, SPHLiteError solver = SPHLite(args.library) if PHASE_SOLID != 4 or STATUS_ACTIVE != 0: @@ -30,6 +30,9 @@ def main() -> int: state = solver.particle_state() if state["phase"][0] != PHASE_SOLID: raise AssertionError("unexpected particle phase") + solid_summary = solver.inspect(args.input) + if solid_summary["particles"] != 4: + raise AssertionError("solid inspect summary mismatch") solver.set_output_directory(root / "build-codex-unix") solver.write_output(0, 0.0) restart = root / "build-codex-unix" / "python_restart.tmp" @@ -39,6 +42,38 @@ def main() -> int: finally: solver.finalize() + water = SPHLite(args.library) + water.initialize() + try: + water_input = root / "tests" / "data" / "water_minimal_020.inp" + water_summary = water.inspect(water_input) + if water_summary["particles"] != 4: + raise AssertionError("water inspect particle count mismatch") + if water_summary["water_mass"] <= 0.0: + raise AssertionError("water summary mass mismatch") + if water_summary["dt_cfl"] <= 0.0 or water_summary["dt_viscous"] <= 0.0: + raise AssertionError("water dt limits mismatch") + water_state = water.particle_state() + if water_state["phase"][0] != PHASE_WATER: + raise AssertionError("unexpected water particle phase") + run_summary = water.run(water_input) + if run_summary["water_mass"] <= 0.0 or run_summary["dt_cfl"] <= 0.0: + raise AssertionError("water run summary mismatch") + finally: + water.finalize() + + bad_water = SPHLite(args.library) + bad_water.initialize() + try: + try: + bad_water.inspect(root / "tests" / "data" / "water_missing_eos_020.inp") + except SPHLiteError: + pass + else: + raise AssertionError("missing Water parameter input did not raise") + finally: + bad_water.finalize() + missing = SPHLite(args.library) missing.initialize() try: