Loading...
Searching...
No Matches
q3_core_module.cpp
Go to the documentation of this file.
1/**
2 * q3_core — nanobind Python extension module.
3 *
4 * Exposes run_scf() and run_nscf() as Python callables that invoke the Q3
5 * DFT engine in-process (Mode B) rather than via subprocess (Mode A).
6 *
7 * Usage from Python::
8 *
9 * import q3_core
10 *
11 * # Returns a JSON string (same schema as results.json)
12 * result_json = q3_core.run_scf("path/to/input.yml")
13 * result_json = q3_core.run_nscf("path/to/nscf_input.yml")
14 *
15 * Thread safety
16 * -------------
17 * The module releases the GIL during C++ computation (``nb::call_guard``
18 * is NOT used here because Q3 is not thread-safe; GIL is held throughout
19 * the C++ call to prevent concurrent invocations).
20 */
21
22#include <stdexcept>
23#include <string>
24#include <vector>
25
26#include <nanobind/nanobind.h>
27#include <nanobind/stl/string.h>
28#include <nanobind/stl/vector.h>
29
32
33namespace nb = nanobind;
34
35NB_MODULE(q3_core, module) {
37 module.doc() = R"doc(
38 q3_core — In-process Q3 DFT engine (Mode B).
39
40 Functions
41 ---------
42 run_scf(input_path, write_output_files=True) -> str
43 Run a self-consistent field calculation. Returns a JSON string.
44
45 run_nscf(input_path, write_output_files=True) -> str
46 Run a non-self-consistent field calculation. Returns a JSON string.
47
48 Notes
49 -----
50 Call reset_counters() between successive calls if profiling metrics
51 are needed per-invocation (run_scf/run_nscf reset automatically).
52 )doc";
53
54 module.def(
55 "run_scf",
56 [](const std::string& input_path, bool write_output_files) -> std::string {
57 return q3::bindings::run_scf(input_path, write_output_files);
58 },
59 nb::arg("input_path"),
60 nb::arg("write_output_files") = true,
61 R"doc(
62 Run a self-consistent field calculation in-process.
63
64 Parameters
65 ----------
66 input_path : str
67 Path to a Q3 YAML input file (calculation = scf).
68 write_output_files : bool, optional
69 If True (default), write results.json and checkpoint files.
70
71 Returns
72 -------
73 str
74 JSON string with the results (same schema as results.json).
75
76 Raises
77 ------
78 RuntimeError
79 If SCF fails or the input is invalid.
80 )doc");
81
82 module.def(
83 "run_nscf",
84 [](const std::string& input_path, bool write_output_files) -> std::string {
85 return q3::bindings::run_nscf(input_path, write_output_files);
86 },
87 nb::arg("input_path"),
88 nb::arg("write_output_files") = true,
89 R"doc(
90 Run a non-self-consistent field calculation in-process.
91
92 Parameters
93 ----------
94 input_path : str
95 Path to a Q3 YAML input file (calculation = nscf) with
96 kpoints_list specified.
97 write_output_files : bool, optional
98 If True (default), write results.json.
99
100 Returns
101 -------
102 str
103 JSON string with eigenvalues at each requested k-point.
104
105 Raises
106 ------
107 RuntimeError
108 If a valid SCF checkpoint is not found.
109 )doc");
110
111 module.def(
112 "reset_counters",
113 []() { q3::ProfilingCounters::instance().reset(); },
114 R"doc(
115 Reset all internal profiling counters.
116
117 run_scf() and run_nscf() call this automatically before each
118 invocation. Call manually only if you need to reset counters
119 mid-run (unusual).
120 )doc");
121
122 nb::class_<q3::bindings::SCFSolver>(module, "SCFSolver")
123 .def(nb::init<const std::string&>(), nb::arg("input_path"))
124 .def("solve",
126 nb::arg("coordinates"),
127 nb::arg("write_output_files") = true,
128 R"doc(
129 Solve the SCF problem for the given atomic coordinates.
130
131 Parameters
132 ----------
133 coordinates : list of list of float
134 Atomic coordinates (crystal/fractional) for all atoms.
135 write_output_files : bool, optional
136 If True (default), write results.json and checkpoint files.
137
138 Returns
139 -------
140 str
141 JSON string with the results.
142 )doc");
143}
std::string solve(const std::vector< std::vector< double > > &coordinates, bool write_output_files=true)
void register_all()
No add-on is compiled in, so there is nothing to register.
NB_MODULE(q3_core, module)