Skip to content

Commit 550e807

Browse files
committed
Validate PPCG H operator input
1 parent 4d21088 commit 550e807

4 files changed

Lines changed: 28 additions & 2 deletions

File tree

source/source_hsolver/diago_ppcg.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ class DiagoPPCG
114114
return row + col * ld;
115115
}
116116

117-
void validate_input(const T* psi_in, const Real* eigenvalue_in,
117+
void validate_input(const HPsiFunc& hpsi_func,
118+
const T* psi_in, const Real* eigenvalue_in,
118119
const std::vector<double>& ethr_band,
119120
const Real* prec) const;
120121

source/source_hsolver/ppcg/diago_ppcg_diag.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ double DiagoPPCG<T, Device>::diag(const HPsiFunc& hpsi_func,
1818
n_band_ = nband;
1919
n_dim_ = dim;
2020

21-
validate_input(psi_in, eigenvalue_in, ethr_band, prec);
21+
validate_input(hpsi_func, psi_in, eigenvalue_in, ethr_band, prec);
2222
spsi_func_ = spsi_func;
2323

2424
// Allocate working storage.

source/source_hsolver/ppcg/diago_ppcg_ops.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ DiagoPPCG<T, Device>::DiagoPPCG(const Real& diag_thr,
4242
// =============================================================================
4343
template <typename T, typename Device>
4444
void DiagoPPCG<T, Device>::validate_input(
45+
const HPsiFunc& hpsi_func,
4546
const T* psi_in,
4647
const Real* eigenvalue_in,
4748
const std::vector<double>& ethr_band,
4849
const Real* prec) const
4950
{
51+
if (!hpsi_func)
52+
throw std::invalid_argument("PPCG: H operator is empty.");
5053
if (psi_in == nullptr || eigenvalue_in == nullptr)
5154
throw std::invalid_argument("PPCG: psi/eigenvalue pointer is null.");
5255
if (prec == nullptr)

source/source_hsolver/test/diago_ppcg_test.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,28 @@ TEST_F(DiagoPPCGDiagonalTest, ConjugateGradientFallback)
269269
<< "Diagonal CG fallback: too many iterations";
270270
}
271271

272+
TEST_F(DiagoPPCGDiagonalTest, EmptyHOperatorThrows)
273+
{
274+
std::vector<T> psi_run = psi;
275+
std::vector<Real> eval(nband, 0.0);
276+
277+
hsolver::DiagoPPCG<T, hsolver::base_device::DEVICE_CPU> solver(
278+
/* diag_thr = */ 1e-12,
279+
/* max_iter = */ 50,
280+
/* sbsize = */ 3,
281+
/* rr_step = */ 3,
282+
/* gamma_g0 = */ false,
283+
hsolver::PpcgStrategy::BLOCK_SUBSPACE
284+
);
285+
286+
hsolver::DiagoPPCG<T, hsolver::base_device::DEVICE_CPU>::HPsiFunc h_op;
287+
EXPECT_THROW(
288+
solver.diag(h_op, nullptr, ld, nband, n_dim,
289+
psi_run.data(), eval.data(), ethr, prec.data()),
290+
std::invalid_argument
291+
);
292+
}
293+
272294
TEST(DiagoPPCGLeadingDimensionTest, BlockSubspaceWithPadding)
273295
{
274296
const int n_dim = 5;

0 commit comments

Comments
 (0)