265 lines
7.3 KiB
C
Raw Normal View History

2023-02-05 07:34:23 +01:00
/*
* Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
* All rights reserved. This file is part of nusif-solver.
* Use of this source code is governed by a MIT style
* license that can be found in the LICENSE file.
*/
#include "math.h"
2023-06-18 10:08:56 +02:00
#include "stdio.h"
#include "stdlib.h"
2023-02-05 07:34:23 +01:00
#include "allocate.h"
2023-06-18 10:08:56 +02:00
#include "parameter.h"
#include "solver.h"
2023-02-05 07:34:23 +01:00
2023-06-18 10:08:56 +02:00
#define PI 3.14159265358979323846
#define P(i, j) p[(j) * (imax + 2) + (i)]
#define RHS(i, j) rhs[(j) * (imax + 2) + (i)]
2023-02-05 07:34:23 +01:00
2023-06-18 10:08:56 +02:00
void initSolver(Solver* solver, Parameter* params, int problem)
2023-02-05 07:34:23 +01:00
{
2023-06-18 10:08:56 +02:00
solver->imax = params->imax;
solver->jmax = params->jmax;
solver->dx = params->xlength / params->imax;
solver->dy = params->ylength / params->jmax;
solver->eps = params->eps;
solver->omega = params->omg;
2023-06-18 13:55:23 +02:00
solver->rho = params->rho;
2023-02-05 07:34:23 +01:00
solver->itermax = params->itermax;
2023-06-18 10:08:56 +02:00
int imax = solver->imax;
int jmax = solver->jmax;
size_t bytesize = (imax + 2) * (jmax + 2) * sizeof(double);
solver->p = allocate(64, bytesize);
solver->rhs = allocate(64, bytesize);
2023-02-05 07:34:23 +01:00
2023-06-18 10:08:56 +02:00
double dx = solver->dx;
double dy = solver->dy;
double* p = solver->p;
2023-02-05 07:34:23 +01:00
double* rhs = solver->rhs;
2023-06-18 10:08:56 +02:00
for (int j = 0; j < jmax + 2; j++) {
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);
2023-02-05 07:34:23 +01:00
}
}
2023-06-18 10:08:56 +02:00
if (problem == 2) {
for (int j = 0; j < jmax + 2; j++) {
for (int i = 0; i < imax + 2; i++) {
RHS(i, j) = sin(2.0 * PI * i * dx);
2023-02-05 07:34:23 +01:00
}
}
} else {
2023-06-18 10:08:56 +02:00
for (int j = 0; j < jmax + 2; j++) {
for (int i = 0; i < imax + 2; i++) {
RHS(i, j) = 0.0;
2023-02-05 07:34:23 +01:00
}
}
}
}
2023-06-18 10:08:56 +02:00
void solve(Solver* solver)
2023-02-05 07:34:23 +01:00
{
2023-06-18 10:08:56 +02:00
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;
2023-02-05 07:34:23 +01:00
2023-06-18 10:08:56 +02:00
while ((res >= epssq) && (it < itermax)) {
2023-02-05 07:34:23 +01:00
res = 0.0;
2023-06-18 10:08:56 +02:00
for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 1; i++) {
2023-02-05 07:34:23 +01:00
2023-06-18 10:08:56 +02:00
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);
2023-02-05 07:34:23 +01:00
2023-06-18 10:08:56 +02:00
P(i, j) -= (factor * r);
2023-02-05 07:34:23 +01:00
res += (r * r);
}
}
2023-06-18 10:08:56 +02:00
for (int i = 1; i < imax + 1; i++) {
P(i, 0) = P(i, 1);
P(i, jmax + 1) = P(i, jmax);
2023-02-05 07:34:23 +01:00
}
2023-06-18 10:08:56 +02:00
for (int j = 1; j < jmax + 1; j++) {
P(0, j) = P(1, j);
P(imax + 1, j) = P(imax, j);
2023-02-05 07:34:23 +01:00
}
2023-06-18 10:08:56 +02:00
res = res / (double)(imax * jmax);
#ifdef DEBUG
printf("%d Residuum: %e\n", it, res);
#endif
2023-02-05 07:34:23 +01:00
it++;
2023-06-18 10:08:56 +02:00
}
2023-11-21 05:27:11 +01:00
// printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
printf("%d, %f\n", it, solver->omega);
2023-06-18 10:08:56 +02:00
}
void solveRB(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 = 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);
}
2023-02-05 07:34:23 +01:00
2023-06-18 10:08:56 +02:00
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);
2023-02-05 07:34:23 +01:00
#ifdef DEBUG
2023-06-18 10:08:56 +02:00
printf("%d Residuum: %e\n", it, res);
2023-02-05 07:34:23 +01:00
#endif
2023-06-18 10:08:56 +02:00
it++;
2023-02-05 07:34:23 +01:00
}
2023-11-21 05:27:11 +01:00
// printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
printf("%d, %f\n", it, solver->omega);
2023-06-18 10:08:56 +02:00
}
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);
2023-06-18 13:55:23 +02:00
double rho = solver->rho;
2023-06-18 10:08:56 +02:00
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++;
2023-02-05 07:34:23 +01:00
}
2023-06-18 10:08:56 +02:00
2023-11-21 05:27:11 +01:00
// printf("Final omega: %f\n", omega);
// printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
printf("%d, %f\n", it, omega);
2023-02-05 07:34:23 +01:00
}
void writeResult(Solver* solver)
{
2023-06-18 10:08:56 +02:00
int imax = solver->imax;
int jmax = solver->jmax;
2023-02-05 07:34:23 +01:00
double* p = solver->p;
2023-06-18 10:08:56 +02:00
FILE* fp;
fp = fopen("p.dat", "w");
2023-02-05 07:34:23 +01:00
2023-06-18 10:08:56 +02:00
if (fp == NULL) {
2023-02-05 07:34:23 +01:00
printf("Error!\n");
exit(EXIT_FAILURE);
}
2023-06-18 10:08:56 +02:00
for (int j = 0; j < jmax + 2; j++) {
for (int i = 0; i < imax + 2; i++) {
fprintf(fp, "%f ", P(i, j));
2023-02-05 07:34:23 +01:00
}
fprintf(fp, "\n");
}
fclose(fp);
}