Repository navigation
Expand file tree
/
Copy pathcsr_reader.cpp
More file actions
140 lines (119 loc) · 3.36 KB
/
Copy pathcsr_reader.cpp
File metadata and controls
140 lines (119 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "csr_reader.h"
#include "source_base/tool_quit.h"
namespace ModuleIO
{
// constructor
template <typename T>
csrFileReader<T>::csrFileReader(const std::string& filename) : FileReader(filename)
{
parseFile();
}
// function to parse file
template <typename T>
void csrFileReader<T>::parseFile()
{
// Check if file is open
if (!isOpen())
{
ModuleBase::WARNING_QUIT("csrFileReader::parseFile", "File is not open");
}
std::string tmp_string;
// Read the step
readLine();
ss >> tmp_string >> step;
// Read the matrix dimension
readLine();
ss >> tmp_string >> tmp_string >> tmp_string >> tmp_string >> matrixDimension;
// Read the number of R
readLine();
ss >> tmp_string >> tmp_string >> tmp_string >> tmp_string >> numberOfR;
// Read the matrices
for (int i = 0; i < numberOfR; i++)
{
std::vector<int> RCoord(3);
int nonZero;
readLine();
ss >> RCoord[0] >> RCoord[1] >> RCoord[2] >> nonZero;
RCoordinates.push_back(RCoord);
std::vector<T> csr_values(nonZero);
std::vector<int> csr_col_ind(nonZero);
std::vector<int> csr_row_ptr(matrixDimension + 1);
// read CSR values
readLine();
for (int i = 0; i < nonZero; i++)
{
ss >> csr_values[i];
}
// read column indices
readLine();
for (int i = 0; i < nonZero; i++)
{
ss >> csr_col_ind[i];
}
// read row pointers
readLine();
for (int i = 0; i < matrixDimension + 1; i++)
{
ss >> csr_row_ptr[i];
}
SparseMatrix<T> matrix(matrixDimension, matrixDimension);
matrix.readCSR(csr_values, csr_col_ind, csr_row_ptr);
sparse_matrices.push_back(matrix);
}
}
// function to get R coordinate
template <typename T>
std::vector<int> csrFileReader<T>::getRCoordinate(int index) const
{
if (index < 0 || index >= RCoordinates.size())
{
ModuleBase::WARNING_QUIT("csrFileReader::getRCoordinate", "Index out of range");
}
return RCoordinates[index];
}
// function to get matrix
template <typename T>
SparseMatrix<T> csrFileReader<T>::getMatrix(int index) const
{
if (index < 0 || index >= sparse_matrices.size())
{
ModuleBase::WARNING_QUIT("csrFileReader::getMatrix", "Index out of range");
}
return sparse_matrices[index];
}
// function to get matrix using R coordinate
template <typename T>
SparseMatrix<T> csrFileReader<T>::getMatrix(int Rx, int Ry, int Rz)
{
for (int i = 0; i < RCoordinates.size(); i++)
{
if (RCoordinates[i][0] == Rx && RCoordinates[i][1] == Ry && RCoordinates[i][2] == Rz)
{
return sparse_matrices[i];
}
}
ModuleBase::WARNING_QUIT("csrFileReader::getMatrix", "R coordinate not found");
}
// function to get matrix
template <typename T>
int csrFileReader<T>::getNumberOfR() const
{
return numberOfR;
}
// function to get matrixDimension
template <typename T>
int csrFileReader<T>::getMatrixDimension() const
{
return matrixDimension;
}
// function to get step
template <typename T>
int csrFileReader<T>::getStep() const
{
return step;
}
// T of AtomPair can be double
template class csrFileReader<double>;
// ToDo: T of AtomPair can be std::complex<double>
template class csrFileReader<std::complex<double>>;
} // namespace ModuleIO