· 9 years ago · Nov 25, 2016, 10:16 PM
1#include <stdio.h>
2#include <stdlib.h>
3#include <cstring>
4#include <stdbool.h>
5#include <cmath>
6#include <iomanip>
7#include <iostream>
8#include <fstream>
9#define NUMROWS 10
10#define NUMCOLS 10
11#define N_MIN 0.001
12#define N_MAX 0.1
13double flowrate[NUMROWS][NUMCOLS];
14using namespace std;
15
16
17struct PARAM {
18 double s; //slope of water, real value
19 double z; //slope of channel side, real value
20 double n; // coefficient of roughness, real value
21};
22
23struct HEADING {
24 double initialValue; //initial value of parameter
25 double stepSize; //step size for parameter
26};
27
28struct FLOW_RATE_TABLE {
29 string name; //table name
30 struct PARAM params; //a PARAM structure
31 struct HEADING baseWidth; //a HEADING structure
32 struct HEADING waterDepth; //a HEADING structure
33 double flowRates[NUMROWS][NUMCOLS];
34};
35
36struct FLOW_RATE_TABLE flowRateTable;
37
38void userInterface();
39void addFlowRateTable();
40bool setflowRateTablename();
41double readValueGTzero();
42double computeFlowRate(struct PARAM params, double b, double y);
43void loadFlowRateTable();
44void displayFlowRateTable();
45void estimateWaterDepth();
46int getColIndex(double bGiven);
47int getRowIndex(int cix, double qGiven);
48
49int main () {
50 string empty = "";
51 flowRateTable.name = empty;
52 userInterface();
53 return(0);
54}
55
56
57void userInterface(){
58 int selection(0);
59 do { //Repeat
60 cout << "1) Create a flow rate table \n"; //Print "1) Create a flow rate table"
61 cout << "2) Load an existing flow rate table \n"; //Print “2) Load an existing flow rate tableâ€
62 cout << "3) Display the currently loaded flow rate table \n"; //Print “3) Display the currently loaded flow rate tableâ€
63 cout << "4) Estimate water depth using flow rate table \n"; //Print “4) Estimate water depth using flow rate tableâ€
64 cout << "5) Terminate \n"; //Print “5) Terminate"
65 string empty = "";
66 flowRateTable.name = empty;
67 if (flowRateTable.name != empty) { //If flowRateTable.name not empty string
68 cout << "Table" << flowRateTable.name << "loaded \n"; //Print “(Table “, flowRateTable.name, “loaded)â€
69 }
70 else { //Otherwise
71 cout << "No table loaded \n"; //Print “(No table loaded)â€
72 }
73
74 do {//Repeat
75 cout << "Please make a selection (1-5) \n"; //Print "Please make a selection (1-5):"
76 cin >> selection; //Read value into selection
77 if (selection == 1) {//If selection equals 1
78 addFlowRateTable(); //addFlowRateTable()
79 }
80 else if (selection == 2) {//Else if selection equals 2
81 loadFlowRateTable(); //loadFlowRateTable()
82 }
83 else if (selection == 3) {//Else if selection equals 3
84 displayFlowRateTable(); //displayFlowRateTable()
85 }
86 else if (selection == 4) {//Else if selection equals 4
87 estimateWaterDepth(); //estimateWaterDepth()
88 }
89 else if (selection == 5) {//Else if selection equals 5
90 cout << "Terminating \n"; //Print “Terminatingâ€
91 }
92 } while (selection < 1 || selection > 5); //while selection less than 1 or greater than 5
93 } while (selection != 5); //while selection not equal to 5
94}
95
96void addFlowRateTable(){
97 bool result = setflowRateTablename();
98 if (result == 1){ //if setFlowRateTableName() equals OK
99 flowRateTable.params.s= readValueGTzero(); //Assign readValueGTzero(“slope of the waterâ€) to flowRateTable.params.s
100 flowRateTable.params.z=readValueGTzero(); //Assign readValueGTzero(“slope of the channel sidesâ€) to flowRateTable.params.z
101 do{
102 cout << "Please give the coefficient of roughness: \n"; //Print “Please give the coefficient of roughness: “
103 cin >> flowRateTable.params.n; //Read value into flowRateTable.params.n
104 if (flowRateTable.params.n < N_MIN || flowRateTable.params.n > N_MAX){ //If flowRateTable.params.n less than N_MIN or greater than N_MAX
105 cout << "Coefficient of roughness must lie within the range of" << N_MAX << "and" << N_MIN << endl; //Print “Coefficient of roughness must lie in the range ofâ€, N_MIN, “ and “, N_MAX
106 }
107 }
108 while (flowRateTable.params.n < N_MIN || flowRateTable.params.n > N_MAX); //While flowRateTable.params.n less than N_MIN or greater than N_MAX
109 flowRateTable.baseWidth.initialValue = readValueGTzero(); //Assign readValueGTzero(“initial base widthâ€) to flowRateTable.baseWidth.initialValue
110 flowRateTable.baseWidth.stepSize = readValueGTzero(); //Assign readValueGTzero(“step sizeâ€) to flowRateTable.baseWidth.stepSize
111 flowRateTable.waterDepth.initialValue = readValueGTzero(); //Assign readValueGTzero(“initial water depthâ€) to flowRateTable.waterDepth.initialValue
112 flowRateTable.waterDepth.stepSize = readValueGTzero(); //Assign readValueGTzero(“step sizeâ€) to flowRateTable.waterDepth.stepSize ( updates the matrix )
113 }
114 int rix = 0; //Assign 0 to rix
115 double y[10], b[10];
116 int cix=0;
117 y[0]=flowRateTable.waterDepth.initialValue;
118 b[0]=flowRateTable.baseWidth.initialValue;
119 double flowRate[NUMROWS][NUMCOLS];
120
121 rix=0;
122
123 while (rix<NUMROWS-1){ //a while loop to calculate all values of waterdepth
124 y[rix+1]=y[rix]+flowRateTable.waterDepth.stepSize;
125 rix++;
126 }
127
128 cix=0; //initialize cix to 0;
129
130
131 while (cix<NUMCOLS-1){ //a while loop to calculate all values of basewidth
132 b[cix+1]=b[cix]+flowRateTable.baseWidth.stepSize;
133 cix++;
134
135 }
136
137
138
139 cix = 0, rix = 0;
140
141 while (rix<NUMROWS){ //a while loop to calculate for the rate flow rate
142 cix=0;
143 while (cix<NUMCOLS){
144 flowRate[rix][cix]= computeFlowRate(flowRateTable.params, b[cix],y[rix]);
145 cix++; //increment cix by 1
146 }
147 rix++; //increment rix by 1
148 }
149
150
151
152
153
154
155
156 string fileName;
157 fileName=flowRateTable.name +".txt"; //Append “.txt†to flowRateTable.name and assign to fileName
158 ifstream file;
159 file.open(fileName, ios::in);
160 if (file.fail()) {//If can open fileName for writing as filePtr
161
162 cerr << "File cannot be opened \n";
163 }
164
165 ofstream filePtr;
166 filePtr.open(fileName, ios::out);
167
168 filePtr << flowRateTable.name<<endl;
169 filePtr << flowRateTable.params.n<<endl;
170 filePtr << flowRateTable.params.s<<endl;
171 filePtr << flowRateTable.params.z<<endl;
172 filePtr << flowRateTable.baseWidth.initialValue<<endl;
173 filePtr << flowRateTable.baseWidth.stepSize<<endl;
174 filePtr << flowRateTable.waterDepth.initialValue<<endl;
175 filePtr << flowRateTable.waterDepth.stepSize<<endl;
176
177
178 for (int i=0; i <NUMROWS;i++)
179 {
180 for (int j=0; j <NUMCOLS; j++) {
181 filePtr<<flowRate[i][j] << setw(10)<<endl;
182 }
183 filePtr << endl;
184 }
185 //Write flowRateTable into file filePtr
186 filePtr.close(); //Close file filePtr
187 cout << "Table " << flowRateTable.name << " created" << endl; //Print “Table “, flowRateTable.name, “ createdâ€
188}
189
190
191bool setflowRateTablename(){
192 bool result = true;
193 ifstream file;
194 int overwrite;
195 cout << "Please enter a table name: \n";
196 cin >> flowRateTable.name; //Get input
197 file.open(flowRateTable.name +".txt", ios::out); //try opening file
198 if (!file.fail()) { //if file exist
199 cout << "The file already exists. Would you like to overwrite it? Press 1 for yes, press 2 for no. \n";
200 cin >> overwrite;
201 if (overwrite == 1) {
202 file.open("flowRateTable.name.txt", ios::out);
203 file.close();
204 result = true;
205 }
206 else {
207 result = false;
208 }
209 }
210
211 return result;
212}
213
214
215
216double readValueGTzero(){
217 double prompt;
218 cout << "Please enter the value \n";
219 cin >> prompt;
220 while (prompt<0){
221 cout << "Please enter a positive value \n";
222 cin >> prompt;
223 }
224
225 return prompt;
226}
227
228double computeFlowRate(struct PARAM params, double b, double y) {
229 double area, perimeter, rh, q;
230 area = y * b + pow(y, 2) * params.z; //assign yb+y2*params.z to area
231 perimeter = b + 2 * y * sqrt(pow(params.z, 2)+1); // assign b+2y(1+params.z2)1/2 to perimeter
232 rh = area / perimeter; //assign area/perimeter to rh
233 q = (1.0/params.n) * area *pow(rh, 2.0/3.0) * pow(params.s, 1.0/2.0); //assign (1/params.n) * area * rh2/3 * params.s1/2 to q
234 return q;
235
236}
237
238
239void loadFlowRateTable() {
240 string filename, fileName;
241 double flowratetable[NUMROWS][NUMCOLS];
242
243 ifstream filePtr;
244 cout<<"Enter the name of the table "<<endl;
245 cin>>filename;
246 filename= filename+".txt";
247 filePtr.open(filename, ios::in);
248 if (filePtr.fail()){
249 cerr<<"Couldn't open the file "<<endl;
250 exit(10);
251 }
252
253 filePtr >> flowRateTable.name;
254 filePtr >> flowRateTable.params.n;
255 filePtr >> flowRateTable.params.s;
256 filePtr >> flowRateTable.params.z;
257 filePtr >> flowRateTable.baseWidth.initialValue;
258 filePtr >> flowRateTable.baseWidth.stepSize;
259 filePtr >> flowRateTable.waterDepth.initialValue;
260 filePtr >> flowRateTable.waterDepth.stepSize;
261
262
263 for (int i=0; i <NUMROWS;i++)
264 {
265 for (int j=0; j <NUMCOLS; j++)
266 {
267 filePtr >> flowrate[i][j];
268 }
269 }
270 cout<<"loading complete"<<endl;
271}
272
273
274void displayFlowRateTable() {
275 loadFlowRateTable();
276
277 cout << flowRateTable.name << endl;
278 cout << "Water surface slope: " << flowRateTable.params.s << "m/m" << setw(10) << "Channel side slope: " << flowRateTable.params.z << endl;
279 cout << "Manning coefficient of roughness: " << flowRateTable.params.n << endl;
280 cout<<setw(10)<<"D | W" << setw(10);
281 for (int i=0; i < NUMCOLS; i++) {
282 cout << flowRateTable.baseWidth.initialValue + i * flowRateTable.baseWidth.stepSize<<setw(9);
283 }
284 cout<<endl;
285 cout << "-------+-------------------------------------------------------------------------" << endl;
286 for (int i=0; i < NUMROWS; i++) {
287 cout << setw(10)<<flowRateTable.waterDepth.initialValue + i * flowRateTable.waterDepth.stepSize << " | " << setw(10);
288 for (int j=0; j < NUMCOLS;j++) {
289
290 cout << flowrate[i][j]<<setw(10);
291 }
292 cout<<endl;
293 }
294}
295
296void estimateWaterDepth() {
297 double b, q, stepFraction, waterDepthEstimate;
298 int cix, rix;
299
300 do//Repeat
301 {
302 cout << "Please enter a base width" << endl; //Print “Please enter a base width: â€
303 cin >> b; //Read value into b
304 cix = getColIndex(b); //Assign getColIndex(b) to cix
305 if (cix == -1){ //If cix equals -1
306 cout << "Invalid base width value, must be in the table heading" << endl; //Print “Invalid base width value, must be in the table headingâ€
307 }
308 } while (cix == -1); //While cix equals -1
309 do//Repeat
310 {
311 cout << "Please enter a flow rate" << endl; //Print “Please enter a flow rate: â€
312 cin >> q; //Read value into q
313 rix = getRowIndex(cix,q); //Assign getRowIndex(cix, q) to rix
314 if (rix == -1){ //If rix equals -1
315 cout << "Invalid value, must be in the range of the base width column" << endl; //Print "Invalid value, must be in the range of the base width column"
316 }
317 } while (rix == -1); //While cix equals -1
318 if (rix == NUMROWS - 1) {//If rix equals NUMROWS-1
319 waterDepthEstimate = flowRateTable.flowRates[NUMROWS - 1][cix]; //Assign flowRateTable.flowRates[NUMROWS-1,cix] to waterDepthEstimate
320 }
321 else {//Otherwise
322 stepFraction = (q-flowRateTable.flowRates[rix][cix]) / (flowRateTable.flowRates[rix + 1][cix]- flowRateTable.flowRates[rix][cix]); //Assign (qflowRateTable.flowRates[rix][cix])/(flowRateTable.flowRates[rix+1][cix]- flowRateTable.flowRates[rix][cix]) to stepFraction
323 waterDepthEstimate = flowRateTable.waterDepth.initialValue + flowRateTable.waterDepth.stepSize * rix + stepFraction * flowRateTable.waterDepth.stepSize; //Assign flowRateTable.waterDepth.initial+ flowRateTable.waterDepth.stepSize*rix +stepFraction*flowRateTable.waterDepth.stepSize to waterDepthEstimate
324 cout << "Water depth is estimated at" << waterDepthEstimate << endl; //Print “Water depth is estimated at “, waterDepthEstimate
325 }
326}
327
328
329int getColIndex(double bGiven) {
330 int retValue = -1; //Assign -1 to retValue
331 double b = flowRateTable.baseWidth.initialValue; //Assign flowRateTable.baseWidth.initialValue to b
332 int cix = 0;
333 double tolerance = 0.001*flowRateTable.baseWidth.stepSize; //Assign 0.001*flowRateTable.baseWidth.stepSize to tolerance
334 while (cix < NUMCOLS && retValue == -1) {//Repeat while cix less than NUMCOLS and retValue equals -1
335 if (abs(b - bGiven) < tolerance) { //If absolute value of b-bGiven less than tolerance
336 cix = retValue; //Assign cix to retValue
337 cix++; //Increment cix
338 }
339 b = b + flowRateTable.baseWidth.stepSize; //Assign b+flowRateTable.baseWidth.stepSize to b
340 }
341 return(retValue); //Return retValue
342}
343
344
345int getRowIndex(int cix, double qGiven) {
346 int retValue = -1; //Assign -1 to retValue
347 double q = flowRateTable.baseWidth.initialValue; //Assign flowRateTable.baseWidth.initialValue to q
348 cix = 0; //Assign 0 to cix
349 double tolerance = 0.001*flowRateTable.baseWidth.stepSize; //Assign 0.001*flowRateTable.baseWidth.stepSize to tolerance
350 while (cix < NUMROWS && retValue == -1) {//Repeat while cix less than NUMROWS and retValue equals -1
351 //f=flowrates[rix][cix];
352 if (abs(q - qGiven) < tolerance) {//If absolute value of q - qGiven less than tolerance
353 cix = retValue; //Assign cix to retValue
354 //f=f+(flowrates[rix+1][cix]-flowrates[rix]cix]);
355 cix++; //Increment cix
356 }
357 q = q + flowRateTable.baseWidth.stepSize; //Assign b+flowRateTable.baseWidth.stepSize to q
358 }
359 return(retValue); //Return retValue
360}