-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathParser.h
163 lines (137 loc) · 3.23 KB
/
Parser.h
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef PARSER_H
#define PARSER_H
#include <fstream>
#include <sys/stat.h>
#include "MyMatrix/Vector.h"
#include "MyMatrix/Matrix.h"
// Returns the file size in the number of bytes
int file_size( const char* filename )
{
std::ifstream f;
f.open( filename, std::ios_base::binary | std::ios_base::in );
if( !f.good() || f.eof() || !f.is_open() )
return 0;
f.seekg(0, std::ios_base::beg);
std::ifstream::pos_type begin_pos = f.tellg();
f.seekg(0, std::ios_base::end);
std::ifstream::pos_type end_pos = f.tellg();
f.close();
return static_cast<int>(end_pos - begin_pos);
}
// Returns a bool indicating whether fileName exists
bool file_exists( const char* fileName )
{
struct stat buf;
int i = stat( fileName, &buf );
return (i == 0);
}
/** Writers **/
template <class T>
void writeBIN(T* a, int N, const char* filename)
{
fstream myFile(filename, ios::out | ios::binary);
myFile.write( (char*) a, N*sizeof(T) );
myFile.close();
}
template <class T>
void writeBIN(vector<T>& a, const char* filename)
{
writeBIN<T>( &a[0], a.size(), filename );
}
/** Readers **/
template <class T>
void parseBIN(const char* filename, T* a, int N)
{
fstream myFile(filename, ios::in | ios::binary);
myFile.read( (char*) a, N*sizeof(T) );
myFile.close();
}
template <class T>
void parseBIN(const char* filename, vector<T>& a)
{
int N = file_size( filename ) / sizeof(T);
a.resize(N);
parseBIN<T>(filename, &a[0], N);
}
template <class T>
void parseBIN( const char* filename, matrix<T>& M, int nCols )
{
int N = file_size(filename) / sizeof(T);
M = matrix<T>( (int) ceil(N/(double)nCols), nCols );
parseBIN<T>(filename, M.val_array(), N);
}
template <class T>
void parseOFF( const char* filename,
matrix<T>& coord, int nDim,
matrix<int>& IEN, int nNPE )
{
ifstream inFile;
inFile.open( filename );
// Read the OFF header
string temp;
inFile >> temp;
// Read the data header
int nNodes;
inFile >> nNodes;
int nElems;
inFile >> nElems;
int N;
inFile >> N;
// Read the coord array
double x;
coord = matrix<T>( nNodes, nDim );
for( int n = 0; n < nNodes; ++n ) {
for( int d = 0; d < nDim; ++d ) {
inFile >> x;
coord(n,d) = (T) x;
}
}
// Read the elem array
int nNPE_;
IEN = matrix<int>( nElems, nNPE );
for( int e = 0; e < nElems; ++e ) {
inFile >> nNPE_;
assert( nNPE_ == nNPE );
for( int n = 0; n < nNPE; ++n ) {
inFile >> N;
IEN(e,n) = (int) N;
}
}
}
template <class T>
void parseDAT( const char* filename,
matrix<T>& coord, int nDim,
matrix<int>& IEN, int nNPE )
{
cout << filename << endl;
ifstream inFile;
inFile.open( filename );
// Read the OFF header
string temp;
inFile >> temp;
// Read the data header
int nNodes;
inFile >> nNodes;
int nElems;
inFile >> nElems;
int N;
inFile >> N;
// Read the coord array
double x;
coord = matrix<T>( nNodes, nDim );
for( int n = 0; n < nNodes; ++n ) {
for( int d = 0; d < nDim; ++d ) {
inFile >> x;
coord(n,d) = (T) x;
}
}
// Read the elem array
IEN = matrix<int>( nElems, nNPE );
for( int e = 0; e < nElems; ++e ) {
for( int n = 0; n < nNPE; ++n ) {
inFile >> N;
IEN(e,n) = N - 1;
}
}
}
#endif