Skip to content

Commit 9c6c510

Browse files
authored
Merge pull request #101 from PeizeLin/develop
add namespace Read_Txt_Input
2 parents 3c50fbf + 2ab53a4 commit 9c6c510

24 files changed

+867
-98
lines changed

source/Makefile.Objects

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ read_cell_pseudopots.o\
247247
read_dm.o\
248248
read_txt_tools.o\
249249
read_txt_stru.o\
250+
read_txt_input_value.o\
251+
read_txt_input_list.o\
252+
read_txt_input_process.o\
253+
read_txt_input_process_global.o\
254+
read_txt_input-general.o\
255+
read_txt_input-pw.o\
256+
read_txt_input-spectrum.o\
250257
write_pot.o\
251258
write_rho.o\
252259
write_rho_cube.o\

source/module_base/timer.cpp

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
#include<vector>
99

1010
#ifdef __MPI
11-
#include "mpi.h"
11+
#include <mpi.h>
12+
#endif
13+
14+
#ifdef _OPENMP
15+
#include <omp.h>
1216
#endif
1317

1418
namespace ModuleBase
@@ -67,7 +71,11 @@ void timer::tick(const std::string &class_name,const std::string &name)
6771
if (disabled)
6872
return;
6973

70-
Timer_One &timer_one = timer_pool[class_name][name];
74+
#ifdef _OPENMP
75+
if(!omp_get_thread_num())
76+
#endif
77+
{
78+
Timer_One &timer_one = timer_pool[class_name][name];
7179

7280
//----------------------------------------------------------
7381
// CALL MEMBER FUNCTION :
@@ -79,32 +87,33 @@ void timer::tick(const std::string &class_name,const std::string &name)
7987
// if start_flag == false, means it's the end of this counting,
8088
// so we add the time during this two 'time point' to the clock time storage.
8189
//----------------------------------------------------------
82-
if(timer_one.start_flag)
83-
{
90+
if(timer_one.start_flag)
91+
{
8492
#ifdef __MPI
85-
timer_one.cpu_start = MPI_Wtime();
93+
timer_one.cpu_start = MPI_Wtime();
8694
#else
87-
timer_one.cpu_start = cpu_time();
95+
timer_one.cpu_start = cpu_time();
8896
#endif
89-
++timer_one.calls;
90-
timer_one.start_flag = false;
91-
}
92-
else
93-
{
97+
++timer_one.calls;
98+
timer_one.start_flag = false;
99+
}
100+
else
101+
{
94102
#ifdef __MPI
95-
timer_one.cpu_second += MPI_Wtime() - timer_one.cpu_start;
103+
timer_one.cpu_second += MPI_Wtime() - timer_one.cpu_start;
96104
#else
97-
// if(class_name=="electrons"&&name=="c_bands")
98-
// {
99-
// cout<<"call times"<<timer_one.calls<<endl;
100-
// cout<<"electrons c_bands cost time:"<<endl;
101-
// cout<<cpu_time()<<"-"<<timer_one.cpu_start<<endl;
102-
// }
103-
104-
timer_one.cpu_second += (cpu_time() - timer_one.cpu_start);
105+
// if(class_name=="electrons"&&name=="c_bands")
106+
// {
107+
// cout<<"call times"<<timer_one.calls<<endl;
108+
// cout<<"electrons c_bands cost time:"<<endl;
109+
// cout<<cpu_time()<<"-"<<timer_one.cpu_start<<endl;
110+
// }
111+
112+
timer_one.cpu_second += (cpu_time() - timer_one.cpu_start);
105113
#endif
106-
timer_one.start_flag = true;
107-
}
114+
timer_one.start_flag = true;
115+
}
116+
} // end if(!omp_get_thread_num())
108117
}
109118

110119
long double timer::print_until_now(void)

source/src_io/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ add_library(
2424
read_dm.cpp
2525
read_rho.cpp
2626
read_txt_tools.cpp
27+
read_txt_input_value.cpp
28+
read_txt_input_list.cpp
29+
read_txt_input_process.cpp
30+
read_txt_input_process_global.cpp
31+
read_txt_input-general.cpp
32+
read_txt_input-pw.cpp
33+
read_txt_input-spectrum.cpp
2734
read_txt_stru.cpp
2835
restart.cpp
2936
rwstream.cpp
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//=======================
2+
// AUTHOR : Peize Lin
3+
// DATE : 2021-12-13
4+
//=======================
5+
6+
#include "read_txt_input_list.h"
7+
8+
#include "src_io/read_txt_tools.h"
9+
#include <stdexcept>
10+
11+
// for convert
12+
#include "module_base/global_variable.h"
13+
#include "src_pw/global.h"
14+
15+
namespace Read_Txt_Input
16+
{
17+
void Input_List::set_items_general()
18+
{
19+
this->output_labels.push_back("Parameters (1.General)");
20+
21+
{
22+
Input_Item item("nspin");
23+
item.default_1(1);
24+
item.annotation = "1: single spin; 2: up and down spin; 4: noncollinear spin";
25+
item.check_transform = [](Input_Item &self)
26+
{
27+
if(!Read_Txt_Tools::in_set(self.values[0].geti(), {1,2,4}))
28+
throw std::invalid_argument("nspin must be 1,2,4");
29+
};
30+
item.convert = [](const Input_Item &self)
31+
{
32+
GlobalV::NSPIN = self.values[0].geti();
33+
};
34+
this->add_item(item);
35+
}
36+
37+
{
38+
Input_Item item("ntype");
39+
item.default_1(0);
40+
item.annotation = "atom species number";
41+
item.check_transform = [](Input_Item &self)
42+
{
43+
if(self.values[0].geti()<=0)
44+
throw std::invalid_argument("ntype must > 0");
45+
};
46+
item.convert = [](const Input_Item &self)
47+
{
48+
GlobalC::ucell.ntype = self.values[0].geti();
49+
};
50+
this->add_item(item);
51+
}
52+
53+
{
54+
Input_Item item("symmetry");
55+
item.default_1(false);
56+
item.annotation = "turn symmetry on or off";
57+
item.convert = [](const Input_Item &self)
58+
{
59+
ModuleSymmetry::Symmetry::symm_flag = self.values[0].getb();
60+
};
61+
this->add_item(item);
62+
}
63+
}
64+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//=======================
2+
// AUTHOR : Peize Lin
3+
// DATE : 2021-12-13
4+
//=======================
5+
6+
#include "read_txt_input_list.h"
7+
8+
#include "module_base/constants.h"
9+
#include <stdexcept>
10+
11+
// for convert
12+
#include "module_base/global_variable.h"
13+
14+
namespace Read_Txt_Input
15+
{
16+
void Input_List::set_items_pw()
17+
{
18+
this->output_labels.push_back("Parameters (2.Pw)");
19+
20+
{
21+
Input_Item item("ecutwfc");
22+
item.default_1(100.0,"Ry");
23+
item.check_values_size(1,2);
24+
item.annotation = "energy cutoff for wave functions";
25+
item.check_transform = [](Input_Item &self)
26+
{
27+
if(self.values[0].getd()<=0)
28+
throw std::invalid_argument("ecutwfc must > 0");
29+
30+
if(self.values[1].gets()=="Ry"){}
31+
else if(self.values[1].gets()=="eV")
32+
self.values[0].setd( self.values[0].getd() / ModuleBase::Ry_to_eV );
33+
else
34+
throw std::invalid_argument(self.values[1].gets());
35+
self.values[1].sets("Ry");
36+
};
37+
item.convert = [](const Input_Item &self)
38+
{
39+
// ?? GlobalC::pw.set()
40+
};
41+
this->add_item(item);
42+
}
43+
}
44+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//=======================
2+
// AUTHOR : Peize Lin
3+
// DATE : 2021-12-13
4+
//=======================
5+
6+
#include "read_txt_input_list.h"
7+
8+
#include <limits>
9+
10+
namespace Read_Txt_Input
11+
{
12+
void Input_List::set_items_spectrum()
13+
{
14+
this->output_labels.push_back("Parameters (15.Spectrum)");
15+
16+
{
17+
Input_Item item("ocp_set");
18+
item.annotation = "set occupation number";
19+
item.check_values_size(0, std::numeric_limits<int>::max());
20+
item.convert = [](const Input_Item &self)
21+
{
22+
// ...
23+
};
24+
this->add_item(item);
25+
}
26+
}
27+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//=======================
2+
// AUTHOR : Peize Lin
3+
// DATE : 2021-12-13
4+
//=======================
5+
6+
#ifndef READ_TXT_INPUT_ITEM_TEMPLATE_H
7+
#define READ_TXT_INPUT_ITEM_TEMPLATE_H
8+
9+
#include "read_txt_input_item.h"
10+
11+
namespace Read_Txt_Input
12+
{
13+
template<typename T>
14+
void Input_Item::default_1(const T &value)
15+
{
16+
set_value(value);
17+
++this->values_size_lower_limit;
18+
++this->values_size_upper_limit;
19+
}
20+
21+
template <typename T_head, typename... T_tail>
22+
void Input_Item::default_1( const T_head &value_head, const T_tail... values_tail)
23+
{
24+
set_value(value_head);
25+
default_1(values_tail...);
26+
++this->values_size_lower_limit;
27+
++this->values_size_upper_limit;
28+
}
29+
30+
template<>
31+
void Input_Item::set_value(const bool &b)
32+
{
33+
this->values.push_back({});
34+
this->values.back().setb(b);
35+
this->values_type.push_back("b");
36+
}
37+
38+
template<>
39+
void Input_Item::set_value(const int &i)
40+
{
41+
this->values.push_back({});
42+
this->values.back().seti(i);
43+
this->values_type.push_back("i");
44+
}
45+
46+
template<>
47+
void Input_Item::set_value(const double &d)
48+
{
49+
this->values.push_back({});
50+
this->values.back().setd(d);
51+
this->values_type.push_back("d");
52+
}
53+
54+
template<>
55+
void Input_Item::set_value(const std::string &s)
56+
{
57+
this->values.push_back({});
58+
this->values.back().sets(s);
59+
this->values_type.push_back("s");
60+
}
61+
62+
template<>
63+
void Input_Item::set_value(const char*const &s)
64+
{
65+
this->values.push_back({});
66+
this->values.back().sets(std::string(s));
67+
this->values_type.push_back("s");
68+
}
69+
}
70+
71+
#endif
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//=======================
2+
// AUTHOR : Peize Lin
3+
// DATE : 2021-12-13
4+
//=======================
5+
6+
#ifndef READ_TXT_INPUT_ITEM_H
7+
#define READ_TXT_INPUT_ITEM_H
8+
9+
#include "read_txt_input_value.h"
10+
11+
#include <vector>
12+
#include <map>
13+
#include <string>
14+
#include <functional>
15+
16+
#ifdef USE_CEREAL_SERIALIZATION
17+
#include <cereal/access.hpp>
18+
#endif
19+
20+
namespace Read_Txt_Input
21+
{
22+
class Input_Item
23+
{
24+
public:
25+
// call these functions
26+
Input_Item(const std::string &label_in) :label(label_in) {}
27+
template<typename T>
28+
void default_1(const T &value);
29+
template <typename T_head, typename... T_tail>
30+
void default_1(const T_head &value_head, const T_tail... values_tail);
31+
32+
// set these variables and functions
33+
std::string annotation;
34+
std::function<void(Input_Item&)> check_transform
35+
= [](Input_Item&self){};
36+
std::function<void(Input_Item&, const std::map<std::string, Input_Item>&)> default_2
37+
= [](Input_Item&self, const std::map<std::string, Input_Item>&list){};
38+
std::function<void(const Input_Item&)> convert
39+
= [](const Input_Item&item){};
40+
41+
std::vector<Input_Value> values;
42+
43+
void check_values_size(const int lower_limit, const int upper_limit)
44+
{ values_size_lower_limit=lower_limit; values_size_upper_limit=upper_limit; }
45+
46+
private:
47+
std::string label;
48+
std::vector<std::string> values_type;
49+
50+
int values_size_read = -1;
51+
int values_size_lower_limit = 0;
52+
int values_size_upper_limit = 0;
53+
54+
friend class Input_List;
55+
friend class Input_Process;
56+
57+
template<typename T>
58+
void set_value(const T &value);
59+
60+
#ifdef USE_CEREAL_SERIALIZATION
61+
public:
62+
Input_Item() = default;
63+
private:
64+
friend class cereal::access;
65+
template <class Archive> void serialize( Archive & ar );
66+
#endif
67+
};
68+
}
69+
70+
#include "read_txt_input_item-template.h"
71+
72+
#endif

0 commit comments

Comments
 (0)