Dense fallback: build full H matrix via h_op on identity columns, then use LAPACK.
| 127 | |
| 128 | // Dense fallback: build full H matrix via h_op on identity columns, then use LAPACK. |
| 129 | MatFreeDavidsonResult dense_fallback( |
| 130 | const HamiltonianOp& h_op, |
| 131 | int n_pw, |
| 132 | int n_bands |
| 133 | ) { |
| 134 | // Build dense H by applying h_op to identity vectors |
| 135 | std::vector<Complex> H(static_cast<std::size_t>(n_pw) * n_pw, Complex{0.0, 0.0}); |
| 136 | // Apply in blocks to avoid excessive memory for very small systems |
| 137 | std::vector<Complex> e_j(static_cast<std::size_t>(n_pw), Complex{0.0, 0.0}); |
| 138 | std::vector<Complex> h_col(static_cast<std::size_t>(n_pw), Complex{0.0, 0.0}); |
| 139 | for (int j = 0; j < n_pw; ++j) { |
| 140 | std::fill(e_j.begin(), e_j.end(), Complex{0.0, 0.0}); |
| 141 | e_j[static_cast<std::size_t>(j)] = Complex{1.0, 0.0}; |
| 142 | h_op(e_j.data(), h_col.data(), n_pw, 1); |
| 143 | for (int i = 0; i < n_pw; ++i) { |
| 144 | H[static_cast<std::size_t>(i + n_pw * j)] = h_col[static_cast<std::size_t>(i)]; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | auto eig = diagonalize_hermitian_dense(H, n_pw); |
| 149 | |
| 150 | MatFreeDavidsonResult result; |
| 151 | result.n_pw = n_pw; |
| 152 | result.n_bands = n_bands; |
| 153 | result.converged = true; |
| 154 | result.iterations = 0; |
| 155 | result.n_matvec = n_pw; |
| 156 | result.max_residual_norm = 0.0; |
| 157 | result.method = "dense_fallback"; |
| 158 | result.stop_reason = "dense_fallback"; |
| 159 | |
| 160 | result.eigenvalues_ha.assign( |
| 161 | eig.eigenvalues_ha.begin(), |
| 162 | eig.eigenvalues_ha.begin() + n_bands |
| 163 | ); |
| 164 | result.eigenvectors_col_major.resize(static_cast<std::size_t>(n_pw) * n_bands); |
| 165 | for (int band = 0; band < n_bands; ++band) { |
| 166 | for (int i = 0; i < n_pw; ++i) { |
| 167 | result.eigenvectors_col_major[static_cast<std::size_t>(i + n_pw * band)] = |
| 168 | eig.eigenvectors_col_major[static_cast<std::size_t>(i + n_pw * band)]; |
| 169 | } |
| 170 | } |
| 171 | return result; |
| 172 | } |
| 173 | |
| 174 | // Dense generalized fallback: build full H and S via callbacks, then use LAPACK. |
| 175 | MatFreeDavidsonResult dense_generalized_fallback( |
no test coverage detected