Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
625c340
feat: write and plot LCAO projected DOS in NumPy format
Stardust0831 Nov 19, 2026
3d5a960
feat: share NumPy output between total and projected DOS
Stardust0831 Nov 19, 2026
5f70ea2
refactor: name the DOS format selector out_dos_format
Stardust0831 Nov 19, 2026
9db8557
refactor: keep NumPy DOS output separate from the text writer
Stardust0831 Nov 19, 2026
abaab22
Share PDOS orbital sums and simplify NumPy output
Stardust0831 Nov 19, 2026
0b14b35
Limit NumPy output to single-point LCAO projected DOS
Stardust0831 Nov 19, 2026
6bf362e
Handle ambiguous PDOS directories and test native NumPy output
Stardust0831 Nov 19, 2026
3f17fd7
Exercise spin-polarized NPY PDOS in Autotest and validate record meta…
Stardust0831 Nov 20, 2026
1fa2f23
Propagate PDOS validation failures to Autotest
Stardust0831 Nov 20, 2026
b45f277
Merge branch 'develop' into feat/lcao-pdos-npy
mohanchen Nov 21, 2026
ad81795
Merge develop and preserve LCAO PDOS output conventions
Stardust0831 Nov 22, 2026
c31189a
Select NumPy total and projected LCAO DOS with out_dos 2
Stardust0831 Nov 22, 2026
96a8d85
Store all total DOS spin channels in dos.npy
Stardust0831 Nov 23, 2026
c3af378
docs: clarify precision and write costs for out_dos 2
Stardust0831 Nov 23, 2026
6fc82ea
refactor: pass out_dos directly to the LCAO DOS writer
Stardust0831 Nov 23, 2026
f885ee7
test: cover in-memory capture of total DOS
Stardust0831 Nov 23, 2026
0787f37
refactor: use explicit types in LCAO DOS output and tests
Stardust0831 Nov 24, 2026
ce1c786
Merge branch 'develop' into feat/lcao-pdos-npy
Stardust0831 Nov 24, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions docs/advanced/elec_properties/dos.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,35 @@ The PDOS file uses a plain-text format. Each row corresponds to one energy point
The `# Ionic_Step` line is only present for geometry-optimization (istep) output. Within each angular momentum `l`, the `2l+1` columns follow the magnetic-quantum-number order `m = 0, +1, -1, +2, -2, ...`.

For nspin=2, two files are written (pdoss1* and pdoss2*), one per spin channel. For nspin=4, the two spinor components are summed into a single file. The unit of PDOS is also `(number of states)/(eV * unitcell)`.

### NumPy output

For a single-point LCAO KSDFT SCF or NSCF calculation, set `out_dos 2`
to write both total and projected DOS as NumPy arrays. Use `out_dos 1`
for text output or `out_dos 0` (the default) to disable DOS output.
Older releases used `out_dos 2` to request text PDOS.

Total DOS is in `dos.npy`, with one record per energy point. The shared
`energy_eV` field has shape `(npoints,)`. The four spectrum fields,
`dos`, `dos_int`, `dos_smear` and `dos_smear_int`, each have shape
`(npoints, 2)` for `nspin=2` (spin up, then spin down) or `(npoints, 1)`
for `nspin=1` or `4`. All five fields use binary64 and the same units as
the text columns. Projected DOS is in `pdos.npy`.
With `out_app_flag 2`, these files are in the `DOS/` output subdirectory.
For plotting, set `tdosfile` or `pdosfile` in the `abacus-plot` config.
Use an explicit file path when the directory contains both formats.

```python
import numpy as np

total = np.load("dos.npy", mmap_mode="r")
dos_per_eV = total["dos_smear"]
rows = np.load("pdos.npy", mmap_mode="r")
spectrum = rows["pdos"][0] # (spin channel, energy point), in 1/eV
energy_eV = rows["energy_min_eV"][0] + np.arange(spectrum.shape[1]) * rows["energy_step_eV"][0]
```

Each PDOS row identifies one channel by `atom_index` (one-based), `species`, `l`
and signed `m`. `nspin` is the input spin mode. Modes 1 and 4 store one
spectrum per row, and mode 2 stores spin-up and spin-down. The array uses
binary64 and the same zeta sums and zero threshold as text.
1 change: 1 addition & 0 deletions source/source_io/module_ctrl/ctrl_scf_lcao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ void ModuleIO::ctrl_scf_lcao(UnitCell& ucell,
inp.dos_edelta_ev,
inp.dos_scale,
inp.dos_sigma,
inp.out_dos,
out_app_flag,
istep,
inp.nspin,
Expand Down
23 changes: 13 additions & 10 deletions source/source_io/module_dos/cal_dos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,14 @@ bool ModuleIO::cal_dos(const int& is, // index for spin
const ModuleBase::matrix& wg, // weight of k-points and bands
const int istep, // ionic step
const int out_app_flag,
const int bndpar)
const int bndpar,
DosSpectrum* spectrum)
{
ModuleBase::TITLE("ModuleIO", "cal_dos");

std::ofstream ofs_dos;

if (GlobalV::MY_RANK == 0)
const bool root = GlobalV::MY_RANK == 0;
if (root && !spectrum)
{
const std::string time_stamp = ModuleIO::local_timestamp();
const int istep_one_based = istep + 1;
Expand Down Expand Up @@ -155,7 +156,7 @@ bool ModuleIO::cal_dos(const int& is, // index for spin
ModuleBase::WARNING("ModuleIO::cal_dos", "npoints <= 0");
return false;
}
if (GlobalV::MY_RANK == 0)
if (root && !spectrum)
{
ofs_dos << npoints << " # number of points" << std::endl;
ofs_dos << std::fixed << std::setprecision(6);
Expand Down Expand Up @@ -222,8 +223,9 @@ bool ModuleIO::cal_dos(const int& is, // index for spin
double sum2 = 0.0;
const double dos_thr = 1.0e-12;

if (GlobalV::MY_RANK == 0)
if (root)
{
if (spectrum) { spectrum->resize(npoints); }
for (int n = 0; n < npoints; ++n)
{
dos[n] /= static_cast<double>(nkstot);
Expand All @@ -236,17 +238,18 @@ bool ModuleIO::cal_dos(const int& is, // index for spin
}
sum2 += dos_smear[n] * de_ev;

if (spectrum)
{
(*spectrum)[n] = {ene[n], dos[n], sum_elec[n], dos_smear[n], sum2};
continue;
}
ofs_dos << std::setw(13) << ene[n]
<< " " << std::setw(12) << dos[n]
<< " " << std::setw(12) << sum_elec[n]
<< " " << std::setw(14) << dos_smear[n]
<< " " << std::setw(14) << sum2 << std::endl;
}
}

if (GlobalV::MY_RANK == 0)
{
ofs_dos.close();
if (!spectrum) { ofs_dos.close(); }
}

ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "Number of bands", nbands);
Expand Down
9 changes: 7 additions & 2 deletions source/source_io/module_dos/cal_dos.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#ifndef DOS_H
#define DOS_H

#include<vector>
#include <array>
#include <vector>
#include "source_base/matrix.h"
#include "source_estate/fp_energy.h"

namespace ModuleIO
{

// Per-energy rows in text column order for one spin channel.
using DosSpectrum = std::vector<std::array<double, 5>>;

void prepare_dos(std::ofstream& ofs_running,
const elecstate::Efermi &energy_fermi,
const ModuleBase::matrix& ekb,
Expand Down Expand Up @@ -38,7 +42,8 @@ namespace ModuleIO
const ModuleBase::matrix &wg, //weight of (kpoint,bands))
const int istep_in, // ionic_step
const int out_app_flag, // 0: no append, 1: append, 2: subdirectory
const int bndpar); // number of band parallel groups
const int bndpar, // number of band parallel groups
DosSpectrum* spectrum); // collect rank-zero rows for NPY output when supplied

}

Expand Down
18 changes: 3 additions & 15 deletions source/source_io/module_dos/cal_pdos_gamma.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
#include "cal_pdos_gamma.h"
#include "write_pdos_text.h"
#include "source_base/parallel_reduce.h"
#include "source_base/module_external/blas_connector.h"
#include "source_base/module_external/scalapack_connector.h"
#include "source_base/global_function.h"
#include "source_base/global_variable.h"
#include "source_lcao/hamilt_lcao.h"
#include "source_io/module_output/write_notice.h"


void ModuleIO::cal_pdos(
std::vector<ModuleBase::matrix> ModuleIO::cal_pdos(
const psi::Psi<double>* psi,
hamilt::Hamilt<double>* p_ham,
const Parallel_Orbitals& pv,
const UnitCell& ucell,
const K_Vectors& kv,
const int nspin0,
const int nbands,
Expand All @@ -22,12 +18,8 @@ void ModuleIO::cal_pdos(
const double& emin,
const double& dos_edelta_ev,
const double& bcoeff,
const int istep,
const int out_app_flag,
const int nlocal,
const int nspin,
const std::string& global_out_dir,
std::ofstream& ofs_running)
const int nspin)
{
ModuleBase::TITLE("ModuleIO", "cal_pdos_gamma");

Expand Down Expand Up @@ -147,9 +139,5 @@ void ModuleIO::cal_pdos(
#endif
} // is

if (GlobalV::MY_RANK == 0)
{
write_pdos_text(ucell, pdos.data(), nspin, nlocal, npoints,
emin, dos_edelta_ev, global_out_dir, istep, out_app_flag, ofs_running);
}
return pdos;
}
12 changes: 3 additions & 9 deletions source/source_io/module_dos/cal_pdos_gamma.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
#include "source_psi/psi.h" // use psi::Psi<T>
#include "source_hamilt/hamilt.h" // use hamilt::Hamilt<T>
#include "source_basis/module_ao/parallel_orbitals.h" // use Parallel_Orbitals
#include <iosfwd>
#include <string>
#include <vector>

namespace ModuleIO
{

void cal_pdos(
std::vector<ModuleBase::matrix> cal_pdos(
const psi::Psi<double>* psi,
hamilt::Hamilt<double>* p_ham,
const Parallel_Orbitals& pv,
const UnitCell& ucell,
const K_Vectors& kv,
const int nspin0,
const int nbands,
Expand All @@ -25,12 +23,8 @@ namespace ModuleIO
const double& emin,
const double& dos_edelta_ev,
const double& bcoeff,
const int istep,
const int out_app_flag,
const int nlocal,
const int nspin,
const std::string& global_out_dir,
std::ofstream& ofs_running);
const int nspin);

}

Expand Down
18 changes: 3 additions & 15 deletions source/source_io/module_dos/cal_pdos_multik.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
#include "cal_pdos_multik.h"
#include "write_pdos_text.h"
#include "source_base/parallel_reduce.h"
#include "source_base/module_external/blas_connector.h"
#include "source_base/module_external/scalapack_connector.h"
#include "source_io/module_output/write_notice.h"
#include "source_base/global_function.h"
#include "source_base/global_variable.h"
#include "source_lcao/hamilt_lcao.h"

void ModuleIO::cal_pdos(
std::vector<ModuleBase::matrix> ModuleIO::cal_pdos(
const psi::Psi<std::complex<double>>* psi,
hamilt::Hamilt<std::complex<double>>* p_ham,
const Parallel_Orbitals& pv,
const UnitCell& ucell,
const K_Vectors& kv,
const int nspin0,
const int nbands,
Expand All @@ -21,12 +17,8 @@ void ModuleIO::cal_pdos(
const double& emin,
const double& dos_edelta_ev,
const double& bcoeff,
const int istep,
const int out_app_flag,
const int nlocal,
const int nspin,
const std::string& global_out_dir,
std::ofstream& ofs_running)
const int nspin)
{
ModuleBase::TITLE("ModuleIO", "cal_pdos_multik");

Expand Down Expand Up @@ -187,9 +179,5 @@ void ModuleIO::cal_pdos(
#endif
} // is

if (GlobalV::MY_RANK == 0)
{
write_pdos_text(ucell, pdos.data(), nspin, nlocal, npoints,
emin, dos_edelta_ev, global_out_dir, istep, out_app_flag, ofs_running);
}
return pdos;
}
12 changes: 3 additions & 9 deletions source/source_io/module_dos/cal_pdos_multik.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
#include "source_psi/psi.h" // use psi::Psi<T>
#include "source_hamilt/hamilt.h" // use hamilt::Hamilt<T>
#include "source_basis/module_ao/parallel_orbitals.h" // use Parallel_Orbitals
#include <iosfwd>
#include <string>
#include <vector>

namespace ModuleIO
{

// pdos for multi-k point
void cal_pdos(
std::vector<ModuleBase::matrix> cal_pdos(
const psi::Psi<std::complex<double>>* psi,
hamilt::Hamilt<std::complex<double>>* p_ham,
const Parallel_Orbitals& pv,
const UnitCell& ucell,
const K_Vectors& kv,
const int nspin0,
const int nbands,
Expand All @@ -26,12 +24,8 @@ namespace ModuleIO
const double& emin,
const double& dos_edelta_ev,
const double& bcoeff,
const int istep,
const int out_app_flag,
const int nlocal,
const int nspin,
const std::string& global_out_dir,
std::ofstream& ofs_running);
const int nspin);

}

Expand Down
36 changes: 36 additions & 0 deletions source/source_io/module_dos/dos_npy_header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef MODULE_IO_DOS_NPY_HEADER_H
#define MODULE_IO_DOS_NPY_HEADER_H

#include <algorithm>
#include <cstdint>
#include <limits>
#include <ostream>
#include <string>

namespace ModuleIO
{
/// Write the NPY v1 header for a one-dimensional array of structured records.
inline void write_dos_npy_header(std::ostream& out, const std::string& fields, std::size_t count)
{
static_assert(sizeof(double) == 8 && std::numeric_limits<double>::is_iec559, "DOS requires binary64");
const std::uint16_t one = 1;
const char endian = *reinterpret_cast<const unsigned char*>(&one) ? '<' : '>';
std::string header = "{'descr': [" + fields + "], 'fortran_order': False, 'shape': ("
+ std::to_string(count) + ",), }";
std::replace(header.begin(), header.end(), '=', endian);
header.append((64 - (11 + header.size()) % 64) % 64, ' ');
header += '\n';
if (header.size() > std::numeric_limits<std::uint16_t>::max())
{
out.setstate(std::ios::failbit);
return;
}
const unsigned char length[] = {static_cast<unsigned char>(header.size()),
static_cast<unsigned char>(header.size() >> 8)};
out.write("\x93NUMPY\x01\x00", 8);
out.write(reinterpret_cast<const char*>(length), sizeof(length));
out.write(header.data(), header.size());
}
}

#endif
9 changes: 9 additions & 0 deletions source/source_io/module_dos/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,13 @@ if(ENABLE_MPI)
../../../source_cell/magnetism.cpp
../../../source_cell/test/support/mock_unitcell.cpp)

find_package(Python3 QUIET COMPONENTS Interpreter)
if(Python3_Interpreter_FOUND)
add_test(
NAME MODULE_DOS_pdos_npy_roundtrip
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/test_pdos_npy.py"
"$<TARGET_FILE:MODULE_DOS_write_pdos_text>")
set_tests_properties(MODULE_DOS_pdos_npy_roundtrip PROPERTIES SKIP_RETURN_CODE 77)
endif()

endif()
Loading
Loading