diff --git a/.gitattributes b/.gitattributes index 9d0229d..022bc3f 100644 --- a/.gitattributes +++ b/.gitattributes @@ -11,6 +11,8 @@ CMakeLists.txt text eol=lf *.inp text eol=lf *.txt text eol=lf *.py text eol=lf +*.bat text eol=lf +*.ps1 text eol=lf *.c text eol=lf *.h text eol=lf *.cpp text eol=lf diff --git a/.gitignore b/.gitignore index 1437b8f..6357f26 100644 --- a/.gitignore +++ b/.gitignore @@ -31,6 +31,8 @@ nodes_*.csv elements_*.csv reactions_*.csv result_*.vtk +result_*.vtu +results.pvd contact_*.csv restart_*.frs /output/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 3253def..ce96b9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.20) project(FemLite - VERSION 0.6.3 + VERSION 0.6.4 LANGUAGES C Fortran ) @@ -180,6 +180,7 @@ add_library(femlite_core STATIC src/base/fem_api_state_access.f90 src/base/fem_api_stable_dt.f90 src/base/fem_api.f90 + src/worker/fem_cclite_worker.f90 src/interface/fem_fortran_bridge.f90 ) @@ -204,6 +205,7 @@ endif() add_library(femlite_c STATIC src/interface/fem_c_api.c + src/interface/fem_cclite_ipc.c ) target_link_libraries(femlite_c PUBLIC femlite_core) @@ -609,6 +611,18 @@ add_executable(test_facet_nodal_c_api target_link_libraries(test_facet_nodal_c_api PRIVATE femlite_c) +add_executable(test_cclite_ipc_adapter + tests/interface/test_cclite_ipc_adapter.c +) + +target_link_libraries(test_cclite_ipc_adapter PRIVATE femlite_c) + +add_executable(test_cclite_ipc_worker_loop + tests/interface/test_cclite_ipc_worker_loop.c +) + +target_link_libraries(test_cclite_ipc_worker_loop PRIVATE femlite_c) + add_executable(test_implicit_inp tests/module/test_implicit_inp.f90 ) @@ -932,6 +946,28 @@ set_tests_properties(facet_nodal_c_api PROPERTIES WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" ) +add_test(NAME cclite_worker_mock + COMMAND "${CMAKE_COMMAND}" + -DFEMLITE_EXE=$ + -DSOURCE_DIR=${CMAKE_SOURCE_DIR} + -DOUT_DIR=${CMAKE_BINARY_DIR}/cclite_worker_mock + -DSTEPS=100 + -P ${CMAKE_SOURCE_DIR}/tests/cmake/run_cclite_worker_mock.cmake +) +set_tests_properties(cclite_worker_mock PROPERTIES + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" +) + +add_test(NAME cclite_ipc_adapter COMMAND test_cclite_ipc_adapter) +set_tests_properties(cclite_ipc_adapter PROPERTIES + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" +) + +add_test(NAME cclite_ipc_worker_loop COMMAND test_cclite_ipc_worker_loop) +set_tests_properties(cclite_ipc_worker_loop PROPERTIES + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" +) + add_test(NAME perf_contact_pair_smoke COMMAND perf_contact_pair 32 1) set_tests_properties(perf_contact_pair_smoke PROPERTIES WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" diff --git a/Python/femlite_debug.py b/Python/femlite_debug.py deleted file mode 100644 index 10b762c..0000000 --- a/Python/femlite_debug.py +++ /dev/null @@ -1,71 +0,0 @@ -from __future__ import annotations - -from dataclasses import dataclass -from pathlib import Path -import re -import subprocess -from typing import Iterable - - -_FIELD_RE = re.compile(r"^\s*([A-Za-z][A-Za-z0-9 _-]*):\s*(.+?)\s*$") - - -@dataclass -class FemLiteResult: - args: list[str] - returncode: int - stdout: str - stderr: str - - def check(self) -> "FemLiteResult": - if self.returncode != 0: - raise RuntimeError( - "FemLite command failed with code " - f"{self.returncode}: {' '.join(self.args)}\n{self.stdout}\n{self.stderr}" - ) - return self - - def fields(self) -> dict[str, str]: - values: dict[str, str] = {} - for line in self.stdout.splitlines(): - match = _FIELD_RE.match(line) - if match: - key = match.group(1).strip().lower().replace(" ", "_") - values[key] = match.group(2).strip() - return values - - def int_field(self, name: str) -> int: - return int(self.fields()[name]) - - def float_field(self, name: str) -> float: - return float(self.fields()[name]) - - -class FemLiteRunner: - def __init__(self, executable: str | Path): - self.executable = Path(executable) - - def inspect_model(self, inp_file: str | Path) -> FemLiteResult: - return self._run([str(inp_file)]) - - def run_explicit(self, inp_file: str | Path) -> FemLiteResult: - return self._run([str(inp_file), "--run"]) - - def solve_implicit_linear(self, inp_file: str | Path) -> FemLiteResult: - return self._run([str(inp_file), "--implicit-linear"]) - - def _run(self, args: Iterable[str]) -> FemLiteResult: - command = [str(self.executable)] - command.extend(args) - completed = subprocess.run( - command, - check=False, - text=True, - capture_output=True, - ) - return FemLiteResult( - args=command, - returncode=completed.returncode, - stdout=completed.stdout, - stderr=completed.stderr, - ) diff --git a/README.md b/README.md index 4705cc9..7cde197 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,9 @@ FemLite 是一个轻量级有限元求解器,核心使用 Fortran 实现,面向结构动力学、静力学、非线性分析、壳/实体单元、接触、材料非线性和紧凑工程 benchmark 开发。 -当前项目版本:**0.6.3**。 +当前项目版本:**0.6.4**。 -0.6.3 是元素基础设施版本,新增 `S4R`、受控 `C3D8R`、积分规则基础设施和元素 introspection API。`v0.5.0` 仍是第五版验收检查点,0.6.x 在此基础上继续维护生命周期诊断、restart 诊断、contact/solver/path 输出硬化和 release gate。 +0.6.4 是 CCLite coupling 基础闭环版本,新增中立 coupling API、worker mock、Windows IPC adapter、surface/facet 扩展导出、外部节点力导入和诊断报告。`v0.5.0` 仍是第五版验收检查点,0.6.x 在此基础上继续维护元素基础设施、生命周期诊断、restart 诊断、contact/solver/path 输出硬化和 release gate。 ## 当前能力 @@ -15,8 +15,9 @@ FemLite 是一个轻量级有限元求解器,核心使用 Fortran 实现,面 - 线弹性、J2/J2_JC 和 Mohr-Coulomb 材料路径。 - 内部 CG、BiCGSTAB、GMRES 和 dense pivoting 线性求解器。 - 接触、mortar/AL、摩擦、cohesive 和颗粒耦合诊断基础设施。 -- CSV、VTK、restart、Fortran API 和 C ABI。 +- CSV、VTU/PVD、restart、Fortran API 和 C ABI。 - 元素元数据、节点、坐标、积分点、形函数、Jacobian 和 B 矩阵查询。 +- CCLite coupling surface/facet 查询、节点力导入、worker mock、IPC adapter 和载荷诊断 CSV。 ## 当前不声明的能力 @@ -46,13 +47,11 @@ cmake --build build-codex-gcc --parallel ctest --test-dir build-codex-gcc --output-on-failure ``` -## 0.6.3 Release Gate +## 0.6.4 Release Gate ```powershell cmake --build build-codex-gcc --parallel -ctest --test-dir build-codex-gcc -R "^(smoke|element_infrastructure|database_topology|element_stiffness|shell_internal|c_api)$" --output-on-failure -ctest --test-dir build-codex-gcc -R "^source_size$" --output-on-failure -ctest --test-dir build-codex-gcc -L v5_acceptance --output-on-failure +ctest --test-dir build-codex-gcc --output-on-failure git diff --check ``` diff --git a/docs/01_requirements_specification.md b/docs/01_requirements_specification.md index 6f9e1c6..05f074e 100644 --- a/docs/01_requirements_specification.md +++ b/docs/01_requirements_specification.md @@ -1,12 +1,12 @@ # 01 需求规格说明 -文档状态:0.6.3 版整理稿 +文档状态:0.6.4 版整理稿 ## 1. 软件定位 FemLite 是一个轻量级有限元求解器,核心使用 Fortran 实现,面向结构动力学、静力学、非线性分析、壳/实体单元、接触、材料非线性和紧凑工程基准算例开发。 -当前项目版本为 0.6.3。0.6.3 是元素基础设施版本,重点交付 `S4R`、受控 `C3D8R`、积分规则基础设施和元素信息查询接口。 +当前项目版本为 0.6.4。0.6.4 是 CCLite coupling 基础闭环版本,重点交付中立 coupling API、worker mock、Windows IPC adapter、surface/facet 扩展导出、外部节点力导入和诊断报告。 ## 2. 当前交付范围 @@ -19,24 +19,24 @@ FemLite 是一个轻量级有限元求解器,核心使用 Fortran 实现,面 - 线弹性、J2/J2_JC 和 Mohr-Coulomb 材料路径。 - 内部 CG、BiCGSTAB、GMRES 和 dense pivoting 线性求解器。 - 基础接触、mortar/AL、摩擦、cohesive 和颗粒耦合诊断基础设施。 -- CSV、VTK、restart、Fortran API 和 C ABI 输出/查询接口。 +- CSV、VTU/PVD、restart、Fortran API 和 C ABI 输出/查询接口。 - 元素 introspection API:元素类型、节点、坐标、积分点、形函数、Jacobian 和 B 矩阵。 +- CCLite coupling surface/facet 查询、节点力导入、worker mock、IPC adapter 和载荷诊断 CSV。 - `continuation_path.csv`、`nonlinear_lifecycle.csv` 和 restart version 5 生命周期诊断。 -## 3. 0.6.3 新增需求 - -0.6.3 的新增需求已经交付: - -- 增加 `S4R` 四节点减缩积分壳单元。 -- 增加 `C3D8R` 八节点减缩积分实体单元受控子集。 -- 为四边形和六面体张量积域增加 Gauss-Lobatto 与闭型 Newton-Cotes 积分规则。 -- 保持单元名称与 Abaqus 常用名称一致。 -- 暴露元素节点列表、节点位置、积分点、形函数、Jacobian 和 B 矩阵。 -- C API 采用调用者分配缓冲区的 dimension-first 模式。 +## 3. 0.6.4 新增需求 +0.6.4 的新增需求已经交付: +- 增加中立 coupling surface/facet 查询和扩展导出接口。 +- 增加 coupling 节点力导入接口,包含 `source_id`、`load_mode` 和 `valid_flags`。 +- 增加本步总力、总力矩、拒绝计数和按 `source_id` 汇总的诊断。 +- 增加 `femlite_cclite_load_report.csv` 与 `femlite_cclite_surface_report.csv` 输出。 +- 增加 worker mock 命令状态机和 VTU/PVD + coupling 诊断输出闭环。 +- 增加 Windows shared memory / named event IPC adapter 的 C 层边界。 +- 保留 0.6.3 的 `S4R`、受控 `C3D8R`、积分规则基础设施和元素 introspection API。 ## 4. 明确不交付范围 -以下内容不属于 0.6.3 交付: +以下内容不属于 0.6.4 交付: - 完整 reduced-integration solid 家族。 - 完整 Abaqus 单元库。 @@ -47,6 +47,7 @@ FemLite 是一个轻量级有限元求解器,核心使用 Fortran 实现,面 - 广义 mixed-control continuation 和分支切换。 - cohesive 主线接触 step。 - DEM/FEM 子循环和大规模颗粒长算例。 +- 真实 SPH-FEM penalty contact、FSI 和强耦合 rollback。 - 生产级滚压成形工艺链。 ## 5. 质量要求 @@ -54,5 +55,5 @@ FemLite 是一个轻量级有限元求解器,核心使用 Fortran 实现,面 - 所有新增功能必须有小型、可重复的测试或 checker。 - 公开接口必须通过 `ierr` 或 C 返回码报告错误,不得直接终止程序。 - 不支持的输入必须明确报错或明确 warning,不得静默回退到不同物理模型。 -- 生成的 CSV、VTK、restart 和临时结果不得提交。 +- 生成的 CSV、VTU/PVD、restart 和临时结果不得提交。 - 发布前必须通过当前版本 release gate。 diff --git a/docs/02_software_architecture_design.md b/docs/02_software_architecture_design.md index f19ae97..90cd3cc 100644 --- a/docs/02_software_architecture_design.md +++ b/docs/02_software_architecture_design.md @@ -1,6 +1,6 @@ # 02 软件架构设计 -文档状态:0.6.3 版整理稿 +文档状态:0.6.4 版整理稿 ## 1. 架构概览 @@ -21,7 +21,7 @@ FemLite 采用 Fortran 95 风格的过程式架构。核心数据集中存放在 | `src/material` | 材料模型、Mohr-Coulomb、强度折减和材料点驱动 | | `src/boundary` | 约束、接触、mortar/AL、cohesive、滚压/刚体工具 | | `src/solver` | 静力、隐式、非线性、线性求解器、屈曲和模态工具 | -| `src/output` | CSV、VTK、restart 和模态输出 | +| `src/output` | CSV、VTU/PVD、restart 和模态输出 | | `src/interface` | Fortran-C bridge 和 C ABI | | `tests` | 单元、模块、接口、CLI、benchmark 和性能测试 | | `tools` | benchmark 编排、测试辅助和脚本 | @@ -33,7 +33,7 @@ FemLite 采用 Fortran 95 风格的过程式架构。核心数据集中存放在 2. `fem_read_inp` 读取输入文件并填充模型数据库。 3. `fem_build_model` 校验模型、建立边界 facet 和必要的派生数据。 4. 求解器根据命令行或 API 调用执行显式、静力、隐式或专用 benchmark 路径。 -5. 输出层写入 CSV、VTK、restart 或诊断文件。 +5. 输出层写入 CSV、VTU/PVD、restart 或诊断文件。 6. API 层提供模型状态、节点、元素、接触和诊断查询。 7. `fem_finalize` 释放数据库、关闭日志并重置生命周期状态。 diff --git a/docs/03_detailed_design.md b/docs/03_detailed_design.md index d80867b..09baa83 100644 --- a/docs/03_detailed_design.md +++ b/docs/03_detailed_design.md @@ -1,6 +1,6 @@ # 03 详细设计 -文档状态:0.6.3 版整理稿 +文档状态:0.6.4 版整理稿 ## 1. 模型数据库 @@ -78,7 +78,7 @@ 主要输出包括: - `history.csv`、节点/单元 CSV、接触 CSV、path CSV。 -- legacy ASCII VTK。 +- 默认场变量输出 `result_XXXXXXXX.vtu` 和 `results.pvd`;legacy ASCII VTK 写入口保留为兼容路径。 - restart 文件。 - `nonlinear_lifecycle.csv`。 - benchmark 专用 summary/profile CSV。 diff --git a/docs/04_mathematical_model_and_algorithms.md b/docs/04_mathematical_model_and_algorithms.md index 8fba9d7..624bff0 100644 --- a/docs/04_mathematical_model_and_algorithms.md +++ b/docs/04_mathematical_model_and_algorithms.md @@ -1,6 +1,6 @@ # 04 数学模型与算法 -文档状态:0.6.3 版整理稿 +文档状态:0.6.4 版整理稿 ## 1. 控制方程 diff --git a/docs/05_interface_control_document.md b/docs/05_interface_control_document.md index 1cb8d1f..1096527 100644 --- a/docs/05_interface_control_document.md +++ b/docs/05_interface_control_document.md @@ -1,6 +1,6 @@ # 05 接口控制文档 -文档状态:0.6.3 版整理稿 +文档状态:0.6.4 版整理稿 ## 1. Fortran API 总则 @@ -118,7 +118,208 @@ C 数组使用 Fortran 顺序展开: 无效 C 参数返回 `-1`;Fortran 侧校验错误返回 FemLite `ierr`。 -## 7. 输出接口 +## 7. Coupling API + +阶段 A 提供中立 coupling wrapper,复用已有 boundary facet 和外部载荷实现。旧 `sph` 命名接口保留兼容。 + +Fortran API: + +```fortran +subroutine fem_get_coupling_surface_count(num_facets, max_face_nodes, ierr) +subroutine fem_get_coupling_surface_facets(nfacet_in, max_face_nodes_in, & + facet_id, elem_id, face_id, & + facet_nnode, facet_conn, x, vnod, & + normal, area, ierr) +subroutine fem_clear_coupling_loads(ierr) +subroutine fem_apply_coupling_nodal_forces(num_loads, node_ids, forces, & + source_ids, load_mode, & + valid_flags, ierr) +subroutine fem_get_coupling_load_diag(total_force, total_moment, & + accepted_count, rejected_nonfinite, & + rejected_unknown_node, & + rejected_capacity, ierr) +``` + +C ABI: + +```c +int femlite_get_coupling_surface_count(int *num_facets, int *max_face_nodes); +int femlite_get_coupling_surface_facets(int num_facets, int max_face_nodes, + int *facet_id, int *elem_id, + int *face_id, int *facet_nnode, + int *facet_conn, double *x, + double *vnod, double *normal, + double *area); +int femlite_clear_coupling_loads(void); +int femlite_apply_coupling_nodal_forces(int num_loads, const int *node_ids, + const double *forces, + const int *source_ids, + const int *load_mode, + const int *valid_flags); +int femlite_get_coupling_load_diagnostics(double *total_force, + double *total_moment, + int *accepted_count, + int *rejected_nonfinite, + int *rejected_unknown_node, + int *rejected_capacity); +int femlite_get_coupling_source_load_diagnostics(int source_id, + double *total_force, + double *total_moment, + int *accepted_count); +int femlite_write_coupling_reports(const char *load_report, + const char *surface_report); +int femlite_get_coupling_surface_count_for_name(const char *surface_name, + int *num_facets, + int *max_face_nodes); +int femlite_get_coupling_surface_facets_ex(int num_facets, + int max_face_nodes, + int *facet_id, + int *surface_id, + int *elem_id, + int *face_id, + int *facet_nnode, + int *facet_conn, + double *x, + double *vnod, + double *normal, + double *area, + int *material_id, + int *section_id, + int *valid_flags, + double *shell_thickness, + double *mass_per_area); +int femlite_get_coupling_surface_facets_ex_for_name( + const char *surface_name, + int num_facets, + int max_face_nodes, + int *facet_id, + int *surface_id, + int *elem_id, + int *face_id, + int *facet_nnode, + int *facet_conn, + double *x, + double *vnod, + double *normal, + double *area, + int *material_id, + int *section_id, + int *valid_flags, + double *shell_thickness, + double *mass_per_area); +``` + +阶段 A 约定: + +- 未指定 surface 时导出全部边界 facet,`surface_id=0`,并写 warning。 +- `surface_name` 过滤支持已有 `*SURFACE, NAME=..., TYPE=ELEMENT`。 +- `facet_conn` 使用外部节点 id。 +- `x[dim + 3 * (local_node + max_face_nodes * facet)]`。 +- `vnod[dim + 3 * (local_node + max_face_nodes * facet)]`。 +- `normal[dim + 3 * facet]`。 +- `forces[dim + 3 * load]`。 +- `valid_flags=0` 的节点力记录会被跳过。 +- 非有限载荷、未知节点和负 metadata 会被拒绝并记录诊断计数。 +- 扩展 surface 导出中,`valid_flags=1` 表示 facet 几何有效,`0` 表示退化或未分类无效。 +- 壳单元导出 `shell_thickness` 和 `mass_per_area`;实体面二者返回 `0`。 + +### 7.1 Coupling 载荷诊断报告 + +- coupling 节点力导入后维护独立诊断缓存,缓存不作为公共全局力数组暴露。 +- `femlite_clear_coupling_loads` 同时清空本步外部载荷和诊断缓存,worker 在每个 `IMPORT_LOADS` 步骤前调用。 +- `femlite_get_coupling_source_load_diagnostics` 按 `source_id` 查询本步接受记录数、总力和总力矩。 +- `femlite_write_coupling_reports` 写出逐记录载荷表和按 source 汇总表。当前 `surface_report` 参数名用于后续 surface/source 映射扩展,现阶段按 `source_id` 汇总。 +- worker mock 在 `WRITE_OUTPUT` 时默认输出 `femlite_cclite_load_report.csv` 和 `femlite_cclite_surface_report.csv`,并与 VTU/PVD 输出同目录保存。 + +## 8. CCLite Worker Mock + +阶段 B 提供 worker mock 模式,用于验证 FemLite 侧命令状态机和空耦合闭环。该模式不是真实 Windows IPC,不打开 shared memory,也不等待 named event。 + +CLI: + +```powershell +femlite --cclite-worker --case input.inp --ipc-name mock ` + --log worker_mock.log --output-dir worker_output --mock-steps 100 +``` + +命令序列: + +```text +INIT +READ_INPUT +BUILD_MODEL +EXPORT_INTERFACE +IMPORT_LOADS +ADVANCE +WRITE_OUTPUT +STOP +``` + +worker 日志字段: + +```text +seq,ack_seq,command_id,step_id,substep_id,iteration_id,state,error_code +``` + +状态约定: + +- `0`:CREATED。 +- `1`:READY。 +- `2`:RUNNING。 +- `3`:DONE。 +- `-1`:ERROR。 + +当前边界: + +- mock 模式内部顺序执行命令,不接收外部 IPC command block。 +- `IMPORT_LOADS` 当前导入空载荷,用于空耦合 smoke。 +- `ADVANCE` 使用单步显式推进。 +- `WRITE_OUTPUT` 调用现有 CSV 与 VTU/PVD 输出路径。 + +## 9. CCLite Windows IPC Adapter + +阶段 C 提供 Windows IPC adapter 的 C 层边界,供后续真实 CCLite worker 接入。当前 adapter 已有 shared memory、named event、command block 读写和 smoke,不等同于主程序已经完成跨进程 worker 联调。 + +C 头文件: + +```c +#include "femlite_cclite_ipc.h" +``` + +command block 二进制布局: + +```c +typedef struct femlite_cclite_command_block { + int32_t version; + int32_t seq; + int32_t ack_seq; + int32_t command_id; + int32_t step_id; + int32_t substep_id; + int32_t iteration_id; + int32_t worker_state; + int32_t error_code; +} femlite_cclite_command_block; +``` + +命名规则: + +```text +_COMMAND_BLOCK +_COMMAND_EVENT +_DONE_EVENT +``` + +adapter 约定: + +- FemLite 只 open CCLite 创建的 shared memory 和 event。 +- FemLite 可以 wait `COMMAND_EVENT`、读取 command block、写回 `ack_seq`/`worker_state`/`error_code` 并 signal `DONE_EVENT`。 +- FemLite 不 reset event;reset 责任属于 CCLite 侧。 +- timeout 使用稳定错误码 `FEMLITE_CCL_IPC_ERR_TIMEOUT`。 +- 当前 smoke 覆盖 open、wait/signal、不 reset event、INIT/STOP、错误路径和 100 step 空耦合。 +- `femlite_cclite_transport` 提供 mock 和 Windows IPC 两个 backend 的统一 `receive/complete` facade。 + +## 10. 输出接口 ```fortran subroutine fem_set_output_directory(path, ierr) diff --git a/docs/06_test_and_verification_plan.md b/docs/06_test_and_verification_plan.md index 7a34306..04dc3dd 100644 --- a/docs/06_test_and_verification_plan.md +++ b/docs/06_test_and_verification_plan.md @@ -1,6 +1,6 @@ # 06 测试与验证计划 -文档状态:0.6.3 版整理稿 +文档状态:0.6.4 版整理稿 ## 1. 测试分层 @@ -13,35 +13,31 @@ | benchmark | 带 checker 的紧凑工程验收 | | release gate | 当前版本发布前必须通过的组合测试 | -## 2. 当前 0.6.3 Release Gate +## 2. 当前 0.6.4 Release Gate ```powershell cmake --build build-codex-gcc --parallel -ctest --test-dir build-codex-gcc -R "^(smoke|element_infrastructure|database_topology|element_stiffness|shell_internal|c_api)$" --output-on-failure -ctest --test-dir build-codex-gcc -R "^source_size$" --output-on-failure -ctest --test-dir build-codex-gcc -L v5_acceptance --output-on-failure +ctest --test-dir build-codex-gcc --output-on-failure git diff --check ``` -当前本地 0.6.3 证据: - +当前本地 0.6.4 证据: - 完整编译通过。 -- 0.6.3 聚焦回归 6/6 通过。 -- `source_size` 1/1 通过。 -- examples 精简后,`v5_acceptance` 34/34 通过。 +- 完整回归 73/73 通过。 +- CCLite/coupling 相关回归覆盖 `c_api`、`facet_nodal_c_api`、`cclite_worker_mock`、`cclite_ipc_adapter`、`cclite_ipc_worker_loop`、`implicit_inp` 和 `vtk_output`。 - `git diff --check` 通过。 -## 3. 0.6.3 聚焦覆盖 +## 3. 0.6.4 聚焦覆盖 | 测试 | 覆盖内容 | |---|---| -| `element_infrastructure` | Abaqus 风格名称、`S4R`/`C3D8R` 拓扑、积分描述符、Gauss-Lobatto/Newton-Cotes 规则 | -| `database_topology` | `S4R`/`C3D8R` 数据库识别、Fortran 元素查询 API、B 矩阵查询 | -| `element_stiffness` | `S4R`/`C3D8R` 刚度、刚体检查、hourglass 稳定性 | -| `shell_internal` | `S4R` 内力、patch 行为和 hourglass 力一致性 | -| `smoke` | `C3D8R` INP 读入、质量、线弹性内力和状态平均 | -| `c_api` | C ABI 元素查询、调用者缓冲区、小缓冲区失败 | - +| `c_api` | coupling 节点力导入、source 诊断、CSV 报告、显式单步响应和 C ABI 版本 smoke | +| `implicit_inp` | coupling 节点力叠加到静力线性 RHS 后产生位移响应 | +| `facet_nodal_c_api` | surface/facet 扩展字段、退化 facet、壳厚度和 surface 名称过滤 | +| `cclite_worker_mock` | worker 命令状态机、seq/ack、100 step 空耦合、VTU/PVD 和 coupling 诊断报告 | +| `cclite_ipc_adapter` | shared memory、named event、timeout、wait/signal 和不 reset event | +| `cclite_ipc_worker_loop` | transport facade 下 INIT 到 STOP 和 100 step 空耦合 | +| `vtk_output` | 默认 VTU/PVD 场变量输出路径 | ## 4. V5 验收子集 `v5_acceptance` 标签保持对第五版核心能力的回归覆盖。当前 examples 已精简,难度较大的历史示例不再作为当前示例交付项。 @@ -74,12 +70,12 @@ git diff --check - README、核心文档、版本历史和 benchmark 矩阵与实际版本一致。 - `git diff --check` 无 whitespace error。 -- 根目录和示例目录没有误提交大型 CSV、VTK、restart 或临时结果。 +- 根目录和示例目录没有误提交大型 CSV、VTU/PVD、restart 或临时结果。 - 文档使用 UTF-8 without BOM 编码。 ## 7. 非默认长跑 -以下属于后续或专项验证,不作为 0.6.3 默认 gate: +以下属于后续或专项验证,不作为 0.6.4 默认 gate: - 完整 `uz=-6.2 m` 球冠后屈曲 PDF 曲线匹配。 - Mohr-Coulomb 临界失稳云图长跑。 diff --git a/docs/07_user_manual.md b/docs/07_user_manual.md index e785354..ff41bb2 100644 --- a/docs/07_user_manual.md +++ b/docs/07_user_manual.md @@ -1,6 +1,6 @@ # 07 用户手册 -文档状态:0.6.3 版整理稿 +文档状态:0.6.4 版整理稿 ## 1. 构建 @@ -66,7 +66,7 @@ femlite input.inp --buckling - `contact_XXXXXXXX.csv`。 - `continuation_path.csv`。 - `nonlinear_lifecycle.csv`。 -- VTK 文件。 +- VTU/PVD 文件。 - restart 文件。 输出目录会自动创建。生成结果通常不应提交。 @@ -88,7 +88,26 @@ C 数组为 Fortran 顺序展开。例如 B 矩阵元素位置为: bmat[row + row_count * col] ``` -## 7. 常见问题 +## 7. CCLite worker mock + +FemLite 提供 CCLite worker mock 模式,用于验证命令状态机、空耦合推进和结果输出: + +```powershell +femlite --cclite-worker --case input.inp --ipc-name mock ` + --log worker_mock.log --output-dir worker_output --mock-steps 100 +``` + +该模式会顺序执行 `INIT`、`READ_INPUT`、`BUILD_MODEL`、`EXPORT_INTERFACE`、`IMPORT_LOADS`、`ADVANCE`、`WRITE_OUTPUT` 和 `STOP`,并写出 worker 日志。它仍不是 Windows shared memory/named event IPC。 + +`WRITE_OUTPUT` 会把 VTU/PVD 结果和 coupling 诊断表写到同一输出目录: + +- `results.pvd` 与 `result_XXXXXXXX.vtu` +- `femlite_cclite_load_report.csv` +- `femlite_cclite_surface_report.csv` + +当前 CCLite 载荷导入是基础节点力闭环:FemLite 接收 `node_id + force[3] + source_id + load_mode + valid_flags`,并将其叠加到显式动力外力和静力/隐式 RHS。真实 SPH-FEM penalty contact、FSI 和强耦合 rollback 仍由后续 CCLite 联调阶段处理。 + +## 8. 常见问题 ### 输入 keyword 被跳过 @@ -106,7 +125,7 @@ FemLite 支持 Abaqus INP 子集。非关键 keyword 可能 warning 后跳过; 检查输出间隔、输出目录、命令行模式和输入控制块。 -## 8. 当前限制 +## 9. 当前限制 当前不声明: @@ -117,3 +136,4 @@ FemLite 支持 Abaqus INP 子集。非关键 keyword 可能 warning 后跳过; - 完整 cohesive 主线接触 step。 - DEM/FEM 子循环。 - 生产级滚压成形流程。 +- 真实 CCLite Windows IPC 联调。 diff --git a/docs/08_developer_manual.md b/docs/08_developer_manual.md index 95d8509..8973464 100644 --- a/docs/08_developer_manual.md +++ b/docs/08_developer_manual.md @@ -1,13 +1,13 @@ # 08 开发者手册 -文档状态:0.6.3 版整理稿 +文档状态:0.6.4 版整理稿 ## 1. 基本原则 - 默认使用现有模块和风格,不为小改动引入新架构。 - 新功能必须有小型可重复测试。 - 不支持的物理或输入必须明确报错,不得静默回退。 -- 不提交 build、results、CSV/VTK 全量结果、restart 和临时文件。 +- 不提交 build、results、CSV/VTU/PVD 全量结果、restart 和临时文件。 - 文档使用 UTF-8 without BOM。 ## 2. 新增单元流程 @@ -50,9 +50,7 @@ ctest --test-dir build-codex-gcc -L v5_acceptance --output-on-failure ```powershell cmake --build build-codex-gcc --parallel -ctest --test-dir build-codex-gcc -R "^(smoke|element_infrastructure|database_topology|element_stiffness|shell_internal|c_api)$" --output-on-failure -ctest --test-dir build-codex-gcc -R "^source_size$" --output-on-failure -ctest --test-dir build-codex-gcc -L v5_acceptance --output-on-failure +ctest --test-dir build-codex-gcc --output-on-failure git diff --check ``` @@ -74,10 +72,13 @@ git diff --check 历史版本计划、验收报告和 benchmark 矩阵不再拆成多个文件;统一维护在 09-11。 -## 7. 0.6.3 开发注意事项 +## 7. 0.6.4 开发注意事项 - `S4R` hourglass 修改必须保持 `S4` 回归绿色。 - `C3D8R` hourglass 修改必须保持 `C3D8` 回归绿色,并覆盖刚度、内力、质量和输出路径。 - B 矩阵约定变更必须同步接口文档和 C API 测试。 - 元素查询 API 不应暴露内部数组指针。 - 新增积分规则不等于开放用户侧积分选择。 +- coupling external load 必须通过中立 API 导入和清理,不得让外部程序直接写 FemLite 全局力数组。 +- worker/IPC 改动必须保持 FemLite 只 open CCLite 创建的 shared memory 和 named event,event reset 责任属于 CCLite。 +- CCLite 诊断报告字段变更必须同步 C API 测试、worker mock 测试和接口文档。 \ No newline at end of file diff --git a/docs/09_release_history.md b/docs/09_release_history.md index 9125af7..04d4889 100644 --- a/docs/09_release_history.md +++ b/docs/09_release_history.md @@ -1,6 +1,6 @@ # 09 版本历史与验收记录 -文档状态:0.6.3 版整理稿 +文档状态:0.6.4 版整理稿 ## 1. 版本线摘要 @@ -16,15 +16,40 @@ | 0.6.1 | lifecycle/contact/path 诊断继续硬化 | | 0.6.2 | 稳定性发布,不新增算法 | | 0.6.3 | 元素基础设施:`S4R`、受控 `C3D8R`、积分规则库、元素 introspection API | +| 0.6.4 | CCLite coupling 基础闭环:中立 API、worker mock、Windows IPC adapter、surface/facet 扩展导出、节点力导入与诊断 | -## 2. 0.6.3 验收记录 +## 2. 0.6.4 验收记录 + +0.6.4 已完成: + +- 中立 coupling surface/facet 查询和扩展导出接口。 +- worker mock 命令状态机、日志、100 step 空耦合和结果输出闭环。 +- Windows shared memory / named event IPC adapter 的 C 层边界和 transport facade。 +- surface/facet 扩展字段:`surface_id`、`material_id`、`section_id`、`valid_flags`、`shell_thickness`、`mass_per_area`。 +- coupling 节点力导入、独立载荷缓存、清理生命周期、总力/总力矩和按 `source_id` 汇总诊断。 +- `femlite_cclite_load_report.csv` 与 `femlite_cclite_surface_report.csv` 输出。 +- 显式单步响应、静力 RHS 响应、force balance CSV 和 worker 输出一致性测试。 + +本地验收命令: +```powershell +cmake --build build-codex-gcc --parallel +ctest --test-dir build-codex-gcc --output-on-failure +git diff --check +``` + +结果: +- 完整编译通过。 +- 完整回归 73/73 通过。 +- `git diff --check` 通过。 + +## 3. 0.6.3 验收记录 0.6.3 已完成: - Abaqus 风格单元名称 canonicalization。 - `S4R` 拓扑、数据库识别、小应变 reduced integration 路径。 - `S4R` 紧凑 hourglass 稳定性覆盖。 -- `C3D8R` 拓扑、数据库识别、1 点积分、刚度/内力/质量/VTK 路径和紧凑 hourglass 稳定性覆盖。 +- `C3D8R` 拓扑、数据库识别、1 点积分、刚度/内力/质量/场变量输出路径和紧凑 hourglass 稳定性覆盖。 - Gauss-Lobatto 和闭型 Newton-Cotes 四边形/六面体张量积规则。 - Fortran/C 元素 introspection API。 - 文档边界整理。 @@ -47,7 +72,7 @@ git diff --check - examples 精简后,`v5_acceptance` 34/34 通过。 - `git diff --check` 通过。 -## 3. 0.6.2 验收摘要 +## 4. 0.6.2 验收摘要 0.6.2 是稳定性发布,重点是 CSV schema、restart 诊断、contact/solver report、INP 错误边界和 CLI 输出目录边界。最终本地 gate 通过: @@ -58,13 +83,13 @@ git diff --check - version smoke。 - `git diff --check`。 -## 4. 0.6.0-0.6.1 验收摘要 +## 5. 0.6.0-0.6.1 验收摘要 0.6.0 引入 nonlinear lifecycle 和 rollback audit。0.6.1 在此基础上扩展 cutback taxonomy、`nonlinear_lifecycle.csv`、contact state counters、Riks/arc path 指标和线性求解器诊断字段。 这些版本不声明完整工业级 AL/mortar 一致切线、mixed-control continuation、cohesive 主线接触或 DEM/FEM 子循环。 -## 5. V5 验收摘要 +## 6. V5 验收摘要 V5 的长期验收入口保留为 `v5_acceptance` 标签。当前 examples 已精简,部分高难度历史示例不再作为当前示例交付项。 @@ -78,7 +103,7 @@ V5 当前文档口径主要覆盖: 已从当前 examples 中移除的历史难例包括:`strengthReduction`、`mohr_coulomb_yield_surface`、`ball_adhesion_decohesion`、`Dynamic_Grain_Packing_of_a_Flexible_Container`、`buckling_corrugated_shell`、`roll_forming`、`two_arches`、`block_on_arch`、`blasting_rock`、`failure_prediction_in_a_layered_shell`、`golfBallImpact`、`nonlinear_harmonic`、`stress_dependent_elasticity`、`cylinder_roller_contact`、`euler_column_buckling`、`membrane_airbag_inflation`、`pinched_hemispherical_shell`、`spherical_cap_with_central_point_load`。这些内容若后续恢复,应重新建立输入、checker 和 release gate。 -## 6. 历史文件合并说明 +## 7. 历史文件合并说明 以下旧文件内容已合并进本文档、`10_development_roadmap.md` 或 `11_benchmark_matrix.md`,因此不再单独维护: diff --git a/docs/10_development_roadmap.md b/docs/10_development_roadmap.md index 4274cb3..5edb3a0 100644 --- a/docs/10_development_roadmap.md +++ b/docs/10_development_roadmap.md @@ -1,6 +1,6 @@ # 10 后续开发路线图 -文档状态:0.6.3 版整理稿 +文档状态:0.6.4 版整理稿 ## 1. 路线图原则 @@ -43,6 +43,22 @@ 4. Shell-4/8/16 后屈曲与网格收敛对比留作研究项。 5. 大规模应用长算例继续留作后续版本。 +### 2.4 CCLite 联合改造 + +CCLite 联合改造建议单独维护在 `docs/12_cclite_integration_suggestions.md`。总体原则是保持 FemLite 独立求解器身份,先固化中立 coupling API,再做 worker/service 和 IPC。 + +建议优先级: + +1. 中立 coupling API wrapper 和 C ABI smoke。 +2. 外部载荷缓存、总力/总力矩和拒绝记录诊断。 +3. worker/service mock transport 和命令状态机。 +4. Windows shared memory + named event adapter。 +5. surface/facet 完整导出、节点力导入和 CCLite 空耦合联调。 + +不建议直接把 CCLite 接触算法写入 FemLite,也不建议一开始就做真实 SPH-FEM 接触力验收。 + +0.6.4 已完成 CCLite coupling 基础闭环:中立 coupling API、worker mock、Windows IPC adapter、surface/facet 扩展导出、节点力导入、诊断报告和对应回归测试。真实 SPH-FEM penalty contact、FSI 与强耦合 rollback 仍保留为后续版本。 + ## 3. 仍需延期的能力 - 完整 Abaqus keyword 兼容。 @@ -53,6 +69,7 @@ - 完整工业级 AL/mortar 一致切线。 - cohesive 主线接触。 - DEM/FEM 子循环。 +- CCLite worker/service、Windows IPC 和真实 SPH-FEM 联合验收。 - 大规模并行和内存优化。 ## 4. 版本规划建议 @@ -61,9 +78,9 @@ | 建议版本 | 目标 | |---|---| -| 0.6.4 | 元素查询 API 文档化示例、更多 C API smoke、用户侧错误边界 | | 0.6.5 | 用户侧积分选择或更完整 hourglass 控制二选一,不建议同时做 | -| 0.7.0 | 壳/实体非线性能力提升和轻量 benchmark 闭环 | -| 0.7.x | 接触一致切线、cohesive 主线和应用 benchmark 按需恢复 | +| 0.6.6 | 元素查询 API 文档化示例、更多 C API smoke、用户侧错误边界 | +| 0.7.0 | 壳/实体非线性能力提升、轻量 benchmark 闭环,或 CCLite worker mock 闭环二选一 | +| 0.7.x | 接触一致切线、cohesive 主线、CCLite IPC adapter 和应用 benchmark 按需恢复 | 实际版本号应以当期风险和测试闭环为准。 diff --git a/docs/11_benchmark_matrix.md b/docs/11_benchmark_matrix.md index 357f99c..37477f4 100644 --- a/docs/11_benchmark_matrix.md +++ b/docs/11_benchmark_matrix.md @@ -1,34 +1,28 @@ # 11 Benchmark 与验收矩阵 -文档状态:0.6.3 版整理稿 +文档状态:0.6.4 版整理稿 ## 1. 当前发布 Gate -0.6.3 发布 gate: - +0.6.4 发布 gate: ```powershell cmake --build build-codex-gcc --parallel -ctest --test-dir build-codex-gcc -R "^(smoke|element_infrastructure|database_topology|element_stiffness|shell_internal|c_api)$" --output-on-failure -ctest --test-dir build-codex-gcc -R "^source_size$" --output-on-failure -ctest --test-dir build-codex-gcc -L v5_acceptance --output-on-failure +ctest --test-dir build-codex-gcc --output-on-failure git diff --check ``` -## 2. 0.6.3 矩阵 +## 2. 0.6.4 矩阵 | 领域 | 测试 | 验收点 | |---|---|---| -| 单元名称 | `element_infrastructure` | 支持名称 canonicalize,不支持名称明确失败 | -| `S4R` 拓扑 | `element_infrastructure`、`database_topology` | 4 节点、shell、1 积分点 | -| `C3D8R` 拓扑 | `element_infrastructure`、`database_topology` | 8 节点、solid、1 积分点 | -| 积分描述符 | `element_infrastructure` | 现有规则行为保持稳定 | -| Gauss-Lobatto | `element_infrastructure` | 四边形/六面体点数、对称性、权重和 | -| Newton-Cotes | `element_infrastructure` | 四边形/六面体点数、对称性、权重和 | -| `S4R` 刚度/内力 | `element_stiffness`、`shell_internal` | reduced path、hourglass、刚体检查 | -| `C3D8R` 刚度/内力 | `element_stiffness`、`smoke` | reduced path、hourglass、刚体检查、线弹性平均状态 | -| Fortran introspection | `database_topology` | 元数据、节点、坐标、积分点、形函数、Jacobian、B 矩阵 | -| C introspection | `c_api` | C wrapper 成功路径和小缓冲区失败 | - +| Coupling C ABI | `c_api` | 节点力导入、source 诊断、CSV 报告、显式单步响应 | +| 静力 RHS | `implicit_inp` | coupling 节点力叠加到静力线性 RHS 后产生位移响应 | +| Surface/facet 扩展 | `facet_nodal_c_api` | material/section/valid/thickness/mass 字段、退化 facet、surface 名称过滤 | +| Worker mock | `cclite_worker_mock` | READY、seq/ack、100 step 空耦合、VTU/PVD 和 coupling 诊断报告 | +| IPC adapter | `cclite_ipc_adapter` | shared memory、named event、timeout、wait/signal、不 reset event | +| IPC worker loop | `cclite_ipc_worker_loop` | transport facade 下 INIT 到 STOP 和 100 step 空耦合 | +| VTU/PVD 输出 | `vtk_output` | 默认场变量 VTU/PVD 输出稳定 | +| 完整回归 | `ctest --output-on-failure` | 当前本地 73/73 通过 | ## 3. V5 Acceptance 矩阵 | 领域 | 代表测试或 benchmark | @@ -81,5 +75,5 @@ git diff --check - 新 benchmark 必须有输入、README、运行命令、checker 和验收指标。 - compact benchmark 不得写成完整工业能力。 -- benchmark 输出默认写入结果目录,不得提交全量 CSV/VTK。 +- benchmark 输出默认写入结果目录,不得提交全量 CSV/VTU/PVD。 - 发布 gate 变化必须同步更新 `06_test_and_verification_plan.md` 和本文档。 diff --git a/docs/12_cclite_integration_suggestions.md b/docs/12_cclite_integration_suggestions.md new file mode 100644 index 0000000..47d3758 --- /dev/null +++ b/docs/12_cclite_integration_suggestions.md @@ -0,0 +1,303 @@ +# 12 CCLite 联合改造建议 + +文档状态:根据 `..\CCLite\femlite_suggests.md` 提取并按 FemLite 当前能力修订 + +## 1. 改造口径 + +FemLite 不建议改造成 CCLite 的内部库,也不建议把 CCLite 的 SPH-FEM 接触搜索、FSI 或调度算法复制进 FemLite。更合适的边界是: + +- FemLite 保持独立求解器、独立 CLI、Fortran API 和 C ABI。 +- 新增一层面向 CCLite 的 worker/service adapter。 +- adapter 只负责命令、通信、数据映射、诊断和错误码。 +- FEM 侧只提供稳定 surface/facet 状态和外部载荷入口。 +- CCLite 负责联合调度、shared memory、named event、SPH-FEM 接触/耦合算法和联合诊断。 + +## 2. FemLite 当前已有基础 + +当前 FemLite 已具备一部分耦合基础,不应重复开发: + +- 已有边界 facet 查询能力:`fem_get_boundary_facet_count`、`fem_get_boundary_facets` 及 C ABI 对应接口。 +- 已有 facet 压力、traction、facet nodal load 入口和总力查询入口。 +- 已有元素 introspection API,可查询元素类型、节点、坐标、积分点、形函数、Jacobian 和 B 矩阵。 +- 已有接触、particle-facet、particle-FEM 相关模块和 smoke 测试,可作为后续联合验证基础。 +- 默认场变量输出已是 `result_XXXXXXXX.vtu` + `results.pvd`,适合 CCLite 联合结果检查。 + +这些能力目前仍偏内部或历史命名,例如 `sph` 前缀较多。后续建议先增加中立 coupling wrapper,而不是直接删改旧接口。 + +## 3. 必须保持的边界 + +FemLite 继续负责: + +- FEM 输入、网格、材料、section、边界条件解析。 +- 实体/壳单元内力、质量、刚度和本构更新。 +- 显式动力学、静力、非线性静力、隐式 Newmark。 +- FEM 内部接触、cohesive、mortar/AL 等已有能力。 +- restart、CSV、VTU/PVD、诊断和自身 release gate。 + +CCLite 负责: + +- 启动、监控和停止 FemLite worker。 +- 创建和销毁 Windows shared memory 与 named event。 +- 解析 CCLite 联合输入。 +- 读取 FemLite boundary facet 和 SPHLite 粒子状态。 +- 计算 SPH-FEM 接触、FSI 或其他联合界面载荷。 +- 写回 FemLite 与 SPHLite 的外部载荷。 +- 输出联合诊断和力平衡报告。 + +FemLite 不应: + +- 直接调度 SPHLite。 +- 读取 SPHLite 私有数据结构。 +- 链接 CCLite Fortran95 算法核心。 +- 在内部复制 CCLite 的 SPH-FEM 接触搜索算法。 +- reset 由 CCLite 创建并拥有的 event。 +- 把 CCLite 预留能力描述成 FemLite 已完成物理算法。 + +## 4. 推荐分阶段路线 + +### 4.1 阶段 A:中立 coupling API 固化 + +目标是把已有 facet/load 能力整理为 CCLite 可稳定依赖的公共接口,不引入 worker IPC。 + +建议内容: + +- 新增或文档化中立命名 C ABI,例如 `femlite_get_coupling_surface_count`、`femlite_get_coupling_surface_facets`、`femlite_apply_coupling_nodal_forces`。 +- 旧 `sph` 命名接口保留为兼容 wrapper。 +- 明确数组展开方式、容量不足返回、无效 id 返回和非有限载荷拒绝。 +- 增加 coupling load 总力、总力矩、拒绝记录数、节点未匹配数查询。 +- 增加 C API smoke,覆盖 facet 导出、空载荷、节点力和错误边界。 + +建议先作为 0.6.x 小版本完成。 + +当前实现状态: + +- 已新增中立 surface count/facets wrapper,阶段 A 暂导出全部边界 facet。 +- 已新增中立 nodal force wrapper,复用现有外部耦合力向量。 +- 已新增 coupling load 诊断,可查询总力、总力矩、接受记录数、非有限载荷拒绝数、未知节点拒绝数和容量/metadata 拒绝数。 +- 已补充 C API smoke,覆盖 surface count、三角形/四边形 facet 导出、当前坐标/速度导出、空载荷、节点力、非有限载荷、未知节点和容量错误。 +- surface 名称过滤、壳厚度、mass per area 和更完整 surface 字段仍留在后续阶段。 + +### 4.2 阶段 B:worker 协议 smoke + +目标是让 FemLite 具备 worker 主循环和命令状态机,但通信层先使用 mock transport 或文件/内存模拟,避免一开始就把 Windows IPC 和求解流程耦合在一起。 + +建议命令: + +```text +INIT +READ_INPUT +BUILD_MODEL +EXPORT_INTERFACE +IMPORT_LOADS +ADVANCE +WRITE_OUTPUT +STOP +``` + +完成标准: + +- `femlite.exe --cclite-worker --case femlite.inp --ipc-name ` 可进入 worker 模式。 +- 未指定 `--cclite-worker` 时原 CLI 行为完全不变。 +- worker 可进入 `READY`、响应 `STOP`、写错误码和日志。 +- mock 传输可完成 100 step 空耦合。 + +当前实现状态: + +- 已新增 `fem_cclite_worker` mock worker,内部命令覆盖 `INIT`、`READ_INPUT`、`BUILD_MODEL`、`EXPORT_INTERFACE`、`IMPORT_LOADS`、`ADVANCE`、`WRITE_OUTPUT` 和 `STOP`。 +- 已新增 worker command block,包含 `seq`、`ack_seq`、`command_id`、`step_id`、`substep_id`、`iteration_id`、`worker_state` 和 `error_code`。 +- 已新增 `--cclite-worker` CLI 入口,支持 `--case`、`--ipc-name`、`--log`、`--output-dir` 和 `--mock-steps`。 +- 已新增 CSV 风格 worker 日志,记录启动参数、命令状态和错误码。 +- 已补充 `cclite_worker_mock` 回归测试,覆盖 READY、seq/ack、100 step 空耦合、VTU/PVD 输出、STOP 和输入错误路径。 +- 当前仍是 mock 协议闭环,不是真实 Windows shared memory/named event IPC。 + +### 4.3 阶段 C:Windows IPC adapter + +目标是接入 CCLite 创建的 command block、data block、named event。 + +建议边界: + +- FemLite 只 open CCLite 创建的对象。 +- FemLite 只 wait/signal,不 reset event。 +- command block 必须包含 `seq`、`ack_seq`、`command_id`、`step_id`、`substep_id`、`iteration_id`、`error_code`。 +- 0.1.x 可固定 `substep_id=0`、`iteration_id=0`。 +- IPC 错误必须写 worker 日志和稳定错误码。 + +该阶段建议只验收空 facet 或少量固定 facet 导出,不验收真实 SPH-FEM 接触力。 + +当前实现状态: + +- 已新增 `femlite_cclite_ipc.h` 和 C adapter,定义稳定 command block 布局、命令枚举、状态枚举和 IPC 错误码。 +- 已新增 shared memory open/close wrapper,Windows 下只打开 CCLite 已创建对象,非 Windows 返回 unsupported。 +- 已新增 named event open/wait/signal/close wrapper,wait/signal 不 reset event。 +- 已新增 `femlite_cclite_wait_command` 和 `femlite_cclite_write_done`,支持等待 `COMMAND_EVENT` 后读取 command block,并写回 ack/state/error 后 signal `DONE_EVENT`。 +- 已新增 `femlite_cclite_transport` facade,提供 mock 与 Windows IPC 两个 backend 的统一 receive/complete 入口。 +- 已补充 `cclite_ipc_adapter` smoke,覆盖命名、布局、open、timeout、wait/signal 和 event 不 reset。 +- 已补充 `cclite_ipc_worker_loop` smoke,使用本地测试 helper 模拟 CCLite,并通过 transport facade 完成 INIT 到 STOP 和 100 step 空耦合。 +- 当前尚未把 `femlite.exe --cclite-worker` 主循环切换为真实跨进程 Windows IPC 运行模式;现阶段先固化 adapter 和 transport 小闭环。 + +### 4.4 阶段 D:真实 surface/facet 导出 + +目标是让 CCLite 能读取稳定 FEM 边界状态。 + +最小字段: + +- `facet_id` +- `surface_id` +- `elem_id` +- `face_id` +- `node_count` +- `node_ids` +- 当前节点坐标 +- 当前节点速度 +- facet normal +- facet area +- material id +- section id +- valid flags + +壳单元扩展字段: + +- shell thickness +- mass per area + +实体面 `shell_thickness=0`。退化 facet 应设置 invalid flag,不应静默导出为有效接触面。 + +当前实现状态: + +- 已新增 coupling surface 扩展导出接口,保留旧 ABI,同时输出 `surface_id`、`material_id`、`section_id`、`valid_flags`、`shell_thickness` 和 `mass_per_area`。 +- 未指定 surface 时继续导出全部边界 facet,`surface_id=0` 并写 warning。 +- 已支持按已有 `*SURFACE, NAME=..., TYPE=ELEMENT` 名称过滤导出。 +- 已补充单三角形、单四边形、退化三角形、退化四边形、实体面厚度为 0、壳厚度、面密度和 normal 单位化 smoke。 +- 当前 surface_id 使用内部 surface 序号;若 CCLite 需要稳定跨文件 ID,后续可增加显式映射表。 + +### 4.5 阶段 E:外部载荷导入与诊断 + +目标是支持 CCLite 写回的节点力,并安全叠加到 FemLite 求解流程。 + +建议先只做: + +- `NODAL_FORCE` +- `node_id` +- `force[3]` +- `source_id` +- `load_mode` +- `valid_flags` + +FemLite 内部应有独立外部耦合载荷缓存。每个 CCLite step 开始前清零,导入后再叠加到当前求解步外力或 RHS。不要让 CCLite 直接写 FemLite 全局力数组。 + +诊断至少包括: + +- 本步接收总力。 +- 本步接收总力矩。 +- 每个 surface 的总载荷。 +- 被拒绝的 load record 数量。 +- 非有限载荷数量。 +- 节点 id 未匹配数量。 + +当前实现状态: + +- 已新增独立 coupling external load 缓存,并通过 `fem_clear_coupling_loads` 统一清理本步载荷和诊断。 +- coupling 节点力已叠加到显式动力外力路径、静力线性 RHS 和隐式/非线性外力装配路径。 +- 已校验 `source_id`、`load_mode`、`valid_flags`、非有限载荷和未知节点,拒绝记录会进入诊断计数。 +- 已提供本步总力、总力矩、按 `source_id` 汇总的诊断查询。 +- 已输出 `femlite_cclite_load_report.csv` 与 `femlite_cclite_surface_report.csv`,worker mock 在写 VTU/PVD 时同步写出两份报告。 +- 已补充显式单步响应、静力 RHS 响应、force balance CSV 和 worker 输出一致性回归测试。 +- 当前仍是基础节点力闭环,不包含真实 SPH-FEM penalty contact、FSI 或 CCLite 强耦合 rollback。 + +## 5. 输入与 surface 语义建议 + +FemLite 自身输入仍由 FemLite 解析。建议优先复用或加强已有 `*SURFACE`/接触 surface 解析能力,而不是新增另一套不兼容语法。 + +建议目标: + +```text +*SURFACE, NAME=CCL_SURF_PLATE, TYPE=ELEMENT +... +``` + +可选增加辅助关键词: + +```text +*FEMLITE CCLITE +WORKER=YES +EXPORT_SURFACE=CCL_SURF_PLATE +EXPORT_VELOCITY=YES +EXPORT_THICKNESS=YES +ACCEPT_EXTERNAL_LOAD=YES +``` + +该关键词只作为 FemLite 侧辅助配置,不替代 CCLite 联合输入。 + +## 6. 测试建议 + +阶段 A 测试: + +- C ABI coupling surface count。 +- 三角形 facet 导出。 +- 四边形 facet 导出。 +- 当前坐标和速度导出。 +- 空载荷导入。 +- 节点力导入。 +- 容量不足错误。 +- 非有限载荷拒绝。 +- 节点 id 未匹配错误。 + +阶段 B/C 测试: + +- worker 启动。 +- `READY` 状态。 +- command seq/ack。 +- wait/signal,不 reset event。 +- 100 step 空耦合。 +- `STOP` 正常退出。 +- 超时和错误码。 + +阶段 D/E 联合测试: + +- 单三角形 facet。 +- 单四边形 facet。 +- 壳厚度导出。 +- 单粒子撞面。 +- 多粒子压面。 +- force balance CSV 对账。 +- VTU/PVD 场输出和 worker 诊断文件一致性检查。 + +可复用算例: + +- `contact_rigid_plane_indentation` +- `shell_rigid_plane_contact` +- `shell_solid_contact` +- `clamped_square_plate_pressure_pulse` +- `rigid_sphere_impact_plate` + +## 7. 建议版本切分 + +| 建议版本 | FemLite 目标 | +|---|---| +| 0.6.x | 中立 coupling API wrapper、文档、C ABI smoke、外部载荷诊断框架 | +| 0.7.0 | worker/service mock transport、命令状态机、空耦合闭环 | +| 0.7.1 | Windows shared memory + named event adapter、CCLite 空耦合联调 | +| 0.7.2 | surface/facet 完整导出、节点力导入、force/moment 诊断 | +| 0.7.x 后续 | shell thickness、mass per area、surface id 稳定化、pressure/facet load | +| 0.8.x | tie/release、rollback/强耦合预留、长时间稳定性 | +| 0.9.x | FSI 工程简化算例、性能基线和异常处理补强 | + +## 8. 不建议立即做的事项 + +- 不建议把 CCLite 接触算法移入 FemLite。 +- 不建议 FemLite 直接链接 CCLite 核心库。 +- 不建议一开始就做真实 SPH-FEM penalty contact 验收。 +- 不建议 worker IPC、真实载荷导入、surface 名称稳定化和强耦合 rollback 一次性开发。 +- 不建议删除旧 `sph` 命名接口,应先提供中立 wrapper 并保留兼容期。 + +## 9. 推荐下一步 + +最稳的小闭环是先做阶段 A: + +1. 盘点现有 boundary facet 与 sph load API。 +2. 增加中立 coupling wrapper。 +3. 增加 coupling load 诊断。 +4. 补 C ABI smoke。 +5. 更新接口文档。 + +完成后再进入 worker/service,而不是直接上 Windows IPC。 diff --git a/docs/13_cclite_coupling_api_inventory.md b/docs/13_cclite_coupling_api_inventory.md new file mode 100644 index 0000000..6c1d2e5 --- /dev/null +++ b/docs/13_cclite_coupling_api_inventory.md @@ -0,0 +1,79 @@ +# 13 CCLite Coupling API 盘点 + +文档状态:阶段 A 初始盘点 + +## 1. Boundary Facet 接口现状 + +已有 Fortran API: + +- `fem_get_boundary_facet_count(num_facets, max_face_nodes, ierr)` +- `fem_get_boundary_facets(nfacet_in, max_face_nodes_in, facet_id, elem_id, face_id, facet_nnode, facet_conn, x, vnod, normal, area, ierr)` + +已有 C ABI: + +- `femlite_get_boundary_facet_count` +- `femlite_get_boundary_facets` + +已有测试: + +- `test_c_api` 覆盖 C3D4 边界 facet 数量和基础字段。 +- `test_facet_nodal_c_api` 覆盖 facet 查询后施加 facet nodal load。 +- `test_smoke` 覆盖 Fortran API 的实体和壳边界 facet 查询。 + +当前缺口: + +- 接口命名偏内部 boundary,不是 CCLite 中立 coupling surface。 +- 当前不携带 `surface_id`、`material_id`、`section_id` 和 `valid_flags`。 +- surface 名称过滤留到后续阶段。 +- 壳厚度和 mass per area 留到后续阶段。 + +## 2. Facet/Load 接口现状 + +已有 Fortran API: + +- `fem_clear_sph_forces(ierr)` +- `fem_apply_sph_nodal_force(node_id, force, ierr)` +- `fem_apply_sph_nodal_forces(num_force_nodes, node_ids, force, ierr)` +- `fem_get_sph_force_summary(total_force, ierr)` +- `fem_apply_sph_facet_pressure` +- `fem_apply_sph_facet_tractions` +- `fem_apply_sph_facet_nodal_loads` + +已有 C ABI: + +- `femlite_clear_sph_forces` +- `femlite_apply_sph_nodal_forces` +- `femlite_get_sph_force_summary` +- `femlite_apply_sph_facet_pressure` +- `femlite_apply_sph_facet_tractions` +- `femlite_apply_sph_facet_nodal_loads` + +已有测试: + +- `test_c_api` 覆盖 nodal force 后显式推进。 +- `test_facet_nodal_c_api` 覆盖 facet nodal load 的总力和错误边界。 +- `test_particle_fem_coupling` 覆盖 Fortran 侧 facet load 与 particle coupling 小闭环。 + +当前缺口: + +- `sph` 命名不适合作为 CCLite 长期公共接口。 +- 当前节点力接口不记录 source id、load mode 和 valid flags。 +- 当前总力查询只有 force,没有 moment、接受计数和拒绝计数。 +- 非有限载荷、节点未匹配、容量不足等诊断尚未统一为 coupling load 诊断。 + +## 3. 阶段 A 处理策略 + +- 新增中立 coupling wrapper,先复用现有 boundary facet 和 sph load 实现。 +- 保留旧 `sph` 命名接口,避免破坏历史调用。 +- 先提供全部边界 facet 导出,不在阶段 A 做 surface 名称过滤。 +- 先提供节点力导入和基础诊断,不在阶段 A 做 facet pressure 的中立包装。 +- C ABI smoke 覆盖新 wrapper,旧测试继续保留。 + +## 4. 当前完成状态 + +- 中立 coupling surface/facet wrapper 已补齐,旧 `boundary` 和 `sph` 命名接口继续保留兼容。 +- 扩展 facet 导出已包含 `surface_id`、`material_id`、`section_id`、`valid_flags`、`shell_thickness` 和 `mass_per_area`。 +- 节点力导入已记录 `source_id`、`load_mode` 和 `valid_flags`,并维护独立诊断缓存。 +- 诊断已覆盖本步总力、总力矩、接受记录数、非有限载荷、未知节点、容量/metadata 错误和按 `source_id` 汇总。 +- C API 已提供 `femlite_get_coupling_source_load_diagnostics` 与 `femlite_write_coupling_reports`。 +- worker mock 已在 VTU/PVD 输出目录同步生成 `femlite_cclite_load_report.csv` 和 `femlite_cclite_surface_report.csv`。 diff --git a/docs/README.md b/docs/README.md index b271a74..78ae8e5 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ # FemLite 文档索引 -文档状态:0.6.3 版整理稿 +文档状态:0.6.4 版整理稿 ## 核心文档 diff --git a/examples/Kirsch/README.md b/examples/Kirsch/README.md index 21f4bb1..11ed792 100644 --- a/examples/Kirsch/README.md +++ b/examples/Kirsch/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..3eeff56 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,14 @@ +# 示例说明 + +本目录保存 FemLite 的小型验证算例和 smoke 算例。算例默认面向 FemLite 独立求解器,通常可以通过根目录的 `examples/run_example_case.bat` 或直接调用 `femlite` 运行。 + +## CCLite coupling 当前边界 + +0.6.x 阶段的 CCLite coupling 支持是基础节点力闭环,不是完整 SPH-FEM 接触或 FSI 联调: + +- FemLite 可导出 coupling surface/facet 的几何、速度、法向、面积、材料/截面、壳厚度和有效性标记。 +- FemLite 可接收 `node_id + force[3] + source_id + load_mode + valid_flags` 格式的节点力,并叠加到显式动力外力和静力/隐式 RHS。 +- worker mock 会在输出目录写出 `results.pvd`、VTU 文件、`femlite_cclite_load_report.csv` 和 `femlite_cclite_surface_report.csv`。 +- 真实 SPH-FEM penalty contact、FSI、强耦合 rollback 和 CCLite 侧调度算法不属于当前示例交付范围。 + +建议新增 coupling 示例时优先保持小规模、可复现、可自动对账;复杂工程级联合算例应放到后续 CCLite 联调版本。 diff --git a/examples/ScordelisLo/README.md b/examples/ScordelisLo/README.md index 514b55d..1be988d 100644 --- a/examples/ScordelisLo/README.md +++ b/examples/ScordelisLo/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/clamped_square_plate_pressure_pulse/README.md b/examples/clamped_square_plate_pressure_pulse/README.md index 9619587..4dd6e16 100644 --- a/examples/clamped_square_plate_pressure_pulse/README.md +++ b/examples/clamped_square_plate_pressure_pulse/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/clamped_square_plate_pressure_pulse/check_pressure_pulse_results.py b/examples/clamped_square_plate_pressure_pulse/check_pressure_pulse_results.py index 77ef63d..a39fed0 100644 --- a/examples/clamped_square_plate_pressure_pulse/check_pressure_pulse_results.py +++ b/examples/clamped_square_plate_pressure_pulse/check_pressure_pulse_results.py @@ -75,8 +75,8 @@ def check_case(inp_path: Path, result_dir: Path) -> Tuple[int, float, float, flo history_file = result_dir / "history.csv" if not history_file.exists(): raise RuntimeError(f"missing {history_file}") - if not list(result_dir.glob("result_*.vtk")): - raise RuntimeError(f"missing VTK output in {result_dir}") + if not list(result_dir.glob("result_*.vtu")): + raise RuntimeError(f"missing VTU output in {result_dir}") if not list(result_dir.glob("elements_*.csv")): raise RuntimeError(f"missing element CSV output in {result_dir}") diff --git a/examples/contact_block_sliding/README.md b/examples/contact_block_sliding/README.md index b99d9a8..df23655 100644 --- a/examples/contact_block_sliding/README.md +++ b/examples/contact_block_sliding/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/contact_block_sliding/check_contact_results.py b/examples/contact_block_sliding/check_contact_results.py index fa01070..801f8d8 100644 --- a/examples/contact_block_sliding/check_contact_results.py +++ b/examples/contact_block_sliding/check_contact_results.py @@ -214,8 +214,8 @@ def main() -> int: if min_fric_work < -1.0e-12 or max_fric_work <= 0.0: print("friction work is not positive dissipative") return 1 - if not glob.glob(os.path.join(result_dir, "result_*.vtk")): - print("no VTK output found") + if not glob.glob(os.path.join(result_dir, "result_*.vtu")): + print("no VTU output found") return 1 print( diff --git a/examples/contact_block_sliding/run_case.bat b/examples/contact_block_sliding/run_case.bat index 8d874fe..e6cd337 100644 --- a/examples/contact_block_sliding/run_case.bat +++ b/examples/contact_block_sliding/run_case.bat @@ -23,7 +23,8 @@ del /Q "%OUT_DIR%\nodes_*.csv" 2>nul del /Q "%OUT_DIR%\elements_*.csv" 2>nul del /Q "%OUT_DIR%\reactions_*.csv" 2>nul del /Q "%OUT_DIR%\contact_*.csv" 2>nul -del /Q "%OUT_DIR%\result_*.vtk" 2>nul +del /Q "%OUT_DIR%\result_*.vtu" 2>nul +del /Q "%OUT_DIR%\results.pvd" 2>nul del /Q "%OUT_DIR%\sliding_profile.csv" 2>nul echo Running FemLite... diff --git a/examples/contact_rigid_plane_indentation/README.md b/examples/contact_rigid_plane_indentation/README.md index f0eb535..cb592ba 100644 --- a/examples/contact_rigid_plane_indentation/README.md +++ b/examples/contact_rigid_plane_indentation/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/contact_rigid_plane_indentation/check_contact_results.py b/examples/contact_rigid_plane_indentation/check_contact_results.py index 72e2caa..d1b4475 100644 --- a/examples/contact_rigid_plane_indentation/check_contact_results.py +++ b/examples/contact_rigid_plane_indentation/check_contact_results.py @@ -37,9 +37,9 @@ def main() -> int: print("active-set status was not recorded") return 1 - vtk_files = glob.glob(os.path.join(result_dir, "result_*.vtk")) + vtk_files = glob.glob(os.path.join(result_dir, "result_*.vtu")) if not vtk_files: - print("no VTK output found") + print("no VTU output found") return 1 print( diff --git a/examples/contact_rigid_plane_indentation/run_case.bat b/examples/contact_rigid_plane_indentation/run_case.bat index f2e2cce..9e3ea97 100644 --- a/examples/contact_rigid_plane_indentation/run_case.bat +++ b/examples/contact_rigid_plane_indentation/run_case.bat @@ -23,7 +23,8 @@ del /Q "%OUT_DIR%\nodes_*.csv" 2>nul del /Q "%OUT_DIR%\elements_*.csv" 2>nul del /Q "%OUT_DIR%\reactions_*.csv" 2>nul del /Q "%OUT_DIR%\contact_*.csv" 2>nul -del /Q "%OUT_DIR%\result_*.vtk" 2>nul +del /Q "%OUT_DIR%\result_*.vtu" 2>nul +del /Q "%OUT_DIR%\results.pvd" 2>nul echo Running FemLite... "%EXE%" "%INP%" --output-dir "%OUT_DIR%" --output-interval 10 --run diff --git a/examples/contact_two_blocks_compression/README.md b/examples/contact_two_blocks_compression/README.md index 2781343..83d87b6 100644 --- a/examples/contact_two_blocks_compression/README.md +++ b/examples/contact_two_blocks_compression/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/contact_two_blocks_compression/check_contact_results.py b/examples/contact_two_blocks_compression/check_contact_results.py index 9df4e31..8055521 100644 --- a/examples/contact_two_blocks_compression/check_contact_results.py +++ b/examples/contact_two_blocks_compression/check_contact_results.py @@ -181,8 +181,8 @@ def main() -> int: if max_multiplier_residual > 5.0e-3: print(f"multiplier residual out of expected range: {max_multiplier_residual:.6e}") return 1 - if not glob.glob(os.path.join(result_dir, "result_*.vtk")): - print("no VTK output found") + if not glob.glob(os.path.join(result_dir, "result_*.vtu")): + print("no VTU output found") return 1 print( diff --git a/examples/contact_two_blocks_compression/run_case.bat b/examples/contact_two_blocks_compression/run_case.bat index 74f2d63..1f700ee 100644 --- a/examples/contact_two_blocks_compression/run_case.bat +++ b/examples/contact_two_blocks_compression/run_case.bat @@ -23,7 +23,8 @@ del /Q "%OUT_DIR%\nodes_*.csv" 2>nul del /Q "%OUT_DIR%\elements_*.csv" 2>nul del /Q "%OUT_DIR%\reactions_*.csv" 2>nul del /Q "%OUT_DIR%\contact_*.csv" 2>nul -del /Q "%OUT_DIR%\result_*.vtk" 2>nul +del /Q "%OUT_DIR%\result_*.vtu" 2>nul +del /Q "%OUT_DIR%\results.pvd" 2>nul del /Q "%OUT_DIR%\pressure_profile.csv" 2>nul echo Running FemLite... diff --git a/examples/elliptic_membrane/README.md b/examples/elliptic_membrane/README.md index c73e0b0..b2574f5 100644 --- a/examples/elliptic_membrane/README.md +++ b/examples/elliptic_membrane/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/falling_objects/README.md b/examples/falling_objects/README.md index 40a4a1f..305ae1e 100644 --- a/examples/falling_objects/README.md +++ b/examples/falling_objects/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/membrane_uniaxial_stretching/README.md b/examples/membrane_uniaxial_stretching/README.md index 0724604..e1a272c 100644 --- a/examples/membrane_uniaxial_stretching/README.md +++ b/examples/membrane_uniaxial_stretching/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/node_analytical_plane_contact/README.md b/examples/node_analytical_plane_contact/README.md index 2cc0afd..7e6e111 100644 --- a/examples/node_analytical_plane_contact/README.md +++ b/examples/node_analytical_plane_contact/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/rigid_sphere_impact_plate/README.md b/examples/rigid_sphere_impact_plate/README.md index eb63d95..6fecbde 100644 --- a/examples/rigid_sphere_impact_plate/README.md +++ b/examples/rigid_sphere_impact_plate/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/rigid_sphere_impact_plate/check_rigid_sphere_impact_results.py b/examples/rigid_sphere_impact_plate/check_rigid_sphere_impact_results.py index aca9d96..fde2fd0 100644 --- a/examples/rigid_sphere_impact_plate/check_rigid_sphere_impact_results.py +++ b/examples/rigid_sphere_impact_plate/check_rigid_sphere_impact_results.py @@ -74,8 +74,8 @@ def check_case(inp_path: Path, result_dir: Path) -> Tuple[int, float, int, float history_file = result_dir / "history.csv" if not history_file.exists(): raise RuntimeError(f"missing {history_file}") - if not list(result_dir.glob("result_*.vtk")): - raise RuntimeError(f"missing VTK output in {result_dir}") + if not list(result_dir.glob("result_*.vtu")): + raise RuntimeError(f"missing VTU output in {result_dir}") if not list(result_dir.glob("contact_*.csv")): raise RuntimeError(f"missing contact CSV output in {result_dir}") diff --git a/examples/run_example_case.bat b/examples/run_example_case.bat index de3af26..2b31e8c 100644 --- a/examples/run_example_case.bat +++ b/examples/run_example_case.bat @@ -33,7 +33,8 @@ del /Q "%OUT_DIR%\nodes_*.csv" 2>nul del /Q "%OUT_DIR%\elements_*.csv" 2>nul del /Q "%OUT_DIR%\reactions_*.csv" 2>nul del /Q "%OUT_DIR%\contact_*.csv" 2>nul -del /Q "%OUT_DIR%\result_*.vtk" 2>nul +del /Q "%OUT_DIR%\result_*.vtu" 2>nul +del /Q "%OUT_DIR%\results.pvd" 2>nul del /Q "%OUT_DIR%\continuation_path.csv" 2>nul echo Running FemLite... diff --git a/examples/scordelis_lo_roof/README.md b/examples/scordelis_lo_roof/README.md index a7c97d7..ec6b4e6 100644 --- a/examples/scordelis_lo_roof/README.md +++ b/examples/scordelis_lo_roof/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/shell_patch_test/README.md b/examples/shell_patch_test/README.md index 3f3c2f8..6e5f847 100644 --- a/examples/shell_patch_test/README.md +++ b/examples/shell_patch_test/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/shell_rigid_plane_contact/README.md b/examples/shell_rigid_plane_contact/README.md index 0ce59f0..bd8c863 100644 --- a/examples/shell_rigid_plane_contact/README.md +++ b/examples/shell_rigid_plane_contact/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/shell_rigid_plane_contact/check_contact_results.py b/examples/shell_rigid_plane_contact/check_contact_results.py index 136d185..aad7870 100644 --- a/examples/shell_rigid_plane_contact/check_contact_results.py +++ b/examples/shell_rigid_plane_contact/check_contact_results.py @@ -27,8 +27,8 @@ def main() -> int: if max_lambda <= 0.0: print("augmented contact multiplier did not grow") return 1 - if not glob.glob(os.path.join(result_dir, "result_*.vtk")): - print("no VTK output found") + if not glob.glob(os.path.join(result_dir, "result_*.vtu")): + print("no VTU output found") return 1 print( diff --git a/examples/shell_rigid_plane_contact/run_case.bat b/examples/shell_rigid_plane_contact/run_case.bat index 46328b5..c6f5641 100644 --- a/examples/shell_rigid_plane_contact/run_case.bat +++ b/examples/shell_rigid_plane_contact/run_case.bat @@ -23,7 +23,8 @@ del /Q "%OUT_DIR%\nodes_*.csv" 2>nul del /Q "%OUT_DIR%\elements_*.csv" 2>nul del /Q "%OUT_DIR%\reactions_*.csv" 2>nul del /Q "%OUT_DIR%\contact_*.csv" 2>nul -del /Q "%OUT_DIR%\result_*.vtk" 2>nul +del /Q "%OUT_DIR%\result_*.vtu" 2>nul +del /Q "%OUT_DIR%\results.pvd" 2>nul echo Running FemLite... "%EXE%" "%INP%" --output-dir "%OUT_DIR%" --output-interval 10 --run diff --git a/examples/shell_shell_contact/README.md b/examples/shell_shell_contact/README.md index 8b4e628..8f4ee0f 100644 --- a/examples/shell_shell_contact/README.md +++ b/examples/shell_shell_contact/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/shell_shell_contact/check_contact_results.py b/examples/shell_shell_contact/check_contact_results.py index e8d6816..d21742b 100644 --- a/examples/shell_shell_contact/check_contact_results.py +++ b/examples/shell_shell_contact/check_contact_results.py @@ -131,8 +131,8 @@ def main() -> int: if max_aug_factor <= 0.0 or max_lambda <= 0.0: print("shell-shell augmented multiplier did not grow") return 1 - if not glob.glob(os.path.join(result_dir, "result_*.vtk")): - print("no VTK output found") + if not glob.glob(os.path.join(result_dir, "result_*.vtu")): + print("no VTU output found") return 1 print( diff --git a/examples/shell_shell_contact/run_case.bat b/examples/shell_shell_contact/run_case.bat index 6846122..27cca4a 100644 --- a/examples/shell_shell_contact/run_case.bat +++ b/examples/shell_shell_contact/run_case.bat @@ -23,7 +23,8 @@ del /Q "%OUT_DIR%\nodes_*.csv" 2>nul del /Q "%OUT_DIR%\elements_*.csv" 2>nul del /Q "%OUT_DIR%\reactions_*.csv" 2>nul del /Q "%OUT_DIR%\contact_*.csv" 2>nul -del /Q "%OUT_DIR%\result_*.vtk" 2>nul +del /Q "%OUT_DIR%\result_*.vtu" 2>nul +del /Q "%OUT_DIR%\results.pvd" 2>nul del /Q "%OUT_DIR%\shell_shell_contact_profile.csv" 2>nul echo Running FemLite... diff --git a/examples/shell_solid_connection/README.md b/examples/shell_solid_connection/README.md index 348f8b2..f4f5299 100644 --- a/examples/shell_solid_connection/README.md +++ b/examples/shell_solid_connection/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/shell_solid_contact/README.md b/examples/shell_solid_contact/README.md index 7281efb..be13280 100644 --- a/examples/shell_solid_contact/README.md +++ b/examples/shell_solid_contact/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/shell_solid_contact/check_contact_results.py b/examples/shell_solid_contact/check_contact_results.py index c58ec2a..3863d73 100644 --- a/examples/shell_solid_contact/check_contact_results.py +++ b/examples/shell_solid_contact/check_contact_results.py @@ -131,8 +131,8 @@ def main() -> int: if max_aug_factor <= 0.0 or max_lambda <= 0.0: print("shell-solid augmented multiplier did not grow") return 1 - if not glob.glob(os.path.join(result_dir, "result_*.vtk")): - print("no VTK output found") + if not glob.glob(os.path.join(result_dir, "result_*.vtu")): + print("no VTU output found") return 1 print( diff --git a/examples/shell_solid_contact/run_case.bat b/examples/shell_solid_contact/run_case.bat index 7be0560..dd18548 100644 --- a/examples/shell_solid_contact/run_case.bat +++ b/examples/shell_solid_contact/run_case.bat @@ -23,7 +23,8 @@ del /Q "%OUT_DIR%\nodes_*.csv" 2>nul del /Q "%OUT_DIR%\elements_*.csv" 2>nul del /Q "%OUT_DIR%\reactions_*.csv" 2>nul del /Q "%OUT_DIR%\contact_*.csv" 2>nul -del /Q "%OUT_DIR%\result_*.vtk" 2>nul +del /Q "%OUT_DIR%\result_*.vtu" 2>nul +del /Q "%OUT_DIR%\results.pvd" 2>nul del /Q "%OUT_DIR%\shell_solid_contact_profile.csv" 2>nul echo Running FemLite... diff --git a/examples/sliding_wedge/README.md b/examples/sliding_wedge/README.md index 376bf39..97df6e9 100644 --- a/examples/sliding_wedge/README.md +++ b/examples/sliding_wedge/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/tension2D/README.md b/examples/tension2D/README.md index b7efdbb..a792300 100644 --- a/examples/tension2D/README.md +++ b/examples/tension2D/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/thick_plate/README.md b/examples/thick_plate/README.md index 84571a8..20e2492 100644 --- a/examples/thick_plate/README.md +++ b/examples/thick_plate/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/examples/thick_plate/check_thick_plate_results.py b/examples/thick_plate/check_thick_plate_results.py index 54469cf..04a62e4 100644 --- a/examples/thick_plate/check_thick_plate_results.py +++ b/examples/thick_plate/check_thick_plate_results.py @@ -24,7 +24,7 @@ def parse_marker(text: str, name: str) -> int: def clean_output(out_dir: Path) -> None: out_dir.mkdir(parents=True, exist_ok=True) - for pattern in ("history.csv", "nodes_*.csv", "elements_*.csv", "reactions_*.csv", "result_*.vtk"): + for pattern in ("history.csv", "nodes_*.csv", "elements_*.csv", "reactions_*.csv", "result_*.vtu"): for path in out_dir.glob(pattern): path.unlink() contact_dir = out_dir / "contact" diff --git a/examples/uniaxialTension3D/README.md b/examples/uniaxialTension3D/README.md index 70a088b..8816570 100644 --- a/examples/uniaxialTension3D/README.md +++ b/examples/uniaxialTension3D/README.md @@ -23,7 +23,7 @@ System.Object[] ## 结果文件 - 结果通常写入 `results`、`results/auto_run` 或命令指定的输出目录。 -- 不要提交全量 CSV、VTK、restart、临时网格或缓存文件。 +- 不要提交全量 CSV、VTU/PVD、restart、临时网格或缓存文件。 - 如需保留参考结果,只保留小型 summary、profile 或 checker 需要的抽样数据。 ## 边界 diff --git a/include/femlite_c_api.h b/include/femlite_c_api.h index a17fbf7..01b151c 100644 --- a/include/femlite_c_api.h +++ b/include/femlite_c_api.h @@ -138,6 +138,76 @@ int femlite_apply_sph_nodal_forces(int num_force_nodes, const int *node_ids, const double *forces); int femlite_get_sph_force_summary(double *total_force); +int femlite_clear_coupling_loads(void); +int femlite_get_coupling_surface_count(int *num_facets, int *max_face_nodes); +int femlite_get_coupling_surface_count_for_name(const char *surface_name, + int *num_facets, + int *max_face_nodes); +int femlite_get_coupling_surface_facets(int num_facets, + int max_face_nodes, + int *facet_id, + int *elem_id, + int *face_id, + int *facet_nnode, + int *facet_conn, + double *x, + double *vnod, + double *normal, + double *area); +int femlite_get_coupling_surface_facets_ex(int num_facets, + int max_face_nodes, + int *facet_id, + int *surface_id, + int *elem_id, + int *face_id, + int *facet_nnode, + int *facet_conn, + double *x, + double *vnod, + double *normal, + double *area, + int *material_id, + int *section_id, + int *valid_flags, + double *shell_thickness, + double *mass_per_area); +int femlite_get_coupling_surface_facets_ex_for_name( + const char *surface_name, + int num_facets, + int max_face_nodes, + int *facet_id, + int *surface_id, + int *elem_id, + int *face_id, + int *facet_nnode, + int *facet_conn, + double *x, + double *vnod, + double *normal, + double *area, + int *material_id, + int *section_id, + int *valid_flags, + double *shell_thickness, + double *mass_per_area); +int femlite_apply_coupling_nodal_forces(int num_loads, + const int *node_ids, + const double *forces, + const int *source_ids, + const int *load_mode, + const int *valid_flags); +int femlite_get_coupling_load_diagnostics(double *total_force, + double *total_moment, + int *accepted_count, + int *rejected_nonfinite, + int *rejected_unknown_node, + int *rejected_capacity); +int femlite_get_coupling_source_load_diagnostics(int source_id, + double *total_force, + double *total_moment, + int *accepted_count); +int femlite_write_coupling_reports(const char *load_report, + const char *surface_report); int femlite_get_boundary_facet_count(int *num_facets, int *max_face_nodes); int femlite_get_boundary_facets(int num_facets, int max_face_nodes, diff --git a/include/femlite_cclite_ipc.h b/include/femlite_cclite_ipc.h new file mode 100644 index 0000000..6b1f611 --- /dev/null +++ b/include/femlite_cclite_ipc.h @@ -0,0 +1,121 @@ +#ifndef FEMLITE_CCLITE_IPC_H +#define FEMLITE_CCLITE_IPC_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define FEMLITE_CCL_IPC_SUCCESS 0 +#define FEMLITE_CCL_IPC_ERR_INPUT 1000 +#define FEMLITE_CCL_IPC_ERR_UNSUPPORTED 1001 +#define FEMLITE_CCL_IPC_ERR_OPEN 1002 +#define FEMLITE_CCL_IPC_ERR_MAP 1003 +#define FEMLITE_CCL_IPC_ERR_TIMEOUT 1004 +#define FEMLITE_CCL_IPC_ERR_WAIT 1005 +#define FEMLITE_CCL_IPC_ERR_SIGNAL 1006 +#define FEMLITE_CCL_IPC_ERR_CAPACITY 1007 + +#define FEMLITE_CCL_IPC_VERSION 1 + +#define FEMLITE_CCL_CMD_INIT 1 +#define FEMLITE_CCL_CMD_READ_INPUT 2 +#define FEMLITE_CCL_CMD_BUILD_MODEL 3 +#define FEMLITE_CCL_CMD_EXPORT_IFACE 4 +#define FEMLITE_CCL_CMD_IMPORT_LOADS 5 +#define FEMLITE_CCL_CMD_ADVANCE 6 +#define FEMLITE_CCL_CMD_WRITE_OUTPUT 7 +#define FEMLITE_CCL_CMD_STOP 8 + +#define FEMLITE_CCL_STATE_CREATED 0 +#define FEMLITE_CCL_STATE_READY 1 +#define FEMLITE_CCL_STATE_RUNNING 2 +#define FEMLITE_CCL_STATE_DONE 3 +#define FEMLITE_CCL_STATE_ERROR -1 + +#define FEMLITE_CCL_BACKEND_MOCK 1 +#define FEMLITE_CCL_BACKEND_WINDOWS_IPC 2 + +typedef struct femlite_cclite_command_block { + int32_t version; + int32_t seq; + int32_t ack_seq; + int32_t command_id; + int32_t step_id; + int32_t substep_id; + int32_t iteration_id; + int32_t worker_state; + int32_t error_code; +} femlite_cclite_command_block; + +typedef struct femlite_cclite_map { + void *handle; + void *view; + size_t size; + int32_t last_native_error; +} femlite_cclite_map; + +typedef struct femlite_cclite_event { + void *handle; + int32_t last_native_error; +} femlite_cclite_event; + +typedef struct femlite_cclite_transport { + int32_t backend; + femlite_cclite_map command_map; + femlite_cclite_event command_event; + femlite_cclite_event done_event; + femlite_cclite_command_block mock_command; + femlite_cclite_command_block mock_done; + int32_t has_mock_command; + int32_t has_mock_done; +} femlite_cclite_transport; + +size_t femlite_cclite_command_block_size(void); +void femlite_cclite_init_command_block(femlite_cclite_command_block *block); + +int femlite_cclite_format_name(const char *prefix, const char *suffix, + char *out_name, size_t out_capacity); + +int femlite_cclite_open_shared_memory(const char *name, size_t min_size, + femlite_cclite_map *map); +int femlite_cclite_close_shared_memory(femlite_cclite_map *map); + +int femlite_cclite_open_event(const char *name, femlite_cclite_event *event); +int femlite_cclite_close_event(femlite_cclite_event *event); +int femlite_cclite_wait_event(femlite_cclite_event *event, + unsigned int timeout_ms); +int femlite_cclite_signal_event(femlite_cclite_event *event); + +int femlite_cclite_wait_command(femlite_cclite_event *command_event, + femlite_cclite_map *command_map, + unsigned int timeout_ms, + femlite_cclite_command_block *out_block); +int femlite_cclite_write_done(femlite_cclite_event *done_event, + femlite_cclite_map *command_map, + const femlite_cclite_command_block *block); + +int femlite_cclite_transport_init_mock(femlite_cclite_transport *transport); +int femlite_cclite_transport_open_windows(const char *prefix, + femlite_cclite_transport *transport); +int femlite_cclite_transport_close(femlite_cclite_transport *transport); +int femlite_cclite_transport_mock_push( + femlite_cclite_transport *transport, + const femlite_cclite_command_block *block); +int femlite_cclite_transport_mock_get_done( + femlite_cclite_transport *transport, + femlite_cclite_command_block *block); +int femlite_cclite_transport_receive(femlite_cclite_transport *transport, + unsigned int timeout_ms, + femlite_cclite_command_block *out_block); +int femlite_cclite_transport_complete( + femlite_cclite_transport *transport, + const femlite_cclite_command_block *block); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/app/fem_program.f90 b/src/app/fem_program.f90 index 33f17da..2677447 100644 --- a/src/app/fem_program.f90 +++ b/src/app/fem_program.f90 @@ -21,6 +21,7 @@ program femlite use fem_api use fem_buckling_column use fem_buckling_general + use fem_cclite_worker implicit none integer :: ierr @@ -46,6 +47,7 @@ program femlite integer :: output_interval_arg integer :: linear_iter_limit integer :: buckling_mode_count + integer :: worker_mock_steps integer :: ios real(rk) :: time real(rk) :: res_norm @@ -56,6 +58,10 @@ program femlite character(len=256) :: filename character(len=256) :: arg character(len=256) :: output_dir_arg + character(len=256) :: worker_case_arg + character(len=256) :: worker_ipc_arg + character(len=256) :: worker_log_arg + character(len=256) :: worker_output_arg logical :: do_run logical :: do_static_linear logical :: do_static_nonlinear @@ -85,6 +91,83 @@ program femlite if (argc .ge. 1) then call getarg(1, filename) + if (trim(filename) .eq. '--cclite-worker') then + worker_case_arg = ' ' + worker_ipc_arg = 'mock' + worker_log_arg = ' ' + worker_output_arg = ' ' + worker_mock_steps = 0 + iarg = 2 + do while (iarg .le. argc) + call getarg(iarg, arg) + if (trim(arg) .eq. '--case') then + if (iarg + 1 .gt. argc) then + write(*,'(A)') 'missing value after --case' + call fem_finalize(ierr) + call exit(2) + end if + iarg = iarg + 1 + call getarg(iarg, worker_case_arg) + else if (trim(arg) .eq. '--ipc-name') then + if (iarg + 1 .gt. argc) then + write(*,'(A)') 'missing value after --ipc-name' + call fem_finalize(ierr) + call exit(2) + end if + iarg = iarg + 1 + call getarg(iarg, worker_ipc_arg) + else if (trim(arg) .eq. '--log') then + if (iarg + 1 .gt. argc) then + write(*,'(A)') 'missing value after --log' + call fem_finalize(ierr) + call exit(2) + end if + iarg = iarg + 1 + call getarg(iarg, worker_log_arg) + else if (trim(arg) .eq. '--output-dir') then + if (iarg + 1 .gt. argc) then + write(*,'(A)') 'missing value after --output-dir' + call fem_finalize(ierr) + call exit(2) + end if + iarg = iarg + 1 + call getarg(iarg, worker_output_arg) + else if (trim(arg) .eq. '--mock-steps') then + if (iarg + 1 .gt. argc) then + write(*,'(A)') 'missing value after --mock-steps' + call fem_finalize(ierr) + call exit(2) + end if + iarg = iarg + 1 + call getarg(iarg, arg) + read(arg,*,iostat=ios) worker_mock_steps + if (ios .ne. 0 .or. worker_mock_steps .lt. 0) then + write(*,'(A,A)') 'invalid --mock-steps value: ', trim(arg) + call fem_finalize(ierr) + call exit(2) + end if + else + write(*,'(A,A)') 'unknown worker argument: ', trim(arg) + call fem_finalize(ierr) + call exit(2) + end if + iarg = iarg + 1 + end do + if (len_trim(worker_case_arg) .eq. 0) then + write(*,'(A)') 'missing required --case for --cclite-worker' + call fem_finalize(ierr) + call exit(2) + end if + call ccl_worker_run_mock(trim(worker_case_arg), trim(worker_ipc_arg), & + trim(worker_log_arg), trim(worker_output_arg), & + worker_mock_steps, ierr) + call exit_if_error(ierr) + write(*,'(A)') 'CCLite worker mock completed' + call fem_finalize(ierr) + call exit_if_error(ierr) + call exit(0) + end if + do_run = .false. do_static_linear = .false. do_static_nonlinear = .false. diff --git a/src/base/fem_api.f90 b/src/base/fem_api.f90 index a793ccc..f1261b9 100644 --- a/src/base/fem_api.f90 +++ b/src/base/fem_api.f90 @@ -30,6 +30,7 @@ contains call error_clear(ierr) call control_set_defaults() call contact_reset_search_stats() + call output_vtu_reset() call nl_lifecycle_reset() call log_open('femlite.log', log_ierr) if (log_ierr .ne. FEM_SUCCESS) then @@ -80,6 +81,7 @@ contains end if call cpl_finalize_facets() call output_csv_reset() + call output_vtu_reset() time_start = ZERO time_end = ZERO dt_user = ZERO @@ -216,7 +218,7 @@ contains call output_csv_write(step, time, ierr) if (ierr .ne. FEM_SUCCESS) return - call output_vtk_write(step, ierr) + call output_vtu_write(step, time, ierr) end subroutine fem_write_output subroutine fem_write_restart(filename, ierr) diff --git a/src/base/fem_api_coupling.f90 b/src/base/fem_api_coupling.f90 index c06104f..ef7cbb0 100644 --- a/src/base/fem_api_coupling.f90 +++ b/src/base/fem_api_coupling.f90 @@ -1,6 +1,7 @@ module fem_api_coupling use fem_precision use fem_error + use fem_log, only: log_warning use fem_database use fem_coupling use fem_contact @@ -34,12 +35,52 @@ module fem_api_coupling public :: fem_get_contact_cache_stats public :: fem_get_boundary_facet_count public :: fem_get_boundary_facets + public :: fem_get_coupling_surface_count + public :: fem_get_cpl_surf_count_name + public :: fem_get_coupling_surface_facets + public :: fem_get_cpl_surf_facets_ex + public :: fem_get_cpl_surf_ex_name + public :: fem_clear_coupling_loads + public :: fem_apply_coupling_nodal_forces + public :: fem_get_coupling_load_diag + public :: fem_get_cpl_source_load_diag + public :: fem_write_cpl_reports public :: fem_get_surface_node_count public :: fem_get_surface_nodes public :: fem_apply_sph_facet_pressure public :: fem_apply_sph_facet_tractions public :: fem_apply_sph_facet_nodal_loads + + real(rk) :: coupling_total_force(NDIM) = ZERO + real(rk) :: coupling_total_moment(NDIM) = ZERO + integer :: coupling_accepted_count = 0 + integer :: coupling_rejected_nonfinite = 0 + integer :: coupling_rejected_unknown_node = 0 + integer :: coupling_rejected_capacity = 0 + integer, parameter :: COUPLING_LOAD_CACHE_MAX = 100000 + integer :: coupling_cache_count = 0 + integer :: coupling_cache_node(COUPLING_LOAD_CACHE_MAX) = 0 + integer :: coupling_cache_source(COUPLING_LOAD_CACHE_MAX) = 0 + integer :: coupling_cache_mode(COUPLING_LOAD_CACHE_MAX) = 0 + real(rk) :: coupling_cache_force(NDIM,COUPLING_LOAD_CACHE_MAX) = ZERO + real(rk) :: coupling_cache_moment(NDIM,COUPLING_LOAD_CACHE_MAX) = ZERO + contains + subroutine reset_coupling_load_diagnostics() + coupling_total_force = ZERO + coupling_total_moment = ZERO + coupling_accepted_count = 0 + coupling_rejected_nonfinite = 0 + coupling_rejected_unknown_node = 0 + coupling_rejected_capacity = 0 + coupling_cache_count = 0 + coupling_cache_node = 0 + coupling_cache_source = 0 + coupling_cache_mode = 0 + coupling_cache_force = ZERO + coupling_cache_moment = ZERO + end subroutine reset_coupling_load_diagnostics + subroutine fem_clear_sph_forces(ierr) integer, intent(out) :: ierr if (.not. api_initialized) then @@ -54,6 +95,15 @@ contains ierr = FEM_SUCCESS end subroutine fem_clear_sph_forces + subroutine fem_clear_coupling_loads(ierr) + integer, intent(out) :: ierr + + call fem_clear_sph_forces(ierr) + if (ierr .ne. FEM_SUCCESS) return + call reset_coupling_load_diagnostics() + ierr = FEM_SUCCESS + end subroutine fem_clear_coupling_loads + subroutine fem_apply_sph_nodal_force(node_id, force, ierr) integer, intent(in) :: node_id real(rk), intent(in) :: force(NDIM) @@ -472,6 +522,409 @@ contains facet_conn, x, vnod, normal, area, ierr) end subroutine fem_get_boundary_facets + subroutine fem_get_coupling_surface_count(num_facets, max_face_nodes, ierr) + integer, intent(out) :: num_facets + integer, intent(out) :: max_face_nodes + integer, intent(out) :: ierr + + call fem_get_boundary_facet_count(num_facets, max_face_nodes, ierr) + end subroutine fem_get_coupling_surface_count + + subroutine fem_get_cpl_surf_count_name(surface, num_facets, & + max_face_nodes, ierr) + character(len=*), intent(in) :: surface + integer, intent(out) :: num_facets + integer, intent(out) :: max_face_nodes + integer, intent(out) :: ierr + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, & + 'fem_get_cpl_surf_count_name called before fem_initialize', ierr) + return + end if + + call cpl_get_facet_count_named(surface, num_facets, max_face_nodes, ierr) + end subroutine fem_get_cpl_surf_count_name + + subroutine fem_get_coupling_surface_facets(nfacet_in, max_face_nodes_in, & + facet_id, elem_id, face_id, & + facet_nnode, facet_conn, x, & + vnod, normal, area, ierr) + integer, intent(in) :: nfacet_in + integer, intent(in) :: max_face_nodes_in + integer, intent(out) :: facet_id(nfacet_in) + integer, intent(out) :: elem_id(nfacet_in) + integer, intent(out) :: face_id(nfacet_in) + integer, intent(out) :: facet_nnode(nfacet_in) + integer, intent(out) :: facet_conn(max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: x(NDIM,max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: vnod(NDIM,max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: normal(NDIM,nfacet_in) + real(rk), intent(out) :: area(nfacet_in) + integer, intent(out) :: ierr + + call fem_get_boundary_facets(nfacet_in, max_face_nodes_in, facet_id, & + elem_id, face_id, facet_nnode, facet_conn, & + x, vnod, normal, area, ierr) + end subroutine fem_get_coupling_surface_facets + + subroutine fem_get_cpl_surf_facets_ex(nfacet_in, max_face_nodes_in, & + facet_id, surface_id, elem_id, & + face_id, facet_nnode, facet_conn, & + x, vnod, normal, area, material_id, & + section_id, valid_flags, & + shell_thickness, mass_per_area, ierr) + integer, intent(in) :: nfacet_in + integer, intent(in) :: max_face_nodes_in + integer, intent(out) :: facet_id(nfacet_in) + integer, intent(out) :: surface_id(nfacet_in) + integer, intent(out) :: elem_id(nfacet_in) + integer, intent(out) :: face_id(nfacet_in) + integer, intent(out) :: facet_nnode(nfacet_in) + integer, intent(out) :: facet_conn(max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: x(NDIM,max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: vnod(NDIM,max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: normal(NDIM,nfacet_in) + real(rk), intent(out) :: area(nfacet_in) + integer, intent(out) :: material_id(nfacet_in) + integer, intent(out) :: section_id(nfacet_in) + integer, intent(out) :: valid_flags(nfacet_in) + real(rk), intent(out) :: shell_thickness(nfacet_in) + real(rk), intent(out) :: mass_per_area(nfacet_in) + integer, intent(out) :: ierr + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, & + 'fem_get_cpl_surf_facets_ex called before fem_initialize', ierr) + return + end if + + call log_warning('coupling surface export has no surface filter; surface_id=0') + call cpl_get_facets_ex(nfacet_in, max_face_nodes_in, facet_id, & + surface_id, elem_id, face_id, facet_nnode, & + facet_conn, x, vnod, normal, area, material_id, & + section_id, valid_flags, shell_thickness, & + mass_per_area, ierr) + end subroutine fem_get_cpl_surf_facets_ex + + subroutine fem_get_cpl_surf_ex_name(surface, nfacet_in, max_face_nodes_in, & + facet_id, surface_id, elem_id, & + face_id, facet_nnode, facet_conn, & + x, vnod, normal, area, material_id, & + section_id, valid_flags, & + shell_thickness, mass_per_area, ierr) + character(len=*), intent(in) :: surface + integer, intent(in) :: nfacet_in + integer, intent(in) :: max_face_nodes_in + integer, intent(out) :: facet_id(nfacet_in) + integer, intent(out) :: surface_id(nfacet_in) + integer, intent(out) :: elem_id(nfacet_in) + integer, intent(out) :: face_id(nfacet_in) + integer, intent(out) :: facet_nnode(nfacet_in) + integer, intent(out) :: facet_conn(max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: x(NDIM,max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: vnod(NDIM,max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: normal(NDIM,nfacet_in) + real(rk), intent(out) :: area(nfacet_in) + integer, intent(out) :: material_id(nfacet_in) + integer, intent(out) :: section_id(nfacet_in) + integer, intent(out) :: valid_flags(nfacet_in) + real(rk), intent(out) :: shell_thickness(nfacet_in) + real(rk), intent(out) :: mass_per_area(nfacet_in) + integer, intent(out) :: ierr + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, & + 'fem_get_cpl_surf_ex_name called before fem_initialize', ierr) + return + end if + + call cpl_get_facets_ex_named(surface, nfacet_in, max_face_nodes_in, & + facet_id, surface_id, elem_id, face_id, & + facet_nnode, facet_conn, x, vnod, normal, & + area, material_id, section_id, valid_flags, & + shell_thickness, mass_per_area, ierr) + end subroutine fem_get_cpl_surf_ex_name + + subroutine fem_apply_coupling_nodal_forces(num_loads, node_ids, forces, & + source_ids, load_mode, & + valid_flags, ierr) + integer, intent(in) :: num_loads + integer, intent(in) :: node_ids(num_loads) + real(rk), intent(in) :: forces(NDIM,num_loads) + integer, intent(in) :: source_ids(num_loads) + integer, intent(in) :: load_mode(num_loads) + integer, intent(in) :: valid_flags(num_loads) + integer, intent(out) :: ierr + integer :: item + integer :: idir + integer :: node + integer :: dof + integer :: nonfinite_count + integer :: unknown_count + integer :: invalid_meta_count + integer :: valid_count + integer :: slot + real(rk) :: xyz(NDIM) + real(rk) :: moment(NDIM) + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, & + 'fem_apply_coupling_nodal_forces called before fem_initialize', ierr) + return + end if + if (.not. allocated(fsph)) then + call error_set(FEM_ERR_INTERNAL, 'coupling force vector is not allocated', ierr) + return + end if + if (num_loads .lt. 0) then + coupling_rejected_capacity = coupling_rejected_capacity + 1 + call error_set(FEM_ERR_COUPLING, 'number of coupling loads is negative', ierr) + return + end if + + nonfinite_count = 0 + unknown_count = 0 + invalid_meta_count = 0 + valid_count = 0 + do item = 1, num_loads + if (valid_flags(item) .eq. 0) cycle + valid_count = valid_count + 1 + if (source_ids(item) .lt. 0 .or. load_mode(item) .lt. 0) then + invalid_meta_count = invalid_meta_count + 1 + end if + do idir = 1, NDIM + if (.not. finite_real(forces(idir,item))) then + nonfinite_count = nonfinite_count + 1 + end if + end do + if (database_find_node(node_ids(item)) .eq. 0) then + unknown_count = unknown_count + 1 + end if + end do + + if (nonfinite_count .gt. 0) then + coupling_rejected_nonfinite = coupling_rejected_nonfinite + nonfinite_count + call error_set(FEM_ERR_COUPLING, 'coupling nodal force is not finite', ierr) + return + end if + if (unknown_count .gt. 0) then + coupling_rejected_unknown_node = coupling_rejected_unknown_node + unknown_count + call error_set(FEM_ERR_COUPLING, 'coupling nodal force references unknown node', ierr) + return + end if + if (invalid_meta_count .gt. 0) then + coupling_rejected_capacity = coupling_rejected_capacity + invalid_meta_count + call error_set(FEM_ERR_COUPLING, 'coupling load metadata is invalid', ierr) + return + end if + if (coupling_cache_count + valid_count .gt. COUPLING_LOAD_CACHE_MAX) then + coupling_rejected_capacity = coupling_rejected_capacity + valid_count + call error_set(FEM_ERR_COUPLING, 'coupling load cache capacity exceeded', ierr) + return + end if + + do item = 1, num_loads + if (valid_flags(item) .eq. 0) cycle + node = database_find_node(node_ids(item)) + xyz = coord0(:,node) + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) xyz(idir) = xyz(idir) + u(dof) + end do + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) fsph(dof) = fsph(dof) + forces(idir,item) + end do + moment = cross3(xyz, forces(:,item)) + slot = coupling_cache_count + 1 + coupling_cache_count = slot + coupling_cache_node(slot) = node_ids(item) + coupling_cache_source(slot) = source_ids(item) + coupling_cache_mode(slot) = load_mode(item) + coupling_cache_force(:,slot) = forces(:,item) + coupling_cache_moment(:,slot) = moment + coupling_total_force = coupling_total_force + forces(:,item) + coupling_total_moment = coupling_total_moment + moment + coupling_accepted_count = coupling_accepted_count + 1 + end do + + ierr = FEM_SUCCESS + end subroutine fem_apply_coupling_nodal_forces + + subroutine fem_get_coupling_load_diag(total_force, total_moment, & + accepted_count, rejected_nonfinite, & + rejected_unknown_node, & + rejected_capacity, ierr) + real(rk), intent(out) :: total_force(NDIM) + real(rk), intent(out) :: total_moment(NDIM) + integer, intent(out) :: accepted_count + integer, intent(out) :: rejected_nonfinite + integer, intent(out) :: rejected_unknown_node + integer, intent(out) :: rejected_capacity + integer, intent(out) :: ierr + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, & + 'fem_get_coupling_load_diag called before fem_initialize', ierr) + return + end if + + total_force = coupling_total_force + total_moment = coupling_total_moment + accepted_count = coupling_accepted_count + rejected_nonfinite = coupling_rejected_nonfinite + rejected_unknown_node = coupling_rejected_unknown_node + rejected_capacity = coupling_rejected_capacity + ierr = FEM_SUCCESS + end subroutine fem_get_coupling_load_diag + + subroutine fem_get_cpl_source_load_diag(source_id, total_force, & + total_moment, accepted_count, ierr) + integer, intent(in) :: source_id + real(rk), intent(out) :: total_force(NDIM) + real(rk), intent(out) :: total_moment(NDIM) + integer, intent(out) :: accepted_count + integer, intent(out) :: ierr + integer :: item + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, & + 'fem_get_cpl_source_load_diag called before fem_initialize', ierr) + return + end if + if (source_id .lt. 0) then + call error_set(FEM_ERR_COUPLING, 'coupling source id is negative', ierr) + return + end if + + total_force = ZERO + total_moment = ZERO + accepted_count = 0 + do item = 1, coupling_cache_count + if (coupling_cache_source(item) .ne. source_id) cycle + total_force = total_force + coupling_cache_force(:,item) + total_moment = total_moment + coupling_cache_moment(:,item) + accepted_count = accepted_count + 1 + end do + ierr = FEM_SUCCESS + end subroutine fem_get_cpl_source_load_diag + + subroutine fem_write_cpl_reports(load_file, surface_file, ierr) + character(len=*), intent(in) :: load_file + character(len=*), intent(in) :: surface_file + integer, intent(out) :: ierr + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, & + 'fem_write_cpl_reports called before fem_initialize', ierr) + return + end if + call write_cpl_load_report(load_file, ierr) + if (ierr .ne. FEM_SUCCESS) return + call write_cpl_surface_report(surface_file, ierr) + end subroutine fem_write_cpl_reports + + subroutine write_cpl_load_report(filename, ierr) + character(len=*), intent(in) :: filename + integer, intent(out) :: ierr + integer :: unit_id + integer :: ios + integer :: item + + if (len_trim(filename) .eq. 0) then + call error_set(FEM_ERR_OUTPUT, 'coupling load report filename empty', ierr) + return + end if + unit_id = 77 + open(unit=unit_id, file=trim(filename), status='replace', & + action='write', iostat=ios) + if (ios .ne. 0) then + call error_set(FEM_ERR_OUTPUT, 'failed to open coupling load report', ierr) + return + end if + write(unit_id,'(A)',iostat=ios) & + 'index,node_id,source_id,load_mode,fx,fy,fz,mx,my,mz' + do item = 1, coupling_cache_count + write(unit_id,'(I0,",",I0,",",I0,",",I0,6(",",ES16.8))',iostat=ios) & + item, coupling_cache_node(item), coupling_cache_source(item), & + coupling_cache_mode(item), coupling_cache_force(:,item), & + coupling_cache_moment(:,item) + if (ios .ne. 0) exit + end do + close(unit_id) + if (ios .ne. 0) then + call error_set(FEM_ERR_OUTPUT, 'failed to write coupling load report', ierr) + else + ierr = FEM_SUCCESS + end if + end subroutine write_cpl_load_report + + subroutine write_cpl_surface_report(filename, ierr) + character(len=*), intent(in) :: filename + integer, intent(out) :: ierr + integer :: unit_id + integer :: ios + integer :: item + integer :: source_id + integer :: count + real(rk) :: total_force(NDIM) + real(rk) :: total_moment(NDIM) + + if (len_trim(filename) .eq. 0) then + call error_set(FEM_ERR_OUTPUT, 'coupling surface report filename empty', ierr) + return + end if + unit_id = 78 + open(unit=unit_id, file=trim(filename), status='replace', & + action='write', iostat=ios) + if (ios .ne. 0) then + call error_set(FEM_ERR_OUTPUT, 'failed to open coupling surface report', ierr) + return + end if + write(unit_id,'(A)',iostat=ios) 'source_id,count,fx,fy,fz,mx,my,mz' + do item = 1, coupling_cache_count + source_id = coupling_cache_source(item) + if (source_seen_before(source_id, item)) cycle + call fem_get_cpl_source_load_diag(source_id, total_force, total_moment, & + count, ierr) + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + write(unit_id,'(I0,",",I0,6(",",ES16.8))',iostat=ios) source_id, & + count, total_force, total_moment + if (ios .ne. 0) exit + end do + close(unit_id) + if (ios .ne. 0) then + call error_set(FEM_ERR_OUTPUT, & + 'failed to write coupling surface report', ierr) + else + ierr = FEM_SUCCESS + end if + end subroutine write_cpl_surface_report + + logical function source_seen_before(source_id, before_item) + integer, intent(in) :: source_id + integer, intent(in) :: before_item + integer :: item + + source_seen_before = .false. + do item = 1, before_item - 1 + if (coupling_cache_source(item) .eq. source_id) then + source_seen_before = .true. + return + end if + end do + end function source_seen_before + + logical function finite_real(value) + real(rk), intent(in) :: value + + finite_real = value .le. huge(value) .and. value .ge. -huge(value) + end function finite_real + subroutine fem_get_surface_node_count(surface, node_count, ierr) character(len=*), intent(in) :: surface integer, intent(out) :: node_count @@ -553,4 +1006,14 @@ contains facet_nnode_in, node_forces, ierr) end subroutine fem_apply_sph_facet_nodal_loads + pure function cross3(a, b) result(c) + real(rk), intent(in) :: a(NDIM) + real(rk), intent(in) :: b(NDIM) + real(rk) :: c(NDIM) + + c(1) = a(2) * b(3) - a(3) * b(2) + c(2) = a(3) * b(1) - a(1) * b(3) + c(3) = a(1) * b(2) - a(2) * b(1) + end function cross3 + end module fem_api_coupling diff --git a/src/base/fem_precision.f90 b/src/base/fem_precision.f90 index 99d76a9..28f74b2 100644 --- a/src/base/fem_precision.f90 +++ b/src/base/fem_precision.f90 @@ -16,7 +16,7 @@ module fem_precision integer, parameter :: FEMLITE_VERSION_MAJOR = 0 integer, parameter :: FEMLITE_VERSION_MINOR = 6 - integer, parameter :: FEMLITE_VERSION_PATCH = 3 + integer, parameter :: FEMLITE_VERSION_PATCH = 4 real(rk), parameter :: ZERO = 0.0_rk real(rk), parameter :: ONE = 1.0_rk diff --git a/src/coupling/fem_coupling.f90 b/src/coupling/fem_coupling.f90 index 3a73d2d..2034619 100644 --- a/src/coupling/fem_coupling.f90 +++ b/src/coupling/fem_coupling.f90 @@ -2,6 +2,7 @@ module fem_coupling use fem_precision use fem_error use fem_database + use fem_database_surface, only: db_surface_has_facet use fem_element_topology implicit none @@ -16,7 +17,10 @@ module fem_coupling public :: cpl_build_facets public :: cpl_finalize_facets public :: cpl_get_facet_count + public :: cpl_get_facet_count_named public :: cpl_get_facets + public :: cpl_get_facets_ex + public :: cpl_get_facets_ex_named public :: cpl_apply_facet_pressure public :: cpl_apply_facet_tractions public :: cpl_apply_facet_nodal_loads @@ -159,6 +163,29 @@ contains ierr = FEM_SUCCESS end subroutine cpl_get_facet_count + subroutine cpl_get_facet_count_named(surface, facet_count, max_face_nodes, & + ierr) + character(len=*), intent(in) :: surface + integer, intent(out) :: facet_count + integer, intent(out) :: max_face_nodes + integer, intent(out) :: ierr + integer :: idx + integer :: filter_id + + call resolve_surface_filter(surface, filter_id, ierr) + if (ierr .ne. FEM_SUCCESS) return + + facet_count = 0 + max_face_nodes = 0 + do idx = 1, nfacet + if (elem_status(facet_elem(idx)) .ne. 0) cycle + if (.not. facet_matches_filter(idx, filter_id)) cycle + facet_count = facet_count + 1 + if (facet_nnode(idx) .gt. max_face_nodes) max_face_nodes = facet_nnode(idx) + end do + ierr = FEM_SUCCESS + end subroutine cpl_get_facet_count_named + subroutine cpl_get_facets(nfacet_in, max_face_nodes_in, & facet_id, elem_id, face_id, & facet_nnode_out, facet_conn_out, & @@ -232,6 +259,148 @@ contains ierr = FEM_SUCCESS end subroutine cpl_get_facets + subroutine cpl_get_facets_ex(nfacet_in, max_face_nodes_in, & + facet_id, surface_id, elem_id, face_id, & + facet_nnode_out, facet_conn_out, & + x, vnod, normal, area, material_id, & + section_id, valid_flags, shell_thickness, & + mass_per_area, ierr) + integer, intent(in) :: nfacet_in + integer, intent(in) :: max_face_nodes_in + integer, intent(out) :: facet_id(nfacet_in) + integer, intent(out) :: surface_id(nfacet_in) + integer, intent(out) :: elem_id(nfacet_in) + integer, intent(out) :: face_id(nfacet_in) + integer, intent(out) :: facet_nnode_out(nfacet_in) + integer, intent(out) :: facet_conn_out(max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: x(NDIM,max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: vnod(NDIM,max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: normal(NDIM,nfacet_in) + real(rk), intent(out) :: area(nfacet_in) + integer, intent(out) :: material_id(nfacet_in) + integer, intent(out) :: section_id(nfacet_in) + integer, intent(out) :: valid_flags(nfacet_in) + real(rk), intent(out) :: shell_thickness(nfacet_in) + real(rk), intent(out) :: mass_per_area(nfacet_in) + integer, intent(out) :: ierr + + call cpl_get_facets_ex_named(' ', nfacet_in, max_face_nodes_in, & + facet_id, surface_id, elem_id, face_id, & + facet_nnode_out, facet_conn_out, x, vnod, & + normal, area, material_id, section_id, & + valid_flags, shell_thickness, & + mass_per_area, ierr) + end subroutine cpl_get_facets_ex + + subroutine cpl_get_facets_ex_named(surface, nfacet_in, max_face_nodes_in, & + facet_id, surface_id, elem_id, & + face_id, facet_nnode_out, & + facet_conn_out, x, vnod, normal, area, & + material_id, section_id, valid_flags, & + shell_thickness, mass_per_area, ierr) + character(len=*), intent(in) :: surface + integer, intent(in) :: nfacet_in + integer, intent(in) :: max_face_nodes_in + integer, intent(out) :: facet_id(nfacet_in) + integer, intent(out) :: surface_id(nfacet_in) + integer, intent(out) :: elem_id(nfacet_in) + integer, intent(out) :: face_id(nfacet_in) + integer, intent(out) :: facet_nnode_out(nfacet_in) + integer, intent(out) :: facet_conn_out(max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: x(NDIM,max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: vnod(NDIM,max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: normal(NDIM,nfacet_in) + real(rk), intent(out) :: area(nfacet_in) + integer, intent(out) :: material_id(nfacet_in) + integer, intent(out) :: section_id(nfacet_in) + integer, intent(out) :: valid_flags(nfacet_in) + real(rk), intent(out) :: shell_thickness(nfacet_in) + real(rk), intent(out) :: mass_per_area(nfacet_in) + integer, intent(out) :: ierr + integer :: idx + integer :: active_count + integer :: out_idx + integer :: local_node + integer :: node + integer :: max_nodes + integer :: elem + integer :: sec + integer :: mat + integer :: filter_id + real(rk) :: coords(NDIM,MAX_FACE_NODES) + real(rk) :: velocities(NDIM,MAX_FACE_NODES) + real(rk) :: nrm(NDIM) + real(rk) :: ar + logical :: is_valid + + call resolve_surface_filter(surface, filter_id, ierr) + if (ierr .ne. FEM_SUCCESS) return + call cpl_get_facet_count_named(surface, active_count, max_nodes, ierr) + if (ierr .ne. FEM_SUCCESS) return + if (nfacet_in .lt. active_count) then + call error_set(FEM_ERR_COUPLING, 'facet output array is too small', ierr) + return + end if + if (max_face_nodes_in .lt. max_nodes) then + call error_set(FEM_ERR_COUPLING, 'facet node dimension is too small', ierr) + return + end if + + facet_id = 0 + surface_id = 0 + elem_id = 0 + face_id = 0 + facet_nnode_out = 0 + facet_conn_out = 0 + x = ZERO + vnod = ZERO + normal = ZERO + area = ZERO + material_id = 0 + section_id = 0 + valid_flags = 0 + shell_thickness = ZERO + mass_per_area = ZERO + + out_idx = 0 + do idx = 1, nfacet + if (elem_status(facet_elem(idx)) .ne. 0) cycle + if (.not. facet_matches_filter(idx, filter_id)) cycle + out_idx = out_idx + 1 + call facet_current_geometry_safe(idx, coords, velocities, nrm, ar, & + is_valid) + + elem = facet_elem(idx) + sec = elem_sec(elem) + mat = elem_mat(elem) + facet_id(out_idx) = idx + surface_id(out_idx) = filter_id + elem_id(out_idx) = elem_id_ext(elem) + face_id(out_idx) = facet_face(idx) + facet_nnode_out(out_idx) = facet_nnode(idx) + material_id(out_idx) = mat + section_id(out_idx) = sec + normal(:,out_idx) = nrm + area(out_idx) = ar + if (is_valid) valid_flags(out_idx) = 1 + if (element_is_shell(elem_type(elem)) .and. sec .gt. 0) then + shell_thickness(out_idx) = section_thickness(sec) + if (mat .gt. 0) then + mass_per_area(out_idx) = section_thickness(sec) * mat_density(mat) + end if + end if + + do local_node = 1, facet_nnode(idx) + node = facet_conn(local_node,idx) + facet_conn_out(local_node,out_idx) = node_id_ext(node) + x(:,local_node,out_idx) = coords(:,local_node) + vnod(:,local_node,out_idx) = velocities(:,local_node) + end do + end do + + ierr = FEM_SUCCESS + end subroutine cpl_get_facets_ex_named + subroutine cpl_apply_facet_pressure(nfacet_in, facet_id, pressure, ierr) integer, intent(in) :: nfacet_in integer, intent(in) :: facet_id(nfacet_in) @@ -557,6 +726,36 @@ contains call face_geometry(facet_nnode(idx), coords, normal, area, ierr) end subroutine facet_current_geometry + subroutine facet_current_geometry_safe(idx, coords, velocities, normal, & + area, is_valid) + integer, intent(in) :: idx + real(rk), intent(out) :: coords(NDIM,MAX_FACE_NODES) + real(rk), intent(out) :: velocities(NDIM,MAX_FACE_NODES) + real(rk), intent(out) :: normal(NDIM) + real(rk), intent(out) :: area + logical, intent(out) :: is_valid + integer :: local_node + integer :: node + integer :: idir + integer :: dof + + coords = ZERO + velocities = ZERO + do local_node = 1, facet_nnode(idx) + node = facet_conn(local_node,idx) + coords(:,local_node) = coord0(:,node) + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) then + coords(idir,local_node) = coords(idir,local_node) + u(dof) + velocities(idir,local_node) = v(dof) + end if + end do + end do + + call face_geometry_safe(facet_nnode(idx), coords, normal, area, is_valid) + end subroutine facet_current_geometry_safe + subroutine face_geometry_from_nodes(num_face_nodes, nodes, coordinates, normal, area, ierr) integer, intent(in) :: num_face_nodes integer, intent(in) :: nodes(MAX_FACE_NODES) @@ -611,6 +810,39 @@ contains ierr = FEM_SUCCESS end subroutine face_geometry + subroutine face_geometry_safe(num_face_nodes, coords, normal, area, is_valid) + integer, intent(in) :: num_face_nodes + real(rk), intent(in) :: coords(NDIM,MAX_FACE_NODES) + real(rk), intent(out) :: normal(NDIM) + real(rk), intent(out) :: area + logical, intent(out) :: is_valid + real(rk) :: tri_normal(NDIM) + real(rk) :: total_normal(NDIM) + real(rk) :: tri_area + logical :: tri_valid + + normal = ZERO + area = ZERO + is_valid = .false. + + if (num_face_nodes .eq. 3 .or. num_face_nodes .eq. 6) then + call triangle_normal_area_safe(coords(:,1), coords(:,2), coords(:,3), & + normal, area, is_valid) + else if (num_face_nodes .eq. 4 .or. num_face_nodes .eq. 8) then + call triangle_normal_area_safe(coords(:,1), coords(:,2), coords(:,3), & + total_normal, area, tri_valid) + if (.not. tri_valid) return + call triangle_normal_area_safe(coords(:,1), coords(:,3), coords(:,4), & + tri_normal, tri_area, tri_valid) + if (.not. tri_valid) return + total_normal = total_normal * area + tri_normal * tri_area + area = area + tri_area + if (area .le. ZERO) return + normal = total_normal / sqrt(sum(total_normal * total_normal)) + is_valid = .true. + end if + end subroutine face_geometry_safe + subroutine triangle_normal_area(a, b, c, normal, area, ierr) real(rk), intent(in) :: a(NDIM) real(rk), intent(in) :: b(NDIM) @@ -637,6 +869,32 @@ contains ierr = FEM_SUCCESS end subroutine triangle_normal_area + subroutine triangle_normal_area_safe(a, b, c, normal, area, is_valid) + real(rk), intent(in) :: a(NDIM) + real(rk), intent(in) :: b(NDIM) + real(rk), intent(in) :: c(NDIM) + real(rk), intent(out) :: normal(NDIM) + real(rk), intent(out) :: area + logical, intent(out) :: is_valid + real(rk) :: ab(NDIM) + real(rk) :: ac(NDIM) + real(rk) :: cr(NDIM) + real(rk) :: norm_cr + + normal = ZERO + area = ZERO + is_valid = .false. + ab = b - a + ac = c - a + call cross3(ab, ac, cr) + norm_cr = sqrt(sum(cr * cr)) + if (norm_cr .le. ZERO) return + + area = HALF * norm_cr + normal = cr / norm_cr + is_valid = .true. + end subroutine triangle_normal_area_safe + subroutine cross3(a, b, c) real(rk), intent(in) :: a(NDIM) real(rk), intent(in) :: b(NDIM) @@ -732,4 +990,47 @@ contains end if end function find_facet + subroutine resolve_surface_filter(surface, filter_id, ierr) + character(len=*), intent(in) :: surface + integer, intent(out) :: filter_id + integer, intent(out) :: ierr + character(len=NAME_LEN) :: local_surface + + local_surface = adjustl(surface) + filter_id = 0 + if (len_trim(local_surface) .eq. 0 .or. & + trim(local_surface) .eq. 'ALL' .or. & + trim(local_surface) .eq. 'all') then + ierr = FEM_SUCCESS + return + end if + + filter_id = database_find_surface(local_surface) + if (filter_id .eq. 0) then + call error_set(FEM_ERR_INPUT, 'coupling surface filter is unknown', ierr) + return + end if + if (allocated(surface_type)) then + if (trim(surface_type(filter_id)) .ne. 'ELEMENT') then + call error_set(FEM_ERR_INPUT, & + 'coupling surface filter requires TYPE=ELEMENT', ierr) + return + end if + end if + ierr = FEM_SUCCESS + end subroutine resolve_surface_filter + + logical function facet_matches_filter(idx, filter_id) + integer, intent(in) :: idx + integer, intent(in) :: filter_id + integer :: elem_ext + + facet_matches_filter = .true. + if (filter_id .le. 0) return + + elem_ext = elem_id_ext(facet_elem(idx)) + facet_matches_filter = db_surface_has_facet(filter_id, elem_ext, & + facet_face(idx)) + end function facet_matches_filter + end module fem_coupling diff --git a/src/input/fem_inp_helpers.f90 b/src/input/fem_inp_helpers.f90 index 7619d9b..50461cc 100644 --- a/src/input/fem_inp_helpers.f90 +++ b/src/input/fem_inp_helpers.f90 @@ -1598,7 +1598,15 @@ subroutine start_nset(set_index, item_pos, set_name_value) line_len = len_trim(line) do i = 1, line_len + 1 - if (i .gt. line_len .or. line(i:i) .eq. ',') then + if (i .gt. line_len) then + if (field .eq. field_index) then + end_pos = i - 1 + if (end_pos .ge. start_pos) token = adjustl(line(start_pos:end_pos)) + return + end if + field = field + 1 + start_pos = i + 1 + else if (line(i:i) .eq. ',') then if (field .eq. field_index) then end_pos = i - 1 if (end_pos .ge. start_pos) token = adjustl(line(start_pos:end_pos)) diff --git a/src/interface/fem_c_api.c b/src/interface/fem_c_api.c index 6b394c7..9d9e003 100644 --- a/src/interface/fem_c_api.c +++ b/src/interface/fem_c_api.c @@ -54,6 +54,16 @@ #define F_GET_SPH_SUMMARY FC_GLOBAL(femlite_f_get_sph_summary, FEMLITE_F_GET_SPH_SUMMARY) #define F_GET_FACET_COUNT FC_GLOBAL(femlite_f_get_facet_count, FEMLITE_F_GET_FACET_COUNT) #define F_GET_FACETS FC_GLOBAL(femlite_f_get_facets, FEMLITE_F_GET_FACETS) +#define F_CLEAR_COUPLING FC_GLOBAL(femlite_f_clear_cpl_loads, FEMLITE_F_CLEAR_CPL_LOADS) +#define F_GET_COUPLING_SURFACE_COUNT FC_GLOBAL(femlite_f_get_cpl_surf_count, FEMLITE_F_GET_CPL_SURF_COUNT) +#define F_GET_COUPLING_SURFACE_COUNT_C FC_GLOBAL(femlite_f_get_cpl_surf_cnt_c, FEMLITE_F_GET_CPL_SURF_CNT_C) +#define F_GET_COUPLING_SURFACE_FACETS FC_GLOBAL(femlite_f_get_cpl_surf_facets, FEMLITE_F_GET_CPL_SURF_FACETS) +#define F_GET_COUPLING_SURFACE_EX FC_GLOBAL(femlite_f_get_cpl_surf_ex, FEMLITE_F_GET_CPL_SURF_EX) +#define F_GET_COUPLING_SURFACE_EX_C FC_GLOBAL(femlite_f_get_cpl_surf_ex_c, FEMLITE_F_GET_CPL_SURF_EX_C) +#define F_APPLY_COUPLING_NODES FC_GLOBAL(femlite_f_apply_cpl_nodes, FEMLITE_F_APPLY_CPL_NODES) +#define F_GET_COUPLING_LOAD_DIAG FC_GLOBAL(femlite_f_get_cpl_load_diag, FEMLITE_F_GET_CPL_LOAD_DIAG) +#define F_GET_COUPLING_SOURCE_DIAG FC_GLOBAL(femlite_f_get_cpl_source_diag, FEMLITE_F_GET_CPL_SOURCE_DIAG) +#define F_WRITE_COUPLING_REPORTS FC_GLOBAL(femlite_f_write_cpl_reports, FEMLITE_F_WRITE_CPL_REPORTS) #define F_APPLY_PRESSURE FC_GLOBAL(femlite_f_apply_facet_pressure, FEMLITE_F_APPLY_FACET_PRESSURE) #define F_APPLY_TRACTIONS FC_GLOBAL(femlite_f_apply_facet_tractions, FEMLITE_F_APPLY_FACET_TRACTIONS) #define F_APPLY_FACET_NODAL FC_GLOBAL(femlite_f_apply_facet_nodal, FEMLITE_F_APPLY_FACET_NODAL) @@ -227,6 +237,89 @@ extern void F_GET_FACETS(int *num_facets, double *normal, double *area, int *ierr); +extern void F_CLEAR_COUPLING(int *ierr); +extern void F_GET_COUPLING_SURFACE_COUNT(int *num_facets, + int *max_face_nodes, + int *ierr); +extern void F_GET_COUPLING_SURFACE_COUNT_C(const char *surface, + int *length, + int *num_facets, + int *max_face_nodes, + int *ierr); +extern void F_GET_COUPLING_SURFACE_FACETS(int *num_facets, + int *max_face_nodes, + int *facet_id, + int *elem_id, + int *face_id, + int *facet_nnode, + int *facet_conn, + double *x, + double *vnod, + double *normal, + double *area, + int *ierr); +extern void F_GET_COUPLING_SURFACE_EX(int *num_facets, + int *max_face_nodes, + int *facet_id, + int *surface_id, + int *elem_id, + int *face_id, + int *facet_nnode, + int *facet_conn, + double *x, + double *vnod, + double *normal, + double *area, + int *material_id, + int *section_id, + int *valid_flags, + double *shell_thickness, + double *mass_per_area, + int *ierr); +extern void F_GET_COUPLING_SURFACE_EX_C(const char *surface, + int *length, + int *num_facets, + int *max_face_nodes, + int *facet_id, + int *surface_id, + int *elem_id, + int *face_id, + int *facet_nnode, + int *facet_conn, + double *x, + double *vnod, + double *normal, + double *area, + int *material_id, + int *section_id, + int *valid_flags, + double *shell_thickness, + double *mass_per_area, + int *ierr); +extern void F_APPLY_COUPLING_NODES(int *num_loads, + const int *node_ids, + const double *forces, + const int *source_ids, + const int *load_mode, + const int *valid_flags, + int *ierr); +extern void F_GET_COUPLING_LOAD_DIAG(double *total_force, + double *total_moment, + int *accepted_count, + int *rejected_nonfinite, + int *rejected_unknown_node, + int *rejected_capacity, + int *ierr); +extern void F_GET_COUPLING_SOURCE_DIAG(int *source_id, + double *total_force, + double *total_moment, + int *accepted_count, + int *ierr); +extern void F_WRITE_COUPLING_REPORTS(const char *load_file, + int *load_len, + const char *surface_file, + int *surface_len, + int *ierr); extern void F_APPLY_PRESSURE(int *num_facets, const int *facet_id, const double *pressure, @@ -795,6 +888,213 @@ int femlite_get_sph_force_summary(double *total_force) return ierr; } +int femlite_clear_coupling_loads(void) +{ + int ierr = 0; + F_CLEAR_COUPLING(&ierr); + return ierr; +} + +int femlite_get_coupling_surface_count(int *num_facets, int *max_face_nodes) +{ + int ierr = 0; + if (num_facets == NULL || max_face_nodes == NULL) { + return -1; + } + F_GET_COUPLING_SURFACE_COUNT(num_facets, max_face_nodes, &ierr); + return ierr; +} + +int femlite_get_coupling_surface_count_for_name(const char *surface_name, + int *num_facets, + int *max_face_nodes) +{ + int ierr = 0; + int length = 0; + if (surface_name == NULL || num_facets == NULL || + max_face_nodes == NULL) { + return -1; + } + length = (int)strlen(surface_name); + if (length <= 0) { + return -1; + } + F_GET_COUPLING_SURFACE_COUNT_C(surface_name, &length, num_facets, + max_face_nodes, &ierr); + return ierr; +} + +int femlite_get_coupling_surface_facets(int num_facets, + int max_face_nodes, + int *facet_id, + int *elem_id, + int *face_id, + int *facet_nnode, + int *facet_conn, + double *x, + double *vnod, + double *normal, + double *area) +{ + int ierr = 0; + if (facet_id == NULL || elem_id == NULL || face_id == NULL || + facet_nnode == NULL || facet_conn == NULL || x == NULL || + vnod == NULL || normal == NULL || area == NULL) { + return -1; + } + F_GET_COUPLING_SURFACE_FACETS(&num_facets, &max_face_nodes, facet_id, + elem_id, face_id, facet_nnode, facet_conn, + x, vnod, normal, area, &ierr); + return ierr; +} + +int femlite_get_coupling_surface_facets_ex(int num_facets, + int max_face_nodes, + int *facet_id, + int *surface_id, + int *elem_id, + int *face_id, + int *facet_nnode, + int *facet_conn, + double *x, + double *vnod, + double *normal, + double *area, + int *material_id, + int *section_id, + int *valid_flags, + double *shell_thickness, + double *mass_per_area) +{ + int ierr = 0; + if (facet_id == NULL || surface_id == NULL || elem_id == NULL || + face_id == NULL || facet_nnode == NULL || facet_conn == NULL || + x == NULL || vnod == NULL || normal == NULL || area == NULL || + material_id == NULL || section_id == NULL || valid_flags == NULL || + shell_thickness == NULL || mass_per_area == NULL) { + return -1; + } + F_GET_COUPLING_SURFACE_EX(&num_facets, &max_face_nodes, facet_id, + surface_id, elem_id, face_id, facet_nnode, + facet_conn, x, vnod, normal, area, + material_id, section_id, valid_flags, + shell_thickness, mass_per_area, &ierr); + return ierr; +} + +int femlite_get_coupling_surface_facets_ex_for_name( + const char *surface_name, + int num_facets, + int max_face_nodes, + int *facet_id, + int *surface_id, + int *elem_id, + int *face_id, + int *facet_nnode, + int *facet_conn, + double *x, + double *vnod, + double *normal, + double *area, + int *material_id, + int *section_id, + int *valid_flags, + double *shell_thickness, + double *mass_per_area) +{ + int ierr = 0; + int length = 0; + if (surface_name == NULL || facet_id == NULL || surface_id == NULL || + elem_id == NULL || face_id == NULL || facet_nnode == NULL || + facet_conn == NULL || x == NULL || vnod == NULL || + normal == NULL || area == NULL || material_id == NULL || + section_id == NULL || valid_flags == NULL || + shell_thickness == NULL || mass_per_area == NULL) { + return -1; + } + length = (int)strlen(surface_name); + if (length <= 0) { + return -1; + } + F_GET_COUPLING_SURFACE_EX_C(surface_name, &length, &num_facets, + &max_face_nodes, facet_id, surface_id, + elem_id, face_id, facet_nnode, facet_conn, + x, vnod, normal, area, material_id, + section_id, valid_flags, shell_thickness, + mass_per_area, &ierr); + return ierr; +} + +int femlite_apply_coupling_nodal_forces(int num_loads, + const int *node_ids, + const double *forces, + const int *source_ids, + const int *load_mode, + const int *valid_flags) +{ + int ierr = 0; + if (num_loads < 0 || node_ids == NULL || forces == NULL || + source_ids == NULL || load_mode == NULL || valid_flags == NULL) { + return -1; + } + F_APPLY_COUPLING_NODES(&num_loads, node_ids, forces, source_ids, + load_mode, valid_flags, &ierr); + return ierr; +} + +int femlite_get_coupling_load_diagnostics(double *total_force, + double *total_moment, + int *accepted_count, + int *rejected_nonfinite, + int *rejected_unknown_node, + int *rejected_capacity) +{ + int ierr = 0; + if (total_force == NULL || total_moment == NULL || + accepted_count == NULL || rejected_nonfinite == NULL || + rejected_unknown_node == NULL || rejected_capacity == NULL) { + return -1; + } + F_GET_COUPLING_LOAD_DIAG(total_force, total_moment, accepted_count, + rejected_nonfinite, rejected_unknown_node, + rejected_capacity, &ierr); + return ierr; +} + +int femlite_get_coupling_source_load_diagnostics(int source_id, + double *total_force, + double *total_moment, + int *accepted_count) +{ + int ierr = 0; + if (source_id < 0 || total_force == NULL || total_moment == NULL || + accepted_count == NULL) { + return -1; + } + F_GET_COUPLING_SOURCE_DIAG(&source_id, total_force, total_moment, + accepted_count, &ierr); + return ierr; +} + +int femlite_write_coupling_reports(const char *load_report, + const char *surface_report) +{ + int ierr = 0; + int load_len = 0; + int surface_len = 0; + if (load_report == NULL || surface_report == NULL) { + return -1; + } + load_len = (int)strlen(load_report); + surface_len = (int)strlen(surface_report); + if (load_len <= 0 || surface_len <= 0) { + return -1; + } + F_WRITE_COUPLING_REPORTS(load_report, &load_len, surface_report, + &surface_len, &ierr); + return ierr; +} + int femlite_get_boundary_facet_count(int *num_facets, int *max_face_nodes) { int ierr = 0; diff --git a/src/interface/fem_cclite_ipc.c b/src/interface/fem_cclite_ipc.c new file mode 100644 index 0000000..2091ba9 --- /dev/null +++ b/src/interface/fem_cclite_ipc.c @@ -0,0 +1,365 @@ +#include "femlite_cclite_ipc.h" + +#include + +#ifdef _WIN32 +#include +#endif + +size_t femlite_cclite_command_block_size(void) +{ + return sizeof(femlite_cclite_command_block); +} + +void femlite_cclite_init_command_block(femlite_cclite_command_block *block) +{ + if (block == NULL) { + return; + } + memset(block, 0, sizeof(*block)); + block->version = FEMLITE_CCL_IPC_VERSION; +} + +int femlite_cclite_format_name(const char *prefix, const char *suffix, + char *out_name, size_t out_capacity) +{ + size_t prefix_len; + size_t suffix_len; + size_t total_len; + + if (prefix == NULL || suffix == NULL || out_name == NULL || + out_capacity == 0) { + return FEMLITE_CCL_IPC_ERR_INPUT; + } + + prefix_len = strlen(prefix); + suffix_len = strlen(suffix); + total_len = prefix_len + 1 + suffix_len; + if (prefix_len == 0 || suffix_len == 0 || total_len + 1 > out_capacity) { + return FEMLITE_CCL_IPC_ERR_CAPACITY; + } + + memcpy(out_name, prefix, prefix_len); + out_name[prefix_len] = '_'; + memcpy(out_name + prefix_len + 1, suffix, suffix_len); + out_name[total_len] = '\0'; + return FEMLITE_CCL_IPC_SUCCESS; +} + +int femlite_cclite_open_shared_memory(const char *name, size_t min_size, + femlite_cclite_map *map) +{ + if (name == NULL || map == NULL || min_size == 0) { + return FEMLITE_CCL_IPC_ERR_INPUT; + } + memset(map, 0, sizeof(*map)); + +#ifdef _WIN32 + map->handle = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, name); + if (map->handle == NULL) { + map->last_native_error = (int32_t)GetLastError(); + return FEMLITE_CCL_IPC_ERR_OPEN; + } + + map->view = MapViewOfFile(map->handle, FILE_MAP_ALL_ACCESS, 0, 0, min_size); + if (map->view == NULL) { + map->last_native_error = (int32_t)GetLastError(); + CloseHandle((HANDLE)map->handle); + map->handle = NULL; + return FEMLITE_CCL_IPC_ERR_MAP; + } + + map->size = min_size; + return FEMLITE_CCL_IPC_SUCCESS; +#else + (void)name; + (void)min_size; + return FEMLITE_CCL_IPC_ERR_UNSUPPORTED; +#endif +} + +int femlite_cclite_close_shared_memory(femlite_cclite_map *map) +{ + if (map == NULL) { + return FEMLITE_CCL_IPC_ERR_INPUT; + } + +#ifdef _WIN32 + if (map->view != NULL) { + UnmapViewOfFile(map->view); + } + if (map->handle != NULL) { + CloseHandle((HANDLE)map->handle); + } +#endif + memset(map, 0, sizeof(*map)); + return FEMLITE_CCL_IPC_SUCCESS; +} + +int femlite_cclite_open_event(const char *name, femlite_cclite_event *event) +{ + if (name == NULL || event == NULL) { + return FEMLITE_CCL_IPC_ERR_INPUT; + } + memset(event, 0, sizeof(*event)); + +#ifdef _WIN32 + event->handle = OpenEventA(SYNCHRONIZE | EVENT_MODIFY_STATE, FALSE, name); + if (event->handle == NULL) { + event->last_native_error = (int32_t)GetLastError(); + return FEMLITE_CCL_IPC_ERR_OPEN; + } + return FEMLITE_CCL_IPC_SUCCESS; +#else + (void)name; + return FEMLITE_CCL_IPC_ERR_UNSUPPORTED; +#endif +} + +int femlite_cclite_close_event(femlite_cclite_event *event) +{ + if (event == NULL) { + return FEMLITE_CCL_IPC_ERR_INPUT; + } + +#ifdef _WIN32 + if (event->handle != NULL) { + CloseHandle((HANDLE)event->handle); + } +#endif + memset(event, 0, sizeof(*event)); + return FEMLITE_CCL_IPC_SUCCESS; +} + +int femlite_cclite_wait_event(femlite_cclite_event *event, + unsigned int timeout_ms) +{ + if (event == NULL || event->handle == NULL) { + return FEMLITE_CCL_IPC_ERR_INPUT; + } + +#ifdef _WIN32 + { + DWORD rc = WaitForSingleObject((HANDLE)event->handle, + (DWORD)timeout_ms); + if (rc == WAIT_OBJECT_0) { + return FEMLITE_CCL_IPC_SUCCESS; + } + if (rc == WAIT_TIMEOUT) { + return FEMLITE_CCL_IPC_ERR_TIMEOUT; + } + event->last_native_error = (int32_t)GetLastError(); + return FEMLITE_CCL_IPC_ERR_WAIT; + } +#else + (void)timeout_ms; + return FEMLITE_CCL_IPC_ERR_UNSUPPORTED; +#endif +} + +int femlite_cclite_signal_event(femlite_cclite_event *event) +{ + if (event == NULL || event->handle == NULL) { + return FEMLITE_CCL_IPC_ERR_INPUT; + } + +#ifdef _WIN32 + if (!SetEvent((HANDLE)event->handle)) { + event->last_native_error = (int32_t)GetLastError(); + return FEMLITE_CCL_IPC_ERR_SIGNAL; + } + return FEMLITE_CCL_IPC_SUCCESS; +#else + return FEMLITE_CCL_IPC_ERR_UNSUPPORTED; +#endif +} + +int femlite_cclite_wait_command(femlite_cclite_event *command_event, + femlite_cclite_map *command_map, + unsigned int timeout_ms, + femlite_cclite_command_block *out_block) +{ + int ierr; + + if (command_event == NULL || command_map == NULL || out_block == NULL || + command_map->view == NULL || + command_map->size < sizeof(femlite_cclite_command_block)) { + return FEMLITE_CCL_IPC_ERR_INPUT; + } + + ierr = femlite_cclite_wait_event(command_event, timeout_ms); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + return ierr; + } + memcpy(out_block, command_map->view, sizeof(*out_block)); + return FEMLITE_CCL_IPC_SUCCESS; +} + +int femlite_cclite_write_done(femlite_cclite_event *done_event, + femlite_cclite_map *command_map, + const femlite_cclite_command_block *block) +{ + if (done_event == NULL || command_map == NULL || block == NULL || + command_map->view == NULL || + command_map->size < sizeof(femlite_cclite_command_block)) { + return FEMLITE_CCL_IPC_ERR_INPUT; + } + + memcpy(command_map->view, block, sizeof(*block)); + return femlite_cclite_signal_event(done_event); +} + +int femlite_cclite_transport_init_mock(femlite_cclite_transport *transport) +{ + if (transport == NULL) { + return FEMLITE_CCL_IPC_ERR_INPUT; + } + memset(transport, 0, sizeof(*transport)); + transport->backend = FEMLITE_CCL_BACKEND_MOCK; + return FEMLITE_CCL_IPC_SUCCESS; +} + +int femlite_cclite_transport_open_windows(const char *prefix, + femlite_cclite_transport *transport) +{ + char map_name[256]; + char command_event_name[256]; + char done_event_name[256]; + int ierr; + + if (prefix == NULL || transport == NULL) { + return FEMLITE_CCL_IPC_ERR_INPUT; + } + + memset(transport, 0, sizeof(*transport)); + transport->backend = FEMLITE_CCL_BACKEND_WINDOWS_IPC; + + ierr = femlite_cclite_format_name(prefix, "COMMAND_BLOCK", map_name, + sizeof(map_name)); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + return ierr; + } + ierr = femlite_cclite_format_name(prefix, "COMMAND_EVENT", + command_event_name, + sizeof(command_event_name)); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + return ierr; + } + ierr = femlite_cclite_format_name(prefix, "DONE_EVENT", done_event_name, + sizeof(done_event_name)); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + return ierr; + } + + ierr = femlite_cclite_open_shared_memory( + map_name, sizeof(femlite_cclite_command_block), + &transport->command_map); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + return ierr; + } + ierr = femlite_cclite_open_event(command_event_name, + &transport->command_event); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + femlite_cclite_close_shared_memory(&transport->command_map); + return ierr; + } + ierr = femlite_cclite_open_event(done_event_name, &transport->done_event); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + femlite_cclite_close_event(&transport->command_event); + femlite_cclite_close_shared_memory(&transport->command_map); + return ierr; + } + return FEMLITE_CCL_IPC_SUCCESS; +} + +int femlite_cclite_transport_close(femlite_cclite_transport *transport) +{ + if (transport == NULL) { + return FEMLITE_CCL_IPC_ERR_INPUT; + } + if (transport->backend == FEMLITE_CCL_BACKEND_WINDOWS_IPC) { + femlite_cclite_close_event(&transport->done_event); + femlite_cclite_close_event(&transport->command_event); + femlite_cclite_close_shared_memory(&transport->command_map); + } + memset(transport, 0, sizeof(*transport)); + return FEMLITE_CCL_IPC_SUCCESS; +} + +int femlite_cclite_transport_mock_push( + femlite_cclite_transport *transport, + const femlite_cclite_command_block *block) +{ + if (transport == NULL || block == NULL || + transport->backend != FEMLITE_CCL_BACKEND_MOCK) { + return FEMLITE_CCL_IPC_ERR_INPUT; + } + if (transport->has_mock_command) { + return FEMLITE_CCL_IPC_ERR_CAPACITY; + } + transport->mock_command = *block; + transport->has_mock_command = 1; + return FEMLITE_CCL_IPC_SUCCESS; +} + +int femlite_cclite_transport_mock_get_done( + femlite_cclite_transport *transport, + femlite_cclite_command_block *block) +{ + if (transport == NULL || block == NULL || + transport->backend != FEMLITE_CCL_BACKEND_MOCK || + !transport->has_mock_done) { + return FEMLITE_CCL_IPC_ERR_INPUT; + } + *block = transport->mock_done; + return FEMLITE_CCL_IPC_SUCCESS; +} + +int femlite_cclite_transport_receive(femlite_cclite_transport *transport, + unsigned int timeout_ms, + femlite_cclite_command_block *out_block) +{ + if (transport == NULL || out_block == NULL) { + return FEMLITE_CCL_IPC_ERR_INPUT; + } + + if (transport->backend == FEMLITE_CCL_BACKEND_MOCK) { + (void)timeout_ms; + if (!transport->has_mock_command) { + return FEMLITE_CCL_IPC_ERR_TIMEOUT; + } + *out_block = transport->mock_command; + transport->has_mock_command = 0; + return FEMLITE_CCL_IPC_SUCCESS; + } + + if (transport->backend == FEMLITE_CCL_BACKEND_WINDOWS_IPC) { + return femlite_cclite_wait_command(&transport->command_event, + &transport->command_map, + timeout_ms, out_block); + } + + return FEMLITE_CCL_IPC_ERR_INPUT; +} + +int femlite_cclite_transport_complete( + femlite_cclite_transport *transport, + const femlite_cclite_command_block *block) +{ + if (transport == NULL || block == NULL) { + return FEMLITE_CCL_IPC_ERR_INPUT; + } + + if (transport->backend == FEMLITE_CCL_BACKEND_MOCK) { + transport->mock_done = *block; + transport->has_mock_done = 1; + return FEMLITE_CCL_IPC_SUCCESS; + } + + if (transport->backend == FEMLITE_CCL_BACKEND_WINDOWS_IPC) { + return femlite_cclite_write_done(&transport->done_event, + &transport->command_map, block); + } + + return FEMLITE_CCL_IPC_ERR_INPUT; +} diff --git a/src/interface/fem_fortran_bridge.f90 b/src/interface/fem_fortran_bridge.f90 index 94326db..98a59f3 100644 --- a/src/interface/fem_fortran_bridge.f90 +++ b/src/interface/fem_fortran_bridge.f90 @@ -505,6 +505,34 @@ subroutine femlite_f_get_facet_count(num_facets, max_fnodes, ierr) call fem_get_boundary_facet_count(num_facets, max_fnodes, ierr) end subroutine femlite_f_get_facet_count +subroutine femlite_f_get_cpl_surf_count(num_facets, max_fnodes, ierr) + use fem_api + implicit none + integer, intent(out) :: num_facets + integer, intent(out) :: max_fnodes + integer, intent(out) :: ierr + + call fem_get_coupling_surface_count(num_facets, max_fnodes, ierr) +end subroutine femlite_f_get_cpl_surf_count + +subroutine femlite_f_get_cpl_surf_cnt_c(surface, length, num_facets, & + max_fnodes, ierr) + use fem_api + use fem_fortran_bridge_utils + implicit none + integer, intent(in) :: length + character(len=1), intent(in) :: surface(length) + integer, intent(out) :: num_facets + integer, intent(out) :: max_fnodes + integer, intent(out) :: ierr + character(len=256) :: local_surface + + call bridge_chars_to_string(surface, length, local_surface, ierr) + if (ierr .ne. 0) return + call fem_get_cpl_surf_count_name(trim(local_surface), num_facets, & + max_fnodes, ierr) +end subroutine femlite_f_get_cpl_surf_cnt_c + subroutine femlite_f_get_contact_summary(active_count, work_step, work_total, & max_penetration, force_norm, ierr) use fem_api @@ -647,6 +675,185 @@ subroutine femlite_f_get_facets(nfacet, max_fnodes, facet_id, elem_id, & normal, area, ierr) end subroutine femlite_f_get_facets +subroutine femlite_f_get_cpl_surf_facets(nfacet, max_fnodes, facet_id, & + elem_id, face_id, facet_nnode, & + facet_conn, x, vnod, normal, area, & + ierr) + use fem_api + use fem_precision + implicit none + integer, intent(in) :: nfacet + integer, intent(in) :: max_fnodes + integer, intent(out) :: facet_id(nfacet) + integer, intent(out) :: elem_id(nfacet) + integer, intent(out) :: face_id(nfacet) + integer, intent(out) :: facet_nnode(nfacet) + integer, intent(out) :: facet_conn(max_fnodes,nfacet) + real(rk), intent(out) :: x(NDIM,max_fnodes,nfacet) + real(rk), intent(out) :: vnod(NDIM,max_fnodes,nfacet) + real(rk), intent(out) :: normal(NDIM,nfacet) + real(rk), intent(out) :: area(nfacet) + integer, intent(out) :: ierr + + call fem_get_coupling_surface_facets(nfacet, max_fnodes, facet_id, & + elem_id, face_id, facet_nnode, & + facet_conn, x, vnod, normal, area, ierr) +end subroutine femlite_f_get_cpl_surf_facets + +subroutine femlite_f_get_cpl_surf_ex(nfacet, max_fnodes, facet_id, & + surface_id, elem_id, face_id, & + facet_nnode, facet_conn, x, vnod, & + normal, area, material_id, section_id, & + valid_flags, shell_thick, mass_area, ierr) + use fem_api + use fem_precision + implicit none + integer, intent(in) :: nfacet + integer, intent(in) :: max_fnodes + integer, intent(out) :: facet_id(nfacet) + integer, intent(out) :: surface_id(nfacet) + integer, intent(out) :: elem_id(nfacet) + integer, intent(out) :: face_id(nfacet) + integer, intent(out) :: facet_nnode(nfacet) + integer, intent(out) :: facet_conn(max_fnodes,nfacet) + real(rk), intent(out) :: x(NDIM,max_fnodes,nfacet) + real(rk), intent(out) :: vnod(NDIM,max_fnodes,nfacet) + real(rk), intent(out) :: normal(NDIM,nfacet) + real(rk), intent(out) :: area(nfacet) + integer, intent(out) :: material_id(nfacet) + integer, intent(out) :: section_id(nfacet) + integer, intent(out) :: valid_flags(nfacet) + real(rk), intent(out) :: shell_thick(nfacet) + real(rk), intent(out) :: mass_area(nfacet) + integer, intent(out) :: ierr + + call fem_get_cpl_surf_facets_ex(nfacet, max_fnodes, facet_id, & + surface_id, elem_id, face_id, facet_nnode, facet_conn, x, vnod, normal, & + area, material_id, section_id, valid_flags, shell_thick, mass_area, ierr) +end subroutine femlite_f_get_cpl_surf_ex + +subroutine femlite_f_get_cpl_surf_ex_c(surface, length, nfacet, max_fnodes, & + facet_id, surface_id, elem_id, & + face_id, facet_nnode, facet_conn, & + x, vnod, normal, area, material_id, & + section_id, valid_flags, shell_thick, & + mass_area, ierr) + use fem_api + use fem_precision + use fem_fortran_bridge_utils + implicit none + integer, intent(in) :: length + character(len=1), intent(in) :: surface(length) + integer, intent(in) :: nfacet + integer, intent(in) :: max_fnodes + integer, intent(out) :: facet_id(nfacet) + integer, intent(out) :: surface_id(nfacet) + integer, intent(out) :: elem_id(nfacet) + integer, intent(out) :: face_id(nfacet) + integer, intent(out) :: facet_nnode(nfacet) + integer, intent(out) :: facet_conn(max_fnodes,nfacet) + real(rk), intent(out) :: x(NDIM,max_fnodes,nfacet) + real(rk), intent(out) :: vnod(NDIM,max_fnodes,nfacet) + real(rk), intent(out) :: normal(NDIM,nfacet) + real(rk), intent(out) :: area(nfacet) + integer, intent(out) :: material_id(nfacet) + integer, intent(out) :: section_id(nfacet) + integer, intent(out) :: valid_flags(nfacet) + real(rk), intent(out) :: shell_thick(nfacet) + real(rk), intent(out) :: mass_area(nfacet) + integer, intent(out) :: ierr + character(len=256) :: local_surface + + call bridge_chars_to_string(surface, length, local_surface, ierr) + if (ierr .ne. 0) return + call fem_get_cpl_surf_ex_name(trim(local_surface), nfacet, max_fnodes, & + facet_id, surface_id, elem_id, face_id, facet_nnode, facet_conn, x, & + vnod, normal, area, material_id, section_id, valid_flags, shell_thick, & + mass_area, ierr) +end subroutine femlite_f_get_cpl_surf_ex_c + +subroutine femlite_f_clear_cpl_loads(ierr) + use fem_api + implicit none + integer, intent(out) :: ierr + + call fem_clear_coupling_loads(ierr) +end subroutine femlite_f_clear_cpl_loads + +subroutine femlite_f_apply_cpl_nodes(num_loads, node_ids, forces, & + source_ids, load_mode, valid_flags, & + ierr) + use fem_api + use fem_precision + implicit none + integer, intent(in) :: num_loads + integer, intent(in) :: node_ids(num_loads) + real(rk), intent(in) :: forces(NDIM,num_loads) + integer, intent(in) :: source_ids(num_loads) + integer, intent(in) :: load_mode(num_loads) + integer, intent(in) :: valid_flags(num_loads) + integer, intent(out) :: ierr + + call fem_apply_coupling_nodal_forces(num_loads, node_ids, forces, & + source_ids, load_mode, valid_flags, & + ierr) +end subroutine femlite_f_apply_cpl_nodes + +subroutine femlite_f_get_cpl_load_diag(total_force, total_moment, & + accepted_count, rejected_nonfinite, & + rejected_unknown_node, & + rejected_capacity, ierr) + use fem_api + use fem_precision + implicit none + real(rk), intent(out) :: total_force(NDIM) + real(rk), intent(out) :: total_moment(NDIM) + integer, intent(out) :: accepted_count + integer, intent(out) :: rejected_nonfinite + integer, intent(out) :: rejected_unknown_node + integer, intent(out) :: rejected_capacity + integer, intent(out) :: ierr + + call fem_get_coupling_load_diag(total_force, total_moment, accepted_count, & + rejected_nonfinite, rejected_unknown_node, & + rejected_capacity, ierr) +end subroutine femlite_f_get_cpl_load_diag + +subroutine femlite_f_get_cpl_source_diag(source_id, total_force, & + total_moment, accepted_count, ierr) + use fem_api + use fem_precision + implicit none + integer, intent(in) :: source_id + real(rk), intent(out) :: total_force(NDIM) + real(rk), intent(out) :: total_moment(NDIM) + integer, intent(out) :: accepted_count + integer, intent(out) :: ierr + + call fem_get_cpl_source_load_diag(source_id, total_force, total_moment, & + accepted_count, ierr) +end subroutine femlite_f_get_cpl_source_diag + +subroutine femlite_f_write_cpl_reports(load_file, load_len, surface_file, & + surface_len, ierr) + use fem_api + use fem_fortran_bridge_utils + implicit none + integer, intent(in) :: load_len + integer, intent(in) :: surface_len + character(len=1), intent(in) :: load_file(load_len) + character(len=1), intent(in) :: surface_file(surface_len) + integer, intent(out) :: ierr + character(len=512) :: local_load + character(len=512) :: local_surface + + call bridge_chars_to_string(load_file, load_len, local_load, ierr) + if (ierr .ne. 0) return + call bridge_chars_to_string(surface_file, surface_len, local_surface, ierr) + if (ierr .ne. 0) return + call fem_write_cpl_reports(trim(local_load), trim(local_surface), ierr) +end subroutine femlite_f_write_cpl_reports + subroutine femlite_f_apply_facet_pressure(nfacet, facet_id, pressure, ierr) use fem_api use fem_precision diff --git a/src/mesh/fem_database_surface.f90 b/src/mesh/fem_database_surface.f90 index 45bbf1f..8ac44df 100644 --- a/src/mesh/fem_database_surface.f90 +++ b/src/mesh/fem_database_surface.f90 @@ -15,6 +15,7 @@ module fem_database_surface public :: db_surface_node_count public :: db_get_surface_nodes + public :: db_surface_has_facet contains diff --git a/src/output/fem_output_vtk.f90 b/src/output/fem_output_vtk.f90 index 42caf15..35f039c 100644 --- a/src/output/fem_output_vtk.f90 +++ b/src/output/fem_output_vtk.f90 @@ -8,10 +8,56 @@ module fem_output_vtk private - public :: output_vtk_write + public :: output_vtk_write, output_vtu_write, output_vtu_reset + + integer, allocatable :: pvd_steps(:) + real(rk), allocatable :: pvd_times(:) + integer :: pvd_count = 0 + character(len=256) :: pvd_directory = ' ' contains + subroutine output_vtu_write(step, time, ierr) + integer, intent(in) :: step + real(rk), intent(in) :: time + integer, intent(out) :: ierr + character(len=512) :: filename + integer :: unit_id + integer :: ios + + call output_vtu_prepare_series() + call output_vtu_step_filename(step, filename) + unit_id = 64 + open(unit=unit_id, file=trim(filename), status='replace', & + action='write', iostat=ios) + if (ios .ne. 0) then + call error_set(FEM_ERR_OUTPUT, 'failed to open VTU output', ierr) + return + end if + + call write_vtu_file(unit_id, ios, ierr) + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + if (ios .ne. 0) then + close(unit_id) + call error_set(FEM_ERR_OUTPUT, 'failed to write VTU output', ierr) + return + end if + close(unit_id) + + call output_vtu_record_step(step, time) + call output_vtu_write_pvd(ierr) + end subroutine output_vtu_write + + subroutine output_vtu_reset() + if (allocated(pvd_steps)) deallocate(pvd_steps) + if (allocated(pvd_times)) deallocate(pvd_times) + pvd_count = 0 + pvd_directory = ' ' + end subroutine output_vtu_reset + subroutine output_vtk_write(step, ierr) integer, intent(in) :: step integer, intent(out) :: ierr @@ -52,6 +98,473 @@ contains call error_set(FEM_ERR_OUTPUT, 'failed to write VTK output', ierr) end subroutine output_vtk_write + subroutine output_vtu_prepare_series() + if (trim(pvd_directory) .ne. trim(csv_output_directory)) then + call output_vtu_reset() + pvd_directory = csv_output_directory + end if + end subroutine output_vtu_prepare_series + + subroutine output_vtu_step_filename(step, filename) + integer, intent(in) :: step + character(len=*), intent(out) :: filename + character(len=8) :: step_text + + write(step_text,'(I8.8)') step + filename = ' ' + if (trim(csv_output_directory) .eq. '.') then + filename = 'result_'//step_text//'.vtu' + else + filename = trim(csv_output_directory)//'/result_'//step_text//'.vtu' + end if + end subroutine output_vtu_step_filename + + subroutine output_vtu_pvd_filename(filename) + character(len=*), intent(out) :: filename + + filename = ' ' + if (trim(csv_output_directory) .eq. '.') then + filename = 'results.pvd' + else + filename = trim(csv_output_directory)//'/results.pvd' + end if + end subroutine output_vtu_pvd_filename + + subroutine output_vtu_record_step(step, time) + integer, intent(in) :: step + real(rk), intent(in) :: time + integer :: i + integer, allocatable :: next_steps(:) + real(rk), allocatable :: next_times(:) + + do i = 1, pvd_count + if (pvd_steps(i) .eq. step) then + pvd_times(i) = time + return + end if + end do + + allocate(next_steps(pvd_count + 1)) + allocate(next_times(pvd_count + 1)) + if (pvd_count .gt. 0) then + next_steps(1:pvd_count) = pvd_steps(1:pvd_count) + next_times(1:pvd_count) = pvd_times(1:pvd_count) + end if + next_steps(pvd_count + 1) = step + next_times(pvd_count + 1) = time + + call move_alloc(next_steps, pvd_steps) + call move_alloc(next_times, pvd_times) + pvd_count = pvd_count + 1 + end subroutine output_vtu_record_step + + subroutine output_vtu_write_pvd(ierr) + integer, intent(out) :: ierr + character(len=512) :: filename + character(len=8) :: step_text + character(len=32) :: time_text + integer :: unit_id + integer :: ios + integer :: i + + call output_vtu_pvd_filename(filename) + unit_id = 65 + open(unit=unit_id, file=trim(filename), status='replace', & + action='write', iostat=ios) + if (ios .ne. 0) then + call error_set(FEM_ERR_OUTPUT, 'failed to open PVD output', ierr) + return + end if + + write(unit_id,'(A)',iostat=ios) '' + if (ios .ne. 0) go to 900 + write(unit_id,'(A)',iostat=ios) & + '' + if (ios .ne. 0) go to 900 + write(unit_id,'(A)',iostat=ios) ' ' + if (ios .ne. 0) go to 900 + do i = 1, pvd_count + write(step_text,'(I8.8)') pvd_steps(i) + write(time_text,'(ES24.16)') pvd_times(i) + time_text = adjustl(time_text) + write(unit_id,'(A,A,A,A,A)',iostat=ios) & + ' ' + if (ios .ne. 0) go to 900 + end do + write(unit_id,'(A)',iostat=ios) ' ' + if (ios .ne. 0) go to 900 + write(unit_id,'(A)',iostat=ios) '' + if (ios .ne. 0) go to 900 + + close(unit_id) + ierr = FEM_SUCCESS + return + +900 continue + close(unit_id) + call error_set(FEM_ERR_OUTPUT, 'failed to write PVD output', ierr) + end subroutine output_vtu_write_pvd + + subroutine write_vtu_file(unit_id, ios, ierr) + integer, intent(in) :: unit_id + integer, intent(out) :: ios + integer, intent(out) :: ierr + + write(unit_id,'(A)',iostat=ios) '' + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + write(unit_id,'(A)',iostat=ios) & + '' + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + write(unit_id,'(A)',iostat=ios) ' ' + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + write(unit_id,'(A,I0,A,I0,A)',iostat=ios) & + ' ' + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + + call write_vtu_point_data(unit_id, ios) + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + call write_vtu_cell_data(unit_id, ios) + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + call write_vtu_points(unit_id, ios) + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + call write_vtu_cells(unit_id, ios, ierr) + if (ierr .ne. FEM_SUCCESS .or. ios .ne. 0) return + + write(unit_id,'(A)',iostat=ios) ' ' + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + write(unit_id,'(A)',iostat=ios) ' ' + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + write(unit_id,'(A)',iostat=ios) '' + ierr = FEM_SUCCESS + end subroutine write_vtu_file + + subroutine write_vtu_points(unit_id, ios) + integer, intent(in) :: unit_id + integer, intent(out) :: ios + integer :: node + integer :: idir + integer :: dof + real(rk) :: xyz(NDIM) + + write(unit_id,'(A)',iostat=ios) ' ' + if (ios .ne. 0) return + call write_vtu_data_array_start(unit_id, 'Float64', 'Points', 3, ios) + if (ios .ne. 0) return + do node = 1, nnode + xyz = coord0(:,node) + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) xyz(idir) = xyz(idir) + u(dof) + end do + write(unit_id,'(6X,3(ES24.16,1X))',iostat=ios) & + xyz(1), xyz(2), xyz(3) + if (ios .ne. 0) return + end do + call write_vtu_data_array_end(unit_id, ios) + if (ios .ne. 0) return + write(unit_id,'(A)',iostat=ios) ' ' + end subroutine write_vtu_points + + subroutine write_vtu_cells(unit_id, ios, ierr) + integer, intent(in) :: unit_id + integer, intent(out) :: ios + integer, intent(out) :: ierr + integer :: elem + integer :: local_node + integer :: offset + integer :: vtk_type + + write(unit_id,'(A)',iostat=ios) ' ' + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + call write_vtu_data_array_start(unit_id, 'Int32', 'connectivity', 0, ios) + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + do elem = 1, nelem + write(unit_id,'(6X)',advance='no',iostat=ios) + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + do local_node = 1, elem_nnode(elem) + write(unit_id,'(1X,I0)',advance='no',iostat=ios) & + elem_conn(local_node,elem) - 1 + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + end do + write(unit_id,*) + end do + call write_vtu_data_array_end(unit_id, ios) + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + + call write_vtu_data_array_start(unit_id, 'Int32', 'offsets', 0, ios) + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + offset = 0 + write(unit_id,'(6X)',advance='no',iostat=ios) + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + do elem = 1, nelem + offset = offset + elem_nnode(elem) + write(unit_id,'(1X,I0)',advance='no',iostat=ios) offset + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + end do + write(unit_id,*) + call write_vtu_data_array_end(unit_id, ios) + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + + call write_vtu_data_array_start(unit_id, 'UInt8', 'types', 0, ios) + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + write(unit_id,'(6X)',advance='no',iostat=ios) + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + do elem = 1, nelem + call vtk_cell_type(elem_type(elem), vtk_type, ierr) + if (ierr .ne. FEM_SUCCESS) return + write(unit_id,'(1X,I0)',advance='no',iostat=ios) vtk_type + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + end do + write(unit_id,*) + call write_vtu_data_array_end(unit_id, ios) + if (ios .ne. 0) then + ierr = FEM_SUCCESS + return + end if + + write(unit_id,'(A)',iostat=ios) ' ' + ierr = FEM_SUCCESS + end subroutine write_vtu_cells + + subroutine write_vtu_point_data(unit_id, ios) + integer, intent(in) :: unit_id + integer, intent(out) :: ios + + write(unit_id,'(A)',iostat=ios) ' ' + if (ios .ne. 0) return + call write_vtu_point_vector(unit_id, 'displacement', u, ios) + if (ios .ne. 0) return + call write_vtu_point_vector(unit_id, 'velocity', v, ios) + if (ios .ne. 0) return + call write_vtu_point_vector(unit_id, 'acceleration', a, ios) + if (ios .ne. 0) return + call write_vtu_point_vector(unit_id, 'reaction', freaction, ios) + if (ios .ne. 0) return + write(unit_id,'(A)',iostat=ios) ' ' + end subroutine write_vtu_point_data + + subroutine write_vtu_point_vector(unit_id, name, values, ios) + integer, intent(in) :: unit_id + character(len=*), intent(in) :: name + real(rk), intent(in) :: values(:) + integer, intent(out) :: ios + integer :: node + integer :: idir + integer :: dof + real(rk) :: vector(NDIM) + + call write_vtu_data_array_start(unit_id, 'Float64', name, 3, ios) + if (ios .ne. 0) return + do node = 1, nnode + vector = ZERO + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) vector(idir) = values(dof) + end do + write(unit_id,'(6X,3(ES24.16,1X))',iostat=ios) & + vector(1), vector(2), vector(3) + if (ios .ne. 0) return + end do + call write_vtu_data_array_end(unit_id, ios) + end subroutine write_vtu_point_vector + + subroutine write_vtu_cell_data(unit_id, ios) + integer, intent(in) :: unit_id + integer, intent(out) :: ios + + write(unit_id,'(A)',iostat=ios) ' ' + if (ios .ne. 0) return + call write_vtu_element_status(unit_id, ios) + if (ios .ne. 0) return + call write_vtu_cell_tensor(unit_id, 'stress', 1, ios) + if (ios .ne. 0) return + call write_vtu_cell_tensor(unit_id, 'strain', 2, ios) + if (ios .ne. 0) return + call write_vtu_cell_tensor(unit_id, 'plastic_strain', 3, ios) + if (ios .ne. 0) return + call write_vtu_cell_scalar(unit_id, 'eqps', 1, ios) + if (ios .ne. 0) return + call write_vtu_cell_scalar(unit_id, 'damage', 2, ios) + if (ios .ne. 0) return + write(unit_id,'(A)',iostat=ios) ' ' + end subroutine write_vtu_cell_data + + subroutine write_vtu_element_status(unit_id, ios) + integer, intent(in) :: unit_id + integer, intent(out) :: ios + integer :: elem + + call write_vtu_data_array_start(unit_id, 'Int32', 'element_status', 0, ios) + if (ios .ne. 0) return + write(unit_id,'(6X)',advance='no',iostat=ios) + if (ios .ne. 0) return + do elem = 1, nelem + write(unit_id,'(1X,I0)',advance='no',iostat=ios) elem_status(elem) + if (ios .ne. 0) return + end do + write(unit_id,*) + call write_vtu_data_array_end(unit_id, ios) + end subroutine write_vtu_element_status + + subroutine write_vtu_cell_tensor(unit_id, name, field_id, ios) + integer, intent(in) :: unit_id + character(len=*), intent(in) :: name + integer, intent(in) :: field_id + integer, intent(out) :: ios + integer :: elem + real(rk) :: sig(NSTRESS) + real(rk) :: eps(NSTRESS) + real(rk) :: peps(NSTRESS) + real(rk) :: eqps + real(rk) :: damage + + call write_vtu_data_array_start(unit_id, 'Float64', name, 9, ios) + if (ios .ne. 0) return + do elem = 1, nelem + call output_csv_average_element(elem, sig, eps, peps, eqps, damage) + if (field_id .eq. 1) then + call write_vtu_voigt_tensor(unit_id, sig, ios) + else if (field_id .eq. 2) then + call write_vtu_voigt_tensor(unit_id, eps, ios) + else + call write_vtu_voigt_tensor(unit_id, peps, ios) + end if + if (ios .ne. 0) return + end do + call write_vtu_data_array_end(unit_id, ios) + end subroutine write_vtu_cell_tensor + + subroutine write_vtu_voigt_tensor(unit_id, voigt, ios) + integer, intent(in) :: unit_id + real(rk), intent(in) :: voigt(NSTRESS) + integer, intent(out) :: ios + + write(unit_id,'(6X,9(ES24.16,1X))',iostat=ios) & + voigt(1), voigt(4), voigt(5), & + voigt(4), voigt(2), voigt(6), & + voigt(5), voigt(6), voigt(3) + end subroutine write_vtu_voigt_tensor + + subroutine write_vtu_cell_scalar(unit_id, name, field_id, ios) + integer, intent(in) :: unit_id + character(len=*), intent(in) :: name + integer, intent(in) :: field_id + integer, intent(out) :: ios + integer :: elem + real(rk) :: sig(NSTRESS) + real(rk) :: eps(NSTRESS) + real(rk) :: peps(NSTRESS) + real(rk) :: eqps + real(rk) :: damage + real(rk) :: value + + call write_vtu_data_array_start(unit_id, 'Float64', name, 0, ios) + if (ios .ne. 0) return + write(unit_id,'(6X)',advance='no',iostat=ios) + if (ios .ne. 0) return + do elem = 1, nelem + call output_csv_average_element(elem, sig, eps, peps, eqps, damage) + if (field_id .eq. 1) then + value = eqps + else + value = damage + end if + write(unit_id,'(1X,ES24.16)',advance='no',iostat=ios) value + if (ios .ne. 0) return + end do + write(unit_id,*) + call write_vtu_data_array_end(unit_id, ios) + end subroutine write_vtu_cell_scalar + + subroutine write_vtu_data_array_start(unit_id, value_type, name, ncomp, ios) + integer, intent(in) :: unit_id + character(len=*), intent(in) :: value_type + character(len=*), intent(in) :: name + integer, intent(in) :: ncomp + integer, intent(out) :: ios + + if (ncomp .gt. 0) then + write(unit_id,'(A,A,A,A,A,I0,A)',iostat=ios) & + ' ' + else + write(unit_id,'(A,A,A,A,A)',iostat=ios) & + ' ' + end if + end subroutine write_vtu_data_array_start + + subroutine write_vtu_data_array_end(unit_id, ios) + integer, intent(in) :: unit_id + integer, intent(out) :: ios + + write(unit_id,'(A)',iostat=ios) ' ' + end subroutine write_vtu_data_array_end + subroutine output_vtk_step_filename(step, filename) integer, intent(in) :: step character(len=*), intent(out) :: filename diff --git a/src/solver/fem_solver_static_nonlinear.f90 b/src/solver/fem_solver_static_nonlinear.f90 index b52fe3e..33216b1 100644 --- a/src/solver/fem_solver_static_nonlinear.f90 +++ b/src/solver/fem_solver_static_nonlinear.f90 @@ -273,13 +273,14 @@ contains have_predictor = .false. predictor_dt = ZERO if (use_static_predictor .and. static_predictor_saved .and. & - allocated(static_predictor_saved_du) .and. & - size(static_predictor_saved_du) .eq. ndof .and. & - current_time .gt. time_start + time_tol .and. & - abs(static_predictor_saved_dt) .gt. time_tol) then - du_last = static_predictor_saved_du - predictor_dt = static_predictor_saved_dt - have_predictor = .true. + allocated(static_predictor_saved_du)) then + if (size(static_predictor_saved_du) .eq. ndof .and. & + current_time .gt. time_start + time_tol .and. & + abs(static_predictor_saved_dt) .gt. time_tol) then + du_last = static_predictor_saved_du + predictor_dt = static_predictor_saved_dt + have_predictor = .true. + end if end if end subroutine prepare_static_predictor_arrays @@ -1006,9 +1007,12 @@ contains integer, intent(out) :: ierr integer :: stat - if (.not. allocated(static_arc_saved_du) .or. & - size(static_arc_saved_du) .ne. size(u_start)) then - if (allocated(static_arc_saved_du)) deallocate(static_arc_saved_du) + if (allocated(static_arc_saved_du)) then + if (size(static_arc_saved_du) .ne. size(u_start)) then + deallocate(static_arc_saved_du) + end if + end if + if (.not. allocated(static_arc_saved_du)) then allocate(static_arc_saved_du(size(u_start)), stat=stat) if (stat .ne. 0) then call error_set(FEM_ERR_SOLVER, & diff --git a/src/worker/fem_cclite_worker.f90 b/src/worker/fem_cclite_worker.f90 new file mode 100644 index 0000000..a539736 --- /dev/null +++ b/src/worker/fem_cclite_worker.f90 @@ -0,0 +1,225 @@ +module fem_cclite_worker + use fem_precision + use fem_error + use fem_api + implicit none + + integer, parameter :: CCL_CMD_INIT = 1 + integer, parameter :: CCL_CMD_READ_INPUT = 2 + integer, parameter :: CCL_CMD_BUILD_MODEL = 3 + integer, parameter :: CCL_CMD_EXPORT_IFACE = 4 + integer, parameter :: CCL_CMD_IMPORT_LOADS = 5 + integer, parameter :: CCL_CMD_ADVANCE = 6 + integer, parameter :: CCL_CMD_WRITE_OUTPUT = 7 + integer, parameter :: CCL_CMD_STOP = 8 + + integer, parameter :: CCL_STATE_CREATED = 0 + integer, parameter :: CCL_STATE_READY = 1 + integer, parameter :: CCL_STATE_RUNNING = 2 + integer, parameter :: CCL_STATE_DONE = 3 + integer, parameter :: CCL_STATE_ERROR = -1 + + type ccl_worker_block + integer :: seq = 0 + integer :: ack_seq = 0 + integer :: command_id = 0 + integer :: step_id = 0 + integer :: substep_id = 0 + integer :: iteration_id = 0 + integer :: worker_state = CCL_STATE_CREATED + integer :: error_code = FEM_SUCCESS + end type ccl_worker_block + + public :: CCL_CMD_INIT + public :: CCL_CMD_READ_INPUT + public :: CCL_CMD_BUILD_MODEL + public :: CCL_CMD_EXPORT_IFACE + public :: CCL_CMD_IMPORT_LOADS + public :: CCL_CMD_ADVANCE + public :: CCL_CMD_WRITE_OUTPUT + public :: CCL_CMD_STOP + public :: CCL_STATE_CREATED + public :: CCL_STATE_READY + public :: CCL_STATE_RUNNING + public :: CCL_STATE_DONE + public :: CCL_STATE_ERROR + public :: ccl_worker_block + public :: ccl_worker_run_mock + +contains + + subroutine ccl_worker_run_mock(case_file, ipc_name, log_path, output_dir, & + step_count, ierr) + character(len=*), intent(in) :: case_file + character(len=*), intent(in) :: ipc_name + character(len=*), intent(in) :: log_path + character(len=*), intent(in) :: output_dir + integer, intent(in) :: step_count + integer, intent(out) :: ierr + type(ccl_worker_block) :: block + integer :: log_unit + integer :: ios + integer :: step + integer :: nstep + character(len=256) :: local_log + + local_log = log_path + if (len_trim(local_log) .eq. 0) local_log = 'femlite_cclite_worker.log' + nstep = max(0, step_count) + + log_unit = 79 + open(unit=log_unit, file=trim(local_log), status='replace', & + action='write', iostat=ios) + if (ios .ne. 0) then + call error_set(FEM_ERR_OUTPUT, 'failed to open CCLite worker log', ierr) + return + end if + + call write_worker_header(log_unit, ipc_name, case_file, nstep) + if (len_trim(output_dir) .gt. 0) then + call fem_set_output_directory(trim(output_dir), ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + end if + + call run_command(log_unit, block, CCL_CMD_INIT, 0, case_file, output_dir, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + call run_command(log_unit, block, CCL_CMD_READ_INPUT, 0, case_file, & + output_dir, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + call run_command(log_unit, block, CCL_CMD_BUILD_MODEL, 0, case_file, & + output_dir, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + call run_command(log_unit, block, CCL_CMD_EXPORT_IFACE, 0, case_file, & + output_dir, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + + do step = 1, nstep + call run_command(log_unit, block, CCL_CMD_IMPORT_LOADS, step, & + case_file, output_dir, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + call run_command(log_unit, block, CCL_CMD_ADVANCE, step, case_file, & + output_dir, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + end do + + call run_command(log_unit, block, CCL_CMD_WRITE_OUTPUT, nstep, case_file, & + output_dir, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + call run_command(log_unit, block, CCL_CMD_STOP, nstep, case_file, & + output_dir, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + + close(log_unit) + ierr = FEM_SUCCESS + return + +900 continue + block%worker_state = CCL_STATE_ERROR + block%error_code = ierr + call write_worker_row(log_unit, block) + close(log_unit) + end subroutine ccl_worker_run_mock + + subroutine run_command(log_unit, block, command_id, step_id, case_file, & + output_dir, ierr) + integer, intent(in) :: log_unit + type(ccl_worker_block), intent(inout) :: block + integer, intent(in) :: command_id + integer, intent(in) :: step_id + character(len=*), intent(in) :: case_file + character(len=*), intent(in) :: output_dir + integer, intent(out) :: ierr + integer :: facet_count + integer :: max_nodes + real(rk) :: time + integer :: current_step + character(len=512) :: load_report + character(len=512) :: surface_report + + block%seq = block%seq + 1 + block%command_id = command_id + block%step_id = step_id + block%substep_id = 0 + block%iteration_id = 0 + block%worker_state = CCL_STATE_RUNNING + block%error_code = FEM_SUCCESS + call write_worker_row(log_unit, block) + + select case (command_id) + case (CCL_CMD_INIT) + block%worker_state = CCL_STATE_READY + ierr = FEM_SUCCESS + case (CCL_CMD_READ_INPUT) + call fem_read_inp(trim(case_file), ierr) + case (CCL_CMD_BUILD_MODEL) + call fem_build_model(ierr) + if (ierr .eq. FEM_SUCCESS) call fem_compute_lumped_mass(ierr) + case (CCL_CMD_EXPORT_IFACE) + call fem_get_coupling_surface_count(facet_count, max_nodes, ierr) + case (CCL_CMD_IMPORT_LOADS) + call fem_clear_coupling_loads(ierr) + case (CCL_CMD_ADVANCE) + call fem_advance_explicit(1.0e-12_rk, ierr) + case (CCL_CMD_WRITE_OUTPUT) + call fem_get_current_time(time, current_step, ierr) + if (ierr .eq. FEM_SUCCESS) call fem_write_output(current_step, time, ierr) + if (ierr .eq. FEM_SUCCESS) then + call build_output_path(output_dir, 'femlite_cclite_load_report.csv', & + load_report) + call build_output_path(output_dir, 'femlite_cclite_surface_report.csv', & + surface_report) + call fem_write_cpl_reports(trim(load_report), trim(surface_report), ierr) + end if + case (CCL_CMD_STOP) + block%worker_state = CCL_STATE_DONE + ierr = FEM_SUCCESS + case default + call error_set(FEM_ERR_INPUT, 'unknown CCLite worker command', ierr) + end select + + block%ack_seq = block%seq + block%error_code = ierr + if (ierr .eq. FEM_SUCCESS) then + if (command_id .ne. CCL_CMD_STOP) block%worker_state = CCL_STATE_READY + else + block%worker_state = CCL_STATE_ERROR + end if + call write_worker_row(log_unit, block) + end subroutine run_command + + subroutine build_output_path(output_dir, filename, full_path) + character(len=*), intent(in) :: output_dir + character(len=*), intent(in) :: filename + character(len=*), intent(out) :: full_path + + if (len_trim(output_dir) .eq. 0) then + full_path = trim(filename) + else + full_path = trim(output_dir) // '/' // trim(filename) + end if + end subroutine build_output_path + + subroutine write_worker_header(log_unit, ipc_name, case_file, step_count) + integer, intent(in) :: log_unit + character(len=*), intent(in) :: ipc_name + character(len=*), intent(in) :: case_file + integer, intent(in) :: step_count + + write(log_unit,'(A)') 'FemLite CCLite worker mock' + write(log_unit,'(A,A)') 'ipc_name,', trim(ipc_name) + write(log_unit,'(A,A)') 'case,', trim(case_file) + write(log_unit,'(A,I0)') 'mock_steps,', step_count + write(log_unit,'(A)') & + 'seq,ack_seq,command_id,step_id,substep_id,iteration_id,state,error_code' + end subroutine write_worker_header + + subroutine write_worker_row(log_unit, block) + integer, intent(in) :: log_unit + type(ccl_worker_block), intent(in) :: block + + write(log_unit,'(8(I0,:,","))') block%seq, block%ack_seq, & + block%command_id, block%step_id, block%substep_id, block%iteration_id, & + block%worker_state, block%error_code + end subroutine write_worker_row + +end module fem_cclite_worker diff --git a/tests/cmake/run_cclite_worker_mock.cmake b/tests/cmake/run_cclite_worker_mock.cmake new file mode 100644 index 0000000..c681a20 --- /dev/null +++ b/tests/cmake/run_cclite_worker_mock.cmake @@ -0,0 +1,124 @@ +if(NOT DEFINED FEMLITE_EXE) + message(FATAL_ERROR "FEMLITE_EXE is required") +endif() +if(NOT DEFINED SOURCE_DIR) + message(FATAL_ERROR "SOURCE_DIR is required") +endif() +if(NOT DEFINED OUT_DIR) + message(FATAL_ERROR "OUT_DIR is required") +endif() +if(NOT DEFINED STEPS) + set(STEPS 100) +endif() + +function(check_seq_ack LOG_PATH) + file(READ "${LOG_PATH}" _content) + string(REPLACE "\r\n" "\n" _content "${_content}") + string(REPLACE "\n" ";" _lines "${_content}") + set(_prev_seq 0) + set(_done_seen FALSE) + + foreach(_line IN LISTS _lines) + if(_line MATCHES "^[0-9]+,") + string(REPLACE "," ";" _fields "${_line}") + list(GET _fields 0 _seq) + list(GET _fields 1 _ack) + list(GET _fields 6 _state) + + if(_state EQUAL 2) + if(NOT _ack EQUAL _prev_seq) + message(FATAL_ERROR "worker running ack mismatch in ${_line}") + endif() + else() + if(NOT _ack EQUAL _seq) + message(FATAL_ERROR "worker completed ack mismatch in ${_line}") + endif() + endif() + + if(_state EQUAL 3) + set(_done_seen TRUE) + endif() + set(_prev_seq "${_seq}") + endif() + endforeach() + + if(NOT _done_seen) + message(FATAL_ERROR "worker STOP/DONE state was not observed") + endif() +endfunction() + +file(REMOVE_RECURSE "${OUT_DIR}") +file(MAKE_DIRECTORY "${OUT_DIR}/results") + +set(_log "${OUT_DIR}/worker_mock.log") +execute_process( + COMMAND "${FEMLITE_EXE}" + --cclite-worker + --case "${SOURCE_DIR}/examples/c3d4_one_element.inp" + --ipc-name mock + --log "${_log}" + --output-dir "${OUT_DIR}/results" + --mock-steps "${STEPS}" + WORKING_DIRECTORY "${SOURCE_DIR}" + RESULT_VARIABLE _rv + OUTPUT_VARIABLE _stdout + ERROR_VARIABLE _stderr +) +if(NOT _rv EQUAL 0) + message(FATAL_ERROR "worker mock failed: ${_stdout}\n${_stderr}") +endif() + +file(READ "${_log}" _log_text) +if(NOT _log_text MATCHES "ipc_name,mock") + message(FATAL_ERROR "worker log missed ipc_name") +endif() +if(NOT _log_text MATCHES "mock_steps,${STEPS}") + message(FATAL_ERROR "worker log missed mock_steps") +endif() +if(NOT _log_text MATCHES "1,1,1,0,0,0,1,0") + message(FATAL_ERROR "worker READY state after INIT was not observed") +endif() +if(NOT _log_text MATCHES ",8,${STEPS},0,0,3,0") + message(FATAL_ERROR "worker STOP row was not observed") +endif() +if(NOT EXISTS "${OUT_DIR}/results/results.pvd") + message(FATAL_ERROR "worker did not write PVD output") +endif() +if(NOT EXISTS "${OUT_DIR}/results/femlite_cclite_load_report.csv") + message(FATAL_ERROR "worker did not write coupling load report") +endif() +if(NOT EXISTS "${OUT_DIR}/results/femlite_cclite_surface_report.csv") + message(FATAL_ERROR "worker did not write coupling surface report") +endif() +file(READ "${OUT_DIR}/results/femlite_cclite_load_report.csv" _load_report) +if(NOT _load_report MATCHES "index,node_id,source_id,load_mode,fx,fy,fz,mx,my,mz") + message(FATAL_ERROR "coupling load report header mismatch") +endif() +file(READ "${OUT_DIR}/results/femlite_cclite_surface_report.csv" _surface_report) +if(NOT _surface_report MATCHES "source_id,count,fx,fy,fz,mx,my,mz") + message(FATAL_ERROR "coupling surface report header mismatch") +endif() + +check_seq_ack("${_log}") + +set(_bad_log "${OUT_DIR}/worker_error.log") +execute_process( + COMMAND "${FEMLITE_EXE}" + --cclite-worker + --case "${SOURCE_DIR}/tests/data/does_not_exist.inp" + --ipc-name mock_error + --log "${_bad_log}" + --output-dir "${OUT_DIR}/bad_results" + --mock-steps 1 + WORKING_DIRECTORY "${SOURCE_DIR}" + RESULT_VARIABLE _bad_rv + OUTPUT_VARIABLE _bad_stdout + ERROR_VARIABLE _bad_stderr +) +if(_bad_rv EQUAL 0) + message(FATAL_ERROR "worker error path unexpectedly succeeded") +endif() +file(READ "${_bad_log}" _bad_text) +if(NOT _bad_text MATCHES ",2,0,0,0,-1,") + message(FATAL_ERROR "worker error row was not observed") +endif() diff --git a/tests/data/c3d4_static_coupling_pull.inp b/tests/data/c3d4_static_coupling_pull.inp new file mode 100644 index 0000000..c2056e5 --- /dev/null +++ b/tests/data/c3d4_static_coupling_pull.inp @@ -0,0 +1,24 @@ +*HEADING +FemLite C3D4 static linear coupling pull verification +*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=C3D4, ELSET=EALL +1, 1, 2, 3, 4 +*MATERIAL, NAME=MAT +*DENSITY +1.0 +*ELASTIC +30.0, 0.25 +*SOLID SECTION, ELSET=EALL, MATERIAL=MAT +*BOUNDARY +1, 1, 3, 0.0 +2, 2, 3, 0.0 +3, 1, 3, 0.0 +4, 1, 3, 0.0 +*STEP +*STATIC +0.1, 1.0, 0.01, 0.2 +*END STEP diff --git a/tests/data/s3_degenerate_surface.inp b/tests/data/s3_degenerate_surface.inp new file mode 100644 index 0000000..cff52cf --- /dev/null +++ b/tests/data/s3_degenerate_surface.inp @@ -0,0 +1,19 @@ +*HEADING +Degenerate S3 surface export test +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 2.0, 0.0, 0.0 +*ELEMENT, TYPE=S3, ELSET=PLATE +1, 1, 2, 3 +*MATERIAL, NAME=MAT +*DENSITY +1000.0 +*ELASTIC +1.0E6, 0.30 +*SHELL SECTION, ELSET=PLATE, MATERIAL=MAT +0.02 +*STEP +*DYNAMIC +1.0E-7, 1.0E-7 +*END STEP diff --git a/tests/data/s3_one_element_surface.inp b/tests/data/s3_one_element_surface.inp new file mode 100644 index 0000000..795bd0c --- /dev/null +++ b/tests/data/s3_one_element_surface.inp @@ -0,0 +1,21 @@ +*HEADING +S3 one element surface export test +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 0.0, 1.0, 0.0 +*ELEMENT, TYPE=S3, ELSET=PLATE +1, 1, 2, 3 +*MATERIAL, NAME=MAT +*DENSITY +1000.0 +*ELASTIC +1.0E6, 0.30 +*SHELL SECTION, ELSET=PLATE, MATERIAL=MAT +0.02 +*SURFACE, NAME=TRI_SURF, TYPE=ELEMENT +PLATE, SPOS +*STEP +*DYNAMIC +1.0E-7, 1.0E-7 +*END STEP diff --git a/tests/data/s4_degenerate_surface.inp b/tests/data/s4_degenerate_surface.inp new file mode 100644 index 0000000..65edfdf --- /dev/null +++ b/tests/data/s4_degenerate_surface.inp @@ -0,0 +1,20 @@ +*HEADING +Degenerate S4 surface export test +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 2.0, 0.0, 0.0 +4, 3.0, 0.0, 0.0 +*ELEMENT, TYPE=S4, ELSET=PLATE +1, 1, 2, 3, 4 +*MATERIAL, NAME=MAT +*DENSITY +1000.0 +*ELASTIC +1.0E6, 0.30 +*SHELL SECTION, ELSET=PLATE, MATERIAL=MAT +0.02 +*STEP +*DYNAMIC +1.0E-7, 1.0E-7 +*END STEP diff --git a/tests/interface/test_c_api.c b/tests/interface/test_c_api.c index 676d1bb..42e270e 100644 --- a/tests/interface/test_c_api.c +++ b/tests/interface/test_c_api.c @@ -18,6 +18,24 @@ static int check_ierr(int ierr, int code) return code; } +static int file_contains(const char *path, const char *needle) +{ + FILE *fp = fopen(path, "r"); + char line[512]; + + if (fp == NULL) { + return 0; + } + while (fgets(line, (int)sizeof(line), fp) != NULL) { + if (strstr(line, needle) != NULL) { + fclose(fp); + return 1; + } + } + fclose(fp); + return 0; +} + int main(void) { int ierr = 0; @@ -61,6 +79,15 @@ int main(void) int shell_sample_count = -1; int shell_last_element = -1; int node_ids[1] = {2}; + int source_ids[1] = {42}; + int load_mode[1] = {1}; + int valid_flags[1] = {1}; + int bad_node_ids[1] = {9999}; + int coupling_accepted = -1; + int coupling_rejected_nonfinite = -1; + int coupling_rejected_unknown = -1; + int coupling_rejected_capacity = -1; + int source_accepted = -1; int elem_nodes[4]; int facet_id[8]; int elem_id[8]; @@ -72,6 +99,11 @@ int main(void) double normal[3 * 8]; double area[8]; double force[3] = {5.0, 1.0, -2.0}; + double bad_force[3] = {NAN, 0.0, 0.0}; + double coupling_force[3] = {0.0, 0.0, 0.0}; + double coupling_moment[3] = {0.0, 0.0, 0.0}; + double source_force[3] = {0.0, 0.0, 0.0}; + double source_moment[3] = {0.0, 0.0, 0.0}; double disp[3] = {0.0, 0.0, 0.0}; double vel[3] = {0.0, 0.0, 0.0}; double acc[3] = {0.0, 0.0, 0.0}; @@ -130,7 +162,7 @@ int main(void) ierr = femlite_get_version(&major, &minor, &patch); if (ierr != 0) return check_ierr(ierr, 2); - if (major != 0 || minor != 6 || patch != 3) return 3; + if (major != 0 || minor != 6 || patch != 4) return 3; ierr = femlite_read_inp("examples/c3d4_one_element.inp"); if (ierr != 0) return check_ierr(ierr, 4); @@ -401,16 +433,123 @@ int main(void) if (ierr != 0) return check_ierr(ierr, 11); if (nfacet != 4 || max_face_nodes != 3) return 12; + ierr = femlite_get_coupling_surface_count(&nfacet, &max_face_nodes); + if (ierr != 0) return check_ierr(ierr, 1301); + if (nfacet != 4 || max_face_nodes != 3) return 1302; + ierr = femlite_get_boundary_facets(8, 8, facet_id, elem_id, face_id, facet_nnode, facet_conn, x, vnod, normal, area); if (ierr != 0) return check_ierr(ierr, 13); if (facet_id[0] != 1 || elem_id[0] != 1 || facet_nnode[0] != 3) return 14; - ierr = femlite_clear_sph_forces(); + ierr = femlite_get_coupling_surface_facets(8, 8, facet_id, elem_id, + face_id, facet_nnode, + facet_conn, x, vnod, normal, + area); + if (ierr != 0) return check_ierr(ierr, 1303); + if (facet_id[0] != 1 || elem_id[0] != 1 || facet_nnode[0] != 3) return 1304; + + ierr = femlite_clear_coupling_loads(); if (ierr != 0) return check_ierr(ierr, 15); - ierr = femlite_apply_sph_nodal_forces(1, node_ids, force); + ierr = femlite_apply_coupling_nodal_forces(0, node_ids, force, source_ids, + load_mode, valid_flags); + if (ierr != 0) return check_ierr(ierr, 1313); + ierr = femlite_get_coupling_load_diagnostics( + coupling_force, coupling_moment, &coupling_accepted, + &coupling_rejected_nonfinite, &coupling_rejected_unknown, + &coupling_rejected_capacity); + if (ierr != 0) return check_ierr(ierr, 1314); + if (coupling_accepted != 0 || coupling_rejected_nonfinite != 0 || + coupling_rejected_unknown != 0 || coupling_rejected_capacity != 0) { + return 1315; + } + ierr = femlite_apply_coupling_nodal_forces(1, node_ids, force, source_ids, + load_mode, valid_flags); if (ierr != 0) return check_ierr(ierr, 16); + ierr = femlite_get_coupling_load_diagnostics( + coupling_force, coupling_moment, &coupling_accepted, + &coupling_rejected_nonfinite, &coupling_rejected_unknown, + &coupling_rejected_capacity); + if (ierr != 0) return check_ierr(ierr, 1305); + if (coupling_accepted != 1 || coupling_rejected_nonfinite != 0 || + coupling_rejected_unknown != 0 || coupling_rejected_capacity != 0) { + return 1306; + } + if (fabs(coupling_force[0] - 5.0) > 1.0e-16 || + fabs(coupling_force[1] - 1.0) > 1.0e-16 || + fabs(coupling_force[2] + 2.0) > 1.0e-16) return 1307; + if (fabs(coupling_moment[0]) > 1.0e-16 || + fabs(coupling_moment[1] - 2.0) > 1.0e-16 || + fabs(coupling_moment[2] - 1.0) > 1.0e-16) return 1308; + ierr = femlite_get_coupling_source_load_diagnostics( + 42, source_force, source_moment, &source_accepted); + if (ierr != 0) return check_ierr(ierr, 1320); + if (source_accepted != 1) return 1321; + if (fabs(source_force[0] - 5.0) > 1.0e-16 || + fabs(source_force[1] - 1.0) > 1.0e-16 || + fabs(source_force[2] + 2.0) > 1.0e-16) return 1322; + if (fabs(source_moment[0]) > 1.0e-16 || + fabs(source_moment[1] - 2.0) > 1.0e-16 || + fabs(source_moment[2] - 1.0) > 1.0e-16) return 1323; + ierr = femlite_get_coupling_source_load_diagnostics( + 7, source_force, source_moment, &source_accepted); + if (ierr != 0) return check_ierr(ierr, 1324); + if (source_accepted != 0 || + fabs(source_force[0]) > 1.0e-16 || + fabs(source_force[1]) > 1.0e-16 || + fabs(source_force[2]) > 1.0e-16) return 1325; + ierr = femlite_write_coupling_reports( + "build-wsl-debug/c_api_coupling_load_report.csv", + "build-wsl-debug/c_api_coupling_surface_report.csv"); + if (ierr != 0) return check_ierr(ierr, 1326); + if (!file_contains("build-wsl-debug/c_api_coupling_load_report.csv", + "index,node_id,source_id,load_mode,fx,fy,fz,mx,my,mz")) { + return 1327; + } + if (!file_contains("build-wsl-debug/c_api_coupling_load_report.csv", + "1,2,42,1")) { + return 1328; + } + if (!file_contains("build-wsl-debug/c_api_coupling_surface_report.csv", + "source_id,count,fx,fy,fz,mx,my,mz")) { + return 1329; + } + if (!file_contains("build-wsl-debug/c_api_coupling_surface_report.csv", + "42,1")) { + return 1330; + } + ierr = femlite_clear_coupling_loads(); + if (ierr != 0) return check_ierr(ierr, 1331); + ierr = femlite_get_coupling_load_diagnostics( + coupling_force, coupling_moment, &coupling_accepted, + &coupling_rejected_nonfinite, &coupling_rejected_unknown, + &coupling_rejected_capacity); + if (ierr != 0) return check_ierr(ierr, 1332); + if (coupling_accepted != 0 || coupling_rejected_nonfinite != 0 || + coupling_rejected_unknown != 0 || coupling_rejected_capacity != 0) { + return 1333; + } + ierr = femlite_apply_coupling_nodal_forces(1, node_ids, force, source_ids, + load_mode, valid_flags); + if (ierr != 0) return check_ierr(ierr, 1334); + ierr = femlite_apply_coupling_nodal_forces(1, node_ids, bad_force, + source_ids, load_mode, + valid_flags); + if (ierr == 0) return 1309; + ierr = femlite_apply_coupling_nodal_forces(1, bad_node_ids, force, + source_ids, load_mode, + valid_flags); + if (ierr == 0) return 1310; + ierr = femlite_get_coupling_load_diagnostics( + coupling_force, coupling_moment, &coupling_accepted, + &coupling_rejected_nonfinite, &coupling_rejected_unknown, + &coupling_rejected_capacity); + if (ierr != 0) return check_ierr(ierr, 1311); + if (coupling_accepted != 1 || coupling_rejected_nonfinite != 1 || + coupling_rejected_unknown != 1 || coupling_rejected_capacity != 0) { + return 1312; + } ierr = femlite_advance_explicit(1.0e-8); if (ierr != 0) return check_ierr(ierr, 17); diff --git a/tests/interface/test_cclite_ipc_adapter.c b/tests/interface/test_cclite_ipc_adapter.c new file mode 100644 index 0000000..449f063 --- /dev/null +++ b/tests/interface/test_cclite_ipc_adapter.c @@ -0,0 +1,232 @@ +#include "femlite_cclite_ipc.h" + +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#endif + +static void fail(int code, int ierr) +{ + fprintf(stderr, "test_cclite_ipc_adapter failed at %d: ierr=%d\n", + code, ierr); + exit(1); +} + +static void check_layout(void) +{ + femlite_cclite_command_block block; + char name[64]; + int ierr; + + if (femlite_cclite_command_block_size() != sizeof(block)) { + fail(10, 0); + } + if (offsetof(femlite_cclite_command_block, version) != 0) { + fail(11, 0); + } + if (offsetof(femlite_cclite_command_block, error_code) != + 8 * sizeof(int32_t)) { + fail(12, 0); + } + + femlite_cclite_init_command_block(&block); + if (block.version != FEMLITE_CCL_IPC_VERSION || block.seq != 0 || + block.ack_seq != 0 || block.error_code != 0) { + fail(13, 0); + } + + ierr = femlite_cclite_format_name("Local\\FemLite", "COMMAND", name, + sizeof(name)); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + fail(14, ierr); + } + if (strcmp(name, "Local\\FemLite_COMMAND") != 0) { + fail(15, 0); + } + ierr = femlite_cclite_format_name("abc", "def", name, 4); + if (ierr != FEMLITE_CCL_IPC_ERR_CAPACITY) { + fail(16, ierr); + } +} + +static void check_ipc_wrappers(void) +{ +#ifdef _WIN32 + char prefix[96]; + char map_name[128]; + char command_event_name[128]; + char done_event_name[128]; + femlite_cclite_map map; + femlite_cclite_event command_event; + femlite_cclite_event done_event; + femlite_cclite_command_block *block; + femlite_cclite_command_block received_block; + HANDLE created_map; + HANDLE created_command_event; + HANDLE created_done_event; + DWORD wait_rc; + int ierr; + + snprintf(prefix, sizeof(prefix), "Local\\FemLiteIpcTest_%lu", + (unsigned long)GetCurrentProcessId()); + ierr = femlite_cclite_format_name(prefix, "COMMAND_BLOCK", map_name, + sizeof(map_name)); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + fail(100, ierr); + } + ierr = femlite_cclite_format_name(prefix, "COMMAND_EVENT", + command_event_name, + sizeof(command_event_name)); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + fail(101, ierr); + } + ierr = femlite_cclite_format_name(prefix, "DONE_EVENT", done_event_name, + sizeof(done_event_name)); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + fail(102, ierr); + } + + created_map = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, + PAGE_READWRITE, 0, + (DWORD)sizeof(*block), map_name); + if (created_map == NULL) { + fail(103, (int)GetLastError()); + } + created_command_event = CreateEventA(NULL, TRUE, FALSE, + command_event_name); + if (created_command_event == NULL) { + fail(104, (int)GetLastError()); + } + created_done_event = CreateEventA(NULL, TRUE, FALSE, done_event_name); + if (created_done_event == NULL) { + fail(105, (int)GetLastError()); + } + + ierr = femlite_cclite_open_shared_memory(map_name, sizeof(*block), &map); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + fail(106, ierr); + } + block = (femlite_cclite_command_block *)map.view; + femlite_cclite_init_command_block(block); + block->seq = 7; + block->command_id = 1; + + ierr = femlite_cclite_open_event(command_event_name, &command_event); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + fail(107, ierr); + } + ierr = femlite_cclite_open_event(done_event_name, &done_event); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + fail(108, ierr); + } + + ierr = femlite_cclite_wait_event(&command_event, 1); + if (ierr != FEMLITE_CCL_IPC_ERR_TIMEOUT) { + fail(109, ierr); + } + if (!SetEvent(created_command_event)) { + fail(110, (int)GetLastError()); + } + ierr = femlite_cclite_wait_command(&command_event, &map, 1000, + &received_block); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + fail(111, ierr); + } + if (received_block.seq != 7 || received_block.command_id != 1) { + fail(115, 0); + } + wait_rc = WaitForSingleObject(created_command_event, 0); + if (wait_rc != WAIT_OBJECT_0) { + fail(112, (int)wait_rc); + } + + received_block.ack_seq = received_block.seq; + received_block.worker_state = 3; + ierr = femlite_cclite_write_done(&done_event, &map, &received_block); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + fail(113, ierr); + } + if (block->ack_seq != 7 || block->worker_state != 3) { + fail(116, 0); + } + wait_rc = WaitForSingleObject(created_done_event, 0); + if (wait_rc != WAIT_OBJECT_0) { + fail(114, (int)wait_rc); + } + + femlite_cclite_close_event(&done_event); + femlite_cclite_close_event(&command_event); + femlite_cclite_close_shared_memory(&map); + CloseHandle(created_done_event); + CloseHandle(created_command_event); + CloseHandle(created_map); +#else + femlite_cclite_map map; + femlite_cclite_event event; + int ierr; + + ierr = femlite_cclite_open_shared_memory("missing", sizeof(int), &map); + if (ierr != FEMLITE_CCL_IPC_ERR_UNSUPPORTED) { + fail(200, ierr); + } + ierr = femlite_cclite_open_event("missing", &event); + if (ierr != FEMLITE_CCL_IPC_ERR_UNSUPPORTED) { + fail(201, ierr); + } +#endif +} + +static void check_mock_transport(void) +{ + femlite_cclite_transport transport; + femlite_cclite_command_block command; + femlite_cclite_command_block received; + femlite_cclite_command_block done; + int ierr; + + ierr = femlite_cclite_transport_init_mock(&transport); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + fail(300, ierr); + } + ierr = femlite_cclite_transport_receive(&transport, 0, &received); + if (ierr != FEMLITE_CCL_IPC_ERR_TIMEOUT) { + fail(301, ierr); + } + + femlite_cclite_init_command_block(&command); + command.seq = 4; + command.command_id = FEMLITE_CCL_CMD_STOP; + ierr = femlite_cclite_transport_mock_push(&transport, &command); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + fail(302, ierr); + } + ierr = femlite_cclite_transport_receive(&transport, 0, &received); + if (ierr != FEMLITE_CCL_IPC_SUCCESS || received.seq != 4 || + received.command_id != FEMLITE_CCL_CMD_STOP) { + fail(303, ierr); + } + received.ack_seq = received.seq; + received.worker_state = FEMLITE_CCL_STATE_DONE; + ierr = femlite_cclite_transport_complete(&transport, &received); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + fail(304, ierr); + } + ierr = femlite_cclite_transport_mock_get_done(&transport, &done); + if (ierr != FEMLITE_CCL_IPC_SUCCESS || done.ack_seq != 4 || + done.worker_state != FEMLITE_CCL_STATE_DONE) { + fail(305, ierr); + } + femlite_cclite_transport_close(&transport); +} + +int main(void) +{ + check_layout(); + check_ipc_wrappers(); + check_mock_transport(); + return 0; +} diff --git a/tests/interface/test_cclite_ipc_worker_loop.c b/tests/interface/test_cclite_ipc_worker_loop.c new file mode 100644 index 0000000..6ba6c8e --- /dev/null +++ b/tests/interface/test_cclite_ipc_worker_loop.c @@ -0,0 +1,302 @@ +#include "femlite_c_api.h" +#include "femlite_cclite_ipc.h" + +#include +#include +#include + +#ifdef _WIN32 +#include +#endif + +static void fail(int code, int ierr) +{ + fprintf(stderr, "test_cclite_ipc_worker_loop failed at %d: ierr=%d\n", + code, ierr); + exit(1); +} + +#ifdef _WIN32 +static int handle_command(femlite_cclite_command_block *block) +{ + int ierr = FEMLITE_CCL_IPC_SUCCESS; + int nfacet = 0; + int max_nodes = 0; + double time = 0.0; + int step = 0; + + block->worker_state = FEMLITE_CCL_STATE_RUNNING; + block->error_code = 0; + + switch (block->command_id) { + case FEMLITE_CCL_CMD_INIT: + ierr = FEMLITE_CCL_IPC_SUCCESS; + break; + case FEMLITE_CCL_CMD_READ_INPUT: + ierr = femlite_read_inp("examples/c3d4_one_element.inp"); + break; + case FEMLITE_CCL_CMD_BUILD_MODEL: + ierr = femlite_build_model(); + if (ierr == 0) { + ierr = femlite_compute_lumped_mass(); + } + break; + case FEMLITE_CCL_CMD_EXPORT_IFACE: + ierr = femlite_get_coupling_surface_count(&nfacet, &max_nodes); + if (ierr == 0 && (nfacet <= 0 || max_nodes <= 0)) { + ierr = FEMLITE_CCL_IPC_ERR_INPUT; + } + break; + case FEMLITE_CCL_CMD_IMPORT_LOADS: + ierr = femlite_clear_coupling_loads(); + break; + case FEMLITE_CCL_CMD_ADVANCE: + ierr = femlite_advance_explicit(1.0e-12); + break; + case FEMLITE_CCL_CMD_WRITE_OUTPUT: + ierr = femlite_get_current_time(&time, &step); + break; + case FEMLITE_CCL_CMD_STOP: + ierr = FEMLITE_CCL_IPC_SUCCESS; + break; + default: + ierr = FEMLITE_CCL_IPC_ERR_INPUT; + break; + } + + block->ack_seq = block->seq; + block->error_code = ierr; + if (ierr == 0) { + block->worker_state = (block->command_id == FEMLITE_CCL_CMD_STOP) ? + FEMLITE_CCL_STATE_DONE : FEMLITE_CCL_STATE_READY; + } else { + block->worker_state = FEMLITE_CCL_STATE_ERROR; + } + return ierr; +} + +static void send_command(void *driver_view, HANDLE command_event, + int seq, int command_id, int step_id) +{ + femlite_cclite_command_block block; + + femlite_cclite_init_command_block(&block); + block.seq = seq; + block.command_id = command_id; + block.step_id = step_id; + memcpy(driver_view, &block, sizeof(block)); + + if (!SetEvent(command_event)) { + fail(1000 + seq, (int)GetLastError()); + } +} + +static void worker_receive_and_reply(femlite_cclite_map *map, + femlite_cclite_event *command_event, + femlite_cclite_event *done_event, + int expected_seq, int expected_cmd) +{ + femlite_cclite_command_block block; + int ierr; + + ierr = femlite_cclite_wait_command(command_event, map, 1000, &block); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + fail(2000 + expected_seq, ierr); + } + if (block.seq != expected_seq || block.command_id != expected_cmd) { + fail(3000 + expected_seq, 0); + } + + ierr = handle_command(&block); + if (expected_cmd != 999 && ierr != 0) { + fail(4000 + expected_seq, ierr); + } + + ierr = femlite_cclite_write_done(done_event, map, &block); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + fail(5000 + expected_seq, ierr); + } +} + +static void worker_transport_receive_and_reply( + femlite_cclite_transport *transport, int expected_seq, int expected_cmd) +{ + femlite_cclite_command_block block; + int ierr; + + ierr = femlite_cclite_transport_receive(transport, 1000, &block); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + fail(10000 + expected_seq, ierr); + } + if (block.seq != expected_seq || block.command_id != expected_cmd) { + fail(11000 + expected_seq, 0); + } + ierr = handle_command(&block); + if (expected_cmd != 999 && ierr != 0) { + fail(12000 + expected_seq, ierr); + } + ierr = femlite_cclite_transport_complete(transport, &block); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + fail(13000 + expected_seq, ierr); + } +} + +static void driver_wait_done(void *driver_view, HANDLE done_event, + int expected_seq, int expect_done_state) +{ + femlite_cclite_command_block *block; + DWORD rc; + + rc = WaitForSingleObject(done_event, 1000); + if (rc != WAIT_OBJECT_0) { + fail(6000 + expected_seq, (int)rc); + } + + block = (femlite_cclite_command_block *)driver_view; + if (block->ack_seq != expected_seq) { + fail(7000 + expected_seq, block->ack_seq); + } + if (expect_done_state && block->worker_state != expect_done_state) { + fail(8000 + expected_seq, block->worker_state); + } +} + +static int run_one_ipc_command(void *driver_view, HANDLE driver_command_event, + HANDLE driver_done_event, + femlite_cclite_transport *worker_transport, + int seq, int command_id, int step_id, + int expected_state) +{ + if (!ResetEvent(driver_command_event)) { + fail(9000 + seq, (int)GetLastError()); + } + if (!ResetEvent(driver_done_event)) { + fail(9100 + seq, (int)GetLastError()); + } + + send_command(driver_view, driver_command_event, seq, command_id, step_id); + worker_transport_receive_and_reply(worker_transport, seq, command_id); + driver_wait_done(driver_view, driver_done_event, seq, expected_state); + return seq + 1; +} + +static void run_windows_loop(void) +{ + char prefix[96]; + char map_name[128]; + char command_event_name[128]; + char done_event_name[128]; + HANDLE driver_map; + HANDLE driver_command_event; + HANDLE driver_done_event; + void *driver_view; + femlite_cclite_transport worker_transport; + int ierr; + int seq = 1; + int step; + + snprintf(prefix, sizeof(prefix), "Local\\FemLiteIpcLoop_%lu", + (unsigned long)GetCurrentProcessId()); + if (femlite_cclite_format_name(prefix, "COMMAND_BLOCK", map_name, + sizeof(map_name)) != 0) { + fail(10, 0); + } + if (femlite_cclite_format_name(prefix, "COMMAND_EVENT", + command_event_name, + sizeof(command_event_name)) != 0) { + fail(11, 0); + } + if (femlite_cclite_format_name(prefix, "DONE_EVENT", done_event_name, + sizeof(done_event_name)) != 0) { + fail(12, 0); + } + + driver_map = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, + PAGE_READWRITE, 0, + sizeof(femlite_cclite_command_block), + map_name); + if (driver_map == NULL) { + fail(13, (int)GetLastError()); + } + driver_view = MapViewOfFile(driver_map, FILE_MAP_ALL_ACCESS, 0, 0, + sizeof(femlite_cclite_command_block)); + if (driver_view == NULL) { + fail(14, (int)GetLastError()); + } + driver_command_event = CreateEventA(NULL, TRUE, FALSE, + command_event_name); + if (driver_command_event == NULL) { + fail(15, (int)GetLastError()); + } + driver_done_event = CreateEventA(NULL, TRUE, FALSE, done_event_name); + if (driver_done_event == NULL) { + fail(16, (int)GetLastError()); + } + + ierr = femlite_cclite_transport_open_windows(prefix, &worker_transport); + if (ierr != FEMLITE_CCL_IPC_SUCCESS) { + fail(17, ierr); + } + + ierr = femlite_initialize(); + if (ierr != 0) { + fail(20, ierr); + } + + seq = run_one_ipc_command(driver_view, driver_command_event, + driver_done_event, &worker_transport, + seq, FEMLITE_CCL_CMD_INIT, 0, + FEMLITE_CCL_STATE_READY); + seq = run_one_ipc_command(driver_view, driver_command_event, + driver_done_event, &worker_transport, + seq, FEMLITE_CCL_CMD_READ_INPUT, 0, + FEMLITE_CCL_STATE_READY); + seq = run_one_ipc_command(driver_view, driver_command_event, + driver_done_event, &worker_transport, + seq, FEMLITE_CCL_CMD_BUILD_MODEL, 0, + FEMLITE_CCL_STATE_READY); + seq = run_one_ipc_command(driver_view, driver_command_event, + driver_done_event, &worker_transport, + seq, FEMLITE_CCL_CMD_EXPORT_IFACE, 0, + FEMLITE_CCL_STATE_READY); + + for (step = 1; step <= 100; ++step) { + seq = run_one_ipc_command(driver_view, driver_command_event, + driver_done_event, &worker_transport, + seq, FEMLITE_CCL_CMD_IMPORT_LOADS, step, + FEMLITE_CCL_STATE_READY); + seq = run_one_ipc_command(driver_view, driver_command_event, + driver_done_event, &worker_transport, + seq, FEMLITE_CCL_CMD_ADVANCE, step, + FEMLITE_CCL_STATE_READY); + } + + seq = run_one_ipc_command(driver_view, driver_command_event, + driver_done_event, &worker_transport, + seq, FEMLITE_CCL_CMD_WRITE_OUTPUT, 100, + FEMLITE_CCL_STATE_READY); + seq = run_one_ipc_command(driver_view, driver_command_event, + driver_done_event, &worker_transport, + seq, FEMLITE_CCL_CMD_STOP, 100, + FEMLITE_CCL_STATE_DONE); + + ierr = femlite_finalize(); + if (ierr != 0) { + fail(21, ierr); + } + + femlite_cclite_transport_close(&worker_transport); + CloseHandle(driver_done_event); + CloseHandle(driver_command_event); + UnmapViewOfFile(driver_view); + CloseHandle(driver_map); +} +#endif + +int main(void) +{ +#ifdef _WIN32 + run_windows_loop(); +#endif + return 0; +} diff --git a/tests/interface/test_facet_nodal_c_api.c b/tests/interface/test_facet_nodal_c_api.c index 0851ef2..3b7bfcc 100644 --- a/tests/interface/test_facet_nodal_c_api.c +++ b/tests/interface/test_facet_nodal_c_api.c @@ -7,6 +7,8 @@ #define MAXN 8 #define NDIM 3 +static int f_index3(int idir, int inode, int ifacet, int max_nodes); + static int check_ierr(int ierr, int code) { int err_code = 0; @@ -50,6 +52,18 @@ static int check_vec(const double actual[NDIM], return 0; } +static int check_normal_unit(const double normal[NDIM * MAXF], + int facet, + int code) +{ + double nx = normal[f_index3(0, 0, facet, 1)]; + double ny = normal[f_index3(1, 0, facet, 1)]; + double nz = normal[f_index3(2, 0, facet, 1)]; + double norm = sqrt(nx * nx + ny * ny + nz * nz); + + return check_close(norm, 1.0, code); +} + static int f_index3(int idir, int inode, int ifacet, int max_nodes) { return idir + NDIM * (inode + max_nodes * ifacet); @@ -99,6 +113,13 @@ static int setup_model(int *nfacet, double area[MAXF]) { int ierr = 0; + int surface_id[MAXF] = {0}; + int material_id[MAXF] = {0}; + int section_id[MAXF] = {0}; + int valid_flags[MAXF] = {0}; + double shell_thickness[MAXF] = {0.0}; + double mass_per_area[MAXF] = {0.0}; + int facet = 0; ierr = femlite_initialize(); if (ierr != 0) return check_ierr(ierr, 100); @@ -114,6 +135,18 @@ static int setup_model(int *nfacet, facet_nnode, facet_conn, x, vnod, normal, area); if (ierr != 0) return check_ierr(ierr, 105); + ierr = femlite_get_coupling_surface_facets_ex( + MAXF, MAXN, facet_id, surface_id, elem_id, face_id, facet_nnode, + facet_conn, x, vnod, normal, area, material_id, section_id, + valid_flags, shell_thickness, mass_per_area); + if (ierr != 0) return check_ierr(ierr, 106); + for (facet = 0; facet < *nfacet; ++facet) { + if (surface_id[facet] != 0) return 107; + if (material_id[facet] != 1 || section_id[facet] != 1) return 108; + if (valid_flags[facet] != 1) return 109; + if (check_close(shell_thickness[facet], 0.0, 110) != 0) return 110; + if (check_close(mass_per_area[facet], 0.0, 111) != 0) return 111; + } return 0; } @@ -129,12 +162,18 @@ static int run_valid_load_case(void) int facet_id[MAXF] = {0}; int elem_id[MAXF] = {0}; int face_id[MAXF] = {0}; + int surface_id[MAXF] = {0}; + int material_id[MAXF] = {0}; + int section_id[MAXF] = {0}; + int valid_flags[MAXF] = {0}; int facet_nnode[MAXF] = {0}; int facet_conn[MAXN * MAXF] = {0}; double x[NDIM * MAXN * MAXF] = {0.0}; double vnod[NDIM * MAXN * MAXF] = {0.0}; double normal[NDIM * MAXF] = {0.0}; double area[MAXF] = {0.0}; + double shell_thickness[MAXF] = {0.0}; + double mass_per_area[MAXF] = {0.0}; double node_loads[NDIM * MAXN] = {0.0}; double total_force[NDIM] = {0.0}; double load[NDIM] = {2.0, -3.0, 4.0}; @@ -214,6 +253,248 @@ static int run_error_case(void) return 0; } +static int run_quad_surface_case(void) +{ + int ierr = 0; + int nfacet = 0; + int max_nodes = 0; + int facet_id[MAXF] = {0}; + int surface_id[MAXF] = {0}; + int elem_id[MAXF] = {0}; + int face_id[MAXF] = {0}; + int material_id[MAXF] = {0}; + int section_id[MAXF] = {0}; + int valid_flags[MAXF] = {0}; + int facet_nnode[MAXF] = {0}; + int facet_conn[MAXN * MAXF] = {0}; + double x[NDIM * MAXN * MAXF] = {0.0}; + double vnod[NDIM * MAXN * MAXF] = {0.0}; + double normal[NDIM * MAXF] = {0.0}; + double area[MAXF] = {0.0}; + double shell_thickness[MAXF] = {0.0}; + double mass_per_area[MAXF] = {0.0}; + + ierr = femlite_initialize(); + if (ierr != 0) return check_ierr(ierr, 300); + ierr = femlite_read_inp("examples/s4_one_element.inp"); + if (ierr != 0) return check_ierr(ierr, 301); + ierr = femlite_build_model(); + if (ierr != 0) return check_ierr(ierr, 302); + + ierr = femlite_get_coupling_surface_count(&nfacet, &max_nodes); + if (ierr != 0) return check_ierr(ierr, 303); + if (nfacet != 1 || max_nodes != 4) return 304; + + ierr = femlite_get_coupling_surface_facets( + 0, MAXN, facet_id, elem_id, face_id, facet_nnode, facet_conn, + x, vnod, normal, area); + if (ierr == 0) return 313; + ierr = femlite_get_coupling_surface_facets( + MAXF, 3, facet_id, elem_id, face_id, facet_nnode, facet_conn, + x, vnod, normal, area); + if (ierr == 0) return 314; + + ierr = femlite_get_coupling_surface_facets( + MAXF, MAXN, facet_id, elem_id, face_id, facet_nnode, facet_conn, + x, vnod, normal, area); + if (ierr != 0) return check_ierr(ierr, 305); + if (facet_id[0] != 1 || elem_id[0] != 1 || facet_nnode[0] != 4) return 306; + if (facet_conn[0] != 1 || facet_conn[1] != 2 || + facet_conn[2] != 3 || facet_conn[3] != 4) return 307; + if (check_close(x[f_index3(0, 1, 0, MAXN)], 1.0, 308) != 0) return 308; + if (check_close(x[f_index3(1, 2, 0, MAXN)], 1.0, 309) != 0) return 309; + if (check_close(vnod[f_index3(0, 0, 0, MAXN)], 0.0, 310) != 0) return 310; + if (area[0] <= 0.0) return 311; + if (check_normal_unit(normal, 0, 321) != 0) return 321; + + ierr = femlite_get_coupling_surface_facets_ex( + MAXF, MAXN, facet_id, surface_id, elem_id, face_id, facet_nnode, + facet_conn, x, vnod, normal, area, material_id, section_id, + valid_flags, shell_thickness, mass_per_area); + if (ierr != 0) return check_ierr(ierr, 315); + if (surface_id[0] != 0) return 316; + if (material_id[0] != 1 || section_id[0] != 1) return 317; + if (valid_flags[0] != 1) return 318; + if (check_close(shell_thickness[0], 0.01, 319) != 0) return 319; + if (check_close(mass_per_area[0], 78.5, 320) != 0) return 320; + if (check_normal_unit(normal, 0, 322) != 0) return 322; + + ierr = femlite_finalize(); + if (ierr != 0) return check_ierr(ierr, 312); + return 0; +} + +static int run_degenerate_surface_case(const char *input_file, int expected_nodes, + int code_base) +{ + int ierr = 0; + int nfacet = 0; + int max_nodes = 0; + int facet_id[MAXF] = {0}; + int surface_id[MAXF] = {0}; + int elem_id[MAXF] = {0}; + int face_id[MAXF] = {0}; + int material_id[MAXF] = {0}; + int section_id[MAXF] = {0}; + int valid_flags[MAXF] = {0}; + int facet_nnode[MAXF] = {0}; + int facet_conn[MAXN * MAXF] = {0}; + double x[NDIM * MAXN * MAXF] = {0.0}; + double vnod[NDIM * MAXN * MAXF] = {0.0}; + double normal[NDIM * MAXF] = {0.0}; + double area[MAXF] = {0.0}; + double shell_thickness[MAXF] = {0.0}; + double mass_per_area[MAXF] = {0.0}; + + ierr = femlite_initialize(); + if (ierr != 0) return check_ierr(ierr, code_base); + ierr = femlite_read_inp(input_file); + if (ierr != 0) return check_ierr(ierr, code_base + 1); + ierr = femlite_build_model(); + if (ierr != 0) return check_ierr(ierr, code_base + 2); + + ierr = femlite_get_coupling_surface_count(&nfacet, &max_nodes); + if (ierr != 0) return check_ierr(ierr, code_base + 3); + if (nfacet != 1 || max_nodes != expected_nodes) return code_base + 4; + + ierr = femlite_get_coupling_surface_facets_ex( + MAXF, MAXN, facet_id, surface_id, elem_id, face_id, facet_nnode, + facet_conn, x, vnod, normal, area, material_id, section_id, + valid_flags, shell_thickness, mass_per_area); + if (ierr != 0) return check_ierr(ierr, code_base + 5); + if (valid_flags[0] != 0) return code_base + 6; + if (check_close(area[0], 0.0, code_base + 7) != 0) return code_base + 7; + if (check_close(normal[f_index3(0, 0, 0, 1)], 0.0, code_base + 11) != 0) { + return code_base + 11; + } + if (check_close(shell_thickness[0], 0.02, code_base + 8) != 0) { + return code_base + 8; + } + if (check_close(mass_per_area[0], 20.0, code_base + 9) != 0) { + return code_base + 9; + } + + ierr = femlite_finalize(); + if (ierr != 0) return check_ierr(ierr, code_base + 10); + return 0; +} + +static int run_tri_surface_case(void) +{ + int ierr = 0; + int nfacet = 0; + int max_nodes = 0; + int facet_id[MAXF] = {0}; + int surface_id[MAXF] = {0}; + int elem_id[MAXF] = {0}; + int face_id[MAXF] = {0}; + int material_id[MAXF] = {0}; + int section_id[MAXF] = {0}; + int valid_flags[MAXF] = {0}; + int facet_nnode[MAXF] = {0}; + int facet_conn[MAXN * MAXF] = {0}; + double x[NDIM * MAXN * MAXF] = {0.0}; + double vnod[NDIM * MAXN * MAXF] = {0.0}; + double normal[NDIM * MAXF] = {0.0}; + double area[MAXF] = {0.0}; + double shell_thickness[MAXF] = {0.0}; + double mass_per_area[MAXF] = {0.0}; + + ierr = femlite_initialize(); + if (ierr != 0) return check_ierr(ierr, 700); + ierr = femlite_read_inp("tests/data/s3_one_element_surface.inp"); + if (ierr != 0) return check_ierr(ierr, 701); + ierr = femlite_build_model(); + if (ierr != 0) return check_ierr(ierr, 702); + + ierr = femlite_get_coupling_surface_count_for_name("TRI_SURF", + &nfacet, &max_nodes); + if (ierr != 0) return check_ierr(ierr, 703); + if (nfacet != 1 || max_nodes != 3) return 704; + + ierr = femlite_get_coupling_surface_facets_ex_for_name( + "TRI_SURF", MAXF, MAXN, facet_id, surface_id, elem_id, face_id, + facet_nnode, facet_conn, x, vnod, normal, area, material_id, + section_id, valid_flags, shell_thickness, mass_per_area); + if (ierr != 0) return check_ierr(ierr, 705); + if (surface_id[0] != 1 || elem_id[0] != 1 || facet_nnode[0] != 3) { + return 706; + } + if (valid_flags[0] != 1) return 707; + if (check_close(area[0], 0.5, 708) != 0) return 708; + if (check_close(shell_thickness[0], 0.02, 709) != 0) return 709; + if (check_close(mass_per_area[0], 20.0, 710) != 0) return 710; + if (check_normal_unit(normal, 0, 711) != 0) return 711; + + ierr = femlite_finalize(); + if (ierr != 0) return check_ierr(ierr, 712); + return 0; +} + +static int run_surface_filter_case(void) +{ + int ierr = 0; + int nfacet = 0; + int max_nodes = 0; + int facet_id[MAXF] = {0}; + int surface_id[MAXF] = {0}; + int elem_id[MAXF] = {0}; + int face_id[MAXF] = {0}; + int material_id[MAXF] = {0}; + int section_id[MAXF] = {0}; + int valid_flags[MAXF] = {0}; + int facet_nnode[MAXF] = {0}; + int facet_conn[MAXN * MAXF] = {0}; + double x[NDIM * MAXN * MAXF] = {0.0}; + double vnod[NDIM * MAXN * MAXF] = {0.0}; + double normal[NDIM * MAXF] = {0.0}; + double area[MAXF] = {0.0}; + double shell_thickness[MAXF] = {0.0}; + double mass_per_area[MAXF] = {0.0}; + + ierr = femlite_initialize(); + if (ierr != 0) return check_ierr(ierr, 600); + ierr = femlite_read_inp("tests/data/s4_contact_pair_surface.inp"); + if (ierr != 0) return check_ierr(ierr, 601); + ierr = femlite_build_model(); + if (ierr != 0) return check_ierr(ierr, 602); + + ierr = femlite_get_coupling_surface_count(&nfacet, &max_nodes); + if (ierr != 0) return check_ierr(ierr, 603); + if (nfacet != 2 || max_nodes != 4) return 604; + + ierr = femlite_get_coupling_surface_count_for_name("MASTER_SURF", + &nfacet, &max_nodes); + if (ierr != 0) return check_ierr(ierr, 605); + if (nfacet != 1 || max_nodes != 4) return 606; + ierr = femlite_get_coupling_surface_facets_ex_for_name( + "MASTER_SURF", MAXF, MAXN, facet_id, surface_id, elem_id, face_id, + facet_nnode, facet_conn, x, vnod, normal, area, material_id, + section_id, valid_flags, shell_thickness, mass_per_area); + if (ierr != 0) return check_ierr(ierr, 607); + if (surface_id[0] != 1 || elem_id[0] != 1 || face_id[0] != 1) return 608; + if (valid_flags[0] != 1) return 609; + + ierr = femlite_get_coupling_surface_count_for_name("SLAVE_SURF", + &nfacet, &max_nodes); + if (ierr != 0) return check_ierr(ierr, 610); + if (nfacet != 1 || max_nodes != 4) return 611; + ierr = femlite_get_coupling_surface_facets_ex_for_name( + "SLAVE_SURF", MAXF, MAXN, facet_id, surface_id, elem_id, face_id, + facet_nnode, facet_conn, x, vnod, normal, area, material_id, + section_id, valid_flags, shell_thickness, mass_per_area); + if (ierr != 0) return check_ierr(ierr, 612); + if (surface_id[0] != 2 || elem_id[0] != 2 || face_id[0] != 1) return 613; + + ierr = femlite_get_coupling_surface_count_for_name("NO_SUCH_SURFACE", + &nfacet, &max_nodes); + if (ierr == 0) return 614; + + ierr = femlite_finalize(); + if (ierr != 0) return check_ierr(ierr, 615); + return 0; +} + int main(void) { int failed = 0; @@ -222,5 +503,17 @@ int main(void) if (failed != 0) return failed; failed = run_error_case(); if (failed != 0) return failed; + failed = run_quad_surface_case(); + if (failed != 0) return failed; + failed = run_degenerate_surface_case( + "tests/data/s3_degenerate_surface.inp", 3, 400); + if (failed != 0) return failed; + failed = run_degenerate_surface_case( + "tests/data/s4_degenerate_surface.inp", 4, 500); + if (failed != 0) return failed; + failed = run_surface_filter_case(); + if (failed != 0) return failed; + failed = run_tri_surface_case(); + if (failed != 0) return failed; return 0; } diff --git a/tests/module/test_implicit_inp.f90 b/tests/module/test_implicit_inp.f90 index 6e2efef..d2da6f2 100644 --- a/tests/module/test_implicit_inp.f90 +++ b/tests/module/test_implicit_inp.f90 @@ -34,6 +34,7 @@ program test_implicit_inp call check_implicit_linear_compat() call check_static_linear_inp() + call check_static_cpl_rhs() call check_static_patch_file('tests/data/c3d8_static_patch.inp', 2, 0.01_rk, 60) call check_static_patch_file('tests/data/c3d10_static_patch.inp', 2, 0.01_rk, 80) call check_static_patch_file('tests/data/c3d20_static_patch.inp', 2, 0.01_rk, 100) @@ -312,6 +313,75 @@ contains if (ierr .ne. 0) call fail(54) end subroutine check_static_linear_inp + subroutine check_static_cpl_rhs() + integer :: ierr + integer :: iter + integer :: accepted_count + integer :: rejected_nonfinite + integer :: rejected_unknown_node + integer :: rejected_capacity + integer :: node_ids(1) + integer :: source_ids(1) + integer :: load_mode(1) + integer :: valid_flags(1) + real(rk) :: res_norm + real(rk) :: forces(NDIM,1) + real(rk) :: total_force(NDIM) + real(rk) :: total_moment(NDIM) + real(rk) :: displacement(NDIM) + real(rk) :: velocity(NDIM) + real(rk) :: acceleration(NDIM) + + call fem_initialize(ierr) + if (ierr .ne. 0) call fail(1360) + + call fem_read_inp('tests/data/c3d4_static_coupling_pull.inp', ierr) + if (ierr .ne. 0) call fail(1361) + + call fem_build_model(ierr) + if (ierr .ne. 0) call fail(1362) + + call fem_clear_coupling_loads(ierr) + if (ierr .ne. 0) call fail(1363) + + node_ids(1) = 2 + source_ids(1) = 77 + load_mode(1) = 1 + valid_flags(1) = 1 + forces(:,1) = ZERO + forces(1,1) = 5.0_rk + call fem_apply_coupling_nodal_forces(1, node_ids, forces, source_ids, & + load_mode, valid_flags, ierr) + if (ierr .ne. 0) call fail(1364) + + call fem_get_coupling_load_diag(total_force, total_moment, accepted_count, & + rejected_nonfinite, rejected_unknown_node, & + rejected_capacity, ierr) + if (ierr .ne. 0) call fail(1365) + if (accepted_count .ne. 1 .or. rejected_nonfinite .ne. 0 .or. & + rejected_unknown_node .ne. 0 .or. rejected_capacity .ne. 0) then + call fail(1366) + end if + call check_close(total_force(1), 5.0_rk, 1367) + call check_close(total_force(2), ZERO, 1368) + call check_close(total_force(3), ZERO, 1369) + call check_close(maxval(abs(total_moment)), ZERO, 1370) + + call fem_solve_static_linear(50, 1.0e-12_rk, iter, res_norm, ierr) + if (ierr .ne. 0) call fail(1371) + if (iter .le. 0 .or. iter .gt. 5) call fail(1372) + if (res_norm .gt. TOL) call fail(1373) + + call fem_get_node_kinematics(2, displacement, velocity, acceleration, ierr) + if (ierr .ne. 0) call fail(1374) + call check_close(displacement(1), 5.0_rk / 6.0_rk, 1375) + call check_close(displacement(2), ZERO, 1376) + call check_close(displacement(3), ZERO, 1377) + + call fem_finalize(ierr) + if (ierr .ne. 0) call fail(1378) + end subroutine check_static_cpl_rhs + subroutine check_buckling_step_inp() integer :: ierr diff --git a/tests/module/test_vtk_output.f90 b/tests/module/test_vtk_output.f90 index bd65374..da986d7 100644 --- a/tests/module/test_vtk_output.f90 +++ b/tests/module/test_vtk_output.f90 @@ -9,8 +9,10 @@ program test_vtk_output integer :: ierr character(len=*), parameter :: OUT_DIR = & 'vtk_output_auto_dir/nested' - character(len=*), parameter :: VTK_FILE = & - 'vtk_output_auto_dir/nested/result_00000007.vtk' + character(len=*), parameter :: VTU_FILE = & + 'vtk_output_auto_dir/nested/result_00000007.vtu' + character(len=*), parameter :: PVD_FILE = & + 'vtk_output_auto_dir/nested/results.pvd' call setup_c3d4(ierr) if (ierr .ne. FEM_SUCCESS) call fail(1) @@ -22,23 +24,25 @@ program test_vtk_output call output_csv_set_directory(OUT_DIR, ierr) if (ierr .ne. FEM_SUCCESS) call fail(2) - call output_vtk_write(7, ierr) + call output_vtu_write(7, 0.25_rk, ierr) if (ierr .ne. FEM_SUCCESS) call fail(3) - call require_contains(VTK_FILE, 'DATASET UNSTRUCTURED_GRID', 4) - call require_contains(VTK_FILE, 'POINTS', 5) - call require_contains(VTK_FILE, 'CELLS', 6) - call require_contains(VTK_FILE, 'CELL_TYPES', 7) - call require_contains(VTK_FILE, 'VECTORS displacement double', 8) - call require_contains(VTK_FILE, 'VECTORS velocity double', 9) - call require_contains(VTK_FILE, 'VECTORS acceleration double', 10) - call require_contains(VTK_FILE, 'SCALARS element_status int 1', 11) - call require_contains(VTK_FILE, 'TENSORS stress double', 12) - call require_contains(VTK_FILE, 'TENSORS strain double', 13) - call require_contains(VTK_FILE, 'SCALARS eqps double 1', 14) + call require_contains(VTU_FILE, '', 6) + call require_contains(VTU_FILE, 'Name="displacement"', 7) + call require_contains(VTU_FILE, 'Name="velocity"', 8) + call require_contains(VTU_FILE, 'Name="acceleration"', 9) + call require_contains(VTU_FILE, 'Name="element_status"', 10) + call require_contains(VTU_FILE, 'Name="stress"', 11) + call require_contains(VTU_FILE, 'Name="strain"', 12) + call require_contains(VTU_FILE, 'Name="eqps"', 13) + call require_contains(VTU_FILE, '', 14) + call require_contains(PVD_FILE, '` 目录,通用测试辅助可放在 `tools/testing`。 -- 生成的 CSV、VTK、restart、临时报告和缓存文件不得提交。 +- 生成的 CSV、VTU/PVD、restart、临时报告和缓存文件不得提交。 - 新增工具时必须在相关示例 README 或 `docs/11_benchmark_matrix.md` 中说明用途、运行命令和验收指标。 ## 常见子目录 - `tools/benchmarks`:benchmark 编排程序。 - `tools/testing`:CTest、示例 smoke 和源码门禁辅助脚本。 +- `tools/release_to_cclite.ps1`:将当前 FemLite 自动构建并发布到 `..\CCLite\FEMLite`,填充 `cpu_release`、`cpu_debug`、`docs` 和 `examples`,并重写目标 examples 的运行脚本路径。 + +## 发布到 CCLite + +默认用法: + +```bat +tools\release_to_cclite.bat +``` + +也可以直接运行 PowerShell 脚本: + +```powershell +.\tools\release_to_cclite.ps1 +``` + +脚本默认使用 `C:\gcc-16.1.0-64`,构建 `build-cclite-release` 和 `build-cclite-debug`,然后同步到 `..\CCLite\FEMLite`。若只想复用已有构建并重新拷贝,可使用: + +```powershell +.\tools\release_to_cclite.ps1 -SkipBuild +``` + +若当前系统禁止直接运行 `.ps1`,使用 `tools\release_to_cclite.bat -SkipBuild` 即可。 ## 边界 -工具目录不是公共 API。对外稳定接口以 `docs/05_interface_control_document.md` 和 `include/femlite_c_api.h` 为准。 \ No newline at end of file +工具目录不是公共 API。对外稳定接口以 `docs/05_interface_control_document.md` 和 `include/femlite_c_api.h` 为准。 diff --git a/tools/benchmarks/strength_reduction_benchmark.f90 b/tools/benchmarks/strength_reduction_benchmark.f90 index c89c1ea..6ed4707 100644 --- a/tools/benchmarks/strength_reduction_benchmark.f90 +++ b/tools/benchmarks/strength_reduction_benchmark.f90 @@ -9,7 +9,7 @@ program strength_reduction_benchmark MC_STATE_REQUESTED_STEPS, MC_STATE_DRIVER_STATUS, & MC_STATE_BRANCH_MASK, MC_STATE_MAX_YIELD, MC_STATE_MAX_PLASTIC_STEP use fem_output_csv, only: output_csv_set_directory - use fem_output_vtk, only: output_vtk_write + use fem_output_vtk, only: output_vtu_write use fem_strength_reduction_driver use sr_benchmark_cli, only: MATERIAL_CASE_COUNT, & nonlinear_chain_options, nonlinear_options_default, & @@ -17,7 +17,7 @@ program strength_reduction_benchmark use sr_benchmark_report, only: mc_struct_diag, & write_cloud_progress_record, write_nonlinear_chain_record, & write_nonlinear_summary, write_nonlinear_cloud_summary, & - write_overall_summary, vtk_result_basename, flow_rule_name, join_path + write_overall_summary, field_result_basename, flow_rule_name, join_path implicit none integer, parameter :: NALPHA = 4 @@ -760,7 +760,7 @@ contains real(rk) :: linear_residual integer :: converged_i logical :: converged - character(len=32) :: vtk_name + character(len=32) :: field_name type(mc_struct_diag) :: mc_diag converged = .false. @@ -794,19 +794,19 @@ contains accepted_time .ge. ONE - 1.0e-12_rk) converged_i = 1 call output_csv_set_directory(trim(dir), ierr) if (ierr .ne. FEM_SUCCESS) return - call output_vtk_write(nl%cloud_step, ierr) + call output_vtu_write(nl%cloud_step, accepted_time, ierr) if (ierr .ne. FEM_SUCCESS) return call fem_get_model_size(node_count, element_count, dof_count, ierr) if (ierr .ne. FEM_SUCCESS) return - vtk_name = vtk_result_basename(nl%cloud_step) + field_name = field_result_basename(nl%cloud_step) summary_id = 98 call open_csv(summary_id, & join_path(dir, 'strength_reduction_cloud_summary.csv'), ierr) if (ierr .ne. FEM_SUCCESS) return call write_nonlinear_cloud_summary(summary_id, nl, factor, target, & - vtk_name, node_count, element_count, & + field_name, node_count, element_count, & solve_ierr, converged_i, & conv_status, failure_code, cutbacks, & iter_total, correction_norm, last_dt, & @@ -902,7 +902,7 @@ contains target_time, diag%accepted_time, & diag, ierr) if (ierr .ne. FEM_SUCCESS) return - call write_cloud_vtk_if_advanced(nl, accepted_before, time_tol, ierr) + call write_cloud_field_if_advanced(nl, accepted_before, time_tol, ierr) if (ierr .ne. FEM_SUCCESS) return call update_cloud_dt(nl, segment, segment_iter, accepted_before, & time_tol, dt_goal, dt_min, dt_max, & @@ -1010,7 +1010,7 @@ contains target_time, diag%accepted_time, & diag, ierr) if (ierr .ne. FEM_SUCCESS) return - call write_cloud_vtk_if_advanced(nl, accepted_before, time_tol, ierr) + call write_cloud_field_if_advanced(nl, accepted_before, time_tol, ierr) if (ierr .ne. FEM_SUCCESS) then static_ramp_load = .true. return @@ -1284,7 +1284,7 @@ contains if (ierr .eq. FEM_SUCCESS) call error_clear(ierr) return end if - call output_vtk_write(nl%cloud_step, ierr) + call output_vtu_write(nl%cloud_step, current_time, ierr) if (ierr .ne. FEM_SUCCESS) then static_ramp_load = .true. return @@ -1465,18 +1465,18 @@ contains call error_clear(ierr) end subroutine append_cloud_progress_row - subroutine write_cloud_vtk_if_advanced(nl, accepted_before, time_tol, ierr) + subroutine write_cloud_field_if_advanced(nl, accepted_before, time_tol, ierr) type(nonlinear_chain_options), intent(in) :: nl real(rk), intent(in) :: accepted_before real(rk), intent(in) :: time_tol integer, intent(out) :: ierr if (current_time .gt. accepted_before + time_tol) then - call output_vtk_write(nl%cloud_step, ierr) + call output_vtu_write(nl%cloud_step, current_time, ierr) if (ierr .ne. FEM_SUCCESS) return end if call error_clear(ierr) - end subroutine write_cloud_vtk_if_advanced + end subroutine write_cloud_field_if_advanced subroutine update_cloud_dt(nl, segment, segment_iter, accepted_before, & time_tol, dt_goal, dt_min, dt_max, plastic_share, & diff --git a/tools/benchmarks/strength_reduction_benchmark_cli.f90 b/tools/benchmarks/strength_reduction_benchmark_cli.f90 index 7ddf49e..faa55e8 100644 --- a/tools/benchmarks/strength_reduction_benchmark_cli.f90 +++ b/tools/benchmarks/strength_reduction_benchmark_cli.f90 @@ -58,9 +58,9 @@ contains write(*,'(A)') ' --nl-sr-tol VALUE FOS bracket tolerance' write(*,'(A)') ' --nl-disp-limit VALUE displacement stop criterion' write(*,'(A)') ' --nl-plastic-limit VALUE plastic integration point fraction limit' - write(*,'(A)') ' --nl-write-cloud run one target-FOS nonlinear trial and write VTK' + write(*,'(A)') ' --nl-write-cloud run one target-FOS nonlinear trial and write VTU/PVD' write(*,'(A)') ' --nl-cloud-factor VALUE target cloud strength factor; default PDF FOS' - write(*,'(A)') ' --nl-cloud-step N VTK step id for target cloud; default 1525' + write(*,'(A)') ' --nl-cloud-step N VTU step id for target cloud; default 1525' write(*,'(A)') ' --nl-no-cutback disable nonlinear load-step cutback' write(*,'(A)') ' --nl-no-line-search disable nonlinear line search' end subroutine write_usage diff --git a/tools/benchmarks/strength_reduction_benchmark_report.f90 b/tools/benchmarks/strength_reduction_benchmark_report.f90 index 326df16..4b5b8bd 100644 --- a/tools/benchmarks/strength_reduction_benchmark_report.f90 +++ b/tools/benchmarks/strength_reduction_benchmark_report.f90 @@ -30,7 +30,7 @@ module sr_benchmark_report public :: write_mc_summary_fields public :: write_overall_summary public :: cloud_status_name - public :: vtk_result_basename + public :: field_result_basename public :: flow_rule_name public :: join_path @@ -197,7 +197,7 @@ contains end subroutine write_nonlinear_summary subroutine write_nonlinear_cloud_summary(unit_id, nl, factor, target, & - vtk_name, node_count, element_count, & + field_name, node_count, element_count, & solve_ierr, converged_i, & conv_status, failure_code, cutbacks, & iter_total, correction_norm, last_dt, & @@ -211,7 +211,7 @@ contains type(nonlinear_chain_options), intent(in) :: nl real(rk), intent(in) :: factor real(rk), intent(in) :: target - character(len=*), intent(in) :: vtk_name + character(len=*), intent(in) :: field_name integer, intent(in) :: node_count integer, intent(in) :: element_count integer, intent(in) :: solve_ierr @@ -246,8 +246,8 @@ contains write(unit_id,'(A,F10.4)') 'alpha_deg,', nl%alpha write(unit_id,'(A,F10.4)') 'factor,', factor write(unit_id,'(A,F10.4)') 'reference_fos,', target - write(unit_id,'(A,A)') 'vtk_file,', trim(vtk_name) - write(unit_id,'(A,I0)') 'vtk_step,', nl%cloud_step + write(unit_id,'(A,A)') 'field_file,', trim(field_name) + write(unit_id,'(A,I0)') 'field_step,', nl%cloud_step write(unit_id,'(A,I0)') 'nodes,', node_count write(unit_id,'(A,I0)') 'elements,', element_count write(unit_id,'(A,I0)') 'solver_ierr,', solve_ierr @@ -397,13 +397,13 @@ contains end if end function cloud_status_name - character(len=32) function vtk_result_basename(step) + character(len=32) function field_result_basename(step) integer, intent(in) :: step character(len=8) :: step_text write(step_text,'(I8.8)') step - vtk_result_basename = 'result_'//step_text//'.vtk' - end function vtk_result_basename + field_result_basename = 'result_'//step_text//'.vtu' + end function field_result_basename character(len=32) function flow_rule_name(material_case) integer, intent(in) :: material_case diff --git a/tools/release_to_cclite.bat b/tools/release_to_cclite.bat new file mode 100644 index 0000000..9c1577c --- /dev/null +++ b/tools/release_to_cclite.bat @@ -0,0 +1,5 @@ +@echo off +setlocal + +powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0release_to_cclite.ps1" %* +exit /b %ERRORLEVEL% diff --git a/tools/release_to_cclite.ps1 b/tools/release_to_cclite.ps1 new file mode 100644 index 0000000..9722c34 --- /dev/null +++ b/tools/release_to_cclite.ps1 @@ -0,0 +1,207 @@ +param( + [string]$TargetRoot = "..\CCLite\FEMLite", + [string]$CCliteRoot = "..\CCLite", + [string]$GccRoot = "C:\gcc-16.1.0-64", + [string]$ReleaseBuildDir = "build-cclite-release", + [string]$DebugBuildDir = "build-cclite-debug", + [switch]$SkipBuild +) + +$ErrorActionPreference = "Stop" + +function Resolve-OrCreateDirectory { + param([string]$Path) + if (-not (Test-Path -LiteralPath $Path)) { + New-Item -ItemType Directory -Force -Path $Path | Out-Null + } + return (Resolve-Path -LiteralPath $Path).Path +} + +function Assert-UnderDirectory { + param( + [string]$Child, + [string]$Parent + ) + $childFull = [System.IO.Path]::GetFullPath($Child) + $parentFull = [System.IO.Path]::GetFullPath($Parent) + if (-not $parentFull.EndsWith([System.IO.Path]::DirectorySeparatorChar)) { + $parentFull += [System.IO.Path]::DirectorySeparatorChar + } + if (-not $childFull.StartsWith($parentFull, [System.StringComparison]::OrdinalIgnoreCase)) { + throw "Refusing to operate outside target root: $childFull" + } +} + +function Reset-Directory { + param( + [string]$Path, + [string]$AllowedRoot + ) + Assert-UnderDirectory -Child $Path -Parent $AllowedRoot + if (Test-Path -LiteralPath $Path) { + Remove-Item -LiteralPath $Path -Recurse -Force + } + New-Item -ItemType Directory -Force -Path $Path | Out-Null +} + +function Invoke-Checked { + param( + [string]$FilePath, + [string[]]$ArgumentList + ) + & $FilePath @ArgumentList + if ($LASTEXITCODE -ne 0) { + throw "Command failed with exit code ${LASTEXITCODE}: $FilePath $($ArgumentList -join ' ')" + } +} + +function Invoke-RobocopyChecked { + param( + [string]$Source, + [string]$Destination, + [string[]]$Arguments + ) + & robocopy $Source $Destination @Arguments + if ($LASTEXITCODE -gt 7) { + throw "robocopy failed with exit code $LASTEXITCODE" + } +} + +function Configure-And-Build { + param( + [string]$BuildDir, + [string]$BuildType, + [string]$GccRootPath + ) + $make = Join-Path $GccRootPath "bin\make.exe" + $gcc = Join-Path $GccRootPath "bin\gcc.exe" + $gfortran = Join-Path $GccRootPath "bin\gfortran.exe" + foreach ($tool in @($make, $gcc, $gfortran)) { + if (-not (Test-Path -LiteralPath $tool)) { + throw "Required build tool was not found: $tool" + } + } + + $configureArgs = @( + "-S", ".", + "-B", $BuildDir, + "-G", "MinGW Makefiles", + "-DCMAKE_BUILD_TYPE=$BuildType", + "-DCMAKE_MAKE_PROGRAM=$make", + "-DCMAKE_C_COMPILER=$gcc", + "-DCMAKE_Fortran_COMPILER=$gfortran" + ) + Invoke-Checked -FilePath "cmake" -ArgumentList $configureArgs + Invoke-Checked -FilePath "cmake" -ArgumentList @("--build", $BuildDir, "--parallel") +} + +function Copy-BinaryPackage { + param( + [string]$BuildDir, + [string]$Destination + ) + $binDir = Join-Path $Destination "bin" + $includeDir = Join-Path $Destination "include" + $modDir = Join-Path $includeDir "mod" + $libraryDir = Join-Path $Destination "library" + $requiredFiles = @( + (Join-Path $BuildDir "femlite.exe"), + (Join-Path $BuildDir "libFemLite.a"), + (Join-Path $BuildDir "libfemlite_c.a"), + (Join-Path $BuildDir "femlite_fortran_mangle.h") + ) + foreach ($file in $requiredFiles) { + if (-not (Test-Path -LiteralPath $file)) { + throw "Required build artifact was not found: $file. Run without -SkipBuild or pass the matching build directory." + } + } + if (-not (Get-ChildItem -Path (Join-Path $BuildDir "mod\*.mod") -ErrorAction SilentlyContinue)) { + throw "Required Fortran module files were not found in: $(Join-Path $BuildDir 'mod')" + } + + foreach ($dir in @($binDir, $includeDir, $modDir, $libraryDir)) { + New-Item -ItemType Directory -Force -Path $dir | Out-Null + } + + Copy-Item -LiteralPath (Join-Path $BuildDir "femlite.exe") -Destination (Join-Path $binDir "femlite.exe") -Force + Copy-Item -LiteralPath (Join-Path $BuildDir "libFemLite.a") -Destination (Join-Path $libraryDir "libFemLite.a") -Force + Copy-Item -LiteralPath (Join-Path $BuildDir "libfemlite_c.a") -Destination (Join-Path $libraryDir "libfemlite_c.a") -Force + Copy-Item -LiteralPath "include\femlite_c_api.h" -Destination (Join-Path $includeDir "femlite_c_api.h") -Force + Copy-Item -LiteralPath (Join-Path $BuildDir "femlite_fortran_mangle.h") -Destination (Join-Path $includeDir "femlite_fortran_mangle.h") -Force + Copy-Item -Path (Join-Path $BuildDir "mod\*.mod") -Destination $modDir -Force +} + +function Update-ExampleScripts { + param([string]$ExamplesDir) + + $scripts = Get-ChildItem -LiteralPath $ExamplesDir -Recurse -File -Filter "run_case.bat" + $sharedRunner = Get-Item -LiteralPath (Join-Path $ExamplesDir "run_example_case.bat") -ErrorAction SilentlyContinue + if ($sharedRunner) { + $scripts += $sharedRunner + } + + foreach ($script in $scripts) { + $text = [System.IO.File]::ReadAllText($script.FullName) + $text = [regex]::Replace($text, 'set "EXE=.*?"', 'set "EXE=%ROOT_DIR%\cpu_release\bin\femlite.exe"') + $text = [regex]::Replace($text, 'set "PYTHON=.*?"', 'set "PYTHON=%ROOT_DIR%\..\Python\.venv\Scripts\python.exe"') + $missingExeBlock = '(?s)if not exist "%EXE%" \(\s+echo femlite\.exe was not found\. Trying to build build-win-gcc-16\.\.\.\s+cmake --build "%ROOT_DIR%\\build-win-gcc-16" --config Release\s+if errorlevel 1 goto fail\s+\)' + $missingExeReplacement = @' +if not exist "%EXE%" ( + echo femlite.exe was not found: "%EXE%" + goto fail +) +'@ + $text = [regex]::Replace($text, $missingExeBlock, $missingExeReplacement.TrimEnd()) + $text = $text -replace "`r?`n", "`r`n" + [System.IO.File]::WriteAllText($script.FullName, $text, [System.Text.UTF8Encoding]::new($false)) + } +} + +function Assert-NoExampleOutputs { + param([string]$ExamplesDir) + $generatedExtensions = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase) + foreach ($extension in @(".csv", ".vtk", ".vtu", ".pvd", ".log", ".restart", ".rst")) { + [void]$generatedExtensions.Add($extension) + } + $resultDirs = Get-ChildItem -LiteralPath $ExamplesDir -Recurse -Directory -Filter "results" -ErrorAction SilentlyContinue + $outputFiles = Get-ChildItem -LiteralPath $ExamplesDir -Recurse -File -ErrorAction SilentlyContinue | + Where-Object { $generatedExtensions.Contains($_.Extension) } + if ($resultDirs -or $outputFiles) { + $sample = @($resultDirs + $outputFiles) | Select-Object -First 5 -ExpandProperty FullName + throw "Published examples contain generated output files: $($sample -join '; ')" + } +} + +$repoRoot = (Resolve-Path -LiteralPath ".").Path +$targetRootPath = Resolve-OrCreateDirectory -Path $TargetRoot +$ccliteRootPath = Resolve-OrCreateDirectory -Path $CCliteRoot +Assert-UnderDirectory -Child $targetRootPath -Parent $ccliteRootPath + +if (-not $SkipBuild) { + Configure-And-Build -BuildDir $ReleaseBuildDir -BuildType "Release" -GccRootPath $GccRoot + Configure-And-Build -BuildDir $DebugBuildDir -BuildType "Debug" -GccRootPath $GccRoot +} + +$releaseRoot = Join-Path $targetRootPath "cpu_release" +$debugRoot = Join-Path $targetRootPath "cpu_debug" +$docsRoot = Join-Path $targetRootPath "docs" +$examplesRoot = Join-Path $targetRootPath "examples" + +Reset-Directory -Path $releaseRoot -AllowedRoot $targetRootPath +Reset-Directory -Path $debugRoot -AllowedRoot $targetRootPath +Reset-Directory -Path $docsRoot -AllowedRoot $targetRootPath +Reset-Directory -Path $examplesRoot -AllowedRoot $targetRootPath + +Copy-BinaryPackage -BuildDir $ReleaseBuildDir -Destination $releaseRoot +Copy-BinaryPackage -BuildDir $DebugBuildDir -Destination $debugRoot + +Copy-Item -LiteralPath "README.md" -Destination (Join-Path $targetRootPath "README.md") -Force +Invoke-RobocopyChecked -Source (Join-Path $repoRoot "docs") -Destination $docsRoot -Arguments @("/E", "/NFL", "/NDL", "/NJH", "/NJS", "/NP") +Invoke-RobocopyChecked -Source (Join-Path $repoRoot "examples") -Destination $examplesRoot -Arguments @("/E", "/XD", "results", "__pycache__", "/XF", "*.csv", "*.vtk", "*.vtu", "*.pvd", "*.log", "*.rst", "*.restart", "/NFL", "/NDL", "/NJH", "/NJS", "/NP") + +Update-ExampleScripts -ExamplesDir $examplesRoot +Assert-NoExampleOutputs -ExamplesDir $examplesRoot + +Write-Host "FemLite package published to $targetRootPath" +Write-Host "Release: $releaseRoot" +Write-Host "Debug: $debugRoot" diff --git a/tools/testing/check_cli_output_dir.py b/tools/testing/check_cli_output_dir.py index dbe33eb..7fb6f71 100644 --- a/tools/testing/check_cli_output_dir.py +++ b/tools/testing/check_cli_output_dir.py @@ -59,8 +59,12 @@ def main(argv: list[str]) -> int: require(out_dir.is_dir(), "CLI did not create nested output directory") require((out_dir / "history.csv").is_file(), "history.csv was not written") require( - (out_dir / "result_00000001.vtk").is_file(), - "VTK output was not written in the requested directory", + (out_dir / "result_00000001.vtu").is_file(), + "VTU output was not written in the requested directory", + ) + require( + (out_dir / "results.pvd").is_file(), + "PVD output was not written in the requested directory", ) bad_char = '"' if os.name == "nt" else "'"