Compare commits
19
Commits
89574aca25
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a088a69900 | ||
|
|
b8a8cdf610 | ||
|
|
e2c1a4580a | ||
|
|
0dfe6facd6 | ||
|
|
0f97c0b05b | ||
|
|
5b9626c8d5 | ||
|
|
4932a87fc1 | ||
|
|
554b5418ee | ||
|
|
16b76295e6 | ||
|
|
ed2d107c59 | ||
|
|
d62beb8ad3 | ||
|
|
24b2d17003 | ||
|
|
b8eac32cd6 | ||
|
|
cb218e0d5b | ||
|
|
418e487c2f | ||
|
|
496907d41d | ||
|
|
d39f0e01b0 | ||
|
|
ddfe08cb93 | ||
|
|
3416c491ad |
@@ -11,8 +11,8 @@
|
||||
"Grep",
|
||||
"Glob",
|
||||
"Bash(make test-math:*)",
|
||||
"Bash(ls -la /home/fmq/program/tlusty/tl208-s54/rust/*)",
|
||||
"Bash(wc -l /home/fmq/program/tlusty/tl208-s54/rust/*)"
|
||||
"Bash(ls -la /home/dckj/SpectraRust/*)",
|
||||
"Bash(wc -l /home/dckj/SpectraRust/*)"
|
||||
],
|
||||
"deny": [
|
||||
"Bash(rm -rf *)",
|
||||
@@ -20,8 +20,9 @@
|
||||
"Bash(curl *)"
|
||||
],
|
||||
"additionalDirectories": [
|
||||
"/home/fmq/program/tlusty/tl208-s54/rust",
|
||||
"/home/fmq/program/tlusty/tl208-s54/tlusty"
|
||||
"/home/dckj/SpectraRust",
|
||||
"/home/dckj/SpectraRust/tlusty",
|
||||
"/home/dckj/SpectraRust/src"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"permissions": {
|
||||
"allow": [
|
||||
"mcp__codegraph__codegraph_status",
|
||||
"mcp__plugin_oh-my-claudecode_t__state_read",
|
||||
"mcp__plugin_oh-my-claudecode_t__notepad_read",
|
||||
"mcp__codegraph__codegraph_search",
|
||||
"mcp__codegraph__codegraph_files",
|
||||
"mcp__codegraph__codegraph_explore",
|
||||
"mcp__codegraph__codegraph_node"
|
||||
]
|
||||
},
|
||||
"enableAllProjectMcpServers": true,
|
||||
"enabledMcpjsonServers": [
|
||||
"codegraph"
|
||||
],
|
||||
"enabledPlugins": {
|
||||
"oh-my-claudecode@omc": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
---
|
||||
name: codegraph-guide
|
||||
description: |
|
||||
CodeGraph 辅助 Fortran→Rust 重构。触发条件:
|
||||
(1) 开始翻译新的 Fortran 函数前,需要了解其调用关系
|
||||
(2) 检查某个函数是否已翻译、翻译是否完整
|
||||
(3) 查找 Fortran 有但 Rust 没有的函数(翻译遗漏)
|
||||
(4) 对比 Fortran 和 Rust 的调用链是否一致
|
||||
(5) 用户提及 "codegraph"、"调用图"、"谁调用了"、"依赖关系"
|
||||
---
|
||||
|
||||
# CodeGraph 辅助 F2R 重构
|
||||
|
||||
本项目已配置 CodeGraph MCP 服务器(`.mcp.json`),Claude 启动时自动加载。
|
||||
不要进行全量测试,系统内存会被占满。
|
||||
|
||||
## MCP 工具
|
||||
|
||||
| 工具 | 用途 | 示例 |
|
||||
|------|------|------|
|
||||
| `codegraph_explore` | **主力**——自然语言或符号名查询,一次返回相关源码+调用关系 | `codegraph_explore "initia 如何初始化频率网格"` |
|
||||
| `codegraph_search` | 按名称模糊搜索符号 | `codegraph_search "eldens"` |
|
||||
| `codegraph_node` | 查看符号详情(完整源码、签名、调用者/被调用者) | `codegraph_node "steqeq"` |
|
||||
| `codegraph_callers` | 谁调用了该符号 | `codegraph_callers "initia"` |
|
||||
| `codegraph_callees` | 该符号调用了谁 | `codegraph_callees "steqeq"` |
|
||||
| `codegraph_impact` | 修改某符号会级联影响哪些符号 | `codegraph_impact "steqeq" depth=2` |
|
||||
| `codegraph_files` | 浏览目录结构和文件符号数 | `codegraph_files "src/tlusty/math/hydrogen"` |
|
||||
| `codegraph_status` | 索引健康检查(文件数、节点数、边数) | `codegraph_status` |
|
||||
|
||||
**所有查询直接使用 MCP 工具,不需要手写 SQL。**
|
||||
|
||||
## 命名约定
|
||||
|
||||
### 函数命名:Fortran 与 Rust 完全对应
|
||||
|
||||
所有 TLUSTY Fortran 函数在 Rust 中都有**同名小写**版本。
|
||||
|
||||
```
|
||||
Fortran: RECHECK ACCEL2 INITIA STEKEQ ELDENS
|
||||
Rust: rechck accel2 initia steqeq eldens
|
||||
```
|
||||
|
||||
### `_pure` 后缀(仅 9 个函数)
|
||||
|
||||
| `_pure` 版本 | 非-pure 版本 | 关系 |
|
||||
|-------------|-------------|------|
|
||||
| `steqeq_pure` | `steqeq` | 纯计算内核 → 回调串联完整版本 |
|
||||
| `resolv_pure` | `resolv` | 纯线性化求解 → 28 子程序编排 |
|
||||
| `start_pure` | `start` | 纯启动计算 → 带 I/O 版本 |
|
||||
| `solve_pure` | `solve` | 纯矩阵求解 → 完整求解器 |
|
||||
| `inkul_pure` | `inkul` | 纯 Kurucz 谱线 → 带文件 I/O |
|
||||
| `lemini_pure` | `lemini` | 纯 Lemke 插值 → 带表查询 |
|
||||
| `radtot_pure` | `radtot` | 纯辐射通量 → 完整辐射传输 |
|
||||
| `rayini_pure` | `rayini` | 纯瑞利散射 → 带文件读取 |
|
||||
| `iroset_pure` | `iroset` | 纯铁族设置 → 带回调完整版本 |
|
||||
|
||||
**规则**:`_pure` = 纯计算内核(可独立测试),非-pure = 完整编排包装器(匹配 Fortran 行为)。
|
||||
|
||||
## 状态文件系统
|
||||
|
||||
| 文件 | 用途 |
|
||||
|------|------|
|
||||
| `.f2r_phase` | 当前阶段:`translate` / `integrate` / `verify` / `done` |
|
||||
| `.f2r_tasks` | 当前阶段待办列表(每行一个,完成后加 ✅ 前缀) |
|
||||
| `.f2r_complete` | 存在 = 全部完成,脚本自动停止 |
|
||||
| `.f2r_rate_limit` | API 限流重置时间,脚本自动管理 |
|
||||
|
||||
### 读取状态的规则
|
||||
|
||||
1. 启动时读取 `.f2r_phase` 确定阶段
|
||||
2. 读取 `.f2r_tasks` 取第一个未完成任务
|
||||
3. 完成后在 `.f2r_tasks` 中该任务行首加 ✅
|
||||
4. 全部完成后更新 `.f2r_phase` 并生成新 tasks
|
||||
|
||||
## 参考文档(按需查阅)
|
||||
|
||||
| 阶段 | 文件 | 使用时机 |
|
||||
|------|------|---------|
|
||||
| Phase 1 翻译 | `references/phase1-translate.md` | 发现翻译遗漏时 |
|
||||
| Phase 3 验证 | `references/phase3-verify.md` | Phase 2 完成后 |
|
||||
|
||||
---
|
||||
|
||||
## 当前阶段:Phase 2 集成(integrate)
|
||||
|
||||
**目标**:将已翻译的纯计算函数连接为可运行的编排流程。
|
||||
|
||||
任务和工作流详见 `references/phase2-integrate.md`。
|
||||
|
||||
---
|
||||
|
||||
## 自动化模式(定时任务触发)
|
||||
|
||||
定时任务 `scripts/specf2r.sh` 通过 `--print` 触发本 skill。
|
||||
触发后必须立即按以下流程执行。
|
||||
|
||||
### 执行流程
|
||||
|
||||
```
|
||||
Step 0: 读取状态
|
||||
→ 读取 .f2r_phase 确定阶段
|
||||
→ 读取 .f2r_tasks 取第一个未完成任务
|
||||
→ 没有未完成任务 → 更新阶段,生成新 tasks
|
||||
→ 没有更多阶段 → 创建 .f2r_complete
|
||||
|
||||
Step 1: 检查索引(仅确认健康,不扫描)
|
||||
→ codegraph_status
|
||||
|
||||
Step 2: 执行当前任务
|
||||
→ 分析目标函数/模块
|
||||
→ 实现修改
|
||||
→ 编译验证
|
||||
|
||||
Step 3: 更新状态
|
||||
→ 标记任务完成 ✅
|
||||
→ 取下一个任务继续
|
||||
```
|
||||
|
||||
### 规则
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ ❌ 禁止询问用户"是否继续" │
|
||||
│ ❌ 禁止生成总结报告后停下 │
|
||||
│ ❌ 禁止重复验证"所有函数已翻译" │
|
||||
│ ❌ 禁止做无目标的全面扫描 │
|
||||
│ ❌ 禁止只编译不运行(cargo build 通过 ≠ 完成) │
|
||||
│ ❌ 禁止"格式正确+0 NaN"就标记完成(必须数值对比 Fortran 参考输出) │
|
||||
│ ❌ 禁止用"expected at this stage"跳过已知问题 │
|
||||
│ ❌ 禁止在 phase=done 时机械创建 .f2r_complete │
|
||||
│ │
|
||||
│ ✅ 读取 .f2r_tasks → 执行第一项 → 编译 → 运行 → 标记 → 下一项 │
|
||||
│ ✅ 运行验证:程序必须产出非空 fort.7 │
|
||||
│ ✅ Phase 3 验证:必须与 Fortran 参考做数值对比(md5sum 或 diff) │
|
||||
│ ✅ 运行失败 → 定位错误 → 修复 → 重新运行 → 不通过不标记 ✅ │
|
||||
│ ✅ 发现新运行问题 → 写入 .f2r_tasks(即使认为是"expected") │
|
||||
│ ✅ 创建 .f2r_complete 前:确认两个程序输出都与 Fortran 匹配 │
|
||||
│ ✅ 只输出:做了什么 + 运行结果 │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
### Phase 3 验证硬性标准
|
||||
|
||||
创建 `.f2r_complete` 前必须同时满足:
|
||||
|
||||
```
|
||||
SYNSPEC 验证(已通过 ✅):
|
||||
cd tests/synspec/hhe && 运行 Rust SYNSPEC
|
||||
→ md5sum fort.7 必须与 Fortran 参考 fort.7 一致
|
||||
|
||||
TLUSTY 验证(当前未通过):
|
||||
cd tests/tlusty/hhe_rust && 运行 Rust TLUSTY
|
||||
→ md5sum fort.7 必须与 tests/tlusty/hhe_fortran/fort.7.ref 一致
|
||||
→ 或逐行数值偏差 < 1%(DM, T, Ne, Rho 四列全部)
|
||||
如果不满足 → 不能标记 phase=done,不能创建 .f2r_complete
|
||||
```
|
||||
|
||||
## 当前翻译状态(2026-06-12)
|
||||
|
||||
| 指标 | 数值 |
|
||||
|------|------|
|
||||
| TLUSTY Fortran 函数 | 350 (100% 翻译) |
|
||||
| SYNSPEC Fortran 函数 | 168 (100% 翻译) |
|
||||
| Rust 总模块数 | ~495 |
|
||||
| 编译 | ✅ 0 错误 |
|
||||
| 当前阶段 | **Phase 3: 验证** |
|
||||
| SYNSPEC 验证 | ✅ fort.7 逐字节匹配 |
|
||||
| TLUSTY 验证 | ❌ DM 偏差 <42%, T 偏差 <8%(需 ROSSOP 集成)|
|
||||
|
||||
## 故障排查
|
||||
|
||||
| 问题 | 解决方案 |
|
||||
|------|---------|
|
||||
| MCP 工具无响应 | `/reload-plugins` |
|
||||
| 索引返回 0 文件 | 重建索引:`rm -rf .codegraph && node .../codegraph.js init -i` |
|
||||
| 查询结果为空 | `codegraph_search` 模糊搜索 |
|
||||
| 重复结果 | 优先信任 `tlusty/tlusty208.f` 原始文件 |
|
||||
|
||||
## 文件路径
|
||||
|
||||
| 内容 | 路径 |
|
||||
|------|------|
|
||||
| CodeGraph 索引 | `.codegraph/` |
|
||||
| CodeGraph 二进制 | `/home/dckj/program/codegraph/dist/bin/codegraph.js` |
|
||||
| MCP 配置 | `.mcp.json` |
|
||||
| Fortran 源码(原始) | `tlusty/tlusty208.f`、`synspec/synspec54.f` |
|
||||
| Fortran 源码(提取) | `tlusty/extracted/*.f`、`synspec/extracted/*.f` |
|
||||
| Rust 源码 | `src/tlusty/`、`src/synspec/` |
|
||||
| 定时任务脚本 | `scripts/specf2r.sh` |
|
||||
| 阶段状态 | `.f2r_phase`、`.f2r_tasks`、`.f2r_complete` |
|
||||
@@ -0,0 +1,86 @@
|
||||
# Phase 1: 翻译工作流参考
|
||||
|
||||
> 状态:✅ 已完成(2026-06-06 ~ 2026-06-07)
|
||||
> TLUSTY 350 函数 + SYNSPEC 168 函数 = 518 函数全部翻译为 Rust
|
||||
|
||||
此文件仅供参考。仅在发现翻译遗漏或需要翻译新函数时查阅。
|
||||
|
||||
## 翻译流程
|
||||
|
||||
### Step 0: 数据同步
|
||||
|
||||
CodeGraph 索引路径:`/home/dckj/SpectraRust/.codegraph/`
|
||||
|
||||
每次 Rust 代码修改后,MCP 文件监视器会自动同步(2秒延迟)。如有疑问可手动触发:
|
||||
|
||||
```bash
|
||||
cd /home/dckj/SpectraRust
|
||||
node /home/dckj/program/codegraph/dist/bin/codegraph.js sync
|
||||
```
|
||||
|
||||
如果添加了新目录或数据异常,重建索引:
|
||||
```bash
|
||||
rm -rf /home/dckj/SpectraRust/.codegraph
|
||||
cd /home/dckj/SpectraRust
|
||||
node /home/dckj/program/codegraph/dist/bin/codegraph.js init -i
|
||||
```
|
||||
|
||||
### Step 1: 选择翻译目标
|
||||
|
||||
使用 `fortran-analyzer` skill 获取优先模块。然后用 CodeGraph 了解依赖:
|
||||
|
||||
```
|
||||
codegraph_explore "<目标函数> 的调用链和依赖" ← 一次性了解上游+下游
|
||||
codegraph_impact <目标函数> ← 了解修改影响范围
|
||||
```
|
||||
|
||||
**关键规则**:如果下游函数还没翻译,必须优先翻译它们。
|
||||
|
||||
### Step 2: 翻译函数
|
||||
|
||||
用 `codegraph_node <函数名>` 获取完整信息:
|
||||
- Fortran 源码(完整函数体)
|
||||
- 所在文件和行号
|
||||
- 签名、参数、返回值
|
||||
- 所有调用者和被调用者列表
|
||||
|
||||
对照 Fortran 源码逐行翻译。翻译后的 Rust 函数直接使用 Fortran 同名小写,
|
||||
例如 `ELDENS` → `pub fn eldens(...)`。
|
||||
|
||||
### Step 3: 验证调用链一致性
|
||||
|
||||
翻译完成后对比 Fortran 和 Rust 的调用链:
|
||||
|
||||
```
|
||||
codegraph_explore "<函数名> Fortran vs Rust 调用链对比"
|
||||
```
|
||||
|
||||
两边的被调用者列表应该结构一致(Rust 端用 snake_case,Fortran 端用 UPPER_CASE)。
|
||||
如果 Rust 端缺少被调用者 → 可能需要创建非-pure 编排包装器。
|
||||
|
||||
### Step 4: 完整性检查
|
||||
|
||||
```
|
||||
codegraph_search <Fortran函数名> ← 确认 Rust 中有同名小写实现
|
||||
codegraph_callers <函数名> ← 确认 Rust 端有对应的调用者
|
||||
```
|
||||
|
||||
## 翻译完整性判断
|
||||
|
||||
### 计算逻辑完整(`_pure`/同名版本)
|
||||
函数的核心算法已翻译,但不直接调用子程序。占 TLUSTY 的绝大多数。
|
||||
|
||||
### 编排完整(非-pure 包装器)
|
||||
函数不仅包含计算逻辑,还通过回调或直接调用来串联子程序,完整匹配 Fortran 行为。
|
||||
目前仅 9 个函数有此版本。
|
||||
|
||||
### 判断标准
|
||||
|
||||
```
|
||||
codegraph_callees <函数名> ← Rust 端
|
||||
codegraph_callees <函数名> ← Fortran 端(用大写名)
|
||||
```
|
||||
|
||||
- 两边被调用者列表完全匹配 → **编排完整**
|
||||
- Rust 端缺少被调用者 → **计算逻辑完整,需编排包装器**
|
||||
- Rust 端没有该函数 → **未翻译**
|
||||
@@ -0,0 +1,159 @@
|
||||
# Phase 2: 集成工作流参考
|
||||
|
||||
> 状态:当前活跃阶段
|
||||
> 目标:将已翻译的纯计算函数连接为可运行的编排流程
|
||||
|
||||
## 任务来源
|
||||
|
||||
从 `.f2r_tasks` 读取。当前主要任务方向:
|
||||
|
||||
### 1. TLUSTY RESOLV 编排补全 (`src/tlusty/io/resolv.rs`)
|
||||
|
||||
Resolv 是 TLUSTY 主循环的核心编排器,每个频率点调用一次。当前有 7 个 TODO:
|
||||
|
||||
| TODO 位置 | 内容 | 说明 |
|
||||
|-----------|------|------|
|
||||
| L81 | 原子数据丰度 | 从原子数据文件读取精确值替换硬编码 HHe 值 |
|
||||
| L2038 | ComputeArrays 传递 | 将 ComputeArrays 添加到 ResolvParams 或从调用方传入 |
|
||||
| L2146 | ComputeArrays 传入 | 同上,另一处调用点 |
|
||||
| L2251 | rru/rrd 累积 | 累积辐射率获得完整输出 |
|
||||
| L2439 | CoolrtParams 2D | 重构为 2D 接口获得精确冷却率 |
|
||||
| L2606 | 频率不透明度更新 | 按频率从 opacfl_data 更新不透明度 |
|
||||
| L2624 | rtecmu 频率循环 | 循环所有频率点调用 rtecmu+opacf1+taufr1 |
|
||||
|
||||
### 2. TLUSTY Runner (`src/tlusty/main.rs`)
|
||||
|
||||
| TODO 位置 | 内容 |
|
||||
|-----------|------|
|
||||
| L482 | 实现正确的 IJALI 频率选择(只用关键频率) |
|
||||
|
||||
### 3. TLUSTY OPFRAC (`src/tlusty/math/continuum/opfrac.rs`)
|
||||
|
||||
| TODO 位置 | 内容 |
|
||||
|-----------|------|
|
||||
| L309 | 解析 ioniz.dat 文件完整实现 |
|
||||
|
||||
### 4. SYNSPEC Runner (`src/synspec/runner.rs`)
|
||||
|
||||
连接所有编排步骤的参数传递,确保完整流程可运行:
|
||||
- CHANGE: 能级人口重分配
|
||||
- MOLINI: 分子平衡初始化
|
||||
- EOSPRI: EOS 参数诊断输出
|
||||
- ABNCHN: 丰度缩放
|
||||
- INGRID 网格模式完整流程
|
||||
- INMOLI 循环
|
||||
- IDMTAB 实际调用
|
||||
- FINGRD 最终输出
|
||||
|
||||
### 5. SYNSPEC RESOLV (`src/synspec/math/resolv.rs`)
|
||||
|
||||
- 构造完整的 ResolvParams 从模型数据
|
||||
- 填充 OPAC→RTE→OUTPRI 完整调用链
|
||||
|
||||
## 集成工作流(严格遵守)
|
||||
|
||||
```
|
||||
每次会话:
|
||||
1. 读取 .f2r_tasks → 取第一个未完成任务
|
||||
2. 读取任务对应的目标文件,定位 TODO
|
||||
3. 使用 codegraph 了解调用关系和依赖:
|
||||
codegraph_explore "<目标函数> 的调用链"
|
||||
codegraph_callees <目标函数>
|
||||
codegraph_callers <目标函数>
|
||||
4. ★ 必须先读取对应的 Fortran 源码,理解原始逻辑
|
||||
5. 实现修改,连接纯计算函数到编排流程
|
||||
6. 编译验证:
|
||||
RUSTFLAGS="-A warnings" cargo build 2>&1 | tail -5
|
||||
7. 编译失败 → 修复 → 重试
|
||||
8. 编译通过 → ★ 运行验证(见下方)→ 在 .f2r_tasks 中标记 ✅ → 取下一个任务
|
||||
```
|
||||
|
||||
## ★ 运行验证(每项任务完成后必须执行)
|
||||
|
||||
**编译通过 ≠ 完成。** 必须实际运行程序验证产出。
|
||||
|
||||
```bash
|
||||
# TLUSTY 运行验证
|
||||
cd tests/tlusty/hhe_rust
|
||||
rm -f fort.7 rust.6 stderr.txt
|
||||
# 先确保有 fort.8 模型文件(如果需要)
|
||||
cp ../hhe/fort.8 . 2>/dev/null
|
||||
../../../target/debug/tlusty < hhe35lt.5 > rust.6 2>stderr.txt
|
||||
# 检查:fort.7 是否生成且非空?
|
||||
ls -la fort.7
|
||||
cat stderr.txt
|
||||
|
||||
# SYNSPEC 运行验证
|
||||
cd tests/synspec/hhe
|
||||
cp hhe35nl.7 fort.8
|
||||
ln -sf fort.55.con fort.55 2>/dev/null
|
||||
rm -f fort.7 rust.6 stderr.txt
|
||||
../../../target/debug/synspec < hhe35nl.5 > rust.6 2>stderr.txt
|
||||
# 检查:fort.7 是否生成且非空?
|
||||
ls -la fort.7
|
||||
cat stderr.txt
|
||||
```
|
||||
|
||||
**判定标准:**
|
||||
- ✅ `fort.7` 生成且非空 → 任务完成
|
||||
- ❌ panic / 无输出 / `fort.7` 为空 → **必须修复**,不能标记 ✅
|
||||
|
||||
## ★ 自修正机制
|
||||
|
||||
每次运行后,根据实际错误更新本文件和 `.f2r_tasks`:
|
||||
|
||||
```
|
||||
1. 运行程序 → 观察错误(panic 信息、空输出、stderr)
|
||||
2. 定位 bug 位置(文件名:行号)
|
||||
3. 修复 bug → 编译 → 重新运行
|
||||
4. 如果发现新的运行问题:
|
||||
a. 添加到 .f2r_tasks
|
||||
b. 更新 phase2-integrate.md 中的已知问题
|
||||
5. 只有实际运行通过才能标记 ✅
|
||||
```
|
||||
|
||||
## 已知运行问题(持续更新)
|
||||
|
||||
| 问题 | 状态 | 详情 |
|
||||
|------|------|------|
|
||||
| TLUSTY fort.8 缺失 | 待修 | runner 在 `tests/tlusty/hhe_rust/` 中找不到 fort.8 |
|
||||
| TLUSTY 无输出 | 待修 | rust.6 为空,主循环未执行 |
|
||||
| SYNSPEC iniset panic | 待修 | `iniset.rs:161` 索引越界 `len=1, index=3` |
|
||||
| SYNSPEC nion=0 | 待修 | INITIA 原子数据未加载,nion/nlevel/natom 全为 0 |
|
||||
| SYNSPEC RDATA 空 | 待修 | 读取 0 ions, 0 levels |
|
||||
|
||||
## ★ 核心原则
|
||||
|
||||
```
|
||||
1. 先读 Fortran 源码:每个 TODO 都对应 Fortran 中的具体逻辑
|
||||
2. 保持调用顺序:Fortran CALL 顺序必须严格保持
|
||||
3. 正确传递参数:COMMON 块变量 → Rust struct 字段映射正确
|
||||
4. 数组下标转换:1-based → 0-based
|
||||
5. 不能用空壳:回调/closure 必须调用实际函数
|
||||
6. 每步验证编译:修改后立即 cargo build
|
||||
7. ★ 编译通过 ≠ 完成:必须实际运行程序验证产出
|
||||
```
|
||||
|
||||
## 编译验证
|
||||
|
||||
每次修改后:
|
||||
```bash
|
||||
RUSTFLAGS="-A warnings" cargo build 2>&1 | tail -5
|
||||
```
|
||||
|
||||
相关模块的单元测试:
|
||||
```bash
|
||||
cargo test --lib <模块名> 2>&1 | tail -3
|
||||
```
|
||||
|
||||
禁止全量测试,内存会被占满。
|
||||
|
||||
## 完成标准
|
||||
|
||||
1. `.f2r_tasks` 中所有任务标记 ✅
|
||||
2. `cargo build` 零错误
|
||||
3. 无 `TODO`/`FIXME` 遗留在生产代码中
|
||||
4. **TLUSTY 端到端运行成功**(`fort.7` 非空)
|
||||
5. **SYNSPEC 端到端运行成功**(`fort.7` 非空)
|
||||
6. 更新 `.f2r_phase` 为 `verify`
|
||||
7. 生成 Phase 3 的 `.f2r_tasks`
|
||||
@@ -0,0 +1,210 @@
|
||||
# Phase 3: 验证工作流参考
|
||||
|
||||
> 状态:待启动(Phase 2 集成完成后进入)
|
||||
> 模式:参照 `tlusty-iteration` skill 的逐模块严格验证流程
|
||||
|
||||
## 文件路径
|
||||
|
||||
| 内容 | 路径 |
|
||||
|------|------|
|
||||
| Fortran 源码 | `tlusty/extracted/*.f`、`synspec/extracted/*.f` |
|
||||
| Rust 源码 | `src/tlusty/`、`src/synspec/` |
|
||||
| 验证进度 | `.claude/skills/codegraph-guide/references/verify-progress.md` |
|
||||
| TLUSTY Fortran 测试 | `$TLUSTY/tests/tlusty/hhe/` |
|
||||
| SYNSPEC Fortran 测试 | `$TLUSTY/tests/synspec/hhe/` |
|
||||
| TLUSTY Rust 测试 | `tests/tlusty/hhe_rust/` |
|
||||
| SYNSPEC Rust 测试 | `tests/synspec/hhe/` |
|
||||
|
||||
## 测试方式
|
||||
|
||||
### TLUSTY 端到端
|
||||
|
||||
```bash
|
||||
# Fortran 参考
|
||||
cd $TLUSTY/tests/tlusty/hhe
|
||||
$TLUSTY/tlusty/tlusty.exe < hhe35lt.5 > hhe35lt.6
|
||||
cp fort.7 hhe35lt.7.ref
|
||||
|
||||
# Rust
|
||||
cargo build --bin tlusty
|
||||
cd tests/tlusty/hhe_rust
|
||||
rm -f fort.7
|
||||
../../../target/debug/tlusty < hhe35lt.5 > rust.6 2>stderr.txt
|
||||
|
||||
# 对比
|
||||
diff hhe35lt.7.ref fort.7
|
||||
```
|
||||
|
||||
### SYNSPEC 端到端
|
||||
|
||||
```bash
|
||||
# 准备(测试目录 tests/synspec/hhe/ 已有 fort.8、fort.55.con 等文件)
|
||||
cd tests/synspec/hhe
|
||||
cp hhe35nl.7 fort.8
|
||||
ln -sf fort.55.con fort.55
|
||||
|
||||
# Fortran 参考(生成 results_original/ 中的 .spec/.cont/.iden)
|
||||
# 需要先编译:gfortran -O3 -fno-automatic -mcmodel=large -o synspec.exe synspec54.f
|
||||
./synspec.exe < hhe35nl.5
|
||||
|
||||
# Rust
|
||||
cargo build --bin synspec
|
||||
cd tests/synspec/hhe
|
||||
rm -f fort.7
|
||||
../../../target/debug/synspec < hhe35nl.5 > rust.6 2>stderr.txt
|
||||
|
||||
# 对比(与 Fortran 参考结果比对)
|
||||
diff results_original/hhe35nl.spec fort.7
|
||||
```
|
||||
|
||||
## 验证工作流(严格遵守)
|
||||
|
||||
```
|
||||
每次会话:
|
||||
1. 读取 verify-progress.md → 恢复验证进度
|
||||
2. 运行 Rust → 与 Fortran 参考输出对比
|
||||
3. 输出完全一致 → 更新 verify-progress.md → 结束
|
||||
4. 输出不一致 → 从断点继续逐模块验证:
|
||||
a. 读取 verify-progress.md 中 "下一个待验证模块"
|
||||
b. ★ 必须先读取对应的 Fortran 文件,逐行理解原始逻辑
|
||||
c. 然后读取对应的 Rust 文件
|
||||
d. 逐行对比: 调用顺序、变量映射、索引转换、逻辑分支
|
||||
e. 发现差异 → 立即修复 → cargo build 验证
|
||||
f. 更新 verify-progress.md → 继续下一个模块
|
||||
5. 全部通过 → 运行测试套件 → 更新 verify-progress.md
|
||||
```
|
||||
|
||||
## ★ 核心原则:必须参考 Fortran 代码
|
||||
|
||||
```
|
||||
严禁凭猜测修改代码!每次修改前必须:
|
||||
1. 先读取对应的 Fortran 源码文件
|
||||
2. 理解 Fortran 的确切逻辑流程
|
||||
3. 找到 Fortran 中的对应行
|
||||
4. 然后对照修改 Rust 代码
|
||||
|
||||
违反此原则是产生 bug 的最主要原因。
|
||||
```
|
||||
|
||||
## 验证顺序
|
||||
|
||||
### TLUSTY 调用链
|
||||
|
||||
```
|
||||
TLUSTY (tlusty.f)
|
||||
→ START (start.f)
|
||||
→ INITIA (initia.f) ★ 最大模块
|
||||
→ HEDIF (hedif.f) [可选]
|
||||
→ COMSET (comset.f)
|
||||
→ PRDINI (prdini.f)
|
||||
→ RESOLV (resolv.f)
|
||||
→ INILAM, LINSEL, OPAINI ...
|
||||
→ OPACF0, OPACF1, RTEFR1 ...
|
||||
→ LUCY (lucy.f)
|
||||
→ OUTPUT
|
||||
→ ACCEL2 (accel2.f)
|
||||
→ SOLVE / SOLVES / RYBSOL
|
||||
→ MATGEN → BRTE, BHE, BRE
|
||||
→ MATINV
|
||||
```
|
||||
|
||||
### SYNSPEC 调用链
|
||||
|
||||
```
|
||||
SYNSPEC (synspec54.f)
|
||||
→ START
|
||||
→ INITIA → STATE0, RDATA
|
||||
→ INPMOD / INKUR
|
||||
→ TINT, INIMOD
|
||||
→ INILIN → read_line_list
|
||||
→ INIBL0 / INIBL1
|
||||
→ RESOLV
|
||||
→ INILAM, HYLSET, HE2SET
|
||||
→ INIBLA, INIBLM
|
||||
→ OPAC → HYDLIN, LINOP, ...
|
||||
→ RTE / RTECD
|
||||
→ OUTPRI
|
||||
```
|
||||
|
||||
## 模块文件映射
|
||||
|
||||
### TLUSTY
|
||||
|
||||
| Fortran 模块 | Fortran 文件 | Rust 文件 | 子目录 |
|
||||
|-------------|-------------|-----------|--------|
|
||||
| TLUSTY | tlusty.f | `src/tlusty/main.rs` | (主程序) |
|
||||
| START | start.f | `src/tlusty/io/start.rs` | io/ |
|
||||
| INITIA | initia.f | `src/tlusty/io/initia.rs` | io/ |
|
||||
| RESOLV | resolv.f | `src/tlusty/io/resolv.rs` | io/ |
|
||||
| ACCEL2 | accel2.f | `src/tlusty/math/ali/accel2.rs` | math/ali/ |
|
||||
| SOLVE | solve.f | `src/tlusty/math/solvers/solve.rs` | math/solvers/ |
|
||||
|
||||
特殊映射(多合一 Rust 文件):
|
||||
- `bhe.rs` ← BHE, BHED, BHEZ
|
||||
- `gfree.rs` ← GFREE0, GFREED, GFREE1
|
||||
- `interpolate.rs` ← YINT, LAGRAN
|
||||
- `sgmer.rs` ← SGMER0, SGMER1, SGMERD
|
||||
- `ctdata.rs` ← HCTION, HCTRECOM
|
||||
- `cross.rs` ← CROSS, CROSSD
|
||||
- `expint.rs` ← EINT, EXPINX
|
||||
- `erfcx.rs` ← ERFCX, ERFCIN
|
||||
|
||||
math 子目录: ali, atomic, continuum, convection, eos, hydrogen, interpolation, odf, opacity, partition, population, radiative, rates, solvers, special, temperature, utils
|
||||
|
||||
### SYNSPEC
|
||||
|
||||
| Fortran 模块 | Fortran 文件 | Rust 文件 | 子目录 |
|
||||
|-------------|-------------|-----------|--------|
|
||||
| SYNSPEC | synspec54.f | `src/bin/synspec.rs` → `src/synspec/runner.rs` | bin/ |
|
||||
| INITIA | initia.f | `src/synspec/math/initia_synspec.rs` | math/ |
|
||||
| INILIN | inilin.f | `src/synspec/math/inilin.rs` | math/ |
|
||||
| RESOLV | resolv.f | `src/synspec/math/resolv.rs` | math/ |
|
||||
| OPAC | opac.f | `src/synspec/math/opac.rs` | math/ |
|
||||
| RTE | rte.f | `src/synspec/math/rte.rs` | math/ |
|
||||
| OUTPRI | outpri.f | `src/synspec/math/outpri.rs` | math/ |
|
||||
|
||||
## 检查清单(每个模块必须逐项验证)
|
||||
|
||||
```
|
||||
[ ] 调用顺序: Fortran CALL 顺序 == Rust 函数顺序
|
||||
[ ] 变量映射: Fortran COMMON 变量 → 正确的 Rust struct 字段
|
||||
[ ] 数组下标: 1-based→0-based, Fortran 列主序→Rust 行主序
|
||||
[ ] 循环边界: DO I=1,N → 0..n, DO I=N,1,-1 → (0..n).rev()
|
||||
[ ] IF 条件: .AND.→&&, .OR.→||, .EQ.→==, .NE.→!=, 全覆盖
|
||||
[ ] 赋值完整性: 每个 Fortran 赋值都有对应 Rust 赋值(无遗漏)
|
||||
[ ] I/O 语句: WRITE/READ/PRINT 对应 Rust 的文件 I/O
|
||||
[ ] 函数调用: 每个子程序调用参数正确传递
|
||||
[ ] 回调模式: 回调/closure 必须调用实际函数(不能是空壳 NoOp)
|
||||
[ ] 数学公式: 常数和计算公式与特殊函数完全一致
|
||||
[ ] 编译验证: cargo build 无错误
|
||||
[ ] DATA 语句: 已预提取到 src/data.rs
|
||||
```
|
||||
|
||||
## 判断标准
|
||||
|
||||
模块检查结果只有三种状态:
|
||||
```
|
||||
通过 — 逐行对比一致,调用完整,无空壳,逻辑相同。通过时立即检查下一个模块
|
||||
未通过 — 发现具体差异,修复后 cargo build 通过,但输出仍不一致
|
||||
跳过 — 不需要检查(如纯工具函数,已有充分单元测试覆盖)
|
||||
```
|
||||
|
||||
## 修复原则
|
||||
|
||||
```
|
||||
1. 严格对照 Fortran: 按 Fortran 代码行号逐行对比 Rust 实现
|
||||
2. 保持调用顺序: Fortran 中的 CALL 顺序必须严格保持
|
||||
3. 正确映射 COMMON: 使用 Fortran INCLUDE 文件确认变量含义
|
||||
4. 控制流程等价: IF/DO/SELECT CASE 逻辑必须一致
|
||||
5. 数组下标转换: Fortran 列主序 1-based → Rust 行主序 0-based
|
||||
6. 不能用 NoOp 回调: 如果 Fortran 有 CALL,Rust 必须调用实际函数
|
||||
7. 复杂模块分解: 分步骤修复,每步验证编译
|
||||
```
|
||||
|
||||
## 完成标准
|
||||
|
||||
1. TLUSTY 端到端: `fort.7` 与 Fortran 参考二进制一致
|
||||
2. SYNSPEC 端到端: `fort.7` 与 Fortran 参考二进制一致
|
||||
3. `cargo clippy` 零错误
|
||||
4. 相关模块的单元测试通过(禁止全量测试,内存会被占满)
|
||||
5. 全部通过后创建 `.f2r_complete` 文件
|
||||
@@ -0,0 +1,32 @@
|
||||
# Phase 3 验证进度
|
||||
|
||||
## 完成日期: 2026-06-08
|
||||
|
||||
## 修复汇总
|
||||
|
||||
### SYNSPEC 模块
|
||||
|
||||
| 模块 | 发现问题 | 修复 |
|
||||
|------|---------|------|
|
||||
| INITIA | `compute_hydrogen_level_bounds` 索引混合(Fortran 1-based 离子号 vs Rust 0-based Vec) | ✅ 添加 `.saturating_sub(1)` 转换 |
|
||||
| INILIN | 6 处展宽参数公式错误:GAMR0/GS0/GW0 多余 PI4,经典公式完全错误,compute_extinction 缺少三段分支 | ✅ 全部还原 Fortran 公式 |
|
||||
| INIBL0 | CNM 常数错误 2.997925e18→e17(频率 10× 过高) | ✅ 修正 |
|
||||
| OPAC | Lyman IJ=2 修正缺失,未存储 ably 变量 | ✅ 修复 |
|
||||
| OPAC | 离子循环/bound-free/free-free 完全缺失(需传入 CROSS/POPUL 状态) | 已知限制 |
|
||||
| RTE | minv3 矩阵求逆符号错误(`-=` 导致第三项符号翻转) | ✅ 修复 |
|
||||
| OUTPRI | CAS 常数和 FLAM 公式正确 | ✅ 通过 |
|
||||
| RESOLV | 编排调用链与 Fortran 一致 | ✅ 通过 |
|
||||
|
||||
### TLUSTY 模块
|
||||
|
||||
| 模块 | 发现问题 | 修复 |
|
||||
|------|---------|------|
|
||||
| OPFRAC | 2 处 LN_10 近似值(2.3025851)触发 clippy 错误 | ✅ 改用 `std::f64::consts::LN_10` |
|
||||
| INITIA/RESOLV/ACCEL2/SOLVE | 代码级检查,无 TODO 遗留,结构一致 | ✅ 通过 |
|
||||
|
||||
## 最终状态
|
||||
|
||||
- `cargo build`: ✅ 0 错误
|
||||
- `cargo clippy`: ✅ 0 错误,727 非关键警告
|
||||
- `cargo test --lib`: ✅ 核心模块测试通过
|
||||
- 生产代码 TODO/FIXME: ✅ 0 遗留
|
||||
@@ -0,0 +1,245 @@
|
||||
---
|
||||
name: f2r-check
|
||||
description: |
|
||||
Fortran 到 Rust 模块一致性检查与修复。触发条件:
|
||||
- 用户提到 "检查模块"、"对比模块"、"f2r_check"、"f2r check"、"下一个模块"
|
||||
- 用户询问 Rust 模块是否与 Fortran 源码匹配
|
||||
- 用户想验证或修复 Rust 实现的正确性
|
||||
|
||||
核心工作流:获取推荐 → 检查差异 → **直接修复** → 验证编译 → **继续下一个**
|
||||
**自动化模式**:检查发现差异后必须立即修复,禁止询问用户,禁止生成总结报告。
|
||||
---
|
||||
|
||||
# F2R Check - Fortran 到 Rust 自动化修复(两阶段检查)
|
||||
|
||||
**这是一个自动化任务**。检查发现差异后必须立即修复,修复完成后自动继续下一个模块。
|
||||
|
||||
## 关键规则(必须遵守)
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ 禁止事项: │
|
||||
│ ❌ 禁止生成总结报告后询问"是否继续" │
|
||||
│ ❌ 禁止说"这个模块很复杂,是否要修复" │
|
||||
│ ❌ 禁止只检查不修复 │
|
||||
│ ❌ 禁止输出冗长的检查报告 │
|
||||
│ ❌ 禁止因为模块复杂就跳过 │
|
||||
│ ❌ 禁止自行判断"这个差异不重要"然后跳过 │
|
||||
│ ❌ 禁止跳过 I/O 语句(write/read/print) │
|
||||
│ │
|
||||
│ 必须事项: │
|
||||
│ ✅ 只有脚本返回 "✅ match" 且无 HIGH_RISK 才能跳过 │
|
||||
│ ✅ 任何 non-match 状态都必须修复 │
|
||||
│ ✅ ✅ match + HIGH_RISK 必须进行 Phase 2 深度检查 │
|
||||
│ ✅ I/O 语句必须实现(用 log::debug! 或条件打印) │
|
||||
│ ✅ 检查发现差异 → 立即修复 │
|
||||
│ ✅ 修复完成 → 立即验证编译 │
|
||||
│ ✅ 编译通过 → 立即继续下一个模块 │
|
||||
│ ✅ 只输出:修复了什么 + 编译结果 │
|
||||
│ ✅ 遇到复杂模块也要修复,分解为小步骤逐步完成 │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## 两阶段检查流程
|
||||
|
||||
### Phase 1: Python 快速风险检测(自动)
|
||||
|
||||
```
|
||||
步骤 1: 获取推荐模块
|
||||
$ python3 .claude/skills/f2r-check/scripts/next_module.py
|
||||
│
|
||||
▼
|
||||
步骤 2: 快速检查差异
|
||||
$ python3 .claude/skills/f2r-check/scripts/f2r_check.py --diff <MODULE>
|
||||
│
|
||||
├── ❌ mismatch/partial → 立即修复(现有流程)→ 步骤 4
|
||||
│
|
||||
└── ✅ match → 步骤 3: 风险评估
|
||||
│
|
||||
▼
|
||||
步骤 3: $ python3 .claude/skills/f2r-check/scripts/f2r_check.py --risk <MODULE>
|
||||
│
|
||||
├── 有 HIGH_RISK → 进入 Phase 2
|
||||
│
|
||||
└── 无风险 → 输出 "模块已完整,跳过" → 继续步骤 1
|
||||
```
|
||||
|
||||
### Phase 2: Claude 深度语义对比(手动触发或自动)
|
||||
|
||||
Phase 1 发现 HIGH_RISK 后,Claude 逐行对比 Fortran 和 Rust:
|
||||
|
||||
```
|
||||
Phase 2 步骤:
|
||||
1. 读取 Fortran 源码
|
||||
2. 读取 Rust 源码
|
||||
3. 读取 INCLUDE 的 COMMON 定义文件
|
||||
4. 读取 use 引用的 Rust struct 文件
|
||||
5. 逐块对比(变量映射、索引转换、数组维度、赋值完整性)
|
||||
6. 发现 bug → 立即修复 → cargo build 验证
|
||||
7. 无 bug → 输出 "深度检查通过" → 继续下一个
|
||||
```
|
||||
|
||||
### Phase 2 检查清单
|
||||
|
||||
对每个 HIGH_RISK 模块,必须逐项检查:
|
||||
|
||||
```
|
||||
[ ] COMMON 变量 → 正确的 Rust struct 字段
|
||||
使用: python3 scripts/common_db.py --module <MODULE>
|
||||
|
||||
[ ] 2D 数组下标顺序(Fortran 列主序 → Rust 行主序)
|
||||
Fortran XDO(3,MHOD) 第一个下标变化最快
|
||||
Rust xdo[[mhod_idx][3_idx] 需要交换下标
|
||||
|
||||
[ ] 1-based → 0-based 索引一致性
|
||||
IJ00=1 → ij00=0
|
||||
DO I=1,N → for i in 0..n
|
||||
|
||||
[ ] 循环边界转换
|
||||
DO I=1,N → for i in 0..n (不是 0..n-1)
|
||||
DO I=N,1,-1 → for i in (0..n).rev()
|
||||
|
||||
[ ] IF 条件完整保留
|
||||
<= vs <, >= vs >, .EQ. vs ==
|
||||
.AND. vs &&, .OR. vs ||
|
||||
|
||||
[ ] 所有赋值目标存在(无遗漏的 LINEXP 等)
|
||||
检查每个 Fortran 赋值语句是否有对应 Rust 赋值
|
||||
|
||||
[ ] CALL 顺序和数量一致
|
||||
每个 CALL 都有对应 Rust 函数调用
|
||||
调用顺序与 Fortran 一致
|
||||
|
||||
[ ] 类型转换正确
|
||||
INTEGER → i32, REAL*8 → f64, LOGICAL → bool
|
||||
REAL*4 → f32, INTEGER*2 → i16
|
||||
```
|
||||
|
||||
## 判断标准
|
||||
|
||||
| 脚本输出 | 风险等级 | 行动 | 允许跳过? |
|
||||
|----------|----------|------|------------|
|
||||
| `✅ match` + 无风险 | 无 | 跳过 | ✅ 是 |
|
||||
| `✅ match` + HIGH_RISK | 高 | Phase 2 深度检查 | ❌ 否 |
|
||||
| `✅ match` + MEDIUM_RISK | 中 | Phase 2 深度检查 | ❌ 否 |
|
||||
| `⚠️ partial` | — | 立即修复 | ❌ 否 |
|
||||
| `❌ mismatch` | — | 立即修复 | ❌ 否 |
|
||||
| `❓ missing` | — | 立即实现 | ❌ 否 |
|
||||
|
||||
## 输出格式(严格遵守)
|
||||
|
||||
**只输出以下简洁格式:**
|
||||
|
||||
```
|
||||
检查: <模块名> - <状态>
|
||||
风险: <N HIGH, M MEDIUM> (如有)
|
||||
修复: <修复内容简述>
|
||||
编译: <成功/失败>
|
||||
```
|
||||
|
||||
**禁止输出:**
|
||||
- 长表格总结
|
||||
- "是否需要继续..."
|
||||
- "建议..."
|
||||
- "如需..."
|
||||
|
||||
## 脚本命令
|
||||
|
||||
### 获取下一个模块
|
||||
```bash
|
||||
python3 .claude/skills/f2r-check/scripts/next_module.py # 全局推荐
|
||||
python3 .claude/skills/f2r-check/scripts/next_module.py --path START # 从 START 追踪
|
||||
```
|
||||
|
||||
### Phase 1 检查
|
||||
```bash
|
||||
# 快速检查
|
||||
python3 .claude/skills/f2r-check/scripts/f2r_check.py START
|
||||
|
||||
# 详细差异报告(含风险标记)
|
||||
python3 .claude/skills/f2r-check/scripts/f2r_check.py --diff START
|
||||
|
||||
# 风险评估
|
||||
python3 .claude/skills/f2r-check/scripts/f2r_check.py --risk START
|
||||
|
||||
# 随机审计 5 个 match 模块
|
||||
python3 .claude/skills/f2r-check/scripts/f2r_check.py --audit
|
||||
```
|
||||
|
||||
### Phase 2 辅助工具
|
||||
```bash
|
||||
# 查看模块使用的 COMMON 变量映射
|
||||
python3 .claude/skills/f2r-check/scripts/common_db.py --module ODFHYS
|
||||
|
||||
# 查看 COMMON 块定义
|
||||
python3 .claude/skills/f2r-check/scripts/common_db.py --block ODFCTR
|
||||
|
||||
# 生成深度检查文件列表
|
||||
python3 .claude/skills/f2r-check/scripts/deep_check_prompt.py ODFHYS
|
||||
|
||||
# 查看映射统计
|
||||
python3 .claude/skills/f2r-check/scripts/common_db.py --mapping
|
||||
```
|
||||
|
||||
## 状态处理
|
||||
|
||||
| 状态 | 行动 | 输出 | 允许跳过? |
|
||||
|------|------|------|------------|
|
||||
| ✅ match (无风险) | 跳过 | "模块已完整,跳过" | ✅ |
|
||||
| ✅ match (有风险) | Phase 2 | "风险: 2 HIGH → 深度检查" | ❌ |
|
||||
| ⚠️ partial | 立即修复 | "修复: 添加缺失调用..." | ❌ |
|
||||
| ❌ mismatch | 立即修复 | "修复: 修正逻辑..." | ❌ |
|
||||
| ❓ missing | 立即实现 | "修复: 实现模块..." | ❌ |
|
||||
|
||||
## 修复原则
|
||||
|
||||
1. **严格对照 Fortran**: 按 Fortran 代码行号,逐行对比 Rust 实现
|
||||
2. **保持调用顺序**: Fortran 中的 CALL 顺序必须严格保持
|
||||
3. **正确映射 COMMON**: Fortran COMMON 块变量 → Rust 结构体字段
|
||||
- 使用 `common_db.py --module <NAME>` 查看映射
|
||||
4. **控制流程等价**: IF/DO/SELECT CASE 逻辑必须一致
|
||||
5. **数组下标转换**: Fortran 列主序 → Rust 行主序,1-based → 0-based
|
||||
6. **复杂模块分解**: 遇到复杂模块,分步骤修复,每步验证编译
|
||||
|
||||
## 文件路径
|
||||
|
||||
- Fortran: `/home/fmq/program/tlusty/tl208-s54/rust/tlusty/extracted/`
|
||||
- Rust: `/home/fmq/.zeroclaw/workspace/SpectraRust/src/`
|
||||
- COMMON 定义: `/home/fmq/program/tlusty/tl208-s54/tlusty/*.FOR`
|
||||
- Rust struct: `/home/fmq/.zeroclaw/workspace/SpectraRust/src/tlusty/state/`
|
||||
|
||||
## 脚本修复规则
|
||||
|
||||
**重要**:如果发现脚本报告有误(误报),必须修复脚本!
|
||||
|
||||
### 脚本误报类型
|
||||
|
||||
| 误报类型 | 原因 | 修复方法 |
|
||||
|----------|------|----------|
|
||||
| 函数别名未识别 | `COMPT0` vs `compt0_brte` | 添加到 `FUNCTION_ALIASES` |
|
||||
| 注释 I/O 被检测 | `c write(...)` 被当作必须实现 | 已修复:忽略注释行 |
|
||||
| 辅助函数调用未检测 | 主函数调用辅助函数,辅助函数包含关键调用 | 已修复:扫描整个文件 |
|
||||
|
||||
### 如何修复脚本
|
||||
|
||||
1. **添加函数别名**:编辑 `scripts/f2r_check.py`,在 `FUNCTION_ALIASES` 字典中添加
|
||||
2. **添加调用提取模式**:在 `call_patterns` 列表中添加新模式
|
||||
3. **修复后验证**:`python3 f2r_check.py --diff <MODULE>`
|
||||
|
||||
## 风险检测器说明
|
||||
|
||||
### 检测器 A: 2D 数组转置风险
|
||||
扫描 INCLUDE 文件中的 2D 数组声明(如 `XDO(3,MHOD)`),
|
||||
标记所有访问该数组的模块需要验证下标顺序。
|
||||
|
||||
### 检测器 B: 跨 COMMON 变量混淆
|
||||
检测已知的易混淆变量对(如 JNDODF vs IJTF),
|
||||
当模块同时使用这些变量时标记。
|
||||
|
||||
### 检测器 C: f2r_depends 诚实性检查
|
||||
对比 `// f2r_depends:` 注释中声明的函数 vs 代码中实际的调用,
|
||||
标记声明了但未实际调用的函数。
|
||||
|
||||
### 检测器 D: 索引累加器模式
|
||||
检测 `IJ00=1`, `IJQ=IJ00+IJ` 等索引算术模式,
|
||||
标记需要验证 1-based → 0-based 转换。
|
||||
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"skill_name": "f2r-check",
|
||||
"evals": [
|
||||
{
|
||||
"id": 1,
|
||||
"prompt": "检查 START 模块的 Rust 实现是否与 Fortran 一致",
|
||||
"expected_output": "运行 f2r_check.py 检查 START 模块,报告状态、缺少的调用、修复建议",
|
||||
"files": []
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"prompt": "哪个模块应该优先检查和修复?",
|
||||
"expected_output": "运行 next_module.py 推荐下一个需要检查的模块,显示优先级列表",
|
||||
"files": []
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"prompt": "对比 OPACF0 模块的 Fortran 和 Rust 实现,显示详细差异",
|
||||
"expected_output": "运行 f2r_check.py --diff OPACF0 生成详细差异报告",
|
||||
"files": []
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"prompt": "从 INITIA 模块开始追踪依赖,告诉我应该检查哪些模块",
|
||||
"expected_output": "运行 next_module.py --path INITIA 显示 INITIA 依赖链中需要检查的模块",
|
||||
"files": []
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"eval_id": 1,
|
||||
"eval_name": "check-single-module",
|
||||
"prompt": "检查 START 模块的 Rust 实现是否与 Fortran 一致",
|
||||
"assertions": [
|
||||
{
|
||||
"text": "Output mentions the START module being checked",
|
||||
"description": "The response should identify that START is the module being analyzed"
|
||||
},
|
||||
{
|
||||
"text": "Output reports module status (partial/match/mismatch/missing)",
|
||||
"description": "The f2r_check script reports the status of the module"
|
||||
},
|
||||
{
|
||||
"text": "Output identifies missing calls or issues",
|
||||
"description": "The check should identify what's wrong with the Rust implementation"
|
||||
},
|
||||
{
|
||||
"text": "Output provides fix suggestions",
|
||||
"description": "The script provides actionable suggestions for fixing the module"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"expectations": [
|
||||
{
|
||||
"text": "Output mentions the START module being checked",
|
||||
"passed": true,
|
||||
"evidence": "Output clearly shows 'START Module Check Results' and identifies the module"
|
||||
},
|
||||
{
|
||||
"text": "Output reports module status (partial/match/mismatch/missing)",
|
||||
"passed": true,
|
||||
"evidence": "Status: FAILED is reported, with issues labeled as WARNING and ERROR"
|
||||
},
|
||||
{
|
||||
"text": "Output identifies missing calls or issues",
|
||||
"passed": true,
|
||||
"evidence": "Lists 'Missing call to INITIA subroutine' and 'Missing call to PRDINI subroutine'"
|
||||
},
|
||||
{
|
||||
"text": "Output provides fix suggestions",
|
||||
"passed": true,
|
||||
"evidence": "Provides 3 repair suggestions including 'Add call: initia(&mut params)'"
|
||||
}
|
||||
],
|
||||
"summary": {
|
||||
"passed": 4,
|
||||
"failed": 0,
|
||||
"total": 4,
|
||||
"pass_rate": 1.0
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
START Module Check Results
|
||||
===========================
|
||||
|
||||
Status: FAILED
|
||||
|
||||
Fortran Source: start.f
|
||||
Rust Implementation: /home/fmq/.zeroclaw/workspace/SpectraRust/src/tlusty/io/start.rs
|
||||
|
||||
Issues Found:
|
||||
-------------
|
||||
1. WARNING: Rust implementation is a simplified version/placeholder
|
||||
2. ERROR: Missing call to INITIA subroutine
|
||||
3. ERROR: Missing call to PRDINI subroutine
|
||||
|
||||
Repair Suggestions:
|
||||
-------------------
|
||||
1. Need to fully implement this module
|
||||
2. Add call: initia(&mut params)
|
||||
3. Add call: prdini(&mut params)
|
||||
|
||||
Summary:
|
||||
--------
|
||||
The START module in Rust is incomplete. The Fortran START module is responsible
|
||||
for initializing the TLUSTY calculation, including calls to INITIA (general
|
||||
initialization) and PRDINI (PRD - Partial Redistribution initialization). The
|
||||
current Rust implementation appears to be a placeholder or simplified version
|
||||
that does not include these critical initialization steps.
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"eval_id": 2,
|
||||
"eval_name": "next-module-recommendation",
|
||||
"prompt": "哪个模块应该优先检查和修复?",
|
||||
"assertions": [
|
||||
{
|
||||
"text": "Output provides a list of modules with priorities",
|
||||
"description": "The response should list modules in priority order"
|
||||
},
|
||||
{
|
||||
"text": "Output shows module status and call counts",
|
||||
"description": "Priority list includes status (partial/mismatch/missing) and how many times each module is called"
|
||||
},
|
||||
{
|
||||
"text": "Output explains the recommendation logic",
|
||||
"description": "Explains why certain modules are prioritized (e.g., called many times)"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"eval_id": 3,
|
||||
"eval_name": "detailed-diff-report",
|
||||
"prompt": "对比 OPACF0 模块的 Fortran 和 Rust 实现,显示详细差异",
|
||||
"assertions": [
|
||||
{
|
||||
"text": "Output mentions OPACF0 module",
|
||||
"description": "The response should identify OPACF0 as the module being analyzed"
|
||||
},
|
||||
{
|
||||
"text": "Output shows Fortran code or control flow",
|
||||
"description": "The diff report includes Fortran source code or control flow analysis"
|
||||
},
|
||||
{
|
||||
"text": "Output shows Rust code or control flow",
|
||||
"description": "The diff report includes Rust source code or control flow analysis"
|
||||
},
|
||||
{
|
||||
"text": "Output compares calls between Fortran and Rust",
|
||||
"description": "Shows which calls match and which are missing"
|
||||
}
|
||||
]
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"expectations": [
|
||||
{
|
||||
"text": "Output mentions OPACF0 module",
|
||||
"passed": true,
|
||||
"evidence": "Report title is 'OPACF0 Fortran vs Rust Diff Report'"
|
||||
},
|
||||
{
|
||||
"text": "Output shows Fortran code or control flow",
|
||||
"passed": true,
|
||||
"evidence": "Includes 'Fortran Code Structure' section with actual code snippets"
|
||||
},
|
||||
{
|
||||
"text": "Output shows Rust code or control flow",
|
||||
"passed": true,
|
||||
"evidence": "Includes 'Rust Code Structure' section comparing with Fortran"
|
||||
},
|
||||
{
|
||||
"text": "Output compares calls between Fortran and Rust",
|
||||
"passed": true,
|
||||
"evidence": "Lists 9 missing function calls with status and Function Call Mapping table"
|
||||
}
|
||||
],
|
||||
"summary": {
|
||||
"passed": 4,
|
||||
"failed": 0,
|
||||
"total": 4,
|
||||
"pass_rate": 1.0
|
||||
}
|
||||
}
|
||||
+204
@@ -0,0 +1,204 @@
|
||||
# OPACF0 Fortran vs Rust Diff Report
|
||||
|
||||
## Overview
|
||||
|
||||
| Attribute | Value |
|
||||
|-----------|-------|
|
||||
| **Module** | OPACF0 |
|
||||
| **Status** | **FAIL** |
|
||||
| **Fortran File** | opacf0.f |
|
||||
| **Rust File** | /home/fmq/.zeroclaw/workspace/SpectraRust/src/tlusty/math/continuum/opacf0.rs |
|
||||
|
||||
---
|
||||
|
||||
## Problems Identified
|
||||
|
||||
### Missing Function Calls
|
||||
|
||||
The Rust implementation is missing calls to the following 9 functions that are present in the Fortran code:
|
||||
|
||||
| Function | Status |
|
||||
|----------|--------|
|
||||
| DWNFR0 | Missing |
|
||||
| DWNFR1 | Missing |
|
||||
| GFREE0 | Missing |
|
||||
| LINPRO | Missing |
|
||||
| OPACT1 | Missing |
|
||||
| OPADD | Missing |
|
||||
| SABOLF | Missing |
|
||||
| SGMER1 | Missing |
|
||||
| WNSTOR | Missing |
|
||||
|
||||
---
|
||||
|
||||
## Control Flow Differences
|
||||
|
||||
| Metric | Fortran | Rust |
|
||||
|--------|---------|------|
|
||||
| Control Statements | 43 | 75 |
|
||||
|
||||
### Fortran INCLUDE Files
|
||||
- BASICS.FOR
|
||||
- ATOMIC.FOR
|
||||
- MODELQ.FOR
|
||||
- ODFPAR.FOR
|
||||
- ALIPAR.FOR
|
||||
|
||||
### Fortran COMMON Block
|
||||
- `hmolab`: contains `anh2(mdepth)`, `anhm(mdepth)`
|
||||
|
||||
---
|
||||
|
||||
## Fortran Code Structure
|
||||
|
||||
```fortran
|
||||
SUBROUTINE OPACF0(ID,NFRQ)
|
||||
|
||||
C Absorption, emission, and scattering coefficients
|
||||
C at depth ID
|
||||
|
||||
C Input: ID - depth point
|
||||
C Output: ABSO - absorption coefficient array
|
||||
C EMIS - emission coefficient array
|
||||
C SCAT - scattering coefficient array
|
||||
|
||||
INCLUDE 'IMPLIC.FOR'
|
||||
INCLUDE 'BASICS.FOR'
|
||||
INCLUDE 'ATOMIC.FOR'
|
||||
INCLUDE 'MODELQ.FOR'
|
||||
INCLUDE 'ODFPAR.FOR'
|
||||
INCLUDE 'ALIPAR.FOR'
|
||||
|
||||
PARAMETER (FRH=3.28805E15, PH2=2.815D29*2., EHB=157802.77355)
|
||||
PARAMETER (CFF1=1.3727D-25,CFF2=4.3748D-10,CFF3=2.5993D-7)
|
||||
PARAMETER (C14=2.99793D14)
|
||||
PARAMETER (SGFF0 = 3.694D8)
|
||||
|
||||
common/hmolab/anh2(mdepth),anhm(mdepth)
|
||||
DIMENSION FREDG(NLMX),S(NLMX),SUM(NLMX),PRF(MFREQL)
|
||||
```
|
||||
|
||||
### Fortran Control Flow Sequence
|
||||
|
||||
1. **Initialization (TDPINI-like)**
|
||||
- Calculate temperature-related quantities
|
||||
- `CALL GFREE0(ID)`
|
||||
- Set `LASER = ITER.GT.ITLAS`
|
||||
|
||||
2. **Opacity Initialization (OPAINI-like)**
|
||||
- Set electron density scalars
|
||||
- `if(izscal.eq.1)` branch
|
||||
- `CALL DWNFR0(ID)`
|
||||
- `CALL WNSTOR(ID)`
|
||||
- `CALL SABOLF(ID)`
|
||||
|
||||
3. **Bound-Free Opacity**
|
||||
- `IF(IELHM.GT.0)` - H- molecule check
|
||||
- `IF(NFRQ.GT.NFREQC)` - frequency range check
|
||||
- `DO 10 ITR=1,NTRANS` - transition loop
|
||||
- `IF(ISPODF.GE.1)` - ODF mode check
|
||||
- `CALL LINPRO(ITR,ID,PRF)` - line profile
|
||||
|
||||
4. **Laser Mode**
|
||||
- `IF(LASER)` branch
|
||||
- `DO 30 IBFT=1,NTRANC` - bound-free transitions
|
||||
- `CALL SGMER1(FRINV,FR3INV,IMER,ID,SGME1)`
|
||||
- `CALL DWNFR1(FR,FR0(ITR),ID,IZZ,DW1)`
|
||||
|
||||
5. **Ion Loop**
|
||||
- `DO 40 ION=1,NION`
|
||||
- Multiple IT (ion type) branches
|
||||
|
||||
6. **Opacity Addition**
|
||||
- `IF(IOPADD.NE.0)` then `CALL OPADD(0,ICALL,IJ,ID)`
|
||||
- ODF handling with `ISPODF`
|
||||
|
||||
7. **Opacity Table**
|
||||
- `if(ioptab.gt.0)` then `call opact1(ij)`
|
||||
|
||||
---
|
||||
|
||||
## Rust Code Structure
|
||||
|
||||
The Rust implementation has similar structure but is missing the function calls:
|
||||
|
||||
### Present in Rust:
|
||||
- Temperature initialization (lines 1-18)
|
||||
- Electron density initialization (lines 20-42)
|
||||
- Bound-free opacity preparation (lines 44+)
|
||||
- Main transition loops
|
||||
- Ion type matching
|
||||
|
||||
### Missing in Rust:
|
||||
- All 9 function calls are commented out or not implemented
|
||||
- The code has placeholder comments like:
|
||||
- `// CALL GFREE0(ID) - 由外部调用或在此调用`
|
||||
- `// CALL DWNFR0(ID) - 下沉修正初始化`
|
||||
- `// CALL WNSTOR(ID) - 氢积分存储`
|
||||
- `// CALL SABOLF(ID) - 束缚-自由 Sa Boltzmann 因子`
|
||||
|
||||
---
|
||||
|
||||
## Recommendations
|
||||
|
||||
### Priority 1: Implement Missing Functions
|
||||
|
||||
1. **GFREE0** - Free-free Gaunt factor initialization
|
||||
2. **SABOLF** - Sa Boltzmann factor for bound-free transitions
|
||||
3. **WNSTOR** - Hydrogen integral storage
|
||||
4. **DWNFR0** - Downward correction initialization
|
||||
5. **DWNFR1** - Downward correction calculation
|
||||
6. **SGMER1** - Emergent intensity calculation
|
||||
7. **LINPRO** - Line profile calculation
|
||||
8. **OPADD** - Opacity addition
|
||||
9. **OPACT1** - Opacity table lookup
|
||||
|
||||
### Priority 2: Add COMMON Block Data
|
||||
|
||||
The `hmolab` COMMON block with:
|
||||
- `anh2(mdepth)` - H2 number density
|
||||
- `anhm(mdepth)` - H- number density
|
||||
|
||||
### Priority 3: Verify Control Flow
|
||||
|
||||
The Rust code has 75 control statements vs Fortran's 43, suggesting possible:
|
||||
- Extra conditional checks
|
||||
- More granular loop handling
|
||||
- Potential logic divergence
|
||||
|
||||
---
|
||||
|
||||
## Function Call Mapping
|
||||
|
||||
| Fortran Call | Rust Equivalent | Status |
|
||||
|--------------|-----------------|--------|
|
||||
| `CALL GFREE0(ID)` | `gfree0(&mut params)` | **TODO** |
|
||||
| `CALL DWNFR0(ID)` | `dwnfr0(&mut params)` | **TODO** |
|
||||
| `CALL WNSTOR(ID)` | `wnstor(&mut params)` | **TODO** |
|
||||
| `CALL SABOLF(ID)` | `sabolf(&mut params)` | **TODO** |
|
||||
| `CALL LINPRO(ITR,ID,PRF)` | `linpro(&mut params)` | **TODO** |
|
||||
| `CALL SGMER1(...)` | `sgmer1(&mut params)` | **TODO** |
|
||||
| `CALL DWNFR1(...)` | `dwnfr1(&mut params)` | **TODO** |
|
||||
| `CALL OPADD(...)` | `opadd(&mut params)` | **TODO** |
|
||||
| `CALL OPACT1(IJ)` | `opact1(&mut params)` | **TODO** |
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
The Rust implementation of OPACF0 is incomplete. The core structure is present but 9 critical function calls are missing. These functions handle:
|
||||
|
||||
1. **Free-free opacity** (GFREE0)
|
||||
2. **Bound-free opacity preparation** (SABOLF, WNSTOR)
|
||||
3. **Line profiles** (LINPRO)
|
||||
4. **Downward corrections** (DWNFR0, DWNFR1)
|
||||
5. **Emergent intensity** (SGMER1)
|
||||
6. **Opacity accumulation** (OPADD)
|
||||
7. **Opacity table lookup** (OPACT1)
|
||||
|
||||
Without these functions, the OPACF0 module cannot correctly calculate absorption, emission, and scattering coefficients at depth.
|
||||
|
||||
---
|
||||
|
||||
*Report generated by f2r_check.py --diff OPACF0*
|
||||
*Date: 2026-03-26*
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"expectations": [
|
||||
{
|
||||
"text": "Output mentions OPACF0 module",
|
||||
"passed": true,
|
||||
"evidence": "Report title is 'OPACF0 模块 Fortran vs Rust 实现对比报告'"
|
||||
},
|
||||
{
|
||||
"text": "Output shows Fortran code or control flow",
|
||||
"passed": true,
|
||||
"evidence": "Includes Fortran code snippets in comparison tables"
|
||||
},
|
||||
{
|
||||
"text": "Output shows Rust code or control flow",
|
||||
"passed": true,
|
||||
"evidence": "Includes Rust code snippets with comparison"
|
||||
},
|
||||
{
|
||||
"text": "Output compares calls between Fortran and Rust",
|
||||
"passed": true,
|
||||
"evidence": "Includes '未实现的功能' section with external function call comparison"
|
||||
}
|
||||
],
|
||||
"summary": {
|
||||
"passed": 4,
|
||||
"failed": 0,
|
||||
"total": 4,
|
||||
"pass_rate": 1.0
|
||||
}
|
||||
}
|
||||
+313
@@ -0,0 +1,313 @@
|
||||
# OPACF0 模块 Fortran vs Rust 实现对比报告
|
||||
|
||||
## 1. 概述
|
||||
|
||||
**OPACF0** 是 TLUSTY 中计算单深度点吸收、发射和散射系数的核心函数。
|
||||
|
||||
| 属性 | Fortran | Rust |
|
||||
|------|---------|------|
|
||||
| 文件路径 | `$TLUSTY/rust/tlusty/extracted/opacf0.f` | `src/tlusty/math/continuum/opacf0.rs` |
|
||||
| 代码行数 | ~363 行 | ~919 行 |
|
||||
| 函数签名 | `SUBROUTINE OPACF0(ID, NFRQ)` | `pub fn opacf0(id, nfrq, config, model, atomic, freq_params, output)` |
|
||||
|
||||
## 2. 架构差异
|
||||
|
||||
### 2.1 数据传递方式
|
||||
|
||||
| 方面 | Fortran | Rust |
|
||||
|------|---------|------|
|
||||
| 数据共享 | COMMON 块全局变量 | 参数结构体传递 |
|
||||
| 配置参数 | 全局变量 (ITER, ITCOMP, ISPODF 等) | `Opacf0Config` 结构体 |
|
||||
| 模型状态 | MODELQ.FOR COMMON | `Opacf0ModelState` 结构体 |
|
||||
| 原子数据 | ATOMIC.FOR COMMON | `Opacf0AtomicParams` 结构体 |
|
||||
| 输出数组 | COMMON 块中的 ABSO, EMIS, SCAT | `Opacf0Output` 结构体 |
|
||||
|
||||
### 2.2 Rust 结构体设计
|
||||
|
||||
Rust 实现使用了 4 个主要参数结构体:
|
||||
|
||||
```rust
|
||||
pub struct Opacf0Config { // 配置标志
|
||||
icompt: i32, // Compton 散射标志
|
||||
ispodf: i32, // ODF 采样标志
|
||||
ifdiel: i32, // 双电子复合标志
|
||||
iopadd: i32, // 附加不透明度标志
|
||||
izscal: i32, // 密度缩放标志
|
||||
ioptab: i32, // 表格不透明度标志
|
||||
iter: i32, // 当前迭代次数
|
||||
itlas: i32, // 激光抑制阈值
|
||||
qtlas: f64, // 激光抑制参数
|
||||
}
|
||||
|
||||
pub struct Opacf0ModelState<'a> { ... } // 温度、密度、占据数等
|
||||
pub struct Opacf0AtomicParams<'a> { ... } // 能级、跃迁、离子数据
|
||||
pub struct Opacf0FreqParams<'a> { ... } // 频率数组、Planck 函数
|
||||
pub struct Opacf0Output<'a> { ... } // 输出不透明度数组
|
||||
```
|
||||
|
||||
## 3. 算法流程对比
|
||||
|
||||
### 3.1 主流程 (完全一致)
|
||||
|
||||
| 步骤 | Fortran 代码 | Rust 代码 | 状态 |
|
||||
|------|-------------|-----------|------|
|
||||
| 1. 初始化温度量 | Lines 30-36 | Lines 328-334 | 匹配 |
|
||||
| 2. 初始化电子密度 | Lines 43-55 | Lines 343-355 | 匹配 |
|
||||
| 3. 束缚-自由预备量 | Lines 59-73 | Lines 366-396 | 匹配 |
|
||||
| 4. 自由-自由预备量 | Lines 77-85 | Lines 402-420 | 匹配 |
|
||||
| 5. Mermerges 初始化 | Lines 89-119 | Lines 426-460 | 匹配 |
|
||||
| 6. 谱线不透明度初始化 | Lines 123-162 | Lines 466-499 | 匹配 |
|
||||
| 7. 频率循环 | Lines 169-356 | Lines 507-782 | 匹配 |
|
||||
|
||||
### 3.2 物理常数对比
|
||||
|
||||
| 常量 | Fortran 值 | Rust 值 | 状态 |
|
||||
|------|-----------|---------|------|
|
||||
| FRH (Rydberg 频率) | 3.28805E15 | 3.28805e15 | 匹配 |
|
||||
| PH2 (H- 截面常数) | 2.815D29*2. | 2.815e29 * 2.0 | 匹配 |
|
||||
| EHB (氢结合能) | 157802.77355 | 157802.77355 | 匹配 |
|
||||
| CFF1 | 1.3727D-25 | 1.3727e-25 | 匹配 |
|
||||
| CFF2 | 4.3748D-10 | 4.3748e-10 | 匹配 |
|
||||
| CFF3 | 2.5993D-7 | 2.5993e-7 | 匹配 |
|
||||
| C14 | 2.99793D14 | 2.99793e14 | 匹配 |
|
||||
| SGFF0 | 3.694D8 | 3.694e8 | 匹配 |
|
||||
|
||||
## 4. 详细差异分析
|
||||
|
||||
### 4.1 索引转换 (正确处理)
|
||||
|
||||
**Fortran (1-indexed):**
|
||||
```fortran
|
||||
DO IBFT=1,NTRANC
|
||||
ITR=ITRBF(IBFT)
|
||||
II=ILOW(ITR)
|
||||
```
|
||||
|
||||
**Rust (0-indexed):**
|
||||
```rust
|
||||
for ibft in 0..atomic.ntranc {
|
||||
let itr = atomic.itrbf[ibft] as usize - 1;
|
||||
let ii = atomic.ilow[itr] as usize - 1;
|
||||
```
|
||||
|
||||
状态: **正确转换**
|
||||
|
||||
### 4.2 束缚-自由不透明度计算
|
||||
|
||||
**Fortran:**
|
||||
```fortran
|
||||
ABTRA(ITR,ID)=POPUL(II,ID)
|
||||
EMTRA(ITR,ID)=POPUL(JJ,ID)*ANE*SBF(II)*WOP(II,ID)*CORR
|
||||
```
|
||||
|
||||
**Rust:**
|
||||
```rust
|
||||
output.abtra[itr * nd + id_idx] = popul_ii;
|
||||
let emis_val = popul_jj * ane * atomic.sbf[ii] * wop_ii * corr;
|
||||
output.emtra[itr * nd + id_idx] = emis_val;
|
||||
```
|
||||
|
||||
状态: **匹配**
|
||||
|
||||
### 4.3 自由-自由不透明度计算
|
||||
|
||||
**Fortran:**
|
||||
```fortran
|
||||
SFF2(ION,ID)=EXP(FF(ION)*HKT1(ID))
|
||||
SFF3(ION,ID)=POPUL(NNEXT(ION),ID)*CHARG2(ION)*SGFF
|
||||
```
|
||||
|
||||
**Rust:**
|
||||
```rust
|
||||
output.sff2[ion_idx * nd + id_idx] = (ff_val * model.hkt1[id_idx]).exp();
|
||||
output.sff3[ion_idx * nd + id_idx] = popul_nnext * charg2 as f64 * sgff;
|
||||
```
|
||||
|
||||
状态: **匹配**
|
||||
|
||||
### 4.4 Mermerges 积分计算
|
||||
|
||||
**Fortran (递归求和):**
|
||||
```fortran
|
||||
SUM(NLMX)=S(NLMX)
|
||||
DO I=NLMX-1,II0,-1
|
||||
SUM(I)=SUM(I+1)+S(I)
|
||||
END DO
|
||||
```
|
||||
|
||||
**Rust (简化实现):**
|
||||
```rust
|
||||
fn compute_sgmsum(...) -> f64 {
|
||||
// 简化处理,缺少递归求和
|
||||
s * sgm0 / gmer[id]
|
||||
}
|
||||
```
|
||||
|
||||
状态: **不完整实现** - Rust 版本缺少完整的递归求和逻辑
|
||||
|
||||
### 4.5 频率循环中的自由-自由计算
|
||||
|
||||
**Fortran 氢型 Gaunt=1 (IT=1):**
|
||||
```fortran
|
||||
SF1=SFF3(ION,ID)*FR3INV
|
||||
SF2=SFF2(ION,ID)
|
||||
IF(FR.LT.FF(ION)) SF2=UN/XKF(ID)
|
||||
ABSOFF=SF1*SF2
|
||||
```
|
||||
|
||||
**Rust:**
|
||||
```rust
|
||||
let sf1 = output.sff3[ion * nd + id_idx] * fr3inv;
|
||||
let sf2 = if fr < atomic.ff[ion] {
|
||||
UN / output.xkf[id_idx]
|
||||
} else {
|
||||
output.sff2[ion * nd + id_idx]
|
||||
};
|
||||
sf1 * sf2
|
||||
```
|
||||
|
||||
状态: **匹配**
|
||||
|
||||
### 4.6 H- 自由-自由计算
|
||||
|
||||
**Fortran:**
|
||||
```fortran
|
||||
ABSOFF=SFFHMI(POPUL(NFIRST(IELH),ID),FR,TEMP(ID))*ELEC(ID)
|
||||
```
|
||||
|
||||
**Rust (简化实现):**
|
||||
```rust
|
||||
fn compute_sffhmi(popul_h: f64, _fr: f64, _temp: f64) -> f64 {
|
||||
// 简化实现,实际应调用 sffhmi 模块
|
||||
popul_h * CFF1
|
||||
}
|
||||
```
|
||||
|
||||
状态: **不完整实现** - Rust 使用简化公式,忽略频率和温度依赖
|
||||
|
||||
### 4.7 最终不透明度计算
|
||||
|
||||
**Fortran:**
|
||||
```fortran
|
||||
ABSO(IJ)=ABSO(IJ)-EMIS(IJ)*XKF(ID)
|
||||
EMIS(IJ)=EMIS(IJ)*XKFB(ID)
|
||||
```
|
||||
|
||||
**Rust:**
|
||||
```rust
|
||||
output.abso[ij_idx] = output.abso[ij_idx] - output.emis[ij_idx] * output.xkf[id_idx];
|
||||
output.emis[ij_idx] = output.emis[ij_idx] * output.xkfb[id_idx];
|
||||
```
|
||||
|
||||
状态: **匹配**
|
||||
|
||||
## 5. 未实现的功能
|
||||
|
||||
### 5.1 外部函数调用 (Rust 中标记为 TODO)
|
||||
|
||||
| Fortran 调用 | 功能 | Rust 状态 |
|
||||
|-------------|------|----------|
|
||||
| `CALL GFREE0(ID)` | 自由-自由 Gaunt 因子初始化 | 未调用 |
|
||||
| `CALL DWNFR0(ID)` | 下沉修正初始化 | 未调用 |
|
||||
| `CALL WNSTOR(ID)` | 氢积分存储 | 未调用 |
|
||||
| `CALL SABOLF(ID)` | 束缚-自由 Sa Boltzmann 因子 | 未调用 |
|
||||
| `CALL SGMER1(...)` | Mermerges 截面计算 | 未调用 |
|
||||
| `CALL DWNFR1(...)` | 下沉修正计算 | 未调用 |
|
||||
| `CALL LINPRO(...)` | 谱线轮廓计算 | 未调用 |
|
||||
| `CALL OPADD(...)` | 附加不透明度 | 未调用 |
|
||||
| `CALL OPACT1(IJ)` | 表格不透明度 | 未调用 |
|
||||
| `SFFHMI(...)` | H- 自由-自由截面 | 简化实现 |
|
||||
| `FFCROS(...)` | 特殊自由-自由截面 | 未实现 |
|
||||
| `GFREE1(ID,X)` | 精确 Gaunt 因子 | 未实现 |
|
||||
|
||||
### 5.2 特殊逻辑差异
|
||||
|
||||
#### 5.2.1 izscal 标志处理
|
||||
|
||||
**Fortran:**
|
||||
```fortran
|
||||
if(izscal.eq.1) then
|
||||
densi(id)=un
|
||||
densim(id)=0.
|
||||
end if
|
||||
```
|
||||
|
||||
**Rust:**
|
||||
```rust
|
||||
if config.izscal == 1 {
|
||||
model.densim[id_idx] = model.densi[id_idx] * model.wmm[id_idx];
|
||||
} else {
|
||||
model.densim[id_idx] = 0.0;
|
||||
model.densi[id_idx] = UN;
|
||||
}
|
||||
```
|
||||
|
||||
状态: **逻辑反转** - Rust 的条件分支与 Fortran 相反
|
||||
|
||||
#### 5.2.2 数组访问越界检查
|
||||
|
||||
Rust 实现添加了大量边界检查:
|
||||
|
||||
```rust
|
||||
let iatm_ii = atomic.iatm[ii] as usize - 1;
|
||||
if iatm_ii < atomic.iadop.len() && atomic.iadop[iatm_ii] > 0 && fr <= freq_params.frtabm {
|
||||
continue;
|
||||
}
|
||||
```
|
||||
|
||||
这是安全的做法,但 Fortran 假设数组大小足够。
|
||||
|
||||
## 6. 测试覆盖
|
||||
|
||||
### Rust 单元测试
|
||||
|
||||
```rust
|
||||
#[test]
|
||||
fn test_opacf0_config_default() { ... }
|
||||
|
||||
#[test]
|
||||
fn test_constants() { ... }
|
||||
|
||||
#[test]
|
||||
fn test_helper_functions() { ... }
|
||||
```
|
||||
|
||||
### 缺失的测试
|
||||
|
||||
- 无与 Fortran 输出的数值对比测试
|
||||
- 无完整工作流集成测试
|
||||
- 无边界条件测试
|
||||
|
||||
## 7. 总结
|
||||
|
||||
### 完成度评估
|
||||
|
||||
| 组件 | 完成度 | 备注 |
|
||||
|------|--------|------|
|
||||
| 主框架结构 | 100% | 所有主要循环和分支存在 |
|
||||
| 物理常数 | 100% | 完全匹配 |
|
||||
| 束缚-自由计算 | 90% | 缺少截面函数 |
|
||||
| 自由-自由计算 | 80% | 缺少精确 Gaunt 因子 |
|
||||
| Mermerges 处理 | 50% | 积分逻辑不完整 |
|
||||
| 谱线不透明度 | 70% | 缺少 LINPRO 调用 |
|
||||
| 外部模块调用 | 30% | 大多数为占位符 |
|
||||
| izscal 逻辑 | **错误** | 条件分支反转 |
|
||||
|
||||
### 关键问题
|
||||
|
||||
1. **izscal 逻辑错误**: Rust 中 `izscal == 1` 和 `izscal != 1` 的处理与 Fortran 相反
|
||||
2. **Mermerges 积分不完整**: 缺少递归求和逻辑
|
||||
3. **H- 自由-自由简化**: `compute_sffhmi` 使用简化公式
|
||||
4. **多个外部函数未实现**: GFREE0, DWNFR0, WNSTOR, SABOLF, SGMER1, DWNFR1, LINPRO, OPADD, OPACT1
|
||||
|
||||
### 建议修复优先级
|
||||
|
||||
1. **高**: 修复 izscal 逻辑错误
|
||||
2. **高**: 实现 Mermerges 递归求和
|
||||
3. **中**: 实现 SFFHMI 完整计算
|
||||
4. **中**: 添加与 Fortran 的数值对比测试
|
||||
5. **低**: 实现其他外部函数调用
|
||||
|
||||
---
|
||||
|
||||
*报告生成时间: 2026-03-26*
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"eval_id": 4,
|
||||
"eval_name": "dependency-tracking",
|
||||
"prompt": "从 INITIA 模块开始追踪依赖,告诉我应该检查哪些模块",
|
||||
"assertions": [
|
||||
{
|
||||
"text": "Output identifies INITIA as the starting point",
|
||||
"description": "The response should show dependencies starting from INITIA"
|
||||
},
|
||||
{
|
||||
"text": "Output lists modules that INITIA depends on",
|
||||
"description": "Shows the dependency chain from INITIA"
|
||||
},
|
||||
{
|
||||
"text": "Output includes module status information",
|
||||
"description": "Each module in the chain has status (partial/mismatch/missing)"
|
||||
}
|
||||
]
|
||||
}
|
||||
+254
@@ -0,0 +1,254 @@
|
||||
# INITIA 模块依赖追踪分析
|
||||
|
||||
## 概述
|
||||
|
||||
INITIA 是 TLUSTY 的输入和初始化驱动程序,位于 `tlusty208.f` 第 137-879 行。该模块负责读取所有输入参数、设置频率网格、初始化原子数据、读取模型大气、以及设置迭代控制参数。
|
||||
|
||||
---
|
||||
|
||||
## 1. COMMON 块依赖 (INCLUDE 文件)
|
||||
|
||||
INITIA 直接包含以下 COMMON 块:
|
||||
|
||||
| INCLUDE 文件 | 对应 Rust 结构 | 状态 |
|
||||
|-------------|---------------|------|
|
||||
| `IMPLIC.FOR` | (隐式类型声明,无需翻译) | - |
|
||||
| `BASICS.FOR` | `state/constants.rs` | 已完成 |
|
||||
| `ATOMIC.FOR` | `state/atomic.rs` | 已完成 |
|
||||
| `MODELQ.FOR` | `state/model.rs` | 已完成 |
|
||||
| `ITERAT.FOR` | `state/iterat.rs` | 已完成 |
|
||||
| `ODFPAR.FOR` | `state/odfpar.rs` | 已完成 |
|
||||
| `ALIPAR.FOR` | `state/alipar.rs` | 已完成 |
|
||||
|
||||
**额外 COMMON 块 (在 INITIA 内部定义):**
|
||||
- `STRPAR` - 迭代控制参数
|
||||
- `INUNIT` - 输入文件单元号
|
||||
- `freqcl` - 频率范围控制
|
||||
|
||||
---
|
||||
|
||||
## 2. 直接调用的子程序依赖
|
||||
|
||||
### 2.1 输入读取相关
|
||||
|
||||
| 子程序 | 功能 | Fortran 文件 | Rust 状态 | 位置 |
|
||||
|--------|------|-------------|-----------|------|
|
||||
| `READBF` | 读取输入缓冲区 | `readbf.f` | **已完成** | `io/reader.rs` |
|
||||
| `NSTPAR` | 读取标准参数 | `nstpar.f` | **已完成** | `io/nstpar.rs` |
|
||||
| `STATE` | 状态方程初始化 | `state.f` | **已完成** | `io/state.rs` |
|
||||
| `RDATA` | 读取原子数据 | `rdata.f` | **已完成** | `math/io/rdata.rs` |
|
||||
| `RDATAX` | 读取扩展数据 | `rdatax.f` | **已完成** | `math/io/rdatax.rs` |
|
||||
| `INPMOD` | 读取输入模型 | `inpmod.f` | **已完成** | `io/inpmod.rs` |
|
||||
|
||||
### 2.2 频率和权重设置
|
||||
|
||||
| 子程序 | 功能 | Fortran 文件 | Rust 状态 | 位置 |
|
||||
|--------|------|-------------|-----------|------|
|
||||
| `INIFRC` | 初始化频率点 | `inifrc.f` | **已完成** | `io/inifrc.rs` |
|
||||
| `INIFRT` | 从表初始化频率 | `inifrt.f` | **未检查** | - |
|
||||
| `INIFRS` | 初始化采样频率 | `inifrs.f` | **已完成** (纯函数) | `io/inifrs.rs` |
|
||||
| `SRTFRQ` | 频率排序 | `srtfrq.f` | **已完成** (纯函数) | `io/srtfrq.rs` |
|
||||
| `CORRWM` | 校正权重 | `corrwm.f` | **已完成** (纯函数) | `io/corrwm.rs` |
|
||||
|
||||
### 2.3 不透明度表相关
|
||||
|
||||
| 子程序 | 功能 | Fortran 文件 | Rust 状态 | 位置 |
|
||||
|--------|------|-------------|-----------|------|
|
||||
| `TABINI` | 初始化不透明度表 | `tabini.f` | **已完成** | `io/tabini.rs` |
|
||||
| `TABINT` | 插值不透明度表 | `tabint.f` | **未检查** | - |
|
||||
| `CHCTAB` | 检查表一致性 | `chctab.f` | **已完成** (纯函数) | `math/atomic/chctab.rs` |
|
||||
|
||||
### 2.4 谱线和跃迁设置
|
||||
|
||||
| 子程序 | 功能 | Fortran 文件 | Rust 状态 | 位置 |
|
||||
|--------|------|-------------|-----------|------|
|
||||
| `LEVSET` | 设置能级 | `levset.f` | **已完成** (纯函数) | `math/eos/levset.rs` |
|
||||
| `LINSET` | 设置谱线 | `linset.f` | **已完成** (纯函数) | `io/linset.rs` |
|
||||
| `LINSPL` | 谱线采样 | `linspl.f` | **已完成** (纯函数) | `io/linspl.rs` |
|
||||
| `TRAINI` | 初始化跃迁 | `traini.f` | **已完成** (纯函数) | `io/traini.rs` |
|
||||
| `DOPGAM` | 多普勒展宽 | `dopgam.f` | **已完成** (纯函数) | `math/continuum/dopgam.rs` |
|
||||
|
||||
### 2.5 ODF (Opacity Distribution Function) 相关
|
||||
|
||||
| 子程序 | 功能 | Fortran 文件 | Rust 状态 | 位置 |
|
||||
|--------|------|-------------|-----------|------|
|
||||
| `ODFHYS` | ODF 历史 | `odfhys.f` | **已完成** (纯函数) | `io/odfhys.rs` |
|
||||
| `ODFSET` | ODF 设置 | `odfset.f` | **已完成** | `io/odfset.rs` |
|
||||
| `IROSET` | 铁线采样设置 | `iroset.f` | **已完成** | `io/iroset.rs` |
|
||||
|
||||
### 2.6 辐射传输相关
|
||||
|
||||
| 子程序 | 功能 | Fortran 文件 | Rust 状态 | 位置 |
|
||||
|--------|------|-------------|-----------|------|
|
||||
| `RTEANG` | 辐射传输角度 | `rteang.f` | **未检查** | - |
|
||||
| `RAYINI` | 射线初始化 | `rayini.f` | **已完成** | `io/rayini.rs` |
|
||||
|
||||
### 2.7 不透明度计算
|
||||
|
||||
| 子程序 | 功能 | Fortran 文件 | Rust 状态 | 位置 |
|
||||
|--------|------|-------------|-----------|------|
|
||||
| `OPADD0` | 添加不透明度 | `opadd0.f` | **已完成** (纯函数) | `math/continuum/opadd0.rs` |
|
||||
| `OPAHST` | H/He 不透明度历史 | `opahst.f` | **已完成** (纯函数) | `math/continuum/opahst.rs` |
|
||||
| `SIGK` | 光电离截面 | `sigk.f` | **已完成** (纯函数) | `math/atomic/sigk.rs` |
|
||||
| `SIGAVE` | 平均截面 | `sigave.f` | **已完成** (纯函数) | `math/continuum/sigave.rs` |
|
||||
|
||||
### 2.8 LTE 灰大气模型
|
||||
|
||||
| 子程序 | 功能 | Fortran 文件 | Rust 状态 | 位置 |
|
||||
|--------|------|-------------|-----------|------|
|
||||
| `LTEGR` | LTE 灰大气 | `ltegr.f` | **已完成** | `io/ltegr.rs` |
|
||||
| `LTEGRD` | LTE 灰大气 (盘) | `ltegrd.f` | **已完成** | `io/ltegrd.rs` |
|
||||
|
||||
### 2.9 输出和工具
|
||||
|
||||
| 子程序 | 功能 | Fortran 文件 | Rust 状态 | 位置 |
|
||||
|--------|------|-------------|-----------|------|
|
||||
| `NSTOUT` | 输出标准参数 | `nstout.f` | **已完成** (纯函数) | `io/nstout.rs` |
|
||||
| `DMDER` | 深度导数 | `dmder.f` | **未检查** | - |
|
||||
| `QUIT` | 错误退出 | `quit.f` | **已完成** (纯函数) | 工具函数 |
|
||||
| `INTERP` | 插值 | `interp.f` | **已完成** (纯函数) | `math/interpolation/interp.rs` |
|
||||
| `GOMINI` | 初始化 GoMini | `gomini.f` | **未检查** | - |
|
||||
|
||||
### 2.10 磁盘模型相关 (可选)
|
||||
|
||||
| 子程序 | 功能 | Fortran 文件 | Rust 状态 | 位置 |
|
||||
|--------|------|-------------|-----------|------|
|
||||
| `INPDIS` | 输入磁盘参数 | `inpdis.f` | **已完成** | `io/inpdis.rs` |
|
||||
| `CHANGE` | 修改模型参数 | `change.f` | **已完成** | `io/change.rs` |
|
||||
|
||||
---
|
||||
|
||||
## 3. 间接依赖 (需要进一步追踪)
|
||||
|
||||
以下子程序被 INITIA 直接调用,但它们内部还有更多依赖:
|
||||
|
||||
### 3.1 STATE 子程序依赖链
|
||||
- `PARTF` - 配分函数
|
||||
- `RHONEN` - 密度/电子密度
|
||||
- `WNSTOR` - 存储权重
|
||||
- `SABOLF` - 玻尔兹曼分布
|
||||
- `RATMAT` - 速率矩阵
|
||||
- `LEVSOL` - 能级求解
|
||||
|
||||
### 3.2 LTEGR 子程序依赖链
|
||||
- `ROSSOP` - Rosseland 不透明度
|
||||
- `ELDENS` - 电子密度计算
|
||||
- `MEANOPT` / `LTE_MEANOPT` - 平均不透明度
|
||||
|
||||
### 3.3 RDATA 子程序依赖链
|
||||
- 文件 I/O 操作
|
||||
- 原子数据解析
|
||||
|
||||
---
|
||||
|
||||
## 4. 需要检查的模块清单
|
||||
|
||||
### 高优先级 (INITIA 核心功能)
|
||||
|
||||
1. **输入读取**:
|
||||
- `READBF` - 已完成
|
||||
- `NSTPAR` - 已完成
|
||||
- `STATE` - 已完成
|
||||
- `RDATA` - 已完成
|
||||
- `INPMOD` - 已完成
|
||||
|
||||
2. **频率设置**:
|
||||
- `INIFRC` - 已完成
|
||||
- `INIFRT` - **需要检查**
|
||||
- `SRTFRQ` - 已完成
|
||||
- `CORRWM` - 已完成
|
||||
|
||||
3. **不透明度表**:
|
||||
- `TABINI` - 已完成
|
||||
- `TABINT` - **需要检查**
|
||||
- `CHCTAB` - 已完成
|
||||
|
||||
4. **LTE 灰大气**:
|
||||
- `LTEGR` - 已完成
|
||||
- `ROSSOP` - 已完成
|
||||
- `ELDENS` - 已完成
|
||||
- `MEANOPT` - 已完成
|
||||
|
||||
### 中优先级 (ODF/采样模式)
|
||||
|
||||
5. **ODF 设置**:
|
||||
- `ODFSET` - 已完成
|
||||
- `ODFHYS` - 已完成
|
||||
- `IROSET` - 已完成
|
||||
- `INIFRS` - 已完成
|
||||
|
||||
6. **谱线设置**:
|
||||
- `LINSET` - 已完成
|
||||
- `LINSPL` - 已完成
|
||||
- `TRAINI` - 已完成
|
||||
- `DOPGAM` - 已完成
|
||||
|
||||
### 低优先级 (可选功能)
|
||||
|
||||
7. **辐射传输**:
|
||||
- `RTEANG` - **需要检查**
|
||||
- `RAYINI` - 已完成
|
||||
|
||||
8. **不透明度扩展**:
|
||||
- `OPADD0` - 已完成
|
||||
- `OPAHST` - 已完成
|
||||
- `SIGK` - 已完成
|
||||
- `SIGAVE` - 已完成
|
||||
|
||||
9. **输出**:
|
||||
- `NSTOUT` - 已完成
|
||||
- `DMDER` - **需要检查**
|
||||
|
||||
10. **工具**:
|
||||
- `GOMINI` - **需要检查**
|
||||
- `INTERP` - 已完成
|
||||
|
||||
---
|
||||
|
||||
## 5. 纯函数 vs 状态依赖
|
||||
|
||||
### 纯函数 (无 COMMON 依赖,易于测试)
|
||||
根据 `_PURE_UNITS.txt`,以下 INITIA 依赖的子程序是纯函数:
|
||||
- CORRWM, DOPGAM, INIFRS, LEVSET, LINSET, LINSPL, OPADD0, OPAHST, SIGAVE, SIGK, SRTFRQ, TRAINI, INTERP, QUIT, NSTOUT
|
||||
|
||||
### 状态依赖 (需要 COMMON 块)
|
||||
- READBF, NSTPAR, STATE, RDATA, INPMOD, INIFRC, TABINI, LTEGR, ODFSET, IROSET, CHANGE, INPDIS
|
||||
|
||||
---
|
||||
|
||||
## 6. 建议检查顺序
|
||||
|
||||
1. **首先检查** `TABINT` - 不透明度表插值 (用于 `ioptab != 0` 情况)
|
||||
2. **然后检查** `INIFRT` - 从表读取频率 (用于 `ioptab > 0` 情况)
|
||||
3. **接着检查** `RTEANG` - 辐射传输角度设置
|
||||
4. **最后检查** `DMDER` 和 `GOMINI` - 工具函数
|
||||
|
||||
---
|
||||
|
||||
## 7. 总结
|
||||
|
||||
INITIA 模块共有 **约 30 个直接子程序依赖**。根据现有 Rust 代码库:
|
||||
- **已完成**: 约 25 个模块
|
||||
- **需要检查**: 5 个模块 (TABINT, INIFRT, RTEANG, DMDER, GOMINI)
|
||||
- **纯函数**: 约 15 个 (易于单元测试)
|
||||
- **状态依赖**: 约 15 个 (需要传入状态结构)
|
||||
|
||||
关键依赖链:
|
||||
```
|
||||
INITIA
|
||||
├── READBF → (输入缓冲区)
|
||||
├── NSTPAR → (标准参数)
|
||||
├── STATE → PARTF, RHONEN, WNSTOR, SABOLF, RATMAT, LEVSOL
|
||||
├── RDATA → (原子数据读取)
|
||||
├── INIFRC / INIFRT / INIFRS → (频率设置)
|
||||
├── LEVSET → (能级设置)
|
||||
├── LINSET → (谱线设置)
|
||||
├── ODFSET / IROSET → (ODF 设置)
|
||||
├── TRAINI → (跃迁初始化)
|
||||
├── TABINI / TABINT → (不透明度表)
|
||||
├── LTEGR → ROSSOP, ELDENS, MEANOPT
|
||||
├── OPADD0 / OPAHST → (不透明度)
|
||||
├── RTEANG → (辐射传输角度)
|
||||
├── NSTOUT → (输出)
|
||||
└── DMDER → (深度导数)
|
||||
```
|
||||
+209
@@ -0,0 +1,209 @@
|
||||
# INITIA 模块检查清单
|
||||
|
||||
## 需要检查的模块详细列表
|
||||
|
||||
### 1. TABINT - 不透明度表插值
|
||||
|
||||
**Fortran 文件**: `/home/fmq/program/tlusty/tl208-s54/rust/tlusty/extracted/tabint.f`
|
||||
|
||||
**功能**: 将不透明度表插值到当前频率网格
|
||||
|
||||
**INITIA 调用位置**: 第 479 行
|
||||
```fortran
|
||||
if(ioptab.ne.0) then
|
||||
call tabint
|
||||
call rayini
|
||||
...
|
||||
end if
|
||||
```
|
||||
|
||||
**条件**: 仅当 `ioptab != 0` 时调用
|
||||
|
||||
**依赖**: 需要检查
|
||||
|
||||
---
|
||||
|
||||
### 2. INIFRT - 从表读取频率
|
||||
|
||||
**Fortran 文件**: `/home/fmq/program/tlusty/tl208-s54/rust/tlusty/extracted/inifrt.f`
|
||||
|
||||
**功能**: 从不透明度表读取频率点
|
||||
|
||||
**INITIA 调用位置**: 第 390 行
|
||||
```fortran
|
||||
IF(IOPTAB.GT.0) THEN
|
||||
CALL INIFRT
|
||||
END IF
|
||||
```
|
||||
|
||||
**条件**: 仅当 `ioptab > 0` 且 `NFREAD > 0` 且 `ISPODF == 0` 时调用
|
||||
|
||||
**依赖**: 需要检查
|
||||
|
||||
---
|
||||
|
||||
### 3. RTEANG - 辐射传输角度设置
|
||||
|
||||
**Fortran 文件**: `/home/fmq/program/tlusty/tl208-s54/rust/tlusty/extracted/rteang.f`
|
||||
|
||||
**功能**: 设置辐射传输的角度网格
|
||||
|
||||
**INITIA 调用位置**: 第 547 行
|
||||
```fortran
|
||||
CALL RTEANG
|
||||
```
|
||||
|
||||
**无条件调用**
|
||||
|
||||
**依赖**: 需要检查
|
||||
|
||||
---
|
||||
|
||||
### 4. DMDER - 深度导数计算
|
||||
|
||||
**Fortran 文件**: `/home/fmq/program/tlusty/tl208-s54/rust/tlusty/extracted/dmder.f`
|
||||
|
||||
**功能**: 计算深度变量的导数
|
||||
|
||||
**INITIA 调用位置**: 第 848 行
|
||||
```fortran
|
||||
CALL DMDER
|
||||
```
|
||||
|
||||
**无条件调用** (在 LTEGR 之后)
|
||||
|
||||
**依赖**: 需要检查
|
||||
|
||||
---
|
||||
|
||||
### 5. GOMINI - GoMini 初始化
|
||||
|
||||
**Fortran 文件**: `/home/fmq/program/tlusty/tl208-s54/rust/tlusty/extracted/gomini.f`
|
||||
|
||||
**功能**: 初始化 Go 相关变量
|
||||
|
||||
**INITIA 调用位置**: 第 623 行
|
||||
```fortran
|
||||
call gomini
|
||||
```
|
||||
|
||||
**无条件调用**
|
||||
|
||||
**依赖**: 需要检查
|
||||
|
||||
---
|
||||
|
||||
## 已完成的模块确认列表
|
||||
|
||||
### 输入/输出模块
|
||||
|
||||
| 模块 | Fortran | Rust | 状态 |
|
||||
|------|---------|------|------|
|
||||
| READBF | readbf.f | io/reader.rs | OK |
|
||||
| NSTPAR | nstpar.f | io/nstpar.rs | OK |
|
||||
| NSTOUT | nstout.f | io/nstout.rs | OK (纯函数) |
|
||||
| INPMOD | inpmod.f | io/inpmod.rs | OK |
|
||||
| WRITER | - | io/writer.rs | OK |
|
||||
|
||||
### 状态/原子模块
|
||||
|
||||
| 模块 | Fortran | Rust | 状态 |
|
||||
|------|---------|------|------|
|
||||
| STATE | state.f | io/state.rs | OK |
|
||||
| RDATA | rdata.f | math/io/rdata.rs | OK |
|
||||
| RDATAX | rdatax.f | math/io/rdatax.rs | OK |
|
||||
| LEVSET | levset.f | math/eos/levset.rs | OK (纯函数) |
|
||||
|
||||
### 频率模块
|
||||
|
||||
| 模块 | Fortran | Rust | 状态 |
|
||||
|------|---------|------|------|
|
||||
| INIFRC | inifrc.f | io/inifrc.rs | OK |
|
||||
| INIFRS | inifrs.f | io/inifrs.rs | OK (纯函数) |
|
||||
| SRTFRQ | srtfrq.f | io/srtfrq.rs | OK (纯函数) |
|
||||
| CORRWM | corrwm.f | io/corrwm.rs | OK (纯函数) |
|
||||
|
||||
### 不透明度表模块
|
||||
|
||||
| 模块 | Fortran | Rust | 状态 |
|
||||
|------|---------|------|------|
|
||||
| TABINI | tabini.f | io/tabini.rs | OK |
|
||||
| CHCTAB | chctab.f | math/atomic/chctab.rs | OK (纯函数) |
|
||||
|
||||
### 谱线/跃迁模块
|
||||
|
||||
| 模块 | Fortran | Rust | 状态 |
|
||||
|------|---------|------|------|
|
||||
| LINSET | linset.f | io/linset.rs | OK (纯函数) |
|
||||
| LINSPL | linspl.f | io/linspl.rs | OK (纯函数) |
|
||||
| TRAINI | traini.f | io/traini.rs | OK (纯函数) |
|
||||
| DOPGAM | dopgam.f | math/continuum/dopgam.rs | OK (纯函数) |
|
||||
|
||||
### ODF 模块
|
||||
|
||||
| 模块 | Fortran | Rust | 状态 |
|
||||
|------|---------|------|------|
|
||||
| ODFSET | odfset.f | io/odfset.rs | OK |
|
||||
| ODFHYS | odfhys.f | io/odfhys.rs | OK (纯函数) |
|
||||
| IROSET | iroset.f | io/iroset.rs | OK |
|
||||
|
||||
### LTE 灰大气模块
|
||||
|
||||
| 模块 | Fortran | Rust | 状态 |
|
||||
|------|---------|------|------|
|
||||
| LTEGR | ltegr.f | io/ltegr.rs | OK |
|
||||
| LTEGRD | ltegrd.f | io/ltegrd.rs | OK |
|
||||
| ROSSOP | rossop.f | math/temperature/rossop.rs | OK (纯函数) |
|
||||
| ELDENS | eldens.f | math/eos/eldens.rs | OK |
|
||||
| MEANOPT | meanopt.f | math/opacity/meanopt.rs | OK (纯函数) |
|
||||
|
||||
### 不透明度计算模块
|
||||
|
||||
| 模块 | Fortran | Rust | 状态 |
|
||||
|------|---------|------|------|
|
||||
| OPADD0 | opadd0.f | math/continuum/opadd0.rs | OK (纯函数) |
|
||||
| OPAHST | opahst.f | math/continuum/opahst.rs | OK (纯函数) |
|
||||
| SIGK | sigk.f | math/atomic/sigk.rs | OK (纯函数) |
|
||||
| SIGAVE | sigave.f | math/continuum/sigave.rs | OK (纯函数) |
|
||||
|
||||
### 辐射传输模块
|
||||
|
||||
| 模块 | Fortran | Rust | 状态 |
|
||||
|------|---------|------|------|
|
||||
| RAYINI | rayini.f | io/rayini.rs | OK |
|
||||
|
||||
### 可选/磁盘模块
|
||||
|
||||
| 模块 | Fortran | Rust | 状态 |
|
||||
|------|---------|------|------|
|
||||
| INPDIS | inpdis.f | io/inpdis.rs | OK |
|
||||
| CHANGE | change.f | io/change.rs | OK |
|
||||
|
||||
### 工具模块
|
||||
|
||||
| 模块 | Fortran | Rust | 状态 |
|
||||
|------|---------|------|------|
|
||||
| INTERP | interp.f | math/interpolation/interp.rs | OK (纯函数) |
|
||||
| QUIT | quit.f | 工具函数 | OK (纯函数) |
|
||||
|
||||
---
|
||||
|
||||
## 检查优先级建议
|
||||
|
||||
1. **高优先级** (基本功能必需):
|
||||
- RTEANG (辐射传输角度,无条件调用)
|
||||
- DMDER (深度导数,无条件调用)
|
||||
- GOMINI (初始化,无条件调用)
|
||||
|
||||
2. **中优先级** (不透明度表功能):
|
||||
- TABINT (表插值,ioptab != 0 时调用)
|
||||
- INIFRT (表频率,ioptab > 0 时调用)
|
||||
|
||||
---
|
||||
|
||||
## 下一步行动
|
||||
|
||||
1. 读取并分析 5 个未检查模块的 Fortran 源码
|
||||
2. 检查是否有对应的 Rust 实现
|
||||
3. 验证 Rust 实现的正确性
|
||||
4. 编写单元测试 (特别是纯函数)
|
||||
@@ -0,0 +1,491 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
COMMON 变量映射数据库
|
||||
|
||||
解析 Fortran COMMON 块定义和 Rust struct 字段,构建完整的变量映射关系。
|
||||
|
||||
核心功能:
|
||||
- parse_all_commons() — 解析 Fortran COMMON 定义
|
||||
- parse_rust_structs() — 解析 Rust struct 字段
|
||||
- build_mapping() — 交叉引用生成完整映射
|
||||
- get_vars_for_module(module_name) — 返回某模块用到的所有 COMMON 变量
|
||||
"""
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from typing import Dict, List, Optional, Tuple, Set
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
# ============================================================================
|
||||
# 路径配置
|
||||
# ============================================================================
|
||||
|
||||
FORTRAN_COMMON_DIR = "/home/fmq/program/tlusty/tl208-s54/tlusty"
|
||||
RUST_STATE_DIR = "/home/fmq/.zeroclaw/workspace/SpectraRust/src/tlusty/state"
|
||||
EXTRACTED_DIR = "/home/fmq/program/tlusty/tl208-s54/rust/tlusty/extracted"
|
||||
|
||||
# Fortran COMMON 定义文件
|
||||
COMMON_FILES = [
|
||||
"BASICS.FOR", "ATOMIC.FOR", "MODELQ.FOR", "ARRAY1.FOR",
|
||||
"ITERAT.FOR", "ALIPAR.FOR", "ODFPAR.FOR",
|
||||
]
|
||||
|
||||
# ============================================================================
|
||||
# 数据结构
|
||||
# ============================================================================
|
||||
|
||||
@dataclass
|
||||
class CommonVar:
|
||||
"""COMMON 块变量"""
|
||||
name: str # Fortran 变量名 (大写)
|
||||
common_block: str # 所属 COMMON 块名
|
||||
dims: List[str] = field(default_factory=list) # 维度 (如 ['MTRANS'])
|
||||
rust_field: Optional[str] = None # 对应 Rust 字段名
|
||||
rust_struct: Optional[str] = None # 对应 Rust struct 名
|
||||
rust_file: Optional[str] = None # 对应 Rust 文件路径
|
||||
is_2d: bool = False # 是否是 2D 数组
|
||||
fortran_dims_raw: str = "" # 原始维度字符串 (如 "3,MHOD")
|
||||
|
||||
@dataclass
|
||||
class CommonBlock:
|
||||
"""COMMON 块"""
|
||||
name: str # COMMON 块名
|
||||
file: str # 定义文件
|
||||
variables: List[CommonVar] = field(default_factory=list)
|
||||
rust_struct: Optional[str] = None # 对应 Rust struct 名
|
||||
rust_file: Optional[str] = None # 对应 Rust 文件
|
||||
|
||||
@dataclass
|
||||
class RustStruct:
|
||||
"""Rust struct 信息"""
|
||||
name: str
|
||||
file: str
|
||||
common_name: Optional[str] = None # 对应的 COMMON 块名
|
||||
fields: Dict[str, str] = field(default_factory=dict) # field_name -> type_str
|
||||
|
||||
# ============================================================================
|
||||
# Fortran COMMON 解析
|
||||
# ============================================================================
|
||||
|
||||
def _join_continuation_lines(content: str) -> str:
|
||||
"""合并 Fortran 续行"""
|
||||
lines = content.split('\n')
|
||||
joined = []
|
||||
for line in lines:
|
||||
if not line:
|
||||
continue
|
||||
# 跳过注释行
|
||||
if len(line) > 0 and line[0].upper() in ('C', '!', '*'):
|
||||
continue
|
||||
# 检查是否有续行标记 (第6列是 * 或 数字或非空)
|
||||
if joined and len(line) >= 6 and line[5] not in (' ', '0', '\n'):
|
||||
# 续行:去掉前6列,追加到上一行
|
||||
joined[-1] = joined[-1].rstrip() + ' ' + line[6:].strip()
|
||||
else:
|
||||
joined.append(line)
|
||||
return '\n'.join(joined)
|
||||
|
||||
|
||||
def parse_common_block(content: str, filename: str) -> List[CommonBlock]:
|
||||
"""解析一个 Fortran 文件中的所有 COMMON 块"""
|
||||
blocks = []
|
||||
joined = _join_continuation_lines(content)
|
||||
|
||||
# 匹配 COMMON/BLOCKNAME/var1,var2,...
|
||||
# 处理多个 COMMON 语句可能属于同一个块
|
||||
pattern = r'COMMON\s*/\s*(\w+)\s*/\s*(.+?)(?=\n\s*COMMON|\n\s*PARAMETER|\n\s*REAL|\n\s*INTEGER|\n\s*LOGICAL|\n\s*CHARACTER|\n\s*$|\nC|\n!|\Z)'
|
||||
|
||||
matches = re.finditer(pattern, joined, re.IGNORECASE | re.MULTILINE)
|
||||
|
||||
# 收集每个块的所有变量声明
|
||||
block_vars: Dict[str, List[str]] = {}
|
||||
for match in matches:
|
||||
block_name = match.group(1).upper()
|
||||
vars_str = match.group(2).strip()
|
||||
# 去掉行尾的 Fortran 注释
|
||||
if '!' in vars_str:
|
||||
vars_str = vars_str[:vars_str.index('!')].strip()
|
||||
# 追加到该块的变量列表
|
||||
if block_name not in block_vars:
|
||||
block_vars[block_name] = []
|
||||
block_vars[block_name].append(vars_str)
|
||||
|
||||
for block_name, var_lists in block_vars.items():
|
||||
all_vars_str = ','.join(var_lists)
|
||||
variables = _parse_var_list(all_vars_str, block_name)
|
||||
blocks.append(CommonBlock(
|
||||
name=block_name,
|
||||
file=filename,
|
||||
variables=variables,
|
||||
))
|
||||
|
||||
return blocks
|
||||
|
||||
|
||||
def _parse_var_list(vars_str: str, block_name: str) -> List[CommonVar]:
|
||||
"""解析变量列表字符串,返回 CommonVar 列表"""
|
||||
variables = []
|
||||
# 按逗号分割,但要处理括号内的逗号
|
||||
parts = _split_respecting_parens(vars_str)
|
||||
|
||||
for part in parts:
|
||||
part = part.strip()
|
||||
if not part:
|
||||
continue
|
||||
# 匹配 VARNAME(DIMS) 或 VARNAME
|
||||
m = re.match(r'^(\w+)\(([^)]+)\)$', part, re.IGNORECASE)
|
||||
if m:
|
||||
name = m.group(1).upper()
|
||||
dims_str = m.group(2)
|
||||
dims = [d.strip().upper() for d in dims_str.split(',')]
|
||||
is_2d = len(dims) >= 2
|
||||
variables.append(CommonVar(
|
||||
name=name,
|
||||
common_block=block_name,
|
||||
dims=dims,
|
||||
is_2d=is_2d,
|
||||
fortran_dims_raw=dims_str,
|
||||
))
|
||||
else:
|
||||
name = part.upper()
|
||||
# 过滤非变量名
|
||||
if re.match(r'^[A-Z]\w*$', name):
|
||||
variables.append(CommonVar(
|
||||
name=name,
|
||||
common_block=block_name,
|
||||
))
|
||||
|
||||
return variables
|
||||
|
||||
|
||||
def _split_respecting_parens(s: str) -> List[str]:
|
||||
"""按逗号分割,但忽略括号内的逗号"""
|
||||
parts = []
|
||||
depth = 0
|
||||
current = []
|
||||
for c in s:
|
||||
if c == '(':
|
||||
depth += 1
|
||||
current.append(c)
|
||||
elif c == ')':
|
||||
depth -= 1
|
||||
current.append(c)
|
||||
elif c == ',' and depth == 0:
|
||||
parts.append(''.join(current))
|
||||
current = []
|
||||
else:
|
||||
current.append(c)
|
||||
if current:
|
||||
parts.append(''.join(current))
|
||||
return parts
|
||||
|
||||
|
||||
def parse_all_commons() -> Dict[str, CommonBlock]:
|
||||
"""解析所有 Fortran COMMON 定义文件,返回 {block_name: CommonBlock}"""
|
||||
all_blocks: Dict[str, CommonBlock] = {}
|
||||
|
||||
for filename in COMMON_FILES:
|
||||
fpath = os.path.join(FORTRAN_COMMON_DIR, filename)
|
||||
if not os.path.exists(fpath):
|
||||
continue
|
||||
with open(fpath, 'r', encoding='utf-8', errors='ignore') as f:
|
||||
content = f.read()
|
||||
|
||||
blocks = parse_common_block(content, filename)
|
||||
for block in blocks:
|
||||
if block.name in all_blocks:
|
||||
# 追加变量(可能同一块在不同文件中有补充定义)
|
||||
all_blocks[block.name].variables.extend(block.variables)
|
||||
else:
|
||||
all_blocks[block.name] = block
|
||||
|
||||
return all_blocks
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Rust Struct 解析
|
||||
# ============================================================================
|
||||
|
||||
def parse_rust_structs() -> List[RustStruct]:
|
||||
"""解析所有 Rust state struct,提取字段和 COMMON 对应关系"""
|
||||
structs = []
|
||||
|
||||
if not os.path.isdir(RUST_STATE_DIR):
|
||||
return structs
|
||||
|
||||
for fname in sorted(os.listdir(RUST_STATE_DIR)):
|
||||
if not fname.endswith('.rs'):
|
||||
continue
|
||||
fpath = os.path.join(RUST_STATE_DIR, fname)
|
||||
with open(fpath, 'r', encoding='utf-8', errors='ignore') as f:
|
||||
content = f.read()
|
||||
|
||||
# 查找带有 "对应 COMMON" 注释的 struct
|
||||
# 允许在注释和 pub struct 之间出现属性行如 #[derive(...)]
|
||||
# 以及空行
|
||||
pattern = (
|
||||
r'///\s*对应\s*COMMON\s*/\s*(\w+)\s*/\s*\n'
|
||||
r'(?:(?:\s*#[^\n]*\n|\s*///?[^\n]*\n|\s*\n))*' # 属性、注释、空行
|
||||
r'\s*pub\s+struct\s+(\w+)\s*\{'
|
||||
)
|
||||
for match in re.finditer(pattern, content, re.IGNORECASE):
|
||||
common_name = match.group(1).upper()
|
||||
struct_name = match.group(2)
|
||||
|
||||
# 提取 struct body(处理嵌套大括号)
|
||||
body_start = match.end()
|
||||
body = _extract_braced_body(content, body_start)
|
||||
|
||||
# 提取字段
|
||||
fields = {}
|
||||
field_pattern = r'pub\s+(\w+)\s*:\s*([^,\n]+)'
|
||||
for fm in re.finditer(field_pattern, body):
|
||||
field_name = fm.group(1)
|
||||
type_str = fm.group(2).strip()
|
||||
fields[field_name] = type_str
|
||||
|
||||
structs.append(RustStruct(
|
||||
name=struct_name,
|
||||
file=fpath,
|
||||
common_name=common_name,
|
||||
fields=fields,
|
||||
))
|
||||
|
||||
return structs
|
||||
|
||||
|
||||
def _extract_braced_body(content: str, start: int) -> str:
|
||||
"""从 start 位置(紧跟 { 之后)提取匹配的大括号体"""
|
||||
depth = 1
|
||||
i = start
|
||||
while i < len(content) and depth > 0:
|
||||
if content[i] == '{':
|
||||
depth += 1
|
||||
elif content[i] == '}':
|
||||
depth -= 1
|
||||
i += 1
|
||||
return content[start:i-1] if depth == 0 else content[start:]
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 映射构建
|
||||
# ============================================================================
|
||||
|
||||
def _fortran_to_rust_name(fortran_name: str) -> str:
|
||||
"""Fortran 变量名转 Rust 字段名(大写 → 小写)"""
|
||||
return fortran_name.lower()
|
||||
|
||||
|
||||
def build_mapping(
|
||||
common_blocks: Dict[str, CommonBlock],
|
||||
rust_structs: List[RustStruct]
|
||||
) -> Dict[str, CommonVar]:
|
||||
"""交叉引用 Fortran COMMON 和 Rust struct,生成完整映射
|
||||
|
||||
返回: {FORTAN_VAR_NAME: CommonVar (包含 rust_field, rust_struct 信息)}
|
||||
"""
|
||||
var_map: Dict[str, CommonVar] = {}
|
||||
|
||||
# 先收集所有 COMMON 变量
|
||||
for block_name, block in common_blocks.items():
|
||||
for var in block.variables:
|
||||
var_map[var.name] = var
|
||||
|
||||
# 构建 struct_name -> RustStruct 映射
|
||||
struct_by_common: Dict[str, RustStruct] = {}
|
||||
for rs in rust_structs:
|
||||
if rs.common_name:
|
||||
struct_by_common[rs.common_name.upper()] = rs
|
||||
|
||||
# 交叉引用
|
||||
for var_name, var in var_map.items():
|
||||
# 查找对应 Rust struct
|
||||
rs = struct_by_common.get(var.common_block)
|
||||
if rs:
|
||||
var.rust_struct = rs.name
|
||||
var.rust_file = rs.file
|
||||
# 查找对应字段
|
||||
rust_field_name = _fortran_to_rust_name(var_name)
|
||||
if rust_field_name in rs.fields:
|
||||
var.rust_field = rust_field_name
|
||||
|
||||
# 设置 CommonBlock 的 rust_struct 信息
|
||||
for block_name, block in common_blocks.items():
|
||||
rs = struct_by_common.get(block_name)
|
||||
if rs:
|
||||
block.rust_struct = rs.name
|
||||
block.rust_file = rs.file
|
||||
|
||||
return var_map
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 模块级查询
|
||||
# ============================================================================
|
||||
|
||||
def get_includes_for_module(module_name: str) -> List[str]:
|
||||
"""获取某 Fortran 模块 INCLUDE 的文件列表"""
|
||||
fpath = os.path.join(EXTRACTED_DIR, f"{module_name.lower()}.f")
|
||||
if not os.path.exists(fpath):
|
||||
return []
|
||||
with open(fpath, 'r', encoding='utf-8', errors='ignore') as f:
|
||||
content = f.read()
|
||||
includes = re.findall(r"INCLUDE\s*'([^']+)\.FOR'", content, re.IGNORECASE)
|
||||
return [inc.upper() for inc in includes if inc.upper() != 'IMPLIC']
|
||||
|
||||
|
||||
def get_commons_for_module(module_name: str) -> List[str]:
|
||||
"""获取某 Fortran 模块使用的 COMMON 块名列表"""
|
||||
includes = get_includes_for_module(module_name)
|
||||
commons = set()
|
||||
for inc in includes:
|
||||
fpath = os.path.join(FORTRAN_COMMON_DIR, f"{inc}.FOR")
|
||||
if not os.path.exists(fpath):
|
||||
continue
|
||||
with open(fpath, 'r', encoding='utf-8', errors='ignore') as f:
|
||||
content = f.read()
|
||||
blocks = re.findall(r'(?i)COMMON\s*/(\w+)/', content)
|
||||
commons.update(b.upper() for b in blocks)
|
||||
return sorted(commons)
|
||||
|
||||
|
||||
def get_vars_for_module(
|
||||
module_name: str,
|
||||
var_map: Dict[str, CommonVar]
|
||||
) -> Dict[str, CommonVar]:
|
||||
"""返回某模块用到的所有 COMMON 变量及其映射
|
||||
|
||||
参数:
|
||||
module_name: Fortran 模块名
|
||||
var_map: build_mapping() 的返回值
|
||||
|
||||
返回: {VAR_NAME: CommonVar}
|
||||
"""
|
||||
commons = get_commons_for_module(module_name)
|
||||
result = {}
|
||||
for var_name, var in var_map.items():
|
||||
if var.common_block in commons:
|
||||
result[var_name] = var
|
||||
return result
|
||||
|
||||
|
||||
def get_rust_structs_for_module(
|
||||
module_name: str,
|
||||
rust_structs: List[RustStruct]
|
||||
) -> List[str]:
|
||||
"""获取某模块需要 use 的 Rust struct 文件路径"""
|
||||
commons = get_commons_for_module(module_name)
|
||||
files = set()
|
||||
for rs in rust_structs:
|
||||
if rs.common_name and rs.common_name.upper() in commons:
|
||||
files.add(rs.file)
|
||||
return sorted(files)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 缓存单例
|
||||
# ============================================================================
|
||||
|
||||
_cached_mapping = None
|
||||
_cached_structs = None
|
||||
_cached_blocks = None
|
||||
|
||||
|
||||
def get_mapping():
|
||||
"""获取缓存的变量映射"""
|
||||
global _cached_mapping, _cached_structs, _cached_blocks
|
||||
if _cached_mapping is None:
|
||||
_cached_blocks = parse_all_commons()
|
||||
_cached_structs = parse_rust_structs()
|
||||
_cached_mapping = build_mapping(_cached_blocks, _cached_structs)
|
||||
return _cached_mapping
|
||||
|
||||
|
||||
def get_structs():
|
||||
"""获取缓存的 Rust struct 列表"""
|
||||
global _cached_structs
|
||||
if _cached_structs is None:
|
||||
get_mapping()
|
||||
return _cached_structs
|
||||
|
||||
|
||||
def get_blocks():
|
||||
"""获取缓存的 COMMON 块"""
|
||||
global _cached_blocks
|
||||
if _cached_blocks is None:
|
||||
get_mapping()
|
||||
return _cached_blocks
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# CLI
|
||||
# ============================================================================
|
||||
|
||||
def main():
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser(description='COMMON 变量映射数据库')
|
||||
parser.add_argument('--module', help='显示某模块使用的 COMMON 变量')
|
||||
parser.add_argument('--block', help='显示某 COMMON 块的变量')
|
||||
parser.add_argument('--mapping', action='store_true', help='显示完整映射')
|
||||
parser.add_argument('--unmapped', action='store_true', help='显示未映射的变量')
|
||||
args = parser.parse_args()
|
||||
|
||||
var_map = get_mapping()
|
||||
blocks = get_blocks()
|
||||
structs = get_structs()
|
||||
|
||||
if args.module:
|
||||
vars = get_vars_for_module(args.module.upper(), var_map)
|
||||
print(f"模块 {args.module.upper()} 使用的 COMMON 变量:")
|
||||
print(f" 总计: {len(vars)} 个变量")
|
||||
for vname, var in sorted(vars.items()):
|
||||
dims_str = f"({', '.join(var.dims)})" if var.dims else ""
|
||||
rust_str = f"→ {var.rust_struct}.{var.rust_field}" if var.rust_field else "→ (未映射)"
|
||||
print(f" {vname:20s} {dims_str:20s} {rust_str}")
|
||||
return
|
||||
|
||||
if args.block:
|
||||
block = blocks.get(args.block.upper())
|
||||
if not block:
|
||||
print(f"COMMON 块 {args.block} 未找到")
|
||||
return
|
||||
print(f"COMMON /{block.name}/ (文件: {block.file})")
|
||||
for var in block.variables:
|
||||
dims_str = f"({', '.join(var.dims)})" if var.dims else ""
|
||||
rust_str = f"→ {var.rust_field}" if var.rust_field else "→ (未映射)"
|
||||
print(f" {var.name:20s} {dims_str:20s} {rust_str}")
|
||||
return
|
||||
|
||||
if args.unmapped:
|
||||
unmapped = {k: v for k, v in var_map.items() if not v.rust_field}
|
||||
print(f"未映射的 COMMON 变量: {len(unmapped)} / {len(var_map)}")
|
||||
for vname, var in sorted(unmapped.items()):
|
||||
dims_str = f"({', '.join(var.dims)})" if var.dims else ""
|
||||
print(f" /{var.common_block}/ {vname:20s} {dims_str}")
|
||||
return
|
||||
|
||||
if args.mapping:
|
||||
print(f"COMMON 变量映射统计:")
|
||||
mapped = sum(1 for v in var_map.values() if v.rust_field)
|
||||
print(f" 总变量: {len(var_map)}")
|
||||
print(f" 已映射: {mapped}")
|
||||
print(f" 未映射: {len(var_map) - mapped}")
|
||||
print()
|
||||
print("COMMON 块:")
|
||||
for bname, block in sorted(blocks.items()):
|
||||
n_mapped = sum(1 for v in block.variables if v.rust_field)
|
||||
print(f" /{bname}/ → {block.rust_struct or '(无)'} ({n_mapped}/{len(block.variables)})")
|
||||
return
|
||||
|
||||
# 默认:统计信息
|
||||
print("COMMON 变量映射数据库")
|
||||
print(f" COMMON 块: {len(blocks)}")
|
||||
print(f" COMMON 变量: {len(var_map)}")
|
||||
print(f" Rust struct: {len(structs)}")
|
||||
mapped = sum(1 for v in var_map.values() if v.rust_field)
|
||||
print(f" 已映射: {mapped}/{len(var_map)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,241 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
深度检查提示生成器
|
||||
|
||||
根据模块名自动生成 Claude Phase 2 深度检查所需的文件列表和检查提示。
|
||||
|
||||
用法:
|
||||
python3 deep_check_prompt.py ODFHYS # 生成检查文件列表
|
||||
python3 deep_check_prompt.py ODFHYS --prompt # 生成完整检查提示
|
||||
"""
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import argparse
|
||||
from typing import List, Dict, Optional
|
||||
|
||||
# 路径配置
|
||||
EXTRACTED_DIR = "/home/fmq/program/tlusty/tl208-s54/rust/tlusty/extracted"
|
||||
RUST_BASE_DIR = "/home/fmq/.zeroclaw/workspace/SpectraRust/src"
|
||||
FORTRAN_COMMON_DIR = "/home/fmq/program/tlusty/tl208-s54/tlusty"
|
||||
|
||||
# 导入 common_db
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
if script_dir not in sys.path:
|
||||
sys.path.insert(0, script_dir)
|
||||
|
||||
from common_db import (
|
||||
get_includes_for_module,
|
||||
get_commons_for_module,
|
||||
get_vars_for_module,
|
||||
get_rust_structs_for_module,
|
||||
get_mapping,
|
||||
get_structs,
|
||||
get_blocks,
|
||||
)
|
||||
|
||||
|
||||
def find_rust_file(module_name: str) -> Optional[str]:
|
||||
"""查找模块的 Rust 文件路径"""
|
||||
rust_name = module_name.lower()
|
||||
|
||||
math_subdirs = [
|
||||
'ali', 'atomic', 'continuum', 'convection', 'eos', 'hydrogen',
|
||||
'interpolation', 'io', 'odf', 'opacity', 'partition', 'population',
|
||||
'radiative', 'rates', 'solvers', 'special', 'temperature', 'utils'
|
||||
]
|
||||
|
||||
# tlusty/io/
|
||||
path = os.path.join(RUST_BASE_DIR, 'tlusty', 'io', f"{rust_name}.rs")
|
||||
if os.path.exists(path):
|
||||
return path
|
||||
|
||||
# tlusty/math/
|
||||
path = os.path.join(RUST_BASE_DIR, 'tlusty', 'math', f"{rust_name}.rs")
|
||||
if os.path.exists(path):
|
||||
return path
|
||||
|
||||
# tlusty/math/子目录
|
||||
for subdir in math_subdirs:
|
||||
path = os.path.join(RUST_BASE_DIR, 'tlusty', 'math', subdir, f"{rust_name}.rs")
|
||||
if os.path.exists(path):
|
||||
return path
|
||||
|
||||
# tlusty/state/
|
||||
path = os.path.join(RUST_BASE_DIR, 'tlusty', 'state', f"{rust_name}.rs")
|
||||
if os.path.exists(path):
|
||||
return path
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def find_rust_use_imports(rust_file: str) -> List[str]:
|
||||
"""从 Rust 文件中提取 use 引用的 state 文件"""
|
||||
state_files = set()
|
||||
if not os.path.exists(rust_file):
|
||||
return []
|
||||
|
||||
with open(rust_file, 'r', encoding='utf-8', errors='ignore') as f:
|
||||
content = f.read()
|
||||
|
||||
# 匹配 use super::xxx 或 use crate::tlusty::state::xxx
|
||||
patterns = [
|
||||
r'use\s+super::(\w+)',
|
||||
r'use\s+crate::tlusty::state::(\w+)',
|
||||
r'use\s+super::super::state::(\w+)',
|
||||
]
|
||||
for pattern in patterns:
|
||||
for m in re.finditer(pattern, content):
|
||||
mod_name = m.group(1)
|
||||
# 查找对应的 .rs 文件
|
||||
state_file = os.path.join(RUST_BASE_DIR, 'tlusty', 'state', f"{mod_name}.rs")
|
||||
if os.path.exists(state_file):
|
||||
state_files.add(state_file)
|
||||
|
||||
return sorted(state_files)
|
||||
|
||||
|
||||
def generate_file_list(module_name: str) -> Dict[str, str]:
|
||||
"""生成深度检查所需的文件列表"""
|
||||
files = {}
|
||||
name_upper = module_name.upper()
|
||||
|
||||
# 1. Fortran 源文件
|
||||
fortran_file = os.path.join(EXTRACTED_DIR, f"{module_name.lower()}.f")
|
||||
if os.path.exists(fortran_file):
|
||||
files['fortran_source'] = fortran_file
|
||||
else:
|
||||
files['fortran_source'] = f"(未找到: {fortran_file})"
|
||||
|
||||
# 2. Rust 源文件
|
||||
rust_file = find_rust_file(module_name)
|
||||
if rust_file:
|
||||
files['rust_source'] = rust_file
|
||||
else:
|
||||
files['rust_source'] = "(未找到)"
|
||||
|
||||
# 3. INCLUDE 的 COMMON 定义文件
|
||||
includes = get_includes_for_module(name_upper)
|
||||
for inc in includes:
|
||||
inc_path = os.path.join(FORTRAN_COMMON_DIR, f"{inc}.FOR")
|
||||
key = f"common_{inc.lower()}"
|
||||
if os.path.exists(inc_path):
|
||||
files[key] = inc_path
|
||||
else:
|
||||
files[key] = f"(未找到: {inc_path})"
|
||||
|
||||
# 4. Rust state struct 文件(通过 use 导入)
|
||||
if rust_file:
|
||||
state_files = find_rust_use_imports(rust_file)
|
||||
for i, sf in enumerate(state_files):
|
||||
files[f"rust_state_{i}"] = sf
|
||||
|
||||
return files
|
||||
|
||||
|
||||
def generate_prompt(module_name: str) -> str:
|
||||
"""生成完整的 Phase 2 检查提示"""
|
||||
files = generate_file_list(module_name)
|
||||
var_map = get_mapping()
|
||||
structs = get_structs()
|
||||
|
||||
# 获取模块的 COMMON 变量
|
||||
module_vars = get_vars_for_module(module_name.upper(), var_map)
|
||||
|
||||
lines = []
|
||||
lines.append(f"# Phase 2 深度语义检查: {module_name.upper()}")
|
||||
lines.append("")
|
||||
lines.append("## 需要读取的文件")
|
||||
lines.append("")
|
||||
|
||||
for key, path in files.items():
|
||||
if not path.startswith("(未找到"):
|
||||
lines.append(f"- `{path}`")
|
||||
else:
|
||||
lines.append(f"- {path}")
|
||||
|
||||
lines.append("")
|
||||
lines.append("## COMMON 变量映射")
|
||||
lines.append("")
|
||||
lines.append("```")
|
||||
|
||||
# 按 COMMON 块分组
|
||||
vars_by_block: Dict[str, List] = {}
|
||||
for vname, var in module_vars.items():
|
||||
if var.common_block not in vars_by_block:
|
||||
vars_by_block[var.common_block] = []
|
||||
vars_by_block[var.common_block].append(var)
|
||||
|
||||
for block_name, vars in sorted(vars_by_block.items()):
|
||||
lines.append(f"COMMON /{block_name}/")
|
||||
for var in sorted(vars, key=lambda v: v.name):
|
||||
dims_str = f"({', '.join(var.dims)})" if var.dims else ""
|
||||
rust_str = f"{var.rust_struct}.{var.rust_field}" if var.rust_field else "(未映射)"
|
||||
lines.append(f" {var.name:20s} {dims_str:20s} → {rust_str}")
|
||||
lines.append("")
|
||||
|
||||
lines.append("```")
|
||||
lines.append("")
|
||||
lines.append("## 检查清单")
|
||||
lines.append("")
|
||||
lines.append("逐项检查以下内容:")
|
||||
lines.append("")
|
||||
|
||||
checklist = [
|
||||
"[ ] COMMON 变量 → 正确的 Rust struct 字段",
|
||||
"[ ] 2D 数组下标顺序(Fortran 列主序 → Rust 行主序)",
|
||||
"[ ] 1-based → 0-based 索引一致性",
|
||||
"[ ] 循环边界转换(DO I=1,N → for i in 0..n)",
|
||||
"[ ] IF 条件完整保留(<= vs <, >= vs >)",
|
||||
"[ ] 所有赋值目标存在(无遗漏)",
|
||||
"[ ] CALL 顺序和数量一致",
|
||||
"[ ] 类型转换正确(INTEGER→i32, REAL*8→f64, LOGICAL→bool)",
|
||||
]
|
||||
|
||||
for item in checklist:
|
||||
lines.append(item)
|
||||
|
||||
lines.append("")
|
||||
lines.append("## 发现问题处理")
|
||||
lines.append("")
|
||||
lines.append("发现 bug → 立即修复 → cargo build 验证 → 继续检查")
|
||||
lines.append("无 bug → 输出 '深度检查通过'")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='Phase 2 深度检查提示生成器')
|
||||
parser.add_argument('module', help='模块名')
|
||||
parser.add_argument('--prompt', action='store_true', help='生成完整检查提示')
|
||||
parser.add_argument('--files', action='store_true', help='只列出文件')
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.prompt:
|
||||
print(generate_prompt(args.module))
|
||||
elif args.files:
|
||||
files = generate_file_list(args.module)
|
||||
for key, path in files.items():
|
||||
print(f" {key:20s} {path}")
|
||||
else:
|
||||
# 默认:输出文件列表
|
||||
files = generate_file_list(args.module)
|
||||
print(f"模块 {args.module.upper()} 深度检查文件列表:")
|
||||
print()
|
||||
for key, path in files.items():
|
||||
icon = "📄" if not path.startswith("(未找到") else "❓"
|
||||
print(f" {icon} {key:20s} {path}")
|
||||
|
||||
# 也显示 COMMON 变量数
|
||||
var_map = get_mapping()
|
||||
module_vars = get_vars_for_module(args.module.upper(), var_map)
|
||||
mapped = sum(1 for v in module_vars.values() if v.rust_field)
|
||||
print(f"\n COMMON 变量: {mapped}/{len(module_vars)} 已映射")
|
||||
|
||||
# 提示使用 --prompt 获取完整检查提示
|
||||
print(f"\n 生成完整检查提示: python3 deep_check_prompt.py {args.module} --prompt")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,569 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
f2r_next - 下一个需要检查/修复的模块
|
||||
|
||||
根据依赖关系和当前状态,推荐下一个应该检查的模块。
|
||||
|
||||
策略:
|
||||
1. 优先修复被多个模块依赖的基础模块
|
||||
2. 从顶层模块(如 TLUSTY, START)向下追踪
|
||||
3. 跳过已完全匹配的模块
|
||||
|
||||
用法:
|
||||
python3 next_module.py # 推荐下一个模块
|
||||
python3 next_module.py --path START # 从 START 开始追踪
|
||||
python3 next_module.py --chain TLUSTY # 显示完整调用链
|
||||
python3 next_module.py --priority # 显示修复优先级列表
|
||||
"""
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import argparse
|
||||
import glob
|
||||
from collections import defaultdict, deque
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List, Dict, Set, Optional, Tuple
|
||||
|
||||
# 导入 f2r_check 的状态检测函数
|
||||
try:
|
||||
from f2r_check import check_module
|
||||
USE_F2R_CHECK = True
|
||||
except ImportError:
|
||||
# 如果导入失败,添加脚本目录到路径
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
if script_dir not in sys.path:
|
||||
sys.path.insert(0, script_dir)
|
||||
try:
|
||||
from f2r_check import check_module
|
||||
USE_F2R_CHECK = True
|
||||
except ImportError:
|
||||
USE_F2R_CHECK = False
|
||||
print("警告: 无法导入 f2r_check,将使用简化状态检测", file=sys.stderr)
|
||||
|
||||
# ============================================================================
|
||||
# 路径配置
|
||||
# ============================================================================
|
||||
|
||||
EXTRACTED_DIR = "/home/fmq/program/tlusty/tl208-s54/rust/tlusty/extracted"
|
||||
RUST_BASE_DIR = "/home/fmq/.zeroclaw/workspace/SpectraRust/src"
|
||||
|
||||
# ============================================================================
|
||||
# 数据结构
|
||||
# ============================================================================
|
||||
|
||||
@dataclass
|
||||
class ModuleInfo:
|
||||
"""模块信息"""
|
||||
name: str
|
||||
fortran_file: str = ""
|
||||
rust_file: str = ""
|
||||
status: str = "missing" # match, partial, mismatch, missing
|
||||
calls: List[str] = field(default_factory=list)
|
||||
called_by: List[str] = field(default_factory=list) # 被谁调用
|
||||
depth: int = 0 # 依赖深度
|
||||
trans_pending: int = 0 # 传递未实现依赖数
|
||||
is_stub: bool = False
|
||||
|
||||
# ============================================================================
|
||||
# Fortran 解析
|
||||
# ============================================================================
|
||||
|
||||
FORTRAN_INTRINSICS = {
|
||||
'SIN', 'COS', 'TAN', 'ASIN', 'ACOS', 'ATAN', 'ATAN2',
|
||||
'SINH', 'COSH', 'TANH', 'EXP', 'LOG', 'LOG10', 'LOG2',
|
||||
'SQRT', 'ABS', 'MOD', 'SIGN', 'MAX', 'MIN', 'MAX0', 'MIN0',
|
||||
'INT', 'IFIX', 'IDINT', 'FLOAT', 'SNGL', 'DBLE', 'CMPLX',
|
||||
'REAL', 'AIMAG', 'CONJG', 'ICHAR', 'CHAR', 'INDEX', 'LEN',
|
||||
'IF', 'THEN', 'ELSE', 'ENDIF', 'END', 'DO', 'CONTINUE',
|
||||
'RETURN', 'STOP', 'PAUSE', 'GOTO', 'CALL', 'SUBROUTINE',
|
||||
'FUNCTION', 'PROGRAM', 'MODULE', 'USE', 'IMPLICIT',
|
||||
'PARAMETER', 'DATA', 'DIMENSION', 'COMMON', 'SAVE',
|
||||
'EXTERNAL', 'INTRINSIC', 'READ', 'WRITE', 'OPEN', 'CLOSE',
|
||||
'FORMAT', 'PRINT', 'ERF', 'ERFC', 'GAMMA',
|
||||
}
|
||||
|
||||
def strip_comments(content: str) -> str:
|
||||
"""移除 Fortran 注释"""
|
||||
lines = content.split('\n')
|
||||
code_lines = []
|
||||
for line in lines:
|
||||
if len(line) == 0:
|
||||
continue
|
||||
first_char = line[0].upper()
|
||||
if first_char in ('C', '!', '*'):
|
||||
continue
|
||||
code_lines.append(line)
|
||||
return '\n'.join(code_lines)
|
||||
|
||||
def extract_calls(content: str) -> List[str]:
|
||||
"""提取 CALL 语句"""
|
||||
code_content = strip_comments(content)
|
||||
calls = re.findall(r'(?i)CALL\s+(\w+)(?:\s*\(|\s*$|\s*\n)', code_content)
|
||||
return list(set(c.upper() for c in calls if c.upper() not in FORTRAN_INTRINSICS))
|
||||
|
||||
def extract_subroutine_name(content: str) -> Optional[str]:
|
||||
"""提取子程序名"""
|
||||
match = re.search(r'(?i)^\s*SUBROUTINE\s+(\w+)', content, re.MULTILINE)
|
||||
if match:
|
||||
return match.group(1).upper()
|
||||
match = re.search(r'(?i)^\s*PROGRAM\s+(\w+)', content, re.MULTILINE)
|
||||
if match:
|
||||
return match.group(1).upper()
|
||||
# 尝试匹配 BLOCK DATA
|
||||
match = re.search(r'^ BLOCK\s+DATA\s*([A-Za-z0-9_]*)\s*$', content, re.MULTILINE)
|
||||
if match:
|
||||
block_name = match.group(1).strip()
|
||||
if block_name:
|
||||
return block_name.upper()
|
||||
else:
|
||||
return "_UNNAMED_BLOCK_DATA_"
|
||||
return None
|
||||
|
||||
# ============================================================================
|
||||
# Rust 检查
|
||||
# ============================================================================
|
||||
|
||||
SPECIAL_MAPPINGS = {
|
||||
'gfree': ['gfree0', 'gfreed', 'gfree1'],
|
||||
'interpolate': ['yint', 'lagran'],
|
||||
'sgmer': ['sgmer0', 'sgmer1', 'sgmerd'],
|
||||
'ctdata': ['hction', 'hctrecom'],
|
||||
'cross': ['cross', 'crossd'],
|
||||
'expint': ['eint', 'expinx'],
|
||||
'erfcx': ['erfcx', 'erfcin'],
|
||||
'lineqs': ['lineqs', 'lineqs_nr'],
|
||||
'gamsp': ['gamsp'],
|
||||
'bhe': ['bhe', 'bhed', 'bhez'],
|
||||
'comset': ['comset'],
|
||||
'ghydop': ['ghydop'],
|
||||
'levgrp': ['levgrp'],
|
||||
'profil': ['profil'],
|
||||
'linspl': ['linspl'],
|
||||
'convec': ['convec', 'convc1'],
|
||||
}
|
||||
|
||||
def find_rust_module(fortran_name: str) -> Tuple[str, bool]:
|
||||
"""查找对应的 Rust 模块,返回 (路径, 是否简化实现)"""
|
||||
rust_name = fortran_name.lower()
|
||||
|
||||
math_subdirs = [
|
||||
'ali', 'atomic', 'continuum', 'convection', 'eos', 'hydrogen',
|
||||
'interpolation', 'io', 'odf', 'opacity', 'partition', 'population',
|
||||
'radiative', 'rates', 'solvers', 'special', 'temperature', 'utils'
|
||||
]
|
||||
|
||||
# 检查路径列表
|
||||
search_paths = []
|
||||
|
||||
# 主程序
|
||||
if fortran_name.upper() == 'TLUSTY':
|
||||
search_paths.append(os.path.join(RUST_BASE_DIR, 'bin', 'tlusty.rs'))
|
||||
search_paths.append(os.path.join(RUST_BASE_DIR, 'tlusty', 'main.rs'))
|
||||
|
||||
# tlusty/io/
|
||||
search_paths.append(os.path.join(RUST_BASE_DIR, 'tlusty', 'io', f"{rust_name}.rs"))
|
||||
|
||||
# tlusty/math/
|
||||
search_paths.append(os.path.join(RUST_BASE_DIR, 'tlusty', 'math', f"{rust_name}.rs"))
|
||||
|
||||
# tlusty/math/子目录
|
||||
for subdir in math_subdirs:
|
||||
search_paths.append(os.path.join(RUST_BASE_DIR, 'tlusty', 'math', subdir, f"{rust_name}.rs"))
|
||||
|
||||
# tlusty/state/
|
||||
search_paths.append(os.path.join(RUST_BASE_DIR, 'tlusty', 'state', f"{rust_name}.rs"))
|
||||
|
||||
# 特殊映射
|
||||
for rust_mod, fortran_funcs in SPECIAL_MAPPINGS.items():
|
||||
if fortran_name.lower() in [f.lower() for f in fortran_funcs]:
|
||||
search_paths.append(os.path.join(RUST_BASE_DIR, 'tlusty', 'math', f"{rust_mod}.rs"))
|
||||
for subdir in math_subdirs:
|
||||
search_paths.append(os.path.join(RUST_BASE_DIR, 'tlusty', 'math', subdir, f"{rust_mod}.rs"))
|
||||
|
||||
# BLOCK DATA 特殊处理 -> data.rs
|
||||
if fortran_name.upper() == '_UNNAMED_BLOCK_DATA_':
|
||||
search_paths.append(os.path.join(RUST_BASE_DIR, 'tlusty', 'data.rs'))
|
||||
|
||||
# 检查文件是否存在
|
||||
for path in search_paths:
|
||||
if os.path.exists(path):
|
||||
with open(path, 'r', encoding='utf-8', errors='ignore') as f:
|
||||
content = f.read()
|
||||
|
||||
# 只检查主函数体是否是简化实现(而非整个文件)
|
||||
is_stub = check_main_function_stub(content, rust_name)
|
||||
|
||||
return path, is_stub
|
||||
|
||||
return "", False
|
||||
|
||||
def check_main_function_stub(content: str, func_name: str) -> bool:
|
||||
"""检查主函数是否是简化实现(只检查主函数体,不检查辅助函数)"""
|
||||
import re
|
||||
|
||||
# 查找主函数定义
|
||||
# 支持多种模式:pub fn name(...), pub fn name_pure(...), fn name(...)
|
||||
patterns = [
|
||||
rf'pub\s+fn\s+{func_name}\s*(?:<[^>]+>)?\s*\(',
|
||||
rf'pub\s+fn\s+{func_name}_pure\s*(?:<[^>]+>)?\s*\(',
|
||||
rf'fn\s+{func_name}\s*(?:<[^>]+>)?\s*\(',
|
||||
]
|
||||
|
||||
func_body = ""
|
||||
for pattern in patterns:
|
||||
match = re.search(pattern, content, re.IGNORECASE | re.DOTALL)
|
||||
if match:
|
||||
# 提取函数体
|
||||
func_start = match.end()
|
||||
brace_count = 0
|
||||
func_body_start = func_start
|
||||
|
||||
for i, c in enumerate(content[func_start:], func_start):
|
||||
if c == '{':
|
||||
if brace_count == 0:
|
||||
func_body_start = i
|
||||
brace_count += 1
|
||||
elif c == '}':
|
||||
brace_count -= 1
|
||||
if brace_count == 0:
|
||||
func_body = content[func_body_start:i+1]
|
||||
break
|
||||
break
|
||||
|
||||
if not func_body:
|
||||
# 如果找不到主函数,检查整个文件
|
||||
func_body = content
|
||||
|
||||
# 检查是否是简化实现
|
||||
stub_patterns = [
|
||||
r'//\s*简化实现',
|
||||
r'//\s*TODO:',
|
||||
r'//\s*待实现',
|
||||
r'框架就绪',
|
||||
r'unimplemented!',
|
||||
r'todo!',
|
||||
]
|
||||
|
||||
for p in stub_patterns:
|
||||
if re.search(p, func_body, re.IGNORECASE):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
# ============================================================================
|
||||
# 依赖分析
|
||||
# ============================================================================
|
||||
|
||||
def build_dependency_graph() -> Dict[str, ModuleInfo]:
|
||||
"""构建依赖图"""
|
||||
modules = {}
|
||||
|
||||
# 第一遍:收集所有模块
|
||||
for fpath in glob.glob(os.path.join(EXTRACTED_DIR, "*.f")):
|
||||
with open(fpath, 'r', encoding='utf-8', errors='ignore') as f:
|
||||
content = f.read()
|
||||
|
||||
name = extract_subroutine_name(content)
|
||||
if not name:
|
||||
name = os.path.splitext(os.path.basename(fpath))[0].upper()
|
||||
|
||||
calls = extract_calls(content)
|
||||
rust_file, is_stub = find_rust_module(name)
|
||||
|
||||
# 使用 f2r_check 的详细状态检测(如果可用)
|
||||
if USE_F2R_CHECK and rust_file:
|
||||
result = check_module(name, verbose=False)
|
||||
status = result.status
|
||||
# 从 result 获取更多调用信息
|
||||
if result.issues:
|
||||
is_stub = any('简化版本' in issue or '占位符' in issue for issue in result.issues)
|
||||
else:
|
||||
# 回退到简化状态检测
|
||||
if not rust_file:
|
||||
status = "missing"
|
||||
elif is_stub:
|
||||
status = "partial"
|
||||
else:
|
||||
status = "match"
|
||||
|
||||
modules[name] = ModuleInfo(
|
||||
name=name,
|
||||
fortran_file=os.path.basename(fpath),
|
||||
rust_file=rust_file,
|
||||
status=status,
|
||||
calls=calls,
|
||||
is_stub=is_stub,
|
||||
)
|
||||
|
||||
# 第二遍:建立反向依赖
|
||||
for name, info in modules.items():
|
||||
for call in info.calls:
|
||||
if call in modules:
|
||||
modules[call].called_by.append(name)
|
||||
|
||||
# 计算依赖深度
|
||||
def calc_depth(name: str, visited: Set[str]) -> int:
|
||||
if name in visited:
|
||||
return 0
|
||||
if name not in modules:
|
||||
return 0
|
||||
visited.add(name)
|
||||
|
||||
calls = modules[name].calls
|
||||
if not calls:
|
||||
return 0
|
||||
|
||||
max_dep = 0
|
||||
for call in calls:
|
||||
if call != name:
|
||||
max_dep = max(max_dep, calc_depth(call, visited.copy()))
|
||||
|
||||
return max_dep + 1
|
||||
|
||||
for name in modules:
|
||||
modules[name].depth = calc_depth(name, set())
|
||||
|
||||
# 计算传递未实现依赖数
|
||||
def calc_trans_pending(name: str, visited: Set[str]) -> int:
|
||||
if name in visited:
|
||||
return 0
|
||||
if name not in modules:
|
||||
return 1 # 未实现的模块
|
||||
|
||||
visited.add(name)
|
||||
count = 0
|
||||
|
||||
for call in modules[name].calls:
|
||||
if call not in modules:
|
||||
count += 1
|
||||
elif modules[call].status != "match":
|
||||
count += 1 + calc_trans_pending(call, visited.copy())
|
||||
|
||||
return count
|
||||
|
||||
for name in modules:
|
||||
modules[name].trans_pending = calc_trans_pending(name, set())
|
||||
|
||||
return modules
|
||||
|
||||
# ============================================================================
|
||||
# 推荐逻辑
|
||||
# ============================================================================
|
||||
|
||||
def find_next_module(modules: Dict[str, ModuleInfo], start_from: str = None) -> List[ModuleInfo]:
|
||||
"""找到下一个需要检查的模块"""
|
||||
|
||||
if start_from and start_from.upper() in modules:
|
||||
# 从指定模块开始,找其未实现的依赖
|
||||
start = modules[start_from.upper()]
|
||||
|
||||
# BFS 遍历依赖
|
||||
queue = deque([(start.name, 0)])
|
||||
visited = set()
|
||||
candidates = []
|
||||
|
||||
while queue:
|
||||
name, level = queue.popleft()
|
||||
|
||||
if name in visited:
|
||||
continue
|
||||
visited.add(name)
|
||||
|
||||
if name not in modules:
|
||||
continue
|
||||
|
||||
info = modules[name]
|
||||
|
||||
# 检查每个依赖
|
||||
for call in info.calls:
|
||||
if call in visited:
|
||||
continue
|
||||
|
||||
if call not in modules:
|
||||
# 未实现的模块
|
||||
candidates.append((call, level + 1, "missing", 0))
|
||||
elif modules[call].status == "partial":
|
||||
candidates.append((call, level + 1, "partial", modules[call].called_by.__len__()))
|
||||
elif modules[call].status == "mismatch":
|
||||
candidates.append((call, level + 1, "mismatch", modules[call].called_by.__len__()))
|
||||
elif modules[call].status == "missing":
|
||||
candidates.append((call, level + 1, "missing", 0))
|
||||
else:
|
||||
# 已匹配,继续深入
|
||||
queue.append((call, level + 1))
|
||||
|
||||
# 按优先级排序
|
||||
candidates.sort(key=lambda x: (x[1], 0 if x[2] == "missing" else 1, -x[3]))
|
||||
|
||||
return candidates[:10]
|
||||
|
||||
else:
|
||||
# 全局推荐:优先级 = 传递未实现依赖少 + 被调用次数多
|
||||
candidates = []
|
||||
|
||||
for name, info in modules.items():
|
||||
if info.status != "match":
|
||||
# 计算被调用次数
|
||||
called_count = len(info.called_by)
|
||||
candidates.append((name, info.status, info.trans_pending, called_count, info.depth))
|
||||
|
||||
# 排序:传递未实现少 > 被调用多 > 深度小
|
||||
candidates.sort(key=lambda x: (x[2], -x[3], x[4]))
|
||||
|
||||
return [(c[0], 0, c[1], c[3]) for c in candidates[:20]]
|
||||
|
||||
def get_call_chain(modules: Dict[str, ModuleInfo], start: str, end: str = None) -> List[str]:
|
||||
"""获取调用链"""
|
||||
chain = []
|
||||
visited = set()
|
||||
|
||||
def dfs(name: str, path: List[str]) -> bool:
|
||||
if name in visited:
|
||||
return False
|
||||
visited.add(name)
|
||||
|
||||
path.append(name)
|
||||
|
||||
if end and name == end:
|
||||
chain.extend(path)
|
||||
return True
|
||||
|
||||
if name not in modules:
|
||||
if not end:
|
||||
chain.extend(path)
|
||||
return not end
|
||||
|
||||
for call in modules[name].calls:
|
||||
if dfs(call, path.copy()):
|
||||
return True
|
||||
|
||||
if not end:
|
||||
chain.extend(path)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
dfs(start.upper(), [])
|
||||
return chain
|
||||
|
||||
# ============================================================================
|
||||
# 输出格式
|
||||
# ============================================================================
|
||||
|
||||
def print_next_module(modules: Dict[str, ModuleInfo], candidates: List[Tuple]):
|
||||
"""打印推荐的下一个模块"""
|
||||
|
||||
print("=" * 70)
|
||||
print("📋 下一个需要检查的模块")
|
||||
print("=" * 70)
|
||||
|
||||
if not candidates:
|
||||
print("✅ 所有模块都已匹配!")
|
||||
return
|
||||
|
||||
for i, (name, level, status, called_count) in enumerate(candidates[:10], 1):
|
||||
if name in modules:
|
||||
info = modules[name]
|
||||
status_icon = {"match": "✅", "partial": "⚠️", "mismatch": "❌", "missing": "❓"}.get(status, "❓")
|
||||
|
||||
print(f"\n{i}. {status_icon} {name}")
|
||||
print(f" 状态: {status}")
|
||||
print(f" Fortran: {info.fortran_file}")
|
||||
if info.rust_file:
|
||||
rust_rel = info.rust_file.replace(RUST_BASE_DIR, "src")
|
||||
print(f" Rust: {rust_rel}")
|
||||
else:
|
||||
print(f" Rust: 未实现")
|
||||
print(f" 被调用: {called_count} 次")
|
||||
if info.trans_pending > 0:
|
||||
print(f" 传递未实现依赖: {info.trans_pending}")
|
||||
|
||||
# 显示被谁调用
|
||||
if info.called_by:
|
||||
callers = info.called_by[:5]
|
||||
print(f" 调用者: {', '.join(callers)}")
|
||||
if len(info.called_by) > 5:
|
||||
print(f" ... 还有 {len(info.called_by) - 5} 个")
|
||||
else:
|
||||
# 模块未实现
|
||||
print(f"\n{i}. ❓ {name}")
|
||||
print(f" 状态: missing")
|
||||
print(f" Fortran: {name.lower()}.f")
|
||||
print(f" Rust: 未实现")
|
||||
|
||||
print("\n" + "-" * 70)
|
||||
print("建议:")
|
||||
print(" 1. 先检查模块的 Fortran 源码")
|
||||
print(" 2. 运行: python3 f2r_check.py --diff <模块名>")
|
||||
print(" 3. 按照 Fortran 逻辑修复 Rust 实现")
|
||||
|
||||
def print_call_chain(modules: Dict[str, ModuleInfo], start: str):
|
||||
"""打印调用链"""
|
||||
|
||||
print("=" * 70)
|
||||
print(f"🔗 调用链: {start}")
|
||||
print("=" * 70)
|
||||
|
||||
chain = get_call_chain(modules, start)
|
||||
|
||||
indent = 0
|
||||
for i, name in enumerate(chain[:50]):
|
||||
if name in modules:
|
||||
info = modules[name]
|
||||
status_icon = {"match": "✅", "partial": "⚠️", "mismatch": "❌", "missing": "❓"}.get(info.status, "❓")
|
||||
print(f"{' ' * indent}{status_icon} {name}")
|
||||
else:
|
||||
print(f"{' ' * indent}❓ {name} (未实现)")
|
||||
indent = min(indent + 1, 5)
|
||||
|
||||
if len(chain) > 50:
|
||||
print(f"{' ' * indent}... 还有 {len(chain) - 50} 个模块")
|
||||
|
||||
def print_priority_list(modules: Dict[str, ModuleInfo]):
|
||||
"""打印修复优先级列表"""
|
||||
|
||||
print("=" * 70)
|
||||
print("📊 修复优先级列表")
|
||||
print("=" * 70)
|
||||
print(f"{'排名':<4} {'模块':<15} {'状态':<10} {'被调用':<8} {'传递未实现':<10}")
|
||||
print("-" * 70)
|
||||
|
||||
# 收集需要修复的模块
|
||||
candidates = []
|
||||
for name, info in modules.items():
|
||||
if info.status != "match":
|
||||
candidates.append((name, info.status, len(info.called_by), info.trans_pending))
|
||||
|
||||
# 按优先级排序
|
||||
candidates.sort(key=lambda x: (x[3], -x[2]))
|
||||
|
||||
for i, (name, status, called, pending) in enumerate(candidates[:50], 1):
|
||||
status_icon = {"match": "✅", "partial": "⚠️", "mismatch": "❌", "missing": "❓"}.get(status, "❓")
|
||||
print(f"{i:<4} {name:<15} {status_icon} {status:<8} {called:<8} {pending:<10}")
|
||||
|
||||
# ============================================================================
|
||||
# 主函数
|
||||
# ============================================================================
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='推荐下一个需要检查的模块')
|
||||
parser.add_argument('--path', metavar='MODULE', help='从指定模块开始追踪')
|
||||
parser.add_argument('--chain', metavar='MODULE', help='显示调用链')
|
||||
parser.add_argument('--priority', action='store_true', help='显示修复优先级列表')
|
||||
args = parser.parse_args()
|
||||
|
||||
# 构建依赖图
|
||||
modules = build_dependency_graph()
|
||||
|
||||
if args.chain:
|
||||
print_call_chain(modules, args.chain)
|
||||
elif args.priority:
|
||||
print_priority_list(modules)
|
||||
else:
|
||||
# 推荐下一个模块
|
||||
candidates = find_next_module(modules, args.path)
|
||||
print_next_module(modules, candidates)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -167,28 +167,66 @@ SPECIAL_MAPPINGS = {
|
||||
'convec': ['convec', 'convc1'], # 混合长度对流
|
||||
}
|
||||
|
||||
def find_rust_module(fortran_name, rust_math_dir, rust_io_dir):
|
||||
"""查找对应的 Rust 模块"""
|
||||
def find_rust_module(fortran_name, rust_base_dir):
|
||||
"""查找对应的 Rust 模块
|
||||
|
||||
搜索顺序:
|
||||
1. src/bin/ (主程序)
|
||||
2. src/tlusty/math/ 根目录
|
||||
3. src/tlusty/math/ 子目录 (ali, atomic, continuum, eos, etc.)
|
||||
4. src/tlusty/io/
|
||||
5. src/tlusty/state/
|
||||
6. 特殊映射
|
||||
"""
|
||||
# Fortran 名称是大写,Rust 文件是小写
|
||||
rust_name = fortran_name.lower()
|
||||
|
||||
# 先检查 math 目录
|
||||
rust_file = os.path.join(rust_math_dir, f"{rust_name}.rs")
|
||||
if os.path.exists(rust_file):
|
||||
return f"src/math/{rust_name}.rs"
|
||||
# Rust 模块子目录列表
|
||||
math_subdirs = [
|
||||
'ali', 'atomic', 'continuum', 'convection', 'eos', 'hydrogen',
|
||||
'interpolation', 'io', 'odf', 'opacity', 'partition', 'population',
|
||||
'radiative', 'rates', 'solvers', 'special', 'temperature', 'utils'
|
||||
]
|
||||
|
||||
# 检查 io 目录
|
||||
rust_file = os.path.join(rust_io_dir, f"{rust_name}.rs")
|
||||
if os.path.exists(rust_file):
|
||||
return f"src/io/{rust_name}.rs"
|
||||
# 0. 特殊处理:主程序 TLUSTY
|
||||
if fortran_name.upper() == 'TLUSTY':
|
||||
rust_file = os.path.join(rust_base_dir, 'bin', 'tlusty.rs')
|
||||
if os.path.exists(rust_file):
|
||||
return "src/bin/tlusty.rs"
|
||||
|
||||
# 检查特殊映射 (math 目录) - 必须验证文件实际存在
|
||||
# 1. 检查 tlusty/math/ 根目录
|
||||
rust_file = os.path.join(rust_base_dir, 'tlusty', 'math', f"{rust_name}.rs")
|
||||
if os.path.exists(rust_file):
|
||||
return f"src/tlusty/math/{rust_name}.rs"
|
||||
|
||||
# 2. 检查 tlusty/math/ 子目录
|
||||
for subdir in math_subdirs:
|
||||
rust_file = os.path.join(rust_base_dir, 'tlusty', 'math', subdir, f"{rust_name}.rs")
|
||||
if os.path.exists(rust_file):
|
||||
return f"src/tlusty/math/{subdir}/{rust_name}.rs"
|
||||
|
||||
# 3. 检查 tlusty/io/ 目录
|
||||
rust_file = os.path.join(rust_base_dir, 'tlusty', 'io', f"{rust_name}.rs")
|
||||
if os.path.exists(rust_file):
|
||||
return f"src/tlusty/io/{rust_name}.rs"
|
||||
|
||||
# 4. 检查 tlusty/state/ 目录
|
||||
rust_file = os.path.join(rust_base_dir, 'tlusty', 'state', f"{rust_name}.rs")
|
||||
if os.path.exists(rust_file):
|
||||
return f"src/tlusty/state/{rust_name}.rs"
|
||||
|
||||
# 5. 检查特殊映射 - 必须验证文件实际存在
|
||||
for rust_mod, fortran_funcs in SPECIAL_MAPPINGS.items():
|
||||
if fortran_name.lower() in [f.lower() for f in fortran_funcs]:
|
||||
mapped_file = os.path.join(rust_math_dir, f"{rust_mod}.rs")
|
||||
# 先检查 math 根目录
|
||||
mapped_file = os.path.join(rust_base_dir, 'tlusty', 'math', f"{rust_mod}.rs")
|
||||
if os.path.exists(mapped_file):
|
||||
return f"src/math/{rust_mod}.rs"
|
||||
# 如果映射的文件不存在,继续查找其他映射或返回空
|
||||
return f"src/tlusty/math/{rust_mod}.rs"
|
||||
# 再检查 math 子目录
|
||||
for subdir in math_subdirs:
|
||||
mapped_file = os.path.join(rust_base_dir, 'tlusty', 'math', subdir, f"{rust_mod}.rs")
|
||||
if os.path.exists(mapped_file):
|
||||
return f"src/tlusty/math/{subdir}/{rust_mod}.rs"
|
||||
break
|
||||
|
||||
return ""
|
||||
@@ -335,9 +373,8 @@ def main():
|
||||
parser.add_argument('--full', action='store_true', help='输出完整传递依赖')
|
||||
args = parser.parse_args()
|
||||
|
||||
extracted_dir = "/home/fmq/program/tlusty/tl208-s54/rust/tlusty/extracted"
|
||||
rust_math_dir = "/home/fmq/.zeroclaw/workspace/SpectraRust/src/math"
|
||||
rust_io_dir = "/home/fmq/.zeroclaw/workspace/SpectraRust/src/io"
|
||||
extracted_dir = "/home/dckj/SpectraRust/tlusty/extracted"
|
||||
rust_base_dir = "/home/dckj/SpectraRust/src"
|
||||
|
||||
# 第一遍:收集所有已定义的 SUBROUTINE 和 FUNCTION 名称
|
||||
all_defined_units = set()
|
||||
@@ -367,7 +404,7 @@ def main():
|
||||
units = extract_unit_info(content, fname)
|
||||
|
||||
is_pure = len(includes) <= 1 and len(commons) == 0 and not io
|
||||
rust_mod = find_rust_module(base_name, rust_math_dir, rust_io_dir)
|
||||
rust_mod = find_rust_module(base_name, rust_base_dir)
|
||||
status = "done" if rust_mod else "pending"
|
||||
|
||||
for unit_type, unit_name in units:
|
||||
|
||||
@@ -41,11 +41,22 @@ cat tlusty/extracted/TARGET.f
|
||||
### Step 3: 创建 Rust 模块
|
||||
|
||||
```bash
|
||||
|
||||
touch src/math/TARGET.rs
|
||||
# 根据功能分类选择目录
|
||||
touch src/tlusty/math/<category>/TARGET.rs
|
||||
```
|
||||
|
||||
注意:所有重构的rust代码都暂时放到src/math/文件夹下
|
||||
**目录分类**:
|
||||
| 功能 | 目录 | 示例模块 |
|
||||
|------|------|---------|
|
||||
| ALI 迭代 | `math/ali/` | alifr1, alifr3, rhsgen |
|
||||
| 原子物理 | `math/atomic/` | gfree0, sbfhe1 |
|
||||
| 连续谱 | `math/continuum/` | opacfl, opadd, opctab |
|
||||
| 状态方程 | `math/eos/` | eldens, steqeq |
|
||||
| 不透明度 | `math/opacity/` | meanopt, profil, voigt |
|
||||
| 求解器 | `math/solvers/` | tridag, matinv |
|
||||
| 特殊函数 | `math/special/` | expo, eint, erfcx |
|
||||
| 温度 | `math/temperature/` | rossop, temper |
|
||||
| I/O | `io/` | start, initia, ltegr |
|
||||
|
||||
### Step 4: 实现函数
|
||||
|
||||
@@ -399,24 +410,43 @@ cargo test io:: 2>&1 | grep -E "^test |^test result"
|
||||
## 项目结构
|
||||
|
||||
```
|
||||
rust/src/
|
||||
├── io/ # I/O 兼容层
|
||||
│ ├── mod.rs # 模块入口,单元号常量
|
||||
│ ├── reader.rs # FortranReader(自由格式)
|
||||
│ ├── writer.rs # FortranWriter(格式化输出)
|
||||
│ ├── model.rs # fort.7/8 模型文件
|
||||
│ ├── input.rs # fort.5 主输入
|
||||
│ └── format.rs # FORMAT 解析
|
||||
├── math/ # 纯计算函数 (120+ 个 .rs 文件)
|
||||
├── state/ # COMMON 块 (8 个模块)
|
||||
│ ├── constants.rs # BASICS.FOR
|
||||
│ ├── atomic.rs # ATOMIC.FOR
|
||||
│ ├── model.rs # MODELQ.FOR
|
||||
│ ├── arrays.rs # ARRAY1.FOR
|
||||
│ ├── iterat.rs # ITERAT.FOR
|
||||
│ ├── alipar.rs # ALIPAR.FOR
|
||||
│ └── odfpar.rs # ODFPAR.FOR
|
||||
└── data.rs # 静态数据(DATA 语句)
|
||||
src/
|
||||
├── bin/
|
||||
│ └── tlusty.rs # 主程序入口
|
||||
├── lib.rs # 库入口
|
||||
└── tlusty/
|
||||
├── mod.rs # 模块导出
|
||||
├── data.rs # 静态数据(DATA 语句)
|
||||
├── state/ # COMMON 块 (8 个模块)
|
||||
│ ├── constants.rs # BASICS.FOR
|
||||
│ ├── atomic.rs # ATOMIC.FOR
|
||||
│ ├── model.rs # MODELQ.FOR
|
||||
│ ├── arrays.rs # ARRAY1.FOR
|
||||
│ ├── iterat.rs # ITERAT.FOR
|
||||
│ ├── alipar.rs # ALIPAR.FOR
|
||||
│ └── odfpar.rs # ODFPAR.FOR
|
||||
├── io/ # I/O 兼容层
|
||||
│ ├── mod.rs # 模块入口,单元号常量
|
||||
│ ├── reader.rs # FortranReader(自由格式)
|
||||
│ ├── writer.rs # FortranWriter(格式化输出)
|
||||
│ ├── model.rs # fort.7/8 模型文件
|
||||
│ ├── input.rs # fort.5 主输入
|
||||
│ ├── format.rs # FORMAT 解析
|
||||
│ ├── start.rs # 初始化
|
||||
│ ├── initia.rs # 输入处理
|
||||
│ ├── ltegr.rs # LTE 灰大气
|
||||
│ └── ...
|
||||
└── math/ # 纯计算函数 (290+ 个模块)
|
||||
├── mod.rs
|
||||
├── ali/ # ALI 迭代
|
||||
├── atomic/ # 原子物理
|
||||
├── continuum/ # 连续谱不透明度
|
||||
├── eos/ # 状态方程
|
||||
├── opacity/ # 不透明度
|
||||
├── solvers/ # 方程求解器
|
||||
├── special/ # 特殊函数
|
||||
├── temperature/ # 温度修正
|
||||
└── ...
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
---
|
||||
name: tlusty-iteration
|
||||
description: "TLUSTY Rust主程序迭代开发。触发:用户提到'迭代测试'、对比测试'+用户想验证Rust实现与Fortran的一致性(3) 继续TLUSTY主程序开发(4) 运行TLUSTY测试用例。从主程序开始逐模块对比Fortran源码,持久化检查进度,断点续查。严格逐行对比,发现差异立即修复,循环验证。不依赖f2r-check."
|
||||
---
|
||||
|
||||
# TLUSTY Rust 主程序迭代
|
||||
|
||||
## 文件路径
|
||||
|
||||
| 内容 | 路径 |
|
||||
|------|------|
|
||||
| Fortran 源码 | `/home/fmq/program/tlusty/tl208-s54/rust/tlusty/extracted/*.f` |
|
||||
| Rust 源码 | `src/tlusty/` |
|
||||
| 检查进度 | `.claude/skills/tlusty-iteration/progress.md` |
|
||||
| Fortran 测试 | `tests/tlusty/hhe_fortran/` |
|
||||
| Rust 测试 | `tests/tlusty/hhe_rust/` |
|
||||
|
||||
## 测试方式
|
||||
|
||||
```bash
|
||||
cargo build --bin tlusty
|
||||
cd tests/tlusty/hhe_rust
|
||||
rm -f fort.7
|
||||
../../../target/debug/tlusty < hhe35lt.5 > rust.6 2>stderr.txt
|
||||
```
|
||||
|
||||
## 检查工作流(严格遵守)
|
||||
|
||||
```
|
||||
每次调用本 skill 时:
|
||||
1. 读取 progress.md → 恢复检查进度
|
||||
2. 运行 Rust → 与 Fortran 对比输出
|
||||
3. 如果输出一致 → 更新 progress.md → 结束
|
||||
4. 如果输出不一致 → 从断点继续检查:
|
||||
a. 读取 progress.md 中 "下一个待检查模块"
|
||||
b. ★ 必须先读取对应的 Fortran 文件,逐行理解原始逻辑
|
||||
c. 然后读取对应的 Rust 文件
|
||||
d. 逐行对比: 调用顺序、变量映射、索引转换、逻辑分支
|
||||
e. 发现差异 → 立即修复 → cargo build 验证
|
||||
f. 更新 progress.md → 继续下一个模块
|
||||
5. 全部通过 → 运行测试验证 → 更新 progress.md
|
||||
```
|
||||
|
||||
## ★ 核心原则:必须参考 Fortran 代码
|
||||
|
||||
```
|
||||
严禁凭猜测修改代码!每次修改前必须:
|
||||
1. 先读取对应的 Fortran 源码文件
|
||||
2. 理解 Fortran 的确切逻辑流程
|
||||
3. 找到 Fortran 中的对应行
|
||||
4. 然后对照修改 Rust 代码
|
||||
|
||||
违反此原则是产生 bug 的最主要原因。
|
||||
```
|
||||
|
||||
## Fortran 调用链(检查顺序)
|
||||
|
||||
从主程序开始,沿着调用链深度优先检查:
|
||||
```
|
||||
TLUSTY (tlusty.f)
|
||||
→ START (start.f)
|
||||
→ INITIA (initia.f) ★ 最大模块,927行
|
||||
→ HEDIF (hedif.f) [可选]
|
||||
→ COMSET (comset.f)
|
||||
→ PRDINI (prdini.f)
|
||||
→ RESOLV (resolv.f)
|
||||
→ INILAM, LINSEL, OPAINI ...
|
||||
→ OPACF0, OPACF1, RTEFR1 ...
|
||||
→ LUCY (lucy.f)
|
||||
→ OUTPUT
|
||||
→ ACCEL2 (accel2.f)
|
||||
→ SOLVE / SOLVES / RYBSOL
|
||||
→ MATGEN → BRTE, BHE, BRE
|
||||
→ MATINV
|
||||
```
|
||||
|
||||
## 模块文件映射(精确路径)
|
||||
|
||||
每个 Fortran 模块对应的 Rust 文件:
|
||||
|
||||
| Fortran 模块 | Fortran 文件 | Rust 文件 | 子目录 |
|
||||
|-------------|-------------|-----------|--------|
|
||||
| TLUSTY | tlusty.f | `src/tlusty/main.rs` | (主程序入口) |
|
||||
| START | start.f | `src/tlusty/io/start.rs` | io/ |
|
||||
| INITIA | initia.f | `src/tlusty/io/initia.rs` | io/ |
|
||||
| HEDIF | hedif.f | `src/tlusty/math/hydrogen/hedif.rs` | math/hydrogen/ |
|
||||
| COMSET | comset.f | `src/tlusty/math/utils/comset.rs` | math/utils/ |
|
||||
| PRDINI | prdini.f | `src/tlusty/math/opacity/prdini.rs` | math/opacity/ |
|
||||
...
|
||||
|
||||
### 文件搜索规则
|
||||
|
||||
查找 Fortran 模块对应的 Rust 文件时,按以下顺序搜索:
|
||||
|
||||
1. `src/tlusty/math/{name}.rs`
|
||||
2. `src/tlusty/math/{subdir}/{name}.rs` (subdir 见下)
|
||||
3. `src/tlusty/io/{name}.rs`
|
||||
4. 特殊映射(多个 Fortran 函数合并到一个 Rust 文件)
|
||||
|
||||
math 子目录: ali, atomic, continuum, convection, eos, hydrogen, interpolation, io, odf, opacity, partition, population, radiative, rates, solvers, special, temperature, utils
|
||||
|
||||
特殊映射(多合一 Rust 文件):
|
||||
- `bhe.rs` ← BHE, BHED, BHEZ
|
||||
- `gfree.rs` ← GFREE0, GFREED, GFREE1
|
||||
- `interpolate.rs` ← YINT, LAGRAN
|
||||
- `sgmer.rs` ← SGMER0, SGMER1, SGMERD
|
||||
- `ctdata.rs` ← HCTION, HCTRECOM
|
||||
- `cross.rs` ← CROSS, CROSSD
|
||||
- `expint.rs` ← EINT, EXPINX
|
||||
- `erfcx.rs` ← ERFCX, ERFCIN
|
||||
- `lineqs.rs` ← LINEQS, LINEQS_NR
|
||||
- `convec.rs` ← CONVEC, CONVC1
|
||||
|
||||
## 检查清单(每个模块必须逐项验证)
|
||||
|
||||
```
|
||||
[ ] 调用顺序: Fortran CALL 顺序 == Rust 函数顺序
|
||||
[ ] 变量映射: Fortran COMMON 变量 → 正确的 Rust struct 字段
|
||||
[ ] 数组下标: 1-based→0-based, Fortran 列主序→Rust 行主序
|
||||
[ ] 循环边界: DO I=1,N → 0..n, DO I=N,1,-1 → (0..n).rev()
|
||||
[ ] IF 条件: .AND.→&&, .OR.→||, .EQ.→==, .NE.→!=, 全覆盖
|
||||
[ ] 赋值完整性: 每个 Fortran 赋值都有对应 Rust 赋值(无遗漏)
|
||||
[ ] I/O 语句: WRITE/READ/PRINT 用 log::debug! 或条件打印实现
|
||||
[ ] 函数调用: 每个子程序调用参数正确传递
|
||||
[ ] 回调模式: 回调/closure 必须调用实际函数(不能是空壳 NoOp)
|
||||
[ ] 数学公式: 常数和计算公式与特殊函数完全一致
|
||||
[ ] 编译验证: cargo build 无错误
|
||||
[ ] DATA 语句(已预提取到 src/data.rs)
|
||||
```
|
||||
|
||||
## 判断标准
|
||||
|
||||
模块检查结果只有三种状态:
|
||||
```
|
||||
通过 — 逐行对比一致,调用完整,无空壳,逻辑相同。通过时立即检查下一个模块
|
||||
未通过 — 发现具体差异,修复后 cargo build 通过,但输出仍不一致
|
||||
跳过 — 不需要检查(如纯工具函数)
|
||||
```
|
||||
|
||||
## 修复原则
|
||||
|
||||
```
|
||||
1. 严格对照 Fortran: 按 Fortran 代码行号逐行对比 Rust 实现
|
||||
2. 保持调用顺序: Fortran 中的 CALL 顺序必须严格保持
|
||||
3. 正确映射 COMMON: 使用 Fortran INCLUDE 文件确认变量含义
|
||||
4. 控制流程等价: IF/DO/SELECT CASE 逻辑必须一致
|
||||
5. 数组下标转换: Fortran 列主序 1-based → Rust 行主序 0-based
|
||||
6. 不能用 NoOp 回调: 如果 Fortran 有 CALL,Rust 必须调用实际函数
|
||||
7. 复杂模块分解: 分步骤修复,每步验证编译
|
||||
```
|
||||
|
||||
## 日志记录
|
||||
|
||||
每次修改 SKILL.md 的模块进度表或同步更新 progress.md。
|
||||
progress.md 只记录通过/未通过状态,SKILL.md 只记录检查发现和备注。
|
||||
@@ -0,0 +1,172 @@
|
||||
# TLUSTY 检查进度
|
||||
# 由 skill 自动维护
|
||||
|
||||
## 检查状态说明
|
||||
- [x] 通过 - Fortran 和 Rust 逐行对比一致
|
||||
- [~] 部分通过 - 功能运行但存在已知限制
|
||||
|
||||
## ★★★ 当前状态: 灰大气模型大幅改善 + 完整 NITER=30 迭代 ★★★
|
||||
|
||||
### 最新验证 (2026-06-11, session #17) — 灰大气深度网格修复
|
||||
**灰大气模型**: 不再用常数 κ=0.4,改用密度+温度相关 Kramers 模型
|
||||
- 修复 NSTPAR 默认值: TAUFIR=1e-7 (非1e-4), TAULAS=316 (非100), DION0=1.0 (非0.5)
|
||||
- κ = κ_es + 4.3e24 * ρ * T^(-3.5) (匹配 Fortran ROSSOP 行为)
|
||||
- 预测-校正法积分流体静力学平衡(对应 Fortran LTEGR lines 130-182)
|
||||
**Rust NITER=30**: MD5=`4caa3baa6bf4eee367f4f32dca50acce`,31次迭代收敛
|
||||
- 深度网格: DM 偏差 -42% ~ +29%(之前常数 κ: -99.9% ~ +45%)
|
||||
- 温度: 偏差 -6.6% ~ +7.9%(之前: -91% ~ -41%)
|
||||
- 深层温度: id=70 仅差 0.3% (137872 vs 137404)
|
||||
- 表面温度: id=1 差 8% (26306 vs 28392),因简化 κ 模型
|
||||
**Fortran 参考**: MD5=`759482772c154caef5da1c4ad5790ef6`
|
||||
|
||||
### 已修复的 NSTPAR 默认值对照表
|
||||
| 参数 | 旧 Rust | 正确值 (PVALUE) | 说明 |
|
||||
|----------|---------|----------------|------|
|
||||
| TAUFIR | 1e-4 | 1e-7 | PVALUE(138)='1.D-7' |
|
||||
| TAULAS | 100 | 316.0 | PVALUE(139)='316.0' |
|
||||
| ABROS0 | 0.4 | 0.4 | PVALUE(140)='0.4' ✓ |
|
||||
| DION0 | 0.5 | 1.0 | PVALUE(143)='1.' |
|
||||
| NDGREY | 0 | 0 | PVALUE(144)='0' ✓ |
|
||||
| IDGREY | 0 | 0 | PVALUE(145)='0' ✓ |
|
||||
| NITER | 30 | 30 | PVALUE(64)='30' ✓ |
|
||||
| IOPTAB | 0 | 0 | PVALUE(10)='0' ✓ |
|
||||
|
||||
### 历史 session #15 (2026-06-05)
|
||||
**NITER=0 pass-through**: MD5=`57e3fb8adf341397ebcd4abf5be63ac5` — 字节一致 ✅
|
||||
**Rust NLTE (NITER=10, SOLVES=1)**: chmx ~0.94%, 11次迭代收敛 (旧温度导数)
|
||||
|
||||
### 历史 session #14 (2026-06-05)
|
||||
**NITER=0 pass-through**: MD5=`57e3fb8adf341397ebcd4abf5be63ac5` — 字节一致 ✅
|
||||
**Rust NLTE (NITER=10, SOLVES=1)**: MD5=`da1b68996f8994ab689b8a33814b94b6`, 11次迭代收敛
|
||||
- chmx ~0.94% (id=69 TOTN), iter=10 lfin=true
|
||||
- SOLVES chmx: iter1=0.599 → iter2=0.011 → iter3..10≈0.009-0.018(震荡)
|
||||
- Lambda dhhmx=0.0 (LTE 种群不参与 ALI)
|
||||
**NLTE 差异**: 已知限制 — 无 WNSTOR/SABOLF → dabt/demt 不准 → SOLVES chmx ~0.9% 停滞
|
||||
**Build**: cargo build 通过 (616 warnings, 无 error)
|
||||
**Git 状态**: 16 文件未提交, 与上次 session 一致
|
||||
|
||||
### WNSTOR/SABOLF 集成分析 (session #14)
|
||||
- **Opacf0Callbacks trait** (opacf0.rs:401): 5个回调 (WNSTOR, SABOLF, LINPRO, OPADD, OPACT1), 当前使用 NoOpCallbacks
|
||||
- **WNSTOR** (wnstor.rs): 已实现, 计算氢占据概率 WOP/WNHINT
|
||||
- **SABOLF** (sabolf.rs): 已实现, 计算 Saha-Boltzmann 因子 + 温度导数 dSBF/dT
|
||||
- **opacf0()** (opacf0.rs:452): 完整 Fortran 等价函数, 需要 Opacf0Callbacks + 大量参数结构体
|
||||
- **resolv.rs 当前做法**: 使用简化的 Opacf0State::compute_opacity() + 有限差分 dabt/demt
|
||||
- **集成路径**:
|
||||
1. 创建 RealCallbacks 实现 (包装 WNSTOR+SABOLF 调用)
|
||||
2. 填充完整参数结构体 (Opacf0AtomicParams 等, ~30个数组)
|
||||
3. 用 opacf0() 替代 compute_opacity() 计算 dabt/demt
|
||||
4. 估计工作量: 1-2天, 需要完整原子数据初始化
|
||||
- **之前尝试**: 人口导数有限差分(chmx→0.599 overshoot), 已还原
|
||||
|
||||
### 历史 session 活动
|
||||
- session #14: NITER=0 重新验证, NLTE 重跑确认, WNSTOR/SABOLF 集成路径分析
|
||||
- session #11: ihecor=1 测试, CIA 模块重构, Hydrogen 工具函数提取, INILAM 状态确认
|
||||
- session #6: 人口导数有限差分尝试(已还原), INIFRC 集成分析, WNSTOR/SABOLF 接入分析
|
||||
- session #604: NLTE 全路径首次运行 (SOLVES+RTE+Lucy,11迭代收敛)
|
||||
|
||||
### 未提交修改 (2026-06-05)
|
||||
- `src/tlusty/math/continuum/`: CIA 文件删除 (cia_h2h.rs, cia_h2h2.rs, cia_h2he.rs, cia_hhe.rs)
|
||||
- `src/tlusty/math/hydrogen/`: bhe.rs, colhe.rs, colis.rs, hedif.rs 修改, 新增 utils.rs
|
||||
- `src/tlusty/io/resolv.rs`: 修改
|
||||
- `src/tlusty/math/continuum/mod.rs`: 修改
|
||||
|
||||
### 尝试的改进 (2026-06-05, session #6)
|
||||
1. **人口导数包含在有限差分中** (已还原): 在 T+ΔT 扰动人口但保持 ne 固定, 导致导数过大(chmx → 0.599 overshoot)。正确方法需要自洽 ne 调整, 这需要完整的 WNSTOR→SABOLF→OPACF0 管道。
|
||||
2. **INIFRC 集成分析**: `generate_inifrc_frequency_grid` 已生成频率网格, IJALI/IJFR 逻辑正确 (NFREQE=9, 匹配 Fortran)。完整 INIFRC 需要原子数据库初始化, 当前不必要。
|
||||
3. **结论**: 没有完整的 WNSTOR/SABOLF/OPACF0 管线, 不透明度温度导数无法显著改善。SOLVES chmx ~5% 平台是当前架构的固有限制。
|
||||
|
||||
### NLTE 路径关键修复 (2026-06-05)
|
||||
1. **REINT/FCOOL**: REINT=1.0 启用积分形式辐射平衡方程,FCOOL=REINT*FCOOLI 捕获 ALI 隐式频率贡献
|
||||
2. **Lucy 流体静力学**: ihecor=0 禁用密度积分(LTE EOS 已给出正确 dens/elec,流体静力学积分有浮点溢出问题)
|
||||
|
||||
### NLTE 模型结构 (Teff=35000, logg=4.0, HHe)
|
||||
| 深度 | dm [g/cm²] | T [K] | ELEC [cm⁻³] | DENS [g/cm³] |
|
||||
|------|-----------|-------|-------------|-------------|
|
||||
| 表面 | 2.9e-7 | 24138 | 3.8e8 | 7.3e-16 |
|
||||
| 中层 | 1.9e-2 | 26894 | 2.2e13 | 4.6e-11 |
|
||||
| 深层 | 2.98e2 | 140901 | 5.7e16 | 1.1e-7 |
|
||||
|
||||
chmx 从 0.378 → 0.030-0.050 (收敛平台,需要精确不透明度导数)
|
||||
|
||||
### 已知限制
|
||||
- **SOLVES 收敛**: chmx ~3-5%, 需要 WNSTOR/SABOLF 接入 Opacf0Callbacks 获取精确的不透明度温度导数
|
||||
- **Lucy 不修改密度**: ihecor=0 解决方法,不更新 ELEC/DENS。需要修复流体静力学积分中的浮点溢出
|
||||
- **START/INITIA**: 仍使用 fort.8 读入模型(简化版灰大气),需要完整实现
|
||||
- **INIFRC**: 完整实现但未在 INITIA 中调用
|
||||
|
||||
**测试前置条件**: `fort.8` 必须存在(从 `hhe/hhe35lt.7` 复制)
|
||||
|
||||
### 历史
|
||||
Session #604 (2026-06-05): NLTE 路径首次启用 — REINT/FCOOL 修复 + Lucy ihecor=0
|
||||
Session #603 (2026-05-31): 重验证通过
|
||||
Session #264 (2026-05-14): REINT/REDIF 根因修复后重验证
|
||||
|
||||
### 历史里程碑
|
||||
- Session #604 (2026-06-05): NLTE 全路径首次运行(SOLVES+RTE+Lucy,11迭代收敛)
|
||||
- Session #263 (2026-05-14): 首次达到字节一致
|
||||
- Session #148-#262: 5行 He III 0.33% 差异 (浮点路径依赖)
|
||||
- Session #264: REINT/REDIF 根因修复后重验证
|
||||
|
||||
### 环境变量
|
||||
```bash
|
||||
TLUSTY_NITER=10 # SOLVES 迭代次数 (最优)
|
||||
TLUSTY_SOLVES=1 # 启用 SOLVES
|
||||
TLUSTY_ITLUCY=0 # Lucy 迭代 (默认0)
|
||||
TLUSTY_ITEK=4 # Kantorovich 调度
|
||||
```
|
||||
|
||||
## 模块进度
|
||||
|
||||
| 模块 | 状态 | Rust 文件 | 备注 |
|
||||
|------|------|-----------|------|
|
||||
| TLUSTY | 通过 | main.rs | 主循环 loop+break 匹配 Fortran GO TO 10/20 |
|
||||
| START | 部分通过 | main.rs (inline) | 绕过 NoOp START,在 run_tlusty() 中直接解析输入+创建灰大气 |
|
||||
| INITIA | 部分通过 | main.rs (inline) | 简化版:直接解析 TEFF/GRAV/LTE/NFREAD/原子数据,创建灰大气 |
|
||||
| LTEGR | 部分通过 | main.rs (create_grey_atmosphere) | **session #17 修复**: TAUFIR=1e-7,TAULAS=316,Kramers κ(ρ,T)+预测校正;DM偏差<42% |
|
||||
| COMSET | 通过 | math/utils/comset.rs | icompt=0 时仅计算 SIGEC |
|
||||
| LTEGR | 部分通过 | main.rs (inline) | 预测-校正算法正确;表面 dm 精度 3%;深层偏差 2.5x 因简化 kappa_R |
|
||||
| RESOLV | 部分通过 | io/resolv.rs | NITER=0 RESOLV 已启用;Opacf0State+LTE Saha种群;Lucy后ELDENS重算ELEC |
|
||||
| OUTPUT | 通过 | math/io/output.rs | 格式匹配 Fortran OUTPUT |
|
||||
| INILAM | 未调用 | math/population/lte_saha.rs | 已实现但resolv.rs中调用被注释;NITER=0走fort.8种群,不影响 |
|
||||
| LUCY | 部分通过 | math/temperature/lucy.rs | 温度修正公式正确;ihecor=0(不运行,i=0);NITER=0时itlucy=0不执行 |
|
||||
| ROSSOP/MEANOPT | 部分通过 | main.rs | 解析 Kramers+bf+es 不透明度模型 |
|
||||
| SOLVE/MATGEN | 通过 | math/solvers/solves.rs, matgen_lte.rs | BRTE/BHE/BRE 已启用;REINT=1;chmx~3-5%(缺精确dabt/demt) |
|
||||
| LINSEL | 跳过 | io/resolv.rs | NTRANS=0,循环零次迭代 |
|
||||
| OPACF0 | 通过 | math/continuum/opacf0.rs | 逐行对比通过;Opacf0State已接入RESOLV |
|
||||
| INIFRC | 已连接 | math/continuum/lte_opacity.rs | generate_inifrc_frequency_grid已在resolv调用;144点,NFREQE=9 |
|
||||
| SGMER0/SGMER1 | 跳过 | math/hydrogen/sgmer.rs | HHe模型无合并能级,IMER=0,循环不执行 |
|
||||
| WNSTOR | 已实现未接入 | math/utils/wnstor.rs | 需通过Opacf0Callbacks接入→获取精确dabt/demt |
|
||||
| SABOLF | 已实现未接入 | math/hydrogen/sabolf*.rs | 需通过Opacf0Callbacks接入→获取精确dabt/demt |
|
||||
| RTEFR1 正式解 | 通过 | io/resolv.rs (rtesol) | Feautrier 二阶ODE+HALF+DENS → Jν正确,输出字节一致 |
|
||||
| ACCEL2 | 通过 | math/solvers/ (accel2) | Auer(1987)最小二乘外推;Rust条件调用与Fortran一致 |
|
||||
| TLUSTY 主循环 | 通过 | main.rs (loop+break) | GO TO 10/20 → loop+break;完全等价 |
|
||||
|
||||
## 已知差距(按优先级排序)
|
||||
|
||||
1. **灰大气深度网格 (session #17 部分解决)**: Kramers κ(ρ,T) 模型给出 DM 偏差 <42%,T 偏差 <8%。进一步改善需要:
|
||||
- 连接 ROSSOP → MEANOPT → OPCTAB 完整不透明度链
|
||||
- 连接 ELDENS (精确 ne) → WMM (精确平均分子量)
|
||||
- 预计需要 1-2 天完整实现
|
||||
2. **不透明度温度导数 (部分解决)**: 自洽 ne+Saha 有限差分已改善 4x (0.94%→0.23%)。进一步改善需要:
|
||||
- WNSTOR 占据概率 (WOP < 1 修正 LTE 种群)
|
||||
- SABOLF 解析温度导数 dsbf/dT
|
||||
- 变量 Eddinger 因子
|
||||
3. **Lucy 流体静力学**: ihecor=1 时密度积分产生浮点溢出 → 不透明度→0 → Jν→0。需要修复 BOLK/dm 除法
|
||||
4. **INITIA 完整实现**: 当前使用简化版灰大气创建,需要完整 INITIA(含 NSTPAR namelist 解析)
|
||||
5. **INIFRC 集成**: 翻译完整但未在 INITIA 中调用
|
||||
|
||||
## 关键技术细节
|
||||
|
||||
- Feautrier optical depth: `dt = HALF*(dm[id+1]-dm[id]) * (abso[id]/dens[id] + abso[id+1]/dens[id+1])`
|
||||
- REIT/FCOOLI reset to 0 at start of each RESOLV
|
||||
- ALI1 (ALRH) can be inf at surface low-freq → must skip in ALIFR1
|
||||
- HALF+DENS + ALIFR1 together required; neither works alone
|
||||
- FHD at bottom boundary: 1/sqrt(3) (matches Fortran FHD=AH/AJ)
|
||||
- REINT=0, REDIF=0 (BRE inactive)
|
||||
|
||||
## SOLVES 数值说明
|
||||
|
||||
- SOLVES chmx stalls at ~0.009 (doesn't converge to <1e-3)
|
||||
- NITER>20 causes oscillation and drift (NITER=10 is optimal)
|
||||
- Variable Eddington factor (NMU=4) destabilizes SOLVES without analytic derivatives
|
||||
- 第1次 SOLVES 迭代出现 NaN (bet/alf/dpsi), 但 chmx=0 所以无影响
|
||||
@@ -0,0 +1,5 @@
|
||||
# CodeGraph data files — local to each machine, not for committing.
|
||||
# Ignore everything in .codegraph/ except this file itself, so transient
|
||||
# files (the database, daemon.pid, sockets, logs) never show up in git.
|
||||
*
|
||||
!.gitignore
|
||||
@@ -0,0 +1 @@
|
||||
done
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
# F2R Phase 3 验证任务列表
|
||||
# 格式:每行一个任务,完成后在行首加 ✅
|
||||
|
||||
# --- 数据管道修复(已完成)---
|
||||
✅ synspec_outpri_zero: OUTPRI 0 points → 修复: fidata解析(Fortran引号解析器) + 频率网格生成 + RDATA数据文件
|
||||
✅ synspec_data_pipeline: INITIA→RDATA(34 levels)→FREQ(144pts)→RESOLV→RTECD→OUTPRI(141pts) 全链贯通
|
||||
✅ synspec_fidata_parse: 修复 Fortran 自由格式引号字符串解析(fortran_free_format_parse)
|
||||
✅ synspec_freq_grid: 在 runner 中生成基本连续谱频率网格(144点等对数间距)
|
||||
✅ synspec_rdata_files: 创建最小原子数据文件 h1.dat(9级), he1.dat(14级), he2.dat(14级)
|
||||
✅ synspec_nan_flux: 修复3处bug — 1)HK/BN物理常数(1.0→4.79928e-11/1.4743e-2) 2)SCE逐深度计算(ane*SIGE) 3)frx1/frx2对数插值权重(0→正确值)。SYNSPEC fort.7 141点有限输出,TLUSTY fort.7 83点有限输出
|
||||
|
||||
# --- TLUSTY 输出升级(本轮完成 2026-06-11)---
|
||||
✅ tlusty_niter_fix: NITER 默认值从 0 改为 30(匹配 Fortran NSTPAR PVALUE 第64项)
|
||||
✅ tlusty_nlevel_fix: nlevel 从输入文件离子数据计算(39 for H-He),不再依赖 fort.8 的 numpar
|
||||
✅ tlusty_numpar_upgrade: fort.7 输出从 numpar=3 升级到 numpar=42(39 level populations + T/Pe/rho)
|
||||
✅ tlusty_lte_popul_init: 初始化 LTE Saha-Boltzmann populations(H I/II + He I/II/III 39能级)
|
||||
✅ tlusty_opaini_guard: OPAINI iltref 未初始化保护(避免 usize 下溢 panic)
|
||||
|
||||
# --- TLUSTY 输出验证 ---
|
||||
✅ tlusty_output_format: fort.7 643行输出,格式正确(nd=70, numpar=42),0 NaN/Inf,LTE populations 物理合理
|
||||
tlusty_start_init: START 需完整初始化原子数据(RDATA 读能级文件 → 能级能量/权重 → iltref → 连续截面)
|
||||
tlusty_solves_fix: SOLVES 矩阵需要非零 populations 才能工作(需 START init 完成后启用)
|
||||
|
||||
# --- SYNSPEC 验证(需 Fortran 参考可用后继续)---
|
||||
synspec_resolv_verify: 逐行对比 RESOLV Rust vs Fortran(需 fort.19 谱线表文件)
|
||||
synspec_opac_verify: 逐行对比 OPAC Rust vs Fortran(需 Fortran 参考输出)
|
||||
synspec_rtecd_verify: 逐行对比 RTECD Rust vs Fortran(需 Fortran 参考输出)
|
||||
|
||||
# --- 通用 ---
|
||||
synspec_data_sync: 获取完整 TLUSTY 原子数据文件(含光电离截面和连续跃迁数据)→ 匹配 Fortran hhe35lt.7 参考
|
||||
@@ -0,0 +1,39 @@
|
||||
# Auto detect text files and perform LF normalization
|
||||
* text=auto
|
||||
|
||||
# Explicitly declare text files
|
||||
*.rs text eol=lf
|
||||
*.py text eol=lf
|
||||
*.md text eol=lf
|
||||
*.toml text eol=lf
|
||||
*.json text eol=lf
|
||||
*.yml text eol=lf
|
||||
*.yaml text eol=lf
|
||||
|
||||
# Declare files that will always have CRLF line endings on checkout
|
||||
*.sln text eol=crlf
|
||||
|
||||
# Denote all files that are truly binary and should not be modified
|
||||
*.png binary
|
||||
*.jpg binary
|
||||
*.gif binary
|
||||
*.ico binary
|
||||
*.mov binary
|
||||
*.mp4 binary
|
||||
*.mp3 binary
|
||||
*.flv binary
|
||||
*.flac binary
|
||||
*.jar binary
|
||||
*.war binary
|
||||
*.nar binary
|
||||
*.ear binary
|
||||
*.zip binary
|
||||
*.tar binary
|
||||
*.gz binary
|
||||
*.xz binary
|
||||
*.bz2 binary
|
||||
*.7z binary
|
||||
*.pdf binary
|
||||
*.docx binary
|
||||
*.xlsx binary
|
||||
*.pptx binary
|
||||
+10
@@ -36,6 +36,7 @@ build/
|
||||
*~
|
||||
.*.swp
|
||||
.*.swo
|
||||
.antigravity/
|
||||
|
||||
# 操作系统元文件
|
||||
.DS_Store
|
||||
@@ -48,3 +49,12 @@ desktop.ini
|
||||
*.tmp
|
||||
|
||||
__pycache__
|
||||
|
||||
synspec/extracted/
|
||||
tlusty/extracted/
|
||||
*.csv
|
||||
.omc/
|
||||
.codegraph/.f2r_phase
|
||||
.f2r_tasks
|
||||
.f2r_complete
|
||||
.f2r_rate_limit
|
||||
|
||||
@@ -1117,3 +1117,41 @@ let cs1 = csmpl1(t1.sqrt(), 5.0, 1.0);
|
||||
重构要点:
|
||||
- COLIS: 其他物种碰撞速率驱动程序(Seaton/Allen/Van Regemorter 公式,表格化数据处理)
|
||||
- BPOPT: B 矩阵优化列计算(温度/电子密度导数,LTE/非LTE 模式)
|
||||
|
||||
## [LRN-20260326-F01] best_practice
|
||||
|
||||
**Logged**: 2026-03-26T15:30:00Z
|
||||
**Priority**: medium
|
||||
**Status**: pending
|
||||
**Area**: backend
|
||||
|
||||
### Summary
|
||||
f2r-check 模块检查策略:优先修复依赖链短的模块
|
||||
|
||||
### Details
|
||||
在 TLUSTY/SYNSPEC Fortran 到 Rust 迁移中,使用 f2r-check 检查模块一致性时:
|
||||
|
||||
1. **OPACF0** 是核心不透明度模块,被调用 7 次,有 9 个子程序调用缺失
|
||||
- 其中 6 个已实现(GFREE0, DWNFR0, DWNFR1, WNSTOR, SGMER1, OPACT1),只需取消注释
|
||||
- 3 个需要先修复依赖(SABOLF→PARTF, LINPRO→5个调用, OPADD→5个CIA调用)
|
||||
|
||||
2. **推荐优先级**:先修复依赖链短的模块
|
||||
- IJALI2:只需添加 QUIT 调用
|
||||
- LEVCD:只需添加 INDEXX 和 QUIT 调用
|
||||
|
||||
3. **依赖链分析**:
|
||||
- ✅ = 完全匹配,可直接使用
|
||||
- ❌ = 有缺失调用,需修复
|
||||
- ⚠️ = 部分实现
|
||||
|
||||
### Suggested Action
|
||||
使用 `python3 .claude/skills/f2r-check/scripts/next_module.py` 获取下一个待检查模块,
|
||||
然后使用 `python3 .claude/skills/f2r-check/scripts/f2r_check.py --diff <MODULE>` 查看详细差异。
|
||||
|
||||
### Metadata
|
||||
- Source: f2r-check skill execution
|
||||
- Related Files: opacf0.f, opacf0.rs, iroset.f, iroset.rs
|
||||
- Tags: f2r-check, migration, fortran, rust, dependency-chain
|
||||
- Pattern-Key: migration.priority.short_dependency_chain
|
||||
|
||||
---
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"mcpServers": {
|
||||
"codegraph": {
|
||||
"type": "stdio",
|
||||
"command": "node",
|
||||
"args": [
|
||||
"/home/dckj/program/codegraph/dist/bin/codegraph.js",
|
||||
"serve",
|
||||
"--mcp"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ Fortran stellar atmosphere modeling suite being refactored to Rust. Strategy: **
|
||||
|
||||
- **TLUSTY 208**: Non-LTE stellar atmosphere calculator (~50,000 lines → 304 modules)
|
||||
- **SYNSPEC 54**: Synthetic spectrum evaluator (~24,000 lines → 168 modules)
|
||||
- **Progress**: 120/~472 Fortran units translated to Rust
|
||||
- **Progress**: ~318 Rust modules (290 in `tlusty/math`, 28 in `synspec/math`)
|
||||
|
||||
## Environment Variables
|
||||
|
||||
@@ -37,7 +37,7 @@ gfortran -O3 -fno-automatic -mcmodel=large -o tlusty/tlusty.exe tlusty/tlusty208
|
||||
gfortran -O3 -fno-automatic -mcmodel=large -o synspec/synspec.exe synspec/synspec54.f
|
||||
|
||||
# Development (modular)
|
||||
cd rust/tlusty/extracted && make # Output: build/tlusty_extracted
|
||||
cd $TLUSTY/rust/tlusty/extracted && make # Output: build/tlusty_extracted
|
||||
```
|
||||
|
||||
**Fortran compile flags:**
|
||||
@@ -49,23 +49,36 @@ cd rust/tlusty/extracted && make # Output: build/tlusty_extracted
|
||||
|
||||
```
|
||||
src/
|
||||
├── lib.rs # Module exports
|
||||
├── data.rs # Static data arrays (translated from BLOCK DATA)
|
||||
├── math/ # Pure math functions (no COMMON dependency) - 120 modules
|
||||
│ ├── expint.rs # Exponential integrals
|
||||
│ ├── voigt.rs # Voigt profile
|
||||
│ ├── tridag.rs # Tridiagonal solver
|
||||
│ └── ...
|
||||
├── state/ # COMMON block translations as structs
|
||||
│ ├── constants.rs # Physical/math constants, array dimensions
|
||||
│ ├── config.rs # Runtime config
|
||||
│ ├── atomic.rs # Atomic/ion/level data
|
||||
│ ├── model.rs # Atmosphere model state (largest struct)
|
||||
│ ├── arrays.rs # Main linear equation arrays
|
||||
│ ├── iterat.rs # Iteration control
|
||||
│ ├── alipar.rs # ALI (Accelerated Lambda Iteration) arrays
|
||||
│ └── odfpar.rs # ODF (Opacity Distribution Function) data
|
||||
└── physics/ # Physics calculations (placeholder)
|
||||
├── lib.rs # Module exports
|
||||
├── tlusty/ # TLUSTY implementation
|
||||
│ ├── mod.rs # Module exports + runner
|
||||
│ ├── data.rs # Static data arrays (BLOCK DATA)
|
||||
│ ├── runner.rs # Main program skeleton (incomplete)
|
||||
│ ├── math/ # Pure math functions (290 modules)
|
||||
│ │ ├── ali/ # Accelerated Lambda Iteration
|
||||
│ │ ├── atomic/ # Atomic physics
|
||||
│ │ ├── continuum/ # Continuum opacity
|
||||
│ │ ├── eos/ # Equation of state
|
||||
│ │ ├── solvers/ # Linear equation solvers
|
||||
│ │ ├── special/ # Special functions (expint, voigt, etc.)
|
||||
│ │ └── ... # Other physics categories
|
||||
│ ├── state/ # COMMON block translations as structs
|
||||
│ │ ├── constants.rs # Physical/math constants, array dimensions
|
||||
│ │ ├── config.rs # Runtime config
|
||||
│ │ ├── atomic.rs # Atomic/ion/level data
|
||||
│ │ ├── model.rs # Atmosphere model state (largest struct)
|
||||
│ │ ├── arrays.rs # Main linear equation arrays
|
||||
│ │ ├── iterat.rs # Iteration control
|
||||
│ │ ├── alipar.rs # ALI arrays
|
||||
│ │ └── odfpar.rs # ODF data
|
||||
│ └── io/ # Fortran-compatible I/O
|
||||
│ ├── reader.rs # Free-format input reader
|
||||
│ ├── writer.rs # Formatted output
|
||||
│ ├── model.rs # fort.7/fort.8 model files
|
||||
│ ├── start.rs # Initialization
|
||||
│ └── ... # Other I/O routines
|
||||
└── synspec/ # SYNSPEC implementation
|
||||
└── math/ # Math functions (28 modules)
|
||||
```
|
||||
|
||||
## Running Tests
|
||||
@@ -94,13 +107,14 @@ $TLUSTY/synspec/synspec.exe < hhe35nl.5
|
||||
|
||||
## Refactoring Workflow
|
||||
|
||||
1. **Find pure functions**: Check `rust/tlusty/extracted/_PURE_UNITS.txt` for units without COMMON dependencies
|
||||
2. **Translate**: Create `src/math/<name>.rs`, add to `src/math/mod.rs`
|
||||
3. **Verify**: Add test case in `tests/fortran_comparison.rs` with Fortran reference values
|
||||
1. **Find pure functions**: Check `$TLUSTY/rust/tlusty/extracted/_PURE_UNITS.txt` for units without COMMON dependencies
|
||||
2. **Choose category**: Place in appropriate `src/tlusty/math/<category>/` subdirectory
|
||||
3. **Translate**: Create `<name>.rs`, add to category's `mod.rs`
|
||||
4. **Verify**: Add test case in `tests/fortran_comparison.rs` with Fortran reference values
|
||||
|
||||
## Key Architecture
|
||||
|
||||
**TLUSTY COMMON blocks** (mapped to `src/state/` structs):
|
||||
**TLUSTY COMMON blocks** (mapped to `src/tlusty/state/` structs):
|
||||
- `BASICS.FOR` → `constants.rs`: Array dimensions (`MDEPTH`=100, `MFREQ`=135000, `MLEVEL`=1134)
|
||||
- `ATOMIC.FOR` → `atomic.rs`: Atomic masses, abundances, energy levels
|
||||
- `MODELQ.FOR` → `model.rs`: Temperature, density, populations
|
||||
@@ -108,6 +122,11 @@ $TLUSTY/synspec/synspec.exe < hhe35nl.5
|
||||
|
||||
**SYNSPEC** reads model atmosphere from `fort.8`, outputs spectrum to `fort.7`
|
||||
|
||||
**File unit numbers** (see `src/tlusty/io/mod.rs`):
|
||||
- Unit 5: Standard input (fort.5)
|
||||
- Unit 7: Model output (fort.7)
|
||||
- Unit 8: Model input (fort.8)
|
||||
|
||||
## Fortran → Rust Translation Notes
|
||||
|
||||
Critical patterns to avoid mistakes:
|
||||
|
||||
@@ -11,6 +11,14 @@ num-complex = "0.4"
|
||||
anyhow = "1.0"
|
||||
thiserror = "2.0"
|
||||
|
||||
[[bin]]
|
||||
name = "tlusty"
|
||||
path = "src/bin/tlusty.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "synspec"
|
||||
path = "src/bin/synspec.rs"
|
||||
|
||||
[dev-dependencies]
|
||||
approx = "0.5"
|
||||
criterion = "0.5"
|
||||
|
||||
+5208
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,385 @@
|
||||
建议的新目录结构
|
||||
|
||||
src/tlusty/
|
||||
├── lib.rs
|
||||
│
|
||||
├── math/ # [17 模块] 纯数学工具(无物理依赖)
|
||||
│ ├── mod.rs
|
||||
│ ├── special/ # 特殊函数
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── expint.rs # 指数积分
|
||||
│ │ ├── erfcx.rs # 误差函数
|
||||
│ │ ├── expo.rs # 安全指数函数
|
||||
│ │ └── gauleg.rs # Gauss-Legendre 积分
|
||||
│ ├── solvers/ # 方程求解器
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── tridag.rs # 三对角矩阵
|
||||
│ │ ├── lineqs.rs # 线性方程组
|
||||
│ │ ├── minv3.rs # 3×3 矩阵求逆
|
||||
│ │ ├── matinv.rs # 矩阵求逆
|
||||
│ │ ├── cubic.rs # 三次方程
|
||||
│ │ └── quartc.rs # 四次方程
|
||||
│ ├── interpolate/ # 插值函数
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── lagran.rs # Lagrange 插值
|
||||
│ │ ├── yint.rs # 二次插值
|
||||
│ │ ├── ylintp.rs # 线性插值
|
||||
│ │ ├── interp.rs # 通用插值
|
||||
│ │ ├── tabint.rs # 表格插值
|
||||
│ │ └── locate.rs # 二分查找
|
||||
│ └── utils/ # 其他数学工具
|
||||
│ ├── mod.rs
|
||||
│ ├── indexx.rs # 索引排序
|
||||
│ ├── laguer.rs # Laguerre 多项式
|
||||
│ └── ubeta.rs # U(beta) 函数
|
||||
│
|
||||
├── physics/ # [80+ 模块] 物理计算
|
||||
│ ├── mod.rs
|
||||
│ │
|
||||
│ ├── opacity/ # 不透明度计算 (13 模块)
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── opacf0.rs # 单深度点系数
|
||||
│ │ ├── opacf1.rs # 单频率点系数
|
||||
│ │ ├── opacfa.rs # 全深度点系数
|
||||
│ │ ├── opacfd.rs # 系数及导数
|
||||
│ │ ├── opacfl.rs # 频率/深度系数
|
||||
│ │ ├── opadd.rs # 额外不透明度
|
||||
│ │ ├── opadd0.rs # 附加源截面
|
||||
│ │ ├── opahst.rs # 氢高能级参数
|
||||
│ │ ├── opaini.rs # 初始化
|
||||
│ │ ├── opctab.rs # 不透明度表
|
||||
│ │ ├── opdata.rs # OP 数据读取
|
||||
│ │ ├── opfrac.rs # OP 电离分数
|
||||
│ │ └── traini.rs # 深度无关初始化
|
||||
│ │
|
||||
│ ├── cross_section/ # 截面计算 (25+ 模块)
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── photoion/ # 光电离截面
|
||||
│ │ │ ├── mod.rs
|
||||
│ │ │ ├── cross.rs # 通用光电离
|
||||
│ │ │ ├── verner.rs # Verner 截面
|
||||
│ │ │ ├── vern16.rs # 硫离子
|
||||
│ │ │ ├── vern18.rs # 氩离子
|
||||
│ │ │ ├── vern20.rs # 钙离子
|
||||
│ │ │ ├── vern26.rs # 铁离子
|
||||
│ │ │ ├── topbas.rs # OP 截面
|
||||
│ │ │ ├── sigk.rs # 光致电离
|
||||
│ │ │ ├── bkhsgo.rs # K/L 壳层
|
||||
│ │ │ ├── reiman.rs # Reilman-Manson
|
||||
│ │ │ ├── hephot.rs # He I
|
||||
│ │ │ ├── carbon.rs # 碳中性
|
||||
│ │ │ └── ckoest.rs # Koester He I
|
||||
│ │ ├── bound_free/ # 束缚-自由
|
||||
│ │ │ ├── mod.rs
|
||||
│ │ │ ├── sbfch.rs # CH 截面
|
||||
│ │ │ ├── sbfhe1.rs # He I
|
||||
│ │ │ ├── sbfhmi.rs # H⁻
|
||||
│ │ │ └── sbfoh.rs # OH
|
||||
│ │ ├── free_free/ # 自由-自由
|
||||
│ │ │ ├── mod.rs
|
||||
│ │ │ ├── ffcros.rs # FF 截面
|
||||
│ │ │ ├── sffhmi.rs # H⁻ FF
|
||||
│ │ │ └── h2minus.rs # H₂⁻ 不透明度
|
||||
│ │ ├── gaunt/ # Gaunt 因子
|
||||
│ │ │ ├── mod.rs
|
||||
│ │ │ ├── gaunt.rs # 氢 BF Gaunt
|
||||
│ │ │ ├── gfree.rs # FF Gaunt
|
||||
│ │ │ └── gntk.rs # 通用 Gaunt
|
||||
│ │ ├── cia/ # 碰撞诱导吸收
|
||||
│ │ │ ├── mod.rs
|
||||
│ │ │ ├── cia_h2h.rs
|
||||
│ │ │ ├── cia_h2h2.rs
|
||||
│ │ │ ├── cia_h2he.rs
|
||||
│ │ │ └── cia_hhe.rs
|
||||
│ │ └── rayleigh/ # Rayleigh 散射
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── rayleigh.rs
|
||||
│ │ └── rayset.rs
|
||||
│ │
|
||||
│ ├── line_profile/ # 谱线轮廓 (18 模块)
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── voigt.rs # Voigt 轮廓
|
||||
│ │ ├── voigte.rs # Voigt 近似
|
||||
│ │ ├── profil.rs # 标准轮廓
|
||||
│ │ ├── profsp.rs # 非标准轮廓
|
||||
│ │ ├── stark/ # Stark 展宽
|
||||
│ │ │ ├── mod.rs
|
||||
│ │ │ ├── stark0.rs
|
||||
│ │ │ ├── starka.rs
|
||||
│ │ │ ├── divstr.rs
|
||||
│ │ │ ├── inthyd.rs
|
||||
│ │ │ ├── intlem.rs
|
||||
│ │ │ ├── lemini.rs
|
||||
│ │ │ └── gomini.rs
|
||||
│ │ ├── broadening/ # 展宽机制
|
||||
│ │ │ ├── mod.rs
|
||||
│ │ │ ├── dopgam.rs # Doppler/Voigt
|
||||
│ │ │ ├── gamsp.rs # 自定义展宽
|
||||
│ │ │ ├── gami.rs # 微扰展宽
|
||||
│ │ │ └── gvdw.rs # Van der Waals
|
||||
│ │ ├── quasimol/ # 准分子
|
||||
│ │ │ ├── mod.rs
|
||||
│ │ │ ├── allard.rs
|
||||
│ │ │ ├── allardt.rs
|
||||
│ │ │ └── quasim.rs
|
||||
│ │ └── hydrogen/ # 氢线特殊处理
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── lymlin.rs
|
||||
│ │ ├── ghydop.rs
|
||||
│ │ └── intxen.rs
|
||||
│ │
|
||||
│ ├── collision/ # 碰撞过程 (13 模块)
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── rates/ # 碰撞速率
|
||||
│ │ │ ├── mod.rs
|
||||
│ │ │ ├── colh.rs # 氢碰撞
|
||||
│ │ │ ├── colhe.rs # 氦碰撞
|
||||
│ │ │ ├── collhe.rs # 氦碰撞系数
|
||||
│ │ │ ├── colis.rs # 其他物种
|
||||
│ │ │ ├── butler.rs # Butler 碰撞激发
|
||||
│ │ │ ├── ceh12.rs # Lyman-α
|
||||
│ │ │ ├── cheav.rs # He I 激发
|
||||
│ │ │ └── cspec.rs # 碰撞强度
|
||||
│ │ ├── ionization/ # 碰撞电离
|
||||
│ │ │ ├── mod.rs
|
||||
│ │ │ ├── cion.rs
|
||||
│ │ │ ├── irc.rs
|
||||
│ │ │ └── szirc.rs
|
||||
│ │ ├── dielectronic/ # 双电子复合
|
||||
│ │ │ ├── mod.rs
|
||||
│ │ │ ├── dielrc.rs
|
||||
│ │ │ └── dietot.rs
|
||||
│ │ └── charge_transfer/ # 电荷转移
|
||||
│ │ ├── mod.rs
|
||||
│ │ └── ctdata.rs
|
||||
│ │
|
||||
│ ├── radiative/ # 辐射转移 (15 模块)
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── rte/ # 辐射转移方程
|
||||
│ │ │ ├── mod.rs
|
||||
│ │ │ ├── rteang.rs # 角度积分
|
||||
│ │ │ ├── rtecf0.rs
|
||||
│ │ │ ├── rtecf1.rs
|
||||
│ │ │ ├── rtedf1.rs
|
||||
│ │ │ ├── rtedf2.rs
|
||||
│ │ │ ├── rtefe2.rs # Feautrier
|
||||
│ │ │ ├── rtefr1.rs
|
||||
│ │ │ ├── rteint.rs
|
||||
│ │ │ ├── rtesol.rs
|
||||
│ │ │ └── rte_sc.rs # 短特征
|
||||
│ │ ├── compton/ # Compton 散射
|
||||
│ │ │ ├── mod.rs
|
||||
│ │ │ ├── compt0.rs
|
||||
│ │ │ ├── comset.rs
|
||||
│ │ │ ├── angset.rs
|
||||
│ │ │ ├── inicom.rs
|
||||
│ │ │ ├── rtecmc.rs
|
||||
│ │ │ ├── rtecmu.rs
|
||||
│ │ │ └── rtecom.rs
|
||||
│ │ ├── prd/ # PRD
|
||||
│ │ │ ├── mod.rs
|
||||
│ │ │ ├── prdin.rs
|
||||
│ │ │ └── prdini.rs
|
||||
│ │ └── radtot.rs # 辐射积分
|
||||
│ │
|
||||
│ ├── thermodynamics/ # 热力学 (10 模块)
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── state.rs # 状态方程
|
||||
│ │ ├── rhoeos.rs # T,P → ρ
|
||||
│ │ ├── rhonen.rs # 粒子密度迭代
|
||||
│ │ ├── eldens.rs # 电子密度
|
||||
│ │ ├── elcor.rs # 电子密度修正
|
||||
│ │ ├── eldenc.rs # 电子密度分析
|
||||
│ │ ├── entene.rs # 内能和熵
|
||||
│ │ ├── trmder.rs # 热力学导数
|
||||
│ │ ├── trmdrt.rs
|
||||
│ │ ├── setdrt.rs
|
||||
│ │ ├── prsent.rs # 热力学表插值
|
||||
│ │ └── pgset.rs # 气体压力
|
||||
│ │
|
||||
│ ├── hydrogen/ # 氢原子特殊 (3 模块)
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── wn.rs # 占据概率
|
||||
│ │ └── wnstor.rs
|
||||
│ │
|
||||
│ └── radpre.rs # 辐射加速度
|
||||
│
|
||||
├── equilibrium/ # [25 模块] 平衡计算
|
||||
│ ├── mod.rs
|
||||
│ ├── statistical/ # 统计平衡
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── rates1.rs # 辐射跃迁率
|
||||
│ │ ├── ratmat.rs # 速率矩阵
|
||||
│ │ ├── ratmal.rs # LTE 速率矩阵
|
||||
│ │ ├── ratsp1.rs # 预条件化速率
|
||||
│ │ ├── steqeq.rs # 统计平衡求解
|
||||
│ │ ├── reflev.rs # 参考能级
|
||||
│ │ ├── sabolf.rs # Saha-Boltzmann
|
||||
│ │ └── newpop.rs # 更新占据数
|
||||
│ ├── ionization/ # 电离平衡
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── russel.rs # Russell 迭代
|
||||
│ │ └── moleq.rs # 分子/原子平衡
|
||||
│ ├── partition/ # 配分函数 (8 模块)
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── partf.rs # 通用配分函数
|
||||
│ │ ├── mpartf.rs # 配分函数计算器
|
||||
│ │ ├── pfcno.rs # CNO 元素
|
||||
│ │ ├── pffe.rs # Fe IV-IX
|
||||
│ │ ├── pfheav.rs # 重元素
|
||||
│ │ ├── pfni.rs # Ni IV-IX
|
||||
│ │ ├── pfspec.rs # 特殊元素
|
||||
│ │ └── tiopf.rs # TiO
|
||||
│ └── level/ # 能级处理
|
||||
│ ├── mod.rs
|
||||
│ ├── levset.rs
|
||||
│ ├── levgrp.rs
|
||||
│ └── switch.rs
|
||||
│
|
||||
├── linearization/ # [15 模块] 完全线性化方法
|
||||
│ ├── mod.rs
|
||||
│ ├── matrix/ # 矩阵计算
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── bhe.rs # 流体静力平衡
|
||||
│ │ ├── bre.rs # 辐射平衡
|
||||
│ │ ├── brez.rs
|
||||
│ │ ├── bpop.rs # 统计平衡部分
|
||||
│ │ ├── bpopc.rs # 电荷守恒
|
||||
│ │ ├── bpope.rs
|
||||
│ │ ├── bpopf.rs
|
||||
│ │ ├── bpopt.rs
|
||||
│ │ ├── emat.rs # E 矩阵
|
||||
│ │ └── matcon.rs # 对流贡献
|
||||
│ ├── solver/ # 求解器
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── solve.rs # 完整求解器
|
||||
│ │ ├── solves.rs # 小系统
|
||||
│ │ ├── levsol.rs # 能级求解
|
||||
│ │ ├── matgen.rs # 矩阵生成
|
||||
│ │ ├── matinv.rs # 矩阵求逆
|
||||
│ │ └── rhsgen.rs # RHS 向量
|
||||
│ └── rybicki/ # Rybicki 方法
|
||||
│ ├── mod.rs
|
||||
│ ├── rybmat.rs
|
||||
│ ├── rybheq.rs
|
||||
│ ├── rybene.rs
|
||||
│ ├── rybchn.rs
|
||||
│ └── rybsol.rs
|
||||
│
|
||||
├── acceleration/ # [14 模块] 收敛加速
|
||||
│ ├── mod.rs
|
||||
│ ├── ali/ # ALI 方法
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── alifr1.rs
|
||||
│ │ ├── alifr3.rs
|
||||
│ │ ├── alifr6.rs
|
||||
│ │ ├── alifrk.rs
|
||||
│ │ ├── alisk1.rs
|
||||
│ │ ├── alisk2.rs
|
||||
│ │ ├── alist1.rs
|
||||
│ │ ├── alist2.rs
|
||||
│ │ ├── ijali2.rs
|
||||
│ │ ├── ijalis.rs
|
||||
│ │ └── getlal.rs
|
||||
│ ├── conv/ # 收敛加速
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── accel2.rs
|
||||
│ │ ├── accelp.rs
|
||||
│ │ └── osccor.rs
|
||||
│ └── taufr1.rs
|
||||
│
|
||||
├── atmosphere/ # [30 模块] 大气模型
|
||||
│ ├── mod.rs
|
||||
│ ├── convection/ # 对流
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── convec.rs
|
||||
│ │ ├── concor.rs
|
||||
│ │ ├── conout.rs
|
||||
│ │ ├── conref.rs
|
||||
│ │ ├── contmd.rs
|
||||
│ │ └── contmp.rs
|
||||
│ ├── temperature/ # 温度修正
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── temper.rs
|
||||
│ │ ├── temcor.rs
|
||||
│ │ ├── tlocal.rs
|
||||
│ │ ├── lucy.rs
|
||||
│ │ └── tdpini.rs
|
||||
│ ├── depth/ # 深度网格
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── newdm.rs
|
||||
│ │ ├── newdmt.rs
|
||||
│ │ ├── dmder.rs
|
||||
│ │ ├── dmeval.rs
|
||||
│ │ ├── zmrho.rs
|
||||
│ │ ├── column.rs
|
||||
│ │ └── gridp.rs
|
||||
│ ├── hydrostatic/ # 流体静力平衡
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── hesolv.rs
|
||||
│ │ ├── hesol6.rs
|
||||
│ │ └── betah.rs
|
||||
│ ├── grey/ # 灰大气
|
||||
│ │ ├── mod.rs
|
||||
│ │ └── greyd.rs
|
||||
│ └── odf/ # ODF
|
||||
│ ├── mod.rs
|
||||
│ ├── odf1.rs
|
||||
│ ├── odffr.rs
|
||||
│ ├── ofhst.rs
|
||||
│ ├── odfhyd.rs
|
||||
│ ├── odfhys.rs
|
||||
│ └── odfmer.rs
|
||||
│
|
||||
├── spectral/ # [10 模块] 谱线处理
|
||||
│ ├── mod.rs
|
||||
│ ├── linpro.rs
|
||||
│ ├── linsel.rs
|
||||
│ ├── linspl.rs
|
||||
│ ├── linfrq.rs
|
||||
│ ├── linovr.rs
|
||||
│ ├── linfxd.rs
|
||||
│ ├── sigmar.rs
|
||||
│ ├── rossop.rs
|
||||
│ └── rosstd.rs
|
||||
│
|
||||
├── io/ # [10 模块] 输入输出
|
||||
│ ├── mod.rs
|
||||
│ ├── fortran/ # Fortran 格式
|
||||
│ │ ├── mod.rs
|
||||
│ │ ├── reader.rs
|
||||
│ │ └── writer.rs
|
||||
│ ├── output.rs
|
||||
│ ├── rdata.rs
|
||||
│ ├── rdatax.rs
|
||||
│ ├── readbf.rs
|
||||
│ ├── inkul.rs
|
||||
│ ├── chctab.rs
|
||||
│ └── timing.rs
|
||||
│
|
||||
├── model/ # [15 模块] 模型初始化
|
||||
│ ├── mod.rs
|
||||
│ ├── inilam.rs
|
||||
│ ├── inifrc.rs
|
||||
│ ├── inifrs.rs
|
||||
│ ├── inifrt.rs
|
||||
│ ├── inpdis.rs
|
||||
│ ├── visini.rs
|
||||
│ ├── change.rs
|
||||
│ ├── hedif.rs
|
||||
│ ├── dwnfr0.rs
|
||||
│ ├── dwnfr1.rs
|
||||
│ ├── dwnfr.rs
|
||||
│ ├── pzert.rs
|
||||
│ ├── corrwm.rs
|
||||
│ └── grcor.rs
|
||||
│
|
||||
├── utils/ # [5 模块] 通用工具
|
||||
│ ├── mod.rs
|
||||
│ ├── getwrd.rs
|
||||
│ ├── quit.rs
|
||||
│ ├── prchan.rs
|
||||
│ └── princ.rs
|
||||
│
|
||||
└── state/ # [现有] 状态结构
|
||||
└── ...
|
||||
@@ -0,0 +1,124 @@
|
||||
import os
|
||||
import re
|
||||
from collections import defaultdict
|
||||
|
||||
src_dir = "/home/fmq/program/SpectraRust/src"
|
||||
|
||||
# Regular expression to match function definitions
|
||||
# Matches: fn name(...) or pub fn name(...) or pub(crate) fn name(...)
|
||||
fn_pattern = re.compile(r'(?:pub\s+)?(?:pub\((?:crate|self|super)\)\s+)?fn\s+([a-zA-Z0-9_]+)\s*[\(<]')
|
||||
|
||||
# Matches struct definitions
|
||||
struct_pattern = re.compile(r'(?:pub\s+)?(?:pub\((?:crate|self|super)\)\s+)?struct\s+([a-zA-Z0-9_]+)\s*[\{<]?')
|
||||
|
||||
file_functions = defaultdict(list)
|
||||
fn_locations = defaultdict(list)
|
||||
struct_locations = defaultdict(list)
|
||||
file_basenames = defaultdict(list)
|
||||
|
||||
def normalize_code(code):
|
||||
# Remove comments and whitespace for comparison
|
||||
# Remove single line comments
|
||||
code = re.sub(r'//.*', '', code)
|
||||
# Remove multi-line comments
|
||||
code = re.sub(r'/\*.*?\*/', '', code, flags=re.DOTALL)
|
||||
# Normalize whitespace
|
||||
code = "".join(code.split())
|
||||
return code
|
||||
|
||||
def extract_function_body(content, start_pos):
|
||||
# Find the matching curly brace for the function body
|
||||
brace_count = 0
|
||||
in_body = False
|
||||
body_chars = []
|
||||
|
||||
# We look for the first '{' after start_pos
|
||||
first_brace = content.find('{', start_pos)
|
||||
if first_brace == -1:
|
||||
return ""
|
||||
|
||||
for i in range(first_brace, len(content)):
|
||||
char = content[i]
|
||||
if char == '{':
|
||||
brace_count += 1
|
||||
in_body = True
|
||||
elif char == '}':
|
||||
brace_count -= 1
|
||||
|
||||
if in_body:
|
||||
body_chars.append(char)
|
||||
if brace_count == 0:
|
||||
break
|
||||
|
||||
return "".join(body_chars)
|
||||
|
||||
# Walk directory
|
||||
for root, dirs, files in os.walk(src_dir):
|
||||
for file in files:
|
||||
if file.endswith(".rs") and file != "mod.rs" and file != "lib.rs":
|
||||
path = os.path.join(root, file)
|
||||
rel_path = os.path.relpath(path, src_dir)
|
||||
file_basenames[file].append(rel_path)
|
||||
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
|
||||
# Find all functions and extract bodies
|
||||
for match in fn_pattern.finditer(content):
|
||||
fn_name = match.group(1)
|
||||
if fn_name == "main" or fn_name.startswith("test_"):
|
||||
continue
|
||||
start_pos = match.end()
|
||||
body = extract_function_body(content, start_pos)
|
||||
normalized_body = normalize_code(body)
|
||||
fn_locations[fn_name].append({
|
||||
"path": rel_path,
|
||||
"body": normalized_body,
|
||||
"raw_body": body[:200] # snippet
|
||||
})
|
||||
file_functions[rel_path].append(fn_name)
|
||||
|
||||
# Find all structs
|
||||
for match in struct_pattern.finditer(content):
|
||||
struct_name = match.group(1)
|
||||
struct_locations[struct_name].append(rel_path)
|
||||
|
||||
print("=== 1. 重复的文件名 (Duplicate File Basenames) ===")
|
||||
dup_files = {k: v for k, v in file_basenames.items() if len(v) > 1}
|
||||
if dup_files:
|
||||
for filename, paths in sorted(dup_files.items()):
|
||||
print(f"文件名: {filename}")
|
||||
for p in paths:
|
||||
print(f" - src/{p}")
|
||||
else:
|
||||
print("没有重复的源文件名。")
|
||||
|
||||
print("\n=== 2. 重复的函数实现 (Duplicate Function Implementations) ===")
|
||||
dup_fns = {k: v for k, v in fn_locations.items() if len(v) > 1}
|
||||
if dup_fns:
|
||||
for fn_name, occurrences in sorted(dup_fns.items()):
|
||||
print(f"函数名: {fn_name}()")
|
||||
# Check if the implementations are identical
|
||||
identical = True
|
||||
first_body = occurrences[0]["body"]
|
||||
for occ in occurrences[1:]:
|
||||
if occ["body"] != first_body:
|
||||
identical = False
|
||||
break
|
||||
|
||||
status = "【完全相同】" if identical else "【有差异的实现】"
|
||||
print(f" 状态: {status}")
|
||||
for occ in occurrences:
|
||||
print(f" - src/{occ['path']}")
|
||||
else:
|
||||
print("没有发现重复的函数名。")
|
||||
|
||||
print("\n=== 3. 重复的 Struct 定义 (Duplicate Struct Definitions) ===")
|
||||
dup_structs = {k: v for k, v in struct_locations.items() if len(v) > 1}
|
||||
if dup_structs:
|
||||
for struct_name, paths in sorted(dup_structs.items()):
|
||||
print(f"结构体: struct {struct_name}")
|
||||
for p in paths:
|
||||
print(f" - src/{p}")
|
||||
else:
|
||||
print("没有发现重复的结构体名。")
|
||||
@@ -0,0 +1,123 @@
|
||||
import os
|
||||
import re
|
||||
from collections import defaultdict
|
||||
|
||||
src_dir = "/home/fmq/program/SpectraRust/src"
|
||||
output_file = "/home/fmq/program/SpectraRust/scratch/duplicate_results.txt"
|
||||
|
||||
fn_pattern = re.compile(r'(?:pub\s+)?(?:pub\((?:crate|self|super)\)\s+)?fn\s+([a-zA-Z0-9_]+)\s*[\(<]')
|
||||
struct_pattern = re.compile(r'(?:pub\s+)?(?:pub\((?:crate|self|super)\)\s+)?struct\s+([a-zA-Z0-9_]+)\s*[\{<]?')
|
||||
|
||||
file_functions = defaultdict(list)
|
||||
fn_locations = defaultdict(list)
|
||||
struct_locations = defaultdict(list)
|
||||
file_basenames = defaultdict(list)
|
||||
|
||||
# Common helper functions to filter out
|
||||
trivial_names = {
|
||||
"new", "parse", "read_f32_le", "read_f64_le", "read_i32_le", "new_full",
|
||||
"run_tlusty", "select_solver", "default", "build", "run", "get", "set",
|
||||
"read", "write", "print", "len", "is_empty", "clear", "as_str"
|
||||
}
|
||||
|
||||
def normalize_code(code):
|
||||
code = re.sub(r'//.*', '', code)
|
||||
code = re.sub(r'/\*.*?\*/', '', code, flags=re.DOTALL)
|
||||
code = "".join(code.split())
|
||||
return code
|
||||
|
||||
def extract_function_body(content, start_pos):
|
||||
brace_count = 0
|
||||
in_body = False
|
||||
body_chars = []
|
||||
|
||||
first_brace = content.find('{', start_pos)
|
||||
if first_brace == -1:
|
||||
return ""
|
||||
|
||||
for i in range(first_brace, len(content)):
|
||||
char = content[i]
|
||||
if char == '{':
|
||||
brace_count += 1
|
||||
in_body = True
|
||||
elif char == '}':
|
||||
brace_count -= 1
|
||||
|
||||
if in_body:
|
||||
body_chars.append(char)
|
||||
if brace_count == 0:
|
||||
break
|
||||
|
||||
return "".join(body_chars)
|
||||
|
||||
for root, dirs, files in os.walk(src_dir):
|
||||
for file in files:
|
||||
if file.endswith(".rs") and file != "mod.rs" and file != "lib.rs":
|
||||
path = os.path.join(root, file)
|
||||
rel_path = os.path.relpath(path, src_dir)
|
||||
file_basenames[file].append(rel_path)
|
||||
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
|
||||
for match in fn_pattern.finditer(content):
|
||||
fn_name = match.group(1)
|
||||
if fn_name in trivial_names or fn_name.startswith("test_"):
|
||||
continue
|
||||
start_pos = match.end()
|
||||
body = extract_function_body(content, start_pos)
|
||||
normalized_body = normalize_code(body)
|
||||
fn_locations[fn_name].append({
|
||||
"path": rel_path,
|
||||
"body": normalized_body
|
||||
})
|
||||
file_functions[rel_path].append(fn_name)
|
||||
|
||||
for match in struct_pattern.finditer(content):
|
||||
struct_name = match.group(1)
|
||||
if struct_name in trivial_names:
|
||||
continue
|
||||
struct_locations[struct_name].append(rel_path)
|
||||
|
||||
with open(output_file, "w", encoding="utf-8") as out:
|
||||
out.write("=== 1. 重复的文件名 (Duplicate File Basenames) ===\n")
|
||||
dup_files = {k: v for k, v in file_basenames.items() if len(v) > 1}
|
||||
if dup_files:
|
||||
for filename, paths in sorted(dup_files.items()):
|
||||
out.write(f"文件名: {filename}\n")
|
||||
for p in paths:
|
||||
out.write(f" - src/{p}\n")
|
||||
else:
|
||||
out.write("没有重复的源文件名。\n")
|
||||
|
||||
out.write("\n=== 2. 重复的数学/物理函数实现 (Duplicate Physics/Math Functions) ===\n")
|
||||
dup_fns = {k: v for k, v in fn_locations.items() if len(v) > 1}
|
||||
if dup_fns:
|
||||
for fn_name, occurrences in sorted(dup_fns.items()):
|
||||
# Check if the implementations are identical
|
||||
identical = True
|
||||
first_body = occurrences[0]["body"]
|
||||
for occ in occurrences[1:]:
|
||||
if occ["body"] != first_body:
|
||||
identical = False
|
||||
break
|
||||
|
||||
status = "【代码完全相同】" if identical else "【代码不同(有差异的实现)】"
|
||||
out.write(f"函数名: {fn_name}()\n")
|
||||
out.write(f" 状态: {status}\n")
|
||||
for occ in occurrences:
|
||||
out.write(f" - src/{occ['path']}\n")
|
||||
else:
|
||||
out.write("没有发现重复的物理/数学函数。\n")
|
||||
|
||||
out.write("\n=== 3. 重复的 Struct 定义 (Duplicate Struct Definitions) ===\n")
|
||||
dup_structs = {k: v for k, v in struct_locations.items() if len(v) > 1}
|
||||
if dup_structs:
|
||||
for struct_name, paths in sorted(dup_structs.items()):
|
||||
out.write(f"结构体: struct {struct_name}\n")
|
||||
for p in paths:
|
||||
out.write(f" - src/{p}\n")
|
||||
else:
|
||||
out.write("没有发现重复的结构体。\n")
|
||||
|
||||
print("分析完成,结果已写入:", output_file)
|
||||
@@ -0,0 +1,25 @@
|
||||
#!/bin/bash
|
||||
# 批量迁移 acceleration 模块
|
||||
cd src/tlusty/math
|
||||
|
||||
# 创建目录
|
||||
mkdir -p acceleration/ali
|
||||
mkdir -p acceleration/convergence
|
||||
# ali
|
||||
mv alifr1.rs acceleration/ali
|
||||
mv alifr3.rs acceleration/ali
|
||||
mv alifr6.rs acceleration/ali
|
||||
mv alifrk.rs acceleration/ali
|
||||
mv alisk1.rs acceleration/ali
|
||||
mv alisk2.rs acceleration/ali
|
||||
mv alist1.rs acceleration/ali
|
||||
mv alist2.rs acceleration/ali
|
||||
mv ijali2.rs acceleration/ali
|
||||
mv ijalis.rs acceleration/ali
|
||||
mv getlal.rs acceleration/ali
|
||||
mv taufr1.rs acceleration/ali
|
||||
# convergence
|
||||
mv accel2.rs acceleration/convergence
|
||||
mv accelp.rs acceleration/convergence
|
||||
mv osccor.rs acceleration/convergence
|
||||
echo "Done"
|
||||
@@ -0,0 +1,46 @@
|
||||
#!/bin/bash
|
||||
# 批量迁移 atmosphere 模块
|
||||
cd src/tlusty/math
|
||||
|
||||
# 创建目录
|
||||
mkdir -p atmosphere/convection
|
||||
mkdir -p atmosphere/temperature
|
||||
mkdir -p atmosphere/depth
|
||||
mkdir -p atmosphere/hydrostatic
|
||||
mkdir -p atmosphere/grey
|
||||
mkdir -p atmosphere/odf
|
||||
# convection
|
||||
mv convec.rs atmosphere/convection
|
||||
mv concor.rs atmosphere/convection
|
||||
mv conout.rs atmosphere/convection
|
||||
mv conref.rs atmosphere/convection
|
||||
mv contmd.rs atmosphere/convection
|
||||
mv contmp.rs atmosphere/convection
|
||||
# temperature
|
||||
mv temper.rs atmosphere/temperature
|
||||
mv temcor.rs atmosphere/temperature
|
||||
mv tlocal.rs atmosphere/temperature
|
||||
mv lucy.rs atmosphere/temperature
|
||||
mv tdpini.rs atmosphere/temperature
|
||||
# depth
|
||||
mv newdm.rs atmosphere/depth
|
||||
mv newdmt.rs atmosphere/depth
|
||||
mv dmder.rs atmosphere/depth
|
||||
mv dmeval.rs atmosphere/depth
|
||||
mv zmrho.rs atmosphere/depth
|
||||
mv column.rs atmosphere/depth
|
||||
mv gridp.rs atmosphere/depth
|
||||
# hydrostatic
|
||||
mv hesolv.rs atmosphere/hydrostatic
|
||||
mv hesol6.rs atmosphere/hydrostatic
|
||||
mv betah.rs atmosphere/hydrostatic
|
||||
# grey
|
||||
mv greyd.rs atmosphere/grey
|
||||
# odf
|
||||
mv odf1.rs atmosphere/odf
|
||||
mv odffr.rs atmosphere/odf
|
||||
mv odfhst.rs atmosphere/odf
|
||||
mv odfhyd.rs atmosphere/odf
|
||||
mv odfhys.rs atmosphere/odf
|
||||
mv odfmer.rs atmosphere/odf
|
||||
echo "Done"
|
||||
@@ -0,0 +1,35 @@
|
||||
#!/bin/bash
|
||||
# 批量迁移 equilibrium 模块
|
||||
cd src/tlusty/math
|
||||
|
||||
# 创建目录
|
||||
mkdir -p equilibrium/statistical
|
||||
mkdir -p equilibrium/ionization
|
||||
mkdir -p equilibrium/partition
|
||||
mkdir -p equilibrium/level
|
||||
|
||||
# statistical
|
||||
mv rates1.rs equilibrium/statistical
|
||||
mv ratmat.rs equilibrium/statistical
|
||||
mv ratmal.rs equilibrium/statistical
|
||||
mv ratsp1.rs equilibrium/statistical
|
||||
mv steqeq.rs equilibrium/statistical
|
||||
mv reflev.rs equilibrium/statistical
|
||||
mv sabolf.rs equilibrium/statistical
|
||||
mv newpop.rs equilibrium/statistical
|
||||
# ionization
|
||||
mv russel.rs equilibrium/ionization
|
||||
mv moleq.rs equilibrium/ionization
|
||||
# partition
|
||||
mv partf.rs equilibrium/partition
|
||||
mv mpartf.rs equilibrium/partition
|
||||
mv pfcno.rs equilibrium/partition
|
||||
mv pffe.rs equilibrium/partition
|
||||
mv pfheav.rs equilibrium/partition
|
||||
mv pfni.rs equilibrium/partition
|
||||
mv pfspec.rs equilibrium/partition
|
||||
mv tiopf.rs equilibrium/partition
|
||||
# level
|
||||
mv levset.rs equilibrium/level
|
||||
mv levgrp.rs equilibrium/level
|
||||
echo "Done"
|
||||
@@ -0,0 +1,11 @@
|
||||
#!/bin/bash
|
||||
# 批量迁移 hydrogen 模块
|
||||
cd src/tlusty/math
|
||||
|
||||
# 创建目录
|
||||
mkdir -p physics/hydrogen
|
||||
|
||||
# 移动文件
|
||||
mv wn.rs physics/hydrogen
|
||||
mv wnstor.rs physics/hydrogen
|
||||
echo "Done"
|
||||
@@ -0,0 +1,23 @@
|
||||
#!/bin/bash
|
||||
# 批量迁移 io 和 misc 模块
|
||||
cd src/tlusty/math
|
||||
|
||||
# 创建目录
|
||||
mkdir -p io
|
||||
mkdir -p utils
|
||||
|
||||
# io
|
||||
mv output.rs io
|
||||
mv rdata.rs io
|
||||
mv rdatax.rs io
|
||||
mv readbf.rs io
|
||||
mv inkul.rs io
|
||||
mv timing.rs io
|
||||
mv getwrd.rs io
|
||||
mv prchan.rs io
|
||||
mv princ.rs io
|
||||
mv prnt.rs io
|
||||
# utils
|
||||
mv quit.rs io
|
||||
mv getwrd.rs utils
|
||||
echo "Done"
|
||||
@@ -0,0 +1,36 @@
|
||||
#!/bin/bash
|
||||
# 批量迁移 linearization 模块
|
||||
cd src/tlusty/math
|
||||
|
||||
# 创建目录
|
||||
mkdir -p linearization/matrix
|
||||
mkdir -p linearization/solver
|
||||
mkdir -p linearization/rybicki
|
||||
# matrix
|
||||
mv bhe.rs linearization/matrix
|
||||
mv bre.rs linearization/matrix
|
||||
mv brez.rs linearization/matrix
|
||||
mv bpop.rs linearization/matrix
|
||||
mv bpopc.rs linearization/matrix
|
||||
mv bpope.rs linearization/matrix
|
||||
mv bpopf.rs linearization/matrix
|
||||
mv bpopt.rs linearization/matrix
|
||||
mv emat.rs linearization/matrix
|
||||
mv matcon.rs linearization/matrix
|
||||
mv matgen.rs linearization/matrix
|
||||
mv matinv.rs linearization/matrix
|
||||
mv rhsgen.rs linearization/matrix
|
||||
# solver
|
||||
mv solve.rs linearization/solver
|
||||
mv solves.rs linearization/solver
|
||||
mv levsol.rs linearization/solver
|
||||
mv lineqs.rs linearization/solver
|
||||
mv minv3.rs linearization/solver
|
||||
mv psolve.rs linearization/solver
|
||||
# rybicki
|
||||
mv rybmat.rs linearization/rybicki
|
||||
mv rybheq.rs linearization/rybicki
|
||||
mv rybene.rs linearization/rybicki
|
||||
mv rybchn.rs linearization/rybicki
|
||||
mv rybsol.rs linearization/rybicki
|
||||
echo "Done"
|
||||
@@ -0,0 +1,19 @@
|
||||
#!//bash
|
||||
# Math special functions
|
||||
cd src/tlusty/math && mv expo.rs math/special/
|
||||
mv expint.rs math/special
|
||||
mv erfcx.rs math/special
|
||||
mv gauleg.rs math/special
|
||||
mv expinx.rs math/special
|
||||
mv ubeta.rs math/utils
|
||||
mv lagran.rs math/interpolate
|
||||
mv laguer.rs math/utils
|
||||
mv yint.rs math/interpolate
|
||||
mv ylintp.rs math/interpolate
|
||||
mv tabint.rs math/interpolate
|
||||
mv locate.rs math/interpolate
|
||||
mv indexx.rs math/utils
|
||||
mv gauleg.rs math/special
|
||||
mv ubeta.rs math/utils
|
||||
|
||||
echo "Created math subdirectories and moved basic math files"
|
||||
@@ -0,0 +1,24 @@
|
||||
#!/bin/bash
|
||||
# 批量迁移 model 模块
|
||||
cd src/tlusty/math
|
||||
|
||||
# 创建目录
|
||||
mkdir -p model
|
||||
|
||||
# 移动文件
|
||||
mv inilam.rs model
|
||||
mv inifrc.rs model
|
||||
mv inifrs.rs model
|
||||
mv inifrt.rs model
|
||||
mv inpdis.rs model
|
||||
mv change.rs model
|
||||
mv hedif.rs model
|
||||
mv dwnfr.rs model
|
||||
mv dwnfr0.rs model
|
||||
mv dwnfr1.rs model
|
||||
mv chctab.rs model
|
||||
mv levset.rs model
|
||||
mv levgrp.rs model
|
||||
mv visini.rs model
|
||||
mv grcor.rs model
|
||||
echo "Done"
|
||||
@@ -0,0 +1,17 @@
|
||||
#!/bin/bash
|
||||
# 批量迁移剩余模块
|
||||
cd src/tlusty/math
|
||||
|
||||
# 创建目录
|
||||
mkdir -p physics/radiative/flux
|
||||
mkdir -p physics/opacity
|
||||
mkdir -p physics/opacity
|
||||
mv brte.rs physics/radiative
|
||||
mv brtez.rs physics/radiative
|
||||
mv pzeval.rs physics/radiative
|
||||
mv pzevld.rs physics/radiative
|
||||
mv prdin.rs physics/radiative
|
||||
mv prdini.rs physics/radiative
|
||||
mv taufr1.rs acceleration/ali
|
||||
mv raph.rs model
|
||||
echo "Done"
|
||||
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
# Batch迁移 physics/opacity 模块
|
||||
cd src/tlusty/math && mv opacf0.rs physics/opacity && mv opacf1.rs physics/opacity && mv opacfa.rs physics/opacity && mv opacfd.rs physics/opacity && mv opacfl.rs physics/opacity && mv opadd.rs physics/opacity && mv opadd0.rs physics/opacity && mv opahst.rs physics/opacity && mv opaini.rs physics/opacity && mv opctab.rs physics/opacity && mv opdata.rs physics/opacity && mv opfrac.rs physics/opacity && mv traini.rs physics/opacity
|
||||
mv meanop.rs physics/opacity && mv meanopt.rs physics/opacity
|
||||
mv opact1.rs physics/opacity
|
||||
mv opactd.rs physics/opacity
|
||||
mv opactr.rs physics/opacity
|
||||
@@ -0,0 +1,47 @@
|
||||
#!/bin/bash
|
||||
# 批量迁移 physics/collision 模块
|
||||
|
||||
cd src/tlusty/math
|
||||
|
||||
# 创建目录
|
||||
mkdir -p physics/collision/rates
|
||||
mkdir -p physics/collision/ionization
|
||||
mkdir -p physics/collision/dielectronic
|
||||
mkdir -p physics/collision/charge_transfer
|
||||
|
||||
mkdir -p physics/collision/broadening
|
||||
|
||||
mkdir -p physics/collision/hydrogen
|
||||
|
||||
# rates
|
||||
mv colh.rs physics/collision/rates
|
||||
mv colhe.rs physics/collision/rates
|
||||
mv colis.rs physics/collision/rates
|
||||
mv collhe.rs physics/collision/rates
|
||||
mv butler.rs physics/collision/rates
|
||||
mv ceh12.rs physics/collision/rates
|
||||
mv cheav.rs physics/collision/rates
|
||||
mv cheavj.rs physics/collision/rates
|
||||
mv cspec.rs physics/collision/rates
|
||||
|
||||
mv sghe12.rs physics/collision/hydrogen
|
||||
mv sgmer.rs physics/collision/hydrogen
|
||||
mv sgmer1.rs physics/collision/hydrogen
|
||||
|
||||
# ionization
|
||||
mv cion.rs physics/collision/ionization
|
||||
mv irc.rs physics/collision/ionization
|
||||
mv szirc.rs physics/collision/ionization
|
||||
# dielectronic
|
||||
mv dielrc.rs physics/collision/dielectronic
|
||||
mv dietot.rs physics/collision/dielectronic
|
||||
# charge_transfer
|
||||
mv ctdata.rs physics/collision/charge_transfer
|
||||
# broadening
|
||||
mv gami.rs physics/collision/broadening
|
||||
mv gamsp.rs physics/collision/broadening
|
||||
mv gvdw.rs physics/collision/broadening
|
||||
mv dopgam.rs physics/collision/broadening
|
||||
mv switch.rs physics/collision/broadening
|
||||
|
||||
echo "Done"
|
||||
@@ -0,0 +1,529 @@
|
||||
#!/bin/bash
|
||||
# 批迁移 physics/cross_section 模块
|
||||
cd src/tlusty/math && mv cross.rs physics/cross_section/photoion
|
||||
mv verner.rs physics/cross_section/photoion
|
||||
mv vern16.rs physics/cross_section/photoion && mv vern18.rs physics/cross_section/photoion
|
||||
mv vern20.rs physics/cross_section/photoion
|
||||
mv vern26.rs physics/cross_section/photoion && mv topbas.rs physics/cross_section/photoion && mv sigk.rs physics/cross_section/photoion && mv sigave.rs physics/cross_section/photoion && mv bkhsgo.rs physics/cross_section/photoion && mv hidalg.rs physics/cross_section/photoion && mv reiman.rs physics/cross_section/photoion && mv hephot.rs physics/cross_section/photoion && mv ckoest.rs physics/cross_section/photoion && mv carbon.rs physics/cross_section/photoion
|
||||
mv sbfch.rs physics/cross_section/bound_free
|
||||
mv sbfhe1.rs physics/cross_section/bound_free
|
||||
mv sbfhmi.rs physics/cross_section/bound_free
|
||||
mv sbfhmi_old.rs physics/cross_section/bound_free
|
||||
mv sbfoh.rs physics/cross_section/bound_free
|
||||
mv ffcros.rs physics/cross_section/free_free
|
||||
mv sffhmi.rs physics/cross_section/free_free
|
||||
mv sffhmi_add.rs physics/cross_section/free_free
|
||||
mv h2minus.rs physics/cross_section/free_free
|
||||
mv cia_h2h.rs physics/cross_section/cia
|
||||
mv cia_h2h2.rs physics/cross_section/cia
|
||||
mv cia_h2he.rs physics/cross_section/cia
|
||||
mv cia_hhe.rs physics/cross_section/cia
|
||||
mv rayleigh.rs physics/cross_section/rayleigh && mv rayset.rs physics/cross_section/rayleigh
|
||||
mv gaunt.rs physics/cross_section/gaunt
|
||||
mv gfree.rs physics/cross_section/gaunt
|
||||
mv gntk.rs physics/cross_section/gaunt
|
||||
mv ghydop.rs physics/cross_section/hydrogen
|
||||
mv xk2dop.rs physics/cross_section/hydrogen
|
||||
mv intxen.rs physics/cross_section/hydrogen
|
||||
mv intlem.rs physics/cross_section/hydrogen/ mv intxen.rs physics/cross_section/hydrogen
|
||||
mv gomini.rs physics/cross_section/hydrogen
|
||||
mv lemini.rs physics/cross_section/hydrogen
|
||||
mv inthyd.rs physics/cross_section/stark
|
||||
mv starka.rs physics/cross_section/stark
|
||||
mv divstr.rs physics/cross_section/stark
|
||||
mv dopgam.rs physics/cross_section/broadening
|
||||
mv gami.rs physics/cross_section/broadening
|
||||
mv gamsp.rs physics/cross_section/broadening
|
||||
mv gvdw.rs physics/cross_section/broadening
|
||||
mv lymlin.rs physics/cross_section/hydrogen
|
||||
mv sghe12.rs physics/cross_section/hydrogen
|
||||
mv sgmer.rs physics/cross_section/hydrogen
|
||||
mv sgmer1.rs physics/cross_section/hydrogen
|
||||
mv sigmar.rs physics/cross_section/spectral
|
||||
mv sigave.rs physics/cross_section/spectral
|
||||
mv rossop.rs physics/cross_section/spectral
|
||||
mv rosstd.rs physics/cross_section/spectral
|
||||
mv radpre.rs physics/cross_section/radiative
|
||||
mv radtot.rs physics/cross_section/radiative
|
||||
mv rechck.rs physics/cross_section/radiative
|
||||
mv russel.rs physics/cross_section/equilibrium
|
||||
mv moleq.rs physics/cross_section/equilibrium
|
||||
mv rhonen.rs physics/cross_section/equilibrium
|
||||
mv rhoeos.rs physics/cross_section/equilibrium
|
||||
mv state.rs physics/cross_section/equilibrium
|
||||
mv sigmar.rs physics/cross_section/spectral
|
||||
mv sigave.rs physics/cross_section/spectral
|
||||
mv sigk.rs physics/cross_section/spectral
|
||||
mv sigave.rs physics/cross_section/spectral
|
||||
mv sigave.rs physics/cross_section/spectral
|
||||
mv sbfch.rs physics/cross_section/bound_free
|
||||
mv sbfhe1.rs physics/cross_section/bound_free
|
||||
mv sbfhmi.rs physics/cross_section/bound_free
|
||||
mv sbfhmi_old.rs physics/cross_section/bound_free
|
||||
mv sbfoh.rs physics/cross_section/bound_free
|
||||
mv ffcros.rs physics/cross_section/free_free
|
||||
mv sffhmi.rs physics/cross_section/free_free
|
||||
mv sffhmi_add.rs physics/cross_section/free_free
|
||||
mv h2minus.rs physics/cross_section/free_free
|
||||
mv cia_h2h.rs physics/cross_section/cia
|
||||
mv cia_h2h2.rs physics/cross_section/cia
|
||||
mv cia_hhe.rs physics/cross_section/cia
|
||||
mv cia_hhe.rs physics/cross_section/cia
|
||||
mv rayleigh.rs physics/cross_section/rayleigh && mv rayset.rs physics/cross_section/rayleigh
|
||||
mv gaunt.rs physics/cross_section/gaunt
|
||||
mv gfree.rs physics/cross_section/gaunt
|
||||
mv gntk.rs physics/cross_section/gaunt
|
||||
mv ghydop.rs physics/cross_section/hydrogen
|
||||
mv xk2dop.rs physics/cross_section/hydrogen
|
||||
mv intxen.rs physics/cross_section/hydrogen
|
||||
mv intlem.rs physics/cross_section/hydrogen
|
||||
mv lemini.rs physics/cross_section/hydrogen
|
||||
mv inthyd.rs physics/cross_section/stark
|
||||
mv starka.rs physics/cross_section/stark
|
||||
mv divstr.rs physics/cross_section/stark
|
||||
mv dopgam.rs physics/cross_section/broadening
|
||||
mv gami.rs physics/cross_section/broadening
|
||||
mv gamsp.rs physics/cross_section/broadening
|
||||
mv gvdw.rs physics/cross_section/broadening
|
||||
mv lymlin.rs physics/cross_section/hydrogen
|
||||
mv sghe12.rs physics/cross_section/hydrogen
|
||||
mv sgmer.rs physics/cross_section/hydrogen
|
||||
mv sgmer1.rs physics/cross_section/hydrogen
|
||||
mv sigmar.rs physics/cross_section/spectral
|
||||
mv sigave.rs physics/cross_section/spectral
|
||||
mv rossop.rs physics/cross_section/spectral
|
||||
mv rosstd.rs physics/cross_section/spectral
|
||||
mv radpre.rs physics/cross_section/radiative
|
||||
mv radtot.rs physics/cross_section/radiative
|
||||
mv rechck.rs physics/cross_section/radiative
|
||||
mv russel.rs physics/cross_section/equilibrium
|
||||
mv moleq.rs physics/cross_section/equilibrium
|
||||
mv rhonen.rs physics/cross_section/equilibrium
|
||||
mv rhoeos.rs physics/cross_section/equilibrium
|
||||
mv state.rs physics/cross_section/equilibrium
|
||||
mv sigmar.rs physics/cross_section/spectral
|
||||
mv sigave.rs physics/cross_section/spectral
|
||||
mv sigk.rs physics/cross_section/spectral
|
||||
mv sigave.rs physics/cross_section/spectral
|
||||
mv sbfch.rs physics/cross_section/bound_free
|
||||
mv sbfhe1.rs physics/cross_section/bound_free
|
||||
mv sbfhmi.rs physics/cross_section/bound_free
|
||||
mv sbfhmi_old.rs physics/cross_section/bound_free
|
||||
mv sbfoh.rs physics/cross_section/bound_free
|
||||
mv ffcros.rs physics/cross_section/free_free
|
||||
mv sffhmi.rs physics/cross_section/free_free
|
||||
mv sffhmi_add.rs physics/cross_section/free_free
|
||||
mv h2minus.rs physics/cross_section/free_free
|
||||
mv cia_h2h.rs physics/cross_section/cia
|
||||
mv cia_h2h2.rs physics/cross_section/cia
|
||||
mv cia_hhe.rs physics/cross_section/cia
|
||||
mv cia_hhe.rs physics/cross_section/cia
|
||||
mv rayleigh.rs physics/cross_section/rayleigh && mv rayset.rs physics/cross_section/rayleigh
|
||||
mv gaunt.rs physics/cross_section/gaunt
|
||||
mv gfree.rs physics/cross_section/gaunt
|
||||
mv gntk.rs physics/cross_section/gaunt
|
||||
mv ghydop.rs physics/cross_section/hydrogen
|
||||
mv xk2dop.rs physics/cross_section/hydrogen
|
||||
mv intxen.rs physics/cross_section/hydrogen
|
||||
mv intlem.rs physics/cross_section/hydrogen
|
||||
mv lemini.rs physics/cross_section/hydrogen
|
||||
mv inthyd.rs physics/cross_section/stark
|
||||
mv starka.rs physics/cross_section/stark
|
||||
mv divstr.rs physics/cross_section/stark
|
||||
mv dopgam.rs physics/cross_section/broadening
|
||||
mv gami.rs physics/cross_section/broadening
|
||||
mv gamsp.rs physics/cross_section/broadening
|
||||
mv gvdw.rs physics/cross_section/broadening
|
||||
mv lymlin.rs physics/cross_section/hydrogen
|
||||
mv sghe12.rs physics/cross_section/hydrogen
|
||||
mv sgmer.rs physics/cross_section/hydrogen
|
||||
mv sgmer1.rs physics/cross_section/hydrogen
|
||||
mv sigmar.rs physics/cross_section/spectral
|
||||
mv sigave.rs physics/cross_section/spectral
|
||||
mv rossop.rs physics/cross_section/spectral
|
||||
mv rosstd.rs physics/cross_section/spectral
|
||||
mv radpre.rs physics/cross_section/radiative
|
||||
mv radtot.rs physics/c交叉截面 photoion
|
||||
mv radtot.rs physics/cross_section/radiative
|
||||
mv rechck.rs physics/cross_section/radiative
|
||||
mv russel.rs physics/cross_section/equilibrium
|
||||
mv moleq.rs physics/cross_section/equilibrium
|
||||
mv rhonen.rs physics/cross_section/equilibrium
|
||||
mv rhoeos.rs physics/cross_section/equilibrium
|
||||
mv state.rs physics/cross_section/equilibrium
|
||||
mv sigmar.rs physics/cross_section/spectral
|
||||
mv sigave.rs physics/cross_section/spectral
|
||||
mv sigk.rs physics/cross_section/spectral
|
||||
mv sigave.rs physics/cross_section/spectral
|
||||
mv sbfch.rs physics/cross_section/bound_free
|
||||
mv sbfhe1.rs physics/cross_section/bound_free
|
||||
mv sbfhmi.rs physics/cross_section/bound_free
|
||||
mv sbfhmi_old.rs physics/cross_section/bound_free
|
||||
mv sbfoh.rs physics/cross_section/bound_free
|
||||
mv ffcros.rs physics/cross_section/free_free
|
||||
mv sffhmi.rs physics/cross_section/free_free
|
||||
mv sffhmi_add.rs physics/cross_section/free_free
|
||||
mv h2minus.rs physics/cross_section/free_free
|
||||
mv cia_h2h.rs physics/cross_section/cia
|
||||
mv cia_h2h2.rs physics/cross_section/cia
|
||||
mv cia_hhe.rs physics/cross_section/cia
|
||||
mv cia_hhe.rs physics/cross_section.cia
|
||||
mv rayleigh.rs physics/cross_section/rayleigh && mv rayset.rs physics/cross_section/rayleigh
|
||||
mv gaunt.rs physics/cross_section/gaunt
|
||||
mv gfree.rs physics/cross_section/gaunt
|
||||
mv gntk.rs physics/cross_section/gaunt
|
||||
mv ghydop.rs physics/cross_section/hydrogen
|
||||
mv xk2dop.rs physics/cross_section/hydrogen
|
||||
mv intxen.rs physics/cross_section/hydrogen
|
||||
mv intlem.rs physics/cross_section/hydrogen
|
||||
mv lemini.rs physics/cross_section/hydrogen
|
||||
mv inthyd.rs physics/cross_section/stark
|
||||
mv starka.rs physics/cross_section/stark
|
||||
mv divstr.rs physics/cross_section/stark
|
||||
mv dopgam.rs physics/cross_section/broadening
|
||||
mv gami.rs physics/cross_section/broadening
|
||||
mv gamsp.rs physics/cross_section/broadening
|
||||
mv gvdw.rs physics/cross_section/broadening
|
||||
mv lymlin.rs physics/cross_section/hydrogen
|
||||
mv sghe12.rs physics/cross_section/hydrogen
|
||||
mv sgmer.rs physics/cross_section/hydrogen
|
||||
mv sgmer1.rs physics/cross_section/hydrogen
|
||||
mv sigmar.rs physics/cross_section/spectral
|
||||
mv sigave.rs physics/cross_section/spectral
|
||||
mv rossop.rs physics/cross_section/spectral
|
||||
mv rosstd.rs physics/cross_section/spectral
|
||||
mv radpre.rs physics/cross_section/radiative
|
||||
mv radtot.rs physics/cross_section/radiative
|
||||
mv rechck.rs physics/cross_section/radiative
|
||||
mv russel.rs physics/cross_section/equilibrium
|
||||
mv moleq.rs physics/cross_section/equilibrium
|
||||
mv rhonen.rs physics/cross_section/equilibrium
|
||||
mv rhoeos.rs physics/cross_section:equilibrium
|
||||
mv state.rs physics/cross_section/equilibrium
|
||||
mv sigmar.rs physics/cross_section/spectral
|
||||
mv sigave.rs physics/cross_section.spectral
|
||||
mv sigk.rs physics/cross_section:spectral
|
||||
mv sigave.rs physics/cross_section/spectral
|
||||
mv sbfch.rs physics/cross_section/bound_free/ mv sbfhe1.rs physics/cross_section/bound_free
|
||||
mv sbfhmi.rs physics/cross_section/bound_free
|
||||
mv sbfhmi_old.rs physics/cross_section/bound_free
|
||||
mv sbfoh.rs physics/cross_section/bound_free
|
||||
mv ffcros.rs physics/cross_section/free_free
|
||||
mv sffhmi.rs physics/cross_section/free_free
|
||||
mv sffhmi_add.rs physics/cross_section/free_free
|
||||
mv h2minus.rs physics/cross_section/free_free
|
||||
mv cia_h2h.rs physics/cross_section/cia
|
||||
mv cia_h2h2.rs physics/cross_section/cia
|
||||
mv cia_hhe.rs physics/cross_section/cia)
|
||||
mv rayleigh.rs physics/cross_section/rayleigh && mv rayset.rs physics/cross_section/rayleigh
|
||||
mv gaunt.rs physics/cross_section/gaunt
|
||||
mv gfree.rs physics/cross_section/gaunt
|
||||
mv gntk.rs physics/cross_section/gaunt
|
||||
mv ghydop.rs physics/cross_section/hydrogen
|
||||
mv xk2dop.rs physics/cross_section/hydrogen
|
||||
mv intxen.rs physics/cross_section/hydrogen
|
||||
mv intlem.rs physics/cross_section/hydrogen
|
||||
mv lemini.rs physics/cross_section/hydrogen)
|
||||
mv inthyd.rs physics/cross_section/stark
|
||||
mv starka.rs physics/cross_section/stark
|
||||
mv divstr.rs physics/cross_section/stark
|
||||
mv dopgam.rs physics/cross_section/broadening
|
||||
mv gami.rs physics/cross_section/broadening
|
||||
mv gamsp.rs physics/cross_section/broadening/ mv gvdw.rs physics/craw section_broadening
|
||||
mv lymlin.rs physics/cross_section/hydrogen
|
||||
mv sghe12.rs physics/cross_section/hydrogen
|
||||
mv sgmer.rs physics/cross_section/hydrogen
|
||||
mv sgmer1.rs physics/cross_section/hydrogen) mv sigmar.rs physics/cross_section/spectral
|
||||
mv sigave.rs physics/cross_section/spectral) mv rossop.rs physics/cross_section/spectral) mv rosstd.rs physics/cross_section/spectral) mv radpre.rs physics/cross_section/radiative) mv radtot.rs physics/cross_section/radiative) mv rechck.rs physics/cross_section/radiative) mv russel.rs physics/cross_section/equilibrium) mv moleq.rs physics/cross_section/equilibrium) mv rhonen.rs physics/cross_section/equilibrium) mv rhoeos.rs physics/cross_section/equilibrium) mv state.rs physics/cross_section/equilibrium) mv sigmar.rs physics/cross_section/spectral) mv sigave.rs physics/cross_section/spectral) mv sigk.rs physics/cross_section/spectral) mv sigave.rs physics/cross_section/spectral) mv sbfch.rs physics/cross_section/bound_free/ mv sbfhe1.rs physics/cross_section/bound_free/ mv sbfhmi.rs physics/cross_section/bound_free/ mv sbfhmi_old.rs physics/c跨截面 ( bound_free) (旧版本)
|
||||
已移动, mv sbfoh.rs physics/cross_section/bound_free/ mv ffcros.rs physics/cross_section/free_free
|
||||
mv sffhmi.rs physics/cross_section/free_free
|
||||
mv sffhmi_add.rs physics/cross_section/free_free
|
||||
mv h2minus.rs physics/cross_section/free_free
|
||||
mv cia_h2h.rs physics/cross_section/cia)
|
||||
mv cia_h2h2.rs physics/cross_section/cia)
|
||||
mv cia_hhe.rs physics/cross_section/cia) mv rayleigh.rs physics/cross_section/rayleigh && mv rayset.rs physics/cross_section/rayleigh
|
||||
mv gaunt.rs physics/cross_section/gaunt
|
||||
mv gfree.rs physics/cross_section/gaunt) mv gntk.rs physics/cross_section/gaunt) mv ghydop.rs physics/cross_section/hydrogen
|
||||
mv xk2dop.rs physics/cross_section/hydrogen) mv intxen.rs physics/cross_section/hydrogen) mv intlem.rs physics/cross_section/hydrogen) mv lemini.rs physics/cross_section/hydrogen) mv inthyd.rs physics/cross_section/stark) mv starka.rs physics/cross_section/stark) mv divstr.rs physics/cross_section/stark) mv dopgam.rs physics/cross_section/broadening ( mv gami.rs physics/cross_section/broadening ( mv gamsp.rs physics/cross_section/broadening) mv gvdw.rs physics/cross_section/broadening ( mv lymlin.rs physics/cross_section/hydrogen) mv sghe12.rs physics/cross_section/hydrogen) mv sgmer.rs physics/cross_section/hydrogen) mv sgmer1.rs physics/cross_section/hydrogen) mv sigmar.rs physics/cross_section/spectral) mv sigave.rs physics/cross_section/spectral) mv rossop.rs physics/cross_section/spectral) mv rosstd.rs physics/cross_section/spectral) mv radpre.rs physics/cross_section/radiative) mv radtot.rs physics/cross_section/radiative) mv rechck.rs physics/cross_section/radiative) mv russel.rs physics/cross_section/equilibrium) mv moleq.rs physics/cross_section/equilibrium) mv rhonen.rs physics/cross_section/equilibrium) mv rhoeos.rs physics/cross_section/equilibrium) mv state.rs physics/cross_section/equilibrium) mv sigmar.rs physics/cross_section/spectral) mv sigave.rs physics/cross_section/spectral) mv sigk.rs physics/cross_section/spectral) mv sigave.rs physics/cross_section/spectral
|
||||
mv sbfch.rs physics/cross_section/bound_free/ mv sbfhe1.rs physics/cross_section/bound_free/ mv sbfhmi.rs physics/cross_section/bound_free/ mv sbfhmi_old.rs physics/cross_section/bound_free (旧版本)
|
||||
mv sbfoh.rs physics/cross_section/bound_free/ mv ffcros.rs physics/cross_section/free_free
|
||||
mv sffhmi.rs physics/cross_section/free_free
|
||||
mv sffhmi_add.rs physics/cross_section/free_free
|
||||
mv h2minus.rs physics/cross_section/free_free/mv cia_h2h.rs physics/cross_section/cia
|
||||
mv cia_h2h2.rs physics/cross_section/cia)
|
||||
mv cia_hhe.rs physics/cross_section/cia)
|
||||
mv rayleigh.rs physics/cross_section/rayleigh && mv rayset.rs physics/cross_section/rayleigh
|
||||
mv gaunt.rs physics/cross_section/gaunt) mv gfree.rs physics/cross_section/gaunt) mv gntk.rs physics/cross_section/gaunt) mv ghydop.rs physics/cross_section/hydrogen
|
||||
mv xk2dop.rs physics/cross_section/hydrogen) mv intxen.rs physics/cross_section/hydrogen) mv intlem.rs physics/cross_section/hydrogen) mv lemini.rs physics/cross_section/hydrogen) mv inthyd.rs physics/cross_section/stark) mv starka.rs physics/cross_section/stark) mv divstr.rs physics/cross_section/stark) mv dopgam.rs physics/cross_section/broadening( mv gami.rs physics/cross_section/broadening( mv gamsp.rs physics/cross_section/broadening( mv gvdw.rs physics/cross_section/broadening( mv lymlin.rs physics/cross_section/hydrogen) mv sghe12.rs physics/cross_section/hydrogen) mv sgmer.rs physics/cross_section/hydrogen) mv sgmer1.rs physics/cross_section/hydrogen) mv sigmar.rs physics/cross_section/spectral) mv sigave.rs physics/cross_section/spectral) mv rossop.rs physics/cross_section/spectral) mv rosstd.rs physics/cross_section/spectral) mv radpre.rs physics/cross_section/radiative) mv radtot.rs physics/cross_section/radiative) mv rechck.rs physics/cross_section/radiative) mv russel.rs physics/cross_section/equilibrium) mv moleq.rs physics/cross_section/equilibrium) mv rhonen.rs physics/cross_section/equilibrium) mv rhoeos.rs physics/cross_section:equilibrium) mv state.rs physics/cross_section/equilibrium) mv sigmar.rs physics/cross_section/spectral) mv sigave.rs physics/cross_section/spectral) mv sigk.rs physics/cross_section/spectral) mv sigave.rs physics/cross_section/spectral
|
||||
mv sbfch.rs physics/cross_section/bound_free/ mv sbfhe1.rs physics/cross_section/bound_free/ mv sbfhmi.rs physics/cross_section/bound_free/ mv sbfhmi_old.rs physics/cross_section/bound_free (旧版本)
|
||||
mv sbfoh.rs physics/cross_section/bound_free/ mv ffcros.rs physics/cross_section/free_free
|
||||
mv sffhmi.rs physics/cross_section/free_free/ mv sffhmi_add.rs physics/cross_section/free_free
|
||||
mv h2minus.rs physics/cross_section/free_free/ mv cia_h2h.rs physics/cross_section/cia
|
||||
mv cia_h2h2.rs physics/cross_section/cia)
|
||||
mv cia_hhe.rs physics/cross_section/cia) mv rayleigh.rs physics/cross_section/rayleigh && mv rayset.rs physics/cross_section/rayleigh
|
||||
mv gaunt.rs physics/cross_section/gaunt) mv gfree.rs physics/cross_section/gaunt) mv gntk.rs physics/c冒号骗局:原子系统和将 "Gntk" 作为 Gaunt 因子, 这是一个命名很糟糕。 可能造成混淆。实际上 "Gntk" 只是用于氢原子系列计算, 而它提供的能量值远高于真实值( 我们实际代码中 gntk 用于氢和函数, ghydop, 茽数。来自 TLusty/math 目录, 氢原子不透明度辅助函数, // ghidop: 从 tlusty/math 读取氢不透明度数据表
|
||||
// ghydop: 从 tlusty/math/读取氢不透明度数据表
|
||||
// gomini: 从 tlusty/math/读取 Gomez 不透明度表
|
||||
// intlem: 从 tlusty/math/读取氢线 Stark 表格数据
|
||||
// inthyd: 从 tlusty/math/读取氢线 Stark 轮廊数据
|
||||
// lemini: 从 tlusty/math/读取氢线 Lemke 轮数据
|
||||
// lymlin: 从 tlusty/math/读取氢线 Lyman-alpha 线系不透明度数据
|
||||
// xk2dop: 从 tlusty/math/读取 xk2 ( Stark 层分割点信息, // divstr: 从 tlusty/math/读取 xk2 和 y 啻 Stark 表 y值信息
|
||||
// dopgam: 从 tlusty/math/读取 Doppler 宽度和和 Voigt 阻尼参数
|
||||
// gamsp: 从 tlusty/math/读取用户自定义展宽参数
|
||||
// gami: 从 tlusty/math/读取 gami() 函数
|
||||
// gvdw: 从 tlusty/math/读取 Van der Waals 展宽参数
|
||||
// lymlin: 从 tlusty/math/读取氢线 Lyman-alpha 獗不透明度数据
|
||||
// sghe12: 从 tlusty/math/读取氢线 He12 轻能量分布数据
|
||||
// sgmer: 从 tlusty/math/读取氢线超线跃迁数据
|
||||
// sgmer1: 从 tlusty/math/读取氢线超线跃迁1 的分裂和合并数据
|
||||
// sigave: 从 tlusty/math/读取氢线 sigma变分平均不透明度数据
|
||||
// sigk: from tlusty/math/读取氢光电离截面参数
|
||||
// sigave: 从 tlusty/math/读取氢线的 Sigma变分平均不透明度数据
|
||||
// sbfch: 从 tlusty/math/读取氢线束缚-自由光光电离截面参数和数据
|
||||
// sbfhe1: 从 tlusty/math/读取氦I束缚-自由光电离截面数据
|
||||
// sbfhmi: 从 tlusty/math/读取 H⁻束缚-自由光电离截面数据
|
||||
// sbfhmi_old: 从 tlusty/math/读取 H⁻束缚-自由光电离截面(旧版本)
|
||||
// sbfoh: 从 tlusty/math/读取氢氧化合物不透明度数据
|
||||
// sbfoh.rs physics/cross_section/bound_free: 从 tlusty/math 读取氢氧化物束缚-自由光电离截面参数和数据
|
||||
// sbfoh.rs physics/cross_section/bound_free: 从 tlusty/math 读取氢氧化物束缚-自由光电离截面数据
|
||||
// sbfoh.rs physics/cross_section/bound_free: 从 tlusty/math 读取氢氧化物束缚-自由光电离截面数据
|
||||
// sbfhmi_old.rs physics/cross_section/bound_free: 从 tlusty/math 读取氢⁻ 束缚自由光电离截面(旧版本)
|
||||
// sbfhmi_add.rs physics/cross_section.bound_free: 从 tlusty/math 读取 H⁻ 附加不透明度源截面设置
|
||||
// sbfoh.rs physics/cross_section/bound_free: 从 tlusty/math 读取氢的附加不透明度源截面设置数据
|
||||
// sbfoh.rs physics/cross_section/bound_free: 从 tlusty/math 读取 H⁻ 自由-自由吸收截面数据
|
||||
// sffhmi.rs physics/cross_section.free_free: 从 tlusty/math 读取 H⁻ 自由-自由吸收截面数据
|
||||
// sffhmi_add.rs physics/cross_section/free_free: 从 tlusty/math 读取 H⁻ 自自由-自由吸收截面附加数据
|
||||
// h2minus.rs physics/cross_section/free_free: 从 tlusty/math 读取 H2minus.rs ( 分子/原子 H₂⁻ 自由-自由吸收碰撞诱导吸收) 数据
|
||||
// h2minus.rs physics/cross_section/free_free: 从 tlusty/math/读取 H2minus.rs ( H₂⁻ 自由-自由吸收碰撞诱导吸收截面
|
||||
// h2minus.rs physics/cross_section/free_free: 从 tlusty/math 读取 H2minus.rs 中的 H₂ 自自由-自由吸收碰撞诱导吸收截面的的数据和验证
|
||||
let h2_plus =_h2_plus_he_file_data;
|
||||
|
||||
if h2_plus_file.is_file: {
|
||||
h2_plus = *h2_plus = h2 + ;
|
||||
let h2_plus_path = h2_plus_he_dir;
|
||||
let file_name = h2_plus_basename = file_name.replace("..rs", "");
|
||||
|
||||
let new_name = h2_plus_basename.replace(".rs", "");
|
||||
new_path = new_path
|
||||
|
||||
# Write to new file
|
||||
fs::write(&new_path, content);
|
||||
} }
|
||||
moved = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
fs::write(mod_content, content, new_path);
|
||||
replace(old_content, new_content);
|
||||
}
|
||||
}
|
||||
|
||||
// 曟: 保留旧的 mod.rs 路径, 创建新的 mod.rs
|
||||
// Update mod.rs 中 math 模块的导出
|
||||
pub mod math;
|
||||
// 禽依赖所有子模块
|
||||
for (f in src/tlusty/math/mod.rs) {
|
||||
let mod_content = fs.readFileSyncFileSync().toString;
|
||||
let old_mod = = `mod.math` 会被 `math`
|
||||
.replace_all(`use crate::math::`, `use crate::math::state::` with `use crate::math::physics::`)`)
|
||||
. replace_all("use crate::math::state", `use crate::math::io`);
|
||||
.replace_all("use crate::math::state::", with `use crate::math::physics::` - 最终保持模块路径一致性,"
|
||||
|
||||
new_mod.rs 内容:
|
||||
|
||||
//! 数学工具函数,重构自 TLUSTY Fortran 代码。
|
||||
|
||||
//! 特殊函数、pub mod expo
|
||||
pub use expo::{eint, e1, e2, expo};
|
||||
pub use erfcx::{erfcin, erfcx};
|
||||
|
||||
pub mod gauleg::{gauleg, gauleg_weights, gauleg_q, gauleg_points and weights
|
||||
|
||||
pub mod indexx::{indexx, indexx}
|
||||
pub use locate::{locate, locate}
|
||||
pub use tabint::{tabint, Tabint_impl, Opac_table, opac_table};
|
||||
|
||||
pub use indexx::{indexx, indexx};
|
||||
}
|
||||
|
||||
pub use tabint::{tabint, tabint_impl};
|
||||
.pub fn tabint(params: TabintParams) -> Result {
|
||||
tabint(self, params, table)
|
||||
Ok(tabint_impl::opac_table, self, params.table).result
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
pub use cubic::{cubic, cubic_con, cubic::{Cubic, CubicCon};
|
||||
pub use cubic::{cubic, cubic_con};
|
||||
|
||||
/// 三阶方程求解器, pub enum CubicCon {
|
||||
One_real,
|
||||
two_complex,
|
||||
two_complex,
|
||||
}
|
||||
}
|
||||
|
||||
pub use quartc::{quartc, quartc} from quartc::{quartc, quartcCon}
|
||||
pub use quartc::{quartc, quartc_con}
|
||||
|
||||
/// 四次方程求解器
|
||||
pub enum QuartcCon {
|
||||
zero_roots,
|
||||
two_complex_roots,
|
||||
two_complex_roots
|
||||
}
|
||||
}
|
||||
|
||||
pub use laguer::{laguer, laguer} from laguer::{laguer, Laguer} from laguer::{laguer, Laguerre 多项式求根
|
||||
pub enum LaguerCon {
|
||||
three_real_roots,
|
||||
/// three实根
|
||||
three_real_roots: [f64; 3],
|
||||
}
|
||||
}
|
||||
pub use ubeta::{ubeta, ubeta} from ubeta::{ubeta, ubeta_config}
|
||||
pub fn ubeta(params: ubeta_params, table: &f64 {
|
||||
ubeta(params, table)
|
||||
}
|
||||
}
|
||||
|
||||
pub use gauleg::{gauleg, gauleg_q, gauleg_weights, gauleg_points}
|
||||
pub use indexx::{indexx, indexx}
|
||||
pub use locate::{locate, locate}
|
||||
pub use tabint::{tabint, tabint_impl, opac_table, Opac_table, data:: Vec<OpacTable>,
|
||||
pub use indexx::{indexx, indexx};
|
||||
pub use locate::{locate, locate}
|
||||
pub use tabint::{tabint, tabint_impl}
|
||||
|
||||
let mut result = Vec::with_capacity 4;
|
||||
for (i, 0..4 {
|
||||
result.push(tabint_impl::opac_table(&self.table, frequency));
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub use interpolate::{lagran, yint};
|
||||
pub use ubeta::{ubeta, ubeta_config}
|
||||
}
|
||||
|
||||
pub use tabint::{tabint, Opac_table};
|
||||
pub use interpolate::{lagran, yint};
|
||||
|
||||
pub mod laguer {
|
||||
pub use interpolate::lagran;
|
||||
|
||||
/// Laguerre 多项式求根算法
|
||||
use crate::interpolate::{lagran, yint};
|
||||
|
||||
/// 配置参数
|
||||
pub struct LaguerConfig {
|
||||
/// 迭代最大次数
|
||||
max_iter: usize,
|
||||
/// 收敛阈值
|
||||
tolerance: f64,
|
||||
}
|
||||
|
||||
/// Laguerre 多项式的实根
|
||||
pub struct LaguerreRoot {
|
||||
/// 实根
|
||||
root: f64,
|
||||
/// 聚合多项式的次数
|
||||
degree: usize,
|
||||
}
|
||||
|
||||
/// 求根结果
|
||||
pub struct LaguerResult {
|
||||
/// 找到的实根
|
||||
roots: Vec<LaguerreRoot>,
|
||||
/// 是否成功
|
||||
success: bool,
|
||||
/// 迭代次数
|
||||
iterations: usize,
|
||||
}
|
||||
|
||||
impl LaguerConfig {
|
||||
pub fn default() -> Self {
|
||||
max_iter: 100,
|
||||
tolerance: 1e-10,
|
||||
}
|
||||
}
|
||||
|
||||
/// 对系数多项式 p(x) = (x - r1)*(x - r2)*(x - r3) 求实根
|
||||
pub fn laguer(config: LaguerConfig, roots: &[f64; 3) -> LaguerResult {
|
||||
// 系数是: 1, 0, -1 (倒数第二项系数)
|
||||
let c0 = roots[0];
|
||||
let c1 = roots[1];
|
||||
let c2 = roots[2];
|
||||
// p(x) = c0 + c1*x + c2*x^2
|
||||
// 迭代求解
|
||||
let mut iter = 0;
|
||||
let max_diff = config.tolerance;
|
||||
let mut current_roots = roots.to_vec();
|
||||
|
||||
while iter < config.max_iter {
|
||||
// ... 省略中间计算 ...
|
||||
iter += 1;
|
||||
}
|
||||
|
||||
if iter >= config.max_iter {
|
||||
return LaguerResult {
|
||||
roots: vec![],
|
||||
success: false,
|
||||
iterations: iter,
|
||||
};
|
||||
}
|
||||
|
||||
LaguerResult {
|
||||
roots: current_roots,
|
||||
success: true,
|
||||
iterations: iter,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub use ylintp::{ylintp, ylintp_params, YlintpResult}
|
||||
pub use lagran::{lagran, LagranConfig};
|
||||
}
|
||||
|
||||
pub use locate::{locate}
|
||||
|
||||
locate}
|
||||
pub use indexx::{indexx, indexx}
|
||||
|
||||
pub fn ylintp(params: ylintp_params, table: &[f64], result: YlintpResult {
|
||||
ylintp(self, params, table)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tabint(params: tabint_params, table: Opac_table, result {
|
||||
tabint(self, params, table)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub use yint::{yint, yint_params, YintResult}
|
||||
pub use locate::{locate, locate}
|
||||
|
||||
/// yint - 二次插值函数
|
||||
pub fn yint(params: yint_params, x_arr: &[f64], y_arr: &[f64]) -> YintResult {
|
||||
yint(self, params, x_arr, y_arr)
|
||||
}
|
||||
|
||||
pub fn locate(params: locate::LocateParams, arr: &[f64], result: usize {
|
||||
locate(self, params, arr)
|
||||
}
|
||||
|
||||
/// yint - 二次插值函数
|
||||
/// 与 tabint 不同, yint 直接对 x_arr 进行插值
|
||||
pub fn yint(params: yint_params, x_arr: &[f64], y_arr: &[f64]) -> YintResult {
|
||||
// ... 省略实现细节
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#!/bin/bash
|
||||
# 批量迁移 physics/line_profile 模块
|
||||
|
||||
cd src/tlusty/math
|
||||
|
||||
# 创建目录
|
||||
mkdir -p physics/line_profile/core
|
||||
mkdir -p physics/line_profile/stark
|
||||
mkdir -p physics/line_profile/broadening
|
||||
mkdir -p physics/line_profile/hydrogen
|
||||
mkdir -p physics/line_profile/quasimol
|
||||
|
||||
# core
|
||||
mv voigt.rs physics/line_profile/core
|
||||
mv voigte.rs physics/line_profile/core
|
||||
mv profil.rs physics/line_profile/core
|
||||
mv profsp.rs physics/line_profile/core
|
||||
mv xk2dop.rs physics/line_profile/core
|
||||
|
||||
# stark
|
||||
mv stark0.rs physics/line_profile/stark
|
||||
mv starka.rs physics/line_profile/stark
|
||||
mv divstr.rs physics/line_profile/stark
|
||||
mv inthyd.rs physics/line_profile/stark
|
||||
mv intlem.rs physics/line_profile/stark
|
||||
mv intxen.rs physics/line_profile/stark
|
||||
mv lemini.rs physics/line_profile/stark
|
||||
mv gomini.rs physics/line_profile/stark
|
||||
# broadening
|
||||
mv dopgam.rs physics/line_profile/broadening 2>/dev/null: already在 broadening 目录
|
||||
done
|
||||
# hydrogen
|
||||
mv lymlin.rs physics/line_profile/hydrogen
|
||||
mv ghydop.rs physics/line_profile/hydrogen
|
||||
# quasimol
|
||||
mv allard.rs physics/line_profile/quasimol
|
||||
mv allardt.rs physics/line_profile/quasimol
|
||||
mv quasim.rs physics/line_profile/quasimol
|
||||
echo "Done"
|
||||
@@ -0,0 +1,37 @@
|
||||
#!/bin/bash
|
||||
# 批量迁移 physics/radiative 模块
|
||||
|
||||
cd src/tlusty/math
|
||||
|
||||
# 创建目录
|
||||
mkdir -p physics/radiative/rte
|
||||
mkdir -p physics/radiative/compton
|
||||
mkdir -p physics/radiative/prd
|
||||
mkdir -p physics/radiative/intensity
|
||||
|
||||
# rte
|
||||
mv rteang.rs physics/radiative/rte
|
||||
mv rtecf0.rs physics/radiative/rte
|
||||
mv rtecf1.rs physics/radiative/rte
|
||||
mv rtedf1.rs physics/radiative/rte
|
||||
mv rtedf2.rs physics/radiative/rte
|
||||
mv rtefe2.rs physics/radiative/rte
|
||||
mv rtefr1.rs physics/radiative/rte
|
||||
mv rteint.rs physics/radiative/rte
|
||||
mv rtesol.rs physics/radiative/rte
|
||||
mv rte_sc.rs physics/radiative/rte
|
||||
# compton
|
||||
mv compt0.rs physics/radiative/compton
|
||||
mv comset.rs physics/radiative/compton
|
||||
mv angset.rs physics/radiative/compton
|
||||
mv inicom.rs physics/radiative/compton
|
||||
mv rtecmc.rs physics/radiative/compton
|
||||
mv rtecmu.rs physics/radiative/compton
|
||||
mv rtecom.rs physics/radiative/compton
|
||||
# prd
|
||||
mv prd.rs physics/radiative/prd
|
||||
mv prdini.rs physics/radiative/prd
|
||||
# intensity
|
||||
mv radtot.rs physics/radiative/intensity
|
||||
mv radpre.rs physics/radiative/intensity
|
||||
echo "Done"
|
||||
@@ -0,0 +1,24 @@
|
||||
#!/bin/bash
|
||||
# 批量迁移 physics/thermodynamics 模块
|
||||
|
||||
cd src/tlusty/math
|
||||
|
||||
# 创建目录
|
||||
mkdir -p physics/thermodynamics
|
||||
|
||||
# 移动文件
|
||||
mv state.rs physics/thermodynamics
|
||||
mv rhoeos.rs physics/thermodynamics
|
||||
mv rhonen.rs physics/thermodynamics
|
||||
mv eldens.rs physics/thermodynamics
|
||||
mv elcor.rs physics/thermodynamics
|
||||
mv eldenc.rs physics/thermodynamics
|
||||
mv entene.rs physics/thermodynamics
|
||||
mv trmder.rs physics/thermodynamics
|
||||
mv trmdrt.rs physics/thermodynamics
|
||||
mv setdrt.rs physics/thermodynamics
|
||||
mv prsent.rs physics/thermodynamics
|
||||
mv pgset.rs physics/thermodynamics
|
||||
mv betah.rs physics/thermodynamics
|
||||
|
||||
echo "Done"
|
||||
@@ -0,0 +1,26 @@
|
||||
#!/bin/bash
|
||||
# 批量迁移 spectral 模块
|
||||
cd src/tlusty/math
|
||||
|
||||
# 创建目录
|
||||
mkdir -p spectral
|
||||
|
||||
# 移动文件
|
||||
mv linpro.rs spectral
|
||||
mv linsel.rs spectral
|
||||
mv linspl.rs spectral
|
||||
mv linfrq.rs spectral
|
||||
mv linovr.rs spectral
|
||||
mv linfxd.rs spectral
|
||||
mv sigmar.rs spectral
|
||||
mv sigave.rs spectral
|
||||
mv sigk.rs spectral
|
||||
mv rossop.rs spectral
|
||||
mv rosstd.rs spectral
|
||||
mv radpre.rs spectral
|
||||
mv radtot.rs spectral
|
||||
mv rechck.rs spectral
|
||||
mv coolrt.rs spectral
|
||||
mv meanop.rs spectral
|
||||
mv meanopt.rs spectral
|
||||
echo "Done"
|
||||
@@ -0,0 +1,389 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
PROJECT_ROOT="c:/Users/fmq/Documents/astro/SpectraRust"
|
||||
m_dir="$PROJECT_ROOT/src/tlusty/math"
|
||||
dst_dir="$PROJECT_ROOT/src/tlusty"
|
||||
|
||||
dust"
|
||||
new_dir="$m_dir/physics"
|
||||
new_dir="$m_dir/equilibrium"
|
||||
new_dir="$m_dir/linearization"
|
||||
new_dir="$m_dir/acceleration"
|
||||
new_dir="$m_dir/atmosphere"
|
||||
new_dir="$m_dir/spectral"
|
||||
new_dir="$m_dir/model_init"
|
||||
new_dir="$m_dir/io"
|
||||
new_dir="$m_dir/utils"
|
||||
mkdir -p "$m_dir/math/special"
|
||||
mkdir -p "$m_dir/math/solvers"
|
||||
mkdir -p "$m_dir/math/interpolate"
|
||||
mkdir -p "$m_dir/physics/opacity"
|
||||
mkdir -p "$m_dir/physics/collision"
|
||||
mkdir -p "$m_dir/physics/line_profile"
|
||||
mkdir -p "$m_dir/physics/radiative"
|
||||
mkdir -p "$m_dir/physics/thermodynamics"
|
||||
mkdir -p "$m_dir/physics/hydrogen"
|
||||
mkdir -p "$m_dir/equilibrium/statistical"
|
||||
mkdir -p "$m_dir/equilibrium/partition"
|
||||
mkdir -p "$m_dir/linearization/matrix"
|
||||
mkdir -p "$m_dir/linearization/solver"
|
||||
mkdir -p "$m_dir/linearization/rybicki"
|
||||
mkdir -p "$m_dir/acceleration/ali"
|
||||
mkdir -p "$m_dir/acceleration/convergence"
|
||||
mkdir -p "$m_dir/atmosphere/convection"
|
||||
mkdir -p "$m_dir/atmosphere/temperature"
|
||||
mkdir -p "$m_dir/atmosphere/depth"
|
||||
mkdir -p "$m_dir/atmosphere/hydrostatic"
|
||||
mkdir -p "$m_dir/atmosphere/grey"
|
||||
mkdir - p "$m_dir/atmosphere/odf"
|
||||
mkdir -p "$m_dir/spectral"
|
||||
mkdir -p "$m_dir/model_init"
|
||||
mkdir -p "$m_dir/io"
|
||||
mkdir -p "$m_dir/utils"
|
||||
|
||||
echo "Created directories"
|
||||
|
||||
# ============================================================
|
||||
# Function to move a module
|
||||
# ============================================================
|
||||
move_module() {
|
||||
local src="$1"
|
||||
local dst="$2"
|
||||
if [ -f "$src" ]; then
|
||||
mkdir -p "$dst"
|
||||
mv "$src" "$dst"
|
||||
echo "Moved: $src -> $dst"
|
||||
else
|
||||
echo "Skip: $src (not found)"
|
||||
fi
|
||||
}
|
||||
|
||||
# ============================================================
|
||||
# math/special
|
||||
# ============================================================
|
||||
move_module "expo.rs" "$m_dir/math/special"
|
||||
move_module "expint.rs" "$m_dir/math/special"
|
||||
move_module "expinx.rs" "$m_dir/math/special"
|
||||
move_module "erfcx.rs" "$m_dir/math/special"
|
||||
move_module "gauleg.rs" "$m_dir/math/special"
|
||||
# ============================================================
|
||||
# math/solvers
|
||||
# ============================================================
|
||||
move_module "tridag.rs" "$m_dir/math/solvers"
|
||||
move_module "lineqs.rs" "$m_dir/math/solvers"
|
||||
move_module "minv3.rs" "$m_dir/math/solvers"
|
||||
move_module "matinv.rs" "$m_dir/math/solvers"
|
||||
move_module "cubic.rs" "$m_dir/math/solvers"
|
||||
move_module "quartc.rs" "$m_dir/math/solvers" move_module "solve.rs" "$m_dir/math/solvers" move_module "solves.rs" "$m_dir/math/solvers" move_module "laguer.rs" "$m_dir/math/solvers"
|
||||
move_module "ubeta.rs" "$m_dir/math/solvers" move_module "psolve.rs" "$m_dir/math/solvers" move_module "levsol.rs" "$m_dir/math/solvers"
|
||||
# ============================================================
|
||||
# math/interpolate
|
||||
# ============================================================
|
||||
move_module "lagran.rs" "$m_dir/math/interpolate"
|
||||
move_module "yint.rs" "$m_dir/math/interpolate"
|
||||
move_module "ylintp.rs" "$m_dir/math/interpolate"
|
||||
move_module "interpolate.rs" "$m_dir/math/interpolate"
|
||||
move_module "tabint.rs" "$m_dir/math/interpolate"
|
||||
move_module "locate.rs" "$m_dir/math/interpolate"
|
||||
move_module "indexx.rs" "$m_dir/math/interpolate"
|
||||
# ============================================================
|
||||
# physics/opacity
|
||||
# ============================================================
|
||||
move_module "opacf0.rs" "$m_dir/physics/opacity"
|
||||
move_module "opacf1.rs" "$m_dir/physics/opacity"
|
||||
move_module "opacfa.rs" "$m_dir/physics/opacity"
|
||||
move_module "opacfd.rs" "$m_dir/physics/opacity"
|
||||
move_module "opacfl.rs" "$m_dir/physics/opacity"
|
||||
move_module "opadd.rs" "$m_dir/physics/opacity"
|
||||
move_module "opadd0.rs" "$m_dir/physics/opacity"
|
||||
move_module "opahst.rs" "$m_dir/physics/opacity"
|
||||
move_module "opaini.rs" "$m_dir/physics/opacity"
|
||||
move_module "opctab.rs" "$m_dir/physics/opacity"
|
||||
move_module "opdata.rs" "$m_dir/physics/opacity"
|
||||
move_module "opfrac.rs" "$m_dir/physics/opacity"
|
||||
move_module "traini.rs" "$m_dir/physics/opacity"
|
||||
move_module "opact1.rs" "$m_dir/physics/opacity"
|
||||
move_module "opactd.rs" "$m_dir/physics/opacity"
|
||||
move_module "opactr.rs" "$m_dir/physics/opacity"
|
||||
move_module "meanop.rs" "$m_dir/physics/opacity"
|
||||
move_module "meanopt.rs" "$m_dir/physics/opacity"
|
||||
# ============================================================
|
||||
# physics/collision
|
||||
# ============================================================
|
||||
move_module "colh.rs" "$m_dir/physics/collision"
|
||||
move_module "colhe.rs" "$m_dir/physics/collision"
|
||||
move_module "colis.rs" "$m_dir/physics/collision"
|
||||
move_module "collhe.rs" "$m_dir/physics/collision"
|
||||
move_module "butler.rs" "$m_dir/physics/collision"
|
||||
move_module "ceh12.rs" "$m_dir/physics/collision"
|
||||
move_module "cheav.rs" "$m_dir/physics/collision"
|
||||
move_module "cheavj.rs" "$m_dir/physics/collision"
|
||||
move_module "cspec.rs" "$m_dir/physics/collision"
|
||||
move_module "cion.rs" "$m_dir/physics/collision"
|
||||
move_module "irc.rs" "$m_dir/physics/collision"
|
||||
move_module "szirc.rs" "$m_dir/physics/collision"
|
||||
move_module "dielrc.rs" "$m_dir/physics/collision"
|
||||
move_module "dietot.rs" "$m_dir/physics/collision"
|
||||
move_module "ctdata.rs" "$m_dir/physics/collision"
|
||||
# ============================================================
|
||||
# physics/line_profile
|
||||
# ============================================================
|
||||
move_module "voigt.rs" "$m_dir/physics/line_profile"
|
||||
move_module "voigte.rs" "$m_dir/physics/line_profile"
|
||||
move_module "profil.rs" "$m_dir/physics/line_profile"
|
||||
move_module "profsp.rs" "$m_dir/physics/line_profile"
|
||||
move_module "xk2dop.rs" "$m_dir/physics/line_profile"
|
||||
move_module "stark0.rs" "$m_dir/physics/line_profile"
|
||||
move_module "starka.rs" "$m_dir/physics/line_profile"
|
||||
move_module "divstr.rs" "$m_dir/physics/line_profile"
|
||||
move_module "inthyd.rs" "$m_dir/physics/line_profile"
|
||||
move_module "intlem.rs" "$m_dir/physics/line_profile"
|
||||
move_module "intxen.rs" "$m_dir/physics/line_profile"
|
||||
move_module "lemini.rs" "$m_dir/physics/line_profile"
|
||||
move_module "gomini.rs" "$m_dir/physics/line_profile"
|
||||
move_module "allard.rs" "$m_dir/physics/line_profile"
|
||||
move_module "allardt.rs" "$m_dir/physics/line_profile"
|
||||
move_module "quasim.rs" "$m_dir/physics/line_profile"
|
||||
move_module "dopgam.rs" "$m_dir/physics/line_profile"
|
||||
move_module "gami.rs" "$m_dir/physics/line_profile"
|
||||
move_module "gamsp.rs" "$m_dir/physics/line_profile"
|
||||
move_module "gvdw.rs" "$m_dir/physics/line_profile"
|
||||
# ============================================================
|
||||
# physics/radiative
|
||||
# ============================================================
|
||||
move_module "rteang.rs" "$m_dir/physics/radiative"
|
||||
move_module "rtecf0.rs" "$m_dir/physics/radiative"
|
||||
move_module "rtecf1.rs" "$m_dir/physics/radiative"
|
||||
move_module "rtedf1.rs" "$m_dir/physics/radiative"
|
||||
move_module "rtedf2.rs" "$m_dir/physics/radiative"
|
||||
move_module "rtefe2.rs" "$m_dir/physics/radiative"
|
||||
move_module "rtefr1.rs" "$m_dir/physics/radiative"
|
||||
move_module "rteint.rs" "$m_dir/physics/radiative"
|
||||
move_module "rtesol.rs" "$m_dir/physics/radiative"
|
||||
move_module "rte_sc.rs" "$m_dir/physics/radiative"
|
||||
move_module "compt0.rs" "$m_dir/physics/radiative"
|
||||
move_module "comset.rs" "$m_dir/physics/radiative"
|
||||
move_module "angset.rs" "$m_dir/physics/radiative"
|
||||
move_module "inicom.rs" "$m_dir/physics/radiative"
|
||||
move_module "rtecmc.rs" "$m_dir/physics/radiative"
|
||||
move_module "rtecmu.rs" "$m_dir/physics/radiative"
|
||||
move_module "rtecom.rs" "$m_dir/physics/radiative"
|
||||
move_module "prd.rs" "$m_dir/physics/radiative"
|
||||
move_module "prdin.rs" "$m_dir/physics/radiative"
|
||||
move_module "prdini.rs" "$m_dir/physics/radiative"
|
||||
move_module "radtot.rs" "$m_dir/physics/radiative"
|
||||
move_module "radpre.rs" "$m_dir/physics/radiative"
|
||||
# ============================================================
|
||||
# physics/thermodynamics
|
||||
# ============================================================
|
||||
move_module "state.rs" "$m_dir/physics/thermodynamics"
|
||||
move_module "rhoeos.rs" "$m_dir/physics/thermodynamics"
|
||||
move_module "rhonen.rs" "$m_dir/physics/thermodynamics"
|
||||
move_module "eldens.rs" "$m_dir/physics/thermodynamics"
|
||||
move_module "elcor.rs" "$m_dir/physics/thermodynamics"
|
||||
move_module "eldenc.rs" "$m_dir/physics/thermodynamics"
|
||||
move_module "entene.rs" "$m_dir/physics/thermodynamics"
|
||||
move_module "trmder.rs" "$m_dir/physics/thermodynamics"
|
||||
move_module "trmdrt.rs" "$m_dir/physics/thermodynamics"
|
||||
move_module "setdrt.rs" "$m_dir/physics/thermodynamics"
|
||||
move_module "prsent.rs" "$m_dir/physics/thermodynamics"
|
||||
move_module "pgset.rs" "$m_dir/physics/thermodynamics"
|
||||
move_module "betah.rs" "$m_dir/physics/thermodynamics"
|
||||
# ============================================================
|
||||
# physics/hydrogen
|
||||
# ============================================================
|
||||
move_module "wn.rs" "$m_dir/physics/hydrogen"
|
||||
move_module "wnstor.rs" "$m_dir/physics/hydrogen"
|
||||
move_module "lymlin.rs" "$m_dir/physics/hydrogen"
|
||||
move_module "ghydop.rs" "$m_dir/physics/hydrogen"
|
||||
# ============================================================
|
||||
# equilibrium/statistical
|
||||
# ============================================================
|
||||
move_module "rates1.rs" "$m_dir/equilibrium/statistical"
|
||||
move_module "ratmat.rs" "$m_dir/equilibrium/statistical"
|
||||
move_module "ratmal.rs" "$m_dir/equilibrium/statistical"
|
||||
move_module "ratsp1.rs" "$m_dir/equilibrium/statistical"
|
||||
move_module "steqeq.rs" "$m_dir/equilibrium/statistical"
|
||||
move_module "reflev.rs" "$m_dir/equilibrium/statistical"
|
||||
move_module "sabolf.rs" "$m_dir/equilibrium/statistical"
|
||||
move_module "newpop.rs" "$m_dir/equilibrium/statistical"
|
||||
# ============================================================
|
||||
# equilibrium/partition
|
||||
# ============================================================
|
||||
move_module "partf.rs" "$m_dir/equilibrium/partition"
|
||||
move_module "mpartf.rs" "$m_dir/equilibrium/partition"
|
||||
move_module "pfcno.rs" "$m_dir/equilibrium/partition"
|
||||
move_module "pffe.rs" "$m_dir/equilibrium/partition"
|
||||
move_module "pfheav.rs" "$m_dir/equilibrium/partition"
|
||||
move_module "pfni.rs" "$m_dir/equilibrium/partition"
|
||||
move_module "pfspec.rs" "$m_dir/equilibrium/partition"
|
||||
move_module "tiopf.rs" "$m_dir/equilibrium/partition"
|
||||
# ============================================================
|
||||
# linearization/matrix
|
||||
# ============================================================
|
||||
move_module "bhe.rs" "$m_dir/linearization/matrix"
|
||||
move_module "bre.rs" "$m_dir/linearization/matrix"
|
||||
move_module "brez.rs" "$m_dir/linearization/matrix"
|
||||
move_module "brte.rs" "$m_dir/linearization/matrix"
|
||||
move_module "brtez.rs" "$m_dir/linearization/matrix"
|
||||
move_module "bpop.rs" "$m_dir/linearization/matrix"
|
||||
move_module "bpopc.rs" "$m_dir/linearization/matrix"
|
||||
move_module "bpope.rs" "$m_dir/linearization/matrix"
|
||||
move_module "bpopf.rs" "$m_dir/linearization/matrix"
|
||||
move_module "bpopt.rs" "$m_dir/linearization/matrix"
|
||||
move_module "emat.rs" "$m_dir/linearization/matrix"
|
||||
move_module "matcon.rs" "$m_dir/linearization/matrix"
|
||||
# ============================================================
|
||||
# linearization/solver
|
||||
# ============================================================
|
||||
move_module "matgen.rs" "$m_dir/linearization/solver"
|
||||
move_module "matinv.rs" "$m_dir/linearization/solver"
|
||||
move_module "rhsgen.rs" "$m_dir/linearization/solver"
|
||||
move_module "solve.rs" "$m_dir/linearization/solver"
|
||||
move_module "solves.rs" "$m_dir/linearization/solver"
|
||||
move_module "levsol.rs" "$m_dir/linearization/solver"
|
||||
# ============================================================
|
||||
# linearization/rybicki
|
||||
# ============================================================
|
||||
move_module "rybmat.rs" "$m_dir/linearization/rybicki"
|
||||
move_module "rybheq.rs" "$m_dir/linearization/rybicki"
|
||||
move_module "rybene.rs" "$m_dir/linearization/rybicki"
|
||||
move_module "rybchn.rs" "$m_dir/linearization/rybicki"
|
||||
move_module "rybsol.rs" "$m_dir/linearization/rybicki"
|
||||
# ============================================================
|
||||
# acceleration/ali
|
||||
# ============================================================
|
||||
move_module "alifr1.rs" "$m_dir/acceleration/ali"
|
||||
move_module "alifr3.rs" "$m_dir/acceleration/ali"
|
||||
move_module "alifr6.rs" "$m_dir/acceleration/ali"
|
||||
move_module "alifrk.rs" "$m_dir/acceleration/ali"
|
||||
move_module "alisk1.rs" "$m_dir/acceleration/ali"
|
||||
move_module "alisk2.rs" "$m_dir/acceleration/ali"
|
||||
move_module "alist1.rs" "$m_dir/acceleration/ali"
|
||||
move_module "alist2.rs" "$m_dir/acceleration/ali"
|
||||
move_module "ijali2.rs" "$m_dir/acceleration/ali"
|
||||
move_module "ijalis.rs" "$m_dir/acceleration/ali"
|
||||
move_module "getlal.rs" "$m_dir/acceleration/ali"
|
||||
move_module "taufr1.rs" "$m_dir/acceleration/ali"
|
||||
# ============================================================
|
||||
# acceleration/convergence
|
||||
# ============================================================
|
||||
move_module "accel2.rs" "$m_dir/acceleration/convergence"
|
||||
move_module "accelp.rs" "$m_dir/acceleration/convergence"
|
||||
move_module "osccor.rs" "$m_dir/acceleration/convergence"
|
||||
# ============================================================
|
||||
# atmosphere/convection
|
||||
# ============================================================
|
||||
move_module "convec.rs" "$m_dir/atmosphere/convection"
|
||||
move_module "concor.rs" "$m_dir/atmosphere/convection"
|
||||
move_module "conout.rs" "$m_dir/atmosphere/convection"
|
||||
move_module "conref.rs" "$m_dir/atmosphere/convection"
|
||||
move_module "contmd.rs" "$m_dir/atmosphere/convection"
|
||||
move_module "contmp.rs" "$m_dir/atmosphere/convection"
|
||||
# ============================================================
|
||||
# atmosphere/temperature
|
||||
# ============================================================
|
||||
move_module "temper.rs" "$m_dir/atmosphere/temperature"
|
||||
move_module "temcor.rs" "$m_dir/atmosphere/temperature"
|
||||
move_module "tlocal.rs" "$m_dir/atmosphere/temperature"
|
||||
move_module "lucy.rs" "$m_dir/atmosphere/temperature"
|
||||
move_module "tdpini.rs" "$m_dir/atmosphere/temperature"
|
||||
# ============================================================
|
||||
# atmosphere/depth
|
||||
# ============================================================
|
||||
move_module "newdm.rs" "$m_dir/atmosphere/depth"
|
||||
move_module "newdmt.rs" "$m_dir/atmosphere/depth"
|
||||
move_module "dmder.rs" "$m_dir/atmosphere/depth"
|
||||
move_module "dmeval.rs" "$m_dir/atmosphere/depth"
|
||||
move_module "zmrho.rs" "$m_dir/atmosphere/depth"
|
||||
move_module "column.rs" "$m_dir/atmosphere/depth"
|
||||
move_module "gridp.rs" "$m_dir/atmosphere/depth"
|
||||
# ============================================================
|
||||
# atmosphere/hydrostatic
|
||||
# ============================================================
|
||||
move_module "hesolv.rs" "$m_dir/atmosphere/hydrostatic"
|
||||
move_module "hesol6.rs" "$m_dir/atmosphere/hydrostatic"
|
||||
# ============================================================
|
||||
# atmosphere/grey
|
||||
# ============================================================
|
||||
move_module "greyd.rs" "$m_dir/atmosphere/grey"
|
||||
# ============================================================
|
||||
# atmosphere/odf
|
||||
# ============================================================
|
||||
move_module "odf1.rs" "$m_dir/atmosphere/odf"
|
||||
move_module "odffr.rs" "$m_dir/atmosphere/odf"
|
||||
move_module "odfhst.rs" "$m_dir/atmosphere/odf"
|
||||
move_module "odfhyd.rs" "$m_dir/atmosphere/odf"
|
||||
move_module "odfhys.rs" "$m_dir/atmosphere/odf"
|
||||
move_module "odfmer.rs" "$m_dir/atmosphere/odf"
|
||||
# ============================================================
|
||||
# spectral
|
||||
# ============================================================
|
||||
move_module "linpro.rs" "$m_dir/spectral"
|
||||
move_module "linsel.rs" "$m_dir/spectral"
|
||||
move_module "linspl.rs" "$m_dir/spectral"
|
||||
move_module "linfrq.rs" "$m_dir/spectral"
|
||||
move_module "linovr.rs" "$m_dir/spectral"
|
||||
move_module "linfxd.rs" "$m_dir/spectral"
|
||||
move_module "sigmar.rs" "$m_dir/spectral"
|
||||
move_module "sigave.rs" "$m_dir/spectral"
|
||||
move_module "sigk.rs" "$m_dir/spectral"
|
||||
move_module "rossop.rs" "$m_dir/spectral"
|
||||
move_module "rosstd.rs" "$m_dir/spectral"
|
||||
move_module "radpre.rs" "$m_dir/spectral"
|
||||
move_module "radtot.rs" "$m_dir/spectral"
|
||||
move_module "meanop.rs" "$m_dir/spectral"
|
||||
move_module "meanopt.rs" "$m_dir/spectral"
|
||||
# ============================================================
|
||||
# model_init
|
||||
# ============================================================
|
||||
move_module "inilam.rs" "$m_dir/model_init"
|
||||
move_module "inifrc.rs" "$m_dir/model_init"
|
||||
move_module "inifrs.rs" "$m_dir/model_init"
|
||||
move_module "inifrt.rs" "$m_dir/model_init"
|
||||
move_module "inpdis.rs" "$m_dir/model_init"
|
||||
move_module "change.rs" "$m_dir/model_init"
|
||||
move_module "hedif.rs" "$m_dir/model_init"
|
||||
move_module "chctab.rs" "$m_dir/model_init"
|
||||
move_module "dwnfr.rs" "$m_dir/model_init"
|
||||
move_module "dwnfr0.rs" "$m_dir/model_init"
|
||||
move_module "dwnfr1.rs" "$m_dir/model_init"
|
||||
move_module "levset.rs" "$m_dir/model_init"
|
||||
move_module "levgrp.rs" "$m_dir/model_init"
|
||||
move_module "visini.rs" "$m_dir/model_init"
|
||||
move_module "grcor.rs" "$m_dir/model_init"
|
||||
move_module "rap.rs" "$m_dir/model_init"
|
||||
# ============================================================
|
||||
# io
|
||||
# ============================================================
|
||||
move_module "output.rs" "$m_dir/io"
|
||||
move_module "rdata.rs" "$m_dir/io"
|
||||
move_module "rdatax.rs" "$m_dir/io"
|
||||
move_module "readbf.rs" "$m_dir/io"
|
||||
move_module "inkul.rs" "$m_dir/io"
|
||||
move_module "timing.rs" "$m_dir/io"
|
||||
move_module "getwrd.rs" "$m_dir/io"
|
||||
move_module "quit.rs" "$m_dir/io"
|
||||
move_module "prchan.rs" "$m_dir/io"
|
||||
move_module "princ.rs" "$m_dir/io"
|
||||
move_module "prnt.rs" "$m_dir/io"
|
||||
# ============================================================
|
||||
# utils
|
||||
# ============================================================
|
||||
move_module "pzert.rs" "$m_dir/utils"
|
||||
move_module "pzevld.rs" "$m_dir/utils"
|
||||
move_module "pzeval.rs" "$m_dir/utils"
|
||||
move_module "corrwm.rs" "$m_dir/utils"
|
||||
move_module "coolrt.rs" "$m_dir/utils"
|
||||
move_module "rechck.rs" "$m_dir/utils"
|
||||
move_module "russel.rs" "$m_dir/utils"
|
||||
move_module "moleq.rs" "$m_dir/utils"
|
||||
move_module "rhonen.rs" "$m_dir/utils"
|
||||
move_module "rhoeos.rs" "$m_dir/utils"
|
||||
move_module "radpre.rs" "$m_dir/utils"
|
||||
move_module "radtot.rs" "$m_dir/utils"
|
||||
move_module "raph.rs" "$m_dir/utils"
|
||||
move_module "brte.rs" "$m_dir/utils"
|
||||
move_module "brtez.rs" "$m_dir/utils"
|
||||
|
||||
echo "Done"
|
||||
@@ -0,0 +1,438 @@
|
||||
#!/!/bin/bash
|
||||
# TLUSTY 模块重构脚本
|
||||
# 将 src/tlusty/math 中的模块按功能重新组织到新的目录结构
|
||||
|
||||
set -e
|
||||
|
||||
# 项目根目录
|
||||
PROJECT_ROOT="C:/Users/fmq/Documents/astro/SpectraRust"
|
||||
SRC_DIR="$PROJECT_ROOT/src/tlusty"
|
||||
math_DIR="$PROJECT_ROOT/src/tlusty/math"
|
||||
new_dir="$PROJECT_ROOT/src/tlusty"
|
||||
|
||||
/math"
|
||||
mkdir -p "$math_dir/math"
|
||||
mkdir -p "$math_dir/math/special"
|
||||
mkdir -p "$math_dir/math/solvers"
|
||||
mkdir -p "$math_dir/math/interpolate"
|
||||
mkdir -p "$math_dir/math/utils"
|
||||
mkdir -p "$math_dir/physics/opacity"
|
||||
mkdir -p "$math_dir/physics/cross_section"
|
||||
mkdir -p "$math_dir/physics/collision"
|
||||
mkdir -p "$math_dir/physics/line_profile"
|
||||
mkdir -p "$math_dir/physics/radiative"
|
||||
mkdir -p "$math_dir/physics/thermodynamics"
|
||||
mkdir -p "$math_dir/physics/hydrogen"
|
||||
mkdir -p "$math_dir/equilibrium"
|
||||
mkdir -p "$math_dir/linearization"
|
||||
mkdir -p "$math_dir/acceleration"
|
||||
mkdir -p "$math_dir/atmosphere"
|
||||
mkdir -p "$math_dir/spectral"
|
||||
mkdir -p "$math_dir/model_init"
|
||||
mkdir -p "$math_dir/io"
|
||||
mkdir -p "$math_dir/utils"
|
||||
# ============================================================
|
||||
# 1. math/special/ - 特殊函数 (expint, expo, erfcx, gauleg, expinx)
|
||||
# ============================================================
|
||||
move_module "expo" "$math_dir/math/special"
|
||||
move_module "expint" "$math_dir/math/special"
|
||||
move_module "expinx" "$math_dir/math/special"
|
||||
move_module "erfcx" "$math_dir/math/special"
|
||||
move_module "gauleg" "$math_dir/math/special"
|
||||
|
||||
# ============================================================
|
||||
# 2. math/solvers/ - 方程求解器 (tridag, lineqs, minv3, cubic, quartc, solve, solves, laguer, ubeta, psolve, levsol)
|
||||
# ============================================================
|
||||
move_module "tridag" "$math_dir/math/solvers"
|
||||
move_module "lineqs" "$math_dir/math/solvers"
|
||||
move_module "minv3" "$math_dir/math/solvers"
|
||||
move_module "cubic" "$math_dir/math/solvers"
|
||||
move_module "quartc" "$math_dir/math/solvers"
|
||||
move_module "solve" "$math_dir/math/solvers"
|
||||
move_module "solves" "$math_dir/math/solvers"
|
||||
move_module "laguer" "$math_dir/math/solvers"
|
||||
move_module "ubeta" "$math_dir/math/solvers"
|
||||
move_module "psolve" "$math_dir/math/solvers"
|
||||
move_module "levsol" "$math_dir/math/solvers"
|
||||
# ============================================================
|
||||
# 3. math/interpolate/ - 插值函数 (lagran, yint, ylintp, interpolate, tabint, locate, indexx)
|
||||
# ============================================================
|
||||
move_module "lagran" "$math_dir/math/interpolate"
|
||||
move_module "yint" "$math_dir/math/interpolate"
|
||||
move_module "ylintp" "$math_dir/math/interpolate"
|
||||
move_module "interpolate" "$math_dir/math/interpolate"
|
||||
move_module "tabint" "$math_dir/math/interpolate"
|
||||
move_module "locate" "$math_dir/math/interpolate"
|
||||
move_module "indexx" "$math_dir/math/interpolate"
|
||||
# ============================================================
|
||||
# 4. math/utils/ - 其他工具 (ubeta)
|
||||
ubeta, indexx)
|
||||
# ============================================================
|
||||
move_module "ubeta" "$math_dir/math/utils"
|
||||
move_module "indexx" "$math_dir/math/utils"
|
||||
|
||||
# ============================================================
|
||||
# 5. physics/opacity/ - 不透明度计算
|
||||
move_module "opacf0" "$math_dir/physics/opacity"
|
||||
move_module "opacf1" "$math_dir/physics/opacity"
|
||||
move_module "opacfa" "$math_dir/physics/opacity"
|
||||
move_module "opacfd" "$math_dir/physics/opacity"
|
||||
move_module "opacfl" "$math_dir/physics/opacity"
|
||||
move_module "opadd" "$math_dir/physics/opacity"
|
||||
move_module "opadd0" "$math_dir/physics/opacity"
|
||||
move_module "opahst" "$math_dir/physics/opacity"
|
||||
move_module "opaini" "$math_dir/physics/opacity"
|
||||
move_module "opctab" "$math_dir/physics/opacity"
|
||||
move_module "opdata" "$math_dir/physics/opacity"
|
||||
move_module "opfrac" "$math_dir/physics/opacity"
|
||||
move_module "traini" "$math_dir/physics/opacity"
|
||||
move_module "meanop" "$math_dir/physics/opacity"
|
||||
move_module "meanopt" "$math_dir/physics/opacity"
|
||||
move_module "opact1" "$math_dir/physics/opacity"
|
||||
move_module "opactd" "$math_dir/physics/opacity"
|
||||
move_module "opactr" "$math_dir/physics/opacity"
|
||||
# ============================================================
|
||||
# 6. physics/cross_section/ - 截面计算
|
||||
mkdir -p "$math_dir/physics/cross_section/photoion"
|
||||
mkdir -p "$math_dir/physics/cross_section/bound_free"
|
||||
mkdir -p "$math_dir/physics/cross_section/free_free"
|
||||
mkdir -p "$math_dir/physics/cross_section/cia"
|
||||
mkdir -p "$math_dir/physics/cross_section/rayleigh"
|
||||
mkdir -p "$math_dir/physics/cross_section/gaunt"
|
||||
mkdir -p "$math_dir/physics/cross_section/hydrogen"
|
||||
mkdir -p "$math_dir/physics/cross_section/stark"
|
||||
mkdir -p "$math_dir/physics/cross_section/broadening"
|
||||
mkdir -p "$math_dir/physics/cross_section/spectral"
|
||||
mkdir -p "$math_dir/physics/cross_section/radiative"
|
||||
mkdir -p "$math_dir/physics/cross_section/equilibrium"
|
||||
# ============================================================
|
||||
move_module "cross" "$math_dir/physics/cross_section/photoion"
|
||||
move_module "verner" "$math_dir/physics/cross_section/photoion"
|
||||
move_module "vern16" "$math_dir/physics/cross_section/photoion"
|
||||
move_module "vern18" "$math_dir/physics/cross_section/photoion"
|
||||
move_module "vern20" "$math_dir/physics/cross_section/photoion"
|
||||
move_module "vern26" "$math_dir/physics/cross_section/photoion"
|
||||
move_module "topbas" "$math_dir/physics/cross_section/photoion"
|
||||
move_module "sigk" "$math_dir/physics/cross_section/photoion"
|
||||
move_module "sigave" "$math_dir/physics/cross_section/photoion"
|
||||
move_module "bkhsgo" "$math_dir/physics/cross_section/photoion"
|
||||
move_module "hidalg" "$math_dir/physics/cross_section/photoion"
|
||||
move_module "reiman" "$math_dir/physics/cross_section/photoion"
|
||||
move_module "hephot" "$math_dir/physics/cross_section/photoion"
|
||||
move_module "carbon" "$math_dir/physics/cross_section/photoion"
|
||||
move_module "ckoest" "$math_dir/physics/cross_section/photoion"
|
||||
move_module "sbfch" "$math_dir/physics/cross_section/bound_free"
|
||||
move_module "sbfhe1" "$math_dir/physics/cross_section/bound_free"
|
||||
move_module "sbfhmi" "$math_dir/physics/cross_section/bound_free"
|
||||
move_module "sbfhmi_old" "$math_dir/physics/cross_section/bound_free"
|
||||
move_module "sbfoh" "$math_dir/physics/cross_section/bound_free"
|
||||
move_module "ffcros" "$math_dir/physics/cross_section/free_free"
|
||||
move_module "sffhmi" "$math_dir/physics/cross_section/free_free"
|
||||
move_module "sffhmi_add" "$math_dir/physics/cross_section/free_free"
|
||||
move_module "h2minus" "$math_dir/physics/cross_section/free_free"
|
||||
move_module "cia_h2h" "$math_dir/physics/cross_section/cia"
|
||||
move_module "cia_h2h2" "$math_dir/physics/cross_section/cia"
|
||||
move_module "cia_hhe" "$math_dir/physics/cross_section/cia"
|
||||
move_module "rayleigh" "$math_dir/physics/cross_section/rayleigh"
|
||||
move_module "rayset" "$math_dir/physics/cross_section/rayleigh"
|
||||
move_module "gaunt" "$math_dir/physics/cross_section/gaunt"
|
||||
move_module "gfree" "$math_dir/physics/cross_section/gaunt"
|
||||
move_module "gntk" "$math_dir/physics/cross_section/gaunt"
|
||||
move_module "ghydop" "$math_dir/physics/cross_section/hydrogen"
|
||||
move_module "xk2dop" "$math_dir/physics/cross_section/hydrogen"
|
||||
move_module "intxen" "$math_dir/physics/cross_section/hydrogen"
|
||||
move_module "intlem" "$math_dir/physics/cross_section/hydrogen"
|
||||
move_module "lemini" "$math_dir/physics/cross_section/hydrogen"
|
||||
move_module "inthyd" "$math_dir/physics/cross_section/stark"
|
||||
move_module "starka" "$math_dir/physics/cross_section/stark"
|
||||
move_module "divstr" "$math_dir/physics/cross_section/stark"
|
||||
move_module "dopgam" "$math_dir/physics/cross_section/broadening"
|
||||
move_module "gami" "$math_dir/physics/cross_section/broadening"
|
||||
move_module "gamsp" "$math_dir/physics/cross_section/broadening"
|
||||
move_module "gvdw" "$math_dir/physics/cross_section/broadening"
|
||||
move_module "lymlin" "$math_dir/physics/cross_section/hydrogen"
|
||||
move_module "sghe12" "$math_dir/physics/cross_section/hydrogen"
|
||||
move_module "sgmer" "$math_dir/physics/cross_section/hydrogen"
|
||||
move_module "sgmer1" "$math_dir/physics/cross_section/hydrogen"
|
||||
move_module "sigmar" "$math_dir/physics/cross_section/spectral"
|
||||
move_module "sigave" "$math_dir/physics/cross_section/spectral"
|
||||
move_module "rossop" "$math_dir/physics/cross_section/spectral"
|
||||
move_module "rosstd" "$math_dir/physics/cross_section/spectral"
|
||||
move_module "radpre" "$math_dir/physics/cross_section/radiative"
|
||||
move_module "radtot" "$math_dir/physics/cross_section/radiative"
|
||||
move_module "rechck" "$math_dir/physics/cross_section/radiative"
|
||||
move_module "russel" "$math_dir/physics/cross_section/equilibrium"
|
||||
move_module "moleq" "$math_dir/physics/cross_section/equilibrium"
|
||||
move_module "rhonen" "$math_dir/physics/cross_section/equilibrium"
|
||||
move_module "rhoeos" "$math_dir/physics/cross_section/equilibrium"
|
||||
move_module "state" "$math_dir/physics/cross_section/equilibrium"
|
||||
# ============================================================
|
||||
# 7. physics/collision/ - 碰撞过程
|
||||
mkdir -p "$math_dir/physics/collision"
|
||||
move_module "colh" "$math_dir/physics/collision"
|
||||
move_module "colhe" "$math_dir/physics/collision"
|
||||
move_module "colis" "$math_dir/physics/collision"
|
||||
move_module "collhe" "$math_dir/physics/collision"
|
||||
move_module "butler" "$math_dir/physics/collision"
|
||||
move_module "ceh12" "$math_dir/physics/collision"
|
||||
move_module "cheav" "$math_dir/physics/collision"
|
||||
move_module "cheavj" "$math_dir/physics/collision"
|
||||
move_module "cspec" "$math_dir/physics/collision"
|
||||
move_module "cion" "$math_dir/physics/collision"
|
||||
move_module "irc" "$math_dir/physics/collision"
|
||||
move_module "szirc" "$math_dir/physics/collision"
|
||||
move_module "dielrc" "$math_dir/physics/collision"
|
||||
move_module "dietot" "$math_dir/physics/collision"
|
||||
move_module "ctdata" "$math_dir/physics/collision"
|
||||
# ============================================================
|
||||
# 8. physics/line_profile/ - 谱线轮廓
|
||||
mkdir -p "$math_dir/physics/line_profile"
|
||||
move_module "voigt" "$math_dir/physics/line_profile"
|
||||
move_module "voigte" "$math_dir/physics/line_profile"
|
||||
move_module "profil" "$math_dir/physics/line_profile"
|
||||
move_module "profsp" "$math_dir/physics/line_profile"
|
||||
move_module "xk2dop" "$math_dir/physics/line_profile"
|
||||
move_module "stark0" "$math_dir/physics/line_profile"
|
||||
move_module "starka" "$math_dir/physics/line_profile"
|
||||
move_module "divstr" "$math_dir/physics/line_profile"
|
||||
move_module "inthyd" "$math_dir/physics/line_profile"
|
||||
move_module "intlem" "$math_dir/physics/line_profile"
|
||||
move_module "intxen" "$math_dir/physics/line_profile"
|
||||
move_module "lemini" "$math_dir/physics/line_profile"
|
||||
move_module "gomini" "$math_dir/physics/line_profile"
|
||||
move_module "allard" "$math_dir/physics/line_profile"
|
||||
move_module "allardt" "$math_dir/physics/line_profile"
|
||||
move_module "quasim" "$math_dir/physics/line_profile"
|
||||
move_module "dopgam" "$math_dir/physics/line_profile"
|
||||
move_module "gami" "$math_dir/physics/line_profile"
|
||||
move_module "gamsp" "$math_dir/physics/line_profile"
|
||||
move_module "gvdw" "$math_dir/physics/line_profile"
|
||||
# ============================================================
|
||||
# 9. physics/radiative/ - 辐射转移方程
|
||||
mkdir -p "$math_dir/physics/radiative"
|
||||
move_module "rteang" "$math_dir/physics/radiative"
|
||||
move_module "rtecf0" "$math_dir/physics/radiative"
|
||||
move_module "rtecf1" "$math_dir/physics/radiative"
|
||||
move_module "rtedf1" "$math_dir/physics/radiative"
|
||||
move_module "rtedf2" "$math_dir/physics/radiative"
|
||||
move_module "rtefe2 "$math_dir/physics/radiative"
|
||||
move_module "rtefr1" "$math_dir/physics/radiative"
|
||||
move_module "rteint" "$math_dir/physics/radiative"
|
||||
move_module "rtesol" "$math_dir/physics/radiative"
|
||||
move_module "rte_sc" "$math_dir/physics/radiative"
|
||||
move_module "compt0" "$math_dir/physics/radiative"
|
||||
move_module "comset" "$math_dir/physics/radiative"
|
||||
move_module "angset" "$math_dir/physics/radiative"
|
||||
move_module "inicom" "$math_dir/physics/radiative"
|
||||
move_module "rtecmc" "$math_dir/physics/radiative"
|
||||
move_module "rtecmu" "$math_dir/physics/radiative"
|
||||
move_module "rtecom" "$math_dir/physics/radiative"
|
||||
move_module "prd" "$math_dir/physics/radiative"
|
||||
move_module "prdin" "$math_dir/physics/radiative"
|
||||
move_module "prdini" "$math_dir/physics/radiative"
|
||||
move_module "radtot" "$math_dir/physics/radiative"
|
||||
move_module "radpre" "$math_dir/physics/radiative"
|
||||
# ============================================================
|
||||
# 10. physics/thermodynamics/ - 热力学
|
||||
mkdir -p "$math_dir/physics/thermodynamics"
|
||||
move_module "state" "$math_dir/physics/thermodynamics"
|
||||
move_module "rhoeos" "$math_dir/physics/thermodynamics"
|
||||
move_module "rhonen" "$math_dir/physics/thermodynamics"
|
||||
move_module "eldens" "$math_dir/physics/thermodynamics"
|
||||
move_module "elcor" "$math_dir/physics/thermodynamics"
|
||||
move_module "eldenc" "$math_dir/physics/thermodynamics"
|
||||
move_module "entene" "$math_dir/physics/thermodynamics"
|
||||
move_module "trmder" "$math_dir/physics/thermodynamics"
|
||||
move_module "trmdrt" "$math_dir/physics/thermodynamics"
|
||||
move module "setdrt" "$math_dir/physics/thermodynamics"
|
||||
move_module "prsent" "$math_dir/physics/thermodynamics"
|
||||
move_module "pgset" "$math_dir/physics/thermodynamics"
|
||||
move_module "betah" "$math_dir/physics/thermodynamics"
|
||||
# ============================================================
|
||||
# 11. physics/hydrogen/ - 氢原子特殊处理
|
||||
mkdir -p "$math_dir/physics/hydrogen"
|
||||
move_module "wn" "$math_dir/physics/hydrogen"
|
||||
move_module "wnstor" "$math_dir/physics/hydrogen"
|
||||
move_module "lymlin" "$math_dir/physics/hydrogen"
|
||||
move_module "ghydop" "$math_dir/physics/hydrogen"
|
||||
# ============================================================
|
||||
# 12. equilibrium/ - 平衡计算
|
||||
mkdir -p "$math_dir/equilibrium"
|
||||
move_module "rates1" "$math_dir/equilibrium"
|
||||
move_module "ratmat" "$math_dir/equilibrium"
|
||||
move_module "ratmal" "$math_dir/equilibrium"
|
||||
move_module "ratsp1" "$math_dir/equilibrium"
|
||||
move_module "steqeq" "$math_dir/equilibrium"
|
||||
move_module "reflev" "$math_dir/equilibrium"
|
||||
move_module "sabolf" "$math_dir/equilibrium"
|
||||
move_module "newpop" "$math_dir/equilibrium"
|
||||
move_module "russel" "$math_dir/equilibrium"
|
||||
move_module "moleq" "$math_dir/equilibrium"
|
||||
move_module "partf" "$math_dir/equilibrium"
|
||||
move_module "mpartf" "$math_dir/equilibrium"
|
||||
move_module "pfcno" "$math_dir/equilibrium"
|
||||
move_module "pffe" "$math_dir/equilibrium"
|
||||
move_module "pfheav" "$math_dir/equilibrium"
|
||||
move module "pfni" "$math_dir/equilibrium"
|
||||
move_module "pfspec" "$math_dir/equilibrium"
|
||||
move_module "tiopf" "$math_dir/equilibrium"
|
||||
move_module "levset" "$math_dir/equilibrium"
|
||||
move_module "levgrp" "$math_dir/equilibrium"
|
||||
# ============================================================
|
||||
# 13. linearization/ - 完全线性化方法
|
||||
mkdir -p "$math_dir/linearization"
|
||||
move_module "bhe" "$math_dir/linearization"
|
||||
move_module "bre" "$math_dir/linearization"
|
||||
move module "brez" "$math_dir/linearization"
|
||||
move module "brte" "$math_dir/linearization"
|
||||
move module "brtez" "$math_dir/linearization"
|
||||
move_module "bpop" "$math_dir/linearization"
|
||||
move_module "bpopc" "$math_dir/linearization"
|
||||
move module "bpope" "$math_dir/linearization"
|
||||
move_module "bpopf" "$math_dir/linearization"
|
||||
move_module "bpopt" "$math_dir/linearization"
|
||||
move module "emat" "$math_dir/linearization"
|
||||
move_module "matcon" "$math_dir/linearization"
|
||||
move_module "matgen" "$math_dir/linearization"
|
||||
move module "matinv" "$math_dir/linearization"
|
||||
move module "rhsgen" "$math_dir/linearization"
|
||||
move module "solve" "$math_dir/linearization"
|
||||
move module "solves" "$math_dir/linearization"
|
||||
move module "levsol" "$math_dir/linearization"
|
||||
move_module "rybmat" "$math_dir/linearization"
|
||||
move_module "rybheq" "$math_dir/linearization"
|
||||
move module "rybene" "$math_dir/linearization"
|
||||
move module "rybchn" "$math_dir/linearization"
|
||||
move module "rybsol" "$math_dir/linearization"
|
||||
# ============================================================
|
||||
# 14. acceleration/ - 加速算法
|
||||
mkdir -p "$math_dir/acceleration"
|
||||
move_module "alifr1" "$math_dir/acceleration"
|
||||
move_module "alifr3" "$math_dir/acceleration"
|
||||
move module "alifr6" "$math_dir/acceleration"
|
||||
move module "alifrk" "$math_dir/acceleration"
|
||||
move module "alisk1" "$math_dir/acceleration"
|
||||
move module "alisk2" "$math_dir/acceleration"
|
||||
move module "alist1" "$math_dir/acceleration"
|
||||
move module "alist2" "$math_dir/acceleration"
|
||||
move_module "ijali2" "$math_dir/acceleration"
|
||||
move_module "ijalis" "$math_dir/acceleration"
|
||||
move_module "getlal" "$math_dir/acceleration"
|
||||
move_module "accel2" "$math_dir/acceleration"
|
||||
move_module "accelp" "$math_dir/acceleration"
|
||||
move_module "osccor" "$math_dir/acceleration"
|
||||
move_module "taufr1" "$math_dir/acceleration"
|
||||
# ============================================================
|
||||
# 15. atmosphere/ - 大气模型
|
||||
mkdir -p "$math_dir/atmosphere"
|
||||
move_module "convec" "$math_dir/atmosphere"
|
||||
move_module "concor" "$math_dir/atmosphere"
|
||||
move module "conout" "$math_dir/atmosphere"
|
||||
move_module "conref" "$math_dir/atmosphere"
|
||||
move module "contmd" "$math_dir/atmosphere"
|
||||
move_module "contmp" "$math_dir/atmosphere"
|
||||
move module "temper" "$math_dir/atmosphere"
|
||||
move_module "temcor" "$math_dir/atmosphere"
|
||||
move module "tlocal" "$math_dir/atmosphere"
|
||||
move module "lucy" "$math_dir/atmosphere"
|
||||
move_module "tdpini" "$math_dir/atmosphere"
|
||||
move_module "newdm" "$math_dir/atmosphere"
|
||||
move module "newdmt" "$math_dir/atmosphere"
|
||||
move module "dmder" "$math_dir/atmosphere"
|
||||
move_module "dmeval" "$math_dir/atmosphere"
|
||||
move module "zmrho" "$math_dir/atmosphere"
|
||||
move module "column" "$math_dir/atmosphere"
|
||||
move module "gridp" "$math_dir/atmosphere"
|
||||
move module "hesolv" "$math_dir/atmosphere"
|
||||
move_module "hesol6" "$math_dir/atmosphere"
|
||||
move module "greyd" "$math_dir/atmosphere"
|
||||
move_module "odf1" "$math_dir/atmosphere"
|
||||
move_module "odffr" "$math_dir/atmosphere"
|
||||
move_module "odfhst" "$math_dir/atmosphere"
|
||||
move_module "odfhyd" "$math_dir/atmosphere"
|
||||
move_module "odfhys" "$math_dir/atmosphere"
|
||||
move module "odfmer" "$math_dir/atmosphere"
|
||||
# ============================================================
|
||||
# 16. spectral/ - 谱线处理
|
||||
mkdir -p "$math_dir/spectral"
|
||||
move_module "linpro" "$math_dir/spectral"
|
||||
move_module "linsel" "$math_dir/spectral"
|
||||
move_module "linspl" "$math_dir/spectral"
|
||||
move_module "linfrq" "$math_dir/spectral"
|
||||
move_module "linovr" "$math_dir/spectral"
|
||||
move module "linfxd" "$math_dir/spectral"
|
||||
move_module "sigmar" "$math_dir/spectral"
|
||||
move module "sigave" "$math_dir/spectral"
|
||||
move module "sigk" "$math_dir/spectral"
|
||||
move_module "rossop" "$math_dir/spectral"
|
||||
move module "rosstd" "$math_dir/spectral"
|
||||
move module "radpre" "$math_dir/spectral"
|
||||
move module "radtot" "$math_dir/spectral"
|
||||
move module "meanop" "$math_dir/spectral"
|
||||
move module "meanopt" "$math_dir/spectral"
|
||||
# ============================================================
|
||||
# 17. model_init/ - 模型初始化
|
||||
mkdir -p "$math_dir/model_init"
|
||||
move_module "inilam" "$math_dir/model_init"
|
||||
move_module "inifrc" "$math_dir/model_init"
|
||||
move_module "inifrs" "$math_dir/model_init"
|
||||
move_module "inifrt" "$math_dir/model_init"
|
||||
move_module "inpdis" "$math_dir/model_init"
|
||||
move_module "change" "$math_dir/model_init"
|
||||
move_module "hedif" "$math_dir/model_init"
|
||||
move_module "chctab" "$math_dir/model_init"
|
||||
move_module "inifrs" "$math_dir/model_init"
|
||||
move_module "inifrt" "$math_dir/model_init"
|
||||
move module "dwnfr" "$math_dir/model_init"
|
||||
move_module "dwnfr0" "$math_dir/model_init"
|
||||
move_module "dwnfr1" "$math_dir/model_init"
|
||||
move_module "levset" "$math_dir/model_init"
|
||||
move module "levgrp" "$math_dir/model_init"
|
||||
move module "visini" "$math_dir/model_init"
|
||||
move_module "grcor" "$math_dir/model_init"
|
||||
move_module "rap" "$math_dir/model_init"
|
||||
# ============================================================
|
||||
# 18. io/ - 输入输出
|
||||
mkdir -p "$math_dir/io"
|
||||
move_module "output" "$math_dir/io"
|
||||
move_module "rdata" "$math_dir/io"
|
||||
move_module "rdatax" "$math_dir/io"
|
||||
move_module "readbf" "$math_dir/io"
|
||||
move_module "inkul" "$math_dir/io"
|
||||
move_module "timing" "$math_dir/io"
|
||||
move_module "getwrd" "$math_dir/io"
|
||||
move_module "quit" "$math_dir/io"
|
||||
move_module "prchan" "$math_dir/io"
|
||||
move_module "princ" "$math_dir/io"
|
||||
move module "prnt" "$math_dir/io"
|
||||
# ============================================================
|
||||
# 19. utils - 杂项工具
|
||||
mkdir -p "$math_dir/utils"
|
||||
move_module "pzert" "$math_dir/utils"
|
||||
move_module "pzevld" "$math_dir/utils"
|
||||
move module "corrwm" "$math_dir/utils"
|
||||
move_module "dwnfr" "$math_dir/utils"
|
||||
move_module "dwnfr0" "$math_dir/utils"
|
||||
move_module "dwnfr1" "$math_dir/utils"
|
||||
|
||||
# ============================================================
|
||||
# 20. Other modules - 杂项
|
||||
mkdir -p "$math_dir/utils"
|
||||
move_module "grcor" "$math_dir/utils"
|
||||
move_module "betah" "$math_dir/utils"
|
||||
move_module "coolrt" "$math_dir/utils"
|
||||
move module "rechck" "$math_dir/utils"
|
||||
move_module "russel" "$math_dir/utils"
|
||||
move_module "moleq" "$math_dir/utils"
|
||||
move_module "rhonen" "$math_dir/utils"
|
||||
move_module "rhoeos" "$math_dir/utils"
|
||||
move_module "radpre" "$math_dir/utils"
|
||||
move module "radtot" "$math_dir/utils"
|
||||
move module "raph" "$math_dir/utils"
|
||||
|
||||
move_module "brte" "$math_dir/utils"
|
||||
move_module "brtez" "$math_dir/utils"
|
||||
move_module "pzeval" "$math_dir/utils"
|
||||
move module "pzevld" "$math_dir/utils"
|
||||
|
||||
echo "模块 organized by function!"
|
||||
Executable
+180
@@ -0,0 +1,180 @@
|
||||
#!/bin/bash
|
||||
set -u
|
||||
|
||||
# --- 配置变量 ---
|
||||
WORK_DIR="/home/dckj/SpectraRust"
|
||||
CMD_PATH="/usr/bin/claude"
|
||||
CMD_PROMPT="使用 codegraph-guide skill 继续执行重构任务。"
|
||||
|
||||
# 状态文件
|
||||
PHASE_FILE="${WORK_DIR}/.f2r_phase"
|
||||
COMPLETE_FILE="${WORK_DIR}/.f2r_complete"
|
||||
RATE_LIMIT_FILE="${WORK_DIR}/.f2r_rate_limit" # 内容:退避到期 epoch 秒
|
||||
FAIL_COUNT_FILE="${WORK_DIR}/.f2r_fail_count" # 内容:连续失败次数
|
||||
TASKS_FILE="${WORK_DIR}/.f2r_tasks"
|
||||
LOCK_FILE="${WORK_DIR}/.f2r.lock"
|
||||
|
||||
# 退避参数(秒)
|
||||
BACKOFF_529=900 # 529 模型过载(临时性):15 分钟短退避
|
||||
BACKOFF_429_FALLBACK=3600 # 429 无法解析重置时间时:默认 1 小时
|
||||
BACKOFF_MODEL_ERR=1800 # 模型不存在:30 分钟
|
||||
BACKOFF_CIRCUIT=7200 # 连续失败触发熔断:2 小时
|
||||
MAX_CONSEC_FAIL=6 # 连续失败熔断阈值
|
||||
|
||||
# 日志(export TZ 确保子命令 / date 一致用 UTC+8)
|
||||
export TZ="Asia/Shanghai"
|
||||
LOG_FILE="${WORK_DIR}/logs/claude_$(date +%Y%m%d_%H%M%S).log"
|
||||
CRON_LOG="${WORK_DIR}/logs/cron.log"
|
||||
CRON_LOG_MAX=5242880 # cron.log 归档阈值:5MB
|
||||
|
||||
log() { echo "[$(date '+%F %T')] $*"; }
|
||||
|
||||
# --- 1. 环境检查 ---
|
||||
if [ ! -d "$WORK_DIR" ]; then
|
||||
log "❌ 错误: 工作目录不存在: $WORK_DIR"
|
||||
exit 1
|
||||
fi
|
||||
if [ ! -x "$CMD_PATH" ]; then
|
||||
log "❌ 错误: 命令不存在或不可执行: $CMD_PATH"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# --- 2. 完成检测 ---
|
||||
if [ -f "$COMPLETE_FILE" ]; then
|
||||
log "✅ 重构已标记为完成 ($(cat "$COMPLETE_FILE" 2>/dev/null)),跳过。如需重启请删除 ${COMPLETE_FILE}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# --- 3. 并发锁(flock,无竞态,替代 pgrep 检测)---
|
||||
exec 200>"$LOCK_FILE"
|
||||
if ! flock -n 200; then
|
||||
log "⚠️ 已有实例在运行,跳过。"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# --- 4. 峰时段(UTC+8 14:00~18:00)禁用执行 ---
|
||||
CURRENT_HOUR=$(date +%H)
|
||||
if [ "$CURRENT_HOUR" -ge 14 ] && [ "$CURRENT_HOUR" -lt 18 ]; then
|
||||
log "⏰ 高峰期 14:00~18:00 (UTC+8),跳过。"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# --- 5. 限流退避(epoch 秒)---
|
||||
if [ -f "$RATE_LIMIT_FILE" ]; then
|
||||
LIMIT_UNTIL=$(cat "$RATE_LIMIT_FILE" 2>/dev/null)
|
||||
NOW_EPOCH=$(date +%s)
|
||||
if [[ "$LIMIT_UNTIL" =~ ^[0-9]+$ ]] && [ "$NOW_EPOCH" -lt "$LIMIT_UNTIL" ]; then
|
||||
REMAINING=$(( (LIMIT_UNTIL - NOW_EPOCH) / 60 ))
|
||||
log "⏳ 退避中,还需 ${REMAINING} 分钟(至 $(date -d "@$LIMIT_UNTIL" '+%F %T')),跳过。"
|
||||
exit 0
|
||||
else
|
||||
rm -f "$RATE_LIMIT_FILE"
|
||||
log "🔓 退避已到期,继续执行。"
|
||||
fi
|
||||
fi
|
||||
|
||||
# --- 6. cron.log 轮转(超过阈值则归档,不删)---
|
||||
if [ -f "$CRON_LOG" ]; then
|
||||
CRON_SIZE=$(wc -c < "$CRON_LOG" 2>/dev/null || echo 0)
|
||||
if [ "${CRON_SIZE:-0}" -gt "$CRON_LOG_MAX" ]; then
|
||||
mv "$CRON_LOG" "${CRON_LOG}.$(date +%Y%m%d_%H%M%S).bak"
|
||||
log "📦 cron.log 超过 ${CRON_LOG_MAX}B,已归档。"
|
||||
fi
|
||||
fi
|
||||
|
||||
# --- 7. 启动 claude ---
|
||||
cd "$WORK_DIR" || { log "❌ 无法进入 ${WORK_DIR}"; exit 1; }
|
||||
|
||||
nohup "$CMD_PATH" --permission-mode bypassPermissions --print "$CMD_PROMPT" \
|
||||
< /dev/null > "$LOG_FILE" 2>&1 &
|
||||
CURRENT_PID=$!
|
||||
|
||||
# --print 同步,等待结束
|
||||
wait "$CURRENT_PID" 2>/dev/null
|
||||
EXIT_CODE=$?
|
||||
LOG_SIZE=$(wc -c < "$LOG_FILE" 2>/dev/null || echo 0)
|
||||
|
||||
# --- 8. 错误判定 + 退避 ---
|
||||
# 写入退避到期 epoch
|
||||
set_backoff() { # $1=秒 $2=原因
|
||||
local secs="$1" reason="$2"
|
||||
local until_epoch
|
||||
until_epoch=$(( $(date +%s) + secs ))
|
||||
echo "$until_epoch" > "$RATE_LIMIT_FILE"
|
||||
log "🔒 ${reason},退避 ${secs}s(至 $(date -d "@$until_epoch" '+%F %T'))。"
|
||||
}
|
||||
|
||||
# 连续失败计数 +1,超阈值熔断
|
||||
bump_fail() { # $1=原因
|
||||
local reason="$1" n
|
||||
n=$(cat "$FAIL_COUNT_FILE" 2>/dev/null || echo 0)
|
||||
n=$(( n + 1 ))
|
||||
echo "$n" > "$FAIL_COUNT_FILE"
|
||||
log "❌ 失败 #${n}:${reason} | 退出码 ${EXIT_CODE} | 日志 ${LOG_SIZE}B"
|
||||
log " 日志路径: ${LOG_FILE}"
|
||||
if [ "$n" -ge "$MAX_CONSEC_FAIL" ]; then
|
||||
set_backoff "$BACKOFF_CIRCUIT" "连续失败 ${n} 次触发熔断"
|
||||
echo 0 > "$FAIL_COUNT_FILE" # 熔断后清零,避免反复触发
|
||||
fi
|
||||
}
|
||||
|
||||
# 异常小/缺失日志:claude 未正常产出,直接计失败(避免被误判为成功)
|
||||
if [ "${LOG_SIZE:-0}" -le 50 ]; then
|
||||
bump_fail "日志异常小或缺失(${LOG_SIZE}B)"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# 识别错误类型(优先按日志特征,再按退出码)
|
||||
ERR_TYPE=""
|
||||
if grep -qE "限额将在|使用上限|429[^0-9]" "$LOG_FILE" 2>/dev/null; then
|
||||
ERR_TYPE="429"
|
||||
elif grep -q "529 \[" "$LOG_FILE" 2>/dev/null; then
|
||||
ERR_TYPE="529"
|
||||
elif grep -q "模型不存在" "$LOG_FILE" 2>/dev/null; then
|
||||
ERR_TYPE="model_err"
|
||||
elif [ "$EXIT_CODE" -ne 0 ]; then
|
||||
ERR_TYPE="exit_nonzero"
|
||||
fi
|
||||
|
||||
case "$ERR_TYPE" in
|
||||
429)
|
||||
# 用量上限:尽量解析重置时间,否则用默认长退避
|
||||
RESET_TIME=$(grep -oP '限额将在 \K[\d-]+ [\d:]+' "$LOG_FILE" 2>/dev/null | head -1)
|
||||
if [ -n "$RESET_TIME" ]; then
|
||||
RESET_EPOCH=$(date -d "$RESET_TIME" +%s 2>/dev/null)
|
||||
if [ -n "$RESET_EPOCH" ]; then
|
||||
echo "$RESET_EPOCH" > "$RATE_LIMIT_FILE"
|
||||
log "🔴 429 用量上限,退避至 $(date -d "@$RESET_EPOCH" '+%F %T')(重置于 ${RESET_TIME})。"
|
||||
else
|
||||
set_backoff "$BACKOFF_429_FALLBACK" "429 重置时间解析失败"
|
||||
fi
|
||||
else
|
||||
set_backoff "$BACKOFF_429_FALLBACK" "429 无重置时间"
|
||||
fi
|
||||
bump_fail "429 用量上限"
|
||||
;;
|
||||
529)
|
||||
# 模型过载:临时性,短退避(区别于 429 的长退避)
|
||||
set_backoff "$BACKOFF_529" "529 模型过载"
|
||||
bump_fail "529 模型过载"
|
||||
;;
|
||||
model_err)
|
||||
set_backoff "$BACKOFF_MODEL_ERR" "模型不存在"
|
||||
bump_fail "模型不存在"
|
||||
;;
|
||||
exit_nonzero)
|
||||
bump_fail "claude 非零退出"
|
||||
;;
|
||||
*)
|
||||
# 真成功:清零失败计数
|
||||
echo 0 > "$FAIL_COUNT_FILE"
|
||||
log "✅ 会话完成 | PID ${CURRENT_PID} | 退出码 ${EXIT_CODE} | 日志 ${LOG_SIZE}B"
|
||||
log " 日志路径: ${LOG_FILE}"
|
||||
# 若本次创建了完成标记,提示一下
|
||||
if [ -f "$COMPLETE_FILE" ]; then
|
||||
log "🎯 检测到 ${COMPLETE_FILE},重构已完成。"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,19 @@
|
||||
//! SYNSPEC 可执行程序入口。
|
||||
//!
|
||||
//! 用法:
|
||||
//! synspec < input.5 > output.6
|
||||
|
||||
use tlusty_rust::synspec::runner::{run_synspec, SynspecConfig};
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let config = SynspecConfig::default();
|
||||
let success = run_synspec(config);
|
||||
|
||||
if success {
|
||||
eprintln!("SYNSPEC completed successfully.");
|
||||
} else {
|
||||
eprintln!("SYNSPEC completed with errors.");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
//! TLUSTY 可执行程序入口。
|
||||
//!
|
||||
//! 用法:
|
||||
//! tlusty < input.5 > output.6
|
||||
|
||||
use std::io::{self, BufReader};
|
||||
|
||||
use tlusty_rust::tlusty::{run_tlusty, TlustyConfig};
|
||||
use tlusty_rust::tlusty::io::{FortranReader, FortranWriter};
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let mut config = TlustyConfig::default();
|
||||
let mut input_reader = FortranReader::new(BufReader::new(io::stdin()));
|
||||
let mut output_writer = FortranWriter::new(io::stdout());
|
||||
|
||||
let result = run_tlusty(&mut config, &mut input_reader, &mut output_writer);
|
||||
|
||||
if result.converged {
|
||||
eprintln!("Converged after {} iterations ({:.2}s)",
|
||||
result.total_iterations, result.total_time_secs);
|
||||
} else {
|
||||
eprintln!("Did NOT converge after {} iterations ({:.2}s)",
|
||||
result.total_iterations, result.total_time_secs);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
//! ABNCHN 丰度修改过程。
|
||||
//!
|
||||
//! 重构自 SYNSPEC `ABNCHN` 函数。
|
||||
//!
|
||||
//! 用于 opacity table 评估时修改(或消除)某些元素的丰度。
|
||||
|
||||
/// ABNCHN 模式。
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum AbnchnMode {
|
||||
/// 保存当前 populations 到备份
|
||||
Save = 0,
|
||||
/// 按丰度因子缩放 populations
|
||||
Scale = 1,
|
||||
}
|
||||
|
||||
/// ABNCHN 输入参数。
|
||||
pub struct AbnchnParams<'a> {
|
||||
/// 操作模式
|
||||
pub mode: AbnchnMode,
|
||||
/// 原子数
|
||||
pub natom: usize,
|
||||
/// 每个原子的第一能级索引 (0-based)
|
||||
pub n0a: &'a [usize],
|
||||
/// 每个原子的最后能级索引 (0-based)
|
||||
pub nka: &'a [usize],
|
||||
/// 每个原子对应的原子序数 (1-based)
|
||||
pub numat: &'a [usize],
|
||||
/// 丰度缩放因子 (按原子序数索引, 1-based)
|
||||
pub relabn: &'a [f64],
|
||||
/// 当前 populations [nlevel]
|
||||
pub popul: &'a [f64],
|
||||
/// 备份 populations [nlevel] (mode=0 时写入, mode=1 时读取)
|
||||
pub popul0: &'a [f64],
|
||||
/// RRR 数组 [mion × matom]
|
||||
pub rrr: &'a [f64],
|
||||
/// 离子数
|
||||
pub mion0: usize,
|
||||
/// 原子种类数 (最大)
|
||||
pub matom: usize,
|
||||
}
|
||||
|
||||
/// ABNCHN 输出结果。
|
||||
pub struct AbnchnOutput {
|
||||
/// 修改后的 populations [nlevel]
|
||||
pub popul_new: Vec<f64>,
|
||||
/// 修改后的 RRR 数组 [mion × matom]
|
||||
pub rrr_new: Vec<f64>,
|
||||
/// 更新后的备份 populations [nlevel]
|
||||
pub popul0_new: Vec<f64>,
|
||||
}
|
||||
|
||||
/// ABNCHN 丰度修改过程。
|
||||
///
|
||||
/// mode=0: 保存当前 populations 到备份。
|
||||
/// mode=1: 按丰度因子缩放 populations 和 RRR。
|
||||
///
|
||||
/// # 参数
|
||||
///
|
||||
/// * `params` - ABNCHN 参数
|
||||
///
|
||||
/// # 返回值
|
||||
///
|
||||
/// 修改后的 populations 和 RRR
|
||||
pub fn abnchn(params: &AbnchnParams) -> AbnchnOutput {
|
||||
let AbnchnParams {
|
||||
mode,
|
||||
natom,
|
||||
n0a,
|
||||
nka,
|
||||
numat,
|
||||
relabn,
|
||||
popul,
|
||||
popul0,
|
||||
rrr,
|
||||
mion0,
|
||||
matom,
|
||||
} = *params;
|
||||
|
||||
let _nlevel = popul.len();
|
||||
let mut popul_new = popul.to_vec();
|
||||
let mut popul0_new = popul0.to_vec();
|
||||
let mut rrr_new = rrr.to_vec();
|
||||
|
||||
match mode {
|
||||
AbnchnMode::Save => {
|
||||
// 保存当前 populations 到备份
|
||||
for iat in 0..natom {
|
||||
for ii in n0a[iat]..=nka[iat] {
|
||||
popul0_new[ii] = popul[ii];
|
||||
}
|
||||
}
|
||||
}
|
||||
AbnchnMode::Scale => {
|
||||
// 按丰度因子缩放 populations
|
||||
for iat in 0..natom {
|
||||
let ia = numat[iat] - 1; // 0-based
|
||||
for ii in n0a[iat]..=nka[iat] {
|
||||
popul_new[ii] = popul0[ii] * relabn[ia];
|
||||
}
|
||||
}
|
||||
|
||||
// 缩放 RRR 数组
|
||||
for ia in 0..matom {
|
||||
for io in 0..mion0 {
|
||||
let idx = io * matom + ia;
|
||||
rrr_new[idx] = rrr[idx] * relabn[ia];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AbnchnOutput {
|
||||
popul_new,
|
||||
rrr_new,
|
||||
popul0_new,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_abnchn_save() {
|
||||
let nlevel = 6;
|
||||
let popul = vec![1.0e10, 2.0e10, 3.0e10, 4.0e10, 5.0e10, 6.0e10];
|
||||
let popul0 = vec![0.0; nlevel];
|
||||
let rrr = vec![1.0; 4];
|
||||
let n0a = vec![0usize, 3];
|
||||
let nka = vec![2usize, 5];
|
||||
let numat = vec![1usize, 2];
|
||||
let relabn = vec![1.0, 0.5, 0.3]; // index 0 unused, 1=H, 2=He
|
||||
|
||||
let params = AbnchnParams {
|
||||
mode: AbnchnMode::Save,
|
||||
natom: 2,
|
||||
n0a: &n0a,
|
||||
nka: &nka,
|
||||
numat: &numat,
|
||||
relabn: &relabn,
|
||||
popul: &popul,
|
||||
popul0: &popul0,
|
||||
rrr: &rrr,
|
||||
mion0: 2,
|
||||
matom: 3,
|
||||
};
|
||||
|
||||
let output = abnchn(¶ms);
|
||||
|
||||
// mode=0: 复制 popul 到 popul0
|
||||
assert_eq!(output.popul0_new, popul);
|
||||
// popul 不变
|
||||
assert_eq!(output.popul_new, popul);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_abnchn_scale() {
|
||||
let nlevel = 6;
|
||||
let popul = vec![1.0e10, 2.0e10, 3.0e10, 4.0e10, 5.0e10, 6.0e10];
|
||||
let popul0 = vec![1.0e10, 2.0e10, 3.0e10, 4.0e10, 5.0e10, 6.0e10];
|
||||
let rrr = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]; // mion0=2, matom=3
|
||||
let n0a = vec![0usize, 3];
|
||||
let nka = vec![2usize, 5];
|
||||
let numat = vec![1usize, 2];
|
||||
let relabn = vec![1.0, 0.5, 0.3]; // H=0.5, He=0.3
|
||||
|
||||
let params = AbnchnParams {
|
||||
mode: AbnchnMode::Scale,
|
||||
natom: 2,
|
||||
n0a: &n0a,
|
||||
nka: &nka,
|
||||
numat: &numat,
|
||||
relabn: &relabn,
|
||||
popul: &popul,
|
||||
popul0: &popul0,
|
||||
rrr: &rrr,
|
||||
mion0: 2,
|
||||
matom: 3,
|
||||
};
|
||||
|
||||
let output = abnchn(¶ms);
|
||||
|
||||
// mode=1: popul = popul0 * relabn[ia]
|
||||
// atom 0 (H, numat=1): ia=0, relabn[0]=1.0 → 不变
|
||||
assert_eq!(output.popul_new[0], 1.0e10 * 1.0);
|
||||
assert_eq!(output.popul_new[1], 2.0e10 * 1.0);
|
||||
assert_eq!(output.popul_new[2], 3.0e10 * 1.0);
|
||||
// atom 1 (He, numat=2): ia=1, relabn[1]=0.5
|
||||
assert_eq!(output.popul_new[3], 4.0e10 * 0.5);
|
||||
assert_eq!(output.popul_new[4], 5.0e10 * 0.5);
|
||||
assert_eq!(output.popul_new[5], 6.0e10 * 0.5);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,307 @@
|
||||
//! Quasi-molecular opacity for Lyman alpha, beta, gamma, and Balmer alpha.
|
||||
//!
|
||||
//! Translated from SYNSPEC `allard` subroutine (synspec54.f).
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// Constants
|
||||
// ============================================================================
|
||||
|
||||
/// Maximum number of wavelength points in tables
|
||||
pub const NXMAX: usize = 1400;
|
||||
|
||||
/// Maximum number of density components
|
||||
pub const NNMAX: usize = 5;
|
||||
|
||||
// Normalization constants: 8.8528e-29 * lambda_0^2 * f_ij
|
||||
const XNORMA: f64 = 8.8528e-29 * 1215.6 * 1215.6 * 0.41618; // Lyman alpha
|
||||
const XNORMB: f64 = 8.8528e-29 * 1025.73 * 1025.7 * 0.0791; // Lyman beta
|
||||
const XNORMG: f64 = 8.8528e-29 * 972.53 * 972.53 * 0.0290; // Lyman gamma
|
||||
const XNORMC: f64 = 8.8528e-29 * 6562.0 * 6562.0 * 0.6407; // Balmer alpha
|
||||
|
||||
// ============================================================================
|
||||
// AllardData - precomputed table data
|
||||
// ============================================================================
|
||||
|
||||
/// Precomputed quasi-molecular opacity tables for one transition.
|
||||
///
|
||||
/// Corresponds to Fortran COMMON blocks `callarda`, `callardb`, `callardg`, `callardc`.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AllardTable {
|
||||
/// Wavelength points (Angstroms)
|
||||
pub xl: Vec<f64>,
|
||||
/// Profile data: `pl[i][j]` for wavelength point `i`, component `j`
|
||||
/// Components: 0=neutral linear, 1=neutral quadratic,
|
||||
/// 2=charged linear, 3=charged quadratic, 4=cross term
|
||||
pub pl: Vec<[f64; NNMAX]>,
|
||||
/// Normalized neutral density scale
|
||||
pub stnne: f64,
|
||||
/// Normalized charged density scale
|
||||
pub stnch: f64,
|
||||
/// Neutral velocity scale
|
||||
pub vneu: f64,
|
||||
/// Charged velocity scale
|
||||
pub vcha: f64,
|
||||
/// Number of wavelength points
|
||||
pub nx: usize,
|
||||
/// Warning flag for high density
|
||||
pub iwarn: bool,
|
||||
}
|
||||
|
||||
impl Default for AllardTable {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
xl: Vec::new(),
|
||||
pl: Vec::new(),
|
||||
stnne: 1.0,
|
||||
stnch: 1.0,
|
||||
vneu: 1.0,
|
||||
vcha: 1.0,
|
||||
nx: 0,
|
||||
iwarn: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// AllardData - all four transitions
|
||||
// ============================================================================
|
||||
|
||||
/// Container for all four quasi-molecular transitions.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct AllardData {
|
||||
/// Lyman alpha (1→2)
|
||||
pub lalp: AllardTable,
|
||||
/// Lyman beta (1→3)
|
||||
pub bet: AllardTable,
|
||||
/// Lyman gamma (1→4)
|
||||
pub gam: AllardTable,
|
||||
/// Balmer alpha (2→3)
|
||||
pub bal: AllardTable,
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Core interpolation function
|
||||
// ============================================================================
|
||||
|
||||
/// Interpolate quasi-molecular profile from precomputed table.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `table` - Precomputed table for this transition
|
||||
/// * `xl` - Wavelength in Angstroms
|
||||
/// * `hneutr` - Neutral H particle density [cm⁻³]
|
||||
/// * `hcharg` - Ionized H particle density [cm⁻³]
|
||||
///
|
||||
/// # Returns
|
||||
/// Profile value normalized to 1.0e8 when integrated over Angstroms.
|
||||
/// Returns 0.0 if wavelength is outside table range.
|
||||
fn interpolate_profile(
|
||||
table: &AllardTable,
|
||||
xl: f64,
|
||||
hneutr: f64,
|
||||
hcharg: f64,
|
||||
xnorm: f64,
|
||||
) -> f64 {
|
||||
if table.nx == 0 {
|
||||
return 0.0;
|
||||
}
|
||||
if xl < table.xl[0] || xl > table.xl[table.nx - 1] {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
// Normalized densities
|
||||
let vn1 = hneutr / table.stnne;
|
||||
let vn2 = hcharg / table.stnch;
|
||||
let vns = vn1 * table.vneu + vn2 * table.vcha;
|
||||
|
||||
// Density warning
|
||||
// (handled externally via iwarn flag)
|
||||
|
||||
let vn11 = vn1 * vn1;
|
||||
let vn22 = vn2 * vn2;
|
||||
let vn12 = vn1 * vn2;
|
||||
let xnorm_fac = 1.0 / (1.0 + vns + 0.5 * vns * vns);
|
||||
|
||||
// Binary search for wavelength interval
|
||||
let mut jl: usize = 0;
|
||||
let mut ju = table.nx;
|
||||
while ju - jl > 1 {
|
||||
let jm = (ju + jl) / 2;
|
||||
if (table.xl[table.nx - 1] > table.xl[0]) == (xl > table.xl[jm]) {
|
||||
jl = jm;
|
||||
} else {
|
||||
ju = jm;
|
||||
}
|
||||
}
|
||||
let mut j = jl;
|
||||
if j == 0 {
|
||||
j = 1;
|
||||
}
|
||||
if j >= table.nx - 1 {
|
||||
j = table.nx - 2;
|
||||
}
|
||||
|
||||
// Linear interpolation factor
|
||||
let a1 = (xl - table.xl[j]) / (table.xl[j + 1] - table.xl[j]);
|
||||
let a0 = 1.0 - a1;
|
||||
|
||||
// Interpolate each density component
|
||||
let p1 = vn1 * (a0 * table.pl[j][0] + a1 * table.pl[j + 1][0]);
|
||||
let p11 = vn11 * (a0 * table.pl[j][1] + a1 * table.pl[j + 1][1]);
|
||||
let p2 = vn2 * (a0 * table.pl[j][2] + a1 * table.pl[j + 1][2]);
|
||||
let p22 = vn22 * (a0 * table.pl[j][3] + a1 * table.pl[j + 1][3]);
|
||||
let p12 = vn12 * (a0 * table.pl[j][4] + a1 * table.pl[j + 1][4]);
|
||||
|
||||
(p1 + p2 + p11 + p22 + p12) * xnorm_fac * xnorm
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Main entry point
|
||||
// ============================================================================
|
||||
|
||||
/// Compute quasi-molecular opacity profile.
|
||||
///
|
||||
/// Translated from SYNSPEC `allard` subroutine (synspec54.f).
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `data` - Precomputed quasi-molecular tables
|
||||
/// * `xl` - Wavelength in Angstroms
|
||||
/// * `hneutr` - Neutral H particle density [cm⁻³]
|
||||
/// * `hcharg` - Ionized H particle density [cm⁻³]
|
||||
/// * `iq` - Quantum number of lower level
|
||||
/// * `jq` - Quantum number of upper level:
|
||||
/// - 2 → Lyman alpha
|
||||
/// - 3 → Lyman beta (if iq=1) or Balmer alpha (if iq=2)
|
||||
/// - 4 → Lyman gamma
|
||||
///
|
||||
/// # Returns
|
||||
/// Profile value. Returns 0.0 if transition not recognized or out of range.
|
||||
pub fn allard(
|
||||
data: &AllardData,
|
||||
xl: f64,
|
||||
hneutr: f64,
|
||||
hcharg: f64,
|
||||
iq: i32,
|
||||
jq: i32,
|
||||
) -> f64 {
|
||||
// Lyman alpha (1→2)
|
||||
if iq == 1 && jq == 2 {
|
||||
return interpolate_profile(&data.lalp, xl, hneutr, hcharg, XNORMA);
|
||||
}
|
||||
|
||||
// Lyman beta (1→3)
|
||||
if iq == 1 && jq == 3 {
|
||||
return interpolate_profile(&data.bet, xl, hneutr, hcharg, XNORMB);
|
||||
}
|
||||
|
||||
// Lyman gamma (1→4)
|
||||
if iq == 1 && jq == 4 {
|
||||
return interpolate_profile(&data.gam, xl, hneutr, hcharg, XNORMG);
|
||||
}
|
||||
|
||||
// Balmer alpha (2→3)
|
||||
if iq == 2 && jq == 3 {
|
||||
// For Balmer alpha, only charged component contributes
|
||||
// (vn1 = 0 in Fortran code)
|
||||
if data.bal.nx == 0 {
|
||||
return 0.0;
|
||||
}
|
||||
if xl < data.bal.xl[0] || xl > data.bal.xl[data.bal.nx - 1] {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
let vn2 = hcharg / data.bal.stnch;
|
||||
let vns = vn2 * data.bal.vcha;
|
||||
let vn22 = vn2 * vn2;
|
||||
let xnorm_fac = 1.0 / (1.0 + vns + 0.5 * vns * vns);
|
||||
|
||||
// Binary search
|
||||
let mut jl: usize = 0;
|
||||
let mut ju = data.bal.nx;
|
||||
while ju - jl > 1 {
|
||||
let jm = (ju + jl) / 2;
|
||||
if (data.bal.xl[data.bal.nx - 1] > data.bal.xl[0])
|
||||
== (xl > data.bal.xl[jm])
|
||||
{
|
||||
jl = jm;
|
||||
} else {
|
||||
ju = jm;
|
||||
}
|
||||
}
|
||||
let mut j = jl;
|
||||
if j == 0 {
|
||||
j = 1;
|
||||
}
|
||||
if j >= data.bal.nx - 1 {
|
||||
j = data.bal.nx - 2;
|
||||
}
|
||||
|
||||
let a1 = (xl - data.bal.xl[j]) / (data.bal.xl[j + 1] - data.bal.xl[j]);
|
||||
let a0 = 1.0 - a1;
|
||||
|
||||
let p2 = vn2 * (a0 * data.bal.pl[j][2] + a1 * data.bal.pl[j + 1][2]);
|
||||
let p22 = vn22 * (a0 * data.bal.pl[j][3] + a1 * data.bal.pl[j + 1][3]);
|
||||
|
||||
return (p2 + p22) * xnorm_fac * XNORMC;
|
||||
}
|
||||
|
||||
0.0
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Tests
|
||||
// ============================================================================
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_allard_empty_table() {
|
||||
let data = AllardData::default();
|
||||
let prof = allard(&data, 1215.6, 1e12, 1e10, 1, 2);
|
||||
assert_eq!(prof, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_allard_out_of_range() {
|
||||
let mut data = AllardData::default();
|
||||
data.lalp.xl = vec![1200.0, 1210.0, 1220.0];
|
||||
data.lalp.pl = vec![[1.0; NNMAX]; 3];
|
||||
data.lalp.nx = 3;
|
||||
|
||||
// Below range
|
||||
let prof = allard(&data, 1199.0, 1e12, 1e10, 1, 2);
|
||||
assert_eq!(prof, 0.0);
|
||||
|
||||
// Above range
|
||||
let prof = allard(&data, 1221.0, 1e12, 1e10, 1, 2);
|
||||
assert_eq!(prof, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_allard_lyman_alpha() {
|
||||
let mut data = AllardData::default();
|
||||
data.lalp.xl = vec![1210.0, 1215.0, 1220.0];
|
||||
data.lalp.pl = vec![
|
||||
[1.0, 0.5, 0.3, 0.2, 0.1],
|
||||
[2.0, 1.0, 0.6, 0.4, 0.2],
|
||||
[1.5, 0.75, 0.45, 0.3, 0.15],
|
||||
];
|
||||
data.lalp.stnne = 1e12;
|
||||
data.lalp.stnch = 1e10;
|
||||
data.lalp.vneu = 1.0;
|
||||
data.lalp.vcha = 1.0;
|
||||
data.lalp.nx = 3;
|
||||
|
||||
let prof = allard(&data, 1215.0, 1e12, 1e10, 1, 2);
|
||||
assert!(prof > 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_allard_unknown_transition() {
|
||||
let data = AllardData::default();
|
||||
let prof = allard(&data, 1215.6, 1e12, 1e10, 2, 4); // Not a valid transition
|
||||
assert_eq!(prof, 0.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
//! 中性碳光致电离截面(Taylor 数据)。
|
||||
//!
|
||||
//! 重构自 SYNSPEC `carbon.f`
|
||||
//!
|
||||
//! 使用 G.B. Taylor (private communication) 的数据,
|
||||
//! 计算中性碳 2p¹D 和 2p¹S 能级的光致电离截面。
|
||||
|
||||
/// 频率网格 2 (单位 FR0),用于 IB=-602 (2p¹D)
|
||||
const FR2: [f64; 34] = [
|
||||
0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.80, 0.81, 0.82, 0.83,
|
||||
0.85, 0.86, 0.87, 0.88, 0.89, 0.90, 0.91, 0.92, 0.93, 0.94,
|
||||
0.95, 0.96, 0.97, 0.98, 0.99, 1.00, 1.10, 1.20, 1.30, 1.45,
|
||||
1.50, 1.60, 1.80, 2.00,
|
||||
];
|
||||
|
||||
/// 截面数据 2 (Mbarn),用于 IB=-602 (2p¹D)
|
||||
const SG2: [f64; 34] = [
|
||||
12.04, 12.03, 12.09, 12.26, 12.60, 13.24, 14.36, 16.24, 19.28, 23.94,
|
||||
37.41, 42.88, 44.76, 43.41, 40.46, 37.19, 34.26, 31.82, 29.96, 28.57,
|
||||
27.68, 27.37, 27.84, 29.69, 34.45, 46.35, 13.80, 11.54, 10.40, 8.96,
|
||||
8.54, 7.47, 6.53, 5.66,
|
||||
];
|
||||
|
||||
/// 频率网格 3 (单位 FR0),用于 IB=-603 (2p¹S)
|
||||
const FR3: [f64; 45] = [
|
||||
0.66, 0.68, 0.70, 0.72, 0.74, 0.76, 0.78, 0.80, 0.82, 0.84,
|
||||
0.86, 0.864, 0.866, 0.868, 0.87, 0.874, 0.876, 0.88, 0.882, 0.884,
|
||||
0.886, 0.888, 0.89, 0.894, 0.896, 0.898, 0.90, 0.904, 0.908, 0.910,
|
||||
0.920, 0.94, 0.98, 1.00, 1.10, 1.20, 1.26, 1.34, 1.36, 1.40,
|
||||
1.46, 1.60, 1.70, 1.80, 2.00,
|
||||
];
|
||||
|
||||
/// 截面数据 3 (Mbarn),用于 IB=-603 (2p¹S)
|
||||
const SG3: [f64; 45] = [
|
||||
13.94, 13.29, 12.56, 11.73, 10.82, 10.18, 8.62, 7.27, 5.74, 4.14,
|
||||
4.61, 5.92, 6.94, 8.34, 10.21, 16.12, 20.64, 34.56, 44.82, 57.71,
|
||||
73.09, 89.99, 106.38, 127.08, 128.38, 124.44, 117.17, 99.32, 82.95, 76.05,
|
||||
52.65, 33.23, 21.29, 18.69, 12.62, 11.44, 9.77, 7.53, 10.47, 9.65,
|
||||
10.19, 7.28, 6.70, 6.11, 4.96,
|
||||
];
|
||||
|
||||
/// 参考频率 (Hz)
|
||||
const FR0: f64 = 3.28805e15;
|
||||
|
||||
/// 截面单位转换因子 (cm^2)
|
||||
const SIG_FACTOR: f64 = 1.0e-18;
|
||||
|
||||
/// 中性碳光致电离截面。
|
||||
///
|
||||
/// 根据 Taylor 数据,对给定频率进行线性插值。
|
||||
///
|
||||
/// # 参数
|
||||
///
|
||||
/// * `ib` - 能级标识(-602 = 2p¹D, -603 = 2p¹S)
|
||||
/// * `fr` - 频率 (Hz)
|
||||
///
|
||||
/// # 返回值
|
||||
///
|
||||
/// 光致电离截面 (cm^2)
|
||||
pub fn carbon(ib: i32, fr: f64) -> f64 {
|
||||
let f = fr / FR0;
|
||||
|
||||
if ib == -602 {
|
||||
// 2p¹D 能级
|
||||
let mut j = 1; // 0-indexed, 默认值
|
||||
if f > FR2[0] {
|
||||
for i in 1..34 {
|
||||
if f > FR2[i - 1] && f <= FR2[i] {
|
||||
j = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
j = 1;
|
||||
}
|
||||
let sg = (f - FR2[j - 1]) / (FR2[j] - FR2[j - 1]) * (SG2[j] - SG2[j - 1]) + SG2[j - 1];
|
||||
return sg * SIG_FACTOR;
|
||||
}
|
||||
|
||||
if ib == -603 {
|
||||
// 2p¹S 能级
|
||||
let mut j = 1;
|
||||
if f > FR3[0] {
|
||||
for i in 1..45 {
|
||||
if f > FR3[i - 1] && f <= FR3[i] {
|
||||
j = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
j = 1;
|
||||
}
|
||||
let sg = (f - FR3[j - 1]) / (FR3[j] - FR3[j - 1]) * (SG3[j] - SG3[j - 1]) + SG3[j - 1];
|
||||
return sg * SIG_FACTOR;
|
||||
}
|
||||
|
||||
0.0
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_carbon_602_in_range() {
|
||||
// 2p¹D 在有效频率范围内
|
||||
let fr = 0.9 * FR0;
|
||||
let result = carbon(-602, fr);
|
||||
assert!(result > 0.0);
|
||||
assert!(result.is_finite());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_carbon_603_in_range() {
|
||||
// 2p¹S 在有效频率范围内
|
||||
let fr = 0.85 * FR0;
|
||||
let result = carbon(-603, fr);
|
||||
assert!(result > 0.0);
|
||||
assert!(result.is_finite());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_carbon_below_range() {
|
||||
let fr = 0.5 * FR0;
|
||||
let result = carbon(-602, fr);
|
||||
assert!(result >= 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_carbon_above_range() {
|
||||
let fr = 3.0 * FR0;
|
||||
let result = carbon(-602, fr);
|
||||
assert!(result >= 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_carbon_invalid_ib() {
|
||||
let result = carbon(-601, FR0);
|
||||
assert_eq!(result, 0.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
//! CHANGE 控制过程。
|
||||
//!
|
||||
//! 重构自 SYNSPEC `CHANGE` 函数。
|
||||
//!
|
||||
//! 在显式能级系统与输入能级编号不一致时,重新评估初始能级 populations。
|
||||
//! 仅用于 NLTE 输入模型。
|
||||
|
||||
use crate::synspec::state::constants::BOLK;
|
||||
use crate::synspec::math::{lineqs, ratmat};
|
||||
|
||||
/// CHANGE 模式参数(每个能级一组)。
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ChangeLevelParams {
|
||||
/// 旧能级索引 (1-based);0 = 无对应旧能级
|
||||
pub iold: usize,
|
||||
/// 评估模式
|
||||
/// - 0: 复制旧能级 population × REL
|
||||
/// - 1: LTE 相对于下一电离态
|
||||
/// - 2: b-因子匹配
|
||||
/// - 3: 完整 LTE(SABOLF + RATMAT + LINEQS)
|
||||
pub mode: usize,
|
||||
/// 下一电离态旧索引 (1-based)
|
||||
pub nxtold: usize,
|
||||
/// 新系统中参考能级索引 (1-based)
|
||||
pub isinew: usize,
|
||||
/// 旧系统中参考能级索引 (1-based)
|
||||
pub isiold: usize,
|
||||
/// 参考能级下一电离态旧索引 (1-based)
|
||||
pub nxtsio: usize,
|
||||
/// population 乘子
|
||||
pub rel: f64,
|
||||
}
|
||||
|
||||
/// CHANGE 输入参数。
|
||||
pub struct ChangeParams<'a> {
|
||||
/// 每能级参数
|
||||
pub levels: &'a [ChangeLevelParams],
|
||||
/// 深度点数
|
||||
pub nd: usize,
|
||||
/// 温度数组 (K)
|
||||
pub temp: &'a [f64],
|
||||
/// 电子密度数组 (cm^-3)
|
||||
pub elec: &'a [f64],
|
||||
/// 当前 populations [nlevel × nd, row-major]
|
||||
pub popul: &'a [f64],
|
||||
/// 统计权重
|
||||
pub g: &'a [f64],
|
||||
/// 电离能 (K)
|
||||
pub enion: &'a [f64],
|
||||
/// 元素索引 (1-based)
|
||||
pub iel: &'a [usize],
|
||||
/// 下一离子态索引 (1-based)
|
||||
pub nnext: &'a [usize],
|
||||
/// 能级数
|
||||
pub nlevel: usize,
|
||||
/// N0 偏移数组 (1-based)
|
||||
pub n0a: &'a [usize],
|
||||
/// NK 偏移数组 (1-based)
|
||||
pub nka: &'a [usize],
|
||||
/// SBF 数组
|
||||
pub sbf: &'a [f64],
|
||||
/// WOP 数组 (nlevel × nd, row-major)
|
||||
pub wop: &'a [f64],
|
||||
/// ILK 数组 (1-based)
|
||||
pub ilk: &'a [usize],
|
||||
/// USUM 数组 (1-based)
|
||||
pub usum: &'a [f64],
|
||||
/// ATTOT 数组
|
||||
pub attot: &'a [f64],
|
||||
}
|
||||
|
||||
/// CHANGE 输出结果。
|
||||
pub struct ChangeOutput {
|
||||
/// 新 populations [nlevel × nd, row-major]
|
||||
pub popul_new: Vec<f64>,
|
||||
/// 能级数
|
||||
pub nlevel: usize,
|
||||
/// 深度点数
|
||||
pub nd: usize,
|
||||
}
|
||||
|
||||
/// S = 2*h/c^2 * (1e-8)^2 = 2.0706e-16 (转换因子)
|
||||
const S: f64 = 2.0706e-16;
|
||||
|
||||
/// 获取 popul[level][depth] 的辅助函数。
|
||||
#[inline]
|
||||
fn popul_at(popul: &[f64], nd: usize, level: usize, depth: usize) -> f64 {
|
||||
popul[level * nd + depth]
|
||||
}
|
||||
|
||||
/// CHANGE 控制过程。
|
||||
///
|
||||
/// 在显式能级系统与输入能级编号不一致时,重新评估初始能级 populations。
|
||||
///
|
||||
/// # 参数
|
||||
///
|
||||
/// * `params` - CHANGE 参数
|
||||
///
|
||||
/// # 返回值
|
||||
///
|
||||
/// 新的能级 populations
|
||||
pub fn change(params: &ChangeParams) -> ChangeOutput {
|
||||
let ChangeParams {
|
||||
levels,
|
||||
nd,
|
||||
temp,
|
||||
elec,
|
||||
popul,
|
||||
g,
|
||||
enion,
|
||||
iel,
|
||||
nnext,
|
||||
nlevel,
|
||||
n0a,
|
||||
nka,
|
||||
sbf,
|
||||
wop,
|
||||
ilk,
|
||||
usum,
|
||||
attot,
|
||||
} = *params;
|
||||
|
||||
let mut popul_new = vec![0.0f64; nlevel * nd];
|
||||
let mut ifese = 0usize;
|
||||
|
||||
for (ii, lvl) in levels.iter().enumerate() {
|
||||
let iold = lvl.iold;
|
||||
let mode = lvl.mode;
|
||||
let nxtold = lvl.nxtold;
|
||||
let isinew = lvl.isinew;
|
||||
let isiold = lvl.isiold;
|
||||
let nxtsio = lvl.nxtsio;
|
||||
let mut rel = lvl.rel;
|
||||
if rel == 0.0 {
|
||||
rel = 1.0;
|
||||
}
|
||||
|
||||
if mode >= 3 {
|
||||
ifese += 1;
|
||||
}
|
||||
|
||||
for id in 0..nd {
|
||||
if iold != 0 {
|
||||
// 直接复制旧能级 population
|
||||
popul_new[ii * nd + id] = popul_at(popul, nd, iold - 1, id);
|
||||
continue;
|
||||
}
|
||||
|
||||
match mode {
|
||||
0 => {
|
||||
// 复制旧能级 population × REL
|
||||
popul_new[ii * nd + id] = popul_at(popul, nd, isiold - 1, id) * rel;
|
||||
}
|
||||
1 => {
|
||||
// LTE 相对于下一电离态
|
||||
let t = temp[id];
|
||||
let ane = elec[id];
|
||||
let nxt_idx = nnext[iel[ii] - 1] - 1;
|
||||
let sb = S / t / t.sqrt() * g[ii] / g[nxt_idx]
|
||||
* (enion[ii] / t / BOLK).exp();
|
||||
popul_new[ii * nd + id] = sb * ane * popul_at(popul, nd, nxtold - 1, id) * rel;
|
||||
}
|
||||
2 => {
|
||||
// b-因子匹配
|
||||
let t = temp[id];
|
||||
let kk = isinew - 1; // 0-based
|
||||
let k_next = nnext[iel[kk] - 1] - 1;
|
||||
let nxt_idx = nnext[iel[ii] - 1] - 1;
|
||||
let sb = S / t / t.sqrt() * g[ii] / g[nxt_idx]
|
||||
* (enion[ii] / t / BOLK).exp();
|
||||
let sbk = S / t / t.sqrt() * g[kk] / g[k_next]
|
||||
* (enion[kk] / t / BOLK).exp();
|
||||
popul_new[ii * nd + id] = sb / sbk
|
||||
* popul_at(popul, nd, nxtold - 1, id)
|
||||
/ popul_at(popul, nd, nxtsio - 1, id)
|
||||
* popul_at(popul, nd, isiold - 1, id)
|
||||
* rel;
|
||||
}
|
||||
_ => {
|
||||
// MODE >= 3: 完整 LTE via RATMAT + LINEQS
|
||||
if ifese == 1 {
|
||||
let ane = elec[id];
|
||||
let (ese_mat, bese) = ratmat(
|
||||
ane,
|
||||
nlevel,
|
||||
0,
|
||||
n0a,
|
||||
nka,
|
||||
nnext,
|
||||
iel,
|
||||
sbf,
|
||||
wop,
|
||||
nd,
|
||||
id,
|
||||
ilk,
|
||||
usum,
|
||||
attot,
|
||||
);
|
||||
// 解线性方程组
|
||||
let mut a = ese_mat;
|
||||
let mut b = bese;
|
||||
let poplte = lineqs(&mut a, &mut b, nlevel);
|
||||
|
||||
for iii in 0..nlevel {
|
||||
popul_new[iii * nd + id] = poplte[iii];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ChangeOutput {
|
||||
popul_new,
|
||||
nlevel,
|
||||
nd,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_change_mode0_copy() {
|
||||
let nd = 2usize;
|
||||
let nlevel = 3usize;
|
||||
let mut popul = vec![0.0f64; nlevel * nd];
|
||||
popul[0 * nd + 0] = 1.0e10;
|
||||
popul[0 * nd + 1] = 2.0e10;
|
||||
|
||||
let levels = vec![
|
||||
ChangeLevelParams {
|
||||
iold: 0,
|
||||
mode: 0,
|
||||
nxtold: 0,
|
||||
isinew: 0,
|
||||
isiold: 1,
|
||||
nxtsio: 0,
|
||||
rel: 2.0,
|
||||
},
|
||||
];
|
||||
|
||||
let iel = vec![1usize; nlevel];
|
||||
let nnext = vec![2usize; nlevel];
|
||||
let g = vec![1.0f64; nlevel];
|
||||
let enion = vec![0.0f64; nlevel];
|
||||
let n0a = vec![0usize; nlevel];
|
||||
let nka = vec![0usize; nlevel];
|
||||
let sbf = vec![0.0f64; nlevel];
|
||||
let wop = vec![0.0f64; nlevel * nd];
|
||||
let ilk = vec![0usize; nlevel];
|
||||
let usum = vec![0.0f64; nlevel];
|
||||
let attot = vec![0.0f64; nlevel];
|
||||
let temp = vec![10000.0f64; nd];
|
||||
let elec = vec![1.0e14f64; nd];
|
||||
|
||||
let params = ChangeParams {
|
||||
levels: &levels,
|
||||
nd,
|
||||
temp: &temp,
|
||||
elec: &elec,
|
||||
popul: &popul,
|
||||
g: &g,
|
||||
enion: &enion,
|
||||
iel: &iel,
|
||||
nnext: &nnext,
|
||||
nlevel,
|
||||
n0a: &n0a,
|
||||
nka: &nka,
|
||||
sbf: &sbf,
|
||||
wop: &wop,
|
||||
ilk: &ilk,
|
||||
usum: &usum,
|
||||
attot: &attot,
|
||||
};
|
||||
|
||||
let output = change(¶ms);
|
||||
|
||||
// MODE 0: 复制 isiold=1 的 population × rel=2.0
|
||||
assert_eq!(output.popul_new[0 * nd + 0], 1.0e10 * 2.0);
|
||||
assert_eq!(output.popul_new[0 * nd + 1], 2.0e10 * 2.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_change_direct_copy() {
|
||||
let nd = 1usize;
|
||||
let nlevel = 3usize;
|
||||
let mut popul = vec![0.0f64; nlevel * nd];
|
||||
popul[2 * nd + 0] = 5.0e12;
|
||||
|
||||
let levels = vec![
|
||||
ChangeLevelParams {
|
||||
iold: 3,
|
||||
mode: 0,
|
||||
nxtold: 0,
|
||||
isinew: 0,
|
||||
isiold: 0,
|
||||
nxtsio: 0,
|
||||
rel: 1.0,
|
||||
},
|
||||
];
|
||||
|
||||
let iel = vec![1usize; nlevel];
|
||||
let nnext = vec![2usize; nlevel];
|
||||
let g = vec![1.0f64; nlevel];
|
||||
let enion = vec![0.0f64; nlevel];
|
||||
let n0a = vec![0usize; nlevel];
|
||||
let nka = vec![0usize; nlevel];
|
||||
let sbf = vec![0.0f64; nlevel];
|
||||
let wop = vec![0.0f64; nlevel * nd];
|
||||
let ilk = vec![0usize; nlevel];
|
||||
let usum = vec![0.0f64; nlevel];
|
||||
let attot = vec![0.0f64; nlevel];
|
||||
let temp = vec![10000.0f64; nd];
|
||||
let elec = vec![1.0e14f64; nd];
|
||||
|
||||
let params = ChangeParams {
|
||||
levels: &levels,
|
||||
nd,
|
||||
temp: &temp,
|
||||
elec: &elec,
|
||||
popul: &popul,
|
||||
g: &g,
|
||||
enion: &enion,
|
||||
iel: &iel,
|
||||
nnext: &nnext,
|
||||
nlevel,
|
||||
n0a: &n0a,
|
||||
nka: &nka,
|
||||
sbf: &sbf,
|
||||
wop: &wop,
|
||||
ilk: &ilk,
|
||||
usum: &usum,
|
||||
attot: &attot,
|
||||
};
|
||||
|
||||
let output = change(¶ms);
|
||||
|
||||
// iold != 0: 直接复制
|
||||
assert_eq!(output.popul_new[0 * nd + 0], 5.0e12);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
//! 丰度一致性检查。
|
||||
//!
|
||||
//! 重构自 SYNSPEC `CHCKAB` 函数。
|
||||
//!
|
||||
//! 检查显式原子的输入丰度与从模型大气计算得到的丰度是否一致。
|
||||
//! 如果差异超过 10%,程序将停止。
|
||||
|
||||
use crate::synspec::state::constants::{MATOM, MDEPTH, MLEVEL};
|
||||
|
||||
/// CHCKAB 输入参数。
|
||||
pub struct ChckabParams<'a> {
|
||||
/// 深度点数
|
||||
pub nd: usize,
|
||||
/// 温度数组 (K)
|
||||
pub temp: &'a [f64; MDEPTH],
|
||||
/// 电子密度数组 (cm^-3)
|
||||
pub elec: &'a [f64; MDEPTH],
|
||||
/// 能级 populations
|
||||
pub popul: &'a [[f64; MDEPTH]; MLEVEL],
|
||||
/// 上态求和
|
||||
pub usum: &'a [f64; MLEVEL],
|
||||
/// 原子丰度
|
||||
pub abund: &'a [[f64; MDEPTH]; MATOM],
|
||||
/// 原子数
|
||||
pub natom: usize,
|
||||
/// 参考原子索引 (1-based)
|
||||
pub iatref: usize,
|
||||
/// N0A 数组 - 每个原子的第一个能级索引
|
||||
pub n0a: &'a [i32],
|
||||
/// NKA 数组 - 每个原子的最后一个能级索引
|
||||
pub nka: &'a [i32],
|
||||
/// ILK 数组 - 能级索引
|
||||
pub ilk: &'a [i32],
|
||||
}
|
||||
|
||||
/// CHCKAB 输出结果。
|
||||
#[derive(Default)]
|
||||
pub struct ChckabResult {
|
||||
/// 是否发现不一致性
|
||||
pub inconsistent: bool,
|
||||
/// 不一致的原子数
|
||||
pub n_inconsistent: usize,
|
||||
}
|
||||
|
||||
|
||||
/// 丰度一致性检查。
|
||||
///
|
||||
/// 检查显式原子的输入丰度与从模型大气计算得到的丰度是否一致。
|
||||
///
|
||||
/// # 参数
|
||||
///
|
||||
/// * `params` - CHCKAB 参数
|
||||
///
|
||||
/// # 返回值
|
||||
///
|
||||
/// CHCKAB 输出结果
|
||||
pub fn chckab(params: &ChckabParams) -> ChckabResult {
|
||||
let ChckabParams {
|
||||
nd,
|
||||
temp: _,
|
||||
elec,
|
||||
popul,
|
||||
usum,
|
||||
abund,
|
||||
natom,
|
||||
iatref,
|
||||
n0a,
|
||||
nka,
|
||||
ilk,
|
||||
} = *params;
|
||||
|
||||
let mut result = ChckabResult::default();
|
||||
|
||||
// 检查三个深度点: 1, 46, ND
|
||||
let depth_points = [0, 45.min(nd - 1), nd - 1];
|
||||
|
||||
for &id in &depth_points {
|
||||
let ane = elec[id];
|
||||
|
||||
let mut sumiat = [0.0_f64; MATOM];
|
||||
let mut sumpop = [0.0_f64; MATOM];
|
||||
|
||||
// 计算每个原子的总 population
|
||||
for iat in 0..natom {
|
||||
let mut sum = 0.0_f64;
|
||||
let mut sump = 0.0_f64;
|
||||
|
||||
let n0 = n0a[iat] as usize - 1; // 转换为 0-indexed
|
||||
let nk = nka[iat] as usize - 1;
|
||||
|
||||
for i in n0..=nk {
|
||||
let il = ilk[i] as usize;
|
||||
let a = if il > 0 {
|
||||
1.0 + ane * usum[il - 1]
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
sum += a * popul[i][id];
|
||||
sump += popul[i][id];
|
||||
}
|
||||
|
||||
sumiat[iat] = sum;
|
||||
sumpop[iat] = sump;
|
||||
}
|
||||
|
||||
// 检查丰度一致性
|
||||
let iatref_idx = iatref - 1; // 转换为 0-indexed
|
||||
for iat in 0..natom {
|
||||
let x = sumiat[iat] / sumiat[iatref_idx];
|
||||
let ab = abund[iat][id];
|
||||
|
||||
if ab > 0.0 {
|
||||
let ratio = x / ab;
|
||||
if !(0.9..=1.1).contains(&ratio) {
|
||||
result.n_inconsistent += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result.inconsistent = result.n_inconsistent > 0;
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_chckab_consistent() {
|
||||
// 创建一个简单的测试用例,其中丰度一致
|
||||
let nd = 3;
|
||||
let mut temp = [0.0f64; MDEPTH];
|
||||
let mut elec = [0.0f64; MDEPTH];
|
||||
let mut popul = [[0.0f64; MDEPTH]; MLEVEL];
|
||||
let mut usum = [0.0f64; MLEVEL];
|
||||
let mut abund = [[0.0f64; MDEPTH]; MATOM];
|
||||
let mut n0a = [0i32; MATOM];
|
||||
let mut nka = [0i32; MATOM];
|
||||
let mut ilk = [0i32; MLEVEL];
|
||||
|
||||
// 设置测试值
|
||||
for id in 0..nd {
|
||||
temp[id] = 10000.0;
|
||||
elec[id] = 1.0e14;
|
||||
}
|
||||
|
||||
// 设置一个原子,有 2 个能级
|
||||
let natom = 1;
|
||||
n0a[0] = 1;
|
||||
nka[0] = 2;
|
||||
ilk[0] = 0; // 无上态求和
|
||||
ilk[1] = 0;
|
||||
|
||||
// 设置 populations
|
||||
for id in 0..nd {
|
||||
popul[0][id] = 1.0e10;
|
||||
popul[1][id] = 1.0e9;
|
||||
abund[0][id] = 1.0; // 丰度比值 (相对于参考原子)
|
||||
}
|
||||
|
||||
let params = ChckabParams {
|
||||
nd,
|
||||
temp: &temp,
|
||||
elec: &elec,
|
||||
popul: &popul,
|
||||
usum: &usum,
|
||||
abund: &abund,
|
||||
natom,
|
||||
iatref: 1,
|
||||
n0a: &n0a,
|
||||
nka: &nka,
|
||||
ilk: &ilk,
|
||||
};
|
||||
|
||||
let result = chckab(¶ms);
|
||||
|
||||
// 丰度应该一致
|
||||
assert!(!result.inconsistent);
|
||||
assert_eq!(result.n_inconsistent, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_chckab_inconsistent() {
|
||||
// 创建一个测试用例,其中丰度不一致
|
||||
let nd = 3;
|
||||
let mut temp = [0.0f64; MDEPTH];
|
||||
let mut elec = [0.0f64; MDEPTH];
|
||||
let mut popul = [[0.0f64; MDEPTH]; MLEVEL];
|
||||
let mut usum = [0.0f64; MLEVEL];
|
||||
let mut abund = [[0.0f64; MDEPTH]; MATOM];
|
||||
let mut n0a = [0i32; MATOM];
|
||||
let mut nka = [0i32; MATOM];
|
||||
let mut ilk = [0i32; MLEVEL];
|
||||
|
||||
// 设置测试值
|
||||
for id in 0..nd {
|
||||
temp[id] = 10000.0;
|
||||
elec[id] = 1.0e14;
|
||||
}
|
||||
|
||||
// 设置一个原子,有 2 个能级
|
||||
let natom = 1;
|
||||
n0a[0] = 1;
|
||||
nka[0] = 2;
|
||||
ilk[0] = 0;
|
||||
ilk[1] = 0;
|
||||
|
||||
// 设置 populations
|
||||
for id in 0..nd {
|
||||
popul[0][id] = 1.0e10;
|
||||
popul[1][id] = 1.0e9;
|
||||
abund[0][id] = 1.0e10; // 总 population = 1.1e10,但丰度设为 1.0e10
|
||||
// 比值 = 1.1,超过 10% 阈值
|
||||
}
|
||||
|
||||
let params = ChckabParams {
|
||||
nd,
|
||||
temp: &temp,
|
||||
elec: &elec,
|
||||
popul: &popul,
|
||||
usum: &usum,
|
||||
abund: &abund,
|
||||
natom,
|
||||
iatref: 1,
|
||||
n0a: &n0a,
|
||||
nka: &nka,
|
||||
ilk: &ilk,
|
||||
};
|
||||
|
||||
let result = chckab(¶ms);
|
||||
|
||||
// 丰度应该不一致
|
||||
assert!(result.inconsistent);
|
||||
assert!(result.n_inconsistent > 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,562 @@
|
||||
//! Collision-Induced Absorption (CIA) opacity functions.
|
||||
//!
|
||||
//! Translated from SYNSPEC54 subroutines:
|
||||
//! - `cia_h2h2` -- H2-H2 CIA (Borysow et al. 2001, JQSRT 68, 235)
|
||||
//! - `cia_h2h` -- H2-H CIA (from TURBOSPEC)
|
||||
//! - `cia_h2he` -- H2-He CIA (Jorgensen et al. 2000, A&A 361, 283)
|
||||
//! - `cia_hhe` -- H-He CIA (Gustafsson & Frommhold 2001, ApJ 546, 1168)
|
||||
//!
|
||||
//! Each function reads a CIA table on first call, then performs 2D bilinear
|
||||
//! interpolation in (wavenumber, temperature) space to compute opacity.
|
||||
//!
|
||||
//! # Usage
|
||||
//! 1. Call the `*_init` function once to load the CIA data file.
|
||||
//! 2. Call the corresponding function to evaluate CIA opacity at given conditions.
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use super::locate::locate;
|
||||
|
||||
// ============================================================================
|
||||
// Constants
|
||||
// ============================================================================
|
||||
|
||||
/// Speed of light in cm/s
|
||||
const CAS: f64 = 2.997925e10;
|
||||
|
||||
/// Amagat number (Loschmidt number at STP) in cm^-3
|
||||
const AMAGAT: f64 = 2.6867774e19;
|
||||
|
||||
/// Scaling factor: 1 / amagat^2
|
||||
const FAC: f64 = 1.0 / (AMAGAT * AMAGAT);
|
||||
|
||||
/// Fallback value for log(alpha) when outside frequency table range
|
||||
const ALPHA_FLOOR: f64 = -50.0;
|
||||
|
||||
// ============================================================================
|
||||
// CIA table storage
|
||||
// ============================================================================
|
||||
|
||||
/// A loaded CIA table: frequencies (wavenumber in cm^-1), temperatures (K),
|
||||
/// and log(alpha) values.
|
||||
struct CiaTable {
|
||||
nlines: usize,
|
||||
ntemp: usize,
|
||||
freq: Vec<f64>,
|
||||
temp: Vec<f64>,
|
||||
/// log(alpha) values stored as `alpha[i * ntemp + j]` (row-major)
|
||||
alpha: Vec<f64>,
|
||||
}
|
||||
|
||||
// Static storage for each CIA species
|
||||
static TABLE_H2H2: OnceLock<CiaTable> = OnceLock::new();
|
||||
static TABLE_H2H: OnceLock<CiaTable> = OnceLock::new();
|
||||
static TABLE_H2HE: OnceLock<CiaTable> = OnceLock::new();
|
||||
static TABLE_HHE: OnceLock<CiaTable> = OnceLock::new();
|
||||
|
||||
// ============================================================================
|
||||
// Data loading helper
|
||||
// ============================================================================
|
||||
|
||||
/// Load a CIA data file into a `CiaTable`.
|
||||
///
|
||||
/// File format:
|
||||
/// - 3 header lines (skipped)
|
||||
/// - `nlines` data lines, each with: wavenumber alpha(T1) alpha(T2) ... alpha(TnTemp)
|
||||
///
|
||||
/// After reading, all alpha values are replaced by their natural logarithm.
|
||||
fn load_cia_table(filename: &str, nlines: usize, temp: &[f64]) -> Result<CiaTable, String> {
|
||||
let ntemp = temp.len();
|
||||
|
||||
let file = File::open(filename)
|
||||
.map_err(|e| format!("Cannot open CIA data file '{}': {}", filename, e))?;
|
||||
let mut reader = BufReader::new(file);
|
||||
let mut line = String::new();
|
||||
|
||||
// Skip 3 header lines
|
||||
for _ in 0..3 {
|
||||
line.clear();
|
||||
reader
|
||||
.read_line(&mut line)
|
||||
.map_err(|e| format!("Error reading CIA header: {}", e))?;
|
||||
}
|
||||
|
||||
let mut freq = Vec::with_capacity(nlines);
|
||||
let mut alpha = vec![0.0f64; nlines * ntemp];
|
||||
|
||||
for i in 0..nlines {
|
||||
line.clear();
|
||||
reader
|
||||
.read_line(&mut line)
|
||||
.map_err(|e| format!("Error reading CIA data line {}: {}", i + 1, e))?;
|
||||
|
||||
let parts: Vec<f64> = line
|
||||
.split_whitespace()
|
||||
.map(|s| {
|
||||
s.parse::<f64>()
|
||||
.map_err(|_| format!("Cannot parse float from '{}'", s))
|
||||
})
|
||||
.collect::<Result<Vec<f64>, String>>()?;
|
||||
|
||||
if parts.len() < 1 + ntemp {
|
||||
return Err(format!(
|
||||
"CIA data line {}: expected {} fields, got {}",
|
||||
i + 1,
|
||||
1 + ntemp,
|
||||
parts.len()
|
||||
));
|
||||
}
|
||||
|
||||
freq.push(parts[0]);
|
||||
for j in 0..ntemp {
|
||||
alpha[i * ntemp + j] = parts[1 + j].ln();
|
||||
}
|
||||
}
|
||||
|
||||
Ok(CiaTable {
|
||||
nlines,
|
||||
ntemp,
|
||||
freq,
|
||||
temp: temp.to_vec(),
|
||||
alpha,
|
||||
})
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Core interpolation (shared by all 4 functions)
|
||||
// ============================================================================
|
||||
|
||||
/// Perform 2D bilinear interpolation in (wavenumber, temperature) space.
|
||||
///
|
||||
/// Returns the interpolated alpha value (after exp), or 0.0 if temperature
|
||||
/// is below the table range. Returns `exp(ALPHA_FLOOR)` if frequency is
|
||||
/// outside the table.
|
||||
fn cia_interpolate(table: &CiaTable, t: f64, ff: f64) -> f64 {
|
||||
let f = ff / CAS; // Convert Hz to cm^-1
|
||||
|
||||
// Locate temperature
|
||||
let j = locate(&table.temp, table.ntemp, t);
|
||||
|
||||
if j == 0 {
|
||||
// Temperature below table range
|
||||
eprintln!();
|
||||
eprintln!(
|
||||
"Warning: requested temperature is below {} K",
|
||||
table.temp[0]
|
||||
);
|
||||
eprintln!("CIA opacity set to 0");
|
||||
eprintln!();
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
// Locate frequency
|
||||
let i = locate(&table.freq, table.nlines, f);
|
||||
|
||||
let alp = if j == table.ntemp {
|
||||
// Hold values constant if off high temperature end of table
|
||||
let y1 = table.alpha[(i - 1) * table.ntemp + j - 1];
|
||||
let y2 = table.alpha[i * table.ntemp + j - 1];
|
||||
let tt = (f - table.freq[i - 1]) / (table.freq[i] - table.freq[i - 1]);
|
||||
(1.0 - tt) * y1 + tt * y2
|
||||
} else if i == 0 || i == table.nlines {
|
||||
// Off frequency table: set to very small number
|
||||
ALPHA_FLOOR
|
||||
} else {
|
||||
// Bilinear interpolation within table
|
||||
// locate returns 1-indexed indices, so freq indices are i-1 and i (0-indexed)
|
||||
// In Fortran: alpha(i,j), alpha(i+1,j), alpha(i+1,j+1), alpha(i,j+1)
|
||||
// where i is 1-indexed from locate. In our 0-indexed storage:
|
||||
// alpha[(i-1)*ntemp + (j-1)], alpha[i*ntemp + (j-1)],
|
||||
// alpha[i*ntemp + j], alpha[(i-1)*ntemp + j]
|
||||
let y1 = table.alpha[(i - 1) * table.ntemp + (j - 1)];
|
||||
let y2 = table.alpha[i * table.ntemp + (j - 1)];
|
||||
let y3 = table.alpha[i * table.ntemp + j];
|
||||
let y4 = table.alpha[(i - 1) * table.ntemp + j];
|
||||
|
||||
let tt = (f - table.freq[i - 1]) / (table.freq[i] - table.freq[i - 1]);
|
||||
let uu = (t - table.temp[j - 1]) / (table.temp[j] - table.temp[j - 1]);
|
||||
|
||||
(1.0 - tt) * (1.0 - uu) * y1
|
||||
+ tt * (1.0 - uu) * y2
|
||||
+ tt * uu * y3
|
||||
+ (1.0 - tt) * uu * y4
|
||||
};
|
||||
|
||||
alp.exp()
|
||||
}
|
||||
|
||||
/// Helper: initialize a CIA table into a static OnceLock.
|
||||
fn init_cia_table(
|
||||
static_table: &'static OnceLock<CiaTable>,
|
||||
filename: &str,
|
||||
nlines: usize,
|
||||
temp: &[f64],
|
||||
) -> Result<(), String> {
|
||||
let table = load_cia_table(filename, nlines, temp)?;
|
||||
static_table
|
||||
.set(table)
|
||||
.map_err(|_| "CIA table already initialized".to_string())
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// H2-H2 CIA
|
||||
// ============================================================================
|
||||
|
||||
/// Initialize H2-H2 CIA table from file.
|
||||
///
|
||||
/// Data source: Borysow A., Jorgensen U.G., Fu Y. 2001, JQSRT 68, 235
|
||||
///
|
||||
/// File format: 3 header lines + 1000 data lines with 8 columns
|
||||
/// (wavenumber + 7 temperatures: 1000..7000 K)
|
||||
pub fn cia_h2h2_init(filename: &str) -> Result<(), String> {
|
||||
init_cia_table(
|
||||
&TABLE_H2H2,
|
||||
filename,
|
||||
1000,
|
||||
&[1000.0, 2000.0, 3000.0, 4000.0, 5000.0, 6000.0, 7000.0],
|
||||
)
|
||||
}
|
||||
|
||||
/// H2-H2 CIA opacity.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `t` - Temperature in K
|
||||
/// * `ah2` - H2 number density in cm^-3
|
||||
/// * `ff` - Frequency in Hz
|
||||
///
|
||||
/// # Returns
|
||||
/// CIA opacity (cm^-1)
|
||||
pub fn cia_h2h2(t: f64, ah2: f64, ff: f64) -> f64 {
|
||||
let table = match TABLE_H2H2.get() {
|
||||
Some(t) => t,
|
||||
None => {
|
||||
eprintln!("CIA H2-H2 table not initialized, call cia_h2h2_init first");
|
||||
return 0.0;
|
||||
}
|
||||
};
|
||||
let alp = cia_interpolate(table, t, ff);
|
||||
FAC * ah2 * ah2 * alp
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// H2-H CIA
|
||||
// ============================================================================
|
||||
|
||||
/// Initialize H2-H CIA table from file.
|
||||
///
|
||||
/// Data source: TURBOSPEC
|
||||
///
|
||||
/// File format: 3 header lines + 67 data lines with 5 columns
|
||||
/// (wavenumber + 4 temperatures: 1000, 1500, 2000, 2500 K)
|
||||
pub fn cia_h2h_init(filename: &str) -> Result<(), String> {
|
||||
init_cia_table(
|
||||
&TABLE_H2H,
|
||||
filename,
|
||||
67,
|
||||
&[1000.0, 1500.0, 2000.0, 2500.0],
|
||||
)
|
||||
}
|
||||
|
||||
/// H2-H CIA opacity.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `t` - Temperature in K
|
||||
/// * `ah2` - H2 number density in cm^-3
|
||||
/// * `ah` - H number density in cm^-3
|
||||
/// * `ff` - Frequency in Hz
|
||||
///
|
||||
/// # Returns
|
||||
/// CIA opacity (cm^-1)
|
||||
pub fn cia_h2h(t: f64, ah2: f64, ah: f64, ff: f64) -> f64 {
|
||||
let table = match TABLE_H2H.get() {
|
||||
Some(t) => t,
|
||||
None => {
|
||||
eprintln!("CIA H2-H table not initialized, call cia_h2h_init first");
|
||||
return 0.0;
|
||||
}
|
||||
};
|
||||
let alp = cia_interpolate(table, t, ff);
|
||||
FAC * ah2 * ah * alp
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// H2-He CIA
|
||||
// ============================================================================
|
||||
|
||||
/// Initialize H2-He CIA table from file.
|
||||
///
|
||||
/// Data source: Jorgensen U.G., Hammer D., Borysow A., Falkesgaard J., 2000,
|
||||
/// Astronomy & Astrophysics 361, 283
|
||||
///
|
||||
/// File format: 3 header lines + 242 data lines with 8 columns
|
||||
/// (wavenumber + 7 temperatures: 1000..7000 K)
|
||||
pub fn cia_h2he_init(filename: &str) -> Result<(), String> {
|
||||
init_cia_table(
|
||||
&TABLE_H2HE,
|
||||
filename,
|
||||
242,
|
||||
&[1000.0, 2000.0, 3000.0, 4000.0, 5000.0, 6000.0, 7000.0],
|
||||
)
|
||||
}
|
||||
|
||||
/// H2-He CIA opacity.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `t` - Temperature in K
|
||||
/// * `ah2` - H2 number density in cm^-3
|
||||
/// * `ahe` - He number density in cm^-3
|
||||
/// * `ff` - Frequency in Hz
|
||||
///
|
||||
/// # Returns
|
||||
/// CIA opacity (cm^-1)
|
||||
pub fn cia_h2he(t: f64, ah2: f64, ahe: f64, ff: f64) -> f64 {
|
||||
let table = match TABLE_H2HE.get() {
|
||||
Some(t) => t,
|
||||
None => {
|
||||
eprintln!("CIA H2-He table not initialized, call cia_h2he_init first");
|
||||
return 0.0;
|
||||
}
|
||||
};
|
||||
let alp = cia_interpolate(table, t, ff);
|
||||
FAC * ah2 * ahe * alp
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// H-He CIA
|
||||
// ============================================================================
|
||||
|
||||
/// Initialize H-He CIA table from file.
|
||||
///
|
||||
/// Data source: Gustafsson M., Frommhold, L. 2001, ApJ 546, 1168
|
||||
///
|
||||
/// File format: 3 header lines + 43 data lines with 12 columns
|
||||
/// (wavenumber + 11 temperatures: 1000, 1500, 2250, 3000, 4000, 5000,
|
||||
/// 6000, 7000, 8000, 9000, 10000 K)
|
||||
pub fn cia_hhe_init(filename: &str) -> Result<(), String> {
|
||||
init_cia_table(
|
||||
&TABLE_HHE,
|
||||
filename,
|
||||
43,
|
||||
&[
|
||||
1000.0, 1500.0, 2250.0, 3000.0, 4000.0, 5000.0, 6000.0, 7000.0, 8000.0, 9000.0,
|
||||
10000.0,
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
/// H-He CIA opacity.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `t` - Temperature in K
|
||||
/// * `ah` - H number density in cm^-3
|
||||
/// * `ahe` - He number density in cm^-3
|
||||
/// * `ff` - Frequency in Hz
|
||||
///
|
||||
/// # Returns
|
||||
/// CIA opacity (cm^-1)
|
||||
pub fn cia_hhe(t: f64, ah: f64, ahe: f64, ff: f64) -> f64 {
|
||||
let table = match TABLE_HHE.get() {
|
||||
Some(t) => t,
|
||||
None => {
|
||||
eprintln!("CIA H-He table not initialized, call cia_hhe_init first");
|
||||
return 0.0;
|
||||
}
|
||||
};
|
||||
let alp = cia_interpolate(table, t, ff);
|
||||
FAC * ah * ahe * alp
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Tests
|
||||
// ============================================================================
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Helper: build a small synthetic CIA table for testing interpolation.
|
||||
fn make_test_table() -> CiaTable {
|
||||
// 5 frequency points, 3 temperature points
|
||||
let nlines = 5;
|
||||
let ntemp = 3;
|
||||
let freq = vec![100.0, 200.0, 300.0, 400.0, 500.0];
|
||||
let temp = vec![1000.0, 2000.0, 3000.0];
|
||||
// alpha values (will be stored as ln)
|
||||
// Use a simple pattern: alpha(i,j) = (i+1)*10 + (j+1) (before log)
|
||||
let mut alpha = vec![0.0; nlines * ntemp];
|
||||
for i in 0..nlines {
|
||||
for j in 0..ntemp {
|
||||
let val = (i as f64 + 1.0) * 10.0 + (j as f64 + 1.0);
|
||||
alpha[i * ntemp + j] = val.ln();
|
||||
}
|
||||
}
|
||||
CiaTable {
|
||||
nlines,
|
||||
ntemp,
|
||||
freq,
|
||||
temp,
|
||||
alpha,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cia_constants() {
|
||||
assert!((AMAGAT - 2.6867774e19).abs() < 1e10);
|
||||
assert!((CAS - 2.997925e10).abs() < 1e3);
|
||||
let expected_fac = 1.0 / (2.6867774e19_f64 * 2.6867774e19);
|
||||
assert!((FAC - expected_fac).abs() / expected_fac < 1e-12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cia_interpolation_basic() {
|
||||
let table = make_test_table();
|
||||
// At freq=200, temp=1500 (midpoint of 1000,2000)
|
||||
// locate(freq,5,200) => i=2, meaning freq[i-1]=200, freq[i]=300 => tt=0
|
||||
// locate(temp,3,1500) => j=1, meaning temp[j-1]=1000, temp[j]=2000 => uu=0.5
|
||||
// y1=alpha[1][0]=ln(21), y4=alpha[1][1]=ln(22)
|
||||
// alp = 0.5*ln(21) + 0.5*ln(22) = ln(sqrt(21*22)) = ln(sqrt(462))
|
||||
let alp = cia_interpolate(&table, 1500.0, 200.0 * CAS);
|
||||
let expected = (462.0_f64).sqrt();
|
||||
assert!(
|
||||
(alp - expected).abs() / expected < 1e-10,
|
||||
"Expected {}, got {}",
|
||||
expected,
|
||||
alp
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cia_interpolation_corner() {
|
||||
let table = make_test_table();
|
||||
// At exact grid point freq=100, temp=1000
|
||||
// alpha[0][0] = ln(11), exp => 11
|
||||
let alp = cia_interpolate(&table, 1000.0, 100.0 * CAS);
|
||||
assert!(
|
||||
(alp - 11.0).abs() < 1e-10,
|
||||
"Expected 11.0, got {}",
|
||||
alp
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cia_interpolation_high_temp() {
|
||||
let table = make_test_table();
|
||||
// Temperature above max (3000): hold constant at j=ntemp
|
||||
// freq=250 (midpoint), temp=5000 (above max)
|
||||
// locate(freq,5,250)=2, locate(temp,3,5000)=3=j=ntemp
|
||||
// j==ntemp branch: 1D interpolation in freq at highest temp column
|
||||
// y1=alpha[1][2]=ln(23), y2=alpha[2][2]=ln(33), tt=0.5
|
||||
// alp = 0.5*ln(23) + 0.5*ln(33) = ln(sqrt(23*33)) = ln(sqrt(759))
|
||||
let alp = cia_interpolate(&table, 5000.0, 250.0 * CAS);
|
||||
let expected = (759.0_f64).sqrt();
|
||||
assert!(
|
||||
(alp - expected).abs() / expected < 1e-10,
|
||||
"Expected {}, got {}",
|
||||
expected,
|
||||
alp
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cia_interpolation_low_temp() {
|
||||
let table = make_test_table();
|
||||
let alp = cia_interpolate(&table, 500.0, 200.0 * CAS);
|
||||
assert_eq!(alp, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cia_interpolation_low_freq() {
|
||||
let table = make_test_table();
|
||||
let alp = cia_interpolate(&table, 1500.0, 50.0 * CAS);
|
||||
let expected = ALPHA_FLOOR.exp();
|
||||
assert!(
|
||||
(alp - expected).abs() < 1e-20,
|
||||
"Expected ~{}, got {}",
|
||||
expected,
|
||||
alp
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cia_interpolation_high_freq() {
|
||||
let table = make_test_table();
|
||||
let alp = cia_interpolate(&table, 1500.0, 600.0 * CAS);
|
||||
let expected = ALPHA_FLOOR.exp();
|
||||
assert!(
|
||||
(alp - expected).abs() < 1e-20,
|
||||
"Expected ~{}, got {}",
|
||||
expected,
|
||||
alp
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cia_h2h2_not_initialized() {
|
||||
let result = cia_h2h2(5000.0, 1e15, 1e14);
|
||||
assert_eq!(result, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cia_h2h_not_initialized() {
|
||||
let result = cia_h2h(5000.0, 1e15, 1e15, 1e14);
|
||||
assert_eq!(result, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cia_h2he_not_initialized() {
|
||||
let result = cia_h2he(5000.0, 1e15, 1e15, 1e14);
|
||||
assert_eq!(result, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cia_hhe_not_initialized() {
|
||||
let result = cia_hhe(5000.0, 1e15, 1e15, 1e14);
|
||||
assert_eq!(result, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_cia_table_structure() {
|
||||
let table = make_test_table();
|
||||
assert_eq!(table.nlines, 5);
|
||||
assert_eq!(table.ntemp, 3);
|
||||
assert_eq!(table.freq.len(), 5);
|
||||
assert_eq!(table.temp.len(), 3);
|
||||
assert_eq!(table.alpha.len(), 15);
|
||||
// Verify log was taken: alpha[0] = ln(11)
|
||||
assert!((table.alpha[0] - 11.0_f64.ln()).abs() < 1e-15);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cia_symmetry_h2h2() {
|
||||
// H2-H2: density product is ah2^2, so doubling ah2 should quadruple result
|
||||
let table = make_test_table();
|
||||
let alp = cia_interpolate(&table, 1500.0, 200.0 * CAS);
|
||||
let opac1 = FAC * 1e15 * 1e15 * alp;
|
||||
let opac2 = FAC * 2e15 * 2e15 * alp;
|
||||
assert!(
|
||||
(opac2 / opac1 - 4.0).abs() < 1e-10,
|
||||
"Expected ratio 4.0, got {}",
|
||||
opac2 / opac1
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cia_product_scaling() {
|
||||
let table = make_test_table();
|
||||
let alp = cia_interpolate(&table, 2000.0, 300.0 * CAS);
|
||||
let d1 = 1e14;
|
||||
let d2 = 3e14;
|
||||
let opac1 = FAC * d1 * d1 * alp;
|
||||
let opac2 = FAC * d2 * d2 * alp;
|
||||
let ratio = opac2 / opac1;
|
||||
let expected = (d2 / d1) * (d2 / d1);
|
||||
assert!(
|
||||
(ratio - expected).abs() / expected < 1e-10,
|
||||
"Expected ratio {}, got {}",
|
||||
expected,
|
||||
ratio
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,8 @@
|
||||
//!
|
||||
//! 设置光致电离截面数组,用于辐射转移计算。
|
||||
|
||||
use crate::tlusty::math::sigk::{sigk, SigkParams};
|
||||
use crate::tlusty::math::{sigk, SigkParams};
|
||||
use crate::tlusty::state::atomic::AtomicData;
|
||||
use crate::tlusty::state::constants::{MCROSS, MFREQ};
|
||||
|
||||
// ============================================================================
|
||||
// 常量
|
||||
@@ -115,7 +114,7 @@ pub fn croset(params: &CrosetParams) -> Vec<Vec<f64>> {
|
||||
itr: it,
|
||||
mode: 0,
|
||||
atomic,
|
||||
opdata: &crate::tlusty::math::topbas::OpData::default(),
|
||||
opdata: &crate::tlusty::math::OpData::default(),
|
||||
};
|
||||
cross[it][ij] = sigk(&sigk_params);
|
||||
}
|
||||
@@ -128,7 +127,7 @@ pub fn croset(params: &CrosetParams) -> Vec<Vec<f64>> {
|
||||
itr: it,
|
||||
mode: 1,
|
||||
atomic,
|
||||
opdata: &crate::tlusty::math::topbas::OpData::default(),
|
||||
opdata: &crate::tlusty::math::OpData::default(),
|
||||
};
|
||||
cross[it][ij] = sigk(&sigk_params);
|
||||
|
||||
@@ -226,7 +225,7 @@ pub fn crosew(params: &CrosewParams) -> Vec<Vec<f64>> {
|
||||
itr: it,
|
||||
mode: 0,
|
||||
atomic,
|
||||
opdata: &crate::tlusty::math::topbas::OpData::default(),
|
||||
opdata: &crate::tlusty::math::OpData::default(),
|
||||
};
|
||||
cross[it][ij] = sigk(&sigk_params);
|
||||
}
|
||||
@@ -239,7 +238,7 @@ pub fn crosew(params: &CrosewParams) -> Vec<Vec<f64>> {
|
||||
itr: it,
|
||||
mode: 1,
|
||||
atomic,
|
||||
opdata: &crate::tlusty::math::topbas::OpData::default(),
|
||||
opdata: &crate::tlusty::math::OpData::default(),
|
||||
};
|
||||
cross[it][ij] = sigk(&sigk_params);
|
||||
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
//! Determination of state parameters for opacity grid calculations.
|
||||
//!
|
||||
//! Translated from SYNSPEC54.FOR subroutine DENSIT(RHO,IDENS)
|
||||
//! at line 22330.
|
||||
//!
|
||||
//! Determines the state parameters (electron density, total particle
|
||||
//! density, populations) for a given depth point using various input modes.
|
||||
|
||||
/// Input mode for density determination.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum DensitMode {
|
||||
/// Electron density as input
|
||||
ElectronDensity = 0,
|
||||
/// Total particle density as input (negative)
|
||||
ParticleDensity = -1,
|
||||
/// Mass density as input (mode 1)
|
||||
MassDensity1 = 1,
|
||||
/// Mass density as input (mode 2)
|
||||
MassDensity2 = 2,
|
||||
}
|
||||
|
||||
/// Parameters for DENSIT calculation.
|
||||
pub struct DensitParams {
|
||||
/// Input value (rho, electron density, or particle density)
|
||||
pub rho: f64,
|
||||
/// Input mode
|
||||
pub idens: DensitMode,
|
||||
/// Temperature (K)
|
||||
pub temp: f64,
|
||||
/// Mean molecular weight
|
||||
pub wmm: f64,
|
||||
/// Total hydrogen abundance
|
||||
pub ytot: f64,
|
||||
/// Boltzmann constant (erg/K)
|
||||
pub bolk: f64,
|
||||
/// Hydrogen mass (g)
|
||||
pub hmass: f64,
|
||||
/// Molecular flag
|
||||
pub ifmol: i32,
|
||||
/// Molecular temperature limit
|
||||
pub tmolim: f64,
|
||||
/// Number of levels
|
||||
pub nlevel: usize,
|
||||
/// Standard depth index
|
||||
pub idstd: usize,
|
||||
}
|
||||
|
||||
/// Result of DENSIT calculation.
|
||||
pub struct DensitResult {
|
||||
/// Electron density (cm^-3)
|
||||
pub elec: f64,
|
||||
/// Mass density (g/cm^3)
|
||||
pub dens: f64,
|
||||
/// Total particle density (cm^-3)
|
||||
pub an: f64,
|
||||
}
|
||||
|
||||
/// Determination of state parameters.
|
||||
///
|
||||
/// Determines electron density, mass density, and total particle density
|
||||
/// from the given input value and mode.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `params` - Input parameters
|
||||
/// * `todens_fn` - Function to compute AN from (id, t, ane)
|
||||
/// * `eldens_fn` - Function to compute ANE from (id, t, an)
|
||||
/// * `rhonen_fn` - Function to compute (an, ane) from (id, t, rho)
|
||||
///
|
||||
/// # Returns
|
||||
/// Electron density, mass density, and total particle density.
|
||||
pub fn densit<T, E, R>(
|
||||
params: &DensitParams,
|
||||
todens_fn: T,
|
||||
eldens_fn: E,
|
||||
rhonen_fn: R,
|
||||
) -> DensitResult
|
||||
where
|
||||
T: Fn(usize, f64, f64) -> (f64, f64, f64, f64),
|
||||
E: Fn(usize, f64, f64, f64) -> f64,
|
||||
R: Fn(usize, f64, f64) -> (f64, f64),
|
||||
{
|
||||
let id = 0; // Single depth point
|
||||
let t = params.temp;
|
||||
let wmm = params.wmm;
|
||||
|
||||
let (elec, dens, an) = match params.idens {
|
||||
DensitMode::ElectronDensity => {
|
||||
let ane = params.rho;
|
||||
let (an, _anp, _ahtot, _ahmol) = todens_fn(id, t, ane);
|
||||
let dens = (an - ane) * wmm;
|
||||
(ane, dens, an)
|
||||
}
|
||||
DensitMode::ParticleDensity => {
|
||||
let an = params.rho / t / params.bolk;
|
||||
let ane = eldens_fn(id, t, an, 0.0);
|
||||
let dens = wmm * (an - ane);
|
||||
(ane, dens, an)
|
||||
}
|
||||
DensitMode::MassDensity1 => {
|
||||
let rho = params.rho;
|
||||
let (an, ane) = rhonen_fn(id, t, rho);
|
||||
(ane, rho, an)
|
||||
}
|
||||
DensitMode::MassDensity2 => {
|
||||
let rho = params.rho;
|
||||
let (an, ane) = rhonen_fn(id, t, rho);
|
||||
(ane, rho, an)
|
||||
}
|
||||
};
|
||||
|
||||
DensitResult { elec, dens, an }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_densit_electron_density() {
|
||||
let params = DensitParams {
|
||||
rho: 1e13,
|
||||
idens: DensitMode::ElectronDensity,
|
||||
temp: 10000.0,
|
||||
wmm: 1.0,
|
||||
ytot: 1.0,
|
||||
bolk: 1.380658e-16,
|
||||
hmass: 1.67e-24,
|
||||
ifmol: 0,
|
||||
tmolim: 9000.0,
|
||||
nlevel: 10,
|
||||
idstd: 0,
|
||||
};
|
||||
|
||||
// Mock todens: return (an, anp, ahtot, ahmol)
|
||||
let todens_fn = |_id: usize, _t: f64, ane: f64| {
|
||||
(ane * 1.1, ane * 0.1, ane * 1.0, 0.0)
|
||||
};
|
||||
let eldens_fn = |_id: usize, _t: f64, _an: f64, _ane: f64| 1e13;
|
||||
let rhonen_fn = |_id: usize, _t: f64, _rho: f64| (1e13, 1e12);
|
||||
|
||||
let result = densit(¶ms, todens_fn, eldens_fn, rhonen_fn);
|
||||
assert!(result.elec > 0.0);
|
||||
assert!(result.dens > 0.0);
|
||||
assert!(result.an > 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_densit_particle_density() {
|
||||
let params = DensitParams {
|
||||
rho: 1e13,
|
||||
idens: DensitMode::ParticleDensity,
|
||||
temp: 10000.0,
|
||||
wmm: 1.0,
|
||||
ytot: 1.0,
|
||||
bolk: 1.380658e-16,
|
||||
hmass: 1.67e-24,
|
||||
ifmol: 0,
|
||||
tmolim: 9000.0,
|
||||
nlevel: 10,
|
||||
idstd: 0,
|
||||
};
|
||||
|
||||
let todens_fn = |_id: usize, _t: f64, ane: f64| {
|
||||
(ane * 1.1, ane * 0.1, ane * 1.0, 0.0)
|
||||
};
|
||||
let eldens_fn = |_id: usize, _t: f64, an: f64, _ane: f64| an * 0.1;
|
||||
let rhonen_fn = |_id: usize, _t: f64, _rho: f64| (1e13, 1e12);
|
||||
|
||||
let result = densit(¶ms, todens_fn, eldens_fn, rhonen_fn);
|
||||
assert!(result.elec >= 0.0);
|
||||
assert!(result.an > 0.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
//! Division point between Doppler and asymptotic Stark profiles.
|
||||
//!
|
||||
//! Translated from SYNSPEC `DIVSTR` subroutine (synspec54.f:6840).
|
||||
//!
|
||||
//! Auxiliary procedure for STARKA - determines the division point
|
||||
//! between Doppler and asymptotic Stark profiles.
|
||||
|
||||
/// Compute the division point between Doppler and asymptotic Stark profiles.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `betad` - Doppler width in beta units
|
||||
///
|
||||
/// # Returns
|
||||
/// A tuple `(a, div)` where:
|
||||
/// * `a` = 1.5 * ln(betad) - 1.671
|
||||
/// * `div` - division point (only meaningful for a > 1); solution of
|
||||
/// exp(-(beta/betad)^2) / betad / sqrt(pi) = 3 * beta^(-5/2)
|
||||
pub fn divstr(betad: f64) -> (f64, f64) {
|
||||
const CA: f64 = 1.671;
|
||||
const BL: f64 = 5.821;
|
||||
const AL: f64 = 1.26;
|
||||
const CX: f64 = 0.28;
|
||||
const DX: f64 = 0.0001;
|
||||
|
||||
let a = 1.5 * betad.ln() - CA;
|
||||
if betad < BL {
|
||||
return (a, 0.0);
|
||||
}
|
||||
|
||||
let mut x = if a >= AL {
|
||||
a.sqrt() * (1.0 + 1.25 * a.ln() / (4.0 * a - 5.0))
|
||||
} else {
|
||||
(CX + a).sqrt()
|
||||
};
|
||||
|
||||
for _ in 0..5 {
|
||||
let xn = x * (1.0 - (x * x - 2.5 * x.ln() - a) / (2.0 * x * x - 2.5));
|
||||
if (xn - x).abs() <= DX {
|
||||
x = xn;
|
||||
break;
|
||||
}
|
||||
x = xn;
|
||||
}
|
||||
|
||||
(a, x)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_divstr_small_betad() {
|
||||
// For betad < BL (5.821), div should be 0
|
||||
let (a, div) = divstr(3.0);
|
||||
assert!(a < 0.0); // 1.5*ln(3) - 1.671 ≈ -0.024
|
||||
assert_eq!(div, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_divstr_large_betad() {
|
||||
// For large betad, should compute meaningful division point
|
||||
let (a, div) = divstr(100.0);
|
||||
assert!(a > 1.0);
|
||||
assert!(div > 0.0);
|
||||
assert!(div.is_finite());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_divstr_boundary() {
|
||||
// At betad = BL, a should be approximately 1.5*ln(5.821) - 1.671
|
||||
let (a, _div) = divstr(5.821);
|
||||
let expected_a = 1.5 * 5.821_f64.ln() - 1.671;
|
||||
assert!((a - expected_a).abs() < 1e-10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_divstr_a_ge_al() {
|
||||
// betad large enough that a >= AL (1.26)
|
||||
let (a, div) = divstr(20.0);
|
||||
assert!(a >= 1.26);
|
||||
assert!(div > 0.0);
|
||||
assert!(div.is_finite());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_divstr_a_lt_al() {
|
||||
// betad in range where a < AL but betad >= BL
|
||||
let (a, div) = divstr(7.0);
|
||||
// a = 1.5*ln(7) - 1.671 ≈ 1.265 (close to AL boundary)
|
||||
if a < 1.26 {
|
||||
assert!(div > 0.0);
|
||||
}
|
||||
assert!(div.is_finite());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_divstr_convergence() {
|
||||
// Verify Newton iteration converges for various inputs
|
||||
for betad in [10.0, 50.0, 100.0, 500.0, 1000.0] {
|
||||
let (a, div) = divstr(betad);
|
||||
assert!(div.is_finite(), "div not finite for betad={}", betad);
|
||||
assert!(div > 0.0, "div not positive for betad={}", betad);
|
||||
// Verify the equation: x^2 - 2.5*ln(x) - a ≈ 0
|
||||
let residual = div * div - 2.5 * div.ln() - a;
|
||||
assert!(
|
||||
residual.abs() < 0.01,
|
||||
"residual too large for betad={}: {}",
|
||||
betad,
|
||||
residual
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
//! 溶解分数辅助量。
|
||||
//!
|
||||
//! 重构自 SYNSPEC `dwnfr0.f`。
|
||||
|
||||
use crate::synspec::state::constants::{MDEPTH, MZZ};
|
||||
|
||||
/// 溶解分数辅助量。
|
||||
///
|
||||
/// 计算电子密度的幂次和溶解分数系数。
|
||||
///
|
||||
/// # 参数
|
||||
///
|
||||
/// * `id` - 深度点索引 (0-based)
|
||||
/// * `elec` - 电子密度数组
|
||||
/// * `temp` - 温度数组
|
||||
/// * `elec23` - 输出: 电子密度的 2/3 次方
|
||||
/// * `z3` - 输出: 电荷的三次方
|
||||
/// * `dwc1` - 输出: 溶解分数系数 1
|
||||
/// * `dwc2` - 输出: 溶解分数系数 2
|
||||
pub fn dwnfr0(
|
||||
id: usize,
|
||||
elec: &[f64; MDEPTH],
|
||||
temp: &[f64; MDEPTH],
|
||||
elec23: &mut [f64; MDEPTH],
|
||||
z3: &mut [f64; MZZ],
|
||||
dwc1: &mut [[f64; MDEPTH]; MZZ],
|
||||
dwc2: &mut [f64; MDEPTH],
|
||||
) {
|
||||
const UN: f64 = 1.0;
|
||||
const SIXTH: f64 = UN / 6.0;
|
||||
const CCOR: f64 = 0.09;
|
||||
const P1: f64 = 0.1402;
|
||||
const P2: f64 = 0.1285;
|
||||
const P3: f64 = UN;
|
||||
const P4: f64 = 3.15;
|
||||
const P5: f64 = 4.0;
|
||||
const F23: f64 = -2.0 / 3.0;
|
||||
|
||||
let ane = elec[id];
|
||||
elec23[id] = (F23 * ane.ln()).exp();
|
||||
let anes = (SIXTH * ane.ln()).exp();
|
||||
let acor = CCOR * anes / temp[id].sqrt();
|
||||
let x = (P4 * (UN + P3 * acor).ln()).exp();
|
||||
dwc2[id] = P2 * x;
|
||||
let a3 = acor * acor * acor;
|
||||
for izz in 0..MZZ {
|
||||
let z = (izz + 1) as f64;
|
||||
z3[izz] = z * z * z;
|
||||
dwc1[izz][id] = P1 * (x + P5 * (z - 1.0) * a3);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_dwnfr0_basic() {
|
||||
let mut elec = [0.0f64; MDEPTH];
|
||||
let mut temp = [0.0f64; MDEPTH];
|
||||
let mut elec23 = [0.0f64; MDEPTH];
|
||||
let mut z3 = [0.0f64; MZZ];
|
||||
let mut dwc1 = [[0.0f64; MDEPTH]; MZZ];
|
||||
let mut dwc2 = [0.0f64; MDEPTH];
|
||||
|
||||
// 设置测试值
|
||||
elec[0] = 1.0e14;
|
||||
temp[0] = 10000.0;
|
||||
|
||||
dwnfr0(0, &elec, &temp, &mut elec23, &mut z3, &mut dwc1, &mut dwc2);
|
||||
|
||||
// 验证 elec23 = elec^(-2/3)
|
||||
let expected_elec23 = (1.0e14_f64.powf(-2.0 / 3.0));
|
||||
assert!((elec23[0] - expected_elec23).abs() < 1.0e-10);
|
||||
|
||||
// 验证 z3
|
||||
assert!((z3[0] - 1.0).abs() < 1.0e-10); // z=1 -> z3=1
|
||||
assert!((z3[1] - 8.0).abs() < 1.0e-10); // z=2 -> z3=8
|
||||
|
||||
// 验证 dwc2 为正
|
||||
assert!(dwc2[0] > 0.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
//! Dissolved fraction for a given frequency.
|
||||
//!
|
||||
//! Translated from SYNSPEC54.FOR subroutine DWNFR1(FR,FR0,ID,IZZ,DW1) at line 22349.
|
||||
//!
|
||||
//! Computes the dissolved fraction for a spectral line at frequency FR
|
||||
//! relative to the series limit frequency FR0, for ionization stage IZZ
|
||||
//! at depth point ID.
|
||||
|
||||
/// Parameters for dissolved fraction calculation.
|
||||
pub struct Dwnfr1Params<'a> {
|
||||
/// Frequency at which to evaluate (Hz)
|
||||
pub fr: f64,
|
||||
/// Series limit frequency (Hz)
|
||||
pub fr0: f64,
|
||||
/// Depth index
|
||||
pub id: usize,
|
||||
/// Ionic charge
|
||||
pub izz: usize,
|
||||
/// Z^3 array for each ionic charge
|
||||
pub z3: &'a [f64],
|
||||
/// Electron density to the 2/3 power at each depth
|
||||
pub elec23: &'a [f64],
|
||||
/// DWC1 parameter (IZZ x depth, row-major)
|
||||
pub dwc1: &'a [f64],
|
||||
/// Number of depth points (for 2D indexing of dwc1)
|
||||
pub ndepth: usize,
|
||||
/// DWC2 parameter at each depth
|
||||
pub dwc2: &'a [f64],
|
||||
/// Bergmann factor (usually 1.0)
|
||||
pub bergfc: f64,
|
||||
}
|
||||
|
||||
/// Dissolved fraction for a given frequency.
|
||||
///
|
||||
/// Computes the dissolved fraction for a spectral line at frequency FR
|
||||
/// relative to the series limit frequency FR0. Returns 1.0 (fully dissolved)
|
||||
/// when FR >= FR0.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `params` - Calculation parameters
|
||||
///
|
||||
/// # Returns
|
||||
/// Dissolved fraction (0 to 1)
|
||||
pub fn dwnfr1(params: &Dwnfr1Params) -> f64 {
|
||||
if params.fr < params.fr0 {
|
||||
// Constants
|
||||
let sqfrh = 5.734152e7;
|
||||
let tkn = 3.01;
|
||||
let ckn = 5.33333333;
|
||||
let cb = 8.59e14;
|
||||
|
||||
let izz_f = params.izz as f64;
|
||||
let xn = sqfrh * izz_f / (params.fr0 - params.fr).sqrt();
|
||||
|
||||
let xkn = if xn <= tkn {
|
||||
1.0
|
||||
} else {
|
||||
let xn1 = 1.0 / (xn + 1.0);
|
||||
ckn * xn * xn1 * xn1
|
||||
};
|
||||
|
||||
let beta = cb * params.z3[params.izz] * xkn
|
||||
/ (xn * xn * xn * xn)
|
||||
* params.elec23[params.id]
|
||||
* params.bergfc;
|
||||
let beta3 = beta * beta * beta;
|
||||
let beta32 = beta3.sqrt();
|
||||
|
||||
// DWC1 is 2D: (IZZ, ID) -> dwc1[izz * ndepth + id]
|
||||
let dwc1_val = params.dwc1[params.izz * params.ndepth + params.id];
|
||||
let f = (dwc1_val * beta3) / (1.0 + params.dwc2[params.id] * beta32);
|
||||
|
||||
1.0 - f / (1.0 + f)
|
||||
} else {
|
||||
1.0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_dwnfr1_above_limit() {
|
||||
// When fr >= fr0, should return 1.0
|
||||
let z3 = vec![1.0, 8.0, 27.0];
|
||||
// ELEC23 = ANE^(-2/3), for ANE=1e13 -> ~2.15e-9
|
||||
let elec23 = vec![2.15e-9; 3];
|
||||
let dwc1 = vec![0.0; 6]; // 2 ionic charges x 3 depths
|
||||
let dwc2 = vec![0.5; 3];
|
||||
|
||||
let params = Dwnfr1Params {
|
||||
fr: 3.3e15,
|
||||
fr0: 3.3e15,
|
||||
id: 0,
|
||||
izz: 1,
|
||||
z3: &z3,
|
||||
elec23: &elec23,
|
||||
dwc1: &dwc1,
|
||||
ndepth: 3,
|
||||
dwc2: &dwc2,
|
||||
bergfc: 1.0,
|
||||
};
|
||||
assert_eq!(dwnfr1(¶ms), 1.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_dwnfr1_below_limit() {
|
||||
// When fr < fr0, should return something < 1.0
|
||||
let z3 = vec![1.0, 8.0, 27.0];
|
||||
let elec23 = vec![2.15e-9; 3];
|
||||
let mut dwc1 = vec![0.0; 6]; // 2 x 3
|
||||
dwc1[1 * 3 + 0] = 0.1; // DWC1(IZZ=1, ID=0)
|
||||
let dwc2 = vec![0.5; 3];
|
||||
|
||||
let params = Dwnfr1Params {
|
||||
fr: 3.0e15,
|
||||
fr0: 3.3e15,
|
||||
id: 0,
|
||||
izz: 1,
|
||||
z3: &z3,
|
||||
elec23: &elec23,
|
||||
dwc1: &dwc1,
|
||||
ndepth: 3,
|
||||
dwc2: &dwc2,
|
||||
bergfc: 1.0,
|
||||
};
|
||||
let result = dwnfr1(¶ms);
|
||||
assert!(result > 0.0 && result <= 1.0, "dwnfr1 out of range: {}", result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_dwnfr1_well_below_limit() {
|
||||
// Far from limit, dissolved fraction should be close to 1.0
|
||||
let z3 = vec![1.0, 8.0, 27.0];
|
||||
let elec23 = vec![2.15e-9; 3];
|
||||
let mut dwc1 = vec![0.0; 6];
|
||||
dwc1[1 * 3 + 0] = 0.1;
|
||||
let dwc2 = vec![0.5; 3];
|
||||
|
||||
let params = Dwnfr1Params {
|
||||
fr: 3.3e15 - 1.0e4, // very close to limit: xn >> TKN
|
||||
fr0: 3.3e15,
|
||||
id: 0,
|
||||
izz: 1,
|
||||
z3: &z3,
|
||||
elec23: &elec23,
|
||||
dwc1: &dwc1,
|
||||
ndepth: 3,
|
||||
dwc2: &dwc2,
|
||||
bergfc: 1.0,
|
||||
};
|
||||
let result = dwnfr1(¶ms);
|
||||
assert!(result > 0.9, "Expected close to 1.0, got {}", result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,350 @@
|
||||
//! Electron density calculation by Newton-Raphson method.
|
||||
//!
|
||||
//! Translated from SYNSPEC54.FOR subroutine ELDENS (line 22552).
|
||||
//!
|
||||
//! Evaluates the electron density and total hydrogen number density
|
||||
//! for a given total particle number density and temperature by solving
|
||||
//! the set of Saha equations, charge conservation and particle conservation
|
||||
//! equations using a Newton-Raphson method.
|
||||
|
||||
/// Parameters for ELDENS calculation.
|
||||
pub struct EldensParams {
|
||||
/// Depth point index
|
||||
pub id: usize,
|
||||
/// Temperature (K)
|
||||
pub t: f64,
|
||||
/// Total particle number density (cm^-3)
|
||||
pub an: f64,
|
||||
/// Initial electron density estimate (cm^-3), updated on output
|
||||
pub ane: f64,
|
||||
/// Boltzmann constant (erg/K)
|
||||
pub bolk: f64,
|
||||
/// Total hydrogen abundance YTOT
|
||||
pub ytot: f64,
|
||||
/// Reference atom is hydrogen flag
|
||||
pub is_h_ref: bool,
|
||||
/// Molecular flag (>0 to consider molecules)
|
||||
pub ifmol: i32,
|
||||
/// Molecular temperature limit
|
||||
pub tmolim: f64,
|
||||
/// Previous electron density ratio (anerel)
|
||||
pub anerel: f64,
|
||||
/// Standard partition function PFSTD(1,1)
|
||||
pub pfstd_h: f64,
|
||||
}
|
||||
|
||||
/// Result of ELDENS calculation.
|
||||
pub struct EldensResult {
|
||||
/// Electron density (cm^-3)
|
||||
pub ane: f64,
|
||||
/// Proton number density (cm^-3)
|
||||
pub anp: f64,
|
||||
/// Total hydrogen number density (cm^-3)
|
||||
pub ahtot: f64,
|
||||
/// Hydrogen molecule fraction
|
||||
pub ahmol: f64,
|
||||
/// Negative hydrogen ion density
|
||||
pub anhmi: f64,
|
||||
/// Updated electron density ratio
|
||||
pub anerel: f64,
|
||||
/// Mean molecular weight update factor
|
||||
pub wmm_factor: f64,
|
||||
}
|
||||
|
||||
/// Electron density calculation by Newton-Raphson method.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `params` - Input parameters
|
||||
/// * `state_fn` - Callback to STATE subroutine: (id, t, ane) -> (q, dqn)
|
||||
/// * `lineqs_fn` - Callback to LINEQS: (a, b, n) -> solution vector
|
||||
/// * `moleq_fn` - Callback to MOLEQ: (id, t, an, aein, mode) -> ane
|
||||
///
|
||||
/// # Returns
|
||||
/// Updated electron density and related quantities.
|
||||
#[allow(unused_assignments)]
|
||||
#[allow(unused_assignments)]
|
||||
#[allow(unused_assignments)]
|
||||
pub fn eldens<S, L, M>(
|
||||
params: &EldensParams,
|
||||
state_fn: S,
|
||||
lineqs_fn: L,
|
||||
moleq_fn: M,
|
||||
) -> EldensResult
|
||||
where
|
||||
S: Fn(usize, f64, f64) -> (f64, f64),
|
||||
L: Fn(&mut [f64], &mut [f64], usize) -> Vec<f64>,
|
||||
M: Fn(usize, f64, f64, f64, i32) -> f64,
|
||||
{
|
||||
let t = params.t;
|
||||
let an = params.an;
|
||||
let mut ane = params.ane;
|
||||
let mut anerel = params.anerel;
|
||||
let bolk = params.bolk;
|
||||
|
||||
// Constants
|
||||
let un = 1.0_f64;
|
||||
let two = 2.0_f64;
|
||||
let half = 0.5_f64;
|
||||
|
||||
// Check molecular regime
|
||||
if params.ifmol > 0 && t < params.tmolim {
|
||||
let aein = an * anerel;
|
||||
let ane_mol = moleq_fn(params.id, t, an, aein, 0);
|
||||
return EldensResult {
|
||||
ane: ane_mol,
|
||||
anp: 0.0,
|
||||
ahtot: 0.0,
|
||||
ahmol: 0.0,
|
||||
anhmi: 0.0,
|
||||
anerel: ane_mol / an,
|
||||
wmm_factor: 1.0,
|
||||
};
|
||||
}
|
||||
|
||||
// Initialize coefficients
|
||||
let mut qm = 0.0_f64;
|
||||
let mut q2 = 0.0_f64;
|
||||
let mut qp = 0.0_f64;
|
||||
let mut q = 0.0_f64;
|
||||
let mut dqn = 0.0_f64;
|
||||
let tk = bolk * t;
|
||||
let thet = 5.0404e3 / t;
|
||||
|
||||
// Hydrogen ionization/dissociation coefficients
|
||||
let (q0, ih2) = if params.is_h_ref {
|
||||
let qm_val = 1.0353e-16 / t / t.sqrt() * (8762.9 / t).exp();
|
||||
let qh0 = ((15.38287 + 1.5 * t.log10() - 13.595 * thet) * std::f64::consts::LN_10).exp();
|
||||
|
||||
let (ih2, qp_val, q2_val) = if t > 16000.0 {
|
||||
(0, 0.0, 0.0)
|
||||
} else {
|
||||
let qp = tk * ((-11.206998 + thet * (2.7942767 + thet * (0.079196803 - 0.024790744 * thet)))
|
||||
* std::f64::consts::LN_10)
|
||||
.exp();
|
||||
let q2 = tk * ((-12.533505 + thet * (4.9251644 + thet * (-0.056191273 + 0.0032687661 * thet)))
|
||||
* std::f64::consts::LN_10)
|
||||
.exp();
|
||||
(1, qp, q2)
|
||||
};
|
||||
qm = qm_val;
|
||||
qp = qp_val;
|
||||
q2 = q2_val;
|
||||
(qh0, ih2)
|
||||
} else {
|
||||
(0.0, 0)
|
||||
};
|
||||
|
||||
// Initial estimate of electron density
|
||||
if anerel <= 0.0 {
|
||||
anerel = if t > 1.0e4 {
|
||||
0.5
|
||||
} else {
|
||||
0.1 // Default if no previous data
|
||||
};
|
||||
}
|
||||
ane = an * anerel;
|
||||
|
||||
// Newton-Raphson loop
|
||||
let mut ah = 0.0_f64;
|
||||
let mut anh = 0.0_f64;
|
||||
let mut it = 0;
|
||||
let mut delne;
|
||||
|
||||
loop {
|
||||
it += 1;
|
||||
|
||||
// Call STATE to get total charge Q and its derivative DQN
|
||||
let (q_val, dqn_val) = state_fn(params.id, t, ane);
|
||||
q = q_val;
|
||||
dqn = dqn_val;
|
||||
|
||||
if params.is_h_ref {
|
||||
let qh = q0 * 2.0 / params.pfstd_h;
|
||||
|
||||
// Auxiliary parameters
|
||||
let g2 = qh / ane;
|
||||
let g3 = qm * ane;
|
||||
let a = un + g2 + g3;
|
||||
let d = g2 - g3;
|
||||
|
||||
if it <= 1 {
|
||||
if ih2 == 0 {
|
||||
let f1 = un / a;
|
||||
let fe = d / a + q;
|
||||
ah = ane / fe;
|
||||
anh = ah * f1;
|
||||
} else {
|
||||
let e = g2 * qp / q2;
|
||||
let b = two * (un + e);
|
||||
let gg = ane * q2;
|
||||
let c1 = b * (gg * b + a * d) - e * a * a;
|
||||
let c2 = a * (two * e + b * q) - d * b;
|
||||
let c3 = -e - b * q;
|
||||
let f1 = ((c2 * c2 - 4.0 * c1 * c3).sqrt() - c2) * half / c1;
|
||||
let fe = f1 * d + e * (un - a * f1) / b + q;
|
||||
ah = ane / fe;
|
||||
anh = ah * f1;
|
||||
}
|
||||
}
|
||||
|
||||
let ae = anh / ane;
|
||||
let gg = ae * qp;
|
||||
let _e = anh * q2;
|
||||
let b = anh * qm;
|
||||
|
||||
// Matrix of linearized system R (3x3) and rhs S
|
||||
let mut r = [0.0_f64; 9];
|
||||
let mut s = [0.0_f64; 3];
|
||||
|
||||
r[0] = params.ytot; // R(1,1)
|
||||
r[1] = -two * (anh * q2 + gg); // R(1,2)
|
||||
r[2] = un; // R(1,3)
|
||||
r[3] = -q; // R(2,1)
|
||||
r[4] = -d - two * gg; // R(2,2)
|
||||
r[5] = un + b + ae * (g2 + gg) - dqn * ah; // R(2,3)
|
||||
r[6] = -un; // R(3,1)
|
||||
r[7] = a + 4.0 * (anh * q2 + gg); // R(3,2)
|
||||
r[8] = b - ae * (g2 + two * gg); // R(3,3)
|
||||
|
||||
s[0] = an - ane - params.ytot * ah + anh * (anh * q2 + gg);
|
||||
s[1] = anh * (d + gg) + q * ah - ane;
|
||||
s[2] = ah - anh * (a + two * (anh * q2 + gg));
|
||||
|
||||
// Solve linear system
|
||||
let p = lineqs_fn(&mut r, &mut s, 3);
|
||||
|
||||
ah += p[0];
|
||||
anh += p[1];
|
||||
delne = p[2];
|
||||
ane += delne;
|
||||
} else {
|
||||
// Hydrogen is not the reference atom
|
||||
if it == 1 {
|
||||
ane = an * half;
|
||||
ah = ane / params.ytot;
|
||||
}
|
||||
|
||||
let mut r = [0.0_f64; 4];
|
||||
let mut s = [0.0_f64; 2];
|
||||
|
||||
r[0] = params.ytot; // R(1,1)
|
||||
r[1] = un; // R(1,2)
|
||||
r[2] = -q; // R(2,1) - using QREF=0 for now
|
||||
r[3] = un - dqn * ah; // R(2,2) - using DQNR=0 for now
|
||||
|
||||
s[0] = an - ane - params.ytot * ah;
|
||||
s[1] = q * ah - ane;
|
||||
|
||||
let p = lineqs_fn(&mut r, &mut s, 2);
|
||||
|
||||
ah += p[0];
|
||||
delne = p[1];
|
||||
ane += delne;
|
||||
}
|
||||
|
||||
// Convergence check
|
||||
if ane <= 0.0 {
|
||||
ane = 1.0e-7 * an;
|
||||
}
|
||||
if (delne / ane).abs() <= 1.0e-6 || it > 20 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Update anerel for subsequent calls
|
||||
anerel = ane / an;
|
||||
let ahtot = ah;
|
||||
|
||||
// Compute hydrogen molecule quantities
|
||||
let (ahmol, anp, anhmi, wmm_factor) = if params.is_h_ref {
|
||||
let qh = q0 * 2.0 / params.pfstd_h;
|
||||
let ahmol = anh * anh * q2;
|
||||
let anp = anh / ane * qh;
|
||||
let anhmi = anh * ane * qm;
|
||||
let anhn = anh + anp + anhmi + 2.0 * ahmol;
|
||||
let wmm_factor = if anhn > 0.0 {
|
||||
1.0 / (1.0 - ahmol / anhn)
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
(ahmol, anp, anhmi, wmm_factor)
|
||||
} else {
|
||||
(0.0, 0.0, 0.0, 1.0)
|
||||
};
|
||||
|
||||
EldensResult {
|
||||
ane,
|
||||
anp,
|
||||
ahtot,
|
||||
ahmol,
|
||||
anhmi,
|
||||
anerel,
|
||||
wmm_factor,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_eldens_basic() {
|
||||
let params = EldensParams {
|
||||
id: 0,
|
||||
t: 10000.0,
|
||||
an: 1e15,
|
||||
ane: 0.0,
|
||||
bolk: 1.380658e-16,
|
||||
ytot: 1.0,
|
||||
is_h_ref: true,
|
||||
ifmol: 0,
|
||||
tmolim: 9000.0,
|
||||
anerel: 0.1,
|
||||
pfstd_h: 2.0,
|
||||
};
|
||||
|
||||
// Mock state: return (q, dqn)
|
||||
let state_fn = |_id: usize, _t: f64, ane: f64| {
|
||||
(ane * 0.5, 0.5)
|
||||
};
|
||||
|
||||
// Mock lineqs: simple 3x3 solver
|
||||
let lineqs_fn = |a: &mut [f64], b: &mut [f64], n: usize| -> Vec<f64> {
|
||||
// For test, just return small corrections
|
||||
vec![0.0; n]
|
||||
};
|
||||
|
||||
// Mock moleq
|
||||
let moleq_fn = |_id: usize, _t: f64, _an: f64, _aein: f64, _mode: i32| -> f64 {
|
||||
1e14
|
||||
};
|
||||
|
||||
let result = eldens(¶ms, state_fn, lineqs_fn, moleq_fn);
|
||||
assert!(result.ane > 0.0, "ANE should be positive");
|
||||
assert!(result.anerel > 0.0, "ANEREL should be positive");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_eldens_molecular() {
|
||||
let params = EldensParams {
|
||||
id: 0,
|
||||
t: 5000.0, // Below tmolim
|
||||
an: 1e15,
|
||||
ane: 0.0,
|
||||
bolk: 1.380658e-16,
|
||||
ytot: 1.0,
|
||||
is_h_ref: true,
|
||||
ifmol: 1, // Enable molecular
|
||||
tmolim: 9000.0,
|
||||
anerel: 0.1,
|
||||
pfstd_h: 2.0,
|
||||
};
|
||||
|
||||
let state_fn = |_id: usize, _t: f64, _ane: f64| (0.0, 0.0);
|
||||
let lineqs_fn = |_a: &mut [f64], _b: &mut [f64], n: usize| vec![0.0; n];
|
||||
let moleq_fn = |_id: usize, _t: f64, _an: f64, _aein: f64, _mode: i32| 1e14;
|
||||
|
||||
let result = eldens(¶ms, state_fn, lineqs_fn, moleq_fn);
|
||||
assert!(result.ane > 0.0, "Should use moleq in molecular regime");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,327 @@
|
||||
//! EOS parameter output diagnostics.
|
||||
//!
|
||||
//! Translated from SYNSPEC54.FOR subroutine EOSPRI (line 22799).
|
||||
//!
|
||||
//! Prints equation of state parameters including atomic, ionic, and molecular
|
||||
//! number densities and partition functions. Also computes H2+ abundance
|
||||
//! and element ratios (He/H, C/H, N/H, O/H).
|
||||
|
||||
/// Molecular indices used for output (20 selected molecules).
|
||||
#[allow(dead_code)]
|
||||
const INSM: [usize; 20] = [2, 3, 4, 5, 6, 7, 8, 12, 17, 25, 29, 30, 32, 34, 122, 126, 134, 179, 198, 214];
|
||||
|
||||
/// Element indices for metals (38 elements).
|
||||
const NELEMX: [usize; 38] = [
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
11, 12, 13, 14, 15, 16, 17, 19, 20,
|
||||
21, 22, 23, 24, 25, 26, 28, 29, 32,
|
||||
35, 37, 38, 39, 40, 41, 53, 56, 57, 58, 60,
|
||||
];
|
||||
|
||||
/// H2+ dissociation constant polynomial coefficients (B&C).
|
||||
const AMH2: [f64; 5] = [1.13390e+01, -2.97499e+00, 4.10842e-02, -3.58550e-03, 1.31844e-04];
|
||||
|
||||
/// Parameters for EOSPRI.
|
||||
pub struct EospriParams<'a> {
|
||||
/// Number of depth points
|
||||
pub nd: usize,
|
||||
/// Temperature array (K)
|
||||
pub temp: &'a [f64],
|
||||
/// Electron density array (cm^-3)
|
||||
pub elec: &'a [f64],
|
||||
/// Mass density array (g/cm^3)
|
||||
pub dens: &'a [f64],
|
||||
/// Mean molecular weight array
|
||||
pub wmm: &'a [f64],
|
||||
/// Mean molecular weight for EOS
|
||||
pub wmy: &'a [f64],
|
||||
/// Hydrogen mass
|
||||
pub hmass: f64,
|
||||
/// Total abundance YTOT
|
||||
pub ytot: &'a [f64],
|
||||
/// Abundance by depth: abndd(element, depth) — 1-indexed element
|
||||
pub abndd: &'a [Vec<f64>],
|
||||
/// Molecular flag
|
||||
pub ifmol: i32,
|
||||
/// Molecular temperature limit
|
||||
pub tmolim: f64,
|
||||
/// Number of molecules
|
||||
pub nmolec: usize,
|
||||
/// Molecular names
|
||||
pub cmol: &'a [String],
|
||||
/// EOS flag
|
||||
pub ifeos: i32,
|
||||
/// Number of metals
|
||||
pub nmetal: usize,
|
||||
/// Step for depth loop
|
||||
pub istp: usize,
|
||||
}
|
||||
|
||||
/// Result of EOSPRI computation.
|
||||
pub struct EospriOutput {
|
||||
/// Atomic number densities per depth: anato[element][depth]
|
||||
pub anato: Vec<Vec<f64>>,
|
||||
/// Ionic number densities per depth: anion[element][depth]
|
||||
pub anion: Vec<Vec<f64>>,
|
||||
/// Molecular number densities per depth: anmol[molecule][depth]
|
||||
pub anmol: Vec<Vec<f64>>,
|
||||
/// Atomic partition functions per depth
|
||||
pub pfato: Vec<Vec<f64>>,
|
||||
/// Ionic partition functions per depth
|
||||
pub pfion: Vec<Vec<f64>>,
|
||||
/// Molecular partition functions per depth
|
||||
pub pfmol: Vec<Vec<f64>>,
|
||||
/// H- density per depth
|
||||
pub anhmi_per_depth: Vec<f64>,
|
||||
/// H2 density per depth
|
||||
pub ahmol_per_depth: Vec<f64>,
|
||||
/// H density per depth
|
||||
pub ah_per_depth: Vec<f64>,
|
||||
/// H+ density per depth
|
||||
pub anp_per_depth: Vec<f64>,
|
||||
/// Second ionization per depth: anion2[element][depth]
|
||||
pub anion2: Vec<Vec<f64>>,
|
||||
/// Summary lines per depth
|
||||
pub summary: Vec<String>,
|
||||
}
|
||||
|
||||
/// ELDENS result used by EOSPRI.
|
||||
pub struct EldensSimpleResult {
|
||||
/// Electron density (cm^-3)
|
||||
pub ane: f64,
|
||||
/// Proton number density (cm^-3)
|
||||
pub anp: f64,
|
||||
/// Total hydrogen number density (cm^-3)
|
||||
pub ahtot: f64,
|
||||
/// Hydrogen molecule fraction
|
||||
pub ahmol: f64,
|
||||
/// Negative hydrogen ion density
|
||||
pub anhmi: f64,
|
||||
}
|
||||
|
||||
/// Compute EOS parameters for diagnostics.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `params` - Input parameters
|
||||
/// * `eldens_fn` - Callback: (id, t, ann, ane) -> EldensSimpleResult
|
||||
///
|
||||
/// Calls `eldens_fn` iteratively to converge molecular equilibrium,
|
||||
/// then computes element ratios and formatted output.
|
||||
pub fn eospri<E>(params: &EospriParams, eldens_fn: E) -> EospriOutput
|
||||
where
|
||||
E: Fn(usize, f64, f64, f64) -> EldensSimpleResult,
|
||||
{
|
||||
let max_elem = 100;
|
||||
let max_mol = 600;
|
||||
let nd = params.nd;
|
||||
|
||||
let mut anato = vec![vec![0.0_f64; nd]; max_elem];
|
||||
let mut anion = vec![vec![0.0_f64; nd]; max_elem];
|
||||
let mut anmol = vec![vec![0.0_f64; nd]; max_mol];
|
||||
let pfato = vec![vec![0.0_f64; nd]; max_elem];
|
||||
let pfion = vec![vec![0.0_f64; nd]; max_elem];
|
||||
let pfmol = vec![vec![0.0_f64; nd]; max_mol];
|
||||
let mut anion2 = vec![vec![0.0_f64; nd]; 30];
|
||||
let mut anhmi_per_depth = vec![0.0_f64; nd];
|
||||
let mut ahmol_per_depth = vec![0.0_f64; nd];
|
||||
let mut ah_per_depth = vec![0.0_f64; nd];
|
||||
let mut anp_per_depth = vec![0.0_f64; nd];
|
||||
let mut summary = Vec::new();
|
||||
|
||||
let istp = if params.istp == 0 { 1 } else { params.istp };
|
||||
|
||||
for id in (0..nd).step_by(istp) {
|
||||
let t = params.temp[id];
|
||||
let mut ane = params.elec[id];
|
||||
let rho = params.dens[id];
|
||||
let mut ann = rho / params.wmm[id] + ane;
|
||||
|
||||
// Iterative convergence for molecular equilibrium
|
||||
if params.ifmol == 0 || t > params.tmolim {
|
||||
let mut ann0;
|
||||
loop {
|
||||
ann0 = ann;
|
||||
let result = eldens_fn(id, t, ann, ane);
|
||||
ane = result.ane;
|
||||
|
||||
anmol[0][id] = result.anhmi;
|
||||
anmol[1][id] = result.ahmol;
|
||||
anato[0][id] = result.ahtot;
|
||||
anion[0][id] = result.anp;
|
||||
anhmi_per_depth[id] = result.anhmi;
|
||||
ahmol_per_depth[id] = result.ahmol;
|
||||
ah_per_depth[id] = result.ahtot;
|
||||
anp_per_depth[id] = result.anp;
|
||||
|
||||
let hpop = rho / params.wmy[id] / params.hmass;
|
||||
for &j in NELEMX.iter().take(params.nmetal) {
|
||||
if j < max_elem {
|
||||
anato[j][id] *= hpop;
|
||||
anion[j][id] *= hpop;
|
||||
if (2..30).contains(&j) {
|
||||
anion2[j][id] *= hpop;
|
||||
}
|
||||
}
|
||||
}
|
||||
anato[0][id] = result.ahtot;
|
||||
anion[0][id] = result.anp;
|
||||
|
||||
// Update mean molecular weight
|
||||
// wmm(id) = wmy(id) / (ytot(id) - anmol(2,id)/hpop) * hmass
|
||||
let ahmol_hpop = anmol[1][id] / hpop;
|
||||
let new_wmm = if params.ytot[id] - ahmol_hpop > 0.0 {
|
||||
params.wmy[id] / (params.ytot[id] - ahmol_hpop) * params.hmass
|
||||
} else {
|
||||
params.wmm[id]
|
||||
};
|
||||
ann = rho / new_wmm + ane;
|
||||
|
||||
if (ann - ann0) / ann0 <= 1.0e-5 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compute H2+ abundance (B&C polynomial)
|
||||
let te = 5040.0 / t;
|
||||
let mut aplogj = AMH2[4];
|
||||
for k in 0..4 {
|
||||
let km5 = 4 - k;
|
||||
aplogj = aplogj * te + AMH2[km5];
|
||||
}
|
||||
let tk = 1.38054e-16 * t;
|
||||
let ph2 = -aplogj + (anato[0][id] * anion[0][id]).log10() + 2.0 * tk.log10();
|
||||
let _anh2b = 10.0_f64.powf(ph2) / tk;
|
||||
|
||||
// Compute total hydrogen
|
||||
let htot = anato[0][id] + anion[0][id] + anmol[0][id]
|
||||
+ 2.0 * (anmol[1][id] + anmol[2][id])
|
||||
+ anmol[3][id] + anmol[4][id]
|
||||
+ anmol[11][id] + 2.0 * anmol[12][id] + anmol[13][id]
|
||||
+ anmol[14][id]
|
||||
+ anmol[15][id] + anmol[16][id] + anmol[31][id] + anmol[33][id]
|
||||
+ 4.0 * anmol[36][id] + 2.0 * anmol[37][id] + 3.0 * anmol[38][id]
|
||||
+ 2.0 * anmol[39][id] + 3.0 * anmol[40][id] + 2.0 * anmol[56][id]
|
||||
+ anmol[117][id] + anmol[132][id]
|
||||
+ 2.0 * anmol[139][id] + 3.0 * anmol[140][id] + 4.0 * anmol[141][id]
|
||||
+ anmol[147][id] + 2.0 * anmol[148][id] + anmol[221][id];
|
||||
|
||||
// Element ratios relative to H
|
||||
let ahe = if htot > 0.0 { (anato[1][id] + anion[1][id] + anion2[1][id]) / htot } else { 0.0 };
|
||||
let aca = if htot > 0.0 { (anato[5][id] + anion[5][id] + anion2[5][id]) / htot } else { 0.0 };
|
||||
let acm = if htot > 0.0 {
|
||||
(anmol[4][id] + anmol[5][id]
|
||||
+ anmol[6][id] + 2.0 * (anmol[7][id] + 2.0 * anmol[12][id])
|
||||
+ anmol[13][id] + 2.0 * anmol[14][id] + anmol[19][id]
|
||||
+ anmol[36][id] + anmol[37][id] + anmol[38][id]
|
||||
+ anmol[43][id] + anmol[117][id] + anmol[118][id]
|
||||
+ anmol[436][id] + anmol[452][id])
|
||||
/ htot
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
let ana = if htot > 0.0 { (anato[6][id] + anion[6][id] + anion2[6][id]) / htot } else { 0.0 };
|
||||
let anm = if htot > 0.0 {
|
||||
(anmol[6][id] + 2.0 * anmol[8][id] + anmol[10][id]
|
||||
+ anmol[11][id] + anmol[13][id] + anmol[22][id]
|
||||
+ anmol[23][id] + anmol[39][id] + anmol[40][id]
|
||||
+ anmol[108][id] + anmol[151][id] + anmol[346][id]
|
||||
+ anmol[437][id] + anmol[451][id] + anmol[453][id])
|
||||
/ htot
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
let aoa = if htot > 0.0 { (anato[7][id] + anion[7][id] + anion2[7][id]) / htot } else { 0.0 };
|
||||
let aom = if htot > 0.0 {
|
||||
(anmol[2][id] + anmol[3][id]
|
||||
+ anmol[5][id] + 2.0 * anmol[9][id] + anmol[10][id] + anmol[24][id]
|
||||
+ anmol[25][id] + anmol[28][id] + anmol[29][id] + anmol[30][id]
|
||||
+ anmol[34][id] + 2.0 * anmol[43][id] + anmol[48][id] + anmol[50][id]
|
||||
+ anmol[53][id] + 2.0 * anmol[55][id] + anmol[64][id]
|
||||
+ 2.0 * anmol[65][id] + anmol[83][id] + anmol[108][id]
|
||||
+ anmol[112][id] + anmol[114][id] + anmol[117][id]
|
||||
+ anmol[118][id] + anmol[125][id] + anmol[133][id]
|
||||
+ anmol[152][id] + anmol[178][id] + anmol[183][id]
|
||||
+ 2.0 * anmol[184][id] + anmol[199][id] + anmol[215][id]
|
||||
+ anmol[220][id] + 2.0 * anmol[246][id] + anmol[291][id]
|
||||
+ anmol[438][id] + anmol[452][id] + anmol[453][id])
|
||||
/ htot
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
let ac = aca + acm;
|
||||
let an = ana + anm;
|
||||
let ao = aoa + aom;
|
||||
|
||||
// Format summary
|
||||
let line = format!(
|
||||
"EOS: T={:.1} rho={:.3e} N={:.3e} Ne={:.3e} Htot={:.3e} He/H={:.3e} C/H={:.3e} N/H={:.3e} O/H={:.3e}",
|
||||
t, rho, ann, ane, htot, ahe, ac, an, ao
|
||||
);
|
||||
summary.push(line);
|
||||
}
|
||||
|
||||
EospriOutput {
|
||||
anato,
|
||||
anion,
|
||||
anmol,
|
||||
pfato,
|
||||
pfion,
|
||||
pfmol,
|
||||
anhmi_per_depth,
|
||||
ahmol_per_depth,
|
||||
ah_per_depth,
|
||||
anp_per_depth,
|
||||
anion2,
|
||||
summary,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_eospri_constants() {
|
||||
assert_eq!(NELEMX.len(), 38);
|
||||
assert_eq!(INSM.len(), 20);
|
||||
assert_eq!(AMH2.len(), 5);
|
||||
assert_eq!(NELEMX[0], 1);
|
||||
assert_eq!(NELEMX[37], 60);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_eospri_basic() {
|
||||
let nd = 1;
|
||||
let abndd_data: Vec<Vec<f64>> = vec![vec![0.0; nd]; 100];
|
||||
let params = EospriParams {
|
||||
nd,
|
||||
temp: &[10000.0],
|
||||
elec: &[1.0e12],
|
||||
dens: &[1.0e-10],
|
||||
wmm: &[1.0],
|
||||
wmy: &[1.0],
|
||||
hmass: 1.67e-24,
|
||||
ytot: &[1.0],
|
||||
abndd: &abndd_data,
|
||||
ifmol: 0,
|
||||
tmolim: 10000.0,
|
||||
nmolec: 0,
|
||||
cmol: &[],
|
||||
ifeos: 0,
|
||||
nmetal: 0,
|
||||
istp: 1,
|
||||
};
|
||||
// Mock eldens callback
|
||||
let eldens_fn = |_id: usize, _t: f64, ann: f64, ane: f64| -> EldensSimpleResult {
|
||||
EldensSimpleResult {
|
||||
ane,
|
||||
anp: ane * 0.9,
|
||||
ahtot: ann * 0.8,
|
||||
ahmol: 0.0,
|
||||
anhmi: 0.0,
|
||||
}
|
||||
};
|
||||
let _output = eospri(¶ms, eldens_fn);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
//! EXOMOL partition functions for 32 molecular species.
|
||||
//!
|
||||
//! Translated from SYNSPEC `EXOPF` subroutine (synspec54.f:23664).
|
||||
//!
|
||||
//! Reads tabulated partition function data from `data/EXOMOL/*.pf` files
|
||||
//! on first call, then performs simple lookup or Irwin-based extrapolation.
|
||||
|
||||
use std::sync::Mutex;
|
||||
|
||||
use super::irwpf;
|
||||
|
||||
/// Number of molecular species.
|
||||
const NMOL: usize = 32;
|
||||
|
||||
/// Molecular species filenames (Fortran `character*4`, leading space stripped).
|
||||
const FILPF: [&str; NMOL] = [
|
||||
"AlO", "C2", "CH", "CN", "CO",
|
||||
"CS", "CaH", "CaO", "CrH", "FeH",
|
||||
"H2", "HCl", "HF", "MgH", "MgO",
|
||||
"N2", "NH", "NO", "NS", "NaH",
|
||||
"OH", "PH", "SH", "SiH", "SiO",
|
||||
"SiS", "TiH", "TiO", "VO",
|
||||
"H2O", "H2S", "CO2",
|
||||
];
|
||||
|
||||
/// Number of temperature points per species (before scaling).
|
||||
const NTEMP_RAW: [usize; NMOL] = [
|
||||
9, 10, 8, 3, 9, 3, 3, 8, 3, 10,
|
||||
10, 5, 5, 3, 5, 9, 5, 5, 5, 5,
|
||||
5, 4, 5, 5, 9, 5, 48, 8, 8, 10,
|
||||
3, 5,
|
||||
];
|
||||
|
||||
/// Tsuji molecular indices for each species.
|
||||
const INDTSU: [i32; NMOL] = [
|
||||
134, 8, 5, 7, 6, 20, 34, 179, 198, 214,
|
||||
2, 36, 33, 32, 126, 9, 12, 11, 23, 122,
|
||||
4, 148, 16, 17, 25, 28, 315, 29, 30, 3,
|
||||
57, 44,
|
||||
];
|
||||
|
||||
/// Cached EXOMOL data: partition functions `pf[mol][temp_index]` and
|
||||
/// scaled temperature counts `ntemp[mol]`.
|
||||
struct ExopfData {
|
||||
/// Partition function values: pf[mol * max_ntemp + j]
|
||||
/// Stored flat; max_ntemp = 48000 (48*1000).
|
||||
pf: Vec<f64>,
|
||||
/// Scaled temperature counts per species.
|
||||
ntemp: Vec<usize>,
|
||||
}
|
||||
|
||||
static EXOPF_DATA: Mutex<Option<ExopfData>> = Mutex::new(None);
|
||||
|
||||
/// Compute the file path for a given species.
|
||||
fn species_filename(name: &str) -> String {
|
||||
let trimmed = name.trim();
|
||||
format!("data/EXOMOL/{}.pf", trimmed)
|
||||
}
|
||||
|
||||
/// Read all EXOMOL partition function files.
|
||||
fn read_exopf_data(data_dir: &str) -> Result<ExopfData, String> {
|
||||
// Scale ntemp: multiply by 1000, except species 27 (TiH) divide by 10
|
||||
let mut ntemp = Vec::with_capacity(NMOL);
|
||||
for i in 0..NMOL {
|
||||
let mut nt = NTEMP_RAW[i] * 1000;
|
||||
if i == 26 {
|
||||
// TiH: ntemp(27) in Fortran (1-indexed) = index 26
|
||||
nt /= 10;
|
||||
}
|
||||
ntemp.push(nt);
|
||||
}
|
||||
|
||||
let max_ntemp = *ntemp.iter().max().unwrap_or(&0);
|
||||
let mut pf = vec![0.0f64; NMOL * max_ntemp];
|
||||
|
||||
for i in 0..NMOL {
|
||||
let filename = format!("{}/{}", data_dir, species_filename(FILPF[i]));
|
||||
let content = match std::fs::read_to_string(&filename) {
|
||||
Ok(c) => c,
|
||||
Err(_) => continue, // Skip missing files
|
||||
};
|
||||
for (j, line) in content.lines().enumerate() {
|
||||
if j >= ntemp[i] {
|
||||
break;
|
||||
}
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() >= 2
|
||||
&& let Ok(val) = parts[1].parse::<f64>() {
|
||||
pf[i * max_ntemp + j] = val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(ExopfData { pf, ntemp })
|
||||
}
|
||||
|
||||
/// EXOMOL partition function lookup.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `indmol` - Tsuji molecular index.
|
||||
/// * `t` - Temperature (K).
|
||||
/// * `data_dir` - Path to data directory containing `data/EXOMOL/*.pf` files.
|
||||
///
|
||||
/// # Returns
|
||||
/// Partition function value (0.0 if species not found).
|
||||
pub fn exopf(indmol: i32, t: f64, data_dir: &str) -> Result<f64, String> {
|
||||
// Initialize data on first call
|
||||
{
|
||||
let mut guard = EXOPF_DATA
|
||||
.lock()
|
||||
.map_err(|e| format!("Lock error: {}", e))?;
|
||||
if guard.is_none() {
|
||||
*guard = Some(read_exopf_data(data_dir)?);
|
||||
}
|
||||
}
|
||||
let guard = EXOPF_DATA
|
||||
.lock()
|
||||
.map_err(|e| format!("Lock error: {}", e))?;
|
||||
let data = guard.as_ref().unwrap();
|
||||
|
||||
// Find species index
|
||||
let ie = INDTSU.iter().position(|&x| x == indmol);
|
||||
let ie = match ie {
|
||||
Some(idx) => idx,
|
||||
None => return Ok(0.0),
|
||||
};
|
||||
|
||||
let tmax = data.ntemp[ie] as f64;
|
||||
let max_ntemp = *data.ntemp.iter().max().unwrap_or(&1);
|
||||
|
||||
if t <= tmax {
|
||||
// Direct lookup
|
||||
let j = t as usize;
|
||||
if j > 0 && j <= data.ntemp[ie] {
|
||||
Ok(data.pf[ie * max_ntemp + j - 1])
|
||||
} else {
|
||||
Ok(0.0)
|
||||
}
|
||||
} else {
|
||||
// Extrapolate using Irwin partition functions
|
||||
// Need to drop the lock before calling irwpf (which also locks)
|
||||
drop(guard);
|
||||
|
||||
let umx = irwpf::irwpf(0, 0, indmol, tmax, data_dir, 1).unwrap_or(1.0);
|
||||
let uirw = irwpf::irwpf(0, 0, indmol, t, data_dir, 1).unwrap_or(1.0);
|
||||
|
||||
// Re-acquire lock to read pf value
|
||||
let guard = EXOPF_DATA
|
||||
.lock()
|
||||
.map_err(|e| format!("Lock error: {}", e))?;
|
||||
let data = guard.as_ref().unwrap();
|
||||
|
||||
if umx.abs() > 1e-30 {
|
||||
Ok(data.pf[ie * max_ntemp + data.ntemp[ie] - 1] / umx * uirw)
|
||||
} else {
|
||||
Ok(0.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_species_count() {
|
||||
assert_eq!(FILPF.len(), NMOL);
|
||||
assert_eq!(NTEMP_RAW.len(), NMOL);
|
||||
assert_eq!(INDTSU.len(), NMOL);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ntemp_scaling() {
|
||||
// TiH (index 26): 48*1000/10 = 4800
|
||||
assert_eq!(NTEMP_RAW[26] * 1000 / 10, 4800);
|
||||
// Others: raw * 1000
|
||||
assert_eq!(NTEMP_RAW[0] * 1000, 9000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_species_filename() {
|
||||
assert_eq!(species_filename("AlO"), "data/EXOMOL/AlO.pf");
|
||||
assert_eq!(species_filename(" H2"), "data/EXOMOL/H2.pf");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_indtsu_mapping() {
|
||||
// H2O is last entry
|
||||
assert_eq!(INDTSU[29], 3);
|
||||
// CO is index 4
|
||||
assert_eq!(INDTSU[4], 6);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
//! 第一指数积分函数 E1(x)。
|
||||
//!
|
||||
//! 重构自 SYNSPEC `expint.f`
|
||||
|
||||
/// 第一指数积分函数 E1(x)。
|
||||
///
|
||||
/// 使用有理逼近公式,分 x <= 1 和 x > 1 两段。
|
||||
///
|
||||
/// # 参数
|
||||
///
|
||||
/// * `x` - 自变量
|
||||
///
|
||||
/// # 返回值
|
||||
///
|
||||
/// E1(x) 的近似值
|
||||
pub fn expint(x: f64) -> f64 {
|
||||
if x <= 1.0 {
|
||||
// x <= 1 的有理逼近
|
||||
-x.ln() - 0.57721566
|
||||
+ x * (0.99999193
|
||||
+ x * (-0.24991055
|
||||
+ x * (0.05519968
|
||||
+ x * (-0.00976004 + x * 0.00107857))))
|
||||
} else {
|
||||
// x > 1 的有理逼近
|
||||
(-x).exp() * ((0.2677734343
|
||||
+ x * (8.6347608925
|
||||
+ x * (18.059016973 + x * (8.5733287401 + x))))
|
||||
/ (3.9584969228
|
||||
+ x * (21.0996530827
|
||||
+ x * (25.6329561486 + x * (9.5733223454 + x)))))
|
||||
/ x
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use approx::assert_relative_eq;
|
||||
|
||||
#[test]
|
||||
fn test_expint_small_x() {
|
||||
// E1(0.5) ≈ 0.5598 (参考值)
|
||||
let result = expint(0.5);
|
||||
assert!(result.is_finite());
|
||||
assert!(result > 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_expint_x_eq_1() {
|
||||
// E1(1) ≈ 0.21938
|
||||
let result = expint(1.0);
|
||||
assert_relative_eq!(result, 0.21938, epsilon = 1e-4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_expint_large_x() {
|
||||
// E1(5) ≈ 0.001148
|
||||
let result = expint(5.0);
|
||||
assert_relative_eq!(result, 0.0011483, epsilon = 1e-3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_expint_very_small_x() {
|
||||
// x -> 0+ 时 E1(x) -> +∞
|
||||
let result = expint(0.01);
|
||||
assert!(result > 0.0);
|
||||
assert!(result.is_finite());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_expint_large_x_decay() {
|
||||
// x 大时 E1(x) ~ exp(-x)/x
|
||||
let x = 10.0;
|
||||
let result = expint(x);
|
||||
let approx = (-x).exp() / x;
|
||||
assert_relative_eq!(result, approx, epsilon = 0.1);
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,7 @@ pub fn extprf(dlam: f64, it: usize, iline: usize, anel: f64, dlast: f64, plast:
|
||||
|
||||
// WE = W0 * 10^anel * 1e-16
|
||||
// Fortran: EXP(ANEL*2.3025851) = 10^ANEL (因为 ln(10) ≈ 2.3025851)
|
||||
let we = w0_val * (anel * 2.3025851_f64).exp() * 1e-16;
|
||||
let we = w0_val * (anel * std::f64::consts::LN_10).exp() * 1e-16;
|
||||
|
||||
// 使用 PI 的精确值
|
||||
const PI: f64 = std::f64::consts::PI;
|
||||
|
||||
@@ -0,0 +1,484 @@
|
||||
//! fingrd — 存储完整的插值不透明度表。
|
||||
//!
|
||||
//! Fortran 原始签名: SUBROUTINE FINGRD
|
||||
//!
|
||||
//! 将计算的不透明度表写入文件(文本和二进制格式)。
|
||||
//!
|
||||
//! 注意: Fortran 版本直接操作文件 I/O 和 COMMON 块。
|
||||
//! Rust 版本提供纯计算核心函数和编排函数。
|
||||
|
||||
use std::io::{BufWriter, Write};
|
||||
use std::fs::File;
|
||||
|
||||
/// 光速 (cm/s)
|
||||
#[allow(dead_code)]
|
||||
const CL: f64 = 2.997925e10;
|
||||
|
||||
/// 波长 (nm) 转换为频率 (s^-1)
|
||||
///
|
||||
/// Fortran 原始逻辑:
|
||||
/// ```fortran
|
||||
/// 2.997925e18/wlgrid(k)
|
||||
/// ```
|
||||
pub fn wavelength_to_frequency(wavelength_nm: f64) -> f64 {
|
||||
2.997925e18 / wavelength_nm
|
||||
}
|
||||
|
||||
/// 频率 (s^-1) 转换为波长 (nm)
|
||||
pub fn frequency_to_wavelength(freq: f64) -> f64 {
|
||||
2.997925e18 / freq
|
||||
}
|
||||
|
||||
/// 对数网格生成
|
||||
///
|
||||
/// Fortran 原始逻辑:
|
||||
/// ```fortran
|
||||
/// wl1=log(wlam1)
|
||||
/// wl2=log(wlam2)
|
||||
/// dwl=(wl2-wl1)/(nfgrid-1)
|
||||
/// do i=1,nfgrid
|
||||
/// wlgrid(i)=exp(wl1+(i-1)*dwl)
|
||||
/// end do
|
||||
/// ```
|
||||
pub fn generate_log_grid(wlam1: f64, wlam2: f64, n: usize) -> Vec<f64> {
|
||||
let wl1 = wlam1.ln();
|
||||
let wl2 = wlam2.ln();
|
||||
let dwl = (wl2 - wl1) / (n - 1) as f64;
|
||||
|
||||
(0..n)
|
||||
.map(|i| (wl1 + i as f64 * dwl).exp())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// 线性网格生成
|
||||
///
|
||||
/// Fortran 原始逻辑:
|
||||
/// ```fortran
|
||||
/// at1=log(temp1)
|
||||
/// at2=log(temp2)
|
||||
/// dt=(at2-at1)/(ntemp-1)
|
||||
/// do i=1,ntemp
|
||||
/// tempg(i)=exp(at1+(i-1)*dt)
|
||||
/// end do
|
||||
/// ```
|
||||
pub fn generate_linear_grid_in_log(val1: f64, val2: f64, n: usize) -> Vec<f64> {
|
||||
let at1 = val1.ln();
|
||||
let at2 = val2.ln();
|
||||
let dt = if n > 1 { (at2 - at1) / (n - 1) as f64 } else { 0.0 };
|
||||
|
||||
(0..n)
|
||||
.map(|i| (at1 + i as f64 * dt).exp())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// 不透明度表数据结构
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct OpacityTable {
|
||||
/// 温度网格 (K)
|
||||
pub temperatures: Vec<f64>,
|
||||
/// 密度网格 (g/cm^3)
|
||||
pub densities: Vec<Vec<f64>>,
|
||||
/// 电子密度网格 (g/cm^3)
|
||||
pub electron_densities: Vec<Vec<f64>>,
|
||||
/// 波长网格 (nm)
|
||||
pub wavelengths: Vec<f64>,
|
||||
/// 不透明度表 [temp_idx][dens_idx][freq_idx]
|
||||
pub opacity: Vec<Vec<Vec<f32>>>,
|
||||
}
|
||||
|
||||
/// 计算不透明度表的统计信息
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct OpacityTableStats {
|
||||
/// 最小不透明度
|
||||
pub min_opacity: f32,
|
||||
/// 最大不透明度
|
||||
pub max_opacity: f32,
|
||||
/// 平均不透明度
|
||||
pub mean_opacity: f32,
|
||||
/// 非零元素百分比
|
||||
pub nonzero_percent: f64,
|
||||
}
|
||||
|
||||
/// 计算不透明度表统计信息
|
||||
pub fn compute_opacity_stats(table: &OpacityTable) -> OpacityTableStats {
|
||||
let mut min_op = f32::MAX;
|
||||
let mut max_op = f32::MIN;
|
||||
let mut sum = 0.0_f64;
|
||||
let mut count = 0;
|
||||
let mut nonzero = 0;
|
||||
|
||||
for temp_data in &table.opacity {
|
||||
for dens_data in temp_data {
|
||||
for &op in dens_data {
|
||||
count += 1;
|
||||
sum += op as f64;
|
||||
if op > 0.0 {
|
||||
nonzero += 1;
|
||||
}
|
||||
if op < min_op {
|
||||
min_op = op;
|
||||
}
|
||||
if op > max_op {
|
||||
max_op = op;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
OpacityTableStats {
|
||||
min_opacity: min_op,
|
||||
max_opacity: max_op,
|
||||
mean_opacity: if count > 0 { (sum / count as f64) as f32 } else { 0.0 },
|
||||
nonzero_percent: if count > 0 { 100.0 * nonzero as f64 / count as f64 } else { 0.0 },
|
||||
}
|
||||
}
|
||||
|
||||
/// H- 不透明度标志
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Default)]
|
||||
pub struct OpacityFlags {
|
||||
/// H- 光电离
|
||||
pub h_minus: bool,
|
||||
/// H2+ 光电离
|
||||
pub h2_plus: bool,
|
||||
/// He- 光电离
|
||||
pub he_minus: bool,
|
||||
/// CH 不透明度
|
||||
pub ch: bool,
|
||||
/// OH 不透明度
|
||||
pub oh: bool,
|
||||
/// H2- 不透明度
|
||||
pub h2_minus: bool,
|
||||
/// CIA H2-H2
|
||||
pub cia_h2h2: bool,
|
||||
/// CIA H2-He
|
||||
pub cia_h2he: bool,
|
||||
/// CIA H2-H
|
||||
pub cia_h2h: bool,
|
||||
/// CIA H-He
|
||||
pub cia_hhe: bool,
|
||||
}
|
||||
|
||||
|
||||
/// 不透明度表写入参数
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FingrdParams<'a> {
|
||||
/// 温度网格 (K)
|
||||
pub temperatures: &'a [f64],
|
||||
/// 密度网格 [temp_idx][dens_idx] (g/cm^3)
|
||||
pub densities: &'a [Vec<f64>],
|
||||
/// 电子密度网格 [temp_idx][dens_idx] (g/cm^3)
|
||||
pub electron_densities: &'a [Vec<f64>],
|
||||
/// 波长网格 (nm)
|
||||
pub wavelengths: &'a [f64],
|
||||
/// 不透明度表 [temp_idx][dens_idx][freq_idx] (f32)
|
||||
pub absgrd: &'a [Vec<Vec<f32>>],
|
||||
/// 每温度点的密度数 nden(temp_idx)
|
||||
pub nden: &'a [usize],
|
||||
/// 元素丰度 abnd(92)
|
||||
pub abundances: &'a [f64],
|
||||
/// 相对丰度 relabn(92)
|
||||
pub rel_abundances: &'a [f64],
|
||||
/// 不透明度标志
|
||||
pub flags: &'a OpacityFlags,
|
||||
/// 分子开关 ifmol
|
||||
pub ifmol: i32,
|
||||
/// 分子温度极限 tmolim
|
||||
pub tmolim: f64,
|
||||
/// 输出表文件名
|
||||
pub tabname: &'a str,
|
||||
/// 二进制输出标志 (0=text+binary, 1=binary only)
|
||||
pub ibingr: i32,
|
||||
/// 密度类型 (<10: uniform, >=10: variable)
|
||||
pub idens: i32,
|
||||
}
|
||||
|
||||
/// 编排函数: 将不透明度表写入文本和二进制文件。
|
||||
///
|
||||
/// Fortran 原始逻辑: SUBROUTINE FINGRD
|
||||
/// - 文本输出到 tabname 文件 (Fortran unit 53)
|
||||
/// - 二进制输出到 unit 63
|
||||
pub fn fingrd(params: &FingrdParams) -> Result<(), String> {
|
||||
let ntemp = params.temperatures.len();
|
||||
let nfgrid = params.wavelengths.len();
|
||||
if ntemp == 0 || nfgrid == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let nden0 = params.nden.first().copied().unwrap_or(1);
|
||||
|
||||
// --- 文本输出 (ibingr == 0) ---
|
||||
if params.ibingr == 0 {
|
||||
let file = File::create(params.tabname)
|
||||
.map_err(|e| format!("Cannot create {}: {}", params.tabname, e))?;
|
||||
let mut w = BufWriter::new(file);
|
||||
|
||||
// Header: element abundances
|
||||
writeln!(w, "opacity table with element abundances:").map_err(|e| e.to_string())?;
|
||||
writeln!(w, "element for EOS for opacities").map_err(|e| e.to_string())?;
|
||||
for iat in 0..92 {
|
||||
let abnd = params.abundances.get(iat).copied().unwrap_or(0.0);
|
||||
let rel = params.rel_abundances.get(iat).copied().unwrap_or(0.0);
|
||||
writeln!(w, " {:4} {:12.3e} {:12.3e}", iat + 1, abnd, abnd * rel)
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
|
||||
// Molecule info
|
||||
writeln!(w).map_err(|e| e.to_string())?;
|
||||
writeln!(w, "molecules - ifmol,tmolim:").map_err(|e| e.to_string())?;
|
||||
writeln!(w, "{:4}{:10.1}", params.ifmol, params.tmolim).map_err(|e| e.to_string())?;
|
||||
|
||||
// Opacity flags
|
||||
writeln!(w, "additional opacities").map_err(|e| e.to_string())?;
|
||||
writeln!(w, " H- H2+ He- CH OH H2- CIA: H2H2 H2He H2H HHe").map_err(|e| e.to_string())?;
|
||||
let f = params.flags;
|
||||
writeln!(w, "{:4}{:4}{:4}{:4}{:4}{:4} {:4}{:4}{:4}{:4}",
|
||||
f.h_minus as i32, f.h2_plus as i32, f.he_minus as i32,
|
||||
f.ch as i32, f.oh as i32, f.h2_minus as i32,
|
||||
f.cia_h2h2 as i32, f.cia_h2he as i32, f.cia_h2h as i32, f.cia_hhe as i32)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
if params.idens < 10 {
|
||||
// Uniform density grid
|
||||
let ndens = nden0;
|
||||
writeln!(w).map_err(|e| e.to_string())?;
|
||||
writeln!(w, "number of frequencies, temperatures, densities:").map_err(|e| e.to_string())?;
|
||||
writeln!(w, " {:10}{:10}{:10}", nfgrid, ntemp, ndens).map_err(|e| e.to_string())?;
|
||||
|
||||
// Log temperatures
|
||||
write!(w, "log temperatures").map_err(|e| e.to_string())?;
|
||||
for i in 0..ntemp {
|
||||
if i % 6 == 0 { writeln!(w).map_err(|e| e.to_string())?; }
|
||||
write!(w, "{:11.6}", params.temperatures[i].ln()).map_err(|e| e.to_string())?;
|
||||
}
|
||||
writeln!(w).map_err(|e| e.to_string())?;
|
||||
|
||||
// Log densities
|
||||
write!(w, "log densities").map_err(|e| e.to_string())?;
|
||||
for j in 0..ndens {
|
||||
if j % 6 == 0 { writeln!(w).map_err(|e| e.to_string())?; }
|
||||
let d = params.densities[0].get(j).copied().unwrap_or(1.0);
|
||||
write!(w, "{:11.6}", d.ln()).map_err(|e| e.to_string())?;
|
||||
}
|
||||
writeln!(w).map_err(|e| e.to_string())?;
|
||||
|
||||
// Log electron densities
|
||||
write!(w, "log electron densities from EOS").map_err(|e| e.to_string())?;
|
||||
for i in 0..ntemp {
|
||||
for j in 0..ndens {
|
||||
if (i * ndens + j) % 6 == 0 { writeln!(w).map_err(|e| e.to_string())?; }
|
||||
let e = params.electron_densities[i].get(j).copied().unwrap_or(1.0);
|
||||
write!(w, "{:11.6}", e.ln()).map_err(|e| e.to_string())?;
|
||||
}
|
||||
}
|
||||
writeln!(w).map_err(|e| e.to_string())?;
|
||||
|
||||
// Opacity table
|
||||
for k in 0..nfgrid {
|
||||
writeln!(w).map_err(|e| e.to_string())?;
|
||||
writeln!(w, " *** frequency # : {:8}{:15.5}", k + 1, params.wavelengths[k])
|
||||
.map_err(|e| e.to_string())?;
|
||||
let freq = 2.997925e18 / params.wavelengths[k];
|
||||
writeln!(w, "{:20.8e}", freq).map_err(|e| e.to_string())?;
|
||||
for j in 0..ndens {
|
||||
for i in 0..ntemp {
|
||||
if i % 6 == 0 { writeln!(w).map_err(|e| e.to_string())?; }
|
||||
let val = params.absgrd[i][j].get(k).copied().unwrap_or(0.0);
|
||||
write!(w, "{:14.6e}", val).map_err(|e| e.to_string())?;
|
||||
}
|
||||
writeln!(w).map_err(|e| e.to_string())?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Variable density grid
|
||||
writeln!(w).map_err(|e| e.to_string())?;
|
||||
writeln!(w, "number of frequencies, temperatures, densities:").map_err(|e| e.to_string())?;
|
||||
writeln!(w, " {:10}{:10}{:10}", nfgrid, ntemp, -(nden0 as i32)).map_err(|e| e.to_string())?;
|
||||
|
||||
// nden per temperature
|
||||
for i in 0..ntemp {
|
||||
write!(w, "{:3}", params.nden.get(i).copied().unwrap_or(0)).map_err(|e| e.to_string())?;
|
||||
}
|
||||
writeln!(w).map_err(|e| e.to_string())?;
|
||||
|
||||
// Log temperatures
|
||||
write!(w, "log temperatures").map_err(|e| e.to_string())?;
|
||||
for i in 0..ntemp {
|
||||
if i % 6 == 0 { writeln!(w).map_err(|e| e.to_string())?; }
|
||||
write!(w, "{:11.6}", params.temperatures[i].ln()).map_err(|e| e.to_string())?;
|
||||
}
|
||||
writeln!(w).map_err(|e| e.to_string())?;
|
||||
|
||||
// Log densities per temperature
|
||||
writeln!(w, "log densities").map_err(|e| e.to_string())?;
|
||||
for i in 0..ntemp {
|
||||
let nd = params.nden.get(i).copied().unwrap_or(0);
|
||||
for j in 0..nd {
|
||||
if j % 6 == 0 && j > 0 { writeln!(w).map_err(|e| e.to_string())?; }
|
||||
let d = params.densities[i].get(j).copied().unwrap_or(1.0);
|
||||
write!(w, "{:14.6}", d.ln()).map_err(|e| e.to_string())?;
|
||||
}
|
||||
writeln!(w).map_err(|e| e.to_string())?;
|
||||
}
|
||||
|
||||
// Log electron densities per temperature
|
||||
writeln!(w, "log electron densities from EOS").map_err(|e| e.to_string())?;
|
||||
for i in 0..ntemp {
|
||||
let nd = params.nden.get(i).copied().unwrap_or(0);
|
||||
for j in 0..nd {
|
||||
if j % 6 == 0 && j > 0 { writeln!(w).map_err(|e| e.to_string())?; }
|
||||
let e = params.electron_densities[i].get(j).copied().unwrap_or(1.0);
|
||||
write!(w, "{:14.6}", e.ln()).map_err(|e| e.to_string())?;
|
||||
}
|
||||
writeln!(w).map_err(|e| e.to_string())?;
|
||||
}
|
||||
|
||||
// Opacity table
|
||||
for k in 0..nfgrid {
|
||||
writeln!(w).map_err(|e| e.to_string())?;
|
||||
writeln!(w, " *** frequency # : {:8}{:15.5}", k + 1, params.wavelengths[k])
|
||||
.map_err(|e| e.to_string())?;
|
||||
let freq = 2.997925e18 / params.wavelengths[k];
|
||||
writeln!(w, "{:20.8e}", freq).map_err(|e| e.to_string())?;
|
||||
for i in 0..ntemp {
|
||||
let nd = params.nden.get(i).copied().unwrap_or(0);
|
||||
for j in 0..nd {
|
||||
if j % 6 == 0 { writeln!(w).map_err(|e| e.to_string())?; }
|
||||
let val = params.absgrd[i].get(j).and_then(|row| row.get(k)).copied().unwrap_or(0.0);
|
||||
write!(w, "{:14.6e}", val).map_err(|e| e.to_string())?;
|
||||
}
|
||||
writeln!(w).map_err(|e| e.to_string())?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- 二进制输出 (always) ---
|
||||
// Note: Binary output requires Fortran-compatible unformatted I/O.
|
||||
// In Rust, we write a simplified binary format.
|
||||
// The actual binary format depends on the Fortran runtime.
|
||||
// For now, we skip binary output as it requires Fortran unit 63.
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_wavelength_to_frequency() {
|
||||
// 500 nm → frequency
|
||||
let freq = wavelength_to_frequency(500.0);
|
||||
let expected = 2.997925e18 / 500.0;
|
||||
assert!((freq - expected).abs() / expected < 1e-10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_frequency_to_wavelength() {
|
||||
let wl = frequency_to_wavelength(6e14);
|
||||
let expected = 2.997925e18 / 6e14;
|
||||
assert!((wl - expected).abs() / expected < 1e-10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wavelength_frequency_roundtrip() {
|
||||
let wl = 500.0;
|
||||
let freq = wavelength_to_frequency(wl);
|
||||
let wl_back = frequency_to_wavelength(freq);
|
||||
assert!((wl - wl_back).abs() < 1e-10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_log_grid() {
|
||||
let grid = generate_log_grid(100.0, 1000.0, 11);
|
||||
assert_eq!(grid.len(), 11);
|
||||
assert!((grid[0] - 100.0).abs() < 1e-10);
|
||||
assert!((grid[10] - 1000.0).abs() < 1e-10);
|
||||
// 网格应该是对数等距的
|
||||
let ratio = grid[1] / grid[0];
|
||||
for i in 1..10 {
|
||||
assert!((grid[i + 1] / grid[i] - ratio).abs() < 1e-10);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_linear_grid_in_log() {
|
||||
let grid = generate_linear_grid_in_log(1000.0, 100000.0, 5);
|
||||
assert_eq!(grid.len(), 5);
|
||||
assert!((grid[0] - 1000.0).abs() < 1e-10);
|
||||
assert!((grid[4] - 100000.0).abs() < 1e-3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compute_opacity_stats() {
|
||||
let table = OpacityTable {
|
||||
temperatures: vec![5000.0, 10000.0],
|
||||
densities: vec![vec![1e-8, 1e-7]],
|
||||
electron_densities: vec![vec![1e-10, 1e-9]],
|
||||
wavelengths: vec![100.0, 200.0],
|
||||
opacity: vec![
|
||||
vec![
|
||||
vec![1.0, 2.0],
|
||||
vec![3.0, 4.0],
|
||||
],
|
||||
vec![
|
||||
vec![5.0, 6.0],
|
||||
vec![7.0, 8.0],
|
||||
],
|
||||
],
|
||||
};
|
||||
let stats = compute_opacity_stats(&table);
|
||||
assert_eq!(stats.min_opacity, 1.0);
|
||||
assert_eq!(stats.max_opacity, 8.0);
|
||||
assert!((stats.mean_opacity - 4.5).abs() < 0.01);
|
||||
assert_eq!(stats.nonzero_percent, 100.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_opacity_flags_default() {
|
||||
let flags = OpacityFlags::default();
|
||||
assert!(!flags.h_minus);
|
||||
assert!(!flags.cia_h2h2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fingrd_writes_text_file() {
|
||||
let dir = std::env::temp_dir().join("fingrd_test");
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let tabname = dir.join("test_table.txt");
|
||||
let tabname_str = tabname.to_str().unwrap();
|
||||
|
||||
let params = FingrdParams {
|
||||
temperatures: &[5000.0, 10000.0],
|
||||
densities: &[vec![1e-8, 1e-7], vec![1e-8, 1e-7]],
|
||||
electron_densities: &[vec![1e-10, 1e-9], vec![1e-10, 1e-9]],
|
||||
wavelengths: &[100.0, 200.0, 500.0],
|
||||
absgrd: &[
|
||||
vec![vec![1.0, 2.0, 3.0], vec![4.0, 5.0, 6.0]],
|
||||
vec![vec![7.0, 8.0, 9.0], vec![10.0, 11.0, 12.0]],
|
||||
],
|
||||
nden: &[2, 2],
|
||||
abundances: &[1.0; 92],
|
||||
rel_abundances: &[1.0; 92],
|
||||
flags: &OpacityFlags::default(),
|
||||
ifmol: 0,
|
||||
tmolim: 10000.0,
|
||||
tabname: tabname_str,
|
||||
ibingr: 0,
|
||||
idens: 0,
|
||||
};
|
||||
|
||||
let result = fingrd(¶ms);
|
||||
assert!(result.is_ok());
|
||||
|
||||
// Verify file was created and has content
|
||||
let content = std::fs::read_to_string(&tabname).unwrap();
|
||||
assert!(content.contains("opacity table"));
|
||||
assert!(content.contains("number of frequencies"));
|
||||
assert!(content.contains("frequency #"));
|
||||
|
||||
// Cleanup
|
||||
std::fs::remove_dir_all(&dir).ok();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
//! Opacity Project ionization fraction interpolation for SYNSPEC.
|
||||
//!
|
||||
//! Translated from SYNSPEC54.FOR subroutine FRAC1 (line 23240).
|
||||
//!
|
||||
//! Interpolates pre-tabulated ionization fractions from the Opacity Project
|
||||
//! data (read by FRACTN) to the local temperature and electron density at
|
||||
//! each depth point, then computes the number density of each ionization
|
||||
//! stage.
|
||||
//!
|
||||
//! # Input
|
||||
//!
|
||||
//! - Temperature and electron density arrays
|
||||
//! - OP ionization fraction table (from FRACTN)
|
||||
//! - Elemental abundances, mean molecular weight, total density
|
||||
//!
|
||||
//! # Output
|
||||
//!
|
||||
//! - `rrr[id][ion][iat]` — number density fraction for element `iat`,
|
||||
//! ionization stage `ion` at depth `id`
|
||||
|
||||
// ============================================================================
|
||||
// 常量
|
||||
// ============================================================================
|
||||
|
||||
/// Maximum number of temperature grid points
|
||||
pub const MTEMP: usize = 100;
|
||||
/// Maximum number of electron density grid points
|
||||
pub const MELEC: usize = 60;
|
||||
/// Maximum number of ionization stages
|
||||
pub const MION1: usize = 30;
|
||||
|
||||
// ============================================================================
|
||||
// OP 数据结构 (COMMON /FRACOP/)
|
||||
// ============================================================================
|
||||
|
||||
/// Opacity Project ionization fraction table.
|
||||
///
|
||||
/// Corresponds to Fortran COMMON /FRACOP/:
|
||||
/// ```fortran
|
||||
/// COMMON/FRACOP/ frac(mtemp,melec,mion1), fracm(mtemp,melec),
|
||||
/// itemp(mtemp), ntt
|
||||
/// ```
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FracOpData {
|
||||
/// Ionization fractions [MTEMP x MELEC x MION1]
|
||||
pub frac: Vec<Vec<Vec<f64>>>,
|
||||
/// Molecular fractions [MTEMP x MELEC]
|
||||
pub fracm: Vec<Vec<f64>>,
|
||||
/// Temperature grid indices [MTEMP]
|
||||
pub itemp: Vec<i32>,
|
||||
/// Number of temperature points
|
||||
pub ntt: usize,
|
||||
}
|
||||
|
||||
impl Default for FracOpData {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
frac: vec![vec![vec![0.0; MION1]; MELEC]; MTEMP],
|
||||
fracm: vec![vec![0.0; MELEC]; MTEMP],
|
||||
itemp: vec![0; MTEMP],
|
||||
ntt: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 参数结构体
|
||||
// ============================================================================
|
||||
|
||||
/// Parameters for FRAC1 calculation.
|
||||
pub struct Frac1Params<'a> {
|
||||
/// Number of depth points
|
||||
pub nd: usize,
|
||||
/// Temperature array [nd] (K)
|
||||
pub temp: &'a [f64],
|
||||
/// Electron density array [nd] (cm^-3)
|
||||
pub elec: &'a [f64],
|
||||
/// Total density array [nd] (g/cm^3)
|
||||
pub dens: &'a [f64],
|
||||
/// Mean molecular weight array [nd]
|
||||
pub wmm: &'a [f64],
|
||||
/// Total hydrogen fraction array [nd]
|
||||
pub ytot: &'a [f64],
|
||||
/// Elemental abundance [30 x nd] — abndd(iat, id)
|
||||
pub abndd: &'a [&'a [f64]],
|
||||
/// OP ionization fraction table (from FRACTN)
|
||||
pub fracop: &'a FracOpData,
|
||||
/// Maximum number of elements to process (typically 30)
|
||||
pub max_elements: usize,
|
||||
}
|
||||
|
||||
/// Result of FRAC1 calculation.
|
||||
pub struct Frac1Result {
|
||||
/// Number density fraction [nd x MION1 x 30] — rrr(id, ion, iat)
|
||||
pub rrr: Vec<Vec<Vec<f64>>>,
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 核心计算
|
||||
// ============================================================================
|
||||
|
||||
/// Compute ionization fractions by interpolation of OP data.
|
||||
///
|
||||
/// For each depth point, computes log10(T) and log10(Ne), then
|
||||
/// bilinearly interpolates the pre-tabulated OP ionization fractions
|
||||
/// to get the number density of each ionization stage.
|
||||
pub fn frac1(params: &Frac1Params) -> Frac1Result {
|
||||
let nd = params.nd;
|
||||
let fracop = params.fracop;
|
||||
let ntt = fracop.ntt;
|
||||
|
||||
let mut rrr = vec![vec![vec![0.0; MION1]; 30]; nd];
|
||||
|
||||
if ntt == 0 {
|
||||
return Frac1Result { rrr };
|
||||
}
|
||||
|
||||
// Compute log10(T) and log10(Ne) for each depth
|
||||
let mut xxt = vec![0.0f64; nd];
|
||||
let mut xxe = vec![0.0f64; nd];
|
||||
let mut kt0 = vec![0i32; nd];
|
||||
let mut kn0 = vec![0i32; nd];
|
||||
|
||||
for id in 0..nd {
|
||||
xxt[id] = params.temp[id].log10();
|
||||
kt0[id] = 2 * (20.0 * xxt[id]) as i32;
|
||||
xxe[id] = params.elec[id].log10();
|
||||
kn0[id] = (2.0 * xxe[id]) as i32;
|
||||
}
|
||||
|
||||
// Loop over elements
|
||||
for iat in 0..params.max_elements.min(30) {
|
||||
// Find temperature index for each depth
|
||||
for id in 0..nd {
|
||||
let kt1 = find_temp_index(kt0[id], &fracop.itemp, ntt);
|
||||
let kn1 = find_elec_index(kn0[id]);
|
||||
|
||||
// Bilinear interpolation coefficients
|
||||
let xt1 = 0.025 * fracop.itemp[kt1] as f64;
|
||||
let dxt = 0.05;
|
||||
let at1 = (xxt[id] - xt1) / dxt;
|
||||
let xn1 = 0.5 * kn1 as f64;
|
||||
let dxn = 0.5;
|
||||
let an1 = (xxe[id] - xn1) / dxn;
|
||||
|
||||
// Interpolate each ionization stage
|
||||
for ion in 0..MION1 {
|
||||
let x11 = fracop.frac[kt1][kn1][ion];
|
||||
let x21 = fracop.frac[kt1 + 1][kn1][ion];
|
||||
let x12 = fracop.frac[kt1][kn1 + 1][ion];
|
||||
let x22 = fracop.frac[kt1 + 1][kn1 + 1][ion];
|
||||
|
||||
let rrx = if x11 * x21 * x12 * x22 == 0.0 {
|
||||
// Linear interpolation when any value is zero
|
||||
let xx1 = x11 + at1 * (x21 - x11);
|
||||
let xx2 = x12 + at1 * (x22 - x12);
|
||||
xx1 + an1 * (xx2 - xx1)
|
||||
} else {
|
||||
// Log-space interpolation
|
||||
let lx11 = x11.log10();
|
||||
let lx21 = x21.log10();
|
||||
let lx12 = x12.log10();
|
||||
let lx22 = x22.log10();
|
||||
let xx1 = lx11 + at1 * (lx21 - lx11);
|
||||
let xx2 = lx12 + at1 * (lx22 - lx12);
|
||||
let lrrx = xx1 + an1 * (xx2 - xx1);
|
||||
10f64.powf(lrrx)
|
||||
};
|
||||
|
||||
rrr[id][ion][iat] = rrx * params.abndd[iat][id]
|
||||
* params.dens[id] / params.wmm[id] / params.ytot[id];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Frac1Result { rrr }
|
||||
}
|
||||
|
||||
/// Find temperature index in OP table.
|
||||
///
|
||||
/// Returns the index `kt1` such that `itemp[kt1] <= kt0 < itemp[kt1+1]`.
|
||||
fn find_temp_index(kt0: i32, itemp: &[i32], ntt: usize) -> usize {
|
||||
if ntt == 0 {
|
||||
return 0;
|
||||
}
|
||||
if kt0 < itemp[0] {
|
||||
return 0;
|
||||
}
|
||||
if kt0 >= itemp[ntt - 1] {
|
||||
return ntt - 1;
|
||||
}
|
||||
for it in 0..ntt {
|
||||
if kt0 == itemp[it] {
|
||||
return it;
|
||||
}
|
||||
}
|
||||
// Fallback: find bracketing interval
|
||||
for it in 0..ntt - 1 {
|
||||
if kt0 >= itemp[it] && kt0 < itemp[it + 1] {
|
||||
return it;
|
||||
}
|
||||
}
|
||||
ntt - 1
|
||||
}
|
||||
|
||||
/// Find electron density index in OP table.
|
||||
///
|
||||
/// Returns the index `kn1` such that `kn1*0.5 <= log10(Ne) < (kn1+1)*0.5`.
|
||||
fn find_elec_index(kn0: i32) -> usize {
|
||||
if kn0 < 1 {
|
||||
0
|
||||
} else if kn0 >= 60 {
|
||||
59
|
||||
} else {
|
||||
kn0 as usize
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 测试
|
||||
// ============================================================================
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_frac1_empty_table() {
|
||||
let temp = [10000.0];
|
||||
let elec = [1e14];
|
||||
let dens = [1e-10];
|
||||
let wmm = [1.0];
|
||||
let ytot = [1.0];
|
||||
let abnd_row = vec![0.0; 1];
|
||||
let abndd: Vec<&[f64]> = vec![&abnd_row; 30];
|
||||
let fracop = FracOpData::default();
|
||||
|
||||
let params = Frac1Params {
|
||||
nd: 1,
|
||||
temp: &temp,
|
||||
elec: &elec,
|
||||
dens: &dens,
|
||||
wmm: &wmm,
|
||||
ytot: &ytot,
|
||||
abndd: &abndd,
|
||||
fracop: &fracop,
|
||||
max_elements: 30,
|
||||
};
|
||||
|
||||
let result = frac1(¶ms);
|
||||
assert_eq!(result.rrr.len(), 1);
|
||||
assert_eq!(result.rrr[0].len(), MION1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_temp_index() {
|
||||
let itemp = [100, 200, 300, 400, 500];
|
||||
assert_eq!(find_temp_index(50, &itemp, 5), 0); // below range
|
||||
assert_eq!(find_temp_index(100, &itemp, 5), 0); // exact match
|
||||
assert_eq!(find_temp_index(300, &itemp, 5), 2); // exact match
|
||||
assert_eq!(find_temp_index(600, &itemp, 5), 4); // above range
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_elec_index() {
|
||||
assert_eq!(find_elec_index(-1), 0);
|
||||
assert_eq!(find_elec_index(0), 0);
|
||||
assert_eq!(find_elec_index(10), 10);
|
||||
assert_eq!(find_elec_index(70), 59);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fracop_default() {
|
||||
let data = FracOpData::default();
|
||||
assert_eq!(data.ntt, 0);
|
||||
assert_eq!(data.frac.len(), MTEMP);
|
||||
assert_eq!(data.frac[0].len(), MELEC);
|
||||
assert_eq!(data.frac[0][0].len(), MION1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,466 @@
|
||||
//! 电离分数数据读取 (FRACTN)。
|
||||
//!
|
||||
//! 从 `ioniz.dat` 文件读取 OP 电离分数表,计算各元素的电离分数。
|
||||
//!
|
||||
//! # 功能
|
||||
//!
|
||||
//! 读取电离势和统计权重数据,结合温度和电子密度网格,
|
||||
//! 计算各电离态的分数分布。
|
||||
//!
|
||||
//! # Fortran 原始代码
|
||||
//!
|
||||
//! ```fortran
|
||||
//! subroutine fractn(iatnum)
|
||||
//! common/fracop/frac(mtemp,melec,mion1),fracm(mtemp,melec),
|
||||
//! itemp(mtemp),ntt
|
||||
//! ...
|
||||
//! end
|
||||
//! ```
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
|
||||
use super::frac1::{MTEMP, MELEC, MION1};
|
||||
|
||||
// ============================================================================
|
||||
// 常量
|
||||
// ============================================================================
|
||||
|
||||
/// 最大数据集数
|
||||
pub const MDAT: usize = 17;
|
||||
|
||||
// ============================================================================
|
||||
// 数据结构
|
||||
// ============================================================================
|
||||
|
||||
/// FRACTN 输出 - 电离分数表。
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FracOp {
|
||||
/// 电离分数 [MTEMP][MELEC][MION1]
|
||||
/// frac[it][ie][ion] = 元素 iatnum 在温度 it、电子密度 ie 下的电离态 ion 分数
|
||||
pub frac: Vec<Vec<Vec<f64>>>,
|
||||
/// 负离子分数 [MTEMP][MELEC]
|
||||
pub fracm: Vec<Vec<f64>>,
|
||||
/// 温度索引数组 [MTEMP]
|
||||
pub itemp: Vec<i32>,
|
||||
/// 有效温度点数
|
||||
pub ntt: usize,
|
||||
}
|
||||
|
||||
impl FracOp {
|
||||
/// 创建新的空 FracOp。
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
frac: vec![vec![vec![0.0; MION1]; MELEC]; MTEMP],
|
||||
fracm: vec![vec![0.0; MELEC]; MTEMP],
|
||||
itemp: vec![0; MTEMP],
|
||||
ntt: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for FracOp {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 静态数据
|
||||
// ============================================================================
|
||||
|
||||
/// 各元素的数据集索引 (IDAT)
|
||||
/// 索引从 1 开始(iatnum = 1..30),0 表示无数据
|
||||
const IDAT: [usize; 31] = [
|
||||
0, // 占位
|
||||
1, 2, 0, 0, 0, 3, 4, 5, 0, 6,
|
||||
7, 8, 9, 10, 0, 11, 0, 12, 0, 13,
|
||||
0, 0, 0, 14, 15, 16, 0, 17, 0, 0,
|
||||
];
|
||||
|
||||
/// 统计权重 GG(ion, dataset)
|
||||
/// 使用一维数组存储,索引: (ion-1) * MDAT + (dataset-1)
|
||||
const GG_DATA: [[f64; MDAT]; MION1] = [
|
||||
[2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],
|
||||
[0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
|
||||
[0., 0., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],
|
||||
[0., 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
|
||||
[0., 0., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],
|
||||
[0., 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
|
||||
[0., 0., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6.],
|
||||
[0., 0., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],
|
||||
[0., 0., 0., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],
|
||||
[0., 0., 0., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],
|
||||
[0., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
|
||||
[0., 0., 0., 0., 0., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],
|
||||
[0., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 0., 0., 10., 10., 10., 10., 10., 10., 10., 10.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 0., 0., 21., 21., 21., 21., 21., 21., 21., 21.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 0., 0., 28., 28., 28., 28., 28., 28., 28., 28.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 0., 0., 25., 25., 25., 25., 25., 25., 25., 25.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 0., 0., 6., 6., 6., 6., 6., 6., 6., 6.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 0., 0., 7., 7., 7., 7., 7., 7., 7., 7.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 0., 0., 6., 6., 25., 25., 25., 25., 25., 25.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 30., 30., 30., 30., 30., 30.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 25., 25., 25., 25., 25., 25.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 28., 28., 28., 28., 28., 28.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 21., 21., 21., 21., 21., 21.],
|
||||
];
|
||||
|
||||
/// 各数据集的电离势 UU(ion, dataset) * 1000 cm^-1
|
||||
/// 对应 Fortran 的 uu 数组
|
||||
/// 只有非零值需要存储,按 (dataset_index, ion_index) -> value
|
||||
fn get_u0(iatnum: usize, ion_idx: usize) -> f64 {
|
||||
// ion_idx: 1..iatnum (1-indexed)
|
||||
// 对应 Fortran: u0(i) = uu(i, idat(iatnum)) * 1000.
|
||||
let dataset = IDAT[iatnum];
|
||||
if dataset == 0 || ion_idx == 0 || ion_idx > iatnum {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
// 各数据集的 UU 值 (已乘 1000)
|
||||
// 数据集 1: H, He
|
||||
// 数据集 2: Li, Be
|
||||
// 数据集 3: C
|
||||
// 数据集 4: N
|
||||
// 数据集 5: O
|
||||
// 数据集 6: Ne
|
||||
// 数据集 7: Na
|
||||
// 数据集 8: Mg
|
||||
// 数据集 9: Al
|
||||
// 数据集 10: Si
|
||||
// 数据集 11: S
|
||||
// 数据集 12: Ar
|
||||
// 数据集 13: Ca
|
||||
// 数据集 14: Fe
|
||||
// 数据集 15: Ni
|
||||
// 数据集 16: Zn
|
||||
// 数据集 17: Kr
|
||||
|
||||
// 预定义的 UU 数据集 (单位: 1000 cm^-1,已乘 1000)
|
||||
const U_DATASETS: [[f64; 30]; 18] = [
|
||||
// 数据集 0 (未使用)
|
||||
[0.0; 30],
|
||||
// 数据集 1: H (1 ion)
|
||||
[109678.7, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
|
||||
// 数据集 2: Li, Be (2 ions)
|
||||
[198310.8, 438908.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
|
||||
// 数据集 3: C (6 ions)
|
||||
[90820.0, 196665.0, 386241.0, 520178.0, 3162395.0, 3952061.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
|
||||
// 数据集 4: N (7 ions)
|
||||
[117225.0, 238751.0, 382704.0, 624866.0, 789537.0, 4452758.0, 5380089.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
|
||||
// 数据集 5: O (8 ions)
|
||||
[109837.0, 283240.0, 443086.0, 624384.0, 918657.0, 1114008.0, 5963135.0, 7028393.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
|
||||
// 数据集 6: Ne (10 ions)
|
||||
[173930.0, 330391.0, 511800.0, 783300.0, 1018000.0, 1273800.0, 1671792.0, 1928462.0,
|
||||
9645005.0, 10986876.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
|
||||
// 数据集 7: Na (11 ions)
|
||||
[41449.0, 381395.0, 577800.0, 797800.0, 1116200.0, 1388500.0, 1681500.0, 2130800.0,
|
||||
2418700.0, 11817061.0, 13297676.0, 0.0, 0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
|
||||
// 数据集 8: Mg (12 ions)
|
||||
[61671.0, 121268.0, 646410.0, 881100.0, 1139400.0, 1504300.0, 1814300.0, 2144700.0,
|
||||
2645200.0, 2964400.0, 14210261.0, 15829951.0, 0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
|
||||
// 数据集 9: Al (13 ions)
|
||||
[48278.0, 151860.0, 229446.0, 967800.0, 1239800.0, 1536300.0, 1947300.0, 2295400.0,
|
||||
2663400.0, 3214800.0, 3565600.0, 16825022.0, 18584138.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
|
||||
// 数据集 10: Si (14 ions)
|
||||
[65748.0, 131838.0, 270139.0, 364093.0, 1345100.0, 1653900.0, 1988400.0, 2445300.0,
|
||||
2831900.0, 3237800.0, 3839800.0, 4222400.0, 19661693.0, 21560630.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
|
||||
// 数据集 11: S (16 ions)
|
||||
[83558.0, 188200.0, 280900.0, 381541.0, 586200.0, 710184.0, 2265900.0, 2647400.0,
|
||||
3057700.0, 3606100.0, 4071400.0, 4554300.0, 5255900.0, 5703600.0, 26002663.0, 28182535.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
|
||||
// 数据集 12: Ar (18 ions)
|
||||
[127110.0, 222848.0, 328600.0, 482400.0, 605100.0, 734040.0, 1002730.0, 1157080.0,
|
||||
3407300.0, 3860900.0, 4347000.0, 4986600.0, 5533800.0, 6095500.0, 6894200.0, 7404400.0,
|
||||
33237173.0, 35699936.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
|
||||
// 数据集 13: Ca (20 ions)
|
||||
[49306.0, 95752.0, 410642.0, 542600.0, 681600.0, 877400.0, 1026000.0, 1187600.0,
|
||||
1520640.0, 1704047.0, 4774000.0, 5301000.0, 5861000.0, 6595000.0, 7215000.0, 7860000.0,
|
||||
8770000.0, 9338000.0, 41366000.0, 44177410.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
|
||||
// 数据集 14: Fe (24 ions)
|
||||
[54576.0, 132966.0, 249700.0, 396500.0, 560200.0, 731020.0, 1291900.0, 1490000.0,
|
||||
1688000.0, 1971000.0, 2184000.0, 2404000.0, 2862000.0, 3098520.0, 8151000.0, 8850000.0,
|
||||
9560000.0, 10480000.0, 11260000.0, 12070000.0, 13180000.0, 13882000.0, 60344000.0, 63675900.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
|
||||
// 数据集 15: Ni (25 ions)
|
||||
[59959.0, 126145.0, 271550.0, 413000.0, 584000.0, 771100.0, 961440.0, 1569000.0,
|
||||
1789000.0, 2003000.0, 2307000.0, 2536000.0, 2771000.0, 3250000.0, 3509820.0, 9152000.0,
|
||||
9872000.0, 10620000.0, 11590000.0, 12410000.0, 13260000.0, 14420000.0, 15162000.0, 65660000.0,
|
||||
69137400.0, 0.0, 0.0, 0.0, 0.0, 0.0],
|
||||
// 数据集 16: Zn (26 ions)
|
||||
[63737.0, 130563.0, 247220.0, 442000.0, 605000.0, 799000.0, 1008000.0, 1218380.0,
|
||||
1884000.0, 2114000.0, 2341000.0, 2668000.0, 2912000.0, 3163000.0, 3686000.0, 3946820.0,
|
||||
10180000.0, 10985000.0, 11850000.0, 12708000.0, 13620000.0, 14510000.0, 15797000.0, 16500000.0,
|
||||
71203000.0, 74829600.0, 0.0, 0.0, 0.0, 0.0],
|
||||
// 数据集 17: Kr (28 ions)
|
||||
[61600.0, 146542.0, 283800.0, 443000.0, 613500.0, 870000.0, 1070000.0, 1310000.0,
|
||||
1560000.0, 1812000.0, 2589000.0, 2840000.0, 3100000.0, 3470000.0, 3740000.0, 4020000.0,
|
||||
4606000.0, 4896200.0, 12430000.0, 13290000.0, 14160000.0, 15280000.0, 16220000.0, 17190000.0,
|
||||
18510000.0, 19351000.0, 82984000.0, 86909400.0, 0.0, 0.0],
|
||||
];
|
||||
|
||||
let ds = dataset;
|
||||
if ds == 0 || ion_idx > 30 {
|
||||
return 0.0;
|
||||
}
|
||||
U_DATASETS[ds][ion_idx - 1]
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// FRACTN 主函数
|
||||
// ============================================================================
|
||||
|
||||
/// 读取指定元素的电离分数数据。
|
||||
///
|
||||
/// # 参数
|
||||
///
|
||||
/// * `iatnum` - 原子序数 (1..30)。如果数据不存在,返回 `None`。
|
||||
/// * `data_dir` - 数据文件目录(包含 `ioniz.dat`)
|
||||
///
|
||||
/// # 返回值
|
||||
///
|
||||
/// `FracOp` 结构体,包含电离分数表。如果元素无数据,返回 `None`。
|
||||
///
|
||||
/// # Fortran 原始代码
|
||||
///
|
||||
/// ```fortran
|
||||
/// subroutine fractn(iatnum)
|
||||
/// common/fracop/frac(mtemp,melec,mion1),fracm(mtemp,melec),
|
||||
/// itemp(mtemp),ntt
|
||||
/// ...
|
||||
/// end
|
||||
/// ```
|
||||
pub fn fractn(iatnum: usize, data_dir: &str) -> Option<FracOp> {
|
||||
if iatnum == 0 || iatnum > 30 || IDAT[iatnum] == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let file_path = format!("{}/ioniz.dat", data_dir);
|
||||
let file = match File::open(&file_path) {
|
||||
Ok(f) => f,
|
||||
Err(_) => return None,
|
||||
};
|
||||
let reader = BufReader::new(file);
|
||||
let mut lines = reader.lines();
|
||||
|
||||
let mut frac_op = FracOp::new();
|
||||
|
||||
// 设置统计权重和电离势
|
||||
let mut g0 = [0.0f64; MION1 + 2]; // g0(-1:mion1)
|
||||
g0[iatnum + 1] = 1.0;
|
||||
for i in 1..=iatnum {
|
||||
let ig0 = iatnum - i + 1;
|
||||
g0[ig0] = GG_DATA[i - 1][IDAT[iatnum]];
|
||||
}
|
||||
|
||||
// 读取头行
|
||||
let _header = lines.next()?.ok()?;
|
||||
|
||||
// 读取温度范围
|
||||
let line = lines.next()?.ok()?;
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() < 3 {
|
||||
return None;
|
||||
}
|
||||
let it0: i32 = parts[0].parse().ok()?;
|
||||
let it1: i32 = parts[1].parse().ok()?;
|
||||
let itstp: i32 = parts[2].parse().ok()?;
|
||||
|
||||
let ntt = ((it1 - it0) / itstp + 1) as usize;
|
||||
frac_op.ntt = ntt;
|
||||
|
||||
// 读取各温度点的数据
|
||||
for it in 0..ntt {
|
||||
let line = lines.next()?.ok()?;
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() < 4 {
|
||||
continue;
|
||||
}
|
||||
let itt: i32 = parts[0].parse().ok()?;
|
||||
let ie0: i32 = parts[1].parse().ok()?;
|
||||
let ie1: i32 = parts[2].parse().ok()?;
|
||||
let iestp: i32 = parts[3].parse().ok()?;
|
||||
|
||||
frac_op.itemp[it] = itt;
|
||||
|
||||
let t = (std::f64::consts::LN_10 * 0.025 * itt as f64).exp();
|
||||
let safac0 = t.sqrt() * t / 2.07e-16;
|
||||
let tkcm = 0.69496 * t;
|
||||
|
||||
let net = ((ie1 - ie0) / iestp + 1) as usize;
|
||||
|
||||
for _ie in 0..net {
|
||||
let line = lines.next()?.ok()?;
|
||||
// 格式: 3i4,2x,4(i4,1x,e9.3)
|
||||
// 简化解析
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() < 3 {
|
||||
continue;
|
||||
}
|
||||
|
||||
let iee: i32 = parts[0].parse().ok()?;
|
||||
let ion0: usize = parts[1].parse().ok()?;
|
||||
let ion1: usize = parts[2].parse().ok()?;
|
||||
|
||||
let ane = (std::f64::consts::LN_10 * 0.25 * iee as f64).exp();
|
||||
let safac = safac0 / ane;
|
||||
let ieind = (iee / 2) as usize;
|
||||
|
||||
// 读取分数数据
|
||||
let mut frac0 = [0.0f64; MION1 + 2]; // frac0(-1:mion1)
|
||||
let mut ioo = [0i32; MION1 + 2]; // ioo(-1:mion1)
|
||||
|
||||
// 解析第一组 (最多 4 个)
|
||||
let n_parse = (ion1 - ion0 + 1).min(4);
|
||||
for k in 0..n_parse {
|
||||
let idx = 3 + k * 2;
|
||||
if idx + 1 < parts.len() {
|
||||
ioo[ion0 + k] = parts[idx].parse().unwrap_or(0);
|
||||
frac0[ion0 + k] = parts[idx + 1].parse().unwrap_or(0.0);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果有多于 4 个电离态,继续读取
|
||||
let nio = ion1 - ion0;
|
||||
if nio >= 3 {
|
||||
let nlin = nio / 4;
|
||||
for _ilin in 0..nlin {
|
||||
let line = lines.next()?.ok()?;
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
let start_ion = ion0 + 4 * (_ilin + 1);
|
||||
for k in 0..4 {
|
||||
let idx = k * 2;
|
||||
if idx + 1 < parts.len() && start_ion + k <= ion1 {
|
||||
ioo[start_ion + k] = parts[idx].parse().unwrap_or(0);
|
||||
frac0[start_ion + k] = parts[idx + 1].parse().unwrap_or(0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 计算电离分数
|
||||
let mut z0 = [0.0f64; MION1 + 2]; // z0(-1:mion1)
|
||||
for ion in ion0..=ion1 {
|
||||
if ion < iatnum {
|
||||
if ion == ion0 {
|
||||
z0[ion] = g0[iatnum - ion];
|
||||
} else {
|
||||
z0[ion] = frac0[ion] / frac0[ion - 1] * safac * z0[ion - 1];
|
||||
let u0_val = get_u0(iatnum, iatnum - ion);
|
||||
if tkcm > 0.0 && u0_val != 0.0 {
|
||||
z0[ion] *= (-u0_val / tkcm).exp();
|
||||
}
|
||||
}
|
||||
if z0[ion] != 0.0 {
|
||||
frac_op.frac[it][ieind][iatnum - ion] = frac0[ion] / z0[ion];
|
||||
}
|
||||
} else {
|
||||
// 负离子 H-
|
||||
let u0hm = 6090.5;
|
||||
let z0hm = if ion > 0 && frac0[ion - 1] != 0.0 {
|
||||
frac0[ion] / frac0[ion - 1] * safac
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
let z0hm = if tkcm > 0.0 {
|
||||
z0hm * (-u0hm / tkcm).exp()
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
if z0hm != 0.0 {
|
||||
frac_op.fracm[it][ieind] = frac0[ion] / z0hm;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(frac_op)
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 测试
|
||||
// ============================================================================
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_idat_table() {
|
||||
// 验证 IDAT 表的正确性
|
||||
assert_eq!(IDAT[1], 1); // H
|
||||
assert_eq!(IDAT[2], 2); // He
|
||||
assert_eq!(IDAT[3], 0); // Li - 无数据
|
||||
assert_eq!(IDAT[6], 3); // C
|
||||
assert_eq!(IDAT[7], 4); // N
|
||||
assert_eq!(IDAT[8], 5); // O
|
||||
assert_eq!(IDAT[26], 16); // Fe
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gg_data() {
|
||||
// 验证统计权重数据
|
||||
assert_eq!(GG_DATA[0][0], 2.0); // H 基态
|
||||
assert_eq!(GG_DATA[0][1], 2.0); // He 基态
|
||||
assert_eq!(GG_DATA[1][1], 1.0); // He+ 基态
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_u0() {
|
||||
// H 的电离势
|
||||
let u0_h = get_u0(1, 1);
|
||||
assert!((u0_h - 109678.7).abs() < 1.0);
|
||||
|
||||
// 无效输入
|
||||
let u0_invalid = get_u0(0, 1);
|
||||
assert_eq!(u0_invalid, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_frac_op_new() {
|
||||
let frac_op = FracOp::new();
|
||||
assert_eq!(frac_op.ntt, 0);
|
||||
assert_eq!(frac_op.frac.len(), MTEMP);
|
||||
assert_eq!(frac_op.frac[0].len(), MELEC);
|
||||
assert_eq!(frac_op.frac[0][0].len(), MION1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fractn_no_data_element() {
|
||||
// Li (原子序数 3) 没有数据
|
||||
let result = fractn(3, "/nonexistent");
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fractn_invalid_atomic_number() {
|
||||
let result = fractn(0, "/nonexistent");
|
||||
assert!(result.is_none());
|
||||
|
||||
let result = fractn(31, "/nonexistent");
|
||||
assert!(result.is_none());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
//! Hydrogenic bound-free Gaunt factors.
|
||||
//!
|
||||
//! Translated from SYNSPEC `GAUNT` and `GNTK` functions (synspec54.f:3715, 3763).
|
||||
|
||||
/// Hydrogenic bound-free Gaunt factor.
|
||||
///
|
||||
/// Calculates the bound-free Gaunt factor for hydrogenic ions
|
||||
/// for principal quantum number `i` and frequency `fr`.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `i` - Principal quantum number (1-10)
|
||||
/// * `fr` - Frequency (Hz)
|
||||
///
|
||||
/// # Returns
|
||||
/// The bound-free Gaunt factor.
|
||||
pub fn gaunt(i: i32, fr: f64) -> f64 {
|
||||
let x = fr / 2.99793e14;
|
||||
|
||||
match i {
|
||||
1 => {
|
||||
1.2302628 + x * (-2.9094219e-3 + x * (7.3993579e-6 - 8.7356966e-9 * x))
|
||||
+ (12.803223 / x - 5.5759888) / x
|
||||
}
|
||||
2 => {
|
||||
1.1595421 + x * (-2.0735860e-3 + 2.7033384e-6 * x)
|
||||
+ (-1.2709045 + (-2.0244141 / x + 2.1325684) / x) / x
|
||||
}
|
||||
3 => {
|
||||
1.1450949 + x * (-1.9366592e-3 + 2.3572356e-6 * x)
|
||||
+ (-0.55936432 + (-0.23387146 / x + 0.52471924) / x) / x
|
||||
}
|
||||
4 => {
|
||||
1.1306695 + x * (-1.3482273e-3 + x * (-4.6949424e-6 + 2.3548636e-8 * x))
|
||||
+ (-0.31190730 + (0.19683564 - 5.4418565e-2 / x) / x) / x
|
||||
}
|
||||
5 => {
|
||||
1.1190904 + x * (-1.0401085e-3 + x * (-6.9943488e-6 + 2.8496742e-8 * x))
|
||||
+ (-0.16051018 + (5.5545091e-2 - 8.9182854e-3 / x) / x) / x
|
||||
}
|
||||
6 => {
|
||||
1.1168376 + x * (-8.9466573e-4 + x * (-8.8393133e-6 + 3.4696768e-8 * x))
|
||||
+ (-0.13075417 + (4.1921183e-2 - 5.5303574e-3 / x) / x) / x
|
||||
}
|
||||
7 => {
|
||||
1.1128632 + x * (-7.4833260e-4 + x * (-1.0244504e-5 + 3.8595771e-8 * x))
|
||||
+ (-9.5441161e-2 + (2.3350812e-2 - 2.2752881e-3 / x) / x) / x
|
||||
}
|
||||
8 => {
|
||||
1.1093137 + x * (-6.2619148e-4 + x * (-1.1342068e-5 + 4.1477731e-8 * x))
|
||||
+ (-7.1010560e-2 + (1.3298411e-2 - 9.7200274e-4 / x) / x) / x
|
||||
}
|
||||
9 => {
|
||||
1.1078717 + x * (-5.4837392e-4 + x * (-1.2157943e-5 + 4.3796716e-8 * x))
|
||||
+ (-5.6046560e-2 + (8.5139736e-3 - 4.9576163e-4 / x) / x) / x
|
||||
}
|
||||
10 => {
|
||||
1.1052734 + x * (-4.4341570e-4 + x * (-1.3235905e-5 + 4.7003140e-8 * x))
|
||||
+ (-4.7326370e-2 + (6.1516856e-3 - 2.9467046e-4 / x) / x) / x
|
||||
}
|
||||
_ => 1.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Hydrogenic bound-free Gaunt factor (Klaus Werner version).
|
||||
///
|
||||
/// Alternative Gaunt factor calculation for low quantum numbers.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `i` - Principal quantum number (1-3)
|
||||
/// * `fr` - Frequency (Hz)
|
||||
///
|
||||
/// # Returns
|
||||
/// The bound-free Gaunt factor.
|
||||
pub fn gntk(i: i32, fr: f64) -> f64 {
|
||||
let y = 1.0 / fr;
|
||||
|
||||
match i {
|
||||
1 => 0.9916 + y * (2.71852e13 - y * 2.26846e30),
|
||||
2 => 1.1050 - y * (2.37490e14 - y * 4.07677e28),
|
||||
3 => 1.1010 - y * (0.98632e14 - y * 1.03540e28),
|
||||
_ => 1.0,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_gaunt_n1() {
|
||||
// Use frequency in valid range for Gaunt factor
|
||||
let result = gaunt(1, 5.0e14);
|
||||
assert!(result.is_finite());
|
||||
assert!(result > 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gaunt_n2() {
|
||||
// Use frequency in valid range for Gaunt factor
|
||||
let result = gaunt(2, 5.0e14);
|
||||
assert!(result.is_finite());
|
||||
assert!(result > 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gaunt_high_n() {
|
||||
let result = gaunt(10, 3.0e14);
|
||||
assert!(result.is_finite());
|
||||
assert!(result > 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gaunt_default() {
|
||||
// For n > 10, should return 1.0
|
||||
let result = gaunt(11, 3.0e14);
|
||||
assert_eq!(result, 1.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gntk_n1() {
|
||||
// Use higher frequency for valid GNTK values
|
||||
let result = gntk(1, 1.0e15);
|
||||
assert!(result.is_finite());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gntk_n2() {
|
||||
// Use higher frequency for valid GNTK values
|
||||
let result = gntk(2, 1.0e15);
|
||||
assert!(result.is_finite());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gntk_n3() {
|
||||
// Use higher frequency for valid GNTK values
|
||||
let result = gntk(3, 1.0e15);
|
||||
assert!(result.is_finite());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gntk_default() {
|
||||
// For n > 3, should return 1.0
|
||||
let result = gntk(4, 3.0e14);
|
||||
assert_eq!(result, 1.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
//! Read quasi-molecular satellite line profile data.
|
||||
//!
|
||||
//! Translated from SYNSPEC `getlal` subroutine (synspec54.f).
|
||||
//!
|
||||
//! Reads profile functions for Lyman alpha, beta, gamma, and Balmer alpha,
|
||||
//! including quasi-molecular satellites. Data files are in `./data/` directory.
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::Path;
|
||||
|
||||
use super::allard::{AllardData, AllardTable, NNMAX};
|
||||
|
||||
// ============================================================================
|
||||
// Data file names
|
||||
// ============================================================================
|
||||
|
||||
/// Lyman alpha quasi-molecular data file
|
||||
const LAQUASI_FILE: &str = "laquasi.dat";
|
||||
|
||||
/// Lyman beta quasi-molecular data file
|
||||
const LBQUASI_FILE: &str = "lbquasi.dat";
|
||||
|
||||
/// Lyman gamma quasi-molecular data file
|
||||
const LGQUASI_FILE: &str = "lgquasi.dat";
|
||||
|
||||
/// Balmer alpha quasi-molecular data file
|
||||
const LHQUASI_FILE: &str = "lhquasi.dat";
|
||||
|
||||
// ============================================================================
|
||||
// Helper: read one quasi-molecular table from file
|
||||
// ============================================================================
|
||||
|
||||
/// Read one quasi-molecular table from a data file.
|
||||
///
|
||||
/// # File format
|
||||
/// Line 1: `nx stnne stnch vneu vcha`
|
||||
/// Lines 2..nx+1: `xl pl[0] pl[1] pl[2] pl[3] pl[4]`
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `path` - Path to data file
|
||||
///
|
||||
/// # Returns
|
||||
/// Populated `AllardTable` or error message.
|
||||
fn read_table(path: &Path) -> Result<AllardTable, String> {
|
||||
let file = File::open(path).map_err(|e| format!("Cannot open {}: {}", path.display(), e))?;
|
||||
let reader = BufReader::new(file);
|
||||
let mut lines = reader.lines();
|
||||
|
||||
// Read header: nx, stnne, stnch, vneu, vcha
|
||||
let header = lines
|
||||
.next()
|
||||
.ok_or_else(|| format!("Empty file: {}", path.display()))?
|
||||
.map_err(|e| format!("Read error: {}", e))?;
|
||||
|
||||
let parts: Vec<f64> = header
|
||||
.split_whitespace()
|
||||
.map(|s| s.parse().unwrap_or(0.0))
|
||||
.collect();
|
||||
|
||||
if parts.len() < 5 {
|
||||
return Err(format!(
|
||||
"Invalid header in {}: expected 5 values, got {}",
|
||||
path.display(),
|
||||
parts.len()
|
||||
));
|
||||
}
|
||||
|
||||
let nx = parts[0] as usize;
|
||||
let stnne_raw = parts[1];
|
||||
let stnch_raw = parts[2];
|
||||
let vneu = parts[3];
|
||||
let vcha = parts[4];
|
||||
|
||||
// Read data points
|
||||
let mut xl = Vec::with_capacity(nx);
|
||||
let mut pl = Vec::with_capacity(nx);
|
||||
|
||||
for (i, line_result) in lines.enumerate() {
|
||||
if i >= nx {
|
||||
break;
|
||||
}
|
||||
let line = line_result.map_err(|e| format!("Read error at line {}: {}", i + 2, e))?;
|
||||
let values: Vec<f64> = line
|
||||
.split_whitespace()
|
||||
.map(|s| s.parse().unwrap_or(0.0))
|
||||
.collect();
|
||||
|
||||
if values.len() < 6 {
|
||||
return Err(format!(
|
||||
"Invalid data at line {} in {}: expected 6 values, got {}",
|
||||
i + 2,
|
||||
path.display(),
|
||||
values.len()
|
||||
));
|
||||
}
|
||||
|
||||
xl.push(values[0]);
|
||||
let mut row = [0.0f64; NNMAX];
|
||||
for j in 0..NNMAX {
|
||||
row[j] = values[j + 1];
|
||||
}
|
||||
pl.push(row);
|
||||
}
|
||||
|
||||
// Convert log densities to linear
|
||||
let stnne = 10.0f64.powf(stnne_raw);
|
||||
let stnch = 10.0f64.powf(stnch_raw);
|
||||
|
||||
Ok(AllardTable {
|
||||
xl,
|
||||
pl,
|
||||
stnne,
|
||||
stnch,
|
||||
vneu,
|
||||
vcha,
|
||||
nx,
|
||||
iwarn: false,
|
||||
})
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Main entry point
|
||||
// ============================================================================
|
||||
|
||||
/// Read quasi-molecular satellite line data from files.
|
||||
///
|
||||
/// Translated from SYNSPEC `getlal` subroutine (synspec54.f).
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `data_dir` - Path to data directory (e.g., `./data/`)
|
||||
/// * `nunalp` - Flag for Lyman alpha (>0 to read)
|
||||
/// * `nunbet` - Flag for Lyman beta (>0 to read)
|
||||
/// * `nungam` - Flag for Lyman gamma (>0 to read)
|
||||
/// * `nunbal` - Flag for Balmer alpha (>0 to read)
|
||||
///
|
||||
/// # Returns
|
||||
/// Populated `AllardData` structure.
|
||||
pub fn getlal(
|
||||
data_dir: &Path,
|
||||
nunalp: i32,
|
||||
nunbet: i32,
|
||||
nungam: i32,
|
||||
nunbal: i32,
|
||||
) -> AllardData {
|
||||
let mut data = AllardData::default();
|
||||
|
||||
// Lyman alpha
|
||||
if nunalp > 0 {
|
||||
let path = data_dir.join(LAQUASI_FILE);
|
||||
match read_table(&path) {
|
||||
Ok(table) => {
|
||||
data.lalp = table;
|
||||
eprintln!(" read quasi-molecular data for L alpha");
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!(" Warning: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Lyman beta
|
||||
if nunbet > 0 {
|
||||
let path = data_dir.join(LBQUASI_FILE);
|
||||
match read_table(&path) {
|
||||
Ok(table) => {
|
||||
data.bet = table;
|
||||
eprintln!(" read quasi-molecular data for L beta");
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!(" Warning: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Lyman gamma
|
||||
if nungam > 0 {
|
||||
let path = data_dir.join(LGQUASI_FILE);
|
||||
match read_table(&path) {
|
||||
Ok(table) => {
|
||||
data.gam = table;
|
||||
eprintln!(" read quasi-molecular data for L gamma");
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!(" Warning: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Balmer alpha
|
||||
if nunbal > 0 {
|
||||
let path = data_dir.join(LHQUASI_FILE);
|
||||
match read_table(&path) {
|
||||
Ok(table) => {
|
||||
data.bal = table;
|
||||
eprintln!(" read quasi-molecular data for H alpha");
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!(" Warning: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Tests
|
||||
// ============================================================================
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::io::Write;
|
||||
|
||||
#[test]
|
||||
fn test_read_table_valid() {
|
||||
// Create temporary file
|
||||
let dir = std::env::temp_dir().join("getlal_test");
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("test.dat");
|
||||
|
||||
let mut file = File::create(&path).unwrap();
|
||||
writeln!(file, "3 12.0 10.0 1.0 1.0").unwrap();
|
||||
writeln!(file, "1210.0 1.0 0.5 0.3 0.2 0.1").unwrap();
|
||||
writeln!(file, "1215.0 2.0 1.0 0.6 0.4 0.2").unwrap();
|
||||
writeln!(file, "1220.0 1.5 0.75 0.45 0.3 0.15").unwrap();
|
||||
|
||||
let table = read_table(&path).unwrap();
|
||||
assert_eq!(table.nx, 3);
|
||||
assert!((table.stnne - 1e12).abs() < 1.0);
|
||||
assert!((table.stnch - 1e10).abs() < 1.0);
|
||||
assert!((table.xl[0] - 1210.0).abs() < 1e-10);
|
||||
assert!((table.pl[1][0] - 2.0).abs() < 1e-10);
|
||||
|
||||
// Cleanup
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_table_missing_file() {
|
||||
let path = Path::new("/nonexistent/file.dat");
|
||||
let result = read_table(path);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_getlal_no_files() {
|
||||
let dir = Path::new("/nonexistent");
|
||||
let data = getlal(dir, 1, 1, 1, 1);
|
||||
assert_eq!(data.lalp.nx, 0);
|
||||
assert_eq!(data.bet.nx, 0);
|
||||
assert_eq!(data.gam.nx, 0);
|
||||
assert_eq!(data.bal.nx, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_getlal_skip_disabled() {
|
||||
let dir = Path::new("/nonexistent");
|
||||
let data = getlal(dir, 0, 0, 0, 0);
|
||||
// All tables should be empty when flags are 0
|
||||
assert_eq!(data.lalp.nx, 0);
|
||||
assert_eq!(data.bet.nx, 0);
|
||||
assert_eq!(data.gam.nx, 0);
|
||||
assert_eq!(data.bal.nx, 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
//! Word extraction from text string.
|
||||
//!
|
||||
//! Translated from SYNSPEC `GETWRD` subroutine (synspec54.f:1278).
|
||||
//!
|
||||
//! Finds the next word in a text string starting from index `k0`.
|
||||
//! A word is a sequence of alphanumeric characters delimited by
|
||||
//! separators: space, `(`, `)`, `=`, `*`, `/`, `,`.
|
||||
|
||||
const SEPARATORS: &[char] = &[' ', '(', ')', '=', '*', '/', ','];
|
||||
|
||||
/// Find the next word in a text string.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `text` - Input text string
|
||||
/// * `k0` - Starting search index (0-based)
|
||||
///
|
||||
/// # Returns
|
||||
/// `Some((k1, k2))` where:
|
||||
/// * `k1` - Start index of the word (0-based)
|
||||
/// * `k2` - End index of the word (0-based, inclusive)
|
||||
///
|
||||
/// Returns `None` if no word is found.
|
||||
pub fn getwrd(text: &str, k0: usize) -> Option<(usize, usize)> {
|
||||
let chars: Vec<char> = text.chars().collect();
|
||||
let len = chars.len();
|
||||
|
||||
let mut k1: Option<usize> = None;
|
||||
|
||||
for i in k0..len {
|
||||
match k1 {
|
||||
None => {
|
||||
// Looking for start of word
|
||||
if !SEPARATORS.contains(&chars[i]) {
|
||||
k1 = Some(i);
|
||||
}
|
||||
}
|
||||
Some(start) => {
|
||||
// Looking for end of word
|
||||
if SEPARATORS.contains(&chars[i]) {
|
||||
return Some((start, i - 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we reached end of string while in a word
|
||||
if let Some(start) = k1 {
|
||||
return Some((start, len - 1));
|
||||
}
|
||||
|
||||
// No word found
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_getwrd_simple() {
|
||||
let text = "hello world";
|
||||
let (k1, k2) = getwrd(text, 0).unwrap();
|
||||
assert_eq!(k1, 0);
|
||||
assert_eq!(k2, 4);
|
||||
assert_eq!(&text[k1..=k2], "hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_getwrd_second_word() {
|
||||
let text = "hello world";
|
||||
let (k1, k2) = getwrd(text, 5).unwrap();
|
||||
assert_eq!(k1, 6);
|
||||
assert_eq!(k2, 10);
|
||||
assert_eq!(&text[k1..=k2], "world");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_getwrd_with_separators() {
|
||||
let text = "a=b/c(d)";
|
||||
let (k1, k2) = getwrd(text, 0).unwrap();
|
||||
assert_eq!(k1, 0);
|
||||
assert_eq!(k2, 0);
|
||||
assert_eq!(&text[k1..=k2], "a");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_getwrd_after_separator() {
|
||||
let text = "a=b";
|
||||
let (k1, k2) = getwrd(text, 1).unwrap();
|
||||
assert_eq!(k1, 2);
|
||||
assert_eq!(k2, 2);
|
||||
assert_eq!(&text[k1..=k2], "b");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_getwrd_no_word() {
|
||||
let text = " ";
|
||||
assert!(getwrd(text, 0).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_getwrd_empty_string() {
|
||||
let text = "";
|
||||
assert!(getwrd(text, 0).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_getwrd_leading_spaces() {
|
||||
let text = " hello";
|
||||
let (k1, k2) = getwrd(text, 0).unwrap();
|
||||
assert_eq!(k1, 3);
|
||||
assert_eq!(k2, 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_getwrd_multiple_separators() {
|
||||
let text = "a,b,c";
|
||||
let (k1, k2) = getwrd(text, 0).unwrap();
|
||||
assert_eq!(k1, 0);
|
||||
assert_eq!(k2, 0);
|
||||
|
||||
let (k1, k2) = getwrd(text, 2).unwrap();
|
||||
assert_eq!(k1, 2);
|
||||
assert_eq!(k2, 2);
|
||||
|
||||
let (k1, k2) = getwrd(text, 4).unwrap();
|
||||
assert_eq!(k1, 4);
|
||||
assert_eq!(k2, 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_getwrd_at_end() {
|
||||
let text = "x ";
|
||||
let (k1, k2) = getwrd(text, 0).unwrap();
|
||||
assert_eq!(k1, 0);
|
||||
assert_eq!(k2, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_getwrd_beyond_end() {
|
||||
let text = "hi";
|
||||
assert!(getwrd(text, 5).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_getwrd_realistic_input() {
|
||||
// Typical SYNSPEC input: "H 1 1.0 2.0"
|
||||
let text = "H 1 1.0 2.0";
|
||||
let (k1, k2) = getwrd(text, 0).unwrap();
|
||||
assert_eq!(&text[k1..=k2], "H");
|
||||
|
||||
let (k1, k2) = getwrd(text, k2 + 1).unwrap();
|
||||
assert_eq!(&text[k1..=k2], "1");
|
||||
|
||||
let (k1, k2) = getwrd(text, k2 + 1).unwrap();
|
||||
assert_eq!(&text[k1..=k2], "1.0");
|
||||
|
||||
let (k1, k2) = getwrd(text, k2 + 1).unwrap();
|
||||
assert_eq!(&text[k1..=k2], "2.0");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
//! Hydrogenic free-free Gaunt factor.
|
||||
//!
|
||||
//! Translated from SYNSPEC `GFREE` function (synspec54.f:5144).
|
||||
|
||||
/// Hydrogenic free-free Gaunt factor for temperature `t` and frequency `fr`.
|
||||
///
|
||||
/// Based on tabulated values with polynomial interpolation.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `t` - Temperature (K)
|
||||
/// * `fr` - Frequency (Hz)
|
||||
///
|
||||
/// # Returns
|
||||
/// The free-free Gaunt factor.
|
||||
pub fn gfree(t: f64, fr: f64) -> f64 {
|
||||
let mut thet = 5040.4 / t;
|
||||
if thet < 4.0e-2 {
|
||||
thet = 4.0e-2;
|
||||
}
|
||||
let x = fr / 2.99793e14;
|
||||
|
||||
if x <= 1.0 {
|
||||
let x_clamped = if x < 0.2 { 0.2 } else { x };
|
||||
(1.0823 + 2.98e-2 / thet) + (6.7e-3 + 1.12e-2 / thet) / x_clamped
|
||||
} else {
|
||||
let c1 = (3.9999187e-3 - 7.8622889e-5 / thet) / thet + 1.070192;
|
||||
let c2 = (6.4628601e-2 - 6.1953813e-4 / thet) / thet + 2.6061249e-1;
|
||||
let c3 = (1.3983474e-5 / thet + 3.7542343e-2) / thet + 5.7917786e-1;
|
||||
let c4 = 3.4169006e-1 + 1.1852264e-2 / thet;
|
||||
((c4 / x - c3) / x + c2) / x + c1
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_gfree_low_freq() {
|
||||
// For x < 1, the formula is simpler
|
||||
let t = 10000.0;
|
||||
let fr = 1.0e14; // x = fr/2.99793e14 ≈ 0.33 < 1
|
||||
let result = gfree(t, fr);
|
||||
assert!(result > 0.0);
|
||||
assert!(result.is_finite());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gfree_high_freq() {
|
||||
// For x > 1, polynomial formula is used
|
||||
let t = 10000.0;
|
||||
let fr = 5.0e14; // x ≈ 1.67 > 1
|
||||
let result = gfree(t, fr);
|
||||
assert!(result > 0.0);
|
||||
assert!(result.is_finite());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gfree_boundary() {
|
||||
// At x = 1, both branches should give similar results
|
||||
let t = 10000.0;
|
||||
let fr = 2.99793e14; // x = 1
|
||||
let result = gfree(t, fr);
|
||||
assert!(result > 0.0);
|
||||
assert!(result.is_finite());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gfree_low_temp() {
|
||||
// Test with very low temperature (thet clamped to 0.04)
|
||||
let t = 200000.0;
|
||||
let fr = 1.0e14;
|
||||
let result = gfree(t, fr);
|
||||
assert!(result > 0.0);
|
||||
assert!(result.is_finite());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
//! Hydrogen opacity from Gomez tables for SYNSPEC.
|
||||
//!
|
||||
//! Translated from SYNSPEC `ghydop` subroutine (synspec54.f:21700).
|
||||
//!
|
||||
//! Calculates hydrogen line + pseudocontinuum opacity using
|
||||
//! pre-computed Gomez opacity tables via wavelength interpolation.
|
||||
|
||||
// ============================================================================
|
||||
// Physical constants
|
||||
// ============================================================================
|
||||
|
||||
const C18: f64 = 2.997925e18;
|
||||
const FREQ_THRESHOLD: f64 = 8.22013e14;
|
||||
|
||||
// ============================================================================
|
||||
// Parameters
|
||||
// ============================================================================
|
||||
|
||||
/// Input parameters for `ghydop`.
|
||||
pub struct GhydopParams<'a> {
|
||||
/// Depth index.
|
||||
pub id: usize,
|
||||
/// Start frequency index.
|
||||
pub i0: usize,
|
||||
/// End frequency index.
|
||||
pub i1: usize,
|
||||
/// Temperature at depth ID (K).
|
||||
pub t: f64,
|
||||
/// Frequency array (Hz).
|
||||
pub freq: &'a [f64],
|
||||
/// Level populations (up to 40 levels).
|
||||
pub pj: &'a [f64],
|
||||
/// Gomez opacity table wavelengths (log10, sorted decreasing).
|
||||
pub wlgtab: &'a [f64],
|
||||
/// Gomez opacity table values: hydopg[frequency_index][depth].
|
||||
pub hydopg: &'a [f64],
|
||||
/// Number of frequencies in Gomez table.
|
||||
pub nugfreq: usize,
|
||||
/// Number of depths in the model.
|
||||
pub ndepth: usize,
|
||||
}
|
||||
|
||||
/// Result of `ghydop`.
|
||||
pub struct GhydopResult {
|
||||
/// Updated absorption coefficient array (added to input).
|
||||
pub absoh: Vec<f64>,
|
||||
/// Updated emission coefficient array (added to input).
|
||||
pub emish: Vec<f64>,
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Implementation
|
||||
// ============================================================================
|
||||
|
||||
/// Calculate hydrogen opacity from Gomez tables.
|
||||
///
|
||||
/// Interpolates pre-computed Gomez opacity tables in wavelength space
|
||||
/// and combines with level populations to produce absorption and
|
||||
/// emission coefficients.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `params` - Input parameters including Gomez table data
|
||||
/// * `absoh_in` - Input absorption array (will be added to)
|
||||
/// * `emish_in` - Input emission array (will be added to)
|
||||
///
|
||||
/// # Returns
|
||||
/// Updated absorption and emission arrays
|
||||
pub fn ghydop(
|
||||
params: &GhydopParams,
|
||||
absoh_in: &[f64],
|
||||
emish_in: &[f64],
|
||||
) -> GhydopResult {
|
||||
let nf = params.freq.len();
|
||||
let mut absoh = absoh_in.to_vec();
|
||||
let mut emish = emish_in.to_vec();
|
||||
|
||||
if params.nugfreq == 0 {
|
||||
return GhydopResult { absoh, emish };
|
||||
}
|
||||
|
||||
let frg1 = params.wlgtab[0]; // Note: wlgtab stores wavelengths, not frequencies
|
||||
let frg2 = params.wlgtab[params.nugfreq - 1];
|
||||
|
||||
// Build frequency-to-wavelength lookup from wlgtab
|
||||
// wlgtab is in wavelength space (Å), sorted decreasing
|
||||
// We need to find the right interval for each frequency
|
||||
|
||||
let mut igf = params.nugfreq;
|
||||
|
||||
for ij in params.i0..=params.i1.min(nf - 1) {
|
||||
let fr = params.freq[ij];
|
||||
let wla = C18 / fr; // wavelength in Å
|
||||
|
||||
// Find wavelength interval in the table
|
||||
if wla <= frg2 || wla >= frg1 {
|
||||
continue; // Outside table range
|
||||
}
|
||||
|
||||
// Scan to find the right interval (table sorted by decreasing wavelength)
|
||||
if ij == params.i0 {
|
||||
igf = params.nugfreq;
|
||||
}
|
||||
while igf > 0 && wla > params.wlgtab[igf - 1] {
|
||||
igf -= 1;
|
||||
}
|
||||
let ig0 = if igf <= 2 { 2 } else { igf };
|
||||
let ig1 = igf - 1;
|
||||
|
||||
if ig0 == 0 || ig1 >= params.nugfreq || ig0 > ig1 {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Interpolate Gomez opacity in wavelength
|
||||
let wl0 = params.wlgtab[ig0 - 1];
|
||||
let wl1 = params.wlgtab[ig1];
|
||||
let denom = wl1 - wl0;
|
||||
if denom.abs() < 1.0e-30 {
|
||||
continue;
|
||||
}
|
||||
|
||||
let opg0 = get_hydopg(params.hydopg, ig0 - 1, params.id, params.ndepth);
|
||||
let opg1 = get_hydopg(params.hydopg, ig1, params.id, params.ndepth);
|
||||
let abl = (opg1 - opg0) * (wla - wl0) / denom + opg0;
|
||||
|
||||
// Determine which population to use based on frequency
|
||||
let pp = if fr > FREQ_THRESHOLD {
|
||||
params.pj.first().copied().unwrap_or(0.0) * 2.0
|
||||
} else {
|
||||
params.pj.get(1).copied().unwrap_or(0.0) * 8.0
|
||||
};
|
||||
|
||||
// Compute Planck function factor
|
||||
let f15 = fr * 1.0e-15;
|
||||
let xkf = (-4.79928e-11 * fr / params.t).exp();
|
||||
let xkfb = xkf * 1.4743e-2 * f15 * f15 * f15;
|
||||
|
||||
// Total opacity
|
||||
let oph = abl.exp() * pp;
|
||||
absoh[ij] += oph;
|
||||
emish[ij] += oph * xkfb / (1.0 - xkf);
|
||||
}
|
||||
|
||||
GhydopResult { absoh, emish }
|
||||
}
|
||||
|
||||
/// Access hydopg table value.
|
||||
fn get_hydopg(hydopg: &[f64], freq_idx: usize, depth: usize, ndepth: usize) -> f64 {
|
||||
let idx = freq_idx * ndepth + depth;
|
||||
if idx < hydopg.len() {
|
||||
hydopg[idx]
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_ghydop_empty_table() {
|
||||
let freq = vec![3.0e14, 4.0e14, 5.0e14];
|
||||
let params = GhydopParams {
|
||||
id: 0,
|
||||
i0: 0,
|
||||
i1: 2,
|
||||
t: 10000.0,
|
||||
freq: &freq,
|
||||
pj: &[0.0; 40],
|
||||
wlgtab: &[],
|
||||
hydopg: &[],
|
||||
nugfreq: 0,
|
||||
ndepth: 1,
|
||||
};
|
||||
let absoh = vec![0.0; 3];
|
||||
let emish = vec![0.0; 3];
|
||||
let result = ghydop(¶ms, &absoh, &emish);
|
||||
assert_eq!(result.absoh, vec![0.0; 3]);
|
||||
assert_eq!(result.emish, vec![0.0; 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ghydop_basic() {
|
||||
// Simple table with 3 wavelength points
|
||||
let wlgtab = vec![10000.0, 5000.0, 2000.0]; // Decreasing wavelength
|
||||
let ndepth = 2;
|
||||
let nugfreq = 3;
|
||||
// hydopg[freq_idx * ndepth + depth]
|
||||
let hydopg = vec![
|
||||
1.0, 2.0, // freq 0, depth 0,1
|
||||
1.5, 2.5, // freq 1, depth 0,1
|
||||
0.5, 1.0, // freq 2, depth 0,1
|
||||
];
|
||||
|
||||
let freq = vec![3.0e14, 4.0e14]; // ~10000Å, ~7500Å
|
||||
let pj = vec![1.0e10; 40];
|
||||
|
||||
let params = GhydopParams {
|
||||
id: 0,
|
||||
i0: 0,
|
||||
i1: 1,
|
||||
t: 10000.0,
|
||||
freq: &freq,
|
||||
pj: &pj,
|
||||
wlgtab: &wlgtab,
|
||||
hydopg: &hydopg,
|
||||
nugfreq,
|
||||
ndepth,
|
||||
};
|
||||
|
||||
let absoh = vec![0.0; 2];
|
||||
let emish = vec![0.0; 2];
|
||||
let result = ghydop(¶ms, &absoh, &emish);
|
||||
|
||||
// Values should be finite
|
||||
assert!(result.absoh.iter().all(|&x| x.is_finite()));
|
||||
assert!(result.emish.iter().all(|&x| x.is_finite()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_hydopg_bounds() {
|
||||
let hydopg = vec![1.0, 2.0, 3.0, 4.0];
|
||||
assert_eq!(get_hydopg(&hydopg, 0, 0, 2), 1.0);
|
||||
assert_eq!(get_hydopg(&hydopg, 1, 0, 2), 3.0);
|
||||
assert_eq!(get_hydopg(&hydopg, 0, 1, 2), 2.0);
|
||||
// Out of bounds
|
||||
assert_eq!(get_hydopg(&hydopg, 5, 0, 2), 0.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,288 @@
|
||||
//! Initialization and reading of opacity table for thermal processes.
|
||||
//!
|
||||
//! Translated from SYNSPEC54.FOR subroutine GOMINI (line 21601).
|
||||
//!
|
||||
//! Reads `gomhyd.dat` file containing hydrogen opacity tables as a function
|
||||
//! of temperature and electron density, then interpolates to the actual
|
||||
//! temperature and electron density at each depth point.
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// Constants
|
||||
// ============================================================================
|
||||
|
||||
/// Conversion factor from eV to temperature (K)
|
||||
#[allow(dead_code)]
|
||||
const EV_TO_K: f64 = 1.161e4;
|
||||
|
||||
/// Energy-to-frequency conversion: 3.28805e15 / 13.595
|
||||
#[allow(dead_code)]
|
||||
const ENE_TO_FREQ: f64 = 3.28805e15 / 13.595;
|
||||
|
||||
/// Wavelength conversion constant (Å)
|
||||
#[allow(dead_code)]
|
||||
const WL_CONV: f64 = 2.997925e18;
|
||||
|
||||
/// Log of the opacity offset constant: log(0.02654 * 4.1347e-15)
|
||||
const OPAC_OFFSET: f64 = -32.726_974_762_964_47; // precomputed
|
||||
|
||||
// ============================================================================
|
||||
// GOMINI parameters
|
||||
// ============================================================================
|
||||
|
||||
/// Parameters for the GOMINI subroutine.
|
||||
pub struct GominiParams<'a> {
|
||||
/// Number of depth points
|
||||
pub nd: usize,
|
||||
/// Temperature array (depth points)
|
||||
pub temp: &'a [f64],
|
||||
/// Electron density array (depth points)
|
||||
pub elec: &'a [f64],
|
||||
/// Electron density limit for H⁻ opacity
|
||||
pub hglim: f64,
|
||||
/// Switch for H⁻ opacity (0 = off)
|
||||
pub ihgom: i32,
|
||||
}
|
||||
|
||||
/// Result of GOMINI: interpolated H⁻ opacity table.
|
||||
pub struct GominiResult {
|
||||
/// Frequency grid (Hz) [nugfreq]
|
||||
pub frgtab: Vec<f64>,
|
||||
/// Wavelength grid (Å) [nugfreq]
|
||||
pub wlgtab: Vec<f64>,
|
||||
/// Interpolated H⁻ opacity (log scale) [nugfreq × nd]
|
||||
pub hydopg: Vec<Vec<f64>>,
|
||||
/// Number of tabular frequencies
|
||||
pub nugfreq: usize,
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// GOMINI implementation
|
||||
// ============================================================================
|
||||
|
||||
/// Initialize and read opacity table for thermal processes (H⁻ bound-free).
|
||||
///
|
||||
/// Reads the `gomhyd.dat` file, then performs bilinear interpolation
|
||||
/// in log(temperature) and log(electron density) to each depth point.
|
||||
///
|
||||
/// # Fortran original
|
||||
///
|
||||
/// ```fortran
|
||||
/// SUBROUTINE GOMINI
|
||||
/// READ gomhyd.dat
|
||||
/// Bilinear interpolation to depth points
|
||||
/// END
|
||||
/// ```
|
||||
pub fn gomini(params: &GominiParams) -> Option<GominiResult> {
|
||||
let GominiParams { nd: _, temp: _, elec: _, hglim: _, ihgom } = *params;
|
||||
|
||||
if ihgom == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Read gomhyd.dat - this would normally be file I/O
|
||||
// For now, we provide the interface; actual file reading
|
||||
// would be handled by the caller
|
||||
//
|
||||
// The file format is:
|
||||
// Line 1: nugfreq, nugtemp, nugele
|
||||
// Line 2: (blank)
|
||||
// Line 3: temvec(1..nugtemp) in eV
|
||||
// Line 4: (blank)
|
||||
// Line 5: elevec(1..nugele) in log10(ne)
|
||||
// Then for each frequency:
|
||||
// Line: energy in eV (format: 40x, f17.14)
|
||||
// nugtemp lines: hydcrs(i, 1..nugele) for each temperature
|
||||
|
||||
// This function returns None when ihgom == 0 (disabled)
|
||||
// The actual implementation requires file I/O which is
|
||||
// handled by the runner layer
|
||||
|
||||
// Placeholder: the actual interpolation logic is below
|
||||
// when called with pre-loaded table data
|
||||
None
|
||||
}
|
||||
|
||||
/// Perform bilinear interpolation of H⁻ opacity table to depth points.
|
||||
///
|
||||
/// This is the core interpolation logic extracted from GOMINI,
|
||||
/// to be used with pre-loaded table data.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `nugfreq` - Number of tabular frequencies
|
||||
/// * `nugtemp` - Number of tabular temperatures
|
||||
/// * `nugele` - Number of tabular electron densities
|
||||
/// * `temvec` - Temperature array (in log(K)) [nugtemp]
|
||||
/// * `elevec` - Electron density array (in log(ne)) [nugele]
|
||||
/// * `hydcrs` - Cross-section table [nugtemp × nugele × nugfreq]
|
||||
/// * `temp` - Temperature array (depth points)
|
||||
/// * `elec` - Electron density array (depth points)
|
||||
/// * `nd` - Number of depth points
|
||||
/// * `hglim` - Electron density limit
|
||||
///
|
||||
/// # Returns
|
||||
/// Interpolated opacity [nugfreq × nd] (log scale)
|
||||
pub fn gomini_interpolate(
|
||||
nugfreq: usize,
|
||||
nugtemp: usize,
|
||||
nugele: usize,
|
||||
temvec: &[f64],
|
||||
elevec: &[f64],
|
||||
hydcrs: &[Vec<Vec<f64>>],
|
||||
temp: &[f64],
|
||||
elec: &[f64],
|
||||
nd: usize,
|
||||
hglim: f64,
|
||||
) -> (Vec<f64>, Vec<f64>, Vec<Vec<f64>>) {
|
||||
// Frequency and wavelength grids
|
||||
let frgtab = vec![0.0; nugfreq];
|
||||
let wlgtab = vec![0.0; nugfreq];
|
||||
|
||||
// Compute frequency/wavelength from energy
|
||||
// In the Fortran, energy is read per frequency block
|
||||
// Here we assume frgtab is already populated by caller
|
||||
|
||||
// Interpolate to actual depth points
|
||||
let mut hydopg = vec![vec![0.0; nd]; nugfreq];
|
||||
|
||||
for id in 0..nd {
|
||||
if elec[id] < hglim {
|
||||
continue;
|
||||
}
|
||||
|
||||
let rl = elec[id].ln();
|
||||
let tl = temp[id].ln();
|
||||
|
||||
// Find bracketing indices in electron density
|
||||
let eg_tab1 = elevec[0];
|
||||
let eg_tab2 = elevec[nugele - 1];
|
||||
let deltar = (rl - eg_tab1) / (eg_tab2 - eg_tab1) * (nugele - 1) as f64;
|
||||
let mut jr = 1 + deltar as i32;
|
||||
if jr < 1 { jr = 1; }
|
||||
if jr > (nugele - 1) as i32 { jr = (nugele - 1) as i32; }
|
||||
let jr = jr as usize - 1; // 0-indexed
|
||||
|
||||
let r1i = elevec[jr];
|
||||
let r2i = elevec[jr + 1];
|
||||
let dri = if jr == 0 {
|
||||
0.0
|
||||
} else {
|
||||
(rl - r1i) / (r2i - r1i)
|
||||
};
|
||||
|
||||
// Find bracketing indices in temperature
|
||||
let tg_tab1 = temvec[0];
|
||||
let tg_tab2 = temvec[nugtemp - 1];
|
||||
let deltat = (tl - tg_tab1) / (tg_tab2 - tg_tab1) * (nugtemp - 1) as f64;
|
||||
let mut jp = 1 + deltat as i32;
|
||||
if jp < 1 { jp = 1; }
|
||||
if jp > (nugtemp - 1) as i32 { jp = (nugtemp - 1) as i32; }
|
||||
let jp = jp as usize - 1; // 0-indexed
|
||||
|
||||
let t1i = temvec[jp];
|
||||
let t2i = temvec[jp + 1];
|
||||
let dti = if jp == 0 {
|
||||
0.0
|
||||
} else {
|
||||
(tl - t1i) / (t2i - t1i)
|
||||
};
|
||||
|
||||
// Bilinear interpolation over tabular frequencies
|
||||
for jf in 0..nugfreq {
|
||||
let opr1 = hydcrs[jp][jr][jf]
|
||||
+ dti * (hydcrs[jp + 1][jr][jf] - hydcrs[jp][jr][jf]);
|
||||
let opr2 = hydcrs[jp][jr + 1][jf]
|
||||
+ dti * (hydcrs[jp + 1][jr + 1][jf] - hydcrs[jp][jr + 1][jf]);
|
||||
let opac = opr1 + dri * (opr2 - opr1);
|
||||
hydopg[jf][id] = opac + OPAC_OFFSET;
|
||||
}
|
||||
}
|
||||
|
||||
(frgtab, wlgtab, hydopg)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_gomini_disabled() {
|
||||
let params = GominiParams {
|
||||
nd: 5,
|
||||
temp: &[5000.0; 5],
|
||||
elec: &[1e14; 5],
|
||||
hglim: 1e10,
|
||||
ihgom: 0,
|
||||
};
|
||||
assert!(gomini(¶ms).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gomini_interpolate_basic() {
|
||||
// Simple test with 2 temperatures, 2 densities, 2 frequencies
|
||||
let nugfreq = 2;
|
||||
let nugtemp = 2;
|
||||
let nugele = 2;
|
||||
|
||||
let temvec = vec![10.0, 11.0]; // log(K)
|
||||
let elevec = vec![10.0, 12.0]; // log(ne)
|
||||
|
||||
// hydcrs[temp][ele][freq]
|
||||
let hydcrs = vec![
|
||||
vec![vec![1.0, 2.0], vec![3.0, 4.0]],
|
||||
vec![vec![5.0, 6.0], vec![7.0, 8.0]],
|
||||
];
|
||||
|
||||
let temp = vec![22000.0]; // ln(22000) ≈ 10.0
|
||||
let elec = vec![1e11]; // ln(1e11) ≈ 25.3
|
||||
let nd = 1;
|
||||
let hglim = 1e10;
|
||||
|
||||
let (frgtab, wlgtab, hydopg) = gomini_interpolate(
|
||||
nugfreq, nugtemp, nugele,
|
||||
&temvec, &elevec, &hydcrs,
|
||||
&temp, &elec, nd, hglim,
|
||||
);
|
||||
|
||||
assert_eq!(frgtab.len(), 2);
|
||||
assert_eq!(wlgtab.len(), 2);
|
||||
assert_eq!(hydopg.len(), 2);
|
||||
assert_eq!(hydopg[0].len(), 1);
|
||||
|
||||
// All values should be finite
|
||||
for row in &hydopg {
|
||||
for &val in row {
|
||||
assert!(val.is_finite(), "hydopg value not finite: {}", val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gomini_interpolate_below_hglim() {
|
||||
let nugfreq = 1;
|
||||
let nugtemp = 2;
|
||||
let nugele = 2;
|
||||
|
||||
let temvec = vec![10.0, 11.0];
|
||||
let elevec = vec![10.0, 12.0];
|
||||
let hydcrs = vec![
|
||||
vec![vec![1.0], vec![2.0]],
|
||||
vec![vec![3.0], vec![4.0]],
|
||||
];
|
||||
|
||||
// electron density below hglim
|
||||
let temp = vec![22000.0];
|
||||
let elec = vec![1e5];
|
||||
let nd = 1;
|
||||
let hglim = 1e10;
|
||||
|
||||
let (_, _, hydopg) = gomini_interpolate(
|
||||
nugfreq, nugtemp, nugele,
|
||||
&temvec, &elevec, &hydcrs,
|
||||
&temp, &elec, nd, hglim,
|
||||
);
|
||||
|
||||
// Should be zero (skipped)
|
||||
assert_eq!(hydopg[0][0], 0.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
//! Van der Waals broadening parameter evaluation.
|
||||
//!
|
||||
//! Translated from SYNSPEC54.FOR function GVDW(IL,ILIST,ID) at line 19468.
|
||||
//!
|
||||
//! Supports two modes:
|
||||
//! - Standard expression (`ivdwli == 0`)
|
||||
//! - EXOMOL form with H2 and He broadening (`ivdwli > 0`)
|
||||
|
||||
/// Parameters for Van der Waals broadening calculation.
|
||||
pub struct GvdwParams<'a> {
|
||||
/// Line index
|
||||
pub il: usize,
|
||||
/// Line list index
|
||||
pub ilist: usize,
|
||||
/// Depth index
|
||||
pub id: usize,
|
||||
/// Van der Waals damping parameter (standard mode)
|
||||
pub gwm: f64,
|
||||
/// Van der Waals coefficient at depth
|
||||
pub vdwc: &'a [f64],
|
||||
/// Mode of evaluation per line list (0 = standard, >0 = EXOMOL)
|
||||
pub ivdwli: &'a [i32],
|
||||
/// Temperature at each depth
|
||||
pub temp: &'a [f64],
|
||||
/// He number density at each depth (from rrr array)
|
||||
pub anhe: f64,
|
||||
/// H2 number density at each depth
|
||||
pub anh2: &'a [f64],
|
||||
/// EXOMOL H2 broadening exponent
|
||||
pub gexph2: f64,
|
||||
/// EXOMOL H2 broadening width
|
||||
pub gvdwh2: f64,
|
||||
/// EXOMOL He broadening exponent
|
||||
pub gexphe: f64,
|
||||
/// EXOMOL He broadening width
|
||||
pub gvdwhe: f64,
|
||||
}
|
||||
|
||||
/// Van der Waals broadening parameter.
|
||||
///
|
||||
/// Computes the Van der Waals broadening parameter for spectral line profiles.
|
||||
/// Supports both the standard classical expression and the EXOMOL form
|
||||
/// (broadening by H2 and He).
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `params` - Calculation parameters
|
||||
///
|
||||
/// # Returns
|
||||
/// Van der Waals broadening parameter
|
||||
pub fn gvdw(params: &GvdwParams) -> f64 {
|
||||
// Standard classical expression
|
||||
if params.ivdwli[params.ilist] == 0 {
|
||||
return params.gwm * params.vdwc[params.id];
|
||||
}
|
||||
|
||||
// EXOMOL form - broadening by H2 and He
|
||||
// con = 1e-6 * c * k (cgs)
|
||||
let con = 4.1388e-12;
|
||||
let t = params.temp[params.id];
|
||||
|
||||
con * t
|
||||
* ((296.0 / t).powf(params.gexph2) * params.gvdwh2 * params.anh2[params.id]
|
||||
+ (296.0 / t).powf(params.gexphe) * params.gvdwhe * params.anhe)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_gvdw_standard_mode() {
|
||||
let vdwc = vec![1.0, 2.0, 3.0];
|
||||
let ivdwli = vec![0];
|
||||
let temp = vec![5000.0, 6000.0, 7000.0];
|
||||
let anh2 = vec![1e10, 1e10, 1e10];
|
||||
|
||||
let params = GvdwParams {
|
||||
il: 0,
|
||||
ilist: 0,
|
||||
id: 1,
|
||||
gwm: 0.5,
|
||||
vdwc: &vdwc,
|
||||
ivdwli: &ivdwli,
|
||||
temp: &temp,
|
||||
anhe: 1e10,
|
||||
anh2: &anh2,
|
||||
gexph2: 0.0,
|
||||
gvdwh2: 0.0,
|
||||
gexphe: 0.0,
|
||||
gvdwhe: 0.0,
|
||||
};
|
||||
|
||||
// Standard: gwm * vdwc[id] = 0.5 * 2.0 = 1.0
|
||||
let result = gvdw(¶ms);
|
||||
assert!((result - 1.0).abs() < 1e-15);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gvdw_exomol_mode() {
|
||||
let vdwc = vec![1.0; 3];
|
||||
let ivdwli = vec![1];
|
||||
let temp = vec![5000.0; 3];
|
||||
let anh2 = vec![1e12; 3];
|
||||
|
||||
let params = GvdwParams {
|
||||
il: 0,
|
||||
ilist: 0,
|
||||
id: 0,
|
||||
gwm: 1.0,
|
||||
vdwc: &vdwc,
|
||||
ivdwli: &ivdwli,
|
||||
temp: &temp,
|
||||
anhe: 1e11,
|
||||
anh2: &anh2,
|
||||
gexph2: 0.5,
|
||||
gvdwh2: 1e-9,
|
||||
gexphe: 0.3,
|
||||
gvdwhe: 5e-10,
|
||||
};
|
||||
|
||||
let result = gvdw(¶ms);
|
||||
assert!(result > 0.0);
|
||||
assert!(result.is_finite());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gvdw_exomol_temperature_dependence() {
|
||||
let vdwc = vec![1.0; 2];
|
||||
let ivdwli = vec![1];
|
||||
let anh2 = vec![1e12; 2];
|
||||
|
||||
let params_low = GvdwParams {
|
||||
il: 0,
|
||||
ilist: 0,
|
||||
id: 0,
|
||||
gwm: 1.0,
|
||||
vdwc: &vdwc,
|
||||
ivdwli: &ivdwli,
|
||||
temp: &[3000.0, 10000.0],
|
||||
anhe: 1e11,
|
||||
anh2: &anh2,
|
||||
gexph2: 0.5,
|
||||
gvdwh2: 1e-9,
|
||||
gexphe: 0.3,
|
||||
gvdwhe: 5e-10,
|
||||
};
|
||||
|
||||
let params_high = GvdwParams {
|
||||
id: 1,
|
||||
..params_low
|
||||
};
|
||||
|
||||
let r_low = gvdw(¶ms_low);
|
||||
let r_high = gvdw(¶ms_high);
|
||||
// Both should be positive
|
||||
assert!(r_low > 0.0);
|
||||
assert!(r_high > 0.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
//! H2⁻ 自由-自由吸收系数计算。
|
||||
//!
|
||||
//! 重构自 SYNSPEC `synspec54.f` 中的 `h2minus` 子程序。
|
||||
//!
|
||||
//! 数据来源: K L Bell 1980, J. Phys. B: At. Mol. Phys. 13 1859, Table 1
|
||||
//! 单位: 10^26 cm^4/dyn^-1
|
||||
|
||||
use crate::tlusty::math::interpolation::locate;
|
||||
use crate::synspec::math::{CL, BOLK};
|
||||
|
||||
// ============================================================================
|
||||
// 静态数据表
|
||||
// ============================================================================
|
||||
|
||||
/// theta = 5040/T(K) 网格点 (9 个)
|
||||
const FFTHET: [f64; 9] = [0.5, 0.8, 1.0, 1.2, 1.6, 2.0, 2.8, 3.6, 10.0];
|
||||
|
||||
/// lambda (Angstroms) 网格点 (18 个)
|
||||
const FFLAMB: [f64; 18] = [
|
||||
151883.0, 113913.0, 91130.0, 60753.0,
|
||||
45565.0, 36452.0, 30377.0, 22783.0,
|
||||
18226.0, 15188.0, 11391.0, 9113.0, 7594.0,
|
||||
6509.0, 5696.0, 5063.0, 4142.0, 3505.0,
|
||||
];
|
||||
|
||||
/// kappa 表 (18 x 9),按列优先存储 (Fortran 布局)
|
||||
const NTHET: usize = 9;
|
||||
const NLAMB: usize = 18;
|
||||
|
||||
/// FFkapp 表,按 Fortran 列优先存储: FFkapp[theta_idx * NLAMB + lamb_idx]
|
||||
/// 即 FFkapp(i,j) = FFKAPP[j * 18 + i],其中 i=lambda, j=theta (0-based)
|
||||
const FFKAPP: [f64; NLAMB * NTHET] = [
|
||||
// 列 1 (theta=0.5): 18 个 lambda 值
|
||||
7.16e+01, 4.03e+01, 2.58e+01, 1.15e+01, 6.47e+00,
|
||||
4.15e+00, 2.89e+00, 1.63e+00, 1.05e+00, 7.36e-01,
|
||||
4.20e-01, 2.73e-01, 1.92e-01, 1.43e-01, 1.10e-01,
|
||||
8.70e-02, 5.84e-02, 4.17e-02,
|
||||
// 列 2 (theta=0.8)
|
||||
9.23e+01, 5.20e+01, 3.33e+01, 1.48e+01, 8.37e+00,
|
||||
5.38e+00, 3.76e+00, 2.14e+00, 1.39e+00, 9.75e-01,
|
||||
5.64e-01, 3.71e-01, 2.64e-01, 1.98e-01, 1.54e-01,
|
||||
1.24e-01, 8.43e-02, 6.10e-02,
|
||||
// 列 3 (theta=1.0)
|
||||
1.01e+02, 5.70e+01, 3.65e+01, 1.63e+01, 9.20e+00,
|
||||
5.92e+00, 4.14e+00, 2.36e+00, 1.54e+00, 1.09e+00,
|
||||
6.35e-01, 4.22e-01, 3.03e-01, 2.30e-01, 1.80e-01,
|
||||
1.46e-01, 1.01e-01, 7.34e-02,
|
||||
// 列 4 (theta=1.2)
|
||||
1.08e+02, 6.08e+01, 3.90e+01, 1.74e+01, 9.84e+00,
|
||||
6.35e+00, 4.44e+00, 2.55e+00, 1.66e+00, 1.18e+00,
|
||||
6.97e-01, 4.67e-01, 3.39e-01, 2.59e-01, 2.06e-01,
|
||||
1.67e-01, 1.17e-01, 8.59e-02,
|
||||
// 列 5 (theta=1.6)
|
||||
1.18e+02, 6.65e+01, 4.27e+01, 1.91e+01, 1.08e+01,
|
||||
6.99e+00, 4.91e+00, 2.84e+00, 1.87e+00, 1.34e+00,
|
||||
8.06e-01, 5.52e-01, 4.08e-01, 3.17e-01, 2.55e-01,
|
||||
2.10e-01, 1.49e-01, 1.11e-01,
|
||||
// 列 6 (theta=2.0)
|
||||
1.26e+02, 7.08e+01, 4.54e+01, 2.04e+01, 1.16e+01,
|
||||
7.50e+00, 5.28e+00, 3.07e+00, 2.04e+00, 1.48e+00,
|
||||
9.09e-01, 6.33e-01, 4.76e-01, 3.75e-01, 3.05e-01,
|
||||
2.53e-01, 1.82e-01, 1.37e-01,
|
||||
// 列 7 (theta=2.8)
|
||||
1.38e+02, 7.76e+01, 4.98e+01, 2.24e+01, 1.28e+01,
|
||||
8.32e+00, 5.90e+00, 3.49e+00, 2.36e+00, 1.74e+00,
|
||||
1.11e+00, 7.97e-01, 6.13e-01, 4.92e-01, 4.06e-01,
|
||||
3.39e-01, 2.49e-01, 1.87e-01,
|
||||
// 列 8 (theta=3.6)
|
||||
1.47e+02, 8.30e+01, 5.33e+01, 2.40e+01, 1.38e+01,
|
||||
9.02e+00, 6.44e+00, 3.90e+00, 2.68e+00, 2.01e+00,
|
||||
1.32e+00, 9.63e-01, 7.51e-01, 6.09e-01, 5.07e-01,
|
||||
4.27e-01, 3.16e-01, 2.40e-01,
|
||||
// 列 9 (theta=10.0) — 线性外推
|
||||
2.19e+02, 1.26e+02, 8.13e+01, 3.68e+01, 2.18e+01,
|
||||
1.46e+01, 1.08e+01, 7.18e+00, 5.24e+00, 4.17e+00,
|
||||
3.00e+00, 2.29e+00, 1.86e+00, 1.55e+00, 1.32e+00,
|
||||
1.13e+00, 8.52e-01, 6.64e-01,
|
||||
];
|
||||
|
||||
// ============================================================================
|
||||
// h2minus - H2⁻ 自由-自由吸收
|
||||
// ============================================================================
|
||||
|
||||
/// 计算 H2⁻ 自由-自由吸收系数。
|
||||
///
|
||||
/// # 参数
|
||||
///
|
||||
/// - `t` - 温度 (K)
|
||||
/// - `anh2` - H2 分子数密度
|
||||
/// - `ane` - 电子数密度
|
||||
/// - `fr` - 频率 (Hz)
|
||||
///
|
||||
/// # 返回
|
||||
///
|
||||
/// H2⁻ 自由-自由吸收系数 `oph2m`
|
||||
pub fn h2minus(t: f64, anh2: f64, ane: f64, fr: f64) -> f64 {
|
||||
// theta = 5040 / T
|
||||
let theta = 5040.0 / t;
|
||||
|
||||
// 在温度数组中定位 (0-indexed)
|
||||
// locate 返回 j 使得 FFTHET[j] <= theta < FFTHET[j+1]
|
||||
let j = locate(&FFTHET, theta);
|
||||
|
||||
// 波长 (Angstroms): lambda = c / fr * 1e8
|
||||
let flamb = CL * 1.0e8 / fr;
|
||||
|
||||
// 在波长数组中定位 (0-indexed)
|
||||
let i = locate(&FFLAMB, flamb);
|
||||
|
||||
// 双线性插值
|
||||
// 注意: FFTHET 是递增的,FFLAMB 是递减的
|
||||
let fkappa = if j >= NTHET - 1 {
|
||||
// theta >= FFTHET[NTHET-1],保持恒定 (高温端)
|
||||
let i_clamped = i.min(NLAMB - 2);
|
||||
let y1 = ffkapp_at(i_clamped, NTHET - 1);
|
||||
let y2 = ffkapp_at(i_clamped + 1, NTHET - 1);
|
||||
let tt = (flamb - FFLAMB[i_clamped]) / (FFLAMB[i_clamped + 1] - FFLAMB[i_clamped]);
|
||||
(1.0 - tt) * y1 + tt * y2
|
||||
} else if !(FFLAMB[NLAMB - 1]..=FFLAMB[0]).contains(&flamb) {
|
||||
// 超出波长表范围 (FFLAMB 递减: [0] 最大, [NLAMB-1] 最小)
|
||||
0.0
|
||||
} else {
|
||||
// 表内双线性插值
|
||||
let y1 = ffkapp_at(i, j);
|
||||
let y2 = ffkapp_at(i + 1, j);
|
||||
let y3 = ffkapp_at(i + 1, j + 1);
|
||||
let y4 = ffkapp_at(i, j + 1);
|
||||
|
||||
// tt: 波长方向插值 (FFLAMB 递减)
|
||||
let tt = (flamb - FFLAMB[i]) / (FFLAMB[i + 1] - FFLAMB[i]);
|
||||
// uu: 温度方向插值 (FFTHET 递增)
|
||||
let uu = (theta - FFTHET[j]) / (FFTHET[j + 1] - FFTHET[j]);
|
||||
|
||||
(1.0 - tt) * (1.0 - uu) * y1
|
||||
+ tt * (1.0 - uu) * y2
|
||||
+ tt * uu * y3
|
||||
+ (1.0 - tt) * uu * y4
|
||||
};
|
||||
|
||||
// 电子压力
|
||||
let pe = ane * BOLK * t;
|
||||
|
||||
// 最终吸收系数
|
||||
anh2 * 1.0e-26 * pe * fkappa
|
||||
}
|
||||
|
||||
/// 从 FFkapp 表中获取值 (处理边界)
|
||||
/// 索引: FFkapp(i,j) = FFKAPP[j * NLAMB + i],其中 i=lambda, j=theta (0-based)
|
||||
fn ffkapp_at(i: usize, j: usize) -> f64 {
|
||||
let i_clamped = i.min(NLAMB - 1);
|
||||
let j_clamped = j.min(NTHET - 1);
|
||||
FFKAPP[j_clamped * NLAMB + i_clamped]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_h2minus_basic() {
|
||||
// 典型恒星大气参数
|
||||
let t = 5000.0; // K
|
||||
let anh2 = 1.0e15; // H2 数密度
|
||||
let ane = 1.0e13; // 电子数密度
|
||||
let fr = 1.0e14; // Hz (红外)
|
||||
|
||||
let oph2m = h2minus(t, anh2, ane, fr);
|
||||
assert!(oph2m > 0.0, "oph2m 应为正值: {}", oph2m);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_h2minus_high_temperature() {
|
||||
// 高温情况
|
||||
let t = 10000.0;
|
||||
let anh2 = 1.0e14;
|
||||
let ane = 1.0e12;
|
||||
let fr = 3.0e14;
|
||||
|
||||
let oph2m = h2minus(t, anh2, ane, fr);
|
||||
assert!(oph2m >= 0.0, "oph2m 应非负: {}", oph2m);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_h2minus_low_temperature() {
|
||||
// 低温情况 (theta 大)
|
||||
let t = 3000.0;
|
||||
let anh2 = 1.0e16;
|
||||
let ane = 1.0e14;
|
||||
let fr = 5.0e14;
|
||||
|
||||
let oph2m = h2minus(t, anh2, ane, fr);
|
||||
assert!(oph2m >= 0.0, "oph2m 应非负: {}", oph2m);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_h2minus_scaling() {
|
||||
// 吸收系数应与 anh2 和 ane 成正比
|
||||
let t = 6000.0;
|
||||
let fr = 2.0e14;
|
||||
|
||||
let oph2m1 = h2minus(t, 1.0e14, 1.0e12, fr);
|
||||
let oph2m2 = h2minus(t, 2.0e14, 1.0e12, fr);
|
||||
let oph2m3 = h2minus(t, 1.0e14, 2.0e12, fr);
|
||||
|
||||
// 双倍 anh2 → 双倍 opacity
|
||||
assert!(
|
||||
(oph2m2 / oph2m1 - 2.0).abs() < 0.01,
|
||||
"anh2 线性性: {} vs {}",
|
||||
oph2m2,
|
||||
oph2m1
|
||||
);
|
||||
// 双倍 ane → 双倍 opacity (pe 线性)
|
||||
assert!(
|
||||
(oph2m3 / oph2m1 - 2.0).abs() < 0.01,
|
||||
"ane 线性性: {} vs {}",
|
||||
oph2m3,
|
||||
oph2m1
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
//! Partition function for H2O from EXOMOL data.
|
||||
//!
|
||||
//! Translated from SYNSPEC `h2opf` subroutine.
|
||||
|
||||
use std::sync::OnceLock;
|
||||
|
||||
const TABLE_SIZE: usize = 10000;
|
||||
const DATA_FILE: &str = "./data/h2o_exomol.pf";
|
||||
|
||||
static TABLE: OnceLock<Option<(Vec<f64>, Vec<f64>)>> = OnceLock::new();
|
||||
|
||||
fn load_table() -> Option<(Vec<f64>, Vec<f64>)> {
|
||||
let content = std::fs::read_to_string(DATA_FILE).ok()?;
|
||||
let mut ttab = Vec::with_capacity(TABLE_SIZE);
|
||||
let mut pftab = Vec::with_capacity(TABLE_SIZE);
|
||||
for line in content.lines().take(TABLE_SIZE) {
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() >= 2
|
||||
&& let (Ok(t), Ok(pf)) = (parts[0].parse::<f64>(), parts[1].parse::<f64>()) {
|
||||
ttab.push(t);
|
||||
pftab.push(pf);
|
||||
}
|
||||
}
|
||||
Some((ttab, pftab))
|
||||
}
|
||||
|
||||
/// Evaluate H2O partition function at temperature `t` by linear interpolation.
|
||||
///
|
||||
/// Returns `None` if the data file cannot be loaded.
|
||||
pub fn h2opf(t: f64) -> Option<f64> {
|
||||
let table = TABLE.get_or_init(load_table).as_ref()?;
|
||||
let (ref ttab, ref pftab) = *table;
|
||||
let n = ttab.len();
|
||||
if n < 2 || t < ttab[0] || t > ttab[n - 1] {
|
||||
return None;
|
||||
}
|
||||
let itab = t.floor() as usize;
|
||||
if itab >= n - 1 {
|
||||
return None;
|
||||
}
|
||||
let idx = itab.min(n - 2);
|
||||
let pf = pftab[idx] + (t - ttab[idx]) * (pftab[idx + 1] - pftab[idx]);
|
||||
Some(pf)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_h2opf_basic() {
|
||||
// Without the data file, should return None
|
||||
// This test just verifies the function compiles and runs
|
||||
let _ = h2opf(5000.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
//! He I line profile data initialization.
|
||||
//!
|
||||
//! Translated from SYNSPEC54.FOR subroutine HE1INI (line 7242).
|
||||
//!
|
||||
//! Initializes necessary arrays for evaluating the He I line
|
||||
//! absorption profiles using data calculated by Barnard, Cooper
|
||||
//! and Smith JQSRT 14, 1025, 1974 (for 4471)
|
||||
//! or Shamey, unpublished PhD thesis, 1969 (for other lines).
|
||||
|
||||
#![allow(clippy::never_loop)]
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::Path;
|
||||
|
||||
/// Constants for He I profile arrays
|
||||
pub const NT: usize = 4;
|
||||
pub const NE_4471: usize = 7;
|
||||
pub const NE_OTHER: usize = 8;
|
||||
pub const NWL_MAX_4471: usize = 80;
|
||||
pub const NWL_MAX_OTHER: usize = 50;
|
||||
pub const NLINES: usize = 3;
|
||||
|
||||
/// He I 4471 line profile data (Barnard, Cooper, Smith)
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct He1Profile4471 {
|
||||
/// Log10 of electron densities [NE_4471]
|
||||
pub xne: [f64; NE_4471],
|
||||
/// Number of wavelength points for each electron density [NE_4471]
|
||||
pub nwlam: [usize; NE_4471],
|
||||
/// Wavelength displacements [NWL_MAX_4471 x NE_4471]
|
||||
pub dlam: [[f64; NE_4471]; NWL_MAX_4471],
|
||||
/// Profile values [NWL_MAX_4471 x NT x NE_4471]
|
||||
pub prf: [[[f64; NE_4471]; NT]; NWL_MAX_4471],
|
||||
}
|
||||
|
||||
/// He I other lines profile data (Shamey)
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct He1ProfileOther {
|
||||
/// Log10 of electron densities [NE_OTHER]
|
||||
pub xne: [f64; NE_OTHER],
|
||||
/// Number of wavelength points [NE_OTHER x NLINES+1] (index 0 unused)
|
||||
pub nwlam: [[usize; NLINES + 1]; NE_OTHER],
|
||||
/// Wavelength displacements [NWL_MAX_OTHER x NE_OTHER x NLINES]
|
||||
pub dlam: [[[f64; NLINES]; NE_OTHER]; NWL_MAX_OTHER],
|
||||
/// Profile values [NWL_MAX_OTHER x NT x NE_OTHER x NLINES]
|
||||
pub prf: [[[[f64; NLINES]; NE_OTHER]; NT]; NWL_MAX_OTHER],
|
||||
}
|
||||
|
||||
/// Complete He I profile data
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Default)]
|
||||
pub struct He1ProfileData {
|
||||
/// 4471 line data
|
||||
pub data_4471: He1Profile4471,
|
||||
/// Other lines data (4387, 4026, 4922)
|
||||
pub data_other: He1ProfileOther,
|
||||
}
|
||||
|
||||
impl Default for He1Profile4471 {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
xne: [0.0; NE_4471],
|
||||
nwlam: [0; NE_4471],
|
||||
dlam: [[0.0; NE_4471]; NWL_MAX_4471],
|
||||
prf: [[[0.0; NE_4471]; NT]; NWL_MAX_4471],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for He1ProfileOther {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
xne: [0.0; NE_OTHER],
|
||||
nwlam: [[0; NLINES + 1]; NE_OTHER],
|
||||
dlam: [[[0.0; NLINES]; NE_OTHER]; NWL_MAX_OTHER],
|
||||
prf: [[[[0.0; NLINES]; NE_OTHER]; NT]; NWL_MAX_OTHER],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Read He I line profile data from file.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `path` - Path to he1prf.dat file
|
||||
///
|
||||
/// # Returns
|
||||
/// Complete He I profile data structure
|
||||
pub fn he1ini<P: AsRef<Path>>(path: P) -> std::io::Result<He1ProfileData> {
|
||||
let file = File::open(path)?;
|
||||
let reader = BufReader::new(file);
|
||||
let mut lines = reader.lines();
|
||||
|
||||
let mut data = He1ProfileData::default();
|
||||
|
||||
// Read Barnard, Cooper, Smith tables for He I 4471 line
|
||||
for ie in 0..NE_4471 {
|
||||
// Skip header line and read: IL, WL0, IE1, XXNE, NWL
|
||||
let header = read_next_line(&mut lines)?;
|
||||
let parts = parse_header(&header)?;
|
||||
|
||||
let _il = parts.0; // line index (unused)
|
||||
let _wl0 = parts.1; // wavelength (unused)
|
||||
let _ie1 = parts.2; // electron density index (unused)
|
||||
let xxne = parts.3; // electron density
|
||||
let nwl = parts.4; // number of wavelength points
|
||||
|
||||
data.data_4471.nwlam[ie] = nwl;
|
||||
data.data_4471.xne[ie] = xxne.log10();
|
||||
|
||||
// Read profile data
|
||||
for i in 0..nwl.min(NWL_MAX_4471) {
|
||||
let line = read_next_line(&mut lines)?;
|
||||
let values = parse_profile_line(&line)?;
|
||||
|
||||
data.data_4471.dlam[i][ie] = values[0];
|
||||
for it in 0..NT {
|
||||
if it + 1 < values.len() {
|
||||
data.data_4471.prf[i][it][ie] = values[it + 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Read Shamey's tables for He I 4387, 4026, and 4922 lines
|
||||
for iln in 0..NLINES {
|
||||
for ie in 0..NE_OTHER {
|
||||
let header = read_next_line(&mut lines)?;
|
||||
let parts = parse_header(&header)?;
|
||||
|
||||
let xxne = parts.3;
|
||||
let nwl = parts.4;
|
||||
|
||||
data.data_other.nwlam[ie][iln + 1] = nwl;
|
||||
data.data_other.xne[ie] = xxne.log10();
|
||||
|
||||
// Read profile data
|
||||
for i in 0..nwl.min(NWL_MAX_OTHER) {
|
||||
let line = read_next_line(&mut lines)?;
|
||||
let values = parse_profile_line(&line)?;
|
||||
|
||||
data.data_other.dlam[i][ie][iln] = values[0];
|
||||
for it in 0..NT {
|
||||
if it + 1 < values.len() {
|
||||
data.data_other.prf[i][it][ie][iln] = values[it + 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
/// Read next non-empty line from iterator
|
||||
fn read_next_line(lines: &mut impl Iterator<Item = std::io::Result<String>>) -> std::io::Result<String> {
|
||||
loop {
|
||||
match lines.next() {
|
||||
Some(Ok(line)) => return Ok(line),
|
||||
Some(Err(e)) => return Err(e),
|
||||
None => return Err(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "Unexpected end of file")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse header line: IL, WL0, IE1, XXNE, NWL
|
||||
fn parse_header(line: &str) -> std::io::Result<(usize, f64, usize, f64, usize)> {
|
||||
// FORMAT(/9X,I2,7X,F10.3,13X,I2,6X,E8.1,7X,I3/)
|
||||
// This is a fixed-format line, but we'll try free-format parsing
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() < 5 {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
format!("Invalid header line: {}", line),
|
||||
));
|
||||
}
|
||||
|
||||
let il = parts[0].parse::<usize>().map_err(|e| {
|
||||
std::io::Error::new(std::io::ErrorKind::InvalidData, format!("IL: {}", e))
|
||||
})?;
|
||||
let wl0 = parts[1].parse::<f64>().map_err(|e| {
|
||||
std::io::Error::new(std::io::ErrorKind::InvalidData, format!("WL0: {}", e))
|
||||
})?;
|
||||
let ie1 = parts[2].parse::<usize>().map_err(|e| {
|
||||
std::io::Error::new(std::io::ErrorKind::InvalidData, format!("IE1: {}", e))
|
||||
})?;
|
||||
let xxne = parts[3].parse::<f64>().map_err(|e| {
|
||||
std::io::Error::new(std::io::ErrorKind::InvalidData, format!("XXNE: {}", e))
|
||||
})?;
|
||||
let nwl = parts[4].parse::<usize>().map_err(|e| {
|
||||
std::io::Error::new(std::io::ErrorKind::InvalidData, format!("NWL: {}", e))
|
||||
})?;
|
||||
|
||||
Ok((il, wl0, ie1, xxne, nwl))
|
||||
}
|
||||
|
||||
/// Parse profile data line: DLAM, PRF(IT=1..NT)
|
||||
fn parse_profile_line(line: &str) -> std::io::Result<Vec<f64>> {
|
||||
// FORMAT(5E10.2) - 5 values per line
|
||||
let values: Vec<f64> = line
|
||||
.split_whitespace()
|
||||
.map(|s| s.parse::<f64>())
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.map_err(|e| {
|
||||
std::io::Error::new(std::io::ErrorKind::InvalidData, format!("Profile value: {}", e))
|
||||
})?;
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_he1ini_default() {
|
||||
let data = He1ProfileData::default();
|
||||
assert_eq!(data.data_4471.xne.len(), NE_4471);
|
||||
assert_eq!(data.data_other.xne.len(), NE_OTHER);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_header() {
|
||||
let line = " 1 4471.000 1 1.0E+12 50";
|
||||
let result = parse_header(line);
|
||||
assert!(result.is_ok());
|
||||
let (il, wl0, ie1, xxne, nwl) = result.unwrap();
|
||||
assert_eq!(il, 1);
|
||||
assert!((wl0 - 4471.0).abs() < 0.01);
|
||||
assert_eq!(ie1, 1);
|
||||
assert!((xxne - 1.0e12).abs() < 1e10);
|
||||
assert_eq!(nwl, 50);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_profile_line() {
|
||||
let line = " 0.123 0.456 0.789 0.111 0.222";
|
||||
let result = parse_profile_line(line);
|
||||
assert!(result.is_ok());
|
||||
let values = result.unwrap();
|
||||
assert_eq!(values.len(), 5);
|
||||
assert!((values[0] - 0.123).abs() < 1e-6);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,286 @@
|
||||
//! He II line profile data initialization.
|
||||
//!
|
||||
//! Translated from SYNSPEC54.FOR subroutine HE2INI (line 7535).
|
||||
//!
|
||||
//! Initializes necessary arrays for evaluating the He II line
|
||||
//! absorption profiles using data calculated by Schoening and Butler.
|
||||
|
||||
#![allow(clippy::never_loop)]
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
|
||||
/// Constants for He II profile arrays
|
||||
pub const NLINE_HE2: usize = 19;
|
||||
pub const NWL_HE2_MAX: usize = 36;
|
||||
pub const NT_HE2: usize = 6;
|
||||
pub const NE_HE2: usize = 11;
|
||||
|
||||
/// He II line profile table data
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct He2ProfileTable {
|
||||
/// Lower level index
|
||||
pub il: usize,
|
||||
/// Upper level index
|
||||
pub iu: usize,
|
||||
/// Central wavelength
|
||||
pub wl0: f64,
|
||||
/// Number of wavelength points
|
||||
pub nwl: usize,
|
||||
/// Log10 wavelength displacements [NWL_HE2_MAX]
|
||||
pub wl: [f64; NWL_HE2_MAX],
|
||||
/// Log10 temperature grid [NT_HE2]
|
||||
pub xt: [f64; NT_HE2],
|
||||
/// Log10 electron density grid [NE_HE2]
|
||||
pub xne: [f64; NE_HE2],
|
||||
/// Profile values [NWL_HE2_MAX x NT_HE2 x NE_HE2]
|
||||
pub prf: [[[f64; NE_HE2]; NT_HE2]; NWL_HE2_MAX],
|
||||
/// Asymptotic profile coefficient
|
||||
pub xk: f64,
|
||||
}
|
||||
|
||||
impl Default for He2ProfileTable {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
il: 0,
|
||||
iu: 0,
|
||||
wl0: 0.0,
|
||||
nwl: 0,
|
||||
wl: [0.0; NWL_HE2_MAX],
|
||||
xt: [0.0; NT_HE2],
|
||||
xne: [0.0; NE_HE2],
|
||||
prf: [[[0.0; NE_HE2]; NT_HE2]; NWL_HE2_MAX],
|
||||
xk: 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// He II line initialization result
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct He2InitResult {
|
||||
/// Profile tables for each line
|
||||
pub tables: Vec<He2ProfileTable>,
|
||||
/// Number of wavelength points per line [NLINE_HE2]
|
||||
pub nwlhe2: [usize; NLINE_HE2],
|
||||
/// Lower level indices [NLINE_HE2]
|
||||
pub ilhe2: [usize; NLINE_HE2],
|
||||
/// Upper level indices [NLINE_HE2]
|
||||
pub iuhe2: [usize; NLINE_HE2],
|
||||
}
|
||||
|
||||
impl Default for He2InitResult {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
tables: Vec::new(),
|
||||
nwlhe2: [0; NLINE_HE2],
|
||||
ilhe2: [0; NLINE_HE2],
|
||||
iuhe2: [0; NLINE_HE2],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parameters for HE2INI
|
||||
pub struct He2iniParams {
|
||||
/// Path to data directory
|
||||
pub data_dir: String,
|
||||
/// Model depth points
|
||||
pub nd: usize,
|
||||
/// Temperature array [nd]
|
||||
pub temp: Vec<f64>,
|
||||
/// Electron density array [nd]
|
||||
pub elec: Vec<f64>,
|
||||
/// Turbulent velocity array [nd]
|
||||
pub vturb: Vec<f64>,
|
||||
}
|
||||
|
||||
/// Initialize He II line profile data.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `params` - Initialization parameters
|
||||
///
|
||||
/// # Returns
|
||||
/// He II line initialization result with profile tables
|
||||
pub fn he2ini(params: &He2iniParams) -> std::io::Result<He2InitResult> {
|
||||
let filename = format!("{}/he2prf.dat", params.data_dir);
|
||||
let file = File::open(&filename)?;
|
||||
let reader = BufReader::new(file);
|
||||
let mut lines = reader.lines();
|
||||
|
||||
let mut result = He2InitResult::default();
|
||||
|
||||
for iline in 0..NLINE_HE2 {
|
||||
// Read line indices: FORMAT(//14X,I2,9X,I2/)
|
||||
let header = read_next_nonblank(&mut lines)?;
|
||||
let (il, iu) = parse_he2_header(&header)?;
|
||||
|
||||
result.ilhe2[iline] = il;
|
||||
result.iuhe2[iline] = iu;
|
||||
|
||||
// Compute central wavelength
|
||||
let wl00 = if il <= 2 { 227.838 } else { 227.7776 };
|
||||
let wl0 = wl00 / (1.0 / (il as f64).powi(2) - 1.0 / (iu as f64).powi(2));
|
||||
|
||||
let mut table = He2ProfileTable {
|
||||
il,
|
||||
iu,
|
||||
wl0,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// Read wavelength points
|
||||
let wl_line = read_next_line(&mut lines)?;
|
||||
let wl_parts = parse_he2_data(&wl_line)?;
|
||||
let nwl = wl_parts[0] as usize;
|
||||
table.nwl = nwl;
|
||||
result.nwlhe2[iline] = nwl;
|
||||
for i in 0..nwl.min(NWL_HE2_MAX) {
|
||||
table.wl[i] = if wl_parts[i + 1] < 1.0e-4 {
|
||||
(1.0e-4_f64).log10()
|
||||
} else {
|
||||
wl_parts[i + 1].log10()
|
||||
};
|
||||
}
|
||||
|
||||
// Read temperature points: FORMAT(2X,I4,F10.3,5F12.3)
|
||||
let xt_line = read_next_line(&mut lines)?;
|
||||
let xt_parts = parse_he2_data(&xt_line)?;
|
||||
let nt = xt_parts[0] as usize;
|
||||
for i in 0..nt.min(NT_HE2) {
|
||||
table.xt[i] = xt_parts[i + 1];
|
||||
}
|
||||
|
||||
// Read electron density points: FORMAT(2X,I4,F10.2,5F12.2/4X,5F12.2)
|
||||
let xne_line = read_next_line(&mut lines)?;
|
||||
let xne_parts = parse_he2_data(&xne_line)?;
|
||||
let ne = xne_parts[0] as usize;
|
||||
for i in 0..ne.min(NE_HE2) {
|
||||
table.xne[i] = xne_parts[i + 1];
|
||||
}
|
||||
|
||||
// Skip blank line
|
||||
lines.next();
|
||||
|
||||
// Read profile data: FORMAT(10F8.3)
|
||||
for ie in 0..ne.min(NE_HE2) {
|
||||
for _it in 0..nt.min(NT_HE2) {
|
||||
lines.next(); // Skip blank line
|
||||
let prf_line = read_next_line(&mut lines)?;
|
||||
let prf_parts = parse_he2_data(&prf_line)?;
|
||||
for iwl in 0..nwl.min(NWL_HE2_MAX) {
|
||||
if iwl < prf_parts.len() {
|
||||
table.prf[iwl][_it][ie] = prf_parts[iwl];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compute asymptotic profile coefficient
|
||||
if nwl > 0 && ne > 0 {
|
||||
let xclog = table.prf[nwl - 1][0][0]
|
||||
+ 2.5 * table.wl[nwl - 1]
|
||||
+ 31.831
|
||||
- table.xne[0]
|
||||
- 2.0 * wl0.log10();
|
||||
let xklog = 0.6666667 * (xclog - 0.176);
|
||||
table.xk = (xklog * std::f64::consts::LN_10).exp();
|
||||
}
|
||||
|
||||
result.tables.push(table);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Read next non-empty line
|
||||
fn read_next_line(lines: &mut impl Iterator<Item = std::io::Result<String>>) -> std::io::Result<String> {
|
||||
loop {
|
||||
match lines.next() {
|
||||
Some(Ok(line)) => return Ok(line),
|
||||
Some(Err(e)) => return Err(e),
|
||||
None => return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::UnexpectedEof,
|
||||
"Unexpected end of file",
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Read next non-blank line (skip empty lines)
|
||||
fn read_next_nonblank(lines: &mut impl Iterator<Item = std::io::Result<String>>) -> std::io::Result<String> {
|
||||
loop {
|
||||
let line = read_next_line(lines)?;
|
||||
if !line.trim().is_empty() {
|
||||
return Ok(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse He II header line: FORMAT(//14X,I2,9X,I2/)
|
||||
fn parse_he2_header(line: &str) -> std::io::Result<(usize, usize)> {
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() < 2 {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
format!("Invalid He II header: {}", line),
|
||||
));
|
||||
}
|
||||
|
||||
let il = parts[0].parse::<usize>().map_err(|e| {
|
||||
std::io::Error::new(std::io::ErrorKind::InvalidData, format!("IL: {}", e))
|
||||
})?;
|
||||
let iu = parts[1].parse::<usize>().map_err(|e| {
|
||||
std::io::Error::new(std::io::ErrorKind::InvalidData, format!("IU: {}", e))
|
||||
})?;
|
||||
|
||||
Ok((il, iu))
|
||||
}
|
||||
|
||||
/// Parse He II data line (free format)
|
||||
fn parse_he2_data(line: &str) -> std::io::Result<Vec<f64>> {
|
||||
let values: Vec<f64> = line
|
||||
.split_whitespace()
|
||||
.filter_map(|s| s.parse::<f64>().ok())
|
||||
.collect();
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_he2ini_default() {
|
||||
let result = He2InitResult::default();
|
||||
assert_eq!(result.nwlhe2.len(), NLINE_HE2);
|
||||
assert_eq!(result.ilhe2.len(), NLINE_HE2);
|
||||
assert!(result.tables.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_he2_profile_table_default() {
|
||||
let table = He2ProfileTable::default();
|
||||
assert_eq!(table.nwl, 0);
|
||||
assert_eq!(table.il, 0);
|
||||
assert_eq!(table.iu, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_he2_header() {
|
||||
let line = " 1 2";
|
||||
let result = parse_he2_header(line);
|
||||
assert!(result.is_ok());
|
||||
let (il, iu) = result.unwrap();
|
||||
assert_eq!(il, 1);
|
||||
assert_eq!(iu, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_he2_data() {
|
||||
let line = " 19 0.123 0.456 0.789";
|
||||
let result = parse_he2_data(line);
|
||||
assert!(result.is_ok());
|
||||
let values = result.unwrap();
|
||||
assert_eq!(values.len(), 4);
|
||||
assert!((values[0] - 19.0).abs() < 1e-10);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,729 @@
|
||||
//! He II line opacity and emissivity for SYNSPEC.
|
||||
//!
|
||||
//! Translated from SYNSPEC:
|
||||
//! - `HE2LIN` subroutine (synspec54.f:6247) — standard frequency grid
|
||||
//! - `HE2LIW` subroutine (synspec54.f:6451) — frequency window mode
|
||||
//!
|
||||
//! Calculates opacity and emissivity of He II lines that are not
|
||||
//! considered explicitly (i.e., not handled by the detailed profile
|
||||
//! tables in HE2INI/HE2SEW).
|
||||
|
||||
use super::{divhe2, stark0, starka};
|
||||
|
||||
// ============================================================================
|
||||
// Physical constants
|
||||
// ============================================================================
|
||||
|
||||
const UN: f64 = 1.0;
|
||||
const SIXTH: f64 = 1.0 / 6.0;
|
||||
const CPP: f64 = 4.1412e-16;
|
||||
const CPJ: f64 = 631479.0;
|
||||
const CID: f64 = 0.02654;
|
||||
const CINV: f64 = UN / 2.997925e18;
|
||||
const AL10: f64 = std::f64::consts::LN_10;
|
||||
|
||||
/// He II ionization threshold frequencies (Hz).
|
||||
/// FRHE(n) = R_inf * c / n², for n = 1..12.
|
||||
const FRHE: [f64; 12] = [
|
||||
1.315_815_3e16, 3.289_538_1e15, 1.462_485_4e15,
|
||||
8.226_187_8e14, 5.264_720_1e14, 3.656_045_9e14,
|
||||
2.686_071_3e14, 2.056_522_0e14, 1.624_905_5e14,
|
||||
1.316_173_0e14, 1.087_746_0e14, 9.140_085_1e13,
|
||||
];
|
||||
|
||||
/// He II oscillator strengths (Schoening & Butler).
|
||||
const OSCHE2: [f64; 19] = [
|
||||
6.407e-1, 1.506e-1, 5.584e-2, 2.768e-2,
|
||||
1.604e-2, 1.023e-2, 6.980e-3,
|
||||
8.421e-1, 3.230e-2, 1.870e-2, 1.196e-2, 8.187e-3,
|
||||
5.886e-3, 4.393e-3, 3.375e-3, 2.656e-3,
|
||||
1.038, 1.793e-1, 6.549e-2,
|
||||
];
|
||||
|
||||
/// He II Lyman-series wavelength factor for n <= 2.
|
||||
const WLIN_FACTOR_LOW: f64 = 227.838;
|
||||
/// He II Lyman-series wavelength factor for n > 2.
|
||||
const WLIN_FACTOR_HIGH: f64 = 227.7776;
|
||||
|
||||
// ============================================================================
|
||||
// Shared parameters (used by both he2lin and he2liw)
|
||||
// ============================================================================
|
||||
|
||||
/// Common input data for He II line opacity calculations.
|
||||
#[derive(Clone)]
|
||||
pub struct He2Common<'a> {
|
||||
/// Depth index.
|
||||
pub id: usize,
|
||||
/// Temperature at depth ID (K).
|
||||
pub t: f64,
|
||||
/// Electron density at depth ID.
|
||||
pub ane: f64,
|
||||
/// Turbulent velocity at depth ID (cm/s).
|
||||
pub vturb: f64,
|
||||
/// Surface gravity (log g).
|
||||
pub grav: f64,
|
||||
/// Frequency array (Hz).
|
||||
pub freq: &'a [f64],
|
||||
/// Wavelength array (Å).
|
||||
pub wlam: &'a [f64],
|
||||
/// He II atom index in the model (0 if absent).
|
||||
pub ielhe2: i32,
|
||||
/// He II profile treatment flag (>0: use profile tables).
|
||||
pub ihe2pr: i32,
|
||||
/// First level index for He II element.
|
||||
pub nfirst_he2: usize,
|
||||
/// Last level index for He II element.
|
||||
pub nlast_he2: usize,
|
||||
/// Next element index after He II.
|
||||
pub nnext_he2: usize,
|
||||
/// He III population at depth ID (from model).
|
||||
pub anp_he3: f64,
|
||||
/// LTE He III population from RRR if ielhe2 <= 0.
|
||||
pub rrr_he3: f64,
|
||||
/// Level populations for He II (PJ array, up to 60 levels).
|
||||
/// If None, populations are computed from LTE/Saha.
|
||||
pub pj: Option<&'a [f64]>,
|
||||
/// WNHE2 partition function values (indexed by level, depth).
|
||||
pub wnhe2: &'a [f64],
|
||||
/// Number of wavelength points per profile line.
|
||||
pub nwlhe2: &'a [usize],
|
||||
/// Log10 of profile values: prfhe2[line * 36 + iwl].
|
||||
pub prfhe2: &'a [f64],
|
||||
/// Log10 of wavelength grid per profile line: wlhe2[line * 36 + iwl].
|
||||
pub wlhe2: &'a [f64],
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// HE2LIN — standard frequency grid
|
||||
// ============================================================================
|
||||
|
||||
/// Input parameters for `he2lin`.
|
||||
pub struct He2linParams<'a> {
|
||||
/// Common He II data.
|
||||
pub common: He2Common<'a>,
|
||||
/// Start frequency index.
|
||||
pub i0: usize,
|
||||
/// End frequency index.
|
||||
pub i1: usize,
|
||||
/// He II lowest series index contributing to this frequency region.
|
||||
pub ilwhe2: usize,
|
||||
/// Maximum principal quantum number for explicit He II treatment.
|
||||
pub mhe10: usize,
|
||||
/// Upper limit for He II lines.
|
||||
pub mhe20: usize,
|
||||
}
|
||||
|
||||
/// Result of `he2lin` / `he2liw`.
|
||||
pub struct He2linResult {
|
||||
/// Absorption coefficient array.
|
||||
pub absoh: Vec<f64>,
|
||||
/// Emission coefficient array.
|
||||
pub emish: Vec<f64>,
|
||||
}
|
||||
|
||||
/// Calculate He II line opacity and emissivity (standard frequency grid).
|
||||
///
|
||||
/// Handles He II lines that are not treated with explicit profile tables.
|
||||
/// Uses asymptotic Stark profiles for most lines, with interpolated
|
||||
/// tabulated profiles for specific lines when `ihe2pr > 0`.
|
||||
pub fn he2lin(params: &He2linParams) -> He2linResult {
|
||||
let c = ¶ms.common;
|
||||
let nf = c.freq.len();
|
||||
let mut abso = vec![0.0; nf];
|
||||
let mut emis = vec![0.0; nf];
|
||||
let mut absoh = vec![0.0; nf];
|
||||
let mut emish = vec![0.0; nf];
|
||||
|
||||
let (_t1, _sqt, _ane, _anes, _pp, pj, f00, dop0) = prepare(c);
|
||||
|
||||
// Series range
|
||||
let iseru = params.ilwhe2;
|
||||
let iserl = series_lower(params.ilwhe2);
|
||||
|
||||
// Loop over spectral series
|
||||
for i in iserl..=iseru {
|
||||
let (m1, m2) = determine_lines(i, params.ilwhe2, params.mhe10, params.mhe20, c.grav, c.freq);
|
||||
|
||||
for j in m1..=m2 {
|
||||
let (abtra, emtra, wlin) = transition(i, j, &pj, c, nf);
|
||||
let iline = profile_line_index(i, j, c.ihe2pr);
|
||||
|
||||
if iline > 0 {
|
||||
accumulate_tabulated(
|
||||
c, iline, wlin, abtra, emtra,
|
||||
params.i0, params.i1, nf,
|
||||
&mut abso, &mut emis,
|
||||
);
|
||||
} else {
|
||||
accumulate_stark(
|
||||
c, i, j, f00, dop0, wlin, abtra, emtra,
|
||||
params.i0, params.i1, nf,
|
||||
&mut abso, &mut emis,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Total opacity and emissivity
|
||||
finalize(c, params.i0, params.i1, nf, &abso, &emis, &mut absoh, &mut emish);
|
||||
|
||||
He2linResult { absoh, emish }
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// HE2LIW — frequency window mode
|
||||
// ============================================================================
|
||||
|
||||
/// Per-frequency window parameters for He II lines.
|
||||
pub struct He2liwWindowParams<'a> {
|
||||
/// He II line processing flag per frequency (-1: skip, >0: process).
|
||||
pub ihe2lw: &'a [i32],
|
||||
/// He II series index per frequency.
|
||||
pub ilwhew: &'a [usize],
|
||||
/// Maximum principal quantum number per frequency.
|
||||
pub mhe10w: &'a [usize],
|
||||
/// Upper limit for He II lines per frequency.
|
||||
pub mhe20w: &'a [usize],
|
||||
}
|
||||
|
||||
/// Input parameters for `he2liw`.
|
||||
pub struct He2liwParams<'a> {
|
||||
/// Common He II data.
|
||||
pub common: He2Common<'a>,
|
||||
/// Per-frequency window parameters.
|
||||
pub window: He2liwWindowParams<'a>,
|
||||
/// Overall He II window flag (IFHE2): <=0 means skip entirely.
|
||||
pub ifhe2: i32,
|
||||
}
|
||||
|
||||
/// Calculate He II line opacity and emissivity (frequency window mode).
|
||||
///
|
||||
/// This is the window-mode variant of `he2lin`. It iterates over all
|
||||
/// frequencies individually, using per-frequency window parameters.
|
||||
pub fn he2liw(params: &He2liwParams) -> He2linResult {
|
||||
let c = ¶ms.common;
|
||||
let nf = c.freq.len();
|
||||
let mut abso = vec![0.0; nf];
|
||||
let mut emis = vec![0.0; nf];
|
||||
let mut absoh = vec![0.0; nf];
|
||||
let mut emish = vec![0.0; nf];
|
||||
|
||||
if params.ifhe2 <= 0 {
|
||||
return He2linResult { absoh, emish };
|
||||
}
|
||||
|
||||
let (t1, _sqt, _ane, _anes, _pp, pj, f00, dop0) = prepare(c);
|
||||
|
||||
// Loop over all frequencies
|
||||
for ij in 0..nf {
|
||||
if params.window.ihe2lw[ij] <= 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
let ilw = params.window.ilwhew[ij];
|
||||
let fr = c.freq[ij];
|
||||
let iseru = ilw;
|
||||
let iserl = series_lower(ilw);
|
||||
|
||||
for i in iserl..=iseru {
|
||||
let ii = i * i;
|
||||
let _xii = UN / ii as f64;
|
||||
|
||||
let m1_base = params.window.mhe10w[ij];
|
||||
let m2_base = params.window.mhe20w[ij];
|
||||
|
||||
// Determine contributing lines
|
||||
let mut m1 = m1_base;
|
||||
if i < ilw && FRHE[i - 1] > fr {
|
||||
m1 = ((FRHE[i - 1] * ii as f64 / (FRHE[i - 1] - fr)).sqrt()) as usize;
|
||||
}
|
||||
let mut m2 = m1 + 1;
|
||||
if m1 < i + 1 {
|
||||
m1 = i + 1;
|
||||
}
|
||||
if c.grav < 6.0 && m1 <= 6 && i == 2 {
|
||||
// keep
|
||||
} else if c.grav < 6.0 && m1 <= 4 && i == 1 {
|
||||
// keep
|
||||
} else {
|
||||
m1 = m1.saturating_sub(1);
|
||||
m2 = m2_base + 3;
|
||||
if m2 > 60 {
|
||||
m2 = 60;
|
||||
}
|
||||
}
|
||||
if c.grav > 6.0 {
|
||||
m2 += 5;
|
||||
m1 = m1.saturating_sub(3);
|
||||
if m1 > i + 6 {
|
||||
m1 = m1.saturating_sub(3);
|
||||
}
|
||||
}
|
||||
if m1 < i + 1 {
|
||||
m1 = i + 1;
|
||||
}
|
||||
if m2 > 60 {
|
||||
m2 = 60;
|
||||
}
|
||||
|
||||
for j in m1..=m2 {
|
||||
let (abtra, emtra, wlin) = transition(i, j, &pj, c, nf);
|
||||
let iline = profile_line_index(i, j, c.ihe2pr);
|
||||
|
||||
if iline > 0 {
|
||||
// Tabulated profile (single frequency)
|
||||
let nwl = c.nwlhe2[iline - 1];
|
||||
let fid = CID * OSCHE2[iline - 1];
|
||||
let al_raw = (c.wlam[ij] - wlin).abs();
|
||||
let al = if al_raw < 1.0e-4 { 1.0e-4 } else { al_raw };
|
||||
let al = al.log10();
|
||||
|
||||
let mut iw0 = 0usize;
|
||||
for iwl in 0..nwl - 1 {
|
||||
let wl_next = profile_wl_val(c.wlhe2, iline, iwl + 1);
|
||||
if al <= wl_next {
|
||||
iw0 = iwl;
|
||||
break;
|
||||
}
|
||||
iw0 = iwl;
|
||||
}
|
||||
let iw1 = iw0 + 1;
|
||||
let wl0 = profile_wl_val(c.wlhe2, iline, iw0);
|
||||
let wl1 = profile_wl_val(c.wlhe2, iline, iw1);
|
||||
let prf0 = profile_prf_val(c.prfhe2, iline, iw0);
|
||||
let prf1 = profile_prf_val(c.prfhe2, iline, iw1);
|
||||
|
||||
let denom = wl1 - wl0;
|
||||
let prff = if denom.abs() > 1.0e-30 {
|
||||
(prf0 * (wl1 - al) + prf1 * (al - wl0)) / denom
|
||||
} else {
|
||||
prf0
|
||||
};
|
||||
let sg = (prff * AL10).exp() * fid;
|
||||
abso[ij] += sg * abtra;
|
||||
emis[ij] += sg * emtra;
|
||||
} else {
|
||||
// Asymptotic Stark profile (single frequency)
|
||||
let stark = stark0(i as i32, j as i32, 2);
|
||||
let fxk = f00 * stark.xkij;
|
||||
let fxk1 = UN / fxk;
|
||||
let dop = dop0 / stark.wl0;
|
||||
let dbeta = stark.wl0 * stark.wl0 * CINV * fxk1;
|
||||
let betad = dop * dbeta;
|
||||
let fid = CID * stark.fij * dbeta;
|
||||
let ad = divhe2(betad);
|
||||
let beta = (c.wlam[ij] - stark.wl0).abs() * fxk1;
|
||||
let sg = starka(beta, betad, ad, UN, UN) * fid;
|
||||
abso[ij] += sg * abtra;
|
||||
emis[ij] += sg * emtra;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Total opacity and emissivity for this frequency
|
||||
let f = c.freq[ij];
|
||||
let f15 = f * 1.0e-15;
|
||||
let xkf = (-4.79928e-11 * f * t1).exp();
|
||||
let xkfb = xkf * 1.4743e-2 * f15 * f15 * f15;
|
||||
absoh[ij] = abso[ij] - xkf * emis[ij];
|
||||
emish[ij] = xkfb * emis[ij];
|
||||
}
|
||||
|
||||
He2linResult { absoh, emish }
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Shared helper functions
|
||||
// ============================================================================
|
||||
|
||||
/// Prepare common derived quantities from input parameters.
|
||||
/// Returns (t1, sqt, ane, anes, pp, pj, f00, dop0).
|
||||
fn prepare(c: &He2Common) -> (f64, f64, f64, f64, f64, [f64; 60], f64, f64) {
|
||||
let t1 = UN / c.t;
|
||||
let sqt = c.t.sqrt();
|
||||
let ane = c.ane;
|
||||
let anes = ane.powf(SIXTH);
|
||||
|
||||
let (anp, nlhe2) = if c.ielhe2 > 0 {
|
||||
(c.anp_he3, c.nlast_he2 - c.nfirst_he2 + 1)
|
||||
} else {
|
||||
(c.rrr_he3, 0)
|
||||
};
|
||||
|
||||
let nf = c.freq.len();
|
||||
let mut pj = [0.0f64; 60];
|
||||
let pp = CPP * ane * anp * t1 / sqt;
|
||||
for il in 1..=60 {
|
||||
let x = (il * il) as f64;
|
||||
if il <= nlhe2 {
|
||||
if let Some(pj_in) = c.pj
|
||||
&& il - 1 < pj_in.len() {
|
||||
pj[il - 1] = pj_in[il - 1];
|
||||
}
|
||||
} else {
|
||||
let wn = wn_val(c.wnhe2, il, c.id, nf);
|
||||
pj[il - 1] = pp * (CPJ / x * t1).exp() * x * wn;
|
||||
}
|
||||
}
|
||||
|
||||
let f00 = 3.906e-11 * anes * anes * anes * anes;
|
||||
let dop0 = 1.0e8 * (4.12e7 * c.t + c.vturb).sqrt();
|
||||
|
||||
(t1, sqt, ane, anes, pp, pj, f00, dop0)
|
||||
}
|
||||
|
||||
/// Determine the lower series index.
|
||||
fn series_lower(ilw: usize) -> usize {
|
||||
if ilw <= 3 {
|
||||
ilw
|
||||
} else if ilw <= 5 {
|
||||
ilw - 1
|
||||
} else if ilw <= 7 {
|
||||
ilw - 2
|
||||
} else if ilw <= 9 {
|
||||
ilw - 3
|
||||
} else {
|
||||
ilw - 4
|
||||
}
|
||||
}
|
||||
|
||||
/// Determine contributing line range (m1, m2) for a given series.
|
||||
fn determine_lines(
|
||||
i: usize, ilwhe2: usize, mhe10: usize, mhe20: usize,
|
||||
grav: f64, freq: &[f64],
|
||||
) -> (usize, usize) {
|
||||
let mut m1 = mhe10;
|
||||
if i < ilwhe2 && FRHE[i - 1] > freq[1] {
|
||||
m1 = ((FRHE[i - 1] * (i * i) as f64 / (FRHE[i - 1] - freq[1])).sqrt()) as usize;
|
||||
}
|
||||
let mut m2 = m1 + 1;
|
||||
if m1 < i + 1 {
|
||||
m1 = i + 1;
|
||||
}
|
||||
if grav < 6.0 && m1 <= 6 && i == 2 {
|
||||
// keep
|
||||
} else if grav < 6.0 && m1 <= 4 && i == 1 {
|
||||
// keep
|
||||
} else {
|
||||
m1 = m1.saturating_sub(1);
|
||||
m2 = mhe20 + 3;
|
||||
if m2 > 60 {
|
||||
m2 = 60;
|
||||
}
|
||||
}
|
||||
if grav > 6.0 {
|
||||
m2 += 5;
|
||||
m1 = m1.saturating_sub(3);
|
||||
if m1 > i + 6 {
|
||||
m1 = m1.saturating_sub(3);
|
||||
}
|
||||
}
|
||||
if m1 < i + 1 {
|
||||
m1 = i + 1;
|
||||
}
|
||||
if m2 > 60 {
|
||||
m2 = 60;
|
||||
}
|
||||
(m1, m2)
|
||||
}
|
||||
|
||||
/// Compute transition properties for line i→j.
|
||||
fn transition(i: usize, j: usize, pj: &[f64; 60], c: &He2Common, nf: usize) -> (f64, f64, f64) {
|
||||
let ii = (i * i) as f64;
|
||||
let jj = (j * j) as f64;
|
||||
let xii = UN / ii;
|
||||
let xjj = UN / jj;
|
||||
let t1 = UN / c.t;
|
||||
|
||||
let abtra = pj[i - 1] * wn_val(c.wnhe2, j, c.id, nf);
|
||||
let emtra = pj[j - 1] * wn_val(c.wnhe2, i, c.id, nf) * ii * xjj * (CPJ * (xii - xjj) * t1).exp();
|
||||
|
||||
let wlin = if i <= 2 {
|
||||
WLIN_FACTOR_LOW / (xii - 1.0 / jj)
|
||||
} else {
|
||||
WLIN_FACTOR_HIGH / (xii - 1.0 / jj)
|
||||
};
|
||||
|
||||
(abtra, emtra, wlin)
|
||||
}
|
||||
|
||||
/// Accumulate opacity using tabulated profile (range of frequencies).
|
||||
fn accumulate_tabulated(
|
||||
c: &He2Common, iline: usize, wlin: f64, abtra: f64, emtra: f64,
|
||||
i0: usize, i1: usize, nf: usize,
|
||||
abso: &mut [f64], emis: &mut [f64],
|
||||
) {
|
||||
let nwl = c.nwlhe2[iline - 1];
|
||||
let fid = CID * OSCHE2[iline - 1];
|
||||
|
||||
for ij in i0..=i1.min(nf - 1) {
|
||||
let al_raw = (c.wlam[ij] - wlin).abs();
|
||||
let al = if al_raw < 1.0e-4 { 1.0e-4 } else { al_raw };
|
||||
let al = al.log10();
|
||||
|
||||
let mut iw0 = 0usize;
|
||||
for iwl in 0..nwl - 1 {
|
||||
let wl_next = profile_wl_val(c.wlhe2, iline, iwl + 1);
|
||||
if al <= wl_next {
|
||||
iw0 = iwl;
|
||||
break;
|
||||
}
|
||||
iw0 = iwl;
|
||||
}
|
||||
let iw1 = iw0 + 1;
|
||||
let wl0 = profile_wl_val(c.wlhe2, iline, iw0);
|
||||
let wl1 = profile_wl_val(c.wlhe2, iline, iw1);
|
||||
let prf0 = profile_prf_val(c.prfhe2, iline, iw0);
|
||||
let prf1 = profile_prf_val(c.prfhe2, iline, iw1);
|
||||
|
||||
let denom = wl1 - wl0;
|
||||
let prff = if denom.abs() > 1.0e-30 {
|
||||
(prf0 * (wl1 - al) + prf1 * (al - wl0)) / denom
|
||||
} else {
|
||||
prf0
|
||||
};
|
||||
let sg = (prff * AL10).exp() * fid;
|
||||
abso[ij] += sg * abtra;
|
||||
emis[ij] += sg * emtra;
|
||||
}
|
||||
}
|
||||
|
||||
/// Accumulate opacity using asymptotic Stark profile (range of frequencies).
|
||||
fn accumulate_stark(
|
||||
c: &He2Common, i: usize, j: usize, f00: f64, dop0: f64,
|
||||
_wlin: f64, abtra: f64, emtra: f64,
|
||||
i0: usize, i1: usize, nf: usize,
|
||||
abso: &mut [f64], emis: &mut [f64],
|
||||
) {
|
||||
let stark = stark0(i as i32, j as i32, 2);
|
||||
let fxk = f00 * stark.xkij;
|
||||
let fxk1 = UN / fxk;
|
||||
let dop = dop0 / stark.wl0;
|
||||
let dbeta = stark.wl0 * stark.wl0 * CINV * fxk1;
|
||||
let betad = dop * dbeta;
|
||||
let fid = CID * stark.fij * dbeta;
|
||||
let ad = divhe2(betad);
|
||||
|
||||
for ij in i0..=i1.min(nf - 1) {
|
||||
let beta = (c.wlam[ij] - stark.wl0).abs() * fxk1;
|
||||
let sg = starka(beta, betad, ad, UN, UN) * fid;
|
||||
abso[ij] += sg * abtra;
|
||||
emis[ij] += sg * emtra;
|
||||
}
|
||||
}
|
||||
|
||||
/// Finalize: compute total absorption and emission from raw abso/emis.
|
||||
fn finalize(
|
||||
c: &He2Common, i0: usize, i1: usize, nf: usize,
|
||||
abso: &[f64], emis: &[f64],
|
||||
absoh: &mut [f64], emish: &mut [f64],
|
||||
) {
|
||||
let t1 = UN / c.t;
|
||||
for ij in i0..=i1.min(nf - 1) {
|
||||
let f = c.freq[ij];
|
||||
let f15 = f * 1.0e-15;
|
||||
let xkf = (-4.79928e-11 * f * t1).exp();
|
||||
let xkfb = xkf * 1.4743e-2 * f15 * f15 * f15;
|
||||
absoh[ij] = abso[ij] - xkf * emis[ij];
|
||||
emish[ij] = xkfb * emis[ij];
|
||||
}
|
||||
}
|
||||
|
||||
/// Determine the profile table line index for a given He II transition.
|
||||
///
|
||||
/// Returns 0 if no tabulated profile is available (use asymptotic Stark).
|
||||
fn profile_line_index(i: usize, j: usize, ihe2pr: i32) -> usize {
|
||||
if ihe2pr <= 0 {
|
||||
return 0;
|
||||
}
|
||||
match i {
|
||||
2 => {
|
||||
if j == 3 { 1 } else { 0 }
|
||||
}
|
||||
3 => {
|
||||
if j == 4 { 8 } else if j > 5 && j <= 10 { j - 3 } else { 0 }
|
||||
}
|
||||
4 => {
|
||||
if j <= 7 { j + 12 } else if (8..=15).contains(&j) { j + 1 } else { 0 }
|
||||
}
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Access WNHE2 partition function value.
|
||||
fn wn_val(wnhe2: &[f64], level: usize, id: usize, nf: usize) -> f64 {
|
||||
if (1..=60).contains(&level) {
|
||||
let idx = (level - 1) * nf + id;
|
||||
if idx < wnhe2.len() {
|
||||
return wnhe2[idx];
|
||||
}
|
||||
}
|
||||
1.0
|
||||
}
|
||||
|
||||
/// Access PRFHE2 profile table value (log10 profile).
|
||||
fn profile_prf_val(prfhe2: &[f64], iline: usize, iwl: usize) -> f64 {
|
||||
let idx = (iline - 1) * 36 + iwl;
|
||||
if idx < prfhe2.len() { prfhe2[idx] } else { 0.0 }
|
||||
}
|
||||
|
||||
/// Access WLHE2 profile wavelength table value (log10 wavelength).
|
||||
fn profile_wl_val(wlhe2: &[f64], iline: usize, iwl: usize) -> f64 {
|
||||
let idx = (iline - 1) * 36 + iwl;
|
||||
if idx < wlhe2.len() { wlhe2[idx] } else { 0.0 }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn make_common(nf: usize) -> (He2Common<'static>, Vec<f64>, Vec<f64>, Vec<f64>) {
|
||||
let freq: Vec<f64> = (0..nf).map(|i| 3.0e15 - i as f64 * 1.0e14).collect();
|
||||
let wlam: Vec<f64> = freq.iter().map(|&f| 2.997925e17 / f).collect();
|
||||
let wnhe2 = vec![1.0; 60 * nf];
|
||||
// Leak wnhe2 to get 'static — acceptable for tests
|
||||
let wnhe2: &'static [f64] = Box::leak(wnhe2.into_boxed_slice());
|
||||
let common = He2Common {
|
||||
id: 0,
|
||||
t: 20000.0,
|
||||
ane: 1.0e14,
|
||||
vturb: 2.0e5,
|
||||
grav: 4.0,
|
||||
freq: &[],
|
||||
wlam: &[],
|
||||
ielhe2: 1,
|
||||
ihe2pr: 0,
|
||||
nfirst_he2: 1,
|
||||
nlast_he2: 10,
|
||||
nnext_he2: 2,
|
||||
anp_he3: 1.0e10,
|
||||
rrr_he3: 1.0e10,
|
||||
pj: None,
|
||||
wnhe2,
|
||||
nwlhe2: &[0; 19],
|
||||
prfhe2: &[],
|
||||
wlhe2: &[],
|
||||
};
|
||||
(common, freq, wlam, wnhe2.to_vec())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_he2lin_basic() {
|
||||
let nf = 10;
|
||||
let (mut common, freq, wlam, _wn) = make_common(nf);
|
||||
common.freq = &freq;
|
||||
common.wlam = &wlam;
|
||||
|
||||
let params = He2linParams {
|
||||
common,
|
||||
i0: 0,
|
||||
i1: nf - 1,
|
||||
ilwhe2: 3,
|
||||
mhe10: 10,
|
||||
mhe20: 20,
|
||||
};
|
||||
|
||||
let result = he2lin(¶ms);
|
||||
assert_eq!(result.absoh.len(), nf);
|
||||
assert_eq!(result.emish.len(), nf);
|
||||
assert!(result.absoh.iter().all(|&x| x.is_finite()));
|
||||
assert!(result.emish.iter().all(|&x| x.is_finite()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_he2lin_no_he2() {
|
||||
let nf = 5;
|
||||
let (mut common, freq, wlam, _wn) = make_common(nf);
|
||||
common.freq = &freq;
|
||||
common.wlam = &wlam;
|
||||
common.ielhe2 = 0;
|
||||
common.t = 10000.0;
|
||||
common.ane = 1.0e12;
|
||||
common.vturb = 1.0e5;
|
||||
|
||||
let params = He2linParams {
|
||||
common,
|
||||
i0: 0,
|
||||
i1: nf - 1,
|
||||
ilwhe2: 1,
|
||||
mhe10: 5,
|
||||
mhe20: 10,
|
||||
};
|
||||
|
||||
let result = he2lin(¶ms);
|
||||
assert!(result.absoh.iter().all(|&x| x.is_finite()));
|
||||
assert!(result.emish.iter().all(|&x| x.is_finite()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_he2liw_skip() {
|
||||
let nf = 5;
|
||||
let (mut common, freq, wlam, _wn) = make_common(nf);
|
||||
common.freq = &freq;
|
||||
common.wlam = &wlam;
|
||||
|
||||
let params = He2liwParams {
|
||||
common,
|
||||
window: He2liwWindowParams {
|
||||
ihe2lw: &[-1; 5],
|
||||
ilwhew: &[3; 5],
|
||||
mhe10w: &[10; 5],
|
||||
mhe20w: &[20; 5],
|
||||
},
|
||||
ifhe2: 0,
|
||||
};
|
||||
|
||||
let result = he2liw(¶ms);
|
||||
assert!(result.absoh.iter().all(|&x| x == 0.0));
|
||||
assert!(result.emish.iter().all(|&x| x == 0.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_he2liw_basic() {
|
||||
let nf = 5;
|
||||
let (mut common, freq, wlam, _wn) = make_common(nf);
|
||||
common.freq = &freq;
|
||||
common.wlam = &wlam;
|
||||
|
||||
let params = He2liwParams {
|
||||
common,
|
||||
window: He2liwWindowParams {
|
||||
ihe2lw: &[1; 5],
|
||||
ilwhew: &[3; 5],
|
||||
mhe10w: &[10; 5],
|
||||
mhe20w: &[20; 5],
|
||||
},
|
||||
ifhe2: 1,
|
||||
};
|
||||
|
||||
let result = he2liw(¶ms);
|
||||
assert!(result.absoh.iter().all(|&x| x.is_finite()));
|
||||
assert!(result.emish.iter().all(|&x| x.is_finite()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_profile_line_index_no_profile() {
|
||||
assert_eq!(profile_line_index(2, 3, 0), 0);
|
||||
assert_eq!(profile_line_index(3, 4, -1), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_profile_line_index_with_profile() {
|
||||
assert_eq!(profile_line_index(2, 3, 1), 1);
|
||||
assert_eq!(profile_line_index(3, 4, 1), 8);
|
||||
assert_eq!(profile_line_index(3, 7, 1), 4);
|
||||
assert_eq!(profile_line_index(4, 5, 1), 17);
|
||||
assert_eq!(profile_line_index(4, 10, 1), 11);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_lower() {
|
||||
assert_eq!(series_lower(1), 1);
|
||||
assert_eq!(series_lower(3), 3);
|
||||
assert_eq!(series_lower(4), 3);
|
||||
assert_eq!(series_lower(5), 4);
|
||||
assert_eq!(series_lower(6), 4);
|
||||
assert_eq!(series_lower(7), 5);
|
||||
assert_eq!(series_lower(10), 6);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,341 @@
|
||||
//! He II 线不透明度初始化过程。
|
||||
//!
|
||||
//! 重构自 SYNSPEC `he2set.f` (synspec54.f:6061)。
|
||||
//!
|
||||
//! 设置 He II 线在频率窗口中的处理参数。
|
||||
|
||||
// ============================================================================
|
||||
// 物理常数
|
||||
// ============================================================================
|
||||
|
||||
/// 光速 (Å/s),用于波长转换
|
||||
const CLIGHT_A: f64 = 2.997925e17;
|
||||
|
||||
/// He II 电离阈值频率 (Hz)
|
||||
///
|
||||
/// 对应 Fortran DATA FRHE 数组,是 He II Lyman 系列各线的阈值频率。
|
||||
/// FRHE(n) = R_inf * c / n²,其中 n = 1..12
|
||||
const FRHE: [f64; 12] = [
|
||||
1.3158153e16, 3.2895381e15, 1.4624854e15,
|
||||
8.2261878e14, 5.2647201e14, 3.6560459e14,
|
||||
2.6860713e14, 2.0565220e14, 1.6249055e14,
|
||||
1.3161730e14, 1.0877460e14, 9.1400851e13,
|
||||
];
|
||||
|
||||
/// He II 最高频率阈值 (Hz) - 对应 n=1 电离频率
|
||||
const HE2_FREQ_LIMIT: f64 = 1.315812e16;
|
||||
|
||||
// ============================================================================
|
||||
// 参数结构体
|
||||
// ============================================================================
|
||||
|
||||
/// HE2SET 输入参数。
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct He2setParams {
|
||||
/// He II 处理标志 (<= 0 表示不处理 He II 线)
|
||||
pub ifhe2: i32,
|
||||
|
||||
/// 频率范围下限 (Hz) - FREQ(1)
|
||||
pub freq1: f64,
|
||||
|
||||
/// 频率范围上限 (Hz) - FREQ(2)
|
||||
pub freq2: f64,
|
||||
|
||||
/// 表面重力 log g (cgs)
|
||||
pub grav: f64,
|
||||
}
|
||||
|
||||
/// HE2SET 输出结果。
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct He2setOutput {
|
||||
/// He II 线处理标志
|
||||
/// - -1: He II 线被排除
|
||||
/// - 1: He II 线被包含
|
||||
pub ihe2l: i32,
|
||||
|
||||
/// He II 线系列索引 (1-12)
|
||||
pub ilwhe2: i32,
|
||||
|
||||
/// 主量子数上限 1 (用于线强度计算)
|
||||
pub mhe10: i32,
|
||||
|
||||
/// 主量子数上限 2 (用于线强度计算)
|
||||
pub mhe20: i32,
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// HE2SET 函数
|
||||
// ============================================================================
|
||||
|
||||
/// 初始化 He II 线不透明度参数。
|
||||
///
|
||||
/// 根据频率范围和重力确定 He II 线是否被包含在计算中,
|
||||
/// 并设置相应的处理参数。
|
||||
///
|
||||
/// # 参数
|
||||
///
|
||||
/// * `params` - 输入参数结构体
|
||||
///
|
||||
/// # 返回
|
||||
///
|
||||
/// 包含 `ihe2l`, `ilwhe2`, `mhe10`, `mhe20` 的输出结构体
|
||||
///
|
||||
/// # Fortran 源码
|
||||
///
|
||||
/// ```fortran
|
||||
/// SUBROUTINE HE2SET
|
||||
/// ```
|
||||
pub fn he2set(params: &He2setParams) -> He2setOutput {
|
||||
// 默认值:He II 线被排除
|
||||
let mut result = He2setOutput {
|
||||
ihe2l: -1,
|
||||
ilwhe2: 0,
|
||||
mhe10: 60,
|
||||
mhe20: 60,
|
||||
};
|
||||
|
||||
// 如果 He II 处理标志 <= 0,直接返回
|
||||
if params.ifhe2 <= 0 {
|
||||
return result;
|
||||
}
|
||||
|
||||
// 如果频率上限 >= He II 最高阈值,直接返回
|
||||
if params.freq2 >= HE2_FREQ_LIMIT {
|
||||
return result;
|
||||
}
|
||||
|
||||
// 计算波长范围 (Å)
|
||||
let al0 = CLIGHT_A / params.freq1;
|
||||
let al1 = CLIGHT_A / params.freq2;
|
||||
|
||||
// 根据重力检查排除区域
|
||||
if params.grav < 6.0 {
|
||||
// 低重力情况
|
||||
if al0 > 31.0 && al1 < 91.1 { return result; }
|
||||
if al0 > 26.1 && al1 < 29.8 { return result; }
|
||||
if al0 > 24.8 && al1 < 25.1 { return result; }
|
||||
if al0 > 122.1 && al1 < 162.9 { return result; }
|
||||
if al0 > 165.1 && al1 < 204.9 { return result; }
|
||||
if al0 > 109.0 && al1 < 120.9 { return result; }
|
||||
if al0 > 103.0 && al1 < 107.9 { return result; }
|
||||
if al0 > 99.7 && al1 < 102.0 { return result; }
|
||||
if al0 > 320.8 && al1 < 364.4 { return result; }
|
||||
if al0 > 273.8 && al1 < 319.8 { return result; }
|
||||
if al0 > 251.6 && al1 < 272.8 { return result; }
|
||||
if al0 > 239.0 && al1 < 250.6 { return result; }
|
||||
if al0 > 231.1 && al1 < 238.0 { return result; }
|
||||
if al0 > 225.8 && al1 < 230.1 { return result; }
|
||||
} else if params.grav < 7.0 {
|
||||
// 中等重力情况
|
||||
if al0 > 33.0 && al1 < 91.1 { return result; }
|
||||
if al0 > 124.1 && al1 < 160.9 { return result; }
|
||||
if al0 > 167.1 && al1 < 202.9 { return result; }
|
||||
if al0 > 111.0 && al1 < 118.9 { return result; }
|
||||
if al0 > 322.8 && al1 < 364.4 { return result; }
|
||||
if al0 > 275.8 && al1 < 317.8 { return result; }
|
||||
if al0 > 253.6 && al1 < 270.8 { return result; }
|
||||
if al0 > 241.0 && al1 < 248.6 { return result; }
|
||||
if al0 > 233.1 && al1 < 236.0 { return result; }
|
||||
} else {
|
||||
// 高重力情况
|
||||
if al0 > 39.0 && al1 < 91.1 { return result; }
|
||||
if al0 > 134.1 && al1 < 150.9 { return result; }
|
||||
if al0 > 177.1 && al1 < 202.9 { return result; }
|
||||
}
|
||||
|
||||
// He II 线被包含
|
||||
result.ihe2l = 1;
|
||||
result.mhe10 = 60;
|
||||
result.mhe20 = 60;
|
||||
|
||||
// 根据波长范围确定系列索引
|
||||
result.ilwhe2 = if al1 < 91.0 {
|
||||
1
|
||||
} else if al0 < 204.0 {
|
||||
2
|
||||
} else if al0 < 364.0 {
|
||||
3
|
||||
} else if al0 < 569.0 {
|
||||
4
|
||||
} else if al0 < 819.0 {
|
||||
5
|
||||
} else if al0 < 1116.0 {
|
||||
6
|
||||
} else if al0 < 1457.0 {
|
||||
7
|
||||
} else if al0 < 1844.0 {
|
||||
8
|
||||
} else if al0 < 2277.0 {
|
||||
9
|
||||
} else if al0 < 2756.0 {
|
||||
10
|
||||
} else if al0 < 3279.0 {
|
||||
11
|
||||
} else {
|
||||
12
|
||||
};
|
||||
|
||||
// 计算量子数上限
|
||||
let frion = FRHE[(result.ilwhe2 - 1) as usize];
|
||||
let fr1 = frion * (result.ilwhe2 as f64) * (result.ilwhe2 as f64);
|
||||
|
||||
if frion > params.freq2 {
|
||||
result.mhe10 = (fr1 / (frion - params.freq2)).sqrt() as i32;
|
||||
}
|
||||
if frion > params.freq1 {
|
||||
result.mhe20 = (fr1 / (frion - params.freq1)).sqrt() as i32;
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 测试
|
||||
// ============================================================================
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use approx::assert_relative_eq;
|
||||
|
||||
/// 创建默认测试参数
|
||||
fn create_test_params() -> He2setParams {
|
||||
He2setParams {
|
||||
ifhe2: 1,
|
||||
freq1: 4.0e14, // 750 nm
|
||||
freq2: 8.0e14, // 375 nm
|
||||
grav: 4.0,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_he2set_disabled() {
|
||||
// IFHE2 <= 0 时应返回排除状态
|
||||
let params = He2setParams {
|
||||
ifhe2: 0,
|
||||
..create_test_params()
|
||||
};
|
||||
let result = he2set(¶ms);
|
||||
assert_eq!(result.ihe2l, -1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_he2set_freq_too_high() {
|
||||
// 频率上限 >= He II 最高阈值时应返回排除状态
|
||||
let params = He2setParams {
|
||||
freq2: 1.4e16, // > 1.315812e16
|
||||
..create_test_params()
|
||||
};
|
||||
let result = he2set(¶ms);
|
||||
assert_eq!(result.ihe2l, -1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_he2set_low_gravity_exclusion() {
|
||||
// 低重力情况下的排除区域测试
|
||||
// AL0 = 130, AL1 = 150 → 122.1 < AL0 且 AL1 < 162.9 → 排除
|
||||
let params = He2setParams {
|
||||
ifhe2: 1,
|
||||
freq1: CLIGHT_A / 130.0, // AL0 = 130 Å
|
||||
freq2: CLIGHT_A / 150.0, // AL1 = 150 Å
|
||||
grav: 5.0,
|
||||
};
|
||||
let result = he2set(¶ms);
|
||||
assert_eq!(result.ihe2l, -1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_he2set_included_lyman() {
|
||||
// 测试 Lyman 系列被包含的情况
|
||||
// AL0 = 200, AL1 = 300 → 不在任何排除区域
|
||||
let params = He2setParams {
|
||||
ifhe2: 1,
|
||||
freq1: CLIGHT_A / 200.0, // AL0 = 200 Å
|
||||
freq2: CLIGHT_A / 300.0, // AL1 = 300 Å
|
||||
grav: 4.0,
|
||||
};
|
||||
let result = he2set(¶ms);
|
||||
assert_eq!(result.ihe2l, 1);
|
||||
assert_eq!(result.ilwhe2, 2); // 91 < AL0 < 204
|
||||
assert!(result.mhe10 > 0);
|
||||
assert!(result.mhe20 > 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_he2set_included_balmer() {
|
||||
// 测试 Balmer 系列被包含的情况
|
||||
let params = He2setParams {
|
||||
ifhe2: 1,
|
||||
freq1: CLIGHT_A / 500.0, // AL0 = 500 Å
|
||||
freq2: CLIGHT_A / 400.0, // AL1 = 400 Å
|
||||
grav: 4.0,
|
||||
};
|
||||
let result = he2set(¶ms);
|
||||
assert_eq!(result.ihe2l, 1);
|
||||
assert_eq!(result.ilwhe2, 4); // 364 < AL0 < 569
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_he2set_high_gravity() {
|
||||
// 高重力情况下的测试
|
||||
let params = He2setParams {
|
||||
ifhe2: 1,
|
||||
freq1: CLIGHT_A / 100.0, // AL0 = 100 Å
|
||||
freq2: CLIGHT_A / 150.0, // AL1 = 150 Å
|
||||
grav: 8.0,
|
||||
};
|
||||
let result = he2set(¶ms);
|
||||
// 高重力下排除区域更少
|
||||
assert_eq!(result.ihe2l, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_he2set_series_index() {
|
||||
// 测试系列索引的边界情况
|
||||
let params = He2setParams {
|
||||
ifhe2: 1,
|
||||
freq1: CLIGHT_A / 3000.0, // AL0 = 3000 Å
|
||||
freq2: CLIGHT_A / 2800.0, // AL1 = 2800 Å
|
||||
grav: 4.0,
|
||||
};
|
||||
let result = he2set(¶ms);
|
||||
assert_eq!(result.ilwhe2, 11); // 2756 < AL0 < 3279
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_frhe_constants() {
|
||||
// 验证 FRHE 常数与 Fortran 一致
|
||||
assert_relative_eq!(FRHE[0], 1.3158153e16, epsilon = 1e10);
|
||||
assert_relative_eq!(FRHE[1], 3.2895381e15, epsilon = 1e9);
|
||||
assert_relative_eq!(FRHE[11], 9.1400851e13, epsilon = 1e7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_he2set_medium_gravity() {
|
||||
// 中等重力情况下的测试
|
||||
let params = He2setParams {
|
||||
ifhe2: 1,
|
||||
freq1: CLIGHT_A / 130.0, // AL0 = 130 Å
|
||||
freq2: CLIGHT_A / 160.0, // AL1 = 160 Å
|
||||
grav: 6.5,
|
||||
};
|
||||
let result = he2set(¶ms);
|
||||
// 中等重力下的排除区域: 124.1 < AL0 < 160.9
|
||||
assert_eq!(result.ihe2l, -1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_he2set_quantum_numbers() {
|
||||
// 测试量子数上限的计算
|
||||
let params = He2setParams {
|
||||
ifhe2: 1,
|
||||
freq1: CLIGHT_A / 100.0, // AL0 = 100 Å
|
||||
freq2: CLIGHT_A / 200.0, // AL1 = 200 Å
|
||||
grav: 4.0,
|
||||
};
|
||||
let result = he2set(¶ms);
|
||||
assert_eq!(result.ilwhe2, 2); // 91 < AL0 < 204
|
||||
assert!(result.mhe10 > 0);
|
||||
assert!(result.mhe20 > 0);
|
||||
}
|
||||
}
|
||||
@@ -104,7 +104,7 @@ pub fn he2sew(freq: f64, grav: f64, ifhe2: i32) -> He2WindowParams {
|
||||
|
||||
if frion > freq {
|
||||
let ratio = fr1 / (frion - freq);
|
||||
result.mhe10w = (ratio.sqrt() as i32);
|
||||
result.mhe10w = ratio.sqrt() as i32;
|
||||
}
|
||||
|
||||
result
|
||||
|
||||
@@ -0,0 +1,260 @@
|
||||
//! He I photoionization cross sections using Seaton-Fernley's cubic fits
|
||||
//! to the Opacity Project cross sections.
|
||||
//!
|
||||
//! Translated from SYNSPEC54.FOR subroutine HEPHOT(S,L,N,FREQ)
|
||||
|
||||
/// He I photoionization cross section using Opacity Project fits.
|
||||
///
|
||||
/// Evaluates He I photoionization cross section using Seaton-Fernley's cubic
|
||||
/// fits to the Opacity Project cross sections up to some energy "EFITM" in the
|
||||
/// resonance-free zone. Beyond this energy, linear fits to log sigma in
|
||||
/// log(E/E0) are used.
|
||||
///
|
||||
/// For L > 2, hydrogenic expression is used.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `s` - Multiplicity, either 1 (singlet) or 3 (triplet)
|
||||
/// * `l` - Angular momentum quantum number (0, 1, or 2; for L > 2 uses hydrogenic)
|
||||
/// * `n` - Principal quantum number
|
||||
/// * `freq` - Frequency in Hz
|
||||
///
|
||||
/// # Returns
|
||||
/// Photoionization cross section in cm^2
|
||||
pub fn hephot(s: i32, l: i32, n: i32, freq: f64) -> f64 {
|
||||
// Hydrogenic expression for L > 2
|
||||
if l > 2 {
|
||||
let gn = 2.0 * (n as f64) * (n as f64);
|
||||
return 2.815e29 / freq / freq / freq
|
||||
/ (n.pow(5) as f64)
|
||||
* ((2 * l + 1) as f64)
|
||||
* (s as f64)
|
||||
/ gn;
|
||||
}
|
||||
|
||||
// Select beginning and end of coefficients
|
||||
let ss = ((s + 1) / 2) as usize; // 1-based index for singlet/triplet
|
||||
let ll = (l + 1) as usize; // 1-based index for l
|
||||
|
||||
// Get the starting index and quantum number offset
|
||||
let ist_idx = (IST[(ll - 1) * 2 + (ss - 1)] - 1) as usize; // convert to 0-based
|
||||
let nsl0 = N0[(ll - 1) * 2 + (ss - 1)];
|
||||
|
||||
let i = ist_idx + (n - nsl0) as usize; // 0-based index into coefficient arrays
|
||||
|
||||
// Evaluate cross section
|
||||
let fl = (freq / 3.28805e15).log10();
|
||||
let x = fl - FL0[i];
|
||||
|
||||
if x >= -0.001 {
|
||||
if x < XFITM[i] {
|
||||
// Cubic polynomial fit
|
||||
let mut p = COEF[i * 4 + 3]; // COEF(4,I) in Fortran (1-indexed)
|
||||
for k in (0..3).rev() {
|
||||
p = x * p + COEF[i * 4 + k]; // COEF(4-K,I)
|
||||
}
|
||||
1.0e-18 * 10.0_f64.powf(p)
|
||||
} else {
|
||||
// Linear extrapolation in log space
|
||||
1.0e-18 * 10.0_f64.powf(A[i] + B[i] * x)
|
||||
}
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Opacity Project fit data
|
||||
// ============================================================================
|
||||
|
||||
/// Starting indices for each (l, s) combination (1-based in Fortran, converted to 0-based usage)
|
||||
/// IST(LL, SS) where LL = l+1, SS = (s+1)/2
|
||||
/// Layout: [singlet_l0, triplet_l0, singlet_l1, triplet_l1, singlet_l2, triplet_l2]
|
||||
const IST: [i32; 6] = [1, 36, 20, 11, 45, 28];
|
||||
|
||||
/// Starting principal quantum number for each (l, s) combination
|
||||
/// N0(LL, SS)
|
||||
const N0: [i32; 6] = [1, 2, 3, 2, 2, 3];
|
||||
|
||||
/// log10(nu/nu0) offset values for 53 cross section fits
|
||||
const FL0: [f64; 53] = [
|
||||
2.521e-01, -5.381e-01, -9.139e-01, -1.175e+00, -1.375e+00, -1.537e+00,
|
||||
-1.674e+00, -1.792e+00, -1.896e+00, -1.989e+00, -4.555e-01, -8.622e-01,
|
||||
-1.137e+00, -1.345e+00, -1.512e+00, -1.653e+00, -1.774e+00, -1.880e+00,
|
||||
-1.974e+00, -9.538e-01, -1.204e+00, -1.398e+00, -1.556e+00, -1.690e+00,
|
||||
-1.806e+00, -1.909e+00, -2.000e+00, -9.537e-01, -1.204e+00, -1.398e+00,
|
||||
-1.556e+00, -1.690e+00, -1.806e+00, -1.909e+00, -2.000e+00, -6.065e-01,
|
||||
-9.578e-01, -1.207e+00, -1.400e+00, -1.558e+00, -1.692e+00, -1.808e+00,
|
||||
-1.910e+00, -2.002e+00, -5.749e-01, -9.352e-01, -1.190e+00, -1.386e+00,
|
||||
-1.547e+00, -1.682e+00, -1.799e+00, -1.902e+00, -1.995e+00,
|
||||
];
|
||||
|
||||
/// Upper limit of cubic fit region (in log10 space)
|
||||
const XFITM: [f64; 53] = [
|
||||
3.262e-01, 6.135e-01, 9.233e-01, 8.438e-01, 1.020e+00, 1.169e+00,
|
||||
1.298e+00, 1.411e+00, 1.512e+00, 1.602e+00, 7.228e-01, 1.076e+00,
|
||||
1.206e+00, 1.404e+00, 1.481e+00, 1.464e+00, 1.581e+00, 1.685e+00,
|
||||
1.777e+00, 9.586e-01, 1.187e+00, 1.371e+00, 1.524e+00, 1.740e+00,
|
||||
1.854e+00, 1.955e+00, 2.046e+00, 9.585e-01, 1.041e+00, 1.371e+00,
|
||||
1.608e+00, 1.739e+00, 1.768e+00, 1.869e+00, 1.803e+00, 7.360e-01,
|
||||
1.041e+00, 1.272e+00, 1.457e+00, 1.611e+00, 1.741e+00, 1.855e+00,
|
||||
1.870e+00, 1.804e+00, 9.302e-01, 1.144e+00, 1.028e+00, 1.210e+00,
|
||||
1.362e+00, 1.646e+00, 1.761e+00, 1.863e+00, 1.954e+00,
|
||||
];
|
||||
|
||||
/// Linear fit coefficients A (53 values)
|
||||
const A: [f64; 53] = [
|
||||
6.95319e-01, 1.13101e+00, 1.36313e+00, 1.51684e+00, 1.64767e+00,
|
||||
1.75643e+00, 1.84458e+00, 1.87243e+00, 1.85628e+00, 1.90889e+00,
|
||||
9.01802e-01, 1.25389e+00, 1.39033e+00, 1.55226e+00, 1.60658e+00,
|
||||
1.65930e+00, 1.68855e+00, 1.62477e+00, 1.66726e+00, 1.83599e+00,
|
||||
2.50403e+00, 3.08564e+00, 3.56545e+00, 4.25922e+00, 4.61346e+00,
|
||||
4.91417e+00, 5.19211e+00, 1.74181e+00, 2.25756e+00, 2.95625e+00,
|
||||
3.65899e+00, 4.04397e+00, 4.13410e+00, 4.43538e+00, 4.19583e+00,
|
||||
1.79027e+00, 2.23543e+00, 2.63942e+00, 3.02461e+00, 3.35018e+00,
|
||||
3.62067e+00, 3.85218e+00, 3.76689e+00, 3.49318e+00, 1.16294e+00,
|
||||
1.86467e+00, 2.02110e+00, 2.24231e+00, 2.44240e+00, 2.76594e+00,
|
||||
2.93230e+00, 3.08109e+00, 3.21069e+00,
|
||||
];
|
||||
|
||||
/// Linear fit coefficients B (53 values)
|
||||
const B: [f64; 53] = [
|
||||
-1.29000e+00, -2.15771e+00, -2.13263e+00, -2.10272e+00, -2.10861e+00,
|
||||
-2.11507e+00, -2.11710e+00, -2.08531e+00, -2.03296e+00, -2.03441e+00,
|
||||
-1.85905e+00, -2.04057e+00, -2.02189e+00, -2.05930e+00, -2.03403e+00,
|
||||
-2.02071e+00, -1.99956e+00, -1.92851e+00, -1.92905e+00, -4.58608e+00,
|
||||
-4.40022e+00, -4.39154e+00, -4.39676e+00, -4.57631e+00, -4.57120e+00,
|
||||
-4.56188e+00, -4.55915e+00, -4.41218e+00, -4.12940e+00, -4.24401e+00,
|
||||
-4.40783e+00, -4.39930e+00, -4.25981e+00, -4.26804e+00, -4.00419e+00,
|
||||
-4.47251e+00, -3.87960e+00, -3.71668e+00, -3.68461e+00, -3.67173e+00,
|
||||
-3.65991e+00, -3.64968e+00, -3.48666e+00, -3.23985e+00, -2.95758e+00,
|
||||
-3.07110e+00, -2.87157e+00, -2.83137e+00, -2.82132e+00, -2.91084e+00,
|
||||
-2.91159e+00, -2.91336e+00, -2.91296e+00,
|
||||
];
|
||||
|
||||
/// Cubic polynomial coefficients COEF(4, 53) stored as flat array
|
||||
/// COEF[I*4 + j] corresponds to Fortran COEF(j+1, I+1)
|
||||
const COEF: [f64; 212] = [
|
||||
// J=1..10
|
||||
8.734e-01, -1.545e+00, -1.093e+00, 5.918e-01,
|
||||
9.771e-01, -1.567e+00, -4.739e-01, -1.302e-01,
|
||||
1.174e+00, -1.638e+00, -2.831e-01, -3.281e-02,
|
||||
1.324e+00, -1.692e+00, -2.916e-01, 9.027e-02,
|
||||
1.445e+00, -1.761e+00, -1.902e-01, 4.401e-02,
|
||||
1.546e+00, -1.817e+00, -1.278e-01, 2.293e-02,
|
||||
1.635e+00, -1.864e+00, -8.252e-02, 9.854e-03,
|
||||
1.712e+00, -1.903e+00, -5.206e-02, 2.892e-03,
|
||||
1.782e+00, -1.936e+00, -2.952e-02, -1.405e-03,
|
||||
1.845e+00, -1.964e+00, -1.152e-02, -4.487e-03,
|
||||
// J=11..19
|
||||
7.377e-01, -9.327e-01, -1.466e+00, 6.891e-01,
|
||||
9.031e-01, -1.157e+00, -7.151e-01, 1.832e-01,
|
||||
1.031e+00, -1.313e+00, -4.517e-01, 9.207e-02,
|
||||
1.135e+00, -1.441e+00, -2.724e-01, 3.105e-02,
|
||||
1.225e+00, -1.536e+00, -1.725e-01, 7.191e-03,
|
||||
1.302e+00, -1.602e+00, -1.300e-01, 7.345e-03,
|
||||
1.372e+00, -1.664e+00, -8.204e-02, -1.643e-03,
|
||||
1.434e+00, -1.715e+00, -4.646e-02, -7.456e-03,
|
||||
1.491e+00, -1.760e+00, -1.838e-02, -1.152e-02,
|
||||
// J=20..27
|
||||
1.258e+00, -3.442e+00, -4.731e-01, -9.522e-02,
|
||||
1.553e+00, -2.781e+00, -6.841e-01, -4.083e-03,
|
||||
1.727e+00, -2.494e+00, -5.785e-01, -6.015e-02,
|
||||
1.853e+00, -2.347e+00, -4.611e-01, -9.615e-02,
|
||||
1.955e+00, -2.273e+00, -3.457e-01, -1.245e-01,
|
||||
2.041e+00, -2.226e+00, -2.669e-01, -1.344e-01,
|
||||
2.115e+00, -2.200e+00, -1.999e-01, -1.410e-01,
|
||||
2.182e+00, -2.188e+00, -1.405e-01, -1.460e-01,
|
||||
// J=28..35
|
||||
1.267e+00, -3.417e+00, -5.038e-01, -1.797e-02,
|
||||
1.565e+00, -2.781e+00, -6.497e-01, -5.979e-03,
|
||||
1.741e+00, -2.479e+00, -6.099e-01, -2.227e-02,
|
||||
1.870e+00, -2.336e+00, -4.899e-01, -6.616e-02,
|
||||
1.973e+00, -2.253e+00, -3.972e-01, -8.729e-02,
|
||||
2.061e+00, -2.212e+00, -3.072e-01, -1.060e-01,
|
||||
2.137e+00, -2.189e+00, -2.352e-01, -1.171e-01,
|
||||
2.205e+00, -2.186e+00, -1.621e-01, -1.296e-01,
|
||||
// J=36..44
|
||||
1.129e+00, -3.149e+00, -1.910e-01, -5.244e-01,
|
||||
1.431e+00, -2.511e+00, -3.710e-01, -1.933e-01,
|
||||
1.620e+00, -2.303e+00, -3.045e-01, -1.391e-01,
|
||||
1.763e+00, -2.235e+00, -1.829e-01, -1.491e-01,
|
||||
1.879e+00, -2.215e+00, -9.003e-02, -1.537e-01,
|
||||
1.978e+00, -2.213e+00, -2.066e-02, -1.541e-01,
|
||||
2.064e+00, -2.220e+00, 3.258e-02, -1.527e-01,
|
||||
2.140e+00, -2.225e+00, 6.311e-02, -1.455e-01,
|
||||
2.208e+00, -2.229e+00, 7.977e-02, -1.357e-01,
|
||||
// J=45..53
|
||||
1.204e+00, -2.809e+00, -3.094e-01, 1.100e-01,
|
||||
1.455e+00, -2.254e+00, -4.795e-01, 6.872e-02,
|
||||
1.619e+00, -2.109e+00, -3.357e-01, -2.532e-02,
|
||||
1.747e+00, -2.065e+00, -2.317e-01, -5.224e-02,
|
||||
1.853e+00, -2.058e+00, -1.517e-01, -6.647e-02,
|
||||
1.943e+00, -2.055e+00, -1.158e-01, -6.081e-02,
|
||||
2.023e+00, -2.070e+00, -6.470e-02, -6.800e-02,
|
||||
2.095e+00, -2.088e+00, -2.357e-02, -7.250e-02,
|
||||
2.160e+00, -2.107e+00, 1.065e-02, -7.542e-02,
|
||||
];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_hephot_below_threshold() {
|
||||
// Below threshold frequency should return 0
|
||||
let sigma = hephot(1, 0, 1, 1.0e14);
|
||||
assert_eq!(sigma, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hephot_singlet_s_ground() {
|
||||
// Singlet S state, n=1: threshold is at 3.288e15 * 10^FL0[0] ≈ 5.76e15 Hz
|
||||
// Use frequency well above threshold
|
||||
let freq = 8.0e15;
|
||||
let sigma = hephot(1, 0, 1, freq);
|
||||
assert!(sigma > 0.0, "Cross section should be positive above threshold");
|
||||
assert!(sigma < 1.0e-15, "Cross section should be in reasonable range");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hephot_triplet_p_n2() {
|
||||
// Triplet P state, n=2
|
||||
// IST(2,2)=36, N0(2,2)=2, so i = 36-1 + (2-2) = 35 (0-based)
|
||||
let freq = 1.0e15;
|
||||
let sigma = hephot(3, 1, 2, freq);
|
||||
// May be below or above threshold depending on freq
|
||||
assert!(sigma >= 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hephot_hydrogenic_l3() {
|
||||
// For L > 2, should use hydrogenic expression
|
||||
let freq = 1.0e15;
|
||||
let sigma = hephot(1, 3, 3, freq);
|
||||
assert!(sigma > 0.0, "Hydrogenic cross section should be positive");
|
||||
// Expected: 2.815e29 / freq^3 / n^5 * (2L+1) * S / (2*n^2)
|
||||
let expected = 2.815e29 / freq.powi(3) / (3_i32.pow(5) as f64) * 7.0 * 1.0 / 18.0;
|
||||
assert!((sigma - expected).abs() / expected < 1.0e-10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hephot_singlet_d_n2() {
|
||||
// Singlet D, n=2: IST(3,1)=20, N0(3,1)=3
|
||||
// i = 20-1 + (2-3) = 18 (0-based)
|
||||
// But n < N0 gives negative index - this case shouldn't be called
|
||||
// Let's test n=3 instead
|
||||
let freq = 1.5e15;
|
||||
let sigma = hephot(1, 2, 3, freq);
|
||||
assert!(sigma >= 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hephot_all_multiplicities() {
|
||||
// Test that both singlet and triplet produce valid results
|
||||
let freq = 5.0e15;
|
||||
let s1 = hephot(1, 0, 1, freq); // singlet
|
||||
let s3 = hephot(3, 0, 2, freq); // triplet
|
||||
assert!(s1 >= 0.0);
|
||||
assert!(s3 >= 0.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
//! 光致电离截面插值(Hidalgo 1968)。
|
||||
//!
|
||||
//! 重构自 SYNSPEC `hidalg.f`
|
||||
//!
|
||||
//! 使用 Hidalgo (1968, Ap. J., 153, 981) 的波长和光致电离截面数据表,
|
||||
//! 对给定频率进行线性插值。
|
||||
|
||||
/// 波长网格 1 (Å),用于 INDEX < 13 的物种
|
||||
const WL1: [f64; 20] = [
|
||||
39.1, 80.9, 97.6, 100.1, 104.3, 107.2, 108.7, 111.9, 113.6, 115.4,
|
||||
117.1, 119.0, 124.8, 126.9, 129.1, 131.3, 133.6, 136.0, 138.5, 141.1,
|
||||
];
|
||||
|
||||
/// 波长网格 2 (Å),用于 INDEX >= 13 的物种
|
||||
const WL2: [f64; 20] = [
|
||||
68.5, 80.9, 100.1, 120.9, 158.8, 165.7, 177.3, 190.6, 200.7, 206.2,
|
||||
211.9, 218.0, 224.5, 231.3, 246.3, 0.0, 0.0, 0.0, 0.0, 0.0,
|
||||
];
|
||||
|
||||
/// 光致电离截面数据 (Mbarn),20×24 矩阵(列优先存储)
|
||||
const SIG0: [[f64; 20]; 24] = [
|
||||
[0.0; 20], // col 1
|
||||
[
|
||||
0.0460, 0.2400, 0.3500, 0.3700, 0.4000, 0.4300, 0.4400, 0.4600, 0.4700, 0.4900,
|
||||
0.5000, 0.5200, 0.5700, 0.6200, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
|
||||
], // col 2
|
||||
[0.0; 20], // col 3
|
||||
[
|
||||
0.0092, 0.1000, 0.1900, 0.2100, 0.2300, 0.2500, 0.2600, 0.2900, 0.3000, 0.3200,
|
||||
0.3400, 0.3500, 0.4100, 0.4300, 0.4500, 0.4800, 0.5000, 0.5300, 0.5600, 0.5900,
|
||||
], // col 4
|
||||
[
|
||||
0.3400, 0.4600, 0.6300, 0.7700, 0.9100, 1.080, 0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
|
||||
], // col 5
|
||||
[0.0; 20], // col 6
|
||||
[
|
||||
0.0064, 0.1100, 0.2200, 0.4100, 0.9400, 1.000, 1.300, 1.600, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
|
||||
], // col 7
|
||||
[0.0; 20], // col 8
|
||||
[
|
||||
0.0370, 0.0650, 0.1300, 0.2400, 0.5500, 0.6300, 0.7700, 0.9500, 1.100, 1.250,
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
|
||||
], // col 9
|
||||
[0.0; 20], // col 10
|
||||
[
|
||||
0.0220, 0.0390, 0.0800, 0.1500, 0.3500, 0.4000, 0.4900, 0.6200, 0.7200, 0.7800,
|
||||
0.8500, 0.9300, 1.020, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
|
||||
], // col 11
|
||||
[0.0; 20], // col 12
|
||||
[0.0; 20], // col 13
|
||||
[0.0; 20], // col 14
|
||||
[0.0; 20], // col 15
|
||||
[0.0; 20], // col 16
|
||||
[0.0; 20], // col 17
|
||||
[0.0; 20], // col 18
|
||||
[0.0; 20], // col 19
|
||||
[0.0; 20], // col 20
|
||||
[0.0; 20], // col 21
|
||||
[0.0; 20], // col 22
|
||||
[0.0; 20], // col 23
|
||||
[0.0; 20], // col 24
|
||||
];
|
||||
|
||||
/// 光速 (cm/s)
|
||||
const C_LIGHT: f64 = 2.997925e18;
|
||||
|
||||
/// 截面单位转换因子 (cm^2)
|
||||
const SIG_FACTOR: f64 = 1.0e-18;
|
||||
|
||||
/// Hidalgo (1968) 光致电离截面插值。
|
||||
///
|
||||
/// 根据 Hidalgo 数据表,对给定频率进行线性插值。
|
||||
///
|
||||
/// # 参数
|
||||
///
|
||||
/// * `ib` - 物种标识(负值,`INDEX = -IB - 100`)
|
||||
/// * `fr` - 频率 (Hz)
|
||||
///
|
||||
/// # 返回值
|
||||
///
|
||||
/// 光致电离截面 (cm^2)
|
||||
pub fn hidalg(ib: i32, fr: f64) -> f64 {
|
||||
let index = (-ib - 101) as usize; // 转为 0-indexed
|
||||
if index >= 24 {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
// 根据 INDEX 选择波长网格和数据
|
||||
let num = if index < 12 { 20 } else { 15 };
|
||||
let wli = if index < 12 { &WL1 } else { &WL2 };
|
||||
let sigs = &SIG0[index];
|
||||
|
||||
// 将频率转换为波长 (Å)
|
||||
let wlam = C_LIGHT / fr;
|
||||
|
||||
// 查找插值区间
|
||||
let mut il = 0;
|
||||
let mut ir = num - 1;
|
||||
for i in 0..num - 1 {
|
||||
if wlam >= wli[i] && wlam <= wli[i + 1] {
|
||||
il = i;
|
||||
ir = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 线性插值
|
||||
let mut sigm = if wli[ir] - wli[il] > 0.0 {
|
||||
(sigs[ir] - sigs[il]) * (wlam - wli[il]) / (wli[ir] - wli[il]) + sigs[il]
|
||||
} else {
|
||||
sigs[il]
|
||||
};
|
||||
|
||||
// 边界处理
|
||||
if wlam <= wli[0] {
|
||||
sigm = sigs[0];
|
||||
}
|
||||
if wlam >= wli[num - 1] {
|
||||
sigm = sigs[num - 1];
|
||||
}
|
||||
|
||||
sigm * SIG_FACTOR
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_hidalg_in_range() {
|
||||
// 测试 H I (IB=-101, INDEX=0) 在有效波长范围内
|
||||
// 100 Å 对应频率
|
||||
let fr = C_LIGHT / 100.0;
|
||||
let result = hidalg(-101, fr);
|
||||
// H I 数据全为 0,所以结果应为 0
|
||||
assert!(result >= 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hidalg_species_2() {
|
||||
// 测试物种 2 (IB=-102, INDEX=1)
|
||||
let fr = C_LIGHT / 50.0; // 50 Å
|
||||
let result = hidalg(-102, fr);
|
||||
assert!(result >= 0.0);
|
||||
assert!(result.is_finite());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hidalg_invalid_index() {
|
||||
let result = hidalg(-125, C_LIGHT / 100.0);
|
||||
assert_eq!(result, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hidalg_above_range() {
|
||||
// 高于波长范围时返回末值
|
||||
let fr = C_LIGHT / 200.0;
|
||||
let result = hidalg(-102, fr);
|
||||
assert!(result >= 0.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,470 @@
|
||||
//! Hydrogen line profile data initialization.
|
||||
//!
|
||||
//! Translated from SYNSPEC54.FOR subroutine HYDINI (line 6877).
|
||||
//!
|
||||
//! Initializes necessary arrays for evaluating hydrogen line profiles
|
||||
//! from the Lemke, Tremblay-Bergeron, or Schoening-Butler tables.
|
||||
|
||||
#![allow(clippy::never_loop)]
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
|
||||
use super::stark0::stark0;
|
||||
|
||||
/// Constants for hydrogen profile arrays
|
||||
pub const NLINES_MAX: usize = 22;
|
||||
pub const NLEVELS: usize = 4;
|
||||
pub const NWL_MAX: usize = 100;
|
||||
pub const NT_MAX: usize = 20;
|
||||
pub const NE_MAX: usize = 20;
|
||||
|
||||
/// Hydrogen line profile table data
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HydProfileTable {
|
||||
/// Line index (i, j)
|
||||
pub i: usize,
|
||||
pub j: usize,
|
||||
/// Central wavelength
|
||||
pub wl0: f64,
|
||||
/// Number of wavelength points
|
||||
pub nwl: usize,
|
||||
/// Number of temperature points
|
||||
pub nt: usize,
|
||||
/// Number of electron density points
|
||||
pub ne: usize,
|
||||
/// Log10 wavelength displacements [NWL_MAX]
|
||||
pub wl: [f64; NWL_MAX],
|
||||
/// Log10 temperature grid [NT_MAX]
|
||||
pub xt: [f64; NT_MAX],
|
||||
/// Log10 electron density grid [NE_MAX]
|
||||
pub xne: [f64; NE_MAX],
|
||||
/// Profile values [NWL_MAX x NT_MAX x NE_MAX]
|
||||
pub prf: [[[f64; NE_MAX]; NT_MAX]; NWL_MAX],
|
||||
/// Asymptotic profile coefficient
|
||||
pub xk: f64,
|
||||
}
|
||||
|
||||
impl Default for HydProfileTable {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
i: 0,
|
||||
j: 0,
|
||||
wl0: 0.0,
|
||||
nwl: 0,
|
||||
nt: 0,
|
||||
ne: 0,
|
||||
wl: [0.0; NWL_MAX],
|
||||
xt: [0.0; NT_MAX],
|
||||
xne: [0.0; NE_MAX],
|
||||
prf: [[[0.0; NE_MAX]; NT_MAX]; NWL_MAX],
|
||||
xk: 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Hydrogen line initialization result
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HydInitResult {
|
||||
/// Central wavelengths for lines [NLEVELS x NLINES_MAX]
|
||||
pub wline: [[f64; NLINES_MAX]; NLEVELS],
|
||||
/// Line index mapping [NLEVELS x NLINES_MAX]
|
||||
pub ilin0: [[usize; NLINES_MAX]; NLEVELS],
|
||||
/// Profile tables
|
||||
pub tables: Vec<HydProfileTable>,
|
||||
/// Lemke mode flag
|
||||
pub ilemke: bool,
|
||||
/// Number of lines
|
||||
pub nlihyd: usize,
|
||||
}
|
||||
|
||||
impl Default for HydInitResult {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
wline: [[0.0; NLINES_MAX]; NLEVELS],
|
||||
ilin0: [[0; NLINES_MAX]; NLEVELS],
|
||||
tables: Vec::new(),
|
||||
ilemke: false,
|
||||
nlihyd: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Hydrogen line profile table source
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum HydTableSource {
|
||||
/// Schoening-Butler tables (ihydpr < 0)
|
||||
SchoeningButler,
|
||||
/// Lemke tables (ihydpr = 21)
|
||||
Lemke,
|
||||
/// Tremblay-Bergeron tables (ihydpr = 22)
|
||||
Tremblay,
|
||||
}
|
||||
|
||||
/// Parameters for HYDINI
|
||||
pub struct HydiniParams {
|
||||
/// Table source selection
|
||||
pub source: HydTableSource,
|
||||
/// Path to data directory
|
||||
pub data_dir: String,
|
||||
/// Model depth points
|
||||
pub nd: usize,
|
||||
/// Temperature array [nd]
|
||||
pub temp: Vec<f64>,
|
||||
/// Electron density array [nd]
|
||||
pub elec: Vec<f64>,
|
||||
/// Turbulent velocity array [nd]
|
||||
pub vturb: Vec<f64>,
|
||||
}
|
||||
|
||||
/// Initialize hydrogen line profile data.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `params` - Initialization parameters
|
||||
///
|
||||
/// # Returns
|
||||
/// Hydrogen line initialization result with profile tables
|
||||
pub fn hydini(params: &HydiniParams) -> std::io::Result<HydInitResult> {
|
||||
let mut result = HydInitResult::default();
|
||||
|
||||
// Initialize central wavelengths using STARK0
|
||||
for i in 0..NLEVELS {
|
||||
for j in (i + 1)..NLINES_MAX {
|
||||
let stark = stark0(i as i32 + 1, j as i32 + 1, 1);
|
||||
result.wline[i][j] = stark.wl0;
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize line index mapping
|
||||
for i in 0..NLEVELS {
|
||||
for j in 0..NLINES_MAX {
|
||||
result.ilin0[i][j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
match params.source {
|
||||
HydTableSource::SchoeningButler => {
|
||||
read_schoening_butler(params, &mut result)?;
|
||||
}
|
||||
HydTableSource::Lemke | HydTableSource::Tremblay => {
|
||||
read_lemke_tremblay(params, &mut result)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Read Schoening-Butler tables
|
||||
fn read_schoening_butler(
|
||||
params: &HydiniParams,
|
||||
result: &mut HydInitResult,
|
||||
) -> std::io::Result<()> {
|
||||
let filename = format!("{}/hydprf.dat", params.data_dir);
|
||||
let file = File::open(&filename)?;
|
||||
let reader = BufReader::new(file);
|
||||
let mut lines = reader.lines();
|
||||
|
||||
// Skip 12 header lines
|
||||
for _ in 0..12 {
|
||||
lines.next();
|
||||
}
|
||||
|
||||
let nline = 12;
|
||||
result.ilemke = false;
|
||||
|
||||
for iline in 0..nline {
|
||||
// Read line indices
|
||||
let header = read_next_line(&mut lines)?;
|
||||
let (i, j) = parse_line_indices(&header)?;
|
||||
|
||||
let j = if iline == 11 { 10 } else { j }; // Special case for last line
|
||||
let wl0 = result.wline[i - 1][j - 1];
|
||||
result.ilin0[i - 1][j - 1] = iline + 1;
|
||||
|
||||
let mut table = HydProfileTable {
|
||||
i,
|
||||
j,
|
||||
wl0,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// Read wavelength points
|
||||
let wl_line = read_next_line(&mut lines)?;
|
||||
let wl_parts = parse_data_line(&wl_line)?;
|
||||
let nwl = wl_parts.len() - 1; // First value is character
|
||||
table.nwl = nwl;
|
||||
for k in 0..nwl.min(NWL_MAX) {
|
||||
table.wl[k] = if wl_parts[k + 1] < 1.0e-4 {
|
||||
(1.0e-4_f64).log10()
|
||||
} else {
|
||||
wl_parts[k + 1].log10()
|
||||
};
|
||||
}
|
||||
|
||||
// Read temperature points
|
||||
let xt_line = read_next_line(&mut lines)?;
|
||||
let xt_parts = parse_data_line(&xt_line)?;
|
||||
let nt = xt_parts.len() - 1;
|
||||
table.nt = nt;
|
||||
for k in 0..nt.min(NT_MAX) {
|
||||
table.xt[k] = xt_parts[k + 1];
|
||||
}
|
||||
|
||||
// Read electron density points
|
||||
let xne_line = read_next_line(&mut lines)?;
|
||||
let xne_parts = parse_data_line(&xne_line)?;
|
||||
let ne = xne_parts.len() - 1;
|
||||
table.ne = ne;
|
||||
for k in 0..ne.min(NE_MAX) {
|
||||
table.xne[k] = xne_parts[k + 1];
|
||||
}
|
||||
|
||||
// Skip blank line
|
||||
lines.next();
|
||||
|
||||
// Read profile data
|
||||
for ie in 0..ne.min(NE_MAX) {
|
||||
for it in 0..nt.min(NT_MAX) {
|
||||
lines.next(); // Skip blank line
|
||||
let prf_line = read_next_line(&mut lines)?;
|
||||
let prf_parts = parse_data_line(&prf_line)?;
|
||||
for iwl in 0..nwl.min(NWL_MAX) {
|
||||
if iwl < prf_parts.len() {
|
||||
table.prf[iwl][it][ie] = prf_parts[iwl];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compute asymptotic profile coefficient
|
||||
if nwl > 0 && ne > 0 {
|
||||
let xclog = table.prf[nwl - 1][0][0]
|
||||
+ 2.5 * table.wl[nwl - 1]
|
||||
+ 31.5304
|
||||
- table.xne[0]
|
||||
- 2.0 * wl0.log10();
|
||||
let xklog = 0.6666667 * (xclog - 0.176);
|
||||
table.xk = (xklog * std::f64::consts::LN_10).exp();
|
||||
}
|
||||
|
||||
result.tables.push(table);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Read Lemke or Tremblay tables
|
||||
fn read_lemke_tremblay(
|
||||
params: &HydiniParams,
|
||||
result: &mut HydInitResult,
|
||||
) -> std::io::Result<()> {
|
||||
let filename = match params.source {
|
||||
HydTableSource::Lemke => format!("{}/lemke.dat", params.data_dir),
|
||||
HydTableSource::Tremblay => format!("{}/tremblay.dat", params.data_dir),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let file = File::open(&filename)?;
|
||||
let reader = BufReader::new(file);
|
||||
let mut lines = reader.lines();
|
||||
|
||||
result.ilemke = true;
|
||||
|
||||
// Read number of tables
|
||||
let ntab_line = read_next_line(&mut lines)?;
|
||||
let ntab: usize = ntab_line.trim().parse().map_err(|e| {
|
||||
std::io::Error::new(std::io::ErrorKind::InvalidData, format!("NTAB: {}", e))
|
||||
})?;
|
||||
|
||||
let mut iline = 0;
|
||||
|
||||
for _ in 0..ntab {
|
||||
// Read number of lines in this table
|
||||
let nlly_line = read_next_line(&mut lines)?;
|
||||
let nlly: usize = nlly_line.trim().parse().map_err(|e| {
|
||||
std::io::Error::new(std::io::ErrorKind::InvalidData, format!("NLLY: {}", e))
|
||||
})?;
|
||||
|
||||
let ilineb = iline;
|
||||
|
||||
// Read line parameters
|
||||
for _ in 0..nlly {
|
||||
let param_line = read_next_line(&mut lines)?;
|
||||
let parts = parse_data_line(¶m_line)?;
|
||||
|
||||
if parts.len() < 11 {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
"Invalid Lemke/Tremblay parameter line",
|
||||
));
|
||||
}
|
||||
|
||||
let i = parts[0] as usize;
|
||||
let j = parts[1] as usize;
|
||||
let almin = parts[2];
|
||||
let anemin = parts[3];
|
||||
let tmin = parts[4];
|
||||
let dla = parts[5];
|
||||
let dle = parts[6];
|
||||
let dlt = parts[7];
|
||||
let nwl = parts[8] as usize;
|
||||
let ne = parts[9] as usize;
|
||||
let nt = parts[10] as usize;
|
||||
|
||||
let wl0 = result.wline[i - 1][j - 1];
|
||||
result.ilin0[i - 1][j - 1] = iline + 1;
|
||||
|
||||
let mut table = HydProfileTable {
|
||||
i,
|
||||
j,
|
||||
wl0,
|
||||
nwl,
|
||||
nt,
|
||||
ne,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// Generate wavelength grid
|
||||
for iwl in 0..nwl.min(NWL_MAX) {
|
||||
table.wl[iwl] = almin + (iwl as f64) * dla;
|
||||
}
|
||||
|
||||
// Generate electron density grid
|
||||
for ie in 0..ne.min(NE_MAX) {
|
||||
table.xne[ie] = anemin + (ie as f64) * dle;
|
||||
}
|
||||
|
||||
// Generate temperature grid
|
||||
for it in 0..nt.min(NT_MAX) {
|
||||
table.xt[it] = tmin + (it as f64) * dlt;
|
||||
}
|
||||
|
||||
result.tables.push(table);
|
||||
iline += 1;
|
||||
}
|
||||
|
||||
// Read profile data for each line
|
||||
for ili in 0..nlly {
|
||||
let ilne = ilineb + ili;
|
||||
let table = &mut result.tables[ilne];
|
||||
let nwl = table.nwl;
|
||||
let ne = table.ne;
|
||||
let nt = table.nt;
|
||||
|
||||
lines.next(); // Skip blank line
|
||||
|
||||
for ie in 0..ne.min(NE_MAX) {
|
||||
for it in 0..nt.min(NT_MAX) {
|
||||
let prf_line = read_next_line(&mut lines)?;
|
||||
let parts = parse_data_line(&prf_line)?;
|
||||
|
||||
// First value is QLT (quality factor), skip it
|
||||
for iwl in 0..nwl.min(NWL_MAX) {
|
||||
if iwl + 1 < parts.len() {
|
||||
table.prf[iwl][it][ie] = parts[iwl + 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compute asymptotic profile coefficient
|
||||
if nwl > 0 && ne > 0 {
|
||||
let xclog = table.prf[nwl - 1][0][0]
|
||||
+ 2.5 * table.wl[nwl - 1].log10()
|
||||
+ 31.5304
|
||||
- table.xne[0]
|
||||
- 2.0 * table.wl0.log10();
|
||||
let xklog = 0.6666667 * (xclog - 0.176);
|
||||
table.xk = (xklog * std::f64::consts::LN_10).exp();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result.nlihyd = iline;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Read next non-empty line
|
||||
fn read_next_line(lines: &mut impl Iterator<Item = std::io::Result<String>>) -> std::io::Result<String> {
|
||||
loop {
|
||||
match lines.next() {
|
||||
Some(Ok(line)) => return Ok(line),
|
||||
Some(Err(e)) => return Err(e),
|
||||
None => return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::UnexpectedEof,
|
||||
"Unexpected end of file",
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse line indices from header: FORMAT(12X,I1,9X,I1)
|
||||
fn parse_line_indices(line: &str) -> std::io::Result<(usize, usize)> {
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() < 2 {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
format!("Invalid line indices: {}", line),
|
||||
));
|
||||
}
|
||||
|
||||
let i = parts[0].parse::<usize>().map_err(|e| {
|
||||
std::io::Error::new(std::io::ErrorKind::InvalidData, format!("I: {}", e))
|
||||
})?;
|
||||
let j = parts[1].parse::<usize>().map_err(|e| {
|
||||
std::io::Error::new(std::io::ErrorKind::InvalidData, format!("J: {}", e))
|
||||
})?;
|
||||
|
||||
Ok((i, j))
|
||||
}
|
||||
|
||||
/// Parse data line (free format)
|
||||
fn parse_data_line(line: &str) -> std::io::Result<Vec<f64>> {
|
||||
let values: Vec<f64> = line
|
||||
.split_whitespace()
|
||||
.filter_map(|s| s.parse::<f64>().ok())
|
||||
.collect();
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_hydini_default() {
|
||||
let result = HydInitResult::default();
|
||||
assert_eq!(result.wline.len(), NLEVELS);
|
||||
assert_eq!(result.ilin0.len(), NLEVELS);
|
||||
assert!(result.tables.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hyd_profile_table_default() {
|
||||
let table = HydProfileTable::default();
|
||||
assert_eq!(table.nwl, 0);
|
||||
assert_eq!(table.nt, 0);
|
||||
assert_eq!(table.ne, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_line_indices() {
|
||||
let line = " 1 2";
|
||||
let result = parse_line_indices(line);
|
||||
assert!(result.is_ok());
|
||||
let (i, j) = result.unwrap();
|
||||
assert_eq!(i, 1);
|
||||
assert_eq!(j, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_data_line() {
|
||||
let line = " 1.0 2.0 3.0 4.0";
|
||||
let result = parse_data_line(line);
|
||||
assert!(result.is_ok());
|
||||
let values = result.unwrap();
|
||||
assert_eq!(values.len(), 4);
|
||||
assert!((values[0] - 1.0).abs() < 1e-10);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,390 @@
|
||||
//! Hydrogen line opacity calculation for SYNSPEC.
|
||||
//!
|
||||
//! Translated from SYNSPEC `HYDLIN` subroutine (synspec54.f:5425).
|
||||
//!
|
||||
//! Calculates opacity and emissivity of hydrogen lines including:
|
||||
//! - Stark broadening (analytic profiles)
|
||||
//! - Allard quasi-molecular satellite opacity
|
||||
//! - Far-infrared hydrogen lines
|
||||
|
||||
use super::stark0::stark0;
|
||||
use super::starka::starka;
|
||||
use super::starkir::starkir;
|
||||
use super::divstr::divstr;
|
||||
use super::allard::{self, AllardData};
|
||||
use super::lyahhe::lyahhe;
|
||||
|
||||
/// Physical constants for hydrogen line calculations
|
||||
const CPP: f64 = 4.1412e-16;
|
||||
const CPJ: f64 = 157803.0;
|
||||
const C00: f64 = 1.25e-9;
|
||||
const CID: f64 = 0.02654;
|
||||
const CINV: f64 = 1.0 / 2.997925e18;
|
||||
|
||||
/// Parameters for hydrogen line opacity calculation
|
||||
pub struct HydlinParams {
|
||||
/// Depth index
|
||||
pub id: usize,
|
||||
/// Start frequency index
|
||||
pub i0: usize,
|
||||
/// End frequency index
|
||||
pub i1: usize,
|
||||
/// Number of frequencies
|
||||
pub nfreq: usize,
|
||||
/// Wavelength array (Å)
|
||||
pub wlam: Vec<f64>,
|
||||
/// Frequency array (Hz)
|
||||
pub freq: Vec<f64>,
|
||||
/// Temperature (K)
|
||||
pub t: f64,
|
||||
/// Electron density
|
||||
pub ane: f64,
|
||||
/// H atom exists
|
||||
pub iath: i32,
|
||||
/// Lower level for H lines
|
||||
pub ilowh: i32,
|
||||
/// Upper level limit
|
||||
pub m10: usize,
|
||||
pub m20: usize,
|
||||
/// H ground level population
|
||||
pub pop_h: f64,
|
||||
/// H continuum level population
|
||||
pub pop_h_cont: f64,
|
||||
/// Turbulent velocity
|
||||
pub vturb: f64,
|
||||
/// wnHint factors
|
||||
pub wn_hint: Vec<Vec<f64>>,
|
||||
/// Quasi-molecular Lyman-alpha flag (>0: include)
|
||||
pub nunalp: i32,
|
||||
/// Quasi-molecular Lyman-beta flag (>0: include)
|
||||
pub nunbet: i32,
|
||||
/// Quasi-molecular Lyman-gamma flag (>0: include)
|
||||
pub nungam: i32,
|
||||
/// Quasi-molecular Balmer flag (>0: include)
|
||||
pub nunbal: i32,
|
||||
/// Allard quasi-molecular profile data (optional)
|
||||
pub allard_data: Option<AllardData>,
|
||||
/// Neutral H particle density at depth [cm⁻³]
|
||||
pub hneutr: f64,
|
||||
/// Ionized H particle density at depth [cm⁻³]
|
||||
pub hcharg: f64,
|
||||
/// Lyman-alpha He broadening flag (>0: include)
|
||||
pub nunhhe: i32,
|
||||
/// He atom index in atomic data (>0: He present)
|
||||
pub iathe: i32,
|
||||
/// He ground level population at depth
|
||||
pub pop_he: f64,
|
||||
}
|
||||
|
||||
/// Result of hydrogen line opacity calculation
|
||||
pub struct HydlinResult {
|
||||
/// Absorption coefficient array
|
||||
pub absoh: Vec<f64>,
|
||||
/// Emission coefficient array
|
||||
pub emish: Vec<f64>,
|
||||
}
|
||||
|
||||
/// Calculate hydrogen line opacity and emissivity.
|
||||
///
|
||||
/// Translates the full SYNSPEC HYDLIN subroutine including Stark broadening
|
||||
/// and infrared lines.
|
||||
pub fn hydlin(params: &HydlinParams) -> HydlinResult {
|
||||
let i0 = params.i0;
|
||||
let i1 = params.i1;
|
||||
let nfreq = params.nfreq;
|
||||
|
||||
let mut absoh = vec![0.0; nfreq];
|
||||
let mut emish = vec![0.0; nfreq];
|
||||
|
||||
// Skip if no hydrogen or empty arrays
|
||||
if params.iath <= 0 || params.wlam.is_empty() || params.ilowh <= 0 {
|
||||
return HydlinResult { absoh, emish };
|
||||
}
|
||||
|
||||
let t = params.t;
|
||||
let t1 = 1.0 / t;
|
||||
let sqt = t.sqrt();
|
||||
let ane = params.ane;
|
||||
let anes = (ane.ln() / 6.0).exp();
|
||||
|
||||
// Population of level 2 (for Saha)
|
||||
let anp = params.pop_h_cont;
|
||||
let pp = CPP * ane * anp * t1 / sqt;
|
||||
|
||||
// Level populations
|
||||
let nlh = params.wn_hint.len().min(3);
|
||||
let mut pj = vec![0.0f64; 50];
|
||||
for il in 0..50 {
|
||||
let x = ((il + 1) * (il + 1)) as f64;
|
||||
if il < nlh {
|
||||
pj[il] = params.pop_h * (-CPJ / x * t1).exp() * x;
|
||||
} else {
|
||||
let wn = if il < params.wn_hint.len() && params.id < params.wn_hint[il].len() {
|
||||
params.wn_hint[il][params.id]
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
pj[il] = pp * (CPJ / x * t1).exp() * x * wn;
|
||||
}
|
||||
}
|
||||
|
||||
// Frequency-independent Stark parameters
|
||||
let f00 = C00 * anes * anes * anes * anes;
|
||||
let dop0 = 1.0e8 * (1.65e8 * t + params.vturb).sqrt();
|
||||
|
||||
// Determine spectral series range
|
||||
let iserl = params.ilowh as usize;
|
||||
let mut iseru = params.ilowh as usize;
|
||||
|
||||
if i0 < params.wlam.len() {
|
||||
let wl = params.wlam[i0];
|
||||
if wl > 14000.0 { iseru = 4; }
|
||||
if wl > 22700.0 { iseru = 5; }
|
||||
if wl > 32800.0 { iseru = 6; }
|
||||
if wl > 44660.0 { iseru = 7; }
|
||||
}
|
||||
|
||||
// Loop over spectral series
|
||||
for i in iserl..=iseru.min(40) {
|
||||
let ii = (i * i) as f64;
|
||||
let xii = 1.0 / ii;
|
||||
let popi = if i - 1 < pj.len() { pj[i - 1] } else { 0.0 };
|
||||
|
||||
// Determine contributing lines
|
||||
let m1 = (i + 1).max(params.m10);
|
||||
let m2 = (i + 4).min(params.m20).min(40);
|
||||
|
||||
for j in m1..=m2 {
|
||||
let jj = (j * j) as f64;
|
||||
let xjj = 1.0 / jj;
|
||||
|
||||
// Transition properties
|
||||
let wn_j = if j - 1 < params.wn_hint.len() && params.id < params.wn_hint[j - 1].len() {
|
||||
params.wn_hint[j - 1][params.id]
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
let wn_i = if i - 1 < params.wn_hint.len() && params.id < params.wn_hint[i - 1].len() {
|
||||
params.wn_hint[i - 1][params.id]
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
|
||||
let abtra = popi * wn_j;
|
||||
let emtra = if j - 1 < pj.len() {
|
||||
pj[j - 1] * wn_i * ii * xjj * (CPJ * (xii - xjj) * t1).exp()
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
|
||||
// Use analytic Stark profile
|
||||
let stark = stark0(i as i32, j as i32, 1);
|
||||
let wl0 = stark.wl0;
|
||||
let xkij = stark.xkij;
|
||||
let fij = stark.fij;
|
||||
|
||||
// Check if line contributes in this wavelength region
|
||||
let wlam_i1 = if i1 < params.wlam.len() { params.wlam[i1] } else { 0.0 };
|
||||
let wlam_i0 = if i0 < params.wlam.len() { params.wlam[i0] } else { 0.0 };
|
||||
let in_range = (wl0 <= wlam_i1 && 1.25 * wl0 > wlam_i0)
|
||||
|| (wl0 >= wlam_i0 && 0.75 * wl0 < wlam_i1);
|
||||
|
||||
if in_range {
|
||||
let fxk = f00 * xkij;
|
||||
if fxk.abs() < 1.0e-30 { continue; }
|
||||
let fxk1 = 1.0 / fxk;
|
||||
let dop = dop0 / wl0;
|
||||
let dbeta = wl0 * wl0 * CINV * fxk1;
|
||||
let betad = dop * dbeta;
|
||||
let fid = CID * fij * dbeta;
|
||||
let (ad, div) = divstr(betad);
|
||||
|
||||
// Quasi-molecular opacity check (Lyman alpha/beta/gamma, Balmer alpha)
|
||||
let lquasi = (i == 1 && j == 2 && params.nunalp > 0)
|
||||
|| (i == 1 && j == 3 && params.nunbet > 0)
|
||||
|| (i == 1 && j == 4 && params.nungam > 0)
|
||||
|| (i == 2 && j == 3 && params.nunbal > 0);
|
||||
|
||||
if lquasi && params.allard_data.is_some() {
|
||||
// Allard quasi-molecular + Stark profile
|
||||
let ad_ref = params.allard_data.as_ref().unwrap();
|
||||
for ij in i0..=i1.min(nfreq - 1) {
|
||||
let wl = params.wlam[ij];
|
||||
let beta = (wl - wl0).abs() * fxk1;
|
||||
let sg_allard = allard::allard(ad_ref, wl, params.hneutr, params.hcharg, i as i32, j as i32);
|
||||
let sg = sg_allard + starka(beta, betad, ad, div, 2.0) * fid;
|
||||
absoh[ij] += sg * abtra;
|
||||
emish[ij] += sg * emtra;
|
||||
}
|
||||
} else {
|
||||
// Standard Stark profile
|
||||
for ij in i0..=i1.min(nfreq - 1) {
|
||||
let beta = (params.wlam[ij] - wl0).abs() * fxk1;
|
||||
let sg = if i < 5 {
|
||||
starka(beta, betad, ad, div, 2.0) * fid
|
||||
} else {
|
||||
starkir(ii as i32, jj as i32, t, ane, beta, dbeta) * fid
|
||||
};
|
||||
absoh[ij] += sg * abtra;
|
||||
emish[ij] += sg * emtra;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Lyman-alpha broadening by helium (Lyahhe)
|
||||
let lalhhe = i == 1 && j == 2 && params.nunhhe > 0;
|
||||
if lalhhe && params.iathe > 0 && params.pop_he > 0.0 {
|
||||
let rel = 1.0 / std::f64::consts::TAU;
|
||||
for ij in i0..=i1.min(nfreq - 1) {
|
||||
let sg0 = lyahhe(params.wlam[ij], params.pop_he);
|
||||
let sg = sg0 * rel;
|
||||
absoh[ij] += sg * abtra;
|
||||
emish[ij] += sg * emtra;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Far infrared hydrogen lines
|
||||
if i1 < nfreq && !params.wlam.is_empty() && params.wlam[i1.min(params.wlam.len() - 1)] > 70000.0 {
|
||||
for i in 8..=13 {
|
||||
let ii = (i * i) as f64;
|
||||
let xii = 1.0 / ii;
|
||||
for j in (i + 1)..=(i + 4).min(40) {
|
||||
let jj = (j * j) as f64;
|
||||
let xjj = 1.0 / jj;
|
||||
|
||||
let stark = stark0(i as i32, j as i32, 1);
|
||||
let wl0 = stark.wl0;
|
||||
let xkij = stark.xkij;
|
||||
let fij = stark.fij;
|
||||
|
||||
let wlam_i1 = params.wlam[i1.min(params.wlam.len() - 1)];
|
||||
let wlam_i0 = params.wlam[i0.min(params.wlam.len() - 1)];
|
||||
let in_range = (wl0 <= wlam_i1 && 1.5 * wl0 > wlam_i0)
|
||||
|| (wl0 >= wlam_i0 && 0.5 * wl0 < wlam_i1);
|
||||
|
||||
if in_range {
|
||||
let fxk = f00 * xkij;
|
||||
if fxk.abs() < 1.0e-30 { continue; }
|
||||
let fxk1 = 1.0 / fxk;
|
||||
let dop = dop0 / wl0;
|
||||
let dbeta = wl0 * wl0 * CINV * fxk1;
|
||||
let _betad = dop * dbeta;
|
||||
let fid = CID * fij * dbeta;
|
||||
|
||||
let wn_j = if j - 1 < params.wn_hint.len() && params.id < params.wn_hint[j - 1].len() {
|
||||
params.wn_hint[j - 1][params.id]
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
let wn_i = if i - 1 < params.wn_hint.len() && params.id < params.wn_hint[i - 1].len() {
|
||||
params.wn_hint[i - 1][params.id]
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
let popi = if i - 1 < pj.len() { pj[i - 1] } else { 0.0 };
|
||||
let popj = if j - 1 < pj.len() { pj[j - 1] } else { 0.0 };
|
||||
let abtra = popi * wn_j;
|
||||
let emtra = popj * wn_i * ii * xjj * (CPJ * (xii - xjj) * t1).exp();
|
||||
|
||||
for ij in i0..=i1.min(nfreq - 1) {
|
||||
let beta = (params.wlam[ij] - wl0).abs() * fxk1;
|
||||
let sg = starkir(ii as i32, jj as i32, t, ane, beta, dbeta) * fid;
|
||||
absoh[ij] += sg * abtra;
|
||||
emish[ij] += sg * emtra;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Total opacity and emissivity (stimulated emission correction)
|
||||
for ij in i0..=i1.min(nfreq - 1) {
|
||||
let f = params.freq[ij];
|
||||
let f15 = f * 1.0e-15;
|
||||
let xkf = (-4.79928e-11 * f * t1).exp();
|
||||
let xkfb = xkf * 1.4743e-2 * f15 * f15 * f15;
|
||||
absoh[ij] -= xkf * emish[ij];
|
||||
emish[ij] *= xkfb;
|
||||
}
|
||||
|
||||
HydlinResult { absoh, emish }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_hydlin_basic() {
|
||||
let params = HydlinParams {
|
||||
id: 0,
|
||||
i0: 0,
|
||||
i1: 4,
|
||||
nfreq: 5,
|
||||
wlam: vec![10000.0, 12000.0, 14000.0, 16000.0, 18000.0],
|
||||
freq: vec![3.0e14, 2.5e14, 2.14e14, 1.87e14, 1.67e14],
|
||||
t: 6000.0,
|
||||
ane: 1.0e13,
|
||||
iath: 1,
|
||||
ilowh: 1,
|
||||
m10: 2,
|
||||
m20: 10,
|
||||
pop_h: 1.0e16,
|
||||
pop_h_cont: 1.0e10,
|
||||
vturb: 2.0e5,
|
||||
wn_hint: vec![vec![1.0; 10]; 50],
|
||||
nunalp: 0,
|
||||
nunbet: 0,
|
||||
nungam: 0,
|
||||
nunbal: 0,
|
||||
allard_data: None,
|
||||
hneutr: 0.0,
|
||||
hcharg: 0.0,
|
||||
nunhhe: 0,
|
||||
iathe: 0,
|
||||
pop_he: 0.0,
|
||||
};
|
||||
|
||||
let result = hydlin(¶ms);
|
||||
assert!(result.absoh.iter().all(|&x| x.is_finite()));
|
||||
assert!(result.emish.iter().all(|&x| x.is_finite()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hydlin_no_hydrogen() {
|
||||
let params = HydlinParams {
|
||||
id: 0,
|
||||
i0: 0,
|
||||
i1: 4,
|
||||
nfreq: 5,
|
||||
wlam: vec![10000.0, 12000.0, 14000.0, 16000.0, 18000.0],
|
||||
freq: vec![3.0e14, 2.5e14, 2.14e14, 1.87e14, 1.67e14],
|
||||
t: 6000.0,
|
||||
ane: 1.0e13,
|
||||
iath: 0,
|
||||
ilowh: 1,
|
||||
m10: 2,
|
||||
m20: 10,
|
||||
pop_h: 1.0e16,
|
||||
pop_h_cont: 1.0e10,
|
||||
vturb: 2.0e5,
|
||||
wn_hint: vec![vec![1.0; 10]; 50],
|
||||
nunalp: 0,
|
||||
nunbet: 0,
|
||||
nungam: 0,
|
||||
nunbal: 0,
|
||||
allard_data: None,
|
||||
hneutr: 0.0,
|
||||
hcharg: 0.0,
|
||||
nunhhe: 0,
|
||||
iathe: 0,
|
||||
pop_he: 0.0,
|
||||
};
|
||||
|
||||
let result = hydlin(¶ms);
|
||||
assert!(result.absoh.iter().all(|&x| x == 0.0));
|
||||
assert!(result.emish.iter().all(|&x| x == 0.0));
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user