Repository navigation
Expand file tree
/
Copy path_alm.cpp
More file actions
391 lines (366 loc) · 20.2 KB
/
Copy path_alm.cpp
File metadata and controls
391 lines (366 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// _alm.cpp -- nanobind binding for the ALAMODE 2.0 ALM C++ class.
//
// Fresh binding (no legacy C wrapper / Cython): nanobind holds the ALM_NS::ALM object directly.
// Chosen over pybind11 for smaller/faster output and STABLE_ABI (abi3) wheels. Raw double*/int*
// C++ methods are wrapped with lambdas that size outputs from ALM's own count getters and return
// capsule-owned NumPy arrays.
//
// Build: see python/CMakeLists.txt (nanobind_add_module + the 2.0 alm sources).
#include <nanobind/nanobind.h>
#include <nanobind/ndarray.h>
#include <nanobind/stl/map.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/vector.h>
#include <cstddef>
#include <stdexcept>
#include <string>
#include <vector>
#include "alm.h"
#include "fatal_error.h"
#include "optimize.h"
namespace nb = nanobind;
using ALM_NS::ALM;
using ALM_NS::OptimizerControl;
// Contiguous CPU input views (the Python layer guarantees dtype/order, so require exact types).
using f64in = nb::ndarray<const double, nb::c_contig, nb::device::cpu>;
using i32in = nb::ndarray<const int, nb::c_contig, nb::device::cpu>;
// Allocate a 1-D NumPy array of length n, owned by a capsule; hand back the raw pointer to fill.
template <typename T>
static nb::ndarray<nb::numpy, T> make_owned(std::size_t n, T **out)
{
T *data = new T[n]();
*out = data;
nb::capsule owner(data, [](void *p) noexcept { delete[] static_cast<T *>(p); });
return nb::ndarray<nb::numpy, T>(data, {n}, owner);
}
NB_MODULE(_alm, m)
{
m.doc() = "nanobind binding for the ALAMODE 2.0 ALM library";
// ALM input errors raise alamode::Error (-> RuntimeError) instead of calling
// std::exit. The flag lives in this module only (see include/fatal_error.h).
alamode::throw_on_error = true;
// ---------------- OptimizerControl ----------------
nb::class_<OptimizerControl>(m, "OptimizerControl")
.def(nb::init<>())
.def_rw("linear_model", &OptimizerControl::linear_model)
.def_rw("use_sparse_solver", &OptimizerControl::use_sparse_solver)
.def_rw("sparsesolver", &OptimizerControl::sparsesolver)
.def_rw("use_cholesky", &OptimizerControl::use_cholesky)
.def_rw("chunk_size", &OptimizerControl::chunk_size)
.def_rw("maxnum_iteration", &OptimizerControl::maxnum_iteration)
.def_rw("tolerance_iteration", &OptimizerControl::tolerance_iteration)
.def_rw("output_frequency", &OptimizerControl::output_frequency)
.def_rw("standardize", &OptimizerControl::standardize)
.def_rw("displacement_normalization_factor", &OptimizerControl::displacement_normalization_factor)
.def_rw("debiase_after_l1opt", &OptimizerControl::debiase_after_l1opt)
.def_rw("l1_solver", &OptimizerControl::l1_solver)
.def_rw("efit_weight", &OptimizerControl::efit_weight)
.def_rw("efit_escale", &OptimizerControl::efit_escale)
.def_rw("efit_cv", &OptimizerControl::efit_cv)
.def_rw("cross_validation", &OptimizerControl::cross_validation)
.def_rw("l1_alpha", &OptimizerControl::l1_alpha)
.def_rw("l1_alpha_min", &OptimizerControl::l1_alpha_min)
.def_rw("l1_alpha_max", &OptimizerControl::l1_alpha_max)
.def_rw("l1_alpha_min_ratio", &OptimizerControl::l1_alpha_min_ratio)
.def_rw("num_l1_alpha", &OptimizerControl::num_l1_alpha)
.def_rw("l1_ratio", &OptimizerControl::l1_ratio)
.def_rw("save_solution_path", &OptimizerControl::save_solution_path)
.def_rw("stop_criterion", &OptimizerControl::stop_criterion)
.def_rw("periodic_image_conv", &OptimizerControl::periodic_image_conv);
// ---------------- ALM ----------------
nb::class_<ALM>(m, "ALMCore")
.def(nb::init<>())
// -- verbosity / output ------------------------------------------------
.def("set_verbosity", &ALM::set_verbosity)
.def("get_verbosity", &ALM::get_verbosity)
.def("set_output_filename_prefix", &ALM::set_output_filename_prefix)
.def("set_print_symmetry", &ALM::set_print_symmetry)
.def("set_symmetry_tolerance", &ALM::set_symmetry_tolerance)
.def("get_symmetry_tolerance", &ALM::get_symmetry_tolerance)
// -- units ---------------------------------------------------------------
// Must be called before set_cell / set_u_train / set_f_train / define;
// those setters convert their input into the internal units (bohr, Ry/bohr).
.def("set_input_units", &ALM::set_input_units, nb::arg("length_unit"), nb::arg("force_unit"))
.def("get_input_units", &ALM::get_input_units)
.def("set_fcs_unit_output", &ALM::set_fcs_unit_output)
.def("get_fcs_unit_output", &ALM::get_fcs_unit_output)
// -- cell --------------------------------------------------------------
.def(
"set_cell",
[](ALM &self, f64in lavec, f64in xcoord, i32in kind) {
if (lavec.ndim() != 2 || lavec.shape(0) != 3 || lavec.shape(1) != 3)
throw std::invalid_argument("set_cell: lavec must have shape (3, 3)");
if (xcoord.ndim() != 2 || xcoord.shape(1) != 3)
throw std::invalid_argument("set_cell: xcoord must have shape (nat, 3)");
if (kind.ndim() != 1 || kind.shape(0) != xcoord.shape(0))
throw std::invalid_argument("set_cell: kind must have shape (nat,)");
auto nat = static_cast<std::size_t>(xcoord.shape(0));
const auto *lv = reinterpret_cast<const double(*)[3]>(lavec.data());
const auto *xc = reinterpret_cast<const double(*)[3]>(xcoord.data());
self.set_cell(nat, lv, xc, kind.data());
},
nb::arg("lavec").noconvert(),
nb::arg("xcoord").noconvert(),
nb::arg("kind").noconvert())
.def("set_element_names", &ALM::set_element_names)
.def(
"set_transformation_matrices",
[](ALM &self, f64in tsuper, f64in tprim, int autoset_primcell) {
for (auto *a: {&tsuper, &tprim})
if (a->ndim() != 2 || a->shape(0) != 3 || a->shape(1) != 3)
throw std::invalid_argument("set_transformation_matrices: matrices must be (3,3)");
const auto *ts = reinterpret_cast<const double(*)[3]>(tsuper.data());
const auto *tp = reinterpret_cast<const double(*)[3]>(tprim.data());
self.set_transformation_matrices(ts, tp, autoset_primcell);
},
nb::arg("transmat_to_super").noconvert(),
nb::arg("transmat_to_prim").noconvert(),
nb::arg("autoset_primcell") = 0)
.def(
"set_magnetic_params",
[](ALM &self, f64in magmom, bool lspin, int noncollinear, int trev_sym_mag, const std::string &str_magmom) {
if (magmom.ndim() != 2 || magmom.shape(1) != 3)
throw std::invalid_argument("set_magnetic_params: magmom must be (nat, 3)");
const std::size_t nat = magmom.shape(0);
const auto *mm = reinterpret_cast<const double(*)[3]>(magmom.data());
self.set_magnetic_params(nat, mm, lspin, noncollinear, trev_sym_mag, str_magmom);
},
nb::arg("magmom").noconvert(),
nb::arg("lspin") = false,
nb::arg("noncollinear") = 0,
nb::arg("trev_sym_mag") = 1,
nb::arg("str_magmom") = "")
.def(
"set_periodicity",
[](ALM &self, i32in is_periodic) {
if (is_periodic.ndim() != 1 || is_periodic.shape(0) != 3)
throw std::invalid_argument("set_periodicity: expected shape (3,)");
int p[3] = {is_periodic.data()[0], is_periodic.data()[1], is_periodic.data()[2]};
self.set_periodicity(p);
},
nb::arg("is_periodic").noconvert())
// -- training / validation data ---------------------------------------
.def(
"set_u_train",
[](ALM &self, f64in u) {
if (u.ndim() != 2) throw std::invalid_argument("set_u_train: u must be 2-D (nsnap, 3*nat)");
const auto nrow = static_cast<std::size_t>(u.shape(0));
const auto ncol = static_cast<std::size_t>(u.shape(1));
const double *d = u.data();
std::vector<std::vector<double>> uv(nrow, std::vector<double>(ncol));
for (std::size_t i = 0; i < nrow; ++i)
for (std::size_t j = 0; j < ncol; ++j) uv[i][j] = d[i * ncol + j];
self.set_u_train(uv);
},
nb::arg("u").noconvert())
.def(
"set_f_train",
[](ALM &self, f64in f) {
if (f.ndim() != 2) throw std::invalid_argument("set_f_train: f must be 2-D (nsnap, 3*nat)");
const auto nrow = static_cast<std::size_t>(f.shape(0));
const auto ncol = static_cast<std::size_t>(f.shape(1));
const double *d = f.data();
std::vector<std::vector<double>> fv(nrow, std::vector<double>(ncol));
for (std::size_t i = 0; i < nrow; ++i)
for (std::size_t j = 0; j < ncol; ++j) fv[i][j] = d[i * ncol + j];
self.set_f_train(fv);
},
nb::arg("f").noconvert())
.def(
"set_validation_data",
[](ALM &self, f64in u, f64in f) {
auto to_vv = [](const f64in &a) {
if (a.ndim() != 2) throw std::invalid_argument("set_validation_data: u/f must be 2-D");
const auto nr = static_cast<std::size_t>(a.shape(0));
const auto nc = static_cast<std::size_t>(a.shape(1));
const double *d = a.data();
std::vector<std::vector<double>> v(nr, std::vector<double>(nc));
for (std::size_t i = 0; i < nr; ++i)
for (std::size_t j = 0; j < nc; ++j) v[i][j] = d[i * nc + j];
return v;
};
self.set_validation_data(to_vv(u), to_vv(f));
},
nb::arg("u").noconvert(),
nb::arg("f").noconvert())
.def("get_u_train", &ALM::get_u_train)
.def("get_f_train", &ALM::get_f_train)
.def("get_number_of_data", &ALM::get_number_of_data)
.def("get_nrows_sensing_matrix", &ALM::get_nrows_sensing_matrix)
.def("get_number_of_free_parameters", &ALM::get_number_of_free_parameters)
// -- model definition --------------------------------------------------
.def(
"define",
[](ALM &self, int maxorder, std::size_t nkd, i32in nbody_include, f64in cutoff_radii) {
const double *cut = (cutoff_radii.size() > 0) ? cutoff_radii.data() : nullptr;
self.define(maxorder, nkd, nbody_include.data(), cut);
},
nb::arg("maxorder"),
nb::arg("nkd"),
nb::arg("nbody_include").noconvert(),
nb::arg("cutoff_radii").noconvert())
.def("set_forceconstant_basis", &ALM::set_forceconstant_basis)
.def("get_forceconstant_basis", &ALM::get_forceconstant_basis)
.def("init_fc_table", &ALM::init_fc_table)
.def("ready_all_constraints", &ALM::ready_all_constraints)
.def("constraints_initialized", &ALM::constraints_initialized)
.def("get_maxorder", &ALM::get_maxorder)
// -- constraints / fixing ---------------------------------------------
.def("set_constraint_mode", &ALM::set_constraint_mode)
.def("set_algebraic_constraint", &ALM::set_algebraic_constraint)
.def("set_rotation_axis", &ALM::set_rotation_axis)
.def("set_stress", &ALM::set_stress) // STRESS: 9 values (GPa, tension positive, row-major)
.def("set_fc_file", &ALM::set_fc_file) // e.g. FC2FIX: set_fc_file(2, "supercell.h5")
.def("set_fc_fix", &ALM::set_fc_fix)
.def("set_forceconstants_to_fix",
[](ALM &self, const std::vector<std::vector<int>> &intpair_fix, const std::vector<double> &values_fix) {
if (intpair_fix.empty())
throw std::invalid_argument("set_forceconstants_to_fix: intpair_fix is empty");
if (values_fix.size() != intpair_fix.size())
throw std::invalid_argument("set_forceconstants_to_fix: values_fix and "
"intpair_fix must have the same length");
for (const auto &row: intpair_fix)
if (row.size() < 2)
throw std::invalid_argument("set_forceconstants_to_fix: each intpair row "
"must have >= 2 flattened indices");
self.set_forceconstants_to_fix(intpair_fix, values_fix);
})
// -- optimizer control -------------------------------------------------
.def("set_optimizer_control", &ALM::set_optimizer_control)
.def("get_optimizer_control", &ALM::get_optimizer_control)
.def("get_cv_l1_alpha", &ALM::get_cv_l1_alpha)
// -- run (release the GIL: long, OpenMP-parallel, no Python callbacks) -----------------
.def("run_optimize", &ALM::run_optimize, nb::call_guard<nb::gil_scoped_release>())
.def("run_suggest", &ALM::run_suggest, nb::call_guard<nb::gil_scoped_release>())
// -- generic input dict ------------------------------------------------
.def("set_input_vars", [](ALM &self, const std::map<std::string, std::string> &d) { self.set_input_vars(d); })
.def("get_input_var", &ALM::get_input_var)
// -- FC element counts -------------------------------------------------
.def("get_number_of_fc_elements", &ALM::get_number_of_fc_elements)
.def("get_number_of_irred_fc_elements", &ALM::get_number_of_irred_fc_elements)
.def("get_number_of_fc_origin", &ALM::get_number_of_fc_origin)
.def("get_atom_mapping_by_pure_translations", &ALM::get_atom_mapping_by_pure_translations)
// -- sensing matrix A, b ----------------------------------------------
.def("get_matrix_elements",
[](ALM &self) {
if (self.get_number_of_data() == 0)
throw std::runtime_error("get_matrix_elements: training data not set "
"(call set_u_train/set_f_train first)");
const std::size_t nrows = self.get_nrows_sensing_matrix();
// get_matrix_elements returns the algebraic-constraint-reduced (compact) matrix,
// so size by the free-parameter count, not the symmetry-irreducible total.
const std::size_t ncols = self.get_number_of_free_parameters();
double *ap = nullptr, *bp = nullptr;
auto amat = make_owned<double>(nrows * ncols, &ap);
auto bvec = make_owned<double>(nrows, &bp);
{
// Heavy, OpenMP-parallel build into raw buffers: drop the GIL.
nb::gil_scoped_release rel;
self.get_matrix_elements(ap, bp);
}
return nb::make_tuple(amat, bvec, nrows, ncols);
})
// -- get / set FCs -----------------------------------------------------
.def(
"get_fc_origin",
[](ALM &self, int fc_order, int permutation) {
const std::size_t n = self.get_number_of_fc_origin(fc_order, permutation);
double *vp = nullptr;
int *ip = nullptr;
auto vals = make_owned<double>(n, &vp);
auto idx = make_owned<int>(n * (fc_order + 1), &ip);
self.get_fc_origin(vp, ip, fc_order, permutation);
return nb::make_tuple(vals, idx);
},
nb::arg("fc_order"),
nb::arg("permutation") = 1)
.def(
"get_fc_irreducible",
[](ALM &self, int fc_order) {
const std::size_t n = self.get_number_of_irred_fc_elements(fc_order);
double *vp = nullptr;
int *ip = nullptr;
auto vals = make_owned<double>(n, &vp);
auto idx = make_owned<int>(n * (fc_order + 1), &ip);
self.get_fc_irreducible(vp, ip, fc_order);
return nb::make_tuple(vals, idx);
},
nb::arg("fc_order"))
.def(
"get_fc_all",
[](ALM &self, int fc_order, int permutation) {
// map_p2s has shape (nat_primitive, ntran); get_fc_all replicates the origin set
// over the ntran pure translations -> use the inner dimension.
const auto &map_p2s = self.get_atom_mapping_by_pure_translations();
const std::size_t ntrans = (map_p2s.empty() || map_p2s[0].empty()) ? 0 : map_p2s[0].size();
const std::size_t n = self.get_number_of_fc_origin(fc_order, permutation) * ntrans;
double *vp = nullptr;
int *ip = nullptr;
auto vals = make_owned<double>(n, &vp);
auto idx = make_owned<int>(n * (fc_order + 1), &ip);
self.get_fc_all(vp, ip, fc_order, permutation);
return nb::make_tuple(vals, idx);
},
nb::arg("fc_order"),
nb::arg("permutation") = 1)
.def(
"set_fc",
[](ALM &self, f64in fc_in) {
const std::size_t n = self.get_number_of_free_parameters();
if (static_cast<std::size_t>(fc_in.size()) != n)
throw std::invalid_argument("set_fc: expected " + std::to_string(n) + " free FCs, got " +
std::to_string(fc_in.size()));
std::vector<double> buf(fc_in.data(), fc_in.data() + fc_in.size());
self.set_fc(buf.data());
},
nb::arg("fc_in").noconvert())
.def("set_fc_zero_threshold", &ALM::set_fc_zero_threshold)
// -- displacement patterns (suggest mode) -----------------------------
.def("get_number_of_displacement_patterns", &ALM::get_number_of_displacement_patterns)
.def(
"get_number_of_displaced_atoms",
[](ALM &self, int fc_order) {
const std::size_t npat = self.get_number_of_displacement_patterns(fc_order);
int *np_ = nullptr;
auto nums = make_owned<int>(npat, &np_);
self.get_number_of_displaced_atoms(np_, fc_order);
return nums;
},
nb::arg("fc_order"))
.def(
"get_displacement_patterns",
[](ALM &self, int fc_order) {
const std::size_t npat = self.get_number_of_displacement_patterns(fc_order);
std::vector<int> nums(npat);
self.get_number_of_displaced_atoms(nums.data(), fc_order);
std::size_t tot = 0;
for (auto v: nums) tot += static_cast<std::size_t>(v);
int *ai = nullptr;
double *dp = nullptr;
auto atom_indices = make_owned<int>(tot, &ai);
auto disp = make_owned<double>(tot * 3, &dp);
const int nbasis = self.get_displacement_patterns(ai, dp, fc_order);
return nb::make_tuple(atom_indices, disp, nbasis);
},
nb::arg("fc_order"))
// -- save --------------------------------------------------------------
.def("set_fcs_save_flag", &ALM::set_fcs_save_flag)
.def(
"save_fc",
[](ALM &self,
const std::string &filename,
const std::string &fc_format,
int maxorder_to_save,
const std::string &fc_unit) {
if (maxorder_to_save < 0) maxorder_to_save = self.get_maxorder();
// Applied on every call (including the default) so a previous
// save with a non-default unit cannot leak into this one.
self.set_fcs_unit_output(fc_unit);
nb::gil_scoped_release rel;
self.save_fc(filename, fc_format, maxorder_to_save);
},
nb::arg("filename"),
nb::arg("fc_format") = "alamode",
nb::arg("maxorder_to_save") = -1,
nb::arg("fc_unit") = "Ry/bohr");
}