SIRIUS 7.5.0
Electronic structure library and applications
check_wave_functions.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2023 Anton Kozhevnikov, Thomas Schulthess
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
5// the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8// following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
10// and the following disclaimer in the documentation and/or other materials provided with the distribution.
11//
12// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
13// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
15// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
18// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
20/** \file check_wave_functions.hpp
21 *
22 * \brief Check orthogonality of wave-functions and their residuals.
23 */
24
25#ifndef __CHECK_WAVE_FUNCTIONS_HPP__
26#define __CHECK_WAVE_FUNCTIONS_HPP__
27
30
31namespace sirius {
32
33template <typename T, typename F>
34void check_wave_functions(Hamiltonian_k<real_type<T>> const& Hk__, wf::Wave_functions<T>& psi__, wf::spin_range sr__,
35 wf::band_range br__, double* eval__)
36{
37 wf::Wave_functions<T> hpsi(psi__.gkvec_sptr(), psi__.num_md(), wf::num_bands(br__.size()), sddk::memory_t::host);
38 wf::Wave_functions<T> spsi(psi__.gkvec_sptr(), psi__.num_md(), wf::num_bands(br__.size()), sddk::memory_t::host);
39
40 la::dmatrix<F> ovlp(br__.size(), br__.size());
41
42 /* apply Hamiltonian and S operators to the wave-functions */
43 Hk__.template apply_h_s<F>(sr__, br__, psi__, &hpsi, &spsi);
44
45 wf::inner(Hk__.H0().ctx().spla_context(), sddk::memory_t::host, sr__, psi__, br__, spsi, br__, ovlp, 0, 0);
46
47 auto diff = check_identity(ovlp, br__.size());
48 if (diff > 1e-12) {
49 std::cout << "[check_wave_functions] Error! Wave-functions are not orthonormal, difference : " << diff << std::endl;
50 } else {
51 std::cout << "[check_wave_functions] Ok! Wave-functions are orthonormal" << std::endl;
52 }
53
54 for (int ib = 0; ib < br__.size(); ib++) {
55 double l2norm{0};
56 for (auto s = sr__.begin(); s != sr__.end(); s++) {
57 auto s1 = hpsi.actual_spin_index(s);
58 for (int ig = 0; ig < psi__.gkvec().count(); ig++) {
59 /* H|psi> - e S|psi> */
60 auto z = hpsi.pw_coeffs(ig, s1, wf::band_index(ib)) -
61 spsi.pw_coeffs(ig, s1, wf::band_index(ib)) * static_cast<real_type<T>>(eval__[ib]);
62 l2norm += std::real(z * std::conj(z));
63 }
64 psi__.gkvec().comm().allreduce(&l2norm, 1);
65 l2norm = std::sqrt(l2norm);
66 std::cout << "[check_wave_functions] band : " << ib << ", residual l2norm : " << l2norm << std::endl;
67 }
68 }
69}
70
71}
72
73#endif
Contains declaration and definition of sirius::Hamiltonian class.
std::enable_if_t< std::is_same< T, real_type< F > >::value, void > inner(::spla::Context &spla_ctx__, sddk::memory_t mem__, spin_range spins__, W const &wf_i__, band_range br_i__, Wave_functions< T > const &wf_j__, band_range br_j__, la::dmatrix< F > &result__, int irow0__, int jcol0__)
Compute inner product between the two sets of wave-functions.
Namespace of the SIRIUS library.
Definition: sirius.f90:5
auto conj(double x__)
Return complex conjugate of a number. For a real value this is the number itself.
Definition: math_tools.hpp:165
Contains declaration and implementation of Wave_functions class.