0% found this document useful (0 votes)
7 views6 pages

Assign 2 Code

The document contains a C++ program that simulates fluid dynamics using numerical methods, specifically for calculating vorticity and stream functions in a defined grid. It includes functions for checking wall boundaries, writing output data to CSV files, and iterating through time steps to update fluid properties. The program employs a finite difference method to solve the governing equations and applies boundary conditions for various sections of the fluid domain.

Uploaded by

11silal04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Assign 2 Code

The document contains a C++ program that simulates fluid dynamics using numerical methods, specifically for calculating vorticity and stream functions in a defined grid. It includes functions for checking wall boundaries, writing output data to CSV files, and iterating through time steps to update fluid properties. The program employs a finite difference method to solve the governing equations and applies boundary conditions for various sections of the fluid domain.

Uploaded by

11silal04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <iostream>

#include <vector>
#include <math.h>
#include <fstream>
#include <string>
#include <bits/stdc++.h>

using namespace std;

bool isWall(int i, int j, int mx, int my, int lt2, double bigD, double smallD)
{
int c3 = (mx / 2) - (lt2 / 2);

double k1l = (bigD - smallD) / 2.0;


double dy = bigD / my;
int k1 = k1l / dy;
int k2 = my - k1 + 1;

if (i > c3 && i <= c3 + lt2)


{
if (1 <= j && j <= k1)
{
return true;
}
else if (k2 <= j && j <= my)
{
return true;
}
}
return false;
}
void writer(vector<std::vector<double>> &v, string name, int mx, int my, int time,
int k1, int k2, int c3, int lt2, double dy)
{
ofstream file("outputs/" + name + "_" + to_string(time) + ".csv");
for (int j = 1; j <= my; j++)
{
for (int i = 1; i <= mx; i++)
{
if (i > c3 && i <= c3 + lt2)
{
if (1 <= j && j <= k1)
{
file << 0 << ",";
continue;
}
if (k2 <= j && j <= my)
{
file << 0 << ",";
continue;
}
}
file << v[i][j] << ",";
}
file << "\n";
}
}
int main()
{
double entry_velocity, nu, lt, ht, dt, d, dy, beta, adv, dif, corr;
int mx, my;
int c3;
double sHt;
double length;
int lt2;

lt = 10.0;
ht = 2.2;
sHt = 0.6;
mx = 50;
my = 11;
entry_velocity = 2.0;
nu = 0.01;
dt = 0.0006;
dy = ht / my; // 0.2
d = dt / (dy * dy);
beta = 1;
length = 4.00;
lt2 = length / dy + 1; // 20
c3 = (mx / 2.0) - (lt2 / 2);

double k1l = (ht - sHt) / 2;


int k1 = k1l / dy;
int k2 = my - k1 + 1;
cout << k1 << " " << k2 << endl;

vector<vector<double>> omgN(mx + 2, vector<double>(my + 2, 0));


vector<vector<double>> psi(mx + 2, vector<double>(my + 2, 0));
vector<vector<double>> vx(mx + 2, vector<double>(my + 2, 0));
vector<vector<double>> vy(mx + 2, vector<double>(my + 2, 0));
vector<vector<double>> omgP(mx + 2, vector<double>(my + 2, 0));
vector<vector<double>> residue(mx + 2, vector<double>(my + 2, 0));

vector<double> y(my + 2, 0);


y[1] = dy / 2;

// GENERATING GRID
for (int j = 0; j < my + 2; j++)
{
for (int i = 0; i < mx + 2; i++)
{
if (isWall(i, j, mx, my, lt2, ht, sHt))
{
cout << " | ";
}
else
{
cout << " - ";
}
}
cout << endl;
}

for (int i = 2; i < my + 1; i++)


{
y[i] = y[1] + (i - 1) * dy;
}
// updation of boundary conditions
for (int i = 0; i < mx + 2; i++)
{
if (i <= c3 || i > c3 + lt2)
{
omgN[i][0] = -4 * (vx[i][1] / dy) - omgN[i][1]; // top bigger
section
omgN[i][my + 1] = 4 * (vx[i][my] / dy) - omgN[i][my]; // bottom bigger
section
}
else
{
omgN[i][k1] = -4 * (vx[i][k1 + 1] / dy) - omgN[i][k1 + 1]; // top
smaller section
omgN[i][k2] = 4 * (vx[i][k2 - 1] / dy) - omgN[i][k2 - 1]; // bottom
smaller section
}
}
for (int j = 0; j < my + 2; j++)
{
omgN[0][j] = -omgN[1][j]; // left or entrance
omgN[mx + 1][j] = omgN[mx][j]; // right or exit

if ((1 <= j && j <= k1) || k2 <= j && j <= my)


{
omgN[c3 + 1][j] = (4 * vy[c3][j] / dy) - omgN[c3]
[j]; // orifice left wall
omgN[c3 + lt2][j] = (4 * vy[c3 + 3][j] / dy) - omgN[c3 + lt2 + 1]
[j]; // orifice right wall
}
}

int step = 1;
do
{
double ue, uw, vn, vs, we, ww, ws, wn;

for (int i = 1; i < mx + 1; i++)


{
for (int j = 1; j < my + 1; j++)
{
if (isWall(i, j, mx, my, lt2, ht, sHt))
continue;
ue = (vx[i][j] + vx[i + 1][j]) / 2;
uw = (vx[i][j] + vx[i - 1][j]) / 2;
vn = (vy[i][j] + vy[i][j + 1]) / 2;
vs = (vy[i][j] + vy[i][j - 1]) / 2;

// FIRST ORDER UPWIND SCHEME FOR VORTICITIES


we = (ue > 0) ? omgP[i][j] : omgP[i + 1][j];
ww = (uw > 0) ? omgP[i - 1][j] : omgP[i][j];
wn = (vn > 0) ? omgP[i][j] : omgP[i][j + 1];
ws = (vs > 0) ? omgP[i][j - 1] : omgP[i][j];

adv = (we * ue - ww * uw) * dy + (wn * vn - ws * vs) * dy;


dif = nu * (omgP[i][j + 1] + omgP[i][j - 1] + omgP[i + 1][j] +
omgP[i - 1][j] - (4 * omgP[i][j]));
omgN[i][j] = d * (dif - adv) + omgP[i][j];
}
}

double sum, rms;

do
{
sum = 0.0;

// Implementation of Stream function's boundary conditions


for (int i = 0; i < mx + 2; i++)
{
if (i <= c3 || i > c3 + lt2)
{
psi[i][my + 1] = 2 * entry_velocity * ht - psi[i][my]; // top
bigger section
psi[i][0] = -psi[i][1]; //
bottom bigger section
}
else
{
psi[i][k1] = -psi[i][k1 + 1]; // top
smaller section
psi[i][k2] = 2 * entry_velocity * ht - psi[i][k2 - 1]; //
bottom smaller section
}
}
for (int j = 0; j < my + 2; j++)
{
psi[0][j] = 2 * entry_velocity * y[j] - psi[1][j]; // entrance wall
psi[mx + 1][j] = psi[mx][j]; // exit wall

if ((1 <= j && j <= k1))


{
psi[c3 + 1][j] = -psi[c3][j]; // contraction wall upper
psi[c3 + lt2][j] = -psi[c3 + lt2 + 1][j];
}
if ((k2 <= j && j <= my))
{
psi[c3 + 1][j] = (2 * entry_velocity * ht) - psi[c3][j];
psi[c3 + lt2][j] = (2 * entry_velocity * ht) - psi[c3 + lt2 +
1][j]; // contraction wall lower
}
}

// GAUSS SEIDEL FOR STREAM FUNCTION CALCULATION

for (int i = 1; i < mx + 1; i++)


{
for (int j = 1; j < my + 1; j++)
{
if (isWall(i, j, mx, my, lt2, ht, sHt))
continue;

residue[i][j] = omgN[i][j] * dy * dy + psi[i + 1][j] + psi[i -


1][j] + psi[i][j + 1] + psi[i][j - 1] - (4 * psi[i][j]);
corr = residue[i][j] * (beta / 4);
psi[i][j] = psi[i][j] + corr;
sum = sum + (residue[i][j] * residue[i][j]);
}
}
rms = sqrt(sum / (mx * my));
// cout << "Hehe: " << rms << endl;
} while (rms > 1.0e-6);
cout << "\n STEP: " << step << "RMS: " << rms;

// velocity components updation


for (int i = 1; i < mx + 1; i++)
{
for (int j = 1; j < my + 1; j++)
{
if (isWall(i, j, mx, my, lt2, ht, sHt))
continue;
vx[i][j] = (psi[i][j + 1] - psi[i][j - 1]) / (2 * dy);
vy[i][j] = (psi[i - 1][j] - psi[i + 1][j]) / (2 * dy);
}
}

// updation of boundary conditions

for (int i = 0; i < mx + 2; i++)


{

if (i <= c3 || i > c3 + lt2) // top and bottom bigger section


{
vy[i][0] = -vy[i][1]; // top
vx[i][0] = -vx[i][1]; // top
omgN[i][0] = -4 * (vx[i][1] / dy) - omgN[i][1]; // top

vy[i][my + 1] = -vy[i][my]; // bottom


vx[i][my + 1] = -vx[i][my]; // bottom
omgN[i][my + 1] = 4 * (vx[i][my] / dy) - omgN[i][my]; // bottom
}
else // Smaller section
{
vy[i][k1] = -vy[i][k1 + 1]; // top
vx[i][k1] = -vx[i][k1 + 1]; // top
omgN[i][k1] = -4 * (vx[i][k1 + 1] / dy) - omgN[i][k1 + 1]; // top

vy[i][k2] = -vy[i][k2 - 1]; // bottom


vx[i][k2] = -vx[i][k2 - 1]; // bottom
omgN[i][k2] = 4 * (vx[i][k2 - 1] / dy) - omgN[i][k2 - 1]; // bottom
}
}
for (int j = 0; j < my + 2; j++)
{
omgN[0][j] = -omgN[0][j]; // entry or left
vx[0][j] = 2 * entry_velocity - vx[0][j]; // entry or left
vy[0][j] = -vy[0][j]; // entry or left

omgN[mx + 1][j] = omgN[mx][j]; // exit or right


vx[mx + 1][j] = vx[mx][j]; // exit or right
vy[mx + 1][j] = vy[mx][j]; // exit or right

if (1 <= j && j <= k1) // upper contraction wall


{
omgN[c3 + 1][j] = (4 * vy[c3][j] / dy) - omgN[c3][j];
omgN[c3 + lt2][j] = (4 * vy[c3 + lt2 + 1][j] / dy) - omgN[c3 + lt2
+ 1][j];
vx[c3 + 1][j] = -vx[c3][j];
vy[c3 + 1][j] = -vy[c3][j];

vx[c3 + lt2][j] = -vx[c3 + lt2 + 1][j];


vy[c3 + lt2][j] = -vy[c3 + lt2 + 1][j];
}
else if (k2 <= j && j <= my) // lower contraction wall
{
omgN[c3 + 1][j] = (4 * vy[c3][j] / dy) - omgN[c3][j];
omgN[c3 + lt2][j] = (4 * vy[c3 + lt2 + 1][j] / dy) - omgN[c3 + lt2
+ 1][j];

vx[c3 + 1][j] = -vx[c3][j];


vy[c3 + 1][j] = -vy[c3][j];

vx[c3 + lt2][j] = -vx[c3 + lt2 + 1][j];


vy[c3 + lt2][j] = -vy[c3 + lt2 + 1][j];
}
}

step++;
// writeToFile(vx,vy,psi, omgN, mx, my, step, k1, k2, c3,dy);
// writeToFile(psi, mx, my, "psi", step, k1, k2, c3);
// writeToFile(omgN, mx, my, "vorticity", step, k1, k2, c3);
writer(vx, "vx", mx, my, step, k1, k2, c3, lt2, dy);
writer(omgN, "vorticity", mx, my, step, k1, k2, c3, lt2, dy);
writer(psi, "psi", mx, my, step, k1, k2, c3, lt2, dy);

omgP = omgN;

// cout << "\nStep: " << step;


} while (step < 200);
}

You might also like