Skip to content

fix: [Phase 3] Implement Delocalization Index (DI)#7

Open
newtontech wants to merge 1 commit into
mainfrom
fix/issue-4
Open

fix: [Phase 3] Implement Delocalization Index (DI)#7
newtontech wants to merge 1 commit into
mainfrom
fix/issue-4

Conversation

@newtontech
Copy link
Copy Markdown
Owner

Summary

This PR implements the Delocalization Index (DI) for analyzing electron pair sharing and aromaticity in molecular systems. The implementation includes 2-center and 3-center DI calculations, aromaticity indices (AI, PDI, FLU), and bond classification based on DI values.

Changes

  • pymultiwfn/bonding/delocalization.py: New module implementing:

    • delocalization_index(): 2-center DI calculation
    • three_center_delocalization_index(): 3-center DI for multi-center bonds
    • calculate_di_matrix(): Full DI matrix generation
    • classify_bond_from_di(): Bond type classification
    • calculate_aromaticity_index(): Ring aromaticity measure
    • calculate_pdi(): Para-Delocalization Index for 6-membered rings
    • calculate_flu(): Aromatic Fluctuation Index
    • DelocalizationIndex class: Main analysis interface
  • pymultiwfn/bonding/init.py: Updated to export new DI classes and functions

  • tests/bonding/test_delocalization_index.py: Comprehensive test suite with 35 passing tests covering:

    • Basic DI calculations
    • 3-center DI symmetry
    • DI matrix properties
    • Bond classification
    • Aromaticity indices
    • Class initialization and methods

Testing

All 35 tests pass:

  • TestDelocalizationIndexFunction: 6 tests
  • TestThreeCenterDI: 2 tests
  • TestDIMatrix: 3 tests
  • TestBondClassification: 6 tests
  • TestAromaticityIndices: 6 tests
  • TestDelocalizationIndexClass: 9 tests
  • TestDelocalizationResult: 3 tests

API Example

from pymultiwfn.bonding import DelocalizationIndex

di = DelocalizationIndex(wfn)
di_val = di.get_delocalization_index(atom_i=0, atom_j=1)
print(f"DI: {di_val:.3f} e")

# For aromaticity analysis
ring_atoms = [0, 1, 2, 3, 4, 5]  # benzene ring
ai = di.get_aromaticity_index(ring_atoms)
pdi = di.get_pdi(ring_atoms)
flu = di.get_flu(ring_atoms)
print(f"Aromaticity Index: {ai:.3f}")
print(f"PDI: {pdi:.3f} e")
print(f"FLU: {flu:.3f}")

Fixes #4

- Add delocalization.py module with 2-center and 3-center DI calculations
- Implement aromaticity indices (AI, PDI, FLU) from DI matrices
- Add bond classification based on DI values
- Create comprehensive test suite with 35 passing tests
- Update bonding __init__.py to export new DI classes and functions

Fixes #4
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 2, 2026

🤖 Hi @newtontech, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 2, 2026

🤖 I'm sorry @newtontech, but I was unable to process your request. Please see the logs for more details.

@newtontech
Copy link
Copy Markdown
Owner Author

Kimi CLI Review

Generated with kimi --print from PR metadata and patch diff. Review method: purpose-first, file/diff-based, severity-ordered findings, with tests/security/compatibility/CI impact checked.

PR Review: Implement Delocalization Index (DI)

总体评价

PR 实现了离域指数(DI)计算模块,包含2中心/3中心DI、芳香性指数(AI/PDI/FLU)和键型分类。代码结构清晰,测试覆盖较全面(35个测试),无阻塞性问题。


🔴 严重问题(Blocking)

无。


🟡 中等风险(Residual Risks / 需关注)

  1. delocalization.py 第159行:three_center_delocalization_index 的公式实现

    • 文档声称公式为 δ(A,B,C) = 2 * Σ_μ∈A Σ_ν∈B Σ_λ∈C |P_μνλ * S_μνλ|,但实际使用的是几何平均近似 √[δ(A,B) * δ(A,C) * δ(B,C)] / 2
    • 风险:这是一个近似而非严格定义,文档与实现不一致。建议在 docstring 中明确标注这是"approximation",避免用户误用。
  2. delocalization.py 第299-302行:calculate_fludi_ref_sum = 2.8 硬编码

    • 苯环参考值 2.8 是经验值(1.4 * 2),但未说明来源或允许自定义。
    • 风险:对其他芳香体系(如五元环、杂环)可能不适用。建议增加 di_ref 参数或文档说明限制。
  3. delocalization.py 第377-378行:_get_overlap_matrix fallback 使用单位矩阵

    • 当波函数无重叠矩阵时回退到 np.eye(n_basis),这是一个非常粗糙的近似。
    • 风险:会导致DI计算结果严重失真。建议至少发出 warnings.warn() 提醒用户。
  4. delocalization.py 第387-400行:_get_atom_basis_indices fallback 均分基函数

    • 当波函数无原子-基函数映射时,均分基函数是完全错误的物理近似。
    • 风险:对于实际分子计算,此fallback会产生无意义的DI值。建议同样增加警告。
  5. delocalization.py 第563行:get_all_dihedral_dis 方法名疑似笔误

    • 方法名为 get_all_dihedral_dis,但返回的是环内所有原子对的DI值,与"dihedral(二面角)"无关。
    • 建议确认是否为 get_all_ring_dis 的笔误。

🟢 低优先级 / 建议

  1. 测试文件第545行:benzene_wfn fixture 中电子数设置

    • num_electrons=30.0 对于6个碳原子(简化模型)的物理意义不明确,但这是测试fixture,可接受。
  2. 键分类阈值 classify_bond_from_di 的边界

    • 阈值(0.3/0.8/1.3/1.8/2.5)是经验值,未在文档中引用来源。建议补充参考文献。

测试评估

  • 35个测试全部通过,覆盖基础计算、矩阵性质、键分类、芳香性指数和类初始化。
  • 测试缺口:缺少对 _get_overlap_matrix_get_atom_basis_indices fallback 路径的测试;缺少真实波函数文件(如 .wfn/.fchk)的集成测试。

结论

无阻塞性问题,可合并。 但建议在合并前或后续迭代中:

  1. 修正 three_center_delocalization_index 的文档/实现一致性;
  2. 为粗糙的 fallback 路径(单位矩阵重叠、均分基函数)添加 warnings
  3. 确认 get_all_dihedral_dis 方法名是否为笔误。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Phase 3] Implement Delocalization Index (DI)

1 participant