StatMech
Loading...
Searching...
No Matches
file_util.hpp
Go to the documentation of this file.
1#include "Headers/mytypes.hpp"
2#include "Headers/mydebug.hpp"
3#include "Headers/matrix_util.hpp"
4#include <iostream>
5#include <fstream>
6#include <sstream>
7#include <string>
8#include <vector>
9#include <regex>
10#include <algorithm>
11#include <tuple>
12#include <complex>
13#include <cassert>
14#include <sysexits.h>
15
16bool checkFileExistence(std::string const& str) {
17 std::ifstream ifs(str);
18 return ifs.is_open();
19}
20
21
22bool checkIsFileOpen(std::ifstream& file, std::string const& filename) {
23 if( file.fail() ) {
24 std::cerr << "Can't open a file " << filename << "." << std::endl;
25 std::exit(EX_CANTCREAT);
26 }
27 return true;
28}
29bool checkIsFileOpen(std::ofstream& file, std::string const& filename) {
30 if( file.fail() ) {
31 std::cerr << "Can't open a file " << filename << "." << std::endl;
32 std::exit(EX_CANTCREAT);
33 }
34 return true;
35}
36
37
38//********** Skip lines with comments **********/
39static inline void skip_header(std::ifstream& Infs){
40 Infs.exceptions(std::fstream::eofbit);
41 std::string line("#");
42 std::ifstream::pos_type pos;
43 while( line[0]=='#' || line.length()==0 ) {
44 pos = Infs.tellg();
45 getline(Infs,line);
46 }
47 Infs.seekg(pos);
48}
49
50
51static inline void readMatrix(std::ifstream& Infs, int const dim1, int const dim2, matrix<Complex_t<double>>& mat, std::string UPLO = "All") {
52 std::string str;
53 bool flag = false;
54 while(std::getline(Infs, str, '\n')) {
55 debug_print("(" << (str.find("[")!=std::string::npos) << "): " << str);
56 if(str.find("[") != std::string::npos) { flag = true; break; };
57 }
58 if(flag == false) {
59 std::cerr << "Error: Can't read matrix from a file." << std::endl;
60 std::exit(EX_DATAERR);
61 }
62
63 mat.resize(dim1,dim2);
64
65 std::stringstream ss;
66 std::string::size_type pos;
67 double real, imag;
68
69 int index1, index2;
70 for(index1 = 0;std::getline(Infs, str); ++index1) {
71 if( str.find("]") != std::string::npos ) break;
72 std::replace(str.begin(), str.end(), ';', ' ');
73 std::regex_replace(str, std::regex(" *"), " ");
74 str.erase(std::remove(str.begin(), str.end(), '\t'), str.end());
75 if( (pos = str.find_first_not_of(' ')) != std::string::npos ) str.erase(std::remove(str.begin(), str.begin()+pos, ' '), str.begin()+pos);
76
77 debug_print("# line = " << str);
78 ss.str(str); ss.clear(std::stringstream::goodbit);
79 for(index2 = 0; std::getline(ss, str, ' ') && index2<dim2; ) {
80 if( (pos = str.find_first_not_of(' ')) != std::string::npos ) str.erase(std::remove(str.begin(), str.begin()+pos, ' '), str.begin()+pos);
81 else break;
82
83 debug_print("# element = " << str);
84 if( (pos = str.find("i")) == std::string::npos ) {
85 if(UPLO=="Upper" || UPLO=="U") mat.at(index1,index1+(index2++)) = DoubleComplexOne*std::stod(str);
86 else mat.at(index1,index2++) = DoubleComplexOne*std::stod(str);
87 } else {
88 str.replace(pos, 1, " ");
89 if( (pos = str.find('*') ) != std::string::npos ) str.replace(pos, 1, " ");
90
91 // Find a seperator between the real and imaginary part.
92 flag = false; pos = str.size();
93 while( (pos = str.rfind('+',pos-1)) != std::string::npos ) {
94 if(pos == 0) break;
95 if(str[pos-1]!='e' && str[pos-1]!='E') { flag=true; break; }
96 }
97 debug_print("# pos of +: " << pos);
98 if(!flag) {
99 pos = str.size();
100 while( (pos = str.rfind('-', pos-1)) != std::string::npos ) {
101 if(pos == 0) { std::cerr << "Can't seperate real and imaginary parts. " << std::endl; std::exit(EX_DATAERR); }
102 if(str[pos-1]!='e' && str[pos-1]!='E') { flag=true; break; }
103 }
104 debug_print("# pos of -: " << pos);
105 }
106 debug_print("# real = " << str.substr(0,pos));
107 debug_print("# imag = " << str.substr(pos));
108 real = std::stod(str.substr(0,pos).c_str());
109 imag = std::stod(str.substr(pos).c_str());
110
111 if(UPLO=="Upper" || UPLO=="U") mat.at(index1,index1+(index2++)) = real + DoubleComplexI*imag;
112 else mat.at(index1,index2++) = real + DoubleComplexI*imag;
113 }
114
115 if(UPLO=="Upper" || UPLO=="U") {
116 debug_print("# index1=" << index1 << ", index2 = " << index2);
117 if(index1+index2 == dim2) break;
118 }
119 }
120 debug_print("index1 = " << index1 << ", index2 = " << index2);
121 if(UPLO=="Upper" || UPLO=="U" || UPLO=="Lower" || UPLO=="L") assert(index1<dim1 && index1+index2==dim2);
122 else assert(index1<dim1 && index2==dim2);
123 }
124 assert(index1 == dim1);
125}
126
127// static inline void readMatrix(std::ifstream& Infs, int const dim1, int const dim2, matrix<Complex_t<double>>& mat) {
128// readMatrix(Infs, dim1, dim2, mat, "All");
129// }
130
131static inline std::tuple<int,int,std::string> readMatrix(std::ifstream& Infs, matrix<Complex_t<double>>& mat) {
132 bool flag = false;
133 int dim1, dim2;
134 std::string str;
135 auto oldpos1 = Infs.tellg(), oldpos2=oldpos1;
136 while(std::getline(Infs, str, '\n')) {
137 debug_print(oldpos2 << "(" << (str.find("[")!=std::string::npos) << "): " << str);
138 if(str.find("[") != std::string::npos) { flag = true; break; };
139 oldpos2 = oldpos1; oldpos1 = Infs.tellg();
140 }
141 if(flag == false) {
142 std::cerr << "Error: Can't read matrix from a file." << std::endl;
143 std::exit(EX_DATAERR);
144 }
145 Infs.seekg(oldpos2); std::getline(Infs, str);
146
147 std::string UPLO = "All";
148 std::stringstream ss(str);
149 ss >> dim1 >> dim2;
150 if(ss >> UPLO) debug_print("# Read UPLO=" << UPLO);
151 debug_print("# dim1=" << dim1 << ", dim2=" << dim2);
152 readMatrix(Infs, dim1, dim2, mat, UPLO);
153 return {dim1, dim2, UPLO};
154}
155
156static inline std::tuple<int,int,std::string> readDiagonal(std::ifstream& Infs, std::vector<double>& vec) {
157 bool flag = false;
158 int dim1, dim2, index;
159 std::string str;
160 auto oldpos1 = Infs.tellg(), oldpos2=oldpos1;
161 auto& pos = oldpos1;
162 while(std::getline(Infs, str, '\n')) {
163 // debug_print(oldpos2 << "(" << (str.find("[")!=std::string::npos) << "): " << str);
164 if(str.find("[") != std::string::npos) { flag = true; break; };
165 oldpos2 = oldpos1; oldpos1 = Infs.tellg();
166 }
167 if(flag == false) {
168 std::cerr << "Error: Can't read matrix from a file." << std::endl;
169 std::exit(EX_DATAERR);
170 }
171 Infs.seekg(oldpos2); std::getline(Infs, str);
172
173 std::string UPLO = "All";
174 std::stringstream ss(str);
175 ss >> dim1 >> dim2;
176 if(ss >> UPLO) debug_print("# Read UPLO=" << UPLO);
177 debug_print("# dim1=" << dim1 << ", dim2=" << dim2);
178 vec.resize(dim1<=dim2 ? dim1: dim2);
179
180
181 //--------------------- skip header ---------------------//
182 for(flag = false; std::getline(Infs, str, '\n'); ) {
183 debug_print("(" << (str.find("[")!=std::string::npos) << "): " << str);
184 if(str.find("[") != std::string::npos) { flag = true; break; };
185 }
186 if(flag == false) {
187 std::cerr << "Error: Can't read matrix from a file." << std::endl;
188 std::exit(EX_DATAERR);
189 }
190
191 //--------------------- Read diagonal elements ---------------------//
192 for(index = 0;index < dim1 && std::getline(Infs, str); ++index) {
193 std::cout << "Reading diagonal elements... " << (double)index/(double)dim1*100 << " %\t\r";
194 if( str.find("]") != std::string::npos ) break;
195 std::replace(str.begin(), str.end(), ';', ' ');
196 std::regex_replace(str, std::regex(" *"), " ");
197 str.erase(std::remove(str.begin(), str.end(), '\t'), str.end());
198 if( (pos = str.find_first_not_of(' ')) != std::string::npos )
199 str.erase(std::remove(str.begin(), str.begin()+pos, ' '), str.begin()+pos);
200 if( (pos = str.find_last_not_of(' ')) != std::string::npos && (size_t)pos+1 < str.size())
201 str.erase(std::remove(str.begin()+pos+1, str.end(), ' '), str.end());
202 ss.str(str); ss.clear(std::stringstream::goodbit);
203
204 if(UPLO == "Upper") {
205 ss >> str;
206 // debug_print(__func__ << ":index=" << index << ", str=" << str);
207 // } else if(UPLO == "Lower") {
208 // if( (pos = str.rfind(' ')) != std::string::npos) {
209 // debug_print("pos=" << pos << "; " << str.substr((size_t)pos+1));
210 // str = str.substr((size_t)pos+1);
211 // } else {
212 // debug_print("pos=" << 0 << "; " << str);
213 // }
214 // // if(pos>=1) pos=str.substr(0,(size_t)pos-(size_t)1).rfind(' ');
215 } else {
216 for(int i = 0;i <= index; ++i) std::getline(ss, str, ' ');
217 // debug_print(__func__ << ":index=" << index << ", str=" << str);
218 }
219 vec.at(index) = std::stod(str);
220 }
221 return {dim1, dim2, UPLO};
222}
Definition mytypes.hpp:147
bool checkIsFileOpen(std::ifstream &file, std::string const &filename)
Definition file_util.hpp:22
bool checkFileExistence(std::string const &str)
Definition file_util.hpp:16
debug_print("# Determining GPU configuration.")