-
Notifications
You must be signed in to change notification settings - Fork 5
/
pipereader.cpp
285 lines (196 loc) · 8.29 KB
/
pipereader.cpp
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "pipereader.h"
#include <iostream>
#include "pressuredropcalculator.h"
#include "beggsbrillcalculator.h"
#include "dptablecalculator.h"
#include "dptable.h"
using std::cout;
using std::endl;
namespace ResOpt
{
PipeReader::PipeReader()
{
}
//-----------------------------------------------------------------------------------------------
// Reads the input file, generates a pressure drop calculator
//-----------------------------------------------------------------------------------------------
PressureDropCalculator* PipeReader::readFile(const QString &file_name)
{
PressureDropCalculator *calc = 0;
// opening the input file
QFile input(file_name);
// checking if file opened ok...
if(!input.open(QIODevice::ReadOnly | QIODevice::Text))
{
qWarning("Could not open PIPE input file: %s", file_name.toLatin1().constData());
exit(1);
}
// starting to read the file, finding what type of calculator to produce
QStringList list = processLine(input.readLine());
while(!input.atEnd() && !list.at(0).startsWith("EOF"))
{
if(list.at(0).startsWith("CORRELATION"))
{
if(list.at(1).startsWith("BB73")) calc = readBeggsBrill(input); // this is a beggs and brill correlation
else if(list.at(1).startsWith("TABLE")) calc = readDpTable(input); // this is a table correlation
}
else if(!isEmpty(list))
{
cout << endl << "### Error detected in PIPE definition file! ###" << endl
<< "File: " << file_name.toLatin1().constData() << endl
<< "Keyword: " << list.join(" ").toLatin1().constData() << endl
<< "Not understood in current context." << endl << endl;
exit(1);
}
list = processLine(input.readLine());
}
// everything ok, closing input file
input.close();
return calc;
}
//-----------------------------------------------------------------------------------------------
// Reads the input file, generates a beggs and brill calculator
//-----------------------------------------------------------------------------------------------
BeggsBrillCalculator* PipeReader::readBeggsBrill(QFile &file)
{
BeggsBrillCalculator* bb = new BeggsBrillCalculator();
double l_diameter = 0.0;
double l_length = 0.0;
double l_angle = 0.0;
double l_temperature = 0.0;
double l_sg_gas = 0.0;
double l_den_oil = 0.0;
double l_den_wat = 0.0;
double l_vis_oil = 0.0;
double l_vis_wat = 0.0;
bool ok = true;
QStringList list = processLine(file.readLine());
while(!file.atEnd() && !list.at(0).startsWith("EOF"))
{
if(list.at(0).startsWith("DIAMETER")) l_diameter = list.at(1).toDouble(&ok); // getting the diameter
else if(list.at(0).startsWith("LENGTH")) l_length = list.at(1).toDouble(&ok); // getting the length
else if(list.at(0).startsWith("ANGLE")) l_angle = list.at(1).toDouble(&ok); // getting the angle
else if(list.at(0).startsWith("TEMPERATURE")) l_temperature = list.at(1).toDouble(&ok); // getting the temp
else if(list.at(0).startsWith("GASGRAVITY")) l_sg_gas = list.at(1).toDouble(&ok); // getting the gas sg
else if(list.at(0).startsWith("OILDENSITY")) l_den_oil = list.at(1).toDouble(&ok); // getting the oil den
else if(list.at(0).startsWith("WATERDENSITY")) l_den_wat = list.at(1).toDouble(&ok); // getting the water den
else if(list.at(0).startsWith("OILVISCOSITY")) l_vis_oil = list.at(1).toDouble(&ok); // getting the oil visc
else if(list.at(0).startsWith("WATERVISCOSITY")) l_vis_wat = list.at(1).toDouble(&ok); // getting the water visc
else
{
if(!isEmpty(list))
{
cout << endl << "### Error detected in PIPE definition file! ###" << endl
<< "File: " << file.fileName().toLatin1().constData() << endl
<< "Keyword: " << list.join(" ").toLatin1().constData() << endl
<< "Not understood in current context." << endl << endl;
exit(1);
}
}
if(!ok) break;
list = processLine(file.readLine());
}
// checking remaining input
if(!ok)
{
cout << endl << "### Error detected in PIPE definition file! ###" << endl
<< "File: " << file.fileName().toLatin1().constData() << endl
<< "Definition is incomplete..." << endl
<< "Last line: " << list.join(" ").toLatin1().constData() << endl;
exit(1);
}
// setting the parameters
bb->setAngle(l_angle);
bb->setDiameter(l_diameter);
bb->setGasSpecificGravity(l_sg_gas);
bb->setLength(l_length);
bb->setOilDensity(l_den_oil);
bb->setOilViscosity(l_vis_oil);
bb->setTemperature(l_temperature);
bb->setWaterDensity(l_den_wat);
bb->setWaterViscosity(l_vis_wat);
return bb;
}
//-----------------------------------------------------------------------------------------------
// Reads the input file, generates a dp table calculator
//-----------------------------------------------------------------------------------------------
DpTableCalculator* PipeReader::readDpTable(QFile &file)
{
DpTableCalculator *dpc = new DpTableCalculator();
DpTable *table = new DpTable();
// starting to read the file
QStringList list = processLine(file.readLine()); // reading a line from the output file
while(!file.atEnd() && !list.at(0).startsWith("EOF"))
{
// format: gas oil water dp
if(list.size() == 4) // the correct number of elements
{
// trying to read the numbers
bool ok_line = true;
QList<double> nums;
for(int i = 0; i < 4; ++i)
{
nums.push_back(list.at(i).toDouble(&ok_line));
if(!ok_line) break;
}
if(ok_line) // all numbers read ok
{
// adding a new row to the vlp table
table->addRow(nums.at(3), nums.at(0), nums.at(1), nums.at(2));
}
else
{
cout << endl << "### Error detected in DP table! ###" << endl
<< "File: " << file.fileName().toLatin1().constData() << endl
<< "Could not convert line to numbers..." << endl
<< "Last line: " << list.join(" ").toLatin1().constData() << endl;
exit(1);
}
}
else if(!isEmpty(list))
{
cout << endl << "### Error detected in DP table! ###" << endl
<< "File: " << file.fileName().toLatin1().constData() << endl
<< "Line does not have the correct number of entries..." << endl
<< "Last line: " << list.join(" ").toLatin1().constData() << endl;
exit(1);
}
list = processLine(file.readLine()); // reading a line from the output file
}
cout << "Added " << table->numberOfRows() << " rows to the table..." << endl;
dpc->setDpTable(table);
return dpc;
}
//-----------------------------------------------------------------------------------------------
// Splits a line read from the driver file into a list of arguments
//-----------------------------------------------------------------------------------------------
QStringList PipeReader::processLine(const QString& line)
{
QString temp = line.split("!").at(0); // removing everything after !
temp = temp.trimmed(); // removing trailing and leadnig whitespaces
QStringList list = temp.split(QRegExp("\\s+"));
while(list.size() < 2)
{
list.push_back(" ");
}
return list;
}
//-----------------------------------------------------------------------------------------------
// Checks if the line is empty or not (white spaces)
//-----------------------------------------------------------------------------------------------
bool PipeReader::isEmpty(const QStringList &list)
{
bool ok = true;
for(int i = 0; i < list.size(); i++)
{
QString temp = list.at(i);
temp.remove(QRegExp("\\s+"));
if(temp.size() != 0)
{
ok = false;
break;
}
}
return ok;
}
} // namespace ResOpt