Skip to main content Skip to navigation

Solver Version 3

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::ofstream;




typedef vector <double> record_t;
typedef vector <record_t> data_t;

//-----------------------------------------------------------------------------
// Let's overload the stream input operator to read a list of CSV fields (which a CSV record).
// Remember, a record is a list of doubles separated by commas ','.
istream& operator >> ( istream& ins, record_t& record )
{
// make sure that the returned record contains only the stuff we read now
record.clear();

// read the entire line into a string (a CSV record is terminated by a newline)
string line;
getline( ins, line );

// now we'll use a stringstream to separate the fields out of the line
stringstream ss( line );
string field;
while (getline( ss, field, ',' ))
{
// for each field we wish to convert it to a double
// (since we require that the CSV contains nothing but floating-point values)
stringstream fs( field );
double f = 0.0; // (default value is 0.0)
fs >> f;

// add the newly-converted field to the end of the record
record.push_back( f );
}

// Now we have read a single line, converted into a list of fields, converted the fields
// from strings to doubles, and stored the results in the argument record, so
// we just return the argument stream as required for this kind of input overload function.
return ins;
}

//-----------------------------------------------------------------------------
// Let's likewise overload the stream input operator to read a list of CSV records.
// This time it is a little easier, just because we only need to worry about reading
// records, and not fields.
istream& operator >> ( istream& ins, data_t& data )
{
// make sure that the returned data only contains the CSV data we read here
data.clear();

// For every record we can read from the file, append it to our resulting data
record_t record;
while (ins >> record)
{
data.push_back( record );
}

// Again, return the argument stream as required for this kind of input stream overload.
return ins;
}

//-----------------------------------------------------------------------------\\
// Now to put it all to use.
int main()
{
// Here is the data we want.
data_t data;

// Here is the file containing the data. Read it into data.
ifstream infile( "example test.csv" );
infile >> data;

// Complain if something went wrong.
if (!infile.eof())
{
cout << "FAIL\n";
return 1;
}

infile.close();

// Otherwise, list some basic information about the file.
cout << "Your CSV file contains " << data.size() << " records.\n" << endl;

unsigned max_record_size = 0;
for (unsigned n = 0; n < data.size(); n++)
if (max_record_size < data[ n ].size())
max_record_size = data[ n ].size();
cout << "The largest record has " << max_record_size << " fields.\n" << endl; // Initialise Line Properties



//-----------------------------------------------------------------------------\\

ofstream myfile;
myfile.open ("output.csv");

//-----------------------------------------------------------------------------\\




const long imax = (data.size()-1);
long n; // current time step
long nmax; //maximum number of time steps
long count = 1;
float dt = 0.0001; // time step delta t
float v = 0.00001511; // Dynamic Viscosity
float rho = 1.205;
int F = 0;

// Boundery Conditions
float mbc1 = 100, mbc2 = -100, cbc1, cbc2;

// Declare Arrays

double a[imax]; // array of coefficient a
double b[imax]; // array of coefficient b
double c[imax]; // array of coefficient c
double A[imax]; // Velocity RHS
double B[imax]; // Pressure RHS
double u[imax]; // array of flow velocities
double p[imax]; // array of flow pressures
double U[imax]; // array of future velocities for the time step n = n + 1


// Set Initial Conditions
int i;
for (i = 0; i <= imax; i++)
{
u[i] = 2;
p[i] = (101325);
}
//p[0] = 101325;
//p[imax] = 100925;






// Calculate coefficients for current time step (a, b, c, A)

do{

for (i = 1; i <= imax; i++)
{
a[i] = (-u[i]/(2*(data[i][1])));
c[i] = (u[i]/((data[i][1])*2));
A[i] = (u[i]*((1/dt)-((4*v)/(data[i][2]*data[i][2]))) - (p[i]*(1/(rho*data[i][1]))) + (p[i-1]*(1/(rho*data[i][1]))) - F);
cout<< a[i]<<"\t"<< b[i]<<"\t"<< c[i]<<"\t"<<A[i]<<"\t"<<B[i]<<endl;
}
for (i = 1; i < imax; i++)
{
b[i] = (0.5*(u[i+1] - u[i])/(data[i][1]))+(1/dt)+((4*v)/(data[i][2]*data[i][2]));
B[i] = ((-2/(data[i][2]))*(0 + (u[i]*((data[i+1][2])-(data[i][2]))/data[i][1]))+;
}
// Calculate b[imax]
b[imax] = (0.5*(0)/(data[imax][1]))+(1/dt)+((4*v)/(data[imax][2]*data[imax][2]));








// Calculate Velocity
for (i=1; i<imax; i++)
{
u[i] = (A[i]/b[i])-((a[i]*u[i-1])/b[i]) - ((c[i]*u[i])/b[i]);
cout<<u[i]<<endl;
}

// Calculate u[imax]and u[0]
u[0] = u[1];
u[imax] = (A[imax]/b[imax])-((a[imax]*u[imax-1])/b[imax]) - ((c[imax]*u[imax+1])/b[imax]);

// Write Data to File

for (i=0; i<imax; i++){
myfile << u[i]<<", ";
}
myfile << u[imax]<<endl;

count++;
}
while (count < 4);

myfile.close();



//// Repeat for pressure

//// Calculate U[i] when time is n + 1 Wtite this to a file

//// Calculate P[i] when time is n + 1/2 Wtite this to a file

//// Increment Time n = n + 1












// identifier = data[m][0];
// dx = data[m][1];
// radius = data[m][2];

//-----------------------------------------------------------------------------\\


// Initialise conditions at time: n = 0 ( u = 0, p = 1000000 throughout )


//cout<< a[imax]<<"\t"<<b[imax]<<"\t"<<c[imax]<<"\t"<<A[imax]<<endl;

return 0;

}