ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
csr_test_helpers.h
Go to the documentation of this file.
1#ifndef CSR_TEST_HELPERS_H
2#define CSR_TEST_HELPERS_H
3
13
14#include "gmock/gmock.h"
15#include "gtest/gtest.h"
16
21
22#include <complex>
23#include <cstdio>
24#include <fstream>
25#include <map>
26#include <set>
27#include <sstream>
28#include <string>
29#include <vector>
30
31namespace
32{
33std::string read_file(const std::string& filename)
34{
35 std::ifstream ifs(filename.c_str());
36 std::ostringstream oss;
37 oss << ifs.rdbuf();
38 return oss.str();
39}
40
41std::vector<int> read_binary_ints(const std::string& filename, const size_t count)
42{
43 std::ifstream ifs(filename.c_str(), std::ios::binary);
44 std::vector<int> values(count, 0);
45 for (size_t i = 0; i < count; ++i)
46 {
47 ifs.read(reinterpret_cast<char*>(&values[i]), sizeof(int));
48 }
49 return values;
50}
51
52template <typename T>
53T read_binary_value(std::ifstream& ifs)
54{
55 T value{};
56 ifs.read(reinterpret_cast<char*>(&value), sizeof(T));
57 return value;
58}
59
60struct NativeDoubleRBlock
61{
62 int rx = 0;
63 int ry = 0;
64 int rz = 0;
65 std::vector<double> values;
66 std::vector<int> columns;
67 std::vector<long long> row_ptr;
68};
69
70struct NativeDoubleRecord
71{
72 int magic = 0;
73 int format_version = 0;
74 int step = 0;
75 int nbasis = 0;
76 std::vector<NativeDoubleRBlock> blocks;
77};
78
79NativeDoubleRecord read_native_double_record(std::ifstream& ifs)
80{
81 NativeDoubleRecord record;
82 record.magic = read_binary_value<int>(ifs);
83 record.format_version = read_binary_value<int>(ifs);
84 record.step = read_binary_value<int>(ifs);
85 record.nbasis = read_binary_value<int>(ifs);
86 const int nR = read_binary_value<int>(ifs);
87 record.blocks.resize(nR);
88 for (NativeDoubleRBlock& block: record.blocks)
89 {
90 block.rx = read_binary_value<int>(ifs);
91 block.ry = read_binary_value<int>(ifs);
92 block.rz = read_binary_value<int>(ifs);
93 const int nnz = read_binary_value<int>(ifs);
94 block.values.resize(nnz);
95 block.columns.resize(nnz);
96 block.row_ptr.resize(record.nbasis + 1);
97 for (double& value: block.values)
98 {
99 value = read_binary_value<double>(ifs);
100 }
101 for (int& column: block.columns)
102 {
103 column = read_binary_value<int>(ifs);
104 }
105 for (long long& pointer: block.row_ptr)
106 {
107 pointer = read_binary_value<long long>(ifs);
108 }
109 }
110 return record;
111}
112
113std::vector<std::string> read_lines(const std::string& filename)
114{
115 std::ifstream ifs(filename.c_str());
116 std::vector<std::string> lines;
117 std::string line;
118 while (std::getline(ifs, line))
119 {
120 lines.push_back(line);
121 }
122 return lines;
123}
124
125int count_substr(const std::string& text, const std::string& pattern)
126{
127 int count = 0;
128 std::string::size_type pos = 0;
129 while ((pos = text.find(pattern, pos)) != std::string::npos)
130 {
131 ++count;
132 pos += pattern.size();
133 }
134 return count;
135}
136
137void init_unitcell(UnitCell& ucell)
138{
139 ucell.latName = "user_defined_lattice";
140 ucell.lat0 = 10.0;
141 ucell.latvec.e11 = 1.0;
142 ucell.latvec.e22 = 1.0;
143 ucell.latvec.e33 = 1.0;
144 ucell.ntype = 1;
145 ucell.nat = 1;
146 ucell.atoms = new Atom[1];
147 ucell.set_atom_flag = true;
148 ucell.atoms[0].label = "Si";
149 ucell.atoms[0].na = 1;
150 ucell.atoms[0].nw = 2;
151 ucell.atoms[0].taud.resize(1);
152 ucell.atoms[0].taud[0] = ModuleBase::Vector3<double>(0.0, 0.25, 0.5);
153}
154
155void init_serial_orbitals(Parallel_Orbitals& pv)
156{
157 pv.atom_begin_row.resize(2);
158 pv.atom_begin_col.resize(2);
159 pv.atom_begin_row[0] = 0;
160 pv.atom_begin_row[1] = 2;
161 pv.atom_begin_col[0] = 0;
162 pv.atom_begin_col[1] = 2;
163 pv.para_2d().set_serial(2, 2);
164}
165
166void fill_matrix(hamilt::HContainer<double>& matrix, Parallel_Orbitals& pv, double* values)
167{
168 hamilt::AtomPair<double> pair(0, 0, 0, 0, 0, &pv, values);
169 matrix.insert_pair(pair);
170}
171
172template <typename T>
173void fill_matrix_at_R(hamilt::HContainer<T>& matrix,
175 const int rx,
176 const int ry,
177 const int rz,
178 T* values)
179{
180 hamilt::AtomPair<T> pair(0, 0, rx, ry, rz, &pv, values);
181 matrix.insert_pair(pair);
182}
183
184void init_sparse_output_globals()
185{
186 GlobalV::DRANK = 0;
187}
188
189void remove_derivative_files(const std::string& fileflag, int step = -1)
190{
191 std::vector<std::string> filenames;
192 const std::string suffix = (step >= 0) ? ("g" + std::to_string(step + 1) + "_nao.csr") : "_nao.csr";
193 // Only dH/dR carries a spin index; dS/dR is spin-independent.
194 const bool spin_suffix = (fileflag == "h");
195 for (const char axis : {'x', 'y', 'z'})
196 {
197 if (spin_suffix)
198 {
199 for (int ispin = 1; ispin <= 2; ++ispin)
200 {
201 filenames.push_back("d" + fileflag + "r" + axis + "s" + std::to_string(ispin) + suffix);
202 }
203 }
204 else
205 {
206 filenames.push_back("d" + fileflag + "r" + axis + suffix);
207 // Clean up files left by the legacy spin-suffixed naming.
208 for (int ispin = 1; ispin <= 2; ++ispin)
209 {
210 filenames.push_back("d" + fileflag + "r" + axis + "s" + std::to_string(ispin) + suffix);
211 }
212 }
213 }
214 for (const std::string& filename: filenames)
215 {
216 std::remove(filename.c_str());
217 }
218}
219
220bool starts_with(const std::string& text, const std::string& prefix)
221{
222 return text.find(prefix) == 0;
223}
224} // namespace
225
226#endif // CSR_TEST_HELPERS_H
Atom class for storing atom information.
Definition atom_spec.h:14
int na
Number of atoms in this type.
Definition atom_spec.h:44
int nw
number of local orbitals (l,n,m) of this type
Definition atom_spec.h:36
std::string label
atomic symbol
Definition atom_spec.h:51
std::vector< ModuleBase::Vector3< double > > taud
Direct coordinates of each atom in this type.
Definition atom_spec.h:54
double e11
element e_ij: i_row, j_column
Definition matrix3.h:22
double e33
Definition matrix3.h:22
double e22
Definition matrix3.h:22
3 elements vector
Definition vector3.h:21
void set_serial(const int nrglb, const int ncglb)
Definition parallel_blkcyc.cpp:239
Definition parallel_orbitals.h:16
Parallel_Blkcyclic & para_2d()
Definition parallel_orbitals.h:24
std::vector< int > atom_begin_col
Definition parallel_orbitals.h:117
std::vector< int > atom_begin_row
Definition parallel_orbitals.h:116
Provide the basic information about unitcell.
Definition unitcell.h:16
int ntype
number of atom species in UnitCell
Definition unitcell.h:154
Atom * atoms
Definition unitcell.h:165
double & lat0
Definition unitcell.h:135
ModuleBase::Matrix3 & latvec
Definition unitcell.h:142
bool set_atom_flag
added on 2009-3-8 by mohan
Definition unitcell.h:215
int nat
total number of atoms of all species in unitcell
Definition unitcell.h:155
std::string & latName
Definition unitcell.h:134
Definition atom_pair.h:45
Definition hcontainer.h:147
#define T
Definition exp.cpp:237
int DRANK
Definition global_variable.cpp:28
int4 i[880]
Definition sincos.cpp:28
const std::string matrix
Definition para_tag.h:35