SIRIUS 7.5.0
Electronic structure library and applications
check_xc_potential.cpp
Go to the documentation of this file.
1// Copyright (c) 2013-2020 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_xc_potential.cpp
21 *
22 * \brief Check XC potential by doing numerical functional derivative.
23 */
24
26#include "dft/energy.hpp"
27
28namespace sirius {
29
30void check_xc_potential(Density const& rho__)
31{
32 Potential p0(const_cast<Simulation_context&>(rho__.ctx()));
33 p0.generate(rho__, rho__.ctx().use_symmetry(), true);
34
35 double evxc{0}, ebxc{0};
36 if (rho__.ctx().full_potential()) {
37 } else {
38 evxc = p0.energy_vxc(rho__) + p0.energy_vxc_core(rho__);
39 ebxc = energy_bxc(rho__, p0);
40 }
41 std::printf("<vxc|rho> : %18.12f\n", evxc);
42 std::printf("<bxc|mag> : %18.12f\n", ebxc);
43
44 double eps{0.1};
45 double best_result{1e10};
46 double best_eps{0};
47 for (int i = 0; i < 10; i++) {
48 Potential p1(const_cast<Simulation_context&>(rho__.ctx()));
49 /* compute Exc, Vxc at rho + delta * rho = (1+delta)rho */
50 p1.add_delta_rho_xc(eps);
51 p1.generate(rho__, rho__.ctx().use_symmetry(), true);
52
53 double deriv_mag{0};
54
55 if (rho__.ctx().num_mag_dims() > 0) {
56 Potential p2(const_cast<Simulation_context&>(rho__.ctx()));
57 /* compute Exc, Vxc at mag + delta * mag = (1+delta)mag */
58 p2.add_delta_mag_xc(eps);
59 p2.generate(rho__, rho__.ctx().use_symmetry(), true);
60
61 deriv_mag = (p2.energy_exc(rho__) - p0.energy_exc(rho__)) / eps;
62 }
63
64 double deriv_rho = (p1.energy_exc(rho__) - p0.energy_exc(rho__)) / eps;
65
66 std::printf("eps: %18.12f, drho: %18.12f, dmag: %18.12f\n", eps, std::abs(evxc - deriv_rho),
67 std::abs(ebxc - deriv_mag));
68
69 if (std::abs(evxc - deriv_rho) + std::abs(ebxc - deriv_mag) < best_result) {
70 best_result = std::abs(evxc - deriv_rho) + std::abs(ebxc - deriv_mag);
71 best_eps = eps;
72 }
73
74 eps /= 10;
75 }
76 std::printf("best total result : %18.12f for epsilon %18.12f\n", best_result, best_eps);
77}
78
79}
Total energy terms.
Namespace of the SIRIUS library.
Definition: sirius.f90:5
double energy_bxc(const Density &density, const Potential &potential)
TODO doc.
Definition: energy.cpp:94
Contains declaration and partial implementation of sirius::Potential class.