Skip to content

Hessian efficient#23

Merged
mesonepigreco merged 17 commits intomainfrom
hessian_efficient
Mar 25, 2026
Merged

Hessian efficient#23
mesonepigreco merged 17 commits intomainfrom
hessian_efficient

Conversation

@mesonepigreco
Copy link
Copy Markdown
Collaborator

We can use an efficient implementation of the Q-space Lanczos for computing the Hessian.
This is now implemented and working.

mesonepigreco and others added 9 commits March 18, 2026 09:09
Implement the Lanczos algorithm in q-space (Bloch basis) where momentum
conservation q1+q2=q_pert makes the 2-phonon sector block-diagonal,
reducing psi vector size and anharmonic computation cost by ~N_cell.

New files:
- Modules/QSpaceLanczos.py: Python class inheriting from DL.Lanczos with
  Hermitian Lanczos iteration, Bloch-transformed ensemble, q-pair mapping,
  and correct PG symmetry construction (Cartesian rotations from spglib
  fractional coords, manual atom permutation with lattice reduction)
- Modules/tdscha_qspace.jl: Julia extension for q-space ensemble averaging
  with separate D3/D4 functions and acoustic mode masking
- tests/test_qspace/test_qspace_lanczos.py: Test comparing q-space vs
  real-space Green functions (match within 2.4e-7 relative error)

Key fixes during development:
- Convert spglib rotations from crystal to Cartesian coords via M@R@M^{-1}
- Replace CC.symmetries.GetIRT (gives wrong atom permutations for some
  symmetries) with manual modular lattice reduction
- Include translation in Bloch phase: exp(-2πi q'·L) where L=Rτ+t-τ'
- Mask acoustic modes (ω<1e-6) in f_Y, f_psi, and chi factors

Also includes the translational projection optimization in DynamicalLanczos
(O(n^2) permutations instead of O(n^3) matmuls for gamma_only).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
New usage guide section covering the QSpaceLanczos workflow (perturbation
setup, mode looping, when to prefer q-space over real-space) and a new
API reference page with auto-generated docs via mkdocstrings.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Computes the anharmonic phonon spectral function at the Brillouin zone
boundary (X point) for SnTe using the Q-space Lanczos algorithm with
the pre-existing test ensemble.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
which should solve the free energy Hessian in q space correctly.
Right now, it seems to work extremely well, except for 1 q point in the test, where none of the algorithms
seems to converge.
We need to investigate the issue.
We fixed a nasty hermitian conjugation in Q points not related to themselves by the inversion symmetries.
These q-points are not hermitian, so the Lanczos was failing in them due to a mismatch between row-major and column-major
transposition done in julia vs python.

This is fixed and now it seems to work properly.
Still an issue on IR prefactor and convergence of the bicg for that q != -q + G points is present to be fixed
(its is only a convergence issue, as by explicitly computing the full L matrix and inverting produces the correct result,
and also the standard Lanczos with no-mode-mixing produces the correct results).
@mesonepigreco mesonepigreco added this to the 1.7 milestone Mar 21, 2026
mesonepigreco and others added 8 commits March 21, 2026 12:05
Documents the q-space free energy Hessian with usage examples,
performance comparison table, and deep-dive on the algorithm
(static Liouvillian, q-space block diagonality, acoustic mode
handling, Hermiticity enforcement, symmetry reduction).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…nd q-space Lanczos

This test verifies that the IR perturbation modulus matches exactly between
DynamicalLanczos and QSpaceLanczos after fixing the double mass scaling
and missing supercell factor in QSpaceLanczos.prepare_ir.

The test:
1. Creates ASR-satisfying effective charges (+I for first atom, -I for second)
2. Compares perturbation_modulus for all three polarization directions
3. Uses existing SnTe test data with 2×2×2 supercell
- Implemented prepare_raman() method in QSpaceLanczos with support for:
  - Polarized Raman (pol_vec_in, pol_vec_out)
  - Unpolarized Raman components (indices 0-6 with correct prefactors)
  - Mixed Raman tensor components

- Implemented prepare_unpolarized_raman() method for raw components
  without prefactors (matching parent class interface)

- Modified prepare_perturbation_q() to support add parameter for
  cumulative perturbations (needed for unpolarized Raman)

- Added comprehensive test test_raman_modulus.py that verifies:
  - Perturbation modulus consistency between real-space and q-space
    Lanczos for all polarized Raman components (xx, xy, yy, zz)
  - Consistency for all 7 unpolarized Raman components
  - Tests use dummy Raman tensor that satisfies translation invariance

- Fixed test setup to ensure Raman tensor is properly attached to
  ens.current_dyn (used by Lanczos constructor) rather than ens.dyn_0

All tests pass, confirming the q-space implementation matches the
real-space implementation for Raman perturbations.
…aceHessian

At high-symmetry q-points, degenerate modes related by the little group
give redundant linear solves. By Schur's lemma, G_q restricted to a
d-dimensional irrep block is c*I_d, so only 1 solve per degenerate block
is needed instead of d. Cross-coupling between same-dimension blocks of
the same irrep type (c_cross*I_d) is also correctly propagated.

Savings on SnTe 2x2x2: Gamma 1 solve instead of 3, boundary q-points
4 solves instead of 6. Results match full solve to machine precision.
The free energy Hessian has a fundamental Hermitian symmetry property:
for conjugate q-pairs (q, -q), the blocks are related by Hermitian conjugate.
This commit exploits this symmetry to:

- Add get_perturb_averages_qspace_fused() in Julia that computes D3 and D4
  contributions in a single pass, properly handling Hermitian conjugate
  for reverse q-pairs (iq1 != iq2 case)
- Add get_static_psi_size() and improve get_block() in QSpaceLanczos to
  support reading from arbitrary source arrays (enables symmetry tests)
- Include IR spectrum calculation examples (compute_ir.py, plot_results.py)
- Add missing import in test_qspace_hessian.py

The fused kernel reduces memory traffic and ensures consistent handling
of the Hermitian symmetry across all perturbation terms.
This commit adds the ability to exclude specific anharmonic terms from
the free energy Hessian calculation in QSpaceHessian, matching the
functionality already present in QSpaceLanczos and DynamicalLanczos.

Changes:
- Add ignore_v3 and ignore_v4 parameters to QSpaceHessian.__init__
- Add __setattr__ to propagate flag changes to underlying QSpaceLanczos
- Update _apply_anharmonic_static_q to respect the flags:
  * Returns zero immediately if both flags are True (harmonic only)
  * Zeros out R1 if ignore_v3=True (excludes D3 contribution)
  * Relies on qlanc._call_julia_qspace for v4 exclusion
- Update documentation in docs/qspace-hessian.md with usage examples
- Add comprehensive tests in tests/test_qspace/test_hessian_v3v4_flags.py:
  * test_hessian_exclude_v4_only: compares with include_v4=False reference
  * test_hessian_exclude_v3_and_v4: verifies harmonic result
  * test_hessian_flags_consistency: checks initialization propagation
  * test_hessian_flags_setattr_propagation: verifies dynamic updates
…l logic

The script was missing the final_dyn parameter and had lost the conditional
logic that allows either running SSCHA minimization or loading a pre-computed
final dynamical matrix. This restores the original functionality:
- Added final_dyn = None input parameter
- Restored if/else block to handle both cases
@mesonepigreco mesonepigreco merged commit eba6053 into main Mar 25, 2026
1 check passed
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