Skip to content

Commit 7506aba

Browse files
committed
1. add namespace Read_Txt_Stru
1 parent a4a7b7a commit 7506aba

File tree

4 files changed

+194
-0
lines changed

4 files changed

+194
-0
lines changed

source/Makefile.Objects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ read_atoms.o\
246246
read_cell_pseudopots.o\
247247
read_dm.o\
248248
read_txt_tools.o\
249+
read_txt_stru.o\
249250
write_pot.o\
250251
write_rho.o\
251252
write_rho_cube.o\

source/src_io/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ add_library(
2424
read_dm.cpp
2525
read_rho.cpp
2626
read_txt_tools.cpp
27+
read_txt_stru.cpp
2728
restart.cpp
2829
rwstream.cpp
2930
to_wannier90.cpp

source/src_io/read_txt_stru.cpp

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//=======================
2+
// AUTHOR : Peize Lin
3+
// DATE : 2021-12-08
4+
//=======================
5+
6+
#include "read_txt_stru.h"
7+
#include "read_txt_tools.h"
8+
9+
#include <algorithm>
10+
11+
namespace Read_Txt_Stru
12+
{
13+
//lat_info:
14+
// {
15+
// Lattice_Constant: [30.0, Bohr],
16+
// Lattice_Vectors:
17+
// [1.0, 0.0, 0.0,
18+
// 0.0, 1.0, 0.0,
19+
// 0.0, 0.0, 1.0],
20+
// Atomic_Positions: [Cartesian, Angstrom]
21+
// }
22+
//elements_info:
23+
// {
24+
// O: {
25+
// Pseudo_Potantial: [O_ONCV_PBE-1.0.upf],
26+
// Numerical_Orbital: [orb_O.dat]
27+
// },
28+
// C: {
29+
// Pseudo_Potantial: [C_ONCV_PBE-1.0.upf]
30+
// }
31+
// }
32+
//atoms:
33+
// [
34+
// [O, 0, 0, 0],
35+
// [H, 0, 0, 3, Force, 1, 1, 1]
36+
// ]
37+
std::tuple<
38+
std::map<std::string, std::vector<std::string>>,
39+
std::map<std::string, std::map<std::string, std::vector<std::string>>>,
40+
std::vector<std::vector<std::string>>>
41+
read_stru(const std::string &file_name)
42+
{
43+
const std::set<std::string> labels_lat = {"Lattice_Constant", "Lattice_Vectors", "Atomic_Positions"};
44+
std::set<std::string> labels = {"Element", "Atoms"};
45+
labels.insert(labels_lat.begin(), labels_lat.end());
46+
47+
std::vector<std::vector<std::string>> stru = Read_Txt_Tools::read_file_to_vector(file_name, {"#","\\"});
48+
49+
std::map<std::string, std::vector<std::string>> lat_info;
50+
std::map<std::string, std::map<std::string, std::vector<std::string>>> elements_info;
51+
std::vector<std::vector<std::string>> atoms_info;
52+
53+
while(!stru.empty())
54+
{
55+
std::vector<std::vector<std::string>> stru_one = Read_Txt_Tools::cut_paragraph(stru, labels);
56+
if(stru_one[0][0]=="Element")
57+
{
58+
std::map<std::string, std::vector<std::string>> &element_info = elements_info[stru_one[0][1]];
59+
for(auto ptr=stru_one.begin()+1; ptr<stru_one.end(); ++ptr)
60+
{
61+
element_info[(*ptr)[0]] = std::vector<std::string>(ptr->begin()+1, ptr->end());
62+
}
63+
}
64+
else if(stru_one[0][0]=="Atoms")
65+
{
66+
// [
67+
// [O, 0, 0, 0],
68+
// [H, 0, 0, 3],
69+
// [Force, 1, 1, 1]
70+
// ]
71+
if(stru_one[0].size()==1)
72+
{
73+
atoms_info.insert(atoms_info.end(), stru_one.begin()+1, stru_one.end());
74+
}
75+
else
76+
{
77+
std::vector<std::string> atom0 = std::vector<std::string>(stru_one[0].begin()+1, stru_one[0].end());
78+
atoms_info.push_back(std::move(atom0));
79+
atoms_info.insert(atoms_info.end(), stru_one.begin()+1, stru_one.end());
80+
}
81+
82+
}
83+
else
84+
{
85+
const auto ptr = std::find(labels_lat.begin(), labels_lat.end(), stru_one[0][0]);
86+
if(ptr!=labels_lat.end())
87+
{
88+
const std::vector<std::string> data = Read_Txt_Tools::chain(stru_one);
89+
lat_info[data[0]] = std::vector<std::string>(data.begin()+1, data.end());
90+
}
91+
else
92+
throw std::invalid_argument(stru_one[0][0]);
93+
}
94+
}
95+
96+
std::set<std::string> elements_label;
97+
for(const auto & i : elements_info)
98+
elements_label.insert(i.first);
99+
std::vector<std::vector<std::string>> atoms_info_new = Read_Txt_Stru::organize_atom_info(std::move(atoms_info), elements_label);
100+
101+
return make_tuple(std::move(lat_info), std::move(elements_info), std::move(atoms_info_new));
102+
}
103+
104+
// atoms_info_old:
105+
// [
106+
// [H, 0, 0, 0],
107+
// [O, 0, 0, 3],
108+
// [Force, 1, 1, 1]
109+
// ]
110+
// atoms_info_new:
111+
// [
112+
// [H, 0, 0, 0],
113+
// [O, 0, 0, 3, Force, 1, 1, 1]
114+
// ]
115+
std::vector<std::vector<std::string>> organize_atom_info(
116+
std::vector<std::vector<std::string>> &&atoms_info_old,
117+
const std::set<std::string> &elements_label)
118+
{
119+
std::vector<std::vector<std::string>> atoms_info_new;
120+
while(!atoms_info_old.empty())
121+
{
122+
const std::vector<std::vector<std::string>> atom_one = Read_Txt_Tools::cut_paragraph(atoms_info_old, elements_label);
123+
atoms_info_new.push_back(Read_Txt_Tools::chain(atom_one));
124+
}
125+
return atoms_info_new;
126+
}
127+
}

source/src_io/read_txt_stru.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//=======================
2+
// AUTHOR : Peize Lin
3+
// DATE : 2021-12-08
4+
//=======================
5+
6+
#ifndef READ_TXT_STRU_H
7+
#define READ_TXT_STRU_H
8+
9+
#include <tuple>
10+
#include <vector>
11+
#include <map>
12+
#include <set>
13+
#include <string>
14+
15+
namespace Read_Txt_Stru
16+
{
17+
//lat_info:
18+
// {
19+
// Lattice_Constant: [30.0, Bohr],
20+
// Lattice_Vectors:
21+
// [1.0, 0.0, 0.0,
22+
// 0.0, 1.0, 0.0,
23+
// 0.0, 0.0, 1.0],
24+
// Atomic_Positions: [Cartesian, Angstrom]
25+
// }
26+
//elements_info:
27+
// {
28+
// O: {
29+
// Pseudo_Potantial: [O_ONCV_PBE-1.0.upf],
30+
// Numerical_Orbital: [orb_O.dat]
31+
// },
32+
// C: {
33+
// Pseudo_Potantial: [C_ONCV_PBE-1.0.upf]
34+
// }
35+
// }
36+
//atoms:
37+
// [
38+
// [O, 0, 0, 0],
39+
// [H, 0, 0, 3, Force, 1, 1, 1]
40+
// ]
41+
std::tuple<
42+
std::map<std::string, std::vector<std::string>>,
43+
std::map<std::string, std::map<std::string, std::vector<std::string>>>,
44+
std::vector<std::vector<std::string>>>
45+
read_stru(const std::string &file_name);
46+
47+
48+
49+
// atoms_info_old:
50+
// [
51+
// [H, 0, 0, 0],
52+
// [O, 0, 0, 3],
53+
// [Force, 1, 1, 1]
54+
// ]
55+
// atoms_info_new:
56+
// [
57+
// [H, 0, 0, 0],
58+
// [O, 0, 0, 3, Force, 1, 1, 1]
59+
// ]
60+
std::vector<std::vector<std::string>> organize_atom_info(
61+
std::vector<std::vector<std::string>> &&atoms_info_old,
62+
const std::set<std::string> &elements_label);
63+
}
64+
65+
#endif

0 commit comments

Comments
 (0)