Skip to content
Merged
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
32 changes: 20 additions & 12 deletions source/module_base/formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,22 @@ class FmtTable
*
* @param titles titles, its size should be the same as the number of columns
* @param nrows number of rows
* @param aligns Alignments instance, can be constructed with initializer_list<char> like {'r', 'c'}, for right and center alignment for values and titles
* @param fmts format strings for each column, its size should be the same as the number of columns
* @param indent indent for each column, default is 0
* @param aligns Alignments instance, for alignment of values and titles, e.g. {Align::LEFT, Align::RIGHT} for left alignment of values and right alignment of titles
* @param frames Frames instance, can be constructed with initializer_list<char> like {'-', '-', '-', ' ', ' '}, for up, middle, down, left and right frames
* @param delimiters Delimiters instance, can be constructed with initializer_list<char> like {'-', ' '}, for horizontal and vertical delimiters
*/
FmtTable(const std::vector<std::string>& titles,
const size_t& nrows,
const size_t nrows,
const std::vector<std::string>& fmts,
const size_t indent = 0,
const Alignments& aligns = {},
const Frames& frames = {},
const Delimiters& delimiters = {}): titles_(titles), data_(nrows, titles.size()), fmts_(fmts), aligns_(aligns), frames_(frames), delimiters_(delimiters)
{ assert(titles.size() == fmts.size()); };
const Delimiters& delimiters = {}):
titles_(titles), data_(nrows, titles.size()), // data
fmts_(fmts), indent_(indent), aligns_(aligns), frames_(frames), delimiters_(delimiters) // styles
{ assert(titles.size() == fmts.size()||titles.size() == 0); };
~FmtTable() {};
/**
* @brief import data from std::vector
Expand Down Expand Up @@ -269,22 +274,24 @@ class FmtTable
*/
std::string concat_title(const std::vector<std::string>& titles) const
{
std::string dst;
std::string dst = "";
// first sum width of all titles
size_t width = std::accumulate(titles.begin(), titles.end(), 0, [](const size_t& acc, const std::string& s) { return acc + s.size(); });
// add width of delimiters
width += titles.size() - 1;
// add width of left and right frames
width += 2;
dst += std::string(width, frames_.up_) + "\n" + std::string(1, frames_.l_);
dst += std::string(indent_, ' ') + std::string(width, frames_.up_) + "\n"; // first line: the upper frame
dst += std::string(indent_, ' ') + std::string(1, frames_.l_); // second line: the left frame + titles + right frame
for(size_t i = 0; i < titles.size(); i++)
{
dst += titles[i];
if (i != titles.size() - 1) {
dst += delimiters_.v_;
}
}
dst += std::string(1, frames_.r_) + "\n" + std::string(width, frames_.mid_) + "\n";
dst += std::string(1, frames_.r_) + "\n";
dst += std::string(indent_, ' ') + std::string(width, frames_.mid_) + "\n"; // third line: the middle frame
return dst;
}
/**
Expand All @@ -303,10 +310,10 @@ class FmtTable
width += row.size() - 1;
// for the left and right frame
width += 2;
if (pos == 't') {
dst += std::string(width, frames_.up_) + "\n";
if (pos == 't') { // 't' for top
dst += std::string(indent_, ' ') + std::string(width, frames_.up_) + "\n";
}
dst += std::string(1, frames_.l_);
dst += std::string(indent_, ' ') + std::string(1, frames_.l_);
for(size_t i = 0; i < row.size(); i++)
{
dst += row[i];
Expand All @@ -315,8 +322,8 @@ class FmtTable
}
}
dst += std::string(1, frames_.r_) + "\n";
if (pos == 'b') {
dst += std::string(width, frames_.dw_) + "\n";
if (pos == 'b') { // 'b' for bottom
dst += std::string(indent_, ' ') + std::string(width, frames_.dw_) + "\n"; // the last line
}
return dst;
}
Expand Down Expand Up @@ -397,6 +404,7 @@ class FmtTable
std::vector<std::string> titles_;
NDArray<std::string> data_; // data
std::vector<std::string> fmts_; // format strings for each column
size_t indent_ = 0; // indent for each column
};

#endif
66 changes: 60 additions & 6 deletions source/module_base/test/formatter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,28 @@ TEST(FormatterTest, FmtTableDefaultArgs)
EXPECT_EQ(result, ref);
}

TEST(FormatterTest, FmtTableHeadless)
{
const std::vector<std::string> titles = {"", "", ""};
const std::vector<std::string> fmts = {"%s", "%d", "%f"};
FmtTable table(titles, 5, fmts);
const std::vector<std::string> col1 = {"row1", "row2", "row3", "row4", "row5"};
const std::vector<int> col2 = {1, 2, 3, 4, 5};
const std::vector<float> col3 = {1.1, 2.2, 3.3, 4.4, 5.5};
table << col1 << col2 << col3;
const std::string result = table.str();
std::cout << result << std::endl;
std::string ref = "";
ref += "-----------------\n";
ref += " row1 1 1.100000 \n";
ref += " row2 2 2.200000 \n";
ref += " row3 3 3.300000 \n";
ref += " row4 4 4.400000 \n";
ref += " row5 5 5.500000 \n";
ref += "-----------------\n";
EXPECT_EQ(result, ref);
}

TEST(FormatterTest, FmtTableCustomArgsAlign)
{
// shared data
Expand All @@ -226,7 +248,7 @@ TEST(FormatterTest, FmtTableCustomArgsAlign)
std::vector<int> col2 = {1, 2, 3, 4, 5};
std::vector<float> col3 = {1.1, 2.2, 3.3, 4.4, 5.5};
// align: l and l
FmtTable table(titles, 5, fmts, {FmtTable::Align::LEFT, FmtTable::Align::LEFT});
FmtTable table(titles, 5, fmts, 0, {FmtTable::Align::LEFT, FmtTable::Align::LEFT});
table << col1 << col2 << col3;
std::string result = table.str();
std::cout << result << std::endl;
Expand All @@ -243,7 +265,7 @@ TEST(FormatterTest, FmtTableCustomArgsAlign)
EXPECT_EQ(result, ref);

// align: r and r
FmtTable table2(titles, 5, fmts, {FmtTable::Align::RIGHT, FmtTable::Align::RIGHT});
FmtTable table2(titles, 5, fmts, 0, {FmtTable::Align::RIGHT, FmtTable::Align::RIGHT});
table2 << col1 << col2 << col3;
result = table2.str();
std::cout << result << std::endl;
Expand All @@ -260,7 +282,7 @@ TEST(FormatterTest, FmtTableCustomArgsAlign)
EXPECT_EQ(result, ref);

// align: l and r
FmtTable table3(titles, 5, fmts, {FmtTable::Align::RIGHT, FmtTable::Align::LEFT});
FmtTable table3(titles, 5, fmts, 0, {FmtTable::Align::RIGHT, FmtTable::Align::LEFT});
table3 << col1 << col2 << col3;
result = table3.str();
std::cout << result << std::endl;
Expand All @@ -277,7 +299,7 @@ TEST(FormatterTest, FmtTableCustomArgsAlign)
EXPECT_EQ(result, ref);

// align: r and l
FmtTable table4(titles, 5, fmts, {FmtTable::Align::LEFT, FmtTable::Align::RIGHT});
FmtTable table4(titles, 5, fmts, 0, {FmtTable::Align::LEFT, FmtTable::Align::RIGHT});
table4 << col1 << col2 << col3;
result = table4.str();
std::cout << result << std::endl;
Expand All @@ -303,7 +325,12 @@ TEST(FormatterTest, FmtTableCustomArgsAlignFrame)
std::vector<int> col2 = {1, 2, 3, 4, 5};
std::vector<float> col3 = {1.1, 2.2, 3.3, 4.4, 5.5};

FmtTable table1(titles, 5, fmts, {FmtTable::Align::LEFT, FmtTable::Align::LEFT}, {'+', '?', '*', '.', '^'});
FmtTable table1(titles,
5,
fmts,
0,
{FmtTable::Align::LEFT, FmtTable::Align::LEFT},
{'+', '?', '*', '.', '^'});
table1 << col1 << col2 << col3;
std::string result = table1.str();
std::cout << result << std::endl;
Expand All @@ -328,7 +355,10 @@ TEST(FormatterTest, FmtTableCustomArgsAlignFrameDelim)
std::vector<std::string> col1 = {"row1", "row2", "row3", "row4", "row5"};
std::vector<int> col2 = {1, 2, 3, 4, 5};
std::vector<float> col3 = {1.1, 2.2, 3.3, 4.4, 5.5};
FmtTable table1(titles, 5, fmts,
FmtTable table1(titles,
5,
fmts,
0,
{FmtTable::Align::LEFT, FmtTable::Align::LEFT},
{'=', '/', '&', '#', '%'},
{'"', ']'});
Expand All @@ -348,6 +378,30 @@ TEST(FormatterTest, FmtTableCustomArgsAlignFrameDelim)
EXPECT_EQ(result, ref);
}

TEST(FormatterTest, FmtTableCustomIndent)
{
const std::vector<std::string> titles = {"title1", "t i t l e 2", "t-i-t-l-e-3"};
const std::vector<std::string> fmts = {"%s", "%d", "%f"};
FmtTable table(titles, 5, fmts, 4);
const std::vector<std::string> col1 = {"row1", "row2", "row3", "row4", "row5"};
const std::vector<int> col2 = {1, 2, 3, 4, 5};
const std::vector<float> col3 = {1.1, 2.2, 3.3, 4.4, 5.5};
table << col1 << col2 << col3;
const std::string result = table.str();
std::cout << result << std::endl;
std::string ref = "";
ref += " --------------------------------\n";
ref += " title1 t i t l e 2 t-i-t-l-e-3 \n";
ref += " --------------------------------\n";
ref += " row1 1 1.100000 \n";
ref += " row2 2 2.200000 \n";
ref += " row3 3 3.300000 \n";
ref += " row4 4 4.400000 \n";
ref += " row5 5 5.500000 \n";
ref += " --------------------------------\n";
EXPECT_EQ(result, ref);
}

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
Expand Down
6 changes: 5 additions & 1 deletion source/module_base/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,11 @@ void timer::print_all(std::ofstream &ofs)

std::vector<std::string> titles = {"CLASS_NAME", "NAME", "TIME/s", "CALLS", "AVG/s", "PER/%"};
std::vector<std::string> formats = {"%-10s", "%-10s", "%6.2f", "%8d", "%6.2f", "%6.2f"};
FmtTable time_statistics(titles, pers.size(), formats, {FmtTable::Align::LEFT, FmtTable::Align::CENTER});
FmtTable time_statistics(/*titles=*/titles,
/*nrows=*/pers.size(),
/*formats=*/formats,
/*indent=*/0,
/*align=*/{/*value*/FmtTable::Align::LEFT, /*title*/FmtTable::Align::CENTER});
time_statistics << class_names << names << times << calls << avgs << pers;
const std::string table = "TIME STATISTICS\n" + time_statistics.str();
std::cout<<table<<std::endl;
Expand Down
9 changes: 5 additions & 4 deletions source/module_elecstate/elecstate_print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,11 @@ void print_etot(const Magnetism& magnet,
std::transform(energies_Ry.begin(), energies_Ry.end(), energies_eV.begin(), [](double ener) {
return ener * ModuleBase::Ry_to_eV;
});
FmtTable table({"Energy", "Rydberg", "eV"},
titles.size(),
{"%-14s", "%20.10f", "%20.10f"},
{FmtTable::Align::LEFT, FmtTable::Align::CENTER});
FmtTable table(/*titles=*/{"Energy", "Rydberg", "eV"},
/*nrows=*/titles.size(),
/*formats=*/{"%-14s", "%20.10f", "%20.10f"},
/*indents=*/0,
/*align=*/{/*value*/FmtTable::Align::LEFT, /*title*/FmtTable::Align::CENTER});
table << titles << energies_Ry << energies_eV;
GlobalV::ofs_running << table.str() << std::endl;
if (PARAM.inp.out_level == "ie" || PARAM.inp.out_level == "m") // xiaohui add 'm' option, 2015-09-16
Expand Down
4 changes: 3 additions & 1 deletion source/module_esolver/esolver_of_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,9 @@ void ESolver_OF::print_info(const bool conv_esolver)
std::transform(energies_Ry.begin(), energies_Ry.end(), energies_eV.begin(), [](double energy) {
return energy * ModuleBase::Ry_to_eV;
});
FmtTable table({"Energy", "Rydberg", "eV"}, titles.size(), {"%20s", "%20.12f", "%20.12f"});
FmtTable table(/*titles=*/{"Energy", "Rydberg", "eV"},
/*nrows=*/titles.size(),
/*formats=*/{"%20s", "%20.12f", "%20.12f"}, 0);
table << titles << energies_Ry << energies_eV;
GlobalV::ofs_running << table.str() << std::endl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,10 @@ void vdw::Vdwd3Parameters::_vdwd3_autoset_xcparam(const std::string& xc_in,
if (plog != nullptr) // logging the autoset
{
param = {s6, s8, a1, a2};
FmtTable vdwd3tab({"Parameters", "Original", "Autoset"}, 4, {"%10s", "%10s", "%10.4f"});
FmtTable vdwd3tab(/*titles=*/{"Parameters", "Original", "Autoset"},
/*nrows=*/4,
/*formats=*/{"%10s", "%10s", "%10.4f"},
/*indent=*/0);
const std::vector<std::string> items = {"s6", "s8", "a1", "a2"};
vdwd3tab << items << flag << param;
(*plog) << "\nDFT-D3 Dispersion correction parameters autoset\n" << vdwd3tab.str()
Expand Down
24 changes: 20 additions & 4 deletions source/module_hamilt_lcao/module_deltaspin/spin_constrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,11 @@ void SpinConstrain<FPTYPE>::print_Mi(std::ofstream& ofs_running)
{
const std::vector<std::string> title = {"Total Magnetism (uB)", ""};
const std::vector<std::string> fmts = {"%-26s", "%20.10f"};
FmtTable table(title, nat, fmts, {FmtTable::Align::RIGHT, FmtTable::Align::LEFT});
FmtTable table(/*titles=*/title,
/*nrows=*/nat,
/*formats=*/fmts,
/*indent=*/0,
/*align=*/{/*value*/FmtTable::Align::RIGHT, /*title*/FmtTable::Align::LEFT});
for (int iat = 0; iat < nat; ++iat)
{
mag_z[iat] = Mi_[iat].z;
Expand All @@ -535,7 +539,11 @@ void SpinConstrain<FPTYPE>::print_Mi(std::ofstream& ofs_running)
{
const std::vector<std::string> title = {"Total Magnetism (uB)", "", "", ""};
const std::vector<std::string> fmts = {"%-26s", "%20.10f", "%20.10f", "%20.10f"};
FmtTable table(title, nat, fmts, {FmtTable::Align::RIGHT, FmtTable::Align::LEFT});
FmtTable table(/*titles=*/title,
/*nrows=*/nat,
/*formats=*/fmts,
/*indent=*/0,
/*align=*/{/*value*/FmtTable::Align::RIGHT, /*title*/FmtTable::Align::LEFT});
for (int iat = 0; iat < nat; ++iat)
{
mag_x[iat] = Mi_[iat].x;
Expand All @@ -560,7 +568,11 @@ void SpinConstrain<FPTYPE>::print_Mag_Force(std::ofstream& ofs_running)
{
const std::vector<std::string> title = {"Magnetic force (eV/uB)", ""};
const std::vector<std::string> fmts = {"%-26s", "%20.10f"};
FmtTable table(title, nat, fmts, {FmtTable::Align::RIGHT, FmtTable::Align::LEFT});
FmtTable table(/*titles=*/title,
/*nrows=*/nat,
/*formats=*/fmts,
/*indent=*/0,
/*align=*/{/*value*/FmtTable::Align::RIGHT, /*title*/FmtTable::Align::LEFT});
for (int iat = 0; iat < nat; ++iat)
{
mag_force_z[iat] = lambda_[iat].z * ModuleBase::Ry_to_eV;
Expand All @@ -572,7 +584,11 @@ void SpinConstrain<FPTYPE>::print_Mag_Force(std::ofstream& ofs_running)
{
const std::vector<std::string> title = {"Magnetic force (eV/uB)", "", "", ""};
const std::vector<std::string> fmts = {"%-26s", "%20.10f", "%20.10f", "%20.10f"};
FmtTable table(title, nat, fmts, {FmtTable::Align::RIGHT, FmtTable::Align::LEFT});
FmtTable table(/*titles=*/title,
/*nrows=*/nat,
/*formats=*/fmts,
/*indent=*/0,
/*align=*/{/*value*/FmtTable::Align::RIGHT, /*title*/FmtTable::Align::LEFT});
for (int iat = 0; iat < nat; ++iat)
{
mag_force_x[iat] = lambda_[iat].x * ModuleBase::Ry_to_eV;
Expand Down
6 changes: 5 additions & 1 deletion source/module_hamilt_pw/hamilt_pwdft/onsite_projector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,11 @@ void projectors::OnsiteProjector<T, Device>::cal_occupations(const psi::Psi<std:
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(title, this->ucell->nat, fmts, {FmtTable::Align::RIGHT, FmtTable::Align::LEFT});
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++)
Expand Down
8 changes: 6 additions & 2 deletions source/module_io/output_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ void print_force(std::ofstream& ofs_running,
}
}

FmtTable fmt(titles, atom_label.size(), {"%10s", "%20.10f", "%20.10f", "%20.10f"});
FmtTable fmt(/*titles=*/titles,
/*nrows=*/atom_label.size(),
/*formats=*/{"%10s", "%20.10f", "%20.10f", "%20.10f"}, 0);
fmt << atom_label << force_x << force_y << force_z;
table = fmt.str();
ofs_running << table << std::endl;
Expand Down Expand Up @@ -283,7 +285,9 @@ void print_stress(const std::string& name, const ModuleBase::matrix& scs, const

double pressure = (scs(0, 0) + scs(1, 1) + scs(2, 2)) / 3.0 * unit_transform;

FmtTable fmt(titles, 3, {"%20.10f", "%20.10f", "%20.10f"});
FmtTable fmt(/*titles=*/titles,
/*nrows=*/3,
/*formats=*/{"%20.10f", "%20.10f", "%20.10f"}, 0);
fmt << stress_x << stress_y << stress_z;
table = fmt.str();
GlobalV::ofs_running << table;
Expand Down
18 changes: 15 additions & 3 deletions source/module_io/output_mulliken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,11 @@ void Output_Mulliken<TK>::print_atom_mag(const std::vector<std::vector<double>>&
{
const std::vector<std::string> title = {"Total Magnetism (uB)", ""};
const std::vector<std::string> fmts = {"%-26s", "%20.10f"};
FmtTable table(title, nat, fmts, {FmtTable::Align::RIGHT, FmtTable::Align::LEFT});
FmtTable table(/*titles=*/title,
/*nrows=*/nat,
/*formats=*/fmts,
/*indent=*/0,
/*align=*/{/*value*/FmtTable::Align::RIGHT, /*title*/FmtTable::Align::LEFT});
for (int iat = 0; iat < nat; ++iat)
{
atom_label.push_back(this->cell_index_->get_atom_label(iat, true));
Expand All @@ -456,7 +460,11 @@ void Output_Mulliken<TK>::print_atom_mag(const std::vector<std::vector<double>>&
std::vector<double> azimuth(nat, 0.0);
const std::vector<std::string> title = {"Total Magnetism (uB)", "x", "y", "z"};
const std::vector<std::string> fmts = {"%26s", "%20.10f", "%20.10f", "%20.10f"};
FmtTable table(title, nat, fmts, {FmtTable::Align::RIGHT, FmtTable::Align::RIGHT});
FmtTable table(/*titles=*/title,
/*nrows=*/nat,
/*formats=*/fmts,
/*indent=*/0,
/*align=*/{/*value*/FmtTable::Align::RIGHT, /*title*/FmtTable::Align::RIGHT});
for (int iat = 0; iat < nat; ++iat)
{
atom_label.push_back(this->cell_index_->get_atom_label(iat, true));
Expand All @@ -472,7 +480,11 @@ void Output_Mulliken<TK>::print_atom_mag(const std::vector<std::vector<double>>&
/// output mag in polar coordinates
const std::vector<std::string> title_polar = {"Total Magnetism (uB)", "Magnitude (uB)", "Polar (degree)", "Azimuth (degree)"};
const std::vector<std::string> fmts_polar = {"%26s", "%20.10f", "%20.10f", "%20.10f"};
FmtTable table_polar(title_polar, nat, fmts_polar, {FmtTable::Align::RIGHT, FmtTable::Align::RIGHT});
FmtTable table_polar(/*titles=*/title_polar,
/*nrows=*/nat,
/*formats=*/fmts_polar,
/*indent=*/0,
/*align=*/{/*value*/FmtTable::Align::RIGHT, /*title*/FmtTable::Align::RIGHT});
table_polar << atom_label << magnitude << polar << azimuth;
os << table_polar.str() << std::endl;
}
Expand Down