Skip to content
Closed
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 @@ -760,6 +760,7 @@ OBJS_SRCPW=H_Ewald_pw.o\
sto_dos.o\
onsite_projector.o\
onsite_proj_tools.o\
onsite_proj_print.o\
vsep_pw.o

OBJS_VDW=vdw.o\
Expand Down
1 change: 1 addition & 0 deletions source/source_pw/module_pwdft/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ list(APPEND objects
radial_proj.cpp
onsite_projector.cpp
onsite_proj_tools.cpp
onsite_proj_print.cpp
vsep_pw.cpp
)

Expand Down
105 changes: 105 additions & 0 deletions source/source_pw/module_pwdft/onsite_proj_print.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "source_pw/module_pwdft/onsite_proj_print.h"
#include "source_base/formatter.h"

namespace projectors {
namespace print {

void print_orb_chg(
const UnitCell* ucell,
const std::vector<std::complex<double>>& occs,
const std::vector<int>& iat_nh,
const std::vector<std::string>& atom_labels)
{
// parameters for orbital charge output
FmtCore fmt_of_chg("%15.4f");
FmtCore fmt_of_label("%-15s");
GlobalV::ofs_running << std::endl;
GlobalV::ofs_running << "-------------------------------------------------------------------------------------------" << std::endl;
GlobalV::ofs_running << "Orbital Charge Analysis Charge Mag(x) Mag(y) Mag(z)" << std::endl;
GlobalV::ofs_running << "-------------------------------------------------------------------------------------------" << std::endl;

// parameters for mag output
std::vector<double> mag_x(ucell->nat, 0.0);
std::vector<double> mag_y(ucell->nat, 0.0);
std::vector<double> mag_z(ucell->nat, 0.0);
const std::vector<std::string> title = {"Total Magnetism (uB)", "", "", ""};
const std::vector<std::string> fmts = {"%-26s", "%20.10f", "%20.10f", "%20.10f"};
const std::vector<std::string> orb_names = {"s", "p", "d", "f", "g"};
FmtTable table(/*titles=*/title,
/*nrows=*/ucell->nat,
/*formats=*/fmts,
/*indent=*/0,
/*align=*/{/*value*/FmtTable::Align::RIGHT, /*title*/FmtTable::Align::LEFT});
// parameters for mag output
int occ_index = 0;
for(int iat=0; iat<ucell->nat; iat++)
{
const int it = ucell->iat2it[iat];
std::string atom_label = atom_labels[it];
int ia = ucell->iat2ia[iat];
GlobalV::ofs_running << FmtCore::format("%-20s", atom_label+std::to_string(ia+1)) << std::endl;
std::vector<double> sum(4, 0.0);
int current_l = 1;
std::vector<double> charge_mag(4, 0.0);
for(int ih=0; ih<iat_nh[iat]; ih++)
{
charge_mag[3] += (occs[occ_index] - occs[occ_index + 3]).real();
charge_mag[1] += (occs[occ_index + 1] + occs[occ_index + 2]).real();
charge_mag[2] += (occs[occ_index + 1] - occs[occ_index + 2]).imag();
charge_mag[0] += (occs[occ_index] + occs[occ_index + 3]).real();
if(ih == current_l * current_l - 1)
{
sum[0] += charge_mag[0];
sum[1] += charge_mag[1];
sum[2] += charge_mag[2];
sum[3] += charge_mag[3];
GlobalV::ofs_running << FmtCore::format("%20s", orb_names[current_l-1])
<< fmt_of_chg.format(charge_mag[0]) << fmt_of_chg.format(charge_mag[1])
<< fmt_of_chg.format(charge_mag[2]) << fmt_of_chg.format(charge_mag[3]) << std::endl;
current_l++;
charge_mag.assign(4, 0.0);
}
occ_index += 4;
}
mag_x[iat] = sum[1];
mag_y[iat] = sum[2];
mag_z[iat] = sum[3];
GlobalV::ofs_running << FmtCore::format("%20s", std::string("Sum")) << ""
<< fmt_of_chg.format(sum[0]) << fmt_of_chg.format(sum[1])
<< fmt_of_chg.format(sum[2]) << fmt_of_chg.format(sum[3]) << std::endl;
}
GlobalV::ofs_running << "-------------------------------------------------------------------------------------------" << std::endl;
GlobalV::ofs_running << std::endl;

// Print magnetism table
print_mag_table(atom_labels, mag_x, mag_y, mag_z);
}

void print_mag_table(
const std::vector<std::string>& atom_labels,
const std::vector<double>& mag_x,
const std::vector<double>& mag_y,
const std::vector<double>& mag_z)
{
const std::vector<std::string> title = {"Total Magnetism (uB)", "", "", ""};
const std::vector<std::string> fmts = {"%-26s", "%20.10f", "%20.10f", "%20.10f"};
FmtTable table(/*titles=*/title,
/*nrows=*/mag_x.size(),
/*formats=*/fmts,
/*indent=*/0,
/*align=*/{/*value*/FmtTable::Align::RIGHT, /*title*/FmtTable::Align::LEFT});

table << atom_labels << mag_x << mag_y << mag_z;
GlobalV::ofs_running << table.str() << std::endl;
}

void print_proj_status(int it, int nproj_it)
{
if(nproj_it == 0)
{
std::cout << "BECP_PW >> No projectors defined for type " << it << std::endl;
}
}

} // namespace print
} // namespace projectors
52 changes: 52 additions & 0 deletions source/source_pw/module_pwdft/onsite_proj_print.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#ifndef MODULEHAMILTPW_ONSITE_PROJ_PRINT_H
#define MODULEHAMILTPW_ONSITE_PROJ_PRINT_H

#include "source_cell/unitcell.h"
#include "source_base/global_variable.h"
#include <vector>
#include <string>
#include <complex>

namespace projectors {
namespace print {

/**
* @brief Print orbital charge analysis
*
* @param ucell Unit cell pointer
* @param occs Occupation numbers
* @param iat_nh Number of projectors per atom
* @param atom_labels Atom labels
*/
void print_orb_chg(
const UnitCell* ucell,
const std::vector<std::complex<double>>& occs,
const std::vector<int>& iat_nh,
const std::vector<std::string>& atom_labels);

/**
* @brief Print magnetism table
*
* @param atom_labels Atom labels
* @param mag_x Magnetic moment in x direction
* @param mag_y Magnetic moment in y direction
* @param mag_z Magnetic moment in z direction
*/
void print_mag_table(
const std::vector<std::string>& atom_labels,
const std::vector<double>& mag_x,
const std::vector<double>& mag_y,
const std::vector<double>& mag_z);

/**
* @brief Print projector initialization status
*
* @param it Atom type index
* @param nproj_it Number of projectors for this type
*/
void print_proj_status(int it, int nproj_it);

} // namespace print
} // namespace projectors

#endif // MODULEHAMILTPW_ONSITE_PROJ_PRINT_H
70 changes: 5 additions & 65 deletions source/source_pw/module_pwdft/onsite_projector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <map>
#include <tuple>
#include "source_pw/module_pwdft/onsite_projector.h"
#include "source_pw/module_pwdft/onsite_proj_print.h"

#include "source_base/projgen.h"
#include "source_base/kernels/math_kernel_op.h"
Expand Down Expand Up @@ -226,9 +227,9 @@ void projectors::OnsiteProjector<T, Device>::init_proj(const std::string& orbita
{
const int nproj_it = nproj[it];
this->it2iproj[it].resize(nproj_it);
print::print_proj_status(it, nproj_it);
if(nproj_it == 0)
{
std::cout << "BECP_PW >> No projectors defined for type " << it << std::endl;
continue;
}
std::ifstream ifs(orbital_dir + orb_files[it]);
Expand Down Expand Up @@ -573,70 +574,9 @@ void projectors::OnsiteProjector<T, Device>::cal_occupations(
const int npool = GlobalV::KPAR * PARAM.inp.bndpar;
Parallel_Reduce::reduce_double_allpool(npool, GlobalV::NPROC_IN_POOL, (double*)(&(occs[0])), occs.size()*2);
// occ has been reduced and calculate mag
// parameters for orbital charge output
FmtCore fmt_of_chg("%15.4f");
FmtCore fmt_of_label("%-15s");
GlobalV::ofs_running << std::endl;
GlobalV::ofs_running << "-------------------------------------------------------------------------------------------" << std::endl;
GlobalV::ofs_running << "Orbital Charge Analysis Charge Mag(x) Mag(y) Mag(z)" << std::endl;
GlobalV::ofs_running << "-------------------------------------------------------------------------------------------" << std::endl;

// parameters for orbital charge output
// parameters for mag output
std::vector<double> mag_x(this->ucell->nat, 0.0);
std::vector<double> mag_y(this->ucell->nat, 0.0);
std::vector<double> mag_z(this->ucell->nat,0.0);
auto atomLabels = this->ucell->get_atomLabels();
const std::vector<std::string> title = {"Total Magnetism (uB)", "", "", ""};
const std::vector<std::string> fmts = {"%-26s", "%20.10f", "%20.10f", "%20.10f"};
const std::vector<std::string> orb_names = {"s", "p", "d", "f", "g"};
FmtTable table(/*titles=*/title,
/*nrows=*/this->ucell->nat,
/*formats=*/fmts,
/*indent=*/0,
/*align=*/{/*value*/FmtTable::Align::RIGHT, /*title*/FmtTable::Align::LEFT});
// parameters for mag output
int occ_index = 0;
for(int iat=0;iat<this->ucell->nat;iat++)
{
const int it = this->ucell->iat2it[iat];
std::string atom_label = atomLabels[it];
int ia = this->ucell->iat2ia[iat];
GlobalV::ofs_running << FmtCore::format("%-20s", atom_label+std::to_string(ia+1)) << std::endl;
std::vector<double> sum(4, 0.0);
int current_l = 1;
std::vector<double> charge_mag(4, 0.0);
for(int ih=0;ih<this->iat_nh[iat];ih++)
{
charge_mag[3] += (occs[occ_index] - occs[occ_index + 3]).real();
charge_mag[1] += (occs[occ_index + 1] + occs[occ_index + 2]).real();
charge_mag[2] += (occs[occ_index + 1] - occs[occ_index + 2]).imag();
charge_mag[0] += (occs[occ_index] + occs[occ_index + 3]).real();
if(ih == current_l * current_l - 1)
{
sum[0] += charge_mag[0];
sum[1] += charge_mag[1];
sum[2] += charge_mag[2];
sum[3] += charge_mag[3];
GlobalV::ofs_running << FmtCore::format("%20s", orb_names[current_l-1])
<< fmt_of_chg.format(charge_mag[0]) << fmt_of_chg.format(charge_mag[1])
<< fmt_of_chg.format(charge_mag[2]) << fmt_of_chg.format(charge_mag[3]) << std::endl;
current_l++;
charge_mag.assign(4, 0.0);
}
occ_index += 4;
}
mag_x[iat] = sum[1];
mag_y[iat] = sum[2];
mag_z[iat] = sum[3];
GlobalV::ofs_running << FmtCore::format("%20s", std::string("Sum")) << ""
<< fmt_of_chg.format(sum[0]) << fmt_of_chg.format(sum[1])
<< fmt_of_chg.format(sum[2]) << fmt_of_chg.format(sum[3]) << std::endl;
}
GlobalV::ofs_running << "-------------------------------------------------------------------------------------------" << std::endl;
GlobalV::ofs_running << std::endl;
table << atomLabels << mag_x << mag_y << mag_z;
GlobalV::ofs_running << table.str() << std::endl;
// Print orbital charge analysis
auto atom_labels = this->ucell->get_atomLabels();
print::print_orb_chg(this->ucell, occs, this->iat_nh, atom_labels);

// print charge
ModuleBase::timer::end("OnsiteProj", "cal_occupation");
Expand Down
Loading