Changes

Jump to: navigation, search

BetaT

2,376 bytes removed, 21:56, 9 April 2017
Application Code
Navier–Stokes equations are useful because they describe the physics of many phenomena of scientific and engineering interest. They may be used to model the weather, ocean currents, water flow in a pipe and air flow around a wing. The Navier–Stokes equations in their full and simplified forms help with the design of aircraft and cars, the study of blood flow, the design of power stations, the analysis of pollution, and many other things. Coupled with Maxwell's
equations they can be used to model and study magnetohydrodynamics. courtesy of wikipedia ("https://en.wikipedia.org/wiki/Navier%E2%80%93Stokes_equations")
 
=== Application Code ===
 
 
/*
* Solves 1D convection equation, also known as the one-way wave equation.
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <cstdlib>
#include <chrono>
 
using namespace std::chrono;
using namespace std;
void saveArray(vector< vector<double> > &u, int nx, int nt, double dx, double dt, int c);
 
void reportTime(const char* msg, steady_clock::duration span) {
auto ms = duration_cast<milliseconds>(span);
std::cout << msg << " - took - " <<
ms.count() << " millisecs" << std::endl;
}
 
 
 
int main(int argc,char** argv)
{
if (argc != 3 )
{
std::cerr << "Invalid number of arguments " << argv[0] << "\n";
return 1;
}
steady_clock::time_point ts, te;
ts = steady_clock::now();
int nx = std::atoi(argv[1]);
int nt = std::atoi(argv[2]);
double dx = double(2)/(nx-1);
double dt = 0.01;
int c = 10;
std::cout << c*dt / dx << "\n";
// ionitalize
 
 
vector< vector<double> > u(nx,vector<double> (nt));
vector< vector<double> > un(nx,vector<double> (nt));
// Initial condition:
//ts = steady_clock::now();
for (int i=0; i <= nx-1; i++)
{
if (i*dx >= 0.5 && i*dx <= 1)
{
u[i][0] = 2;
}
else
{
u[i][0] = 1;
}
}
// te= steady_clock::now();
// reportTime("Fist for loop", te - ts);
 
// Finite-difference loop:
//ts = steady_clock::now();
for (int it=1; it<=nt-1; it++)
{
for (int k=0; k<=nx-1; k++)
{
un[k][it-1] = u[k][it-1];
}
for (int i=1; i<=nx-1; i++)
{
u[0][it] = un[1][it-1];
u[i][it] = un[i][it-1] - c*dt/dx*(un[i][it-1]-un[i-1][it-1]);
}
}
 
saveArray(un, nx, nt, dx, dt, c);
te = steady_clock::now();
reportTime("Total Time", te - ts);
 
system("pause");
return 0;
}
// Save array to file:
void saveArray(vector< vector<double> > &u, int nx, int nt, double dx, double dt, int c)
{
ofstream myfile;
myfile.open("1d_convection02.dat");
myfile << "%\t" << "nx = " << nx << "\tnt = " << nt <<
"\tdt = " << dt << "\tc = " << c << endl;
for (int i=0; i<=nx-1; i++)
{
myfile << std::setw(8) << std::setfill(' ') << std::left << i*dx << "\t\t";
for (int j=0; j<=nt-1; j++)
{
myfile << std::setw(8) << std::setfill(' ') << std::left << u[i][j] << "\t\t";
}
myfile << endl;
}
}
=== problem ===
212
edits

Navigation menu