diff --git a/CMakeLists.txt b/CMakeLists.txt index aeb3d78..4fa0c79 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,15 @@ cmake_minimum_required(VERSION 3.20) -project(SPHLite VERSION 0.1.0 LANGUAGES C Fortran) +project(SPHLite VERSION 0.1.1 LANGUAGES C CXX Fortran) set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_C_EXTENSIONS OFF) +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + set(CMAKE_Fortran_STANDARD 95) set(CMAKE_Fortran_STANDARD_REQUIRED ON) set(CMAKE_Fortran_EXTENSIONS OFF) @@ -34,6 +38,12 @@ if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU") #define SPHLITE_FC_sphft_run_explicit sphft_run_explicit_ #define SPHLITE_FC_sphft_get_current_time sphft_get_current_time_ #define SPHLITE_FC_sphft_get_particle_count sphft_get_particle_count_ +#define SPHLITE_FC_sphft_get_particle_state_capacity sphft_get_particle_state_capacity_ +#define SPHLITE_FC_sphft_get_particle_state sphft_get_particle_state_ +#define SPHLITE_FC_sphft_set_output_directory_chars sphft_set_output_directory_chars_ +#define SPHLITE_FC_sphft_set_output_interval sphft_set_output_interval_ +#define SPHLITE_FC_sphft_write_output sphft_write_output_ +#define SPHLITE_FC_sphft_get_input_warning_count sphft_get_input_warning_count_ #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_ @@ -106,11 +116,39 @@ add_executable(test_c_api tests/interface/test_c_api.c) target_include_directories(test_c_api PRIVATE include) target_compile_definitions(test_c_api PRIVATE SPHLITE_TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests/data" + SPHLITE_TEST_WORK_DIR="${CMAKE_BINARY_DIR}" ) target_link_libraries(test_c_api PRIVATE sphlite_c) add_test(NAME c_api COMMAND test_c_api) set_tests_properties(c_api PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_executable(test_cpp_api tests/interface/test_cpp_api.cpp) +target_include_directories(test_cpp_api PRIVATE include) +target_compile_definitions(test_cpp_api PRIVATE + SPHLITE_TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests/data" +) +target_link_libraries(test_cpp_api PRIVATE sphlite_c) +add_test(NAME cpp_api COMMAND test_cpp_api) +set_tests_properties(cpp_api PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(test_restart tests/module/test_restart.f90) +target_link_libraries(test_restart PRIVATE sphlite_core) +set_target_properties(test_restart PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME restart COMMAND test_restart) +set_tests_properties(restart PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(test_output tests/module/test_output.f90) +target_link_libraries(test_output PRIVATE sphlite_core) +set_target_properties(test_output PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME output COMMAND test_output) +set_tests_properties(output PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(test_coupling_interface tests/module/test_coupling_interface.f90) +target_link_libraries(test_coupling_interface PRIVATE sphlite_core) +set_target_properties(test_coupling_interface PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME coupling_interface COMMAND test_coupling_interface) +set_tests_properties(coupling_interface PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + find_package(Python3 REQUIRED COMPONENTS Interpreter) add_test( NAME python_debug @@ -121,6 +159,17 @@ add_test( ) set_tests_properties(python_debug PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_test( + NAME cli + COMMAND "${Python3_EXECUTABLE}" + "${CMAKE_CURRENT_SOURCE_DIR}/tools/testing/test_cli.py" + --exe "$" + --input "${CMAKE_CURRENT_SOURCE_DIR}/tests/data/minimal_particles_011.inp" + --bad-input "${CMAKE_CURRENT_SOURCE_DIR}/tests/data/missing_material_011.inp" + --output-dir "${CMAKE_BINARY_DIR}" +) +set_tests_properties(cli PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_test( NAME source_size COMMAND "${Python3_EXECUTABLE}" diff --git a/Python/sphlite_debug.py b/Python/sphlite_debug.py index f962dfd..1e39045 100644 --- a/Python/sphlite_debug.py +++ b/Python/sphlite_debug.py @@ -3,6 +3,21 @@ from __future__ import annotations import ctypes from pathlib import Path +PHASE_WATER = 1 +PHASE_GAS = 2 +PHASE_EXPLOSIVE = 3 +PHASE_SOLID = 4 +PHASE_SHELL = 5 +PHASE_BOUNDARY = 6 +PHASE_COUPLING = 7 +PHASE_SOLID_FAILED = 8 +PHASE_SHELL_FAILED = 9 + +STATUS_ACTIVE = 0 +STATUS_INACTIVE = 1 +STATUS_FAILED = 2 +STATUS_RESERVED = 3 + class SPHLiteError(RuntimeError): pass @@ -27,6 +42,43 @@ class SPHLite: self.lib.sphlite_get_particle_count.argtypes = [ctypes.POINTER(ctypes.c_int)] self.lib.sphlite_get_particle_count.restype = ctypes.c_int + self.lib.sphlite_get_input_warning_count.argtypes = [ctypes.POINTER(ctypes.c_int)] + self.lib.sphlite_get_input_warning_count.restype = ctypes.c_int + + self.lib.sphlite_set_output_directory.argtypes = [ctypes.c_char_p] + self.lib.sphlite_set_output_directory.restype = ctypes.c_int + + self.lib.sphlite_set_output_interval.argtypes = [ctypes.c_int] + self.lib.sphlite_set_output_interval.restype = ctypes.c_int + + self.lib.sphlite_write_output.argtypes = [ctypes.c_int, ctypes.c_double] + self.lib.sphlite_write_output.restype = ctypes.c_int + + self.lib.sphlite_write_restart.argtypes = [ctypes.c_char_p] + self.lib.sphlite_write_restart.restype = ctypes.c_int + + self.lib.sphlite_read_restart.argtypes = [ctypes.c_char_p] + self.lib.sphlite_read_restart.restype = ctypes.c_int + + self.lib.sphlite_get_particle_state.argtypes = [ + ctypes.c_int, + ctypes.POINTER(ctypes.c_int), + ctypes.POINTER(ctypes.c_int), + ctypes.POINTER(ctypes.c_int), + ctypes.POINTER(ctypes.c_int), + ctypes.POINTER(ctypes.c_int), + 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), + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_int), + ] + self.lib.sphlite_get_particle_state.restype = ctypes.c_int + self.lib.sphlite_get_last_error.argtypes = [ ctypes.POINTER(ctypes.c_int), ctypes.c_char_p, @@ -62,5 +114,88 @@ class SPHLite: self._check(self.lib.sphlite_get_particle_count(ctypes.byref(count))) return int(count.value) + def warning_count(self) -> int: + count = ctypes.c_int(0) + self._check(self.lib.sphlite_get_input_warning_count(ctypes.byref(count))) + return int(count.value) + + def particle_state(self) -> dict[str, list[float] | list[int]]: + count = self.particle_count() + int_array = ctypes.c_int * count + vec_array = ctypes.c_double * (3 * count) + real_array = ctypes.c_double * count + particle_id_ext = int_array() + phase = int_array() + status = int_array() + material_id = int_array() + section_id = int_array() + x = vec_array() + v = vec_array() + a = vec_array() + rho = real_array() + pressure = real_array() + internal_energy = real_array() + temperature = real_array() + h = real_array() + actual = ctypes.c_int(0) + self._check(self.lib.sphlite_get_particle_state( + count, + particle_id_ext, + phase, + status, + material_id, + section_id, + x, + v, + a, + rho, + pressure, + internal_energy, + temperature, + h, + ctypes.byref(actual), + )) + return { + "particle_id_ext": list(particle_id_ext), + "phase": list(phase), + "status": list(status), + "material_id": list(material_id), + "section_id": list(section_id), + "x": list(x), + "v": list(v), + "a": list(a), + "rho": list(rho), + "pressure": list(pressure), + "internal_energy": list(internal_energy), + "temperature": list(temperature), + "h": list(h), + } + def run_explicit(self) -> None: self._check(self.lib.sphlite_run_explicit()) + + def set_output_directory(self, directory: str | Path) -> None: + self._check(self.lib.sphlite_set_output_directory(str(directory).encode("utf-8"))) + + def set_output_interval(self, interval: int) -> None: + self._check(self.lib.sphlite_set_output_interval(int(interval))) + + def write_output(self, step: int = 0, time: float = 0.0) -> None: + self._check(self.lib.sphlite_write_output(int(step), float(time))) + + def write_restart(self, filename: str | Path) -> None: + self._check(self.lib.sphlite_write_restart(str(filename).encode("utf-8"))) + + 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]: + self.read_inp(filename) + self.build_model() + return {"particles": self.particle_count(), "warnings": self.warning_count()} + + def run(self, filename: str | Path) -> dict[str, int]: + summary = self.inspect(filename) + self.run_explicit() + summary["particles"] = self.particle_count() + return summary diff --git a/README.md b/README.md index d4234fd..c5aab5f 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ SPHLite 是一个面向船舶水下接触爆炸的轻量级 SPH 多物理仿真软件。项目目标是支持水、爆炸产物气体、实体结构粒子、板壳结构粒子、破裂损伤,以及通过外部耦合模块与 FEMLite 进行接触和连续体耦合。 -当前目标版本:0.1.0。该版本定位为工程骨架版,只建立可编译、可测试、可扩展的 Fortran 95 求解器框架,不交付生产级水下爆炸、气泡脉动、结构破裂或 FEM-SPH 联合计算能力。 +当前目标版本:0.1.1。该版本定位为框架加固和接口闭合版,在 0.1.0 工程骨架基础上完善输入元数据、粒子状态查询、C ABI/C++/Python/CLI、output、restart 和 CouplingInterface 占位测试,不交付生产级水下爆炸、气泡脉动、结构破裂或 FEM-SPH 联合计算能力。 代码仓库地址: @@ -63,11 +63,13 @@ SPHLite 的编译和测试要求应与 FEMLite 保持一致风格: ## 当前状态 -0.1.0 当前实现重点包括: +0.1.1 当前实现重点包括: - CMake/CTest 工程骨架。 - Fortran 95 核心库、C ABI、C++ 头文件薄包装和 Python `ctypes` 调试入口。 -- Abaqus `.inp` 明确子集的最小解析框架。 -- Velocity-Verlet 显式推进骨架。 +- Abaqus `.inp` 明确子集的元数据占位解析。 +- 粒子状态查询和常量一致性检查。 +- Velocity-Verlet 显式推进骨架加固。 +- CSV output 和 restart 占位闭环。 - Water、Gas、Explosive、Solid、Shell 和 CouplingInterface 的占位模块。 -- 0.1.0 release gate 与 source size 门禁。 +- 0.1.1 release gate 与 source size 门禁。 diff --git a/docs/09_release_history.md b/docs/09_release_history.md index 50c01d3..7126b3c 100644 --- a/docs/09_release_history.md +++ b/docs/09_release_history.md @@ -1,16 +1,17 @@ # 09 版本历史与验收记录 -文档状态:0.1.0 开发中 +文档状态:0.1.1 本地验收通过 ## 1. 当前状态 -SPHLite 当前正在搭建 0.1.0 工程骨架版本。该版本只交付可编译、可测试、可扩展的求解器框架,不交付生产级物理算法。 +SPHLite 当前完成 0.1.1 框架加固版本的本地验收。该版本只交付可编译、可测试、可扩展的求解器框架和接口闭合,不交付生产级物理算法。 ## 2. 规划版本线 | 版本线 | 目标 | |---|---| | 0.1.0 | 工程骨架、生命周期、错误码、C ABI、CLI、Python 调试入口和测试框架 | +| 0.1.1 | 输入元数据、粒子状态查询、C++ wrapper、CLI/output/restart/CouplingInterface gate 加固 | | 0.2 | 粒子状态、核函数、邻域搜索和基础 `Water` | | 0.3 | `Gas`、基础 EOS、气体统计和气泡诊断 | | 0.4 | `Explosive`、解析点火、爆轰传播和产物气体注入 | @@ -47,7 +48,30 @@ cmake --build build-codex-unix --parallel - 已通过 `.gitignore` 排除 build 目录、CSV/VTK/restart 临时结果和 Python 缓存。 - 文档已明确 0.1.0 不交付生产级 Water、Gas、Explosive、Solid、Shell 物理算法。 -说明:当前目录已初始化本地 git 仓库,并设置远端 `origin` 为 `http://123.57.213.67:3062/shen/SPHLite.git`,尚未执行提交或推送。 +说明:0.1.0 已提交并推送到远端,标签为 `v0.1.0`。 + +### 0.1.1 本地验收记录 + +记录日期:2026-07-02 + +本地构建目录:`build-codex-unix` + +构建配置: + +```powershell +cmake -S . -B build-codex-unix -G "Unix Makefiles" -DCMAKE_C_COMPILER=C:/gcc-16.1.0-64/bin/gcc.exe -DCMAKE_CXX_COMPILER=C:/gcc-16.1.0-64/bin/g++.exe -DCMAKE_Fortran_COMPILER=C:/gcc-16.1.0-64/bin/gfortran.exe -DCMAKE_MAKE_PROGRAM=C:/gcc-16.1.0-64/bin/make.exe +cmake --build build-codex-unix --parallel +``` + +验收结果: + +- `sphlite_core`、`sphlite_c`、`sphlite` 和 `test_cpp_api` 完整编译通过。 +- `ctest --test-dir build-codex-unix --output-on-failure` 通过,10/10。 +- `source_size` 通过。 +- `git diff --check` 通过。 +- CLI `--version`、inspect、`--check`、`--run` 和错误输入路径通过。 +- output、restart 和 CouplingInterface 占位测试通过。 +- 文档已明确 0.1.1 不交付生产级 Water、Gas、Explosive、Solid、Shell 物理算法。 后续每个版本应记录: diff --git a/docs/11_benchmark_matrix.md b/docs/11_benchmark_matrix.md index db4e17b..b200aeb 100644 --- a/docs/11_benchmark_matrix.md +++ b/docs/11_benchmark_matrix.md @@ -1,10 +1,10 @@ # 11 Benchmark 与验收矩阵 -文档状态:0.1.0 gate 草案 +文档状态:0.1.1 gate 记录 ## 1. 当前状态 -0.1.0 不交付工程 benchmark,只建立 release gate 和 source size 门禁。后续物理 benchmark 矩阵保留为长期规划。 +0.1.1 不交付工程 benchmark,只建立并加固 release gate 和 source size 门禁。后续物理 benchmark 矩阵保留为长期规划。 ## 2. 0.1.0 Release Gate @@ -24,6 +24,29 @@ |---|---|---| | 2026-07-02 | `build-codex-unix` | `smoke`、`input`、`c_api`、`python_debug`、`source_size` 和 `git diff --check` 均通过 | +## 2.1 0.1.1 Release Gate + +| 测试名 | 类型 | 验收点 | +|---|---|---| +| `smoke` | Fortran smoke | 版本 0.1.1、生命周期、数据库、常量、显式步进错误路径 | +| `input` | Fortran input | 元数据占位、set 不作为类型判断、warning/error 路径 | +| `c_api` | C ABI | 空指针、非正容量、粒子状态、output、restart | +| `cpp_api` | C++ wrapper | RAII 生命周期、读入、构建、粒子状态 | +| `python_debug` | Python `ctypes` | 常量、粒子状态、output、restart、错误路径 | +| `cli` | CLI | `--version`、inspect、`--check`、`--run`、错误输入 | +| `restart` | Fortran restart | 写入、读回、继续最小显式步 | +| `output` | Fortran output | CSV 字段和输出路径控制 | +| `coupling_interface` | Fortran coupling | 中立导出/导入占位和诊断字段 | +| `source_size` | 工具门禁 | Fortran/C/C++/Python 文件大小和 stale exception | + +0.1.1 不验收 Water、Gas、Explosive、Solid、Shell 的生产级物理结果,也不验收真实 FEM-SPH 联合运行。 + +本地验收记录: + +| 日期 | 构建目录 | 结果 | +|---|---|---| +| 2026-07-02 | `build-codex-unix` | 0.1.1 gate 10/10 通过,`git diff --check` 通过 | + ## 3. 基础数值矩阵 | 领域 | 测试或 benchmark | 验收点 | diff --git a/docs/v1.1_development_plan.md b/docs/v1.1_development_plan.md new file mode 100644 index 0000000..ed1a09d --- /dev/null +++ b/docs/v1.1_development_plan.md @@ -0,0 +1,374 @@ +# SPHLite v1.1 开发计划 + +文档状态:0.1.1 开发计划稿 + +目标软件版本:SPHLite 0.1.1 + +依据文档: + +- `01_requirements_specification.md` +- `02_software_architecture_design.md` +- `03_detailed_design.md` +- `04_mathematical_model_and_algorithms.md` +- `05_interface_control_document.md` +- `06_test_and_verification_plan.md` +- `v1.0_development_plan.md` + +## 1. 版本定位 + +SPHLite 0.1.1 是 0.1 系列的框架加固版本。0.1.0 已完成工程骨架、最小数据库、最小 `.inp` 解析、显式步进骨架、C ABI、CLI、Python 调试入口和 release gate。0.1.1 的目标是在不进入生产级物理算法的前提下,把输入、数据库、接口、restart、输出和测试闭环进一步整理到可支撑 0.2 `Water` 开发的状态。 + +0.1.1 不定位为 `Water` 物理版本,也不交付气泡、爆炸、结构断裂或外部 FEM-SPH 联合计算。该版本应减少后续物理模块开发时需要反复修改的基础接口和数据契约。 + +## 2. 版本目标 + +0.1.1 应完成: + +- 锁定 0.1 系列 phase/status 常量,并确保 Fortran、C、C++ 和 Python 一致。 +- 完善 `.inp` 子集解析的模型元数据保存,包括 material、section、set 和基础 `*SPHLITE ...` 控制项。 +- 增加粒子状态查询 API,采用 dimension query + caller-allocated buffer 风格。 +- 完善 C ABI,使 05 号文档中 0.1 阶段需要的输出、restart、粒子查询和基础错误路径具备占位实现。 +- 完善 Python 调试入口,使其能读取粒子状态、执行 inspect/run smoke,并覆盖错误路径。 +- 完善 CLI 的 inspect/check/run 行为,输出字段稳定、错误消息明确。 +- 使 restart 占位格式更接近模块级状态清单,至少覆盖 0.1 已有全部通用状态。 +- 使 CSV 输出占位可由 API/CLI 驱动,并避免输出产物进入仓库。 +- 增加 C++ wrapper smoke 测试,但不把 C++ 作为核心实现语言。 +- 增加 CouplingInterface 的中立接口测试占位,确认命名中不出现 FEMLite 内部依赖。 +- 保持 0.1 release gate 快速、稳定,并扩展到 0.1.1 新增接口。 + +## 3. 明确不交付范围 + +0.1.1 不交付: + +- 完整 Water 弱可压 SPH。 +- 核函数、邻域搜索和 Tait EOS 的生产计算路径。 +- Gas EOS 生产计算。 +- Explosive programmed burn 真实质量/动量/能量注入。 +- Solid Johnson-Cook 应力更新、损伤和失效。 +- Shell Mindlin-Reissner 板壳计算。 +- 水-气-结构真实相互作用。 +- 外部 CouplingModule、CoupleLite、CoupledLite 或 CouplingLite。 +- 与 FEMLite 的联合运行。 +- 几何、网格或粒子生成能力。 +- 工程 benchmark 和长跑算例。 + +0.1.1 可以新增数据字段、接口占位、解析占位和测试占位,但不得将这些占位描述为已实现生产级物理能力。 + +## 4. 目录结构调整 + +0.1.1 沿用 0.1.0 目录结构,不新增 `geometry/` 或 `mesh/` 目录。 + +建议保留并完善: + +```text +include/ +src/ + core/ + input/ + database/ + particle/ + material/ + water/ + gas/ + explosive/ + solid/ + shell/ + coupling_interface/ + solver/ + output/ + interface/ + app/ +Python/ +tests/ + unit/ + module/ + interface/ + data/ +tools/ + testing/ +examples/ +docs/ +``` + +测试或示例所需的 `.inp` 生成脚本只允许放入 `tools/` 或 `examples/`,并明确标注为辅助工具,不作为求解器几何或粒子生成能力。 + +## 5. 数据库加固范围 + +0.1.1 数据库应继续采用集中型数据管理模式。建议新增或完善以下能力: + +- 明确 phase/status 常量整数值,并在 Fortran/C/Python 中保持一致。 +- 保存输入 material 的名称、类型、内部 id 和外部名称映射占位。 +- 保存 section 的名称、类型、材料引用、厚度/积分点占位和内部 id。 +- 保存 set 名称、成员 id 和 set 类型占位,明确 set 不参与物理类型判断。 +- 保存 region/coupling region 占位数据,用于后续输出和耦合。 +- 提供粒子状态查询例程,输出 id、phase、status、material、section、位置、速度、加速度、密度、压力、内能、温度和 `h`。 +- 保证 `sph_finalize`、重复 `sph_initialize` 和重复读入模型时数据库状态一致释放。 + +0.1.1 不要求完整材料参数库,也不要求 material/section 驱动真实物理计算。 + +## 6. 输入解析范围 + +0.1.1 在 0.1.0 的基础上加固 Abaqus `.inp` 明确子集: + +- `*NODE`:继续支持 3D 坐标解析,增加错误行号或上下文消息。 +- `*ELEMENT`:保存 element id、element type 和节点连接占位。 +- `*NSET`:保存 set 名称和节点集合占位。 +- `*ELSET`:保存 set 名称和 element 集合占位。 +- `*MATERIAL`:保存 material 名称和后续材料卡片入口占位。 +- `*SECTION` 或 Abaqus 等价 section 关键词:保存 section 和 material 引用占位。 +- `*SPHLITE TIME CONTROL`:解析 `DT`、`TIME`、`STEPS` 和安全系数占位。 +- `*SPHLITE OUTPUT`:解析输出目录和输出间隔占位。 +- `*SPHLITE COUPLING REGION`:解析 SPHLite 侧 coupling region 名称和集合引用占位。 + +未知关键词处理原则保持不变:不影响程序运行的未支持关键词 warning 后跳过;缺失关键数据或导致无法运行的输入 error。 + +0.1.1 应新增输入测试,覆盖大小写混合、空行、注释、逗号字段空白、未知关键词 warning、缺失 material/section/time control error 和 set 不作为类型判断依据。 + +## 7. Fortran API 范围 + +0.1.1 Fortran API 在 0.1.0 基础上建议扩展: + +```fortran +subroutine sph_get_particle_state_capacity(count, ndim, ierr) +subroutine sph_get_particle_state(max_particles, particle_id_ext, phase, status, & + material_id, section_id, x, v, a, rho, pressure, & + internal_energy, temperature, h, count, ierr) +subroutine sph_set_output_directory(directory, ierr) +subroutine sph_set_output_interval(interval, ierr) +subroutine sph_write_output(step, time, ierr) +subroutine sph_write_restart(filename, ierr) +subroutine sph_read_restart(filename, ierr) +subroutine sph_get_input_warning_count(count, ierr) +``` + +Fortran API 仍是语义来源。所有可失败接口必须返回错误码,并通过最近错误消息说明原因。 + +## 8. C ABI 范围 + +0.1.1 C ABI 应覆盖 05 号接口文档中 0.1 阶段需要闭合的接口: + +```c +int sphlite_set_output_directory(const char *path); +int sphlite_set_output_interval(int interval); +int sphlite_write_output(int step, double time); +int sphlite_write_restart(const char *filename); +int sphlite_read_restart(const char *filename); +int sphlite_get_particle_state(int max_particles, + int *particle_id_ext, + int *phase, + int *status, + int *material_id, + int *section_id, + double *x, + double *v, + double *a, + double *rho, + double *pressure, + double *internal_energy, + double *temperature, + double *h, + int *count); +``` + +C ABI 参数错误仍返回 `-1`。Fortran 物理、输入或数据库错误仍返回 SPHLite 错误码。多维数组按 Fortran 顺序展开到 C 缓冲区,即 `value[dim + 3 * particle]`。 + +0.1.1 可为 Gas、Explosive 和 CouplingInterface 诊断接口建立占位声明,但默认测试只验证占位接口的错误处理、容量检查和中立命名,不验证真实物理数值。 + +## 9. C++ 和 Python 范围 + +C++ wrapper 在 0.1.1 中保持薄包装定位: + +- 不重新定义物理语义。 +- 提供 RAII 生命周期辅助。 +- 包装版本、读入、构建、运行、粒子数量和粒子状态查询。 +- 增加一个最小 C++ smoke 测试。 + +Python `ctypes` 调试入口应扩展: + +- 包装输出目录、输出间隔、restart 读写占位。 +- 包装粒子状态查询。 +- 提供 inspect/run 辅助函数。 +- 增加错误路径测试,如空路径、缺失文件、缺失关键输入。 + +Python 仍不作为唯一入口,CLI 和 C ABI 必须完整可用。 + +## 10. CLI 范围 + +0.1.1 CLI 建议支持: + +```powershell +sphlite model.inp +sphlite model.inp --check +sphlite model.inp --run +sphlite model.inp --run --output output +sphlite --version +``` + +CLI inspect/check 输出字段建议稳定为: + +- `version` +- `input` +- `mode` +- `particles` +- `warnings` +- `time` +- `step` +- `status` + +CLI 错误时应打印最近错误码和错误消息。CLI 内部必须走公共生命周期 API,不绕过 Fortran API 或 C ABI 的状态管理。 + +## 11. Solver、Output 和 Restart 范围 + +0.1.1 继续使用 Velocity-Verlet 作为唯一主时间推进格式。求解器仍不引入隐式、静力或非线性静力接口。 + +求解器加固内容: + +- 稳定时间步估计保留占位,但应返回输入控制中的默认时间步。 +- `sph_advance_explicit` 对负时间步、零时间步和未初始化状态给出明确错误。 +- `sph_run_explicit` 对 `end_time`、`max_steps` 和浮点截断进行更稳定的边界处理。 + +输出加固内容: + +- CSV 输出包含 id、phase、status、material、section、坐标、速度、密度、压力和温度。 +- 输出目录和输出间隔可由 input/API/CLI 设置。 +- 输出文件命名规则稳定,测试产物写入 build 或临时目录,不提交仓库。 + +restart 加固内容: + +- restart 头部包含格式名、软件版本和状态版本。 +- restart 保存当前 0.1 系列已有通用粒子状态、时间、步数和输出控制。 +- restart read 后能够继续执行最小显式步。 +- restart 格式仍为 0.1 占位格式,不承诺长期二进制兼容。 + +## 12. CouplingInterface 范围 + +0.1.1 不实现外部耦合模块,也不依赖 FEMLite。CouplingInterface 只做中立接口加固: + +- coupling region 名称和内部 id 占位。 +- coupling particle count 查询。 +- coupling particle 状态导出占位。 +- coupling facet count 和 facet 导出占位。 +- 外部粒子力、面力或体力导入占位。 +- 耦合功、动量交换、能量交换诊断占位。 +- 接触耦合和连续体耦合类型字段占位。 + +接口、文件名和变量名中不得出现 FEMLite 内部数据结构依赖,也不得把连续体耦合简化成接触耦合。 + +## 13. 测试计划 + +0.1.1 默认 gate 建议包含: + +- `smoke`:生命周期、数据库、显式步进。 +- `input`:`.inp` 子集、warning/error、material/section/set 占位。 +- `c_api`:生命周期、空指针、非正容量、小缓冲区、粒子状态查询。 +- `cpp_api`:C++ wrapper 最小生命周期和粒子数量 smoke。 +- `python_debug`:Python `ctypes` 读入、查询、run 和错误路径。 +- `cli`:inspect、check、run、version 和错误输入。 +- `restart`:写入、读回、继续最小显式步。 +- `output`:CSV 输出字段和输出路径控制。 +- `coupling_interface`:中立导出/导入占位和错误路径。 +- `source_size`:Fortran/C/C++/Python 文件大小和 stale exception。 + +0.1.1 默认 gate 仍不包含 Water/Gas/Explosive/Solid/Shell 的真实物理 benchmark。 + +## 14. 0.1.1 Release Gate + +建议 release gate: + +```powershell +cmake --build build-codex-unix --parallel +ctest --test-dir build-codex-unix -R "^(smoke|input|c_api|cpp_api|python_debug|cli|restart|output|coupling_interface)$" --output-on-failure +ctest --test-dir build-codex-unix -R "^source_size$" --output-on-failure +git diff --check +``` + +验收标准: + +- 完整配置 CMake。 +- 完整编译 `sphlite_core`、`sphlite_c` 和 `sphlite`。 +- 新增接口测试全部通过。 +- source size 通过。 +- `git diff --check` 通过。 +- README、09 和 11 号文档记录 0.1.1 gate 结果。 +- 文档状态与实际能力一致,不宣称已实现生产级物理算法。 + +## 15. 开发阶段拆分 + +### 阶段 A:版本和常量加固 + +- 将软件版本更新为 0.1.1。 +- 锁定 phase/status 常量。 +- 同步 Fortran、C、C++ 和 Python 常量。 +- 增加常量一致性测试。 + +### 阶段 B:数据库和输入元数据 + +- 增加 material/section/set/region 占位数据结构。 +- 增加 `.inp` 对 material、section、set 和 `*SPHLITE ...` 控制项的保存。 +- 增加输入 warning/error 上下文。 +- 增加输入解析测试数据。 + +### 阶段 C:粒子查询和接口闭合 + +- 实现 Fortran 粒子状态查询。 +- 实现 C ABI 粒子状态查询。 +- 扩展 C++ wrapper。 +- 扩展 Python wrapper。 +- 增加 C/C++/Python 查询测试。 + +### 阶段 D:CLI、Output 和 Restart + +- 完善 CLI inspect/check/run/version。 +- 完善 CSV 输出字段。 +- 完善 output directory 和 interval 控制。 +- 加固 restart 头、写入、读回和继续步进。 +- 增加 CLI/output/restart 测试。 + +### 阶段 E:CouplingInterface 占位测试 + +- 完善 coupling region 占位状态。 +- 增加 coupling particle/facet 导出和载荷导入错误路径测试。 +- 增加耦合功、动量和能量诊断占位检查。 +- 确认接口命名中立。 + +### 阶段 F:文档和 Gate + +- 更新 README 当前版本。 +- 更新 `docs/09_release_history.md`。 +- 更新 `docs/11_benchmark_matrix.md`。 +- 更新 `todo.md` 或新增 0.1.1 TODO。 +- 跑通 0.1.1 release gate。 + +## 16. 风险和控制 + +| 风险 | 控制 | +|---|---| +| 0.1.1 膨胀到 0.2 Water 物理 | 只做接口、数据、输出和测试加固,不实现真实 SPH 相互作用 | +| `.inp` 解析过早追求完整 Abaqus 兼容 | 只支持明确子集,保存占位元数据,不做完整关键字覆盖 | +| C ABI 和 Fortran API 语义分叉 | Fortran API 仍是语义来源,C ABI 只做薄包装 | +| restart 格式过早固化 | 0.1.1 标注为 0.1 占位格式,只保证当前最小状态可恢复 | +| CouplingInterface 绑定外部模块 | 只使用中立命名和 SPHLite 侧 region,不出现 FEMLite 内部结构 | +| CLI 输出不稳定影响测试 | 固定 inspect/check 字段,测试按字段名检查 | +| source size 例外膨胀 | 必要例外登记,stale exception 必须失败 | + +## 17. 后续版本衔接 + +0.1.1 完成后,0.2 可集中进入 `Water` 基础 SPH: + +- 核函数模块。 +- uniform cell/bin 邻域搜索。 +- Tait EOS。 +- 连续性方程。 +- Monaghan 人工黏性。 +- 小型压力传播 smoke。 + +0.1.1 不应把上述内容实现成生产路径,但应保证数据库、输入、查询、输出和测试基础足以承接这些工作。 + +## 18. 待确认事项 + +- 0.1.1 是否需要把 `cpp_api` 纳入默认 release gate。 +- `*SPHLITE OUTPUT` 和 `*SPHLITE COUPLING REGION` 的第一版字段名。 +- restart 0.1 占位格式是否继续使用文本格式。 +- CLI `--check` 与 inspect 模式的输出差异。 +- source size 是否把 C++ 头文件和 Python 文件纳入同一门禁。 +- 0.1.1 是否需要单独创建 `todo_0.1.1.md`,还是继续维护根目录 `todo.md`。 diff --git a/docs/v2.0_development_plan.md b/docs/v2.0_development_plan.md new file mode 100644 index 0000000..29e4de6 --- /dev/null +++ b/docs/v2.0_development_plan.md @@ -0,0 +1,580 @@ +# SPHLite v2.0 开发计划 + +文档状态:0.2.0 开发计划稿 + +目标软件版本:SPHLite 0.2.0 + +依据文档: + +- `01_requirements_specification.md` +- `02_software_architecture_design.md` +- `03_detailed_design.md` +- `04_mathematical_model_and_algorithms.md` +- `05_interface_control_document.md` +- `06_test_and_verification_plan.md` +- `v1.0_development_plan.md` +- `v1.1_development_plan.md` + +## 1. 版本定位 + +SPHLite 0.2.0 是第一个真实物理计算版本,定位为 `Water` 基础 SPH 最小闭环。该版本应在 0.1.1 已完成的工程骨架、输入元数据、粒子状态查询、C/C++/Python/CLI、output、restart 和 release gate 基础上,交付可测试的 3D 水相弱可压 SPH 计算路径。 + +0.2.0 的核心价值不是完成水下爆炸,而是把真实 SPH 计算链路打通: + +```text +输入粒子状态 -> 构建邻域 -> 核函数 -> Tait EOS -> 连续性方程 -> 压力/黏性力 -> Velocity-Verlet -> 输出/restart/checker +``` + +0.2.0 不应扩展为 `Gas`、`Explosive`、`Solid`、`Shell` 或 FEM-SPH 联合运行版本。后续水下接触爆炸能力必须建立在本版本稳定的 `Water` 基础计算、邻域搜索和测试体系之上。 + +## 2. 版本目标 + +0.2.0 应完成: + +- 将软件版本更新为 `0.2.0`。 +- 建立 `Particle` 模块中的核函数和 uniform cell/bin 邻域搜索。 +- 实现每粒子独立光滑长度 `h(i)` 的核函数计算路径。 +- 实现 Wendland C2 默认核函数。 +- 保留 Wendland C4 和 cubic spline 作为可选或回归测试核函数。 +- 实现 Tait EOS 水相压力计算。 +- 实现水相连续性方程,采用密度演化作为主流程。 +- 实现 Monaghan 型人工黏性。 +- 将水相压力梯度、人工黏性和外部体力贡献接入显式加速度计算。 +- 使 Velocity-Verlet 从 0.1 系列的运动骨架升级为可调用水相力计算的真实推进流程。 +- 实现水相稳定时间步估计,至少覆盖 CFL 声速约束、加速度约束和黏性约束。 +- 完善 input/API/CLI/output/restart,使 0.2.0 水相状态可以读入、运行、输出和恢复。 +- 建立 Water 单元测试、模块测试和小型 pressure wave smoke。 +- 保持 C ABI、C++ wrapper 和 Python 调试入口与新增水相能力一致。 +- 保持 release gate 快速、稳定、小规模。 + +## 3. 明确不交付范围 + +0.2.0 不交付: + +- 生产级水下爆炸近场强冲击计算。 +- Riemann-SPH、Godunov-SPH 或高阶强冲击格式。 +- 真实 `Gas` EOS 计算、气体团簇和气泡脉动。 +- `Explosive` 解析点火、programmed burn 和产物气体注入。 +- `Solid` 3D 结构应力更新、Johnson-Cook 材料和失效。 +- `Shell` Mindlin-Reissner 板壳计算。 +- 水-气、水-结构、气-结构的生产级相互作用。 +- 外部 CouplingModule、CoupleLite、CoupledLite 或 CouplingLite。 +- 与 FEMLite 的联合运行。 +- 几何、网格或粒子生成能力。 +- 完整自由面重构、锐界面追踪、复杂边界条件库。 +- 大规模并行、GPU 后端和性能优化专项。 +- 工程 benchmark 和长跑算例。 + +0.2.0 可以为后续多物理能力保留接口和字段,但不得把占位能力描述为已实现物理算法。 + +## 4. 目录结构调整 + +0.2.0 沿用 0.1.1 目录结构,不新增 `geometry/` 或 `mesh/` 目录。 + +建议重点完善: + +```text +src/ + particle/ + kernel + neighbor + material/ + water material and EOS helpers + water/ + density, pressure, viscosity, acceleration, diagnostics + solver/ + explicit Velocity-Verlet force coupling +tests/ + unit/ + module/ + interface/ + data/ +tools/ + testing/ +``` + +测试或示例所需的 `.inp` 文件可以手写或由 Python 辅助脚本生成,但这些脚本仍只属于测试/示例辅助工具,不属于求解器几何或粒子生成功能。 + +## 5. 数据库和运行时状态范围 + +0.2.0 继续采用集中型数据库。通用粒子状态仍由 `Database` 管理,`Water` 只维护水相计算所需参数、派生量、临时数组和诊断量。 + +0.2.0 应确认或完善以下通用状态: + +- `particle_id_ext` +- `particle_phase` +- `particle_status` +- `material_id` +- `section_id` +- `x0(3,npart)` +- `x(3,npart)` +- `v(3,npart)` +- `a(3,npart)` +- `vhalf(3,npart)` +- `mass(npart)` +- `rho(npart)` +- `volume(npart)` +- `pressure(npart)` +- `internal_energy(npart)` +- `temperature(npart)` +- `h(npart)` + +0.2.0 新增或完善的运行时状态建议包括: + +- 每粒子的密度变化率 `drho_dt(npart)`。 +- 每粒子的压力加速度贡献 `acc_pressure(3,npart)`。 +- 每粒子的人工黏性加速度贡献 `acc_viscosity(3,npart)`。 +- 每粒子的外部体力加速度贡献或等效力缓存。 +- 邻域搜索 cell id、链表或压缩索引。 +- 邻域对或邻域表。 +- 每粒子邻居数量。 +- 水相总质量、总动量、最大压力、压力冲量和时间步约束诊断。 + +邻域表应由 `Particle` 或邻域模块统一维护,水相模块按 phase、material、section 或 region 过滤相互作用。0.2.0 不建立多张互相独立的邻域表。 + +## 6. 输入解析范围 + +0.2.0 在 0.1.1 `.inp` 子集基础上增加水相计算所需参数解析。仍不追求完整 Abaqus 兼容,能复用 Abaqus 关键词时不新增关键词;Abaqus 无法表达的 SPHLite 专有能力使用 `*SPHLITE ...` 扩展关键词。 + +建议输入能力: + +- `*NODE`:继续读取 3D 粒子坐标。 +- `*ELEMENT`:继续保存粒子或粒子集合连接占位,用于 set/section/material 映射。 +- `*NSET` / `*ELSET`:继续作为集合,不作为物理类型判断依据。 +- `*MATERIAL`:保存材料名称,并允许识别水材料类型。 +- `*DENSITY`:优先复用 Abaqus 关键词读取参考密度。 +- `*SECTION` 或等价 section 关键词:保存 section 与 material 引用。 +- `*SPHLITE TIME CONTROL`:继续控制 `DT`、`TIME`、`STEPS` 和安全系数。 +- `*SPHLITE OUTPUT`:继续控制输出目录和输出间隔。 +- `*SPHLITE EOS`:建议作为 Tait EOS 参数入口。 +- `*SPHLITE VISCOSITY`:建议作为 Monaghan 人工黏性参数入口。 +- `*SPHLITE PARTICLE CONTROL`:建议作为 `h`、初始质量或体积等粒子数值参数入口。 +- `*SPHLITE BODY FORCE`:可选,用于重力或小型验证算例体力。 + +建议 Tait EOS 字段: + +- `TYPE=TAIT` +- `MATERIAL=` +- `RHO0` +- `C0` +- `GAMMA` +- `P0` +- `B` 可选;若未给定,可由 `rho0*c0*c0/gamma` 计算。 + +建议 Monaghan 人工黏性字段: + +- `TYPE=MONAGHAN` +- `MATERIAL=` +- `ALPHA` +- `BETA` +- `ETA2` + +0.2.0 应对缺失关键水相参数报 error,对不影响运行的未知关键词报 warning。输入错误消息应继续包含文件名和行号上下文。 + +## 7. 核函数范围 + +0.2.0 应建立 `Particle` 核函数模块,提供: + +- `W(r,h)`。 +- `grad W(r,h)`。 +- 核函数支持半径。 +- 3D 归一化系数。 +- 核函数类型选择。 +- 对非法 `h <= 0` 和非法核函数类型的错误路径。 + +默认核函数为 Wendland C2。Wendland C4 作为可选核函数,cubic spline 作为对比和回归测试核函数。 + +单元测试应覆盖: + +- `r = 0` 处函数值有限。 +- 超出支持半径时 `W = 0` 且梯度为零。 +- 梯度方向与粒子连线方向一致。 +- 对称粒子对的梯度反对称性。 +- 不同 `h(i)` 下结果有限且符号正确。 + +0.2.0 不要求做完整三维积分归一性高精度 benchmark,但应保留后续 checker 入口。 + +## 8. 邻域搜索范围 + +0.2.0 邻域搜索采用 uniform cell/bin 方法,不引入树结构。 + +邻域搜索应支持: + +- 根据当前坐标计算全局包围盒。 +- 根据最大支持半径确定 cell 尺寸。 +- 建立粒子到 cell 的映射。 +- 遍历相邻 cell 生成邻域。 +- 支持每粒子独立 `h(i)`。 +- 对 phase、status 或 region 的过滤留给物理模块。 +- 粒子移动后可重建邻域。 + +邻域测试应覆盖: + +- 与朴素 `O(n^2)` 搜索结果一致。 +- 不同 `h(i)` 的搜索半径。 +- 空模型和单粒子模型。 +- 粒子移动后重建。 +- 边界 cell 和负坐标。 +- 最大邻居数量或邻域表容量不足时的明确错误。 + +0.2.0 默认 gate 中邻域测试应保持小规模,不进入性能基准。 + +## 9. Water 模块范围 + +0.2.0 `Water` 模块应实现弱可压 SPH 最小闭环。 + +水相 EOS: + +- 使用 Tait EOS。 +- 压力由 `rho`、`rho0`、`c0`、`gamma`、`p0` 和可选 `B` 计算。 +- 压力可允许为负值或设置拉伸限制,具体策略应在实现前写入测试说明。 + +水相密度: + +- 主流程采用连续性方程。 +- 不使用 summation density 作为主流程。 +- 0.2.0 可保留 summation density 对比 checker 的后续入口,但不进入生产路径。 + +水相动量: + +- 包含压力梯度项。 +- 包含 Monaghan 型人工黏性项。 +- 支持外部体力或体力占位。 +- 默认只处理 water-water 相互作用。 +- 对非 water phase 的粒子默认跳过,后续多相版本再引入水-气或水-结构相互作用。 + +水相诊断: + +- 水相粒子数。 +- 水相总质量。 +- 水相总动量。 +- 最大压力。 +- 最小/最大密度。 +- 压力冲量占位。 +- CFL、加速度和黏性时间步约束贡献。 + +## 10. Solver 集成范围 + +0.2.0 继续使用 Velocity-Verlet 作为唯一主时间推进格式。 + +建议单步流程: + +```text +校验模型和时间步 +必要时重建邻域 +根据 rho 计算 pressure +计算 drho_dt +计算水相压力和黏性加速度 +加入体力或外部载荷占位 +Velocity-Verlet 更新速度、位置、密度和时间 +更新诊断 +按间隔输出 +``` + +稳定时间步估计应至少考虑: + +- `dt_cfl = h / (c + |v|)` 类型的声速约束。 +- 加速度约束。 +- 人工黏性约束。 +- 输入安全系数。 + +0.2.0 不引入隐式、静力、非线性静力或隐式-显式混合求解接口。 + +## 11. Output 和 Restart 范围 + +0.2.0 CSV 输出应在 0.1.1 字段基础上确保可检查水相计算结果: + +- id +- phase +- status +- material +- section +- x +- v +- rho +- pressure +- temperature +- h + +建议增加可选诊断输出或独立 summary: + +- step +- time +- water mass +- water momentum +- max pressure +- min/max density +- dtcrit + +restart 应保存 0.2.0 继续计算所需状态: + +- 0.1 系列通用粒子状态。 +- 当前时间、步数、输出控制。 +- 速度、加速度和半步速度。 +- 水相 EOS 和人工黏性参数。 +- 密度、压力、内能、温度和 `h`。 +- 邻域搜索控制参数。 +- 水相诊断状态。 + +邻域表本身可以不写入 restart,读入 restart 后允许重建邻域,但控制参数必须可恢复。 + +## 12. Fortran API 范围 + +0.2.0 Fortran API 继续作为核心语义来源。建议在 0.1.1 API 基础上增加或完善: + +```fortran +subroutine sph_rebuild_neighbors(ierr) +subroutine sph_get_neighbor_stats(max_neighbors, average_neighbors, ierr) +subroutine sph_get_water_summary(mass, momentum, max_pressure, min_density, max_density, ierr) +subroutine sph_get_water_dt_limits(dt_cfl, dt_accel, dt_viscous, ierr) +``` + +其中 `momentum` 为三维向量。若 0.2.0 实现阶段认为公开 neighbor API 过早,可先保留为 Fortran 模块测试入口,不进入 C ABI。 + +已有 API 必须继续可用: + +- 生命周期。 +- `.inp` 读取。 +- build model。 +- stable dt。 +- advance/run explicit。 +- current time。 +- particle count/state。 +- output/restart。 +- error query。 + +所有新增可失败接口必须返回错误码,并写入最近错误消息。 + +## 13. C ABI、C++ 和 Python 范围 + +C ABI 应保持 0.1.1 已有接口兼容。0.2.0 建议增加水相诊断查询: + +```c +int sphlite_get_water_summary(double *mass, + double *momentum, + double *max_pressure, + double *min_density, + double *max_density); + +int sphlite_get_water_dt_limits(double *dt_cfl, + double *dt_accel, + double *dt_viscous); +``` + +数组约定: + +- `momentum[0..2]` 为三维总动量。 + +C 参数错误仍返回 `-1`。Fortran 物理、输入或数据库错误仍返回 SPHLite 错误码。 + +C++ wrapper 仍保持薄包装定位: + +- 包装新增水相 summary 和 dt limits。 +- 不重新定义物理语义。 +- 不作为核心实现语言。 + +Python `ctypes` 调试入口应扩展: + +- 读取水相 summary。 +- 读取 dt limits。 +- 提供小型 run/checker 辅助函数。 +- 保持缺失输入、缺失参数和错误路径测试。 + +Python 仍不是唯一入口,CLI 和 C ABI 必须完整可用。 + +## 14. CLI 范围 + +0.2.0 CLI 应保持 0.1.1 用法: + +```powershell +sphlite model.inp +sphlite model.inp --check +sphlite model.inp --run +sphlite model.inp --run --output output +sphlite --version +``` + +建议增加或稳定以下 inspect/check 输出字段: + +- `version` +- `input` +- `mode` +- `particles` +- `warnings` +- `time` +- `step` +- `status` +- `water_particles` +- `water_mass` +- `max_pressure` +- `dtcrit` + +CLI 内部仍必须走公共生命周期 API,不绕过 Fortran API 或 C ABI。 + +## 15. CouplingInterface 边界 + +0.2.0 不实现外部耦合模块,也不依赖 FEMLite。CouplingInterface 维持 0.1.1 中立接口占位。 + +0.2.0 可做的最小加固: + +- 确认水相运行后 coupling particle 导出仍可用。 +- 确认外部粒子力导入可以进入显式步体力或外载占位。 +- 继续记录耦合功、动量和能量诊断占位。 +- 保持接触耦合和连续体耦合类型区分。 + +0.2.0 默认 gate 不包含真实 FEM-SPH 联合运行。 + +## 16. 测试计划 + +0.2.0 默认 gate 建议包含: + +- `smoke`:版本 0.2.0、生命周期、显式步进和基础错误路径。 +- `input`:Water material、Tait EOS、viscosity、particle control、warning/error。 +- `kernel`:Wendland C2、Wendland C4、cubic spline 的基础性质。 +- `neighbor`:uniform cell/bin 与朴素搜索一致。 +- `water_eos`:Tait EOS 压力计算和非法参数错误。 +- `water_density`:连续性方程密度变化率方向和量级。 +- `water_viscosity`:Monaghan 人工黏性方向、有限性和对称性。 +- `water_force`:压力梯度和黏性加速度 smoke。 +- `solver_water`:Velocity-Verlet 与 Water 力计算闭环。 +- `output`:CSV 字段包含 0.2.0 水相状态。 +- `restart`:水相参数和状态写入、读回、继续最小显式步。 +- `c_api`:新增水相诊断接口和原有 C ABI 兼容性。 +- `cpp_api`:新增水相诊断薄包装 smoke。 +- `python_debug`:水相 inspect/run/checker smoke。 +- `cli`:0.2.0 inspect/check/run/version/error。 +- `pressure_wave_smoke`:小型 3D 压力扰动传播 smoke。 +- `source_size`:Fortran/C/C++/Python 文件大小和 stale exception。 + +0.2.0 的 `pressure_wave_smoke` 应只验证方向、有限性、守恒量趋势和输出字段,不作为工程精度 benchmark。 + +非默认测试可保留: + +- 更大粒子数压力传播。 +- 水柱或自由面示例。 +- 参数收敛研究。 +- 与解析波速的更严格对比。 + +这些不进入默认 release gate。 + +## 17. 0.2.0 Release Gate + +建议 release gate: + +```powershell +cmake --build build-codex-unix --parallel +ctest --test-dir build-codex-unix -R "^(smoke|input|kernel|neighbor|water_eos|water_density|water_viscosity|water_force|solver_water|output|restart|c_api|cpp_api|python_debug|cli|pressure_wave_smoke)$" --output-on-failure +ctest --test-dir build-codex-unix -R "^source_size$" --output-on-failure +git diff --check +``` + +验收标准: + +- 完整配置 CMake。 +- 完整编译 `sphlite_core`、`sphlite_c`、`sphlite` 和新增测试目标。 +- 默认 gate 全部通过。 +- source size 通过。 +- `git diff --check` 通过。 +- README、09 和 11 号文档记录 0.2.0 gate 结果。 +- 文档状态与实际能力一致,不宣称已实现水下爆炸完整能力。 +- 输出目录、CSV、VTK、restart 和 Python 缓存不进入提交。 +- 文档使用 UTF-8 without BOM。 + +## 18. 开发阶段拆分 + +### 阶段 A:版本和测试骨架 + +- 将软件版本更新为 0.2.0。 +- 新增 `kernel`、`neighbor`、`water_eos` 等 CTest 目标占位。 +- 准备最小 Water `.inp` 测试数据。 +- 保持 0.1.1 gate 继续通过。 + +### 阶段 B:核函数模块 + +- 实现 Wendland C2。 +- 实现 Wendland C4。 +- 实现 cubic spline。 +- 统一核函数类型选择。 +- 增加核函数单元测试。 + +### 阶段 C:邻域搜索模块 + +- 实现 uniform cell/bin 数据结构。 +- 实现邻域构建和重建。 +- 支持每粒子独立 `h(i)`。 +- 增加与朴素搜索对比测试。 + +### 阶段 D:Water 参数和 Tait EOS + +- 扩展 input 解析水材料、Tait EOS 和粒子控制参数。 +- 实现 Tait EOS。 +- 增加非法参数错误路径。 +- 增加 water summary 诊断占位。 + +### 阶段 E:连续性方程和人工黏性 + +- 实现 `drho_dt` 计算。 +- 实现 Monaghan 人工黏性。 +- 实现压力梯度加速度。 +- 建立水相力计算主入口。 + +### 阶段 F:Solver、Output 和 Restart + +- 将 Water 力计算接入 Velocity-Verlet。 +- 实现稳定时间步估计。 +- 扩展 CSV 输出字段和 summary。 +- 扩展 restart 保存水相状态和控制参数。 +- 增加继续运行测试。 + +### 阶段 G:接口和 CLI + +- 增加 Fortran 水相诊断 API。 +- 增加 C ABI 水相诊断 API。 +- 扩展 C++ wrapper。 +- 扩展 Python 调试入口。 +- 扩展 CLI inspect/check/run 输出。 + +### 阶段 H:Pressure Wave Smoke 和 Gate + +- 创建小型 3D pressure wave `.inp`。 +- 实现 pressure wave checker。 +- 更新 README、09 和 11 号文档。 +- 更新 `todo.md`。 +- 跑通 0.2.0 release gate。 + +## 19. 风险和控制 + +| 风险 | 控制 | +|---|---| +| 0.2.0 膨胀为完整水下爆炸版本 | 只交付 Water 基础弱可压 SPH,不做 Gas/Explosive/Solid/Shell 生产路径 | +| 邻域搜索过早复杂化 | 只做 uniform cell/bin,树结构和并行优化推迟 | +| 输入参数语法摇摆 | 先锁定 Tait EOS、viscosity 和 particle control 的最小字段 | +| 压力波 smoke 变成长跑 benchmark | 默认 gate 只保留小粒子数、短时间、趋势检查 | +| Tait EOS 单位混乱 | 继续沿用 Abaqus/FEMLite 风格,不做内置单位换算,示例中明确参数单位一致性 | +| 人工黏性导致数值不稳定 | 从小型对称测试和有限性测试开始,默认参数保守 | +| restart 状态缺失导致继续运行不一致 | 0.2.0 明确保存水相参数、密度、压力、速度、加速度和时间控制 | +| C ABI 与 Fortran API 分叉 | Fortran API 仍是语义来源,C ABI 只做薄包装 | +| 文档宣称超过实际能力 | README、09、11 和本计划均明确 0.2.0 不交付水下爆炸完整能力 | + +## 20. 后续版本衔接 + +0.2.0 完成后,0.3 可集中进入 `Gas`: + +- JWL EOS。 +- 理想气体 EOS。 +- Stiffened gas EOS。 +- 气体总体积、中心、平均压力和最大压力。 +- 等效气泡半径和半径变化率。 +- 气体团簇统计。 + +0.2.0 应为 0.3 保留统一粒子数据库、phase 过滤、邻域搜索、输出、restart 和诊断接口,但不提前实现气体生产计算。 + +## 21. 待确认事项 + +- Tait EOS 第一版是否允许负压,或是否设置拉伸压力限制。 +- `*SPHLITE EOS`、`*SPHLITE VISCOSITY` 和 `*SPHLITE PARTICLE CONTROL` 的最终字段名。 +- 初始质量、体积和 `h` 是统一给定、逐粒子给定,还是由 section/material 默认值推导。 +- Monaghan 人工黏性默认 `alpha`、`beta` 和 `eta2` 建议值。 +- `pressure_wave_smoke` 的最小粒子布置和 checker 阈值。 +- 水相 summary 是否进入 C ABI,还是先保留为 Fortran/Python 测试入口。 +- 0.2.0 是否需要新增 VTK 输出占位,或继续只用 CSV 作为默认 gate 输出。 diff --git a/include/sphlite.hpp b/include/sphlite.hpp index d28acec..9ed1e26 100644 --- a/include/sphlite.hpp +++ b/include/sphlite.hpp @@ -5,15 +5,51 @@ #include #include +#include namespace sphlite { +enum Phase { + PhaseWater = SPHLITE_PHASE_WATER, + PhaseGas = SPHLITE_PHASE_GAS, + PhaseExplosive = SPHLITE_PHASE_EXPLOSIVE, + PhaseSolid = SPHLITE_PHASE_SOLID, + PhaseShell = SPHLITE_PHASE_SHELL, + PhaseBoundary = SPHLITE_PHASE_BOUNDARY, + PhaseCoupling = SPHLITE_PHASE_COUPLING, + PhaseSolidFailed = SPHLITE_PHASE_SOLID_FAILED, + PhaseShellFailed = SPHLITE_PHASE_SHELL_FAILED +}; + +enum Status { + StatusActive = SPHLITE_STATUS_ACTIVE, + StatusInactive = SPHLITE_STATUS_INACTIVE, + StatusFailed = SPHLITE_STATUS_FAILED, + StatusReserved = SPHLITE_STATUS_RESERVED +}; + struct Version { int major; int minor; int patch; }; +struct ParticleState { + std::vector particle_id_ext; + std::vector phase; + std::vector status; + std::vector material_id; + std::vector section_id; + std::vector x; + std::vector v; + std::vector a; + std::vector rho; + std::vector pressure; + std::vector internal_energy; + std::vector temperature; + std::vector h; +}; + class Error : public std::runtime_error { public: explicit Error(const std::string &message) : std::runtime_error(message) {} @@ -75,6 +111,78 @@ inline void run_explicit() check(sphlite_run_explicit()); } +inline ParticleState particle_state() +{ + int count = particle_count(); + ParticleState state; + state.particle_id_ext.resize(count); + state.phase.resize(count); + state.status.resize(count); + state.material_id.resize(count); + state.section_id.resize(count); + state.x.resize(3 * count); + state.v.resize(3 * count); + state.a.resize(3 * count); + state.rho.resize(count); + state.pressure.resize(count); + state.internal_energy.resize(count); + state.temperature.resize(count); + state.h.resize(count); + int actual = 0; + check(sphlite_get_particle_state(count, state.particle_id_ext.data(), + state.phase.data(), state.status.data(), state.material_id.data(), + state.section_id.data(), state.x.data(), state.v.data(), state.a.data(), + state.rho.data(), state.pressure.data(), state.internal_energy.data(), + state.temperature.data(), state.h.data(), &actual)); + return state; +} + +inline void set_output_directory(const std::string &path) +{ + check(sphlite_set_output_directory(path.c_str())); +} + +inline void write_output(int step, double time) +{ + check(sphlite_write_output(step, time)); +} + +inline void write_restart(const std::string &filename) +{ + check(sphlite_write_restart(filename.c_str())); +} + +inline void read_restart(const std::string &filename) +{ + check(sphlite_read_restart(filename.c_str())); +} + +class Session { +public: + Session() : active_(true) { initialize(); } + + ~Session() + { + if (active_) { + sphlite_finalize(); + } + } + + Session(const Session &) = delete; + Session &operator=(const Session &) = delete; + + void close() + { + if (active_) { + finalize(); + active_ = false; + } + } + +private: + bool active_; +}; + } // namespace sphlite #endif diff --git a/include/sphlite_c_api.h b/include/sphlite_c_api.h index 28bbd3e..6a5f977 100644 --- a/include/sphlite_c_api.h +++ b/include/sphlite_c_api.h @@ -25,6 +25,11 @@ extern "C" { #define SPHLITE_PHASE_SOLID_FAILED 8 #define SPHLITE_PHASE_SHELL_FAILED 9 +#define SPHLITE_STATUS_ACTIVE 0 +#define SPHLITE_STATUS_INACTIVE 1 +#define SPHLITE_STATUS_FAILED 2 +#define SPHLITE_STATUS_RESERVED 3 + SPHLITE_API int sphlite_initialize(void); SPHLITE_API int sphlite_finalize(void); SPHLITE_API int sphlite_get_version(int *major, int *minor, int *patch); @@ -36,9 +41,28 @@ SPHLITE_API int sphlite_advance_explicit(double dt); SPHLITE_API int sphlite_run_explicit(void); SPHLITE_API int sphlite_get_current_time(double *time, int *step); SPHLITE_API int sphlite_get_particle_count(int *count); +SPHLITE_API int sphlite_get_input_warning_count(int *count); +SPHLITE_API int sphlite_set_output_directory(const char *path); +SPHLITE_API int sphlite_set_output_interval(int interval); +SPHLITE_API int sphlite_write_output(int step, double time); SPHLITE_API int sphlite_write_csv(const char *filename); SPHLITE_API int sphlite_write_restart(const char *filename); SPHLITE_API int sphlite_read_restart(const char *filename); +SPHLITE_API int sphlite_get_particle_state(int max_particles, + int *particle_id_ext, + int *phase, + int *status, + int *material_id, + int *section_id, + double *x, + double *v, + double *a, + double *rho, + double *pressure, + double *internal_energy, + double *temperature, + double *h, + int *count); #ifdef __cplusplus } diff --git a/src/app/sph_main.c b/src/app/sph_main.c index 700e245..d7fe731 100644 --- a/src/app/sph_main.c +++ b/src/app/sph_main.c @@ -8,13 +8,14 @@ static void sphlite_print_error(void) int code = 0; char message[256]; if (sphlite_get_last_error(&code, message, (int)sizeof(message)) == 0) { - fprintf(stderr, "SPHLite error %d: %s\n", code, message); + fprintf(stderr, "error_code: %d\n", code); + fprintf(stderr, "error_message: %s\n", message); } } static void sphlite_print_usage(void) { - printf("usage: sphlite model.inp [--run]\n"); + printf("usage: sphlite [--version] model.inp [--check|--run] [--output path]\n"); } int main(int argc, char **argv) @@ -25,19 +26,50 @@ int main(int argc, char **argv) int patch = 0; int count = 0; int step = 0; + int warnings = 0; double time = 0.0; int run_mode = 0; + int check_mode = 0; + const char *input = NULL; + const char *output = NULL; + int i; - if (argc < 2 || argc > 3) { + if (argc == 2 && strcmp(argv[1], "--version") == 0) { + ierr = sphlite_get_version(&major, &minor, &patch); + if (ierr != 0) { + sphlite_print_error(); + return 1; + } + printf("version: %d.%d.%d\n", major, minor, patch); + return 0; + } + + if (argc < 2) { sphlite_print_usage(); return 1; } - if (argc == 3) { - if (strcmp(argv[2], "--run") != 0) { + + input = argv[1]; + for (i = 2; i < argc; ++i) { + if (strcmp(argv[i], "--run") == 0) { + run_mode = 1; + } else if (strcmp(argv[i], "--check") == 0) { + check_mode = 1; + } else if (strcmp(argv[i], "--output") == 0) { + if (i + 1 >= argc) { + sphlite_print_usage(); + return 1; + } + output = argv[i + 1]; + ++i; + } else { sphlite_print_usage(); return 1; } - run_mode = 1; + } + if (run_mode && check_mode) { + sphlite_print_usage(); + return 1; } ierr = sphlite_initialize(); @@ -53,7 +85,16 @@ int main(int argc, char **argv) return 1; } - ierr = sphlite_read_inp(argv[1]); + if (output != NULL) { + ierr = sphlite_set_output_directory(output); + if (ierr != 0) { + sphlite_print_error(); + (void)sphlite_finalize(); + return 1; + } + } + + ierr = sphlite_read_inp(input); if (ierr != 0) { sphlite_print_error(); (void)sphlite_finalize(); @@ -74,6 +115,14 @@ int main(int argc, char **argv) (void)sphlite_finalize(); return 1; } + if (output != NULL) { + ierr = sphlite_write_output(1, 0.0); + if (ierr != 0) { + sphlite_print_error(); + (void)sphlite_finalize(); + return 1; + } + } } ierr = sphlite_get_particle_count(&count); @@ -88,13 +137,21 @@ int main(int argc, char **argv) (void)sphlite_finalize(); return 1; } + ierr = sphlite_get_input_warning_count(&warnings); + if (ierr != 0) { + sphlite_print_error(); + (void)sphlite_finalize(); + return 1; + } - printf("SPHLite %d.%d.%d\n", major, minor, patch); - printf("input: %s\n", argv[1]); - printf("mode: %s\n", run_mode ? "run" : "inspect"); + printf("version: %d.%d.%d\n", major, minor, patch); + printf("input: %s\n", input); + printf("mode: %s\n", run_mode ? "run" : (check_mode ? "check" : "inspect")); printf("particles: %d\n", count); + printf("warnings: %d\n", warnings); printf("time: %.16e\n", time); printf("step: %d\n", step); + printf("status: ok\n"); ierr = sphlite_finalize(); if (ierr != 0) { diff --git a/src/core/sph_precision.f90 b/src/core/sph_precision.f90 index a349586..9b0070a 100644 --- a/src/core/sph_precision.f90 +++ b/src/core/sph_precision.f90 @@ -4,7 +4,7 @@ module sph_precision integer, parameter :: rk = selected_real_kind(12, 100) integer, parameter :: SPH_VERSION_MAJOR = 0 integer, parameter :: SPH_VERSION_MINOR = 1 - integer, parameter :: SPH_VERSION_PATCH = 0 + integer, parameter :: SPH_VERSION_PATCH = 1 integer, parameter :: SPH_NDIM = 3 integer, parameter :: SPH_ERROR_MESSAGE_LEN = 256 integer, parameter :: SPH_PATH_LEN = 1024 diff --git a/src/coupling_interface/sph_coupling_interface.f90 b/src/coupling_interface/sph_coupling_interface.f90 index ac98e72..ec1fda6 100644 --- a/src/coupling_interface/sph_coupling_interface.f90 +++ b/src/coupling_interface/sph_coupling_interface.f90 @@ -5,15 +5,19 @@ module sph_coupling_interface implicit none integer :: coupling_region_count = 0 + integer :: coupling_type = 0 real(rk) :: coupling_work = 0.0_rk real(rk) :: coupling_energy = 0.0_rk + real(rk) :: coupling_momentum(SPH_NDIM) = 0.0_rk contains subroutine sph_coupling_reset() coupling_region_count = 0 + coupling_type = 0 coupling_work = 0.0_rk coupling_energy = 0.0_rk + coupling_momentum = 0.0_rk end subroutine sph_coupling_reset subroutine sph_coupling_get_particle_count(count, ierr) @@ -61,7 +65,7 @@ contains node_ids = 0 ierr = SPH_SUCCESS if (index /= 1) then - call sph_error_set(SPH_ERROR_ARGUMENT, 'no coupling facets are available in 0.1.0') + call sph_error_set(SPH_ERROR_ARGUMENT, 'no coupling facets are available in 0.1.1') ierr = SPH_ERROR_ARGUMENT end if end subroutine sph_coupling_export_facet @@ -78,6 +82,7 @@ contains return end if coupling_work = coupling_work + sum(force) + coupling_momentum(:) = coupling_momentum(:) + force(:) end subroutine sph_coupling_import_particle_force subroutine sph_coupling_import_facet_load(index, load, ierr) @@ -92,6 +97,17 @@ contains return end if coupling_energy = coupling_energy + sum(load) + coupling_momentum(:) = coupling_momentum(:) + load(:) end subroutine sph_coupling_import_facet_load + subroutine sph_coupling_get_diagnostics(work, energy, momentum) + real(rk), intent(out) :: work + real(rk), intent(out) :: energy + real(rk), intent(out) :: momentum(SPH_NDIM) + + work = coupling_work + energy = coupling_energy + momentum(:) = coupling_momentum(:) + end subroutine sph_coupling_get_diagnostics + end module sph_coupling_interface diff --git a/src/database/sph_database.f90 b/src/database/sph_database.f90 index 2808508..c92a57f 100644 --- a/src/database/sph_database.f90 +++ b/src/database/sph_database.f90 @@ -3,6 +3,14 @@ module sph_database use sph_error, only: SPH_SUCCESS, SPH_ERROR_ARGUMENT, SPH_ERROR_DATABASE, sph_error_set implicit none + integer, parameter :: SPH_NAME_LEN = 64 + integer, parameter :: SPH_MAX_MATERIALS = 32 + integer, parameter :: SPH_MAX_SECTIONS = 32 + integer, parameter :: SPH_MAX_SETS = 32 + integer, parameter :: SPH_MAX_REGIONS = 32 + integer, parameter :: SPH_MAX_ELEMENTS = 256 + integer, parameter :: SPH_MAX_ELEMENT_NODES = 8 + integer, parameter :: SPH_PHASE_WATER = 1 integer, parameter :: SPH_PHASE_GAS = 2 integer, parameter :: SPH_PHASE_EXPLOSIVE = 3 @@ -16,6 +24,10 @@ module sph_database integer, parameter :: SPH_STATUS_ACTIVE = 0 integer, parameter :: SPH_STATUS_INACTIVE = 1 integer, parameter :: SPH_STATUS_FAILED = 2 + integer, parameter :: SPH_STATUS_RESERVED = 3 + + integer, parameter :: SPH_REGION_COUPLING_CONTACT = 1 + integer, parameter :: SPH_REGION_COUPLING_CONTINUUM = 2 integer :: npart = 0 integer, allocatable :: particle_id_ext(:) @@ -36,6 +48,41 @@ module sph_database real(rk), allocatable :: temperature(:) real(rk), allocatable :: h(:) + integer :: material_count = 0 + character(len=SPH_NAME_LEN) :: material_name(SPH_MAX_MATERIALS) + character(len=SPH_NAME_LEN) :: material_type(SPH_MAX_MATERIALS) + integer :: material_internal_id(SPH_MAX_MATERIALS) + + integer :: section_count = 0 + character(len=SPH_NAME_LEN) :: section_name(SPH_MAX_SECTIONS) + character(len=SPH_NAME_LEN) :: section_type(SPH_MAX_SECTIONS) + character(len=SPH_NAME_LEN) :: section_material_name(SPH_MAX_SECTIONS) + integer :: section_material_id(SPH_MAX_SECTIONS) + real(rk) :: section_thickness(SPH_MAX_SECTIONS) + integer :: section_integration_points(SPH_MAX_SECTIONS) + + integer :: nset_count = 0 + character(len=SPH_NAME_LEN) :: nset_name(SPH_MAX_SETS) + integer :: nset_member_count(SPH_MAX_SETS) + integer :: nset_first_member(SPH_MAX_SETS) + + integer :: elset_count = 0 + character(len=SPH_NAME_LEN) :: elset_name(SPH_MAX_SETS) + integer :: elset_member_count(SPH_MAX_SETS) + integer :: elset_first_member(SPH_MAX_SETS) + + integer :: element_count = 0 + integer :: element_id_ext(SPH_MAX_ELEMENTS) + character(len=SPH_NAME_LEN) :: element_type(SPH_MAX_ELEMENTS) + integer :: element_conn_count(SPH_MAX_ELEMENTS) + integer :: element_conn(SPH_MAX_ELEMENT_NODES, SPH_MAX_ELEMENTS) + + integer :: coupling_region_count = 0 + character(len=SPH_NAME_LEN) :: coupling_region_name(SPH_MAX_REGIONS) + character(len=SPH_NAME_LEN) :: coupling_region_set_name(SPH_MAX_REGIONS) + integer :: coupling_region_type(SPH_MAX_REGIONS) + integer :: coupling_region_internal_id(SPH_MAX_REGIONS) + contains subroutine sph_db_allocate(count, ierr) @@ -114,8 +161,41 @@ contains if (allocated(temperature)) deallocate(temperature) if (allocated(h)) deallocate(h) npart = 0 + call sph_db_clear_metadata() end subroutine sph_db_free + subroutine sph_db_clear_metadata() + material_count = 0 + material_name = ' ' + material_type = ' ' + material_internal_id = 0 + section_count = 0 + section_name = ' ' + section_type = ' ' + section_material_name = ' ' + section_material_id = 0 + section_thickness = 0.0_rk + section_integration_points = 0 + nset_count = 0 + nset_name = ' ' + nset_member_count = 0 + nset_first_member = 0 + elset_count = 0 + elset_name = ' ' + elset_member_count = 0 + elset_first_member = 0 + element_count = 0 + element_id_ext = 0 + element_type = ' ' + element_conn_count = 0 + element_conn = 0 + coupling_region_count = 0 + coupling_region_name = ' ' + coupling_region_set_name = ' ' + coupling_region_type = 0 + coupling_region_internal_id = 0 + end subroutine sph_db_clear_metadata + subroutine sph_db_get_particle_count(count, ierr) integer, intent(out) :: count integer, intent(out) :: ierr @@ -124,6 +204,63 @@ contains ierr = SPH_SUCCESS end subroutine sph_db_get_particle_count + subroutine sph_db_get_particle_state_capacity(count, ndim, ierr) + integer, intent(out) :: count + integer, intent(out) :: ndim + integer, intent(out) :: ierr + + count = npart + ndim = SPH_NDIM + ierr = SPH_SUCCESS + end subroutine sph_db_get_particle_state_capacity + + subroutine sph_db_get_particle_state(max_particles, particle_id_ext_out, & + phase_out, status_out, material_id_out, section_id_out, x_out, v_out, & + a_out, rho_out, pressure_out, internal_energy_out, temperature_out, & + h_out, count, ierr) + integer, intent(in) :: max_particles + integer, intent(out) :: particle_id_ext_out(max_particles) + integer, intent(out) :: phase_out(max_particles) + integer, intent(out) :: status_out(max_particles) + integer, intent(out) :: material_id_out(max_particles) + integer, intent(out) :: section_id_out(max_particles) + real(rk), intent(out) :: x_out(SPH_NDIM,max_particles) + real(rk), intent(out) :: v_out(SPH_NDIM,max_particles) + real(rk), intent(out) :: a_out(SPH_NDIM,max_particles) + real(rk), intent(out) :: rho_out(max_particles) + real(rk), intent(out) :: pressure_out(max_particles) + real(rk), intent(out) :: internal_energy_out(max_particles) + real(rk), intent(out) :: temperature_out(max_particles) + real(rk), intent(out) :: h_out(max_particles) + integer, intent(out) :: count + integer, intent(out) :: ierr + integer :: i + + ierr = SPH_SUCCESS + count = npart + if (max_particles < npart) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'particle state buffer is too small') + ierr = SPH_ERROR_ARGUMENT + return + end if + + do i = 1, npart + particle_id_ext_out(i) = particle_id_ext(i) + phase_out(i) = particle_phase(i) + status_out(i) = particle_status(i) + material_id_out(i) = material_id(i) + section_id_out(i) = section_id(i) + x_out(:,i) = x(:,i) + v_out(:,i) = v(:,i) + a_out(:,i) = a(:,i) + rho_out(i) = rho(i) + pressure_out(i) = pressure(i) + internal_energy_out(i) = internal_energy(i) + temperature_out(i) = temperature(i) + h_out(i) = h(i) + end do + end subroutine sph_db_get_particle_state + subroutine sph_db_set_node(index, ext_id, coords, ierr) integer, intent(in) :: index integer, intent(in) :: ext_id @@ -141,4 +278,124 @@ contains x(:,index) = coords(:) end subroutine sph_db_set_node + subroutine sph_db_add_material(name, mtype, ierr) + character(len=*), intent(in) :: name + character(len=*), intent(in) :: mtype + integer, intent(out) :: ierr + + ierr = SPH_SUCCESS + if (material_count >= SPH_MAX_MATERIALS) then + call sph_error_set(SPH_ERROR_DATABASE, 'too many material records') + ierr = SPH_ERROR_DATABASE + return + end if + material_count = material_count + 1 + material_name(material_count) = name + material_type(material_count) = mtype + material_internal_id(material_count) = material_count + end subroutine sph_db_add_material + + subroutine sph_db_add_section(name, stype, mat_name, thickness, points, ierr) + character(len=*), intent(in) :: name + character(len=*), intent(in) :: stype + character(len=*), intent(in) :: mat_name + real(rk), intent(in) :: thickness + integer, intent(in) :: points + integer, intent(out) :: ierr + + ierr = SPH_SUCCESS + if (section_count >= SPH_MAX_SECTIONS) then + call sph_error_set(SPH_ERROR_DATABASE, 'too many section records') + ierr = SPH_ERROR_DATABASE + return + end if + section_count = section_count + 1 + section_name(section_count) = name + section_type(section_count) = stype + section_material_name(section_count) = mat_name + section_material_id(section_count) = 1 + section_thickness(section_count) = thickness + section_integration_points(section_count) = points + end subroutine sph_db_add_section + + subroutine sph_db_add_nset(name, member_count, first_member, ierr) + character(len=*), intent(in) :: name + integer, intent(in) :: member_count + integer, intent(in) :: first_member + integer, intent(out) :: ierr + + ierr = SPH_SUCCESS + if (nset_count >= SPH_MAX_SETS) then + call sph_error_set(SPH_ERROR_DATABASE, 'too many node set records') + ierr = SPH_ERROR_DATABASE + return + end if + nset_count = nset_count + 1 + nset_name(nset_count) = name + nset_member_count(nset_count) = member_count + nset_first_member(nset_count) = first_member + end subroutine sph_db_add_nset + + subroutine sph_db_add_elset(name, member_count, first_member, ierr) + character(len=*), intent(in) :: name + integer, intent(in) :: member_count + integer, intent(in) :: first_member + integer, intent(out) :: ierr + + ierr = SPH_SUCCESS + if (elset_count >= SPH_MAX_SETS) then + call sph_error_set(SPH_ERROR_DATABASE, 'too many element set records') + ierr = SPH_ERROR_DATABASE + return + end if + elset_count = elset_count + 1 + elset_name(elset_count) = name + elset_member_count(elset_count) = member_count + elset_first_member(elset_count) = first_member + end subroutine sph_db_add_elset + + subroutine sph_db_add_element(ext_id, etype, conn_count, conn_values, ierr) + integer, intent(in) :: ext_id + character(len=*), intent(in) :: etype + integer, intent(in) :: conn_count + integer, intent(in) :: conn_values(SPH_MAX_ELEMENT_NODES) + integer, intent(out) :: ierr + integer :: n + + ierr = SPH_SUCCESS + if (element_count >= SPH_MAX_ELEMENTS) then + call sph_error_set(SPH_ERROR_DATABASE, 'too many element records') + ierr = SPH_ERROR_DATABASE + return + end if + element_count = element_count + 1 + element_id_ext(element_count) = ext_id + element_type(element_count) = etype + n = conn_count + if (n > SPH_MAX_ELEMENT_NODES) n = SPH_MAX_ELEMENT_NODES + if (n < 0) n = 0 + element_conn_count(element_count) = n + element_conn(:,element_count) = 0 + if (n > 0) element_conn(1:n,element_count) = conn_values(1:n) + end subroutine sph_db_add_element + + subroutine sph_db_add_coupling_region(name, set_name, region_type, ierr) + character(len=*), intent(in) :: name + character(len=*), intent(in) :: set_name + integer, intent(in) :: region_type + integer, intent(out) :: ierr + + ierr = SPH_SUCCESS + if (coupling_region_count >= SPH_MAX_REGIONS) then + call sph_error_set(SPH_ERROR_DATABASE, 'too many coupling region records') + ierr = SPH_ERROR_DATABASE + return + end if + coupling_region_count = coupling_region_count + 1 + coupling_region_name(coupling_region_count) = name + coupling_region_set_name(coupling_region_count) = set_name + coupling_region_type(coupling_region_count) = region_type + coupling_region_internal_id(coupling_region_count) = coupling_region_count + end subroutine sph_db_add_coupling_region + end module sph_database diff --git a/src/input/sph_input_reader.f90 b/src/input/sph_input_reader.f90 index 77ea7e4..23d4246 100644 --- a/src/input/sph_input_reader.f90 +++ b/src/input/sph_input_reader.f90 @@ -2,7 +2,12 @@ module sph_input_reader use sph_precision, only: rk, SPH_NDIM use sph_error, only: SPH_SUCCESS, SPH_ERROR_IO, SPH_ERROR_INPUT, sph_error_set use sph_control, only: sph_control_set_time - use sph_database, only: npart, sph_db_allocate, sph_db_free, sph_db_set_node + use sph_output_csv, only: sph_output_set_directory, sph_output_set_interval + use sph_database, only: npart, SPH_MAX_ELEMENT_NODES, & + SPH_REGION_COUPLING_CONTACT, SPH_REGION_COUPLING_CONTINUUM, & + sph_db_allocate, sph_db_free, sph_db_set_node, sph_db_add_material, & + sph_db_add_section, sph_db_add_nset, sph_db_add_elset, & + sph_db_add_element, sph_db_add_coupling_region use sph_inp_defs, only: SPH_INPUT_LINE_LEN, SPH_INPUT_FIELD_LEN, & SPH_INPUT_MAX_FIELDS, SPH_KEY_NODE, SPH_KEY_ELEMENT, SPH_KEY_NSET, & SPH_KEY_ELSET, SPH_KEY_MATERIAL, SPH_KEY_SECTION, SPH_KEY_SPHLITE, & @@ -16,6 +21,11 @@ module sph_input_reader logical :: input_has_material = .false. logical :: input_has_section = .false. logical :: input_has_time_control = .false. + character(len=SPH_INPUT_LINE_LEN) :: input_current_file = ' ' + integer :: input_current_line = 0 + character(len=SPH_INPUT_FIELD_LEN) :: active_element_type = 'UNKNOWN' + character(len=SPH_INPUT_FIELD_LEN) :: active_nset_name = 'NSET' + character(len=SPH_INPUT_FIELD_LEN) :: active_elset_name = 'ELSET' contains @@ -24,6 +34,11 @@ contains input_has_material = .false. input_has_section = .false. input_has_time_control = .false. + input_current_file = ' ' + input_current_line = 0 + active_element_type = 'UNKNOWN' + active_nset_name = 'NSET' + active_elset_name = 'ELSET' end subroutine sph_input_reset subroutine sph_input_get_warning_count(count) @@ -38,11 +53,12 @@ contains integer :: node_count call sph_input_reset() + input_current_file = filename call sph_input_scan(filename, node_count, ierr) if (ierr /= SPH_SUCCESS) return call sph_db_allocate(node_count, ierr) if (ierr /= SPH_SUCCESS) return - call sph_input_fill_nodes(filename, node_count, ierr) + call sph_input_fill_model(filename, node_count, ierr) end subroutine sph_input_read subroutine sph_input_build_model(ierr) @@ -80,10 +96,12 @@ contains integer :: ios integer :: active_key integer :: key_id + integer :: line_number ierr = SPH_SUCCESS node_count = 0 active_key = SPH_KEY_UNKNOWN + line_number = 0 open(unit=SPH_INPUT_UNIT, file=trim(filename), status='old', action='read', iostat=ios) if (ios /= 0) then @@ -95,13 +113,13 @@ contains do read(SPH_INPUT_UNIT, '(A)', iostat=ios) raw_line if (ios /= 0) exit + line_number = line_number + 1 line = sph_inp_clean_line(raw_line) if (len_trim(line) == 0) cycle if (line(1:1) == '*') then key_id = sph_inp_keyword_id(line) active_key = key_id - call sph_input_record_keyword(key_id, line) else if (active_key == SPH_KEY_NODE) then node_count = node_count + 1 end if @@ -110,7 +128,7 @@ contains close(unit=SPH_INPUT_UNIT) end subroutine sph_input_scan - subroutine sph_input_fill_nodes(filename, expected_nodes, ierr) + subroutine sph_input_fill_model(filename, expected_nodes, ierr) character(len=*), intent(in) :: filename integer, intent(in) :: expected_nodes integer, intent(out) :: ierr @@ -120,10 +138,12 @@ contains integer :: active_key integer :: key_id integer :: node_index + integer :: line_number ierr = SPH_SUCCESS active_key = SPH_KEY_UNKNOWN node_index = 0 + line_number = 0 if (expected_nodes == 0) then call sph_db_free() @@ -140,12 +160,19 @@ contains do read(SPH_INPUT_UNIT, '(A)', iostat=ios) raw_line if (ios /= 0) exit + line_number = line_number + 1 + input_current_line = line_number line = sph_inp_clean_line(raw_line) if (len_trim(line) == 0) cycle if (line(1:1) == '*') then key_id = sph_inp_keyword_id(line) active_key = key_id + call sph_input_record_keyword(key_id, line, ierr) + if (ierr /= SPH_SUCCESS) then + close(unit=SPH_INPUT_UNIT) + return + end if else if (active_key == SPH_KEY_NODE) then node_index = node_index + 1 call sph_input_parse_node(line, node_index, ierr) @@ -153,28 +180,132 @@ contains close(unit=SPH_INPUT_UNIT) return end if + else if (active_key == SPH_KEY_ELEMENT) then + call sph_input_parse_element(line, ierr) + if (ierr /= SPH_SUCCESS) then + close(unit=SPH_INPUT_UNIT) + return + end if + else if (active_key == SPH_KEY_NSET) then + call sph_input_parse_set_members(line, active_nset_name, .true., ierr) + if (ierr /= SPH_SUCCESS) then + close(unit=SPH_INPUT_UNIT) + return + end if + else if (active_key == SPH_KEY_ELSET) then + call sph_input_parse_set_members(line, active_elset_name, .false., ierr) + if (ierr /= SPH_SUCCESS) then + close(unit=SPH_INPUT_UNIT) + return + end if end if end do close(unit=SPH_INPUT_UNIT) - end subroutine sph_input_fill_nodes + end subroutine sph_input_fill_model - subroutine sph_input_record_keyword(key_id, line) + subroutine sph_input_record_keyword(key_id, line, ierr) integer, intent(in) :: key_id character(len=*), intent(in) :: line + integer, intent(out) :: ierr + character(len=SPH_INPUT_FIELD_LEN) :: value + character(len=SPH_INPUT_FIELD_LEN) :: material_value + character(len=SPH_INPUT_FIELD_LEN) :: section_value + character(len=SPH_INPUT_FIELD_LEN) :: type_value + real(rk) :: thickness + integer :: points + integer :: ios + integer :: local_ierr + ierr = SPH_SUCCESS if (key_id == SPH_KEY_MATERIAL) then input_has_material = .true. + call sph_input_get_option(line, 'NAME', value) + if (len_trim(value) == 0) value = 'MATERIAL' + call sph_input_get_option(line, 'TYPE', type_value) + if (len_trim(type_value) == 0) type_value = 'UNSPECIFIED' + call sph_db_add_material(value, type_value, ierr) else if (key_id == SPH_KEY_SECTION) then input_has_section = .true. + call sph_input_get_option(line, 'ELSET', section_value) + if (len_trim(section_value) == 0) call sph_input_get_option(line, 'NSET', section_value) + if (len_trim(section_value) == 0) section_value = 'SECTION' + call sph_input_get_option(line, 'MATERIAL', material_value) + if (len_trim(material_value) == 0) material_value = 'MATERIAL' + thickness = 0.0_rk + points = 3 + call sph_input_get_option(line, 'THICKNESS', value) + if (len_trim(value) > 0) read(value, *, iostat=ios) thickness + call sph_input_get_option(line, 'POINTS', value) + if (len_trim(value) > 0) read(value, *, iostat=ios) points + call sph_db_add_section(section_value, 'SECTION', material_value, & + thickness, points, ierr) else if (key_id == SPH_KEY_SPHLITE) then - input_has_time_control = .true. - call sph_input_parse_time_control(line) + if (index(sph_inp_upper(line), 'TIME CONTROL') > 0) then + input_has_time_control = .true. + call sph_input_parse_time_control(line) + else if (index(sph_inp_upper(line), 'OUTPUT') > 0) then + call sph_input_parse_output_control(line, ierr) + else if (index(sph_inp_upper(line), 'COUPLING REGION') > 0) then + call sph_input_parse_coupling_region(line, ierr) + end if + else if (key_id == SPH_KEY_ELEMENT) then + call sph_input_get_option(line, 'TYPE', active_element_type) + if (len_trim(active_element_type) == 0) active_element_type = 'UNKNOWN' + else if (key_id == SPH_KEY_NSET) then + call sph_input_get_option(line, 'NSET', active_nset_name) + if (len_trim(active_nset_name) == 0) active_nset_name = 'NSET' + else if (key_id == SPH_KEY_ELSET) then + call sph_input_get_option(line, 'ELSET', active_elset_name) + if (len_trim(active_elset_name) == 0) active_elset_name = 'ELSET' else if (key_id == SPH_KEY_UNKNOWN) then input_warning_count = input_warning_count + 1 end if end subroutine sph_input_record_keyword + subroutine sph_input_parse_output_control(line, ierr) + character(len=*), intent(in) :: line + integer, intent(out) :: ierr + character(len=SPH_INPUT_FIELD_LEN) :: value + integer :: interval + integer :: ios + + ierr = SPH_SUCCESS + call sph_input_get_option(line, 'DIRECTORY', value) + if (len_trim(value) == 0) call sph_input_get_option(line, 'DIR', value) + if (len_trim(value) > 0) then + call sph_output_set_directory(value, ierr) + if (ierr /= SPH_SUCCESS) return + end if + call sph_input_get_option(line, 'INTERVAL', value) + if (len_trim(value) > 0) then + read(value, *, iostat=ios) interval + if (ios == 0) call sph_output_set_interval(interval, ierr) + end if + end subroutine sph_input_parse_output_control + + subroutine sph_input_parse_coupling_region(line, ierr) + character(len=*), intent(in) :: line + integer, intent(out) :: ierr + character(len=SPH_INPUT_FIELD_LEN) :: name + character(len=SPH_INPUT_FIELD_LEN) :: set_name + character(len=SPH_INPUT_FIELD_LEN) :: type_name + integer :: region_type + + ierr = SPH_SUCCESS + call sph_input_get_option(line, 'NAME', name) + if (len_trim(name) == 0) name = 'COUPLING' + call sph_input_get_option(line, 'SET', set_name) + if (len_trim(set_name) == 0) call sph_input_get_option(line, 'NSET', set_name) + if (len_trim(set_name) == 0) call sph_input_get_option(line, 'ELSET', set_name) + if (len_trim(set_name) == 0) set_name = 'ALL' + call sph_input_get_option(line, 'TYPE', type_name) + type_name = sph_inp_upper(type_name) + region_type = SPH_REGION_COUPLING_CONTACT + if (index(type_name, 'CONTINUUM') > 0) region_type = SPH_REGION_COUPLING_CONTINUUM + call sph_db_add_coupling_region(name, set_name, region_type, ierr) + end subroutine sph_input_parse_coupling_region + subroutine sph_input_parse_time_control(line) character(len=*), intent(in) :: line character(len=SPH_INPUT_FIELD_LEN) :: fields(SPH_INPUT_MAX_FIELDS) @@ -223,31 +354,31 @@ contains ierr = SPH_SUCCESS call sph_inp_split_csv(line, fields, nfields) if (nfields < 4) then - call sph_error_set(SPH_ERROR_INPUT, 'invalid *NODE record') + call sph_input_set_error('invalid *NODE record') ierr = SPH_ERROR_INPUT return end if read(fields(1), *, iostat=ios) ext_id if (ios /= 0) then - call sph_error_set(SPH_ERROR_INPUT, 'invalid node id') + call sph_input_set_error('invalid node id') ierr = SPH_ERROR_INPUT return end if read(fields(2), *, iostat=ios) coords(1) if (ios /= 0) then - call sph_error_set(SPH_ERROR_INPUT, 'invalid node x coordinate') + call sph_input_set_error('invalid node x coordinate') ierr = SPH_ERROR_INPUT return end if read(fields(3), *, iostat=ios) coords(2) if (ios /= 0) then - call sph_error_set(SPH_ERROR_INPUT, 'invalid node y coordinate') + call sph_input_set_error('invalid node y coordinate') ierr = SPH_ERROR_INPUT return end if read(fields(4), *, iostat=ios) coords(3) if (ios /= 0) then - call sph_error_set(SPH_ERROR_INPUT, 'invalid node z coordinate') + call sph_input_set_error('invalid node z coordinate') ierr = SPH_ERROR_INPUT return end if @@ -255,4 +386,97 @@ contains call sph_db_set_node(node_index, ext_id, coords, ierr) end subroutine sph_input_parse_node + subroutine sph_input_parse_element(line, ierr) + character(len=*), intent(in) :: line + integer, intent(out) :: ierr + character(len=SPH_INPUT_FIELD_LEN) :: fields(SPH_INPUT_MAX_FIELDS) + integer :: nfields + integer :: ext_id + integer :: conn_values(SPH_MAX_ELEMENT_NODES) + integer :: conn_count + integer :: i + integer :: ios + + ierr = SPH_SUCCESS + conn_values = 0 + call sph_inp_split_csv(line, fields, nfields) + if (nfields < 2) then + call sph_input_set_error('invalid *ELEMENT record') + ierr = SPH_ERROR_INPUT + return + end if + read(fields(1), *, iostat=ios) ext_id + if (ios /= 0) then + call sph_input_set_error('invalid element id') + ierr = SPH_ERROR_INPUT + return + end if + conn_count = nfields - 1 + if (conn_count > SPH_MAX_ELEMENT_NODES) conn_count = SPH_MAX_ELEMENT_NODES + do i = 1, conn_count + read(fields(i+1), *, iostat=ios) conn_values(i) + if (ios /= 0) then + call sph_input_set_error('invalid element connectivity') + ierr = SPH_ERROR_INPUT + return + end if + end do + call sph_db_add_element(ext_id, active_element_type, conn_count, conn_values, ierr) + end subroutine sph_input_parse_element + + subroutine sph_input_parse_set_members(line, set_name, is_node_set, ierr) + character(len=*), intent(in) :: line + character(len=*), intent(in) :: set_name + logical, intent(in) :: is_node_set + integer, intent(out) :: ierr + character(len=SPH_INPUT_FIELD_LEN) :: fields(SPH_INPUT_MAX_FIELDS) + integer :: nfields + integer :: first_member + integer :: ios + + ierr = SPH_SUCCESS + first_member = 0 + call sph_inp_split_csv(line, fields, nfields) + if (nfields > 0) read(fields(1), *, iostat=ios) first_member + if (is_node_set) then + call sph_db_add_nset(set_name, nfields, first_member, ierr) + else + call sph_db_add_elset(set_name, nfields, first_member, ierr) + end if + end subroutine sph_input_parse_set_members + + subroutine sph_input_get_option(line, option_name, value) + character(len=*), intent(in) :: line + character(len=*), intent(in) :: option_name + character(len=*), intent(out) :: value + character(len=SPH_INPUT_FIELD_LEN) :: fields(SPH_INPUT_MAX_FIELDS) + character(len=SPH_INPUT_FIELD_LEN) :: item + character(len=SPH_INPUT_FIELD_LEN) :: key + integer :: nfields + integer :: i + integer :: eq_pos + + value = ' ' + call sph_inp_split_csv(line, fields, nfields) + do i = 1, nfields + item = adjustl(fields(i)) + eq_pos = index(item, '=') + if (eq_pos <= 0) cycle + key = sph_inp_upper(adjustl(item(1:eq_pos-1))) + if (trim(key) == trim(sph_inp_upper(option_name))) then + value = adjustl(item(eq_pos+1:len_trim(item))) + return + end if + end do + end subroutine sph_input_get_option + + subroutine sph_input_set_error(message) + character(len=*), intent(in) :: message + character(len=SPH_INPUT_LINE_LEN) :: full_message + + write(full_message, '(A,":",I0,": ",A)') trim(input_current_file), & + input_current_line, trim(message) + call sph_error_set(SPH_ERROR_INPUT, trim(full_message)) + end subroutine sph_input_set_error + end module sph_input_reader diff --git a/src/interface/sph_api.f90 b/src/interface/sph_api.f90 index 019df9b..8a7d2af 100644 --- a/src/interface/sph_api.f90 +++ b/src/interface/sph_api.f90 @@ -5,12 +5,14 @@ module sph_api sph_error_set, sph_error_get use sph_log, only: sph_log_close use sph_control, only: sph_control_reset, sph_control_get_time - use sph_database, only: sph_db_free, sph_db_get_particle_count - use sph_input_reader, only: sph_input_read, sph_input_build_model + use sph_database, only: sph_db_free, sph_db_get_particle_count, & + sph_db_get_particle_state_capacity, sph_db_get_particle_state + use sph_input_reader, only: sph_input_read, sph_input_build_model, & + sph_input_get_warning_count use sph_solver_explicit, only: sph_solver_estimate_stable_dt, & sph_solver_advance_explicit, sph_solver_run_explicit - use sph_output_csv, only: sph_output_write_csv, sph_output_set_directory, & - sph_output_set_interval + use sph_output_csv, only: sph_output_write_csv, sph_output_write_step, & + sph_output_set_directory, sph_output_set_interval use sph_restart, only: sph_restart_write, sph_restart_read use sph_coupling_interface, only: sph_coupling_reset use sph_particle, only: sph_particle_initialize, sph_particle_reset @@ -156,6 +158,50 @@ contains call sph_db_get_particle_count(count, ierr) end subroutine sph_get_particle_count + subroutine sph_get_particle_state_capacity(count, ndim, ierr) + integer, intent(out) :: count + integer, intent(out) :: ndim + integer, intent(out) :: ierr + + call sph_api_require_initialized(ierr) + if (ierr /= SPH_SUCCESS) then + count = 0 + ndim = 0 + return + end if + call sph_db_get_particle_state_capacity(count, ndim, ierr) + end subroutine sph_get_particle_state_capacity + + subroutine sph_get_particle_state(max_particles, particle_id_ext, phase, & + status, material_id, section_id, x, v, a, rho, pressure, & + internal_energy, temperature, h, count, ierr) + integer, intent(in) :: max_particles + integer, intent(out) :: particle_id_ext(max_particles) + integer, intent(out) :: phase(max_particles) + integer, intent(out) :: status(max_particles) + integer, intent(out) :: material_id(max_particles) + integer, intent(out) :: section_id(max_particles) + real(rk), intent(out) :: x(3,max_particles) + real(rk), intent(out) :: v(3,max_particles) + real(rk), intent(out) :: a(3,max_particles) + real(rk), intent(out) :: rho(max_particles) + real(rk), intent(out) :: pressure(max_particles) + real(rk), intent(out) :: internal_energy(max_particles) + real(rk), intent(out) :: temperature(max_particles) + real(rk), intent(out) :: h(max_particles) + integer, intent(out) :: count + integer, intent(out) :: ierr + + call sph_api_require_initialized(ierr) + if (ierr /= SPH_SUCCESS) then + count = 0 + return + end if + call sph_db_get_particle_state(max_particles, particle_id_ext, phase, & + status, material_id, section_id, x, v, a, rho, pressure, & + internal_energy, temperature, h, count, ierr) + end subroutine sph_get_particle_state + subroutine sph_write_csv(filename, ierr) character(len=*), intent(in) :: filename integer, intent(out) :: ierr @@ -165,6 +211,16 @@ contains call sph_output_write_csv(filename, ierr) end subroutine sph_write_csv + subroutine sph_write_output(step, time, ierr) + integer, intent(in) :: step + real(rk), intent(in) :: time + integer, intent(out) :: ierr + + call sph_api_require_initialized(ierr) + if (ierr /= SPH_SUCCESS) return + call sph_output_write_step(step, time, ierr) + end subroutine sph_write_output + subroutine sph_set_output_directory(directory, ierr) character(len=*), intent(in) :: directory integer, intent(out) :: ierr @@ -201,6 +257,19 @@ contains call sph_restart_read(filename, ierr) end subroutine sph_read_restart + subroutine sph_get_input_warning_count(count, ierr) + integer, intent(out) :: count + integer, intent(out) :: ierr + + call sph_api_require_initialized(ierr) + if (ierr /= SPH_SUCCESS) then + count = 0 + return + end if + call sph_input_get_warning_count(count) + ierr = SPH_SUCCESS + end subroutine sph_get_input_warning_count + subroutine sph_api_require_initialized(ierr) integer, intent(out) :: ierr @@ -340,6 +409,92 @@ subroutine sphft_get_particle_count(count, ierr) call sph_get_particle_count(count, ierr) end subroutine sphft_get_particle_count +subroutine sphft_get_particle_state_capacity(count, ndim, ierr) + use sph_api, only: sph_get_particle_state_capacity + implicit none + integer, intent(out) :: count + integer, intent(out) :: ndim + integer, intent(out) :: ierr + + call sph_get_particle_state_capacity(count, ndim, ierr) +end subroutine sphft_get_particle_state_capacity + +subroutine sphft_get_particle_state(max_particles, particle_id_ext, phase, & + status, material_id, section_id, x, v, a, rho, pressure, & + internal_energy, temperature, h, count, ierr) + use sph_api, only: sph_get_particle_state + use sph_precision, only: rk + implicit none + integer, intent(in) :: max_particles + integer, intent(out) :: particle_id_ext(max_particles) + integer, intent(out) :: phase(max_particles) + integer, intent(out) :: status(max_particles) + integer, intent(out) :: material_id(max_particles) + integer, intent(out) :: section_id(max_particles) + real(rk), intent(out) :: x(3,max_particles) + real(rk), intent(out) :: v(3,max_particles) + real(rk), intent(out) :: a(3,max_particles) + real(rk), intent(out) :: rho(max_particles) + real(rk), intent(out) :: pressure(max_particles) + real(rk), intent(out) :: internal_energy(max_particles) + real(rk), intent(out) :: temperature(max_particles) + real(rk), intent(out) :: h(max_particles) + integer, intent(out) :: count + integer, intent(out) :: ierr + + call sph_get_particle_state(max_particles, particle_id_ext, phase, & + status, material_id, section_id, x, v, a, rho, pressure, & + internal_energy, temperature, h, count, ierr) +end subroutine sphft_get_particle_state + +subroutine sphft_set_output_directory_chars(chars, count, ierr) + use sph_api, only: sph_set_output_directory + implicit none + integer, intent(in) :: chars(*) + integer, intent(in) :: count + integer, intent(out) :: ierr + character(len=1024) :: directory + integer :: i + integer :: n + + directory = ' ' + n = count + if (n > len(directory)) n = len(directory) + do i = 1, n + directory(i:i) = char(chars(i)) + end do + call sph_set_output_directory(directory(1:n), ierr) +end subroutine sphft_set_output_directory_chars + +subroutine sphft_set_output_interval(interval, ierr) + use sph_api, only: sph_set_output_interval + implicit none + integer, intent(in) :: interval + integer, intent(out) :: ierr + + call sph_set_output_interval(interval, ierr) +end subroutine sphft_set_output_interval + +subroutine sphft_write_output(step, time, ierr) + use sph_api, only: sph_write_output + use sph_precision, only: rk + implicit none + integer, intent(in) :: step + real(rk), intent(in) :: time + integer, intent(out) :: ierr + + call sph_write_output(step, time, ierr) +end subroutine sphft_write_output + +subroutine sphft_get_input_warning_count(count, ierr) + use sph_api, only: sph_get_input_warning_count + implicit none + integer, intent(out) :: count + integer, intent(out) :: ierr + + call sph_get_input_warning_count(count, ierr) +end subroutine sphft_get_input_warning_count + subroutine sphft_write_csv_chars(chars, count, ierr) use sph_api, only: sph_write_csv implicit none diff --git a/src/interface/sph_c_api.c b/src/interface/sph_c_api.c index 01837ef..f97d0c5 100644 --- a/src/interface/sph_c_api.c +++ b/src/interface/sph_c_api.c @@ -23,6 +23,30 @@ extern void SPHLITE_FC_sphft_get_current_time( double *time, int *step, int *ierr); extern void SPHLITE_FC_sphft_get_particle_count( int *count, int *ierr); +extern void SPHLITE_FC_sphft_get_particle_state_capacity( + int *count, int *ndim, int *ierr); +extern void SPHLITE_FC_sphft_get_particle_state( + const int *max_particles, + int *particle_id_ext, + int *phase, + int *status, + int *material_id, + int *section_id, + double *x, + double *v, + double *a, + double *rho, + double *pressure, + double *internal_energy, + double *temperature, + double *h, + int *count, + int *ierr); +extern void SPHLITE_FC_sphft_set_output_directory_chars( + const int *chars, const int *count, int *ierr); +extern void SPHLITE_FC_sphft_set_output_interval(const int *interval, int *ierr); +extern void SPHLITE_FC_sphft_write_output(const int *step, const double *time, int *ierr); +extern void SPHLITE_FC_sphft_get_input_warning_count(int *count, int *ierr); extern void SPHLITE_FC_sphft_write_csv_chars( const int *chars, const int *count, int *ierr); extern void SPHLITE_FC_sphft_write_restart_chars( @@ -170,6 +194,68 @@ int sphlite_get_particle_count(int *count) return ierr; } +int sphlite_get_input_warning_count(int *count) +{ + int ierr = 0; + if (count == NULL) { + return -1; + } + SPHLITE_FC_sphft_get_input_warning_count(count, &ierr); + return ierr; +} + +int sphlite_set_output_directory(const char *path) +{ + return sphlite_call_path(path, SPHLITE_FC_sphft_set_output_directory_chars); +} + +int sphlite_set_output_interval(int interval) +{ + int ierr = 0; + if (interval <= 0) { + return -1; + } + SPHLITE_FC_sphft_set_output_interval(&interval, &ierr); + return ierr; +} + +int sphlite_write_output(int step, double time) +{ + int ierr = 0; + SPHLITE_FC_sphft_write_output(&step, &time, &ierr); + return ierr; +} + +int sphlite_get_particle_state(int max_particles, + int *particle_id_ext, + int *phase, + int *status, + int *material_id, + int *section_id, + double *x, + double *v, + double *a, + double *rho, + double *pressure, + double *internal_energy, + double *temperature, + double *h, + int *count) +{ + int ierr = 0; + if (max_particles <= 0 || particle_id_ext == NULL || phase == NULL || + status == NULL || material_id == NULL || section_id == NULL || + x == NULL || v == NULL || a == NULL || rho == NULL || + pressure == NULL || internal_energy == NULL || temperature == NULL || + h == NULL || count == NULL) { + return -1; + } + SPHLITE_FC_sphft_get_particle_state(&max_particles, particle_id_ext, phase, + status, material_id, section_id, x, v, a, rho, pressure, + internal_energy, temperature, h, count, &ierr); + return ierr; +} + int sphlite_write_csv(const char *filename) { return sphlite_call_path( diff --git a/src/output/sph_output_csv.f90 b/src/output/sph_output_csv.f90 index 24307dd..211dad1 100644 --- a/src/output/sph_output_csv.f90 +++ b/src/output/sph_output_csv.f90 @@ -2,7 +2,8 @@ module sph_output_csv use sph_precision, only: rk use sph_error, only: SPH_SUCCESS, SPH_ERROR_ARGUMENT, SPH_ERROR_IO, sph_error_set use sph_control, only: output_directory, output_interval - use sph_database, only: npart, particle_id_ext, x, v + use sph_database, only: npart, particle_id_ext, particle_phase, particle_status, & + material_id, section_id, x, v, rho, pressure, temperature implicit none integer, parameter :: SPH_OUTPUT_UNIT = 41 @@ -50,12 +51,25 @@ contains return end if - write(SPH_OUTPUT_UNIT, '(A)') 'id,x,y,z,vx,vy,vz' + write(SPH_OUTPUT_UNIT, '(A)') & + 'id,phase,status,material,section,x,y,z,vx,vy,vz,rho,pressure,temperature' do i = 1, npart - write(SPH_OUTPUT_UNIT, '(I0,6(",",ES24.16))') particle_id_ext(i), & - x(1,i), x(2,i), x(3,i), v(1,i), v(2,i), v(3,i) + write(SPH_OUTPUT_UNIT, '(5(I0,","),6(ES24.16,","),3(ES24.16,:,","))') & + particle_id_ext(i), particle_phase(i), particle_status(i), & + material_id(i), section_id(i), x(1,i), x(2,i), x(3,i), & + v(1,i), v(2,i), v(3,i), rho(i), pressure(i), temperature(i) end do close(unit=SPH_OUTPUT_UNIT) end subroutine sph_output_write_csv + subroutine sph_output_write_step(step, time, ierr) + integer, intent(in) :: step + real(rk), intent(in) :: time + integer, intent(out) :: ierr + character(len=1024) :: filename + + write(filename, '(A,"/sphlite_",I8.8,".csv")') trim(output_directory), step + call sph_output_write_csv(filename, ierr) + end subroutine sph_output_write_step + end module sph_output_csv diff --git a/src/output/sph_restart.f90 b/src/output/sph_restart.f90 index 2a36225..1521cb2 100644 --- a/src/output/sph_restart.f90 +++ b/src/output/sph_restart.f90 @@ -1,7 +1,7 @@ module sph_restart - use sph_precision, only: rk + use sph_precision, only: rk, SPH_VERSION_MAJOR, SPH_VERSION_MINOR, SPH_VERSION_PATCH use sph_error, only: SPH_SUCCESS, SPH_ERROR_IO, SPH_ERROR_INPUT, sph_error_set - use sph_control, only: current_time, current_step + use sph_control, only: current_time, current_step, output_interval, output_directory use sph_database, only: npart, sph_db_allocate, particle_id_ext, particle_phase, & particle_status, material_id, section_id, x0, x, v, a, vhalf, mass, rho, & volume, pressure, internal_energy, temperature, h @@ -26,8 +26,11 @@ contains return end if - write(SPH_RESTART_UNIT, '(A)') 'SPHLITE_RESTART 0.1.0' + write(SPH_RESTART_UNIT, '(A,1X,I0,1X,I0,1X,I0,1X,A)') 'SPHLITE_RESTART', & + SPH_VERSION_MAJOR, SPH_VERSION_MINOR, SPH_VERSION_PATCH, 'STATE_0_1' write(SPH_RESTART_UNIT, *) current_time, current_step, npart + write(SPH_RESTART_UNIT, *) output_interval + write(SPH_RESTART_UNIT, '(A)') trim(output_directory) do i = 1, npart write(SPH_RESTART_UNIT, *) particle_id_ext(i), particle_phase(i), & particle_status(i), material_id(i), section_id(i) @@ -42,7 +45,10 @@ contains character(len=*), intent(in) :: filename integer, intent(out) :: ierr character(len=64) :: tag - character(len=32) :: version + integer :: major + integer :: minor + integer :: patch + character(len=32) :: state_version integer :: ios integer :: count integer :: i @@ -56,7 +62,7 @@ contains return end if - read(SPH_RESTART_UNIT, *, iostat=ios) tag, version + read(SPH_RESTART_UNIT, *, iostat=ios) tag, major, minor, patch, state_version if (ios /= 0 .or. trim(tag) /= 'SPHLITE_RESTART') then call sph_error_set(SPH_ERROR_INPUT, 'invalid restart header') ierr = SPH_ERROR_INPUT @@ -71,6 +77,20 @@ contains close(unit=SPH_RESTART_UNIT) return end if + read(SPH_RESTART_UNIT, *, iostat=ios) output_interval + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart output interval') + ierr = SPH_ERROR_INPUT + close(unit=SPH_RESTART_UNIT) + return + end if + read(SPH_RESTART_UNIT, '(A)', iostat=ios) output_directory + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart output directory') + ierr = SPH_ERROR_INPUT + close(unit=SPH_RESTART_UNIT) + return + end if call sph_db_allocate(count, ierr) if (ierr /= SPH_SUCCESS) then close(unit=SPH_RESTART_UNIT) diff --git a/tests/data/coupling_region_011.inp b/tests/data/coupling_region_011.inp new file mode 100644 index 0000000..d845a86 --- /dev/null +++ b/tests/data/coupling_region_011.inp @@ -0,0 +1,9 @@ +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +*NSET, NSET=COUPLED +1, 2 +*MATERIAL, NAME=SOLID_PLACEHOLDER, TYPE=SOLID +*SOLID SECTION, NSET=COUPLED, MATERIAL=SOLID_PLACEHOLDER +*SPHLITE TIME CONTROL, DT=1.0E-6, TIME=1.0E-6, STEPS=1 +*SPHLITE COUPLING REGION, NAME=CONTINUUM_SIDE, SET=COUPLED, TYPE=CONTINUUM diff --git a/tests/data/minimal_particles_011.inp b/tests/data/minimal_particles_011.inp new file mode 100644 index 0000000..9278795 --- /dev/null +++ b/tests/data/minimal_particles_011.inp @@ -0,0 +1,16 @@ +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 0.0, 1.0, 0.0 +4, 0.0, 0.0, 1.0 +*ELEMENT, TYPE=SPH4 +1, 1, 2, 3, 4 +*NSET, NSET=ALLNODES +1, 2, 3, 4 +*ELSET, ELSET=ALLELEMENTS +1 +*MATERIAL, NAME=WATER_PLACEHOLDER, TYPE=WATER +*SOLID SECTION, ELSET=ALLELEMENTS, MATERIAL=WATER_PLACEHOLDER, THICKNESS=0.0, POINTS=3 +*SPHLITE TIME CONTROL, DT=1.0E-6, TIME=1.0E-6, STEPS=1, SAFETY=0.8 +*SPHLITE OUTPUT, DIRECTORY=., INTERVAL=1 +*SPHLITE COUPLING REGION, NAME=CONTACT_SIDE, SET=ALLNODES, TYPE=CONTACT diff --git a/tests/data/missing_material_011.inp b/tests/data/missing_material_011.inp new file mode 100644 index 0000000..1c6d08a --- /dev/null +++ b/tests/data/missing_material_011.inp @@ -0,0 +1,4 @@ +*NODE +1, 0.0, 0.0, 0.0 +*SOLID SECTION, ELSET=ALL, MATERIAL=MISSING +*SPHLITE TIME CONTROL, DT=1.0E-6, TIME=1.0E-6, STEPS=1 diff --git a/tests/data/missing_section_011.inp b/tests/data/missing_section_011.inp new file mode 100644 index 0000000..2137e5a --- /dev/null +++ b/tests/data/missing_section_011.inp @@ -0,0 +1,4 @@ +*NODE +1, 0.0, 0.0, 0.0 +*MATERIAL, NAME=SOLID_PLACEHOLDER, TYPE=SOLID +*SPHLITE TIME CONTROL, DT=1.0E-6, TIME=1.0E-6, STEPS=1 diff --git a/tests/data/missing_time_control_011.inp b/tests/data/missing_time_control_011.inp new file mode 100644 index 0000000..a274081 --- /dev/null +++ b/tests/data/missing_time_control_011.inp @@ -0,0 +1,4 @@ +*NODE +1, 0.0, 0.0, 0.0 +*MATERIAL, NAME=SOLID_PLACEHOLDER, TYPE=SOLID +*SOLID SECTION, ELSET=ALL, MATERIAL=SOLID_PLACEHOLDER diff --git a/tests/data/mixed_case_011.inp b/tests/data/mixed_case_011.inp new file mode 100644 index 0000000..6887fa6 --- /dev/null +++ b/tests/data/mixed_case_011.inp @@ -0,0 +1,15 @@ +** leading comment + +*Node +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +*Element, Type=SPH2 +1, 1, 2 +*Nset, Nset=MixedNodes +1, 2 +*Elset, Elset=MixedElements +1 +*Material, Name=MixedMaterial, Type=Solid +*Solid Section, Elset=MixedElements, Material=MixedMaterial +*SphLite Time Control, DT=1.0E-6, TIME=1.0E-6, STEPS=1 +*SphLite Output, Directory=., Interval=1 diff --git a/tests/data/output_control_011.inp b/tests/data/output_control_011.inp new file mode 100644 index 0000000..145849d --- /dev/null +++ b/tests/data/output_control_011.inp @@ -0,0 +1,7 @@ +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +*MATERIAL, NAME=SOLID_PLACEHOLDER, TYPE=SOLID +*SOLID SECTION, ELSET=ALL, MATERIAL=SOLID_PLACEHOLDER +*SPHLITE TIME CONTROL, DT=1.0E-6, TIME=1.0E-6, STEPS=1 +*SPHLITE OUTPUT, DIRECTORY=., INTERVAL=1 diff --git a/tests/interface/test_c_api.c b/tests/interface/test_c_api.c index 62afba2..9111150 100644 --- a/tests/interface/test_c_api.c +++ b/tests/interface/test_c_api.c @@ -17,24 +17,44 @@ int main(void) int minor = 0; int patch = 0; int count = 0; + int warnings = 0; int step = 0; + int ids[4]; + int phase[4]; + int status[4]; + int material_id[4]; + int section_id[4]; + double x[12]; + double v[12]; + double a[12]; + double rho[4]; + double pressure[4]; + double internal_energy[4]; + double temperature[4]; + double h[4]; double time = 0.0; + if (SPHLITE_PHASE_SOLID != 4 || SPHLITE_STATUS_ACTIVE != 0) { + return fail("C constants are not locked"); + } if (sphlite_get_particle_count(NULL) != -1) { return fail("null particle count pointer did not return -1"); } if (sphlite_get_last_error(NULL, path, (int)sizeof(path)) != -1) { return fail("null last-error code pointer did not return -1"); } + if (sphlite_set_output_interval(0) != -1) { + return fail("non-positive output interval did not return -1"); + } ierr = sphlite_initialize(); if (ierr != 0) return fail("initialize failed"); ierr = sphlite_get_version(&major, &minor, &patch); if (ierr != 0) return fail("get_version failed"); - if (major != 0 || minor != 1 || patch != 0) return fail("unexpected version"); + if (major != 0 || minor != 1 || patch != 1) return fail("unexpected version"); - snprintf(path, sizeof(path), "%s/minimal_particles.inp", SPHLITE_TEST_DATA_DIR); + snprintf(path, sizeof(path), "%s/minimal_particles_011.inp", SPHLITE_TEST_DATA_DIR); ierr = sphlite_read_inp(path); if (ierr != 0) return fail("read_inp failed"); @@ -43,12 +63,43 @@ int main(void) ierr = sphlite_get_particle_count(&count); if (ierr != 0 || count != 4) return fail("particle count mismatch"); + ierr = sphlite_get_input_warning_count(&warnings); + if (ierr != 0 || warnings != 0) return fail("warning count mismatch"); + + if (sphlite_get_particle_state(1, ids, phase, status, material_id, section_id, + x, v, a, rho, pressure, internal_energy, temperature, h, &count) == 0) { + return fail("small particle-state buffer did not fail"); + } + + ierr = sphlite_get_particle_state(4, ids, phase, status, material_id, section_id, + x, v, a, rho, pressure, internal_energy, temperature, h, &count); + if (ierr != 0 || count != 4) return fail("particle state query failed"); + if (ids[0] != 1 || phase[0] != SPHLITE_PHASE_SOLID || + status[0] != SPHLITE_STATUS_ACTIVE) { + return fail("particle state content mismatch"); + } + + if (sphlite_get_particle_state(4, NULL, phase, status, material_id, section_id, + x, v, a, rho, pressure, internal_energy, temperature, h, &count) != -1) { + return fail("null particle-state array did not return -1"); + } ierr = sphlite_advance_explicit(1.0e-6); if (ierr != 0) return fail("advance explicit failed"); ierr = sphlite_get_current_time(&time, &step); if (ierr != 0 || step != 1 || time <= 0.0) return fail("time query failed"); + ierr = sphlite_set_output_directory(SPHLITE_TEST_WORK_DIR); + if (ierr != 0) return fail("set output directory failed"); + ierr = sphlite_write_output(step, time); + if (ierr != 0) return fail("write output failed"); + + snprintf(path, sizeof(path), "%s/restart_c_api.tmp", SPHLITE_TEST_WORK_DIR); + ierr = sphlite_write_restart(path); + if (ierr != 0) return fail("write restart failed"); + ierr = sphlite_read_restart(path); + if (ierr != 0) return fail("read restart failed"); + ierr = sphlite_finalize(); if (ierr != 0) return fail("finalize failed"); return 0; diff --git a/tests/interface/test_cpp_api.cpp b/tests/interface/test_cpp_api.cpp new file mode 100644 index 0000000..6cdcc7c --- /dev/null +++ b/tests/interface/test_cpp_api.cpp @@ -0,0 +1,32 @@ +#include "sphlite.hpp" + +#include + +int main() +{ + if (sphlite::PhaseSolid != SPHLITE_PHASE_SOLID || + sphlite::StatusActive != SPHLITE_STATUS_ACTIVE) { + return 1; + } + + sphlite::Session session; + sphlite::Version version = sphlite::version(); + if (version.major != 0 || version.minor != 1 || version.patch != 1) { + return 1; + } + + std::string input = std::string(SPHLITE_TEST_DATA_DIR) + "/minimal_particles_011.inp"; + sphlite::read_inp(input); + sphlite::build_model(); + if (sphlite::particle_count() != 4) { + return 1; + } + + sphlite::ParticleState state = sphlite::particle_state(); + if (state.particle_id_ext.size() != 4 || state.phase[0] != sphlite::PhaseSolid) { + return 1; + } + + session.close(); + return 0; +} diff --git a/tests/module/test_coupling_interface.f90 b/tests/module/test_coupling_interface.f90 new file mode 100644 index 0000000..e0bcd54 --- /dev/null +++ b/tests/module/test_coupling_interface.f90 @@ -0,0 +1,71 @@ +program test_coupling_interface + use sph_precision, only: rk + use sph_error, only: SPH_SUCCESS + use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model + use sph_coupling_interface, only: sph_coupling_get_particle_count, & + sph_coupling_export_particle, sph_coupling_get_facet_count, & + sph_coupling_export_facet, sph_coupling_import_particle_force, & + sph_coupling_import_facet_load, sph_coupling_get_diagnostics, & + coupling_type + implicit none + + integer :: ierr + integer :: count + integer :: ext_id + integer :: nodes(3) + real(rk) :: position(3) + real(rk) :: velocity(3) + real(rk) :: force(3) + real(rk) :: work + real(rk) :: energy + real(rk) :: momentum(3) + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed') + call sph_read_inp('tests/data/coupling_region_011.inp', ierr) + call assert_ok(ierr, 'read input failed') + call sph_build_model(ierr) + call assert_ok(ierr, 'build model failed') + + call sph_coupling_get_particle_count(count, ierr) + call assert_ok(ierr, 'coupling particle count failed') + if (count /= 2) call fail('coupling particle count mismatch') + call sph_coupling_export_particle(1, ext_id, position, velocity, ierr) + call assert_ok(ierr, 'coupling particle export failed') + if (ext_id /= 1) call fail('coupling particle export id mismatch') + + call sph_coupling_get_facet_count(count, ierr) + call assert_ok(ierr, 'coupling facet count failed') + if (count /= 0) call fail('0.1.1 should not export real facets') + call sph_coupling_export_facet(2, nodes, ierr) + if (ierr == SPH_SUCCESS) call fail('facet export error path did not fail') + + force = 1.0_rk + call sph_coupling_import_particle_force(1, force, ierr) + call assert_ok(ierr, 'coupling particle force import failed') + call sph_coupling_import_facet_load(1, force, ierr) + call assert_ok(ierr, 'coupling facet load import failed') + call sph_coupling_get_diagnostics(work, energy, momentum) + if (work <= 0.0_rk .or. energy <= 0.0_rk) call fail('coupling diagnostics not updated') + if (coupling_type /= 0) call fail('default coupling type should remain neutral') + + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed') + +contains + + subroutine assert_ok(ierr_value, message) + integer, intent(in) :: ierr_value + character(len=*), intent(in) :: message + + if (ierr_value /= SPH_SUCCESS) call fail(message) + end subroutine assert_ok + + subroutine fail(message) + character(len=*), intent(in) :: message + + write(*, '(A)') trim(message) + stop 1 + end subroutine fail + +end program test_coupling_interface diff --git a/tests/module/test_input.f90 b/tests/module/test_input.f90 index 2252635..0bfa048 100644 --- a/tests/module/test_input.f90 +++ b/tests/module/test_input.f90 @@ -1,25 +1,30 @@ program test_input use sph_error, only: SPH_SUCCESS use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, & - sph_build_model, sph_get_particle_count + sph_build_model, sph_get_particle_count, sph_get_input_warning_count use sph_input_reader, only: sph_input_get_warning_count + use sph_database, only: material_count, section_count, nset_count, & + elset_count, element_count, coupling_region_count implicit none integer :: ierr integer :: count integer :: warnings - call sph_initialize(ierr) - call assert_ok(ierr, 'initialize failed for minimal input') - call sph_read_inp('tests/data/minimal_particles.inp', ierr) - call assert_ok(ierr, 'minimal input read failed') - call sph_build_model(ierr) - call assert_ok(ierr, 'minimal model build failed') - call sph_get_particle_count(count, ierr) - call assert_ok(ierr, 'minimal particle count failed') - if (count /= 4) call fail('minimal input particle count mismatch') + call check_good_input('tests/data/minimal_particles_011.inp', 4, .true.) + if (material_count /= 1) call fail('material metadata was not saved') + if (section_count /= 1) call fail('section metadata was not saved') + if (nset_count <= 0) call fail('node set metadata was not saved') + if (elset_count <= 0) call fail('element set metadata was not saved') + if (element_count <= 0) call fail('element metadata was not saved') + if (coupling_region_count <= 0) call fail('coupling region metadata was not saved') + call sph_finalize(ierr) - call assert_ok(ierr, 'finalize failed after minimal input') + call assert_ok(ierr, 'finalize failed after metadata check') + + call check_good_input('tests/data/mixed_case_011.inp', 2, .false.) + call check_good_input('tests/data/output_control_011.inp', 2, .false.) + call check_good_input('tests/data/coupling_region_011.inp', 2, .false.) call sph_initialize(ierr) call assert_ok(ierr, 'initialize failed for unknown keyword input') @@ -27,22 +32,53 @@ program test_input call assert_ok(ierr, 'unknown keyword input read failed') call sph_input_get_warning_count(warnings) if (warnings <= 0) call fail('unknown keyword warning was not recorded') + call sph_get_input_warning_count(warnings, ierr) + call assert_ok(ierr, 'warning count API failed') + if (warnings <= 0) call fail('warning count API did not report warning') call sph_build_model(ierr) call assert_ok(ierr, 'unknown keyword input build failed') call sph_finalize(ierr) call assert_ok(ierr, 'finalize failed after unknown keyword input') - call sph_initialize(ierr) - call assert_ok(ierr, 'initialize failed for missing required input') - call sph_read_inp('tests/data/missing_required.inp', ierr) - call assert_ok(ierr, 'missing required input read failed') - call sph_build_model(ierr) - if (ierr == SPH_SUCCESS) call fail('missing required input did not fail') - call sph_finalize(ierr) - call assert_ok(ierr, 'finalize failed after missing required input') + call check_bad_input('tests/data/missing_material_011.inp') + call check_bad_input('tests/data/missing_section_011.inp') + call check_bad_input('tests/data/missing_time_control_011.inp') contains + subroutine check_good_input(filename, expected_count, leave_open) + character(len=*), intent(in) :: filename + integer, intent(in) :: expected_count + logical, intent(in) :: leave_open + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for good input') + call sph_read_inp(filename, ierr) + call assert_ok(ierr, 'good input read failed') + call sph_build_model(ierr) + call assert_ok(ierr, 'good model build failed') + call sph_get_particle_count(count, ierr) + call assert_ok(ierr, 'good particle count failed') + if (count /= expected_count) call fail('good input particle count mismatch') + if (.not. leave_open) then + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed after good input') + end if + end subroutine check_good_input + + subroutine check_bad_input(filename) + character(len=*), intent(in) :: filename + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for bad input') + call sph_read_inp(filename, ierr) + call assert_ok(ierr, 'bad input read should still parse nodes') + call sph_build_model(ierr) + if (ierr == SPH_SUCCESS) call fail('bad input did not fail model build') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed after bad input') + end subroutine check_bad_input + subroutine assert_ok(ierr_value, message) integer, intent(in) :: ierr_value character(len=*), intent(in) :: message diff --git a/tests/module/test_output.f90 b/tests/module/test_output.f90 new file mode 100644 index 0000000..953853e --- /dev/null +++ b/tests/module/test_output.f90 @@ -0,0 +1,41 @@ +program test_output + use sph_precision, only: rk + use sph_error, only: SPH_SUCCESS + use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model, & + sph_set_output_directory, sph_set_output_interval, sph_write_output + implicit none + + integer :: ierr + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed') + call sph_read_inp('tests/data/output_control_011.inp', ierr) + call assert_ok(ierr, 'read input failed') + call sph_build_model(ierr) + call assert_ok(ierr, 'build model failed') + call sph_set_output_directory('build-codex-unix', ierr) + call assert_ok(ierr, 'set output directory failed') + call sph_set_output_interval(1, ierr) + call assert_ok(ierr, 'set output interval failed') + call sph_write_output(0, 0.0_rk, ierr) + call assert_ok(ierr, 'write output failed') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed') + +contains + + subroutine assert_ok(ierr_value, message) + integer, intent(in) :: ierr_value + character(len=*), intent(in) :: message + + if (ierr_value /= SPH_SUCCESS) call fail(message) + end subroutine assert_ok + + subroutine fail(message) + character(len=*), intent(in) :: message + + write(*, '(A)') trim(message) + stop 1 + end subroutine fail + +end program test_output diff --git a/tests/module/test_restart.f90 b/tests/module/test_restart.f90 new file mode 100644 index 0000000..3429ffa --- /dev/null +++ b/tests/module/test_restart.f90 @@ -0,0 +1,49 @@ +program test_restart + use sph_precision, only: rk + use sph_error, only: SPH_SUCCESS + use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model, & + sph_run_explicit, sph_get_current_time, sph_write_restart, sph_read_restart, & + sph_advance_explicit + implicit none + + integer :: ierr + integer :: step + real(rk) :: time + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed') + call sph_read_inp('tests/data/minimal_particles_011.inp', ierr) + call assert_ok(ierr, 'read input failed') + call sph_build_model(ierr) + call assert_ok(ierr, 'build model failed') + call sph_run_explicit(ierr) + call assert_ok(ierr, 'run explicit failed') + call sph_write_restart('build-codex-unix/restart_test.tmp', ierr) + call assert_ok(ierr, 'write restart failed') + call sph_read_restart('build-codex-unix/restart_test.tmp', ierr) + call assert_ok(ierr, 'read restart failed') + call sph_get_current_time(time, step, ierr) + call assert_ok(ierr, 'time after restart failed') + if (step /= 1) call fail('restart did not restore step') + call sph_advance_explicit(1.0e-6_rk, ierr) + call assert_ok(ierr, 'advance after restart failed') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed') + +contains + + subroutine assert_ok(ierr_value, message) + integer, intent(in) :: ierr_value + character(len=*), intent(in) :: message + + if (ierr_value /= SPH_SUCCESS) call fail(message) + end subroutine assert_ok + + subroutine fail(message) + character(len=*), intent(in) :: message + + write(*, '(A)') trim(message) + stop 1 + end subroutine fail + +end program test_restart diff --git a/tests/unit/test_smoke.f90 b/tests/unit/test_smoke.f90 index 6635d67..247b0cf 100644 --- a/tests/unit/test_smoke.f90 +++ b/tests/unit/test_smoke.f90 @@ -2,8 +2,9 @@ program test_smoke use sph_precision, only: rk use sph_error, only: SPH_SUCCESS use sph_api, only: sph_initialize, sph_finalize, sph_get_version, & - sph_get_particle_count, sph_get_current_time, sph_advance_explicit - use sph_database, only: sph_db_allocate + sph_get_particle_count, sph_get_current_time, sph_advance_explicit, & + sph_get_particle_state_capacity, sph_get_particle_state + use sph_database, only: sph_db_allocate, SPH_PHASE_SOLID, SPH_STATUS_ACTIVE implicit none integer :: ierr @@ -11,15 +12,31 @@ program test_smoke integer :: minor integer :: patch integer :: count + integer :: ndim integer :: step + integer :: particle_id_ext(2) + integer :: phase(2) + integer :: status(2) + integer :: material_id(2) + integer :: section_id(2) + real(rk) :: x(3,2) + real(rk) :: v(3,2) + real(rk) :: a(3,2) + real(rk) :: rho(2) + real(rk) :: pressure(2) + real(rk) :: internal_energy(2) + real(rk) :: temperature(2) + real(rk) :: h(2) real(rk) :: time call sph_initialize(ierr) call assert_ok(ierr, 'initialize failed') + call sph_initialize(ierr) + call assert_ok(ierr, 'reinitialize failed') call sph_get_version(major, minor, patch, ierr) call assert_ok(ierr, 'version failed') - if (major /= 0 .or. minor /= 1 .or. patch /= 0) call fail('unexpected version') + if (major /= 0 .or. minor /= 1 .or. patch /= 1) call fail('unexpected version') call sph_db_allocate(2, ierr) call assert_ok(ierr, 'database allocation failed') @@ -27,6 +44,22 @@ program test_smoke call assert_ok(ierr, 'particle count failed') if (count /= 2) call fail('unexpected particle count') + call sph_get_particle_state_capacity(count, ndim, ierr) + call assert_ok(ierr, 'state capacity failed') + if (count /= 2 .or. ndim /= 3) call fail('unexpected state capacity') + + call sph_get_particle_state(2, particle_id_ext, phase, status, material_id, & + section_id, x, v, a, rho, pressure, internal_energy, temperature, h, & + count, ierr) + call assert_ok(ierr, 'particle state failed') + if (phase(1) /= SPH_PHASE_SOLID) call fail('unexpected default phase') + if (status(1) /= SPH_STATUS_ACTIVE) call fail('unexpected default status') + + call sph_advance_explicit(-1.0e-6_rk, ierr) + if (ierr == SPH_SUCCESS) call fail('negative time step did not fail') + call sph_advance_explicit(0.0_rk, ierr) + if (ierr == SPH_SUCCESS) call fail('zero time step did not fail') + call sph_advance_explicit(1.0e-6_rk, ierr) call assert_ok(ierr, 'explicit advance failed') call sph_get_current_time(time, step, ierr) @@ -36,6 +69,8 @@ program test_smoke call sph_finalize(ierr) call assert_ok(ierr, 'finalize failed') + call sph_finalize(ierr) + call assert_ok(ierr, 'repeat finalize failed') contains diff --git a/todo.md b/todo.md index 7ca81b9..993a164 100644 --- a/todo.md +++ b/todo.md @@ -1,224 +1,468 @@ # SPHLite TODO -当前目标版本:0.1.0 +当前目标版本:0.2.0 -本文档依据 `docs/v1.0_development_plan.md` 拆分。0.1.0 只搭建软件框架,不实现生产级 Water、Gas、Explosive、Solid、Shell 物理算法。 +本文档依据 `docs/v2.0_development_plan.md` 拆分。0.2.0 是 `Water` 基础 SPH 最小闭环版本,目标是打通 3D 水相弱可压 SPH 的真实计算链路,不交付 Gas、Explosive、Solid、Shell 生产路径,不交付真实 FEM-SPH 联合运行。 -## 阶段 A:工程骨架 +## 阶段 A:版本和构建骨架 -- [x] 创建 0.1.0 规划目录结构。 -- [x] 创建根目录 `CMakeLists.txt`。 -- [x] 配置 CMake 项目版本为 `0.1.0`。 -- [x] 配置项目语言为 C 和 Fortran。 -- [x] 配置 Fortran 95 编译标准。 -- [x] 配置 Fortran module 输出目录。 -- [x] 配置 `FortranCInterface` 名字修饰头生成。 -- [x] 创建 `sphlite_core` 静态库目标。 -- [x] 创建 `sphlite_c` 静态库目标。 -- [x] 创建 `sphlite` 命令行程序目标。 -- [x] 启用 CTest。 -- [x] 添加最小 `test_smoke` 测试目标占位。 +- [ ] 将 `sph_precision` 中的软件版本更新为 `0.2.0`。 +- [ ] 将 CMake 项目版本更新为 `0.2.0`。 +- [ ] 确认 `sphlite_core` 仍以 Fortran 95 核心源文件为主。 +- [ ] 确认 `sphlite_c` 仍为 C ABI 薄包装库。 +- [ ] 确认 `sphlite` CLI 仍通过公共接口调用求解器。 +- [ ] 在 CMake 中加入 `kernel` CTest 目标。 +- [ ] 在 CMake 中加入 `neighbor` CTest 目标。 +- [ ] 在 CMake 中加入 `water_eos` CTest 目标。 +- [ ] 在 CMake 中加入 `water_density` CTest 目标。 +- [ ] 在 CMake 中加入 `water_viscosity` CTest 目标。 +- [ ] 在 CMake 中加入 `water_force` CTest 目标。 +- [ ] 在 CMake 中加入 `solver_water` CTest 目标。 +- [ ] 在 CMake 中加入 `pressure_wave_smoke` CTest 目标。 +- [ ] 确认 0.1.1 既有测试目标仍保留。 +- [ ] 确认不新增 `geometry/` 目录。 +- [ ] 确认不新增 `mesh/` 目录。 -## 阶段 B:Core 基础模块 +## 阶段 B:0.2.0 输入数据文件 -- [x] 实现 `src/core/sph_precision.f90`。 -- [x] 在 `sph_precision` 中定义 `rk` 精度。 -- [x] 在 `sph_precision` 中定义版本号 `0.1.0`。 -- [x] 在 `sph_precision` 中定义 3D 维度常量。 -- [x] 实现 `src/core/sph_error.f90`。 -- [x] 定义 SPHLite 基础错误码。 -- [x] 实现最近错误码保存。 -- [x] 实现最近错误消息保存。 -- [x] 实现错误清理接口。 -- [x] 实现 `src/core/sph_log.f90`。 -- [x] 实现日志打开接口。 -- [x] 实现日志关闭接口。 -- [x] 实现基础 info 日志接口。 -- [x] 实现 `src/core/sph_control.f90`。 -- [x] 定义默认时间控制参数。 -- [x] 定义默认输出控制参数。 -- [x] 实现控制参数默认初始化。 +- [ ] 创建最小 Water `.inp` 测试数据。 +- [ ] 创建 Tait EOS 参数完整的 `.inp` 测试数据。 +- [ ] 创建 Monaghan 人工黏性参数完整的 `.inp` 测试数据。 +- [ ] 创建粒子控制参数完整的 `.inp` 测试数据。 +- [ ] 创建 body force 参数测试 `.inp`。 +- [ ] 创建缺失 Tait EOS 参数错误测试 `.inp`。 +- [ ] 创建非法 Tait EOS 参数错误测试 `.inp`。 +- [ ] 创建缺失人工黏性参数错误测试 `.inp`。 +- [ ] 创建非法人工黏性参数错误测试 `.inp`。 +- [ ] 创建非法粒子控制参数错误测试 `.inp`。 +- [ ] 创建非 water phase 被 Water 计算跳过的测试 `.inp`。 +- [ ] 创建小型 3D pressure wave `.inp`。 +- [ ] 确认测试数据不依赖求解器内置几何生成。 -## 阶段 C:Database 最小状态 +## 阶段 C:Database 运行时状态 -- [x] 实现 `src/database/sph_database.f90`。 -- [x] 定义 `npart` 粒子数量。 -- [x] 定义粒子 external id 数组。 -- [x] 定义 phase/status/material/section 数组。 -- [x] 定义 `x0(3,npart)` 和 `x(3,npart)`。 -- [x] 定义 `v(3,npart)`、`a(3,npart)` 和 `vhalf(3,npart)`。 -- [x] 定义 `mass/rho/volume/pressure/internal_energy/temperature/h` 数组。 -- [x] 实现数据库分配接口。 -- [x] 实现数据库默认初始化接口。 -- [x] 实现数据库释放接口。 -- [x] 实现粒子数量查询接口。 -- [x] 定义 phase 常量。 -- [x] 定义 status 常量占位。 +- [ ] 确认 `particle_id_ext` 在 0.2.0 路径中完整初始化。 +- [ ] 确认 `particle_phase` 在 0.2.0 路径中完整初始化。 +- [ ] 确认 `particle_status` 在 0.2.0 路径中完整初始化。 +- [ ] 确认 `material_id` 在 0.2.0 路径中完整初始化。 +- [ ] 确认 `section_id` 在 0.2.0 路径中完整初始化。 +- [ ] 确认 `x0(3,npart)` 在 0.2.0 路径中完整初始化。 +- [ ] 确认 `x(3,npart)` 在 0.2.0 路径中完整初始化。 +- [ ] 确认 `v(3,npart)` 在 0.2.0 路径中完整初始化。 +- [ ] 确认 `a(3,npart)` 在 0.2.0 路径中完整初始化。 +- [ ] 确认 `vhalf(3,npart)` 在 0.2.0 路径中完整初始化。 +- [ ] 确认 `mass(npart)` 在 0.2.0 路径中完整初始化。 +- [ ] 确认 `rho(npart)` 在 0.2.0 路径中完整初始化。 +- [ ] 确认 `volume(npart)` 在 0.2.0 路径中完整初始化。 +- [ ] 确认 `pressure(npart)` 在 0.2.0 路径中完整初始化。 +- [ ] 确认 `internal_energy(npart)` 在 0.2.0 路径中完整初始化。 +- [ ] 确认 `temperature(npart)` 在 0.2.0 路径中完整初始化。 +- [ ] 确认 `h(npart)` 在 0.2.0 路径中完整初始化。 +- [ ] 增加 `drho_dt(npart)` 运行时状态。 +- [ ] 增加 `acc_pressure(3,npart)` 运行时状态。 +- [ ] 增加 `acc_viscosity(3,npart)` 运行时状态。 +- [ ] 增加外部体力或等效外载缓存。 +- [ ] 增加每粒子 cell id 存储。 +- [ ] 增加 cell 链表或压缩索引存储。 +- [ ] 增加邻域表或邻域对存储。 +- [ ] 增加每粒子邻居数量存储。 +- [ ] 增加水相诊断状态存储。 +- [ ] 确认 `sph_finalize` 释放 0.2.0 新增状态。 +- [ ] 确认重复 `sph_initialize` 后新增状态一致。 +- [ ] 确认重复读取模型前新增状态被释放。 -## 阶段 D:API 状态和 Fortran API +## 阶段 D:Water 参数和输入解析 -- [x] 实现 `src/interface/sph_api_state.f90`。 -- [x] 保存 API 初始化状态。 -- [x] 实现 `src/interface/sph_api.f90`。 -- [x] 实现 `sph_initialize`。 -- [x] 实现 `sph_finalize`。 -- [x] 实现 `sph_get_version`。 -- [x] 实现 `sph_get_last_error`。 -- [x] 实现 `sph_get_particle_count`。 -- [x] 实现 `sph_get_current_time`。 -- [x] 为未实现 API 返回明确错误或占位成功码。 +- [ ] 在 input 中识别 water material 类型。 +- [ ] 复用 `*DENSITY` 读取参考密度。 +- [ ] 解析 `*SPHLITE EOS` 关键词。 +- [ ] 解析 `*SPHLITE EOS` 的 `TYPE` 字段。 +- [ ] 解析 `*SPHLITE EOS` 的 `MATERIAL` 字段。 +- [ ] 解析 `*SPHLITE EOS` 的 `RHO0` 字段。 +- [ ] 解析 `*SPHLITE EOS` 的 `C0` 字段。 +- [ ] 解析 `*SPHLITE EOS` 的 `GAMMA` 字段。 +- [ ] 解析 `*SPHLITE EOS` 的 `P0` 字段。 +- [ ] 解析 `*SPHLITE EOS` 的可选 `B` 字段。 +- [ ] 未给定 `B` 时由 `rho0*c0*c0/gamma` 推导。 +- [ ] 解析 `*SPHLITE VISCOSITY` 关键词。 +- [ ] 解析 `*SPHLITE VISCOSITY` 的 `TYPE` 字段。 +- [ ] 解析 `*SPHLITE VISCOSITY` 的 `MATERIAL` 字段。 +- [ ] 解析 `*SPHLITE VISCOSITY` 的 `ALPHA` 字段。 +- [ ] 解析 `*SPHLITE VISCOSITY` 的 `BETA` 字段。 +- [ ] 解析 `*SPHLITE VISCOSITY` 的 `ETA2` 字段。 +- [ ] 解析 `*SPHLITE PARTICLE CONTROL` 关键词。 +- [ ] 解析统一光滑长度 `H` 或等价字段。 +- [ ] 解析统一粒子质量 `MASS` 或等价字段。 +- [ ] 解析统一粒子体积 `VOLUME` 或等价字段。 +- [ ] 解析 `*SPHLITE BODY FORCE` 关键词。 +- [ ] 解析体力三维分量。 +- [ ] 对缺失 Tait EOS 关键字段报 error。 +- [ ] 对非法 Tait EOS 数值报 error。 +- [ ] 对缺失人工黏性关键字段报 error。 +- [ ] 对非法人工黏性数值报 error。 +- [ ] 对非法粒子控制数值报 error。 +- [ ] 对不影响运行的未知 Water 关键词报 warning。 +- [ ] 确认输入错误消息包含文件名。 +- [ ] 确认输入错误消息包含行号。 -## 阶段 E:Input 解析框架 +## 阶段 E:Kernel 模块 -- [x] 实现 `src/input/sph_inp_defs.f90`。 -- [x] 定义输入关键词识别常量。 -- [x] 实现 `src/input/sph_inp_helpers.f90`。 -- [x] 实现行清理和大小写规范化工具。 -- [x] 实现逗号分隔字段解析工具。 -- [x] 实现 `src/input/sph_input_reader.f90`。 -- [x] 实现 `.inp` 文件打开和逐行读取。 -- [x] 实现 `*NODE` 最小解析。 -- [x] 实现 `*ELEMENT` 占位解析。 -- [x] 实现 `*NSET` 占位解析。 -- [x] 实现 `*ELSET` 占位解析。 -- [x] 实现 `*MATERIAL` 占位解析。 -- [x] 实现 `*SECTION` 占位解析。 -- [x] 实现 `*SPHLITE ...` 扩展关键词入口。 -- [x] 对不影响运行的未知关键词发出 warning。 -- [x] 对缺失关键数据返回 error。 -- [x] 在 Fortran API 中接入 `sph_read_inp`。 -- [x] 在 Fortran API 中接入 `sph_build_model`。 +- [ ] 创建 `Particle` 核函数模块文件。 +- [ ] 定义核函数类型常量。 +- [ ] 定义 Wendland C2 类型常量。 +- [ ] 定义 Wendland C4 类型常量。 +- [ ] 定义 cubic spline 类型常量。 +- [ ] 实现核函数支持半径查询。 +- [ ] 实现 Wendland C2 核函数值。 +- [ ] 实现 Wendland C2 核函数梯度。 +- [ ] 实现 Wendland C4 核函数值。 +- [ ] 实现 Wendland C4 核函数梯度。 +- [ ] 实现 cubic spline 核函数值。 +- [ ] 实现 cubic spline 核函数梯度。 +- [ ] 支持 3D 归一化系数。 +- [ ] 支持每粒子独立 `h(i)` 传入。 +- [ ] 对 `h <= 0` 返回明确错误。 +- [ ] 对未知核函数类型返回明确错误。 +- [ ] 将 Wendland C2 设置为默认核函数。 +- [ ] 增加 `r = 0` 核函数有限性测试。 +- [ ] 增加超出支持半径测试。 +- [ ] 增加梯度方向测试。 +- [ ] 增加梯度反对称性测试。 +- [ ] 增加不同 `h(i)` 测试。 -## 阶段 F:Solver、Output 和 Restart 占位 +## 阶段 F:Neighbor 模块 -- [x] 实现 `src/solver/sph_solver_explicit.f90`。 -- [x] 实现稳定时间步估计占位。 -- [x] 实现 Velocity-Verlet 最小步进。 -- [x] 在步进中更新时间和步数。 -- [x] 在 Fortran API 中接入 `sph_estimate_stable_dt`。 -- [x] 在 Fortran API 中接入 `sph_advance_explicit`。 -- [x] 在 Fortran API 中接入 `sph_run_explicit`。 -- [x] 实现 `src/output/sph_output_csv.f90`。 -- [x] 实现输出目录设置占位。 -- [x] 实现输出间隔设置占位。 -- [x] 实现最小 CSV 输出占位。 -- [x] 实现 `src/output/sph_restart.f90`。 -- [x] 实现 restart 写入占位。 -- [x] 实现 restart 读取占位。 +- [ ] 创建 uniform cell/bin 邻域模块文件。 +- [ ] 实现当前坐标包围盒计算。 +- [ ] 实现最大支持半径计算。 +- [ ] 根据最大支持半径确定 cell 尺寸。 +- [ ] 实现 cell 网格维度计算。 +- [ ] 实现粒子到 cell 的映射。 +- [ ] 实现 cell 链表或压缩索引构建。 +- [ ] 实现相邻 cell 遍历。 +- [ ] 实现邻域表生成。 +- [ ] 支持每粒子独立 `h(i)` 的搜索半径。 +- [ ] 支持空模型邻域构建。 +- [ ] 支持单粒子模型邻域构建。 +- [ ] 支持负坐标粒子邻域构建。 +- [ ] 支持边界 cell 粒子邻域构建。 +- [ ] 支持粒子移动后重建邻域。 +- [ ] 对邻域表容量不足返回明确错误。 +- [ ] 增加朴素 `O(n^2)` 对比测试辅助例程。 +- [ ] 增加 uniform cell/bin 与朴素搜索一致性测试。 +- [ ] 增加不同 `h(i)` 邻域测试。 +- [ ] 增加空模型邻域测试。 +- [ ] 增加单粒子邻域测试。 +- [ ] 增加粒子移动后重建测试。 +- [ ] 增加负坐标和边界 cell 测试。 +- [ ] 增加邻域容量不足错误路径测试。 -## 阶段 G:物理模块占位 +## 阶段 G:Water EOS 和诊断 -- [x] 创建 `src/water/sph_water.f90` 占位模块。 -- [x] 创建 `src/gas/sph_gas.f90` 占位模块。 -- [x] 创建 `src/explosive/sph_explosive.f90` 占位模块。 -- [x] 创建 `src/solid/sph_solid.f90` 占位模块。 -- [x] 创建 `src/shell/sph_shell.f90` 占位模块。 -- [x] 创建 `src/particle/sph_particle.f90` 占位模块。 -- [x] 创建 `src/material/sph_material.f90` 占位模块。 -- [x] 每个占位模块提供 reset 或 initialize 入口。 -- [x] 确保占位模块不声明生产级物理能力。 +- [ ] 创建或完善 Water 参数存储模块。 +- [ ] 保存 Tait EOS 的 `rho0`。 +- [ ] 保存 Tait EOS 的 `c0`。 +- [ ] 保存 Tait EOS 的 `gamma`。 +- [ ] 保存 Tait EOS 的 `p0`。 +- [ ] 保存 Tait EOS 的 `B`。 +- [ ] 保存人工黏性 `alpha`。 +- [ ] 保存人工黏性 `beta`。 +- [ ] 保存人工黏性 `eta2`。 +- [ ] 保存 body force 三维分量。 +- [ ] 实现 Tait EOS 压力计算。 +- [ ] 实现 Tait EOS 声速或声速占位计算。 +- [ ] 实现 Tait EOS 参数校验。 +- [ ] 实现 water phase 粒子筛选。 +- [ ] 实现非 water phase 默认跳过。 +- [ ] 实现水相粒子数诊断。 +- [ ] 实现水相总质量诊断。 +- [ ] 实现水相总动量诊断。 +- [ ] 实现水相最大压力诊断。 +- [ ] 实现水相最小密度诊断。 +- [ ] 实现水相最大密度诊断。 +- [ ] 实现压力冲量占位诊断。 +- [ ] 增加 Tait EOS 标准压力测试。 +- [ ] 增加 Tait EOS 参考密度零压测试。 +- [ ] 增加 Tait EOS 非法参数测试。 +- [ ] 增加 water summary 诊断测试。 -## 阶段 H:CouplingInterface 占位 +## 阶段 H:Water 连续性方程 -- [x] 实现 `src/coupling_interface/sph_coupling_interface.f90`。 -- [x] 定义 coupling region 占位数据。 -- [x] 实现 coupling particle count 占位接口。 -- [x] 实现 coupling particle 导出占位接口。 -- [x] 实现 coupling facet count 占位接口。 -- [x] 实现 coupling facet 导出占位接口。 -- [x] 实现 coupling particle force 导入占位接口。 -- [x] 实现 coupling facet load 导入占位接口。 -- [x] 添加耦合功和能量诊断占位字段。 -- [x] 保持接口命名中立,不出现 FEMLite 依赖。 +- [ ] 创建或完善 Water 密度计算入口。 +- [ ] 初始化 `drho_dt` 数组。 +- [ ] 基于邻域表遍历 water-water 粒子对。 +- [ ] 计算粒子相对速度。 +- [ ] 计算核函数梯度。 +- [ ] 累加连续性方程密度变化率。 +- [ ] 保持 summation density 不进入主流程。 +- [ ] 对非 water 粒子跳过密度贡献。 +- [ ] 对失效或 inactive 粒子按 status 跳过。 +- [ ] 增加双粒子压缩密度变化测试。 +- [ ] 增加双粒子拉伸密度变化测试。 +- [ ] 增加静止均匀粒子密度变化近零测试。 +- [ ] 增加非 water 粒子跳过测试。 +- [ ] 增加 `drho_dt` 有限性测试。 -## 阶段 I:C ABI +## 阶段 I:Water 人工黏性和力 -- [x] 创建 `include/sphlite_c_api.h`。 -- [x] 在 C 头文件中定义 phase 常量。 -- [x] 声明生命周期 C API。 -- [x] 声明输入和显式求解 C API。 -- [x] 声明粒子数量查询 C API。 -- [x] 声明输出和 restart C API 占位。 -- [x] 实现 `src/interface/sph_c_api.c`。 -- [x] 接入 Fortran-C 名字修饰头。 -- [x] 实现 C 字符串到 Fortran 字符串桥接。 -- [x] 实现 C 空指针参数检查。 -- [x] 实现 C 非正容量参数检查。 -- [x] 将 C 参数错误返回 `-1`。 -- [x] 将 Fortran 错误码原样返回 C 调用方。 -- [x] 创建 `tests/interface/test_c_api.c`。 +- [ ] 创建或完善 Water 人工黏性计算入口。 +- [ ] 实现 Monaghan 人工黏性开关条件。 +- [ ] 实现人工黏性平均声速计算。 +- [ ] 实现人工黏性平均密度计算。 +- [ ] 实现人工黏性 `mu_ij` 计算。 +- [ ] 实现人工黏性加速度贡献。 +- [ ] 对远离粒子返回零人工黏性。 +- [ ] 创建或完善 Water 压力力计算入口。 +- [ ] 实现 SPH 压力梯度加速度。 +- [ ] 累加 `acc_pressure(3,npart)`。 +- [ ] 累加 `acc_viscosity(3,npart)`。 +- [ ] 将 body force 加入总加速度。 +- [ ] 将外部耦合载荷占位加入总加速度。 +- [ ] 对非 water 粒子跳过水相力贡献。 +- [ ] 增加人工黏性方向测试。 +- [ ] 增加人工黏性有限性测试。 +- [ ] 增加人工黏性对称性测试。 +- [ ] 增加压力梯度双粒子测试。 +- [ ] 增加均匀压力净力近零测试。 +- [ ] 增加 body force 加速度测试。 -## 阶段 J:CLI +## 阶段 J:Solver Water 集成 -- [x] 实现 `src/app/sph_program.f90`。 -- [x] 支持 `sphlite model.inp` inspect 模式。 -- [x] 支持 `sphlite model.inp --run` 运行模式。 -- [x] CLI 调用公共生命周期 API。 -- [x] CLI 打印版本号。 -- [x] CLI 打印粒子数量。 -- [x] CLI 打印当前时间和步数。 -- [x] CLI 在错误时打印最近错误消息。 -- [x] CLI 语法风格与 FEMLite 保持一致。 +- [ ] 在 build model 中构建 Water 运行时状态。 +- [ ] 在 build model 中构建邻域搜索状态。 +- [ ] 在显式步开始前校验 Water 参数。 +- [ ] 在显式步中必要时重建邻域。 +- [ ] 在显式步中根据 `rho` 更新 `pressure`。 +- [ ] 在显式步中计算 `drho_dt`。 +- [ ] 在显式步中计算水相压力加速度。 +- [ ] 在显式步中计算水相黏性加速度。 +- [ ] 在显式步中合成总加速度。 +- [ ] 在 Velocity-Verlet 中更新速度。 +- [ ] 在 Velocity-Verlet 中更新位置。 +- [ ] 在 Velocity-Verlet 中更新密度。 +- [ ] 在 Velocity-Verlet 中更新时间和步数。 +- [ ] 在显式步结束后更新 Water 诊断。 +- [ ] 在显式运行中按输出间隔写出结果。 +- [ ] 对负时间步保持明确错误。 +- [ ] 对零时间步保持明确错误。 +- [ ] 对未 build model 的显式步返回明确错误。 +- [ ] 增加单步 Water 推进测试。 +- [ ] 增加多步 Water 推进测试。 +- [ ] 增加 `sph_run_explicit` Water 闭环测试。 -## 阶段 K:Python 调试入口 +## 阶段 K:稳定时间步估计 -- [x] 创建 `Python/sphlite_debug.py`。 -- [x] 使用 `ctypes` 加载 C ABI。 -- [x] 包装生命周期接口。 -- [x] 包装 `read_inp`。 -- [x] 包装粒子数量查询。 -- [x] 包装显式运行入口。 -- [x] 创建 `tools/testing/test_python_debug.py`。 -- [x] Python smoke 读取最小 `.inp`。 -- [x] Python smoke 查询粒子数量。 +- [ ] 实现水相 CFL 声速约束估计。 +- [ ] 实现加速度约束估计。 +- [ ] 实现人工黏性约束估计。 +- [ ] 应用输入安全系数。 +- [ ] 保存 `dt_cfl` 诊断。 +- [ ] 保存 `dt_accel` 诊断。 +- [ ] 保存 `dt_viscous` 诊断。 +- [ ] 将三类约束最小值返回为 `dtcrit`。 +- [ ] 对无 water 粒子时返回明确策略。 +- [ ] 对非法 `h` 或声速返回明确错误。 +- [ ] 增加 CFL 约束测试。 +- [ ] 增加加速度约束测试。 +- [ ] 增加人工黏性约束测试。 +- [ ] 增加安全系数测试。 -## 阶段 L:测试数据和测试目标 +## 阶段 L:Output 和 Restart -- [x] 创建 `tests/data/minimal_particles.inp`。 -- [x] 在最小输入中包含 `*NODE`。 -- [x] 在最小输入中包含基础 material。 -- [x] 在最小输入中包含基础 section。 -- [x] 在最小输入中包含 `*SPHLITE ...` 时间控制占位。 -- [x] 创建 `tests/unit/test_smoke.f90`。 -- [x] `test_smoke` 覆盖 initialize/finalize。 -- [x] `test_smoke` 覆盖数据库最小分配。 -- [x] `test_smoke` 覆盖最小显式步。 -- [x] 创建 `tests/module/test_input.f90`。 -- [x] `test_input` 覆盖最小 `.inp` 解析。 -- [x] `test_input` 覆盖 unknown keyword warning。 -- [x] `test_input` 覆盖关键数据缺失 error。 -- [x] 添加 CTest `smoke`。 -- [x] 添加 CTest `input`。 -- [x] 添加 CTest `c_api`。 -- [x] 添加 CTest `python_debug`。 +- [ ] 确认 CSV 输出包含 id 字段。 +- [ ] 确认 CSV 输出包含 phase 字段。 +- [ ] 确认 CSV 输出包含 status 字段。 +- [ ] 确认 CSV 输出包含 material 字段。 +- [ ] 确认 CSV 输出包含 section 字段。 +- [ ] 确认 CSV 输出包含坐标字段。 +- [ ] 确认 CSV 输出包含速度字段。 +- [ ] 确认 CSV 输出包含 rho 字段。 +- [ ] 确认 CSV 输出包含 pressure 字段。 +- [ ] 确认 CSV 输出包含 temperature 字段。 +- [ ] 确认 CSV 输出包含 h 字段。 +- [ ] 增加 Water summary 输出或诊断写出入口。 +- [ ] 输出 step 诊断。 +- [ ] 输出 time 诊断。 +- [ ] 输出 water mass 诊断。 +- [ ] 输出 water momentum 诊断。 +- [ ] 输出 max pressure 诊断。 +- [ ] 输出 min/max density 诊断。 +- [ ] 输出 dtcrit 诊断。 +- [ ] 扩展 restart 头部状态版本到 0.2。 +- [ ] restart 写入 Water EOS 参数。 +- [ ] restart 写入人工黏性参数。 +- [ ] restart 写入 body force 参数。 +- [ ] restart 写入密度状态。 +- [ ] restart 写入压力状态。 +- [ ] restart 写入 `h` 状态。 +- [ ] restart 写入速度、加速度和半步速度。 +- [ ] restart 写入邻域搜索控制参数。 +- [ ] restart 写入 Water 诊断状态。 +- [ ] restart 读入后重建邻域。 +- [ ] restart 读入后继续执行 Water 显式步。 +- [ ] 增加 0.2.0 output 测试。 +- [ ] 增加 0.2.0 restart 测试。 -## 阶段 M:Source Size 和工具 +## 阶段 M:Fortran API -- [x] 创建 `tools/testing/check_source_size.py`。 -- [x] 创建 `tests/source_size_exceptions.txt`。 -- [x] 设置 Fortran 过程大小检查。 -- [x] 设置 Fortran 源文件大小检查。 -- [x] 设置 C 源文件大小检查。 -- [x] 设置 stale exception 检查。 -- [x] 添加 CTest `source_size`。 +- [ ] 实现 `sph_rebuild_neighbors`。 +- [ ] 实现 `sph_get_neighbor_stats`。 +- [ ] 实现 `sph_get_water_summary`。 +- [ ] 实现 `sph_get_water_dt_limits`。 +- [ ] 确认生命周期 API 兼容 0.1.1。 +- [ ] 确认 `.inp` 读取 API 兼容 0.1.1。 +- [ ] 确认 build model API 兼容 0.1.1。 +- [ ] 确认 stable dt API 兼容 0.1.1。 +- [ ] 确认 advance/run explicit API 兼容 0.1.1。 +- [ ] 确认 current time API 兼容 0.1.1。 +- [ ] 确认 particle count/state API 兼容 0.1.1。 +- [ ] 确认 output/restart API 兼容 0.1.1。 +- [ ] 确认 error query API 兼容 0.1.1。 +- [ ] 为新增 Fortran API 增加未初始化错误路径。 +- [ ] 为新增 Fortran API 增加空模型错误路径。 +- [ ] 为新增 Fortran API 增加正常路径测试。 -## 阶段 N:文档同步 +## 阶段 N:C ABI、C++ 和 Python -- [x] 更新 README 的当前版本为 0.1.0。 -- [x] 更新 `docs/09_release_history.md` 的 0.1.0 计划项。 -- [x] 更新 `docs/11_benchmark_matrix.md` 的 0.1.0 gate。 -- [x] 确认 01-06 文档不声明 0.1.0 已实现物理算法。 -- [x] 记录 0.1.0 不交付范围。 +- [ ] 在 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 不重新定义物理语义。 -## 阶段 O:0.1.0 验收 +## 阶段 O:CLI -- [x] 完整配置 CMake。 -- [x] 完整编译 `sphlite_core`。 -- [x] 完整编译 `sphlite_c`。 -- [x] 完整编译 `sphlite`。 -- [x] 运行 `ctest -R "^(smoke|input|c_api|python_debug)$"`。 -- [x] 运行 `ctest -R "^source_size$"`。 -- [x] 运行 `git diff --check`。 -- [x] 确认没有提交 build 目录。 -- [x] 确认没有提交 CSV/VTK/restart 临时结果。 -- [x] 记录 0.1.0 release gate 结果。 +- [ ] 保持 `sphlite --version` 可用。 +- [ ] 保持 `sphlite model.inp` inspect 模式可用。 +- [ ] 保持 `sphlite model.inp --check` 模式可用。 +- [ ] 保持 `sphlite model.inp --run` 模式可用。 +- [ ] 保持 `sphlite model.inp --run --output output` 可用。 +- [ ] CLI 输出 `version` 字段。 +- [ ] CLI 输出 `input` 字段。 +- [ ] CLI 输出 `mode` 字段。 +- [ ] CLI 输出 `particles` 字段。 +- [ ] CLI 输出 `warnings` 字段。 +- [ ] CLI 输出 `time` 字段。 +- [ ] CLI 输出 `step` 字段。 +- [ ] CLI 输出 `status` 字段。 +- [ ] CLI 输出 `water_particles` 字段。 +- [ ] CLI 输出 `water_mass` 字段。 +- [ ] CLI 输出 `max_pressure` 字段。 +- [ ] CLI 输出 `dtcrit` 字段。 +- [ ] CLI 错误时打印最近错误码。 +- [ ] CLI 错误时打印最近错误消息。 +- [ ] 确认 CLI 不绕过公共生命周期接口。 +- [ ] 增加 0.2.0 CLI inspect 测试。 +- [ ] 增加 0.2.0 CLI check 测试。 +- [ ] 增加 0.2.0 CLI run 测试。 +- [ ] 增加 0.2.0 CLI output 测试。 +- [ ] 增加 0.2.0 CLI 错误输入测试。 + +## 阶段 P:CouplingInterface 最小加固 + +- [ ] 确认 Water 运行后 coupling particle count 可用。 +- [ ] 确认 Water 运行后 coupling particle 导出可用。 +- [ ] 确认 coupling particle 导出包含 pressure。 +- [ ] 确认 coupling particle 导出包含 phase。 +- [ ] 确认 coupling particle 导出包含 material。 +- [ ] 确认外部粒子力导入接口仍可用。 +- [ ] 将外部粒子力导入接入显式步外载占位。 +- [ ] 继续记录 coupling work 占位。 +- [ ] 继续记录 coupling momentum 占位。 +- [ ] 继续记录 coupling energy 占位。 +- [ ] 保持接触耦合类型字段。 +- [ ] 保持连续体耦合类型字段。 +- [ ] 确认接口命名不出现 FEMLite 内部结构。 +- [ ] 确认默认 gate 不运行真实 FEM-SPH 联合测试。 +- [ ] 扩展 coupling_interface 测试覆盖 Water 运行后状态。 + +## 阶段 Q:Pressure Wave Smoke + +- [ ] 确定 pressure wave smoke 的最小粒子布置。 +- [ ] 确定 pressure wave smoke 的初始压力扰动。 +- [ ] 确定 pressure wave smoke 的运行步数。 +- [ ] 确定 pressure wave smoke 的 checker 阈值。 +- [ ] 实现 pressure wave smoke 输入文件。 +- [ ] 实现 pressure wave smoke 运行脚本或测试程序。 +- [ ] 检查 pressure wave smoke 输出有限性。 +- [ ] 检查 pressure wave smoke 压力扰动传播方向。 +- [ ] 检查 pressure wave smoke 质量趋势。 +- [ ] 检查 pressure wave smoke 动量趋势。 +- [ ] 检查 pressure wave smoke 输出字段。 +- [ ] 确认 pressure wave smoke 不作为工程精度 benchmark。 +- [ ] 确认 pressure wave smoke 保持小规模。 +- [ ] 将 pressure wave smoke 纳入默认 gate。 + +## 阶段 R:Source Size 和测试门禁 + +- [ ] 确认 source size 覆盖 Fortran 文件。 +- [ ] 确认 source size 覆盖 C 文件。 +- [ ] 确认 source size 覆盖 C++ 头文件。 +- [ ] 确认 source size 覆盖 Python 文件。 +- [ ] 为新增大文件登记必要例外。 +- [ ] 确认 stale exception 检查有效。 +- [ ] 运行 `kernel` 测试。 +- [ ] 运行 `neighbor` 测试。 +- [ ] 运行 `water_eos` 测试。 +- [ ] 运行 `water_density` 测试。 +- [ ] 运行 `water_viscosity` 测试。 +- [ ] 运行 `water_force` 测试。 +- [ ] 运行 `solver_water` 测试。 +- [ ] 运行 `pressure_wave_smoke` 测试。 +- [ ] 运行 0.1.1 既有接口测试。 +- [ ] 运行 `source_size` 测试。 +- [ ] 运行 `git diff --check`。 + +## 阶段 S:文档同步 + +- [ ] 更新 README 当前版本为 0.2.0。 +- [ ] 更新 README 的 0.2.0 实际能力说明。 +- [ ] 记录 0.2.0 交付 Water 基础弱可压 SPH。 +- [ ] 记录 0.2.0 不交付 Gas 生产路径。 +- [ ] 记录 0.2.0 不交付 Explosive 生产路径。 +- [ ] 记录 0.2.0 不交付 Solid 生产路径。 +- [ ] 记录 0.2.0 不交付 Shell 生产路径。 +- [ ] 记录 0.2.0 不交付真实 FEM-SPH 联合运行。 +- [ ] 更新 `docs/09_release_history.md` 的 0.2.0 条目。 +- [ ] 更新 `docs/11_benchmark_matrix.md` 的 0.2.0 gate。 +- [ ] 确认 01-06 文档与 0.2.0 实际能力不冲突。 +- [ ] 确认文档使用 UTF-8 without BOM。 +- [ ] 确认文档不宣称已实现完整水下爆炸。 +- [ ] 确认文档不宣称已实现生产级多物理耦合。 + +## 阶段 T:0.2.0 验收 + +- [ ] 完整配置 CMake。 +- [ ] 完整编译 `sphlite_core`。 +- [ ] 完整编译 `sphlite_c`。 +- [ ] 完整编译 `sphlite`。 +- [ ] 完整编译新增 Fortran 测试目标。 +- [ ] 完整编译新增 C/C++ 测试目标。 +- [ ] 运行 `ctest -R "^(smoke|input|kernel|neighbor|water_eos|water_density|water_viscosity|water_force|solver_water|output|restart|c_api|cpp_api|python_debug|cli|pressure_wave_smoke)$"`。 +- [ ] 运行 `ctest -R "^source_size$"`。 +- [ ] 运行 `git diff --check`。 +- [ ] 确认没有提交 build 目录。 +- [ ] 确认没有提交 Python 缓存。 +- [ ] 确认没有提交 CSV 临时结果。 +- [ ] 确认没有提交 VTK 临时结果。 +- [ ] 确认没有提交 restart 临时结果。 +- [ ] 确认没有残留旧模块命名。 +- [ ] 确认没有新增 FEMLite 源码依赖。 +- [ ] 确认没有新增几何生成能力。 +- [ ] 确认没有新增网格生成能力。 +- [ ] 记录 0.2.0 release gate 结果。 diff --git a/tools/testing/check_source_size.py b/tools/testing/check_source_size.py index 88b4d12..0adb850 100644 --- a/tools/testing/check_source_size.py +++ b/tools/testing/check_source_size.py @@ -7,6 +7,7 @@ from pathlib import Path FORTRAN_FILE_MAX_LINES = 700 C_FILE_MAX_LINES = 500 +PYTHON_FILE_MAX_LINES = 500 FORTRAN_PROCEDURE_MAX_LINES = 140 @@ -34,7 +35,7 @@ def source_files(root: Path) -> list[Path]: continue if rel_path.parts and rel_path.parts[0].startswith("build"): continue - if path.suffix.lower() in {".f90", ".c", ".h", ".hpp"}: + if path.suffix.lower() in {".f90", ".c", ".cpp", ".h", ".hpp", ".py"}: result.append(path) return sorted(result) @@ -49,8 +50,10 @@ def check_file_lines(root: Path, path: Path, exceptions: set[str], errors: list[ suffix = path.suffix.lower() if suffix in {".f90"}: limit = FORTRAN_FILE_MAX_LINES - elif suffix in {".c", ".h", ".hpp"}: + elif suffix in {".c", ".cpp", ".h", ".hpp"}: limit = C_FILE_MAX_LINES + elif suffix == ".py": + limit = PYTHON_FILE_MAX_LINES else: return if len(lines) > limit and rel not in exceptions: diff --git a/tools/testing/test_cli.py b/tools/testing/test_cli.py new file mode 100644 index 0000000..376b73c --- /dev/null +++ b/tools/testing/test_cli.py @@ -0,0 +1,63 @@ +from __future__ import annotations + +import argparse +import subprocess +from pathlib import Path + + +def run_command(args: list[str], expect_ok: bool = True) -> subprocess.CompletedProcess[str]: + result = subprocess.run(args, text=True, capture_output=True) + if expect_ok and result.returncode != 0: + raise AssertionError(result.stderr or result.stdout) + if not expect_ok and result.returncode == 0: + raise AssertionError("command unexpectedly succeeded") + return result + + +def require_field(output: str, field: str) -> None: + if f"{field}:" not in output: + raise AssertionError(f"missing CLI field {field}") + + +def main() -> int: + parser = argparse.ArgumentParser() + parser.add_argument("--exe", required=True) + parser.add_argument("--input", required=True) + parser.add_argument("--bad-input", required=True) + parser.add_argument("--output-dir", required=True) + args = parser.parse_args() + + exe = str(Path(args.exe)) + model = str(Path(args.input)) + bad_model = str(Path(args.bad_input)) + output_dir = str(Path(args.output_dir)) + + version = run_command([exe, "--version"]) + require_field(version.stdout, "version") + if "0.1.1" not in version.stdout: + raise AssertionError("CLI version mismatch") + + inspect = run_command([exe, model]) + for field in ("version", "input", "mode", "particles", "warnings", "time", "step", "status"): + require_field(inspect.stdout, field) + if "mode: inspect" not in inspect.stdout: + raise AssertionError("inspect mode not reported") + + check = run_command([exe, model, "--check"]) + if "mode: check" not in check.stdout: + raise AssertionError("check mode not reported") + + run = run_command([exe, model, "--run", "--output", output_dir]) + if "mode: run" not in run.stdout: + raise AssertionError("run mode not reported") + if "step: 1" not in run.stdout: + raise AssertionError("run mode did not advance") + + bad = run_command([exe, bad_model, "--check"], expect_ok=False) + require_field(bad.stderr, "error_code") + require_field(bad.stderr, "error_message") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/testing/test_python_debug.py b/tools/testing/test_python_debug.py index 7e11f47..db96e30 100644 --- a/tools/testing/test_python_debug.py +++ b/tools/testing/test_python_debug.py @@ -14,9 +14,12 @@ def main() -> int: root = Path(__file__).resolve().parents[2] sys.path.insert(0, str(root / "Python")) - from sphlite_debug import SPHLite + from sphlite_debug import PHASE_SOLID, STATUS_ACTIVE, SPHLite, SPHLiteError solver = SPHLite(args.library) + if PHASE_SOLID != 4 or STATUS_ACTIVE != 0: + raise AssertionError("Python constants are not locked") + solver.initialize() try: solver.read_inp(args.input) @@ -24,9 +27,29 @@ def main() -> int: count = solver.particle_count() if count != 4: raise AssertionError(f"expected 4 particles, got {count}") + state = solver.particle_state() + if state["phase"][0] != PHASE_SOLID: + raise AssertionError("unexpected particle phase") + solver.set_output_directory(root / "build-codex-unix") + solver.write_output(0, 0.0) + restart = root / "build-codex-unix" / "python_restart.tmp" + solver.write_restart(restart) + solver.read_restart(restart) solver.run_explicit() finally: solver.finalize() + + missing = SPHLite(args.library) + missing.initialize() + try: + try: + missing.read_inp(root / "tests" / "data" / "does_not_exist.inp") + except SPHLiteError: + pass + else: + raise AssertionError("missing input did not raise") + finally: + missing.finalize() return 0