Skip to content

Support skewed in-plane SPM cells#21

Open
cpignedoli wants to merge 4 commits into
codex/numpy2-compatfrom
codex/skewed-cell-support
Open

Support skewed in-plane SPM cells#21
cpignedoli wants to merge 4 commits into
codex/numpy2-compatfrom
codex/skewed-cell-support

Conversation

@cpignedoli

Copy link
Copy Markdown
Member

Summary

This draft adds initial support for skewed in-plane CP2K SPM cells in the WFN gridding path.

The implementation keeps the existing orthogonal-cell behavior intact while introducing full cell-vector handling for slab-like cells where A and B lie in the xy plane and C is along z. More general tilted/triclinic cells are deliberately rejected for now instead of being treated incorrectly.

This PR is stacked on top of #20 (codex/numpy2-compat) and should be retargeted to main after #20 is merged.

Changes

  • Parse CP2K A, B, and C cell vectors without requiring a rectangular cell.
  • Store full cell vectors alongside the existing cell-vector lengths used by older code paths.
  • Convert atom positions through fractional/grid coordinates before placing local orbital grids.
  • Evaluate radial functions and solid harmonics in Cartesian coordinates on skewed grids.
  • Use the in-plane reciprocal metric for FFT extrapolation instead of assuming kx**2 + ky**2.
  • Write CUBE files with the full evaluation-cell matrix and integrate densities using the voxel determinant.
  • Convert p-tip finite-difference derivatives from lattice-axis derivatives to Cartesian in-plane gradients.
  • Save skewed-cell metadata in STM/orbital .npz outputs.
  • Add examples/validate_skewed_cell_support.py for lightweight manual validation with bundled benzene data.

Validation

  • python -m py_compile cp2k_spm_tools/cp2k_grid_orbitals.py cp2k_spm_tools/cp2k_stm_sts.py examples/validate_skewed_cell_support.py
  • git diff --check
  • PYTHONPATH=/home/jovyan/opt/cp2k-spm-tools python examples/validate_skewed_cell_support.py
  • pre-commit run --files cp2k_spm_tools/cp2k_grid_orbitals.py cp2k_spm_tools/cp2k_stm_sts.py examples/validate_skewed_cell_support.py

Additional local check: the standard orthogonal benzene CUBE output was compared against the base NumPy-2 branch and matched exactly for HOMO and LUMO.

Still To Test

  • Real non-90-degree CP2K/SPM calculations from production-like inputs.
  • Plotting behavior for skewed STM/orbital maps.
  • HRSTM-specific paths, which still contain additional rectangular-grid assumptions and are not claimed as fixed here.

@cpignedoli

Copy link
Copy Markdown
Member Author

Follow-up fix pushed in 21b778b: a real AiiDA STM submission exposed that the ionization-potential diagnostic assumed the HOMO is always present in the selected STM orbital window. That is not guaranteed for finite energy-window runs, so the diagnostic now skips itself cleanly when the HOMO is outside the selected window instead of crashing the STM CalcJob.

Extra validation after the fix:

  • python -m py_compile cp2k_spm_tools/cli/stm_sts_from_wfn.py
  • git diff --check
  • pre-commit run --files cp2k_spm_tools/cli/stm_sts_from_wfn.py
  • Replayed the failing local STM command against the generated CP2K parent folder; it now completes and writes stm.npz.

@cpignedoli
cpignedoli requested a review from eimrek July 14, 2026 12:32
@cpignedoli

cpignedoli commented Jul 14, 2026

Copy link
Copy Markdown
Member Author

Added a follow-up validation for the p-tip path.

What changed:

  • Factored the in-plane p-tip gradient norm into STM.in_plane_gradient_squared() without changing the formula.
  • Added an analytic skew-grid check in examples/validate_skewed_cell_support.py using a linear field with known Cartesian gradient.
  • The check verifies that the skewed-grid axis derivatives recover the correct grad_x**2 + grad_y**2 p-tip contribution.

Validation run:

  • python examples/validate_skewed_cell_support.py
  • python -m py_compile cp2k_spm_tools/cp2k_stm_sts.py examples/validate_skewed_cell_support.py
  • pre-commit run --files cp2k_spm_tools/cp2k_stm_sts.py examples/validate_skewed_cell_support.py

Prepared with assistance from Codex.

@cpignedoli
cpignedoli marked this pull request as ready for review July 14, 2026 12:51
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.

1 participant