Add solver variants

This commit is contained in:
Jan Eitzinger 2023-06-18 10:08:56 +02:00
parent 1d99310b2e
commit 9c8585c7bc
6 changed files with 217 additions and 157 deletions

View File

@ -428,6 +428,7 @@ int solve(Solver* solver)
double dy2 = solver->dy * solver->dy; double dy2 = solver->dy * solver->dy;
double idx2 = 1.0 / dx2; double idx2 = 1.0 / dx2;
double idy2 = 1.0 / dy2; double idy2 = 1.0 / dy2;
// identical to 1/((2/dx2)+(2/dy2))
double factor = solver->omega * 0.5 * (dx2 * dy2) / (dx2 + dy2); double factor = solver->omega * 0.5 * (dx2 * dy2) / (dx2 + dy2);
double* p = solver->p; double* p = solver->p;
double* rhs = solver->rhs; double* rhs = solver->rhs;

View File

@ -247,68 +247,6 @@ void solveRB(Solver* solver)
#endif #endif
} }
void solveRBA(Solver* solver)
{
int imax = solver->imax;
int jmax = solver->jmax;
double eps = solver->eps;
int itermax = solver->itermax;
double dx2 = solver->dx * solver->dx;
double dy2 = solver->dy * solver->dy;
double idx2 = 1.0 / dx2;
double idy2 = 1.0 / dy2;
double factor = 0.5 * (dx2 * dy2) / (dx2 + dy2);
double omega = solver->omega;
double* p = solver->p;
double* rhs = solver->rhs;
double epssq = eps * eps;
int it = 0;
double anorm = 1.0, anormf = 0.0;
int pass, jsw, isw;
while ((anorm >= eps * anormf) && (it < itermax)) {
anorm = 0.0;
jsw = 1;
for (pass = 0; pass < 2; pass++) {
isw = jsw;
for (int j = 1; j < jmax + 1; j++) {
for (int i = isw; i < imax + 1; i += 2) {
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);
anorm += fabs(r);
P(i, j) -= (omega * factor * r);
}
isw = 3 - isw;
}
jsw = 3 - jsw;
}
for (int i = 1; i < imax + 1; i++) {
P(i, 0) = P(i, 1);
P(i, jmax + 1) = P(i, jmax);
}
for (int j = 1; j < jmax + 1; j++) {
P(0, j) = P(1, j);
P(imax + 1, j) = P(imax, j);
}
#ifdef DEBUG
printf("%d Residuum: %e\n", it, res);
#endif
it++;
}
#ifdef VERBOSE
printf("Solver took %d iterations to reach %f\n", it, anorm);
#endif
}
static double maxElement(Solver* solver, double* m) static double maxElement(Solver* solver, double* m)
{ {
int size = (solver->imax + 2) * (solver->jmax + 2); int size = (solver->imax + 2) * (solver->jmax + 2);

View File

@ -16,6 +16,7 @@ jmax 200 # number of interior cells in y-direction
itermax 10000 # maximal number of pressure iteration in one time step itermax 10000 # maximal number of pressure iteration in one time step
eps 0.000001 # stopping tolerance for pressure iteration eps 0.000001 # stopping tolerance for pressure iteration
omg 1.9 # relaxation parameter for SOR iteration # omg 0.9999 # relaxation parameter for SOR iteration
omg 1.93 # relaxation parameter for SOR iteration
#=============================================================================== #===============================================================================

View File

@ -4,24 +4,25 @@
* Use of this source code is governed by a MIT-style * Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file. * license that can be found in the LICENSE file.
*/ */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <float.h> #include <float.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "parameter.h" #include "parameter.h"
#include "solver.h" #include "solver.h"
#include "timing.h"
int main(int argc, char** argv)
int main (int argc, char** argv)
{ {
double startTime, endTime;
Parameter params; Parameter params;
Solver solver; Solver solver;
initParameter(&params); initParameter(&params);
if ( argc != 2 ) { if (argc != 2) {
printf("Usage: %s <configFile>\n",argv[0]); printf("Usage: %s <configFile>\n", argv[0]);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
@ -29,7 +30,10 @@ int main (int argc, char** argv)
printParameter(&params); printParameter(&params);
initSolver(&solver, &params, 2); initSolver(&solver, &params, 2);
startTime = getTimeStamp();
solve(&solver); solve(&solver);
endTime = getTimeStamp();
printf("Solution took %.2fs\n", endTime - startTime);
writeResult(&solver); writeResult(&solver);
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -4,31 +4,31 @@
* Use of this source code is governed by a MIT style * Use of this source code is governed by a MIT style
* license that can be found in the LICENSE file. * license that can be found in the LICENSE file.
*/ */
#include "stdlib.h"
#include "stdio.h"
#include "math.h" #include "math.h"
#include "stdio.h"
#include "stdlib.h"
#include "solver.h"
#include "parameter.h"
#include "allocate.h" #include "allocate.h"
#include "parameter.h"
#include "solver.h"
#define PI 3.14159265358979323846 #define PI 3.14159265358979323846
#define P(i,j) p[(j)*(imax+2) + (i)] #define P(i, j) p[(j) * (imax + 2) + (i)]
#define RHS(i,j) rhs[(j)*(imax+2) + (i)] #define RHS(i, j) rhs[(j) * (imax + 2) + (i)]
void initSolver(Solver *solver, Parameter *params, int problem) void initSolver(Solver* solver, Parameter* params, int problem)
{ {
solver->imax = params->imax; solver->imax = params->imax;
solver->jmax = params->jmax; solver->jmax = params->jmax;
solver->dx = params->xlength/params->imax; solver->dx = params->xlength / params->imax;
solver->dy = params->ylength/params->jmax; solver->dy = params->ylength / params->jmax;
solver->eps = params->eps; solver->eps = params->eps;
solver->omega = params->omg; solver->omega = params->omg;
solver->itermax = params->itermax; solver->itermax = params->itermax;
int imax = solver->imax; int imax = solver->imax;
int jmax = solver->jmax; int jmax = solver->jmax;
size_t bytesize = (imax+2) * (jmax+2) * sizeof(double); size_t bytesize = (imax + 2) * (jmax + 2) * sizeof(double);
solver->p = allocate(64, bytesize); solver->p = allocate(64, bytesize);
solver->rhs = allocate(64, bytesize); solver->rhs = allocate(64, bytesize);
@ -37,88 +37,202 @@ void initSolver(Solver *solver, Parameter *params, int problem)
double* p = solver->p; double* p = solver->p;
double* rhs = solver->rhs; double* rhs = solver->rhs;
for( int j=0; j<jmax+2; j++ ) { for (int j = 0; j < jmax + 2; j++) {
for( int i=0; i<imax+2; i++ ) { for (int i = 0; i < imax + 2; i++) {
P(i,j) = sin(2.0*PI*i*dx*2.0)+sin(2.0*PI*j*dy*2.0); P(i, j) = sin(2.0 * PI * i * dx * 2.0) + sin(2.0 * PI * j * dy * 2.0);
} }
} }
if(problem == 2) { if (problem == 2) {
for( int j=0; j<jmax+2; j++ ) { for (int j = 0; j < jmax + 2; j++) {
for( int i=0; i<imax+2; i++ ) { for (int i = 0; i < imax + 2; i++) {
RHS(i,j) = sin(2.0*PI*i*dx); RHS(i, j) = sin(2.0 * PI * i * dx);
} }
} }
} else { } else {
for( int j=0; j<jmax+2; j++ ) { for (int j = 0; j < jmax + 2; j++) {
for( int i=0; i<imax+2; i++ ) { for (int i = 0; i < imax + 2; i++) {
RHS(i,j) = 0.0; RHS(i, j) = 0.0;
} }
} }
} }
} }
int solve(Solver *solver) void solve(Solver* solver)
{ {
double r;
int it = 0;
double res;
int imax = solver->imax; int imax = solver->imax;
int jmax = solver->jmax; int jmax = solver->jmax;
double eps= solver->eps; double eps = solver->eps;
double omega = solver->omega;
int itermax = solver->itermax; int itermax = solver->itermax;
double dx2 = solver->dx * solver->dx; double dx2 = solver->dx * solver->dx;
double dy2 = solver->dy * solver->dy; double dy2 = solver->dy * solver->dy;
double idx2 = 1.0/dx2; double idx2 = 1.0 / dx2;
double idy2 = 1.0/dy2; double idy2 = 1.0 / dy2;
double factor = omega * 0.5 * (dx2*dy2) / (dx2+dy2); double factor = solver->omega * 0.5 * (dx2 * dy2) / (dx2 + dy2);
double* p = solver->p; double* p = solver->p;
double* rhs = solver->rhs; double* rhs = solver->rhs;
double epssq = eps * eps;
int it = 0;
double res = 1.0;
res = eps + 1.0; while ((res >= epssq) && (it < itermax)) {
while((res >= eps) && (it < itermax)) {
res = 0.0; res = 0.0;
for( int j=1; j<jmax+1; j++ ) { for (int j = 1; j < jmax + 1; j++) {
for( int i=1; i<imax+1; i++ ) { for (int i = 1; i < imax + 1; i++) {
r = RHS(i,j) - ((P(i-1,j) - 2.0 * P(i,j) + P(i+1,j)) * idx2 + double r = RHS(i, j) -
(P(i,j-1) - 2.0 *P(i,j) + P(i,j+1)) * idy2); ((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); P(i, j) -= (factor * r);
res += (r * r); res += (r * r);
} }
} }
for( int i=1; i<imax+1; i++ ) { for (int i = 1; i < imax + 1; i++) {
P(i,0) = P(i,1); P(i, 0) = P(i, 1);
P(i,jmax+1) = P(i,jmax); P(i, jmax + 1) = P(i, jmax);
} }
for( int j=1; j<jmax+1; j++ ) { for (int j = 1; j < jmax + 1; j++) {
P(0,j) = P(1,j); P(0, j) = P(1, j);
P(imax+1, j) = P(imax, j); P(imax + 1, j) = P(imax, j);
} }
it++; res = res / (double)(imax * jmax);
//Residuum
res = sqrt(res / (imax*jmax));
#ifdef DEBUG #ifdef DEBUG
printf("%d Residuum: %e\n",it, res); printf("%d Residuum: %e\n", it, res);
#endif #endif
it++;
} }
printf("Solver took %d iterations\n",it); printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
if( res < eps ){ }
return 1;
} else{ void solveRB(Solver* solver)
return 0; {
int imax = solver->imax;
int jmax = solver->jmax;
double eps = solver->eps;
int itermax = solver->itermax;
double dx2 = solver->dx * solver->dx;
double dy2 = solver->dy * solver->dy;
double idx2 = 1.0 / dx2;
double idy2 = 1.0 / dy2;
double factor = solver->omega * 0.5 * (dx2 * dy2) / (dx2 + dy2);
double* p = solver->p;
double* rhs = solver->rhs;
double epssq = eps * eps;
int it = 0;
double res = 1.0;
int pass, jsw, isw;
while ((res >= epssq) && (it < itermax)) {
res = 0.0;
jsw = 1;
for (pass = 0; pass < 2; pass++) {
isw = jsw;
for (int j = 1; j < jmax + 1; j++) {
for (int i = isw; i < imax + 1; i += 2) {
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);
} }
isw = 3 - isw;
}
jsw = 3 - jsw;
}
for (int i = 1; i < imax + 1; i++) {
P(i, 0) = P(i, 1);
P(i, jmax + 1) = P(i, jmax);
}
for (int j = 1; j < jmax + 1; j++) {
P(0, j) = P(1, j);
P(imax + 1, j) = P(imax, j);
}
res = res / (double)(imax * jmax);
#ifdef DEBUG
printf("%d Residuum: %e\n", it, res);
#endif
it++;
}
printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
}
void solveRBA(Solver* solver)
{
int imax = solver->imax;
int jmax = solver->jmax;
double eps = solver->eps;
int itermax = solver->itermax;
double dx2 = solver->dx * solver->dx;
double dy2 = solver->dy * solver->dy;
double idx2 = 1.0 / dx2;
double idy2 = 1.0 / dy2;
double factor = 0.5 * (dx2 * dy2) / (dx2 + dy2);
double rho = solver->omega;
double* p = solver->p;
double* rhs = solver->rhs;
double epssq = eps * eps;
int it = 0;
double res = 1.0;
int pass, jsw, isw;
double omega = 1.0;
while ((res >= epssq) && (it < itermax)) {
res = 0.0;
jsw = 1;
for (pass = 0; pass < 2; pass++) {
isw = jsw;
for (int j = 1; j < jmax + 1; j++) {
for (int i = isw; i < imax + 1; i += 2) {
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) -= (omega * factor * r);
res += (r * r);
}
isw = 3 - isw;
}
jsw = 3 - jsw;
omega = (it == 0 && pass == 0 ? 1.0 / (1.0 - 0.5 * rho * rho)
: 1.0 / (1.0 - 0.25 * rho * rho * omega));
}
for (int i = 1; i < imax + 1; i++) {
P(i, 0) = P(i, 1);
P(i, jmax + 1) = P(i, jmax);
}
for (int j = 1; j < jmax + 1; j++) {
P(0, j) = P(1, j);
P(imax + 1, j) = P(imax, j);
}
res = res / (double)(imax * jmax);
#ifdef DEBUG
printf("%d Residuum: %e Omega: %e\n", it, res, omega);
#endif
it++;
}
printf("Final omega: %f\n", omega);
printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
} }
void writeResult(Solver* solver) void writeResult(Solver* solver)
@ -127,17 +241,17 @@ void writeResult(Solver* solver)
int jmax = solver->jmax; int jmax = solver->jmax;
double* p = solver->p; double* p = solver->p;
FILE *fp; FILE* fp;
fp= fopen("p.dat", "w"); fp = fopen("p.dat", "w");
if (fp== NULL) { if (fp == NULL) {
printf("Error!\n"); printf("Error!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
for( int j=0; j<jmax+2; j++ ) { for (int j = 0; j < jmax + 2; j++) {
for( int i=0; i<imax+2; i++ ) { for (int i = 0; i < imax + 2; i++) {
fprintf(fp, "%f ", P(i,j)); fprintf(fp, "%f ", P(i, j));
} }
fprintf(fp, "\n"); fprintf(fp, "\n");
} }

View File

@ -16,7 +16,9 @@ typedef struct {
int itermax; int itermax;
} Solver; } Solver;
void initSolver(Solver*, Parameter*, int problem); extern void initSolver(Solver*, Parameter*, int problem);
void writeResult(Solver*); extern void writeResult(Solver*);
int solve(Solver*); extern void solve(Solver*);
extern void solveRB(Solver*);
extern void solveRBA(Solver*);
#endif #endif