• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

adc-connect / adcc / 13945654759

19 Mar 2025 11:40AM UTC coverage: 73.602% (-0.3%) from 73.946%
13945654759

Pull #169

github

web-flow
Merge 905afe0b9 into f5662a8a2
Pull Request #169: Additional operator integrals and selection of gauge origin

1167 of 1876 branches covered (62.21%)

Branch coverage included in aggregate %.

185 of 288 new or added lines in 12 files covered. (64.24%)

2 existing lines in 2 files now uncovered.

7267 of 9583 relevant lines covered (75.83%)

282621.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.11
/libadcc/pyiface/export_ReferenceState.cc
1
//
2
// Copyright (C) 2018 by the adcc authors
3
//
4
// This file is part of adcc.
5
//
6
// adcc is free software: you can redistribute it and/or modify
7
// it under the terms of the GNU General Public License as published
8
// by the Free Software Foundation, either version 3 of the License, or
9
// (at your option) any later version.
10
//
11
// adcc is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
// GNU General Public License for more details.
15
//
16
// You should have received a copy of the GNU General Public License
17
// along with adcc. If not, see <http://www.gnu.org/licenses/>.
18
//
19

20
#include "../ReferenceState.hh"
21
#include "hartree_fock_solution_hack.hh"
22
#include <pybind11/numpy.h>
23
#include <pybind11/pybind11.h>
24
#include <pybind11/stl.h>
25

26
namespace libadcc {
27
namespace py = pybind11;
28

29
py::object convert_timer(const Timer& timer) {
354✔
30
  // Determine the shift between the C++ and the python clocks
31
  py::object pynow         = py::module::import("time").attr("perf_counter");
354✔
32
  const double clock_shift = pynow().cast<double>() - Timer::now();
354✔
33

34
  // Shift the data and convert to python
35
  py::dict shifted_intervals;
354✔
36
  for (const auto& kv : timer.intervals) {
4,320✔
37
    py::list intlist;
3,966✔
38
    for (const auto& p : kv.second) {
7,932✔
39
      intlist.append(py::make_tuple(clock_shift + p.first, clock_shift + p.second));
3,966✔
40
    }
41
    shifted_intervals[py::cast(kv.first)] = intlist;
3,966✔
42
  }
3,966✔
43
  py::dict shifted_start_times;
354✔
44
  for (const auto& kv : timer.start_times) {
354✔
45
    shifted_start_times[py::cast(kv.first)] = kv.second + clock_shift;
×
46
  }
47

48
  py::object pyTimer            = py::module::import("adcc.timings").attr("Timer");
354✔
49
  py::object ret                = pyTimer();
354✔
50
  ret.attr("raw_data")          = shifted_intervals;
354✔
51
  ret.attr("start_times")       = shifted_start_times;
354✔
52
  ret.attr("time_construction") = timer.time_construction + clock_shift;
354✔
53
  return ret;
708✔
54
}
354✔
55

56
void export_ReferenceState(py::module& m) {
7✔
57

58
  py::class_<ReferenceState, std::shared_ptr<ReferenceState>>(
7✔
59
        m, "ReferenceState",
60
        "Class representing information about the reference state for adcc. Python "
61
        "binding to"
62
        ":cpp:class:`libadcc::ReferenceState`.")
63
        .def(py::init<std::shared_ptr<const HartreeFockSolution_i>,
7✔
64
                      std::shared_ptr<const MoSpaces>, bool>(),
7✔
65
             "Setup a ReferenceStateject using an MoSpaces object.\n"
66
             "\n"
67
             "hfsoln_ptr        Pointer to the Interface to the host program,\n"
68
             "                  providing the HartreeFockSolution data, which\n"
69
             "                  will be provided by this object.\n"
70
             "mo_ptr            MoSpaces object containing info about the MoSpace setup\n"
71
             "                  and the point group symmetry.\n"
72
             "symmetry_check_on_import\n"
73
             "                  Should symmetry of the imported objects be checked\n"
74
             "                  explicitly during the import process. This massively "
75
             "slows\n"
76
             "                  down the import process and has a dramatic impact on "
77
             "memory\n"
78
             "                  usage and should thus only be used for debugging import "
79
             "routines\n"
80
             "                  from the host programs. Do not enable this unless you "
81
             "know\n"
82
             "                  that you really want to.\n")
83
        .def_property_readonly("restricted", &ReferenceState::restricted,
7✔
84
                               "Return whether the reference is restricted or not.")
85
        .def_property_readonly(
14✔
86
              "spin_multiplicity", &ReferenceState::spin_multiplicity,
7✔
87
              "Return the spin multiplicity of the reference state. 0 indicates "
88
              "that the spin cannot be determined or is not integer (e.g. UHF)")
89
        .def_property_readonly("has_core_occupied_space",
14✔
90
                               &ReferenceState::has_core_occupied_space,
7✔
91
                               "Is a core occupied space setup, such that a core-valence "
92
                               "separation can be applied.")
93
        .def_property_readonly("irreducible_representation",
14✔
94
                               &ReferenceState::irreducible_representation,
7✔
95
                               "Reference state irreducible representation")
96
        .def_property_readonly("mospaces", &ReferenceState::mospaces_ptr,
7✔
97
                               "The MoSpaces object supplied on initialisation")
98
        .def_property_readonly(
14✔
99
              "backend", &ReferenceState::backend,
7✔
100
              "The identifier of the back end used for the SCF calculation.")
101
        .def_property_readonly("n_orbs", &ReferenceState::n_orbs,
7✔
102
                               "Number of molecular orbitals")
103
        .def_property_readonly("n_orbs_alpha", &ReferenceState::n_orbs_alpha,
7✔
104
                               "Number of alpha orbitals")
105
        .def_property_readonly("n_orbs_beta", &ReferenceState::n_orbs_beta,
7✔
106
                               "Number of beta orbitals")
107
        .def_property_readonly("n_alpha", &ReferenceState::n_alpha,
7✔
108
                               "Number of alpha electrons")
109
        .def_property_readonly("n_beta", &ReferenceState::n_beta,
7✔
110
                               "Number of beta electrons")
111
        .def_property_readonly(
7✔
112
              "nuclear_total_charge",
113
              [](const ReferenceState& ref) { return ref.nuclear_multipole(0)[0]; })
114✔
114
        .def_property_readonly("nuclear_dipole",
7✔
115
                               [](const ReferenceState& ref) {
×
116
                                 py::array_t<scalar_type> ret(std::vector<ssize_t>{3});
864✔
117
                                 auto res = ref.nuclear_multipole(1);
432✔
118
                                 std::copy(res.begin(), res.end(), ret.mutable_data());
432✔
119
                                 return ret;
864✔
120
                               })
432✔
121
        .def("nuclear_quadrupole",
7✔
NEW
122
             [](const ReferenceState& ref, std::array<scalar_type, 3> gauge_origin) {
×
123
               py::array_t<scalar_type> ret(std::vector<ssize_t>{6});
324✔
124
               auto res = ref.nuclear_multipole(2, gauge_origin);
162✔
125
               std::copy(res.begin(), res.end(), ret.mutable_data());
162✔
126
               return ret;
324✔
127
             })
162✔
128
        .def("gauge_origin_to_xyz",
7✔
NEW
129
             [](const ReferenceState& ref, std::string gauge_origin) {
×
130
               auto vec = ref.gauge_origin_to_xyz(gauge_origin);
162✔
131
               // Make sure a tuple is returned
132
               return py::make_tuple(vec[0], vec[1], vec[2]);
324✔
133
             })
134
        .def_property_readonly("conv_tol", &ReferenceState::conv_tol,
7✔
135
                               "SCF convergence tolererance")
136
        .def_property_readonly("energy_scf", &ReferenceState::energy_scf,
7✔
137
                               "Final total SCF energy")
138
        //
139
        .def("orbital_energies", &ReferenceState::orbital_energies,
7✔
140
             "Return the orbital energies corresponding to the provided space")
141
        .def("orbital_coefficients", &ReferenceState::orbital_coefficients,
7✔
142
             "Return the molecular orbital coefficients corresponding to the provided "
143
             "space (alpha and beta coefficients are returned)")
144
        .def("orbital_coefficients_alpha", &ReferenceState::orbital_coefficients_alpha,
7✔
145
             "Return the alpha molecular orbital coefficients corresponding to the "
146
             "provided space")
147
        .def("orbital_coefficients_beta", &ReferenceState::orbital_coefficients_beta,
7✔
148
             "Return the beta molecular orbital coefficients corresponding to the "
149
             "provided space")
150
        .def("fock", &ReferenceState::fock,
7✔
151
             "Return the Fock matrix block corresponding to the provided space.")
152
        .def("eri", &ReferenceState::eri,
7✔
153
             "Return the ERI (electron-repulsion integrals) tensor block corresponding "
154
             "to the provided space.")
155
        //
156
        .def("import_all", &ReferenceState::import_all,
7✔
157
             "Normally the class only imports the Fock matrix blocks and "
158
             "electron-repulsion integrals of a particular space combination when this "
159
             "is requested by a call to above fock() or eri() functions. This function "
160
             "call, however, instructs the class to immediately import *all* such "
161
             "blocks. Typically you do not want to do this.")
162
        .def_property("cached_fock_blocks", &ReferenceState::cached_fock_blocks,
×
163
                      &ReferenceState::set_cached_fock_blocks,
7✔
164
                      "Get or set the list of momentarily cached Fock matrix blocks\n"
165
                      "\n"
166
                      "Setting this property allows to drop fock matrix blocks if they "
167
                      "are no longer needed to save memory.")
168
        .def_property("cached_eri_blocks", &ReferenceState::cached_eri_blocks,
×
169
                      &ReferenceState::set_cached_eri_blocks,
7✔
170
                      "Get or set the list of momentarily cached ERI tensor blocks\n"
171
                      "\n"
172
                      "Setting this property allows to drop ERI tensor blocks if they "
173
                      "are no longer needed to save memory.")
174
        .def("flush_hf_cache", &ReferenceState::flush_hf_cache,
7✔
175
             "Tell the contained HartreeFockSolution_i object (which was passed upon "
176
             "construction), that a larger amount of import operations is done and that "
177
             "the next request for further imports will most likely take some time, such "
178
             "that intermediate caches can now be flushed to save some memory or other "
179
             "resources.")
180
        .def_property_readonly(
7✔
181
              "timer",
182
              [](const ReferenceState& self) { return convert_timer(self.timer()); },
361✔
183
              "Obtain the timer object of this class.")
184
        //
185
        ;
186
}
7✔
187

188
}  // namespace libadcc
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc