-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWriteReadData.h
264 lines (212 loc) · 7.79 KB
/
WriteReadData.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
============================================================================
Name : SIMPLE_TS_MPI
Author : Kiril S. Shterev
Version : v.1.1
Copyright : All rights reserved. The source code is freely distributed for non-commercial use.
Non-commercial use: Developers or distributors can compile, use all code or any part of the code, redistribute, sell results calculated using code or part of it and not-only, but except commercial use.
Commercial use : It is consider any use of the code or part of it in any way related to software, which is sold. In this case has to be contacted to Kiril Shterev to negotiate terms and conditions of use.
In any usage of the code, the derivatives has to include the following statement: "This software contains source code provided by Kiril Shterev."
In any case, that is used algorithm SIMPLE-TS has to be cited the main paper presented the algorithm [1] and any other related paper presented further development of the algorithm. The list of papers related to the algorithm are on web site contains source code of the algorithm or on the web page of the Kiril Shterev:
http://www.imbm.bas.bg/index.php/en_US/pressure-based-finite-volume-method
http://www.imbm.bas.bg/index.php/en_US/kiril-stoyanov-shterev
No Support : Kiril Shterev has no obligation to support or to continue providing or updating any of Materials.
No Warranties : This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Description : Computational Fluid Dynamic using method unsteady SIMPLE-TS, MPI 1.2, C++
References:
1. K. Shterev and S. Stefanov, Pressure based finite volume method for calculation of compressible viscous gas flows, Journal of Computational Physics 229 (2010) pp. 461-480, doi:10.1016/j.jcp.2009.09.042
============================================================================
*/
//File name: WriteReadData.h
#ifndef WriteReadData_H
#define WriteReadData_H
#include <fstream> //This is for writing and reading data from file
#include <iomanip> //This is for formatting when writing data to file
#include <iostream>
using namespace std;
//Read massive from binary file.
template <typename T_FileName, typename T_massive>
void ReadMassiveFromBinaryFile(
const T_FileName& FileName, T_massive massive[],
unsigned int& size)
/*
Target: Read massive from binary file.
Receive: FileName - name of file from where data will be readed
massive - massive which will be readed from file
size - number of elements in massive
*/
{
using namespace std;
ifstream input_data;
input_data.open(FileName, ios::in | ios::binary);
if (input_data.is_open())
{
input_data.read((char *)& massive[0], sizeof(T_massive) * size);
input_data.close();
}
else
{
cout << "The program can not open file " << FileName << " to read data." << endl;
}
}
//To read data (massive) from file.
template <typename T_FileName, typename T_massive>
void ReadMassiveFromFile_1D(
const T_FileName& FileName, T_massive massive[],
const unsigned int& size)
/*
Target: To read data (massive) from file.
Receive: FileName - name of file from where data will be readed
massive - massive which will be readed from file
size - number of elements of massive
Node: This procedure read massive from file like 1D in ASCII code.
*/
{
using namespace std;
ifstream input_data;
input_data.open(FileName);
if (input_data.is_open())
{
unsigned int counter;
for (counter = 0; counter < size; counter++)
input_data >> massive[counter];
input_data.close();
}
else
{
cout << "The program can not open file " << FileName << " to read data." << endl;
}
}
//To write data (massive) to file.
template <typename T_FileName, typename T_massive>
void WriteMassiveToBinaryFile(
const T_FileName& FileName, const T_massive massive[],
const unsigned int& size)
/*
Target: To write data (massive) to file.
Receive: FileName - name of file where data will be written
Extension - extension of file
massive - massive which will be writted to file
size - number of elements of massive
Node: This procedure write massive to file in bunary mode.
*/
{
using namespace std;
ofstream output_data;
output_data.open(FileName, ios::out | ios::trunc | ios::binary);
if(output_data.is_open())
{
output_data.write((char *)& massive[0], sizeof(T_massive) * size);
output_data.close();
}
else
{
cout << "The program can not open file " << FileName << " to write data." << endl;
}
}
//To write data (massive) to file.
template <typename T_string, typename T_massive>
void WriteMassiveToFile_1D(
const T_string& FileName, const T_massive massive[],
const unsigned int& size, const unsigned int& Ndigits = 15)
/*
Target: To write data (massive) to file.
Receive: FileName - name of file where data will be written
massive - massive which will be writted to file
size - number of elements of massive
Node: This procedure write massive from file like 1D in ASCII code.
*/
{
using namespace std;
ofstream output_data;
output_data.open(FileName);
if(output_data.is_open())
{
unsigned int counter;
for (counter = 0; counter < size; counter++)
{
output_data << std::setiosflags(std::ios::scientific)
<< std::setprecision(Ndigits)
<< massive[counter]
<< endl;
}
output_data.close();
}
else
{
cout << "The program can not open file " << FileName << " to write data." << endl;
}
}
//To add line to and of text file without delete file.
template <typename T_FileName, typename T_Line>
void AddLineToTextFile(
const T_FileName& FileName, const T_Line& AddLine)
/*
Receive: FileName - name of file where data will be added
AddLine - CString which will be added
Node: This procedure write string to end of text file.
*/
{
using namespace std;
ofstream output_data;
output_data.open(FileName, ios::out | ios::app);
if(output_data.is_open())
{
output_data << AddLine << endl;
output_data.close();
}
else
{
cout << "The program can not open file " << FileName << " to write data." << endl;
}
}
template <typename T_string, typename T_massive>
void WriteMassiveToFile_2D(
const T_string& FileName, const T_massive massive[],
const unsigned int& Nx, const unsigned int& Ny,
const unsigned int& width_between_columns = 30,
const unsigned int& Ndigits = 15)
/*
Target: To write data (massive) to file
Receive: file_name - name of file where data will be written
Extension - extension of file
massive - massive which will be writted to file
Nx - number of nodes on OX axis
Ny - number of nodes on OY axis
width_between_columns - width between columns of data in file
Node: This procedure write massive from floating points data
like 2D, the massive in program looks diferent,
like vector - 1D
*/
{
using namespace std;
ofstream output_data;
output_data.open(FileName);
if(output_data.is_open())
{
unsigned int i, j, node_a;
for (i = 0; i < Nx; i++)
{
for (j = 0; j < Ny; j++)
{
node_a = i + j * Nx;
if (j != 0)
output_data << std::setiosflags(std::ios::scientific)
<< std::setw(width_between_columns)
<< std::setprecision(Ndigits)
<< massive[node_a];
else output_data << std::setiosflags(std::ios::scientific)
<< std::setw(width_between_columns)
<< std::setprecision(Ndigits)
<< massive[node_a];
}
if (i < (Nx - 1)) output_data << endl;
}
output_data.close();
}
else
{
cout << "The program can not open file " << FileName << " to write data." << endl;
}
}
#endif //ifndef WriteReadData_H