Skip to content

Commit 2d27a13

Browse files
committed
Feature: binary format of backup charge density
1 parent 9bf2533 commit 2d27a13

File tree

7 files changed

+52
-64
lines changed

7 files changed

+52
-64
lines changed

docs/advanced/input_files/input-main.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,10 +1561,8 @@ These variables are used to control the output of properties.
15611561
### out_freq_elec
15621562

15631563
- **Type**: Integer
1564-
- **Description**: The output frequency of the charge density (controlled by [out_chg](#out_chg)), wavefunction (controlled by [out_wfc_pw](#out_wfc_pw) or [out_wfc_r](#out_wfc_r)), and density matrix of localized orbitals (controlled by [out_dm](#out_dm)).
1565-
- \>0: Output them every `out_freq_elec` iteration numbers in electronic iterations.
1566-
- 0: Output them when the electronic iteration is converged or reaches the maximal iteration number.
1567-
- **Default**: 0
1564+
- **Description**: Output the charge density (only binary format, controlled by [out_chg](#out_chg)), wavefunction (controlled by [out_wfc_pw](#out_wfc_pw) or [out_wfc_r](#out_wfc_r)) per `out_freq_elec` electronic iterations. Note that they are always output when converged or reach the maximum iterations [scf_nmax](#scf_nmax).
1565+
- **Default**: [scf_nmax](#scf_nmax)
15681566

15691567
### out_chg
15701568

source/module_esolver/esolver_fp.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -183,24 +183,6 @@ void ESolver_FP::after_scf(UnitCell& ucell, const int istep)
183183
}
184184
}
185185
}
186-
if (PARAM.inp.out_chg[0] != -1)
187-
{
188-
std::complex<double>** rhog_tot = (PARAM.inp.dm_to_rho)? this->pelec->charge->rhog : this->pelec->charge->rhog_save;
189-
double** rhor_tot = (PARAM.inp.dm_to_rho)? this->pelec->charge->rho : this->pelec->charge->rho_save;
190-
for (int is = 0; is < PARAM.inp.nspin; is++)
191-
{
192-
this->pw_rhod->real2recip(rhor_tot[is], rhog_tot[is]);
193-
}
194-
ModuleIO::write_rhog(PARAM.globalv.global_out_dir + PARAM.inp.suffix + "-CHARGE-DENSITY.restart",
195-
PARAM.globalv.gamma_only_pw || PARAM.globalv.gamma_only_local,
196-
this->pw_rhod,
197-
PARAM.inp.nspin,
198-
ucell.GT,
199-
rhog_tot,
200-
GlobalV::MY_POOL,
201-
GlobalV::RANK_IN_POOL,
202-
GlobalV::NPROC_IN_POOL);
203-
}
204186

205187
// 4) write potential
206188
if (PARAM.inp.out_pot == 1 || PARAM.inp.out_pot == 3)

source/module_esolver/esolver_ks.cpp

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "module_io/json_output/output_info.h"
88
#include "module_io/output_log.h"
99
#include "module_io/print_info.h"
10+
#include "module_io/rhog_io.h"
1011
#include "module_io/write_istate_info.h"
1112
#include "module_parameter/parameter.h"
1213

@@ -46,9 +47,6 @@ ESolver_KS<T, Device>::ESolver_KS()
4647
maxniter = PARAM.inp.scf_nmax;
4748
niter = maxniter;
4849

49-
// should not use GlobalV here, mohan 2024-05-12
50-
out_freq_elec = PARAM.inp.out_freq_elec;
51-
5250
// pw_rho = new ModuleBase::PW_Basis();
5351
// temporary, it will be removed
5452
std::string fft_device = PARAM.inp.device;
@@ -693,42 +691,50 @@ void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& i
693691
std::cout << " SCF restart after this step!" << std::endl;
694692
}
695693

696-
//! output charge density and density matrix
697-
if (this->out_freq_elec && iter % this->out_freq_elec == 0)
694+
//! output charge density
695+
if (PARAM.inp.out_chg[0] != -1)
698696
{
699-
for (int is = 0; is < PARAM.inp.nspin; is++)
697+
if (iter % PARAM.inp.out_freq_elec == 0 || iter == PARAM.inp.scf_nmax || this->conv_esolver)
700698
{
701-
double* data = nullptr;
702-
if (PARAM.inp.dm_to_rho)
703-
{
704-
data = this->pelec->charge->rho[is];
705-
}
706-
else
699+
std::complex<double>** rhog_tot
700+
= (PARAM.inp.dm_to_rho) ? this->pelec->charge->rhog : this->pelec->charge->rhog_save;
701+
double** rhor_tot = (PARAM.inp.dm_to_rho) ? this->pelec->charge->rho : this->pelec->charge->rho_save;
702+
for (int is = 0; is < PARAM.inp.nspin; is++)
707703
{
708-
data = this->pelec->charge->rho_save[is];
704+
this->pw_rhod->real2recip(rhor_tot[is], rhog_tot[is]);
709705
}
710-
std::string fn = PARAM.globalv.global_out_dir + "/tmp_SPIN" + std::to_string(is + 1) + "_CHG.cube";
711-
ModuleIO::write_vdata_palgrid(Pgrid,
712-
data,
713-
is,
714-
PARAM.inp.nspin,
715-
0,
716-
fn,
717-
this->pelec->eferm.get_efval(is),
718-
&(ucell),
719-
3,
720-
1);
706+
ModuleIO::write_rhog(PARAM.globalv.global_out_dir + PARAM.inp.suffix + "-CHARGE-DENSITY.restart",
707+
PARAM.globalv.gamma_only_pw || PARAM.globalv.gamma_only_local,
708+
this->pw_rhod,
709+
PARAM.inp.nspin,
710+
ucell.GT,
711+
rhog_tot,
712+
GlobalV::MY_POOL,
713+
GlobalV::RANK_IN_POOL,
714+
GlobalV::NPROC_IN_POOL);
715+
721716
if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5)
722717
{
723-
fn = PARAM.globalv.global_out_dir + "/tmp_SPIN" + std::to_string(is + 1) + "_TAU.cube";
724-
ModuleIO::write_vdata_palgrid(Pgrid,
725-
this->pelec->charge->kin_r_save[is],
726-
is,
727-
PARAM.inp.nspin,
728-
0,
729-
fn,
730-
this->pelec->eferm.get_efval(is),
731-
&(ucell));
718+
std::complex<double>** kin_g = new std::complex<double>*[PARAM.inp.nspin];
719+
for (int is = 0; is < PARAM.inp.nspin; is++)
720+
{
721+
kin_g[is] = new std::complex<double>[this->pelec->charge->ngmc];
722+
this->pw_rhod->real2recip(this->pelec->charge->kin_r_save[is], kin_g[is]);
723+
}
724+
ModuleIO::write_rhog(PARAM.globalv.global_out_dir + PARAM.inp.suffix + "-TAU-DENSITY.restart",
725+
PARAM.globalv.gamma_only_pw || PARAM.globalv.gamma_only_local,
726+
this->pw_rhod,
727+
PARAM.inp.nspin,
728+
ucell.GT,
729+
kin_g,
730+
GlobalV::MY_POOL,
731+
GlobalV::RANK_IN_POOL,
732+
GlobalV::NPROC_IN_POOL);
733+
for (int is = 0; is < PARAM.inp.nspin; is++)
734+
{
735+
delete[] kin_g[is];
736+
}
737+
delete[] kin_g;
732738
}
733739
}
734740
}

source/module_esolver/esolver_ks.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ class ESolver_KS : public ESolver_FP
9595
double hsolver_error; //! the error of HSolver
9696
int maxniter; //! maximum iter steps for scf
9797
int niter; //! iter steps actually used in scf
98-
int out_freq_elec; //! frequency for output
9998
};
10099
} // namespace ModuleESolver
101100
#endif

source/module_esolver/esolver_ks_pw.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -617,10 +617,10 @@ void ESolver_KS_PW<T, Device>::iter_finish(UnitCell& ucell, const int istep, int
617617
this->ppcell.cal_effective_D(veff, this->pw_rhod, ucell);
618618
}
619619

620-
if (this->out_freq_elec && iter % this->out_freq_elec == 0)
620+
// 4) Print out electronic wavefunctions
621+
if (PARAM.inp.out_wfc_pw == 1 || PARAM.inp.out_wfc_pw == 2)
621622
{
622-
// 4) Print out electronic wavefunctions
623-
if (PARAM.inp.out_wfc_pw == 1 || PARAM.inp.out_wfc_pw == 2)
623+
if (iter % PARAM.inp.out_freq_elec == 0 || iter == PARAM.inp.scf_nmax || this->conv_esolver)
624624
{
625625
std::stringstream ssw;
626626
ssw << PARAM.globalv.global_out_dir << "WAVEFUNC";

source/module_io/read_input_item_output.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ void ReadInput::item_output()
2121
}
2222
{
2323
Input_Item item("out_freq_elec");
24-
item.annotation = "the frequency ( >= 0) of electronic iter to output "
25-
"charge density and wavefunction. 0: "
26-
"output only when converged";
24+
item.annotation = "the frequency of electronic iter to output charge density and wavefunction ";
25+
item.reset_value = [](const Input_Item& item, Parameter& para) {
26+
if (para.input.out_freq_elec <= 0)
27+
{
28+
para.input.out_freq_elec = para.input.scf_nmax;
29+
}
30+
};
2731
read_sync_int(input.out_freq_elec);
2832
this->add_item(item);
2933
}

source/module_parameter/input_parameter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,7 @@ struct Input_para
316316
std::vector<int> aims_nbasis = {}; ///< the number of basis functions for each atom type used in FHI-aims (for benchmark)
317317
// ============== #Parameters (11.Output) ===========================
318318
bool out_stru = false; ///< outut stru file each ion step
319-
int out_freq_elec = 0; ///< the frequency ( >= 0) of electronic iter to output charge
320-
///< 0: output only when converged
319+
int out_freq_elec = 0; ///< the frequency of electronic iter to output charge and wavefunction
321320
int out_freq_ion = 0; ///< the frequency ( >= 0 ) of ionic step to output charge density;
322321
///< 0: output only when ion steps are finished
323322
std::vector<int> out_chg = {0, 3}; ///< output charge density. 0: no; 1: yes

0 commit comments

Comments
 (0)