forked from abacusmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathpy_driver.cpp
More file actions
660 lines (575 loc) · 19.8 KB
/
Copy pathpy_driver.cpp
File metadata and controls
660 lines (575 loc) · 19.8 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/**
* @file py_driver.cpp
* @brief Implementation of PyDriver and pybind11 bindings
*
* This file implements the PyDriver class that wraps the complete ABACUS
* calculation workflow for Python access.
*/
#include "py_driver.hpp"
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
// ABACUS headers
#include "source_main/driver.h"
#include "source_cell/unitcell.h"
#include "source_cell/check_atomic_stru.h"
#include "source_esolver/esolver.h"
#include "source_io/read_input.h"
#include "source_io/input_conv.h"
#include "source_io/module_parameter/parameter.h"
#include "source_base/global_variable.h"
#include "source_base/global_file.h"
#include "source_base/timer.h"
#include "source_base/memory.h"
#include "source_base/matrix.h"
#include "source_relax/relax_driver.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <ctime>
#include <stdexcept>
#include <filesystem>
namespace py = pybind11;
namespace fs = std::filesystem;
namespace py_driver
{
/**
* @brief RAII class for managing global state
*
* Saves and restores global state to allow multiple calculations
* in the same Python session.
*/
class GlobalStateGuard
{
public:
GlobalStateGuard()
{
// Save current state
saved_my_rank_ = GlobalV::MY_RANK;
saved_nproc_ = GlobalV::NPROC;
}
~GlobalStateGuard()
{
// Restore state
GlobalV::MY_RANK = saved_my_rank_;
GlobalV::NPROC = saved_nproc_;
}
private:
int saved_my_rank_ = 0;
int saved_nproc_ = 1;
};
/**
* @brief Implementation class for PyDriver (PIMPL pattern)
*/
class PyDriver::Impl
{
public:
Impl() = default;
~Impl() { cleanup(); }
void cleanup()
{
if (p_esolver_)
{
delete p_esolver_;
p_esolver_ = nullptr;
}
ucell_.reset();
}
// ESolver instance
ModuleESolver::ESolver* p_esolver_ = nullptr;
// UnitCell instance
std::unique_ptr<UnitCell> ucell_;
// Output stream for running log
std::ofstream ofs_running_;
std::ofstream ofs_warning_;
// Original working directory
std::string original_cwd_;
// Null stream for silent mode
std::ofstream null_stream_;
// Store original stream buffers
std::streambuf* orig_running_buf_ = nullptr;
std::streambuf* orig_warning_buf_ = nullptr;
};
PyDriver::PyDriver() : impl_(std::make_unique<Impl>())
{
}
PyDriver::~PyDriver()
{
cleanup_context();
}
void PyDriver::initialize_context()
{
// Set up for serial mode (no MPI in Python context)
PARAM.set_pal_param(0, 1, 1); // rank=0, nproc=1, nthread=1
GlobalV::MY_RANK = 0;
GlobalV::NPROC = 1;
initialized_ = true;
}
void PyDriver::cleanup_context()
{
if (impl_)
{
impl_->cleanup();
// Restore original stream buffers
// Note: We use static_cast to std::ostream& because std::ofstream::rdbuf()
// doesn't accept arguments, but std::ostream::rdbuf(streambuf*) does
if (impl_->orig_running_buf_)
{
static_cast<std::ostream&>(GlobalV::ofs_running).rdbuf(impl_->orig_running_buf_);
impl_->orig_running_buf_ = nullptr;
}
if (impl_->orig_warning_buf_)
{
static_cast<std::ostream&>(GlobalV::ofs_warning).rdbuf(impl_->orig_warning_buf_);
impl_->orig_warning_buf_ = nullptr;
}
// Close output streams
if (impl_->ofs_running_.is_open())
{
impl_->ofs_running_.close();
}
if (impl_->ofs_warning_.is_open())
{
impl_->ofs_warning_.close();
}
if (impl_->null_stream_.is_open())
{
impl_->null_stream_.close();
}
// Restore working directory if changed
if (!impl_->original_cwd_.empty())
{
try
{
fs::current_path(impl_->original_cwd_);
}
catch (...)
{
// Ignore errors
}
impl_->original_cwd_.clear();
}
}
initialized_ = false;
}
void PyDriver::setup_output(const std::string& output_dir, int verbosity)
{
std::string out_dir = output_dir.empty() ? "OUT.PYABACUS" : output_dir;
// Create output directory
fs::create_directories(out_dir);
// Save original stream buffers
impl_->orig_running_buf_ = GlobalV::ofs_running.rdbuf();
impl_->orig_warning_buf_ = GlobalV::ofs_warning.rdbuf();
// Open log files based on verbosity
if (verbosity >= 1)
{
std::string running_log = out_dir + "/running.log";
impl_->ofs_running_.open(running_log);
if (impl_->ofs_running_.is_open())
{
static_cast<std::ostream&>(GlobalV::ofs_running).rdbuf(impl_->ofs_running_.rdbuf());
}
}
else
{
// Silent mode - redirect to null
impl_->null_stream_.open("/dev/null");
if (impl_->null_stream_.is_open())
{
static_cast<std::ostream&>(GlobalV::ofs_running).rdbuf(impl_->null_stream_.rdbuf());
}
}
std::string warning_log = out_dir + "/warning.log";
impl_->ofs_warning_.open(warning_log);
if (impl_->ofs_warning_.is_open())
{
static_cast<std::ostream&>(GlobalV::ofs_warning).rdbuf(impl_->ofs_warning_.rdbuf());
}
}
void PyDriver::read_input(
const std::string& input_dir,
const std::string& input_file,
const std::string& stru_file,
const std::string& kpt_file,
const std::string& pseudo_dir,
const std::string& orbital_dir,
const std::string& output_dir)
{
// Save original working directory
impl_->original_cwd_ = fs::current_path().string();
// Determine input file path
std::string input_path;
if (!input_file.empty())
{
input_path = fs::absolute(input_file).string();
}
else
{
input_path = (fs::absolute(input_dir) / "INPUT").string();
}
// Check if input file exists
if (!fs::exists(input_path))
{
throw std::runtime_error("INPUT file not found: " + input_path);
}
// Change to input directory for relative paths
std::string work_dir = input_dir;
if (work_dir.empty())
{
work_dir = fs::path(input_path).parent_path().string();
}
if (!work_dir.empty() && work_dir != ".")
{
fs::current_path(work_dir);
}
// Read INPUT file
// Note: ReadInput will set PARAM.globalv.global_in_card internally
ModuleIO::ReadInput reader(0); // rank 0
std::string input_filename = fs::path(input_path).filename().string();
reader.read_parameters(PARAM, input_filename);
// Create output directory
reader.create_directory(PARAM);
// Convert input parameters to internal format
Input_Conv::Convert();
}
CalculationResult PyDriver::collect_results(bool calculate_force, bool calculate_stress)
{
CalculationResult result;
if (!impl_->p_esolver_ || !impl_->ucell_)
{
return result;
}
// Get convergence info
result.converged = impl_->p_esolver_->conv_esolver;
// Get energy
result.etot = impl_->p_esolver_->cal_energy();
// Get system info from UnitCell
result.nat = impl_->ucell_->nat;
result.ntype = impl_->ucell_->ntype;
// Calculate forces if requested
if (calculate_force)
{
ModuleBase::matrix force(result.nat, 3);
impl_->p_esolver_->cal_force(*impl_->ucell_, force);
// Convert to numpy array
std::vector<ssize_t> shape = {static_cast<ssize_t>(result.nat), 3};
result.forces = py::array_t<double>(shape);
auto buf = result.forces.request();
double* ptr = static_cast<double*>(buf.ptr);
for (int i = 0; i < result.nat; ++i)
{
for (int j = 0; j < 3; ++j)
{
ptr[i * 3 + j] = force(i, j);
}
}
result.has_forces = true;
}
// Calculate stress if requested
if (calculate_stress)
{
ModuleBase::matrix stress(3, 3);
impl_->p_esolver_->cal_stress(*impl_->ucell_, stress);
// Convert to numpy array
std::vector<ssize_t> shape = {3, 3};
result.stress = py::array_t<double>(shape);
auto buf = result.stress.request();
double* ptr = static_cast<double*>(buf.ptr);
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
ptr[i * 3 + j] = stress(i, j);
}
}
result.has_stress = true;
}
// Collect output file tracking information
result.output_dir = PARAM.sys.global_out_dir;
// Find the log file
if (!result.output_dir.empty() && fs::exists(result.output_dir))
{
// Look for running_*.log files
std::vector<std::string> log_patterns = {
"running_scf.log",
"running_relax.log",
"running_cell-relax.log",
"running_nscf.log",
"running_md.log"
};
for (const auto& log_name : log_patterns)
{
std::string log_path = result.output_dir + "/" + log_name;
if (fs::exists(log_path))
{
result.log_file = log_path;
break;
}
}
// Iterate directory to populate output_files map
try
{
for (const auto& entry : fs::directory_iterator(result.output_dir))
{
if (entry.is_regular_file())
{
std::string filename = entry.path().filename().string();
std::string full_path = entry.path().string();
result.output_files[filename] = full_path;
}
}
}
catch (const std::exception& e)
{
// Ignore errors during directory iteration
}
}
return result;
}
CalculationResult PyDriver::run(
const std::string& input_dir,
const std::string& input_file,
const std::string& stru_file,
const std::string& kpt_file,
const std::string& pseudo_dir,
const std::string& orbital_dir,
const std::string& output_dir,
bool calculate_force,
bool calculate_stress,
int verbosity)
{
// Use RAII guard for global state
GlobalStateGuard state_guard;
// Clean up any previous calculation
cleanup_context();
// Initialize context
initialize_context();
// Setup output
setup_output(output_dir, verbosity);
// Start timer
ModuleBase::timer::start();
try
{
// Read input files
read_input(input_dir, input_file, stru_file, kpt_file,
pseudo_dir, orbital_dir, output_dir);
// Create UnitCell
impl_->ucell_ = std::make_unique<UnitCell>();
impl_->ucell_->setup(
PARAM.inp.latname,
PARAM.inp.ntype,
PARAM.inp.lmaxmax,
PARAM.inp.init_vel,
PARAM.inp.fixed_axes
);
// Read structure
impl_->ucell_->setup_cell(PARAM.globalv.global_in_stru, GlobalV::ofs_running);
// Check atomic structure
unitcell::check_atomic_stru(*impl_->ucell_, PARAM.inp.min_dist_coef);
// Initialize ESolver
impl_->p_esolver_ = ModuleESolver::init_esolver(PARAM.inp, *impl_->ucell_);
// Run before_all_runners
impl_->p_esolver_->before_all_runners(*impl_->ucell_, PARAM.inp);
// Run calculation based on calculation type
const std::string& cal = PARAM.inp.calculation;
if (cal == "scf" || cal == "relax" || cal == "cell-relax" || cal == "nscf")
{
Relax_Driver rl_driver;
rl_driver.relax_driver(impl_->p_esolver_, *impl_->ucell_, PARAM.inp, GlobalV::ofs_running);
}
else if (cal == "get_s")
{
impl_->p_esolver_->runner(*impl_->ucell_, 0);
}
else
{
throw std::runtime_error("Unsupported calculation type: " + cal);
}
// Collect results
last_result_ = collect_results(calculate_force, calculate_stress);
// Run after_all_runners
impl_->p_esolver_->after_all_runners(*impl_->ucell_);
}
catch (const std::exception& e)
{
// Stop timer on error
ModuleBase::timer::finish(GlobalV::ofs_running);
// Clean up on error
cleanup_context();
throw;
}
// Stop timer
ModuleBase::timer::finish(GlobalV::ofs_running);
// Print memory usage
ModuleBase::Memory::print_all(GlobalV::ofs_running);
return last_result_;
}
} // namespace py_driver
// ============================================================================
// Pybind11 Module Definition
// ============================================================================
PYBIND11_MODULE(_driver_pack, m)
{
m.doc() = R"pbdoc(
PyABACUS Driver Module
----------------------
This module provides Python bindings for running complete ABACUS
DFT calculations.
Main Classes
------------
PyDriver : Main driver class for running calculations
CalculationResult : Container for calculation results
Example
-------
>>> from pyabacus.driver import PyDriver
>>> driver = PyDriver()
>>> result = driver.run("./Si_scf/")
>>> print(f"Energy: {result.etot_eV()} eV")
>>> print(result.summary())
)pbdoc";
// Bind CalculationResult
py::class_<py_driver::CalculationResult>(m, "CalculationResult",
R"pbdoc(
Container for DFT calculation results.
Attributes
----------
converged : bool
Whether SCF converged
niter : int
Number of SCF iterations
drho : float
Final charge density difference
etot : float
Total energy in Rydberg
forces : numpy.ndarray
Forces on atoms (nat, 3) in Ry/Bohr
stress : numpy.ndarray
Stress tensor (3, 3) in kbar
)pbdoc")
.def(py::init<>())
.def_readonly("converged", &py_driver::CalculationResult::converged,
"Whether SCF converged")
.def_readonly("niter", &py_driver::CalculationResult::niter,
"Number of SCF iterations")
.def_readonly("drho", &py_driver::CalculationResult::drho,
"Final charge density difference")
.def_readonly("etot", &py_driver::CalculationResult::etot,
"Total energy (Ry)")
.def_readonly("eband", &py_driver::CalculationResult::eband,
"Band energy (Ry)")
.def_readonly("hartree_energy", &py_driver::CalculationResult::hartree_energy,
"Hartree energy (Ry)")
.def_readonly("etxc", &py_driver::CalculationResult::etxc,
"Exchange-correlation energy (Ry)")
.def_readonly("ewald_energy", &py_driver::CalculationResult::ewald_energy,
"Ewald energy (Ry)")
.def_readonly("demet", &py_driver::CalculationResult::demet,
"-TS term for metals (Ry)")
.def_readonly("exx", &py_driver::CalculationResult::exx,
"Exact exchange energy (Ry)")
.def_readonly("evdw", &py_driver::CalculationResult::evdw,
"van der Waals energy (Ry)")
.def_readonly("forces", &py_driver::CalculationResult::forces,
"Forces on atoms (nat, 3) in Ry/Bohr")
.def_readonly("has_forces", &py_driver::CalculationResult::has_forces,
"Whether forces are available")
.def_readonly("stress", &py_driver::CalculationResult::stress,
"Stress tensor (3, 3) in kbar")
.def_readonly("has_stress", &py_driver::CalculationResult::has_stress,
"Whether stress is available")
.def_readonly("fermi_energy", &py_driver::CalculationResult::fermi_energy,
"Fermi energy (eV)")
.def_readonly("bandgap", &py_driver::CalculationResult::bandgap,
"Band gap (eV)")
.def_readonly("nat", &py_driver::CalculationResult::nat,
"Number of atoms")
.def_readonly("ntype", &py_driver::CalculationResult::ntype,
"Number of atom types")
.def_readonly("nbands", &py_driver::CalculationResult::nbands,
"Number of bands")
.def_readonly("nks", &py_driver::CalculationResult::nks,
"Number of k-points")
.def_readonly("output_dir", &py_driver::CalculationResult::output_dir,
"Path to output directory (OUT.$suffix)")
.def_readonly("log_file", &py_driver::CalculationResult::log_file,
"Path to the main log file")
.def_readonly("output_files", &py_driver::CalculationResult::output_files,
"Dictionary of output files (filename -> full path)")
.def("etot_eV", &py_driver::CalculationResult::etot_eV,
"Get total energy in eV")
.def("get_energies", &py_driver::CalculationResult::get_energies,
"Get all energies as a dictionary")
.def("get_forces_eV_Ang", &py_driver::CalculationResult::get_forces_eV_Ang,
"Get forces in eV/Angstrom")
.def("summary", &py_driver::CalculationResult::summary,
"Get a summary string of the calculation result")
.def("__repr__", [](const py_driver::CalculationResult& r) {
std::ostringstream ss;
ss << "<CalculationResult converged=" << (r.converged ? "True" : "False")
<< " etot=" << r.etot << " Ry>";
return ss.str();
});
// Bind PyDriver
py::class_<py_driver::PyDriver>(m, "PyDriver",
R"pbdoc(
Python wrapper for ABACUS Driver.
This class provides a Python interface for running complete ABACUS
DFT calculations.
Example
-------
>>> driver = PyDriver()
>>> result = driver.run(
... input_dir="./Si_scf/",
... calculate_force=True,
... calculate_stress=True
... )
>>> print(f"Energy: {result.etot_eV()} eV")
>>> print(f"Converged: {result.converged}")
)pbdoc")
.def(py::init<>())
.def("run", &py_driver::PyDriver::run,
R"pbdoc(
Run a complete DFT calculation.
Parameters
----------
input_dir : str, optional
Directory containing INPUT, STRU, KPT files (default: ".")
input_file : str, optional
Explicit path to INPUT file
stru_file : str, optional
Explicit path to STRU file
kpt_file : str, optional
Explicit path to KPT file
pseudo_dir : str, optional
Directory containing pseudopotentials
orbital_dir : str, optional
Directory containing orbital files
output_dir : str, optional
Directory for output files
calculate_force : bool, optional
Whether to calculate forces (default: True)
calculate_stress : bool, optional
Whether to calculate stress (default: False)
verbosity : int, optional
Output verbosity level (0=silent, 1=normal, 2=verbose)
Returns
-------
CalculationResult
Container with all calculation results
)pbdoc",
py::arg("input_dir") = ".",
py::arg("input_file") = "",
py::arg("stru_file") = "",
py::arg("kpt_file") = "",
py::arg("pseudo_dir") = "",
py::arg("orbital_dir") = "",
py::arg("output_dir") = "",
py::arg("calculate_force") = true,
py::arg("calculate_stress") = false,
py::arg("verbosity") = 1)
.def("is_ready", &py_driver::PyDriver::is_ready,
"Check if the driver is ready for calculation")
.def("get_last_result", &py_driver::PyDriver::get_last_result,
py::return_value_policy::reference_internal,
"Get the last calculation result");
}