Line data Source code
1 : // SPDX-FileCopyrightText: 2024 PairInteraction Developers 2 : // SPDX-License-Identifier: LGPL-3.0-or-later 3 : 4 : #include "./Ket.py.hpp" 5 : 6 : #include "pairinteraction/basis/BasisAtom.hpp" 7 : #include "pairinteraction/database/Database.hpp" 8 : #include "pairinteraction/ket/Ket.hpp" 9 : #include "pairinteraction/ket/KetAtom.hpp" 10 : #include "pairinteraction/ket/KetAtomCreator.hpp" 11 : #include "pairinteraction/ket/KetNotUniqueError.hpp" 12 : #include "pairinteraction/ket/KetPair.hpp" 13 : #include "pairinteraction/utils/traits.hpp" 14 : 15 : #include <complex> 16 : #include <nanobind/nanobind.h> 17 : #include <nanobind/operators.h> 18 : #include <nanobind/stl/shared_ptr.h> 19 : #include <nanobind/stl/string.h> 20 : #include <nanobind/stl/vector.h> 21 : 22 : namespace nb = nanobind; 23 : using namespace pairinteraction; 24 : 25 2 : static void declare_ket(nb::module_ &m) { 26 2 : std::string pyclass_name = "Ket"; 27 2 : nb::class_<Ket> pyclass(m, pyclass_name.c_str()); 28 2 : pyclass.def("get_energy", &Ket::get_energy); 29 2 : } 30 : 31 2 : static void declare_ket_atom(nb::module_ &m) { 32 2 : std::string pyclass_name = "KetAtom"; 33 2 : nb::class_<KetAtom, Ket> pyclass(m, pyclass_name.c_str()); 34 2 : pyclass.def("get_database", &KetAtom::get_database, nb::rv_policy::reference) 35 2 : .def("get_species", &KetAtom::get_species) 36 2 : .def("get_quantum_number", &KetAtom::get_quantum_number) 37 2 : .def("get_quantum_number_std", &KetAtom::get_quantum_number_std) 38 2 : .def(nb::self == nb::self) // NOLINT(misc-redundant-expression) 39 4164 : .def("__hash__", [](const KetAtom &self) { return KetAtom::hash{}(self); }); 40 2 : } 41 : 42 2 : static void declare_ket_atom_creator(nb::module_ &m) { 43 2 : std::string pyclass_name = "KetAtomCreator"; 44 4 : nb::class_<KetAtomCreator> pyclass(m, pyclass_name.c_str()); 45 2 : pyclass.def(nb::init<>()) 46 2 : .def(nb::init<std::string, int, double, double, double>()) 47 2 : .def("set_species", &KetAtomCreator::set_species) 48 2 : .def("set_energy", &KetAtomCreator::set_energy) 49 2 : .def("set_quantum_number", &KetAtomCreator::set_quantum_number) 50 4 : .def("create", &KetAtomCreator::create); 51 2 : } 52 : 53 : template <typename T> 54 4 : static void declare_ket_pair(nb::module_ &m, std::string const &type_name) { 55 4 : std::string pyclass_name = "KetPair" + type_name; 56 4 : nb::class_<KetPair<T>, Ket> pyclass(m, pyclass_name.c_str()); 57 4 : pyclass.def("get_atomic_states", &KetPair<T>::get_atomic_states) 58 4 : .def("get_quantum_number_m", &KetPair<T>::get_quantum_number_m) 59 4 : .def(nb::self == nb::self) // NOLINT(misc-redundant-expression) 60 228 : .def("__hash__", [](const KetPair<T> &self) { return typename KetPair<T>::hash{}(self); }); 61 4 : } 62 : 63 2 : static void declare_ket_not_unique_error(nb::module_ &m) { 64 2 : static nb::object exc_type = nb::exception<KetNotUniqueError>(m, "KetNotUniqueError"); 65 2 : nb::register_exception_translator( 66 12 : [](const std::exception_ptr &p, void *payload) { 67 : try { 68 20 : std::rethrow_exception(p); 69 10 : } catch (const KetNotUniqueError &e) { 70 0 : auto *type = static_cast<PyObject *>(payload); 71 0 : nb::object exc = nb::borrow(type)(e.what()); 72 0 : exc.attr("kets") = nb::cast(e.get_kets()); 73 0 : PyErr_SetObject(type, exc.ptr()); 74 0 : } 75 0 : }, 76 2 : exc_type.ptr()); 77 2 : } 78 : 79 2 : void bind_ket(nb::module_ &m) { 80 2 : declare_ket(m); 81 2 : declare_ket_atom(m); 82 2 : declare_ket_atom_creator(m); 83 2 : declare_ket_pair<double>(m, "Real"); 84 2 : declare_ket_pair<std::complex<double>>(m, "Complex"); 85 2 : declare_ket_not_unique_error(m); 86 2 : }