Skip to content

Commit dadbed9

Browse files
author
nscc-gz_pinchen_1
committed
Support NPZ output for LCAO HSR matrices
1 parent 128d8d8 commit dadbed9

8 files changed

Lines changed: 187 additions & 4 deletions

File tree

docs/advanced/input_files/input-main.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@
177177
- [out\_mat\_l](#out_mat_l)
178178
- [out\_xc\_r](#out_xc_r)
179179
- [out\_eband\_terms](#out_eband_terms)
180+
- [out\_hr\_npz](#out_hr_npz)
181+
- [out\_hsr\_npz](#out_hsr_npz)
182+
- [out\_dm\_npz](#out_dm_npz)
180183
- [out\_mul](#out_mul)
181184
- [out\_app\_flag](#out_app_flag)
182185
- [out\_ndigits](#out_ndigits)
@@ -2049,6 +2052,28 @@
20492052
- **Description**: Whether to print the band energy terms separately in the file OUT.{term}_out.dat. The terms include the kinetic, pseudopotential (local + nonlocal), Hartree and exchange-correlation (including exact exchange if calculated).
20502053
- **Default**: False
20512054

2055+
### out_hr_npz
2056+
2057+
- **Type**: Boolean
2058+
- **Availability**: *Numerical atomic orbital basis (not gamma-only algorithm)*
2059+
- **Description**: Whether to print Hamiltonian matrices H(R) in npz format. The output files are named output_HR0.npz, output_HR1.npz, and so on according to spin channel. This feature requires ABACUS to be built with CNPY.
2060+
- **Default**: False
2061+
- **Unit**: Ry
2062+
2063+
### out_hsr_npz
2064+
2065+
- **Type**: Boolean
2066+
- **Availability**: *Numerical atomic orbital basis (not gamma-only algorithm)*
2067+
- **Description**: Whether to print Hamiltonian matrices H(R) and overlap matrix S(R) in npz format. The output files are named output_SR.npz, output_HR0.npz, output_HR1.npz, and so on according to spin channel. This feature requires ABACUS to be built with CNPY.
2068+
- **Default**: False
2069+
2070+
### out_dm_npz
2071+
2072+
- **Type**: Boolean
2073+
- **Availability**: *Numerical atomic orbital basis (not gamma-only algorithm)*
2074+
- **Description**: Whether to print density matrices DM(R) in npz format. The output files are named output_DM0.npz, output_DM1.npz, and so on according to spin channel. This feature requires ABACUS to be built with CNPY.
2075+
- **Default**: False
2076+
20522077
### out_mul
20532078

20542079
- **Type**: Boolean

docs/parameters.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3147,6 +3147,30 @@ parameters:
31473147
default_value: "False"
31483148
unit: ""
31493149
availability: Numerical atomic orbital basis
3150+
- name: out_hr_npz
3151+
category: Output information
3152+
type: Boolean
3153+
description: |
3154+
Whether to print Hamiltonian matrices H(R) in npz format. The output files are named output_HR0.npz, output_HR1.npz, and so on according to spin channel. This feature requires ABACUS to be built with CNPY.
3155+
default_value: "False"
3156+
unit: Ry
3157+
availability: Numerical atomic orbital basis (not gamma-only algorithm)
3158+
- name: out_hsr_npz
3159+
category: Output information
3160+
type: Boolean
3161+
description: |
3162+
Whether to print Hamiltonian matrices H(R) and overlap matrix S(R) in npz format. The output files are named output_SR.npz, output_HR0.npz, output_HR1.npz, and so on according to spin channel. This feature requires ABACUS to be built with CNPY.
3163+
default_value: "False"
3164+
unit: ""
3165+
availability: Numerical atomic orbital basis (not gamma-only algorithm)
3166+
- name: out_dm_npz
3167+
category: Output information
3168+
type: Boolean
3169+
description: |
3170+
Whether to print density matrices DM(R) in npz format. The output files are named output_DM0.npz, output_DM1.npz, and so on according to spin channel. This feature requires ABACUS to be built with CNPY.
3171+
default_value: "False"
3172+
unit: ""
3173+
availability: Numerical atomic orbital basis (not gamma-only algorithm)
31503174
- name: out_mul
31513175
category: Output information
31523176
type: Boolean

source/source_io/module_ctrl/ctrl_scf_lcao.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "../module_unk/berryphase.h" // use berryphase
1212
#include "../module_hs/cal_pLpR.h" // use AngularMomentumCalculator()
1313
#include "source_io/module_hs/output_mat_sparse.h" // use ModuleIO::output_mat_sparse()
14+
#include "source_io/module_ml/io_npz.h" // use ModuleIO::output_mat_npz()
1415
#include "../module_hs/write_HS_R.h" // use ModuleIO::write_hsr()
1516
#include "../module_mulliken/output_mulliken.h" // use cal_mag()
1617
#include "../module_wannier/to_wannier90_lcao.h" // use toWannier90_LCAO
@@ -228,6 +229,35 @@ void ModuleIO::ctrl_scf_lcao(UnitCell& ucell,
228229
out_app_flag, ucell.get_iat2iwt(), ucell.nat, istep);
229230
}
230231

232+
//------------------------------------------------------------------
233+
//! 7a.1) Output H(R), S(R), and DM(R) matrices in NPZ format
234+
//------------------------------------------------------------------
235+
if (inp.out_hsr_npz)
236+
{
237+
std::string zipname = "output_SR.npz";
238+
ModuleIO::output_mat_npz(ucell, zipname, *(p_hamilt->getSR()));
239+
}
240+
241+
if (inp.out_hr_npz || inp.out_hsr_npz)
242+
{
243+
std::vector<hamilt::HContainer<TR>*> hr_vec = p_hamilt->getHR_vector();
244+
for (int ispin = 0; ispin < hr_vec.size(); ++ispin)
245+
{
246+
std::string zipname = "output_HR" + std::to_string(ispin) + ".npz";
247+
ModuleIO::output_mat_npz(ucell, zipname, *(hr_vec[ispin]));
248+
}
249+
}
250+
251+
if (inp.out_dm_npz)
252+
{
253+
const std::vector<hamilt::HContainer<double>*>& dmr_vec = dm->get_DMR_vector();
254+
for (int ispin = 0; ispin < dmr_vec.size(); ++ispin)
255+
{
256+
std::string zipname = "output_DM" + std::to_string(ispin) + ".npz";
257+
ModuleIO::output_mat_npz(ucell, zipname, *(dmr_vec[ispin]));
258+
}
259+
}
260+
231261
//------------------------------------------------------------------
232262
//! 7b) Output dH, dS, T, r matrices (old sparse path, without H/S)
233263
//------------------------------------------------------------------

source/source_io/module_ml/io_npz.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ void read_mat_npz(const Parallel_Orbitals* paraV,
321321
#endif
322322
}
323323

324-
void output_mat_npz(const UnitCell& ucell, std::string& zipname, const hamilt::HContainer<double>& hR)
324+
template <typename T>
325+
void output_mat_npz_impl(const UnitCell& ucell, std::string& zipname, const hamilt::HContainer<T>& hR)
325326
{
326327
ModuleBase::TITLE("ModuleIO", "output_mat_npz");
327328

@@ -412,13 +413,13 @@ void output_mat_npz(const UnitCell& ucell, std::string& zipname, const hamilt::H
412413

413414
//fourth block: hr(i0,jR)
414415
#ifdef __MPI
415-
hamilt::HContainer<double>* HR_serial;
416+
hamilt::HContainer<T>* HR_serial;
416417
Parallel_Orbitals serialV;
417418
serialV.set_serial(PARAM.globalv.nlocal, PARAM.globalv.nlocal);
418419
serialV.set_atomic_trace(ucell.get_iat2iwt(), ucell.nat, PARAM.globalv.nlocal);
419420
if(GlobalV::MY_RANK == 0)
420421
{
421-
HR_serial = new hamilt::HContainer<double>(&serialV);
422+
HR_serial = new hamilt::HContainer<T>(&serialV);
422423
}
423424
hamilt::gatherParallels(hR, HR_serial, 0);
424425

@@ -471,4 +472,16 @@ void output_mat_npz(const UnitCell& ucell, std::string& zipname, const hamilt::H
471472
#endif
472473
}
473474

475+
void output_mat_npz(const UnitCell& ucell, std::string& zipname, const hamilt::HContainer<double>& hR)
476+
{
477+
output_mat_npz_impl(ucell, zipname, hR);
478+
}
479+
480+
void output_mat_npz(const UnitCell& ucell,
481+
std::string& zipname,
482+
const hamilt::HContainer<std::complex<double>>& hR)
483+
{
484+
output_mat_npz_impl(ucell, zipname, hR);
485+
}
486+
474487
} // namespace ModuleIO

source/source_io/module_ml/io_npz.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "source_cell/unitcell.h"
66
#include "source_lcao/module_hcontainer/hcontainer.h"
77

8+
#include <complex>
89
#include <string>
910
#include <vector>
1011

@@ -17,6 +18,9 @@ void read_mat_npz(const Parallel_Orbitals* paraV,
1718
hamilt::HContainer<double>& hR);
1819

1920
void output_mat_npz(const UnitCell& ucell, std::string& zipname, const hamilt::HContainer<double>& hR);
21+
void output_mat_npz(const UnitCell& ucell,
22+
std::string& zipname,
23+
const hamilt::HContainer<std::complex<double>>& hR);
2024

2125
} // namespace ModuleIO
2226

source/source_io/module_parameter/input_parameter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ struct Input_para
400400
///< KS-orbital representation.
401401
std::vector<int> out_mat_xc2 = {0, 8}; ///< output Vxc(R) matrix with precision
402402
bool out_eband_terms = false; ///< output the band energy terms separately
403+
bool out_hr_npz = false; ///< output H(R) matrix in npz format
404+
bool out_hsr_npz = false; ///< output H(R) and S(R) matrices in npz format
405+
bool out_dm_npz = false; ///< output DM(R) matrix in npz format
403406
int out_interval = 1;
404407
bool out_app_flag = true; ///< whether output r(R), H(R), S(R), T(R), and dH(R) matrices
405408
///< in an append manner during MD liuyu 2023-03-20

source/source_io/module_parameter/read_input_item_output.cpp

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ Also controled by out_freq_ion and out_app_flag.
594594
};
595595
item.check_value = [](const Input_Item& item, const Parameter& para) {
596596
if ((para.inp.out_mat_r[0] || para.inp.out_mat_hs2[0] || para.inp.out_mat_t[0] || para.inp.out_mat_dh[0]
597-
|| para.inp.dm_to_rho)
597+
|| para.inp.out_hr_npz || para.inp.out_hsr_npz || para.inp.out_dm_npz || para.inp.dm_to_rho)
598598
&& para.sys.gamma_only_local)
599599
{
600600
ModuleBase::WARNING_QUIT("ReadInput",
@@ -816,6 +816,72 @@ The circle order of the charge density on real space grids is: x is the outer lo
816816
read_sync_bool(input.out_eband_terms);
817817
this->add_item(item);
818818
}
819+
{
820+
Input_Item item("out_hr_npz");
821+
item.annotation = "output H(R) matrix in npz format";
822+
item.category = "Output information";
823+
item.type = "Boolean";
824+
item.description = "Whether to print Hamiltonian matrices H(R) in npz format. This feature does not work for gamma-only calculations.";
825+
item.default_value = "False";
826+
item.unit = "Ry";
827+
item.availability = "Numerical atomic orbital basis (not gamma-only algorithm)";
828+
read_sync_bool(input.out_hr_npz);
829+
item.check_value = [](const Input_Item& item, const Parameter& para) {
830+
if (para.input.out_hr_npz)
831+
{
832+
#ifndef __USECNPY
833+
ModuleBase::WARNING_QUIT("ReadInput",
834+
"to write in npz format, please "
835+
"recompile with -DENABLE_CNPY=1");
836+
#endif
837+
}
838+
};
839+
this->add_item(item);
840+
}
841+
{
842+
Input_Item item("out_hsr_npz");
843+
item.annotation = "output H(R) and S(R) matrices in npz format";
844+
item.category = "Output information";
845+
item.type = "Boolean";
846+
item.description = "Whether to print Hamiltonian matrices H(R) and overlap matrix S(R) in npz format. This feature does not work for gamma-only calculations.";
847+
item.default_value = "False";
848+
item.unit = "Ry";
849+
item.availability = "Numerical atomic orbital basis (not gamma-only algorithm)";
850+
read_sync_bool(input.out_hsr_npz);
851+
item.check_value = [](const Input_Item& item, const Parameter& para) {
852+
if (para.input.out_hsr_npz)
853+
{
854+
#ifndef __USECNPY
855+
ModuleBase::WARNING_QUIT("ReadInput",
856+
"to write in npz format, please "
857+
"recompile with -DENABLE_CNPY=1");
858+
#endif
859+
}
860+
};
861+
this->add_item(item);
862+
}
863+
{
864+
Input_Item item("out_dm_npz");
865+
item.annotation = "output DM(R) matrix in npz format";
866+
item.category = "Output information";
867+
item.type = "Boolean";
868+
item.description = "Whether to print density matrices DM(R) in npz format. This feature does not work for gamma-only calculations.";
869+
item.default_value = "False";
870+
item.unit = "";
871+
item.availability = "Numerical atomic orbital basis (not gamma-only algorithm)";
872+
read_sync_bool(input.out_dm_npz);
873+
item.check_value = [](const Input_Item& item, const Parameter& para) {
874+
if (para.input.out_dm_npz)
875+
{
876+
#ifndef __USECNPY
877+
ModuleBase::WARNING_QUIT("ReadInput",
878+
"to write in npz format, please "
879+
"recompile with -DENABLE_CNPY=1");
880+
#endif
881+
}
882+
};
883+
this->add_item(item);
884+
}
819885
{
820886
Input_Item item("out_mul");
821887
item.annotation = "mulliken charge or not";

source/source_io/test_serial/read_input_item_test.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,24 @@ TEST_F(InputTest, Item_test)
973973
it->second.reset_value(it->second, param);
974974
EXPECT_EQ(param.input.out_mat_hs[0], 1);
975975
}
976+
{ // out_hr_npz
977+
auto it = find_label("out_hr_npz", readinput.input_lists);
978+
it->second.str_values = {"1"};
979+
it->second.read_value(it->second, param);
980+
EXPECT_EQ(param.input.out_hr_npz, true);
981+
}
982+
{ // out_hsr_npz
983+
auto it = find_label("out_hsr_npz", readinput.input_lists);
984+
it->second.str_values = {"1"};
985+
it->second.read_value(it->second, param);
986+
EXPECT_EQ(param.input.out_hsr_npz, true);
987+
}
988+
{ // out_dm_npz
989+
auto it = find_label("out_dm_npz", readinput.input_lists);
990+
it->second.str_values = {"1"};
991+
it->second.read_value(it->second, param);
992+
EXPECT_EQ(param.input.out_dm_npz, true);
993+
}
976994
}
977995
TEST_F(InputTest, Item_test2)
978996
{

0 commit comments

Comments
 (0)