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

adc-connect / adcc / 13899231401

17 Mar 2025 12:18PM UTC coverage: 73.381% (-0.3%) from 73.683%
13899231401

Pull #169

github

web-flow
Merge 18bd3ac28 into cc4761121
Pull Request #169: Additional operator integrals and selection of gauge origin

1122 of 1830 branches covered (61.31%)

Branch coverage included in aggregate %.

180 of 277 new or added lines in 12 files covered. (64.98%)

12 existing lines in 2 files now uncovered.

7217 of 9534 relevant lines covered (75.7%)

217460.4 hits per line

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

91.25
/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) {
236✔
30
  // Determine the shift between the C++ and the python clocks
31
  py::object pynow         = py::module::import("time").attr("perf_counter");
236✔
32
  const double clock_shift = pynow().cast<double>() - Timer::now();
236✔
33

34
  // Shift the data and convert to python
35
  py::dict shifted_intervals;
236✔
36
  for (const auto& kv : timer.intervals) {
2,880✔
37
    py::list intlist;
2,644✔
38
    for (const auto& p : kv.second) {
5,288✔
39
      intlist.append(py::make_tuple(clock_shift + p.first, clock_shift + p.second));
2,644✔
40
    }
41
    shifted_intervals[py::cast(kv.first)] = intlist;
2,644✔
42
  }
2,644✔
43
  py::dict shifted_start_times;
236✔
44
  for (const auto& kv : timer.start_times) {
236✔
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");
236✔
49
  py::object ret                = pyTimer();
236✔
50
  ret.attr("raw_data")          = shifted_intervals;
236✔
51
  ret.attr("start_times")       = shifted_start_times;
236✔
52
  ret.attr("time_construction") = timer.time_construction + clock_shift;
236✔
53
  return ret;
472✔
54
}
236✔
55

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

58
  py::class_<ReferenceState, std::shared_ptr<ReferenceState>>(
5✔
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>,
5✔
64
                      std::shared_ptr<const MoSpaces>, bool>(),
5✔
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,
5✔
84
                               "Return whether the reference is restricted or not.")
85
        .def_property_readonly(
10✔
86
              "spin_multiplicity", &ReferenceState::spin_multiplicity,
5✔
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",
10✔
90
                               &ReferenceState::has_core_occupied_space,
5✔
91
                               "Is a core occupied space setup, such that a core-valence "
92
                               "separation can be applied.")
93
        .def_property_readonly("irreducible_representation",
10✔
94
                               &ReferenceState::irreducible_representation,
5✔
95
                               "Reference state irreducible representation")
96
        .def_property_readonly("mospaces", &ReferenceState::mospaces_ptr,
5✔
97
                               "The MoSpaces object supplied on initialisation")
98
        .def_property_readonly(
10✔
99
              "backend", &ReferenceState::backend,
5✔
100
              "The identifier of the back end used for the SCF calculation.")
101
        .def_property_readonly("n_orbs", &ReferenceState::n_orbs,
5✔
102
                               "Number of molecular orbitals")
103
        .def_property_readonly("n_orbs_alpha", &ReferenceState::n_orbs_alpha,
5✔
104
                               "Number of alpha orbitals")
105
        .def_property_readonly("n_orbs_beta", &ReferenceState::n_orbs_beta,
5✔
106
                               "Number of beta orbitals")
107
        .def_property_readonly("n_alpha", &ReferenceState::n_alpha,
5✔
108
                               "Number of alpha electrons")
109
        .def_property_readonly("n_beta", &ReferenceState::n_beta,
5✔
110
                               "Number of beta electrons")
111
        .def_property_readonly(
5✔
112
              "nuclear_total_charge",
113
              [](const ReferenceState& ref) { return ref.nuclear_multipole(0)[0]; })
76✔
114
        .def_property_readonly("nuclear_dipole",
5✔
115
                               [](const ReferenceState& ref) {
×
116
                                 py::array_t<scalar_type> ret(std::vector<ssize_t>{3});
576✔
117
                                 auto res = ref.nuclear_multipole(1);
288✔
118
                                 std::copy(res.begin(), res.end(), ret.mutable_data());
288✔
119
                                 return res;
576✔
120
                               })
288✔
121
        .def("nuclear_quadrupole",
5✔
NEW
122
             [](const ReferenceState& ref, std::array<scalar_type, 3> gauge_origin) {
×
123
               py::array_t<scalar_type> ret(std::vector<ssize_t>{6});
216✔
124
               auto res = ref.nuclear_multipole(2, gauge_origin);
108✔
125
               std::copy(res.begin(), res.end(), ret.mutable_data());
108✔
126
               return res;
216✔
127
             })
108✔
128
        .def("gauge_origin_to_xyz",
5✔
NEW
129
             [](const ReferenceState& ref, std::string gauge_origin) {
×
130
               auto vec = ref.gauge_origin_to_xyz(gauge_origin);
108✔
131
               py::tuple coords(3);
108✔
132
               for (size_t i = 0; i < vec.size(); ++i) {
540✔
133
                 coords[i] = vec[i];  // Assign values to the tuple
324✔
134
               }
135
               return coords;
216✔
NEW
136
             })
×
137
        .def_property_readonly("conv_tol", &ReferenceState::conv_tol,
5✔
138
                               "SCF convergence tolererance")
139
        .def_property_readonly("energy_scf", &ReferenceState::energy_scf,
5✔
140
                               "Final total SCF energy")
141
        //
142
        .def("orbital_energies", &ReferenceState::orbital_energies,
5✔
143
             "Return the orbital energies corresponding to the provided space")
144
        .def("orbital_coefficients", &ReferenceState::orbital_coefficients,
5✔
145
             "Return the molecular orbital coefficients corresponding to the provided "
146
             "space (alpha and beta coefficients are returned)")
147
        .def("orbital_coefficients_alpha", &ReferenceState::orbital_coefficients_alpha,
5✔
148
             "Return the alpha molecular orbital coefficients corresponding to the "
149
             "provided space")
150
        .def("orbital_coefficients_beta", &ReferenceState::orbital_coefficients_beta,
5✔
151
             "Return the beta molecular orbital coefficients corresponding to the "
152
             "provided space")
153
        .def("fock", &ReferenceState::fock,
5✔
154
             "Return the Fock matrix block corresponding to the provided space.")
155
        .def("eri", &ReferenceState::eri,
5✔
156
             "Return the ERI (electron-repulsion integrals) tensor block corresponding "
157
             "to the provided space.")
158
        //
159
        .def("import_all", &ReferenceState::import_all,
5✔
160
             "Normally the class only imports the Fock matrix blocks and "
161
             "electron-repulsion integrals of a particular space combination when this "
162
             "is requested by a call to above fock() or eri() functions. This function "
163
             "call, however, instructs the class to immediately import *all* such "
164
             "blocks. Typically you do not want to do this.")
165
        .def_property("cached_fock_blocks", &ReferenceState::cached_fock_blocks,
×
166
                      &ReferenceState::set_cached_fock_blocks,
5✔
167
                      "Get or set the list of momentarily cached Fock matrix blocks\n"
168
                      "\n"
169
                      "Setting this property allows to drop fock matrix blocks if they "
170
                      "are no longer needed to save memory.")
171
        .def_property("cached_eri_blocks", &ReferenceState::cached_eri_blocks,
×
172
                      &ReferenceState::set_cached_eri_blocks,
5✔
173
                      "Get or set the list of momentarily cached ERI tensor blocks\n"
174
                      "\n"
175
                      "Setting this property allows to drop ERI tensor blocks if they "
176
                      "are no longer needed to save memory.")
177
        .def("flush_hf_cache", &ReferenceState::flush_hf_cache,
5✔
178
             "Tell the contained HartreeFockSolution_i object (which was passed upon "
179
             "construction), that a larger amount of import operations is done and that "
180
             "the next request for further imports will most likely take some time, such "
181
             "that intermediate caches can now be flushed to save some memory or other "
182
             "resources.")
183
        .def_property_readonly(
5✔
184
              "timer",
185
              [](const ReferenceState& self) { return convert_timer(self.timer()); },
241✔
186
              "Obtain the timer object of this class.")
187
        //
188
        ;
189
}
5✔
190

191
}  // 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