Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions source/Makefile.Objects
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,7 @@ OBJS_SRCPW=h_ewald_pw.o\
sto_tool.o\
sto_elecond.o\
sto_dos.o\
sto_wfc_output.o\
onsite_proj.o\
onsite_proj_init.o\
onsite_proj_overlap.o\
Expand Down
16 changes: 14 additions & 2 deletions source/source_base/module_out/filename.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

namespace ModuleIO
{
std::string filename_output(const std::string& directory, const std::string& property,
const std::string& basis, const int ik_local,
const std::vector<int>& ik2iktot, const int nspin, const int nkstot,
const int out_type, const int out_app_flag, const bool gamma_only,
const int istep, const int iter)
{
const std::string tag;
return filename_output(directory, property, basis, ik_local, ik2iktot, nspin, nkstot,
out_type, out_app_flag, gamma_only, istep, iter, tag);
}


std::string filename_output(
const std::string &directory,
Expand All @@ -17,7 +28,8 @@ std::string filename_output(
const int out_app_flag,
const bool gamma_only,
const int istep,
const int iter)
const int iter,
const std::string &tag)
{
// output filename = "{global_out_dir}/property{s}{spin index}
// {k(optional)}{k-point index}{g(optional)}{geometry index1}{_basis(nao|pw)}
Expand Down Expand Up @@ -126,7 +138,7 @@ std::string filename_output(
}

std::string fn_out
= directory + property + kpoint_block + spin_block
= directory + property + tag + kpoint_block + spin_block
+ istep_block + iter_block + "_" + basis + suffix_block;

return fn_out;
Expand Down
17 changes: 17 additions & 0 deletions source/source_base/module_out/filename.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace ModuleIO
* filename in mode 2 (out_app_flag == 2), and only when istep >= 0.
* @param iter: index of the electronic step starting from 0. The step number is only embedded in
* the filename in mode 2 (out_app_flag == 2), and only when iter >= 0.
* @param tag: optional tag inserted after the property, used to write several independent dumps of
* the same property in one run (e.g. the stochastic orbitals of an SDFT run).
* @return The generated filename.
*/
std::string filename_output(
Expand All @@ -39,5 +41,20 @@ std::string filename_output(
const int istep=-1,
const int iter=-1);

std::string filename_output(
const std::string &directory,
const std::string &property,
const std::string &basis,
const int ik_local,
const std::vector<int> &ik2iktot,
const int nspin,
const int nkstot,
const int out_type,
const int out_app_flag,
const bool gamma_only,
const int istep,
const int iter,
const std::string &tag);

}
#endif
6 changes: 3 additions & 3 deletions source/source_esolver/esolver_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ std::string determine_type(const Input_para &inp)
}

// Some API to operate E_Solver
ESolver *init_esolver(const Input_para &inp)
ESolver *init_esolver(const Input_para &inp, const ModuleIO::OutputContext& output)
{
// determine type of esolver based on INPUT information
const std::string esolver_type = determine_type(inp);
Expand Down Expand Up @@ -226,7 +226,7 @@ ESolver *init_esolver(const Input_para &inp)
// }
// else
// {
return new ESolver_SDFT_PW<std::complex<double>, base_device::DEVICE_GPU>();
return new ESolver_SDFT_PW<std::complex<double>, base_device::DEVICE_GPU>(output);
// }
}
#endif
Expand All @@ -236,7 +236,7 @@ ESolver *init_esolver(const Input_para &inp)
// }
// else
// {
return new ESolver_SDFT_PW<std::complex<double>, base_device::DEVICE_CPU>();
return new ESolver_SDFT_PW<std::complex<double>, base_device::DEVICE_CPU>(output);
// }
}
#ifdef __LCAO
Expand Down
3 changes: 2 additions & 1 deletion source/source_esolver/esolver_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define ESOLVER_FACTORY_H

#include <string>
#include "source_io/module_output/output_context.h"

struct Input_para;

Expand Down Expand Up @@ -32,7 +33,7 @@ std::string determine_type(const Input_para &inp);
* @param [in] inp Input parameters used to select and configure the ESolver.
* @return [out] A pointer to the newly created ESolver object.
*/
ESolver *init_esolver(const Input_para &inp);
ESolver *init_esolver(const Input_para &inp, const ModuleIO::OutputContext& output);

} // namespace ModuleESolver

Expand Down
14 changes: 12 additions & 2 deletions source/source_esolver/esolver_sdft_pw.cpp

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

output_wfc_sdft should not belong to 'esolver_sdft_pw', it should be put inside the function 'after_scf'

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: output_wfc_sdft() removed, the dump is inside after_scf().

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "esolver_sdft_pw.h"

#include "source_base/global_variable.h"
#include "source_base/module_device/memory_op.h"
#include "source_base/memory_recorder.h"
#include "source_base/parallel_comm.h"
#include "source_estate/module_charge/chg_symm.h"
Expand All @@ -9,6 +10,7 @@
#include "source_io/module_parameter/parameter.h"
#include "source_lcao/module_deltaspin/spin_constrain.h"
#include "source_pw/module_stodft/sto_dos.h"
#include "source_pw/module_stodft/sto_wfc_output.h"
#include "source_pw/module_stodft/sto_elecond.h"
#include "source_pw/module_stodft/sto_forces.h"
#include "source_pw/module_stodft/sto_hsolver_pw.h"
Expand All @@ -21,7 +23,8 @@ namespace ModuleESolver
{

template <typename T, typename Device>
ESolver_SDFT_PW<T, Device>::ESolver_SDFT_PW()
ESolver_SDFT_PW<T, Device>::ESolver_SDFT_PW(const ModuleIO::OutputContext& output_in)
: output(output_in)
{
this->classname = "ESolver_SDFT_PW";
this->basisname = "PW";
Expand Down Expand Up @@ -137,6 +140,13 @@ void ESolver_SDFT_PW<T, Device>::after_scf(UnitCell &ucell, const int istep, con
// 1) call after_scf() of ESolver_KS_PW
ESolver_KS_PW<T, Device>::after_scf(ucell, istep, conv_esolver);

if (istep == 0 && (this->inp_->out_wfc_pw == 1 || this->inp_->out_wfc_pw == 2))
{
const auto* ks = this->stp.template get_psi_t<T, Device>();
ModuleIO::write_sto_wfc(this->output, *this->inp_, this->kv, *this->pw_wfc, *ks, this->stowf);
}


ModuleBase::timer::end("ESolver_SDFT_PW", "after_scf");
}

Expand Down Expand Up @@ -171,7 +181,7 @@ void ESolver_SDFT_PW<T, Device>::hamilt2rho_single(

sto_hsolver_pw_obj.solve(
ucell, static_cast<hamilt::Hamilt<T, Device> *>(this->p_hamilt), *this->stp.template get_psi_t<T, Device>(),
this->stp.psi_cpu[0], this->pelec, this->pw_wfc, this->stowf, istep, iter, GlobalV::ofs_running, skip_charge);
this->stp.psi_cpu[0], this->pelec, this->pw_wfc, this->stowf, istep, iter, this->output.log, skip_charge);

// set_diagethr need it
this->esolver_KS_ne = sto_hsolver_pw_obj.stoiter.KS_ne;
Expand Down
4 changes: 3 additions & 1 deletion source/source_esolver/esolver_sdft_pw.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define ESOLVER_SDFT_PW_H

#include "esolver_ks_pw.h"
#include "source_io/module_output/output_context.h"
#include "source_pw/module_stodft/sto_che.h"
#include "source_pw/module_stodft/sto_hamilt_pw.h"
#include "source_pw/module_stodft/sto_iter.h"
Expand All @@ -17,7 +18,7 @@ class ESolver_SDFT_PW : public ESolver_KS_PW<T, Device>
using Real = typename GetTypeReal<T>::type;

public:
ESolver_SDFT_PW();
ESolver_SDFT_PW(const ModuleIO::OutputContext& output);
~ESolver_SDFT_PW();

void before_all_runners(BaseCell &basecell, const Input_para &inp) override;
Expand Down Expand Up @@ -53,6 +54,7 @@ class ESolver_SDFT_PW : public ESolver_KS_PW<T, Device>
virtual void after_all_runners(BaseCell &basecell) override;

private:
const ModuleIO::OutputContext output;
int nche_sto; ///< norder of Chebyshev
int method_sto; ///< method of SDFT
};
Expand Down
21 changes: 21 additions & 0 deletions source/source_io/module_output/output_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef MODULEIO_OUTPUT_CONTEXT_H
#define MODULEIO_OUTPUT_CONTEXT_H

#include <fstream>
#include <string>

namespace ModuleIO
{
// Application-owned output destinations and parallel identity, passed explicitly
// to solvers. No workflow state or global lookups are kept in this context.
struct OutputContext
{
std::string directory;
std::string wavefunction_directory;
std::ofstream& log;
int rank;
int pool;
int band_group;
};
}
#endif
16 changes: 12 additions & 4 deletions source/source_io/module_parameter/read_inp_out.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,15 @@ For format 2, the files are named dmrs{spin index}_nao.npz, where the spin index
item.annotation = "output wave functions";
item.category = "Output information";
item.type = "Integer";
item.description = R"(Controls whether plane-wave Kohn-Sham wavefunction coefficients are written to `OUT.${suffix}/`.
item.description = R"(Controls whether plane-wave wavefunction coefficients are written to `OUT.${suffix}/`.
For `esolver_type=ksdft` the Kohn-Sham coefficients are written. For `esolver_type=sdft` this
parameter switches on the same writer for the SDFT/MDFT orbitals, once, at the first ionic step:
at most four deterministic (Kohn-Sham) orbitals (`wf_ks...`), at most four raw stochastic
orbitals (`wf_sto...`), at most four occupation-filtered stochastic orbitals (`wf_shchi...`, the
ones that enter the charge density without a separate diagonalization) and at most two stochastic
orbitals orthogonalized against the Kohn-Sham subspace (`wf_chiortho...`). The two last sets are
written on CPU and GPU builds; with `bndpar > 1` every band group
writes its own `bg<i>` files (a band group without orbitals writes a header-only file).

Available values are:
* `0`: Do not write wavefunction coefficients.
Expand All @@ -382,14 +390,14 @@ Available values are:

The file-name pattern is `wfk{k}[s{spin}][g{geometry step}][e{electronic iteration}]_pw.txt` for `out_wfc_pw=1` and `wfk{k}[s{spin}][g{geometry step}][e{electronic iteration}]_pw.dat` for `out_wfc_pw=2`. All PW output files include a `k*` label, including Gamma-only calculations. Without geometry-step or electronic-iteration indices, representative names are `wfk1_pw.txt` or `wfk1_pw.dat` for `nspin=1`, `wfk1s1_pw.txt` and `wfk1s2_pw.txt` or their `.dat` equivalents for `nspin=2`, and `wfk1s4_pw.txt` or `wfk1s4_pw.dat` for `nspin=4`.

With `out_freq_ion=0`, files are written only when the electronic calculation converges or reaches `scf_nmax`; no `g*` or `e*` index is added. During structural relaxation or molecular dynamics with `out_app_flag=1`, the first written step truncates the unindexed files and later ionic steps append one frame per step (mirroring `out_wfc_lcao`); with `out_app_flag=0` the unindexed files are overwritten each step. With `out_freq_ion` > 0, output is restricted to the ionic steps selected by `out_freq_ion`. For `out_app_flag=0` or `2`, output is written when the electronic iteration is a multiple of `out_freq_elec`, when the calculation converges, or when it reaches `scf_nmax`; for `out_app_flag=1` only the last electronic iteration of each selected ionic step is written (out_freq_elec is ignored), so the appended file holds exactly one frame per step. Both `g*` and `e*` indices are then added only when `out_app_flag=2`; with `out_app_flag=0` the fixed files are truncated and overwritten at every write, and with `out_app_flag=1` they accumulate one frame per written step.
For KSDFT, with `out_freq_ion=0`, files are written only when the electronic calculation converges or reaches `scf_nmax`; no `g*` or `e*` index is added. During structural relaxation or molecular dynamics with `out_app_flag=1`, the first written step truncates the unindexed files and later ionic steps append one frame per step (mirroring `out_wfc_lcao`); with `out_app_flag=0` the unindexed files are overwritten each step. With `out_freq_ion` > 0, output is restricted to the ionic steps selected by `out_freq_ion`. For `out_app_flag=0` or `2`, output is written when the electronic iteration is a multiple of `out_freq_elec`, when the calculation converges, or when it reaches `scf_nmax`; for `out_app_flag=1` only the last electronic iteration of each selected ionic step is written (out_freq_elec is ignored), so the appended file holds exactly one frame per step. Both `g*` and `e*` indices are then added only when `out_app_flag=2`; with `out_app_flag=0` the fixed files are truncated and overwritten at every write, and with `out_app_flag=1` they accumulate one frame per written step.

With `init_wfc file binary`, ABACUS reads only unindexed binary `wf*_pw.dat` files from `read_file_dir`, starting from the first frame. Such directly reusable files are normally generated with `out_wfc_pw=2` and `out_freq_ion=0`; multi-frame files accumulated under `out_app_flag=1` are trajectories and only their first frame is visible to the reader. Text `wf*_pw.txt` files and files containing `g*` or `e*` indices are not matched automatically.
For KSDFT, with `init_wfc file binary`, ABACUS reads only unindexed binary `wf*_pw.dat` files from `read_file_dir`, starting from the first frame. Such directly reusable files are normally generated with `out_wfc_pw=2` and `out_freq_ion=0`; multi-frame files accumulated under `out_app_flag=1` are trajectories and only their first frame is visible to the reader. Text `wf*_pw.txt` files and files containing `g*` or `e*` indices are not matched automatically.

[NOTE] In the 3.10-LTS version, the binary files are named `WAVEFUNC1.dat`, `WAVEFUNC2.dat`, etc.)";
item.default_value = "0";
item.unit = "";
item.set_availability("basis_type==pw and esolver_type==ksdft");
item.set_availability("basis_type==pw and (esolver_type==ksdft or esolver_type==sdft)");
read_sync_int(input.out_wfc_pw);
this->add_item(item);
}
Expand Down
73 changes: 71 additions & 2 deletions source/source_io/module_wf/write_wfc_pw.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "write_wfc_pw.h"
#include "source_base/tool_quit.h"

#ifdef __MPI
#include "mpi.h"
Expand Down Expand Up @@ -31,7 +32,8 @@ void ModuleIO::write_wfc_pw(
const psi::Psi<std::complex<double>>& psi,
const K_Vectors& kv,
const ModulePW::PW_Basis_K* wfcpw,
std::ofstream &ofs_running)
std::ofstream &ofs_running,
const std::string& property_tag)
{
ModuleBase::TITLE("ModuleIO", "write_wfc_pw");

Expand All @@ -58,7 +60,7 @@ void ModuleIO::write_wfc_pw(
{
std::string fn = filename_output(global_out_dir,"wf","pw",
ik_local,kv.ik2iktot,nspin,nkstot,
out_wfc_pw,out_app_flag,gamma_only,istep,iter);
out_wfc_pw,out_app_flag,gamma_only,istep,iter,property_tag);

ModuleIO::write_notice(ofs_running, "G-space wave functions", fn);

Expand All @@ -79,7 +81,17 @@ void ModuleIO::write_wfc_pw(
if (out_wfc_pw == 1)
{
std::ofstream ofs(fn.c_str()); // clear all wavefunc files.
if (!ofs)
{
const std::string message = "Cannot open " + fn;
ModuleBase::WARNING_QUIT("write_wfc_pw", message);
}
ofs.close();
if (!ofs)
{
const std::string message = "Cannot write " + fn;
ModuleBase::WARNING_QUIT("write_wfc_pw", message);
}
}
else if (out_wfc_pw == 2)
{
Expand Down Expand Up @@ -130,6 +142,11 @@ void ModuleIO::write_wfc_pw(
if (out_wfc_pw == 1)
{
std::ofstream ofs2(wfilename[ik].c_str(), std::ios::app);
if (!ofs2)
{
const std::string message = "Cannot open " + wfilename[ik];
ModuleBase::WARNING_QUIT("write_wfc_pw", message);
}
if (id == 0)
{
ofs2 << std::setprecision(6);
Expand Down Expand Up @@ -169,6 +186,11 @@ void ModuleIO::write_wfc_pw(
ofs2 << "<G vectors>\n" << std::endl;
}
ofs2.close();
if (!ofs2)
{
const std::string message = "Cannot write " + wfilename[ik];
ModuleBase::WARNING_QUIT("write_wfc_pw", message);
}
}
else if (out_wfc_pw == 2)
{
Expand Down Expand Up @@ -221,6 +243,11 @@ void ModuleIO::write_wfc_pw(
if (out_wfc_pw == 1)
{
std::ofstream ofs2(wfilename[ik].c_str(), std::ios::app);
if (!ofs2)
{
const std::string message = "Cannot open " + wfilename[ik];
ModuleBase::WARNING_QUIT("write_wfc_pw", message);
}
if (id == 0)
{
ofs2 << "\n< Band " << ib + 1 << " >" << std::endl;
Expand All @@ -239,6 +266,11 @@ void ModuleIO::write_wfc_pw(
ofs2 << "\n< Band " << ib + 1 << " >" << std::endl;
}
ofs2.close();
if (!ofs2)
{
const std::string message = "Cannot write " + wfilename[ik];
ModuleBase::WARNING_QUIT("write_wfc_pw", message);
}
}
else if (out_wfc_pw == 2)
{
Expand Down Expand Up @@ -275,6 +307,11 @@ void ModuleIO::write_wfc_pw(
if (out_wfc_pw == 1)
{
std::ofstream ofs2(wfilename[ik].c_str(), std::ios::app);
if (!ofs2)
{
const std::string message = "Cannot open " + wfilename[ik];
ModuleBase::WARNING_QUIT("write_wfc_pw", message);
}

ofs2 << std::scientific;
for (int ig = 0; ig < ng; ig++)
Expand All @@ -291,6 +328,11 @@ void ModuleIO::write_wfc_pw(
ofs2 << "\n< Band " << ib + 1 << " >" << std::endl;
}
ofs2.close();
if (!ofs2)
{
const std::string message = "Cannot write " + wfilename[ik];
ModuleBase::WARNING_QUIT("write_wfc_pw", message);
}
}
else if (out_wfc_pw == 2)
{
Expand Down Expand Up @@ -321,3 +363,30 @@ void ModuleIO::write_wfc_pw(
delete[] wfilename;
return;
}

void ModuleIO::write_wfc_pw(
const int istep,
const int iter,
const int append_step,
const int kpar,
const int my_pool,
const int my_rank,
const int nbands,
const int nspin,
const int npol,
const int rank_in_pool,
const int nproc_in_pool,
const int out_wfc_pw,
const int out_app_flag,
const double& ecutwfc,
const std::string& global_out_dir,
const psi::Psi<std::complex<double>>& psi,
const K_Vectors& kv,
const ModulePW::PW_Basis_K* wfcpw,
std::ofstream &ofs_running)
{
const std::string property_tag;
write_wfc_pw(istep, iter, append_step, kpar, my_pool, my_rank, nbands, nspin, npol,
rank_in_pool, nproc_in_pool, out_wfc_pw, out_app_flag, ecutwfc, global_out_dir,
psi, kv, wfcpw, ofs_running, property_tag);
}
Loading
Loading