diff --git a/source/source_base/module_out/filename.cpp b/source/source_base/module_out/filename.cpp index cbd4d28749..a1be32a387 100644 --- a/source/source_base/module_out/filename.cpp +++ b/source/source_base/module_out/filename.cpp @@ -17,7 +17,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)} @@ -126,7 +127,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; diff --git a/source/source_base/module_out/filename.h b/source/source_base/module_out/filename.h index d95b24c245..507c3192b6 100644 --- a/source/source_base/module_out/filename.h +++ b/source/source_base/module_out/filename.h @@ -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( @@ -37,7 +39,8 @@ std::string filename_output( const int out_app_flag, const bool gamma_only, const int istep=-1, - const int iter=-1); + const int iter=-1, + const std::string &tag=""); } #endif diff --git a/source/source_esolver/esolver_sdft_pw.cpp b/source/source_esolver/esolver_sdft_pw.cpp index d648da9760..0220857a73 100644 --- a/source/source_esolver/esolver_sdft_pw.cpp +++ b/source/source_esolver/esolver_sdft_pw.cpp @@ -1,12 +1,15 @@ #include "esolver_sdft_pw.h" #include "source_base/global_variable.h" +#include "source_base/fs_compat.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" #include "source_hsolver/diago_iter_assist.h" #include "source_hsolver/diago_params.h" #include "source_io/module_parameter/parameter.h" +#include "source_io/module_wf/write_wfc_pw.h" #include "source_pw/module_stodft/sto_dos.h" #include "source_pw/module_stodft/sto_elecond.h" #include "source_pw/module_stodft/sto_forces.h" @@ -129,6 +132,114 @@ void ESolver_SDFT_PW::after_scf(UnitCell &ucell, const int istep, con // 1) call after_scf() of ESolver_KS_PW ESolver_KS_PW::after_scf(ucell, istep, conv_esolver); + // 2) dump the SDFT/MDFT wavefunctions through the same writer that the + // KSDFT path uses (ModuleIO::write_wfc_pw), so the file layout, the + // binary mode and the collective pattern are shared. `out_wfc_pw` is + // reused as the switch; nothing is written for other values. + // + // Every band group enters the writer even when it owns no orbital, + // because the writer synchronises the whole world: returning early for + // an empty group would leave the other groups waiting in MPI_Barrier. + if (istep != 0 || this->inp_->basis_type != "pw" + || (this->inp_->out_wfc_pw != 1 && this->inp_->out_wfc_pw != 2)) + { + ModuleBase::timer::end("ESolver_SDFT_PW", "after_scf"); + return; + } + + ModuleBase::TITLE("ESolver_SDFT_PW", "after_scf_dump_wfc"); + + // On device builds the master copy of the deterministic orbitals lives on + // the device; bring the host copy up to date before reading it, exactly as + // the KSDFT output path does (ModuleIO::ctrl_iter_pw). + this->stp.sync_cpu(); + const std::string psi_dir = PARAM.globalv.global_psi_dir; + ModuleBase::ensure_directory(psi_dir); + + // The number of stochastic orbitals can differ between k points when + // `nbands_sto all` is used; dump only the rows that every k point owns, so + // that the header and the coefficients agree and no uncomputed padding row + // is written. + int nsto = 0; + if (this->stowf.nchip_max > 0 && this->stowf.nchip != nullptr) + { + nsto = this->stowf.nchip[0]; + for (int ik = 1; ik < this->kv.get_nks(); ++ik) + { + nsto = std::min(nsto, this->stowf.nchip[ik]); + } + } + + // At most four orbitals of each set are dumped: enough to inspect the + // orbitals in real space without writing hundreds of files per run. + const int max_dump = 4; + const int nband_ks = std::min(this->stp.psi_cpu->get_nbands(), max_dump); + const int nband_sto = std::min(nsto, max_dump); + const int nband_filtered = std::min(nsto, max_dump); + const int nband_chiortho = std::min(this->stp.psi_cpu->get_nbands(), 2); + + auto dump = [&](const std::string& tag, const psi::Psi>& psi, const int nbands_out) { + ModuleIO::write_wfc_pw(-1, // -1 keeps the ionic step out of the file name + -1, // same for the electronic step + -1, // a single-point dump always truncates + this->inp_->kpar, + GlobalV::MY_POOL, + 0, // my_rank: unused by the writer + nbands_out, + this->inp_->nspin, + PARAM.globalv.npol, + this->pw_wfc->poolrank, + this->pw_wfc->poolnproc, + this->inp_->out_wfc_pw, + 0, // out_app_flag: one frame, no append + this->inp_->ecutwfc, + psi_dir, + psi, + this->kv, + this->pw_wfc, + GlobalV::ofs_running, + tag); + }; + + // With band parallelism every band group owns a different subset of the + // orbitals, so the files of the groups must not share a name. + std::string bg = ""; + if (this->inp_->bndpar > 1) + { + bg = "_bg" + std::to_string(GlobalV::MY_BNDGROUP); + } + + if (this->inp_->nbands > 0 && this->stp.psi_cpu != nullptr) + { + dump(bg + "_ks", *this->stp.psi_cpu, nband_ks); + } + if (this->inp_->nbands_sto > 0 && this->stowf.chi0_cpu != nullptr) + { + dump(bg + "_sto", *this->stowf.chi0_cpu, nband_sto); + } + // The filtered and the KS-orthogonalized stochastic orbitals live on the + // wavefunction device (and may use a single-precision type), while the + // writer works on host double-precision data. Copying through + // cast_memory_op handles device -> host and float -> double in one step, so + // all four dump sets are available on CPU and GPU builds alike. + auto dump_on_host = [&](const std::string& tag, const psi::Psi& src, const int nbands_out) { + psi::Psi> host( + this->kv.get_nks(), src.get_nbands(), this->pw_wfc->npwk_max, this->kv.ngk, true); + base_device::memory::cast_memory_op, T, base_device::DEVICE_CPU, Device>()( + host.get_pointer() - host.get_psi_bias(), + src.get_pointer() - src.get_psi_bias(), + host.size()); + dump(tag, host, nbands_out); + }; + if (this->stowf.shchi != nullptr) + { + dump_on_host(bg + "_shchi", *this->stowf.shchi, nband_filtered); + } + if (this->inp_->nbands > 0 && this->stowf.chiortho != nullptr) + { + dump_on_host(bg + "_chiortho", *this->stowf.chiortho, nband_chiortho); + } + ModuleBase::timer::end("ESolver_SDFT_PW", "after_scf"); } diff --git a/source/source_io/module_parameter/read_inp_out.cpp b/source/source_io/module_parameter/read_inp_out.cpp index 5b66f054b1..815dd5730a 100644 --- a/source/source_io/module_parameter/read_inp_out.cpp +++ b/source/source_io/module_parameter/read_inp_out.cpp @@ -316,7 +316,15 @@ The optional second integer controls the output precision. If not provided, the 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 +only written when the wavefunction device is the CPU, and with `bndpar > 1` every band group +writes its own `bg` files (a band group without orbitals writes a header-only file). Available values are: * `0`: Do not write wavefunction coefficients. @@ -332,7 +340,7 @@ With `init_wfc file binary`, ABACUS reads only unindexed binary `wf*_pw.dat` fil [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); } diff --git a/source/source_io/module_wf/write_wfc_pw.cpp b/source/source_io/module_wf/write_wfc_pw.cpp index 2371013917..7d32981c22 100644 --- a/source/source_io/module_wf/write_wfc_pw.cpp +++ b/source/source_io/module_wf/write_wfc_pw.cpp @@ -31,7 +31,8 @@ void ModuleIO::write_wfc_pw( const psi::Psi>& 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"); @@ -58,7 +59,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); diff --git a/source/source_io/module_wf/write_wfc_pw.h b/source/source_io/module_wf/write_wfc_pw.h index fbe6c38741..f400cf1d86 100644 --- a/source/source_io/module_wf/write_wfc_pw.h +++ b/source/source_io/module_wf/write_wfc_pw.h @@ -30,7 +30,8 @@ void write_wfc_pw( const psi::Psi>& psi, const K_Vectors& kv, const ModulePW::PW_Basis_K* wfcpw, - std::ofstream &ofs_running); + std::ofstream &ofs_running, + const std::string& property_tag = ""); }