Structural changes in 2D versions

This commit is contained in:
2023-06-27 16:24:55 +02:00
parent acc831e0b0
commit ca99356d45
62 changed files with 100926 additions and 56 deletions

View File

@@ -17,9 +17,14 @@
#include "timing.h"
#include <mpi.h>
int main(int argc, char** argv)
enum VARIANT { SOR = 1, RB, RBA };
int main (int argc, char** argv)
{
int rank;
int variant = SOR;
double S, E;
Parameter params;
Solver solver;
@@ -28,12 +33,16 @@ int main(int argc, char** argv)
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
initParameter(&params);
if (argc != 2) {
if (argc < 2) {
printf("Usage: %s <configFile>\n", argv[0]);
exit(EXIT_SUCCESS);
}
readParameter(&params, argv[1]);
if (argc == 3)
{
variant = atoi(argv[2]);
}
if (rank == 0) {
printParameter(&params);
}
@@ -47,7 +56,11 @@ int main(int argc, char** argv)
double t = 0.0;
S = getTimeStamp();
while (t <= te) {
switch (variant) {
case SOR:
printf("Plain SOR\n");
while (t <= te) {
if (tau > 0.0) {
computeTimestep(&solver);
}
@@ -68,6 +81,58 @@ int main(int argc, char** argv)
printProgress(t);
#endif
}
break;
case RB:
printf("Red-black SOR\n");
while (t <= te) {
if (tau > 0.0) {
computeTimestep(&solver);
}
setBoundaryConditions(&solver);
setSpecialBoundaryCondition(&solver);
computeFG(&solver);
computeRHS(&solver);
solveRB(&solver);
adaptUV(&solver);
t += solver.dt;
#ifdef VERBOSE
if (rank == 0) {
printf("TIME %f , TIMESTEP %f\n", t, solver.dt);
}
#else
printProgress(t);
#endif
}
break;
case RBA:
printf("Red-black SOR with acceleration\n");
while (t <= te) {
if (tau > 0.0) {
computeTimestep(&solver);
}
setBoundaryConditions(&solver);
setSpecialBoundaryCondition(&solver);
computeFG(&solver);
computeRHS(&solver);
solveRBA(&solver);
adaptUV(&solver);
t += solver.dt;
#ifdef VERBOSE
if (rank == 0) {
printf("TIME %f , TIMESTEP %f\n", t, solver.dt);
}
#else
printProgress(t);
#endif
}
break;
}
E = getTimeStamp();
stopProgress();
if (rank == 0) {

View File

@@ -21,6 +21,7 @@ void initParameter(Parameter* param)
param->itermax = 1000;
param->eps = 0.0001;
param->omg = 1.8;
param->rho = 0.99;
}
void readParameter(Parameter* param, const char* filename)
@@ -75,6 +76,8 @@ void readParameter(Parameter* param, const char* filename)
PARSE_REAL(u_init);
PARSE_REAL(v_init);
PARSE_REAL(p_init);
PARSE_REAL(rho);
}
}

View File

@@ -11,7 +11,7 @@ typedef struct {
double xlength, ylength;
int imax, jmax;
int itermax;
double eps, omg;
double eps, omg, rho;
double re, tau, gamma;
double te, dt;
double gx, gy;

View File

@@ -243,6 +243,156 @@ void computeTimestep(Solver* s)
s->dt = dt * s->tau;
}
int solveRB(Solver* s)
{
int imax = s->imax;
int jmax = s->jmax;
int imaxLocal = s->comm.imaxLocal;
int jmaxLocal = s->comm.jmaxLocal;
double eps = s->eps;
int itermax = s->itermax;
double dx2 = s->dx * s->dx;
double dy2 = s->dy * s->dy;
double idx2 = 1.0 / dx2;
double idy2 = 1.0 / dy2;
double factor = s->omega * 0.5 * (dx2 * dy2) / (dx2 + dy2);
double* p = s->p;
double* rhs = s->rhs;
double epssq = eps * eps;
int it = 0;
double res = 1.0;
commExchange(&s->comm, p);
while ((res >= epssq) && (it < itermax)) {
res = 0.0;
for (int j = 1; j < jmaxLocal + 1; j++) {
for (int i = 1; i < imaxLocal + 1; i++) {
double r = RHS(i, j) -
((P(i + 1, j) - 2.0 * P(i, j) + P(i - 1, j)) * idx2 +
(P(i, j + 1) - 2.0 * P(i, j) + P(i, j - 1)) * idy2);
P(i, j) -= (factor * r);
res += (r * r);
}
}
if (commIsBoundary(&s->comm, BOTTOM)) { // set bottom bc
for (int i = 1; i < imaxLocal + 1; i++) {
P(i, 0) = P(i, 1);
}
}
if (commIsBoundary(&s->comm, TOP)) { // set top bc
for (int i = 1; i < imaxLocal + 1; i++) {
P(i, jmaxLocal + 1) = P(i, jmaxLocal);
}
}
if (commIsBoundary(&s->comm, LEFT)) { // set left bc
for (int j = 1; j < jmaxLocal + 1; j++) {
P(0, j) = P(1, j);
}
}
if (commIsBoundary(&s->comm, RIGHT)) { // set right bc
for (int j = 1; j < jmaxLocal + 1; j++) {
P(imaxLocal + 1, j) = P(imaxLocal, j);
}
}
commReduction(&res, SUM);
res = res / (double)(imax * jmax);
#ifdef DEBUG
if (commIsMaster(&s->comm)) {
printf("%d Residuum: %e\n", it, res);
}
#endif
it++;
}
}
int solveRBA(Solver* s)
{
int imax = s->imax;
int jmax = s->jmax;
int imaxLocal = s->comm.imaxLocal;
int jmaxLocal = s->comm.jmaxLocal;
double eps = s->eps;
int itermax = s->itermax;
double dx2 = s->dx * s->dx;
double dy2 = s->dy * s->dy;
double idx2 = 1.0 / dx2;
double idy2 = 1.0 / dy2;
double factor = s->omega * 0.5 * (dx2 * dy2) / (dx2 + dy2);
double* p = s->p;
double* rhs = s->rhs;
double epssq = eps * eps;
int it = 0;
double res = 1.0;
commExchange(&s->comm, p);
while ((res >= epssq) && (it < itermax)) {
res = 0.0;
for (int j = 1; j < jmaxLocal + 1; j++) {
for (int i = 1; i < imaxLocal + 1; i++) {
double r = RHS(i, j) -
((P(i + 1, j) - 2.0 * P(i, j) + P(i - 1, j)) * idx2 +
(P(i, j + 1) - 2.0 * P(i, j) + P(i, j - 1)) * idy2);
P(i, j) -= (factor * r);
res += (r * r);
}
}
if (commIsBoundary(&s->comm, BOTTOM)) { // set bottom bc
for (int i = 1; i < imaxLocal + 1; i++) {
P(i, 0) = P(i, 1);
}
}
if (commIsBoundary(&s->comm, TOP)) { // set top bc
for (int i = 1; i < imaxLocal + 1; i++) {
P(i, jmaxLocal + 1) = P(i, jmaxLocal);
}
}
if (commIsBoundary(&s->comm, LEFT)) { // set left bc
for (int j = 1; j < jmaxLocal + 1; j++) {
P(0, j) = P(1, j);
}
}
if (commIsBoundary(&s->comm, RIGHT)) { // set right bc
for (int j = 1; j < jmaxLocal + 1; j++) {
P(imaxLocal + 1, j) = P(imaxLocal, j);
}
}
commReduction(&res, SUM);
res = res / (double)(imax * jmax);
#ifdef DEBUG
if (commIsMaster(&s->comm)) {
printf("%d Residuum: %e\n", it, res);
}
#endif
it++;
}
#ifdef VERBOSE
if (commIsMaster(&s->comm)) {
printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
}
#endif
if (res < eps) {
return 0;
} else {
return 1;
}
}
void setBoundaryConditions(Solver* s)
{
int imaxLocal = s->comm.imaxLocal;
@@ -448,6 +598,7 @@ void computeFG(Solver* s)
}
}
/* ----------------------------- boundary of F --------------------------- */
if (commIsBoundary(&s->comm, LEFT)) {
for (int j = 1; j < jmaxLocal + 1; j++) {

View File

@@ -21,7 +21,7 @@ typedef struct {
double *f, *g;
double *u, *v;
/* parameters */
double eps, omega;
double eps, omega, rho;
double re, tau, gamma;
double gx, gy;
/* time stepping */
@@ -37,6 +37,8 @@ typedef struct {
void initSolver(Solver*, Parameter*);
void computeRHS(Solver*);
int solve(Solver*);
int solveRB(Solver*);
int solveRBA(Solver*);
void normalizePressure(Solver*);
void computeTimestep(Solver*);
void setBoundaryConditions(Solver*);