TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
lattice.hpp
1// Copyright (c) 2022-2023 Simons Foundation
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You may obtain a copy of the License at
14// https://www.gnu.org/licenses/gpl-3.0.txt
15//
16// Authors: Nils Wentzell
17
18#pragma once
19
21
22#include <c2py/converters/basic_types.hpp>
23#include <c2py/converters/stl/array.hpp>
24#include <c2py/converters/wrapped.hpp>
25#include <c2py/py_converter.hpp>
26#include <c2py/pyref.hpp>
27#include <Python.h>
28
29#include <array>
30#include <iostream>
31#include <string>
32
33namespace c2py {
34
35 // -----------------------------------
36 // bravais_lattice::point_t
37 // -----------------------------------
38
39 template <> struct py_converter<triqs::lattice::bravais_lattice::point_t> {
40 using c_t = triqs::lattice::bravais_lattice::point_t;
41
42 static constexpr const char *tp_name = "LatticePoint";
43
44 static PyObject *c2py(c_t const &x) {
45 pyref cls = pyref::get_class("triqs.lattice", "LatticePoint", true);
46 if (cls.is_null()) return NULL;
47
48 pyref kw = PyDict_New();
49
50 pyref index = cxx2py(x.index());
51 if (index.is_null()) return NULL;
52 pyref lattice = cxx2py(x.lattice());
53 if (lattice.is_null()) return NULL;
54 PyDict_SetItemString(kw, "index", index);
55 PyDict_SetItemString(kw, "lattice", lattice);
56
57 pyref empty_tuple = PyTuple_New(0);
58 return PyObject_Call(cls, empty_tuple, kw);
59 }
60
61 static bool is_convertible(PyObject *ob, bool raise_exception) {
62 pyref cls = pyref::get_class("triqs.lattice", "LatticePoint", true);
63 if (not pyref::check_is_instance(ob, cls, raise_exception)) return false;
64 return true;
65 }
66
67 // ----------------------------------------------
68
69 using lattice_py_type = struct {
70 PyObject_HEAD;
71 triqs::lattice::bravais_lattice *_c;
72 };
73
74 static c_t py2c(PyObject *ob) {
75
76 pyref x = pyref::borrowed(ob);
77 pyref index = x.attr("index");
78 pyref lattice = x.attr("lattice");
79 auto *lattice_c_ptr = reinterpret_cast<lattice_py_type *>(static_cast<PyObject *>(lattice))->_c;
80 if (lattice_c_ptr == NULL) {
81 std::cerr << "Severe internal error : lattice_ptr is null in py2c\n";
82 std::terminate();
83 }
84
85 return c_t{py2cxx<std::array<long, 3>>(index), lattice_c_ptr};
86 }
87 };
88
89} // namespace c2py
Provides a Bravais lattice class.
Additional converters for gf.