SIRIUS 7.5.0
Electronic structure library and applications
ultrasoft_precond.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 Simon Pintarelli, 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#ifndef __ULTRASOFT_PRECOND_HPP__
21#define __ULTRASOFT_PRECOND_HPP__
22
23#include "adaptor.hpp"
26#include <nlcglib/interface.hpp>
28#include "preconditioner/ultrasoft_precond_k.hpp"
29#include "adaptor.hpp"
30#include <stdexcept>
31#include "SDDK/memory.hpp"
32#include <memory>
33#include <complex>
34
35#ifdef SIRIUS_NLCGLIB
36
37namespace sirius {
38
39class UltrasoftPrecond : public nlcglib::UltrasoftPrecondBase
40{
41 private:
42 using key_t = std::pair<int, int>;
43 using numeric_t = std::complex<double>;
44 using op_t = Ultrasoft_preconditioner<numeric_t>;
45
46 public:
47 using buffer_t = nlcglib::MatrixBaseZ::buffer_t;
48
49 public:
50 UltrasoftPrecond(const K_point_set& kset, Simulation_context& ctx, const Q_operator<double>& q_op);
51
52 virtual void apply(const key_t& key, buffer_t& out, buffer_t& in) const override;
53 virtual std::vector<std::pair<int, int>> get_keys() const override;
54
55 private:
56 std::map<key_t, std::shared_ptr<op_t>> data_;
57};
58
59inline UltrasoftPrecond::UltrasoftPrecond(K_point_set const& kset, Simulation_context& ctx,
60 Q_operator<double> const& q_op)
61{
62 for (auto it : kset.spl_num_kpoints()) {
63 auto& kp = *kset.get<double>(it.i);
64 for (int ispn = 0; ispn < ctx.num_spins(); ++ispn) {
65 key_t key{it.i.get(), ispn};
66 data_[key] = std::make_shared<op_t>(ctx, q_op, ispn, kp.beta_projectors(), kp.gkvec());
67 }
68 }
69}
70
71inline void
72UltrasoftPrecond::apply(const key_t& key, buffer_t& out, buffer_t& in) const
73{
74 auto& op = data_.at(key);
75 auto array_out = make_matrix_view(out);
76 auto array_in = make_matrix_view(in);
77 sddk::memory_t pm = out.memtype == nlcglib::memory_type::host ? sddk::memory_t::host : sddk::memory_t::device;
78 op->apply(array_out, array_in, pm);
79}
80
81inline std::vector<std::pair<int, int>>
82UltrasoftPrecond::get_keys() const
83{
84 std::vector<key_t> keys;
85 for (auto& elem : data_) {
86 keys.push_back(elem.first);
87 }
88 return keys;
89}
90
91} // namespace sirius
92
93#endif /* SIRIUS_NLCGLIB */
94#endif /* __ULTRASOFT_PRECOND_HPP__ */
Contains defintion of nlcglib interface.
Contains declaration and partial implementation of sirius::K_point_set class.
Memory management functions and classes.
@ key
the parser read a key of a value in an object
op_t
Tyoe of MPI reduction.
Namespace of the SIRIUS library.
Definition: sirius.f90:5
Contains declaration of sirius::Non_local_operator class.
Contains definition and implementation of Simulation_context class.